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/base/network_change_notifier.h"
18 #include "net/dns/host_resolver.h"
19 #include "net/socket/tcp_client_socket.h"
21 #if defined(OS_CHROMEOS)
22 #include "extensions/browser/api/socket/app_firewall_hole_manager.h"
27 class ResourceContext
;
32 class URLRequestContextGetter
;
33 class SSLClientSocket
;
36 namespace extensions
{
40 // A simple interface to ApiResourceManager<Socket> or derived class. The goal
41 // of this interface is to allow Socket API functions to use distinct instances
42 // of ApiResourceManager<> depending on the type of socket (old version in
43 // "socket" namespace vs new version in "socket.xxx" namespaces).
44 class SocketResourceManagerInterface
{
46 virtual ~SocketResourceManagerInterface() {}
48 virtual bool SetBrowserContext(content::BrowserContext
* context
) = 0;
49 virtual int Add(Socket
* socket
) = 0;
50 virtual Socket
* Get(const std::string
& extension_id
, int api_resource_id
) = 0;
51 virtual void Remove(const std::string
& extension_id
, int api_resource_id
) = 0;
52 virtual void Replace(const std::string
& extension_id
,
55 virtual base::hash_set
<int>* GetResourceIds(
56 const std::string
& extension_id
) = 0;
59 // Implementation of SocketResourceManagerInterface using an
60 // ApiResourceManager<T> instance (where T derives from Socket).
62 class SocketResourceManager
: public SocketResourceManagerInterface
{
64 SocketResourceManager() : manager_(NULL
) {}
66 bool SetBrowserContext(content::BrowserContext
* context
) override
{
67 manager_
= ApiResourceManager
<T
>::Get(context
);
69 << "There is no socket manager. "
70 "If this assertion is failing during a test, then it is likely that "
71 "TestExtensionSystem is failing to provide an instance of "
72 "ApiResourceManager<Socket>.";
73 return manager_
!= NULL
;
76 int Add(Socket
* socket
) override
{
77 // Note: Cast needed here, because "T" may be a subclass of "Socket".
78 return manager_
->Add(static_cast<T
*>(socket
));
81 Socket
* Get(const std::string
& extension_id
, int api_resource_id
) override
{
82 return manager_
->Get(extension_id
, api_resource_id
);
85 void Replace(const std::string
& extension_id
,
87 Socket
* socket
) override
{
88 manager_
->Replace(extension_id
, api_resource_id
, static_cast<T
*>(socket
));
91 void Remove(const std::string
& extension_id
, int api_resource_id
) override
{
92 manager_
->Remove(extension_id
, api_resource_id
);
95 base::hash_set
<int>* GetResourceIds(
96 const std::string
& extension_id
) override
{
97 return manager_
->GetResourceIds(extension_id
);
101 ApiResourceManager
<T
>* manager_
;
104 class SocketAsyncApiFunction
: public AsyncApiFunction
{
106 SocketAsyncApiFunction();
109 ~SocketAsyncApiFunction() override
;
112 bool PrePrepare() override
;
113 bool Respond() override
;
115 virtual scoped_ptr
<SocketResourceManagerInterface
>
116 CreateSocketResourceManager();
118 int AddSocket(Socket
* socket
);
119 Socket
* GetSocket(int api_resource_id
);
120 void ReplaceSocket(int api_resource_id
, Socket
* socket
);
121 void RemoveSocket(int api_resource_id
);
122 base::hash_set
<int>* GetSocketIds();
124 // A no-op outside of Chrome OS.
125 void OpenFirewallHole(const std::string
& address
,
130 #if defined(OS_CHROMEOS)
131 void OpenFirewallHoleOnUIThread(AppFirewallHole::PortType type
,
134 void OnFirewallHoleOpened(
136 scoped_ptr
<AppFirewallHole
, content::BrowserThread::DeleteOnUIThread
>
138 #endif // OS_CHROMEOS
140 scoped_ptr
<SocketResourceManagerInterface
> manager_
;
143 class SocketExtensionWithDnsLookupFunction
: public SocketAsyncApiFunction
{
145 SocketExtensionWithDnsLookupFunction();
146 ~SocketExtensionWithDnsLookupFunction() override
;
149 bool PrePrepare() override
;
151 void StartDnsLookup(const net::HostPortPair
& host_port_pair
);
152 virtual void AfterDnsLookup(int lookup_result
) = 0;
154 net::AddressList addresses_
;
157 void OnDnsLookup(int resolve_result
);
159 // Weak pointer to the resource context.
160 content::ResourceContext
* resource_context_
;
163 class SocketCreateFunction
: public SocketAsyncApiFunction
{
165 DECLARE_EXTENSION_FUNCTION("socket.create", SOCKET_CREATE
)
167 SocketCreateFunction();
170 ~SocketCreateFunction() override
;
173 bool Prepare() override
;
174 void Work() override
;
177 FRIEND_TEST_ALL_PREFIXES(SocketUnitTest
, Create
);
178 enum SocketType
{ kSocketTypeInvalid
= -1, kSocketTypeTCP
, kSocketTypeUDP
};
180 scoped_ptr
<api::socket::Create::Params
> params_
;
181 SocketType socket_type_
;
184 class SocketDestroyFunction
: public SocketAsyncApiFunction
{
186 DECLARE_EXTENSION_FUNCTION("socket.destroy", SOCKET_DESTROY
)
189 ~SocketDestroyFunction() override
{}
192 bool Prepare() override
;
193 void Work() override
;
199 class SocketConnectFunction
: public SocketExtensionWithDnsLookupFunction
{
201 DECLARE_EXTENSION_FUNCTION("socket.connect", SOCKET_CONNECT
)
203 SocketConnectFunction();
206 ~SocketConnectFunction() override
;
209 bool Prepare() override
;
210 void AsyncWorkStart() override
;
212 // SocketExtensionWithDnsLookupFunction:
213 void AfterDnsLookup(int lookup_result
) override
;
217 void OnConnect(int result
);
220 std::string hostname_
;
224 class SocketDisconnectFunction
: public SocketAsyncApiFunction
{
226 DECLARE_EXTENSION_FUNCTION("socket.disconnect", SOCKET_DISCONNECT
)
229 ~SocketDisconnectFunction() override
{}
232 bool Prepare() override
;
233 void Work() override
;
239 class SocketBindFunction
: public SocketAsyncApiFunction
{
241 DECLARE_EXTENSION_FUNCTION("socket.bind", SOCKET_BIND
)
244 ~SocketBindFunction() override
{}
247 bool Prepare() override
;
248 void AsyncWorkStart() override
;
252 std::string address_
;
256 class SocketListenFunction
: public SocketAsyncApiFunction
{
258 DECLARE_EXTENSION_FUNCTION("socket.listen", SOCKET_LISTEN
)
260 SocketListenFunction();
263 ~SocketListenFunction() override
;
266 bool Prepare() override
;
267 void AsyncWorkStart() override
;
270 scoped_ptr
<api::socket::Listen::Params
> params_
;
273 class SocketAcceptFunction
: public SocketAsyncApiFunction
{
275 DECLARE_EXTENSION_FUNCTION("socket.accept", SOCKET_ACCEPT
)
277 SocketAcceptFunction();
280 ~SocketAcceptFunction() override
;
283 bool Prepare() override
;
284 void AsyncWorkStart() override
;
287 void OnAccept(int result_code
, net::TCPClientSocket
* socket
);
289 scoped_ptr
<api::socket::Accept::Params
> params_
;
292 class SocketReadFunction
: public SocketAsyncApiFunction
{
294 DECLARE_EXTENSION_FUNCTION("socket.read", SOCKET_READ
)
296 SocketReadFunction();
299 ~SocketReadFunction() override
;
302 bool Prepare() override
;
303 void AsyncWorkStart() override
;
304 void OnCompleted(int result
, scoped_refptr
<net::IOBuffer
> io_buffer
);
307 scoped_ptr
<api::socket::Read::Params
> params_
;
310 class SocketWriteFunction
: public SocketAsyncApiFunction
{
312 DECLARE_EXTENSION_FUNCTION("socket.write", SOCKET_WRITE
)
314 SocketWriteFunction();
317 ~SocketWriteFunction() override
;
320 bool Prepare() override
;
321 void AsyncWorkStart() override
;
322 void OnCompleted(int result
);
326 scoped_refptr
<net::IOBuffer
> io_buffer_
;
327 size_t io_buffer_size_
;
330 class SocketRecvFromFunction
: public SocketAsyncApiFunction
{
332 DECLARE_EXTENSION_FUNCTION("socket.recvFrom", SOCKET_RECVFROM
)
334 SocketRecvFromFunction();
337 ~SocketRecvFromFunction() override
;
340 bool Prepare() override
;
341 void AsyncWorkStart() override
;
342 void OnCompleted(int result
,
343 scoped_refptr
<net::IOBuffer
> io_buffer
,
344 const std::string
& address
,
348 scoped_ptr
<api::socket::RecvFrom::Params
> params_
;
351 class SocketSendToFunction
: public SocketExtensionWithDnsLookupFunction
{
353 DECLARE_EXTENSION_FUNCTION("socket.sendTo", SOCKET_SENDTO
)
355 SocketSendToFunction();
358 ~SocketSendToFunction() override
;
361 bool Prepare() override
;
362 void AsyncWorkStart() override
;
363 void OnCompleted(int result
);
365 // SocketExtensionWithDnsLookupFunction:
366 void AfterDnsLookup(int lookup_result
) override
;
372 scoped_refptr
<net::IOBuffer
> io_buffer_
;
373 size_t io_buffer_size_
;
374 std::string hostname_
;
378 class SocketSetKeepAliveFunction
: public SocketAsyncApiFunction
{
380 DECLARE_EXTENSION_FUNCTION("socket.setKeepAlive", SOCKET_SETKEEPALIVE
)
382 SocketSetKeepAliveFunction();
385 ~SocketSetKeepAliveFunction() override
;
388 bool Prepare() override
;
389 void Work() override
;
392 scoped_ptr
<api::socket::SetKeepAlive::Params
> params_
;
395 class SocketSetNoDelayFunction
: public SocketAsyncApiFunction
{
397 DECLARE_EXTENSION_FUNCTION("socket.setNoDelay", SOCKET_SETNODELAY
)
399 SocketSetNoDelayFunction();
402 ~SocketSetNoDelayFunction() override
;
405 bool Prepare() override
;
406 void Work() override
;
409 scoped_ptr
<api::socket::SetNoDelay::Params
> params_
;
412 class SocketGetInfoFunction
: public SocketAsyncApiFunction
{
414 DECLARE_EXTENSION_FUNCTION("socket.getInfo", SOCKET_GETINFO
)
416 SocketGetInfoFunction();
419 ~SocketGetInfoFunction() override
;
422 bool Prepare() override
;
423 void Work() override
;
426 scoped_ptr
<api::socket::GetInfo::Params
> params_
;
429 class SocketGetNetworkListFunction
: public AsyncExtensionFunction
{
431 DECLARE_EXTENSION_FUNCTION("socket.getNetworkList", SOCKET_GETNETWORKLIST
)
434 ~SocketGetNetworkListFunction() override
{}
435 bool RunAsync() override
;
438 void GetNetworkListOnFileThread();
439 void HandleGetNetworkListError();
440 void SendResponseOnUIThread(const net::NetworkInterfaceList
& interface_list
);
443 class SocketJoinGroupFunction
: public SocketAsyncApiFunction
{
445 DECLARE_EXTENSION_FUNCTION("socket.joinGroup", SOCKET_MULTICAST_JOIN_GROUP
)
447 SocketJoinGroupFunction();
450 ~SocketJoinGroupFunction() override
;
453 bool Prepare() override
;
454 void Work() override
;
457 scoped_ptr
<api::socket::JoinGroup::Params
> params_
;
460 class SocketLeaveGroupFunction
: public SocketAsyncApiFunction
{
462 DECLARE_EXTENSION_FUNCTION("socket.leaveGroup", SOCKET_MULTICAST_LEAVE_GROUP
)
464 SocketLeaveGroupFunction();
467 ~SocketLeaveGroupFunction() override
;
470 bool Prepare() override
;
471 void Work() override
;
474 scoped_ptr
<api::socket::LeaveGroup::Params
> params_
;
477 class SocketSetMulticastTimeToLiveFunction
: public SocketAsyncApiFunction
{
479 DECLARE_EXTENSION_FUNCTION("socket.setMulticastTimeToLive",
480 SOCKET_MULTICAST_SET_TIME_TO_LIVE
)
482 SocketSetMulticastTimeToLiveFunction();
485 ~SocketSetMulticastTimeToLiveFunction() override
;
488 bool Prepare() override
;
489 void Work() override
;
492 scoped_ptr
<api::socket::SetMulticastTimeToLive::Params
> params_
;
495 class SocketSetMulticastLoopbackModeFunction
: public SocketAsyncApiFunction
{
497 DECLARE_EXTENSION_FUNCTION("socket.setMulticastLoopbackMode",
498 SOCKET_MULTICAST_SET_LOOPBACK_MODE
)
500 SocketSetMulticastLoopbackModeFunction();
503 ~SocketSetMulticastLoopbackModeFunction() override
;
506 bool Prepare() override
;
507 void Work() override
;
510 scoped_ptr
<api::socket::SetMulticastLoopbackMode::Params
> params_
;
513 class SocketGetJoinedGroupsFunction
: public SocketAsyncApiFunction
{
515 DECLARE_EXTENSION_FUNCTION("socket.getJoinedGroups",
516 SOCKET_MULTICAST_GET_JOINED_GROUPS
)
518 SocketGetJoinedGroupsFunction();
521 ~SocketGetJoinedGroupsFunction() override
;
524 bool Prepare() override
;
525 void Work() override
;
528 scoped_ptr
<api::socket::GetJoinedGroups::Params
> params_
;
531 class SocketSecureFunction
: public SocketAsyncApiFunction
{
533 DECLARE_EXTENSION_FUNCTION("socket.secure", SOCKET_SECURE
);
534 SocketSecureFunction();
537 ~SocketSecureFunction() override
;
540 bool Prepare() override
;
541 void AsyncWorkStart() override
;
544 // Callback from TLSSocket::UpgradeSocketToTLS().
545 void TlsConnectDone(scoped_ptr
<TLSSocket
> socket
, int result
);
547 scoped_ptr
<api::socket::Secure::Params
> params_
;
548 scoped_refptr
<net::URLRequestContextGetter
> url_request_getter_
;
550 DISALLOW_COPY_AND_ASSIGN(SocketSecureFunction
);
553 } // namespace extensions
555 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_