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

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: Code refactoring and cleanup. 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.
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
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
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 // Sends a "Drop Channel" message to the remote side.
58 void SendDropChannel(uint16 code, const std::string& reason);
59
60 // Starts the connection process, using a different factory function from the
61 // default. This is used for testing.
62 void SendAddChannelRequestWithFactory(
63 const std::vector<std::string>& requested_protocols,
64 const GURL& origin,
65 URLRequestContext* url_request_context,
66 base::Callback<scoped_ptr<WebSocketStreamRequest>(
67 const GURL&,
68 const std::vector<std::string>&,
69 const GURL&,
70 URLRequestContext*,
71 const BoundNetLog&,
72 scoped_ptr<WebSocketStream::ConnectDelegate>)> factory);
73
74 private:
75 // We have a simple linear progression of states from FRESHLY_CONSTRUCTED to
76 // CLOSED, except that the SEND_CLOSED and RECV_CLOSED states may be skipped
77 // in case of error.
78 enum State {
79 FRESHLY_CONSTRUCTED,
80 CONNECTING,
81 CONNECTED,
82 SEND_CLOSED,
83 RECV_CLOSED,
84 CLOSED, // also used for FAILED states.
85 };
86
87 // When failing a channel, we may or may not want to send the real reason for
88 // failing to the remote server. This enum is used by FailChannel() to
89 // choose.
90 enum ExposeError {
91 SEND_REAL_ERROR,
92 SEND_INTERNAL_ERROR,
93 };
94
95 // Our implementation of WebSocketStream::ConnectDelegate. We do not inherit
96 // from WebSocketStream::ConnectDelegate directly to avoid cluttering our
97 // public interface with the implementation of those methods, and because the
98 // lifetime of a WebSocketChannel is longer than the lifetime of the
99 // connection process.
100 class ConnectDelegate;
101
102 // Success callback from WebSocketStream::CreateAndConnectStream(). Reports
103 // success to the event interface.
104 void OnConnectSuccess(scoped_ptr<WebSocketStream> stream);
105
106 // Failure callback from WebSocketStream::CreateAndConnectStream(). Reports
107 // failure to the event interface.
108 void OnConnectFailure(uint16 websocket_error);
109
110 // Calls WebSocketStream::WriteFrames() with the appropriate arguments
111 void WriteFrames();
112
113 // Callback from WebSocketStream::WriteFrames. Sends pending data or adjusts
114 // the send quota of the renderer channel as appropriate. |result| is a met
115 // error code, usually OK. If |synchronous| is true, then OnWriteDone() is
116 // being called from within the WriteFrames() loop and does not need to call
117 // WriteFrames() itself.
118 void OnWriteDone(bool synchronous, int result);
119
120 // Calls WebSocketStream::ReadFrames() with the appropriate arguments.
121 void ReadFrames();
122
123 // Callback from WebSocketStream::ReadFrames. Handles any errors and processes
124 // the returned chunks appropriately to their type. If |synchronous| is true,
125 // is being called directly from within ReadFrames(). |result| is a net error
126 // code.
127 void OnReadDone(bool synchronous, int result);
128
129 // Processes a single chunk that has been read from the stream.
130 void ProcessFrameChunk(scoped_ptr<WebSocketFrameChunk> chunk);
131
132 // Handle a frame that we have received enough of to process. May call
133 // event_interface_ methods, send responses to the server, and change the
134 // value of state_.
135 void HandleFrame(const WebSocketFrameHeader::OpCode opcode,
136 const bool is_first_chunk,
137 const bool is_final_chunk,
138 const scoped_refptr<IOBufferWithSize>& data_buffer);
139
140 // Low-level method to send a single frame. Used for both data and control
141 // frames. Either sends the frame immediately or buffers it to be scheduled
142 // when the current write finishes.
143 void SendIOBufferWithSize(bool fin,
144 WebSocketFrameHeader::OpCode op_code,
145 const scoped_refptr<IOBufferWithSize>& buffer);
146
147 // Internal method to fail a channel in a manner appropriate the current
148 // state. The supplied code and reason are sent back to the renderer; the
149 // server just gets a generic "going away" error (if the current state is
150 // CONNECTED and |expose| is SEND_INTERNAL_ERROR). Resets
151 // current_frame_header_ as a side-effect.
152 void FailChannel(ExposeError expose,
153 uint16 code,
154 const std::string& reason);
155
156 // Sends a close frame to Start the WebSocket Closing Handshake, or to respond
157 // to a close frame from the server.
158 void SendClose(uint16 code, const std::string& reason);
159
160 // Parses a Close frame. If no status code is supplied, then |code| is set to
161 // 1005 (No status code) with empty |reason|. If the supplied code is
162 // outside the valid range, then 1002 (Protocol error) is set instead. If the
163 // reason text is not valid UTF-8, then |reason| is set to an empty string
164 // instead.
165 void ParseClose(const scoped_refptr<IOBufferWithSize>& buffer,
166 uint16* code,
167 std::string* reason);
168
169 // The URL to which we connect.
170 const GURL socket_url_;
171
172 // The event_interface_ object to call back into.
173 const scoped_ptr<WebSocketEventInterface> event_interface_;
174
175 // The WebSocketStream to which we are sending/receiving data.
176 scoped_ptr<WebSocketStream> stream_;
177
178 // A data structure containing a vector of frames to be sent and the total
179 // number of bytes contained in the vector.
180 struct SendBuffer;
181 // Data that is currently pending write, or NULL if no write is pending.
182 scoped_ptr<SendBuffer> data_being_sent_;
183 // Data that is queued up to write after the current write completes.
184 // Only non-NULL when such data actually exists.
185 scoped_ptr<SendBuffer> data_to_send_next_;
186
187 // Destination for the current call to WebSocketStream::ReadFrames
188 ScopedVector<WebSocketFrameChunk> read_frame_chunks_;
189 // Frame header for the frame currently being received. Only non-NULL while we
190 // are processing the frame. If the frame arrives in multiple chunks, can
191 // remain non-NULL while we wait for additional chunks to arrive. If the
192 // header of the frame was invalid, this is set to NULL, the channel is
193 // failed, and subsequent chunks of the same frame will be ignored.
194 scoped_ptr<WebSocketFrameHeader> current_frame_header_;
195 // Handle to an in-progress WebSocketStream creation request. Only non-NULL
196 // during the connection process.
197 scoped_ptr<WebSocketStreamRequest> stream_request_;
198 // Although it will almost never happen in practice, we can be passed an
199 // incomplete control frame, in which case we need to keep the data around
200 // long enough to reassemble it. This variable will be NULL the rest of the
201 // time.
202 scoped_refptr<IOBufferWithSize> incomplete_control_frame_body_;
203 // The point at which we give the renderer a quota refresh (bytes).
204 int send_quota_low_water_mark_;
205 // The amount which we refresh the quota to when it reaches the
206 // low_water_mark (bytes).
207 int send_quota_high_water_mark_;
208 // The current amount of quota that the renderer has available for sending
209 // on this logical channel (bytes).
210 int current_send_quota_;
211
212 // The current state of the channel. Mainly used for sanity checking, but also
213 // used to track the close state.
214 State state_;
215
216 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel);
217 };
218
219 } // namespace net
220
221 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698