Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(372)

Side by Side Diff: extensions/browser/api/cast_channel/cast_socket.h

Issue 417403002: Remove weak pointers from CastSocket. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Clarify deletion policies. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 must
49 // not be deleted by the delegate.
Wez 2014/08/04 21:32:32 Who does delete it? The API resource manager? In w
mark a. foltz 2014/08/04 22:20:43 Both cases are possible, updated comment to mentio
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.
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.
59 virtual void OnMessage(const CastSocket* socket, 55 virtual void OnMessage(const CastSocket* socket,
60 const MessageInfo& message) = 0; 56 const MessageInfo& message) = 0;
61 57
62 protected: 58 protected:
63 virtual ~Delegate() {} 59 virtual ~Delegate() {}
64 }; 60 };
65 61
66 // Creates a new CastSocket that connects to |ip_endpoint| with 62 // Creates a new CastSocket that connects to |ip_endpoint| with
67 // |channel_auth|. |owner_extension_id| is the id of the extension that opened 63 // |channel_auth|. |owner_extension_id| is the id of the extension that opened
68 // the socket. |channel_auth| must not be CHANNEL_AUTH_NONE. 64 // the socket. |channel_auth| must not be CHANNEL_AUTH_NONE.
69 CastSocket(const std::string& owner_extension_id, 65 CastSocket(const std::string& owner_extension_id,
70 const net::IPEndPoint& ip_endpoint, 66 const net::IPEndPoint& ip_endpoint,
71 ChannelAuthType channel_auth, 67 ChannelAuthType channel_auth,
72 CastSocket::Delegate* delegate, 68 CastSocket::Delegate* delegate,
73 net::NetLog* net_log, 69 net::NetLog* net_log,
74 const base::TimeDelta& connect_timeout); 70 const base::TimeDelta& connect_timeout);
71
72 // Ensures that the socket is closed.
75 virtual ~CastSocket(); 73 virtual ~CastSocket();
76 74
77 // The IP endpoint for the destination of the channel. 75 // The IP endpoint for the destination of the channel.
78 const net::IPEndPoint& ip_endpoint() const { return ip_endpoint_; } 76 const net::IPEndPoint& ip_endpoint() const { return ip_endpoint_; }
79 77
80 // The authentication level requested for the channel. 78 // The authentication level requested for the channel.
81 ChannelAuthType channel_auth() const { return channel_auth_; } 79 ChannelAuthType channel_auth() const { return channel_auth_; }
82 80
83 // Returns a cast:// or casts:// URL for the channel endpoint. 81 // Returns a cast:// or casts:// URL for the channel endpoint.
84 // For backwards compatibility. 82 // For backwards compatibility.
85 std::string CastUrl() const; 83 std::string CastUrl() const;
86 84
87 // Channel id for the ApiResourceManager. 85 // Channel id for the ApiResourceManager.
88 int id() const { return channel_id_; } 86 int id() const { return channel_id_; }
89 87
90 // Sets the channel id. 88 // Sets the channel id.
91 void set_id(int channel_id) { channel_id_ = channel_id; } 89 void set_id(int channel_id) { channel_id_ = channel_id; }
92 90
93 // Returns the state of the channel. Virtual for testing. 91 // Returns the state of the channel. Virtual for testing.
94 virtual ReadyState ready_state() const; 92 virtual ReadyState ready_state() const;
95 93
96 // Returns the last error that occurred on this channel, or 94 // Returns the last error that occurred on this channel, or
97 // CHANNEL_ERROR_NONE if no error has occurred. Virtual for testing. 95 // CHANNEL_ERROR_NONE if no error has occurred. Virtual for testing.
98 virtual ChannelError error_state() const; 96 virtual ChannelError error_state() const;
99 97
100 // Connects the channel to the peer. If successful, the channel will be in 98 // Connects the channel to the peer. If successful, the channel will be in
101 // READY_STATE_OPEN. 99 // READY_STATE_OPEN. DO NOT delete the CastSocket object in |callback|.
102 // It is fine to delete the CastSocket object in |callback|. 100 // Instead use Close().
103 virtual void Connect(const net::CompletionCallback& callback); 101 virtual void Connect(const net::CompletionCallback& callback);
104 102
105 // Sends a message over a connected channel. The channel must be in 103 // Sends a message over a connected channel. The channel must be in
106 // READY_STATE_OPEN. 104 // READY_STATE_OPEN.
107 // 105 //
108 // Note that if an error occurs the following happens: 106 // Note that if an error occurs the following happens:
109 // 1. Completion callbacks for all pending writes are invoked with error. 107 // 1. Completion callbacks for all pending writes are invoked with error.
110 // 2. Delegate::OnError is called once. 108 // 2. Delegate::OnError is called once.
111 // 3. Castsocket is closed. 109 // 3. CastSocket is closed.
112 // 110 //
113 // DO NOT delete the CastSocket object in write completion callback. 111 // 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, 112 virtual void SendMessage(const MessageInfo& message,
116 const net::CompletionCallback& callback); 113 const net::CompletionCallback& callback);
117 114
118 // Closes the channel. On completion, the channel will be in 115 // Closes the channel if not already closed. On completion, the channel will
119 // READY_STATE_CLOSED. 116 // be in READY_STATE_CLOSED.
117 //
120 // It is fine to delete the CastSocket object in |callback|. 118 // It is fine to delete the CastSocket object in |callback|.
121 virtual void Close(const net::CompletionCallback& callback); 119 virtual void Close(const net::CompletionCallback& callback);
122 120
123 protected: 121 protected:
124 // Message header struct. If fields are added, be sure to update 122 // Message header struct. If fields are added, be sure to update
125 // header_size(). Protected to allow use of *_size() methods in unit tests. 123 // header_size(). Protected to allow use of *_size() methods in unit tests.
126 struct MessageHeader { 124 struct MessageHeader {
127 MessageHeader(); 125 MessageHeader();
128 // Sets the message size. 126 // Sets the message size.
129 void SetMessageSize(size_t message_size); 127 void SetMessageSize(size_t message_size);
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 void DoConnectLoop(int result); 212 void DoConnectLoop(int result);
215 // Each of the below Do* method is executed in the corresponding 213 // Each of the below Do* method is executed in the corresponding
216 // connection state. For example when connection state is TCP_CONNECT 214 // connection state. For example when connection state is TCP_CONNECT
217 // DoTcpConnect is called, and so on. 215 // DoTcpConnect is called, and so on.
218 int DoTcpConnect(); 216 int DoTcpConnect();
219 int DoTcpConnectComplete(int result); 217 int DoTcpConnectComplete(int result);
220 int DoSslConnect(); 218 int DoSslConnect();
221 int DoSslConnectComplete(int result); 219 int DoSslConnectComplete(int result);
222 int DoAuthChallengeSend(); 220 int DoAuthChallengeSend();
223 int DoAuthChallengeSendComplete(int result); 221 int DoAuthChallengeSendComplete(int result);
222 void DoAuthChallengeSendWriteComplete(int result);
224 int DoAuthChallengeReplyComplete(int result); 223 int DoAuthChallengeReplyComplete(int result);
225 ///////////////////////////////////////////////////////////////////////////// 224 /////////////////////////////////////////////////////////////////////////////
226 225
227 ///////////////////////////////////////////////////////////////////////////// 226 /////////////////////////////////////////////////////////////////////////////
228 // Following methods work together to implement write flow. 227 // Following methods work together to implement write flow.
229 // 228 //
230 // Main method that performs write flow state transitions. 229 // Main method that performs write flow state transitions.
231 void DoWriteLoop(int result); 230 void DoWriteLoop(int result);
232 // Each of the below Do* method is executed in the corresponding 231 // Each of the below Do* method is executed in the corresponding
233 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE 232 // write state. For example when write state is WRITE_STATE_WRITE_COMPLETE
(...skipping 25 matching lines...) Expand all
259 const net::CompletionCallback& callback); 258 const net::CompletionCallback& callback);
260 void PostTaskToStartConnectLoop(int result); 259 void PostTaskToStartConnectLoop(int result);
261 void PostTaskToStartReadLoop(); 260 void PostTaskToStartReadLoop();
262 void StartReadLoop(); 261 void StartReadLoop();
263 // Parses the contents of header_read_buffer_ and sets current_message_size_ 262 // Parses the contents of header_read_buffer_ and sets current_message_size_
264 // to the size of the body of the message. 263 // to the size of the body of the message.
265 bool ProcessHeader(); 264 bool ProcessHeader();
266 // Parses the contents of body_read_buffer_ and sets current_message_ to 265 // Parses the contents of body_read_buffer_ and sets current_message_ to
267 // the message received. 266 // the message received.
268 bool ProcessBody(); 267 bool ProcessBody();
269 // Closes socket, updating the error state and signaling the delegate that 268 // Closes the socket, sets |error_state_| to |error| and signals the
270 // |error| has occurred. 269 // delegate that |error| has occurred.
271 void CloseWithError(ChannelError error); 270 void CloseWithError(ChannelError error);
271 // Frees resources and cancels pending callbacks. |ready_state_| will be set
272 // READY_STATE_CLOSED on completion. A no-op if |ready_state_| is already
273 // READY_STATE_CLOSED.
274 void CloseInternal();
275 // Runs pending callbacks that are passed into us to notify API clients that
276 // pending operations will fail because the socket has been closed.
277 void RunPendingCallbacksOnClose();
278
272 // Serializes the content of message_proto (with a header) to |message_data|. 279 // Serializes the content of message_proto (with a header) to |message_data|.
273 static bool Serialize(const CastMessage& message_proto, 280 static bool Serialize(const CastMessage& message_proto,
274 std::string* message_data); 281 std::string* message_data);
275 282
276 virtual bool CalledOnValidThread() const; 283 virtual bool CalledOnValidThread() const;
277 284
278 virtual base::Timer* GetTimer(); 285 virtual base::Timer* GetTimer();
279 286
280 base::ThreadChecker thread_checker_; 287 base::ThreadChecker thread_checker_;
281 288
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 scoped_ptr<net::SSLClientSocket> socket_; 324 scoped_ptr<net::SSLClientSocket> socket_;
318 // Certificate of the peer. This field may be empty if the peer 325 // Certificate of the peer. This field may be empty if the peer
319 // certificate is not yet fetched. 326 // certificate is not yet fetched.
320 std::string peer_cert_; 327 std::string peer_cert_;
321 // Reply received from the receiver to a challenge request. 328 // Reply received from the receiver to a challenge request.
322 scoped_ptr<CastMessage> challenge_reply_; 329 scoped_ptr<CastMessage> challenge_reply_;
323 330
324 // Callback invoked when the socket is connected or fails to connect. 331 // Callback invoked when the socket is connected or fails to connect.
325 net::CompletionCallback connect_callback_; 332 net::CompletionCallback connect_callback_;
326 333
334 // Callback invoked by |connect_timeout_timer_| to cancel the connection.
335 base::CancelableClosure connect_timeout_callback_;
327 // Duration to wait before timing out. 336 // Duration to wait before timing out.
328 base::TimeDelta connect_timeout_; 337 base::TimeDelta connect_timeout_;
329 // Timer invoked when the connection has timed out. 338 // Timer invoked when the connection has timed out.
330 scoped_ptr<base::Timer> connect_timeout_timer_; 339 scoped_ptr<base::Timer> connect_timeout_timer_;
331 // Set when a timeout is triggered and the connection process has 340 // Set when a timeout is triggered and the connection process has
332 // canceled. 341 // canceled.
333 bool is_canceled_; 342 bool is_canceled_;
334 343
335 // Connection flow state machine state. 344 // Connection flow state machine state.
336 ConnectionState connect_state_; 345 ConnectionState connect_state_;
337 // Write flow state machine state. 346 // Write flow state machine state.
338 WriteState write_state_; 347 WriteState write_state_;
339 // Read flow state machine state. 348 // Read flow state machine state.
340 ReadState read_state_; 349 ReadState read_state_;
341 // The last error encountered by the channel. 350 // The last error encountered by the channel.
342 ChannelError error_state_; 351 ChannelError error_state_;
343 // The current status of the channel. 352 // The current status of the channel.
344 ReadyState ready_state_; 353 ReadyState ready_state_;
345 354
355 // Task invoked to (re)start the connect loop. Canceled on entry to the
356 // connect loop.
357 base::CancelableClosure connect_loop_callback_;
358 // Task invoked to send the auth challenge. Canceled when the auth challenge
359 // has been sent.
360 base::CancelableClosure send_auth_challenge_callback_;
361 // Callback invoked to (re)start the read loop. Canceled on entry to the read
362 // loop.
363 base::CancelableClosure read_loop_callback_;
364
346 // Holds a message to be written to the socket. |callback| is invoked when the 365 // Holds a message to be written to the socket. |callback| is invoked when the
347 // message is fully written or an error occurrs. 366 // message is fully written or an error occurrs.
348 struct WriteRequest { 367 struct WriteRequest {
349 explicit WriteRequest(const net::CompletionCallback& callback); 368 explicit WriteRequest(const net::CompletionCallback& callback);
350 ~WriteRequest(); 369 ~WriteRequest();
351 // Sets the content of the request by serializing |message| into |io_buffer| 370 // Sets the content of the request by serializing |message| into |io_buffer|
352 // and prepending the header. Must only be called once. 371 // and prepending the header. Must only be called once.
353 bool SetContent(const CastMessage& message_proto); 372 bool SetContent(const CastMessage& message_proto);
354 373
355 net::CompletionCallback callback; 374 net::CompletionCallback callback;
356 scoped_refptr<net::DrainableIOBuffer> io_buffer; 375 scoped_refptr<net::DrainableIOBuffer> io_buffer;
357 }; 376 };
358 // Queue of pending writes. The message at the front of the queue is the one 377 // Queue of pending writes. The message at the front of the queue is the one
359 // being written. 378 // being written.
360 std::queue<WriteRequest> write_queue_; 379 std::queue<WriteRequest> write_queue_;
361 380
362 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestFullSecureConnectionFlowAsync); 381 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestFullSecureConnectionFlowAsync);
363 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead); 382 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestRead);
364 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadHeaderParseError); 383 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadHeaderParseError);
365 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany); 384 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestReadMany);
366 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestWriteErrorLargeMessage); 385 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestWriteErrorLargeMessage);
367 DISALLOW_COPY_AND_ASSIGN(CastSocket); 386 DISALLOW_COPY_AND_ASSIGN(CastSocket);
368 }; 387 };
369 388
370 } // namespace cast_channel 389 } // namespace cast_channel
371 } // namespace core_api 390 } // namespace core_api
372 } // namespace extensions 391 } // namespace extensions
373 392
374 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_ 393 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_SOCKET_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698