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

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

Issue 393023003: Added connection timeout functionality to CastSocket. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: JS API plumbing, switch approach from MessageLoop to Timer, better testing. Created 6 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
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
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/callback.h" 12 #include "base/callback.h"
13 #include "base/cancelable_callback.h"
13 #include "base/gtest_prod_util.h" 14 #include "base/gtest_prod_util.h"
14 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h" 16 #include "base/memory/weak_ptr.h"
17 #include "base/task/cancelable_task_tracker.h"
16 #include "base/threading/thread_checker.h" 18 #include "base/threading/thread_checker.h"
19 #include "base/timer/timer.h"
17 #include "chrome/common/extensions/api/cast_channel.h" 20 #include "chrome/common/extensions/api/cast_channel.h"
18 #include "extensions/browser/api/api_resource.h" 21 #include "extensions/browser/api/api_resource.h"
19 #include "extensions/browser/api/api_resource_manager.h" 22 #include "extensions/browser/api/api_resource_manager.h"
20 #include "net/base/completion_callback.h" 23 #include "net/base/completion_callback.h"
21 #include "net/base/io_buffer.h" 24 #include "net/base/io_buffer.h"
22 #include "net/base/ip_endpoint.h" 25 #include "net/base/ip_endpoint.h"
23 #include "net/base/net_log.h" 26 #include "net/base/net_log.h"
24 27
25 namespace net { 28 namespace net {
26 class AddressList; 29 class AddressList;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 virtual ~Delegate() {} 64 virtual ~Delegate() {}
62 }; 65 };
63 66
64 // Creates a new CastSocket that connects to |ip_endpoint| with 67 // Creates a new CastSocket that connects to |ip_endpoint| with
65 // |channel_auth|. |owner_extension_id| is the id of the extension that opened 68 // |channel_auth|. |owner_extension_id| is the id of the extension that opened
66 // the socket. |channel_auth| must not be CHANNEL_AUTH_NONE. 69 // the socket. |channel_auth| must not be CHANNEL_AUTH_NONE.
67 CastSocket(const std::string& owner_extension_id, 70 CastSocket(const std::string& owner_extension_id,
68 const net::IPEndPoint& ip_endpoint, 71 const net::IPEndPoint& ip_endpoint,
69 ChannelAuthType channel_auth, 72 ChannelAuthType channel_auth,
70 CastSocket::Delegate* delegate, 73 CastSocket::Delegate* delegate,
71 net::NetLog* net_log); 74 net::NetLog* net_log,
75 int connect_timeout_ms);
72 virtual ~CastSocket(); 76 virtual ~CastSocket();
73 77
74 // The IP endpoint for the destination of the channel. 78 // The IP endpoint for the destination of the channel.
75 const net::IPEndPoint& ip_endpoint() const { return ip_endpoint_; } 79 const net::IPEndPoint& ip_endpoint() const { return ip_endpoint_; }
76 80
77 // The authentication level requested for the channel. 81 // The authentication level requested for the channel.
78 ChannelAuthType channel_auth() const { return channel_auth_; } 82 ChannelAuthType channel_auth() const { return channel_auth_; }
79 83
80 // Returns a cast:// or casts:// URL for the channel endpoint. 84 // Returns a cast:// or casts:// URL for the channel endpoint.
81 // For backwards compatibility. 85 // For backwards compatibility.
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 static uint32 max_message_size() { return 65536; } 141 static uint32 max_message_size() { return 65536; }
138 142
139 std::string ToString(); 143 std::string ToString();
140 // The size of the following protocol message in bytes, in host byte order. 144 // The size of the following protocol message in bytes, in host byte order.
141 uint32 message_size; 145 uint32 message_size;
142 }; 146 };
143 147
144 private: 148 private:
145 friend class ApiResourceManager<CastSocket>; 149 friend class ApiResourceManager<CastSocket>;
146 friend class CastSocketTest; 150 friend class CastSocketTest;
151 friend class TestCastSocket;
147 152
148 static const char* service_name() { return "CastSocketManager"; } 153 static const char* service_name() { return "CastSocketManager"; }
149 154
150 // Internal connection states. 155 // Internal connection states.
151 enum ConnectionState { 156 enum ConnectionState {
152 CONN_STATE_NONE, 157 CONN_STATE_NONE,
153 CONN_STATE_TCP_CONNECT, 158 CONN_STATE_TCP_CONNECT,
154 CONN_STATE_TCP_CONNECT_COMPLETE, 159 CONN_STATE_TCP_CONNECT_COMPLETE,
155 CONN_STATE_SSL_CONNECT, 160 CONN_STATE_SSL_CONNECT,
156 CONN_STATE_SSL_CONNECT_COMPLETE, 161 CONN_STATE_SSL_CONNECT_COMPLETE,
(...skipping 27 matching lines...) Expand all
184 scoped_ptr<net::StreamSocket> socket); 189 scoped_ptr<net::StreamSocket> socket);
185 // Extracts peer certificate from SSLClientSocket instance when the socket 190 // Extracts peer certificate from SSLClientSocket instance when the socket
186 // is in cert error state. 191 // is in cert error state.
187 // Returns whether certificate is successfully extracted. 192 // Returns whether certificate is successfully extracted.
188 virtual bool ExtractPeerCert(std::string* cert); 193 virtual bool ExtractPeerCert(std::string* cert);
189 // Verifies whether the challenge reply received from the peer is valid: 194 // Verifies whether the challenge reply received from the peer is valid:
190 // 1. Signature in the reply is valid. 195 // 1. Signature in the reply is valid.
191 // 2. Certificate is rooted to a trusted CA. 196 // 2. Certificate is rooted to a trusted CA.
192 virtual bool VerifyChallengeReply(); 197 virtual bool VerifyChallengeReply();
193 198
199 // Invoked by a cancelable closure when connection setup time
200 // exceeds the interval specified at |connect_timeout_ms_|.
201 void CancelConnect();
202
194 ///////////////////////////////////////////////////////////////////////////// 203 /////////////////////////////////////////////////////////////////////////////
195 // Following methods work together to implement the following flow: 204 // Following methods work together to implement the following flow:
196 // 1. Create a new TCP socket and connect to it 205 // 1. Create a new TCP socket and connect to it
197 // 2. Create a new SSL socket and try connecting to it 206 // 2. Create a new SSL socket and try connecting to it
198 // 3. If connection fails due to invalid cert authority, then extract the 207 // 3. If connection fails due to invalid cert authority, then extract the
199 // peer certificate from the error. 208 // peer certificate from the error.
200 // 4. Whitelist the peer certificate and try #1 and #2 again. 209 // 4. Whitelist the peer certificate and try #1 and #2 again.
201 // 5. If SSL socket is connected successfully, and if protocol is casts:// 210 // 5. If SSL socket is connected successfully, and if protocol is casts://
202 // then issue an auth challenge request. 211 // then issue an auth challenge request.
203 // 6. Validate the auth challenge response. 212 // 6. Validate the auth challenge response.
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 bool ProcessBody(); 269 bool ProcessBody();
261 // Closes socket, updating the error state and signaling the delegate that 270 // Closes socket, updating the error state and signaling the delegate that
262 // |error| has occurred. 271 // |error| has occurred.
263 void CloseWithError(ChannelError error); 272 void CloseWithError(ChannelError error);
264 // Serializes the content of message_proto (with a header) to |message_data|. 273 // Serializes the content of message_proto (with a header) to |message_data|.
265 static bool Serialize(const CastMessage& message_proto, 274 static bool Serialize(const CastMessage& message_proto,
266 std::string* message_data); 275 std::string* message_data);
267 276
268 virtual bool CalledOnValidThread() const; 277 virtual bool CalledOnValidThread() const;
269 278
279 void InjectTimerForTesting(scoped_ptr<base::Timer> injected_timer);
280
270 base::ThreadChecker thread_checker_; 281 base::ThreadChecker thread_checker_;
271 282
272 // The id of the channel. 283 // The id of the channel.
273 int channel_id_; 284 int channel_id_;
274 285
275 // The IP endpoint that the the channel is connected to. 286 // The IP endpoint that the the channel is connected to.
276 net::IPEndPoint ip_endpoint_; 287 net::IPEndPoint ip_endpoint_;
277 // Receiver authentication requested for the channel. 288 // Receiver authentication requested for the channel.
278 ChannelAuthType channel_auth_; 289 ChannelAuthType channel_auth_;
279 // Delegate to inform of incoming messages and errors. 290 // Delegate to inform of incoming messages and errors.
(...skipping 24 matching lines...) Expand all
304 // Owned ptr to the underlying TCP socket. 315 // Owned ptr to the underlying TCP socket.
305 scoped_ptr<net::TCPClientSocket> tcp_socket_; 316 scoped_ptr<net::TCPClientSocket> tcp_socket_;
306 // Owned ptr to the underlying SSL socket. 317 // Owned ptr to the underlying SSL socket.
307 scoped_ptr<net::SSLClientSocket> socket_; 318 scoped_ptr<net::SSLClientSocket> socket_;
308 // Certificate of the peer. This field may be empty if the peer 319 // Certificate of the peer. This field may be empty if the peer
309 // certificate is not yet fetched. 320 // certificate is not yet fetched.
310 std::string peer_cert_; 321 std::string peer_cert_;
311 // Reply received from the receiver to a challenge request. 322 // Reply received from the receiver to a challenge request.
312 scoped_ptr<CastMessage> challenge_reply_; 323 scoped_ptr<CastMessage> challenge_reply_;
313 324
314 // Callback invoked when the socket is connected. 325 // Callback invoked when the socket is connected or fails to connect.
315 net::CompletionCallback connect_callback_; 326 net::CompletionCallback connect_callback_;
316 327
328 // Number of milliseconds to wait before timing out.
329 int timeout_interval_ms_;
330 // Timer invoked when the connection has timed out.
331 scoped_ptr<base::Timer> connect_timeout_timer_;
332 // Used to cancel all pending connection event loop tasks on timeout.
333 base::CancelableTaskTracker task_tracker_;
334
317 // Connection flow state machine state. 335 // Connection flow state machine state.
318 ConnectionState connect_state_; 336 ConnectionState connect_state_;
319 // Write flow state machine state. 337 // Write flow state machine state.
320 WriteState write_state_; 338 WriteState write_state_;
321 // Read flow state machine state. 339 // Read flow state machine state.
322 ReadState read_state_; 340 ReadState read_state_;
323 // The last error encountered by the channel. 341 // The last error encountered by the channel.
324 ChannelError error_state_; 342 ChannelError error_state_;
325 // The current status of the channel. 343 // The current status of the channel.
326 ReadyState ready_state_; 344 ReadyState ready_state_;
(...skipping 20 matching lines...) Expand all
347 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); 365 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany);
348 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestWriteErrorLargeMessage); 366 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestWriteErrorLargeMessage);
349 DISALLOW_COPY_AND_ASSIGN(CastSocket); 367 DISALLOW_COPY_AND_ASSIGN(CastSocket);
350 }; 368 };
351 369
352 } // namespace cast_channel 370 } // namespace cast_channel
353 } // namespace api 371 } // namespace api
354 } // namespace extensions 372 } // namespace extensions
355 373
356 #endif // CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_ 374 #endif // CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_SOCKET_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698