Chromium Code Reviews| 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. | 52 // It is fine to delete the socket in this callback. |
| 56 virtual void OnError(const CastSocket* socket, ChannelError error) = 0; | 53 virtual void OnError(const CastSocket* socket, ChannelError error) = 0; |
| 57 // A message was received on the channel. | 54 // A message was received on the channel. |
| 58 // Do NOT delete the socket in this callback. | 55 // Do NOT delete the socket in this callback. |
|
Wez
2014/07/31 22:57:53
Is it now always OK to delete the socket from thes
mark a. foltz
2014/08/01 23:21:48
Removed.
| |
| 59 virtual void OnMessage(const CastSocket* socket, | 56 virtual void OnMessage(const CastSocket* socket, |
| 60 const MessageInfo& message) = 0; | 57 const MessageInfo& message) = 0; |
| 61 | 58 |
| 62 protected: | 59 protected: |
| 63 virtual ~Delegate() {} | 60 virtual ~Delegate() {} |
| 64 }; | 61 }; |
| 65 | 62 |
| 66 // Creates a new CastSocket that connects to |ip_endpoint| with | 63 // Creates a new CastSocket that connects to |ip_endpoint| with |
| 67 // |channel_auth|. |owner_extension_id| is the id of the extension that opened | 64 // |channel_auth|. |owner_extension_id| is the id of the extension that opened |
| 68 // the socket. |channel_auth| must not be CHANNEL_AUTH_NONE. | 65 // the socket. |channel_auth| must not be CHANNEL_AUTH_NONE. |
| 69 CastSocket(const std::string& owner_extension_id, | 66 CastSocket(const std::string& owner_extension_id, |
| 70 const net::IPEndPoint& ip_endpoint, | 67 const net::IPEndPoint& ip_endpoint, |
| 71 ChannelAuthType channel_auth, | 68 ChannelAuthType channel_auth, |
| 72 CastSocket::Delegate* delegate, | 69 CastSocket::Delegate* delegate, |
| 73 net::NetLog* net_log, | 70 net::NetLog* net_log, |
| 74 const base::TimeDelta& connect_timeout); | 71 const base::TimeDelta& connect_timeout); |
| 72 | |
| 73 // Ensures that the socket is closed. | |
| 75 virtual ~CastSocket(); | 74 virtual ~CastSocket(); |
| 76 | 75 |
| 77 // The IP endpoint for the destination of the channel. | 76 // The IP endpoint for the destination of the channel. |
| 78 const net::IPEndPoint& ip_endpoint() const { return ip_endpoint_; } | 77 const net::IPEndPoint& ip_endpoint() const { return ip_endpoint_; } |
| 79 | 78 |
| 80 // The authentication level requested for the channel. | 79 // The authentication level requested for the channel. |
| 81 ChannelAuthType channel_auth() const { return channel_auth_; } | 80 ChannelAuthType channel_auth() const { return channel_auth_; } |
| 82 | 81 |
| 83 // Returns a cast:// or casts:// URL for the channel endpoint. | 82 // Returns a cast:// or casts:// URL for the channel endpoint. |
| 84 // For backwards compatibility. | 83 // For backwards compatibility. |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 void DoConnectLoop(int result); | 213 void DoConnectLoop(int result); |
| 215 // Each of the below Do* method is executed in the corresponding | 214 // Each of the below Do* method is executed in the corresponding |
| 216 // connection state. For example when connection state is TCP_CONNECT | 215 // connection state. For example when connection state is TCP_CONNECT |
| 217 // DoTcpConnect is called, and so on. | 216 // DoTcpConnect is called, and so on. |
| 218 int DoTcpConnect(); | 217 int DoTcpConnect(); |
| 219 int DoTcpConnectComplete(int result); | 218 int DoTcpConnectComplete(int result); |
| 220 int DoSslConnect(); | 219 int DoSslConnect(); |
| 221 int DoSslConnectComplete(int result); | 220 int DoSslConnectComplete(int result); |
| 222 int DoAuthChallengeSend(); | 221 int DoAuthChallengeSend(); |
| 223 int DoAuthChallengeSendComplete(int result); | 222 int DoAuthChallengeSendComplete(int result); |
| 223 void DoAuthChallengeSendWriteComplete(int result); | |
| 224 int DoAuthChallengeReplyComplete(int result); | 224 int DoAuthChallengeReplyComplete(int result); |
| 225 ///////////////////////////////////////////////////////////////////////////// | 225 ///////////////////////////////////////////////////////////////////////////// |
| 226 | 226 |
| 227 ///////////////////////////////////////////////////////////////////////////// | 227 ///////////////////////////////////////////////////////////////////////////// |
| 228 // Following methods work together to implement write flow. | 228 // Following methods work together to implement write flow. |
| 229 // | 229 // |
| 230 // Main method that performs write flow state transitions. | 230 // Main method that performs write flow state transitions. |
| 231 void DoWriteLoop(int result); | 231 void DoWriteLoop(int result); |
| 232 // Each of the below Do* method is executed in the corresponding | 232 // Each of the below Do* method is executed in the corresponding |
| 233 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE | 233 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 259 const net::CompletionCallback& callback); | 259 const net::CompletionCallback& callback); |
| 260 void PostTaskToStartConnectLoop(int result); | 260 void PostTaskToStartConnectLoop(int result); |
| 261 void PostTaskToStartReadLoop(); | 261 void PostTaskToStartReadLoop(); |
| 262 void StartReadLoop(); | 262 void StartReadLoop(); |
| 263 // Parses the contents of header_read_buffer_ and sets current_message_size_ | 263 // Parses the contents of header_read_buffer_ and sets current_message_size_ |
| 264 // to the size of the body of the message. | 264 // to the size of the body of the message. |
| 265 bool ProcessHeader(); | 265 bool ProcessHeader(); |
| 266 // Parses the contents of body_read_buffer_ and sets current_message_ to | 266 // Parses the contents of body_read_buffer_ and sets current_message_ to |
| 267 // the message received. | 267 // the message received. |
| 268 bool ProcessBody(); | 268 bool ProcessBody(); |
| 269 // Closes socket, updating the error state and signaling the delegate that | 269 // Closes the socket, sets |error_state_| to |error| and signals the |
| 270 // |error| has occurred. | 270 // delegate that |error| has occurred. |
| 271 void CloseWithError(ChannelError error); | 271 void CloseWithError(ChannelError error); |
| 272 // Frees resources and cancels pending callbacks. |ready_state_| will be set | |
| 273 // READY_STATE_CLOSED on completion. A no-op if |ready_state_| is already | |
| 274 // READY_STATE_CLOSED. | |
| 275 void CloseInternal(); | |
| 276 | |
| 272 // Serializes the content of message_proto (with a header) to |message_data|. | 277 // Serializes the content of message_proto (with a header) to |message_data|. |
| 273 static bool Serialize(const CastMessage& message_proto, | 278 static bool Serialize(const CastMessage& message_proto, |
| 274 std::string* message_data); | 279 std::string* message_data); |
| 275 | 280 |
| 276 virtual bool CalledOnValidThread() const; | 281 virtual bool CalledOnValidThread() const; |
| 277 | 282 |
| 278 virtual base::Timer* GetTimer(); | 283 virtual base::Timer* GetTimer(); |
| 279 | 284 |
| 280 base::ThreadChecker thread_checker_; | 285 base::ThreadChecker thread_checker_; |
| 281 | 286 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 scoped_ptr<net::SSLClientSocket> socket_; | 322 scoped_ptr<net::SSLClientSocket> socket_; |
| 318 // Certificate of the peer. This field may be empty if the peer | 323 // Certificate of the peer. This field may be empty if the peer |
| 319 // certificate is not yet fetched. | 324 // certificate is not yet fetched. |
| 320 std::string peer_cert_; | 325 std::string peer_cert_; |
| 321 // Reply received from the receiver to a challenge request. | 326 // Reply received from the receiver to a challenge request. |
| 322 scoped_ptr<CastMessage> challenge_reply_; | 327 scoped_ptr<CastMessage> challenge_reply_; |
| 323 | 328 |
| 324 // Callback invoked when the socket is connected or fails to connect. | 329 // Callback invoked when the socket is connected or fails to connect. |
| 325 net::CompletionCallback connect_callback_; | 330 net::CompletionCallback connect_callback_; |
| 326 | 331 |
| 332 // Callback invoked to cancel the connection. | |
|
Wez
2014/07/30 19:14:53
nit: Invoked by whom?
mark a. foltz
2014/07/31 18:33:17
Done.
| |
| 333 base::CancelableClosure cancel_connect_callback_; | |
|
Wez
2014/07/30 19:14:53
nit: Consider connect_timeout_callback_
mark a. foltz
2014/07/31 18:33:17
Done.
| |
| 327 // Duration to wait before timing out. | 334 // Duration to wait before timing out. |
| 328 base::TimeDelta connect_timeout_; | 335 base::TimeDelta connect_timeout_; |
| 329 // Timer invoked when the connection has timed out. | 336 // Timer invoked when the connection has timed out. |
| 330 scoped_ptr<base::Timer> connect_timeout_timer_; | 337 scoped_ptr<base::Timer> connect_timeout_timer_; |
| 331 // Set when a timeout is triggered and the connection process has | 338 // Set when a timeout is triggered and the connection process has |
| 332 // canceled. | 339 // canceled. |
| 333 bool is_canceled_; | 340 bool is_canceled_; |
| 334 | 341 |
| 335 // Connection flow state machine state. | 342 // Connection flow state machine state. |
| 336 ConnectionState connect_state_; | 343 ConnectionState connect_state_; |
| 337 // Write flow state machine state. | 344 // Write flow state machine state. |
| 338 WriteState write_state_; | 345 WriteState write_state_; |
| 339 // Read flow state machine state. | 346 // Read flow state machine state. |
| 340 ReadState read_state_; | 347 ReadState read_state_; |
| 341 // The last error encountered by the channel. | 348 // The last error encountered by the channel. |
| 342 ChannelError error_state_; | 349 ChannelError error_state_; |
| 343 // The current status of the channel. | 350 // The current status of the channel. |
| 344 ReadyState ready_state_; | 351 ReadyState ready_state_; |
| 345 | 352 |
| 353 // Callback invoked to (re)start the connect loop. | |
|
Wez
2014/07/30 19:14:53
nit: Clarify when these callbacks is are in the "c
mark a. foltz
2014/07/31 18:33:17
Done.
| |
| 354 base::CancelableClosure connect_loop_callback_; | |
| 355 // Callback invoked after the auth challenge has been sent. | |
| 356 base::CancelableClosure send_auth_challenge_callback_; | |
| 357 // Callback invoked to (re)start the read loop. | |
| 358 base::CancelableClosure read_loop_callback_; | |
| 359 | |
| 346 // Holds a message to be written to the socket. |callback| is invoked when the | 360 // Holds a message to be written to the socket. |callback| is invoked when the |
| 347 // message is fully written or an error occurrs. | 361 // message is fully written or an error occurrs. |
| 348 struct WriteRequest { | 362 struct WriteRequest { |
| 349 explicit WriteRequest(const net::CompletionCallback& callback); | 363 explicit WriteRequest(const net::CompletionCallback& callback); |
| 350 ~WriteRequest(); | 364 ~WriteRequest(); |
| 351 // Sets the content of the request by serializing |message| into |io_buffer| | 365 // Sets the content of the request by serializing |message| into |io_buffer| |
| 352 // and prepending the header. Must only be called once. | 366 // and prepending the header. Must only be called once. |
| 353 bool SetContent(const CastMessage& message_proto); | 367 bool SetContent(const CastMessage& message_proto); |
| 354 | 368 |
| 355 net::CompletionCallback callback; | 369 net::CompletionCallback callback; |
| 356 scoped_refptr<net::DrainableIOBuffer> io_buffer; | 370 scoped_refptr<net::DrainableIOBuffer> io_buffer; |
| 357 }; | 371 }; |
| 358 // Queue of pending writes. The message at the front of the queue is the one | 372 // Queue of pending writes. The message at the front of the queue is the one |
| 359 // being written. | 373 // being written. |
| 360 std::queue<WriteRequest> write_queue_; | 374 std::queue<WriteRequest> write_queue_; |
| 361 | 375 |
| 362 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestFullSecureConnectionFlowAsync); | 376 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestFullSecureConnectionFlowAsync); |
| 363 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead); | 377 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead); |
| 364 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadHeaderParseError); | 378 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadHeaderParseError); |
| 365 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); | 379 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); |
| 366 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestWriteErrorLargeMessage); | 380 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestWriteErrorLargeMessage); |
| 367 DISALLOW_COPY_AND_ASSIGN(CastSocket); | 381 DISALLOW_COPY_AND_ASSIGN(CastSocket); |
| 368 }; | 382 }; |
| 369 | 383 |
| 370 } // namespace cast_channel | 384 } // namespace cast_channel |
| 371 } // namespace core_api | 385 } // namespace core_api |
| 372 } // namespace extensions | 386 } // namespace extensions |
| 373 | 387 |
| 374 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ | 388 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ |
| OLD | NEW |