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" | 12 #include "base/callback_forward.h" |
| 13 #include "base/cancelable_callback.h" | 13 #include "base/cancelable_callback.h" |
|
Kevin M
2014/07/28 22:08:17
Not used?
mark a. foltz
2014/07/29 00:16:25
Done.
| |
| 14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/weak_ptr.h" | |
| 17 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
| 18 #include "base/timer/timer.h" | 17 #include "base/timer/timer.h" |
| 19 #include "extensions/browser/api/api_resource.h" | 18 #include "extensions/browser/api/api_resource.h" |
| 20 #include "extensions/browser/api/api_resource_manager.h" | 19 #include "extensions/browser/api/api_resource_manager.h" |
| 21 #include "extensions/common/api/cast_channel.h" | 20 #include "extensions/common/api/cast_channel.h" |
| 22 #include "net/base/completion_callback.h" | 21 #include "net/base/completion_callback.h" |
| 23 #include "net/base/io_buffer.h" | 22 #include "net/base/io_buffer.h" |
| 24 #include "net/base/ip_endpoint.h" | 23 #include "net/base/ip_endpoint.h" |
| 25 #include "net/base/net_log.h" | 24 #include "net/base/net_log.h" |
| 26 | 25 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 38 namespace cast_channel { | 37 namespace cast_channel { |
| 39 | 38 |
| 40 class CastMessage; | 39 class CastMessage; |
| 41 | 40 |
| 42 // This class implements a channel between Chrome and a Cast device using a TCP | 41 // 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 | 42 // 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. | 43 // Cast device. All CastSocket objects must be used only on the IO thread. |
| 45 // | 44 // |
| 46 // NOTE: Not called "CastChannel" to reduce confusion with the generated API | 45 // NOTE: Not called "CastChannel" to reduce confusion with the generated API |
| 47 // code. | 46 // code. |
| 48 class CastSocket : public ApiResource, | 47 class CastSocket : public ApiResource { |
| 49 public base::SupportsWeakPtr<CastSocket> { | |
| 50 public: | 48 public: |
| 51 // Object to be informed of incoming messages and errors. | 49 // Object to be informed of incoming messages and errors. |
| 52 class Delegate { | 50 class Delegate { |
| 53 public: | 51 public: |
| 54 // An error occurred on the channel. | 52 // An error occurred on the channel. |
| 55 // It is fine to delete the socket in this callback. | 53 // It is fine to delete the socket in this callback. |
| 56 virtual void OnError(const CastSocket* socket, ChannelError error) = 0; | 54 virtual void OnError(const CastSocket* socket, ChannelError error) = 0; |
| 57 // A message was received on the channel. | 55 // A message was received on the channel. |
| 58 // Do NOT delete the socket in this callback. | 56 // Do NOT delete the socket in this callback. |
| 59 virtual void OnMessage(const CastSocket* socket, | 57 virtual void OnMessage(const CastSocket* socket, |
| 60 const MessageInfo& message) = 0; | 58 const MessageInfo& message) = 0; |
| 61 | 59 |
| 62 protected: | 60 protected: |
| 63 virtual ~Delegate() {} | 61 virtual ~Delegate() {} |
| 64 }; | 62 }; |
| 65 | 63 |
| 66 // Creates a new CastSocket that connects to |ip_endpoint| with | 64 // Creates a new CastSocket that connects to |ip_endpoint| with |
| 67 // |channel_auth|. |owner_extension_id| is the id of the extension that opened | 65 // |channel_auth|. |owner_extension_id| is the id of the extension that opened |
| 68 // the socket. |channel_auth| must not be CHANNEL_AUTH_NONE. | 66 // the socket. |channel_auth| must not be CHANNEL_AUTH_NONE. |
| 69 CastSocket(const std::string& owner_extension_id, | 67 CastSocket(const std::string& owner_extension_id, |
| 70 const net::IPEndPoint& ip_endpoint, | 68 const net::IPEndPoint& ip_endpoint, |
| 71 ChannelAuthType channel_auth, | 69 ChannelAuthType channel_auth, |
| 72 CastSocket::Delegate* delegate, | 70 CastSocket::Delegate* delegate, |
| 73 net::NetLog* net_log, | 71 net::NetLog* net_log, |
| 74 const base::TimeDelta& connect_timeout); | 72 const base::TimeDelta& connect_timeout); |
| 73 | |
| 74 // Ensures that the socket is closed. | |
|
Wez
2014/07/28 23:14:57
Not sure what this comment adds?
mark a. foltz
2014/07/29 00:16:25
An invariant.
| |
| 75 virtual ~CastSocket(); | 75 virtual ~CastSocket(); |
| 76 | 76 |
| 77 // The IP endpoint for the destination of the channel. | 77 // The IP endpoint for the destination of the channel. |
| 78 const net::IPEndPoint& ip_endpoint() const { return ip_endpoint_; } | 78 const net::IPEndPoint& ip_endpoint() const { return ip_endpoint_; } |
| 79 | 79 |
| 80 // The authentication level requested for the channel. | 80 // The authentication level requested for the channel. |
| 81 ChannelAuthType channel_auth() const { return channel_auth_; } | 81 ChannelAuthType channel_auth() const { return channel_auth_; } |
| 82 | 82 |
| 83 // Returns a cast:// or casts:// URL for the channel endpoint. | 83 // Returns a cast:// or casts:// URL for the channel endpoint. |
| 84 // For backwards compatibility. | 84 // For backwards compatibility. |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 // Closes sockets, frees resources, and cancels pending callbacks. | |
|
Wez
2014/07/28 23:14:56
nit: underlying sockets?
Does "Closes sockets" no
mark a. foltz
2014/07/29 00:16:25
Done.
| |
| 273 // |ready_state_| will be set READY_STATE_CLOSED on completion. A no-op if | |
| 274 // |ready_state_| is already 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. | |
| 333 base::Closure cancel_connect_callback_; | |
| 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. | |
| 354 base::Closure connect_loop_callback_; | |
| 355 // Callback invoked to send the auth challenge. | |
| 356 base::Closure send_auth_challenge_callback_; | |
| 357 // Callback invoked to (re)start the read loop. | |
| 358 base::Closure 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 |