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_SOCKET_SOCKET_API_H_
6 #define EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/ref_counted.h"
12 #include "extensions/browser/api/api_resource_manager.h"
13 #include "extensions/browser/api/async_api_function.h"
14 #include "extensions/browser/extension_function.h"
15 #include "extensions/common/api/socket.h"
16 #include "net/base/address_list.h"
17 #include "net/dns/host_resolver.h"
18 #include "net/socket/tcp_client_socket.h"
20 #if defined(OS_CHROMEOS)
21 #include "extensions/browser/api/socket/app_firewall_hole_manager.h"
26 class ResourceContext
;
31 class URLRequestContextGetter
;
32 class SSLClientSocket
;
35 namespace extensions
{
39 // A simple interface to ApiResourceManager<Socket> or derived class. The goal
40 // of this interface is to allow Socket API functions to use distinct instances
41 // of ApiResourceManager<> depending on the type of socket (old version in
42 // "socket" namespace vs new version in "socket.xxx" namespaces).
43 class SocketResourceManagerInterface
{
45 virtual ~SocketResourceManagerInterface() {}
47 virtual bool SetBrowserContext(content::BrowserContext
* context
) = 0;
48 virtual int Add(Socket
* socket
) = 0;
49 virtual Socket
* Get(const std::string
& extension_id
, int api_resource_id
) = 0;
50 virtual void Remove(const std::string
& extension_id
, int api_resource_id
) = 0;
51 virtual void Replace(const std::string
& extension_id
,
54 virtual base::hash_set
<int>* GetResourceIds(
55 const std::string
& extension_id
) = 0;
58 // Implementation of SocketResourceManagerInterface using an
59 // ApiResourceManager<T> instance (where T derives from Socket).
61 class SocketResourceManager
: public SocketResourceManagerInterface
{
63 SocketResourceManager() : manager_(NULL
) {}
65 bool SetBrowserContext(content::BrowserContext
* context
) override
{
66 manager_
= ApiResourceManager
<T
>::Get(context
);
68 << "There is no socket manager. "
69 "If this assertion is failing during a test, then it is likely that "
70 "TestExtensionSystem is failing to provide an instance of "
71 "ApiResourceManager<Socket>.";
72 return manager_
!= NULL
;
75 int Add(Socket
* socket
) override
{
76 // Note: Cast needed here, because "T" may be a subclass of "Socket".
77 return manager_
->Add(static_cast<T
*>(socket
));
80 Socket
* Get(const std::string
& extension_id
, int api_resource_id
) override
{
81 return manager_
->Get(extension_id
, api_resource_id
);
84 void Replace(const std::string
& extension_id
,
86 Socket
* socket
) override
{
87 manager_
->Replace(extension_id
, api_resource_id
, static_cast<T
*>(socket
));
90 void Remove(const std::string
& extension_id
, int api_resource_id
) override
{
91 manager_
->Remove(extension_id
, api_resource_id
);
94 base::hash_set
<int>* GetResourceIds(
95 const std::string
& extension_id
) override
{
96 return manager_
->GetResourceIds(extension_id
);
100 ApiResourceManager
<T
>* manager_
;
103 class SocketAsyncApiFunction
: public AsyncApiFunction
{
105 SocketAsyncApiFunction();
108 ~SocketAsyncApiFunction() override
;
111 bool PrePrepare() override
;
112 bool Respond() override
;
114 virtual scoped_ptr
<SocketResourceManagerInterface
>
115 CreateSocketResourceManager();
117 int AddSocket(Socket
* socket
);
118 Socket
* GetSocket(int api_resource_id
);
119 void ReplaceSocket(int api_resource_id
, Socket
* socket
);
120 void RemoveSocket(int api_resource_id
);
121 base::hash_set
<int>* GetSocketIds();
123 // A no-op outside of Chrome OS.
124 void OpenFirewallHole(const std::string
& address
,
129 #if defined(OS_CHROMEOS)
130 void OpenFirewallHoleOnUIThread(AppFirewallHole::PortType type
,
133 void OnFirewallHoleOpened(
135 scoped_ptr
<AppFirewallHole
, content::BrowserThread::DeleteOnUIThread
>
137 #endif // OS_CHROMEOS
139 scoped_ptr
<SocketResourceManagerInterface
> manager_
;
142 class SocketExtensionWithDnsLookupFunction
: public SocketAsyncApiFunction
{
144 SocketExtensionWithDnsLookupFunction();
145 ~SocketExtensionWithDnsLookupFunction() override
;
148 bool PrePrepare() override
;
150 void StartDnsLookup(const net::HostPortPair
& host_port_pair
);
151 virtual void AfterDnsLookup(int lookup_result
) = 0;
153 net::AddressList addresses_
;
156 void OnDnsLookup(int resolve_result
);
158 // Weak pointer to the resource context.
159 content::ResourceContext
* resource_context_
;
162 class SocketCreateFunction
: public SocketAsyncApiFunction
{
164 DECLARE_EXTENSION_FUNCTION("socket.create", SOCKET_CREATE
)
166 SocketCreateFunction();
169 ~SocketCreateFunction() override
;
172 bool Prepare() override
;
173 void Work() override
;
176 FRIEND_TEST_ALL_PREFIXES(SocketUnitTest
, Create
);
177 enum SocketType
{ kSocketTypeInvalid
= -1, kSocketTypeTCP
, kSocketTypeUDP
};
179 scoped_ptr
<api::socket::Create::Params
> params_
;
180 SocketType socket_type_
;
183 class SocketDestroyFunction
: public SocketAsyncApiFunction
{
185 DECLARE_EXTENSION_FUNCTION("socket.destroy", SOCKET_DESTROY
)
188 ~SocketDestroyFunction() override
{}
191 bool Prepare() override
;
192 void Work() override
;
198 class SocketConnectFunction
: public SocketExtensionWithDnsLookupFunction
{
200 DECLARE_EXTENSION_FUNCTION("socket.connect", SOCKET_CONNECT
)
202 SocketConnectFunction();
205 ~SocketConnectFunction() override
;
208 bool Prepare() override
;
209 void AsyncWorkStart() override
;
211 // SocketExtensionWithDnsLookupFunction:
212 void AfterDnsLookup(int lookup_result
) override
;
216 void OnConnect(int result
);
219 std::string hostname_
;
223 class SocketDisconnectFunction
: public SocketAsyncApiFunction
{
225 DECLARE_EXTENSION_FUNCTION("socket.disconnect", SOCKET_DISCONNECT
)
228 ~SocketDisconnectFunction() override
{}
231 bool Prepare() override
;
232 void Work() override
;
238 class SocketBindFunction
: public SocketAsyncApiFunction
{
240 DECLARE_EXTENSION_FUNCTION("socket.bind", SOCKET_BIND
)
243 ~SocketBindFunction() override
{}
246 bool Prepare() override
;
247 void AsyncWorkStart() override
;
251 std::string address_
;
255 class SocketListenFunction
: public SocketAsyncApiFunction
{
257 DECLARE_EXTENSION_FUNCTION("socket.listen", SOCKET_LISTEN
)
259 SocketListenFunction();
262 ~SocketListenFunction() override
;
265 bool Prepare() override
;
266 void AsyncWorkStart() override
;
269 scoped_ptr
<api::socket::Listen::Params
> params_
;
272 class SocketAcceptFunction
: public SocketAsyncApiFunction
{
274 DECLARE_EXTENSION_FUNCTION("socket.accept", SOCKET_ACCEPT
)
276 SocketAcceptFunction();
279 ~SocketAcceptFunction() override
;
282 bool Prepare() override
;
283 void AsyncWorkStart() override
;
286 void OnAccept(int result_code
, net::TCPClientSocket
* socket
);
288 scoped_ptr
<api::socket::Accept::Params
> params_
;
291 class SocketReadFunction
: public SocketAsyncApiFunction
{
293 DECLARE_EXTENSION_FUNCTION("socket.read", SOCKET_READ
)
295 SocketReadFunction();
298 ~SocketReadFunction() override
;
301 bool Prepare() override
;
302 void AsyncWorkStart() override
;
303 void OnCompleted(int result
, scoped_refptr
<net::IOBuffer
> io_buffer
);
306 scoped_ptr
<api::socket::Read::Params
> params_
;
309 class SocketWriteFunction
: public SocketAsyncApiFunction
{
311 DECLARE_EXTENSION_FUNCTION("socket.write", SOCKET_WRITE
)
313 SocketWriteFunction();
316 ~SocketWriteFunction() override
;
319 bool Prepare() override
;
320 void AsyncWorkStart() override
;
321 void OnCompleted(int result
);
325 scoped_refptr
<net::IOBuffer
> io_buffer_
;
326 size_t io_buffer_size_
;
329 class SocketRecvFromFunction
: public SocketAsyncApiFunction
{
331 DECLARE_EXTENSION_FUNCTION("socket.recvFrom", SOCKET_RECVFROM
)
333 SocketRecvFromFunction();
336 ~SocketRecvFromFunction() override
;
339 bool Prepare() override
;
340 void AsyncWorkStart() override
;
341 void OnCompleted(int result
,
342 scoped_refptr
<net::IOBuffer
> io_buffer
,
343 const std::string
& address
,
347 scoped_ptr
<api::socket::RecvFrom::Params
> params_
;
350 class SocketSendToFunction
: public SocketExtensionWithDnsLookupFunction
{
352 DECLARE_EXTENSION_FUNCTION("socket.sendTo", SOCKET_SENDTO
)
354 SocketSendToFunction();
357 ~SocketSendToFunction() override
;
360 bool Prepare() override
;
361 void AsyncWorkStart() override
;
362 void OnCompleted(int result
);
364 // SocketExtensionWithDnsLookupFunction:
365 void AfterDnsLookup(int lookup_result
) override
;
371 scoped_refptr
<net::IOBuffer
> io_buffer_
;
372 size_t io_buffer_size_
;
373 std::string hostname_
;
377 class SocketSetKeepAliveFunction
: public SocketAsyncApiFunction
{
379 DECLARE_EXTENSION_FUNCTION("socket.setKeepAlive", SOCKET_SETKEEPALIVE
)
381 SocketSetKeepAliveFunction();
384 ~SocketSetKeepAliveFunction() override
;
387 bool Prepare() override
;
388 void Work() override
;
391 scoped_ptr
<api::socket::SetKeepAlive::Params
> params_
;
394 class SocketSetNoDelayFunction
: public SocketAsyncApiFunction
{
396 DECLARE_EXTENSION_FUNCTION("socket.setNoDelay", SOCKET_SETNODELAY
)
398 SocketSetNoDelayFunction();
401 ~SocketSetNoDelayFunction() override
;
404 bool Prepare() override
;
405 void Work() override
;
408 scoped_ptr
<api::socket::SetNoDelay::Params
> params_
;
411 class SocketGetInfoFunction
: public SocketAsyncApiFunction
{
413 DECLARE_EXTENSION_FUNCTION("socket.getInfo", SOCKET_GETINFO
)
415 SocketGetInfoFunction();
418 ~SocketGetInfoFunction() override
;
421 bool Prepare() override
;
422 void Work() override
;
425 scoped_ptr
<api::socket::GetInfo::Params
> params_
;
428 class SocketGetNetworkListFunction
: public AsyncExtensionFunction
{
430 DECLARE_EXTENSION_FUNCTION("socket.getNetworkList", SOCKET_GETNETWORKLIST
)
433 ~SocketGetNetworkListFunction() override
{}
434 bool RunAsync() override
;
437 void GetNetworkListOnFileThread();
438 void HandleGetNetworkListError();
439 void SendResponseOnUIThread(const net::NetworkInterfaceList
& interface_list
);
442 class SocketJoinGroupFunction
: public SocketAsyncApiFunction
{
444 DECLARE_EXTENSION_FUNCTION("socket.joinGroup", SOCKET_MULTICAST_JOIN_GROUP
)
446 SocketJoinGroupFunction();
449 ~SocketJoinGroupFunction() override
;
452 bool Prepare() override
;
453 void Work() override
;
456 scoped_ptr
<api::socket::JoinGroup::Params
> params_
;
459 class SocketLeaveGroupFunction
: public SocketAsyncApiFunction
{
461 DECLARE_EXTENSION_FUNCTION("socket.leaveGroup", SOCKET_MULTICAST_LEAVE_GROUP
)
463 SocketLeaveGroupFunction();
466 ~SocketLeaveGroupFunction() override
;
469 bool Prepare() override
;
470 void Work() override
;
473 scoped_ptr
<api::socket::LeaveGroup::Params
> params_
;
476 class SocketSetMulticastTimeToLiveFunction
: public SocketAsyncApiFunction
{
478 DECLARE_EXTENSION_FUNCTION("socket.setMulticastTimeToLive",
479 SOCKET_MULTICAST_SET_TIME_TO_LIVE
)
481 SocketSetMulticastTimeToLiveFunction();
484 ~SocketSetMulticastTimeToLiveFunction() override
;
487 bool Prepare() override
;
488 void Work() override
;
491 scoped_ptr
<api::socket::SetMulticastTimeToLive::Params
> params_
;
494 class SocketSetMulticastLoopbackModeFunction
: public SocketAsyncApiFunction
{
496 DECLARE_EXTENSION_FUNCTION("socket.setMulticastLoopbackMode",
497 SOCKET_MULTICAST_SET_LOOPBACK_MODE
)
499 SocketSetMulticastLoopbackModeFunction();
502 ~SocketSetMulticastLoopbackModeFunction() override
;
505 bool Prepare() override
;
506 void Work() override
;
509 scoped_ptr
<api::socket::SetMulticastLoopbackMode::Params
> params_
;
512 class SocketGetJoinedGroupsFunction
: public SocketAsyncApiFunction
{
514 DECLARE_EXTENSION_FUNCTION("socket.getJoinedGroups",
515 SOCKET_MULTICAST_GET_JOINED_GROUPS
)
517 SocketGetJoinedGroupsFunction();
520 ~SocketGetJoinedGroupsFunction() override
;
523 bool Prepare() override
;
524 void Work() override
;
527 scoped_ptr
<api::socket::GetJoinedGroups::Params
> params_
;
530 class SocketSecureFunction
: public SocketAsyncApiFunction
{
532 DECLARE_EXTENSION_FUNCTION("socket.secure", SOCKET_SECURE
);
533 SocketSecureFunction();
536 ~SocketSecureFunction() override
;
539 bool Prepare() override
;
540 void AsyncWorkStart() override
;
543 // Callback from TLSSocket::UpgradeSocketToTLS().
544 void TlsConnectDone(scoped_ptr
<TLSSocket
> socket
, int result
);
546 scoped_ptr
<api::socket::Secure::Params
> params_
;
547 scoped_refptr
<net::URLRequestContextGetter
> url_request_getter_
;
549 DISALLOW_COPY_AND_ASSIGN(SocketSecureFunction
);
552 } // namespace extensions
554 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_