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" | |
44 #include "core/loader/SubresourceFilter.h" | |
42 #include "core/loader/FrameLoader.h" | 45 #include "core/loader/FrameLoader.h" |
43 #include "core/loader/MixedContentChecker.h" | 46 #include "core/loader/MixedContentChecker.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" |
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" |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
177 m_url = url; | 180 m_url = url; |
178 Vector<String> protocols; | 181 Vector<String> protocols; |
179 // Avoid placing an empty token in the Vector when the protocol string is | 182 // Avoid placing an empty token in the Vector when the protocol string is |
180 // empty. | 183 // empty. |
181 if (!protocol.isEmpty()) { | 184 if (!protocol.isEmpty()) { |
182 // Since protocol is already verified and escaped, we can simply split | 185 // Since protocol is already verified and escaped, we can simply split |
183 // it. | 186 // it. |
184 protocol.split(", ", true, protocols); | 187 protocol.split(", ", true, protocols); |
185 } | 188 } |
186 | 189 |
190 // If the connection needs to be filtered, asynchronously fail. Note that | |
engedy
2017/03/06 14:12:46
nit: Let's make this comment more specific, as it
Charlie Harrison
2017/03/06 14:48:23
It's based on that and based on the fact that we d
| |
191 // returning "true" just indicates that this was not synchronous security | |
192 // error. | |
193 if (shouldDisallowConnection(url)) { | |
194 TaskRunnerHelper::get(TaskType::Networking, document()) | |
195 ->postTask( | |
196 BLINK_FROM_HERE, | |
197 WTF::bind( | |
198 &DocumentWebSocketChannel::failWithClosureCode, | |
199 wrapPersistent(this), CloseEventCodePolicyViolation, | |
engedy
2017/03/06 14:12:46
nit: Is this error code really appropriate here?
Charlie Harrison
2017/03/06 14:48:23
Hm yeah I think you're right. Since we never open
| |
200 String("Connection disallowed by the subresource filter"), | |
201 WarningMessageLevel, | |
202 WTF::passed(SourceLocation::create(String(), 0, 0, nullptr)))); | |
203 return true; | |
204 } | |
205 | |
187 // TODO(kinuko): document() should return nullptr if we don't | 206 // TODO(kinuko): document() should return nullptr if we don't |
188 // have valid document/frame that returns non-empty interface provider. | 207 // have valid document/frame that returns non-empty interface provider. |
189 if (document() && document()->frame() && | 208 if (document() && document()->frame() && |
190 document()->frame()->interfaceProvider() != | 209 document()->frame()->interfaceProvider() != |
191 InterfaceProvider::getEmptyInterfaceProvider()) { | 210 InterfaceProvider::getEmptyInterfaceProvider()) { |
192 // Initialize the WebSocketHandle with the frame's InterfaceProvider to | 211 // Initialize the WebSocketHandle with the frame's InterfaceProvider to |
193 // provide the WebSocket implementation with context about this frame. | 212 // provide the WebSocket implementation with context about this frame. |
194 // This is important so that the browser can show UI associated with | 213 // This is important so that the browser can show UI associated with |
195 // the WebSocket (e.g., for certificate errors). | 214 // the WebSocket (e.g., for certificate errors). |
196 m_handle->initialize(document()->frame()->interfaceProvider()); | 215 m_handle->initialize(document()->frame()->interfaceProvider()); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 DCHECK(m_handle); | 308 DCHECK(m_handle); |
290 unsigned short codeToSend = static_cast<unsigned short>( | 309 unsigned short codeToSend = static_cast<unsigned short>( |
291 code == CloseEventCodeNotSpecified ? CloseEventCodeNoStatusRcvd : code); | 310 code == CloseEventCodeNotSpecified ? CloseEventCodeNoStatusRcvd : code); |
292 m_messages.append(new Message(codeToSend, reason)); | 311 m_messages.append(new Message(codeToSend, reason)); |
293 processSendQueue(); | 312 processSendQueue(); |
294 } | 313 } |
295 | 314 |
296 void DocumentWebSocketChannel::fail(const String& reason, | 315 void DocumentWebSocketChannel::fail(const String& reason, |
297 MessageLevel level, | 316 MessageLevel level, |
298 std::unique_ptr<SourceLocation> location) { | 317 std::unique_ptr<SourceLocation> location) { |
318 return failWithClosureCode(CloseEventCodeAbnormalClosure, reason, level, | |
319 std::move(location)); | |
320 } | |
321 | |
322 void DocumentWebSocketChannel::failWithClosureCode( | |
323 unsigned short code, | |
324 const String& reason, | |
325 MessageLevel level, | |
326 std::unique_ptr<SourceLocation> location) { | |
299 NETWORK_DVLOG(1) << this << " fail(" << reason << ")"; | 327 NETWORK_DVLOG(1) << this << " fail(" << reason << ")"; |
300 // m_handle and m_client can be null here. | 328 // m_handle and m_client can be null here. |
301 | 329 |
302 connection_handle_for_scheduler_.reset(); | 330 connection_handle_for_scheduler_.reset(); |
303 | 331 |
304 if (document()) { | 332 if (document()) { |
305 InspectorInstrumentation::didReceiveWebSocketFrameError( | 333 InspectorInstrumentation::didReceiveWebSocketFrameError( |
306 document(), m_identifier, reason); | 334 document(), m_identifier, reason); |
307 const String message = "WebSocket connection to '" + m_url.elidedString() + | 335 const String message = "WebSocket connection to '" + m_url.elidedString() + |
308 "' failed: " + reason; | 336 "' failed: " + reason; |
309 document()->addConsoleMessage(ConsoleMessage::create( | 337 document()->addConsoleMessage(ConsoleMessage::create( |
310 JSMessageSource, level, message, std::move(location))); | 338 JSMessageSource, level, message, std::move(location))); |
311 } | 339 } |
312 | 340 |
313 if (m_client) | 341 if (m_client) |
314 m_client->didError(); | 342 m_client->didError(); |
315 // |reason| is only for logging and should not be provided for scripts, | 343 // |reason| is only for logging and should not be provided for scripts, |
316 // hence close reason must be empty. | 344 // hence close reason must be empty. |
317 handleDidClose(false, CloseEventCodeAbnormalClosure, String()); | 345 handleDidClose(false, code, String()); |
318 // handleDidClose may delete this object. | 346 // handleDidClose may delete this object. |
319 } | 347 } |
320 | 348 |
321 void DocumentWebSocketChannel::disconnect() { | 349 void DocumentWebSocketChannel::disconnect() { |
322 NETWORK_DVLOG(1) << this << " disconnect()"; | 350 NETWORK_DVLOG(1) << this << " disconnect()"; |
323 if (m_identifier) { | 351 if (m_identifier) { |
324 TRACE_EVENT_INSTANT1( | 352 TRACE_EVENT_INSTANT1( |
325 "devtools.timeline", "WebSocketDestroy", TRACE_EVENT_SCOPE_THREAD, | 353 "devtools.timeline", "WebSocketDestroy", TRACE_EVENT_SCOPE_THREAD, |
326 "data", InspectorWebSocketEvent::data(document(), m_identifier)); | 354 "data", InspectorWebSocketEvent::data(document(), m_identifier)); |
327 InspectorInstrumentation::didCloseWebSocket(document(), m_identifier); | 355 InspectorInstrumentation::didCloseWebSocket(document(), m_identifier); |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
674 m_blobLoader.clear(); | 702 m_blobLoader.clear(); |
675 if (errorCode == FileError::kAbortErr) { | 703 if (errorCode == FileError::kAbortErr) { |
676 // The error is caused by cancel(). | 704 // The error is caused by cancel(). |
677 return; | 705 return; |
678 } | 706 } |
679 // FIXME: Generate human-friendly reason message. | 707 // FIXME: Generate human-friendly reason message. |
680 failAsError("Failed to load Blob: error code = " + String::number(errorCode)); | 708 failAsError("Failed to load Blob: error code = " + String::number(errorCode)); |
681 // |this| can be deleted here. | 709 // |this| can be deleted here. |
682 } | 710 } |
683 | 711 |
712 bool DocumentWebSocketChannel::shouldDisallowConnection(const KURL& url) { | |
713 if (!m_handle) | |
engedy
2017/03/06 14:12:46
Can this ever evaluate to false? If not, let's mak
Charlie Harrison
2017/03/06 14:48:23
Done.
| |
714 return false; | |
715 DocumentLoader* loader = document()->loader(); | |
716 if (!loader) | |
717 return false; | |
718 SubresourceFilter* subresourceFilter = loader->subresourceFilter(); | |
719 if (!subresourceFilter) | |
720 return false; | |
721 return !subresourceFilter->allowWebSocketConnection(url); | |
722 } | |
723 | |
684 DEFINE_TRACE(DocumentWebSocketChannel) { | 724 DEFINE_TRACE(DocumentWebSocketChannel) { |
685 visitor->trace(m_blobLoader); | 725 visitor->trace(m_blobLoader); |
686 visitor->trace(m_messages); | 726 visitor->trace(m_messages); |
687 visitor->trace(m_client); | 727 visitor->trace(m_client); |
688 visitor->trace(m_loadingContext); | 728 visitor->trace(m_loadingContext); |
689 WebSocketChannel::trace(visitor); | 729 WebSocketChannel::trace(visitor); |
690 } | 730 } |
691 | 731 |
692 std::ostream& operator<<(std::ostream& ostream, | 732 std::ostream& operator<<(std::ostream& ostream, |
693 const DocumentWebSocketChannel* channel) { | 733 const DocumentWebSocketChannel* channel) { |
694 return ostream << "DocumentWebSocketChannel " | 734 return ostream << "DocumentWebSocketChannel " |
695 << static_cast<const void*>(channel); | 735 << static_cast<const void*>(channel); |
696 } | 736 } |
697 | 737 |
698 } // namespace blink | 738 } // namespace blink |
OLD | NEW |