OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ | 5 #ifndef NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ |
6 #define NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ | 6 #define NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ |
7 | 7 |
| 8 #include <stdint.h> |
8 #include <queue> | 9 #include <queue> |
9 #include <string> | 10 #include <string> |
10 #include <vector> | 11 #include <vector> |
11 | 12 |
12 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
13 #include "base/callback.h" | 14 #include "base/callback.h" |
14 #include "base/compiler_specific.h" // for WARN_UNUSED_RESULT | 15 #include "base/compiler_specific.h" // for WARN_UNUSED_RESULT |
15 #include "base/i18n/streaming_utf8_validator.h" | 16 #include "base/i18n/streaming_utf8_validator.h" |
16 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
17 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 // is kOpCodeContinuation and the type the message is Text, then |data| must | 78 // is kOpCodeContinuation and the type the message is Text, then |data| must |
78 // be a chunk of a valid UTF-8 message, however there is no requirement for | 79 // be a chunk of a valid UTF-8 message, however there is no requirement for |
79 // |data| to be split on character boundaries. | 80 // |data| to be split on character boundaries. |
80 void SendFrame(bool fin, | 81 void SendFrame(bool fin, |
81 WebSocketFrameHeader::OpCode op_code, | 82 WebSocketFrameHeader::OpCode op_code, |
82 const std::vector<char>& data); | 83 const std::vector<char>& data); |
83 | 84 |
84 // Sends |quota| units of flow control to the remote side. If the underlying | 85 // Sends |quota| units of flow control to the remote side. If the underlying |
85 // transport has a concept of |quota|, then it permits the remote server to | 86 // transport has a concept of |quota|, then it permits the remote server to |
86 // send up to |quota| units of data. | 87 // send up to |quota| units of data. |
87 void SendFlowControl(int64 quota); | 88 void SendFlowControl(int64_t quota); |
88 | 89 |
89 // Starts the closing handshake for a client-initiated shutdown of the | 90 // Starts the closing handshake for a client-initiated shutdown of the |
90 // connection. There is no API to close the connection without a closing | 91 // connection. There is no API to close the connection without a closing |
91 // handshake, but destroying the WebSocketChannel object while connected will | 92 // handshake, but destroying the WebSocketChannel object while connected will |
92 // effectively do that. |code| must be in the range 1000-4999. |reason| should | 93 // effectively do that. |code| must be in the range 1000-4999. |reason| should |
93 // be a valid UTF-8 string or empty. | 94 // be a valid UTF-8 string or empty. |
94 // | 95 // |
95 // This does *not* trigger the event OnClosingHandshake(). The caller should | 96 // This does *not* trigger the event OnClosingHandshake(). The caller should |
96 // assume that the closing handshake has started and perform the equivalent | 97 // assume that the closing handshake has started and perform the equivalent |
97 // processing to OnClosingHandshake() if necessary. | 98 // processing to OnClosingHandshake() if necessary. |
98 void StartClosingHandshake(uint16 code, const std::string& reason); | 99 void StartClosingHandshake(uint16_t code, const std::string& reason); |
99 | 100 |
100 // Starts the connection process, using a specified creator callback rather | 101 // Starts the connection process, using a specified creator callback rather |
101 // than the default. This is exposed for testing. | 102 // than the default. This is exposed for testing. |
102 void SendAddChannelRequestForTesting( | 103 void SendAddChannelRequestForTesting( |
103 const GURL& socket_url, | 104 const GURL& socket_url, |
104 const std::vector<std::string>& requested_protocols, | 105 const std::vector<std::string>& requested_protocols, |
105 const url::Origin& origin, | 106 const url::Origin& origin, |
106 const WebSocketStreamCreator& creator); | 107 const WebSocketStreamCreator& creator); |
107 | 108 |
108 // The default timout for the closing handshake is a sensible value (see | 109 // The default timout for the closing handshake is a sensible value (see |
(...skipping 19 matching lines...) Expand all Loading... |
128 private: | 129 private: |
129 class HandshakeNotificationSender; | 130 class HandshakeNotificationSender; |
130 | 131 |
131 // The Windows implementation of std::queue requires that this declaration be | 132 // The Windows implementation of std::queue requires that this declaration be |
132 // visible in the header. | 133 // visible in the header. |
133 class PendingReceivedFrame { | 134 class PendingReceivedFrame { |
134 public: | 135 public: |
135 PendingReceivedFrame(bool final, | 136 PendingReceivedFrame(bool final, |
136 WebSocketFrameHeader::OpCode opcode, | 137 WebSocketFrameHeader::OpCode opcode, |
137 const scoped_refptr<IOBuffer>& data, | 138 const scoped_refptr<IOBuffer>& data, |
138 uint64 offset, | 139 uint64_t offset, |
139 uint64 size); | 140 uint64_t size); |
140 ~PendingReceivedFrame(); | 141 ~PendingReceivedFrame(); |
141 | 142 |
142 bool final() const { return final_; } | 143 bool final() const { return final_; } |
143 WebSocketFrameHeader::OpCode opcode() const { return opcode_; } | 144 WebSocketFrameHeader::OpCode opcode() const { return opcode_; } |
144 // ResetOpcode() to Continuation. | 145 // ResetOpcode() to Continuation. |
145 void ResetOpcode(); | 146 void ResetOpcode(); |
146 const scoped_refptr<IOBuffer>& data() const { return data_; } | 147 const scoped_refptr<IOBuffer>& data() const { return data_; } |
147 uint64 offset() const { return offset_; } | 148 uint64_t offset() const { return offset_; } |
148 uint64 size() const { return size_; } | 149 uint64_t size() const { return size_; } |
149 // Increase |offset_| by |bytes|. | 150 // Increase |offset_| by |bytes|. |
150 void DidConsume(uint64 bytes); | 151 void DidConsume(uint64_t bytes); |
151 | 152 |
152 // This object needs to be copyable and assignable, since it will be placed | 153 // This object needs to be copyable and assignable, since it will be placed |
153 // in a std::queue. The compiler-generated copy constructor and assignment | 154 // in a std::queue. The compiler-generated copy constructor and assignment |
154 // operator will do the right thing. | 155 // operator will do the right thing. |
155 | 156 |
156 private: | 157 private: |
157 bool final_; | 158 bool final_; |
158 WebSocketFrameHeader::OpCode opcode_; | 159 WebSocketFrameHeader::OpCode opcode_; |
159 scoped_refptr<IOBuffer> data_; | 160 scoped_refptr<IOBuffer> data_; |
160 // Where to start reading from data_. Everything prior to offset_ has | 161 // Where to start reading from data_. Everything prior to offset_ has |
161 // already been sent to the browser. | 162 // already been sent to the browser. |
162 uint64 offset_; | 163 uint64_t offset_; |
163 // The size of data_. | 164 // The size of data_. |
164 uint64 size_; | 165 uint64_t size_; |
165 }; | 166 }; |
166 | 167 |
167 // Methods which return a value of type ChannelState may delete |this|. If the | 168 // Methods which return a value of type ChannelState may delete |this|. If the |
168 // return value is CHANNEL_DELETED, then the caller must return without making | 169 // return value is CHANNEL_DELETED, then the caller must return without making |
169 // any further access to member variables or methods. | 170 // any further access to member variables or methods. |
170 typedef WebSocketEventInterface::ChannelState ChannelState; | 171 typedef WebSocketEventInterface::ChannelState ChannelState; |
171 | 172 |
172 // The object passes through a linear progression of states from | 173 // The object passes through a linear progression of states from |
173 // FRESHLY_CONSTRUCTED to CLOSED, except that the SEND_CLOSED and RECV_CLOSED | 174 // FRESHLY_CONSTRUCTED to CLOSED, except that the SEND_CLOSED and RECV_CLOSED |
174 // states may be skipped in case of error. | 175 // states may be skipped in case of error. |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 // change the value of |state_|. | 254 // change the value of |state_|. |
254 // | 255 // |
255 // This method performs sanity checks on the frame that are needed regardless | 256 // This method performs sanity checks on the frame that are needed regardless |
256 // of the current state. Then, calls the HandleFrameByState() method below | 257 // of the current state. Then, calls the HandleFrameByState() method below |
257 // which performs the appropriate action(s) depending on the current state. | 258 // which performs the appropriate action(s) depending on the current state. |
258 ChannelState HandleFrame( | 259 ChannelState HandleFrame( |
259 scoped_ptr<WebSocketFrame> frame) WARN_UNUSED_RESULT; | 260 scoped_ptr<WebSocketFrame> frame) WARN_UNUSED_RESULT; |
260 | 261 |
261 // Handles a single frame depending on the current state. It's used by the | 262 // Handles a single frame depending on the current state. It's used by the |
262 // HandleFrame() method. | 263 // HandleFrame() method. |
263 ChannelState HandleFrameByState( | 264 ChannelState HandleFrameByState(const WebSocketFrameHeader::OpCode opcode, |
264 const WebSocketFrameHeader::OpCode opcode, | 265 bool final, |
265 bool final, | 266 const scoped_refptr<IOBuffer>& data_buffer, |
266 const scoped_refptr<IOBuffer>& data_buffer, | 267 uint64_t size) WARN_UNUSED_RESULT; |
267 uint64 size) WARN_UNUSED_RESULT; | |
268 | 268 |
269 // Forward a received data frame to the renderer, if connected. If | 269 // Forward a received data frame to the renderer, if connected. If |
270 // |expecting_continuation| is not equal to |expecting_to_read_continuation_|, | 270 // |expecting_continuation| is not equal to |expecting_to_read_continuation_|, |
271 // will fail the channel. Also checks the UTF-8 validity of text frames. | 271 // will fail the channel. Also checks the UTF-8 validity of text frames. |
272 ChannelState HandleDataFrame(WebSocketFrameHeader::OpCode opcode, | 272 ChannelState HandleDataFrame(WebSocketFrameHeader::OpCode opcode, |
273 bool final, | 273 bool final, |
274 const scoped_refptr<IOBuffer>& data_buffer, | 274 const scoped_refptr<IOBuffer>& data_buffer, |
275 uint64 size) WARN_UNUSED_RESULT; | 275 uint64_t size) WARN_UNUSED_RESULT; |
276 | 276 |
277 // Low-level method to send a single frame. Used for both data and control | 277 // Low-level method to send a single frame. Used for both data and control |
278 // frames. Either sends the frame immediately or buffers it to be scheduled | 278 // frames. Either sends the frame immediately or buffers it to be scheduled |
279 // when the current write finishes. |fin| and |op_code| are defined as for | 279 // when the current write finishes. |fin| and |op_code| are defined as for |
280 // SendFrame() above, except that |op_code| may also be a control frame | 280 // SendFrame() above, except that |op_code| may also be a control frame |
281 // opcode. | 281 // opcode. |
282 ChannelState SendFrameFromIOBuffer(bool fin, | 282 ChannelState SendFrameFromIOBuffer(bool fin, |
283 WebSocketFrameHeader::OpCode op_code, | 283 WebSocketFrameHeader::OpCode op_code, |
284 const scoped_refptr<IOBuffer>& buffer, | 284 const scoped_refptr<IOBuffer>& buffer, |
285 uint64 size) WARN_UNUSED_RESULT; | 285 uint64_t size) WARN_UNUSED_RESULT; |
286 | 286 |
287 // Performs the "Fail the WebSocket Connection" operation as defined in | 287 // Performs the "Fail the WebSocket Connection" operation as defined in |
288 // RFC6455. A NotifyFailure message is sent to the renderer with |message|. | 288 // RFC6455. A NotifyFailure message is sent to the renderer with |message|. |
289 // The renderer will log the message to the console but not expose it to | 289 // The renderer will log the message to the console but not expose it to |
290 // Javascript. Javascript will see a Close code of AbnormalClosure (1006) with | 290 // Javascript. Javascript will see a Close code of AbnormalClosure (1006) with |
291 // an empty reason string. If state_ is CONNECTED then a Close message is sent | 291 // an empty reason string. If state_ is CONNECTED then a Close message is sent |
292 // to the remote host containing the supplied |code| and |reason|. If the | 292 // to the remote host containing the supplied |code| and |reason|. If the |
293 // stream is open, closes it and sets state_ to CLOSED. FailChannel() always | 293 // stream is open, closes it and sets state_ to CLOSED. FailChannel() always |
294 // returns CHANNEL_DELETED. It is not valid to access any member variables or | 294 // returns CHANNEL_DELETED. It is not valid to access any member variables or |
295 // methods after calling FailChannel(). | 295 // methods after calling FailChannel(). |
296 ChannelState FailChannel(const std::string& message, | 296 ChannelState FailChannel(const std::string& message, |
297 uint16 code, | 297 uint16_t code, |
298 const std::string& reason) WARN_UNUSED_RESULT; | 298 const std::string& reason) WARN_UNUSED_RESULT; |
299 | 299 |
300 // Sends a Close frame to Start the WebSocket Closing Handshake, or to respond | 300 // Sends a Close frame to Start the WebSocket Closing Handshake, or to respond |
301 // to a Close frame from the server. As a special case, setting |code| to | 301 // to a Close frame from the server. As a special case, setting |code| to |
302 // kWebSocketErrorNoStatusReceived will create a Close frame with no payload; | 302 // kWebSocketErrorNoStatusReceived will create a Close frame with no payload; |
303 // this is symmetric with the behaviour of ParseClose. | 303 // this is symmetric with the behaviour of ParseClose. |
304 ChannelState SendClose(uint16 code, | 304 ChannelState SendClose(uint16_t code, |
305 const std::string& reason) WARN_UNUSED_RESULT; | 305 const std::string& reason) WARN_UNUSED_RESULT; |
306 | 306 |
307 // Parses a Close frame payload. If no status code is supplied, then |code| is | 307 // Parses a Close frame payload. If no status code is supplied, then |code| is |
308 // set to 1005 (No status code) with empty |reason|. If the reason text is not | 308 // set to 1005 (No status code) with empty |reason|. If the reason text is not |
309 // valid UTF-8, then |reason| is set to an empty string. If the payload size | 309 // valid UTF-8, then |reason| is set to an empty string. If the payload size |
310 // is 1, or the supplied code is not permitted to be sent over the network, | 310 // is 1, or the supplied code is not permitted to be sent over the network, |
311 // then false is returned and |message| is set to an appropriate console | 311 // then false is returned and |message| is set to an appropriate console |
312 // message. | 312 // message. |
313 bool ParseClose(const scoped_refptr<IOBuffer>& buffer, | 313 bool ParseClose(const scoped_refptr<IOBuffer>& buffer, |
314 uint64 size, | 314 uint64_t size, |
315 uint16* code, | 315 uint16_t* code, |
316 std::string* reason, | 316 std::string* reason, |
317 std::string* message); | 317 std::string* message); |
318 | 318 |
319 // Drop this channel. | 319 // Drop this channel. |
320 // If there are pending opening handshake notifications, notify them | 320 // If there are pending opening handshake notifications, notify them |
321 // before dropping. | 321 // before dropping. |
322 // | 322 // |
323 // Always returns CHANNEL_DELETED. | 323 // Always returns CHANNEL_DELETED. |
324 ChannelState DoDropChannel(bool was_clean, | 324 ChannelState DoDropChannel(bool was_clean, |
325 uint16 code, | 325 uint16_t code, |
326 const std::string& reason); | 326 const std::string& reason); |
327 | 327 |
328 // Called if the closing handshake times out. Closes the connection and | 328 // Called if the closing handshake times out. Closes the connection and |
329 // informs the |event_interface_| if appropriate. | 329 // informs the |event_interface_| if appropriate. |
330 void CloseTimeout(); | 330 void CloseTimeout(); |
331 | 331 |
332 // The URL of the remote server. | 332 // The URL of the remote server. |
333 GURL socket_url_; | 333 GURL socket_url_; |
334 | 334 |
335 // The object receiving events. | 335 // The object receiving events. |
(...skipping 30 matching lines...) Expand all Loading... |
366 // definition of quota units when necessary. | 366 // definition of quota units when necessary. |
367 int send_quota_low_water_mark_; | 367 int send_quota_low_water_mark_; |
368 // The level the quota is refreshed to when it reaches the low_water_mark | 368 // The level the quota is refreshed to when it reaches the low_water_mark |
369 // (quota units). | 369 // (quota units). |
370 int send_quota_high_water_mark_; | 370 int send_quota_high_water_mark_; |
371 // The current amount of quota that the renderer has available for sending | 371 // The current amount of quota that the renderer has available for sending |
372 // on this logical channel (quota units). | 372 // on this logical channel (quota units). |
373 int current_send_quota_; | 373 int current_send_quota_; |
374 // The remaining amount of quota that the renderer will allow us to send on | 374 // The remaining amount of quota that the renderer will allow us to send on |
375 // this logical channel (quota units). | 375 // this logical channel (quota units). |
376 uint64 current_receive_quota_; | 376 uint64_t current_receive_quota_; |
377 | 377 |
378 // Timer for the closing handshake. | 378 // Timer for the closing handshake. |
379 base::OneShotTimer close_timer_; | 379 base::OneShotTimer close_timer_; |
380 | 380 |
381 // Timeout for the closing handshake. | 381 // Timeout for the closing handshake. |
382 base::TimeDelta closing_handshake_timeout_; | 382 base::TimeDelta closing_handshake_timeout_; |
383 | 383 |
384 // Timeout for the underlying connection close after completion of closing | 384 // Timeout for the underlying connection close after completion of closing |
385 // handshake. | 385 // handshake. |
386 base::TimeDelta underlying_connection_close_timeout_; | 386 base::TimeDelta underlying_connection_close_timeout_; |
387 | 387 |
388 // Storage for the status code and reason from the time the Close frame | 388 // Storage for the status code and reason from the time the Close frame |
389 // arrives until the connection is closed and they are passed to | 389 // arrives until the connection is closed and they are passed to |
390 // OnDropChannel(). | 390 // OnDropChannel(). |
391 bool has_received_close_frame_; | 391 bool has_received_close_frame_; |
392 uint16 received_close_code_; | 392 uint16_t received_close_code_; |
393 std::string received_close_reason_; | 393 std::string received_close_reason_; |
394 | 394 |
395 // The current state of the channel. Mainly used for sanity checking, but also | 395 // The current state of the channel. Mainly used for sanity checking, but also |
396 // used to track the close state. | 396 // used to track the close state. |
397 State state_; | 397 State state_; |
398 | 398 |
399 // |notification_sender_| is owned by this object. | 399 // |notification_sender_| is owned by this object. |
400 scoped_ptr<HandshakeNotificationSender> notification_sender_; | 400 scoped_ptr<HandshakeNotificationSender> notification_sender_; |
401 | 401 |
402 // UTF-8 validator for outgoing Text messages. | 402 // UTF-8 validator for outgoing Text messages. |
(...skipping 14 matching lines...) Expand all Loading... |
417 // For UMA. The time when OnConnectSuccess() method was called and |stream_| | 417 // For UMA. The time when OnConnectSuccess() method was called and |stream_| |
418 // was set. | 418 // was set. |
419 base::TimeTicks established_on_; | 419 base::TimeTicks established_on_; |
420 | 420 |
421 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel); | 421 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel); |
422 }; | 422 }; |
423 | 423 |
424 } // namespace net | 424 } // namespace net |
425 | 425 |
426 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ | 426 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ |
OLD | NEW |