Chromium Code Reviews| 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 15 matching lines...) Expand all Loading... | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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 <memory> | 33 #include <memory> |
| 34 #include "core/dom/DOMArrayBuffer.h" | 34 #include "core/dom/DOMArrayBuffer.h" |
| 35 #include "core/dom/ExecutionContext.h" | 35 #include "core/dom/ExecutionContext.h" |
| 36 #include "core/dom/TaskRunnerHelper.h" | |
| 36 #include "core/fileapi/FileReaderLoader.h" | 37 #include "core/fileapi/FileReaderLoader.h" |
| 37 #include "core/fileapi/FileReaderLoaderClient.h" | 38 #include "core/fileapi/FileReaderLoaderClient.h" |
| 38 #include "core/frame/LocalFrame.h" | 39 #include "core/frame/LocalFrame.h" |
| 39 #include "core/frame/LocalFrameClient.h" | 40 #include "core/frame/LocalFrameClient.h" |
| 40 #include "core/inspector/ConsoleMessage.h" | 41 #include "core/inspector/ConsoleMessage.h" |
| 41 #include "core/inspector/InspectorInstrumentation.h" | 42 #include "core/inspector/InspectorInstrumentation.h" |
| 43 #include "core/loader/DocumentLoader.h" | |
| 42 #include "core/loader/FrameLoader.h" | 44 #include "core/loader/FrameLoader.h" |
| 43 #include "core/loader/MixedContentChecker.h" | 45 #include "core/loader/MixedContentChecker.h" |
| 46 #include "core/loader/SubresourceFilter.h" | |
| 44 #include "core/loader/ThreadableLoadingContext.h" | 47 #include "core/loader/ThreadableLoadingContext.h" |
| 45 #include "modules/websockets/InspectorWebSocketEvents.h" | 48 #include "modules/websockets/InspectorWebSocketEvents.h" |
| 46 #include "modules/websockets/WebSocketChannelClient.h" | 49 #include "modules/websockets/WebSocketChannelClient.h" |
| 47 #include "modules/websockets/WebSocketFrame.h" | 50 #include "modules/websockets/WebSocketFrame.h" |
| 48 #include "modules/websockets/WebSocketHandleImpl.h" | 51 #include "modules/websockets/WebSocketHandleImpl.h" |
| 49 #include "platform/WebFrameScheduler.h" | 52 #include "platform/WebFrameScheduler.h" |
| 53 #include "platform/WebTaskRunner.h" | |
| 50 #include "platform/loader/fetch/UniqueIdentifier.h" | 54 #include "platform/loader/fetch/UniqueIdentifier.h" |
| 51 #include "platform/network/NetworkLog.h" | 55 #include "platform/network/NetworkLog.h" |
| 52 #include "platform/network/WebSocketHandshakeRequest.h" | 56 #include "platform/network/WebSocketHandshakeRequest.h" |
| 53 #include "platform/weborigin/SecurityOrigin.h" | 57 #include "platform/weborigin/SecurityOrigin.h" |
| 54 #include "public/platform/InterfaceProvider.h" | 58 #include "public/platform/InterfaceProvider.h" |
| 55 #include "public/platform/Platform.h" | 59 #include "public/platform/Platform.h" |
| 60 #include "public/platform/WebTraceLocation.h" | |
| 61 #include "wtf/Functional.h" | |
| 56 #include "wtf/PtrUtil.h" | 62 #include "wtf/PtrUtil.h" |
| 57 | 63 |
| 58 namespace blink { | 64 namespace blink { |
| 59 | 65 |
| 60 class DocumentWebSocketChannel::BlobLoader final | 66 class DocumentWebSocketChannel::BlobLoader final |
| 61 : public GarbageCollectedFinalized<DocumentWebSocketChannel::BlobLoader>, | 67 : public GarbageCollectedFinalized<DocumentWebSocketChannel::BlobLoader>, |
| 62 public FileReaderLoaderClient { | 68 public FileReaderLoaderClient { |
| 63 public: | 69 public: |
| 64 BlobLoader(PassRefPtr<BlobDataHandle>, DocumentWebSocketChannel*); | 70 BlobLoader(PassRefPtr<BlobDataHandle>, DocumentWebSocketChannel*); |
| 65 ~BlobLoader() override {} | 71 ~BlobLoader() override {} |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 m_url = url; | 183 m_url = url; |
| 178 Vector<String> protocols; | 184 Vector<String> protocols; |
| 179 // Avoid placing an empty token in the Vector when the protocol string is | 185 // Avoid placing an empty token in the Vector when the protocol string is |
| 180 // empty. | 186 // empty. |
| 181 if (!protocol.isEmpty()) { | 187 if (!protocol.isEmpty()) { |
| 182 // Since protocol is already verified and escaped, we can simply split | 188 // Since protocol is already verified and escaped, we can simply split |
| 183 // it. | 189 // it. |
| 184 protocol.split(", ", true, protocols); | 190 protocol.split(", ", true, protocols); |
| 185 } | 191 } |
| 186 | 192 |
| 193 // If the connection needs to be filtered, asynchronously fail. Synchronous | |
| 194 // failure blocks the worker thread which should be avoided. Note that | |
| 195 // returning "true" just indicates that this was not a mixed content error. | |
| 196 if (shouldDisallowConnection(url)) { | |
| 197 TaskRunnerHelper::get(TaskType::Networking, document()) | |
| 198 ->postTask( | |
| 199 BLINK_FROM_HERE, | |
| 200 WTF::bind(&DocumentWebSocketChannel::tearDownFailedConnection, | |
| 201 wrapPersistent(this))); | |
| 202 return true; | |
| 203 } | |
| 204 | |
| 187 // TODO(kinuko): document() should return nullptr if we don't | 205 // TODO(kinuko): document() should return nullptr if we don't |
| 188 // have valid document/frame that returns non-empty interface provider. | 206 // have valid document/frame that returns non-empty interface provider. |
| 189 if (document() && document()->frame() && | 207 if (document() && document()->frame() && |
| 190 document()->frame()->interfaceProvider() != | 208 document()->frame()->interfaceProvider() != |
| 191 InterfaceProvider::getEmptyInterfaceProvider()) { | 209 InterfaceProvider::getEmptyInterfaceProvider()) { |
| 192 // Initialize the WebSocketHandle with the frame's InterfaceProvider to | 210 // Initialize the WebSocketHandle with the frame's InterfaceProvider to |
| 193 // provide the WebSocket implementation with context about this frame. | 211 // provide the WebSocket implementation with context about this frame. |
| 194 // This is important so that the browser can show UI associated with | 212 // This is important so that the browser can show UI associated with |
| 195 // the WebSocket (e.g., for certificate errors). | 213 // the WebSocket (e.g., for certificate errors). |
| 196 m_handle->initialize(document()->frame()->interfaceProvider()); | 214 m_handle->initialize(document()->frame()->interfaceProvider()); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 unsigned short codeToSend = static_cast<unsigned short>( | 308 unsigned short codeToSend = static_cast<unsigned short>( |
| 291 code == CloseEventCodeNotSpecified ? CloseEventCodeNoStatusRcvd : code); | 309 code == CloseEventCodeNotSpecified ? CloseEventCodeNoStatusRcvd : code); |
| 292 m_messages.append(new Message(codeToSend, reason)); | 310 m_messages.append(new Message(codeToSend, reason)); |
| 293 processSendQueue(); | 311 processSendQueue(); |
| 294 } | 312 } |
| 295 | 313 |
| 296 void DocumentWebSocketChannel::fail(const String& reason, | 314 void DocumentWebSocketChannel::fail(const String& reason, |
| 297 MessageLevel level, | 315 MessageLevel level, |
| 298 std::unique_ptr<SourceLocation> location) { | 316 std::unique_ptr<SourceLocation> location) { |
| 299 NETWORK_DVLOG(1) << this << " fail(" << reason << ")"; | 317 NETWORK_DVLOG(1) << this << " fail(" << reason << ")"; |
| 300 // m_handle and m_client can be null here. | |
| 301 | |
| 302 connection_handle_for_scheduler_.reset(); | |
| 303 | |
| 304 if (document()) { | 318 if (document()) { |
| 305 InspectorInstrumentation::didReceiveWebSocketFrameError( | 319 InspectorInstrumentation::didReceiveWebSocketFrameError( |
| 306 document(), m_identifier, reason); | 320 document(), m_identifier, reason); |
| 307 const String message = "WebSocket connection to '" + m_url.elidedString() + | 321 const String message = "WebSocket connection to '" + m_url.elidedString() + |
| 308 "' failed: " + reason; | 322 "' failed: " + reason; |
| 309 document()->addConsoleMessage(ConsoleMessage::create( | 323 document()->addConsoleMessage(ConsoleMessage::create( |
| 310 JSMessageSource, level, message, std::move(location))); | 324 JSMessageSource, level, message, std::move(location))); |
| 311 } | 325 } |
| 312 | 326 tearDownFailedConnection(); |
| 313 if (m_client) | |
| 314 m_client->didError(); | |
| 315 // |reason| is only for logging and should not be provided for scripts, | |
| 316 // hence close reason must be empty. | |
| 317 handleDidClose(false, CloseEventCodeAbnormalClosure, String()); | |
| 318 // handleDidClose may delete this object. | |
| 319 } | 327 } |
| 320 | 328 |
| 321 void DocumentWebSocketChannel::disconnect() { | 329 void DocumentWebSocketChannel::disconnect() { |
| 322 NETWORK_DVLOG(1) << this << " disconnect()"; | 330 NETWORK_DVLOG(1) << this << " disconnect()"; |
| 323 if (m_identifier) { | 331 if (m_identifier) { |
| 324 TRACE_EVENT_INSTANT1( | 332 TRACE_EVENT_INSTANT1( |
| 325 "devtools.timeline", "WebSocketDestroy", TRACE_EVENT_SCOPE_THREAD, | 333 "devtools.timeline", "WebSocketDestroy", TRACE_EVENT_SCOPE_THREAD, |
| 326 "data", InspectorWebSocketEvent::data(document(), m_identifier)); | 334 "data", InspectorWebSocketEvent::data(document(), m_identifier)); |
| 327 InspectorInstrumentation::didCloseWebSocket(document(), m_identifier); | 335 InspectorInstrumentation::didCloseWebSocket(document(), m_identifier); |
| 328 } | 336 } |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 674 m_blobLoader.clear(); | 682 m_blobLoader.clear(); |
| 675 if (errorCode == FileError::kAbortErr) { | 683 if (errorCode == FileError::kAbortErr) { |
| 676 // The error is caused by cancel(). | 684 // The error is caused by cancel(). |
| 677 return; | 685 return; |
| 678 } | 686 } |
| 679 // FIXME: Generate human-friendly reason message. | 687 // FIXME: Generate human-friendly reason message. |
| 680 failAsError("Failed to load Blob: error code = " + String::number(errorCode)); | 688 failAsError("Failed to load Blob: error code = " + String::number(errorCode)); |
| 681 // |this| can be deleted here. | 689 // |this| can be deleted here. |
| 682 } | 690 } |
| 683 | 691 |
| 692 void DocumentWebSocketChannel::tearDownFailedConnection() { | |
| 693 // m_handle and m_client can be null here. | |
| 694 connection_handle_for_scheduler_.reset(); | |
| 695 | |
| 696 if (m_client) | |
| 697 m_client->didError(); | |
| 698 | |
| 699 // |reason| is only for logging and should not be provided for scripts, | |
|
yhirano
2017/03/06 20:39:01
This comment does not make sense any more. Moving
Charlie Harrison
2017/03/06 20:48:21
Done.
| |
| 700 // hence close reason must be empty. | |
| 701 handleDidClose(false, CloseEventCodeAbnormalClosure, String()); | |
| 702 // handleDidClose may delete this object. | |
| 703 } | |
| 704 | |
| 705 bool DocumentWebSocketChannel::shouldDisallowConnection(const KURL& url) { | |
| 706 DCHECK(m_handle); | |
| 707 DocumentLoader* loader = document()->loader(); | |
| 708 if (!loader) | |
| 709 return false; | |
| 710 SubresourceFilter* subresourceFilter = loader->subresourceFilter(); | |
| 711 if (!subresourceFilter) | |
| 712 return false; | |
| 713 return !subresourceFilter->allowWebSocketConnection(url); | |
| 714 } | |
| 715 | |
| 684 DEFINE_TRACE(DocumentWebSocketChannel) { | 716 DEFINE_TRACE(DocumentWebSocketChannel) { |
| 685 visitor->trace(m_blobLoader); | 717 visitor->trace(m_blobLoader); |
| 686 visitor->trace(m_messages); | 718 visitor->trace(m_messages); |
| 687 visitor->trace(m_client); | 719 visitor->trace(m_client); |
| 688 visitor->trace(m_loadingContext); | 720 visitor->trace(m_loadingContext); |
| 689 WebSocketChannel::trace(visitor); | 721 WebSocketChannel::trace(visitor); |
| 690 } | 722 } |
| 691 | 723 |
| 692 std::ostream& operator<<(std::ostream& ostream, | 724 std::ostream& operator<<(std::ostream& ostream, |
| 693 const DocumentWebSocketChannel* channel) { | 725 const DocumentWebSocketChannel* channel) { |
| 694 return ostream << "DocumentWebSocketChannel " | 726 return ostream << "DocumentWebSocketChannel " |
| 695 << static_cast<const void*>(channel); | 727 << static_cast<const void*>(channel); |
| 696 } | 728 } |
| 697 | 729 |
| 698 } // namespace blink | 730 } // namespace blink |
| OLD | NEW |