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