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

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: Fix "OverFlow" -> "Overflow" 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 "base/memory/weak_ptr.h"
16 #include "googleurl/src/gurl.h"
17 #include "net/base/net_export.h"
18 #include "net/websockets/websocket_frame.h"
19 #include "net/websockets/websocket_stream.h"
20
21 namespace net {
22
23 class URLRequestContext;
24 class WebSocketEventInterface;
25
26 // Transport-independent implementation of WebSockets. Implements protocol
27 // semantics that do not depend on the underlying transport. Provides the
28 // interface to the content layer.
29 class NET_EXPORT WebSocketChannel {
30 public:
31 // Creates a new WebSocketChannel with the specified parameters.
32 // SendAddChannelRequest() must be called immediately afterwards to start the
33 // connection process.
34 WebSocketChannel(const GURL& socket_url,
35 scoped_ptr<WebSocketEventInterface> event_interface);
36 virtual ~WebSocketChannel();
37
38 // Starts the connection process.
39 void SendAddChannelRequest(
40 const std::vector<std::string>& requested_protocols,
41 const GURL& origin,
42 URLRequestContext* url_request_context);
43
44 // Sends a data frame to the remote side. The frame should usually be no
45 // larger than 32KB to avoid blocking the IO thread. This method has a hard
46 // limit of 2GB. It is the responsibility of the caller to ensure that they
47 // have sufficient send quota to send this data, otherwise the connection will
48 // be closed without sending.
49 void SendFrame(bool fin,
50 WebSocketFrameHeader::OpCode op_code,
51 const std::vector<char>& data);
52
53 // Sends "quota" units of flow control to the remote side. If the underlying
54 // transport has a concept of "quota", then it permits the remote server to
55 // send up to "quota" units of data.
56 void SendFlowControl(int64 quota);
57
58 // Sends a "Drop Channel" message to the remote side.
59 void SendDropChannel(unsigned short code, const std::string& reason);
60
61 // Starts the connection process, using a different factory function from the
62 // default. This is used for testing.
63 void SendAddChannelRequestWithFactory(
64 const std::vector<std::string>& requested_protocols,
65 const GURL& origin,
66 URLRequestContext* url_request_context,
67 base::Callback<scoped_ptr<WebSocketStreamRequest>(
68 const GURL&,
69 const std::vector<std::string>&,
70 const GURL&,
71 URLRequestContext*,
72 const BoundNetLog&,
73 scoped_ptr<WebSocketStream::ConnectDelegate>)> factory);
74
75 private:
76 // We have a simple linear progression of states from CONSTRUCTED to CLOSED,
77 // except that the SEND_CLOSED and RECV_CLOSED states may be skipped in case
78 // of error.
79 enum State {
80 FRESHLY_CONSTRUCTED,
81 CONNECTING,
82 CONNECTED,
83 SEND_CLOSED,
84 RECV_CLOSED,
85 CLOSED, // also used for FAILED states.
86 };
87
88 // When failing a channel, we may or may not want to send the real reason for
89 // failing to the remote server. This enum is used by FailChannel() to
90 // choose.
91 enum ExposeError {
92 SEND_REAL_ERROR,
93 SEND_INTERNAL_ERROR,
94 };
95
96 // Our implementation of WebSocketStream::ConnectDelegate. We do not inherit
97 // from WebSocketStream::ConnectDelegate directly to avoid cluttering our
98 // public interface with the implementation of those methods, and because the
99 // lifetime of a WebSocketChannel is longer than the lifetime of the
100 // connection process.
101 class ConnectDelegate;
102
103 // Success callback from WebSocketStream::CreateAndConnectStream(). Reports
104 // success to the event interface.
105 void OnConnectSuccess(scoped_ptr<WebSocketStream> stream);
106
107 // Failure callback from WebSocketStream::CreateAndConnectStream(). Reports
108 // failure to the event interface.
109 void OnConnectFailure(unsigned short websocket_error);
110
111 // Calls WebSocketStream::WriteFrames() with the appropriate arguments
112 void WriteFrames();
113
114 // Callback from WebSocketStream::WriteFrames. Sends pending data or
115 // adjusts the send quota of the renderer channel as appropriate.
116 void OnWriteDone(int result);
117
118 // Calls WebSocketStream::ReadFrames() with the appropriate arguments.
119 void ReadFrames();
120
121 // Callback from WebSocketStream::ReadFrames. Handles any errors and processes
122 // the returned chunks appropriately to their type.
123 void OnReadDone(int result);
124
125 // Processes a single chunk that has been read from the stream.
126 void ProcessFrameChunk(scoped_ptr<WebSocketFrameChunk> chunk);
127
128 // Low-level method to send a single frame. Used for both data and control
129 // frames. Either sends the frame immediately or buffers it to be scheduled
130 // when the current write finishes.
131 void SendIOBufferWithSize(bool fin,
132 WebSocketFrameHeader::OpCode op_code,
133 const scoped_refptr<IOBufferWithSize>& buffer);
134
135 // Internal method to fail a channel in a manner appropriate the current
136 // state. The suppled code and reason are sent back to the renderer; the
137 // server just gets a generic "going away" error (if the current state is
138 // CONNECTED and |expose| is SendInternalError). Resets current_frame_header_
139 // as a side-effect.
140 void FailChannel(ExposeError expose,
141 unsigned short code,
142 const std::string& reason);
143
144 // Sends a close frame to Start the WebSocket Closing Handshake, or to respond
145 // to a close frame from the server.
146 void SendClose(unsigned short code, const std::string& reason);
147
148 // Parses a Close frame. If no status code is supplied, then |code| is set to
149 // 1005 (No status code) with empty |reason|. If the supplied code is
150 // outside the valid range, then 1002 (Protocol error) is set instead. If the
151 // reason text is not valid UTF-8, then |reason| is set to an empty string
152 // instead.
153 void ParseClose(const scoped_refptr<IOBufferWithSize>& buffer,
154 unsigned short* code,
155 std::string* reason);
156
157 // The URL to which we connect.
158 const GURL socket_url_;
159
160 // The event_interface_ object to call back into.
161 const scoped_ptr<WebSocketEventInterface> event_interface_;
162
163 // The WebSocketStream to which we are sending/receiving data.
164 scoped_ptr<WebSocketStream> stream_;
165
166 // A data structure containing a vector of frames to be sent and the total
167 // number of bytes contained in the vector.
168 struct SendBuffer;
169 // Data that is currently pending write, or NULL if no write is pending.
170 scoped_ptr<SendBuffer> data_being_sent_;
171 // Data that is queued up to write after the current write completes.
172 // Only non-NULL when such data actually exists.
173 scoped_ptr<SendBuffer> data_to_send_next_;
174
175 // Destination for the current call to WebSocketStream::ReadFrames
176 ScopedVector<WebSocketFrameChunk> read_frame_chunks_;
177 // Frame header for the frame currently being received. Only non-NULL if the
178 // current frame has been broken into multiple chunks.
179 scoped_ptr<WebSocketFrameHeader> current_frame_header_;
180 // Handle to an in-progress WebSocketStream creation request. Only non-NULL
181 // during the connection process.
182 scoped_ptr<WebSocketStreamRequest> stream_request_;
183 // Although it will almost never happen in practice, we can be passed an
184 // incomplete control frame, in which case we need to keep the data around
185 // long enough to reassemble it. This variable will be NULL the rest of the
186 // time.
187 scoped_refptr<IOBufferWithSize> incomplete_control_frame_body_;
188 // The point at which we give the renderer a quota refresh (bytes).
189 int send_quota_low_water_mark_;
190 // The amount which we refresh the quota to when it reaches the
191 // low_water_mark (bytes).
192 int send_quota_high_water_mark_;
193 // The current amount of quota that the renderer has available for sending
194 // on this logical channel (bytes).
195 int current_send_quota_;
196
197 // The current state of the channel. Mainly used for sanity checking, but also
198 // used to track the close state.
199 State state_;
200
201 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel);
202 };
203
204 } // namespace net
205
206 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698