OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ |
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ | 6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "components/cast_channel/cast_channel_enum.h" | 15 #include "components/cast_channel/cast_channel_enum.h" |
16 #include "extensions/browser/api/api_resource_manager.h" | 16 #include "components/cast_channel/cast_socket.h" |
17 #include "components/cast_channel/proto/logging.pb.h" | |
17 #include "extensions/browser/api/async_api_function.h" | 18 #include "extensions/browser/api/async_api_function.h" |
18 #include "extensions/browser/api/cast_channel/cast_socket.h" | |
19 #include "extensions/browser/browser_context_keyed_api_factory.h" | 19 #include "extensions/browser/browser_context_keyed_api_factory.h" |
20 #include "extensions/common/api/cast_channel.h" | 20 #include "extensions/common/api/cast_channel.h" |
21 | 21 |
22 class CastChannelAPITest; | 22 class CastChannelAPITest; |
23 | 23 |
24 namespace content { | 24 namespace content { |
25 class BrowserContext; | 25 class BrowserContext; |
26 } | 26 } |
27 | 27 |
28 namespace net { | 28 namespace net { |
29 class IPEndPoint; | 29 class IPEndPoint; |
30 } | 30 } |
31 | 31 |
32 namespace cast_channel { | |
33 class CastSocketService; | |
34 class Logger; | |
35 } // namespace cast_channel | |
36 | |
32 namespace extensions { | 37 namespace extensions { |
33 | 38 |
34 struct Event; | 39 struct Event; |
35 | 40 |
36 namespace api { | |
37 namespace cast_channel { | |
38 class CastSocketService; | |
39 class Logger; | |
40 } // namespace cast_channel | |
41 } // namespace api | |
42 | |
43 namespace cast_channel = api::cast_channel; | 41 namespace cast_channel = api::cast_channel; |
Devlin
2017/06/05 14:05:45
Is this the declaration that's causing the need to
zhaobin
2017/06/05 23:59:26
namespace using removed.
Seems not able to clean
Devlin
2017/06/08 14:56:34
Dang. Having three separate "cast_channel" namesp
zhaobin
2017/06/08 17:57:00
Sure. Created crbug.com/731189 to track namespace
| |
44 | 42 |
45 class CastChannelAPI : public BrowserContextKeyedAPI, | 43 class CastChannelAPI : public BrowserContextKeyedAPI, |
46 public base::SupportsWeakPtr<CastChannelAPI> { | 44 public base::SupportsWeakPtr<CastChannelAPI> { |
47 public: | 45 public: |
48 explicit CastChannelAPI(content::BrowserContext* context); | 46 explicit CastChannelAPI(content::BrowserContext* context); |
49 | 47 |
50 static CastChannelAPI* Get(content::BrowserContext* context); | 48 static CastChannelAPI* Get(content::BrowserContext* context); |
51 | 49 |
52 // BrowserContextKeyedAPI implementation. | 50 // BrowserContextKeyedAPI implementation. |
53 static BrowserContextKeyedAPIFactory<CastChannelAPI>* GetFactoryInstance(); | 51 static BrowserContextKeyedAPIFactory<CastChannelAPI>* GetFactoryInstance(); |
54 | 52 |
55 // Returns a pointer to the Logger member variable. | 53 // Returns a pointer to the Logger member variable. |
56 // TODO(imcheng): Consider whether it is possible for this class to own the | 54 // TODO(imcheng): Consider whether it is possible for this class to own the |
57 // CastSockets and make this class the sole owner of Logger. | 55 // CastSockets and make this class the sole owner of Logger. |
58 // Alternatively, | 56 // Alternatively, |
59 // consider making Logger not ref-counted by passing a weak | 57 // consider making Logger not ref-counted by passing a weak |
60 // reference of Logger to the CastSockets instead. | 58 // reference of Logger to the CastSockets instead. |
61 scoped_refptr<cast_channel::Logger> GetLogger(); | 59 scoped_refptr<::cast_channel::Logger> GetLogger(); |
62 | 60 |
63 // Sets the CastSocket instance to be used for testing. | 61 // Sets the CastSocket instance to be used for testing. |
64 void SetSocketForTest( | 62 void SetSocketForTest( |
65 std::unique_ptr<cast_channel::CastSocket> socket_for_test); | 63 std::unique_ptr<::cast_channel::CastSocket> socket_for_test); |
66 | 64 |
67 // Returns a test CastSocket instance, if it is defined. | 65 // Returns a test CastSocket instance, if it is defined. |
68 // Otherwise returns a scoped_ptr with a nullptr value. | 66 // Otherwise returns a scoped_ptr with a nullptr value. |
69 std::unique_ptr<cast_channel::CastSocket> GetSocketForTest(); | 67 std::unique_ptr<::cast_channel::CastSocket> GetSocketForTest(); |
70 | 68 |
71 // Returns the API browser context. | 69 // Returns the API browser context. |
72 content::BrowserContext* GetBrowserContext() const; | 70 content::BrowserContext* GetBrowserContext() const; |
73 | 71 |
74 // Sets injected ping timeout timer for testing. | 72 // Sets injected ping timeout timer for testing. |
75 void SetPingTimeoutTimerForTest(std::unique_ptr<base::Timer> timer); | 73 void SetPingTimeoutTimerForTest(std::unique_ptr<base::Timer> timer); |
76 | 74 |
77 // Gets the injected ping timeout timer, if set. | 75 // Gets the injected ping timeout timer, if set. |
78 // Returns a null scoped ptr if there is no injected timer. | 76 // Returns a null scoped ptr if there is no injected timer. |
79 std::unique_ptr<base::Timer> GetInjectedTimeoutTimerForTest(); | 77 std::unique_ptr<base::Timer> GetInjectedTimeoutTimerForTest(); |
80 | 78 |
81 // Sends an event to the extension's EventRouter, if it exists. | 79 // Sends an event to the extension's EventRouter, if it exists. |
82 void SendEvent(const std::string& extension_id, std::unique_ptr<Event> event); | 80 void SendEvent(const std::string& extension_id, std::unique_ptr<Event> event); |
83 | 81 |
84 private: | 82 private: |
85 friend class BrowserContextKeyedAPIFactory<CastChannelAPI>; | 83 friend class BrowserContextKeyedAPIFactory<CastChannelAPI>; |
86 friend class ::CastChannelAPITest; | 84 friend class ::CastChannelAPITest; |
87 friend class CastTransportDelegate; | 85 friend class CastTransportDelegate; |
88 | 86 |
89 ~CastChannelAPI() override; | 87 ~CastChannelAPI() override; |
90 | 88 |
91 // BrowserContextKeyedAPI implementation. | 89 // BrowserContextKeyedAPI implementation. |
92 static const char* service_name() { return "CastChannelAPI"; } | 90 static const char* service_name() { return "CastChannelAPI"; } |
93 | 91 |
94 content::BrowserContext* const browser_context_; | 92 content::BrowserContext* const browser_context_; |
95 scoped_refptr<cast_channel::Logger> logger_; | 93 scoped_refptr<::cast_channel::Logger> logger_; |
96 std::unique_ptr<cast_channel::CastSocket> socket_for_test_; | 94 std::unique_ptr<::cast_channel::CastSocket> socket_for_test_; |
97 std::unique_ptr<base::Timer> injected_timeout_timer_; | 95 std::unique_ptr<base::Timer> injected_timeout_timer_; |
98 | 96 |
99 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI); | 97 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI); |
100 }; | 98 }; |
101 | 99 |
102 class CastChannelAsyncApiFunction : public AsyncApiFunction { | 100 class CastChannelAsyncApiFunction : public AsyncApiFunction { |
103 public: | 101 public: |
104 CastChannelAsyncApiFunction(); | 102 CastChannelAsyncApiFunction(); |
105 | 103 |
106 protected: | 104 protected: |
107 ~CastChannelAsyncApiFunction() override; | 105 ~CastChannelAsyncApiFunction() override; |
108 | 106 |
109 // AsyncApiFunction: | 107 // AsyncApiFunction: |
110 bool PrePrepare() override; | 108 bool PrePrepare() override; |
111 bool Respond() override; | 109 bool Respond() override; |
112 | 110 |
113 // Returns the socket corresponding to |channel_id| if one exists. Otherwise, | 111 // Returns the socket corresponding to |channel_id| if one exists. Otherwise, |
114 // sets the function result with CHANNEL_ERROR_INVALID_CHANNEL_ID, completes | 112 // sets the function result with CHANNEL_ERROR_INVALID_CHANNEL_ID, completes |
115 // the function, and returns null. | 113 // the function, and returns null. |
116 cast_channel::CastSocket* GetSocketOrCompleteWithError(int channel_id); | 114 ::cast_channel::CastSocket* GetSocketOrCompleteWithError(int channel_id); |
117 | 115 |
118 // Adds |socket| to |manager_| and returns the new channel_id. |manager_| | 116 // Adds |socket| to |manager_| and returns the new channel_id. |manager_| |
119 // assumes ownership of |socket|. | 117 // assumes ownership of |socket|. |
120 int AddSocket(std::unique_ptr<cast_channel::CastSocket> socket); | 118 int AddSocket(std::unique_ptr<::cast_channel::CastSocket> socket); |
121 | 119 |
122 // Removes the CastSocket corresponding to |channel_id| from the resource | 120 // Removes the CastSocket corresponding to |channel_id| from the resource |
123 // manager. | 121 // manager. |
124 void RemoveSocket(int channel_id); | 122 void RemoveSocket(int channel_id); |
125 | 123 |
126 // Sets the function result to a ChannelInfo obtained from the state of | 124 // Sets the function result to a ChannelInfo obtained from the state of |
127 // |socket|. | 125 // |socket|. |
128 void SetResultFromSocket(const cast_channel::CastSocket& socket); | 126 void SetResultFromSocket(const ::cast_channel::CastSocket& socket); |
129 | 127 |
130 // Sets the function result to a ChannelInfo populated with |channel_id| and | 128 // Sets the function result to a ChannelInfo populated with |channel_id| and |
131 // |error|. | 129 // |error|. |
132 void SetResultFromError(int channel_id, cast_channel::ChannelError error); | 130 void SetResultFromError(int channel_id, cast_channel::ChannelError error); |
133 | 131 |
134 // Returns the socket corresponding to |channel_id| if one exists, or null | 132 // Returns the socket corresponding to |channel_id| if one exists, or null |
135 // otherwise. | 133 // otherwise. |
136 cast_channel::CastSocket* GetSocket(int channel_id) const; | 134 ::cast_channel::CastSocket* GetSocket(int channel_id) const; |
137 | 135 |
138 private: | 136 private: |
139 // Sets the function result from |channel_info|. | 137 // Sets the function result from |channel_info|. |
140 void SetResultFromChannelInfo(const cast_channel::ChannelInfo& channel_info); | 138 void SetResultFromChannelInfo(const cast_channel::ChannelInfo& channel_info); |
141 | 139 |
142 // Manages creating and removing cast sockets. | 140 // Manages creating and removing cast sockets. |
143 api::cast_channel::CastSocketService* cast_socket_service_; | 141 ::cast_channel::CastSocketService* cast_socket_service_; |
144 }; | 142 }; |
145 | 143 |
146 class CastChannelOpenFunction : public CastChannelAsyncApiFunction { | 144 class CastChannelOpenFunction : public CastChannelAsyncApiFunction { |
147 public: | 145 public: |
148 CastChannelOpenFunction(); | 146 CastChannelOpenFunction(); |
149 | 147 |
150 protected: | 148 protected: |
151 ~CastChannelOpenFunction() override; | 149 ~CastChannelOpenFunction() override; |
152 | 150 |
153 // AsyncApiFunction: | 151 // AsyncApiFunction: |
154 bool PrePrepare() override; | 152 bool PrePrepare() override; |
155 bool Prepare() override; | 153 bool Prepare() override; |
156 void AsyncWorkStart() override; | 154 void AsyncWorkStart() override; |
157 | 155 |
158 private: | 156 private: |
159 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN) | 157 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN) |
160 | 158 |
161 // Defines a callback used to send events to the extension's | 159 // Defines a callback used to send events to the extension's |
162 // EventRouter. | 160 // EventRouter. |
163 // Parameter #0 is a scoped pointer to the event payload. | 161 // Parameter #0 is a scoped pointer to the event payload. |
164 using EventDispatchCallback = base::Callback<void(std::unique_ptr<Event>)>; | 162 using EventDispatchCallback = base::Callback<void(std::unique_ptr<Event>)>; |
165 | 163 |
166 // Receives incoming messages and errors and provides additional API and | 164 // Receives incoming messages and errors and provides additional API and |
167 // origin socket context. | 165 // origin socket context. |
168 class CastMessageHandler : public cast_channel::CastTransport::Delegate { | 166 class CastMessageHandler : public ::cast_channel::CastTransport::Delegate { |
169 public: | 167 public: |
170 CastMessageHandler(const EventDispatchCallback& ui_dispatch_cb, | 168 CastMessageHandler(const EventDispatchCallback& ui_dispatch_cb, |
171 cast_channel::CastSocket* socket, | 169 ::cast_channel::CastSocket* socket, |
172 scoped_refptr<api::cast_channel::Logger> logger); | 170 scoped_refptr<::cast_channel::Logger> logger); |
173 ~CastMessageHandler() override; | 171 ~CastMessageHandler() override; |
174 | 172 |
175 // CastTransport::Delegate implementation. | 173 // CastTransport::Delegate implementation. |
176 void OnError(::cast_channel::ChannelError error_state) override; | 174 void OnError(::cast_channel::ChannelError error_state) override; |
177 void OnMessage(const cast_channel::CastMessage& message) override; | 175 void OnMessage(const ::cast_channel::CastMessage& message) override; |
178 void Start() override; | 176 void Start() override; |
179 | 177 |
180 private: | 178 private: |
181 // Callback for sending events to the extension. | 179 // Callback for sending events to the extension. |
182 // Should be bound to a weak pointer, to prevent any use-after-free | 180 // Should be bound to a weak pointer, to prevent any use-after-free |
183 // conditions. | 181 // conditions. |
184 EventDispatchCallback const ui_dispatch_cb_; | 182 EventDispatchCallback const ui_dispatch_cb_; |
185 cast_channel::CastSocket* const socket_; | 183 ::cast_channel::CastSocket* const socket_; |
186 // Logger object for reporting error details. | 184 // Logger object for reporting error details. |
187 scoped_refptr<api::cast_channel::Logger> logger_; | 185 scoped_refptr<::cast_channel::Logger> logger_; |
188 | 186 |
189 DISALLOW_COPY_AND_ASSIGN(CastMessageHandler); | 187 DISALLOW_COPY_AND_ASSIGN(CastMessageHandler); |
190 }; | 188 }; |
191 | 189 |
192 // Validates that |connect_info| represents a valid IP end point and returns a | 190 // Validates that |connect_info| represents a valid IP end point and returns a |
193 // new IPEndPoint if so. Otherwise returns nullptr. | 191 // new IPEndPoint if so. Otherwise returns nullptr. |
194 static net::IPEndPoint* ParseConnectInfo( | 192 static net::IPEndPoint* ParseConnectInfo( |
195 const cast_channel::ConnectInfo& connect_info); | 193 const cast_channel::ConnectInfo& connect_info); |
196 | 194 |
197 void OnOpen(::cast_channel::ChannelError result); | 195 void OnOpen(::cast_channel::ChannelError result); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 | 247 |
250 std::unique_ptr<cast_channel::Close::Params> params_; | 248 std::unique_ptr<cast_channel::Close::Params> params_; |
251 CastChannelAPI* api_; | 249 CastChannelAPI* api_; |
252 | 250 |
253 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction); | 251 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction); |
254 }; | 252 }; |
255 | 253 |
256 } // namespace extensions | 254 } // namespace extensions |
257 | 255 |
258 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ | 256 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ |
OLD | NEW |