OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 16 matching lines...) Expand all Loading... |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "modules/websockets/DocumentWebSocketChannel.h" | 31 #include "modules/websockets/DocumentWebSocketChannel.h" |
32 | 32 |
33 #include "core/dom/DOMArrayBuffer.h" | 33 #include "core/dom/DOMArrayBuffer.h" |
34 #include "core/dom/Document.h" | 34 #include "core/dom/Document.h" |
35 #include "core/dom/ExecutionContext.h" | 35 #include "core/dom/ExecutionContext.h" |
36 #include "core/fetch/UniqueIdentifier.h" | 36 #include "core/fetch/UniqueIdentifier.h" |
37 #include "core/fileapi/FileReaderLoader.h" | |
38 #include "core/fileapi/FileReaderLoaderClient.h" | |
39 #include "core/frame/LocalFrame.h" | 37 #include "core/frame/LocalFrame.h" |
40 #include "core/inspector/ConsoleMessage.h" | 38 #include "core/inspector/ConsoleMessage.h" |
41 #include "core/inspector/InspectorInstrumentation.h" | 39 #include "core/inspector/InspectorInstrumentation.h" |
42 #include "core/loader/FrameLoader.h" | 40 #include "core/loader/FrameLoader.h" |
43 #include "core/loader/FrameLoaderClient.h" | 41 #include "core/loader/FrameLoaderClient.h" |
44 #include "core/loader/MixedContentChecker.h" | 42 #include "core/loader/MixedContentChecker.h" |
45 #include "modules/websockets/InspectorWebSocketEvents.h" | 43 #include "modules/websockets/InspectorWebSocketEvents.h" |
46 #include "modules/websockets/WebSocketChannelClient.h" | 44 #include "modules/websockets/WebSocketChannelClient.h" |
47 #include "modules/websockets/WebSocketFrame.h" | 45 #include "modules/websockets/WebSocketFrame.h" |
48 #include "platform/Logging.h" | 46 #include "platform/Logging.h" |
49 #include "platform/network/WebSocketHandshakeRequest.h" | 47 #include "platform/network/WebSocketHandshakeRequest.h" |
50 #include "platform/weborigin/SecurityOrigin.h" | 48 #include "platform/weborigin/SecurityOrigin.h" |
51 #include "public/platform/Platform.h" | 49 #include "public/platform/Platform.h" |
52 #include "public/platform/WebSecurityOrigin.h" | 50 #include "public/platform/WebSecurityOrigin.h" |
53 #include "public/platform/WebString.h" | 51 #include "public/platform/WebString.h" |
54 #include "public/platform/WebURL.h" | 52 #include "public/platform/WebURL.h" |
55 #include "public/platform/WebVector.h" | 53 #include "public/platform/WebVector.h" |
56 #include "public/platform/modules/websockets/WebSocketHandshakeRequestInfo.h" | 54 #include "public/platform/modules/websockets/WebSocketHandshakeRequestInfo.h" |
57 #include "public/platform/modules/websockets/WebSocketHandshakeResponseInfo.h" | 55 #include "public/platform/modules/websockets/WebSocketHandshakeResponseInfo.h" |
58 | 56 |
59 using blink::WebSocketHandle; | 57 using blink::WebSocketHandle; |
60 | 58 |
61 namespace blink { | 59 namespace blink { |
62 | 60 |
63 class DocumentWebSocketChannel::BlobLoader final : public GarbageCollectedFinali
zed<DocumentWebSocketChannel::BlobLoader>, public FileReaderLoaderClient { | |
64 public: | |
65 BlobLoader(PassRefPtr<BlobDataHandle>, DocumentWebSocketChannel*); | |
66 ~BlobLoader() override { } | |
67 | |
68 void cancel(); | |
69 | |
70 // FileReaderLoaderClient functions. | |
71 void didStartLoading() override { } | |
72 void didReceiveData() override { } | |
73 void didFinishLoading() override; | |
74 void didFail(FileError::ErrorCode) override; | |
75 | |
76 DEFINE_INLINE_TRACE() | |
77 { | |
78 visitor->trace(m_channel); | |
79 } | |
80 | |
81 private: | |
82 Member<DocumentWebSocketChannel> m_channel; | |
83 FileReaderLoader m_loader; | |
84 }; | |
85 | |
86 DocumentWebSocketChannel::BlobLoader::BlobLoader(PassRefPtr<BlobDataHandle> blob
DataHandle, DocumentWebSocketChannel* channel) | |
87 : m_channel(channel) | |
88 , m_loader(FileReaderLoader::ReadAsArrayBuffer, this) | |
89 { | |
90 m_loader.start(channel->executionContext(), blobDataHandle); | |
91 } | |
92 | |
93 void DocumentWebSocketChannel::BlobLoader::cancel() | |
94 { | |
95 m_loader.cancel(); | |
96 // didFail will be called immediately. | |
97 // |this| is deleted here. | |
98 } | |
99 | |
100 void DocumentWebSocketChannel::BlobLoader::didFinishLoading() | |
101 { | |
102 m_channel->didFinishLoadingBlob(m_loader.arrayBufferResult()); | |
103 // |this| is deleted here. | |
104 } | |
105 | |
106 void DocumentWebSocketChannel::BlobLoader::didFail(FileError::ErrorCode errorCod
e) | |
107 { | |
108 m_channel->didFailLoadingBlob(errorCode); | |
109 // |this| is deleted here. | |
110 } | |
111 | |
112 DocumentWebSocketChannel::DocumentWebSocketChannel(Document* document, WebSocket
ChannelClient* client, const String& sourceURL, unsigned lineNumber, WebSocketHa
ndle *handle) | 61 DocumentWebSocketChannel::DocumentWebSocketChannel(Document* document, WebSocket
ChannelClient* client, const String& sourceURL, unsigned lineNumber, WebSocketHa
ndle *handle) |
113 : ContextLifecycleObserver(document) | 62 : ContextLifecycleObserver(document) |
114 , m_handle(adoptPtr(handle ? handle : Platform::current()->createWebSocketHa
ndle())) | 63 , m_handle(adoptPtr(handle ? handle : Platform::current()->createWebSocketHa
ndle())) |
115 , m_client(client) | 64 , m_client(client) |
116 , m_identifier(createUniqueIdentifier()) | 65 , m_identifier(createUniqueIdentifier()) |
| 66 , m_blobLoadingMode(false) |
| 67 , m_blobDataPending(0) |
117 , m_sendingQuota(0) | 68 , m_sendingQuota(0) |
118 , m_receivedDataSizeForFlowControl(receivedDataSizeForFlowControlHighWaterMa
rk * 2) // initial quota | 69 , m_receivedDataSizeForFlowControl(receivedDataSizeForFlowControlHighWaterMa
rk * 2) // initial quota |
119 , m_sentSizeOfTopMessage(0) | 70 , m_sentSizeOfTopMessage(0) |
120 , m_sourceURLAtConstruction(sourceURL) | 71 , m_sourceURLAtConstruction(sourceURL) |
121 , m_lineNumberAtConstruction(lineNumber) | 72 , m_lineNumberAtConstruction(lineNumber) |
122 { | 73 { |
123 } | 74 } |
124 | 75 |
125 DocumentWebSocketChannel::~DocumentWebSocketChannel() | 76 DocumentWebSocketChannel::~DocumentWebSocketChannel() |
126 { | 77 { |
127 ASSERT(!m_blobLoader); | 78 ASSERT(!m_blobLoadingMode); |
128 } | 79 } |
129 | 80 |
130 bool DocumentWebSocketChannel::connect(const KURL& url, const String& protocol) | 81 bool DocumentWebSocketChannel::connect(const KURL& url, const String& protocol) |
131 { | 82 { |
132 WTF_LOG(Network, "DocumentWebSocketChannel %p connect()", this); | 83 WTF_LOG(Network, "DocumentWebSocketChannel %p connect()", this); |
133 if (!m_handle) | 84 if (!m_handle) |
134 return false; | 85 return false; |
135 | 86 |
136 if (document()->frame()) { | 87 if (document()->frame()) { |
137 if (MixedContentChecker::shouldBlockWebSocket(document()->frame(), url)) | 88 if (MixedContentChecker::shouldBlockWebSocket(document()->frame(), url)) |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 // handleDidClose may delete this object. | 199 // handleDidClose may delete this object. |
249 } | 200 } |
250 | 201 |
251 void DocumentWebSocketChannel::disconnect() | 202 void DocumentWebSocketChannel::disconnect() |
252 { | 203 { |
253 WTF_LOG(Network, "DocumentWebSocketChannel %p disconnect()", this); | 204 WTF_LOG(Network, "DocumentWebSocketChannel %p disconnect()", this); |
254 if (m_identifier) { | 205 if (m_identifier) { |
255 TRACE_EVENT_INSTANT1("devtools.timeline", "WebSocketDestroy", TRACE_EVEN
T_SCOPE_THREAD, "data", InspectorWebSocketEvent::data(document(), m_identifier))
; | 206 TRACE_EVENT_INSTANT1("devtools.timeline", "WebSocketDestroy", TRACE_EVEN
T_SCOPE_THREAD, "data", InspectorWebSocketEvent::data(document(), m_identifier))
; |
256 InspectorInstrumentation::didCloseWebSocket(document(), m_identifier); | 207 InspectorInstrumentation::didCloseWebSocket(document(), m_identifier); |
257 } | 208 } |
258 abortAsyncOperations(); | 209 m_blobLoadingMode = false; |
259 m_handle.clear(); | 210 m_handle.clear(); |
260 m_client = nullptr; | 211 m_client = nullptr; |
261 m_identifier = 0; | 212 m_identifier = 0; |
262 } | 213 } |
263 | 214 |
264 DocumentWebSocketChannel::Message::Message(const CString& text) | 215 DocumentWebSocketChannel::Message::Message(const CString& text) |
265 : type(MessageTypeText) | 216 : type(MessageTypeText) |
266 , text(text) { } | 217 , text(text) { } |
267 | 218 |
268 DocumentWebSocketChannel::Message::Message(PassRefPtr<BlobDataHandle> blobDataHa
ndle) | 219 DocumentWebSocketChannel::Message::Message(PassRefPtr<BlobDataHandle> blobDataHa
ndle) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 m_sentSizeOfTopMessage += size; | 252 m_sentSizeOfTopMessage += size; |
302 m_sendingQuota -= size; | 253 m_sendingQuota -= size; |
303 *consumedBufferedAmount += size; | 254 *consumedBufferedAmount += size; |
304 | 255 |
305 if (final) { | 256 if (final) { |
306 m_messages.removeFirst(); | 257 m_messages.removeFirst(); |
307 m_sentSizeOfTopMessage = 0; | 258 m_sentSizeOfTopMessage = 0; |
308 } | 259 } |
309 } | 260 } |
310 | 261 |
| 262 void DocumentWebSocketChannel::sendBlob(const BlobDataHandle& blob) |
| 263 { |
| 264 ASSERT(!m_blobLoadingMode); |
| 265 m_blobLoadingMode = true; |
| 266 m_blobDataPending = blob.size(); |
| 267 m_handle->sendBlob(blob.uuid(), blob.size()); |
| 268 // The message is still in |m_messages| so that the renderer holds a |
| 269 // reference to it until it is fully sent. |
| 270 } |
| 271 |
311 void DocumentWebSocketChannel::processSendQueue() | 272 void DocumentWebSocketChannel::processSendQueue() |
312 { | 273 { |
313 ASSERT(m_handle); | 274 ASSERT(m_handle); |
314 uint64_t consumedBufferedAmount = 0; | 275 uint64_t consumedBufferedAmount = 0; |
315 while (!m_messages.isEmpty() && !m_blobLoader) { | 276 while (!m_messages.isEmpty() && !m_blobLoadingMode) { |
316 Message* message = m_messages.first().get(); | 277 Message* message = m_messages.first().get(); |
317 if (m_sendingQuota == 0 && message->type != MessageTypeClose) | 278 if (m_sendingQuota == 0 && message->type != MessageTypeClose) |
318 break; | 279 break; |
319 switch (message->type) { | 280 switch (message->type) { |
320 case MessageTypeText: | 281 case MessageTypeText: |
321 sendInternal(WebSocketHandle::MessageTypeText, message->text.data(),
message->text.length(), &consumedBufferedAmount); | 282 sendInternal(WebSocketHandle::MessageTypeText, message->text.data(),
message->text.length(), &consumedBufferedAmount); |
322 break; | 283 break; |
323 case MessageTypeBlob: | 284 case MessageTypeBlob: |
324 ASSERT(!m_blobLoader); | 285 sendBlob(*message->blobDataHandle); |
325 m_blobLoader = new BlobLoader(message->blobDataHandle, this); | |
326 break; | 286 break; |
327 case MessageTypeArrayBuffer: | 287 case MessageTypeArrayBuffer: |
328 sendInternal(WebSocketHandle::MessageTypeBinary, static_cast<const c
har*>(message->arrayBuffer->data()), message->arrayBuffer->byteLength(), &consum
edBufferedAmount); | 288 sendInternal(WebSocketHandle::MessageTypeBinary, static_cast<const c
har*>(message->arrayBuffer->data()), message->arrayBuffer->byteLength(), &consum
edBufferedAmount); |
329 break; | 289 break; |
330 case MessageTypeTextAsCharVector: | 290 case MessageTypeTextAsCharVector: |
331 sendInternal(WebSocketHandle::MessageTypeText, message->vectorData->
data(), message->vectorData->size(), &consumedBufferedAmount); | 291 sendInternal(WebSocketHandle::MessageTypeText, message->vectorData->
data(), message->vectorData->size(), &consumedBufferedAmount); |
332 break; | 292 break; |
333 case MessageTypeBinaryAsCharVector: | 293 case MessageTypeBinaryAsCharVector: |
334 sendInternal(WebSocketHandle::MessageTypeBinary, message->vectorData
->data(), message->vectorData->size(), &consumedBufferedAmount); | 294 sendInternal(WebSocketHandle::MessageTypeBinary, message->vectorData
->data(), message->vectorData->size(), &consumedBufferedAmount); |
335 break; | 295 break; |
(...skipping 13 matching lines...) Expand all Loading... |
349 | 309 |
350 void DocumentWebSocketChannel::flowControlIfNecessary() | 310 void DocumentWebSocketChannel::flowControlIfNecessary() |
351 { | 311 { |
352 if (!m_handle || m_receivedDataSizeForFlowControl < receivedDataSizeForFlowC
ontrolHighWaterMark) { | 312 if (!m_handle || m_receivedDataSizeForFlowControl < receivedDataSizeForFlowC
ontrolHighWaterMark) { |
353 return; | 313 return; |
354 } | 314 } |
355 m_handle->flowControl(m_receivedDataSizeForFlowControl); | 315 m_handle->flowControl(m_receivedDataSizeForFlowControl); |
356 m_receivedDataSizeForFlowControl = 0; | 316 m_receivedDataSizeForFlowControl = 0; |
357 } | 317 } |
358 | 318 |
359 void DocumentWebSocketChannel::abortAsyncOperations() | |
360 { | |
361 if (m_blobLoader) { | |
362 m_blobLoader->cancel(); | |
363 m_blobLoader.clear(); | |
364 } | |
365 } | |
366 | |
367 void DocumentWebSocketChannel::handleDidClose(bool wasClean, unsigned short code
, const String& reason) | 319 void DocumentWebSocketChannel::handleDidClose(bool wasClean, unsigned short code
, const String& reason) |
368 { | 320 { |
369 m_handle.clear(); | 321 m_handle.clear(); |
370 abortAsyncOperations(); | 322 m_blobLoadingMode = false; |
371 if (!m_client) { | 323 if (!m_client) { |
372 return; | 324 return; |
373 } | 325 } |
374 WebSocketChannelClient* client = m_client; | 326 WebSocketChannelClient* client = m_client; |
375 m_client = nullptr; | 327 m_client = nullptr; |
376 WebSocketChannelClient::ClosingHandshakeCompletionStatus status = | 328 WebSocketChannelClient::ClosingHandshakeCompletionStatus status = |
377 wasClean ? WebSocketChannelClient::ClosingHandshakeComplete : WebSocketC
hannelClient::ClosingHandshakeIncomplete; | 329 wasClean ? WebSocketChannelClient::ClosingHandshakeComplete : WebSocketC
hannelClient::ClosingHandshakeIncomplete; |
378 client->didClose(status, code, reason); | 330 client->didClose(status, code, reason); |
379 // client->didClose may delete this object. | 331 // client->didClose may delete this object. |
380 } | 332 } |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 | 460 |
509 void DocumentWebSocketChannel::didReceiveFlowControl(WebSocketHandle* handle, in
t64_t quota) | 461 void DocumentWebSocketChannel::didReceiveFlowControl(WebSocketHandle* handle, in
t64_t quota) |
510 { | 462 { |
511 WTF_LOG(Network, "DocumentWebSocketChannel %p didReceiveFlowControl(%p, %ld)
", this, handle, static_cast<long>(quota)); | 463 WTF_LOG(Network, "DocumentWebSocketChannel %p didReceiveFlowControl(%p, %ld)
", this, handle, static_cast<long>(quota)); |
512 | 464 |
513 ASSERT(m_handle); | 465 ASSERT(m_handle); |
514 ASSERT(handle == m_handle); | 466 ASSERT(handle == m_handle); |
515 ASSERT(quota >= 0); | 467 ASSERT(quota >= 0); |
516 | 468 |
517 m_sendingQuota += quota; | 469 m_sendingQuota += quota; |
518 processSendQueue(); | 470 if (m_blobLoadingMode) { |
| 471 uint64_t blobUsedQuota = std::min(static_cast<uint64_t>(quota), m_blobDa
taPending); |
| 472 if (blobUsedQuota > 0) { |
| 473 // Reflect bufferedAmount. If non-Blob messages were sent |
| 474 // immediately before the Blob, their quota refresh will still be |
| 475 // counted against bufferedAmount. However, bufferedAmount will |
| 476 // always be correct when blob loading mode finishes. |
| 477 m_sendingQuota -= blobUsedQuota; |
| 478 m_blobDataPending -= blobUsedQuota; |
| 479 if (m_client) |
| 480 m_client->didConsumeBufferedAmount(blobUsedQuota); |
| 481 } |
| 482 } else { |
| 483 processSendQueue(); |
| 484 } |
519 } | 485 } |
520 | 486 |
521 void DocumentWebSocketChannel::didStartClosingHandshake(WebSocketHandle* handle) | 487 void DocumentWebSocketChannel::didStartClosingHandshake(WebSocketHandle* handle) |
522 { | 488 { |
523 WTF_LOG(Network, "DocumentWebSocketChannel %p didStartClosingHandshake(%p)",
this, handle); | 489 WTF_LOG(Network, "DocumentWebSocketChannel %p didStartClosingHandshake(%p)",
this, handle); |
524 | 490 |
525 ASSERT(m_handle); | 491 ASSERT(m_handle); |
526 ASSERT(handle == m_handle); | 492 ASSERT(handle == m_handle); |
527 | 493 |
528 if (m_client) | 494 if (m_client) |
529 m_client->didStartClosingHandshake(); | 495 m_client->didStartClosingHandshake(); |
530 } | 496 } |
531 | 497 |
532 void DocumentWebSocketChannel::didFinishLoadingBlob(PassRefPtr<DOMArrayBuffer> b
uffer) | 498 void DocumentWebSocketChannel::didCompleteSendingBlob(WebSocketHandle* handle) |
533 { | 499 { |
534 m_blobLoader.clear(); | 500 WTF_LOG(Network, "DocumentWebSocketChannel %p didCompleteSendingBlob(%p)", t
his, handle); |
| 501 |
| 502 // If the IPC was unexpected, do nothing. |
| 503 if (!m_blobLoadingMode) |
| 504 return; |
| 505 |
535 ASSERT(m_handle); | 506 ASSERT(m_handle); |
536 // The loaded blob is always placed on m_messages[0]. | 507 ASSERT(handle == m_handle); |
537 ASSERT(m_messages.size() > 0 && m_messages.first()->type == MessageTypeBlob)
; | 508 ASSERT(m_sendingQuota >= m_blobDataPending); |
538 // We replace it with the loaded blob. | 509 |
539 m_messages.first() = adoptPtr(new Message(buffer)); | 510 m_sendingQuota -= m_blobDataPending; |
| 511 if (m_blobDataPending && m_client) |
| 512 m_client->didConsumeBufferedAmount(m_blobDataPending); |
| 513 m_blobDataPending = 0; |
| 514 m_blobLoadingMode = false; |
| 515 // Remove the reference to the Blob. |
| 516 m_messages.removeFirst(); |
540 processSendQueue(); | 517 processSendQueue(); |
541 } | 518 } |
542 | 519 |
543 void DocumentWebSocketChannel::didFailLoadingBlob(FileError::ErrorCode errorCode
) | |
544 { | |
545 m_blobLoader.clear(); | |
546 if (errorCode == FileError::ABORT_ERR) { | |
547 // The error is caused by cancel(). | |
548 return; | |
549 } | |
550 // FIXME: Generate human-friendly reason message. | |
551 failAsError("Failed to load Blob: error code = " + String::number(errorCode)
); | |
552 // |this| can be deleted here. | |
553 } | |
554 | |
555 DEFINE_TRACE(DocumentWebSocketChannel) | 520 DEFINE_TRACE(DocumentWebSocketChannel) |
556 { | 521 { |
557 visitor->trace(m_blobLoader); | |
558 visitor->trace(m_client); | 522 visitor->trace(m_client); |
559 WebSocketChannel::trace(visitor); | 523 WebSocketChannel::trace(visitor); |
560 ContextLifecycleObserver::trace(visitor); | 524 ContextLifecycleObserver::trace(visitor); |
561 } | 525 } |
562 | 526 |
563 } // namespace blink | 527 } // namespace blink |
OLD | NEW |