| 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/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
| 13 #include "components/cast_channel/cast_socket.h" | 13 #include "components/cast_channel/cast_socket.h" |
| 14 #include "components/keyed_service/core/refcounted_keyed_service.h" | 14 #include "components/keyed_service/core/refcounted_keyed_service.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 | 16 |
| 17 namespace cast_channel { | 17 namespace cast_channel { |
| 18 | 18 |
| 19 // This class adds, removes, and returns cast sockets created by CastChannelAPI | 19 // This class adds, removes, and returns cast sockets created by CastChannelAPI |
| 20 // to underlying storage. | 20 // to underlying storage. |
| 21 // Instance of this class is created on the UI thread and destroyed on the IO | 21 // 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. | 22 // thread. All public API must be called from the IO thread. |
| 23 class CastSocketService : public RefcountedKeyedService { | 23 class CastSocketService : public RefcountedKeyedService { |
| 24 public: | 24 public: |
| 25 CastSocketService(); | 25 CastSocketService(); |
| 26 | 26 |
| 27 // Adds |socket| to |sockets_| and returns the new channel_id. Takes ownership | 27 // Returns a pointer to the Logger member variable. |
| 28 // of |socket|. | 28 scoped_refptr<cast_channel::Logger> GetLogger(); |
| 29 int AddSocket(std::unique_ptr<CastSocket> socket); | 29 |
| 30 // Adds |socket| to |sockets_| and returns raw pointer of |socket|. Takes |
| 31 // ownership of |socket|. |
| 32 CastSocket* AddSocket(std::unique_ptr<CastSocket> socket); |
| 30 | 33 |
| 31 // Removes the CastSocket corresponding to |channel_id| from the | 34 // Removes the CastSocket corresponding to |channel_id| from the |
| 32 // CastSocketRegistry. Returns nullptr if no such CastSocket exists. | 35 // CastSocketRegistry. Returns nullptr if no such CastSocket exists. |
| 33 std::unique_ptr<CastSocket> RemoveSocket(int channel_id); | 36 std::unique_ptr<CastSocket> RemoveSocket(int channel_id); |
| 34 | 37 |
| 35 // Returns the socket corresponding to |channel_id| if one exists, or nullptr | 38 // Returns the socket corresponding to |channel_id| if one exists, or nullptr |
| 36 // otherwise. | 39 // otherwise. |
| 37 CastSocket* GetSocket(int channel_id) const; | 40 CastSocket* GetSocket(int channel_id) const; |
| 38 | 41 |
| 42 CastSocket* GetSocket(const net::IPEndPoint& ip_endpoint) const; |
| 43 |
| 44 // Opens cast socket with |ip_endpoint| and invokes |open_cb| when opening |
| 45 // operation finishes. If cast socket with |ip_endpoint| already exists, |
| 46 // invoke |open_cb| directly with existing socket's channel ID. |
| 47 // Parameters: |
| 48 // |ip_endpoint|: IP address and port of the remote host. |
| 49 // |net_log|: Log of socket events. |
| 50 // |connect_timeout|: Connection timeout interval. |
| 51 // |liveness_timeout|: Liveness timeout for connect calls. |
| 52 // |ping_interval|: Ping interval. |
| 53 // |logger|: Log of cast channel events. |
| 54 // |device_capabilities|: Device capabilities. |
| 55 // |open_cb|: OnOpenCallback invoked when cast socket is opened. |
| 56 // |observer|: Observer handles messages and errors on newly opened socket. |
| 57 // Does not take ownership of |observer|. |
| 58 int OpenSocket(const net::IPEndPoint& ip_endpoint, |
| 59 net::NetLog* net_log, |
| 60 const base::TimeDelta& connect_timeout, |
| 61 const base::TimeDelta& liveness_timeout, |
| 62 const base::TimeDelta& ping_interval, |
| 63 uint64_t device_capabilities, |
| 64 const CastSocket::OnOpenCallback& open_cb, |
| 65 CastSocket::Observer* observer); |
| 66 |
| 67 // Opens cast socket with |ip_endpoint| and invokes |open_cb| when opening |
| 68 // operation finishes. If cast socket with |ip_endpoint| already exists, |
| 69 // invoke |open_cb| directly with existing socket's channel ID. |
| 70 // |ip_endpoint|: IP address and port of the remote host. |
| 71 // |net_log|: Log of socket events. |
| 72 // |open_cb|: OnOpenCallback invoked when cast socket is opened. |
| 73 // |observer|: Observer handles messages and errors on newly opened socket. |
| 74 // Does not take ownership of |observer|. |
| 75 int OpenSocket(const net::IPEndPoint& ip_endpoint, |
| 76 net::NetLog* net_log, |
| 77 const CastSocket::OnOpenCallback& open_cb, |
| 78 CastSocket::Observer* observer); |
| 79 |
| 39 // Returns an observer corresponding to |id|. | 80 // Returns an observer corresponding to |id|. |
| 40 CastSocket::Observer* GetObserver(const std::string& id); | 81 CastSocket::Observer* GetObserver(const std::string& id); |
| 41 | 82 |
| 42 // Adds |observer| to |socket_observer_map_| keyed by |id|. Return raw pointer | 83 // Adds |observer| to |socket_observer_map_| keyed by |id|. Return raw pointer |
| 43 // of the newly added observer. | 84 // of the newly added observer. |
| 44 CastSocket::Observer* AddObserver( | 85 CastSocket::Observer* AddObserver( |
| 45 const std::string& id, | 86 const std::string& id, |
| 46 std::unique_ptr<CastSocket::Observer> observer); | 87 std::unique_ptr<CastSocket::Observer> observer); |
| 47 | 88 |
| 89 // Allow test to inject a mock cast socket. |
| 90 void SetSocketForTest(std::unique_ptr<CastSocket> socket_for_test); |
| 91 |
| 48 private: | 92 private: |
| 49 ~CastSocketService() override; | 93 ~CastSocketService() override; |
| 50 | 94 |
| 51 // RefcountedKeyedService implementation. | 95 // RefcountedKeyedService implementation. |
| 52 void ShutdownOnUIThread() override; | 96 void ShutdownOnUIThread() override; |
| 53 | 97 |
| 54 // Used to generate CastSocket id. | 98 // Used to generate CastSocket id. |
| 55 static int last_channel_id_; | 99 static int last_channel_id_; |
| 56 | 100 |
| 57 // The collection of CastSocket keyed by channel_id. | 101 // The collection of CastSocket keyed by channel_id. |
| 58 std::map<int, std::unique_ptr<CastSocket>> sockets_; | 102 std::map<int, std::unique_ptr<CastSocket>> sockets_; |
| 59 | 103 |
| 60 // Map of CastSocket::Observer keyed by observer id. For extension side | 104 // Map of CastSocket::Observer keyed by observer id. For extension side |
| 61 // observers, id is extension_id; For browser side observers, id is a hard | 105 // observers, id is extension_id; For browser side observers, id is a hard |
| 62 // coded string. | 106 // coded string. |
| 63 std::map<std::string, std::unique_ptr<CastSocket::Observer>> | 107 std::map<std::string, std::unique_ptr<CastSocket::Observer>> |
| 64 socket_observer_map_; | 108 socket_observer_map_; |
| 65 | 109 |
| 110 scoped_refptr<cast_channel::Logger> logger_; |
| 111 |
| 112 std::unique_ptr<CastSocket> socket_for_test_; |
| 113 |
| 66 THREAD_CHECKER(thread_checker_); | 114 THREAD_CHECKER(thread_checker_); |
| 67 | 115 |
| 68 DISALLOW_COPY_AND_ASSIGN(CastSocketService); | 116 DISALLOW_COPY_AND_ASSIGN(CastSocketService); |
| 69 }; | 117 }; |
| 70 | 118 |
| 71 } // namespace cast_channel | 119 } // namespace cast_channel |
| 72 | 120 |
| 73 #endif // COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ | 121 #endif // COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ |
| OLD | NEW |