[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ |
| 6 | #define NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ |
| 7 | |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 8 | #include <queue> |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "base/basictypes.h" |
| 13 | #include "base/callback.h" |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 14 | #include "base/compiler_specific.h" // for WARN_UNUSED_RESULT |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 15 | #include "base/i18n/streaming_utf8_validator.h" |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 17 | #include "base/memory/scoped_ptr.h" |
| 18 | #include "base/memory/scoped_vector.h" |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 19 | #include "base/time/time.h" |
| 20 | #include "base/timer/timer.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 21 | #include "net/base/net_export.h" |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 22 | #include "net/websockets/websocket_event_interface.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 23 | #include "net/websockets/websocket_frame.h" |
| 24 | #include "net/websockets/websocket_stream.h" |
[email protected] | 15fbdb4 | 2013-07-20 00:09:38 | [diff] [blame] | 25 | #include "url/gurl.h" |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 26 | |
[email protected] | 7824cf8 | 2014-03-13 10:22:57 | [diff] [blame] | 27 | namespace url { |
| 28 | class Origin; |
| 29 | } // namespace url |
| 30 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 31 | namespace net { |
| 32 | |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 33 | class BoundNetLog; |
| 34 | class IOBuffer; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 35 | class URLRequestContext; |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 36 | struct WebSocketHandshakeRequestInfo; |
| 37 | struct WebSocketHandshakeResponseInfo; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 38 | |
| 39 | // Transport-independent implementation of WebSockets. Implements protocol |
| 40 | // semantics that do not depend on the underlying transport. Provides the |
| 41 | // interface to the content layer. Some WebSocket concepts are used here without |
| 42 | // definition; please see the RFC at http://tools.ietf.org/html/rfc6455 for |
| 43 | // clarification. |
| 44 | class NET_EXPORT WebSocketChannel { |
| 45 | public: |
[email protected] | 969dde7 | 2013-11-13 15:59:14 | [diff] [blame] | 46 | // The type of a WebSocketStream creator callback. Must match the signature of |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 47 | // WebSocketStream::CreateAndConnectStream(). |
| 48 | typedef base::Callback<scoped_ptr<WebSocketStreamRequest>( |
| 49 | const GURL&, |
| 50 | const std::vector<std::string>&, |
[email protected] | 7824cf8 | 2014-03-13 10:22:57 | [diff] [blame] | 51 | const url::Origin&, |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 52 | URLRequestContext*, |
| 53 | const BoundNetLog&, |
[email protected] | 969dde7 | 2013-11-13 15:59:14 | [diff] [blame] | 54 | scoped_ptr<WebSocketStream::ConnectDelegate>)> WebSocketStreamCreator; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 55 | |
[email protected] | dab33eb | 2013-10-08 02:27:51 | [diff] [blame] | 56 | // Creates a new WebSocketChannel in an idle state. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 57 | // SendAddChannelRequest() must be called immediately afterwards to start the |
| 58 | // connection process. |
[email protected] | dab33eb | 2013-10-08 02:27:51 | [diff] [blame] | 59 | WebSocketChannel(scoped_ptr<WebSocketEventInterface> event_interface, |
| 60 | URLRequestContext* url_request_context); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 61 | virtual ~WebSocketChannel(); |
| 62 | |
| 63 | // Starts the connection process. |
| 64 | void SendAddChannelRequest( |
[email protected] | dab33eb | 2013-10-08 02:27:51 | [diff] [blame] | 65 | const GURL& socket_url, |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 66 | const std::vector<std::string>& requested_protocols, |
[email protected] | 7824cf8 | 2014-03-13 10:22:57 | [diff] [blame] | 67 | const url::Origin& origin); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 68 | |
| 69 | // Sends a data frame to the remote side. The frame should usually be no |
| 70 | // larger than 32KB to prevent the time required to copy the buffers from from |
| 71 | // unduly delaying other tasks that need to run on the IO thread. This method |
| 72 | // has a hard limit of 2GB. It is the responsibility of the caller to ensure |
| 73 | // that they have sufficient send quota to send this data, otherwise the |
| 74 | // connection will be closed without sending. |fin| indicates the last frame |
| 75 | // in a message, equivalent to "FIN" as specified in section 5.2 of |
| 76 | // RFC6455. |data| is the "Payload Data". If |op_code| is kOpCodeText, or it |
| 77 | // 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 | // |data| to be split on character boundaries. |
| 80 | void SendFrame(bool fin, |
| 81 | WebSocketFrameHeader::OpCode op_code, |
| 82 | const std::vector<char>& data); |
| 83 | |
| 84 | // 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 | // send up to |quota| units of data. |
| 87 | void SendFlowControl(int64 quota); |
| 88 | |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 89 | // Starts the closing handshake for a client-initiated shutdown of the |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 90 | // connection. There is no API to close the connection without a closing |
| 91 | // handshake, but destroying the WebSocketChannel object while connected will |
| 92 | // effectively do that. |code| must be in the range 1000-4999. |reason| should |
| 93 | // be a valid UTF-8 string or empty. |
| 94 | // |
| 95 | // This does *not* trigger the event OnClosingHandshake(). The caller should |
| 96 | // assume that the closing handshake has started and perform the equivalent |
| 97 | // processing to OnClosingHandshake() if necessary. |
| 98 | void StartClosingHandshake(uint16 code, const std::string& reason); |
| 99 | |
[email protected] | 969dde7 | 2013-11-13 15:59:14 | [diff] [blame] | 100 | // Starts the connection process, using a specified creator callback rather |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 101 | // than the default. This is exposed for testing. |
| 102 | void SendAddChannelRequestForTesting( |
[email protected] | dab33eb | 2013-10-08 02:27:51 | [diff] [blame] | 103 | const GURL& socket_url, |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 104 | const std::vector<std::string>& requested_protocols, |
[email protected] | 7824cf8 | 2014-03-13 10:22:57 | [diff] [blame] | 105 | const url::Origin& origin, |
[email protected] | 969dde7 | 2013-11-13 15:59:14 | [diff] [blame] | 106 | const WebSocketStreamCreator& creator); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 107 | |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 108 | // The default timout for the closing handshake is a sensible value (see |
| 109 | // kClosingHandshakeTimeoutSeconds in websocket_channel.cc). However, we can |
| 110 | // set it to a very small value for testing purposes. |
| 111 | void SetClosingHandshakeTimeoutForTesting(base::TimeDelta delay); |
| 112 | |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 113 | // Called when the stream starts the WebSocket Opening Handshake. |
| 114 | // This method is public for testing. |
| 115 | void OnStartOpeningHandshake( |
| 116 | scoped_ptr<WebSocketHandshakeRequestInfo> request); |
| 117 | |
| 118 | // Called when the stream ends the WebSocket Opening Handshake. |
| 119 | // This method is public for testing. |
| 120 | void OnFinishOpeningHandshake( |
| 121 | scoped_ptr<WebSocketHandshakeResponseInfo> response); |
| 122 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 123 | private: |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 124 | class HandshakeNotificationSender; |
| 125 | |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 126 | // The Windows implementation of std::queue requires that this declaration be |
| 127 | // visible in the header. |
| 128 | class PendingReceivedFrame { |
| 129 | public: |
| 130 | PendingReceivedFrame(bool final, |
| 131 | WebSocketFrameHeader::OpCode opcode, |
| 132 | const scoped_refptr<IOBuffer>& data, |
| 133 | size_t offset, |
| 134 | size_t size); |
| 135 | ~PendingReceivedFrame(); |
| 136 | |
| 137 | bool final() const { return final_; } |
| 138 | WebSocketFrameHeader::OpCode opcode() const { return opcode_; } |
| 139 | // ResetOpcode() to Continuation. |
| 140 | void ResetOpcode(); |
| 141 | const scoped_refptr<IOBuffer>& data() const { return data_; } |
| 142 | size_t offset() const { return offset_; } |
| 143 | size_t size() const { return size_; } |
| 144 | // Increase |offset_| by |bytes|. |
| 145 | void DidConsume(size_t bytes); |
| 146 | |
| 147 | // This object needs to be copyable and assignable, since it will be placed |
| 148 | // in a std::queue. The compiler-generated copy constructor and assignment |
| 149 | // operator will do the right thing. |
| 150 | |
| 151 | private: |
| 152 | bool final_; |
| 153 | WebSocketFrameHeader::OpCode opcode_; |
| 154 | scoped_refptr<IOBuffer> data_; |
| 155 | // Where to start reading from data_. Everything prior to offset_ has |
| 156 | // already been sent to the browser. |
| 157 | size_t offset_; |
| 158 | // The size of data_. |
| 159 | size_t size_; |
| 160 | }; |
| 161 | |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 162 | // Methods which return a value of type ChannelState may delete |this|. If the |
| 163 | // return value is CHANNEL_DELETED, then the caller must return without making |
| 164 | // any further access to member variables or methods. |
| 165 | typedef WebSocketEventInterface::ChannelState ChannelState; |
| 166 | |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 167 | // The object passes through a linear progression of states from |
| 168 | // FRESHLY_CONSTRUCTED to CLOSED, except that the SEND_CLOSED and RECV_CLOSED |
| 169 | // states may be skipped in case of error. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 170 | enum State { |
| 171 | FRESHLY_CONSTRUCTED, |
| 172 | CONNECTING, |
| 173 | CONNECTED, |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 174 | SEND_CLOSED, // A Close frame has been sent but not received. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 175 | RECV_CLOSED, // Used briefly between receiving a Close frame and sending |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 176 | // the response. Once the response is sent, the state changes |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 177 | // to CLOSED. |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 178 | CLOSE_WAIT, // The Closing Handshake has completed, but the remote server |
| 179 | // has not yet closed the connection. |
| 180 | CLOSED, // The Closing Handshake has completed and the connection |
| 181 | // has been closed; or the connection is failed. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 182 | }; |
| 183 | |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 184 | // Implementation of WebSocketStream::ConnectDelegate for |
| 185 | // WebSocketChannel. WebSocketChannel does not inherit from |
| 186 | // WebSocketStream::ConnectDelegate directly to avoid cluttering the public |
| 187 | // interface with the implementation of those methods, and because the |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 188 | // lifetime of a WebSocketChannel is longer than the lifetime of the |
| 189 | // connection process. |
| 190 | class ConnectDelegate; |
| 191 | |
[email protected] | 969dde7 | 2013-11-13 15:59:14 | [diff] [blame] | 192 | // Starts the connection process, using the supplied creator callback. |
| 193 | void SendAddChannelRequestWithSuppliedCreator( |
[email protected] | dab33eb | 2013-10-08 02:27:51 | [diff] [blame] | 194 | const GURL& socket_url, |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 195 | const std::vector<std::string>& requested_protocols, |
[email protected] | 7824cf8 | 2014-03-13 10:22:57 | [diff] [blame] | 196 | const url::Origin& origin, |
[email protected] | 969dde7 | 2013-11-13 15:59:14 | [diff] [blame] | 197 | const WebSocketStreamCreator& creator); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 198 | |
| 199 | // Success callback from WebSocketStream::CreateAndConnectStream(). Reports |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 200 | // success to the event interface. May delete |this|. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 201 | void OnConnectSuccess(scoped_ptr<WebSocketStream> stream); |
| 202 | |
| 203 | // Failure callback from WebSocketStream::CreateAndConnectStream(). Reports |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 204 | // failure to the event interface. May delete |this|. |
[email protected] | 9686820 | 2014-01-09 10:38:04 | [diff] [blame] | 205 | void OnConnectFailure(const std::string& message); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 206 | |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 207 | // Posts a task that sends pending notifications relating WebSocket Opening |
| 208 | // Handshake to the renderer. |
| 209 | void ScheduleOpeningHandshakeNotification(); |
| 210 | |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame^] | 211 | // Sets |state_| to |new_state| and updates UMA if necessary. |
| 212 | void SetState(State new_state); |
| 213 | |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 214 | // Returns true if state_ is SEND_CLOSED, CLOSE_WAIT or CLOSED. |
| 215 | bool InClosingState() const; |
| 216 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 217 | // Calls WebSocketStream::WriteFrames() with the appropriate arguments |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 218 | ChannelState WriteFrames() WARN_UNUSED_RESULT; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 219 | |
| 220 | // Callback from WebSocketStream::WriteFrames. Sends pending data or adjusts |
| 221 | // the send quota of the renderer channel as appropriate. |result| is a net |
| 222 | // error code, usually OK. If |synchronous| is true, then OnWriteDone() is |
| 223 | // being called from within the WriteFrames() loop and does not need to call |
| 224 | // WriteFrames() itself. |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 225 | ChannelState OnWriteDone(bool synchronous, int result) WARN_UNUSED_RESULT; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 226 | |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 227 | // Calls WebSocketStream::ReadFrames() with the appropriate arguments. Stops |
| 228 | // calling ReadFrames if current_receive_quota_ is 0. |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 229 | ChannelState ReadFrames() WARN_UNUSED_RESULT; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 230 | |
| 231 | // Callback from WebSocketStream::ReadFrames. Handles any errors and processes |
| 232 | // the returned chunks appropriately to their type. |result| is a net error |
| 233 | // code. If |synchronous| is true, then OnReadDone() is being called from |
| 234 | // within the ReadFrames() loop and does not need to call ReadFrames() itself. |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 235 | ChannelState OnReadDone(bool synchronous, int result) WARN_UNUSED_RESULT; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 236 | |
[email protected] | 8b3d14e | 2014-02-13 14:24:28 | [diff] [blame] | 237 | // Handles a single frame that the object has received enough of to process. |
| 238 | // May call |event_interface_| methods, send responses to the server, and |
| 239 | // change the value of |state_|. |
| 240 | // |
| 241 | // This method performs sanity checks on the frame that are needed regardless |
| 242 | // of the current state. Then, calls the HandleFrameByState() method below |
| 243 | // which performs the appropriate action(s) depending on the current state. |
| 244 | ChannelState HandleFrame( |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 245 | scoped_ptr<WebSocketFrame> frame) WARN_UNUSED_RESULT; |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 246 | |
[email protected] | 8b3d14e | 2014-02-13 14:24:28 | [diff] [blame] | 247 | // Handles a single frame depending on the current state. It's used by the |
| 248 | // HandleFrame() method. |
| 249 | ChannelState HandleFrameByState( |
| 250 | const WebSocketFrameHeader::OpCode opcode, |
| 251 | bool final, |
| 252 | const scoped_refptr<IOBuffer>& data_buffer, |
| 253 | size_t size) WARN_UNUSED_RESULT; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 254 | |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 255 | // Forward a received data frame to the renderer, if connected. If |
| 256 | // |expecting_continuation| is not equal to |expecting_to_read_continuation_|, |
| 257 | // will fail the channel. Also checks the UTF-8 validity of text frames. |
[email protected] | 326b8fb | 2014-02-21 21:14:00 | [diff] [blame] | 258 | ChannelState HandleDataFrame(WebSocketFrameHeader::OpCode opcode, |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 259 | bool final, |
| 260 | const scoped_refptr<IOBuffer>& data_buffer, |
[email protected] | 326b8fb | 2014-02-21 21:14:00 | [diff] [blame] | 261 | size_t size) WARN_UNUSED_RESULT; |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 262 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 263 | // Low-level method to send a single frame. Used for both data and control |
| 264 | // frames. Either sends the frame immediately or buffers it to be scheduled |
| 265 | // when the current write finishes. |fin| and |op_code| are defined as for |
| 266 | // SendFrame() above, except that |op_code| may also be a control frame |
| 267 | // opcode. |
[email protected] | a691b6c3 | 2014-03-24 16:09:08 | [diff] [blame] | 268 | ChannelState SendFrameFromIOBuffer(bool fin, |
| 269 | WebSocketFrameHeader::OpCode op_code, |
| 270 | const scoped_refptr<IOBuffer>& buffer, |
| 271 | size_t size) WARN_UNUSED_RESULT; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 272 | |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 273 | // Performs the "Fail the WebSocket Connection" operation as defined in |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 274 | // RFC6455. A NotifyFailure message is sent to the renderer with |message|. |
| 275 | // The renderer will log the message to the console but not expose it to |
| 276 | // Javascript. Javascript will see a Close code of AbnormalClosure (1006) with |
| 277 | // an empty reason string. If state_ is CONNECTED then a Close message is sent |
| 278 | // to the remote host containing the supplied |code| and |reason|. If the |
| 279 | // stream is open, closes it and sets state_ to CLOSED. FailChannel() always |
| 280 | // returns CHANNEL_DELETED. It is not valid to access any member variables or |
| 281 | // methods after calling FailChannel(). |
| 282 | ChannelState FailChannel(const std::string& message, |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 283 | uint16 code, |
| 284 | const std::string& reason) WARN_UNUSED_RESULT; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 285 | |
| 286 | // Sends a Close frame to Start the WebSocket Closing Handshake, or to respond |
[email protected] | c0d29c2 | 2013-07-26 20:40:41 | [diff] [blame] | 287 | // to a Close frame from the server. As a special case, setting |code| to |
| 288 | // kWebSocketErrorNoStatusReceived will create a Close frame with no payload; |
| 289 | // this is symmetric with the behaviour of ParseClose. |
[email protected] | f485985e | 2013-10-24 13:47:44 | [diff] [blame] | 290 | ChannelState SendClose(uint16 code, |
| 291 | const std::string& reason) WARN_UNUSED_RESULT; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 292 | |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 293 | // Parses a Close frame payload. If no status code is supplied, then |code| is |
| 294 | // set to 1005 (No status code) with empty |reason|. If the reason text is not |
| 295 | // valid UTF-8, then |reason| is set to an empty string. If the payload size |
| 296 | // is 1, or the supplied code is not permitted to be sent over the network, |
| 297 | // then false is returned and |message| is set to an appropriate console |
| 298 | // message. |
| 299 | bool ParseClose(const scoped_refptr<IOBuffer>& buffer, |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 300 | size_t size, |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 301 | uint16* code, |
[email protected] | ea56b98 | 2014-01-27 03:21:03 | [diff] [blame] | 302 | std::string* reason, |
| 303 | std::string* message); |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 304 | |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 305 | // Drop this channel. |
| 306 | // If there are pending opening handshake notifications, notify them |
| 307 | // before dropping. |
[email protected] | 3d563769 | 2014-03-19 16:48:22 | [diff] [blame] | 308 | // |
| 309 | // Always returns CHANNEL_DELETED. |
[email protected] | 86ec5550 | 2014-02-10 13:16:16 | [diff] [blame] | 310 | ChannelState DoDropChannel(bool was_clean, |
| 311 | uint16 code, |
| 312 | const std::string& reason); |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 313 | |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 314 | // Called if the closing handshake times out. Closes the connection and |
| 315 | // informs the |event_interface_| if appropriate. |
| 316 | void CloseTimeout(); |
| 317 | |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 318 | // The URL of the remote server. |
[email protected] | dab33eb | 2013-10-08 02:27:51 | [diff] [blame] | 319 | GURL socket_url_; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 320 | |
| 321 | // The object receiving events. |
| 322 | const scoped_ptr<WebSocketEventInterface> event_interface_; |
| 323 | |
[email protected] | 969dde7 | 2013-11-13 15:59:14 | [diff] [blame] | 324 | // The URLRequestContext to pass to the WebSocketStream creator. |
[email protected] | dab33eb | 2013-10-08 02:27:51 | [diff] [blame] | 325 | URLRequestContext* const url_request_context_; |
| 326 | |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 327 | // The WebSocketStream on which to send and receive data. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 328 | scoped_ptr<WebSocketStream> stream_; |
| 329 | |
| 330 | // A data structure containing a vector of frames to be sent and the total |
| 331 | // number of bytes contained in the vector. |
| 332 | class SendBuffer; |
| 333 | // Data that is currently pending write, or NULL if no write is pending. |
| 334 | scoped_ptr<SendBuffer> data_being_sent_; |
| 335 | // Data that is queued up to write after the current write completes. |
| 336 | // Only non-NULL when such data actually exists. |
| 337 | scoped_ptr<SendBuffer> data_to_send_next_; |
| 338 | |
| 339 | // Destination for the current call to WebSocketStream::ReadFrames |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 340 | ScopedVector<WebSocketFrame> read_frames_; |
| 341 | |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 342 | // Frames that have been read but not yet forwarded to the renderer due to |
| 343 | // lack of quota. |
| 344 | std::queue<PendingReceivedFrame> pending_received_frames_; |
| 345 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 346 | // Handle to an in-progress WebSocketStream creation request. Only non-NULL |
| 347 | // during the connection process. |
| 348 | scoped_ptr<WebSocketStreamRequest> stream_request_; |
[email protected] | 2f5d9f6 | 2013-09-26 12:14:28 | [diff] [blame] | 349 | |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 350 | // If the renderer's send quota reaches this level, it is sent a quota |
| 351 | // refresh. "quota units" are currently bytes. TODO(ricea): Update the |
| 352 | // definition of quota units when necessary. |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 353 | int send_quota_low_water_mark_; |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 354 | // The level the quota is refreshed to when it reaches the low_water_mark |
| 355 | // (quota units). |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 356 | int send_quota_high_water_mark_; |
| 357 | // The current amount of quota that the renderer has available for sending |
| 358 | // on this logical channel (quota units). |
| 359 | int current_send_quota_; |
[email protected] | 4256dbb | 2014-03-24 15:39:36 | [diff] [blame] | 360 | // The remaining amount of quota that the renderer will allow us to send on |
| 361 | // this logical channel (quota units). |
| 362 | int current_receive_quota_; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 363 | |
[email protected] | 3a26676 | 2013-10-23 08:15:10 | [diff] [blame] | 364 | // Timer for the closing handshake. |
| 365 | base::OneShotTimer<WebSocketChannel> timer_; |
| 366 | |
| 367 | // Timeout for the closing handshake. |
| 368 | base::TimeDelta timeout_; |
| 369 | |
[email protected] | caab2cc | 2013-08-27 10:24:37 | [diff] [blame] | 370 | // Storage for the status code and reason from the time the Close frame |
| 371 | // arrives until the connection is closed and they are passed to |
| 372 | // OnDropChannel(). |
[email protected] | 8b3d14e | 2014-02-13 14:24:28 | [diff] [blame] | 373 | uint16 received_close_code_; |
| 374 | std::string received_close_reason_; |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 375 | |
| 376 | // The current state of the channel. Mainly used for sanity checking, but also |
| 377 | // used to track the close state. |
| 378 | State state_; |
| 379 | |
[email protected] | cd48ed1 | 2014-01-22 14:34:22 | [diff] [blame] | 380 | // |notification_sender_| is owned by this object. |
| 381 | scoped_ptr<HandshakeNotificationSender> notification_sender_; |
| 382 | |
[email protected] | 48cc692 | 2014-02-10 14:20:48 | [diff] [blame] | 383 | // UTF-8 validator for outgoing Text messages. |
| 384 | base::StreamingUtf8Validator outgoing_utf8_validator_; |
| 385 | bool sending_text_message_; |
| 386 | |
| 387 | // UTF-8 validator for incoming Text messages. |
| 388 | base::StreamingUtf8Validator incoming_utf8_validator_; |
| 389 | bool receiving_text_message_; |
| 390 | |
[email protected] | 4e4bbaae7e | 2014-02-19 08:28:53 | [diff] [blame] | 391 | // True if we are in the middle of receiving a message. |
| 392 | bool expecting_to_handle_continuation_; |
| 393 | |
[email protected] | 326b8fb | 2014-02-21 21:14:00 | [diff] [blame] | 394 | // True if we have already sent the type (Text or Binary) of the current |
| 395 | // message to the renderer. This can be false if the message is empty so far. |
| 396 | bool initial_frame_forwarded_; |
| 397 | |
[email protected] | 09ef6736 | 2014-04-24 08:48:58 | [diff] [blame^] | 398 | // For UMA. The time when OnConnectSuccess() method was called and |stream_| |
| 399 | // was set. |
| 400 | base::TimeTicks established_on_; |
| 401 | |
[email protected] | 999bcaa | 2013-07-17 13:42:54 | [diff] [blame] | 402 | DISALLOW_COPY_AND_ASSIGN(WebSocketChannel); |
| 403 | }; |
| 404 | |
| 405 | } // namespace net |
| 406 | |
| 407 | #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ |