Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: Source/modules/websockets/NewWebSocketChannelImpl.cpp

Issue 22914026: [ABANDONED] Introduce blink-side bridges for the new WebSocket implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/modules/websockets/NewWebSocketChannelImpl.h ('k') | public/platform/Platform.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2013 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/NewWebSocketChannelImpl.h"
33
34 #include "bindings/v8/ScriptCallStackFactory.h"
35 #include "core/dom/ScriptExecutionContext.h"
36 #include "core/fileapi/Blob.h"
37 #include "core/fileapi/FileReaderLoader.h"
38 #include "core/inspector/InspectorInstrumentation.h"
39 #include "core/inspector/ScriptCallStack.h"
40 #include "core/loader/UniqueIdentifier.h"
41 #include "core/platform/Logging.h"
42 #include "modules/websockets/WebSocketChannel.h"
43 #include "modules/websockets/WebSocketChannelClient.h"
44 #include "public/platform/Platform.h"
45 #include "public/platform/WebData.h"
46 #include "public/platform/WebSocketHandle.h"
47 #include "public/platform/WebString.h"
48 #include "public/platform/WebURL.h"
49 #include "public/platform/WebVector.h"
50 #include "weborigin/SecurityOrigin.h"
51 #include "wtf/ArrayBuffer.h"
52 #include "wtf/OwnPtr.h"
53 #include "wtf/PassRefPtr.h"
54 #include "wtf/RefPtr.h"
55 #include "wtf/Vector.h"
56 #include "wtf/text/WTFString.h"
57
58 // FIXME: The following notifications are not implemented:
59 // InspectorInstrument::willSendWebSocketHandshake
60 // InspectorInstrument::didReceiveWebSocketHandshakeResponse
61 // InspectorInstrument::didReceiveWebSocketFrame
62 // InspectorInstrument::didSendWebSocketFrame
63
64 namespace WebCore {
65
66 namespace {
67
68 bool isClean(int code)
69 {
70 return code == WebSocketChannel::CloseEventCodeNormalClosure
71 || (WebSocketChannel::CloseEventCodeMinimumUserDefined <= code
72 && code <= WebSocketChannel::CloseEventCodeMaximumUserDefined);
73 }
74
75 } // namespace
76
77 NewWebSocketChannelImpl::NewWebSocketChannelImpl(ScriptExecutionContext* context , WebSocketChannelClient* client, const String& sourceURL, unsigned lineNumber)
78 : m_context(context)
79 , m_handle(adoptPtr(WebKit::Platform::current()->createWebSocketHandle()))
80 , m_client(client)
81 , m_identifier(0)
82 , m_state(NotConnected)
83 , m_sendingQuota(0)
84 , m_receivedDataSizeForFlowControl(0)
85 , m_bufferedAmount(0)
86 , m_sentSizeOfTopMessage(0)
87 , m_hasAlreadyFailed(false)
88 , m_isSuspended(false)
89 , m_sourceURLAtConnection(sourceURL)
90 , m_lineNumberAtConnection(lineNumber)
91 {
92 if (context->isDocument() && toDocument(context)->page()) {
93 m_identifier = createUniqueIdentifier();
94 }
95 }
96
97 void NewWebSocketChannelImpl::connect(const KURL& url, const String& protocol)
98 {
99 ASSERT(m_state == NotConnected);
100 LOG(Network, "NewWebSocketChannelImpl %p connect()", this);
101 if (m_identifier) {
102 InspectorInstrumentation::didCreateWebSocket(toDocument(m_context), m_id entifier, url, protocol);
103 }
104 m_state = Connecting;
105 m_url = url;
106 Vector<String> protocols;
107 // Since protocol is already verified and escaped, we can simply split it.
108 protocol.split(", ", true, protocols);
109 WebKit::WebVector<WebKit::WebString> webProtocols(protocols.size());
110 for (size_t i = 0; i < protocols.size(); ++i) {
111 webProtocols[i] = protocols[i];
112 }
113 m_handle->connect(url, webProtocols, m_context->securityOrigin()->toString() , this);
114
115 RefPtr<ScriptCallStack> callStack = createScriptCallStack(1, true);
116 if (callStack && callStack->size()) {
117 m_sourceURLAtConnection = callStack->at(0).sourceURL();
118 m_lineNumberAtConnection = callStack->at(0).lineNumber();
119 }
120 }
121
122 String NewWebSocketChannelImpl::subprotocol()
123 {
124 LOG(Network, "NewWebSocketChannelImpl %p subprotocol()", this);
125 return m_subprotocol;
126 }
127
128 String NewWebSocketChannelImpl::extensions()
129 {
130 LOG(Network, "NewWebSocketChannelImpl %p extensions()", this);
131 return m_extensions;
132 }
133
134 WebSocketChannel::SendResult NewWebSocketChannelImpl::send(const String& message )
135 {
136 LOG(Network, "NewWebSocketChannelImpl %p sendText(%s)", this, message.utf8() .data());
137 if (m_state != Open) {
138 return SendFail;
139 }
140 m_messages.append(Message(message));
141 sendInternal();
142 return SendSuccess;
143 }
144
145 WebSocketChannel::SendResult NewWebSocketChannelImpl::send(const Blob& blob)
146 {
147 LOG(Network, "NewWebSocketChannelImpl %p sendBlob()", this);
148 if (m_state != Open) {
149 return SendFail;
150 }
151 m_messages.append(Message(blob));
152 sendInternal();
153 return SendSuccess;
154 }
155
156 WebSocketChannel::SendResult NewWebSocketChannelImpl::send(const ArrayBuffer& bu ffer, unsigned byteOffset, unsigned byteLength)
157 {
158 LOG(Network, "NewWebSocketChannelImpl %p sendArrayBuffer(%p, %u, %u)", this, buffer.data(), byteOffset, byteLength);
159 if (m_state != Open) {
160 return SendFail;
161 }
162 m_messages.append(buffer.slice(byteOffset, byteOffset + byteLength));
163 sendInternal();
164 return SendSuccess;
165 }
166
167 unsigned long NewWebSocketChannelImpl::bufferedAmount() const
168 {
169 LOG(Network, "NewWebSocketChannelImpl %p bufferedAmount()", this);
170 return m_bufferedAmount;
171 }
172
173 void NewWebSocketChannelImpl::close(int code, const String& reason)
174 {
175 LOG(Network, "NewWebSocketChannelImpl %p close(%d, %s)", this, code, reason. utf8().data());
176 if (m_state == Closing || m_state == Closed) {
177 return;
178 }
179 ASSERT(m_handle);
180 if (m_state == Open) {
181 m_handle->close(static_cast<unsigned short>(code), reason);
182 }
183 m_state = Closing;
184 }
185
186 void NewWebSocketChannelImpl::fail(const String& reason, MessageLevel level, con st String& sourceURL, unsigned lineNumber)
187 {
188 LOG(Network, "NewWebSocketChannelImpl %p fail(%s)", this, reason.utf8().data ());
189 // m_handle and m_client can be null here.
190 const String message = "WebSocket connection to '" + m_url.elidedString() + "' failed: " + reason;
191 m_context->addConsoleMessage(JSMessageSource, level, message, sourceURL, lin eNumber);
192 if (m_identifier) {
193 InspectorInstrumentation::didReceiveWebSocketFrameError(toDocument(m_con text), m_identifier, reason);
194 }
195
196 if (m_client && !m_hasAlreadyFailed) {
197 if (m_isSuspended) {
198 m_pendingEvents.append(PendingEvent(PendingEvent::DidReceiveError));
199 } else {
200 m_client->didReceiveMessageError();
201 }
202 }
203 m_hasAlreadyFailed = true;
204 if (m_state != Closing && m_state != Closed) {
205 disconnect();
206 }
207 }
208
209 void NewWebSocketChannelImpl::disconnect()
210 {
211 LOG(Network, "NewWebSocketChannelImpl %p disconnect()", this);
212 if (m_state == Closed) {
213 return;
214 }
215 if (m_identifier) {
216 InspectorInstrumentation::didCloseWebSocket(toDocument(m_context), m_ide ntifier);
217 }
218 ASSERT(m_handle);
219 if (m_state != Closing) {
220 m_handle->close(CloseEventCodeAbnormalClosure, "");
221 }
222 m_state = Closed;
223 m_handle = 0;
224 m_client = 0;
225 }
226
227 void NewWebSocketChannelImpl::suspend()
228 {
229 LOG(Network, "NewWebSocketChannelImpl %p suspend()", this);
230 m_isSuspended = true;
231 }
232
233 void NewWebSocketChannelImpl::resume()
234 {
235 LOG(Network, "NewWebSocketChannelImpl %p resume()", this);
236 m_isSuspended = false;
237 if (!m_client) {
238 ASSERT(m_state == Closed);
239 ASSERT(m_pendingEvents.isEmpty());
240 return;
241 }
242 sendInternal();
243 flowControlIfNecessary();
244 processPendingEvents();
245 // processPendingEvents may delete this object.
246 }
247
248 NewWebSocketChannelImpl::Message::Message(const String& text)
249 : type(MessageTypeText)
250 , text(text.utf8(String::StrictConversionReplacingUnpairedSurrogatesWithFFFD )) { }
251
252 NewWebSocketChannelImpl::Message::Message(const Blob& blob)
253 : type(MessageTypeBlob)
254 , blob(Blob::create(blob.url(), blob.type(), blob.size())) { }
255
256 NewWebSocketChannelImpl::Message::Message(PassRefPtr<ArrayBuffer> arrayBuffer)
257 : type(MessageTypeArrayBuffer)
258 , arrayBuffer(arrayBuffer) { }
259
260 void NewWebSocketChannelImpl::sendInternal()
261 {
262 if (m_state != Open || m_blobLoader || !m_sendingQuota) {
263 return;
264 }
265 ASSERT(m_handle);
266 ASSERT(m_client);
267 unsigned long bufferedAmount = m_bufferedAmount;
268 size_t i;
269 for (i = 0; i < m_messages.size(); ++i) {
270 if (!m_sendingQuota) {
271 break;
272 }
273 const Message& message = m_messages[i];
274 switch (message.type) {
275 case MessageTypeText: {
276 WebKit::WebSocketHandle::MessageType type =
277 m_sentSizeOfTopMessage ? WebKit::WebSocketHandle::MessageTypeCon tinuation : WebKit::WebSocketHandle::MessageTypeText;
278 size_t size = std::min(static_cast<size_t>(m_sendingQuota), message. text.length() - m_sentSizeOfTopMessage);
279 m_handle->send(type, message.text.data() + m_sentSizeOfTopMessage, s ize, m_sendingQuota == size);
280 m_sentSizeOfTopMessage += size;
281 m_sendingQuota -= size;
282 break;
283 }
284 case MessageTypeBlob:
285 startLoadingBlob(*message.blob);
286 break;
287 case MessageTypeArrayBuffer: {
288 WebKit::WebSocketHandle::MessageType type =
289 m_sentSizeOfTopMessage ? WebKit::WebSocketHandle::MessageTypeCon tinuation : WebKit::WebSocketHandle::MessageTypeBinary;
290 size_t size = std::min(static_cast<size_t>(m_sendingQuota), message. arrayBuffer->byteLength() - m_sentSizeOfTopMessage);
291 m_handle->send(type, static_cast<const char*>(message.arrayBuffer->d ata()) + m_sentSizeOfTopMessage, size, m_sendingQuota == size);
292 m_sentSizeOfTopMessage += size;
293 m_sendingQuota -= size;
294 break;
295 }
296 default:
297 ASSERT_NOT_REACHED();
298 }
299 if (m_blobLoader || !m_sendingQuota) {
300 break;
301 }
302
303 m_sentSizeOfTopMessage = 0;
304 }
305 // Drop consumed messages.
306 m_messages.remove(0, i);
307 if (!m_isSuspended && m_bufferedAmount != bufferedAmount) {
308 m_client->didUpdateBufferedAmount(m_bufferedAmount);
309 }
310 }
311
312 void NewWebSocketChannelImpl::flowControlIfNecessary()
313 {
314 if (m_state != Open) {
315 return;
316 }
317 ASSERT(m_handle);
318 if (m_receivedDataSizeForFlowControl < receivedDataSizeForFlowControlHighWat erMark) {
319 return;
320 }
321 m_handle->flowControl(m_receivedDataSizeForFlowControl);
322 m_receivedDataSizeForFlowControl = 0;
323 }
324
325 void NewWebSocketChannelImpl::didConnect(WebKit::WebSocketHandle* handle, bool s ucceed, const WebKit::WebString& selectedProtocol, const WebKit::WebString& exte nsions)
326 {
327 LOG(Network, "NewWebSocketChannelImpl %p didConnect(%p, %d, %s, %s)", this, handle, succeed, selectedProtocol.utf8().data(), extensions.utf8().data());
328 if (m_state != Connecting) {
329 return;
330 }
331 ASSERT(handle == m_handle);
332 ASSERT(m_client);
333 if (!succeed) {
334 failAsError("Cannot connect to " + m_url.string() + ".");
335 return;
336 }
337 m_state = Open;
338 m_subprotocol = selectedProtocol;
339 m_extensions = extensions;
340 if (m_isSuspended) {
341 m_pendingEvents.append(PendingEvent(PendingEvent::DidConnectComplete));
342 } else {
343 m_client->didConnect();
344 }
345 }
346
347 void NewWebSocketChannelImpl::didReceiveData(WebKit::WebSocketHandle* handle, We bKit::WebSocketHandle::MessageType type, const char* data, size_t size, bool fin )
348 {
349 LOG(Network, "NewWebSocketChannelImpl %p didReceiveData(%p, (%p, %zu), %d, % d)", this, handle, data.data(), data.size(), type, fin);
350 if (m_state != Open) {
351 return;
352 }
353 ASSERT(handle == m_handle);
354 ASSERT(m_client);
355 if (type == WebKit::WebSocketHandle::MessageTypeText) {
tyoshino (SeeGerritForStatus) 2013/08/26 09:35:42 switch?
yhirano 2013/08/26 10:21:35 Done.
356 ASSERT(m_receivingMessageData.isEmpty());
357 m_receivingMessageTypeIsText = true;
358 } else if (type == WebKit::WebSocketHandle::MessageTypeBinary) {
359 ASSERT(m_receivingMessageData.isEmpty());
360 m_receivingMessageTypeIsText = false;
361 } else {
362 ASSERT(type == WebKit::WebSocketHandle::MessageTypeContinuation);
363 ASSERT(!m_receivingMessageData.isEmpty());
364 }
365 m_receivingMessageData.append(data, size);
366 m_receivedDataSizeForFlowControl += size;
367 flowControlIfNecessary();
368 if (fin) {
369 if (m_isSuspended) {
370 m_pendingEvents.append(PendingEvent(m_receivingMessageTypeIsText ? P endingEvent::DidReceiveTextMessage : PendingEvent::DidReceiveBinaryMessage));
371 m_pendingEvents.last().message.swap(m_receivingMessageData);
372 } else {
373 if (m_receivingMessageTypeIsText) {
374 String message = "";
375 if (m_receivingMessageData.size() > 0) {
376 message = String::fromUTF8(m_receivingMessageData.data(), m_ receivingMessageData.size());
377 }
378 if (message.isNull()) {
379 failAsError("Could not decode a text frame as UTF-8.");
380 } else {
381 m_client->didReceiveMessage(message);
382 }
383 } else {
384 OwnPtr<Vector<char> > binaryData = adoptPtr(new Vector<char>);
385 m_receivingMessageData.swap(*binaryData);
386 m_client->didReceiveBinaryData(binaryData.release());
387 }
388 }
389 m_receivingMessageData.clear();
390 }
391 }
392
393 void NewWebSocketChannelImpl::didClose(WebKit::WebSocketHandle* handle, unsigned short code, const WebKit::WebString& reason)
394 {
395 // FIXME: Maybe we should notify an error to m_client for some didClose mess ages.
396 LOG(Network, "NewWebSocketChannelImpl %p didClose(%p, %d, %s)", this, code, String(reason).utf8().data());
397 if (m_state == Closed) {
398 return;
399 }
400 if (m_identifier) {
401 InspectorInstrumentation::didCloseWebSocket(toDocument(m_context), m_ide ntifier);
402 }
403 ASSERT(handle == m_handle);
404 m_handle = 0;
405 m_state = Closed;
406 if (m_isSuspended) {
407 m_pendingEvents.append(PendingEvent(code, reason));
408 return;
409 }
410 WebSocketChannelClient* client = m_client;
411 m_client = 0;
412 ASSERT(client);
413 client->didClose(m_bufferedAmount,
414 isClean(code) ? WebSocketChannelClient::ClosingHandshakeComplete : WebSo cketChannelClient::ClosingHandshakeIncomplete,
415 code,
416 reason);
417 // client->didClose may delete this object.
418 }
419
420 void NewWebSocketChannelImpl::didFinishLoading()
421 {
422 // m_client can be invalid here.
423 LOG(Network, "NewWebSocketChannelImpl %p didFinishLoading()", this);
424 if (m_state == Open) {
425 ASSERT(m_handle);
426 // The loaded blob is always placed on m_messages[0].
427 ASSERT(m_messages.size() > 0 && m_messages[0].type == MessageTypeBlob);
428 // We replace it with the loaded blob.
429 m_messages[0] = Message(m_blobLoader->arrayBufferResult());
430 sendInternal();
431 }
432 m_blobLoader.clear();
433
434 deref();
435 // deref() may delete this object.
436 }
437
438 void NewWebSocketChannelImpl::didFail(FileError::ErrorCode errorCode)
439 {
440 // m_client can be invalid here.
441 LOG(Network, "NewWebSocketChannelImpl %p didFail(%d)", this, errorCode);
442 m_blobLoader.clear();
443 failAsError("Failed to load Blob: error code = " + String::number(errorCode) ); // FIXME: Generate human-friendly reason message.
444 deref();
445 // deref() may delete this object.
446 }
447
448 void NewWebSocketChannelImpl::startLoadingBlob(const Blob& blob)
449 {
450 LOG(Network, "NewWebSocketChannelImpl %p startLoadingBlob(%s)", this, blob.u rl().string().utf8().data());
451 ASSERT(!m_blobLoader);
452 // Protect this object until the loading completes or fails.
453 ref();
454
455 m_blobLoader = adoptPtr(new FileReaderLoader(FileReaderLoader::ReadAsArrayBu ffer, this));
456 m_blobLoader->start(m_context, blob);
457 }
458
459 void NewWebSocketChannelImpl::processPendingEvents()
460 {
461 RefPtr<NewWebSocketChannelImpl> protect(this);
462 ASSERT(!m_isSuspended);
463
464 for (size_t i = 0; i < m_pendingEvents.size(); ++i) {
465 ASSERT(m_client);
466 PendingEvent& event = m_pendingEvents[i];
467 switch (event.type) {
468 case PendingEvent::DidConnectComplete:
469 m_client->didConnect();
470 break;
471 case PendingEvent::DidReceiveTextMessage: {
472 String message = "";
473 if (event.message.size() > 0) {
474 message = String::fromUTF8(event.message.data(), event.message.s ize());
475 }
476 if (message.isNull()) {
477 failAsError("Could not decode a text frame as UTF-8.");
478 // m_client can be null here.
479 } else {
480 m_client->didReceiveMessage(message);
481 }
482 break;
483 }
484 case PendingEvent::DidReceiveBinaryMessage: {
485 OwnPtr<Vector<char> > binaryData = adoptPtr(new Vector<char>);
486 event.message.swap(*binaryData);
487 m_client->didReceiveBinaryData(binaryData.release());
488 break;
489 }
490 case PendingEvent::DidReceiveError:
491 m_client->didReceiveMessageError();
492 break;
493 case PendingEvent::DidClose:
494 ASSERT(m_state == Closed);
495 ASSERT(!m_handle);
496 m_client->didClose(m_bufferedAmount,
497 isClean(event.closingCode) ? WebSocketChannelClient::ClosingHand shakeComplete : WebSocketChannelClient::ClosingHandshakeIncomplete,
tyoshino (SeeGerritForStatus) 2013/08/26 09:35:42 let's define a variable for this argument.
yhirano 2013/08/26 10:21:35 Done.
498 event.closingCode,
499 event.closingReason);
500 // m_client can be invalid here.
501 m_client = 0;
502 break;
503 default:
504 ASSERT_NOT_REACHED();
505 break;
506 }
507
508 if (event.type == PendingEvent::DidClose || !m_client) {
509 // Drop remaining messages.
510 break;
511 }
512 }
513 m_pendingEvents.clear();
514 }
515
516 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/modules/websockets/NewWebSocketChannelImpl.h ('k') | public/platform/Platform.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698