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 #ifndef DocumentWebSocketChannel_h | 31 #ifndef DocumentWebSocketChannel_h |
| 32 #define DocumentWebSocketChannel_h | 32 #define DocumentWebSocketChannel_h |
| 33 | 33 |
| 34 #include <stdint.h> | 34 #include <stdint.h> |
| 35 #include <memory> | 35 #include <memory> |
| 36 #include <utility> | |
| 36 #include "bindings/core/v8/SourceLocation.h" | 37 #include "bindings/core/v8/SourceLocation.h" |
| 37 #include "core/fileapi/Blob.h" | 38 #include "core/fileapi/Blob.h" |
| 38 #include "core/fileapi/FileError.h" | 39 #include "core/fileapi/FileError.h" |
| 39 #include "core/loader/ThreadableLoadingContext.h" | 40 #include "core/loader/ThreadableLoadingContext.h" |
| 40 #include "modules/ModulesExport.h" | 41 #include "modules/ModulesExport.h" |
| 41 #include "modules/websockets/WebSocketChannel.h" | 42 #include "modules/websockets/WebSocketChannel.h" |
| 42 #include "modules/websockets/WebSocketHandle.h" | 43 #include "modules/websockets/WebSocketHandle.h" |
| 43 #include "modules/websockets/WebSocketHandleClient.h" | 44 #include "modules/websockets/WebSocketHandleClient.h" |
| 44 #include "platform/WebFrameScheduler.h" | 45 #include "platform/WebFrameScheduler.h" |
| 45 #include "platform/heap/Handle.h" | 46 #include "platform/heap/Handle.h" |
| 46 #include "platform/weborigin/KURL.h" | 47 #include "platform/weborigin/KURL.h" |
| 47 #include "platform/wtf/Deque.h" | 48 #include "platform/wtf/Deque.h" |
| 48 #include "platform/wtf/PassRefPtr.h" | 49 #include "platform/wtf/PassRefPtr.h" |
| 49 #include "platform/wtf/RefPtr.h" | 50 #include "platform/wtf/RefPtr.h" |
| 50 #include "platform/wtf/Vector.h" | 51 #include "platform/wtf/Vector.h" |
| 51 #include "platform/wtf/text/CString.h" | 52 #include "platform/wtf/text/CString.h" |
| 52 #include "platform/wtf/text/WTFString.h" | 53 #include "platform/wtf/text/WTFString.h" |
| 54 #include "public/platform/WebCallbacks.h" | |
| 53 | 55 |
| 54 namespace blink { | 56 namespace blink { |
| 55 | 57 |
| 56 class ThreadableLoadingContext; | 58 class ThreadableLoadingContext; |
| 57 class WebSocketHandshakeRequest; | 59 class WebSocketHandshakeRequest; |
| 60 class WebSocketHandshakeThrottle; | |
| 58 | 61 |
| 59 // This class is a WebSocketChannel subclass that works with a Document in a | 62 // This class is a WebSocketChannel subclass that works with a Document in a |
| 60 // DOMWindow (i.e. works in the main thread). | 63 // DOMWindow (i.e. works in the main thread). |
| 61 class MODULES_EXPORT DocumentWebSocketChannel final | 64 class MODULES_EXPORT DocumentWebSocketChannel final |
| 62 : public WebSocketChannel, | 65 : public WebSocketChannel, |
| 63 public WebSocketHandleClient { | 66 public WebSocketHandleClient, |
| 67 public WebCallbacks<void, const WebString&> { | |
| 64 public: | 68 public: |
| 65 // You can specify the source file and the line number information | 69 // You can specify the source file and the line number information |
| 66 // explicitly by passing the last parameter. | 70 // explicitly by passing the last parameter. |
| 67 // In the usual case, they are set automatically and you don't have to | 71 // In the usual case, they are set automatically and you don't have to |
| 68 // pass it. | 72 // pass it. |
| 69 // Specify handle explicitly only in tests. | 73 // Specify handle explicitly only in tests. |
| 70 static DocumentWebSocketChannel* Create( | 74 static DocumentWebSocketChannel* Create( |
| 71 Document* document, | 75 Document* document, |
| 72 WebSocketChannelClient* client, | 76 WebSocketChannelClient* client, |
| 73 std::unique_ptr<SourceLocation> location, | 77 std::unique_ptr<SourceLocation> location, |
| 74 WebSocketHandle* handle = 0) { | 78 WebSocketHandle* handle = 0) { |
|
yhirano
2017/06/13 09:14:39
Please remove this parameter. It's only for tests,
Adam Rice
2017/06/13 12:53:42
Cool, thanks, done.
| |
| 75 DCHECK(document); | 79 DCHECK(document); |
| 76 return Create(ThreadableLoadingContext::Create(*document), client, | 80 return Create(ThreadableLoadingContext::Create(*document), client, |
| 77 std::move(location), handle); | 81 std::move(location), handle); |
| 78 } | 82 } |
| 79 static DocumentWebSocketChannel* Create( | 83 static DocumentWebSocketChannel* Create(ThreadableLoadingContext*, |
| 80 ThreadableLoadingContext* loading_context, | 84 WebSocketChannelClient*, |
| 81 WebSocketChannelClient* client, | 85 std::unique_ptr<SourceLocation>, |
| 82 std::unique_ptr<SourceLocation> location, | 86 WebSocketHandle* = 0); |
|
yhirano
2017/06/13 09:14:39
ditto
Adam Rice
2017/06/13 12:53:42
Done.
| |
| 83 WebSocketHandle* handle = 0) { | 87 static DocumentWebSocketChannel* CreateForTesting( |
| 84 return new DocumentWebSocketChannel(loading_context, client, | 88 Document*, |
| 85 std::move(location), handle); | 89 WebSocketChannelClient*, |
| 86 } | 90 std::unique_ptr<SourceLocation>, |
| 91 WebSocketHandle*, | |
| 92 std::unique_ptr<WebSocketHandshakeThrottle>); | |
| 93 | |
| 87 ~DocumentWebSocketChannel() override; | 94 ~DocumentWebSocketChannel() override; |
| 88 | 95 |
| 89 // WebSocketChannel functions. | 96 // WebSocketChannel functions. |
| 90 bool Connect(const KURL&, const String& protocol) override; | 97 bool Connect(const KURL&, const String& protocol) override; |
| 91 void Send(const CString& message) override; | 98 void Send(const CString& message) override; |
| 92 void Send(const DOMArrayBuffer&, | 99 void Send(const DOMArrayBuffer&, |
| 93 unsigned byte_offset, | 100 unsigned byte_offset, |
| 94 unsigned byte_length) override; | 101 unsigned byte_length) override; |
| 95 void Send(PassRefPtr<BlobDataHandle>) override; | 102 void Send(PassRefPtr<BlobDataHandle>) override; |
| 96 void SendTextAsCharVector(std::unique_ptr<Vector<char>> data) override; | 103 void SendTextAsCharVector(std::unique_ptr<Vector<char>> data) override; |
| 97 void SendBinaryAsCharVector(std::unique_ptr<Vector<char>> data) override; | 104 void SendBinaryAsCharVector(std::unique_ptr<Vector<char>> data) override; |
| 98 // Start closing handshake. Use the CloseEventCodeNotSpecified for the code | 105 // Start closing handshake. Use the CloseEventCodeNotSpecified for the code |
| 99 // argument to omit payload. | 106 // argument to omit payload. |
| 100 void Close(int code, const String& reason) override; | 107 void Close(int code, const String& reason) override; |
| 101 void Fail(const String& reason, | 108 void Fail(const String& reason, |
| 102 MessageLevel, | 109 MessageLevel, |
| 103 std::unique_ptr<SourceLocation>) override; | 110 std::unique_ptr<SourceLocation>) override; |
| 104 void Disconnect() override; | 111 void Disconnect() override; |
| 105 | 112 |
| 106 DECLARE_VIRTUAL_TRACE(); | 113 DECLARE_VIRTUAL_TRACE(); |
| 107 | 114 |
| 108 private: | 115 private: |
| 109 class BlobLoader; | 116 class BlobLoader; |
| 110 class Message; | 117 class Message; |
| 118 struct ConnectInfo; | |
| 111 | 119 |
| 112 enum MessageType { | 120 enum MessageType { |
| 113 kMessageTypeText, | 121 kMessageTypeText, |
| 114 kMessageTypeBlob, | 122 kMessageTypeBlob, |
| 115 kMessageTypeArrayBuffer, | 123 kMessageTypeArrayBuffer, |
| 116 kMessageTypeTextAsCharVector, | 124 kMessageTypeTextAsCharVector, |
| 117 kMessageTypeBinaryAsCharVector, | 125 kMessageTypeBinaryAsCharVector, |
| 118 kMessageTypeClose, | 126 kMessageTypeClose, |
| 119 }; | 127 }; |
| 120 | 128 |
| 121 struct ReceivedMessage { | 129 struct ReceivedMessage { |
| 122 bool is_message_text; | 130 bool is_message_text; |
| 123 Vector<char> data; | 131 Vector<char> data; |
| 124 }; | 132 }; |
| 125 | 133 |
| 126 DocumentWebSocketChannel(ThreadableLoadingContext*, | 134 DocumentWebSocketChannel(ThreadableLoadingContext*, |
| 127 WebSocketChannelClient*, | 135 WebSocketChannelClient*, |
| 128 std::unique_ptr<SourceLocation>, | 136 std::unique_ptr<SourceLocation>, |
| 129 WebSocketHandle*); | 137 WebSocketHandle*, |
| 138 std::unique_ptr<WebSocketHandshakeThrottle>); | |
| 139 | |
| 130 void SendInternal(WebSocketHandle::MessageType, | 140 void SendInternal(WebSocketHandle::MessageType, |
| 131 const char* data, | 141 const char* data, |
| 132 size_t total_size, | 142 size_t total_size, |
| 133 uint64_t* consumed_buffered_amount); | 143 uint64_t* consumed_buffered_amount); |
| 134 void ProcessSendQueue(); | 144 void ProcessSendQueue(); |
| 135 void FlowControlIfNecessary(); | 145 void FlowControlIfNecessary(); |
| 136 void FailAsError(const String& reason) { | 146 void FailAsError(const String& reason) { |
| 137 Fail(reason, kErrorMessageLevel, location_at_construction_->Clone()); | 147 Fail(reason, kErrorMessageLevel, location_at_construction_->Clone()); |
| 138 } | 148 } |
| 139 void AbortAsyncOperations(); | 149 void AbortAsyncOperations(); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 160 WebSocketHandle::MessageType, | 170 WebSocketHandle::MessageType, |
| 161 const char* data, | 171 const char* data, |
| 162 size_t) override; | 172 size_t) override; |
| 163 void DidClose(WebSocketHandle*, | 173 void DidClose(WebSocketHandle*, |
| 164 bool was_clean, | 174 bool was_clean, |
| 165 unsigned short code, | 175 unsigned short code, |
| 166 const String& reason) override; | 176 const String& reason) override; |
| 167 void DidReceiveFlowControl(WebSocketHandle*, int64_t quota) override; | 177 void DidReceiveFlowControl(WebSocketHandle*, int64_t quota) override; |
| 168 void DidStartClosingHandshake(WebSocketHandle*) override; | 178 void DidStartClosingHandshake(WebSocketHandle*) override; |
| 169 | 179 |
| 180 // WebCallbacks<void, const WebString&> functions. These are called with the | |
| 181 // results of throttling. | |
| 182 void OnSuccess() override; | |
| 183 void OnError(const WebString& console_message) override; | |
| 184 | |
| 170 // Methods for BlobLoader. | 185 // Methods for BlobLoader. |
| 171 void DidFinishLoadingBlob(DOMArrayBuffer*); | 186 void DidFinishLoadingBlob(DOMArrayBuffer*); |
| 172 void DidFailLoadingBlob(FileError::ErrorCode); | 187 void DidFailLoadingBlob(FileError::ErrorCode); |
| 173 | 188 |
| 174 void TearDownFailedConnection(); | 189 void TearDownFailedConnection(); |
| 175 bool ShouldDisallowConnection(const KURL&); | 190 bool ShouldDisallowConnection(const KURL&); |
| 176 | 191 |
| 177 // m_handle is a handle of the connection. | 192 // m_handle is a handle of the connection. |
| 178 // m_handle == 0 means this channel is closed. | 193 // m_handle == 0 means this channel is closed. |
| 179 std::unique_ptr<WebSocketHandle> handle_; | 194 std::unique_ptr<WebSocketHandle> handle_; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 191 | 206 |
| 192 bool receiving_message_type_is_text_; | 207 bool receiving_message_type_is_text_; |
| 193 uint64_t sending_quota_; | 208 uint64_t sending_quota_; |
| 194 uint64_t received_data_size_for_flow_control_; | 209 uint64_t received_data_size_for_flow_control_; |
| 195 size_t sent_size_of_top_message_; | 210 size_t sent_size_of_top_message_; |
| 196 std::unique_ptr<WebFrameScheduler::ActiveConnectionHandle> | 211 std::unique_ptr<WebFrameScheduler::ActiveConnectionHandle> |
| 197 connection_handle_for_scheduler_; | 212 connection_handle_for_scheduler_; |
| 198 | 213 |
| 199 std::unique_ptr<SourceLocation> location_at_construction_; | 214 std::unique_ptr<SourceLocation> location_at_construction_; |
| 200 RefPtr<WebSocketHandshakeRequest> handshake_request_; | 215 RefPtr<WebSocketHandshakeRequest> handshake_request_; |
| 216 std::unique_ptr<WebSocketHandshakeThrottle> handshake_throttle_; | |
| 217 // This field is only initialised if the object is still waiting for a | |
| 218 // throttle response when DidConnect is called. | |
| 219 std::unique_ptr<ConnectInfo> connect_info_; | |
| 220 bool throttle_passed_; | |
| 201 | 221 |
| 202 static const uint64_t kReceivedDataSizeForFlowControlHighWaterMark = 1 << 15; | 222 static const uint64_t kReceivedDataSizeForFlowControlHighWaterMark = 1 << 15; |
| 203 }; | 223 }; |
| 204 | 224 |
| 205 std::ostream& operator<<(std::ostream&, const DocumentWebSocketChannel*); | 225 std::ostream& operator<<(std::ostream&, const DocumentWebSocketChannel*); |
| 206 | 226 |
| 207 } // namespace blink | 227 } // namespace blink |
| 208 | 228 |
| 209 #endif // DocumentWebSocketChannel_h | 229 #endif // DocumentWebSocketChannel_h |
| OLD | NEW |