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 |
(...skipping 24 matching lines...) Expand all Loading... |
35 namespace core_api { | 35 namespace core_api { |
36 namespace cast_channel { | 36 namespace cast_channel { |
37 | 37 |
38 class CastMessage; | 38 class CastMessage; |
39 class Logger; | 39 class Logger; |
40 struct LastErrors; | 40 struct LastErrors; |
41 class MessageFramer; | 41 class MessageFramer; |
42 | 42 |
43 // This class implements a channel between Chrome and a Cast device using a TCP | 43 // This class implements a channel between Chrome and a Cast device using a TCP |
44 // socket with SSL. The channel may authenticate that the receiver is a genuine | 44 // socket with SSL. The channel may authenticate that the receiver is a genuine |
45 // Cast device. All CastSocket objects must be used only on the IO thread. | 45 // Cast device. All CastSocketImpl objects must be used only on the IO thread. |
46 // | 46 // |
47 // NOTE: Not called "CastChannel" to reduce confusion with the generated API | 47 // NOTE: Not called "CastChannel" to reduce confusion with the generated API |
48 // code. | 48 // code. |
49 class CastSocket : public ApiResource { | 49 class CastSocketImpl : public ApiResource { |
50 public: | 50 public: |
51 // Object to be informed of incoming messages and errors. The CastSocket that | 51 // Object to be informed of incoming messages and errors. The CastSocketImpl |
| 52 // that |
52 // owns the delegate must not be deleted by it, only by the ApiResourceManager | 53 // owns the delegate must not be deleted by it, only by the ApiResourceManager |
53 // or in the callback to Close(). | 54 // or in the callback to Close(). |
54 class Delegate { | 55 class Delegate { |
55 public: | 56 public: |
56 // An error occurred on the channel. |last_errors| contains the last errors | 57 // An error occurred on the channel. |last_errors| contains the last errors |
57 // logged for the channel from the implementation. | 58 // logged for the channel from the implementation. |
58 virtual void OnError(const CastSocket* socket, | 59 virtual void OnError(const CastSocketImpl* socket, |
59 ChannelError error_state, | 60 ChannelError error_state, |
60 const LastErrors& last_errors) = 0; | 61 const LastErrors& last_errors) = 0; |
61 // A message was received on the channel. | 62 // A message was received on the channel. |
62 virtual void OnMessage(const CastSocket* socket, | 63 virtual void OnMessage(const CastSocketImpl* socket, |
63 const MessageInfo& message) = 0; | 64 const MessageInfo& message) = 0; |
64 | 65 |
65 protected: | 66 protected: |
66 virtual ~Delegate() {} | 67 virtual ~Delegate() {} |
67 }; | 68 }; |
68 | 69 |
69 // Creates a new CastSocket that connects to |ip_endpoint| with | 70 // Creates a new CastSocketImpl that connects to |ip_endpoint| with |
70 // |channel_auth|. |owner_extension_id| is the id of the extension that opened | 71 // |channel_auth|. |owner_extension_id| is the id of the extension that opened |
71 // the socket. |channel_auth| must not be CHANNEL_AUTH_NONE. | 72 // the socket. |channel_auth| must not be CHANNEL_AUTH_NONE. |
72 CastSocket(const std::string& owner_extension_id, | 73 CastSocketImpl(const std::string& owner_extension_id, |
73 const net::IPEndPoint& ip_endpoint, | 74 const net::IPEndPoint& ip_endpoint, |
74 ChannelAuthType channel_auth, | 75 ChannelAuthType channel_auth, |
75 CastSocket::Delegate* delegate, | 76 CastSocketImpl::Delegate* delegate, |
76 net::NetLog* net_log, | 77 net::NetLog* net_log, |
77 const base::TimeDelta& connect_timeout, | 78 const base::TimeDelta& connect_timeout, |
78 const scoped_refptr<Logger>& logger); | 79 const scoped_refptr<Logger>& logger); |
79 | 80 |
80 // Ensures that the socket is closed. | 81 // Ensures that the socket is closed. |
81 virtual ~CastSocket(); | 82 virtual ~CastSocketImpl(); |
82 | 83 |
83 // The IP endpoint for the destination of the channel. | 84 // The IP endpoint for the destination of the channel. |
84 const net::IPEndPoint& ip_endpoint() const { return ip_endpoint_; } | 85 const net::IPEndPoint& ip_endpoint() const { return ip_endpoint_; } |
85 | 86 |
86 // The authentication level requested for the channel. | 87 // The authentication level requested for the channel. |
87 ChannelAuthType channel_auth() const { return channel_auth_; } | 88 ChannelAuthType channel_auth() const { return channel_auth_; } |
88 | 89 |
89 // Returns a cast:// or casts:// URL for the channel endpoint. | 90 // Returns a cast:// or casts:// URL for the channel endpoint. |
90 // For backwards compatibility. | 91 // For backwards compatibility. |
91 std::string CastUrl() const; | 92 std::string CastUrl() const; |
92 | 93 |
93 // Channel id for the ApiResourceManager. | 94 // Channel id for the ApiResourceManager. |
94 int id() const { return channel_id_; } | 95 int id() const { return channel_id_; } |
95 | 96 |
96 // Sets the channel id. | 97 // Sets the channel id. |
97 void set_id(int channel_id) { channel_id_ = channel_id; } | 98 void set_id(int channel_id) { channel_id_ = channel_id; } |
98 | 99 |
99 // Returns the state of the channel. Virtual for testing. | 100 // Returns the state of the channel. Virtual for testing. |
100 virtual ReadyState ready_state() const; | 101 virtual ReadyState ready_state() const; |
101 | 102 |
102 // Returns the last error that occurred on this channel, or | 103 // Returns the last error that occurred on this channel, or |
103 // CHANNEL_ERROR_NONE if no error has occurred. Virtual for testing. | 104 // CHANNEL_ERROR_NONE if no error has occurred. Virtual for testing. |
104 virtual ChannelError error_state() const; | 105 virtual ChannelError error_state() const; |
105 | 106 |
106 // Connects the channel to the peer. If successful, the channel will be in | 107 // Connects the channel to the peer. If successful, the channel will be in |
107 // READY_STATE_OPEN. DO NOT delete the CastSocket object in |callback|. | 108 // READY_STATE_OPEN. DO NOT delete the CastSocketImpl object in |callback|. |
108 // Instead use Close(). | 109 // Instead use Close(). |
109 virtual void Connect(const net::CompletionCallback& callback); | 110 virtual void Connect(const net::CompletionCallback& callback); |
110 | 111 |
111 // Sends a message over a connected channel. The channel must be in | 112 // Sends a message over a connected channel. The channel must be in |
112 // READY_STATE_OPEN. | 113 // READY_STATE_OPEN. |
113 // | 114 // |
114 // Note that if an error occurs the following happens: | 115 // Note that if an error occurs the following happens: |
115 // 1. Completion callbacks for all pending writes are invoked with error. | 116 // 1. Completion callbacks for all pending writes are invoked with error. |
116 // 2. Delegate::OnError is called once. | 117 // 2. Delegate::OnError is called once. |
117 // 3. CastSocket is closed. | 118 // 3. CastSocketImpl is closed. |
118 // | 119 // |
119 // DO NOT delete the CastSocket object in |callback|. Instead use Close(). | 120 // DO NOT delete the CastSocketImpl object in |callback|. Instead use Close(). |
120 virtual void SendMessage(const MessageInfo& message, | 121 virtual void SendMessage(const MessageInfo& message, |
121 const net::CompletionCallback& callback); | 122 const net::CompletionCallback& callback); |
122 | 123 |
123 // Closes the channel if not already closed. On completion, the channel will | 124 // Closes the channel if not already closed. On completion, the channel will |
124 // be in READY_STATE_CLOSED. | 125 // be in READY_STATE_CLOSED. |
125 // | 126 // |
126 // It is fine to delete the CastSocket object in |callback|. | 127 // It is fine to delete the CastSocketImpl object in |callback|. |
127 virtual void Close(const net::CompletionCallback& callback); | 128 virtual void Close(const net::CompletionCallback& callback); |
128 | 129 |
129 // Internal connection states. | 130 // Internal connection states. |
130 enum ConnectionState { | 131 enum ConnectionState { |
131 CONN_STATE_NONE, | 132 CONN_STATE_NONE, |
132 CONN_STATE_TCP_CONNECT, | 133 CONN_STATE_TCP_CONNECT, |
133 CONN_STATE_TCP_CONNECT_COMPLETE, | 134 CONN_STATE_TCP_CONNECT_COMPLETE, |
134 CONN_STATE_SSL_CONNECT, | 135 CONN_STATE_SSL_CONNECT, |
135 CONN_STATE_SSL_CONNECT_COMPLETE, | 136 CONN_STATE_SSL_CONNECT_COMPLETE, |
136 CONN_STATE_AUTH_CHALLENGE_SEND, | 137 CONN_STATE_AUTH_CHALLENGE_SEND, |
(...skipping 13 matching lines...) Expand all Loading... |
150 // Internal read states. | 151 // Internal read states. |
151 enum ReadState { | 152 enum ReadState { |
152 READ_STATE_NONE, | 153 READ_STATE_NONE, |
153 READ_STATE_READ, | 154 READ_STATE_READ, |
154 READ_STATE_READ_COMPLETE, | 155 READ_STATE_READ_COMPLETE, |
155 READ_STATE_DO_CALLBACK, | 156 READ_STATE_DO_CALLBACK, |
156 READ_STATE_ERROR, | 157 READ_STATE_ERROR, |
157 }; | 158 }; |
158 | 159 |
159 private: | 160 private: |
160 friend class ApiResourceManager<CastSocket>; | 161 friend class ApiResourceManager<CastSocketImpl>; |
161 friend class CastSocketTest; | 162 friend class CastSocketImplTest; |
162 friend class TestCastSocket; | 163 friend class TestCastSocketImpl; |
163 | 164 |
164 static const char* service_name() { return "CastSocketManager"; } | 165 static const char* service_name() { return "CastSocketImplManager"; } |
165 | 166 |
166 // Creates an instance of TCPClientSocket. | 167 // Creates an instance of TCPClientSocket. |
167 virtual scoped_ptr<net::TCPClientSocket> CreateTcpSocket(); | 168 virtual scoped_ptr<net::TCPClientSocket> CreateTcpSocket(); |
168 // Creates an instance of SSLClientSocket with the given underlying |socket|. | 169 // Creates an instance of SSLClientSocket with the given underlying |socket|. |
169 virtual scoped_ptr<net::SSLClientSocket> CreateSslSocket( | 170 virtual scoped_ptr<net::SSLClientSocket> CreateSslSocket( |
170 scoped_ptr<net::StreamSocket> socket); | 171 scoped_ptr<net::StreamSocket> socket); |
171 // Extracts peer certificate from SSLClientSocket instance when the socket | 172 // Extracts peer certificate from SSLClientSocket instance when the socket |
172 // is in cert error state. | 173 // is in cert error state. |
173 // Returns whether certificate is successfully extracted. | 174 // Returns whether certificate is successfully extracted. |
174 virtual bool ExtractPeerCert(std::string* cert); | 175 virtual bool ExtractPeerCert(std::string* cert); |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 | 281 |
281 // IOBuffer for reading the message header. | 282 // IOBuffer for reading the message header. |
282 scoped_refptr<net::GrowableIOBuffer> read_buffer_; | 283 scoped_refptr<net::GrowableIOBuffer> read_buffer_; |
283 scoped_ptr<MessageFramer> framer_; | 284 scoped_ptr<MessageFramer> framer_; |
284 | 285 |
285 // The NetLog for this service. | 286 // The NetLog for this service. |
286 net::NetLog* net_log_; | 287 net::NetLog* net_log_; |
287 // The NetLog source for this service. | 288 // The NetLog source for this service. |
288 net::NetLog::Source net_log_source_; | 289 net::NetLog::Source net_log_source_; |
289 | 290 |
290 // Logger used to track multiple CastSockets. Does NOT own this object. | 291 // Logger used to track multiple CastSocketImpls. Does NOT own this object. |
291 scoped_refptr<Logger> logger_; | 292 scoped_refptr<Logger> logger_; |
292 | 293 |
293 // CertVerifier is owned by us but should be deleted AFTER SSLClientSocket | 294 // CertVerifier is owned by us but should be deleted AFTER SSLClientSocket |
294 // since in some cases the destructor of SSLClientSocket may call a method | 295 // since in some cases the destructor of SSLClientSocket may call a method |
295 // to cancel a cert verification request. | 296 // to cancel a cert verification request. |
296 scoped_ptr<net::CertVerifier> cert_verifier_; | 297 scoped_ptr<net::CertVerifier> cert_verifier_; |
297 scoped_ptr<net::TransportSecurityState> transport_security_state_; | 298 scoped_ptr<net::TransportSecurityState> transport_security_state_; |
298 | 299 |
299 // Owned ptr to the underlying TCP socket. | 300 // Owned ptr to the underlying TCP socket. |
300 scoped_ptr<net::TCPClientSocket> tcp_socket_; | 301 scoped_ptr<net::TCPClientSocket> tcp_socket_; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 bool SetContent(const CastMessage& message_proto); | 353 bool SetContent(const CastMessage& message_proto); |
353 | 354 |
354 net::CompletionCallback callback; | 355 net::CompletionCallback callback; |
355 std::string message_namespace; | 356 std::string message_namespace; |
356 scoped_refptr<net::DrainableIOBuffer> io_buffer; | 357 scoped_refptr<net::DrainableIOBuffer> io_buffer; |
357 }; | 358 }; |
358 // Queue of pending writes. The message at the front of the queue is the one | 359 // Queue of pending writes. The message at the front of the queue is the one |
359 // being written. | 360 // being written. |
360 std::queue<WriteRequest> write_queue_; | 361 std::queue<WriteRequest> write_queue_; |
361 | 362 |
362 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestFullSecureConnectionFlowAsync); | 363 FRIEND_TEST_ALL_PREFIXES(CastSocketImplTest, |
363 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead); | 364 TestFullSecureConnectionFlowAsync); |
364 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadHeaderParseError); | 365 FRIEND_TEST_ALL_PREFIXES(CastSocketImplTest, TestRead); |
365 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); | 366 FRIEND_TEST_ALL_PREFIXES(CastSocketImplTest, TestReadHeaderParseError); |
366 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestWriteErrorLargeMessage); | 367 FRIEND_TEST_ALL_PREFIXES(CastSocketImplTest, TestReadMany); |
367 DISALLOW_COPY_AND_ASSIGN(CastSocket); | 368 FRIEND_TEST_ALL_PREFIXES(CastSocketImplTest, TestWriteErrorLargeMessage); |
| 369 DISALLOW_COPY_AND_ASSIGN(CastSocketImpl); |
368 }; | 370 }; |
369 } // namespace cast_channel | 371 } // namespace cast_channel |
370 } // namespace core_api | 372 } // namespace core_api |
371 } // namespace extensions | 373 } // namespace extensions |
372 | 374 |
373 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ | 375 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ |
OLD | NEW |