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