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
{
34 namespace cast_channel
{
36 } // namespace cast_channel
37 } // namespace core_api
39 namespace cast_channel
= core_api::cast_channel
;
41 class CastChannelAPI
: public BrowserContextKeyedAPI
{
43 explicit CastChannelAPI(content::BrowserContext
* context
);
45 static CastChannelAPI
* Get(content::BrowserContext
* context
);
47 // BrowserContextKeyedAPI implementation.
48 static BrowserContextKeyedAPIFactory
<CastChannelAPI
>* GetFactoryInstance();
50 // Returns a pointer to the Logger member variable.
51 // TODO(imcheng): Consider whether it is possible for this class to own the
52 // CastSockets and make this class the sole owner of Logger.
54 // consider making Logger not ref-counted by passing a weak
55 // reference of Logger to the CastSockets instead.
56 scoped_refptr
<cast_channel::Logger
> GetLogger();
58 // Sets the CastSocket instance to be used for testing.
59 void SetSocketForTest(scoped_ptr
<cast_channel::CastSocket
> socket_for_test
);
61 // Returns a test CastSocket instance, if it is defined.
62 // Otherwise returns a scoped_ptr with a NULL ptr value.
63 scoped_ptr
<cast_channel::CastSocket
> GetSocketForTest();
65 // Returns the API browser context.
66 content::BrowserContext
* GetBrowserContext() const;
69 friend class BrowserContextKeyedAPIFactory
<CastChannelAPI
>;
70 friend class ::CastChannelAPITest
;
71 friend class CastTransportDelegate
;
73 ~CastChannelAPI() override
;
75 // BrowserContextKeyedAPI implementation.
76 static const char* service_name() { return "CastChannelAPI"; }
78 content::BrowserContext
* const browser_context_
;
79 scoped_refptr
<cast_channel::Logger
> logger_
;
80 scoped_ptr
<cast_channel::CastSocket
> socket_for_test_
;
82 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI
);
85 class CastChannelAsyncApiFunction
: public AsyncApiFunction
{
87 CastChannelAsyncApiFunction();
90 ~CastChannelAsyncApiFunction() override
;
93 bool PrePrepare() override
;
94 bool Respond() override
;
96 // Returns the socket corresponding to |channel_id| if one exists. Otherwise,
97 // sets the function result with CHANNEL_ERROR_INVALID_CHANNEL_ID, completes
98 // the function, and returns null.
99 cast_channel::CastSocket
* GetSocketOrCompleteWithError(int channel_id
);
101 // Adds |socket| to |manager_| and returns the new channel_id. |manager_|
102 // assumes ownership of |socket|.
103 int AddSocket(cast_channel::CastSocket
* socket
);
105 // Removes the CastSocket corresponding to |channel_id| from the resource
107 void RemoveSocket(int channel_id
);
109 // Sets the function result to a ChannelInfo obtained from the state of
111 void SetResultFromSocket(const cast_channel::CastSocket
& socket
);
113 // Sets the function result to a ChannelInfo populated with |channel_id| and
115 void SetResultFromError(int channel_id
, cast_channel::ChannelError error
);
117 // Returns the socket corresponding to |channel_id| if one exists, or null
119 cast_channel::CastSocket
* GetSocket(int channel_id
) const;
122 // Sets the function result from |channel_info|.
123 void SetResultFromChannelInfo(const cast_channel::ChannelInfo
& channel_info
);
125 // The API resource manager for CastSockets.
126 ApiResourceManager
<cast_channel::CastSocket
>* manager_
;
129 class CastChannelOpenFunction
: public CastChannelAsyncApiFunction
{
131 CastChannelOpenFunction();
134 ~CastChannelOpenFunction() override
;
137 bool PrePrepare() override
;
138 bool Prepare() override
;
139 void AsyncWorkStart() override
;
142 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN
)
144 // Processes incoming cast message events and errors,
145 // and provides additional API and socket context for those events.
146 class CastMessageHandler
: public cast_channel::CastTransport::Delegate
{
148 CastMessageHandler(CastChannelAPI
* api
, cast_channel::CastSocket
* socket
);
149 ~CastMessageHandler() override
;
151 void OnError(cast_channel::ChannelError error_state
,
152 const cast_channel::LastErrors
& last_errors
) override
;
153 void OnMessage(const cast_channel::CastMessage
& message
) override
;
156 CastChannelAPI
* const api
;
157 cast_channel::CastSocket
* const socket
;
159 DISALLOW_COPY_AND_ASSIGN(CastMessageHandler
);
162 // Parses the cast:// or casts:// |url|, fills |connect_info| with the
163 // corresponding details, and returns true. Returns false if |url| is not a
165 static bool ParseChannelUrl(const GURL
& url
,
166 cast_channel::ConnectInfo
* connect_info
);
168 // Validates that |connect_info| represents a valid IP end point and returns a
169 // new IPEndPoint if so. Otherwise returns NULL.
170 static net::IPEndPoint
* ParseConnectInfo(
171 const cast_channel::ConnectInfo
& connect_info
);
173 void OnOpen(cast_channel::ChannelError result
);
175 scoped_ptr
<cast_channel::Open::Params
> params_
;
176 // The id of the newly opened socket.
178 CastChannelAPI
* api_
;
179 scoped_ptr
<cast_channel::ConnectInfo
> connect_info_
;
180 scoped_ptr
<net::IPEndPoint
> ip_endpoint_
;
181 cast_channel::ChannelAuthType channel_auth_
;
183 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest
, TestParseChannelUrl
);
184 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest
, TestParseConnectInfo
);
185 DISALLOW_COPY_AND_ASSIGN(CastChannelOpenFunction
);
188 class CastChannelSendFunction
: public CastChannelAsyncApiFunction
{
190 CastChannelSendFunction();
193 ~CastChannelSendFunction() override
;
196 bool Prepare() override
;
197 void AsyncWorkStart() override
;
200 DECLARE_EXTENSION_FUNCTION("cast.channel.send", CAST_CHANNEL_SEND
)
202 void OnSend(int result
);
204 scoped_ptr
<cast_channel::Send::Params
> params_
;
206 DISALLOW_COPY_AND_ASSIGN(CastChannelSendFunction
);
209 class CastChannelCloseFunction
: public CastChannelAsyncApiFunction
{
211 CastChannelCloseFunction();
214 ~CastChannelCloseFunction() override
;
217 bool Prepare() override
;
218 void AsyncWorkStart() override
;
221 DECLARE_EXTENSION_FUNCTION("cast.channel.close", CAST_CHANNEL_CLOSE
)
223 void OnClose(int result
);
225 scoped_ptr
<cast_channel::Close::Params
> params_
;
227 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction
);
230 class CastChannelGetLogsFunction
: public CastChannelAsyncApiFunction
{
232 CastChannelGetLogsFunction();
235 ~CastChannelGetLogsFunction() override
;
238 bool PrePrepare() override
;
239 bool Prepare() override
;
240 void AsyncWorkStart() override
;
243 DECLARE_EXTENSION_FUNCTION("cast.channel.getLogs", CAST_CHANNEL_GETLOGS
)
245 CastChannelAPI
* api_
;
247 DISALLOW_COPY_AND_ASSIGN(CastChannelGetLogsFunction
);
250 class CastChannelSetAuthorityKeysFunction
: public CastChannelAsyncApiFunction
{
252 CastChannelSetAuthorityKeysFunction();
255 virtual ~CastChannelSetAuthorityKeysFunction();
258 virtual bool Prepare() override
;
259 virtual void AsyncWorkStart() override
;
262 DECLARE_EXTENSION_FUNCTION("cast.channel.setAuthorityKeys",
263 CAST_CHANNEL_SETAUTHORITYKEYS
)
265 scoped_ptr
<cast_channel::SetAuthorityKeys::Params
> params_
;
267 DISALLOW_COPY_AND_ASSIGN(CastChannelSetAuthorityKeysFunction
);
270 } // namespace extensions
272 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_