OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ |
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ | 6 #define EXTENSIONS_BROWSER_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" | |
13 #include "base/cancelable_callback.h" | 12 #include "base/cancelable_callback.h" |
14 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
15 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
16 #include "base/memory/weak_ptr.h" | |
17 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
18 #include "base/timer/timer.h" | 16 #include "base/timer/timer.h" |
19 #include "extensions/browser/api/api_resource.h" | 17 #include "extensions/browser/api/api_resource.h" |
20 #include "extensions/browser/api/api_resource_manager.h" | 18 #include "extensions/browser/api/api_resource_manager.h" |
21 #include "extensions/common/api/cast_channel.h" | 19 #include "extensions/common/api/cast_channel.h" |
22 #include "net/base/completion_callback.h" | 20 #include "net/base/completion_callback.h" |
23 #include "net/base/io_buffer.h" | 21 #include "net/base/io_buffer.h" |
24 #include "net/base/ip_endpoint.h" | 22 #include "net/base/ip_endpoint.h" |
25 #include "net/base/net_log.h" | 23 #include "net/base/net_log.h" |
26 | 24 |
(...skipping 11 matching lines...) Expand all Loading... |
38 namespace cast_channel { | 36 namespace cast_channel { |
39 | 37 |
40 class CastMessage; | 38 class CastMessage; |
41 | 39 |
42 // This class implements a channel between Chrome and a Cast device using a TCP | 40 // This class implements a channel between Chrome and a Cast device using a TCP |
43 // socket with SSL. The channel may authenticate that the receiver is a genuine | 41 // socket with SSL. The channel may authenticate that the receiver is a genuine |
44 // Cast device. All CastSocket objects must be used only on the IO thread. | 42 // Cast device. All CastSocket objects must be used only on the IO thread. |
45 // | 43 // |
46 // NOTE: Not called "CastChannel" to reduce confusion with the generated API | 44 // NOTE: Not called "CastChannel" to reduce confusion with the generated API |
47 // code. | 45 // code. |
48 class CastSocket : public ApiResource, | 46 class CastSocket : public ApiResource { |
49 public base::SupportsWeakPtr<CastSocket> { | |
50 public: | 47 public: |
51 // Object to be informed of incoming messages and errors. | 48 // Object to be informed of incoming messages and errors. |
52 class Delegate { | 49 class Delegate { |
53 public: | 50 public: |
54 // An error occurred on the channel. | 51 // An error occurred on the channel. |
55 // It is fine to delete the socket in this callback. | |
56 virtual void OnError(const CastSocket* socket, ChannelError error) = 0; | 52 virtual void OnError(const CastSocket* socket, ChannelError error) = 0; |
57 // A message was received on the channel. | 53 // A message was received on the channel. |
58 // Do NOT delete the socket in this callback. | |
59 virtual void OnMessage(const CastSocket* socket, | 54 virtual void OnMessage(const CastSocket* socket, |
60 const MessageInfo& message) = 0; | 55 const MessageInfo& message) = 0; |
61 | 56 |
62 protected: | 57 protected: |
63 virtual ~Delegate() {} | 58 virtual ~Delegate() {} |
64 }; | 59 }; |
65 | 60 |
66 // Creates a new CastSocket that connects to |ip_endpoint| with | 61 // Creates a new CastSocket that connects to |ip_endpoint| with |
67 // |channel_auth|. |owner_extension_id| is the id of the extension that opened | 62 // |channel_auth|. |owner_extension_id| is the id of the extension that opened |
68 // the socket. |channel_auth| must not be CHANNEL_AUTH_NONE. | 63 // the socket. |channel_auth| must not be CHANNEL_AUTH_NONE. |
69 CastSocket(const std::string& owner_extension_id, | 64 CastSocket(const std::string& owner_extension_id, |
70 const net::IPEndPoint& ip_endpoint, | 65 const net::IPEndPoint& ip_endpoint, |
71 ChannelAuthType channel_auth, | 66 ChannelAuthType channel_auth, |
72 CastSocket::Delegate* delegate, | 67 CastSocket::Delegate* delegate, |
73 net::NetLog* net_log, | 68 net::NetLog* net_log, |
74 const base::TimeDelta& connect_timeout); | 69 const base::TimeDelta& connect_timeout); |
| 70 |
| 71 // Ensures that the socket is closed. |
75 virtual ~CastSocket(); | 72 virtual ~CastSocket(); |
76 | 73 |
77 // The IP endpoint for the destination of the channel. | 74 // The IP endpoint for the destination of the channel. |
78 const net::IPEndPoint& ip_endpoint() const { return ip_endpoint_; } | 75 const net::IPEndPoint& ip_endpoint() const { return ip_endpoint_; } |
79 | 76 |
80 // The authentication level requested for the channel. | 77 // The authentication level requested for the channel. |
81 ChannelAuthType channel_auth() const { return channel_auth_; } | 78 ChannelAuthType channel_auth() const { return channel_auth_; } |
82 | 79 |
83 // Returns a cast:// or casts:// URL for the channel endpoint. | 80 // Returns a cast:// or casts:// URL for the channel endpoint. |
84 // For backwards compatibility. | 81 // For backwards compatibility. |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 void DoConnectLoop(int result); | 211 void DoConnectLoop(int result); |
215 // Each of the below Do* method is executed in the corresponding | 212 // Each of the below Do* method is executed in the corresponding |
216 // connection state. For example when connection state is TCP_CONNECT | 213 // connection state. For example when connection state is TCP_CONNECT |
217 // DoTcpConnect is called, and so on. | 214 // DoTcpConnect is called, and so on. |
218 int DoTcpConnect(); | 215 int DoTcpConnect(); |
219 int DoTcpConnectComplete(int result); | 216 int DoTcpConnectComplete(int result); |
220 int DoSslConnect(); | 217 int DoSslConnect(); |
221 int DoSslConnectComplete(int result); | 218 int DoSslConnectComplete(int result); |
222 int DoAuthChallengeSend(); | 219 int DoAuthChallengeSend(); |
223 int DoAuthChallengeSendComplete(int result); | 220 int DoAuthChallengeSendComplete(int result); |
| 221 void DoAuthChallengeSendWriteComplete(int result); |
224 int DoAuthChallengeReplyComplete(int result); | 222 int DoAuthChallengeReplyComplete(int result); |
225 ///////////////////////////////////////////////////////////////////////////// | 223 ///////////////////////////////////////////////////////////////////////////// |
226 | 224 |
227 ///////////////////////////////////////////////////////////////////////////// | 225 ///////////////////////////////////////////////////////////////////////////// |
228 // Following methods work together to implement write flow. | 226 // Following methods work together to implement write flow. |
229 // | 227 // |
230 // Main method that performs write flow state transitions. | 228 // Main method that performs write flow state transitions. |
231 void DoWriteLoop(int result); | 229 void DoWriteLoop(int result); |
232 // Each of the below Do* method is executed in the corresponding | 230 // Each of the below Do* method is executed in the corresponding |
233 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE | 231 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE |
(...skipping 25 matching lines...) Expand all Loading... |
259 const net::CompletionCallback& callback); | 257 const net::CompletionCallback& callback); |
260 void PostTaskToStartConnectLoop(int result); | 258 void PostTaskToStartConnectLoop(int result); |
261 void PostTaskToStartReadLoop(); | 259 void PostTaskToStartReadLoop(); |
262 void StartReadLoop(); | 260 void StartReadLoop(); |
263 // Parses the contents of header_read_buffer_ and sets current_message_size_ | 261 // Parses the contents of header_read_buffer_ and sets current_message_size_ |
264 // to the size of the body of the message. | 262 // to the size of the body of the message. |
265 bool ProcessHeader(); | 263 bool ProcessHeader(); |
266 // Parses the contents of body_read_buffer_ and sets current_message_ to | 264 // Parses the contents of body_read_buffer_ and sets current_message_ to |
267 // the message received. | 265 // the message received. |
268 bool ProcessBody(); | 266 bool ProcessBody(); |
269 // Closes socket, updating the error state and signaling the delegate that | 267 // Closes the socket, sets |error_state_| to |error| and signals the |
270 // |error| has occurred. | 268 // delegate that |error| has occurred. |
271 void CloseWithError(ChannelError error); | 269 void CloseWithError(ChannelError error); |
| 270 // Frees resources and cancels pending callbacks. |ready_state_| will be set |
| 271 // READY_STATE_CLOSED on completion. A no-op if |ready_state_| is already |
| 272 // READY_STATE_CLOSED. |
| 273 void CloseInternal(); |
| 274 |
272 // Serializes the content of message_proto (with a header) to |message_data|. | 275 // Serializes the content of message_proto (with a header) to |message_data|. |
273 static bool Serialize(const CastMessage& message_proto, | 276 static bool Serialize(const CastMessage& message_proto, |
274 std::string* message_data); | 277 std::string* message_data); |
275 | 278 |
276 virtual bool CalledOnValidThread() const; | 279 virtual bool CalledOnValidThread() const; |
277 | 280 |
278 virtual base::Timer* GetTimer(); | 281 virtual base::Timer* GetTimer(); |
279 | 282 |
280 base::ThreadChecker thread_checker_; | 283 base::ThreadChecker thread_checker_; |
281 | 284 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 scoped_ptr<net::SSLClientSocket> socket_; | 320 scoped_ptr<net::SSLClientSocket> socket_; |
318 // Certificate of the peer. This field may be empty if the peer | 321 // Certificate of the peer. This field may be empty if the peer |
319 // certificate is not yet fetched. | 322 // certificate is not yet fetched. |
320 std::string peer_cert_; | 323 std::string peer_cert_; |
321 // Reply received from the receiver to a challenge request. | 324 // Reply received from the receiver to a challenge request. |
322 scoped_ptr<CastMessage> challenge_reply_; | 325 scoped_ptr<CastMessage> challenge_reply_; |
323 | 326 |
324 // Callback invoked when the socket is connected or fails to connect. | 327 // Callback invoked when the socket is connected or fails to connect. |
325 net::CompletionCallback connect_callback_; | 328 net::CompletionCallback connect_callback_; |
326 | 329 |
| 330 // Callback invoked by |connect_timeout_timer_| to cancel the connection. |
| 331 base::CancelableClosure connect_timeout_callback_; |
327 // Duration to wait before timing out. | 332 // Duration to wait before timing out. |
328 base::TimeDelta connect_timeout_; | 333 base::TimeDelta connect_timeout_; |
329 // Timer invoked when the connection has timed out. | 334 // Timer invoked when the connection has timed out. |
330 scoped_ptr<base::Timer> connect_timeout_timer_; | 335 scoped_ptr<base::Timer> connect_timeout_timer_; |
331 // Set when a timeout is triggered and the connection process has | 336 // Set when a timeout is triggered and the connection process has |
332 // canceled. | 337 // canceled. |
333 bool is_canceled_; | 338 bool is_canceled_; |
334 | 339 |
335 // Connection flow state machine state. | 340 // Connection flow state machine state. |
336 ConnectionState connect_state_; | 341 ConnectionState connect_state_; |
337 // Write flow state machine state. | 342 // Write flow state machine state. |
338 WriteState write_state_; | 343 WriteState write_state_; |
339 // Read flow state machine state. | 344 // Read flow state machine state. |
340 ReadState read_state_; | 345 ReadState read_state_; |
341 // The last error encountered by the channel. | 346 // The last error encountered by the channel. |
342 ChannelError error_state_; | 347 ChannelError error_state_; |
343 // The current status of the channel. | 348 // The current status of the channel. |
344 ReadyState ready_state_; | 349 ReadyState ready_state_; |
345 | 350 |
| 351 // Task invoked to (re)start the connect loop. Canceled on entry to the |
| 352 // connect loop. |
| 353 base::CancelableClosure connect_loop_callback_; |
| 354 // Task invoked to send the auth challenge. Canceled when the auth challenge |
| 355 // has been sent. |
| 356 base::CancelableClosure send_auth_challenge_callback_; |
| 357 // Callback invoked to (re)start the read loop. Canceled on entry to the read |
| 358 // loop. |
| 359 base::CancelableClosure read_loop_callback_; |
| 360 |
346 // Holds a message to be written to the socket. |callback| is invoked when the | 361 // Holds a message to be written to the socket. |callback| is invoked when the |
347 // message is fully written or an error occurrs. | 362 // message is fully written or an error occurrs. |
348 struct WriteRequest { | 363 struct WriteRequest { |
349 explicit WriteRequest(const net::CompletionCallback& callback); | 364 explicit WriteRequest(const net::CompletionCallback& callback); |
350 ~WriteRequest(); | 365 ~WriteRequest(); |
351 // Sets the content of the request by serializing |message| into |io_buffer| | 366 // Sets the content of the request by serializing |message| into |io_buffer| |
352 // and prepending the header. Must only be called once. | 367 // and prepending the header. Must only be called once. |
353 bool SetContent(const CastMessage& message_proto); | 368 bool SetContent(const CastMessage& message_proto); |
354 | 369 |
355 net::CompletionCallback callback; | 370 net::CompletionCallback callback; |
356 scoped_refptr<net::DrainableIOBuffer> io_buffer; | 371 scoped_refptr<net::DrainableIOBuffer> io_buffer; |
357 }; | 372 }; |
358 // Queue of pending writes. The message at the front of the queue is the one | 373 // Queue of pending writes. The message at the front of the queue is the one |
359 // being written. | 374 // being written. |
360 std::queue<WriteRequest> write_queue_; | 375 std::queue<WriteRequest> write_queue_; |
361 | 376 |
362 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestFullSecureConnectionFlowAsync); | 377 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestFullSecureConnectionFlowAsync); |
363 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead); | 378 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead); |
364 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadHeaderParseError); | 379 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadHeaderParseError); |
365 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); | 380 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); |
366 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestWriteErrorLargeMessage); | 381 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestWriteErrorLargeMessage); |
367 DISALLOW_COPY_AND_ASSIGN(CastSocket); | 382 DISALLOW_COPY_AND_ASSIGN(CastSocket); |
368 }; | 383 }; |
369 | 384 |
370 } // namespace cast_channel | 385 } // namespace cast_channel |
371 } // namespace core_api | 386 } // namespace core_api |
372 } // namespace extensions | 387 } // namespace extensions |
373 | 388 |
374 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ | 389 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ |
OLD | NEW |