be90d12f1008262d0ccc27789d8da56fb31c646e
[WebKit-https.git] / Source / WebCore / Modules / websockets / WorkerThreadableWebSocketChannel.h
1 /*
2  * Copyright (C) 2011 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #pragma once
32
33 #if ENABLE(WEB_SOCKETS)
34
35 #include "ThreadableWebSocketChannel.h"
36 #include "WebSocketChannelClient.h"
37 #include "WorkerGlobalScope.h"
38
39 #include <wtf/RefCounted.h>
40 #include <wtf/RefPtr.h>
41 #include <wtf/Threading.h>
42 #include <wtf/text/WTFString.h>
43
44 namespace WebCore {
45
46 class URL;
47 class ScriptExecutionContext;
48 class ThreadableWebSocketChannelClientWrapper;
49 class WorkerGlobalScope;
50 class WorkerLoaderProxy;
51 class WorkerRunLoop;
52
53 class WorkerThreadableWebSocketChannel : public RefCounted<WorkerThreadableWebSocketChannel>, public ThreadableWebSocketChannel {
54     WTF_MAKE_FAST_ALLOCATED;
55 public:
56     static Ref<ThreadableWebSocketChannel> create(WorkerGlobalScope& workerGlobalScope, WebSocketChannelClient& client, const String& taskMode, SocketProvider& provider)
57     {
58         return adoptRef(*new WorkerThreadableWebSocketChannel(workerGlobalScope, client, taskMode, provider));
59     }
60     virtual ~WorkerThreadableWebSocketChannel();
61
62     // ThreadableWebSocketChannel functions.
63     void connect(const URL&, const String& protocol) override;
64     String subprotocol() override;
65     String extensions() override;
66     ThreadableWebSocketChannel::SendResult send(const String& message) override;
67     ThreadableWebSocketChannel::SendResult send(const JSC::ArrayBuffer&, unsigned byteOffset, unsigned byteLength) override;
68     ThreadableWebSocketChannel::SendResult send(Blob&) override;
69     unsigned bufferedAmount() const override;
70     void close(int code, const String& reason) override;
71     void fail(const String& reason) override;
72     void disconnect() override; // Will suppress didClose().
73     void suspend() override;
74     void resume() override;
75
76     // Generated by the bridge.  The Peer and its bridge should have identical
77     // lifetimes.
78     class Peer : public WebSocketChannelClient {
79         WTF_MAKE_NONCOPYABLE(Peer); WTF_MAKE_FAST_ALLOCATED;
80     public:
81         Peer(Ref<ThreadableWebSocketChannelClientWrapper>&&, WorkerLoaderProxy&, ScriptExecutionContext&, const String& taskMode, SocketProvider&);
82         ~Peer();
83
84         void connect(const URL&, const String& protocol);
85         void send(const String& message);
86         void send(const JSC::ArrayBuffer&);
87         void send(Blob&);
88         void bufferedAmount();
89         void close(int code, const String& reason);
90         void fail(const String& reason);
91         void disconnect();
92         void suspend();
93         void resume();
94
95         // WebSocketChannelClient functions.
96         void didConnect() final;
97         void didReceiveMessage(const String& message) final;
98         void didReceiveBinaryData(Vector<uint8_t>&&) final;
99         void didUpdateBufferedAmount(unsigned bufferedAmount) final;
100         void didStartClosingHandshake() final;
101         void didClose(unsigned unhandledBufferedAmount, ClosingHandshakeCompletionStatus, unsigned short code, const String& reason) final;
102         void didReceiveMessageError() final;
103         void didUpgradeURL() final;
104
105     private:
106         Ref<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper;
107         WorkerLoaderProxy& m_loaderProxy;
108         RefPtr<ThreadableWebSocketChannel> m_mainWebSocketChannel;
109         String m_taskMode;
110     };
111
112     using RefCounted<WorkerThreadableWebSocketChannel>::ref;
113     using RefCounted<WorkerThreadableWebSocketChannel>::deref;
114
115 protected:
116     void refThreadableWebSocketChannel() override { ref(); }
117     void derefThreadableWebSocketChannel() override { deref(); }
118
119 private:
120     // Bridge for Peer.  Running on the worker thread.
121     class Bridge : public RefCounted<Bridge> {
122     public:
123         static Ref<Bridge> create(Ref<ThreadableWebSocketChannelClientWrapper>&& workerClientWrapper, Ref<WorkerGlobalScope>&& workerGlobalScope, const String& taskMode, Ref<SocketProvider>&& provider)
124         {
125             return adoptRef(*new Bridge(WTFMove(workerClientWrapper), WTFMove(workerGlobalScope), taskMode, WTFMove(provider)));
126         }
127         ~Bridge();
128         void initialize();
129         void connect(const URL&, const String& protocol);
130         ThreadableWebSocketChannel::SendResult send(const String& message);
131         ThreadableWebSocketChannel::SendResult send(const JSC::ArrayBuffer&, unsigned byteOffset, unsigned byteLength);
132         ThreadableWebSocketChannel::SendResult send(Blob&);
133         unsigned bufferedAmount();
134         void close(int code, const String& reason);
135         void fail(const String& reason);
136         void disconnect();
137         void suspend();
138         void resume();
139
140         using RefCounted<Bridge>::ref;
141         using RefCounted<Bridge>::deref;
142
143     private:
144         Bridge(Ref<ThreadableWebSocketChannelClientWrapper>&&, Ref<WorkerGlobalScope>&&, const String& taskMode, Ref<SocketProvider>&&);
145
146         static void setWebSocketChannel(ScriptExecutionContext*, Bridge* thisPtr, Peer*, Ref<ThreadableWebSocketChannelClientWrapper>&&);
147
148         // Executed on the main thread to create a Peer for this bridge.
149         static void mainThreadInitialize(ScriptExecutionContext&, WorkerLoaderProxy&, Ref<ThreadableWebSocketChannelClientWrapper>&&, const String& taskMode, Ref<SocketProvider>&&);
150
151         // Executed on the worker context's thread.
152         void clearClientWrapper();
153
154         void setMethodNotCompleted();
155         void waitForMethodCompletion();
156
157         Ref<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper;
158         RefPtr<WorkerGlobalScope> m_workerGlobalScope;
159         WorkerLoaderProxy& m_loaderProxy;
160         String m_taskMode;
161         Peer* m_peer { nullptr };
162         Ref<SocketProvider> m_socketProvider;
163     };
164
165     WEBCORE_EXPORT WorkerThreadableWebSocketChannel(WorkerGlobalScope&, WebSocketChannelClient&, const String& taskMode, SocketProvider&);
166
167     class WorkerGlobalScopeDidInitializeTask;
168
169     Ref<WorkerGlobalScope> m_workerGlobalScope;
170     Ref<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper;
171     RefPtr<Bridge> m_bridge;
172     Ref<SocketProvider> m_socketProvider;
173 };
174
175 } // namespace WebCore
176
177 #endif // ENABLE(WEB_SOCKETS)