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

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

Powered by Google App Engine
This is Rietveld 408576698