1 // Copyright 2013 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 CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_CHANNEL_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_CHANNEL_API_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/threading/thread_checker.h"
11 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h"
12 #include "chrome/common/extensions/api/cast_channel.h"
13 #include "extensions/browser/api/api_resource_manager.h"
14 #include "extensions/browser/api/async_api_function.h"
15 #include "extensions/browser/browser_context_keyed_api_factory.h"
18 class CastChannelAPITest
;
28 namespace extensions
{
30 namespace cast_channel
= api::cast_channel
;
32 class CastChannelAPI
: public BrowserContextKeyedAPI
,
33 public cast_channel::CastSocket::Delegate
{
36 explicit CastChannelAPI(content::BrowserContext
* context
);
38 static CastChannelAPI
* Get(content::BrowserContext
* context
);
40 // BrowserContextKeyedAPI implementation.
41 static BrowserContextKeyedAPIFactory
<CastChannelAPI
>* GetFactoryInstance();
43 // Returns a new CastSocket that connects to |ip_endpoint| with authentication
44 // |channel_auth| and is to be owned by |extension_id|.
45 scoped_ptr
<cast_channel::CastSocket
> CreateCastSocket(
46 const std::string
& extension_id
,
47 const net::IPEndPoint
& ip_endpoint
,
48 cast_channel::ChannelAuthType channel_auth
);
50 // Sets the CastSocket instance to be returned by CreateCastSocket for
52 void SetSocketForTest(scoped_ptr
<cast_channel::CastSocket
> socket_for_test
);
55 friend class BrowserContextKeyedAPIFactory
<CastChannelAPI
>;
56 friend class ::CastChannelAPITest
;
58 virtual ~CastChannelAPI();
60 // CastSocket::Delegate. Called on IO thread.
61 virtual void OnError(const cast_channel::CastSocket
* socket
,
62 cast_channel::ChannelError error
) OVERRIDE
;
63 virtual void OnMessage(const cast_channel::CastSocket
* socket
,
64 const cast_channel::MessageInfo
& message
) OVERRIDE
;
66 // BrowserContextKeyedAPI implementation.
67 static const char* service_name() { return "CastChannelAPI"; }
69 content::BrowserContext
* const browser_context_
;
70 scoped_ptr
<cast_channel::CastSocket
> socket_for_test_
;
72 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI
);
75 class CastChannelAsyncApiFunction
: public AsyncApiFunction
{
77 CastChannelAsyncApiFunction();
80 virtual ~CastChannelAsyncApiFunction();
83 virtual bool PrePrepare() OVERRIDE
;
84 virtual bool Respond() OVERRIDE
;
86 // Returns the socket corresponding to |channel_id| if one exists. Otherwise,
87 // sets the function result with CHANNEL_ERROR_INVALID_CHANNEL_ID, completes
88 // the function, and returns null.
89 cast_channel::CastSocket
* GetSocketOrCompleteWithError(int channel_id
);
91 // Adds |socket| to |manager_| and returns the new channel_id. |manager_|
92 // assumes ownership of |socket|.
93 int AddSocket(cast_channel::CastSocket
* socket
);
95 // Removes the CastSocket corresponding to |channel_id| from the resource
97 void RemoveSocket(int channel_id
);
99 // Sets the function result to a ChannelInfo obtained from the state of the
100 // CastSocket corresponding to |channel_id|.
101 void SetResultFromSocket(int channel_id
);
103 // Sets the function result to a ChannelInfo with |error|.
104 void SetResultFromError(cast_channel::ChannelError error
);
106 // Returns the socket corresponding to |channel_id| if one exists, or null
108 cast_channel::CastSocket
* GetSocket(int channel_id
);
111 // Sets the function result from |channel_info|.
112 void SetResultFromChannelInfo(const cast_channel::ChannelInfo
& channel_info
);
114 // The API resource manager for CastSockets.
115 ApiResourceManager
<cast_channel::CastSocket
>* manager_
;
117 // The result of the function.
118 cast_channel::ChannelError error_
;
121 class CastChannelOpenFunction
: public CastChannelAsyncApiFunction
{
123 CastChannelOpenFunction();
126 virtual ~CastChannelOpenFunction();
129 virtual bool PrePrepare() OVERRIDE
;
130 virtual bool Prepare() OVERRIDE
;
131 virtual void AsyncWorkStart() OVERRIDE
;
134 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN
)
136 // Parses the cast:// or casts:// |url|, fills |connect_info| with the
137 // corresponding details, and returns true. Returns false if |url| is not a
139 static bool ParseChannelUrl(const GURL
& url
,
140 api::cast_channel::ConnectInfo
* connect_info
);
142 // Validates that |connect_info| represents a valid IP end point and returns a
143 // new IPEndPoint if so. Otherwise returns NULL.
144 static net::IPEndPoint
* ParseConnectInfo(
145 const api::cast_channel::ConnectInfo
& connect_info
);
147 void OnOpen(int result
);
149 scoped_ptr
<cast_channel::Open::Params
> params_
;
150 // The id of the newly opened socket.
152 CastChannelAPI
* api_
;
153 scoped_ptr
<api::cast_channel::ConnectInfo
> connect_info_
;
154 scoped_ptr
<net::IPEndPoint
> ip_endpoint_
;
155 api::cast_channel::ChannelAuthType channel_auth_
;
157 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest
, TestParseChannelUrl
);
158 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest
, TestParseConnectInfo
);
159 DISALLOW_COPY_AND_ASSIGN(CastChannelOpenFunction
);
162 class CastChannelSendFunction
: public CastChannelAsyncApiFunction
{
164 CastChannelSendFunction();
167 virtual ~CastChannelSendFunction();
170 virtual bool Prepare() OVERRIDE
;
171 virtual void AsyncWorkStart() OVERRIDE
;
174 DECLARE_EXTENSION_FUNCTION("cast.channel.send", CAST_CHANNEL_SEND
)
176 void OnSend(int result
);
178 scoped_ptr
<cast_channel::Send::Params
> params_
;
180 DISALLOW_COPY_AND_ASSIGN(CastChannelSendFunction
);
183 class CastChannelCloseFunction
: public CastChannelAsyncApiFunction
{
185 CastChannelCloseFunction();
188 virtual ~CastChannelCloseFunction();
191 virtual bool Prepare() OVERRIDE
;
192 virtual void AsyncWorkStart() OVERRIDE
;
195 DECLARE_EXTENSION_FUNCTION("cast.channel.close", CAST_CHANNEL_CLOSE
)
197 void OnClose(int result
);
199 scoped_ptr
<cast_channel::Close::Params
> params_
;
201 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction
);
204 } // namespace extensions
206 #endif // CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_CHANNEL_API_H_