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 #include "config.h"
32 #include "modules/websockets/ThreadableWebSocketChannelClientWrapper.h"
33
34 #include "core/dom/CrossThreadTask.h"
35 #include "platform/CrossThreadCopier.h"
36 #include "wtf/PassRefPtr.h"
37 #include "wtf/RefPtr.h"
38
39 namespace WebCore {
40
ThreadableWebSocketChannelClientWrapper(ExecutionContext * context,WebSocketChannelClient * client)41 ThreadableWebSocketChannelClientWrapper::ThreadableWebSocketChannelClientWrapper(ExecutionContext* context, WebSocketChannelClient* client)
42 : m_context(context)
43 , m_client(client)
44 , m_peer(0)
45 , m_failedWebSocketChannelCreation(false)
46 , m_syncMethodDone(true)
47 , m_sendRequestResult(WebSocketChannel::SendFail)
48 , m_bufferedAmount(0)
49 , m_suspended(false)
50 {
51 }
52
create(ExecutionContext * context,WebSocketChannelClient * client)53 PassRefPtr<ThreadableWebSocketChannelClientWrapper> ThreadableWebSocketChannelClientWrapper::create(ExecutionContext* context, WebSocketChannelClient* client)
54 {
55 return adoptRef(new ThreadableWebSocketChannelClientWrapper(context, client));
56 }
57
clearSyncMethodDone()58 void ThreadableWebSocketChannelClientWrapper::clearSyncMethodDone()
59 {
60 m_syncMethodDone = false;
61 }
62
setSyncMethodDone()63 void ThreadableWebSocketChannelClientWrapper::setSyncMethodDone()
64 {
65 m_syncMethodDone = true;
66 }
67
syncMethodDone() const68 bool ThreadableWebSocketChannelClientWrapper::syncMethodDone() const
69 {
70 return m_syncMethodDone;
71 }
72
peer() const73 WorkerThreadableWebSocketChannel::Peer* ThreadableWebSocketChannelClientWrapper::peer() const
74 {
75 return m_peer;
76 }
77
didCreateWebSocketChannel(WorkerThreadableWebSocketChannel::Peer * peer)78 void ThreadableWebSocketChannelClientWrapper::didCreateWebSocketChannel(WorkerThreadableWebSocketChannel::Peer* peer)
79 {
80 m_peer = peer;
81 m_syncMethodDone = true;
82 }
83
clearPeer()84 void ThreadableWebSocketChannelClientWrapper::clearPeer()
85 {
86 m_peer = 0;
87 }
88
failedWebSocketChannelCreation() const89 bool ThreadableWebSocketChannelClientWrapper::failedWebSocketChannelCreation() const
90 {
91 return m_failedWebSocketChannelCreation;
92 }
93
setFailedWebSocketChannelCreation()94 void ThreadableWebSocketChannelClientWrapper::setFailedWebSocketChannelCreation()
95 {
96 m_failedWebSocketChannelCreation = true;
97 }
98
subprotocol() const99 String ThreadableWebSocketChannelClientWrapper::subprotocol() const
100 {
101 if (m_subprotocol.isEmpty())
102 return emptyString();
103 return String(m_subprotocol);
104 }
105
setSubprotocol(const String & subprotocol)106 void ThreadableWebSocketChannelClientWrapper::setSubprotocol(const String& subprotocol)
107 {
108 m_subprotocol.clear();
109 append(m_subprotocol, subprotocol);
110 }
111
extensions() const112 String ThreadableWebSocketChannelClientWrapper::extensions() const
113 {
114 if (m_extensions.isEmpty())
115 return emptyString();
116 return String(m_extensions);
117 }
118
setExtensions(const String & extensions)119 void ThreadableWebSocketChannelClientWrapper::setExtensions(const String& extensions)
120 {
121 m_extensions.clear();
122 append(m_extensions, extensions);
123 }
124
sendRequestResult() const125 WebSocketChannel::SendResult ThreadableWebSocketChannelClientWrapper::sendRequestResult() const
126 {
127 return m_sendRequestResult;
128 }
129
setSendRequestResult(WebSocketChannel::SendResult sendRequestResult)130 void ThreadableWebSocketChannelClientWrapper::setSendRequestResult(WebSocketChannel::SendResult sendRequestResult)
131 {
132 m_sendRequestResult = sendRequestResult;
133 m_syncMethodDone = true;
134 }
135
bufferedAmount() const136 unsigned long ThreadableWebSocketChannelClientWrapper::bufferedAmount() const
137 {
138 return m_bufferedAmount;
139 }
140
setBufferedAmount(unsigned long bufferedAmount)141 void ThreadableWebSocketChannelClientWrapper::setBufferedAmount(unsigned long bufferedAmount)
142 {
143 m_bufferedAmount = bufferedAmount;
144 m_syncMethodDone = true;
145 }
146
clearClient()147 void ThreadableWebSocketChannelClientWrapper::clearClient()
148 {
149 m_client = 0;
150 }
151
didConnect()152 void ThreadableWebSocketChannelClientWrapper::didConnect()
153 {
154 m_pendingTasks.append(createCallbackTask(&didConnectCallback, this));
155 if (!m_suspended)
156 processPendingTasks();
157 }
158
didReceiveMessage(const String & message)159 void ThreadableWebSocketChannelClientWrapper::didReceiveMessage(const String& message)
160 {
161 m_pendingTasks.append(createCallbackTask(&didReceiveMessageCallback, this, message));
162 if (!m_suspended)
163 processPendingTasks();
164 }
165
didReceiveBinaryData(PassOwnPtr<Vector<char>> binaryData)166 void ThreadableWebSocketChannelClientWrapper::didReceiveBinaryData(PassOwnPtr<Vector<char> > binaryData)
167 {
168 m_pendingTasks.append(createCallbackTask(&didReceiveBinaryDataCallback, this, binaryData));
169 if (!m_suspended)
170 processPendingTasks();
171 }
172
didUpdateBufferedAmount(unsigned long bufferedAmount)173 void ThreadableWebSocketChannelClientWrapper::didUpdateBufferedAmount(unsigned long bufferedAmount)
174 {
175 m_pendingTasks.append(createCallbackTask(&didUpdateBufferedAmountCallback, this, bufferedAmount));
176 if (!m_suspended)
177 processPendingTasks();
178 }
179
didStartClosingHandshake()180 void ThreadableWebSocketChannelClientWrapper::didStartClosingHandshake()
181 {
182 m_pendingTasks.append(createCallbackTask(&didStartClosingHandshakeCallback, this));
183 if (!m_suspended)
184 processPendingTasks();
185 }
186
didClose(unsigned long unhandledBufferedAmount,WebSocketChannelClient::ClosingHandshakeCompletionStatus closingHandshakeCompletion,unsigned short code,const String & reason)187 void ThreadableWebSocketChannelClientWrapper::didClose(unsigned long unhandledBufferedAmount, WebSocketChannelClient::ClosingHandshakeCompletionStatus closingHandshakeCompletion, unsigned short code, const String& reason)
188 {
189 m_pendingTasks.append(createCallbackTask(&didCloseCallback, this, unhandledBufferedAmount, closingHandshakeCompletion, code, reason));
190 if (!m_suspended)
191 processPendingTasks();
192 }
193
didReceiveMessageError()194 void ThreadableWebSocketChannelClientWrapper::didReceiveMessageError()
195 {
196 m_pendingTasks.append(createCallbackTask(&didReceiveMessageErrorCallback, this));
197 if (!m_suspended)
198 processPendingTasks();
199 }
200
suspend()201 void ThreadableWebSocketChannelClientWrapper::suspend()
202 {
203 m_suspended = true;
204 }
205
resume()206 void ThreadableWebSocketChannelClientWrapper::resume()
207 {
208 m_suspended = false;
209 processPendingTasks();
210 }
211
processPendingTasksCallback(ExecutionContext * context,PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper)212 void ThreadableWebSocketChannelClientWrapper::processPendingTasksCallback(ExecutionContext* context, PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper)
213 {
214 ASSERT_UNUSED(context, context->isWorkerGlobalScope());
215 wrapper->processPendingTasks();
216 }
217
processPendingTasks()218 void ThreadableWebSocketChannelClientWrapper::processPendingTasks()
219 {
220 if (m_suspended)
221 return;
222 if (!m_syncMethodDone) {
223 // When a synchronous operation is in progress (i.e. the execution stack contains
224 // WorkerThreadableWebSocketChannel::waitForMethodCompletion()), we cannot invoke callbacks in this run loop.
225 m_context->postTask(createCallbackTask(&ThreadableWebSocketChannelClientWrapper::processPendingTasksCallback, this));
226 return;
227 }
228 Vector<OwnPtr<ExecutionContextTask> > tasks;
229 tasks.swap(m_pendingTasks);
230 for (Vector<OwnPtr<ExecutionContextTask> >::const_iterator iter = tasks.begin(); iter != tasks.end(); ++iter)
231 (*iter)->performTask(0);
232 }
233
didConnectCallback(ExecutionContext * context,PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper)234 void ThreadableWebSocketChannelClientWrapper::didConnectCallback(ExecutionContext* context, PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper)
235 {
236 ASSERT_UNUSED(context, !context);
237 if (wrapper->m_client)
238 wrapper->m_client->didConnect();
239 }
240
didReceiveMessageCallback(ExecutionContext * context,PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper,const String & message)241 void ThreadableWebSocketChannelClientWrapper::didReceiveMessageCallback(ExecutionContext* context, PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper, const String& message)
242 {
243 ASSERT_UNUSED(context, !context);
244 if (wrapper->m_client)
245 wrapper->m_client->didReceiveMessage(message);
246 }
247
didReceiveBinaryDataCallback(ExecutionContext * context,PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper,PassOwnPtr<Vector<char>> binaryData)248 void ThreadableWebSocketChannelClientWrapper::didReceiveBinaryDataCallback(ExecutionContext* context, PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper, PassOwnPtr<Vector<char> > binaryData)
249 {
250 ASSERT_UNUSED(context, !context);
251 if (wrapper->m_client)
252 wrapper->m_client->didReceiveBinaryData(binaryData);
253 }
254
didUpdateBufferedAmountCallback(ExecutionContext * context,PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper,unsigned long bufferedAmount)255 void ThreadableWebSocketChannelClientWrapper::didUpdateBufferedAmountCallback(ExecutionContext* context, PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper, unsigned long bufferedAmount)
256 {
257 ASSERT_UNUSED(context, !context);
258 if (wrapper->m_client)
259 wrapper->m_client->didUpdateBufferedAmount(bufferedAmount);
260 }
261
didStartClosingHandshakeCallback(ExecutionContext * context,PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper)262 void ThreadableWebSocketChannelClientWrapper::didStartClosingHandshakeCallback(ExecutionContext* context, PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper)
263 {
264 ASSERT_UNUSED(context, !context);
265 if (wrapper->m_client)
266 wrapper->m_client->didStartClosingHandshake();
267 }
268
didCloseCallback(ExecutionContext * context,PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper,unsigned long unhandledBufferedAmount,WebSocketChannelClient::ClosingHandshakeCompletionStatus closingHandshakeCompletion,unsigned short code,const String & reason)269 void ThreadableWebSocketChannelClientWrapper::didCloseCallback(ExecutionContext* context, PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper, unsigned long unhandledBufferedAmount, WebSocketChannelClient::ClosingHandshakeCompletionStatus closingHandshakeCompletion, unsigned short code, const String& reason)
270 {
271 ASSERT_UNUSED(context, !context);
272 if (wrapper->m_client)
273 wrapper->m_client->didClose(unhandledBufferedAmount, closingHandshakeCompletion, code, reason);
274 }
275
didReceiveMessageErrorCallback(ExecutionContext * context,PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper)276 void ThreadableWebSocketChannelClientWrapper::didReceiveMessageErrorCallback(ExecutionContext* context, PassRefPtr<ThreadableWebSocketChannelClientWrapper> wrapper)
277 {
278 ASSERT_UNUSED(context, !context);
279 if (wrapper->m_client)
280 wrapper->m_client->didReceiveMessageError();
281 }
282
283 } // namespace WebCore
284