| OLD | NEW |
| 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" |
| 16 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 18 #include "base/timer/timer.h" |
| 17 #include "chrome/common/extensions/api/cast_channel.h" | 19 #include "chrome/common/extensions/api/cast_channel.h" |
| 18 #include "extensions/browser/api/api_resource.h" | 20 #include "extensions/browser/api/api_resource.h" |
| 19 #include "extensions/browser/api/api_resource_manager.h" | 21 #include "extensions/browser/api/api_resource_manager.h" |
| 20 #include "net/base/completion_callback.h" | 22 #include "net/base/completion_callback.h" |
| 21 #include "net/base/io_buffer.h" | 23 #include "net/base/io_buffer.h" |
| 22 #include "net/base/ip_endpoint.h" | 24 #include "net/base/ip_endpoint.h" |
| 23 #include "net/base/net_log.h" | 25 #include "net/base/net_log.h" |
| 24 | 26 |
| 25 namespace net { | 27 namespace net { |
| 26 class AddressList; | 28 class AddressList; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 virtual ~Delegate() {} | 63 virtual ~Delegate() {} |
| 62 }; | 64 }; |
| 63 | 65 |
| 64 // Creates a new CastSocket that connects to |ip_endpoint| with | 66 // Creates a new CastSocket that connects to |ip_endpoint| with |
| 65 // |channel_auth|. |owner_extension_id| is the id of the extension that opened | 67 // |channel_auth|. |owner_extension_id| is the id of the extension that opened |
| 66 // the socket. |channel_auth| must not be CHANNEL_AUTH_NONE. | 68 // the socket. |channel_auth| must not be CHANNEL_AUTH_NONE. |
| 67 CastSocket(const std::string& owner_extension_id, | 69 CastSocket(const std::string& owner_extension_id, |
| 68 const net::IPEndPoint& ip_endpoint, | 70 const net::IPEndPoint& ip_endpoint, |
| 69 ChannelAuthType channel_auth, | 71 ChannelAuthType channel_auth, |
| 70 CastSocket::Delegate* delegate, | 72 CastSocket::Delegate* delegate, |
| 71 net::NetLog* net_log); | 73 net::NetLog* net_log, |
| 74 const base::TimeDelta& connect_timeout); |
| 72 virtual ~CastSocket(); | 75 virtual ~CastSocket(); |
| 73 | 76 |
| 74 // The IP endpoint for the destination of the channel. | 77 // The IP endpoint for the destination of the channel. |
| 75 const net::IPEndPoint& ip_endpoint() const { return ip_endpoint_; } | 78 const net::IPEndPoint& ip_endpoint() const { return ip_endpoint_; } |
| 76 | 79 |
| 77 // The authentication level requested for the channel. | 80 // The authentication level requested for the channel. |
| 78 ChannelAuthType channel_auth() const { return channel_auth_; } | 81 ChannelAuthType channel_auth() const { return channel_auth_; } |
| 79 | 82 |
| 80 // Returns a cast:// or casts:// URL for the channel endpoint. | 83 // Returns a cast:// or casts:// URL for the channel endpoint. |
| 81 // For backwards compatibility. | 84 // For backwards compatibility. |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 static uint32 max_message_size() { return 65536; } | 140 static uint32 max_message_size() { return 65536; } |
| 138 | 141 |
| 139 std::string ToString(); | 142 std::string ToString(); |
| 140 // The size of the following protocol message in bytes, in host byte order. | 143 // The size of the following protocol message in bytes, in host byte order. |
| 141 uint32 message_size; | 144 uint32 message_size; |
| 142 }; | 145 }; |
| 143 | 146 |
| 144 private: | 147 private: |
| 145 friend class ApiResourceManager<CastSocket>; | 148 friend class ApiResourceManager<CastSocket>; |
| 146 friend class CastSocketTest; | 149 friend class CastSocketTest; |
| 150 friend class TestCastSocket; |
| 147 | 151 |
| 148 static const char* service_name() { return "CastSocketManager"; } | 152 static const char* service_name() { return "CastSocketManager"; } |
| 149 | 153 |
| 150 // Internal connection states. | 154 // Internal connection states. |
| 151 enum ConnectionState { | 155 enum ConnectionState { |
| 152 CONN_STATE_NONE, | 156 CONN_STATE_NONE, |
| 153 CONN_STATE_TCP_CONNECT, | 157 CONN_STATE_TCP_CONNECT, |
| 154 CONN_STATE_TCP_CONNECT_COMPLETE, | 158 CONN_STATE_TCP_CONNECT_COMPLETE, |
| 155 CONN_STATE_SSL_CONNECT, | 159 CONN_STATE_SSL_CONNECT, |
| 156 CONN_STATE_SSL_CONNECT_COMPLETE, | 160 CONN_STATE_SSL_CONNECT_COMPLETE, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 184 scoped_ptr<net::StreamSocket> socket); | 188 scoped_ptr<net::StreamSocket> socket); |
| 185 // Extracts peer certificate from SSLClientSocket instance when the socket | 189 // Extracts peer certificate from SSLClientSocket instance when the socket |
| 186 // is in cert error state. | 190 // is in cert error state. |
| 187 // Returns whether certificate is successfully extracted. | 191 // Returns whether certificate is successfully extracted. |
| 188 virtual bool ExtractPeerCert(std::string* cert); | 192 virtual bool ExtractPeerCert(std::string* cert); |
| 189 // Verifies whether the challenge reply received from the peer is valid: | 193 // Verifies whether the challenge reply received from the peer is valid: |
| 190 // 1. Signature in the reply is valid. | 194 // 1. Signature in the reply is valid. |
| 191 // 2. Certificate is rooted to a trusted CA. | 195 // 2. Certificate is rooted to a trusted CA. |
| 192 virtual bool VerifyChallengeReply(); | 196 virtual bool VerifyChallengeReply(); |
| 193 | 197 |
| 198 // Invoked by a cancelable closure when connection setup time |
| 199 // exceeds the interval specified at |connect_timeout|. |
| 200 void CancelConnect(); |
| 201 |
| 194 ///////////////////////////////////////////////////////////////////////////// | 202 ///////////////////////////////////////////////////////////////////////////// |
| 195 // Following methods work together to implement the following flow: | 203 // Following methods work together to implement the following flow: |
| 196 // 1. Create a new TCP socket and connect to it | 204 // 1. Create a new TCP socket and connect to it |
| 197 // 2. Create a new SSL socket and try connecting to it | 205 // 2. Create a new SSL socket and try connecting to it |
| 198 // 3. If connection fails due to invalid cert authority, then extract the | 206 // 3. If connection fails due to invalid cert authority, then extract the |
| 199 // peer certificate from the error. | 207 // peer certificate from the error. |
| 200 // 4. Whitelist the peer certificate and try #1 and #2 again. | 208 // 4. Whitelist the peer certificate and try #1 and #2 again. |
| 201 // 5. If SSL socket is connected successfully, and if protocol is casts:// | 209 // 5. If SSL socket is connected successfully, and if protocol is casts:// |
| 202 // then issue an auth challenge request. | 210 // then issue an auth challenge request. |
| 203 // 6. Validate the auth challenge response. | 211 // 6. Validate the auth challenge response. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 bool ProcessBody(); | 268 bool ProcessBody(); |
| 261 // Closes socket, updating the error state and signaling the delegate that | 269 // Closes socket, updating the error state and signaling the delegate that |
| 262 // |error| has occurred. | 270 // |error| has occurred. |
| 263 void CloseWithError(ChannelError error); | 271 void CloseWithError(ChannelError error); |
| 264 // Serializes the content of message_proto (with a header) to |message_data|. | 272 // Serializes the content of message_proto (with a header) to |message_data|. |
| 265 static bool Serialize(const CastMessage& message_proto, | 273 static bool Serialize(const CastMessage& message_proto, |
| 266 std::string* message_data); | 274 std::string* message_data); |
| 267 | 275 |
| 268 virtual bool CalledOnValidThread() const; | 276 virtual bool CalledOnValidThread() const; |
| 269 | 277 |
| 278 virtual base::Timer* GetTimer(); |
| 279 |
| 270 base::ThreadChecker thread_checker_; | 280 base::ThreadChecker thread_checker_; |
| 271 | 281 |
| 272 // The id of the channel. | 282 // The id of the channel. |
| 273 int channel_id_; | 283 int channel_id_; |
| 274 | 284 |
| 275 // The IP endpoint that the the channel is connected to. | 285 // The IP endpoint that the the channel is connected to. |
| 276 net::IPEndPoint ip_endpoint_; | 286 net::IPEndPoint ip_endpoint_; |
| 277 // Receiver authentication requested for the channel. | 287 // Receiver authentication requested for the channel. |
| 278 ChannelAuthType channel_auth_; | 288 ChannelAuthType channel_auth_; |
| 279 // Delegate to inform of incoming messages and errors. | 289 // Delegate to inform of incoming messages and errors. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 304 // Owned ptr to the underlying TCP socket. | 314 // Owned ptr to the underlying TCP socket. |
| 305 scoped_ptr<net::TCPClientSocket> tcp_socket_; | 315 scoped_ptr<net::TCPClientSocket> tcp_socket_; |
| 306 // Owned ptr to the underlying SSL socket. | 316 // Owned ptr to the underlying SSL socket. |
| 307 scoped_ptr<net::SSLClientSocket> socket_; | 317 scoped_ptr<net::SSLClientSocket> socket_; |
| 308 // Certificate of the peer. This field may be empty if the peer | 318 // Certificate of the peer. This field may be empty if the peer |
| 309 // certificate is not yet fetched. | 319 // certificate is not yet fetched. |
| 310 std::string peer_cert_; | 320 std::string peer_cert_; |
| 311 // Reply received from the receiver to a challenge request. | 321 // Reply received from the receiver to a challenge request. |
| 312 scoped_ptr<CastMessage> challenge_reply_; | 322 scoped_ptr<CastMessage> challenge_reply_; |
| 313 | 323 |
| 314 // Callback invoked when the socket is connected. | 324 // Callback invoked when the socket is connected or fails to connect. |
| 315 net::CompletionCallback connect_callback_; | 325 net::CompletionCallback connect_callback_; |
| 316 | 326 |
| 327 // Duration to wait before timing out. |
| 328 base::TimeDelta connect_timeout_; |
| 329 // Timer invoked when the connection has timed out. |
| 330 scoped_ptr<base::Timer> connect_timeout_timer_; |
| 331 // Set when a timeout is triggered and the connection process has |
| 332 // canceled. |
| 333 bool is_canceled_; |
| 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 Loading... |
| 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_ |
| OLD | NEW |