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"
20 class CastChannelAPITest
;
30 namespace extensions
{
35 namespace cast_channel
{
37 } // namespace cast_channel
40 namespace cast_channel
= api::cast_channel
;
42 class CastChannelAPI
: public BrowserContextKeyedAPI
,
43 public base::SupportsWeakPtr
<CastChannelAPI
> {
45 explicit CastChannelAPI(content::BrowserContext
* context
);
47 static CastChannelAPI
* Get(content::BrowserContext
* context
);
49 // BrowserContextKeyedAPI implementation.
50 static BrowserContextKeyedAPIFactory
<CastChannelAPI
>* GetFactoryInstance();
52 // Returns a pointer to the Logger member variable.
53 // TODO(imcheng): Consider whether it is possible for this class to own the
54 // CastSockets and make this class the sole owner of Logger.
56 // consider making Logger not ref-counted by passing a weak
57 // reference of Logger to the CastSockets instead.
58 scoped_refptr
<cast_channel::Logger
> GetLogger();
60 // Sets the CastSocket instance to be used for testing.
61 void SetSocketForTest(scoped_ptr
<cast_channel::CastSocket
> socket_for_test
);
63 // Returns a test CastSocket instance, if it is defined.
64 // Otherwise returns a scoped_ptr with a nullptr value.
65 scoped_ptr
<cast_channel::CastSocket
> GetSocketForTest();
67 // Returns the API browser context.
68 content::BrowserContext
* GetBrowserContext() const;
70 // Sets injected ping timeout timer for testing.
71 void SetPingTimeoutTimerForTest(scoped_ptr
<base::Timer
> timer
);
73 // Gets the injected ping timeout timer, if set.
74 // Returns a null scoped ptr if there is no injected timer.
75 scoped_ptr
<base::Timer
> GetInjectedTimeoutTimerForTest();
77 // Sends an event to the extension's EventRouter, if it exists.
78 void SendEvent(const std::string
& extension_id
, scoped_ptr
<Event
> event
);
81 friend class BrowserContextKeyedAPIFactory
<CastChannelAPI
>;
82 friend class ::CastChannelAPITest
;
83 friend class CastTransportDelegate
;
85 ~CastChannelAPI() override
;
87 // BrowserContextKeyedAPI implementation.
88 static const char* service_name() { return "CastChannelAPI"; }
90 content::BrowserContext
* const browser_context_
;
91 scoped_refptr
<cast_channel::Logger
> logger_
;
92 scoped_ptr
<cast_channel::CastSocket
> socket_for_test_
;
93 scoped_ptr
<base::Timer
> injected_timeout_timer_
;
95 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI
);
98 class CastChannelAsyncApiFunction
: public AsyncApiFunction
{
100 CastChannelAsyncApiFunction();
103 typedef ApiResourceManager
<cast_channel::CastSocket
>::ApiResourceData
106 ~CastChannelAsyncApiFunction() override
;
109 bool PrePrepare() override
;
110 bool Respond() override
;
112 // Returns the socket corresponding to |channel_id| if one exists. Otherwise,
113 // sets the function result with CHANNEL_ERROR_INVALID_CHANNEL_ID, completes
114 // the function, and returns null.
115 cast_channel::CastSocket
* GetSocketOrCompleteWithError(int channel_id
);
117 // Adds |socket| to |manager_| and returns the new channel_id. |manager_|
118 // assumes ownership of |socket|.
119 int AddSocket(cast_channel::CastSocket
* socket
);
121 // Removes the CastSocket corresponding to |channel_id| from the resource
123 void RemoveSocket(int channel_id
);
125 // Sets the function result to a ChannelInfo obtained from the state of
127 void SetResultFromSocket(const cast_channel::CastSocket
& socket
);
129 // Sets the function result to a ChannelInfo populated with |channel_id| and
131 void SetResultFromError(int channel_id
, cast_channel::ChannelError error
);
133 // Returns the socket corresponding to |channel_id| if one exists, or null
135 cast_channel::CastSocket
* GetSocket(int channel_id
) const;
138 // Sets the function result from |channel_info|.
139 void SetResultFromChannelInfo(const cast_channel::ChannelInfo
& channel_info
);
141 // The collection of CastSocket API resources.
142 scoped_refptr
<SocketData
> sockets_
;
145 class CastChannelOpenFunction
: public CastChannelAsyncApiFunction
{
147 CastChannelOpenFunction();
150 ~CastChannelOpenFunction() override
;
153 bool PrePrepare() override
;
154 bool Prepare() override
;
155 void AsyncWorkStart() override
;
158 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN
)
160 // Defines a callback used to send events to the extension's
162 // Parameter #0 is the extension's ID.
163 // Parameter #1 is a scoped pointer to the event payload.
164 using EventDispatchCallback
=
165 base::Callback
<void(const std::string
&, scoped_ptr
<Event
>)>;
167 // Receives incoming messages and errors and provides additional API and
168 // origin socket context.
169 class CastMessageHandler
: public cast_channel::CastTransport::Delegate
{
171 CastMessageHandler(const EventDispatchCallback
& ui_dispatch_cb
,
172 cast_channel::CastSocket
* socket
,
173 scoped_refptr
<api::cast_channel::Logger
> logger
);
174 ~CastMessageHandler() override
;
176 // CastTransport::Delegate implementation.
177 void OnError(cast_channel::ChannelError error_state
) override
;
178 void OnMessage(const cast_channel::CastMessage
& message
) override
;
179 void Start() override
;
182 // Callback for sending events to the extension.
183 // Should be bound to a weak pointer, to prevent any use-after-free
185 EventDispatchCallback
const ui_dispatch_cb_
;
186 cast_channel::CastSocket
* const socket_
;
187 // Logger object for reporting error details.
188 scoped_refptr
<api::cast_channel::Logger
> logger_
;
190 DISALLOW_COPY_AND_ASSIGN(CastMessageHandler
);
193 // Validates that |connect_info| represents a valid IP end point and returns a
194 // new IPEndPoint if so. Otherwise returns nullptr.
195 static net::IPEndPoint
* ParseConnectInfo(
196 const cast_channel::ConnectInfo
& connect_info
);
198 void OnOpen(cast_channel::ChannelError result
);
200 scoped_ptr
<cast_channel::Open::Params
> params_
;
201 // The id of the newly opened socket.
203 CastChannelAPI
* api_
;
204 scoped_ptr
<net::IPEndPoint
> ip_endpoint_
;
205 cast_channel::ChannelAuthType channel_auth_
;
206 base::TimeDelta liveness_timeout_
;
207 base::TimeDelta ping_interval_
;
209 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest
, TestParseConnectInfo
);
210 DISALLOW_COPY_AND_ASSIGN(CastChannelOpenFunction
);
213 class CastChannelSendFunction
: public CastChannelAsyncApiFunction
{
215 CastChannelSendFunction();
218 ~CastChannelSendFunction() override
;
221 bool Prepare() override
;
222 void AsyncWorkStart() override
;
225 DECLARE_EXTENSION_FUNCTION("cast.channel.send", CAST_CHANNEL_SEND
)
227 void OnSend(int result
);
229 scoped_ptr
<cast_channel::Send::Params
> params_
;
231 DISALLOW_COPY_AND_ASSIGN(CastChannelSendFunction
);
234 class CastChannelCloseFunction
: public CastChannelAsyncApiFunction
{
236 CastChannelCloseFunction();
239 ~CastChannelCloseFunction() override
;
242 bool Prepare() override
;
243 void AsyncWorkStart() override
;
246 DECLARE_EXTENSION_FUNCTION("cast.channel.close", CAST_CHANNEL_CLOSE
)
248 void OnClose(int result
);
250 scoped_ptr
<cast_channel::Close::Params
> params_
;
252 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction
);
255 class CastChannelGetLogsFunction
: public CastChannelAsyncApiFunction
{
257 CastChannelGetLogsFunction();
260 ~CastChannelGetLogsFunction() override
;
263 bool PrePrepare() override
;
264 bool Prepare() override
;
265 void AsyncWorkStart() override
;
268 DECLARE_EXTENSION_FUNCTION("cast.channel.getLogs", CAST_CHANNEL_GETLOGS
)
270 CastChannelAPI
* api_
;
272 DISALLOW_COPY_AND_ASSIGN(CastChannelGetLogsFunction
);
275 class CastChannelSetAuthorityKeysFunction
: public CastChannelAsyncApiFunction
{
277 CastChannelSetAuthorityKeysFunction();
280 ~CastChannelSetAuthorityKeysFunction() override
;
283 bool Prepare() override
;
284 void AsyncWorkStart() override
;
287 DECLARE_EXTENSION_FUNCTION("cast.channel.setAuthorityKeys",
288 CAST_CHANNEL_SETAUTHORITYKEYS
)
290 scoped_ptr
<cast_channel::SetAuthorityKeys::Params
> params_
;
292 DISALLOW_COPY_AND_ASSIGN(CastChannelSetAuthorityKeysFunction
);
295 } // namespace extensions
297 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_