| 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 } // namespace cast_channel |
| 35 |
| 32 namespace extensions { | 36 namespace extensions { |
| 33 | 37 |
| 34 struct Event; | 38 struct Event; |
| 35 | 39 |
| 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; | |
| 44 | |
| 45 class CastChannelAPI : public BrowserContextKeyedAPI, | 40 class CastChannelAPI : public BrowserContextKeyedAPI, |
| 46 public base::SupportsWeakPtr<CastChannelAPI> { | 41 public base::SupportsWeakPtr<CastChannelAPI> { |
| 47 public: | 42 public: |
| 48 explicit CastChannelAPI(content::BrowserContext* context); | 43 explicit CastChannelAPI(content::BrowserContext* context); |
| 49 | 44 |
| 50 static CastChannelAPI* Get(content::BrowserContext* context); | 45 static CastChannelAPI* Get(content::BrowserContext* context); |
| 51 | 46 |
| 52 // BrowserContextKeyedAPI implementation. | 47 // BrowserContextKeyedAPI implementation. |
| 53 static BrowserContextKeyedAPIFactory<CastChannelAPI>* GetFactoryInstance(); | 48 static BrowserContextKeyedAPIFactory<CastChannelAPI>* GetFactoryInstance(); |
| 54 | 49 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 // AsyncApiFunction: | 104 // AsyncApiFunction: |
| 110 bool PrePrepare() override; | 105 bool PrePrepare() override; |
| 111 bool Respond() override; | 106 bool Respond() override; |
| 112 | 107 |
| 113 // Sets the function result to a ChannelInfo obtained from the state of | 108 // Sets the function result to a ChannelInfo obtained from the state of |
| 114 // |socket|. | 109 // |socket|. |
| 115 void SetResultFromSocket(const cast_channel::CastSocket& socket); | 110 void SetResultFromSocket(const cast_channel::CastSocket& socket); |
| 116 | 111 |
| 117 // Sets the function result to a ChannelInfo populated with |channel_id| and | 112 // Sets the function result to a ChannelInfo populated with |channel_id| and |
| 118 // |error|. | 113 // |error|. |
| 119 void SetResultFromError(int channel_id, cast_channel::ChannelError error); | 114 void SetResultFromError(int channel_id, |
| 115 api::cast_channel::ChannelError error); |
| 120 | 116 |
| 121 // Manages creating and removing Cast sockets. | 117 // Manages creating and removing Cast sockets. |
| 122 scoped_refptr<api::cast_channel::CastSocketService> cast_socket_service_; | 118 scoped_refptr<cast_channel::CastSocketService> cast_socket_service_; |
| 123 | 119 |
| 124 private: | 120 private: |
| 125 // Sets the function result from |channel_info|. | 121 // Sets the function result from |channel_info|. |
| 126 void SetResultFromChannelInfo(const cast_channel::ChannelInfo& channel_info); | 122 void SetResultFromChannelInfo( |
| 123 const api::cast_channel::ChannelInfo& channel_info); |
| 127 }; | 124 }; |
| 128 | 125 |
| 129 class CastChannelOpenFunction : public CastChannelAsyncApiFunction { | 126 class CastChannelOpenFunction : public CastChannelAsyncApiFunction { |
| 130 public: | 127 public: |
| 131 CastChannelOpenFunction(); | 128 CastChannelOpenFunction(); |
| 132 | 129 |
| 133 protected: | 130 protected: |
| 134 ~CastChannelOpenFunction() override; | 131 ~CastChannelOpenFunction() override; |
| 135 | 132 |
| 136 // AsyncApiFunction: | 133 // AsyncApiFunction: |
| 137 bool PrePrepare() override; | 134 bool PrePrepare() override; |
| 138 bool Prepare() override; | 135 bool Prepare() override; |
| 139 void AsyncWorkStart() override; | 136 void AsyncWorkStart() override; |
| 140 | 137 |
| 141 private: | 138 private: |
| 142 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN) | 139 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN) |
| 143 | 140 |
| 144 // Defines a callback used to send events to the extension's | 141 // Defines a callback used to send events to the extension's |
| 145 // EventRouter. | 142 // EventRouter. |
| 146 // Parameter #0 is a scoped pointer to the event payload. | 143 // Parameter #0 is a scoped pointer to the event payload. |
| 147 using EventDispatchCallback = base::Callback<void(std::unique_ptr<Event>)>; | 144 using EventDispatchCallback = base::Callback<void(std::unique_ptr<Event>)>; |
| 148 | 145 |
| 149 // Receives incoming messages and errors and provides additional API and | 146 // Receives incoming messages and errors and provides additional API and |
| 150 // origin socket context. | 147 // origin socket context. |
| 151 class CastMessageHandler : public cast_channel::CastTransport::Delegate { | 148 class CastMessageHandler : public cast_channel::CastTransport::Delegate { |
| 152 public: | 149 public: |
| 153 CastMessageHandler(const EventDispatchCallback& ui_dispatch_cb, | 150 CastMessageHandler(const EventDispatchCallback& ui_dispatch_cb, |
| 154 cast_channel::CastSocket* socket, | 151 cast_channel::CastSocket* socket, |
| 155 scoped_refptr<api::cast_channel::Logger> logger); | 152 scoped_refptr<cast_channel::Logger> logger); |
| 156 ~CastMessageHandler() override; | 153 ~CastMessageHandler() override; |
| 157 | 154 |
| 158 // CastTransport::Delegate implementation. | 155 // CastTransport::Delegate implementation. |
| 159 void OnError(::cast_channel::ChannelError error_state) override; | 156 void OnError(cast_channel::ChannelError error_state) override; |
| 160 void OnMessage(const cast_channel::CastMessage& message) override; | 157 void OnMessage(const cast_channel::CastMessage& message) override; |
| 161 void Start() override; | 158 void Start() override; |
| 162 | 159 |
| 163 private: | 160 private: |
| 164 // Callback for sending events to the extension. | 161 // Callback for sending events to the extension. |
| 165 // Should be bound to a weak pointer, to prevent any use-after-free | 162 // Should be bound to a weak pointer, to prevent any use-after-free |
| 166 // conditions. | 163 // conditions. |
| 167 EventDispatchCallback const ui_dispatch_cb_; | 164 EventDispatchCallback const ui_dispatch_cb_; |
| 168 cast_channel::CastSocket* const socket_; | 165 cast_channel::CastSocket* const socket_; |
| 169 // Logger object for reporting error details. | 166 // Logger object for reporting error details. |
| 170 scoped_refptr<api::cast_channel::Logger> logger_; | 167 scoped_refptr<cast_channel::Logger> logger_; |
| 171 | 168 |
| 172 DISALLOW_COPY_AND_ASSIGN(CastMessageHandler); | 169 DISALLOW_COPY_AND_ASSIGN(CastMessageHandler); |
| 173 }; | 170 }; |
| 174 | 171 |
| 175 // Validates that |connect_info| represents a valid IP end point and returns a | 172 // Validates that |connect_info| represents a valid IP end point and returns a |
| 176 // new IPEndPoint if so. Otherwise returns nullptr. | 173 // new IPEndPoint if so. Otherwise returns nullptr. |
| 177 static net::IPEndPoint* ParseConnectInfo( | 174 static net::IPEndPoint* ParseConnectInfo( |
| 178 const cast_channel::ConnectInfo& connect_info); | 175 const api::cast_channel::ConnectInfo& connect_info); |
| 179 | 176 |
| 180 void OnOpen(::cast_channel::ChannelError result); | 177 void OnOpen(cast_channel::ChannelError result); |
| 181 | 178 |
| 182 std::unique_ptr<cast_channel::Open::Params> params_; | 179 std::unique_ptr<api::cast_channel::Open::Params> params_; |
| 183 // The id of the newly opened socket. | 180 // The id of the newly opened socket. |
| 184 int new_channel_id_; | 181 int new_channel_id_; |
| 185 CastChannelAPI* api_; | 182 CastChannelAPI* api_; |
| 186 std::unique_ptr<net::IPEndPoint> ip_endpoint_; | 183 std::unique_ptr<net::IPEndPoint> ip_endpoint_; |
| 187 ::cast_channel::ChannelAuthType channel_auth_; | 184 cast_channel::ChannelAuthType channel_auth_; |
| 188 base::TimeDelta liveness_timeout_; | 185 base::TimeDelta liveness_timeout_; |
| 189 base::TimeDelta ping_interval_; | 186 base::TimeDelta ping_interval_; |
| 190 | 187 |
| 191 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest, TestParseConnectInfo); | 188 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest, TestParseConnectInfo); |
| 192 DISALLOW_COPY_AND_ASSIGN(CastChannelOpenFunction); | 189 DISALLOW_COPY_AND_ASSIGN(CastChannelOpenFunction); |
| 193 }; | 190 }; |
| 194 | 191 |
| 195 class CastChannelSendFunction : public CastChannelAsyncApiFunction { | 192 class CastChannelSendFunction : public CastChannelAsyncApiFunction { |
| 196 public: | 193 public: |
| 197 CastChannelSendFunction(); | 194 CastChannelSendFunction(); |
| 198 | 195 |
| 199 protected: | 196 protected: |
| 200 ~CastChannelSendFunction() override; | 197 ~CastChannelSendFunction() override; |
| 201 | 198 |
| 202 // AsyncApiFunction: | 199 // AsyncApiFunction: |
| 203 bool Prepare() override; | 200 bool Prepare() override; |
| 204 void AsyncWorkStart() override; | 201 void AsyncWorkStart() override; |
| 205 | 202 |
| 206 private: | 203 private: |
| 207 DECLARE_EXTENSION_FUNCTION("cast.channel.send", CAST_CHANNEL_SEND) | 204 DECLARE_EXTENSION_FUNCTION("cast.channel.send", CAST_CHANNEL_SEND) |
| 208 | 205 |
| 209 void OnSend(int result); | 206 void OnSend(int result); |
| 210 | 207 |
| 211 std::unique_ptr<cast_channel::Send::Params> params_; | 208 std::unique_ptr<api::cast_channel::Send::Params> params_; |
| 212 | 209 |
| 213 DISALLOW_COPY_AND_ASSIGN(CastChannelSendFunction); | 210 DISALLOW_COPY_AND_ASSIGN(CastChannelSendFunction); |
| 214 }; | 211 }; |
| 215 | 212 |
| 216 class CastChannelCloseFunction : public CastChannelAsyncApiFunction { | 213 class CastChannelCloseFunction : public CastChannelAsyncApiFunction { |
| 217 public: | 214 public: |
| 218 CastChannelCloseFunction(); | 215 CastChannelCloseFunction(); |
| 219 | 216 |
| 220 protected: | 217 protected: |
| 221 ~CastChannelCloseFunction() override; | 218 ~CastChannelCloseFunction() override; |
| 222 | 219 |
| 223 // AsyncApiFunction: | 220 // AsyncApiFunction: |
| 224 bool PrePrepare() override; | 221 bool PrePrepare() override; |
| 225 bool Prepare() override; | 222 bool Prepare() override; |
| 226 void AsyncWorkStart() override; | 223 void AsyncWorkStart() override; |
| 227 | 224 |
| 228 private: | 225 private: |
| 229 DECLARE_EXTENSION_FUNCTION("cast.channel.close", CAST_CHANNEL_CLOSE) | 226 DECLARE_EXTENSION_FUNCTION("cast.channel.close", CAST_CHANNEL_CLOSE) |
| 230 | 227 |
| 231 void OnClose(int result); | 228 void OnClose(int result); |
| 232 | 229 |
| 233 std::unique_ptr<cast_channel::Close::Params> params_; | 230 std::unique_ptr<api::cast_channel::Close::Params> params_; |
| 234 CastChannelAPI* api_; | 231 CastChannelAPI* api_; |
| 235 | 232 |
| 236 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction); | 233 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction); |
| 237 }; | 234 }; |
| 238 | 235 |
| 239 } // namespace extensions | 236 } // namespace extensions |
| 240 | 237 |
| 241 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ | 238 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ |
| OLD | NEW |