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 <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 const GURL& socket_url, | 75 const GURL& socket_url, |
76 const std::vector<std::string>& requested_protocols, | 76 const std::vector<std::string>& requested_protocols, |
77 const url::Origin& origin, | 77 const url::Origin& origin, |
78 const GURL& first_party_for_cookies, | 78 const GURL& first_party_for_cookies, |
79 const std::string& additional_headers); | 79 const std::string& additional_headers); |
80 | 80 |
81 // Sends a data frame to the remote side. It is the responsibility of the | 81 // Sends a data frame to the remote side. It is the responsibility of the |
82 // caller to ensure that they have sufficient send quota to send this data, | 82 // caller to ensure that they have sufficient send quota to send this data, |
83 // otherwise the connection will be closed without sending. |fin| indicates | 83 // otherwise the connection will be closed without sending. |fin| indicates |
84 // the last frame in a message, equivalent to "FIN" as specified in section | 84 // the last frame in a message, equivalent to "FIN" as specified in section |
85 // 5.2 of RFC6455. |data| is the "Payload Data". If |op_code| is kOpCodeText, | 85 // 5.2 of RFC6455. |buffer->data()| is the "Payload Data". If |op_code| is |
86 // or it is kOpCodeContinuation and the type the message is Text, then |data| | 86 // kOpCodeText, or it is kOpCodeContinuation and the type the message is |
87 // must be a chunk of a valid UTF-8 message, however there is no requirement | 87 // Text, then |buffer->data()| must be a chunk of a valid UTF-8 message, |
88 // for |data| to be split on character boundaries. Calling SendFrame may | 88 // however there is no requirement for |buffer->data()| to be split on |
89 // result in synchronous calls to |event_interface_| which may result in this | 89 // character boundaries. Calling SendFrame may result in synchronous calls to |
90 // object being deleted. In that case, the return value will be | 90 // |event_interface_| which may result in this object being deleted. In that |
91 // CHANNEL_DELETED. | 91 // case, the return value will be CHANNEL_DELETED. |
92 ChannelState SendFrame(bool fin, | 92 ChannelState SendFrame(bool fin, |
93 WebSocketFrameHeader::OpCode op_code, | 93 WebSocketFrameHeader::OpCode op_code, |
94 const std::vector<char>& data); | 94 scoped_refptr<IOBuffer> buffer, |
| 95 size_t buffer_size); |
95 | 96 |
96 // Sends |quota| units of flow control to the remote side. If the underlying | 97 // Sends |quota| units of flow control to the remote side. If the underlying |
97 // transport has a concept of |quota|, then it permits the remote server to | 98 // transport has a concept of |quota|, then it permits the remote server to |
98 // send up to |quota| units of data. | 99 // send up to |quota| units of data. |
99 // | 100 // |
100 // Calling this function may result in synchronous calls to |event_interface_| | 101 // Calling this function may result in synchronous calls to |event_interface_| |
101 // which may result in this object being deleted. In that case, the return | 102 // which may result in this object being deleted. In that case, the return |
102 // value will be CHANNEL_DELETED. | 103 // value will be CHANNEL_DELETED. |
103 ChannelState SendFlowControl(int64_t quota) WARN_UNUSED_RESULT; | 104 ChannelState SendFlowControl(int64_t quota) WARN_UNUSED_RESULT; |
104 | 105 |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 // This method performs sanity checks on the frame that are needed regardless | 280 // This method performs sanity checks on the frame that are needed regardless |
280 // of the current state. Then, calls the HandleFrameByState() method below | 281 // of the current state. Then, calls the HandleFrameByState() method below |
281 // which performs the appropriate action(s) depending on the current state. | 282 // which performs the appropriate action(s) depending on the current state. |
282 ChannelState HandleFrame(std::unique_ptr<WebSocketFrame> frame) | 283 ChannelState HandleFrame(std::unique_ptr<WebSocketFrame> frame) |
283 WARN_UNUSED_RESULT; | 284 WARN_UNUSED_RESULT; |
284 | 285 |
285 // Handles a single frame depending on the current state. It's used by the | 286 // Handles a single frame depending on the current state. It's used by the |
286 // HandleFrame() method. | 287 // HandleFrame() method. |
287 ChannelState HandleFrameByState(const WebSocketFrameHeader::OpCode opcode, | 288 ChannelState HandleFrameByState(const WebSocketFrameHeader::OpCode opcode, |
288 bool final, | 289 bool final, |
289 const scoped_refptr<IOBuffer>& data_buffer, | 290 scoped_refptr<IOBuffer> data_buffer, |
290 uint64_t size) WARN_UNUSED_RESULT; | 291 uint64_t size) WARN_UNUSED_RESULT; |
291 | 292 |
292 // Forwards a received data frame to the renderer, if connected. If | 293 // Forwards a received data frame to the renderer, if connected. If |
293 // |expecting_continuation| is not equal to |expecting_to_read_continuation_|, | 294 // |expecting_continuation| is not equal to |expecting_to_read_continuation_|, |
294 // will fail the channel. Also checks the UTF-8 validity of text frames. | 295 // will fail the channel. Also checks the UTF-8 validity of text frames. |
295 ChannelState HandleDataFrame(WebSocketFrameHeader::OpCode opcode, | 296 ChannelState HandleDataFrame(WebSocketFrameHeader::OpCode opcode, |
296 bool final, | 297 bool final, |
297 const scoped_refptr<IOBuffer>& data_buffer, | 298 scoped_refptr<IOBuffer> data_buffer, |
298 uint64_t size) WARN_UNUSED_RESULT; | 299 uint64_t size) WARN_UNUSED_RESULT; |
299 | 300 |
300 // Handles an incoming close frame with |code| and |reason|. | 301 // Handles an incoming close frame with |code| and |reason|. |
301 ChannelState HandleCloseFrame(uint16_t code, | 302 ChannelState HandleCloseFrame(uint16_t code, |
302 const std::string& reason) WARN_UNUSED_RESULT; | 303 const std::string& reason) WARN_UNUSED_RESULT; |
303 | 304 |
304 // Responds to a closing handshake initiated by the server. | 305 // Responds to a closing handshake initiated by the server. |
305 ChannelState RespondToClosingHandshake() WARN_UNUSED_RESULT; | 306 ChannelState RespondToClosingHandshake() WARN_UNUSED_RESULT; |
306 | 307 |
307 // Low-level method to send a single frame. Used for both data and control | 308 // Low-level method to send a single frame. Used for both data and control |
308 // frames. Either sends the frame immediately or buffers it to be scheduled | 309 // frames. Either sends the frame immediately or buffers it to be scheduled |
309 // when the current write finishes. |fin| and |op_code| are defined as for | 310 // when the current write finishes. |fin| and |op_code| are defined as for |
310 // SendFrame() above, except that |op_code| may also be a control frame | 311 // SendFrame() above, except that |op_code| may also be a control frame |
311 // opcode. | 312 // opcode. |
312 ChannelState SendFrameFromIOBuffer(bool fin, | 313 ChannelState SendFrameFromIOBuffer(bool fin, |
313 WebSocketFrameHeader::OpCode op_code, | 314 WebSocketFrameHeader::OpCode op_code, |
314 const scoped_refptr<IOBuffer>& buffer, | 315 scoped_refptr<IOBuffer> buffer, |
315 uint64_t size) WARN_UNUSED_RESULT; | 316 uint64_t buffer_size) WARN_UNUSED_RESULT; |
316 | 317 |
317 // Performs the "Fail the WebSocket Connection" operation as defined in | 318 // Performs the "Fail the WebSocket Connection" operation as defined in |
318 // RFC6455. A NotifyFailure message is sent to the renderer with |message|. | 319 // RFC6455. A NotifyFailure message is sent to the renderer with |message|. |
319 // The renderer will log the message to the console but not expose it to | 320 // The renderer will log the message to the console but not expose it to |
320 // Javascript. Javascript will see a Close code of AbnormalClosure (1006) with | 321 // Javascript. Javascript will see a Close code of AbnormalClosure (1006) with |
321 // an empty reason string. If state_ is CONNECTED then a Close message is sent | 322 // an empty reason string. If state_ is CONNECTED then a Close message is sent |
322 // to the remote host containing the supplied |code| and |reason|. If the | 323 // to the remote host containing the supplied |code| and |reason|. If the |
323 // stream is open, closes it and sets state_ to CLOSED. FailChannel() always | 324 // stream is open, closes it and sets state_ to CLOSED. FailChannel() always |
324 // returns CHANNEL_DELETED. It is not valid to access any member variables or | 325 // returns CHANNEL_DELETED. It is not valid to access any member variables or |
325 // methods after calling FailChannel(). | 326 // methods after calling FailChannel(). |
326 ChannelState FailChannel(const std::string& message, | 327 ChannelState FailChannel(const std::string& message, |
327 uint16_t code, | 328 uint16_t code, |
328 const std::string& reason) WARN_UNUSED_RESULT; | 329 const std::string& reason) WARN_UNUSED_RESULT; |
329 | 330 |
330 // Sends a Close frame to Start the WebSocket Closing Handshake, or to respond | 331 // Sends a Close frame to Start the WebSocket Closing Handshake, or to respond |
331 // to a Close frame from the server. As a special case, setting |code| to | 332 // to a Close frame from the server. As a special case, setting |code| to |
332 // kWebSocketErrorNoStatusReceived will create a Close frame with no payload; | 333 // kWebSocketErrorNoStatusReceived will create a Close frame with no payload; |
333 // this is symmetric with the behaviour of ParseClose. | 334 // this is symmetric with the behaviour of ParseClose. |
334 ChannelState SendClose(uint16_t code, | 335 ChannelState SendClose(uint16_t code, |
335 const std::string& reason) WARN_UNUSED_RESULT; | 336 const std::string& reason) WARN_UNUSED_RESULT; |
336 | 337 |
337 // Parses a Close frame payload. If no status code is supplied, then |code| is | 338 // Parses a Close frame payload. If no status code is supplied, then |code| is |
338 // set to 1005 (No status code) with empty |reason|. If the reason text is not | 339 // set to 1005 (No status code) with empty |reason|. If the reason text is not |
339 // valid UTF-8, then |reason| is set to an empty string. If the payload size | 340 // valid UTF-8, then |reason| is set to an empty string. If the payload size |
340 // is 1, or the supplied code is not permitted to be sent over the network, | 341 // is 1, or the supplied code is not permitted to be sent over the network, |
341 // then false is returned and |message| is set to an appropriate console | 342 // then false is returned and |message| is set to an appropriate console |
342 // message. | 343 // message. |
343 bool ParseClose(const scoped_refptr<IOBuffer>& buffer, | 344 bool ParseClose(scoped_refptr<IOBuffer> buffer, |
344 uint64_t size, | 345 uint64_t size, |
345 uint16_t* code, | 346 uint16_t* code, |
346 std::string* reason, | 347 std::string* reason, |
347 std::string* message); | 348 std::string* message); |
348 | 349 |
349 // Drop this channel. | 350 // Drop this channel. |
350 // If there are pending opening handshake notifications, notify them | 351 // If there are pending opening handshake notifications, notify them |
351 // before dropping. | 352 // before dropping. |
352 // | 353 // |
353 // Always returns CHANNEL_DELETED. | 354 // Always returns CHANNEL_DELETED. |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 // For UMA. The time when OnConnectSuccess() method was called and |stream_| | 448 // For UMA. The time when OnConnectSuccess() method was called and |stream_| |
448 // was set. | 449 // was set. |
449 base::TimeTicks established_on_; | 450 base::TimeTicks established_on_; |
450 | 451 |
451 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel); | 452 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel); |
452 }; | 453 }; |
453 | 454 |
454 } // namespace net | 455 } // namespace net |
455 | 456 |
456 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ | 457 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ |
OLD | NEW |