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 |
(...skipping 19 matching lines...) Expand all Loading... |
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. |
36 std::unique_ptr<CastSocket> RemoveSocket(int channel_id); | 36 std::unique_ptr<CastSocket> RemoveSocket(int channel_id); |
37 | 37 |
38 // 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 |
39 // otherwise. | 39 // otherwise. |
40 CastSocket* GetSocket(int channel_id) const; | 40 virtual CastSocket* GetSocket(int channel_id) const; |
41 | 41 |
42 CastSocket* GetSocket(const net::IPEndPoint& ip_endpoint) const; | 42 CastSocket* GetSocket(const net::IPEndPoint& ip_endpoint) const; |
43 | 43 |
44 // Opens cast socket with |ip_endpoint| and invokes |open_cb| when opening | 44 // Opens cast socket with |ip_endpoint| and invokes |open_cb| when opening |
45 // operation finishes. If cast socket with |ip_endpoint| already exists, | 45 // operation finishes. If cast socket with |ip_endpoint| already exists, |
46 // invoke |open_cb| directly with existing socket's channel ID. | 46 // invoke |open_cb| directly with existing socket's channel ID. |
47 // Parameters: | 47 // Parameters: |
48 // |ip_endpoint|: IP address and port of the remote host. | 48 // |ip_endpoint|: IP address and port of the remote host. |
49 // |net_log|: Log of socket events. | 49 // |net_log|: Log of socket events. |
50 // |connect_timeout|: Connection timeout interval. | 50 // |connect_timeout|: Connection timeout interval. |
(...skipping 14 matching lines...) Expand all Loading... |
65 CastSocket::Observer* observer); | 65 CastSocket::Observer* observer); |
66 | 66 |
67 // Opens cast socket with |ip_endpoint| and invokes |open_cb| when opening | 67 // Opens cast socket with |ip_endpoint| and invokes |open_cb| when opening |
68 // operation finishes. If cast socket with |ip_endpoint| already exists, | 68 // operation finishes. If cast socket with |ip_endpoint| already exists, |
69 // invoke |open_cb| directly with existing socket's channel ID. | 69 // invoke |open_cb| directly with existing socket's channel ID. |
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 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 // Returns an observer corresponding to |id|. |
81 CastSocket::Observer* GetObserver(const std::string& id); | 81 CastSocket::Observer* GetObserver(const std::string& id); |
82 | 82 |
83 // 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 |
84 // of the newly added observer. | 84 // of the newly added observer. |
85 CastSocket::Observer* AddObserver( | 85 CastSocket::Observer* AddObserver( |
86 const std::string& id, | 86 const std::string& id, |
87 std::unique_ptr<CastSocket::Observer> observer); | 87 std::unique_ptr<CastSocket::Observer> observer); |
88 | 88 |
89 // Allow test to inject a mock cast socket. | 89 // Allow test to inject a mock cast socket. |
90 void SetSocketForTest(std::unique_ptr<CastSocket> socket_for_test); | 90 void SetSocketForTest(std::unique_ptr<CastSocket> socket_for_test); |
91 | 91 |
92 private: | 92 protected: |
93 ~CastSocketService() override; | 93 ~CastSocketService() override; |
94 | 94 |
| 95 private: |
95 // RefcountedKeyedService implementation. | 96 // RefcountedKeyedService implementation. |
96 void ShutdownOnUIThread() override; | 97 void ShutdownOnUIThread() override; |
97 | 98 |
98 // Used to generate CastSocket id. | 99 // Used to generate CastSocket id. |
99 static int last_channel_id_; | 100 static int last_channel_id_; |
100 | 101 |
101 // The collection of CastSocket keyed by channel_id. | 102 // The collection of CastSocket keyed by channel_id. |
102 std::map<int, std::unique_ptr<CastSocket>> sockets_; | 103 std::map<int, std::unique_ptr<CastSocket>> sockets_; |
103 | 104 |
104 // Map of CastSocket::Observer keyed by observer id. For extension side | 105 // Map of CastSocket::Observer keyed by observer id. For extension side |
105 // observers, id is extension_id; For browser side observers, id is a hard | 106 // observers, id is extension_id; For browser side observers, id is a hard |
106 // coded string. | 107 // coded string. |
107 std::map<std::string, std::unique_ptr<CastSocket::Observer>> | 108 std::map<std::string, std::unique_ptr<CastSocket::Observer>> |
108 socket_observer_map_; | 109 socket_observer_map_; |
109 | 110 |
110 scoped_refptr<cast_channel::Logger> logger_; | 111 scoped_refptr<cast_channel::Logger> logger_; |
111 | 112 |
112 std::unique_ptr<CastSocket> socket_for_test_; | 113 std::unique_ptr<CastSocket> socket_for_test_; |
113 | 114 |
114 THREAD_CHECKER(thread_checker_); | 115 THREAD_CHECKER(thread_checker_); |
115 | 116 |
116 DISALLOW_COPY_AND_ASSIGN(CastSocketService); | 117 DISALLOW_COPY_AND_ASSIGN(CastSocketService); |
117 }; | 118 }; |
118 | 119 |
119 } // namespace cast_channel | 120 } // namespace cast_channel |
120 | 121 |
121 #endif // COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ | 122 #endif // COMPONENTS_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ |
OLD | NEW |