Chromium Code Reviews| Index: components/cast_channel/cast_socket.h |
| diff --git a/components/cast_channel/cast_socket.h b/components/cast_channel/cast_socket.h |
| index 6afee6a91bb34101507820d7478776f5d6f3a2a4..5d7d58ea3fd4d3ae2a465843f8b0f18772a31d81 100644 |
| --- a/components/cast_channel/cast_socket.h |
| +++ b/components/cast_channel/cast_socket.h |
| @@ -14,6 +14,7 @@ |
| #include "base/gtest_prod_util.h" |
| #include "base/macros.h" |
| #include "base/memory/ref_counted.h" |
| +#include "base/observer_list.h" |
| #include "base/threading/thread_checker.h" |
| #include "base/timer/timer.h" |
| #include "components/cast_channel/cast_auth_util.h" |
| @@ -55,6 +56,19 @@ enum CastDeviceCapability { |
| // Public interface of the CastSocket class. |
| class CastSocket { |
| public: |
| + class Observer { |
| + public: |
| + virtual ~Observer() {} |
| + |
| + // Invoked when an error occurs on |socket|. |
| + virtual void OnError(const CastSocket& socket, |
| + cast_channel::ChannelError error_state) = 0; |
| + |
| + // Invoked when |socket| receives a message. |
| + virtual void OnMessage(const CastSocket& socket, |
| + const cast_channel::CastMessage& message) = 0; |
| + }; |
| + |
| virtual ~CastSocket() {} |
| // Used by BrowserContextKeyedAPIFactory. |
| @@ -68,10 +82,8 @@ class CastSocket { |
| // If the CastSocket is destroyed while the connection is pending, |callback| |
| // will be invoked with CHANNEL_ERROR_UNKNOWN. In this case, invoking |
| // |callback| must not result in any re-entrancy behavior. |
| - // |delegate| receives message receipt and error events. |
| // 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.
|
| - virtual void Connect(std::unique_ptr<CastTransport::Delegate> delegate, |
| - base::Callback<void(ChannelError)> callback) = 0; |
| + virtual void Connect(base::Callback<void(ChannelError)> callback) = 0; |
| // Closes the channel if not already closed. On completion, the channel will |
| // be in READY_STATE_CLOSED. |
| @@ -112,6 +124,10 @@ class CastSocket { |
| // Returns a pointer to the socket's message transport layer. Can be used to |
| // send and receive CastMessages over the socket. |
| virtual CastTransport* transport() const = 0; |
| + |
| + // 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.
|
| + // and error events. |
| + virtual void AddObserver(Observer* observer) = 0; |
| }; |
| // This class implements a channel between Chrome and a Cast device using a TCP |
| @@ -153,8 +169,7 @@ class CastSocketImpl : public CastSocket { |
| ~CastSocketImpl() override; |
| // CastSocket interface. |
| - void Connect(std::unique_ptr<CastTransport::Delegate> delegate, |
| - base::Callback<void(ChannelError)> callback) override; |
| + void Connect(base::Callback<void(ChannelError)> callback) override; |
| CastTransport* transport() const override; |
| void Close(const net::CompletionCallback& callback) override; |
| const net::IPEndPoint& ip_endpoint() const override; |
| @@ -164,6 +179,7 @@ class CastSocketImpl : public CastSocket { |
| ChannelError error_state() const override; |
| bool keep_alive() const override; |
| bool audio_only() const override; |
| + void AddObserver(Observer* observer) override; |
| protected: |
| // CastTransport::Delegate methods for receiving handshake messages. |
| @@ -189,6 +205,22 @@ class CastSocketImpl : public CastSocket { |
| LastError last_error_; |
| }; |
| + // CastTransport::Delegate methods to receive normal messages and errors. |
| + class CastSocketMessageDelegate : public CastTransport::Delegate { |
| + public: |
| + CastSocketMessageDelegate(cast_channel::CastSocketImpl* socket); |
| + ~CastSocketMessageDelegate() override; |
| + |
| + // CastTransport::Delegate implementation. |
| + void OnError(cast_channel::ChannelError error_state) override; |
| + void OnMessage(const cast_channel::CastMessage& message) override; |
| + void Start() override; |
| + |
| + private: |
| + CastSocketImpl* const socket_; |
| + DISALLOW_COPY_AND_ASSIGN(CastSocketMessageDelegate); |
| + }; |
| + |
| // Replaces the internally-constructed transport object with one provided |
| // by the caller (e.g. a mock). |
| void SetTransportForTesting(std::unique_ptr<CastTransport> transport); |
| @@ -204,7 +236,9 @@ class CastSocketImpl : public CastSocket { |
| TestConnectChallengeReplyReceiveError); |
| FRIEND_TEST_ALL_PREFIXES(CastSocketTest, |
| TestConnectChallengeVerificationFails); |
| + FRIEND_TEST_ALL_PREFIXES(CastSocketTest, TestObservers); |
| friend class AuthTransportDelegate; |
| + friend class CastSocketMessageDelegate; |
| friend class CastSocketTest; |
| friend class TestCastSocket; |
| @@ -377,6 +411,11 @@ class CastSocketImpl : public CastSocket { |
| // information. |
| AuthTransportDelegate* auth_delegate_; |
| + // Map of CastSocket::Observer keyed by observer id. For extension side |
| + // observers, id is extension_id; For browser side observers, id is a hard |
| + // 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.
|
| + base::ObserverList<Observer> observers_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(CastSocketImpl); |
| }; |
| } // namespace cast_channel |