Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(128)

Side by Side Diff: net/websockets/websocket_channel.h

Issue 12764006: WebSocketChannel implementation (Closed) Base URL: http://git.chromium.org/chromium/src.git@web_socket_dispatcher
Patch Set: Comment clarifications and formatting tweak Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.
szym 2013/07/09 15:59:51 Add link to the RFC: http://tools.ietf.org/html/rf
Adam Rice 2013/07/11 13:28:17 Done.
28 class NET_EXPORT WebSocketChannel {
29 public:
30 // Creates a new WebSocketChannel with the specified parameters.
31 // SendAddChannelRequest() must be called immediately afterwards to start the
32 // connection process.
33 WebSocketChannel(const GURL& socket_url,
34 scoped_ptr<WebSocketEventInterface> event_interface);
35 virtual ~WebSocketChannel();
36
37 // Starts the connection process.
38 void SendAddChannelRequest(
39 const std::vector<std::string>& requested_protocols,
40 const GURL& origin,
41 URLRequestContext* url_request_context);
42
43 // Sends a data frame to the remote side. The frame should usually be no
44 // larger than 32KB to avoid blocking the IO thread. This method has a hard
szym 2013/07/09 15:59:51 Why would this block the IO thread? Do you mean th
Adam Rice 2013/07/11 13:28:17 I am mostly talking about the memory copy overhead
45 // limit of 2GB. It is the responsibility of the caller to ensure that they
46 // have sufficient send quota to send this data, otherwise the connection will
47 // be closed without sending.
48 void SendFrame(bool fin,
49 WebSocketFrameHeader::OpCode op_code,
50 const std::vector<char>& data);
51
52 // Sends "quota" units of flow control to the remote side. If the underlying
szym 2013/07/09 15:59:51 nit: Use pipes, i.e.: Sends |quota| units. Are th
Adam Rice 2013/07/11 13:28:17 Currently they are implemented as bytes. When we h
53 // transport has a concept of "quota", then it permits the remote server to
54 // send up to "quota" units of data.
55 void SendFlowControl(int64 quota);
56
57 // Start the closing handshake for a client-initiated shutdown of the
58 // connection. There is no API to close the connection without a closing
59 // handshake, but destroying the WebSocketChannel object while connected will
60 // effectively do that. |code| must be in the range 1000-4999. |reason| should
61 // be a valid UTF-8 string or empty.
62 //
63 // This does *not* trigger the event OnClosingHandshake(). The caller should
64 // assume that the closing handshake has started and perform the equivalent
65 // processing to OnClosingHandshake() if necessary.
66 void StartClosingHandshake(uint16 code, const std::string& reason);
67
68 // Starts the connection process, using a different factory function from the
69 // default. This is used for testing.
szym 2013/07/09 15:59:51 Change "a different factory" to "specified factory
Adam Rice 2013/07/11 13:28:17 That is better, thank you.
70 void SendAddChannelRequestWithFactory(
71 const std::vector<std::string>& requested_protocols,
72 const GURL& origin,
73 URLRequestContext* url_request_context,
74 base::Callback<scoped_ptr<WebSocketStreamRequest>(
szym 2013/07/09 15:59:51 Add a typedef for this Callback type, even if it's
Adam Rice 2013/07/11 13:28:17 Done.
75 const GURL&,
76 const std::vector<std::string>&,
77 const GURL&,
78 URLRequestContext*,
79 const BoundNetLog&,
80 scoped_ptr<WebSocketStream::ConnectDelegate>)> factory);
81
82 private:
83 // We have a simple linear progression of states from FRESHLY_CONSTRUCTED to
84 // CLOSED, except that the SEND_CLOSED and RECV_CLOSED states may be skipped
85 // in case of error.
86 enum State {
87 FRESHLY_CONSTRUCTED,
88 CONNECTING,
89 CONNECTED,
90 SEND_CLOSED, // We have sent a Close frame but not received a Close frame.
91 RECV_CLOSED, // Used briefly between receiving a Close frame and sending
92 // the response. Once we have responded, the state changes
93 // to CLOSED.
94 CLOSED, // The Closing Handshake has completed or the connection is failed.
95 };
96
97 // When failing a channel, we may or may not want to send the real reason for
98 // failing to the remote server. This enum is used by FailChannel() to
99 // choose.
100 enum ExposeError {
101 SEND_REAL_ERROR,
102 SEND_GOING_AWAY,
103 };
104
105 // Our implementation of WebSocketStream::ConnectDelegate. We do not inherit
106 // from WebSocketStream::ConnectDelegate directly to avoid cluttering our
107 // public interface with the implementation of those methods, and because the
108 // lifetime of a WebSocketChannel is longer than the lifetime of the
109 // connection process.
110 class ConnectDelegate;
111
112 // Success callback from WebSocketStream::CreateAndConnectStream(). Reports
113 // success to the event interface.
114 void OnConnectSuccess(scoped_ptr<WebSocketStream> stream);
115
116 // Failure callback from WebSocketStream::CreateAndConnectStream(). Reports
117 // failure to the event interface.
118 void OnConnectFailure(uint16 websocket_error);
119
120 // Calls WebSocketStream::WriteFrames() with the appropriate arguments
121 void WriteFrames();
122
123 // Callback from WebSocketStream::WriteFrames. Sends pending data or adjusts
124 // the send quota of the renderer channel as appropriate. |result| is a met
125 // error code, usually OK. If |synchronous| is true, then OnWriteDone() is
126 // being called from within the WriteFrames() loop and does not need to call
127 // WriteFrames() itself.
128 void OnWriteDone(bool synchronous, int result);
129
130 // Calls WebSocketStream::ReadFrames() with the appropriate arguments.
131 void ReadFrames();
132
133 // Callback from WebSocketStream::ReadFrames. Handles any errors and processes
134 // the returned chunks appropriately to their type. If |synchronous| is true,
135 // is being called directly from within ReadFrames(). |result| is a net error
szym 2013/07/09 15:59:51 Since this is also called from a loop within ReadF
Adam Rice 2013/07/11 13:28:17 Done.
136 // code.
137 void OnReadDone(bool synchronous, int result);
138
139 // Processes a single chunk that has been read from the stream.
140 void ProcessFrameChunk(scoped_ptr<WebSocketFrameChunk> chunk);
141
142 // Handle a frame that we have received enough of to process. May call
143 // event_interface_ methods, send responses to the server, and change the
144 // value of state_.
145 void HandleFrame(const WebSocketFrameHeader::OpCode opcode,
146 const bool is_first_chunk,
147 const bool is_final_chunk,
szym 2013/07/09 15:59:51 remove consts from variables passed by value
Adam Rice 2013/07/11 13:28:17 Done.
148 const scoped_refptr<IOBufferWithSize>& data_buffer);
149
150 // Low-level method to send a single frame. Used for both data and control
151 // frames. Either sends the frame immediately or buffers it to be scheduled
152 // when the current write finishes.
szym 2013/07/09 15:59:51 What does |fin| do?
Adam Rice 2013/07/11 13:28:17 I added some explanation to SendFrame(), and the r
153 void SendIOBufferWithSize(bool fin,
154 WebSocketFrameHeader::OpCode op_code,
155 const scoped_refptr<IOBufferWithSize>& buffer);
156
157 // Perform the "Fail the WebSocket Connection" operation as defined in
158 // RFC6455. The supplied code and reason are sent back to the renderer in an
159 // OnDropChannel message. If state_ is CONNECTED then a Close message is sent
160 // to the remote host. If |expose| is SEND_REAL_ERROR then the remote host is
161 // given the same status code we gave the renderer; otherwise it is sent a
162 // fixed "Going Away" code. Resets current_frame_header_, closes the
163 // stream_, and sets state_ to CLOSED.
szym 2013/07/09 15:59:51 Suggest adding a comment that OnDropChannel is all
Adam Rice 2013/07/11 13:28:17 I did the opposite and took away OnDropChannel's d
164 void FailChannel(ExposeError expose, uint16 code, const std::string& reason);
165
166 // Sends a close frame to Start the WebSocket Closing Handshake, or to respond
167 // to a close frame from the server.
szym 2013/07/09 15:59:51 Be consistent: "a Close frame".
Adam Rice 2013/07/11 13:28:17 Done.
168 void SendClose(uint16 code, const std::string& reason);
169
170 // Parses a Close frame. If no status code is supplied, then |code| is set to
171 // 1005 (No status code) with empty |reason|. If the supplied code is
172 // outside the valid range, then 1002 (Protocol error) is set instead. If the
173 // reason text is not valid UTF-8, then |reason| is set to an empty string
174 // instead.
175 void ParseClose(const scoped_refptr<IOBufferWithSize>& buffer,
176 uint16* code,
177 std::string* reason);
178
179 // The URL to which we connect.
180 const GURL socket_url_;
181
182 // The event_interface_ object to call back into.
szym 2013/07/09 15:59:51 "event_interface_" is unnecessary in the comment.
Adam Rice 2013/07/11 13:28:17 Good point. Done.
183 const scoped_ptr<WebSocketEventInterface> event_interface_;
184
185 // The WebSocketStream to which we are sending/receiving data.
186 scoped_ptr<WebSocketStream> stream_;
187
188 // A data structure containing a vector of frames to be sent and the total
189 // number of bytes contained in the vector.
190 struct SendBuffer;
191 // Data that is currently pending write, or NULL if no write is pending.
192 scoped_ptr<SendBuffer> data_being_sent_;
193 // Data that is queued up to write after the current write completes.
194 // Only non-NULL when such data actually exists.
195 scoped_ptr<SendBuffer> data_to_send_next_;
196
197 // Destination for the current call to WebSocketStream::ReadFrames
198 ScopedVector<WebSocketFrameChunk> read_frame_chunks_;
199 // Frame header for the frame currently being received. Only non-NULL while we
200 // are processing the frame. If the frame arrives in multiple chunks, can
201 // remain non-NULL while we wait for additional chunks to arrive. If the
202 // header of the frame was invalid, this is set to NULL, the channel is
203 // failed, and subsequent chunks of the same frame will be ignored.
204 scoped_ptr<WebSocketFrameHeader> current_frame_header_;
205 // Handle to an in-progress WebSocketStream creation request. Only non-NULL
206 // during the connection process.
207 scoped_ptr<WebSocketStreamRequest> stream_request_;
208 // Although it will almost never happen in practice, we can be passed an
209 // incomplete control frame, in which case we need to keep the data around
210 // long enough to reassemble it. This variable will be NULL the rest of the
211 // time.
212 scoped_refptr<IOBufferWithSize> incomplete_control_frame_body_;
213 // The point at which we give the renderer a quota refresh (bytes).
214 int send_quota_low_water_mark_;
215 // The amount which we refresh the quota to when it reaches the
216 // low_water_mark (bytes).
217 int send_quota_high_water_mark_;
218 // The current amount of quota that the renderer has available for sending
219 // on this logical channel (bytes).
220 int current_send_quota_;
szym 2013/07/09 15:59:51 Why not use an unsigned type for the quotas so tha
Adam Rice 2013/07/11 13:28:17 The style guide seems to be pulling me in both dir
221
222 // Storage for the status code and reason from the time we receive the Close
223 // frame until the connection is closed and we can call OnDropChannel().
224 uint16 closing_code_;
225 std::string closing_reason_;
226
227 // The current state of the channel. Mainly used for sanity checking, but also
228 // used to track the close state.
229 State state_;
230
231 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel);
232 };
233
234 } // namespace net
235
236 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698