Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_CHANNEL_SERVICE_H_ | 5 #ifndef COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ |
| 6 #define COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ | 6 #define COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/gtest_prod_util.h" | |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
| 13 #include "components/cast_channel/cast_socket.h" | 14 #include "components/cast_channel/cast_socket.h" |
| 14 #include "components/keyed_service/core/refcounted_keyed_service.h" | 15 #include "components/keyed_service/core/refcounted_keyed_service.h" |
| 15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 16 | 17 |
| 17 namespace cast_channel { | 18 namespace cast_channel { |
| 18 | 19 |
| 20 // Receives incoming messages and errors and pass them to inner delegate. | |
| 21 class PassThroughMessageHandler : public CastTransport::Delegate { | |
| 22 public: | |
| 23 PassThroughMessageHandler(); | |
| 24 ~PassThroughMessageHandler() override; | |
| 25 | |
| 26 void RegisterDelegate(std::unique_ptr<CastTransport::Delegate> delegate); | |
| 27 | |
| 28 // CastTransport::Delegate implementation. | |
| 29 void OnError(ChannelError error_state) override; | |
| 30 void OnMessage(const CastMessage& message) override; | |
| 31 void Start() override; | |
| 32 | |
| 33 private: | |
| 34 FRIEND_TEST_ALL_PREFIXES(CastSocketServiceTest, TestRegisterDelegate); | |
| 35 | |
| 36 std::unique_ptr<CastTransport::Delegate> inner_delegate_; | |
| 37 | |
| 38 THREAD_CHECKER(thread_checker_); | |
| 39 | |
| 40 DISALLOW_COPY_AND_ASSIGN(PassThroughMessageHandler); | |
| 41 }; | |
| 42 | |
| 19 // This class adds, removes, and returns cast sockets created by CastChannelAPI | 43 // This class adds, removes, and returns cast sockets created by CastChannelAPI |
| 20 // to underlying storage. | 44 // to underlying storage. |
| 21 // Instance of this class is created on the UI thread and destroyed on the IO | 45 // Instance of this class is created on the UI thread and destroyed on the IO |
| 22 // thread. All public API must be called from the IO thread. | 46 // thread. All public API must be called from the IO thread. |
| 23 class CastSocketService : public RefcountedKeyedService { | 47 class CastSocketService : public RefcountedKeyedService { |
| 24 public: | 48 public: |
| 49 // Callback invoked when cast socket opends. | |
| 50 // |channel_id|: Channel id of cast socket being opened | |
| 51 // |error_state|: Channel error encountered when opening cast socket. | |
| 52 using OnOpenCallback = | |
| 53 base::Callback<void(int channel_id, ChannelError error_state)>; | |
| 54 | |
| 25 CastSocketService(); | 55 CastSocketService(); |
| 26 | 56 |
| 27 // Adds |socket| to |sockets_| and returns the new channel_id. Takes ownership | |
| 28 // of |socket|. | |
| 29 int AddSocket(std::unique_ptr<CastSocket> socket); | |
| 30 | |
| 31 // Removes the CastSocket corresponding to |channel_id| from the | 57 // Removes the CastSocket corresponding to |channel_id| from the |
| 32 // CastSocketRegistry. Returns nullptr if no such CastSocket exists. | 58 // CastSocketRegistry. Returns nullptr if no such CastSocket exists. |
| 33 std::unique_ptr<CastSocket> RemoveSocket(int channel_id); | 59 std::unique_ptr<CastSocket> RemoveSocket(int channel_id); |
| 34 | 60 |
| 35 // Returns the socket corresponding to |channel_id| if one exists, or nullptr | 61 // Returns the socket corresponding to |channel_id| if one exists, or nullptr |
| 36 // otherwise. | 62 // otherwise. |
| 37 CastSocket* GetSocket(int channel_id) const; | 63 CastSocket* GetSocket(int channel_id) const; |
| 38 | 64 |
| 65 // Opens cast socket with |ip_endpoint| and invokes |open_cb| when opening | |
| 66 // operation finishes. If cast socket with |ip_endpoint| already exists, | |
| 67 // invoke |open_cb| directly with existing socket's channel ID. | |
| 68 // Parameters: | |
| 69 // |ip_endpoint|: IP address of the remote host. | |
| 70 // |channel_auth|: Authentication method used for connecting to a Cast | |
| 71 // receiver. |channel_auth| must not be CHANNEL_AUTH_NONE. | |
|
mark a. foltz
2017/06/12 21:14:08
See previous comment re: CHANNEL_AUTH_NONE
zhaobin
2017/06/20 01:37:42
Created crbug.com/732669.
| |
| 72 // |net_log|: Log of socket events. | |
| 73 // |connect_timeout|: Connection timeout interval. | |
| 74 // |ping_interval|: Ping interval. | |
| 75 // |liveness_timeout|: Liveness timeout for connect calls. | |
| 76 // |logger|: Log of cast channel events. | |
| 77 // |device_capabilities|: Device capabilities. | |
| 78 // |open_cb|: Invoked when cast socket opens. | |
| 79 int OpenSocket(const net::IPEndPoint& ip_endpoint, | |
|
mark a. foltz
2017/06/12 21:14:08
With 7 parameters (not including the deprecated Ch
zhaobin
2017/06/20 01:37:42
Created crbug.com/734855
| |
| 80 ChannelAuthType channel_auth, | |
| 81 net::NetLog* net_log, | |
| 82 const base::TimeDelta& connect_timeout, | |
| 83 const base::TimeDelta& ping_interval, | |
| 84 const base::TimeDelta& liveness_timeout, | |
| 85 const scoped_refptr<Logger>& logger, | |
| 86 uint64_t device_capabilities, | |
| 87 const OnOpenCallback& open_cb); | |
| 88 | |
| 89 // Opens cast socket with |ip_endpoint| and invokes |open_cb| when opening | |
| 90 // operation finishes. If cast socket with |ip_endpoint| already exists, | |
| 91 // invoke |open_cb| directly with existing socket's channel ID. | |
| 92 // |ip_endpoint|: IP endpoint to be connected to. | |
| 93 // |net_log|: Net log passed to cast socket. | |
| 94 // |open_cb|: first parameter is channel id of newly created socket; second | |
| 95 // parameter is channel error encountered during channel opening. | |
| 96 int OpenSocket(const net::IPEndPoint& ip_endpoint, | |
| 97 net::NetLog* net_log, | |
| 98 const OnOpenCallback& open_cb); | |
| 99 | |
| 100 // Register read delegate to cast socket with |channel_id|. Returns false if | |
| 101 // such socket does not exist. | |
| 102 bool RegisterDelegate(int channel_id, | |
| 103 std::unique_ptr<CastTransport::Delegate> delegate); | |
| 104 | |
| 105 // Sets the CastSocket instance to be used for testing. | |
| 106 void SetSocketForTest( | |
| 107 std::unique_ptr<cast_channel::CastSocket> socket_for_test); | |
| 108 | |
| 109 // Sets injected ping timeout timer for testing. | |
| 110 void SetPingTimeoutTimerForTest(std::unique_ptr<base::Timer> timer); | |
| 111 | |
| 39 private: | 112 private: |
| 113 FRIEND_TEST_ALL_PREFIXES(CastSocketServiceTest, TestOpenChannel); | |
| 114 friend class CastSocketServiceTest; | |
| 115 | |
| 116 class CastSocketRecord { | |
| 117 public: | |
| 118 // Does not take ownership of |message_handler|. | |
| 119 CastSocketRecord(std::unique_ptr<CastSocket> socket, | |
| 120 const OnOpenCallback& on_open_callback, | |
| 121 PassThroughMessageHandler* message_handler); | |
| 122 | |
| 123 ~CastSocketRecord(); | |
| 124 | |
| 125 // Underlying cast socket. | |
| 126 std::unique_ptr<CastSocket> cast_socket; | |
| 127 | |
| 128 // Pending callbacks to be invoked when cast socket opens. | |
| 129 std::vector<OnOpenCallback> pending_on_open_callbacks; | |
| 130 | |
| 131 // Real read delegate for incoming errors and messages. Owned by |socket|'s | |
| 132 // keep alive delegate. | |
| 133 PassThroughMessageHandler* pass_through_message_handler; | |
| 134 }; | |
| 135 | |
| 40 ~CastSocketService() override; | 136 ~CastSocketService() override; |
| 41 | 137 |
| 42 // RefcountedKeyedService implementation. | 138 // RefcountedKeyedService implementation. |
| 43 void ShutdownOnUIThread() override; | 139 void ShutdownOnUIThread() override; |
| 44 | 140 |
| 141 // Creates a socket record, adds it to |socket_records_| and returns the | |
| 142 // new channel_id. Takes ownership of |socket|. | |
| 143 // |socket|: Underlying cast socket. | |
| 144 // |on_open_callback|: Callback to be invoked when cast socket opens. | |
| 145 // |message_handler|: Real read delegate for incoming errors and messages. | |
| 146 // Does not take ownership of |message_handler|. | |
| 147 int AddSocketRecord(std::unique_ptr<CastSocket> socket, | |
| 148 const OnOpenCallback& on_open_callback, | |
| 149 PassThroughMessageHandler* message_handler); | |
| 150 | |
| 151 // Returns the socket record corresponding to |channel_id| if one exists, or | |
| 152 // nullptr otherwise. | |
| 153 CastSocketRecord* GetSocketRecord(int channel_id) const; | |
|
mark a. foltz
2017/06/12 21:14:08
Should this return a pointer to const, or is it ok
zhaobin
2017/06/20 01:37:42
Code removed.
| |
| 154 | |
| 155 // Returns the socket record whose ip_endpoint() equals to |ip_endpoint|, or | |
| 156 // nullptr if none exists. | |
| 157 CastSocketRecord* GetSocketRecord(const net::IPEndPoint& ip_endpoint) const; | |
|
mark a. foltz
2017/06/12 21:14:08
Same comment applies here.
zhaobin
2017/06/20 01:37:42
Code removed.
| |
| 158 | |
| 159 // Invoked when cast socket opens. | |
| 160 // |channel_id|: Channel id of cast socket. | |
| 161 // |error_state|: error state encountered during cast socket opening. None if | |
| 162 // socket open succeeds. | |
| 163 void OnOpen(int channel_id, ChannelError error_state); | |
| 164 | |
| 45 // Used to generate CastSocket id. | 165 // Used to generate CastSocket id. |
| 46 static int last_channel_id_; | 166 static int last_channel_id_; |
| 47 | 167 |
| 48 // The collection of CastSocket keyed by channel_id. | 168 // The collection of cast socket record keyed by channel_id. |
| 49 std::map<int, std::unique_ptr<CastSocket>> sockets_; | 169 std::map<int, std::unique_ptr<CastSocketRecord>> socket_records_; |
| 170 | |
| 171 // Logger object for reporting error details. | |
| 172 scoped_refptr<Logger> logger_; | |
| 173 | |
| 174 std::unique_ptr<cast_channel::CastSocket> socket_for_test_; | |
| 175 std::unique_ptr<base::Timer> injected_timeout_timer_; | |
| 50 | 176 |
| 51 THREAD_CHECKER(thread_checker_); | 177 THREAD_CHECKER(thread_checker_); |
| 52 | 178 |
| 53 DISALLOW_COPY_AND_ASSIGN(CastSocketService); | 179 DISALLOW_COPY_AND_ASSIGN(CastSocketService); |
| 54 }; | 180 }; |
| 55 | 181 |
| 56 } // namespace cast_channel | 182 } // namespace cast_channel |
| 57 | 183 |
| 58 #endif // COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ | 184 #endif // COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ |
| OLD | NEW |