| 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 <string> | 9 #include <string> |
| 9 | 10 |
| 10 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
| 15 #include "extensions/browser/api/api_resource_manager.h" | 15 #include "extensions/browser/api/api_resource_manager.h" |
| 16 #include "extensions/browser/api/async_api_function.h" | 16 #include "extensions/browser/api/async_api_function.h" |
| 17 #include "extensions/browser/api/cast_channel/cast_socket.h" | 17 #include "extensions/browser/api/cast_channel/cast_socket.h" |
| 18 #include "extensions/browser/browser_context_keyed_api_factory.h" | 18 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 19 #include "extensions/common/api/cast_channel.h" | 19 #include "extensions/common/api/cast_channel.h" |
| 20 | 20 |
| 21 class CastChannelAPITest; | 21 class CastChannelAPITest; |
| 22 | 22 |
| 23 namespace content { | 23 namespace content { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 52 | 52 |
| 53 // Returns a pointer to the Logger member variable. | 53 // Returns a pointer to the Logger member variable. |
| 54 // 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 |
| 55 // CastSockets and make this class the sole owner of Logger. | 55 // CastSockets and make this class the sole owner of Logger. |
| 56 // Alternatively, | 56 // Alternatively, |
| 57 // consider making Logger not ref-counted by passing a weak | 57 // consider making Logger not ref-counted by passing a weak |
| 58 // reference of Logger to the CastSockets instead. | 58 // reference of Logger to the CastSockets instead. |
| 59 scoped_refptr<cast_channel::Logger> GetLogger(); | 59 scoped_refptr<cast_channel::Logger> GetLogger(); |
| 60 | 60 |
| 61 // Sets the CastSocket instance to be used for testing. | 61 // Sets the CastSocket instance to be used for testing. |
| 62 void SetSocketForTest(scoped_ptr<cast_channel::CastSocket> socket_for_test); | 62 void SetSocketForTest( |
| 63 std::unique_ptr<cast_channel::CastSocket> socket_for_test); |
| 63 | 64 |
| 64 // Returns a test CastSocket instance, if it is defined. | 65 // Returns a test CastSocket instance, if it is defined. |
| 65 // Otherwise returns a scoped_ptr with a nullptr value. | 66 // Otherwise returns a scoped_ptr with a nullptr value. |
| 66 scoped_ptr<cast_channel::CastSocket> GetSocketForTest(); | 67 std::unique_ptr<cast_channel::CastSocket> GetSocketForTest(); |
| 67 | 68 |
| 68 // Returns the API browser context. | 69 // Returns the API browser context. |
| 69 content::BrowserContext* GetBrowserContext() const; | 70 content::BrowserContext* GetBrowserContext() const; |
| 70 | 71 |
| 71 // Sets injected ping timeout timer for testing. | 72 // Sets injected ping timeout timer for testing. |
| 72 void SetPingTimeoutTimerForTest(scoped_ptr<base::Timer> timer); | 73 void SetPingTimeoutTimerForTest(std::unique_ptr<base::Timer> timer); |
| 73 | 74 |
| 74 // Gets the injected ping timeout timer, if set. | 75 // Gets the injected ping timeout timer, if set. |
| 75 // Returns a null scoped ptr if there is no injected timer. | 76 // Returns a null scoped ptr if there is no injected timer. |
| 76 scoped_ptr<base::Timer> GetInjectedTimeoutTimerForTest(); | 77 std::unique_ptr<base::Timer> GetInjectedTimeoutTimerForTest(); |
| 77 | 78 |
| 78 // Sends an event to the extension's EventRouter, if it exists. | 79 // Sends an event to the extension's EventRouter, if it exists. |
| 79 void SendEvent(const std::string& extension_id, scoped_ptr<Event> event); | 80 void SendEvent(const std::string& extension_id, std::unique_ptr<Event> event); |
| 80 | 81 |
| 81 private: | 82 private: |
| 82 friend class BrowserContextKeyedAPIFactory<CastChannelAPI>; | 83 friend class BrowserContextKeyedAPIFactory<CastChannelAPI>; |
| 83 friend class ::CastChannelAPITest; | 84 friend class ::CastChannelAPITest; |
| 84 friend class CastTransportDelegate; | 85 friend class CastTransportDelegate; |
| 85 | 86 |
| 86 ~CastChannelAPI() override; | 87 ~CastChannelAPI() override; |
| 87 | 88 |
| 88 // BrowserContextKeyedAPI implementation. | 89 // BrowserContextKeyedAPI implementation. |
| 89 static const char* service_name() { return "CastChannelAPI"; } | 90 static const char* service_name() { return "CastChannelAPI"; } |
| 90 | 91 |
| 91 content::BrowserContext* const browser_context_; | 92 content::BrowserContext* const browser_context_; |
| 92 scoped_refptr<cast_channel::Logger> logger_; | 93 scoped_refptr<cast_channel::Logger> logger_; |
| 93 scoped_ptr<cast_channel::CastSocket> socket_for_test_; | 94 std::unique_ptr<cast_channel::CastSocket> socket_for_test_; |
| 94 scoped_ptr<base::Timer> injected_timeout_timer_; | 95 std::unique_ptr<base::Timer> injected_timeout_timer_; |
| 95 | 96 |
| 96 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI); | 97 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI); |
| 97 }; | 98 }; |
| 98 | 99 |
| 99 class CastChannelAsyncApiFunction : public AsyncApiFunction { | 100 class CastChannelAsyncApiFunction : public AsyncApiFunction { |
| 100 public: | 101 public: |
| 101 CastChannelAsyncApiFunction(); | 102 CastChannelAsyncApiFunction(); |
| 102 | 103 |
| 103 protected: | 104 protected: |
| 104 typedef ApiResourceManager<cast_channel::CastSocket>::ApiResourceData | 105 typedef ApiResourceManager<cast_channel::CastSocket>::ApiResourceData |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 void AsyncWorkStart() override; | 157 void AsyncWorkStart() override; |
| 157 | 158 |
| 158 private: | 159 private: |
| 159 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN) | 160 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN) |
| 160 | 161 |
| 161 // Defines a callback used to send events to the extension's | 162 // Defines a callback used to send events to the extension's |
| 162 // EventRouter. | 163 // EventRouter. |
| 163 // Parameter #0 is the extension's ID. | 164 // Parameter #0 is the extension's ID. |
| 164 // Parameter #1 is a scoped pointer to the event payload. | 165 // Parameter #1 is a scoped pointer to the event payload. |
| 165 using EventDispatchCallback = | 166 using EventDispatchCallback = |
| 166 base::Callback<void(const std::string&, scoped_ptr<Event>)>; | 167 base::Callback<void(const std::string&, std::unique_ptr<Event>)>; |
| 167 | 168 |
| 168 // Receives incoming messages and errors and provides additional API and | 169 // Receives incoming messages and errors and provides additional API and |
| 169 // origin socket context. | 170 // origin socket context. |
| 170 class CastMessageHandler : public cast_channel::CastTransport::Delegate { | 171 class CastMessageHandler : public cast_channel::CastTransport::Delegate { |
| 171 public: | 172 public: |
| 172 CastMessageHandler(const EventDispatchCallback& ui_dispatch_cb, | 173 CastMessageHandler(const EventDispatchCallback& ui_dispatch_cb, |
| 173 cast_channel::CastSocket* socket, | 174 cast_channel::CastSocket* socket, |
| 174 scoped_refptr<api::cast_channel::Logger> logger); | 175 scoped_refptr<api::cast_channel::Logger> logger); |
| 175 ~CastMessageHandler() override; | 176 ~CastMessageHandler() override; |
| 176 | 177 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 191 DISALLOW_COPY_AND_ASSIGN(CastMessageHandler); | 192 DISALLOW_COPY_AND_ASSIGN(CastMessageHandler); |
| 192 }; | 193 }; |
| 193 | 194 |
| 194 // Validates that |connect_info| represents a valid IP end point and returns a | 195 // Validates that |connect_info| represents a valid IP end point and returns a |
| 195 // new IPEndPoint if so. Otherwise returns nullptr. | 196 // new IPEndPoint if so. Otherwise returns nullptr. |
| 196 static net::IPEndPoint* ParseConnectInfo( | 197 static net::IPEndPoint* ParseConnectInfo( |
| 197 const cast_channel::ConnectInfo& connect_info); | 198 const cast_channel::ConnectInfo& connect_info); |
| 198 | 199 |
| 199 void OnOpen(cast_channel::ChannelError result); | 200 void OnOpen(cast_channel::ChannelError result); |
| 200 | 201 |
| 201 scoped_ptr<cast_channel::Open::Params> params_; | 202 std::unique_ptr<cast_channel::Open::Params> params_; |
| 202 // The id of the newly opened socket. | 203 // The id of the newly opened socket. |
| 203 int new_channel_id_; | 204 int new_channel_id_; |
| 204 CastChannelAPI* api_; | 205 CastChannelAPI* api_; |
| 205 scoped_ptr<net::IPEndPoint> ip_endpoint_; | 206 std::unique_ptr<net::IPEndPoint> ip_endpoint_; |
| 206 cast_channel::ChannelAuthType channel_auth_; | 207 cast_channel::ChannelAuthType channel_auth_; |
| 207 base::TimeDelta liveness_timeout_; | 208 base::TimeDelta liveness_timeout_; |
| 208 base::TimeDelta ping_interval_; | 209 base::TimeDelta ping_interval_; |
| 209 | 210 |
| 210 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest, TestParseConnectInfo); | 211 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest, TestParseConnectInfo); |
| 211 DISALLOW_COPY_AND_ASSIGN(CastChannelOpenFunction); | 212 DISALLOW_COPY_AND_ASSIGN(CastChannelOpenFunction); |
| 212 }; | 213 }; |
| 213 | 214 |
| 214 class CastChannelSendFunction : public CastChannelAsyncApiFunction { | 215 class CastChannelSendFunction : public CastChannelAsyncApiFunction { |
| 215 public: | 216 public: |
| 216 CastChannelSendFunction(); | 217 CastChannelSendFunction(); |
| 217 | 218 |
| 218 protected: | 219 protected: |
| 219 ~CastChannelSendFunction() override; | 220 ~CastChannelSendFunction() override; |
| 220 | 221 |
| 221 // AsyncApiFunction: | 222 // AsyncApiFunction: |
| 222 bool Prepare() override; | 223 bool Prepare() override; |
| 223 void AsyncWorkStart() override; | 224 void AsyncWorkStart() override; |
| 224 | 225 |
| 225 private: | 226 private: |
| 226 DECLARE_EXTENSION_FUNCTION("cast.channel.send", CAST_CHANNEL_SEND) | 227 DECLARE_EXTENSION_FUNCTION("cast.channel.send", CAST_CHANNEL_SEND) |
| 227 | 228 |
| 228 void OnSend(int result); | 229 void OnSend(int result); |
| 229 | 230 |
| 230 scoped_ptr<cast_channel::Send::Params> params_; | 231 std::unique_ptr<cast_channel::Send::Params> params_; |
| 231 | 232 |
| 232 DISALLOW_COPY_AND_ASSIGN(CastChannelSendFunction); | 233 DISALLOW_COPY_AND_ASSIGN(CastChannelSendFunction); |
| 233 }; | 234 }; |
| 234 | 235 |
| 235 class CastChannelCloseFunction : public CastChannelAsyncApiFunction { | 236 class CastChannelCloseFunction : public CastChannelAsyncApiFunction { |
| 236 public: | 237 public: |
| 237 CastChannelCloseFunction(); | 238 CastChannelCloseFunction(); |
| 238 | 239 |
| 239 protected: | 240 protected: |
| 240 ~CastChannelCloseFunction() override; | 241 ~CastChannelCloseFunction() override; |
| 241 | 242 |
| 242 // AsyncApiFunction: | 243 // AsyncApiFunction: |
| 243 bool Prepare() override; | 244 bool Prepare() override; |
| 244 void AsyncWorkStart() override; | 245 void AsyncWorkStart() override; |
| 245 | 246 |
| 246 private: | 247 private: |
| 247 DECLARE_EXTENSION_FUNCTION("cast.channel.close", CAST_CHANNEL_CLOSE) | 248 DECLARE_EXTENSION_FUNCTION("cast.channel.close", CAST_CHANNEL_CLOSE) |
| 248 | 249 |
| 249 void OnClose(int result); | 250 void OnClose(int result); |
| 250 | 251 |
| 251 scoped_ptr<cast_channel::Close::Params> params_; | 252 std::unique_ptr<cast_channel::Close::Params> params_; |
| 252 | 253 |
| 253 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction); | 254 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction); |
| 254 }; | 255 }; |
| 255 | 256 |
| 256 class CastChannelGetLogsFunction : public CastChannelAsyncApiFunction { | 257 class CastChannelGetLogsFunction : public CastChannelAsyncApiFunction { |
| 257 public: | 258 public: |
| 258 CastChannelGetLogsFunction(); | 259 CastChannelGetLogsFunction(); |
| 259 | 260 |
| 260 protected: | 261 protected: |
| 261 ~CastChannelGetLogsFunction() override; | 262 ~CastChannelGetLogsFunction() override; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 289 private: | 290 private: |
| 290 DECLARE_EXTENSION_FUNCTION("cast.channel.setAuthorityKeys", | 291 DECLARE_EXTENSION_FUNCTION("cast.channel.setAuthorityKeys", |
| 291 CAST_CHANNEL_SETAUTHORITYKEYS) | 292 CAST_CHANNEL_SETAUTHORITYKEYS) |
| 292 | 293 |
| 293 DISALLOW_COPY_AND_ASSIGN(CastChannelSetAuthorityKeysFunction); | 294 DISALLOW_COPY_AND_ASSIGN(CastChannelSetAuthorityKeysFunction); |
| 294 }; | 295 }; |
| 295 | 296 |
| 296 } // namespace extensions | 297 } // namespace extensions |
| 297 | 298 |
| 298 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ | 299 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ |
| OLD | NEW |