5d84495430defaefe3e356f948010722bb268f62
[WebKit-https.git] / Source / WebCore / websockets / WorkerThreadableWebSocketChannel.cpp
1 /*
2  * Copyright (C) 2009, 2010 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 #include "config.h"
32
33 #if ENABLE(WEB_SOCKETS) && ENABLE(WORKERS)
34
35 #include "WorkerThreadableWebSocketChannel.h"
36
37 #include "CrossThreadTask.h"
38 #include "PlatformString.h"
39 #include "ScriptExecutionContext.h"
40 #include "ThreadableWebSocketChannelClientWrapper.h"
41 #include "WebSocketChannel.h"
42 #include "WebSocketChannelClient.h"
43 #include "WorkerContext.h"
44 #include "WorkerLoaderProxy.h"
45 #include "WorkerRunLoop.h"
46 #include "WorkerThread.h"
47
48 #include <wtf/PassRefPtr.h>
49
50 namespace WebCore {
51
52 WorkerThreadableWebSocketChannel::WorkerThreadableWebSocketChannel(WorkerContext* context, WebSocketChannelClient* client, const String& taskMode, const KURL& url, const String& protocol)
53     : m_workerContext(context)
54     , m_workerClientWrapper(ThreadableWebSocketChannelClientWrapper::create(client))
55     , m_bridge(Bridge::create(m_workerClientWrapper, m_workerContext, taskMode, url, protocol))
56 {
57 }
58
59 WorkerThreadableWebSocketChannel::~WorkerThreadableWebSocketChannel()
60 {
61     if (m_bridge)
62         m_bridge->disconnect();
63 }
64
65 void WorkerThreadableWebSocketChannel::connect()
66 {
67     if (m_bridge)
68         m_bridge->connect();
69 }
70
71 bool WorkerThreadableWebSocketChannel::send(const String& message)
72 {
73     if (!m_bridge)
74         return false;
75     return m_bridge->send(message);
76 }
77
78 unsigned long WorkerThreadableWebSocketChannel::bufferedAmount() const
79 {
80     if (!m_bridge)
81         return 0;
82     return m_bridge->bufferedAmount();
83 }
84
85 void WorkerThreadableWebSocketChannel::close()
86 {
87     if (m_bridge)
88         m_bridge->close();
89 }
90
91 void WorkerThreadableWebSocketChannel::disconnect()
92 {
93     m_bridge->disconnect();
94     m_bridge.clear();
95 }
96
97 void WorkerThreadableWebSocketChannel::suspend()
98 {
99     m_workerClientWrapper->suspend();
100     if (m_bridge)
101         m_bridge->suspend();
102 }
103
104 void WorkerThreadableWebSocketChannel::resume()
105 {
106     m_workerClientWrapper->resume();
107     if (m_bridge)
108         m_bridge->resume();
109 }
110
111 WorkerThreadableWebSocketChannel::Peer::Peer(RefPtr<ThreadableWebSocketChannelClientWrapper> clientWrapper, WorkerLoaderProxy& loaderProxy, ScriptExecutionContext* context, const String& taskMode, const KURL& url, const String& protocol)
112     : m_workerClientWrapper(clientWrapper)
113     , m_loaderProxy(loaderProxy)
114     , m_mainWebSocketChannel(WebSocketChannel::create(context, this, url, protocol))
115     , m_taskMode(taskMode)
116 {
117     ASSERT(isMainThread());
118 }
119
120 WorkerThreadableWebSocketChannel::Peer::~Peer()
121 {
122     ASSERT(isMainThread());
123     if (m_mainWebSocketChannel)
124         m_mainWebSocketChannel->disconnect();
125 }
126
127 void WorkerThreadableWebSocketChannel::Peer::connect()
128 {
129     ASSERT(isMainThread());
130     if (!m_mainWebSocketChannel)
131         return;
132     m_mainWebSocketChannel->connect();
133 }
134
135 static void workerContextDidSend(ScriptExecutionContext* context, RefPtr<ThreadableWebSocketChannelClientWrapper> workerClientWrapper, bool sent)
136 {
137     ASSERT_UNUSED(context, context->isWorkerContext());
138     workerClientWrapper->setSent(sent);
139 }
140
141 void WorkerThreadableWebSocketChannel::Peer::send(const String& message)
142 {
143     ASSERT(isMainThread());
144     if (!m_mainWebSocketChannel || !m_workerClientWrapper)
145         return;
146     bool sent = m_mainWebSocketChannel->send(message);
147     m_loaderProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidSend, m_workerClientWrapper, sent), m_taskMode);
148 }
149
150 static void workerContextDidGetBufferedAmount(ScriptExecutionContext* context, RefPtr<ThreadableWebSocketChannelClientWrapper> workerClientWrapper, unsigned long bufferedAmount)
151 {
152     ASSERT_UNUSED(context, context->isWorkerContext());
153     workerClientWrapper->setBufferedAmount(bufferedAmount);
154 }
155
156 void WorkerThreadableWebSocketChannel::Peer::bufferedAmount()
157 {
158     ASSERT(isMainThread());
159     if (!m_mainWebSocketChannel || !m_workerClientWrapper)
160         return;
161     unsigned long bufferedAmount = m_mainWebSocketChannel->bufferedAmount();
162     m_loaderProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidGetBufferedAmount, m_workerClientWrapper, bufferedAmount), m_taskMode);
163 }
164
165 void WorkerThreadableWebSocketChannel::Peer::close()
166 {
167     ASSERT(isMainThread());
168     if (!m_mainWebSocketChannel)
169         return;
170     m_mainWebSocketChannel->close();
171     m_mainWebSocketChannel = 0;
172 }
173
174 void WorkerThreadableWebSocketChannel::Peer::disconnect()
175 {
176     ASSERT(isMainThread());
177     if (!m_mainWebSocketChannel)
178         return;
179     m_mainWebSocketChannel->disconnect();
180     m_mainWebSocketChannel = 0;
181 }
182
183 void WorkerThreadableWebSocketChannel::Peer::suspend()
184 {
185     ASSERT(isMainThread());
186     if (!m_mainWebSocketChannel)
187         return;
188     m_mainWebSocketChannel->suspend();
189 }
190
191 void WorkerThreadableWebSocketChannel::Peer::resume()
192 {
193     ASSERT(isMainThread());
194     if (!m_mainWebSocketChannel)
195         return;
196     m_mainWebSocketChannel->resume();
197 }
198
199 static void workerContextDidConnect(ScriptExecutionContext* context, RefPtr<ThreadableWebSocketChannelClientWrapper> workerClientWrapper)
200 {
201     ASSERT_UNUSED(context, context->isWorkerContext());
202     workerClientWrapper->didConnect();
203 }
204
205 void WorkerThreadableWebSocketChannel::Peer::didConnect()
206 {
207     ASSERT(isMainThread());
208     m_loaderProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidConnect, m_workerClientWrapper), m_taskMode);
209 }
210
211 static void workerContextDidReceiveMessage(ScriptExecutionContext* context, RefPtr<ThreadableWebSocketChannelClientWrapper> workerClientWrapper, const String& message)
212 {
213     ASSERT_UNUSED(context, context->isWorkerContext());
214     workerClientWrapper->didReceiveMessage(message);
215 }
216
217 void WorkerThreadableWebSocketChannel::Peer::didReceiveMessage(const String& message)
218 {
219     ASSERT(isMainThread());
220     m_loaderProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidReceiveMessage, m_workerClientWrapper, message), m_taskMode);
221 }
222
223 static void workerContextDidClose(ScriptExecutionContext* context, RefPtr<ThreadableWebSocketChannelClientWrapper> workerClientWrapper, unsigned long unhandledBufferedAmount)
224 {
225     ASSERT_UNUSED(context, context->isWorkerContext());
226     workerClientWrapper->didClose(unhandledBufferedAmount);
227 }
228
229 void WorkerThreadableWebSocketChannel::Peer::didClose(unsigned long unhandledBufferedAmount)
230 {
231     ASSERT(isMainThread());
232     m_mainWebSocketChannel = 0;
233     m_loaderProxy.postTaskForModeToWorkerContext(createCallbackTask(&workerContextDidClose, m_workerClientWrapper, unhandledBufferedAmount), m_taskMode);
234 }
235
236 void WorkerThreadableWebSocketChannel::Bridge::setWebSocketChannel(ScriptExecutionContext* context, Bridge* thisPtr, Peer* peer, RefPtr<ThreadableWebSocketChannelClientWrapper> workerClientWrapper)
237 {
238     ASSERT_UNUSED(context, context->isWorkerContext());
239     thisPtr->m_peer = peer;
240     workerClientWrapper->setSyncMethodDone();
241 }
242
243 void WorkerThreadableWebSocketChannel::Bridge::mainThreadCreateWebSocketChannel(ScriptExecutionContext* context, Bridge* thisPtr, RefPtr<ThreadableWebSocketChannelClientWrapper> clientWrapper, const String& taskMode, const KURL& url, const String& protocol)
244 {
245     ASSERT(isMainThread());
246     ASSERT_UNUSED(context, context->isDocument());
247
248     Peer* peer = Peer::create(clientWrapper, thisPtr->m_loaderProxy, context, taskMode, url, protocol);
249     thisPtr->m_loaderProxy.postTaskForModeToWorkerContext(
250         createCallbackTask(&Bridge::setWebSocketChannel,
251                            AllowCrossThreadAccess(thisPtr),
252                            AllowCrossThreadAccess(peer), clientWrapper), taskMode);
253 }
254
255 WorkerThreadableWebSocketChannel::Bridge::Bridge(PassRefPtr<ThreadableWebSocketChannelClientWrapper> workerClientWrapper, PassRefPtr<WorkerContext> workerContext, const String& taskMode, const KURL& url, const String& protocol)
256     : m_workerClientWrapper(workerClientWrapper)
257     , m_workerContext(workerContext)
258     , m_loaderProxy(m_workerContext->thread()->workerLoaderProxy())
259     , m_taskMode(taskMode)
260     , m_peer(0)
261 {
262     ASSERT(m_workerClientWrapper.get());
263     setMethodNotCompleted();
264     m_loaderProxy.postTaskToLoader(
265         createCallbackTask(&Bridge::mainThreadCreateWebSocketChannel,
266                            AllowCrossThreadAccess(this), m_workerClientWrapper, m_taskMode, url, protocol));
267     waitForMethodCompletion();
268     ASSERT(m_peer);
269 }
270
271 WorkerThreadableWebSocketChannel::Bridge::~Bridge()
272 {
273     disconnect();
274 }
275
276 void WorkerThreadableWebSocketChannel::mainThreadConnect(ScriptExecutionContext* context, Peer* peer)
277 {
278     ASSERT(isMainThread());
279     ASSERT_UNUSED(context, context->isDocument());
280     ASSERT(peer);
281
282     peer->connect();
283 }
284
285 void WorkerThreadableWebSocketChannel::Bridge::connect()
286 {
287     ASSERT(m_workerClientWrapper);
288     ASSERT(m_peer);
289     m_loaderProxy.postTaskToLoader(
290         createCallbackTask(&WorkerThreadableWebSocketChannel::mainThreadConnect, AllowCrossThreadAccess(m_peer)));
291 }
292
293 void WorkerThreadableWebSocketChannel::mainThreadSend(ScriptExecutionContext* context, Peer* peer, const String& message)
294 {
295     ASSERT(isMainThread());
296     ASSERT_UNUSED(context, context->isDocument());
297     ASSERT(peer);
298
299     peer->send(message);
300 }
301
302 bool WorkerThreadableWebSocketChannel::Bridge::send(const String& message)
303 {
304     if (!m_workerClientWrapper)
305         return false;
306     ASSERT(m_peer);
307     setMethodNotCompleted();
308     m_loaderProxy.postTaskToLoader(
309         createCallbackTask(&WorkerThreadableWebSocketChannel::mainThreadSend,
310                            AllowCrossThreadAccess(m_peer), message));
311     RefPtr<Bridge> protect(this);
312     waitForMethodCompletion();
313     ThreadableWebSocketChannelClientWrapper* clientWrapper = m_workerClientWrapper.get();
314     return clientWrapper && clientWrapper->sent();
315 }
316
317 void WorkerThreadableWebSocketChannel::mainThreadBufferedAmount(ScriptExecutionContext* context, Peer* peer)
318 {
319     ASSERT(isMainThread());
320     ASSERT_UNUSED(context, context->isDocument());
321     ASSERT(peer);
322
323     peer->bufferedAmount();
324 }
325
326 unsigned long WorkerThreadableWebSocketChannel::Bridge::bufferedAmount()
327 {
328     if (!m_workerClientWrapper)
329         return 0;
330     ASSERT(m_peer);
331     setMethodNotCompleted();
332     m_loaderProxy.postTaskToLoader(
333         createCallbackTask(&WorkerThreadableWebSocketChannel::mainThreadBufferedAmount, AllowCrossThreadAccess(m_peer)));
334     RefPtr<Bridge> protect(this);
335     waitForMethodCompletion();
336     ThreadableWebSocketChannelClientWrapper* clientWrapper = m_workerClientWrapper.get();
337     if (clientWrapper)
338         return clientWrapper->bufferedAmount();
339     return 0;
340 }
341
342 void WorkerThreadableWebSocketChannel::mainThreadClose(ScriptExecutionContext* context, Peer* peer)
343 {
344     ASSERT(isMainThread());
345     ASSERT_UNUSED(context, context->isDocument());
346     ASSERT(peer);
347
348     peer->close();
349 }
350
351 void WorkerThreadableWebSocketChannel::Bridge::close()
352 {
353     ASSERT(m_peer);
354     m_loaderProxy.postTaskToLoader(
355         createCallbackTask(&WorkerThreadableWebSocketChannel::mainThreadClose, AllowCrossThreadAccess(m_peer)));
356 }
357
358 void WorkerThreadableWebSocketChannel::mainThreadDestroy(ScriptExecutionContext* context, Peer* peer)
359 {
360     ASSERT(isMainThread());
361     ASSERT_UNUSED(context, context->isDocument());
362     ASSERT(peer);
363
364     delete peer;
365 }
366
367 void WorkerThreadableWebSocketChannel::Bridge::disconnect()
368 {
369     clearClientWrapper();
370     if (m_peer) {
371         Peer* peer = m_peer;
372         m_peer = 0;
373         m_loaderProxy.postTaskToLoader(
374             createCallbackTask(&mainThreadDestroy, AllowCrossThreadAccess(peer)));
375     }
376     m_workerContext = 0;
377 }
378
379 void WorkerThreadableWebSocketChannel::mainThreadSuspend(ScriptExecutionContext* context, Peer* peer)
380 {
381     ASSERT(isMainThread());
382     ASSERT_UNUSED(context, context->isDocument());
383     ASSERT(peer);
384
385     peer->suspend();
386 }
387
388 void WorkerThreadableWebSocketChannel::Bridge::suspend()
389 {
390     ASSERT(m_peer);
391     m_loaderProxy.postTaskToLoader(
392         createCallbackTask(&WorkerThreadableWebSocketChannel::mainThreadSuspend, AllowCrossThreadAccess(m_peer)));
393 }
394
395 void WorkerThreadableWebSocketChannel::mainThreadResume(ScriptExecutionContext* context, Peer* peer)
396 {
397     ASSERT(isMainThread());
398     ASSERT_UNUSED(context, context->isDocument());
399     ASSERT(peer);
400
401     peer->resume();
402 }
403
404 void WorkerThreadableWebSocketChannel::Bridge::resume()
405 {
406     ASSERT(m_peer);
407     m_loaderProxy.postTaskToLoader(
408         createCallbackTask(&WorkerThreadableWebSocketChannel::mainThreadResume, AllowCrossThreadAccess(m_peer)));
409 }
410
411 void WorkerThreadableWebSocketChannel::Bridge::clearClientWrapper()
412 {
413     m_workerClientWrapper->clearClient();
414 }
415
416 void WorkerThreadableWebSocketChannel::Bridge::setMethodNotCompleted()
417 {
418     ASSERT(m_workerClientWrapper);
419     m_workerClientWrapper->clearSyncMethodDone();
420 }
421
422 // Caller of this function should hold a reference to the bridge, because this function may call WebSocket::didClose() in the end,
423 // which causes the bridge to get disconnected from the WebSocket and deleted if there is no other reference.
424 void WorkerThreadableWebSocketChannel::Bridge::waitForMethodCompletion()
425 {
426     if (!m_workerContext)
427         return;
428     WorkerRunLoop& runLoop = m_workerContext->thread()->runLoop();
429     MessageQueueWaitResult result = MessageQueueMessageReceived;
430     ThreadableWebSocketChannelClientWrapper* clientWrapper = m_workerClientWrapper.get();
431     while (m_workerContext && clientWrapper && !clientWrapper->syncMethodDone() && result != MessageQueueTerminated) {
432         result = runLoop.runInMode(m_workerContext.get(), m_taskMode); // May cause this bridge to get disconnected, which makes m_workerContext become null.
433         clientWrapper = m_workerClientWrapper.get();
434     }
435 }
436
437 } // namespace WebCore
438
439 #endif // ENABLE(WEB_SOCKETS)