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

Side by Side Diff: chrome/browser/extensions/api/cast_channel/cast_socket.h

Issue 79673003: Refactor CastSocket code for the following: (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years 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 | Annotate | Revision Log
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 CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ 6 #define CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_
7 7
8 #include <queue> 8 #include <queue>
9 #include <string> 9 #include <string>
10 10
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 108
109 protected: 109 protected:
110 // Creates an instance of TCPClientSocket. 110 // Creates an instance of TCPClientSocket.
111 virtual scoped_ptr<net::TCPClientSocket> CreateTcpSocket(); 111 virtual scoped_ptr<net::TCPClientSocket> CreateTcpSocket();
112 // Creates an instance of SSLClientSocket. 112 // Creates an instance of SSLClientSocket.
113 virtual scoped_ptr<net::SSLClientSocket> CreateSslSocket(); 113 virtual scoped_ptr<net::SSLClientSocket> CreateSslSocket();
114 // Extracts peer certificate from SSLClientSocket instance when the socket 114 // Extracts peer certificate from SSLClientSocket instance when the socket
115 // is in cert error state. 115 // is in cert error state.
116 // Returns whether certificate is successfully extracted. 116 // Returns whether certificate is successfully extracted.
117 virtual bool ExtractPeerCert(std::string* cert); 117 virtual bool ExtractPeerCert(std::string* cert);
118 // Sends a challenge request to the receiver.
119 virtual int SendAuthChallenge();
120 // Reads auth challenge reply from the receiver.
121 virtual int ReadAuthChallengeReply();
122 // Verifies whether the challenge reply received from the peer is valid: 118 // Verifies whether the challenge reply received from the peer is valid:
123 // 1. Signature in the reply is valid. 119 // 1. Signature in the reply is valid.
124 // 2. Certificate is rooted to a trusted CA. 120 // 2. Certificate is rooted to a trusted CA.
125 virtual bool VerifyChallengeReply(); 121 virtual bool VerifyChallengeReply();
126 122
127 // Returns whether we are executing in a valid thread. 123 // Returns whether we are executing in a valid thread.
128 virtual bool CalledOnValidThread() const; 124 virtual bool CalledOnValidThread() const;
129 125
130 private: 126 private:
131 friend class ApiResourceManager<CastSocket>; 127 friend class ApiResourceManager<CastSocket>;
132 friend class CastSocketTest; 128 friend class CastSocketTest;
133 129
134 static const char* service_name() { 130 static const char* service_name() {
135 return "CastSocketManager"; 131 return "CastSocketManager";
136 } 132 }
137 133
138 // Internal connection states. 134 // Internal connection states.
139 enum ConnectionState { 135 enum ConnectionState {
140 CONN_STATE_NONE, 136 CONN_STATE_NONE,
141 CONN_STATE_TCP_CONNECT, 137 CONN_STATE_TCP_CONNECT,
142 CONN_STATE_TCP_CONNECT_COMPLETE, 138 CONN_STATE_TCP_CONNECT_COMPLETE,
143 CONN_STATE_SSL_CONNECT, 139 CONN_STATE_SSL_CONNECT,
144 CONN_STATE_SSL_CONNECT_COMPLETE, 140 CONN_STATE_SSL_CONNECT_COMPLETE,
145 CONN_STATE_AUTH_CHALLENGE_SEND, 141 CONN_STATE_AUTH_CHALLENGE_SEND,
146 CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE, 142 CONN_STATE_AUTH_CHALLENGE_SEND_COMPLETE,
147 CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE, 143 CONN_STATE_AUTH_CHALLENGE_REPLY_COMPLETE,
148 }; 144 };
149 145
146 // Internal write states.
147 enum WriteState {
148 WRITE_STATE_NONE,
149 WRITE_STATE_WRITE,
150 WRITE_STATE_WRITE_COMPLETE,
151 WRITE_STATE_ERROR,
152 };
153
154 // Internal read states.
155 enum ReadState {
156 READ_STATE_NONE,
157 READ_STATE_READ,
158 READ_STATE_READ_COMPLETE,
159 READ_STATE_ERROR,
160 };
161
150 ///////////////////////////////////////////////////////////////////////////// 162 /////////////////////////////////////////////////////////////////////////////
151 // Following methods work together to implement the following flow: 163 // Following methods work together to implement the following flow:
152 // 1. Create a new TCP socket and connect to it 164 // 1. Create a new TCP socket and connect to it
153 // 2. Create a new SSL socket and try connecting to it 165 // 2. Create a new SSL socket and try connecting to it
154 // 3. If connection fails due to invalid cert authority, then extract the 166 // 3. If connection fails due to invalid cert authority, then extract the
155 // peer certificate from the error. 167 // peer certificate from the error.
156 // 4. Whitelist the peer certificate and try #1 and #2 again. 168 // 4. Whitelist the peer certificate and try #1 and #2 again.
157 // 5. If SSL socket is connected successfully, and if protocol is casts:// 169 // 5. If SSL socket is connected successfully, and if protocol is casts://
158 // then issue an auth challenge request. 170 // then issue an auth challenge request.
159 // 6. Validate the auth challenge response. 171 // 6. Validate the auth challenge response.
160 172 //
161 // Main method that performs connection state transitions. 173 // Main method that performs connection state transitions.
162 int DoConnectLoop(int result); 174 void DoConnectLoop(int result);
163 // Each of the below Do* method is executed in the corresponding 175 // Each of the below Do* method is executed in the corresponding
164 // connection state. For e.g. when connection state is TCP_CONNECT 176 // connection state. For e.g. when connection state is TCP_CONNECT
165 // DoTcpConnect is called, and so on. 177 // DoTcpConnect is called, and so on.
166 int DoTcpConnect(); 178 int DoTcpConnect();
167 int DoTcpConnectComplete(int result); 179 int DoTcpConnectComplete(int result);
168 int DoSslConnect(); 180 int DoSslConnect();
169 int DoSslConnectComplete(int result); 181 int DoSslConnectComplete(int result);
170 int DoAuthChallengeSend(); 182 int DoAuthChallengeSend();
171 int DoAuthChallengeSendComplete(int result); 183 int DoAuthChallengeSendComplete(int result);
172 int DoAuthChallengeReplyComplete(int result); 184 int DoAuthChallengeReplyComplete(int result);
173 ///////////////////////////////////////////////////////////////////////////// 185 /////////////////////////////////////////////////////////////////////////////
174 186
175 // Callback method for callbacks from underlying sockets.
176 void OnConnectComplete(int result);
177
178 // Callback method when a challenge request is sent or a reply is received.
179 void OnChallengeEvent(int result);
180
181 // Runs the external connection callback and resets it. 187 // Runs the external connection callback and resets it.
182 void DoConnectCallback(int result); 188 void DoConnectCallback(int result);
183
184 // Verifies that the URL is a valid cast:// or casts:// URL and sets url_ to 189 // Verifies that the URL is a valid cast:// or casts:// URL and sets url_ to
185 // the result. 190 // the result.
186 bool ParseChannelUrl(const GURL& url); 191 bool ParseChannelUrl(const GURL& url);
187 192
188 // Sends the given |message| and invokes the given callback when done. 193 /////////////////////////////////////////////////////////////////////////////
189 int SendMessageInternal(const CastMessage& message, 194 // Following methods work together to implement write flow.
190 const net::CompletionCallback& callback); 195 //
196 // Main method that performs write flow state transitions.
197 void DoWriteLoop(int result);
198 // Each of the below Do* method is executed in the corresponding
199 // write state. For e.g. when write state is WRITE_STATE_WRITE_COMPLETE
200 // DowriteComplete is called, and so on.
201 int DoWrite();
202 int DoWriteComplete(int result);
203 int DoWriteError(int result);
204 /////////////////////////////////////////////////////////////////////////////
191 205
192 // Writes data to the socket from the WriteRequest at the head of the queue. 206 // Adds |message| to the write queue and starts the write loop if needed.
193 // Calls OnWriteData() on completion. 207 void SendCastMessageInternal(const CastMessage& message,
194 int WriteData(); 208 const net::CompletionCallback& callback);
195 void OnWriteData(int result); 209 // Removes all messages from the write queue.
210 void ClearWriteQueue();
196 211
197 // Reads data from the socket into one of the read buffers. Calls 212 /////////////////////////////////////////////////////////////////////////////
198 // OnReadData() on completion. 213 // Following methods work together to implement read flow.
199 int ReadData(); 214 //
200 void OnReadData(int result); 215 // Main method that performs write flow state transitions.
216 void DoReadLoop(int result);
217 // Each of the below Do* method is executed in the corresponding
218 // write state. For e.g. when write state is READ_STATE_READ_COMPLETE
219 // DoReadComplete is called, and so on.
220 int DoRead();
221 int DoReadComplete(int result);
222 int DoReadError(int result);
223 /////////////////////////////////////////////////////////////////////////////
201 224
225 // Posts a task to start the read loop.
226 void PostTaskToStartReadLoop();
227 // Stars the read loop if not already started.
mark a. foltz 2013/12/03 00:56:00 s/Stars/Starts/
228 void StartReadLoop();
202 // Processes the contents of header_read_buffer_ and returns true on success. 229 // Processes the contents of header_read_buffer_ and returns true on success.
203 bool ProcessHeader(); 230 bool ProcessHeader();
204 // Processes the contents of body_read_buffer_ and returns true on success. 231 // Processes the contents of body_read_buffer_ and returns true on success.
205 bool ProcessBody(); 232 bool ProcessBody();
206 // Parses the message held in body_read_buffer_ and notifies |delegate_| if a 233 // Parses the message held in body_read_buffer_ and notifies |delegate_| if a
207 // message was extracted from the buffer. Returns true on success. 234 // message was extracted from the buffer. Returns true on success.
208 bool ParseMessageFromBody(); 235 bool ParseMessageFromBody();
209 236
210 // Serializes the content of message_proto (with a header) to |message_data|. 237 // Serializes the content of message_proto (with a header) to |message_data|.
211 static bool Serialize(const CastMessage& message_proto, 238 static bool Serialize(const CastMessage& message_proto,
212 std::string* message_data); 239 std::string* message_data);
213 240
214 // Closes the socket and sets |error_state_|. Also signals |error| via 241 // Closes the socket and sets |error_state_|. Also signals |error| via
215 // |delegate_|. 242 // |delegate_|.
216 void CloseWithError(ChannelError error); 243 void CloseWithError(ChannelError error);
217 244
218 base::ThreadChecker thread_checker_; 245 base::ThreadChecker thread_checker_;
219 246
220 // The id of the channel. 247 // The id of the channel.
221 int channel_id_; 248 int channel_id_;
222 249
223 // The URL of the peer (cast:// or casts://). 250 // The URL of the peer (cast:// or casts://).
224 GURL url_; 251 GURL url_;
225 // Delegate to inform of incoming messages and errors. 252 // Delegate to inform of incoming messages and errors.
226 Delegate* delegate_; 253 Delegate* delegate_;
227 // True if we should perform receiver authentication. 254 // True if we should perform receiver authentication.
228 bool auth_required_; 255 bool auth_required_;
229 // The IP endpoint of the peer. 256 // The IP endpoint of the peer.
230 net::IPEndPoint ip_endpoint_; 257 net::IPEndPoint ip_endpoint_;
231 // The last error encountered by the channel.
232 ChannelError error_state_;
233 // The current status of the channel.
234 ReadyState ready_state_;
235
236 // True when there is a write callback pending.
237 bool write_callback_pending_;
238 // True when there is a read callback pending.
239 bool read_callback_pending_;
240 258
241 // IOBuffer for reading the message header. 259 // IOBuffer for reading the message header.
242 scoped_refptr<net::GrowableIOBuffer> header_read_buffer_; 260 scoped_refptr<net::GrowableIOBuffer> header_read_buffer_;
243 // IOBuffer for reading the message body. 261 // IOBuffer for reading the message body.
244 scoped_refptr<net::GrowableIOBuffer> body_read_buffer_; 262 scoped_refptr<net::GrowableIOBuffer> body_read_buffer_;
245 // IOBuffer we are currently reading into. 263 // IOBuffer we are currently reading into.
246 scoped_refptr<net::GrowableIOBuffer> current_read_buffer_; 264 scoped_refptr<net::GrowableIOBuffer> current_read_buffer_;
247 // The number of bytes in the current message body. 265 // The number of bytes in the current message body.
248 uint32 current_message_size_; 266 uint32 current_message_size_;
249 267
250 // The NetLog for this service. 268 // The NetLog for this service.
251 net::NetLog* net_log_; 269 net::NetLog* net_log_;
252 // The NetLog source for this service. 270 // The NetLog source for this service.
253 net::NetLog::Source net_log_source_; 271 net::NetLog::Source net_log_source_;
254 272
255 // Next connection state to transition to.
256 ConnectionState next_state_;
257 // Owned ptr to the underlying TCP socket. 273 // Owned ptr to the underlying TCP socket.
258 scoped_ptr<net::TCPClientSocket> tcp_socket_; 274 scoped_ptr<net::TCPClientSocket> tcp_socket_;
259 // Owned ptr to the underlying SSL socket. 275 // Owned ptr to the underlying SSL socket.
260 scoped_ptr<net::SSLClientSocket> socket_; 276 scoped_ptr<net::SSLClientSocket> socket_;
261 // Certificate of the peer. This field may be empty if the peer 277 // Certificate of the peer. This field may be empty if the peer
262 // certificate is not yet fetched. 278 // certificate is not yet fetched.
263 std::string peer_cert_; 279 std::string peer_cert_;
264 scoped_ptr<net::CertVerifier> cert_verifier_; 280 scoped_ptr<net::CertVerifier> cert_verifier_;
265 scoped_ptr<net::TransportSecurityState> transport_security_state_; 281 scoped_ptr<net::TransportSecurityState> transport_security_state_;
266 // Reply received from the receiver to a challenge request. 282 // Reply received from the receiver to a challenge request.
267 scoped_ptr<CastMessage> challenge_reply_; 283 scoped_ptr<CastMessage> challenge_reply_;
268 284
269 // Callback invoked when the socket is connected. 285 // Callback invoked when the socket is connected.
270 net::CompletionCallback connect_callback_; 286 net::CompletionCallback connect_callback_;
271 287
288 // Connection flow state machine state.
289 ConnectionState connect_state_;
290 // Write flow state machine state.
291 WriteState write_state_;
292 // Read flow state machine state.
293 ReadState read_state_;
294 // The last error encountered by the channel.
295 ChannelError error_state_;
296 // The current status of the channel.
297 ReadyState ready_state_;
298
272 // Message header struct. If fields are added, be sure to update 299 // Message header struct. If fields are added, be sure to update
273 // kMessageHeaderSize in the .cc. 300 // kMessageHeaderSize in the .cc.
274 struct MessageHeader { 301 struct MessageHeader {
275 MessageHeader(); 302 MessageHeader();
276 // Sets the message size. 303 // Sets the message size.
277 void SetMessageSize(size_t message_size); 304 void SetMessageSize(size_t message_size);
278 // Prepends this header to |str|. 305 // Prepends this header to |str|.
279 void PrependToString(std::string* str); 306 void PrependToString(std::string* str);
280 // Reads |header| from the beginning of |buffer|. 307 // Reads |header| from the beginning of |buffer|.
281 static void ReadFromIOBuffer(net::GrowableIOBuffer* buffer, 308 static void ReadFromIOBuffer(net::GrowableIOBuffer* buffer,
(...skipping 12 matching lines...) Expand all
294 // and prepending the header. Must only be called once. 321 // and prepending the header. Must only be called once.
295 bool SetContent(const CastMessage& message_proto); 322 bool SetContent(const CastMessage& message_proto);
296 323
297 net::CompletionCallback callback; 324 net::CompletionCallback callback;
298 scoped_refptr<net::DrainableIOBuffer> io_buffer; 325 scoped_refptr<net::DrainableIOBuffer> io_buffer;
299 }; 326 };
300 // Queue of pending writes. The message at the front of the queue is the one 327 // Queue of pending writes. The message at the front of the queue is the one
301 // being written. 328 // being written.
302 std::queue<WriteRequest> write_queue_; 329 std::queue<WriteRequest> write_queue_;
303 330
304 // Used to protect against DoConnectLoop() re-entrancy.
305 bool in_connect_loop_;
306
307 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestCastURLs); 331 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestCastURLs);
308 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead); 332 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead);
309 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); 333 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany);
310 DISALLOW_COPY_AND_ASSIGN(CastSocket); 334 DISALLOW_COPY_AND_ASSIGN(CastSocket);
311 }; 335 };
312 336
313 } // namespace cast_channel 337 } // namespace cast_channel
314 } // namespace api 338 } // namespace api
315 } // namespace extensions 339 } // namespace extensions
316 340
317 #endif // CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ 341 #endif // CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698