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

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

Issue 26544003: Make net::WebSocketChannel deletion safe and enable new IPCs (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Rebase (again). Created 7 years, 1 month 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
« no previous file with comments | « content/browser/renderer_host/websocket_host.cc ('k') | net/websockets/websocket_channel.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/compiler_specific.h" // for WARN_UNUSED_RESULT
13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h" 15 #include "base/memory/scoped_vector.h"
15 #include "base/time/time.h" 16 #include "base/time/time.h"
16 #include "base/timer/timer.h" 17 #include "base/timer/timer.h"
17 #include "net/base/net_export.h" 18 #include "net/base/net_export.h"
19 #include "net/websockets/websocket_event_interface.h"
18 #include "net/websockets/websocket_frame.h" 20 #include "net/websockets/websocket_frame.h"
19 #include "net/websockets/websocket_stream.h" 21 #include "net/websockets/websocket_stream.h"
20 #include "url/gurl.h" 22 #include "url/gurl.h"
21 23
22 namespace net { 24 namespace net {
23 25
24 class BoundNetLog; 26 class BoundNetLog;
25 class IOBuffer; 27 class IOBuffer;
26 class URLRequestContext; 28 class URLRequestContext;
27 class WebSocketEventInterface;
28 29
29 // Transport-independent implementation of WebSockets. Implements protocol 30 // Transport-independent implementation of WebSockets. Implements protocol
30 // semantics that do not depend on the underlying transport. Provides the 31 // semantics that do not depend on the underlying transport. Provides the
31 // interface to the content layer. Some WebSocket concepts are used here without 32 // interface to the content layer. Some WebSocket concepts are used here without
32 // definition; please see the RFC at http://tools.ietf.org/html/rfc6455 for 33 // definition; please see the RFC at http://tools.ietf.org/html/rfc6455 for
33 // clarification. 34 // clarification.
34 class NET_EXPORT WebSocketChannel { 35 class NET_EXPORT WebSocketChannel {
35 public: 36 public:
36 // The type of a WebSocketStream factory callback. Must match the signature of 37 // The type of a WebSocketStream factory callback. Must match the signature of
37 // WebSocketStream::CreateAndConnectStream(). 38 // WebSocketStream::CreateAndConnectStream().
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 const std::vector<std::string>& requested_protocols, 95 const std::vector<std::string>& requested_protocols,
95 const GURL& origin, 96 const GURL& origin,
96 const WebSocketStreamFactory& factory); 97 const WebSocketStreamFactory& factory);
97 98
98 // The default timout for the closing handshake is a sensible value (see 99 // The default timout for the closing handshake is a sensible value (see
99 // kClosingHandshakeTimeoutSeconds in websocket_channel.cc). However, we can 100 // kClosingHandshakeTimeoutSeconds in websocket_channel.cc). However, we can
100 // set it to a very small value for testing purposes. 101 // set it to a very small value for testing purposes.
101 void SetClosingHandshakeTimeoutForTesting(base::TimeDelta delay); 102 void SetClosingHandshakeTimeoutForTesting(base::TimeDelta delay);
102 103
103 private: 104 private:
105 // Methods which return a value of type ChannelState may delete |this|. If the
106 // return value is CHANNEL_DELETED, then the caller must return without making
107 // any further access to member variables or methods.
108 typedef WebSocketEventInterface::ChannelState ChannelState;
109
104 // The object passes through a linear progression of states from 110 // The object passes through a linear progression of states from
105 // FRESHLY_CONSTRUCTED to CLOSED, except that the SEND_CLOSED and RECV_CLOSED 111 // FRESHLY_CONSTRUCTED to CLOSED, except that the SEND_CLOSED and RECV_CLOSED
106 // states may be skipped in case of error. 112 // states may be skipped in case of error.
107 enum State { 113 enum State {
108 FRESHLY_CONSTRUCTED, 114 FRESHLY_CONSTRUCTED,
109 CONNECTING, 115 CONNECTING,
110 CONNECTED, 116 CONNECTED,
111 SEND_CLOSED, // A Close frame has been sent but not received. 117 SEND_CLOSED, // A Close frame has been sent but not received.
112 RECV_CLOSED, // Used briefly between receiving a Close frame and sending 118 RECV_CLOSED, // Used briefly between receiving a Close frame and sending
113 // the response. Once the response is sent, the state changes 119 // the response. Once the response is sent, the state changes
(...skipping 21 matching lines...) Expand all
135 class ConnectDelegate; 141 class ConnectDelegate;
136 142
137 // Starts the connection progress, using a specified factory function. 143 // Starts the connection progress, using a specified factory function.
138 void SendAddChannelRequestWithFactory( 144 void SendAddChannelRequestWithFactory(
139 const GURL& socket_url, 145 const GURL& socket_url,
140 const std::vector<std::string>& requested_protocols, 146 const std::vector<std::string>& requested_protocols,
141 const GURL& origin, 147 const GURL& origin,
142 const WebSocketStreamFactory& factory); 148 const WebSocketStreamFactory& factory);
143 149
144 // Success callback from WebSocketStream::CreateAndConnectStream(). Reports 150 // Success callback from WebSocketStream::CreateAndConnectStream(). Reports
145 // success to the event interface. 151 // success to the event interface. May delete |this|.
146 void OnConnectSuccess(scoped_ptr<WebSocketStream> stream); 152 void OnConnectSuccess(scoped_ptr<WebSocketStream> stream);
147 153
148 // Failure callback from WebSocketStream::CreateAndConnectStream(). Reports 154 // Failure callback from WebSocketStream::CreateAndConnectStream(). Reports
149 // failure to the event interface. 155 // failure to the event interface. May delete |this|.
150 void OnConnectFailure(uint16 websocket_error); 156 void OnConnectFailure(uint16 websocket_error);
151 157
152 // Returns true if state_ is SEND_CLOSED, CLOSE_WAIT or CLOSED. 158 // Returns true if state_ is SEND_CLOSED, CLOSE_WAIT or CLOSED.
153 bool InClosingState() const; 159 bool InClosingState() const;
154 160
155 // Calls WebSocketStream::WriteFrames() with the appropriate arguments 161 // Calls WebSocketStream::WriteFrames() with the appropriate arguments
156 void WriteFrames(); 162 ChannelState WriteFrames() WARN_UNUSED_RESULT;
157 163
158 // Callback from WebSocketStream::WriteFrames. Sends pending data or adjusts 164 // Callback from WebSocketStream::WriteFrames. Sends pending data or adjusts
159 // the send quota of the renderer channel as appropriate. |result| is a net 165 // the send quota of the renderer channel as appropriate. |result| is a net
160 // error code, usually OK. If |synchronous| is true, then OnWriteDone() is 166 // error code, usually OK. If |synchronous| is true, then OnWriteDone() is
161 // being called from within the WriteFrames() loop and does not need to call 167 // being called from within the WriteFrames() loop and does not need to call
162 // WriteFrames() itself. 168 // WriteFrames() itself.
163 void OnWriteDone(bool synchronous, int result); 169 ChannelState OnWriteDone(bool synchronous, int result) WARN_UNUSED_RESULT;
164 170
165 // Calls WebSocketStream::ReadFrames() with the appropriate arguments. 171 // Calls WebSocketStream::ReadFrames() with the appropriate arguments.
166 void ReadFrames(); 172 ChannelState ReadFrames() WARN_UNUSED_RESULT;
167 173
168 // Callback from WebSocketStream::ReadFrames. Handles any errors and processes 174 // Callback from WebSocketStream::ReadFrames. Handles any errors and processes
169 // the returned chunks appropriately to their type. |result| is a net error 175 // the returned chunks appropriately to their type. |result| is a net error
170 // code. If |synchronous| is true, then OnReadDone() is being called from 176 // code. If |synchronous| is true, then OnReadDone() is being called from
171 // within the ReadFrames() loop and does not need to call ReadFrames() itself. 177 // within the ReadFrames() loop and does not need to call ReadFrames() itself.
172 void OnReadDone(bool synchronous, int result); 178 ChannelState OnReadDone(bool synchronous, int result) WARN_UNUSED_RESULT;
173 179
174 // Processes a single frame that has been read from the stream. 180 // Processes a single frame that has been read from the stream.
175 void ProcessFrame(scoped_ptr<WebSocketFrame> frame); 181 ChannelState ProcessFrame(
182 scoped_ptr<WebSocketFrame> frame) WARN_UNUSED_RESULT;
176 183
177 // Handles a frame that the object has received enough of to process. May call 184 // Handles a frame that the object has received enough of to process. May call
178 // |event_interface_| methods, send responses to the server, and change the 185 // |event_interface_| methods, send responses to the server, and change the
179 // value of |state_|. 186 // value of |state_|.
180 void HandleFrame(const WebSocketFrameHeader::OpCode opcode, 187 ChannelState HandleFrame(const WebSocketFrameHeader::OpCode opcode,
181 bool final, 188 bool final,
182 const scoped_refptr<IOBuffer>& data_buffer, 189 const scoped_refptr<IOBuffer>& data_buffer,
183 size_t size); 190 size_t size) WARN_UNUSED_RESULT;
184 191
185 // Low-level method to send a single frame. Used for both data and control 192 // Low-level method to send a single frame. Used for both data and control
186 // frames. Either sends the frame immediately or buffers it to be scheduled 193 // frames. Either sends the frame immediately or buffers it to be scheduled
187 // when the current write finishes. |fin| and |op_code| are defined as for 194 // when the current write finishes. |fin| and |op_code| are defined as for
188 // SendFrame() above, except that |op_code| may also be a control frame 195 // SendFrame() above, except that |op_code| may also be a control frame
189 // opcode. 196 // opcode.
190 void SendIOBuffer(bool fin, 197 ChannelState SendIOBuffer(bool fin,
191 WebSocketFrameHeader::OpCode op_code, 198 WebSocketFrameHeader::OpCode op_code,
192 const scoped_refptr<IOBuffer>& buffer, 199 const scoped_refptr<IOBuffer>& buffer,
193 size_t size); 200 size_t size) WARN_UNUSED_RESULT;
194 201
195 // Performs the "Fail the WebSocket Connection" operation as defined in 202 // Performs the "Fail the WebSocket Connection" operation as defined in
196 // RFC6455. The supplied code and reason are sent back to the renderer in an 203 // RFC6455. The supplied code and reason are sent back to the renderer in an
197 // OnDropChannel message. If state_ is CONNECTED then a Close message is sent 204 // OnDropChannel message. If state_ is CONNECTED then a Close message is sent
198 // to the remote host. If |expose| is SEND_REAL_ERROR then the remote host is 205 // to the remote host. If |expose| is SEND_REAL_ERROR then the remote host is
199 // given the same status code passed to the renderer; otherwise it is sent a 206 // given the same status code passed to the renderer; otherwise it is sent a
200 // fixed "Going Away" code. Closes the stream_ and sets state_ to CLOSED. 207 // fixed "Going Away" code. Closes the stream_ and sets state_ to CLOSED.
201 void FailChannel(ExposeError expose, uint16 code, const std::string& reason); 208 // FailChannel() always returns CHANNEL_DELETED. It is not valid to access any
209 // member variables or methods after calling FailChannel().
210 ChannelState FailChannel(ExposeError expose,
211 uint16 code,
212 const std::string& reason) WARN_UNUSED_RESULT;
202 213
203 // Sends a Close frame to Start the WebSocket Closing Handshake, or to respond 214 // Sends a Close frame to Start the WebSocket Closing Handshake, or to respond
204 // to a Close frame from the server. As a special case, setting |code| to 215 // to a Close frame from the server. As a special case, setting |code| to
205 // kWebSocketErrorNoStatusReceived will create a Close frame with no payload; 216 // kWebSocketErrorNoStatusReceived will create a Close frame with no payload;
206 // this is symmetric with the behaviour of ParseClose. 217 // this is symmetric with the behaviour of ParseClose.
207 void SendClose(uint16 code, const std::string& reason); 218 ChannelState SendClose(uint16 code,
219 const std::string& reason) WARN_UNUSED_RESULT;
208 220
209 // Parses a Close frame. If no status code is supplied, then |code| is set to 221 // Parses a Close frame. If no status code is supplied, then |code| is set to
210 // 1005 (No status code) with empty |reason|. If the supplied code is 222 // 1005 (No status code) with empty |reason|. If the supplied code is
211 // outside the valid range, then 1002 (Protocol error) is set instead. If the 223 // outside the valid range, then 1002 (Protocol error) is set instead. If the
212 // reason text is not valid UTF-8, then |reason| is set to an empty string 224 // reason text is not valid UTF-8, then |reason| is set to an empty string
213 // instead. 225 // instead.
214 void ParseClose(const scoped_refptr<IOBuffer>& buffer, 226 void ParseClose(const scoped_refptr<IOBuffer>& buffer,
215 size_t size, 227 size_t size,
216 uint16* code, 228 uint16* code,
217 std::string* reason); 229 std::string* reason);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 // The current state of the channel. Mainly used for sanity checking, but also 286 // The current state of the channel. Mainly used for sanity checking, but also
275 // used to track the close state. 287 // used to track the close state.
276 State state_; 288 State state_;
277 289
278 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel); 290 DISALLOW_COPY_AND_ASSIGN(WebSocketChannel);
279 }; 291 };
280 292
281 } // namespace net 293 } // namespace net
282 294
283 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_ 295 #endif // NET_WEBSOCKETS_WEBSOCKET_CHANNEL_H_
OLDNEW
« no previous file with comments | « content/browser/renderer_host/websocket_host.cc ('k') | net/websockets/websocket_channel.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698