1/*
2 * Copyright (C) 2017-2018 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "MessageReceiver.h"
29#include "MessageSender.h"
30#include <WebCore/SocketStreamHandle.h>
31#include <pal/SessionID.h>
32#include <wtf/Identified.h>
33
34namespace IPC {
35class Connection;
36class Decoder;
37class DataReference;
38}
39
40namespace WebCore {
41class SocketStreamError;
42}
43
44namespace WebKit {
45
46class WebSocketStream : public IPC::MessageSender, public IPC::MessageReceiver, public WebCore::SocketStreamHandle, public Identified<WebSocketStream> {
47public:
48 static Ref<WebSocketStream> create(const URL&, WebCore::SocketStreamHandleClient&, PAL::SessionID, const String& credentialPartition);
49 static void networkProcessCrashed();
50 static WebSocketStream* streamWithIdentifier(uint64_t);
51
52 void didReceiveMessage(IPC::Connection&, IPC::Decoder&);
53
54 // SocketStreamHandle
55 void platformSend(const uint8_t*, size_t, Function<void(bool)>&&) final;
56 void platformSendHandshake(const uint8_t*, size_t, const Optional<WebCore::CookieRequestHeaderFieldProxy>&, Function<void(bool, bool)>&&);
57 void platformClose() final;
58 size_t bufferedAmount() final;
59
60 // Message receivers
61 void didOpenSocketStream();
62 void didCloseSocketStream();
63 void didReceiveSocketStreamData(const IPC::DataReference&);
64 void didFailToReceiveSocketStreamData();
65 void didUpdateBufferedAmount(uint64_t);
66 void didFailSocketStream(WebCore::SocketStreamError&&);
67
68 void didSendData(uint64_t, bool success);
69 void didSendHandshake(uint64_t, bool success, bool didAccessSecureCookies);
70
71private:
72 // MessageSender
73 IPC::Connection* messageSenderConnection() const final;
74 uint64_t messageSenderDestinationID() const final;
75
76 WebSocketStream(const URL&, WebCore::SocketStreamHandleClient&, PAL::SessionID, const String& credentialPartition);
77 ~WebSocketStream();
78
79 size_t m_bufferedAmount { 0 };
80 WebCore::SocketStreamHandleClient& m_client;
81 HashMap<uint64_t, Function<void(bool)>> m_sendDataCallbacks;
82 HashMap<uint64_t, Function<void(bool, bool)>> m_sendHandshakeCallbacks;
83};
84
85} // namespace WebKit
86