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 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
49 VIDEO_OUT = 1 << 0, | 49 VIDEO_OUT = 1 << 0, |
50 VIDEO_IN = 1 << 1, | 50 VIDEO_IN = 1 << 1, |
51 AUDIO_OUT = 1 << 2, | 51 AUDIO_OUT = 1 << 2, |
52 AUDIO_IN = 1 << 3, | 52 AUDIO_IN = 1 << 3, |
53 DEV_MODE = 1 << 4 | 53 DEV_MODE = 1 << 4 |
54 }; | 54 }; |
55 | 55 |
56 // Public interface of the CastSocket class. | 56 // Public interface of the CastSocket class. |
57 class CastSocket { | 57 class CastSocket { |
58 public: | 58 public: |
59 using OnOpenCallback = | |
60 base::Callback<void(int channel_id, ChannelError error_state)>; | |
61 | |
59 class Observer { | 62 class Observer { |
60 public: | 63 public: |
61 virtual ~Observer() {} | 64 virtual ~Observer() {} |
62 | 65 |
63 // Invoked when an error occurs on |socket|. | 66 // Invoked when an error occurs on |socket|. |
64 virtual void OnError(const CastSocket& socket, | 67 virtual void OnError(const CastSocket& socket, |
65 cast_channel::ChannelError error_state) = 0; | 68 cast_channel::ChannelError error_state) = 0; |
66 | 69 |
67 // Invoked when |socket| receives a message. | 70 // Invoked when |socket| receives a message. |
68 virtual void OnMessage(const CastSocket& socket, | 71 virtual void OnMessage(const CastSocket& socket, |
69 const cast_channel::CastMessage& message) = 0; | 72 const cast_channel::CastMessage& message) = 0; |
70 }; | 73 }; |
71 | 74 |
72 virtual ~CastSocket() {} | 75 virtual ~CastSocket() {} |
73 | 76 |
74 // Used by BrowserContextKeyedAPIFactory. | 77 // Used by BrowserContextKeyedAPIFactory. |
75 static const char* service_name() { return "CastSocketImplManager"; } | 78 static const char* service_name() { return "CastSocketImplManager"; } |
76 | 79 |
77 // Connects the channel to the peer. If successful, the channel will be in | 80 // Connects the channel to the peer. If successful, the channel will be in |
78 // READY_STATE_OPEN. DO NOT delete the CastSocket object in |callback|. | 81 // READY_STATE_OPEN. DO NOT delete the CastSocket object in |callback|. |
79 // Instead use Close(). | 82 // Instead use Close(). |
80 // |callback| will be invoked with any ChannelError that occurred, or | 83 // |callback| will be invoked with any ChannelError that occurred, or |
81 // CHANNEL_ERROR_NONE if successful. | 84 // CHANNEL_ERROR_NONE if successful. |
82 // If the CastSocket is destroyed while the connection is pending, |callback| | 85 // If the CastSocket is destroyed while the connection is pending, |callback| |
83 // will be invoked with CHANNEL_ERROR_UNKNOWN. In this case, invoking | 86 // will be invoked with CHANNEL_ERROR_UNKNOWN. In this case, invoking |
84 // |callback| must not result in any re-entrancy behavior. | 87 // |callback| must not result in any re-entrancy behavior. |
85 // Ownership of |delegate| is transferred to this CastSocket. | 88 // Ownership of |delegate| is transferred to this CastSocket. |
86 virtual void Connect(base::Callback<void(ChannelError)> callback) = 0; | 89 virtual void Connect(const OnOpenCallback& callback) = 0; |
87 | 90 |
88 // Closes the channel if not already closed. On completion, the channel will | 91 // Closes the channel if not already closed. On completion, the channel will |
89 // be in READY_STATE_CLOSED. | 92 // be in READY_STATE_CLOSED. |
90 // | 93 // |
91 // It is fine to delete this object in |callback|. | 94 // It is fine to delete this object in |callback|. |
92 virtual void Close(const net::CompletionCallback& callback) = 0; | 95 virtual void Close(const net::CompletionCallback& callback) = 0; |
93 | 96 |
94 // The IP endpoint for the destination of the channel. | 97 // The IP endpoint for the destination of the channel. |
95 virtual const net::IPEndPoint& ip_endpoint() const = 0; | 98 virtual const net::IPEndPoint& ip_endpoint() const = 0; |
96 | 99 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 base::TimeDelta liveness_timeout, | 165 base::TimeDelta liveness_timeout, |
163 base::TimeDelta ping_interval, | 166 base::TimeDelta ping_interval, |
164 const scoped_refptr<Logger>& logger, | 167 const scoped_refptr<Logger>& logger, |
165 uint64_t device_capabilities, | 168 uint64_t device_capabilities, |
166 const AuthContext& auth_context); | 169 const AuthContext& auth_context); |
167 | 170 |
168 // Ensures that the socket is closed. | 171 // Ensures that the socket is closed. |
169 ~CastSocketImpl() override; | 172 ~CastSocketImpl() override; |
170 | 173 |
171 // CastSocket interface. | 174 // CastSocket interface. |
172 void Connect(base::Callback<void(ChannelError)> callback) override; | 175 void Connect(const OnOpenCallback& callback) override; |
173 CastTransport* transport() const override; | 176 CastTransport* transport() const override; |
174 void Close(const net::CompletionCallback& callback) override; | 177 void Close(const net::CompletionCallback& callback) override; |
175 const net::IPEndPoint& ip_endpoint() const override; | 178 const net::IPEndPoint& ip_endpoint() const override; |
176 int id() const override; | 179 int id() const override; |
177 void set_id(int channel_id) override; | 180 void set_id(int channel_id) override; |
178 ReadyState ready_state() const override; | 181 ReadyState ready_state() const override; |
179 ChannelError error_state() const override; | 182 ChannelError error_state() const override; |
180 bool keep_alive() const override; | 183 bool keep_alive() const override; |
181 bool audio_only() const override; | 184 bool audio_only() const override; |
182 void AddObserver(Observer* observer) override; | 185 void AddObserver(Observer* observer) override; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
223 | 226 |
224 // Replaces the internally-constructed transport object with one provided | 227 // Replaces the internally-constructed transport object with one provided |
225 // by the caller (e.g. a mock). | 228 // by the caller (e.g. a mock). |
226 void SetTransportForTesting(std::unique_ptr<CastTransport> transport); | 229 void SetTransportForTesting(std::unique_ptr<CastTransport> transport); |
227 | 230 |
228 // Verifies whether the socket complies with cast channel policy. | 231 // Verifies whether the socket complies with cast channel policy. |
229 // Audio only channel policy mandates that a device declaring a video out | 232 // Audio only channel policy mandates that a device declaring a video out |
230 // capability must not have a certificate with audio only policy. | 233 // capability must not have a certificate with audio only policy. |
231 bool VerifyChannelPolicy(const AuthResult& result); | 234 bool VerifyChannelPolicy(const AuthResult& result); |
232 | 235 |
236 void Connect(); | |
237 | |
233 private: | 238 private: |
234 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestConnectAuthMessageCorrupted); | 239 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestConnectAuthMessageCorrupted); |
235 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, | 240 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, |
236 TestConnectChallengeReplyReceiveError); | 241 TestConnectChallengeReplyReceiveError); |
237 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, | 242 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, |
238 TestConnectChallengeVerificationFails); | 243 TestConnectChallengeVerificationFails); |
239 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestObservers); | 244 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestObservers); |
240 friend class AuthTransportDelegate; | 245 friend class AuthTransportDelegate; |
241 friend class CastSocketMessageDelegate; | 246 friend class CastSocketMessageDelegate; |
242 friend class CastSocketTest; | 247 friend class CastSocketTest; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
349 // Certificate of the peer. This field may be empty if the peer | 354 // Certificate of the peer. This field may be empty if the peer |
350 // certificate is not yet fetched. | 355 // certificate is not yet fetched. |
351 scoped_refptr<net::X509Certificate> peer_cert_; | 356 scoped_refptr<net::X509Certificate> peer_cert_; |
352 | 357 |
353 // The challenge context for the current connection. | 358 // The challenge context for the current connection. |
354 const AuthContext auth_context_; | 359 const AuthContext auth_context_; |
355 | 360 |
356 // Reply received from the receiver to a challenge request. | 361 // Reply received from the receiver to a challenge request. |
357 std::unique_ptr<CastMessage> challenge_reply_; | 362 std::unique_ptr<CastMessage> challenge_reply_; |
358 | 363 |
359 // Callback invoked when the socket is connected or fails to connect. | 364 // Callback invoked when the socket is connected or fails to connect. |
mark a. foltz
2017/06/21 17:41:42
nit: Callbacks
zhaobin
2017/06/21 21:58:25
Done.
| |
360 base::Callback<void(ChannelError)> connect_callback_; | 365 std::vector<OnOpenCallback> connect_callbacks_; |
361 | 366 |
362 // Callback invoked by |connect_timeout_timer_| to cancel the connection. | 367 // Callback invoked by |connect_timeout_timer_| to cancel the connection. |
363 base::CancelableClosure connect_timeout_callback_; | 368 base::CancelableClosure connect_timeout_callback_; |
364 | 369 |
365 // Duration to wait before timing out. | 370 // Duration to wait before timing out. |
366 base::TimeDelta connect_timeout_; | 371 base::TimeDelta connect_timeout_; |
367 | 372 |
368 // Timer invoked when the connection has timed out. | 373 // Timer invoked when the connection has timed out. |
369 std::unique_ptr<base::Timer> connect_timeout_timer_; | 374 std::unique_ptr<base::Timer> connect_timeout_timer_; |
370 | 375 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
414 // Map of CastSocket::Observer keyed by observer id. For extension side | 419 // 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 | 420 // observers, id is extension_id; For browser side observers, id is a hard |
416 // coded string. | 421 // coded string. |
417 base::ObserverList<Observer> observers_; | 422 base::ObserverList<Observer> observers_; |
418 | 423 |
419 DISALLOW_COPY_AND_ASSIGN(CastSocketImpl); | 424 DISALLOW_COPY_AND_ASSIGN(CastSocketImpl); |
420 }; | 425 }; |
421 } // namespace cast_channel | 426 } // namespace cast_channel |
422 | 427 |
423 #endif // COMPONENTS_CAST_CHANNEL_CAST_SOCKET_H_ | 428 #endif // COMPONENTS_CAST_CHANNEL_CAST_SOCKET_H_ |
OLD | NEW |