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 <memory> | 33 #include <memory> |
34 #include "core/dom/DOMArrayBuffer.h" | 34 #include "core/dom/DOMArrayBuffer.h" |
35 #include "core/dom/Document.h" | 35 #include "core/dom/Document.h" |
36 #include "core/dom/ExecutionContext.h" | 36 #include "core/dom/ExecutionContext.h" |
37 #include "core/dom/TaskRunnerHelper.h" | |
37 #include "core/fileapi/FileReaderLoader.h" | 38 #include "core/fileapi/FileReaderLoader.h" |
38 #include "core/fileapi/FileReaderLoaderClient.h" | 39 #include "core/fileapi/FileReaderLoaderClient.h" |
39 #include "core/frame/LocalFrame.h" | 40 #include "core/frame/LocalFrame.h" |
40 #include "core/frame/LocalFrameClient.h" | 41 #include "core/frame/LocalFrameClient.h" |
41 #include "core/inspector/ConsoleMessage.h" | 42 #include "core/inspector/ConsoleMessage.h" |
42 #include "core/inspector/InspectorInstrumentation.h" | 43 #include "core/inspector/InspectorInstrumentation.h" |
44 #include "core/loader/DocumentLoader.h" | |
45 #include "core/loader/SubresourceFilter.h" | |
43 #include "core/loader/FrameLoader.h" | 46 #include "core/loader/FrameLoader.h" |
44 #include "core/loader/MixedContentChecker.h" | 47 #include "core/loader/MixedContentChecker.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" |
50 #include "platform/loader/fetch/UniqueIdentifier.h" | 53 #include "platform/loader/fetch/UniqueIdentifier.h" |
51 #include "platform/network/NetworkLog.h" | 54 #include "platform/network/NetworkLog.h" |
52 #include "platform/network/WebSocketHandshakeRequest.h" | 55 #include "platform/network/WebSocketHandshakeRequest.h" |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 m_url = url; | 177 m_url = url; |
175 Vector<String> protocols; | 178 Vector<String> protocols; |
176 // Avoid placing an empty token in the Vector when the protocol string is | 179 // Avoid placing an empty token in the Vector when the protocol string is |
177 // empty. | 180 // empty. |
178 if (!protocol.isEmpty()) { | 181 if (!protocol.isEmpty()) { |
179 // Since protocol is already verified and escaped, we can simply split | 182 // Since protocol is already verified and escaped, we can simply split |
180 // it. | 183 // it. |
181 protocol.split(", ", true, protocols); | 184 protocol.split(", ", true, protocols); |
182 } | 185 } |
183 | 186 |
187 // If the connection needs to be filtered, asynchronously fail. Note that | |
188 // returning "true" just indicates that this was not synchronous security | |
189 // error. | |
190 if (shouldFilterConnection(url)) { | |
191 // TODO(csharrison): Include a reason string here. | |
pkalinnikov
2017/03/02 12:33:53
Could this be done in this CL? E.g., "Connection b
Charlie Harrison
2017/03/02 14:48:01
OK. This is a little hard to understand but it is
| |
192 TaskRunnerHelper::get(TaskType::Networking, document()) | |
193 ->postTask( | |
194 BLINK_FROM_HERE, | |
195 WTF::bind( | |
196 &DocumentWebSocketChannel::failWithClosureCode, | |
197 wrapPersistent(this), CloseEventCodePolicyViolation, String(""), | |
198 WarningMessageLevel, | |
199 WTF::passed(SourceLocation::create(String(), 0, 0, nullptr)))); | |
200 return true; | |
201 } | |
202 | |
184 if (document()->frame() && | 203 if (document()->frame() && |
185 document()->frame()->interfaceProvider() != | 204 document()->frame()->interfaceProvider() != |
186 InterfaceProvider::getEmptyInterfaceProvider()) { | 205 InterfaceProvider::getEmptyInterfaceProvider()) { |
187 // Initialize the WebSocketHandle with the frame's InterfaceProvider to | 206 // Initialize the WebSocketHandle with the frame's InterfaceProvider to |
188 // provide the WebSocket implementation with context about this frame. | 207 // provide the WebSocket implementation with context about this frame. |
189 // This is important so that the browser can show UI associated with | 208 // This is important so that the browser can show UI associated with |
190 // the WebSocket (e.g., for certificate errors). | 209 // the WebSocket (e.g., for certificate errors). |
191 m_handle->initialize(document()->frame()->interfaceProvider()); | 210 m_handle->initialize(document()->frame()->interfaceProvider()); |
192 } else { | 211 } else { |
193 m_handle->initialize(Platform::current()->interfaceProvider()); | 212 m_handle->initialize(Platform::current()->interfaceProvider()); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 DCHECK(m_handle); | 303 DCHECK(m_handle); |
285 unsigned short codeToSend = static_cast<unsigned short>( | 304 unsigned short codeToSend = static_cast<unsigned short>( |
286 code == CloseEventCodeNotSpecified ? CloseEventCodeNoStatusRcvd : code); | 305 code == CloseEventCodeNotSpecified ? CloseEventCodeNoStatusRcvd : code); |
287 m_messages.append(new Message(codeToSend, reason)); | 306 m_messages.append(new Message(codeToSend, reason)); |
288 processSendQueue(); | 307 processSendQueue(); |
289 } | 308 } |
290 | 309 |
291 void DocumentWebSocketChannel::fail(const String& reason, | 310 void DocumentWebSocketChannel::fail(const String& reason, |
292 MessageLevel level, | 311 MessageLevel level, |
293 std::unique_ptr<SourceLocation> location) { | 312 std::unique_ptr<SourceLocation> location) { |
313 return failWithClosureCode(CloseEventCodeAbnormalClosure, reason, level, | |
314 std::move(location)); | |
315 } | |
316 | |
317 void DocumentWebSocketChannel::failWithClosureCode( | |
318 unsigned short code, | |
319 const String& reason, | |
320 MessageLevel level, | |
321 std::unique_ptr<SourceLocation> location) { | |
294 NETWORK_DVLOG(1) << this << " fail(" << reason << ")"; | 322 NETWORK_DVLOG(1) << this << " fail(" << reason << ")"; |
295 // m_handle and m_client can be null here. | 323 // m_handle and m_client can be null here. |
296 | 324 |
297 connection_handle_for_scheduler_.reset(); | 325 connection_handle_for_scheduler_.reset(); |
298 | 326 |
299 InspectorInstrumentation::didReceiveWebSocketFrameError(document(), | 327 InspectorInstrumentation::didReceiveWebSocketFrameError(document(), |
300 m_identifier, reason); | 328 m_identifier, reason); |
301 const String message = "WebSocket connection to '" + m_url.elidedString() + | 329 const String message = "WebSocket connection to '" + m_url.elidedString() + |
302 "' failed: " + reason; | 330 "' failed: " + reason; |
303 document()->addConsoleMessage(ConsoleMessage::create( | 331 document()->addConsoleMessage(ConsoleMessage::create( |
304 JSMessageSource, level, message, std::move(location))); | 332 JSMessageSource, level, message, std::move(location))); |
305 | 333 |
306 if (m_client) | 334 if (m_client) |
307 m_client->didError(); | 335 m_client->didError(); |
308 // |reason| is only for logging and should not be provided for scripts, | 336 // |reason| is only for logging and should not be provided for scripts, |
309 // hence close reason must be empty. | 337 // hence close reason must be empty. |
310 handleDidClose(false, CloseEventCodeAbnormalClosure, String()); | 338 handleDidClose(false, code, String()); |
311 // handleDidClose may delete this object. | 339 // handleDidClose may delete this object. |
312 } | 340 } |
313 | 341 |
314 void DocumentWebSocketChannel::disconnect() { | 342 void DocumentWebSocketChannel::disconnect() { |
315 NETWORK_DVLOG(1) << this << " disconnect()"; | 343 NETWORK_DVLOG(1) << this << " disconnect()"; |
316 if (m_identifier) { | 344 if (m_identifier) { |
317 TRACE_EVENT_INSTANT1( | 345 TRACE_EVENT_INSTANT1( |
318 "devtools.timeline", "WebSocketDestroy", TRACE_EVENT_SCOPE_THREAD, | 346 "devtools.timeline", "WebSocketDestroy", TRACE_EVENT_SCOPE_THREAD, |
319 "data", InspectorWebSocketEvent::data(document(), m_identifier)); | 347 "data", InspectorWebSocketEvent::data(document(), m_identifier)); |
320 InspectorInstrumentation::didCloseWebSocket(document(), m_identifier); | 348 InspectorInstrumentation::didCloseWebSocket(document(), m_identifier); |
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
655 m_blobLoader.clear(); | 683 m_blobLoader.clear(); |
656 if (errorCode == FileError::kAbortErr) { | 684 if (errorCode == FileError::kAbortErr) { |
657 // The error is caused by cancel(). | 685 // The error is caused by cancel(). |
658 return; | 686 return; |
659 } | 687 } |
660 // FIXME: Generate human-friendly reason message. | 688 // FIXME: Generate human-friendly reason message. |
661 failAsError("Failed to load Blob: error code = " + String::number(errorCode)); | 689 failAsError("Failed to load Blob: error code = " + String::number(errorCode)); |
662 // |this| can be deleted here. | 690 // |this| can be deleted here. |
663 } | 691 } |
664 | 692 |
693 bool DocumentWebSocketChannel::shouldFilterConnection(const KURL& url) { | |
pkalinnikov
2017/03/02 12:33:53
nit: How about should(Disallow|Block)Connection?
Charlie Harrison
2017/03/02 14:48:01
Changed to Disallow.
| |
694 if (!m_handle) | |
695 return false; | |
696 DocumentLoader* loader = document()->loader(); | |
697 if (!loader) | |
698 return false; | |
699 SubresourceFilter* subresourceFilter = loader->subresourceFilter(); | |
700 if (!subresourceFilter) | |
701 return false; | |
702 return !subresourceFilter->allowWebSocketConnection(url); | |
703 } | |
704 | |
665 DEFINE_TRACE(DocumentWebSocketChannel) { | 705 DEFINE_TRACE(DocumentWebSocketChannel) { |
666 visitor->trace(m_blobLoader); | 706 visitor->trace(m_blobLoader); |
667 visitor->trace(m_messages); | 707 visitor->trace(m_messages); |
668 visitor->trace(m_client); | 708 visitor->trace(m_client); |
669 visitor->trace(m_document); | 709 visitor->trace(m_document); |
670 WebSocketChannel::trace(visitor); | 710 WebSocketChannel::trace(visitor); |
671 } | 711 } |
672 | 712 |
673 std::ostream& operator<<(std::ostream& ostream, | 713 std::ostream& operator<<(std::ostream& ostream, |
674 const DocumentWebSocketChannel* channel) { | 714 const DocumentWebSocketChannel* channel) { |
675 return ostream << "DocumentWebSocketChannel " | 715 return ostream << "DocumentWebSocketChannel " |
676 << static_cast<const void*>(channel); | 716 << static_cast<const void*>(channel); |
677 } | 717 } |
678 | 718 |
679 } // namespace blink | 719 } // namespace blink |
OLD | NEW |