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
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/threading/thread_checker.h"
14 #include "extensions/browser/api/api_resource_manager.h"
15 #include "extensions/browser/api/async_api_function.h"
16 #include "extensions/browser/api/cast_channel/cast_socket.h"
17 #include "extensions/browser/browser_context_keyed_api_factory.h"
18 #include "extensions/common/api/cast_channel.h"
21 class CastChannelAPITest
;
31 namespace extensions
{
36 namespace cast_channel
{
38 } // namespace cast_channel
39 } // namespace core_api
41 namespace cast_channel
= core_api::cast_channel
;
43 class CastChannelAPI
: public BrowserContextKeyedAPI
,
44 public base::SupportsWeakPtr
<CastChannelAPI
> {
46 explicit CastChannelAPI(content::BrowserContext
* context
);
48 static CastChannelAPI
* Get(content::BrowserContext
* context
);
50 // BrowserContextKeyedAPI implementation.
51 static BrowserContextKeyedAPIFactory
<CastChannelAPI
>* GetFactoryInstance();
53 // Returns a pointer to the Logger member variable.
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.
57 // consider making Logger not ref-counted by passing a weak
58 // reference of Logger to the CastSockets instead.
59 scoped_refptr
<cast_channel::Logger
> GetLogger();
61 // Sets the CastSocket instance to be used for testing.
62 void SetSocketForTest(scoped_ptr
<cast_channel::CastSocket
> socket_for_test
);
64 // Returns a test CastSocket instance, if it is defined.
65 // Otherwise returns a scoped_ptr with a nullptr value.
66 scoped_ptr
<cast_channel::CastSocket
> GetSocketForTest();
68 // Returns the API browser context.
69 content::BrowserContext
* GetBrowserContext() const;
71 // Sets injected ping timeout timer for testing.
72 void SetPingTimeoutTimerForTest(scoped_ptr
<base::Timer
> timer
);
74 // Gets the injected ping timeout timer, if set.
75 // Returns a null scoped ptr if there is no injected timer.
76 scoped_ptr
<base::Timer
> GetInjectedTimeoutTimerForTest();
78 // Sends an event to the extension's EventRouter, if it exists.
79 void SendEvent(const std::string
& extension_id
, scoped_ptr
<Event
> event
);
82 friend class BrowserContextKeyedAPIFactory
<CastChannelAPI
>;
83 friend class ::CastChannelAPITest
;
84 friend class CastTransportDelegate
;
86 ~CastChannelAPI() override
;
88 // BrowserContextKeyedAPI implementation.
89 static const char* service_name() { return "CastChannelAPI"; }
91 content::BrowserContext
* const browser_context_
;
92 scoped_refptr
<cast_channel::Logger
> logger_
;
93 scoped_ptr
<cast_channel::CastSocket
> socket_for_test_
;
94 scoped_ptr
<base::Timer
> injected_timeout_timer_
;
96 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI
);
99 class CastChannelAsyncApiFunction
: public AsyncApiFunction
{
101 CastChannelAsyncApiFunction();
104 ~CastChannelAsyncApiFunction() override
;
107 bool PrePrepare() override
;
108 bool Respond() override
;
110 // Returns the socket corresponding to |channel_id| if one exists. Otherwise,
111 // sets the function result with CHANNEL_ERROR_INVALID_CHANNEL_ID, completes
112 // the function, and returns null.
113 cast_channel::CastSocket
* GetSocketOrCompleteWithError(int channel_id
);
115 // Adds |socket| to |manager_| and returns the new channel_id. |manager_|
116 // assumes ownership of |socket|.
117 int AddSocket(cast_channel::CastSocket
* socket
);
119 // Removes the CastSocket corresponding to |channel_id| from the resource
121 void RemoveSocket(int channel_id
);
123 // Sets the function result to a ChannelInfo obtained from the state of
125 void SetResultFromSocket(const cast_channel::CastSocket
& socket
);
127 // Sets the function result to a ChannelInfo populated with |channel_id| and
129 void SetResultFromError(int channel_id
, cast_channel::ChannelError error
);
131 // Returns the socket corresponding to |channel_id| if one exists, or null
133 cast_channel::CastSocket
* GetSocket(int channel_id
) const;
136 // Sets the function result from |channel_info|.
137 void SetResultFromChannelInfo(const cast_channel::ChannelInfo
& channel_info
);
139 // The API resource manager for CastSockets.
140 ApiResourceManager
<cast_channel::CastSocket
>* manager_
;
143 class CastChannelOpenFunction
: public CastChannelAsyncApiFunction
{
145 CastChannelOpenFunction();
148 ~CastChannelOpenFunction() override
;
151 bool PrePrepare() override
;
152 bool Prepare() override
;
153 void AsyncWorkStart() override
;
156 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN
)
158 // Defines a callback used to send events to the extension's
160 // Parameter #0 is the extension's ID.
161 // Parameter #1 is a scoped pointer to the event payload.
162 using EventDispatchCallback
=
163 base::Callback
<void(const std::string
&, scoped_ptr
<Event
>)>;
165 // Receives incoming messages and errors and provides additional API and
166 // origin socket context.
167 class CastMessageHandler
: public cast_channel::CastTransport::Delegate
{
169 CastMessageHandler(const EventDispatchCallback
& ui_dispatch_cb
,
170 cast_channel::CastSocket
* socket
,
171 scoped_refptr
<core_api::cast_channel::Logger
> logger
);
172 ~CastMessageHandler() override
;
174 // CastTransport::Delegate implementation.
175 void OnError(cast_channel::ChannelError error_state
) override
;
176 void OnMessage(const cast_channel::CastMessage
& message
) override
;
177 void Start() override
;
180 // Callback for sending events to the extension.
181 // Should be bound to a weak pointer, to prevent any use-after-free
183 EventDispatchCallback
const ui_dispatch_cb_
;
184 cast_channel::CastSocket
* const socket_
;
185 // Logger object for reporting error details.
186 scoped_refptr
<core_api::cast_channel::Logger
> logger_
;
188 DISALLOW_COPY_AND_ASSIGN(CastMessageHandler
);
191 // Parses the cast:// or casts:// |url|, fills |connect_info| with the
192 // corresponding details, and returns true. Returns false if |url| is not a
194 static bool ParseChannelUrl(const GURL
& url
,
195 cast_channel::ConnectInfo
* connect_info
);
197 // Validates that |connect_info| represents a valid IP end point and returns a
198 // new IPEndPoint if so. Otherwise returns nullptr.
199 static net::IPEndPoint
* ParseConnectInfo(
200 const cast_channel::ConnectInfo
& connect_info
);
202 void OnOpen(cast_channel::ChannelError result
);
204 scoped_ptr
<cast_channel::Open::Params
> params_
;
205 // The id of the newly opened socket.
207 CastChannelAPI
* api_
;
208 scoped_ptr
<cast_channel::ConnectInfo
> connect_info_
;
209 scoped_ptr
<net::IPEndPoint
> ip_endpoint_
;
210 cast_channel::ChannelAuthType channel_auth_
;
211 base::TimeDelta liveness_timeout_
;
212 base::TimeDelta ping_interval_
;
214 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest
, TestParseChannelUrl
);
215 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest
, TestParseConnectInfo
);
216 DISALLOW_COPY_AND_ASSIGN(CastChannelOpenFunction
);
219 class CastChannelSendFunction
: public CastChannelAsyncApiFunction
{
221 CastChannelSendFunction();
224 ~CastChannelSendFunction() override
;
227 bool Prepare() override
;
228 void AsyncWorkStart() override
;
231 DECLARE_EXTENSION_FUNCTION("cast.channel.send", CAST_CHANNEL_SEND
)
233 void OnSend(int result
);
235 scoped_ptr
<cast_channel::Send::Params
> params_
;
237 DISALLOW_COPY_AND_ASSIGN(CastChannelSendFunction
);
240 class CastChannelCloseFunction
: public CastChannelAsyncApiFunction
{
242 CastChannelCloseFunction();
245 ~CastChannelCloseFunction() override
;
248 bool Prepare() override
;
249 void AsyncWorkStart() override
;
252 DECLARE_EXTENSION_FUNCTION("cast.channel.close", CAST_CHANNEL_CLOSE
)
254 void OnClose(int result
);
256 scoped_ptr
<cast_channel::Close::Params
> params_
;
258 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction
);
261 class CastChannelGetLogsFunction
: public CastChannelAsyncApiFunction
{
263 CastChannelGetLogsFunction();
266 ~CastChannelGetLogsFunction() override
;
269 bool PrePrepare() override
;
270 bool Prepare() override
;
271 void AsyncWorkStart() override
;
274 DECLARE_EXTENSION_FUNCTION("cast.channel.getLogs", CAST_CHANNEL_GETLOGS
)
276 CastChannelAPI
* api_
;
278 DISALLOW_COPY_AND_ASSIGN(CastChannelGetLogsFunction
);
281 class CastChannelSetAuthorityKeysFunction
: public CastChannelAsyncApiFunction
{
283 CastChannelSetAuthorityKeysFunction();
286 ~CastChannelSetAuthorityKeysFunction() override
;
289 bool Prepare() override
;
290 void AsyncWorkStart() override
;
293 DECLARE_EXTENSION_FUNCTION("cast.channel.setAuthorityKeys",
294 CAST_CHANNEL_SETAUTHORITYKEYS
)
296 scoped_ptr
<cast_channel::SetAuthorityKeys::Params
> params_
;
298 DISALLOW_COPY_AND_ASSIGN(CastChannelSetAuthorityKeysFunction
);
301 } // namespace extensions
303 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_