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_
10 #include "base/basictypes.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/threading/thread_checker.h"
13 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h"
14 #include "chrome/common/extensions/api/cast_channel.h"
15 #include "extensions/browser/api/api_resource_manager.h"
16 #include "extensions/browser/api/async_api_function.h"
17 #include "extensions/browser/browser_context_keyed_api_factory.h"
20 class CastChannelAPITest
;
30 namespace extensions
{
32 namespace cast_channel
= api::cast_channel
;
34 class CastChannelAPI
: public BrowserContextKeyedAPI
,
35 public cast_channel::CastSocket::Delegate
{
37 explicit CastChannelAPI(content::BrowserContext
* context
);
39 static CastChannelAPI
* Get(content::BrowserContext
* context
);
41 // BrowserContextKeyedAPI implementation.
42 static BrowserContextKeyedAPIFactory
<CastChannelAPI
>* GetFactoryInstance();
44 // Returns a new CastSocket that connects to |ip_endpoint| with authentication
45 // |channel_auth| and is to be owned by |extension_id|.
46 scoped_ptr
<cast_channel::CastSocket
> CreateCastSocket(
47 const std::string
& extension_id
,
48 const net::IPEndPoint
& ip_endpoint
,
49 cast_channel::ChannelAuthType channel_auth
,
50 const base::TimeDelta
& timeout
);
52 // Sets the CastSocket instance to be returned by CreateCastSocket for
54 void SetSocketForTest(scoped_ptr
<cast_channel::CastSocket
> socket_for_test
);
57 friend class BrowserContextKeyedAPIFactory
<CastChannelAPI
>;
58 friend class ::CastChannelAPITest
;
60 virtual ~CastChannelAPI();
62 // CastSocket::Delegate. Called on IO thread.
63 virtual void OnError(const cast_channel::CastSocket
* socket
,
64 cast_channel::ChannelError error
) OVERRIDE
;
65 virtual void OnMessage(const cast_channel::CastSocket
* socket
,
66 const cast_channel::MessageInfo
& message
) OVERRIDE
;
68 // BrowserContextKeyedAPI implementation.
69 static const char* service_name() { return "CastChannelAPI"; }
71 content::BrowserContext
* const browser_context_
;
72 scoped_ptr
<cast_channel::CastSocket
> socket_for_test_
;
74 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI
);
77 class CastChannelAsyncApiFunction
: public AsyncApiFunction
{
79 CastChannelAsyncApiFunction();
82 virtual ~CastChannelAsyncApiFunction();
85 virtual bool PrePrepare() OVERRIDE
;
86 virtual bool Respond() OVERRIDE
;
88 // Returns the socket corresponding to |channel_id| if one exists. Otherwise,
89 // sets the function result with CHANNEL_ERROR_INVALID_CHANNEL_ID, completes
90 // the function, and returns null.
91 cast_channel::CastSocket
* GetSocketOrCompleteWithError(int channel_id
);
93 // Adds |socket| to |manager_| and returns the new channel_id. |manager_|
94 // assumes ownership of |socket|.
95 int AddSocket(cast_channel::CastSocket
* socket
);
97 // Removes the CastSocket corresponding to |channel_id| from the resource
99 void RemoveSocket(int channel_id
);
101 // Sets the function result to a ChannelInfo obtained from the state of the
102 // CastSocket corresponding to |channel_id|.
103 void SetResultFromSocket(int channel_id
);
105 // Sets the function result to a ChannelInfo with |error|.
106 void SetResultFromError(cast_channel::ChannelError error
);
108 // Returns the socket corresponding to |channel_id| if one exists, or null
110 cast_channel::CastSocket
* GetSocket(int channel_id
);
113 // Sets the function result from |channel_info|.
114 void SetResultFromChannelInfo(const cast_channel::ChannelInfo
& channel_info
);
116 // The API resource manager for CastSockets.
117 ApiResourceManager
<cast_channel::CastSocket
>* manager_
;
119 // The result of the function.
120 cast_channel::ChannelError error_
;
123 class CastChannelOpenFunction
: public CastChannelAsyncApiFunction
{
125 CastChannelOpenFunction();
128 virtual ~CastChannelOpenFunction();
131 virtual bool PrePrepare() OVERRIDE
;
132 virtual bool Prepare() OVERRIDE
;
133 virtual void AsyncWorkStart() OVERRIDE
;
136 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN
)
138 // Parses the cast:// or casts:// |url|, fills |connect_info| with the
139 // corresponding details, and returns true. Returns false if |url| is not a
141 static bool ParseChannelUrl(const GURL
& url
,
142 cast_channel::ConnectInfo
* connect_info
);
144 // Validates that |connect_info| represents a valid IP end point and returns a
145 // new IPEndPoint if so. Otherwise returns NULL.
146 static net::IPEndPoint
* ParseConnectInfo(
147 const cast_channel::ConnectInfo
& connect_info
);
149 void OnOpen(int result
);
151 scoped_ptr
<cast_channel::Open::Params
> params_
;
152 // The id of the newly opened socket.
154 CastChannelAPI
* api_
;
155 scoped_ptr
<cast_channel::ConnectInfo
> connect_info_
;
156 scoped_ptr
<net::IPEndPoint
> ip_endpoint_
;
157 cast_channel::ChannelAuthType channel_auth_
;
159 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest
, TestParseChannelUrl
);
160 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest
, TestParseConnectInfo
);
161 DISALLOW_COPY_AND_ASSIGN(CastChannelOpenFunction
);
164 class CastChannelSendFunction
: public CastChannelAsyncApiFunction
{
166 CastChannelSendFunction();
169 virtual ~CastChannelSendFunction();
172 virtual bool Prepare() OVERRIDE
;
173 virtual void AsyncWorkStart() OVERRIDE
;
176 DECLARE_EXTENSION_FUNCTION("cast.channel.send", CAST_CHANNEL_SEND
)
178 void OnSend(int result
);
180 scoped_ptr
<cast_channel::Send::Params
> params_
;
182 DISALLOW_COPY_AND_ASSIGN(CastChannelSendFunction
);
185 class CastChannelCloseFunction
: public CastChannelAsyncApiFunction
{
187 CastChannelCloseFunction();
190 virtual ~CastChannelCloseFunction();
193 virtual bool Prepare() OVERRIDE
;
194 virtual void AsyncWorkStart() OVERRIDE
;
197 DECLARE_EXTENSION_FUNCTION("cast.channel.close", CAST_CHANNEL_CLOSE
)
199 void OnClose(int result
);
201 scoped_ptr
<cast_channel::Close::Params
> params_
;
203 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction
);
206 } // namespace extensions
208 #endif // CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_CHANNEL_API_H_