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
,
42 public cast_channel::CastSocket::Delegate
{
44 explicit CastChannelAPI(content::BrowserContext
* context
);
46 static CastChannelAPI
* Get(content::BrowserContext
* context
);
48 // BrowserContextKeyedAPI implementation.
49 static BrowserContextKeyedAPIFactory
<CastChannelAPI
>* GetFactoryInstance();
51 // Returns a pointer to the Logger member variable.
52 // TODO(imcheng): Consider whether it is possible for this class to own the
53 // CastSockets and make this class the sole owner of Logger.
55 // consider making Logger not ref-counted by passing a weak
56 // reference of Logger to the CastSockets instead.
57 scoped_refptr
<cast_channel::Logger
> GetLogger();
59 // Sets the CastSocket instance to be returned by CreateCastSocket for
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 NULL ptr value.
65 scoped_ptr
<cast_channel::CastSocket
> GetSocketForTest();
68 friend class BrowserContextKeyedAPIFactory
<CastChannelAPI
>;
69 friend class ::CastChannelAPITest
;
71 ~CastChannelAPI() override
;
73 // CastSocket::Delegate. Called on IO thread.
74 void OnError(const cast_channel::CastSocket
* socket
,
75 cast_channel::ChannelError error_state
,
76 const cast_channel::LastErrors
& last_errors
) override
;
77 void OnMessage(const cast_channel::CastSocket
* socket
,
78 const cast_channel::MessageInfo
& message
) override
;
80 // BrowserContextKeyedAPI implementation.
81 static const char* service_name() { return "CastChannelAPI"; }
83 content::BrowserContext
* const browser_context_
;
84 scoped_refptr
<cast_channel::Logger
> logger_
;
85 scoped_ptr
<cast_channel::CastSocket
> socket_for_test_
;
87 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI
);
90 class CastChannelAsyncApiFunction
: public AsyncApiFunction
{
92 CastChannelAsyncApiFunction();
95 ~CastChannelAsyncApiFunction() override
;
98 bool PrePrepare() override
;
99 bool Respond() override
;
101 // Returns the socket corresponding to |channel_id| if one exists. Otherwise,
102 // sets the function result with CHANNEL_ERROR_INVALID_CHANNEL_ID, completes
103 // the function, and returns null.
104 cast_channel::CastSocket
* GetSocketOrCompleteWithError(int channel_id
);
106 // Adds |socket| to |manager_| and returns the new channel_id. |manager_|
107 // assumes ownership of |socket|.
108 int AddSocket(cast_channel::CastSocket
* socket
);
110 // Removes the CastSocket corresponding to |channel_id| from the resource
112 void RemoveSocket(int channel_id
);
114 // Sets the function result to a ChannelInfo obtained from the state of
116 void SetResultFromSocket(const cast_channel::CastSocket
& socket
);
118 // Sets the function result to a ChannelInfo populated with |channel_id| and
120 void SetResultFromError(int channel_id
, cast_channel::ChannelError error
);
122 // Returns the socket corresponding to |channel_id| if one exists, or null
124 cast_channel::CastSocket
* GetSocket(int channel_id
);
127 // Sets the function result from |channel_info|.
128 void SetResultFromChannelInfo(const cast_channel::ChannelInfo
& channel_info
);
130 // The API resource manager for CastSockets.
131 ApiResourceManager
<cast_channel::CastSocket
>* manager_
;
133 // The result of the function.
134 cast_channel::ChannelError error_
;
137 class CastChannelOpenFunction
: public CastChannelAsyncApiFunction
{
139 CastChannelOpenFunction();
142 ~CastChannelOpenFunction() override
;
145 bool PrePrepare() override
;
146 bool Prepare() override
;
147 void AsyncWorkStart() override
;
150 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN
)
152 // Parses the cast:// or casts:// |url|, fills |connect_info| with the
153 // corresponding details, and returns true. Returns false if |url| is not a
155 static bool ParseChannelUrl(const GURL
& url
,
156 cast_channel::ConnectInfo
* connect_info
);
158 // Validates that |connect_info| represents a valid IP end point and returns a
159 // new IPEndPoint if so. Otherwise returns NULL.
160 static net::IPEndPoint
* ParseConnectInfo(
161 const cast_channel::ConnectInfo
& connect_info
);
163 void OnOpen(int result
);
165 scoped_ptr
<cast_channel::Open::Params
> params_
;
166 // The id of the newly opened socket.
168 CastChannelAPI
* api_
;
169 scoped_ptr
<cast_channel::ConnectInfo
> connect_info_
;
170 scoped_ptr
<net::IPEndPoint
> ip_endpoint_
;
171 cast_channel::ChannelAuthType channel_auth_
;
173 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest
, TestParseChannelUrl
);
174 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest
, TestParseConnectInfo
);
175 DISALLOW_COPY_AND_ASSIGN(CastChannelOpenFunction
);
178 class CastChannelSendFunction
: public CastChannelAsyncApiFunction
{
180 CastChannelSendFunction();
183 ~CastChannelSendFunction() override
;
186 bool Prepare() override
;
187 void AsyncWorkStart() override
;
190 DECLARE_EXTENSION_FUNCTION("cast.channel.send", CAST_CHANNEL_SEND
)
192 void OnSend(int result
);
194 scoped_ptr
<cast_channel::Send::Params
> params_
;
196 DISALLOW_COPY_AND_ASSIGN(CastChannelSendFunction
);
199 class CastChannelCloseFunction
: public CastChannelAsyncApiFunction
{
201 CastChannelCloseFunction();
204 ~CastChannelCloseFunction() override
;
207 bool Prepare() override
;
208 void AsyncWorkStart() override
;
211 DECLARE_EXTENSION_FUNCTION("cast.channel.close", CAST_CHANNEL_CLOSE
)
213 void OnClose(int result
);
215 scoped_ptr
<cast_channel::Close::Params
> params_
;
217 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction
);
220 class CastChannelGetLogsFunction
: public CastChannelAsyncApiFunction
{
222 CastChannelGetLogsFunction();
225 ~CastChannelGetLogsFunction() override
;
228 bool PrePrepare() override
;
229 bool Prepare() override
;
230 void AsyncWorkStart() override
;
233 DECLARE_EXTENSION_FUNCTION("cast.channel.getLogs", CAST_CHANNEL_GETLOGS
)
235 CastChannelAPI
* api_
;
237 DISALLOW_COPY_AND_ASSIGN(CastChannelGetLogsFunction
);
240 class CastChannelSetAuthorityKeysFunction
: public CastChannelAsyncApiFunction
{
242 CastChannelSetAuthorityKeysFunction();
245 virtual ~CastChannelSetAuthorityKeysFunction();
248 virtual bool Prepare() override
;
249 virtual void AsyncWorkStart() override
;
252 DECLARE_EXTENSION_FUNCTION("cast.channel.setAuthorityKeys",
253 CAST_CHANNEL_SETAUTHORITYKEYS
)
255 scoped_ptr
<cast_channel::SetAuthorityKeys::Params
> params_
;
257 DISALLOW_COPY_AND_ASSIGN(CastChannelSetAuthorityKeysFunction
);
260 } // namespace extensions
262 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_