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/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
15 #include "net/base/net_export.h" | 15 #include "net/base/net_export.h" |
16 #include "net/websockets/websocket_frame.h" | 16 #include "net/websockets/websocket_frame.h" |
17 #include "net/websockets/websocket_stream.h" | 17 #include "net/websockets/websocket_stream.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 | 19 |
20 namespace net { | 20 namespace net { |
21 | 21 |
22 class GrowableIOBuffer; | 22 class BoundNetLog; |
| 23 class IOBuffer; |
23 class URLRequestContext; | 24 class URLRequestContext; |
24 class WebSocketEventInterface; | 25 class WebSocketEventInterface; |
25 class BoundNetLog; | |
26 | 26 |
27 // Transport-independent implementation of WebSockets. Implements protocol | 27 // Transport-independent implementation of WebSockets. Implements protocol |
28 // semantics that do not depend on the underlying transport. Provides the | 28 // semantics that do not depend on the underlying transport. Provides the |
29 // interface to the content layer. Some WebSocket concepts are used here without | 29 // 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 | 30 // definition; please see the RFC at http://tools.ietf.org/html/rfc6455 for |
31 // clarification. | 31 // clarification. |
32 class NET_EXPORT WebSocketChannel { | 32 class NET_EXPORT WebSocketChannel { |
33 public: | 33 public: |
34 // The type of a WebSocketStream factory callback. Must match the signature of | 34 // The type of a WebSocketStream factory callback. Must match the signature of |
35 // WebSocketStream::CreateAndConnectStream(). | 35 // WebSocketStream::CreateAndConnectStream(). |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 | 157 |
158 // Calls WebSocketStream::ReadFrames() with the appropriate arguments. | 158 // Calls WebSocketStream::ReadFrames() with the appropriate arguments. |
159 void ReadFrames(); | 159 void ReadFrames(); |
160 | 160 |
161 // Callback from WebSocketStream::ReadFrames. Handles any errors and processes | 161 // Callback from WebSocketStream::ReadFrames. Handles any errors and processes |
162 // the returned chunks appropriately to their type. |result| is a net error | 162 // the returned chunks appropriately to their type. |result| is a net error |
163 // code. If |synchronous| is true, then OnReadDone() is being called from | 163 // code. If |synchronous| is true, then OnReadDone() is being called from |
164 // within the ReadFrames() loop and does not need to call ReadFrames() itself. | 164 // within the ReadFrames() loop and does not need to call ReadFrames() itself. |
165 void OnReadDone(bool synchronous, int result); | 165 void OnReadDone(bool synchronous, int result); |
166 | 166 |
167 // Processes a single chunk that has been read from the stream. | 167 // Processes a single frame that has been read from the stream. |
168 void ProcessFrameChunk(scoped_ptr<WebSocketFrameChunk> chunk); | 168 void ProcessFrame(scoped_ptr<WebSocketFrame> frame); |
169 | |
170 // Appends |data_buffer| to |incomplete_control_frame_body_|. | |
171 void AddToIncompleteControlFrameBody( | |
172 const scoped_refptr<IOBufferWithSize>& data_buffer); | |
173 | 169 |
174 // Handles a frame that the object has received enough of to process. May call | 170 // Handles a frame that the object has received enough of to process. May call |
175 // event_interface_ methods, send responses to the server, and change the | 171 // |event_interface_| methods, send responses to the server, and change the |
176 // value of state_. | 172 // value of |state_|. |
177 void HandleFrame(const WebSocketFrameHeader::OpCode opcode, | 173 void HandleFrame(const WebSocketFrameHeader::OpCode opcode, |
178 bool is_first_chunk, | 174 bool final, |
179 bool is_final_chunk, | 175 const scoped_refptr<IOBuffer>& data_buffer, |
180 const scoped_refptr<IOBufferWithSize>& data_buffer); | 176 size_t size); |
181 | 177 |
182 // Low-level method to send a single frame. Used for both data and control | 178 // Low-level method to send a single frame. Used for both data and control |
183 // frames. Either sends the frame immediately or buffers it to be scheduled | 179 // frames. Either sends the frame immediately or buffers it to be scheduled |
184 // when the current write finishes. |fin| and |op_code| are defined as for | 180 // when the current write finishes. |fin| and |op_code| are defined as for |
185 // SendFrame() above, except that |op_code| may also be a control frame | 181 // SendFrame() above, except that |op_code| may also be a control frame |
186 // opcode. | 182 // opcode. |
187 void SendIOBufferWithSize(bool fin, | 183 void SendIOBuffer(bool fin, |
188 WebSocketFrameHeader::OpCode op_code, | 184 WebSocketFrameHeader::OpCode op_code, |
189 const scoped_refptr<IOBufferWithSize>& buffer); | 185 const scoped_refptr<IOBuffer>& buffer, |
| 186 size_t size); |
190 | 187 |
191 // Performs the "Fail the WebSocket Connection" operation as defined in | 188 // Performs the "Fail the WebSocket Connection" operation as defined in |
192 // RFC6455. The supplied code and reason are sent back to the renderer in an | 189 // RFC6455. The supplied code and reason are sent back to the renderer in an |
193 // OnDropChannel message. If state_ is CONNECTED then a Close message is sent | 190 // OnDropChannel message. If state_ is CONNECTED then a Close message is sent |
194 // to the remote host. If |expose| is SEND_REAL_ERROR then the remote host is | 191 // to the remote host. If |expose| is SEND_REAL_ERROR then the remote host is |
195 // given the same status code passed to the renderer; otherwise it is sent a | 192 // given the same status code passed to the renderer; otherwise it is sent a |
196 // fixed "Going Away" code. Resets current_frame_header_, closes the stream_, | 193 // fixed "Going Away" code. Closes the stream_ and sets state_ to CLOSED. |
197 // and sets state_ to CLOSED. | |
198 void FailChannel(ExposeError expose, uint16 code, const std::string& reason); | 194 void FailChannel(ExposeError expose, uint16 code, const std::string& reason); |
199 | 195 |
200 // Sends a Close frame to Start the WebSocket Closing Handshake, or to respond | 196 // Sends a Close frame to Start the WebSocket Closing Handshake, or to respond |
201 // to a Close frame from the server. As a special case, setting |code| to | 197 // to a Close frame from the server. As a special case, setting |code| to |
202 // kWebSocketErrorNoStatusReceived will create a Close frame with no payload; | 198 // kWebSocketErrorNoStatusReceived will create a Close frame with no payload; |
203 // this is symmetric with the behaviour of ParseClose. | 199 // this is symmetric with the behaviour of ParseClose. |
204 void SendClose(uint16 code, const std::string& reason); | 200 void SendClose(uint16 code, const std::string& reason); |
205 | 201 |
206 // Parses a Close frame. If no status code is supplied, then |code| is set to | 202 // Parses a Close frame. If no status code is supplied, then |code| is set to |
207 // 1005 (No status code) with empty |reason|. If the supplied code is | 203 // 1005 (No status code) with empty |reason|. If the supplied code is |
208 // outside the valid range, then 1002 (Protocol error) is set instead. If the | 204 // outside the valid range, then 1002 (Protocol error) is set instead. If the |
209 // reason text is not valid UTF-8, then |reason| is set to an empty string | 205 // reason text is not valid UTF-8, then |reason| is set to an empty string |
210 // instead. | 206 // instead. |
211 void ParseClose(const scoped_refptr<IOBufferWithSize>& buffer, | 207 void ParseClose(const scoped_refptr<IOBuffer>& buffer, |
| 208 size_t size, |
212 uint16* code, | 209 uint16* code, |
213 std::string* reason); | 210 std::string* reason); |
214 | 211 |
215 // The URL of the remote server. | 212 // The URL of the remote server. |
216 const GURL socket_url_; | 213 const GURL socket_url_; |
217 | 214 |
218 // The object receiving events. | 215 // The object receiving events. |
219 const scoped_ptr<WebSocketEventInterface> event_interface_; | 216 const scoped_ptr<WebSocketEventInterface> event_interface_; |
220 | 217 |
221 // The WebSocketStream on which to send and receive data. | 218 // The WebSocketStream on which to send and receive data. |
222 scoped_ptr<WebSocketStream> stream_; | 219 scoped_ptr<WebSocketStream> stream_; |
223 | 220 |
224 // A data structure containing a vector of frames to be sent and the total | 221 // A data structure containing a vector of frames to be sent and the total |
225 // number of bytes contained in the vector. | 222 // number of bytes contained in the vector. |
226 class SendBuffer; | 223 class SendBuffer; |
227 // Data that is currently pending write, or NULL if no write is pending. | 224 // Data that is currently pending write, or NULL if no write is pending. |
228 scoped_ptr<SendBuffer> data_being_sent_; | 225 scoped_ptr<SendBuffer> data_being_sent_; |
229 // Data that is queued up to write after the current write completes. | 226 // Data that is queued up to write after the current write completes. |
230 // Only non-NULL when such data actually exists. | 227 // Only non-NULL when such data actually exists. |
231 scoped_ptr<SendBuffer> data_to_send_next_; | 228 scoped_ptr<SendBuffer> data_to_send_next_; |
232 | 229 |
233 // Destination for the current call to WebSocketStream::ReadFrames | 230 // Destination for the current call to WebSocketStream::ReadFrames |
234 ScopedVector<WebSocketFrameChunk> read_frame_chunks_; | 231 ScopedVector<WebSocketFrame> read_frames_; |
235 // Frame header for the frame currently being received. Only non-NULL while we | 232 |
236 // are processing the frame. If the frame arrives in multiple chunks, it can | |
237 // remain non-NULL until additional chunks arrive. If the header of the frame | |
238 // was invalid, this is set to NULL, the channel is failed, and subsequent | |
239 // chunks of the same frame will be ignored. | |
240 scoped_ptr<WebSocketFrameHeader> current_frame_header_; | |
241 // Handle to an in-progress WebSocketStream creation request. Only non-NULL | 233 // Handle to an in-progress WebSocketStream creation request. Only non-NULL |
242 // during the connection process. | 234 // during the connection process. |
243 scoped_ptr<WebSocketStreamRequest> stream_request_; | 235 scoped_ptr<WebSocketStreamRequest> stream_request_; |
244 // Although it should rarely happen in practice, a control frame can arrive | 236 |
245 // broken into chunks. This variable provides storage for a partial control | |
246 // frame until the rest arrives. It will be NULL the rest of the time. | |
247 scoped_refptr<GrowableIOBuffer> incomplete_control_frame_body_; | |
248 // If the renderer's send quota reaches this level, it is sent a quota | 237 // If the renderer's send quota reaches this level, it is sent a quota |
249 // refresh. "quota units" are currently bytes. TODO(ricea): Update the | 238 // refresh. "quota units" are currently bytes. TODO(ricea): Update the |
250 // definition of quota units when necessary. | 239 // definition of quota units when necessary. |
251 int send_quota_low_water_mark_; | 240 int send_quota_low_water_mark_; |
252 // The level the quota is refreshed to when it reaches the low_water_mark | 241 // The level the quota is refreshed to when it reaches the low_water_mark |
253 // (quota units). | 242 // (quota units). |
254 int send_quota_high_water_mark_; | 243 int send_quota_high_water_mark_; |
255 // The current amount of quota that the renderer has available for sending | 244 // The current amount of quota that the renderer has available for sending |
256 // on this logical channel (quota units). | 245 // on this logical channel (quota units). |
257 int current_send_quota_; | 246 int current_send_quota_; |
258 | 247 |
259 // Storage for the status code and reason from the time the Close frame | 248 // Storage for the status code and reason from the time the Close frame |
260 // arrives until the connection is closed and they are passed to | 249 // arrives until the connection is closed and they are passed to |
261 // OnDropChannel(). | 250 // OnDropChannel(). |
262 uint16 closing_code_; | 251 uint16 closing_code_; |
263 std::string closing_reason_; | 252 std::string closing_reason_; |
264 | 253 |
265 // The current state of the channel. Mainly used for sanity checking, but also | 254 // The current state of the channel. Mainly used for sanity checking, but also |
266 // used to track the close state. | 255 // used to track the close state. |
267 State state_; | 256 State state_; |
268 | 257 |
269 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel); | 258 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel); |
270 }; | 259 }; |
271 | 260 |
272 } // namespace net | 261 } // namespace net |
273 | 262 |
274 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ | 263 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ |
OLD | NEW |