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