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> |
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 Loading... |
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 ChannelError error_state) = 0; |
| 66 |
| 67 // Invoked when |socket| receives a message. |
| 68 virtual void OnMessage(const CastSocket& socket, |
| 69 const 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. | 85 virtual void Connect(base::Callback<void(ChannelError)> callback) = 0; |
72 // Ownership of |delegate| is transferred to this CastSocket. | |
73 virtual void Connect(std::unique_ptr<CastTransport::Delegate> delegate, | |
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 Loading... |
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 // Registers |observer| with the socket to receive messages and error events. |
| 128 virtual void AddObserver(Observer* observer) = 0; |
115 }; | 129 }; |
116 | 130 |
117 // This class implements a channel between Chrome and a Cast device using a TCP | 131 // 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 | 132 // 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. | 133 // Cast device. All CastSocketImpl objects must be used only on the IO thread. |
120 // | 134 // |
121 // NOTE: Not called "CastChannel" to reduce confusion with the generated API | 135 // NOTE: Not called "CastChannel" to reduce confusion with the generated API |
122 // code. | 136 // code. |
123 class CastSocketImpl : public CastSocket { | 137 class CastSocketImpl : public CastSocket { |
124 public: | 138 public: |
(...skipping 21 matching lines...) Expand all Loading... |
146 base::TimeDelta liveness_timeout, | 160 base::TimeDelta liveness_timeout, |
147 base::TimeDelta ping_interval, | 161 base::TimeDelta ping_interval, |
148 const scoped_refptr<Logger>& logger, | 162 const scoped_refptr<Logger>& logger, |
149 uint64_t device_capabilities, | 163 uint64_t device_capabilities, |
150 const AuthContext& auth_context); | 164 const AuthContext& auth_context); |
151 | 165 |
152 // Ensures that the socket is closed. | 166 // Ensures that the socket is closed. |
153 ~CastSocketImpl() override; | 167 ~CastSocketImpl() override; |
154 | 168 |
155 // CastSocket interface. | 169 // CastSocket interface. |
156 void Connect(std::unique_ptr<CastTransport::Delegate> delegate, | 170 void Connect(base::Callback<void(ChannelError)> callback) override; |
157 base::Callback<void(ChannelError)> callback) override; | |
158 CastTransport* transport() const override; | 171 CastTransport* transport() const override; |
159 void Close(const net::CompletionCallback& callback) override; | 172 void Close(const net::CompletionCallback& callback) override; |
160 const net::IPEndPoint& ip_endpoint() const override; | 173 const net::IPEndPoint& ip_endpoint() const override; |
161 int id() const override; | 174 int id() const override; |
162 void set_id(int channel_id) override; | 175 void set_id(int channel_id) override; |
163 ReadyState ready_state() const override; | 176 ReadyState ready_state() const override; |
164 ChannelError error_state() const override; | 177 ChannelError error_state() const override; |
165 bool keep_alive() const override; | 178 bool keep_alive() const override; |
166 bool audio_only() const override; | 179 bool audio_only() const override; |
| 180 void AddObserver(Observer* observer) override; |
167 | 181 |
168 protected: | 182 protected: |
169 // CastTransport::Delegate methods for receiving handshake messages. | 183 // CastTransport::Delegate methods for receiving handshake messages. |
170 class AuthTransportDelegate : public CastTransport::Delegate { | 184 class AuthTransportDelegate : public CastTransport::Delegate { |
171 public: | 185 public: |
172 explicit AuthTransportDelegate(CastSocketImpl* socket); | 186 explicit AuthTransportDelegate(CastSocketImpl* socket); |
173 | 187 |
174 // Gets the error state of the channel. | 188 // Gets the error state of the channel. |
175 // Returns CHANNEL_ERROR_NONE if no errors are present. | 189 // Returns CHANNEL_ERROR_NONE if no errors are present. |
176 ChannelError error_state() const; | 190 ChannelError error_state() const; |
177 | 191 |
178 // Gets recorded error details. | 192 // Gets recorded error details. |
179 LastError last_error() const; | 193 LastError last_error() const; |
180 | 194 |
181 // CastTransport::Delegate interface. | 195 // CastTransport::Delegate interface. |
182 void OnError(ChannelError error_state) override; | 196 void OnError(ChannelError error_state) override; |
183 void OnMessage(const CastMessage& message) override; | 197 void OnMessage(const CastMessage& message) override; |
184 void Start() override; | 198 void Start() override; |
185 | 199 |
186 private: | 200 private: |
187 CastSocketImpl* socket_; | 201 CastSocketImpl* socket_; |
188 ChannelError error_state_; | 202 ChannelError error_state_; |
189 LastError last_error_; | 203 LastError last_error_; |
190 }; | 204 }; |
191 | 205 |
| 206 // CastTransport::Delegate methods to receive normal messages and errors. |
| 207 class CastSocketMessageDelegate : public CastTransport::Delegate { |
| 208 public: |
| 209 CastSocketMessageDelegate(CastSocketImpl* socket); |
| 210 ~CastSocketMessageDelegate() override; |
| 211 |
| 212 // CastTransport::Delegate implementation. |
| 213 void OnError(ChannelError error_state) override; |
| 214 void OnMessage(const CastMessage& message) override; |
| 215 void Start() override; |
| 216 |
| 217 private: |
| 218 CastSocketImpl* const socket_; |
| 219 DISALLOW_COPY_AND_ASSIGN(CastSocketMessageDelegate); |
| 220 }; |
| 221 |
192 // Replaces the internally-constructed transport object with one provided | 222 // Replaces the internally-constructed transport object with one provided |
193 // by the caller (e.g. a mock). | 223 // by the caller (e.g. a mock). |
194 void SetTransportForTesting(std::unique_ptr<CastTransport> transport); | 224 void SetTransportForTesting(std::unique_ptr<CastTransport> transport); |
195 | 225 |
196 // Verifies whether the socket complies with cast channel policy. | 226 // Verifies whether the socket complies with cast channel policy. |
197 // Audio only channel policy mandates that a device declaring a video out | 227 // Audio only channel policy mandates that a device declaring a video out |
198 // capability must not have a certificate with audio only policy. | 228 // capability must not have a certificate with audio only policy. |
199 bool VerifyChannelPolicy(const AuthResult& result); | 229 bool VerifyChannelPolicy(const AuthResult& result); |
200 | 230 |
201 private: | 231 private: |
202 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestConnectAuthMessageCorrupted); | 232 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestConnectAuthMessageCorrupted); |
203 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, | 233 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, |
204 TestConnectChallengeReplyReceiveError); | 234 TestConnectChallengeReplyReceiveError); |
205 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, | 235 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, |
206 TestConnectChallengeVerificationFails); | 236 TestConnectChallengeVerificationFails); |
| 237 FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestObservers); |
207 friend class AuthTransportDelegate; | 238 friend class AuthTransportDelegate; |
| 239 friend class CastSocketMessageDelegate; |
208 friend class CastSocketTest; | 240 friend class CastSocketTest; |
209 friend class TestCastSocket; | 241 friend class TestCastSocket; |
210 | 242 |
211 void SetErrorState(ChannelError error_state) override; | 243 void SetErrorState(ChannelError error_state) override; |
212 | 244 |
213 // Frees resources and cancels pending callbacks. |ready_state_| will be set | 245 // 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 | 246 // READY_STATE_CLOSED on completion. A no-op if |ready_state_| is already |
215 // READY_STATE_CLOSED. | 247 // READY_STATE_CLOSED. |
216 void CloseInternal(); | 248 void CloseInternal(); |
217 | 249 |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 // Cast message formatting and parsing layer. | 402 // Cast message formatting and parsing layer. |
371 std::unique_ptr<CastTransport> transport_; | 403 std::unique_ptr<CastTransport> transport_; |
372 | 404 |
373 // Caller's message read and error handling delegate. | 405 // Caller's message read and error handling delegate. |
374 std::unique_ptr<CastTransport::Delegate> delegate_; | 406 std::unique_ptr<CastTransport::Delegate> delegate_; |
375 | 407 |
376 // Raw pointer to the auth handshake delegate. Used to get detailed error | 408 // Raw pointer to the auth handshake delegate. Used to get detailed error |
377 // information. | 409 // information. |
378 AuthTransportDelegate* auth_delegate_; | 410 AuthTransportDelegate* auth_delegate_; |
379 | 411 |
| 412 // List of socket observers. |
| 413 base::ObserverList<Observer> observers_; |
| 414 |
380 DISALLOW_COPY_AND_ASSIGN(CastSocketImpl); | 415 DISALLOW_COPY_AND_ASSIGN(CastSocketImpl); |
381 }; | 416 }; |
382 } // namespace cast_channel | 417 } // namespace cast_channel |
383 | 418 |
384 #endif // COMPONENTS_CAST_CHANNEL_CAST_SOCKET_H_ | 419 #endif // COMPONENTS_CAST_CHANNEL_CAST_SOCKET_H_ |
OLD | NEW |