| OLD | NEW |
| (Empty) | |
| 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 |
| 8 #include <string> |
| 9 #include <vector> |
| 10 |
| 11 #include "base/basictypes.h" |
| 12 #include "base/callback.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/scoped_vector.h" |
| 15 #include "googleurl/src/gurl.h" |
| 16 #include "net/base/net_export.h" |
| 17 #include "net/websockets/websocket_frame.h" |
| 18 #include "net/websockets/websocket_stream.h" |
| 19 |
| 20 namespace net { |
| 21 |
| 22 class URLRequestContext; |
| 23 class WebSocketEventInterface; |
| 24 |
| 25 // Transport-independent implementation of WebSockets. Implements protocol |
| 26 // semantics that do not depend on the underlying transport. Provides the |
| 27 // interface to the content layer. Some WebSocket concepts are used here without |
| 28 // definition; please see the RFC at http://tools.ietf.org/html/rfc6455 for |
| 29 // clarification. |
| 30 class NET_EXPORT WebSocketChannel { |
| 31 public: |
| 32 // The type of a WebSocketStream factory callback. Must match the signature of |
| 33 // WebSocketStream::CreateAndConnectStream(). |
| 34 typedef base::Callback<scoped_ptr<WebSocketStreamRequest>( |
| 35 const GURL&, |
| 36 const std::vector<std::string>&, |
| 37 const GURL&, |
| 38 URLRequestContext*, |
| 39 const BoundNetLog&, |
| 40 scoped_ptr<WebSocketStream::ConnectDelegate>)> WebSocketStreamFactory; |
| 41 |
| 42 // Creates a new WebSocketChannel with the specified parameters. |
| 43 // SendAddChannelRequest() must be called immediately afterwards to start the |
| 44 // connection process. |
| 45 WebSocketChannel(const GURL& socket_url, |
| 46 scoped_ptr<WebSocketEventInterface> event_interface); |
| 47 virtual ~WebSocketChannel(); |
| 48 |
| 49 // Starts the connection process. |
| 50 void SendAddChannelRequest( |
| 51 const std::vector<std::string>& requested_protocols, |
| 52 const GURL& origin, |
| 53 URLRequestContext* url_request_context); |
| 54 |
| 55 // Sends a data frame to the remote side. The frame should usually be no |
| 56 // larger than 32KB to prevent the time required to copy the buffers from from |
| 57 // unduly delaying other tasks that need to run on the IO thread. This method |
| 58 // has a hard limit of 2GB. It is the responsibility of the caller to ensure |
| 59 // that they have sufficient send quota to send this data, otherwise the |
| 60 // connection will be closed without sending. |fin| indicates the last frame |
| 61 // in a message, equivalent to "FIN" as specified in section 5.2 of |
| 62 // RFC6455. |data| is the "Payload Data". If |op_code| is kOpCodeText, or it |
| 63 // is kOpCodeContinuation and the type the message is Text, then |data| must |
| 64 // be a chunk of a valid UTF-8 message, however there is no requirement for |
| 65 // |data| to be split on character boundaries. |
| 66 void SendFrame(bool fin, |
| 67 WebSocketFrameHeader::OpCode op_code, |
| 68 const std::vector<char>& data); |
| 69 |
| 70 // Sends |quota| units of flow control to the remote side. If the underlying |
| 71 // transport has a concept of |quota|, then it permits the remote server to |
| 72 // send up to |quota| units of data. |
| 73 void SendFlowControl(int64 quota); |
| 74 |
| 75 // Start the closing handshake for a client-initiated shutdown of the |
| 76 // connection. There is no API to close the connection without a closing |
| 77 // handshake, but destroying the WebSocketChannel object while connected will |
| 78 // effectively do that. |code| must be in the range 1000-4999. |reason| should |
| 79 // be a valid UTF-8 string or empty. |
| 80 // |
| 81 // This does *not* trigger the event OnClosingHandshake(). The caller should |
| 82 // assume that the closing handshake has started and perform the equivalent |
| 83 // processing to OnClosingHandshake() if necessary. |
| 84 void StartClosingHandshake(uint16 code, const std::string& reason); |
| 85 |
| 86 // Starts the connection process, using a specified factory function rather |
| 87 // than the default. This is exposed for testing. |
| 88 void SendAddChannelRequestForTesting( |
| 89 const std::vector<std::string>& requested_protocols, |
| 90 const GURL& origin, |
| 91 URLRequestContext* url_request_context, |
| 92 const WebSocketStreamFactory& factory); |
| 93 |
| 94 private: |
| 95 // We have a simple linear progression of states from FRESHLY_CONSTRUCTED to |
| 96 // CLOSED, except that the SEND_CLOSED and RECV_CLOSED states may be skipped |
| 97 // in case of error. |
| 98 enum State { |
| 99 FRESHLY_CONSTRUCTED, |
| 100 CONNECTING, |
| 101 CONNECTED, |
| 102 SEND_CLOSED, // We have sent a Close frame but not received a Close frame. |
| 103 RECV_CLOSED, // Used briefly between receiving a Close frame and sending |
| 104 // the response. Once we have responded, the state changes |
| 105 // to CLOSED. |
| 106 CLOSED, // The Closing Handshake has completed or the connection is failed. |
| 107 }; |
| 108 |
| 109 // When failing a channel, we may or may not want to send the real reason for |
| 110 // failing to the remote server. This enum is used by FailChannel() to |
| 111 // choose. |
| 112 enum ExposeError { |
| 113 SEND_REAL_ERROR, |
| 114 SEND_GOING_AWAY, |
| 115 }; |
| 116 |
| 117 // Our implementation of WebSocketStream::ConnectDelegate. We do not inherit |
| 118 // from WebSocketStream::ConnectDelegate directly to avoid cluttering our |
| 119 // public interface with the implementation of those methods, and because the |
| 120 // lifetime of a WebSocketChannel is longer than the lifetime of the |
| 121 // connection process. |
| 122 class ConnectDelegate; |
| 123 |
| 124 // Starts the connection progress, using a specified factory function. |
| 125 void SendAddChannelRequestWithFactory( |
| 126 const std::vector<std::string>& requested_protocols, |
| 127 const GURL& origin, |
| 128 URLRequestContext* url_request_context, |
| 129 const WebSocketStreamFactory& factory); |
| 130 |
| 131 // Success callback from WebSocketStream::CreateAndConnectStream(). Reports |
| 132 // success to the event interface. |
| 133 void OnConnectSuccess(scoped_ptr<WebSocketStream> stream); |
| 134 |
| 135 // Failure callback from WebSocketStream::CreateAndConnectStream(). Reports |
| 136 // failure to the event interface. |
| 137 void OnConnectFailure(uint16 websocket_error); |
| 138 |
| 139 // Calls WebSocketStream::WriteFrames() with the appropriate arguments |
| 140 void WriteFrames(); |
| 141 |
| 142 // Callback from WebSocketStream::WriteFrames. Sends pending data or adjusts |
| 143 // the send quota of the renderer channel as appropriate. |result| is a net |
| 144 // error code, usually OK. If |synchronous| is true, then OnWriteDone() is |
| 145 // being called from within the WriteFrames() loop and does not need to call |
| 146 // WriteFrames() itself. |
| 147 void OnWriteDone(bool synchronous, int result); |
| 148 |
| 149 // Calls WebSocketStream::ReadFrames() with the appropriate arguments. |
| 150 void ReadFrames(); |
| 151 |
| 152 // Callback from WebSocketStream::ReadFrames. Handles any errors and processes |
| 153 // the returned chunks appropriately to their type. |result| is a net error |
| 154 // code. If |synchronous| is true, then OnReadDone() is being called from |
| 155 // within the ReadFrames() loop and does not need to call ReadFrames() itself. |
| 156 void OnReadDone(bool synchronous, int result); |
| 157 |
| 158 // Processes a single chunk that has been read from the stream. |
| 159 void ProcessFrameChunk(scoped_ptr<WebSocketFrameChunk> chunk); |
| 160 |
| 161 // Handle a frame that we have received enough of to process. May call |
| 162 // event_interface_ methods, send responses to the server, and change the |
| 163 // value of state_. |
| 164 void HandleFrame(const WebSocketFrameHeader::OpCode opcode, |
| 165 bool is_first_chunk, |
| 166 bool is_final_chunk, |
| 167 const scoped_refptr<IOBufferWithSize>& data_buffer); |
| 168 |
| 169 // Low-level method to send a single frame. Used for both data and control |
| 170 // frames. Either sends the frame immediately or buffers it to be scheduled |
| 171 // when the current write finishes. |fin| and |op_code| are defined as for |
| 172 // SendFrame() above, except that |op_code| may also be a control frame |
| 173 // opcode. |
| 174 void SendIOBufferWithSize(bool fin, |
| 175 WebSocketFrameHeader::OpCode op_code, |
| 176 const scoped_refptr<IOBufferWithSize>& buffer); |
| 177 |
| 178 // Perform the "Fail the WebSocket Connection" operation as defined in |
| 179 // RFC6455. The supplied code and reason are sent back to the renderer in an |
| 180 // OnDropChannel message. If state_ is CONNECTED then a Close message is sent |
| 181 // to the remote host. If |expose| is SEND_REAL_ERROR then the remote host is |
| 182 // given the same status code we gave the renderer; otherwise it is sent a |
| 183 // fixed "Going Away" code. Resets current_frame_header_, closes the |
| 184 // stream_, and sets state_ to CLOSED. |
| 185 void FailChannel(ExposeError expose, uint16 code, const std::string& reason); |
| 186 |
| 187 // Sends a Close frame to Start the WebSocket Closing Handshake, or to respond |
| 188 // to a Close frame from the server. |
| 189 void SendClose(uint16 code, const std::string& reason); |
| 190 |
| 191 // Parses a Close frame. If no status code is supplied, then |code| is set to |
| 192 // 1005 (No status code) with empty |reason|. If the supplied code is |
| 193 // outside the valid range, then 1002 (Protocol error) is set instead. If the |
| 194 // reason text is not valid UTF-8, then |reason| is set to an empty string |
| 195 // instead. |
| 196 void ParseClose(const scoped_refptr<IOBufferWithSize>& buffer, |
| 197 uint16* code, |
| 198 std::string* reason); |
| 199 |
| 200 // The URL to which we connect. |
| 201 const GURL socket_url_; |
| 202 |
| 203 // The object receiving events. |
| 204 const scoped_ptr<WebSocketEventInterface> event_interface_; |
| 205 |
| 206 // The WebSocketStream to which we are sending/receiving data. |
| 207 scoped_ptr<WebSocketStream> stream_; |
| 208 |
| 209 // A data structure containing a vector of frames to be sent and the total |
| 210 // number of bytes contained in the vector. |
| 211 class SendBuffer; |
| 212 // Data that is currently pending write, or NULL if no write is pending. |
| 213 scoped_ptr<SendBuffer> data_being_sent_; |
| 214 // Data that is queued up to write after the current write completes. |
| 215 // Only non-NULL when such data actually exists. |
| 216 scoped_ptr<SendBuffer> data_to_send_next_; |
| 217 |
| 218 // Destination for the current call to WebSocketStream::ReadFrames |
| 219 ScopedVector<WebSocketFrameChunk> read_frame_chunks_; |
| 220 // Frame header for the frame currently being received. Only non-NULL while we |
| 221 // are processing the frame. If the frame arrives in multiple chunks, can |
| 222 // remain non-NULL while we wait for additional chunks to arrive. If the |
| 223 // header of the frame was invalid, this is set to NULL, the channel is |
| 224 // failed, and subsequent chunks of the same frame will be ignored. |
| 225 scoped_ptr<WebSocketFrameHeader> current_frame_header_; |
| 226 // Handle to an in-progress WebSocketStream creation request. Only non-NULL |
| 227 // during the connection process. |
| 228 scoped_ptr<WebSocketStreamRequest> stream_request_; |
| 229 // Although it will almost never happen in practice, we can be passed an |
| 230 // incomplete control frame, in which case we need to keep the data around |
| 231 // long enough to reassemble it. This variable will be NULL the rest of the |
| 232 // time. |
| 233 scoped_refptr<IOBufferWithSize> incomplete_control_frame_body_; |
| 234 // The point at which we give the renderer a quota refresh (quota units). |
| 235 // "quota units" are currently bytes. TODO(ricea): Update the definition of |
| 236 // quota units when necessary. |
| 237 int send_quota_low_water_mark_; |
| 238 // The amount which we refresh the quota to when it reaches the |
| 239 // low_water_mark (quota units). |
| 240 int send_quota_high_water_mark_; |
| 241 // The current amount of quota that the renderer has available for sending |
| 242 // on this logical channel (quota units). |
| 243 int current_send_quota_; |
| 244 |
| 245 // Storage for the status code and reason from the time we receive the Close |
| 246 // frame until the connection is closed and we can call OnDropChannel(). |
| 247 uint16 closing_code_; |
| 248 std::string closing_reason_; |
| 249 |
| 250 // The current state of the channel. Mainly used for sanity checking, but also |
| 251 // used to track the close state. |
| 252 State state_; |
| 253 |
| 254 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel); |
| 255 }; |
| 256 |
| 257 } // namespace net |
| 258 |
| 259 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ |
| OLD | NEW |