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/memory/singleton.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 "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 { |
24 public: | 24 public: |
25 CastSocketService(); | 25 static CastSocketService* GetInstance(); |
26 | 26 |
27 // Returns a pointer to the Logger member variable. | 27 // Returns a pointer to the Logger member variable. |
28 scoped_refptr<cast_channel::Logger> GetLogger(); | 28 scoped_refptr<cast_channel::Logger> GetLogger(); |
29 | 29 |
30 // Adds |socket| to |sockets_| and returns raw pointer of |socket|. Takes | 30 // Adds |socket| to |sockets_| and returns raw pointer of |socket|. Takes |
31 // ownership of |socket|. | 31 // ownership of |socket|. |
32 CastSocket* AddSocket(std::unique_ptr<CastSocket> socket); | 32 CastSocket* AddSocket(std::unique_ptr<CastSocket> socket); |
33 | 33 |
34 // Removes the CastSocket corresponding to |channel_id| from the | 34 // Removes the CastSocket corresponding to |channel_id| from the |
35 // CastSocketRegistry. Returns nullptr if no such CastSocket exists. | 35 // CastSocketRegistry. Returns nullptr if no such CastSocket exists. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 // |ip_endpoint|: IP address and port of the remote host. | 70 // |ip_endpoint|: IP address and port of the remote host. |
71 // |net_log|: Log of socket events. | 71 // |net_log|: Log of socket events. |
72 // |open_cb|: OnOpenCallback invoked when cast socket is opened. | 72 // |open_cb|: OnOpenCallback invoked when cast socket is opened. |
73 // |observer|: Observer handles messages and errors on newly opened socket. | 73 // |observer|: Observer handles messages and errors on newly opened socket. |
74 // Does not take ownership of |observer|. | 74 // Does not take ownership of |observer|. |
75 virtual int OpenSocket(const net::IPEndPoint& ip_endpoint, | 75 virtual int OpenSocket(const net::IPEndPoint& ip_endpoint, |
76 net::NetLog* net_log, | 76 net::NetLog* net_log, |
77 const CastSocket::OnOpenCallback& open_cb, | 77 const CastSocket::OnOpenCallback& open_cb, |
78 CastSocket::Observer* observer); | 78 CastSocket::Observer* observer); |
79 | 79 |
80 // Returns an observer corresponding to |id|. | 80 // Remove |observer| from each socket in |sockets_| |
81 CastSocket::Observer* GetObserver(const std::string& id); | 81 void RemoveObserver(CastSocket::Observer* observer); |
82 | |
83 // Adds |observer| to |socket_observer_map_| keyed by |id|. Return raw pointer | |
84 // of the newly added observer. | |
85 CastSocket::Observer* AddObserver( | |
86 const std::string& id, | |
87 std::unique_ptr<CastSocket::Observer> observer); | |
88 | 82 |
89 // Allow test to inject a mock cast socket. | 83 // Allow test to inject a mock cast socket. |
90 void SetSocketForTest(std::unique_ptr<CastSocket> socket_for_test); | 84 void SetSocketForTest(std::unique_ptr<CastSocket> socket_for_test); |
91 | 85 |
92 protected: | 86 private: |
93 ~CastSocketService() override; | 87 friend class CastSocketServiceTest; |
| 88 friend class MockCastSocketService; |
| 89 friend struct base::DefaultSingletonTraits<CastSocketService>; |
| 90 friend struct std::default_delete<CastSocketService>; |
94 | 91 |
95 private: | 92 CastSocketService(); |
96 // RefcountedKeyedService implementation. | 93 virtual ~CastSocketService(); |
97 void ShutdownOnUIThread() override; | |
98 | 94 |
99 // Used to generate CastSocket id. | 95 // Used to generate CastSocket id. |
100 static int last_channel_id_; | 96 static int last_channel_id_; |
101 | 97 |
102 // The collection of CastSocket keyed by channel_id. | 98 // The collection of CastSocket keyed by channel_id. |
103 std::map<int, std::unique_ptr<CastSocket>> sockets_; | 99 std::map<int, std::unique_ptr<CastSocket>> sockets_; |
104 | 100 |
105 // Map of CastSocket::Observer keyed by observer id. For extension side | 101 scoped_refptr<Logger> logger_; |
106 // observers, id is extension_id; For browser side observers, id is a hard | |
107 // coded string. | |
108 std::map<std::string, std::unique_ptr<CastSocket::Observer>> | |
109 socket_observer_map_; | |
110 | |
111 scoped_refptr<cast_channel::Logger> logger_; | |
112 | 102 |
113 std::unique_ptr<CastSocket> socket_for_test_; | 103 std::unique_ptr<CastSocket> socket_for_test_; |
114 | 104 |
115 THREAD_CHECKER(thread_checker_); | 105 THREAD_CHECKER(thread_checker_); |
116 | 106 |
117 DISALLOW_COPY_AND_ASSIGN(CastSocketService); | 107 DISALLOW_COPY_AND_ASSIGN(CastSocketService); |
118 }; | 108 }; |
119 | 109 |
120 } // namespace cast_channel | 110 } // namespace cast_channel |
121 | 111 |
122 #endif // COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ | 112 #endif // COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ |
OLD | NEW |