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. The CastSocket that |
| 49 // owns the delegate must not be deleted by it, only by the ApiResourceManager |
| 50 // or in the callback to Close(). |
52 class Delegate { | 51 class Delegate { |
53 public: | 52 public: |
54 // An error occurred on the channel. | 53 // 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; | 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. | |
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. |
85 std::string CastUrl() const; | 84 std::string CastUrl() const; |
86 | 85 |
87 // Channel id for the ApiResourceManager. | 86 // Channel id for the ApiResourceManager. |
88 int id() const { return channel_id_; } | 87 int id() const { return channel_id_; } |
89 | 88 |
90 // Sets the channel id. | 89 // Sets the channel id. |
91 void set_id(int channel_id) { channel_id_ = channel_id; } | 90 void set_id(int channel_id) { channel_id_ = channel_id; } |
92 | 91 |
93 // Returns the state of the channel. Virtual for testing. | 92 // Returns the state of the channel. Virtual for testing. |
94 virtual ReadyState ready_state() const; | 93 virtual ReadyState ready_state() const; |
95 | 94 |
96 // Returns the last error that occurred on this channel, or | 95 // Returns the last error that occurred on this channel, or |
97 // CHANNEL_ERROR_NONE if no error has occurred. Virtual for testing. | 96 // CHANNEL_ERROR_NONE if no error has occurred. Virtual for testing. |
98 virtual ChannelError error_state() const; | 97 virtual ChannelError error_state() const; |
99 | 98 |
100 // Connects the channel to the peer. If successful, the channel will be in | 99 // Connects the channel to the peer. If successful, the channel will be in |
101 // READY_STATE_OPEN. | 100 // READY_STATE_OPEN. DO NOT delete the CastSocket object in |callback|. |
102 // It is fine to delete the CastSocket object in |callback|. | 101 // Instead use Close(). |
103 virtual void Connect(const net::CompletionCallback& callback); | 102 virtual void Connect(const net::CompletionCallback& callback); |
104 | 103 |
105 // Sends a message over a connected channel. The channel must be in | 104 // Sends a message over a connected channel. The channel must be in |
106 // READY_STATE_OPEN. | 105 // READY_STATE_OPEN. |
107 // | 106 // |
108 // Note that if an error occurs the following happens: | 107 // Note that if an error occurs the following happens: |
109 // 1. Completion callbacks for all pending writes are invoked with error. | 108 // 1. Completion callbacks for all pending writes are invoked with error. |
110 // 2. Delegate::OnError is called once. | 109 // 2. Delegate::OnError is called once. |
111 // 3. Castsocket is closed. | 110 // 3. CastSocket is closed. |
112 // | 111 // |
113 // DO NOT delete the CastSocket object in write completion callback. | 112 // DO NOT delete the CastSocket object in |callback|. Instead use Close(). |
114 // But it is fine to delete the socket in Delegate::OnError | |
115 virtual void SendMessage(const MessageInfo& message, | 113 virtual void SendMessage(const MessageInfo& message, |
116 const net::CompletionCallback& callback); | 114 const net::CompletionCallback& callback); |
117 | 115 |
118 // Closes the channel. On completion, the channel will be in | 116 // Closes the channel if not already closed. On completion, the channel will |
119 // READY_STATE_CLOSED. | 117 // be in READY_STATE_CLOSED. |
| 118 // |
120 // It is fine to delete the CastSocket object in |callback|. | 119 // It is fine to delete the CastSocket object in |callback|. |
121 virtual void Close(const net::CompletionCallback& callback); | 120 virtual void Close(const net::CompletionCallback& callback); |
122 | 121 |
123 protected: | 122 protected: |
124 // Message header struct. If fields are added, be sure to update | 123 // Message header struct. If fields are added, be sure to update |
125 // header_size(). Protected to allow use of *_size() methods in unit tests. | 124 // header_size(). Protected to allow use of *_size() methods in unit tests. |
126 struct MessageHeader { | 125 struct MessageHeader { |
127 MessageHeader(); | 126 MessageHeader(); |
128 // Sets the message size. | 127 // Sets the message size. |
129 void SetMessageSize(size_t message_size); | 128 void SetMessageSize(size_t message_size); |
(...skipping 84 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 // Runs pending callbacks that are passed into us to notify API clients that |
| 277 // pending operations will fail because the socket has been closed. |
| 278 void RunPendingCallbacksOnClose(); |
| 279 |
272 // Serializes the content of message_proto (with a header) to |message_data|. | 280 // Serializes the content of message_proto (with a header) to |message_data|. |
273 static bool Serialize(const CastMessage& message_proto, | 281 static bool Serialize(const CastMessage& message_proto, |
274 std::string* message_data); | 282 std::string* message_data); |
275 | 283 |
276 virtual bool CalledOnValidThread() const; | 284 virtual bool CalledOnValidThread() const; |
277 | 285 |
278 virtual base::Timer* GetTimer(); | 286 virtual base::Timer* GetTimer(); |
279 | 287 |
280 base::ThreadChecker thread_checker_; | 288 base::ThreadChecker thread_checker_; |
281 | 289 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 scoped_ptr<net::SSLClientSocket> socket_; | 325 scoped_ptr<net::SSLClientSocket> socket_; |
318 // Certificate of the peer. This field may be empty if the peer | 326 // Certificate of the peer. This field may be empty if the peer |
319 // certificate is not yet fetched. | 327 // certificate is not yet fetched. |
320 std::string peer_cert_; | 328 std::string peer_cert_; |
321 // Reply received from the receiver to a challenge request. | 329 // Reply received from the receiver to a challenge request. |
322 scoped_ptr<CastMessage> challenge_reply_; | 330 scoped_ptr<CastMessage> challenge_reply_; |
323 | 331 |
324 // Callback invoked when the socket is connected or fails to connect. | 332 // Callback invoked when the socket is connected or fails to connect. |
325 net::CompletionCallback connect_callback_; | 333 net::CompletionCallback connect_callback_; |
326 | 334 |
| 335 // Callback invoked by |connect_timeout_timer_| to cancel the connection. |
| 336 base::CancelableClosure connect_timeout_callback_; |
327 // Duration to wait before timing out. | 337 // Duration to wait before timing out. |
328 base::TimeDelta connect_timeout_; | 338 base::TimeDelta connect_timeout_; |
329 // Timer invoked when the connection has timed out. | 339 // Timer invoked when the connection has timed out. |
330 scoped_ptr<base::Timer> connect_timeout_timer_; | 340 scoped_ptr<base::Timer> connect_timeout_timer_; |
331 // Set when a timeout is triggered and the connection process has | 341 // Set when a timeout is triggered and the connection process has |
332 // canceled. | 342 // canceled. |
333 bool is_canceled_; | 343 bool is_canceled_; |
334 | 344 |
335 // Connection flow state machine state. | 345 // Connection flow state machine state. |
336 ConnectionState connect_state_; | 346 ConnectionState connect_state_; |
337 // Write flow state machine state. | 347 // Write flow state machine state. |
338 WriteState write_state_; | 348 WriteState write_state_; |
339 // Read flow state machine state. | 349 // Read flow state machine state. |
340 ReadState read_state_; | 350 ReadState read_state_; |
341 // The last error encountered by the channel. | 351 // The last error encountered by the channel. |
342 ChannelError error_state_; | 352 ChannelError error_state_; |
343 // The current status of the channel. | 353 // The current status of the channel. |
344 ReadyState ready_state_; | 354 ReadyState ready_state_; |
345 | 355 |
| 356 // Task invoked to (re)start the connect loop. Canceled on entry to the |
| 357 // connect loop. |
| 358 base::CancelableClosure connect_loop_callback_; |
| 359 // Task invoked to send the auth challenge. Canceled when the auth challenge |
| 360 // has been sent. |
| 361 base::CancelableClosure send_auth_challenge_callback_; |
| 362 // Callback invoked to (re)start the read loop. Canceled on entry to the read |
| 363 // loop. |
| 364 base::CancelableClosure read_loop_callback_; |
| 365 |
346 // Holds a message to be written to the socket. |callback| is invoked when the | 366 // Holds a message to be written to the socket. |callback| is invoked when the |
347 // message is fully written or an error occurrs. | 367 // message is fully written or an error occurrs. |
348 struct WriteRequest { | 368 struct WriteRequest { |
349 explicit WriteRequest(const net::CompletionCallback& callback); | 369 explicit WriteRequest(const net::CompletionCallback& callback); |
350 ~WriteRequest(); | 370 ~WriteRequest(); |
351 // Sets the content of the request by serializing |message| into |io_buffer| | 371 // Sets the content of the request by serializing |message| into |io_buffer| |
352 // and prepending the header. Must only be called once. | 372 // and prepending the header. Must only be called once. |
353 bool SetContent(const CastMessage& message_proto); | 373 bool SetContent(const CastMessage& message_proto); |
354 | 374 |
355 net::CompletionCallback callback; | 375 net::CompletionCallback callback; |
356 scoped_refptr<net::DrainableIOBuffer> io_buffer; | 376 scoped_refptr<net::DrainableIOBuffer> io_buffer; |
357 }; | 377 }; |
358 // Queue of pending writes. The message at the front of the queue is the one | 378 // Queue of pending writes. The message at the front of the queue is the one |
359 // being written. | 379 // being written. |
360 std::queue<WriteRequest> write_queue_; | 380 std::queue<WriteRequest> write_queue_; |
361 | 381 |
362 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestFullSecureConnectionFlowAsync); | 382 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestFullSecureConnectionFlowAsync); |
363 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead); | 383 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead); |
364 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadHeaderParseError); | 384 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadHeaderParseError); |
365 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); | 385 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); |
366 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestWriteErrorLargeMessage); | 386 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestWriteErrorLargeMessage); |
367 DISALLOW_COPY_AND_ASSIGN(CastSocket); | 387 DISALLOW_COPY_AND_ASSIGN(CastSocket); |
368 }; | 388 }; |
369 | 389 |
370 } // namespace cast_channel | 390 } // namespace cast_channel |
371 } // namespace core_api | 391 } // namespace core_api |
372 } // namespace extensions | 392 } // namespace extensions |
373 | 393 |
374 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ | 394 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ |
OLD | NEW |