| 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 <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/compiler_specific.h" // for WARN_UNUSED_RESULT |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
| 15 #include "net/base/net_export.h" | 16 #include "net/base/net_export.h" |
| 17 #include "net/websockets/websocket_event_interface.h" |
| 16 #include "net/websockets/websocket_frame.h" | 18 #include "net/websockets/websocket_frame.h" |
| 17 #include "net/websockets/websocket_stream.h" | 19 #include "net/websockets/websocket_stream.h" |
| 18 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 19 | 21 |
| 20 namespace net { | 22 namespace net { |
| 21 | 23 |
| 22 class BoundNetLog; | 24 class BoundNetLog; |
| 23 class IOBuffer; | 25 class IOBuffer; |
| 24 class URLRequestContext; | 26 class URLRequestContext; |
| 25 class WebSocketEventInterface; | |
| 26 | 27 |
| 27 // Transport-independent implementation of WebSockets. Implements protocol | 28 // Transport-independent implementation of WebSockets. Implements protocol |
| 28 // semantics that do not depend on the underlying transport. Provides the | 29 // semantics that do not depend on the underlying transport. Provides the |
| 29 // interface to the content layer. Some WebSocket concepts are used here without | 30 // interface to the content layer. Some WebSocket concepts are used here without |
| 30 // definition; please see the RFC at http://tools.ietf.org/html/rfc6455 for | 31 // definition; please see the RFC at http://tools.ietf.org/html/rfc6455 for |
| 31 // clarification. | 32 // clarification. |
| 32 class NET_EXPORT WebSocketChannel { | 33 class NET_EXPORT WebSocketChannel { |
| 33 public: | 34 public: |
| 34 // The type of a WebSocketStream factory callback. Must match the signature of | 35 // The type of a WebSocketStream factory callback. Must match the signature of |
| 35 // WebSocketStream::CreateAndConnectStream(). | 36 // WebSocketStream::CreateAndConnectStream(). |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 | 88 |
| 88 // Starts the connection process, using a specified factory function rather | 89 // Starts the connection process, using a specified factory function rather |
| 89 // than the default. This is exposed for testing. | 90 // than the default. This is exposed for testing. |
| 90 void SendAddChannelRequestForTesting( | 91 void SendAddChannelRequestForTesting( |
| 91 const GURL& socket_url, | 92 const GURL& socket_url, |
| 92 const std::vector<std::string>& requested_protocols, | 93 const std::vector<std::string>& requested_protocols, |
| 93 const GURL& origin, | 94 const GURL& origin, |
| 94 const WebSocketStreamFactory& factory); | 95 const WebSocketStreamFactory& factory); |
| 95 | 96 |
| 96 private: | 97 private: |
| 98 // Methods which return a value of type ChannelState may delete |this|. If the |
| 99 // return value is CHANNEL_DELETED, then the caller must return without making |
| 100 // any further access to member variables or methods. |
| 101 typedef WebSocketEventInterface::ChannelState ChannelState; |
| 102 |
| 97 // The object passes through a linear progression of states from | 103 // The object passes through a linear progression of states from |
| 98 // FRESHLY_CONSTRUCTED to CLOSED, except that the SEND_CLOSED and RECV_CLOSED | 104 // FRESHLY_CONSTRUCTED to CLOSED, except that the SEND_CLOSED and RECV_CLOSED |
| 99 // states may be skipped in case of error. | 105 // states may be skipped in case of error. |
| 100 enum State { | 106 enum State { |
| 101 FRESHLY_CONSTRUCTED, | 107 FRESHLY_CONSTRUCTED, |
| 102 CONNECTING, | 108 CONNECTING, |
| 103 CONNECTED, | 109 CONNECTED, |
| 104 SEND_CLOSED, // A Close frame has been sent but not received. | 110 SEND_CLOSED, // A Close frame has been sent but not received. |
| 105 RECV_CLOSED, // Used briefly between receiving a Close frame and sending | 111 RECV_CLOSED, // Used briefly between receiving a Close frame and sending |
| 106 // the response. Once the response is sent, the state changes | 112 // the response. Once the response is sent, the state changes |
| (...skipping 21 matching lines...) Expand all Loading... |
| 128 class ConnectDelegate; | 134 class ConnectDelegate; |
| 129 | 135 |
| 130 // Starts the connection progress, using a specified factory function. | 136 // Starts the connection progress, using a specified factory function. |
| 131 void SendAddChannelRequestWithFactory( | 137 void SendAddChannelRequestWithFactory( |
| 132 const GURL& socket_url, | 138 const GURL& socket_url, |
| 133 const std::vector<std::string>& requested_protocols, | 139 const std::vector<std::string>& requested_protocols, |
| 134 const GURL& origin, | 140 const GURL& origin, |
| 135 const WebSocketStreamFactory& factory); | 141 const WebSocketStreamFactory& factory); |
| 136 | 142 |
| 137 // Success callback from WebSocketStream::CreateAndConnectStream(). Reports | 143 // Success callback from WebSocketStream::CreateAndConnectStream(). Reports |
| 138 // success to the event interface. | 144 // success to the event interface. May delete |this|. |
| 139 void OnConnectSuccess(scoped_ptr<WebSocketStream> stream); | 145 void OnConnectSuccess(scoped_ptr<WebSocketStream> stream); |
| 140 | 146 |
| 141 // Failure callback from WebSocketStream::CreateAndConnectStream(). Reports | 147 // Failure callback from WebSocketStream::CreateAndConnectStream(). Reports |
| 142 // failure to the event interface. | 148 // failure to the event interface. May delete |this|. |
| 143 void OnConnectFailure(uint16 websocket_error); | 149 void OnConnectFailure(uint16 websocket_error); |
| 144 | 150 |
| 145 // Returns true if state_ is SEND_CLOSED, CLOSE_WAIT or CLOSED. | 151 // Returns true if state_ is SEND_CLOSED, CLOSE_WAIT or CLOSED. |
| 146 bool InClosingState() const; | 152 bool InClosingState() const; |
| 147 | 153 |
| 148 // Calls WebSocketStream::WriteFrames() with the appropriate arguments | 154 // Calls WebSocketStream::WriteFrames() with the appropriate arguments |
| 149 void WriteFrames(); | 155 ChannelState WriteFrames() WARN_UNUSED_RESULT; |
| 150 | 156 |
| 151 // Callback from WebSocketStream::WriteFrames. Sends pending data or adjusts | 157 // Callback from WebSocketStream::WriteFrames. Sends pending data or adjusts |
| 152 // the send quota of the renderer channel as appropriate. |result| is a net | 158 // the send quota of the renderer channel as appropriate. |result| is a net |
| 153 // error code, usually OK. If |synchronous| is true, then OnWriteDone() is | 159 // error code, usually OK. If |synchronous| is true, then OnWriteDone() is |
| 154 // being called from within the WriteFrames() loop and does not need to call | 160 // being called from within the WriteFrames() loop and does not need to call |
| 155 // WriteFrames() itself. | 161 // WriteFrames() itself. |
| 156 void OnWriteDone(bool synchronous, int result); | 162 ChannelState OnWriteDone(bool synchronous, int result) WARN_UNUSED_RESULT; |
| 157 | 163 |
| 158 // Calls WebSocketStream::ReadFrames() with the appropriate arguments. | 164 // Calls WebSocketStream::ReadFrames() with the appropriate arguments. |
| 159 void ReadFrames(); | 165 ChannelState ReadFrames() WARN_UNUSED_RESULT; |
| 160 | 166 |
| 161 // Callback from WebSocketStream::ReadFrames. Handles any errors and processes | 167 // Callback from WebSocketStream::ReadFrames. Handles any errors and processes |
| 162 // the returned chunks appropriately to their type. |result| is a net error | 168 // the returned chunks appropriately to their type. |result| is a net error |
| 163 // code. If |synchronous| is true, then OnReadDone() is being called from | 169 // code. If |synchronous| is true, then OnReadDone() is being called from |
| 164 // within the ReadFrames() loop and does not need to call ReadFrames() itself. | 170 // within the ReadFrames() loop and does not need to call ReadFrames() itself. |
| 165 void OnReadDone(bool synchronous, int result); | 171 ChannelState OnReadDone(bool synchronous, int result) WARN_UNUSED_RESULT; |
| 166 | 172 |
| 167 // Processes a single frame that has been read from the stream. | 173 // Processes a single frame that has been read from the stream. |
| 168 void ProcessFrame(scoped_ptr<WebSocketFrame> frame); | 174 ChannelState ProcessFrame( |
| 175 scoped_ptr<WebSocketFrame> frame) WARN_UNUSED_RESULT; |
| 169 | 176 |
| 170 // Handles a frame that the object has received enough of to process. May call | 177 // Handles a frame that the object has received enough of to process. May call |
| 171 // |event_interface_| methods, send responses to the server, and change the | 178 // |event_interface_| methods, send responses to the server, and change the |
| 172 // value of |state_|. | 179 // value of |state_|. |
| 173 void HandleFrame(const WebSocketFrameHeader::OpCode opcode, | 180 ChannelState HandleFrame(const WebSocketFrameHeader::OpCode opcode, |
| 174 bool final, | 181 bool final, |
| 175 const scoped_refptr<IOBuffer>& data_buffer, | 182 const scoped_refptr<IOBuffer>& data_buffer, |
| 176 size_t size); | 183 size_t size) WARN_UNUSED_RESULT; |
| 177 | 184 |
| 178 // Low-level method to send a single frame. Used for both data and control | 185 // Low-level method to send a single frame. Used for both data and control |
| 179 // frames. Either sends the frame immediately or buffers it to be scheduled | 186 // frames. Either sends the frame immediately or buffers it to be scheduled |
| 180 // when the current write finishes. |fin| and |op_code| are defined as for | 187 // when the current write finishes. |fin| and |op_code| are defined as for |
| 181 // SendFrame() above, except that |op_code| may also be a control frame | 188 // SendFrame() above, except that |op_code| may also be a control frame |
| 182 // opcode. | 189 // opcode. |
| 183 void SendIOBuffer(bool fin, | 190 ChannelState SendIOBuffer(bool fin, |
| 184 WebSocketFrameHeader::OpCode op_code, | 191 WebSocketFrameHeader::OpCode op_code, |
| 185 const scoped_refptr<IOBuffer>& buffer, | 192 const scoped_refptr<IOBuffer>& buffer, |
| 186 size_t size); | 193 size_t size) WARN_UNUSED_RESULT; |
| 187 | 194 |
| 188 // Performs the "Fail the WebSocket Connection" operation as defined in | 195 // Performs the "Fail the WebSocket Connection" operation as defined in |
| 189 // RFC6455. The supplied code and reason are sent back to the renderer in an | 196 // RFC6455. The supplied code and reason are sent back to the renderer in an |
| 190 // OnDropChannel message. If state_ is CONNECTED then a Close message is sent | 197 // OnDropChannel message. If state_ is CONNECTED then a Close message is sent |
| 191 // to the remote host. If |expose| is SEND_REAL_ERROR then the remote host is | 198 // to the remote host. If |expose| is SEND_REAL_ERROR then the remote host is |
| 192 // given the same status code passed to the renderer; otherwise it is sent a | 199 // given the same status code passed to the renderer; otherwise it is sent a |
| 193 // fixed "Going Away" code. Closes the stream_ and sets state_ to CLOSED. | 200 // fixed "Going Away" code. Closes the stream_ and sets state_ to CLOSED. |
| 194 void FailChannel(ExposeError expose, uint16 code, const std::string& reason); | 201 // FailChannel() always returns CHANNEL_DELETED. It is not valid to access any |
| 202 // member variables or methods after calling FailChannel(). |
| 203 ChannelState FailChannel(ExposeError expose, |
| 204 uint16 code, |
| 205 const std::string& reason) WARN_UNUSED_RESULT; |
| 195 | 206 |
| 196 // Sends a Close frame to Start the WebSocket Closing Handshake, or to respond | 207 // Sends a Close frame to Start the WebSocket Closing Handshake, or to respond |
| 197 // to a Close frame from the server. As a special case, setting |code| to | 208 // to a Close frame from the server. As a special case, setting |code| to |
| 198 // kWebSocketErrorNoStatusReceived will create a Close frame with no payload; | 209 // kWebSocketErrorNoStatusReceived will create a Close frame with no payload; |
| 199 // this is symmetric with the behaviour of ParseClose. | 210 // this is symmetric with the behaviour of ParseClose. |
| 200 void SendClose(uint16 code, const std::string& reason); | 211 ChannelState SendClose(uint16 code, |
| 212 const std::string& reason) WARN_UNUSED_RESULT; |
| 201 | 213 |
| 202 // Parses a Close frame. If no status code is supplied, then |code| is set to | 214 // Parses a Close frame. If no status code is supplied, then |code| is set to |
| 203 // 1005 (No status code) with empty |reason|. If the supplied code is | 215 // 1005 (No status code) with empty |reason|. If the supplied code is |
| 204 // outside the valid range, then 1002 (Protocol error) is set instead. If the | 216 // outside the valid range, then 1002 (Protocol error) is set instead. If the |
| 205 // reason text is not valid UTF-8, then |reason| is set to an empty string | 217 // reason text is not valid UTF-8, then |reason| is set to an empty string |
| 206 // instead. | 218 // instead. |
| 207 void ParseClose(const scoped_refptr<IOBuffer>& buffer, | 219 void ParseClose(const scoped_refptr<IOBuffer>& buffer, |
| 208 size_t size, | 220 size_t size, |
| 209 uint16* code, | 221 uint16* code, |
| 210 std::string* reason); | 222 std::string* reason); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 // The current state of the channel. Mainly used for sanity checking, but also | 269 // The current state of the channel. Mainly used for sanity checking, but also |
| 258 // used to track the close state. | 270 // used to track the close state. |
| 259 State state_; | 271 State state_; |
| 260 | 272 |
| 261 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel); | 273 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel); |
| 262 }; | 274 }; |
| 263 | 275 |
| 264 } // namespace net | 276 } // namespace net |
| 265 | 277 |
| 266 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ | 278 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ |
| OLD | NEW |