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

Side by Side Diff: components/cast_channel/cast_socket.h

Issue 2942993003: [cast_channel] Make CastMessageHandler a CastSocket::Observer instead of CastTransport::Delegate (Closed)
Patch Set: resolve code review comments from Derek and Mark Created 3 years, 6 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
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 COMPONENTS_CAST_CHANNEL_CAST_SOCKET_H_ 5 #ifndef COMPONENTS_CAST_CHANNEL_CAST_SOCKET_H_
6 #define COMPONENTS_CAST_CHANNEL_CAST_SOCKET_H_ 6 #define COMPONENTS_CAST_CHANNEL_CAST_SOCKET_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <queue> 10 #include <queue>
11 #include <string> 11 #include <string>
12 12
13 #include "base/cancelable_callback.h" 13 #include "base/cancelable_callback.h"
14 #include "base/gtest_prod_util.h" 14 #include "base/gtest_prod_util.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
17 #include "base/observer_list.h"
17 #include "base/threading/thread_checker.h" 18 #include "base/threading/thread_checker.h"
18 #include "base/timer/timer.h" 19 #include "base/timer/timer.h"
19 #include "components/cast_channel/cast_auth_util.h" 20 #include "components/cast_channel/cast_auth_util.h"
20 #include "components/cast_channel/cast_channel_enum.h" 21 #include "components/cast_channel/cast_channel_enum.h"
21 #include "components/cast_channel/cast_socket.h" 22 #include "components/cast_channel/cast_socket.h"
22 #include "components/cast_channel/cast_transport.h" 23 #include "components/cast_channel/cast_transport.h"
23 #include "net/base/completion_callback.h" 24 #include "net/base/completion_callback.h"
24 #include "net/base/io_buffer.h" 25 #include "net/base/io_buffer.h"
25 #include "net/base/ip_endpoint.h" 26 #include "net/base/ip_endpoint.h"
26 #include "net/log/net_log_source.h" 27 #include "net/log/net_log_source.h"
(...skipping 21 matching lines...) Expand all
48 VIDEO_OUT = 1 << 0, 49 VIDEO_OUT = 1 << 0,
49 VIDEO_IN = 1 << 1, 50 VIDEO_IN = 1 << 1,
50 AUDIO_OUT = 1 << 2, 51 AUDIO_OUT = 1 << 2,
51 AUDIO_IN = 1 << 3, 52 AUDIO_IN = 1 << 3,
52 DEV_MODE = 1 << 4 53 DEV_MODE = 1 << 4
53 }; 54 };
54 55
55 // Public interface of the CastSocket class. 56 // Public interface of the CastSocket class.
56 class CastSocket { 57 class CastSocket {
57 public: 58 public:
59 class Observer {
60 public:
61 virtual ~Observer() {}
62
63 // Invoked when an error occurs on |socket|.
64 virtual void OnError(const CastSocket& socket,
65 cast_channel::ChannelError error_state) = 0;
66
67 // Invoked when |socket| receives a message.
68 virtual void OnMessage(const CastSocket& socket,
69 const cast_channel::CastMessage& message) = 0;
70 };
71
58 virtual ~CastSocket() {} 72 virtual ~CastSocket() {}
59 73
60 // Used by BrowserContextKeyedAPIFactory. 74 // Used by BrowserContextKeyedAPIFactory.
61 static const char* service_name() { return "CastSocketImplManager"; } 75 static const char* service_name() { return "CastSocketImplManager"; }
62 76
63 // Connects the channel to the peer. If successful, the channel will be in 77 // Connects the channel to the peer. If successful, the channel will be in
64 // READY_STATE_OPEN. DO NOT delete the CastSocket object in |callback|. 78 // READY_STATE_OPEN. DO NOT delete the CastSocket object in |callback|.
65 // Instead use Close(). 79 // Instead use Close().
66 // |callback| will be invoked with any ChannelError that occurred, or 80 // |callback| will be invoked with any ChannelError that occurred, or
67 // CHANNEL_ERROR_NONE if successful. 81 // CHANNEL_ERROR_NONE if successful.
68 // If the CastSocket is destroyed while the connection is pending, |callback| 82 // If the CastSocket is destroyed while the connection is pending, |callback|
69 // will be invoked with CHANNEL_ERROR_UNKNOWN. In this case, invoking 83 // will be invoked with CHANNEL_ERROR_UNKNOWN. In this case, invoking
70 // |callback| must not result in any re-entrancy behavior. 84 // |callback| must not result in any re-entrancy behavior.
71 // |delegate| receives message receipt and error events.
72 // Ownership of |delegate| is transferred to this CastSocket. 85 // Ownership of |delegate| is transferred to this CastSocket.
mark a. foltz 2017/06/20 01:16:43 This comment no longer applies
zhaobin 2017/06/20 17:50:05 Done.
73 virtual void Connect(std::unique_ptr<CastTransport::Delegate> delegate, 86 virtual void Connect(base::Callback<void(ChannelError)> callback) = 0;
74 base::Callback<void(ChannelError)> callback) = 0;
75 87
76 // Closes the channel if not already closed. On completion, the channel will 88 // Closes the channel if not already closed. On completion, the channel will
77 // be in READY_STATE_CLOSED. 89 // be in READY_STATE_CLOSED.
78 // 90 //
79 // It is fine to delete this object in |callback|. 91 // It is fine to delete this object in |callback|.
80 virtual void Close(const net::CompletionCallback& callback) = 0; 92 virtual void Close(const net::CompletionCallback& callback) = 0;
81 93
82 // The IP endpoint for the destination of the channel. 94 // The IP endpoint for the destination of the channel.
83 virtual const net::IPEndPoint& ip_endpoint() const = 0; 95 virtual const net::IPEndPoint& ip_endpoint() const = 0;
84 96
(...skipping 20 matching lines...) Expand all
105 // Marks a socket as invalid due to an error, and sends an OnError 117 // Marks a socket as invalid due to an error, and sends an OnError
106 // event to |delegate_|. 118 // event to |delegate_|.
107 // The OnError event receipient is responsible for closing the socket in the 119 // The OnError event receipient is responsible for closing the socket in the
108 // event of an error. 120 // event of an error.
109 // Setting the error state does not close the socket if it is open. 121 // Setting the error state does not close the socket if it is open.
110 virtual void SetErrorState(ChannelError error_state) = 0; 122 virtual void SetErrorState(ChannelError error_state) = 0;
111 123
112 // Returns a pointer to the socket's message transport layer. Can be used to 124 // Returns a pointer to the socket's message transport layer. Can be used to
113 // send and receive CastMessages over the socket. 125 // send and receive CastMessages over the socket.
114 virtual CastTransport* transport() const = 0; 126 virtual CastTransport* transport() const = 0;
127
128 // Register |observer| with current socket to receive message receipt
mark a. foltz 2017/06/20 01:16:43 Registers ... receive messages and ...
zhaobin 2017/06/20 17:50:05 Done.
129 // and error events.
130 virtual void AddObserver(Observer* observer) = 0;
115 }; 131 };
116 132
117 // This class implements a channel between Chrome and a Cast device using a TCP 133 // This class implements a channel between Chrome and a Cast device using a TCP
118 // socket with SSL. The channel may authenticate that the receiver is a genuine 134 // socket with SSL. The channel may authenticate that the receiver is a genuine
119 // Cast device. All CastSocketImpl objects must be used only on the IO thread. 135 // Cast device. All CastSocketImpl objects must be used only on the IO thread.
120 // 136 //
121 // NOTE: Not called "CastChannel" to reduce confusion with the generated API 137 // NOTE: Not called "CastChannel" to reduce confusion with the generated API
122 // code. 138 // code.
123 class CastSocketImpl : public CastSocket { 139 class CastSocketImpl : public CastSocket {
124 public: 140 public:
(...skipping 21 matching lines...) Expand all
146 base::TimeDelta liveness_timeout, 162 base::TimeDelta liveness_timeout,
147 base::TimeDelta ping_interval, 163 base::TimeDelta ping_interval,
148 const scoped_refptr<Logger>& logger, 164 const scoped_refptr<Logger>& logger,
149 uint64_t device_capabilities, 165 uint64_t device_capabilities,
150 const AuthContext& auth_context); 166 const AuthContext& auth_context);
151 167
152 // Ensures that the socket is closed. 168 // Ensures that the socket is closed.
153 ~CastSocketImpl() override; 169 ~CastSocketImpl() override;
154 170
155 // CastSocket interface. 171 // CastSocket interface.
156 void Connect(std::unique_ptr<CastTransport::Delegate> delegate, 172 void Connect(base::Callback<void(ChannelError)> callback) override;
157 base::Callback<void(ChannelError)> callback) override;
158 CastTransport* transport() const override; 173 CastTransport* transport() const override;
159 void Close(const net::CompletionCallback& callback) override; 174 void Close(const net::CompletionCallback& callback) override;
160 const net::IPEndPoint& ip_endpoint() const override; 175 const net::IPEndPoint& ip_endpoint() const override;
161 int id() const override; 176 int id() const override;
162 void set_id(int channel_id) override; 177 void set_id(int channel_id) override;
163 ReadyState ready_state() const override; 178 ReadyState ready_state() const override;
164 ChannelError error_state() const override; 179 ChannelError error_state() const override;
165 bool keep_alive() const override; 180 bool keep_alive() const override;
166 bool audio_only() const override; 181 bool audio_only() const override;
182 void AddObserver(Observer* observer) override;
167 183
168 protected: 184 protected:
169 // CastTransport::Delegate methods for receiving handshake messages. 185 // CastTransport::Delegate methods for receiving handshake messages.
170 class AuthTransportDelegate : public CastTransport::Delegate { 186 class AuthTransportDelegate : public CastTransport::Delegate {
171 public: 187 public:
172 explicit AuthTransportDelegate(CastSocketImpl* socket); 188 explicit AuthTransportDelegate(CastSocketImpl* socket);
173 189
174 // Gets the error state of the channel. 190 // Gets the error state of the channel.
175 // Returns CHANNEL_ERROR_NONE if no errors are present. 191 // Returns CHANNEL_ERROR_NONE if no errors are present.
176 ChannelError error_state() const; 192 ChannelError error_state() const;
177 193
178 // Gets recorded error details. 194 // Gets recorded error details.
179 LastError last_error() const; 195 LastError last_error() const;
180 196
181 // CastTransport::Delegate interface. 197 // CastTransport::Delegate interface.
182 void OnError(ChannelError error_state) override; 198 void OnError(ChannelError error_state) override;
183 void OnMessage(const CastMessage& message) override; 199 void OnMessage(const CastMessage& message) override;
184 void Start() override; 200 void Start() override;
185 201
186 private: 202 private:
187 CastSocketImpl* socket_; 203 CastSocketImpl* socket_;
188 ChannelError error_state_; 204 ChannelError error_state_;
189 LastError last_error_; 205 LastError last_error_;
190 }; 206 };
191 207
208 // CastTransport::Delegate methods to receive normal messages and errors.
209 class CastSocketMessageDelegate : public CastTransport::Delegate {
210 public:
211 CastSocketMessageDelegate(cast_channel::CastSocketImpl* socket);
212 ~CastSocketMessageDelegate() override;
213
214 // CastTransport::Delegate implementation.
215 void OnError(cast_channel::ChannelError error_state) override;
216 void OnMessage(const cast_channel::CastMessage& message) override;
217 void Start() override;
218
219 private:
220 CastSocketImpl* const socket_;
221 DISALLOW_COPY_AND_ASSIGN(CastSocketMessageDelegate);
222 };
223
192 // Replaces the internally-constructed transport object with one provided 224 // Replaces the internally-constructed transport object with one provided
193 // by the caller (e.g. a mock). 225 // by the caller (e.g. a mock).
194 void SetTransportForTesting(std::unique_ptr<CastTransport> transport); 226 void SetTransportForTesting(std::unique_ptr<CastTransport> transport);
195 227
196 // Verifies whether the socket complies with cast channel policy. 228 // Verifies whether the socket complies with cast channel policy.
197 // Audio only channel policy mandates that a device declaring a video out 229 // Audio only channel policy mandates that a device declaring a video out
198 // capability must not have a certificate with audio only policy. 230 // capability must not have a certificate with audio only policy.
199 bool VerifyChannelPolicy(const AuthResult& result); 231 bool VerifyChannelPolicy(const AuthResult& result);
200 232
201 private: 233 private:
202 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestConnectAuthMessageCorrupted); 234 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestConnectAuthMessageCorrupted);
203 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, 235 FRIEND_TEST_ALL_PREFIXES(CastSocketTest,
204 TestConnectChallengeReplyReceiveError); 236 TestConnectChallengeReplyReceiveError);
205 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, 237 FRIEND_TEST_ALL_PREFIXES(CastSocketTest,
206 TestConnectChallengeVerificationFails); 238 TestConnectChallengeVerificationFails);
239 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestObservers);
207 friend class AuthTransportDelegate; 240 friend class AuthTransportDelegate;
241 friend class CastSocketMessageDelegate;
208 friend class CastSocketTest; 242 friend class CastSocketTest;
209 friend class TestCastSocket; 243 friend class TestCastSocket;
210 244
211 void SetErrorState(ChannelError error_state) override; 245 void SetErrorState(ChannelError error_state) override;
212 246
213 // Frees resources and cancels pending callbacks. |ready_state_| will be set 247 // Frees resources and cancels pending callbacks. |ready_state_| will be set
214 // READY_STATE_CLOSED on completion. A no-op if |ready_state_| is already 248 // READY_STATE_CLOSED on completion. A no-op if |ready_state_| is already
215 // READY_STATE_CLOSED. 249 // READY_STATE_CLOSED.
216 void CloseInternal(); 250 void CloseInternal();
217 251
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 // Cast message formatting and parsing layer. 404 // Cast message formatting and parsing layer.
371 std::unique_ptr<CastTransport> transport_; 405 std::unique_ptr<CastTransport> transport_;
372 406
373 // Caller's message read and error handling delegate. 407 // Caller's message read and error handling delegate.
374 std::unique_ptr<CastTransport::Delegate> delegate_; 408 std::unique_ptr<CastTransport::Delegate> delegate_;
375 409
376 // Raw pointer to the auth handshake delegate. Used to get detailed error 410 // Raw pointer to the auth handshake delegate. Used to get detailed error
377 // information. 411 // information.
378 AuthTransportDelegate* auth_delegate_; 412 AuthTransportDelegate* auth_delegate_;
379 413
414 // Map of CastSocket::Observer keyed by observer id. For extension side
415 // observers, id is extension_id; For browser side observers, id is a hard
416 // coded string.
mark a. foltz 2017/06/20 01:16:43 This comment is outdated and can be removed
zhaobin 2017/06/20 17:50:05 Done.
417 base::ObserverList<Observer> observers_;
418
380 DISALLOW_COPY_AND_ASSIGN(CastSocketImpl); 419 DISALLOW_COPY_AND_ASSIGN(CastSocketImpl);
381 }; 420 };
382 } // namespace cast_channel 421 } // namespace cast_channel
383 422
384 #endif // COMPONENTS_CAST_CHANNEL_CAST_SOCKET_H_ 423 #endif // COMPONENTS_CAST_CHANNEL_CAST_SOCKET_H_
OLDNEW
« no previous file with comments | « no previous file | components/cast_channel/cast_socket.cc » ('j') | components/cast_channel/cast_socket.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698