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

Unified Diff: components/cast_channel/cast_socket.h

Issue 2942993003: [cast_channel] Make CastMessageHandler a CastSocket::Observer instead of CastTransport::Delegate (Closed)
Patch Set: resolve code review comments from Mark 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 side-by-side diff with in-line comments
Download patch
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..0e0bb54cb4932eb848fbd0b9336d82047ea9afbc 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;
imcheng 2017/06/21 01:22:52 cast_channel:: not needed, here and below
zhaobin 2017/06/21 17:48:13 Done.
+
+ // 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,7 @@ 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.
- 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 +123,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;
+
+ // 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.
+ // events.
+ virtual void AddObserver(Observer* observer) = 0;
};
// This class implements a channel between Chrome and a Cast device using a TCP
@@ -153,8 +168,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 +178,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 +204,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 +235,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 +410,9 @@ class CastSocketImpl : public CastSocket {
// information.
AuthTransportDelegate* auth_delegate_;
+ // List of socket observers.
+ base::ObserverList<Observer> observers_;
+
DISALLOW_COPY_AND_ASSIGN(CastSocketImpl);
};
} // namespace cast_channel
« no previous file with comments | « no previous file | components/cast_channel/cast_socket.cc » ('j') | components/cast_channel/cast_socket_service.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698