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 std::string
& hostname
);
151 virtual void AfterDnsLookup(int lookup_result
) = 0;
153 std::string resolved_address_
;
156 void OnDnsLookup(int resolve_result
);
158 // Weak pointer to the resource context.
159 content::ResourceContext
* resource_context_
;
161 scoped_ptr
<net::HostResolver::RequestHandle
> request_handle_
;
162 scoped_ptr
<net::AddressList
> addresses_
;
165 class SocketCreateFunction
: public SocketAsyncApiFunction
{
167 DECLARE_EXTENSION_FUNCTION("socket.create", SOCKET_CREATE
)
169 SocketCreateFunction();
172 ~SocketCreateFunction() override
;
175 bool Prepare() override
;
176 void Work() override
;
179 FRIEND_TEST_ALL_PREFIXES(SocketUnitTest
, Create
);
180 enum SocketType
{ kSocketTypeInvalid
= -1, kSocketTypeTCP
, kSocketTypeUDP
};
182 scoped_ptr
<core_api::socket::Create::Params
> params_
;
183 SocketType socket_type_
;
186 class SocketDestroyFunction
: public SocketAsyncApiFunction
{
188 DECLARE_EXTENSION_FUNCTION("socket.destroy", SOCKET_DESTROY
)
191 ~SocketDestroyFunction() override
{}
194 bool Prepare() override
;
195 void Work() override
;
201 class SocketConnectFunction
: public SocketExtensionWithDnsLookupFunction
{
203 DECLARE_EXTENSION_FUNCTION("socket.connect", SOCKET_CONNECT
)
205 SocketConnectFunction();
208 ~SocketConnectFunction() override
;
211 bool Prepare() override
;
212 void AsyncWorkStart() override
;
214 // SocketExtensionWithDnsLookupFunction:
215 void AfterDnsLookup(int lookup_result
) override
;
219 void OnConnect(int result
);
222 std::string hostname_
;
226 class SocketDisconnectFunction
: public SocketAsyncApiFunction
{
228 DECLARE_EXTENSION_FUNCTION("socket.disconnect", SOCKET_DISCONNECT
)
231 ~SocketDisconnectFunction() override
{}
234 bool Prepare() override
;
235 void Work() override
;
241 class SocketBindFunction
: public SocketAsyncApiFunction
{
243 DECLARE_EXTENSION_FUNCTION("socket.bind", SOCKET_BIND
)
246 ~SocketBindFunction() override
{}
249 bool Prepare() override
;
250 void AsyncWorkStart() override
;
254 std::string address_
;
258 class SocketListenFunction
: public SocketAsyncApiFunction
{
260 DECLARE_EXTENSION_FUNCTION("socket.listen", SOCKET_LISTEN
)
262 SocketListenFunction();
265 ~SocketListenFunction() override
;
268 bool Prepare() override
;
269 void AsyncWorkStart() override
;
272 scoped_ptr
<core_api::socket::Listen::Params
> params_
;
275 class SocketAcceptFunction
: public SocketAsyncApiFunction
{
277 DECLARE_EXTENSION_FUNCTION("socket.accept", SOCKET_ACCEPT
)
279 SocketAcceptFunction();
282 ~SocketAcceptFunction() override
;
285 bool Prepare() override
;
286 void AsyncWorkStart() override
;
289 void OnAccept(int result_code
, net::TCPClientSocket
* socket
);
291 scoped_ptr
<core_api::socket::Accept::Params
> params_
;
294 class SocketReadFunction
: public SocketAsyncApiFunction
{
296 DECLARE_EXTENSION_FUNCTION("socket.read", SOCKET_READ
)
298 SocketReadFunction();
301 ~SocketReadFunction() override
;
304 bool Prepare() override
;
305 void AsyncWorkStart() override
;
306 void OnCompleted(int result
, scoped_refptr
<net::IOBuffer
> io_buffer
);
309 scoped_ptr
<core_api::socket::Read::Params
> params_
;
312 class SocketWriteFunction
: public SocketAsyncApiFunction
{
314 DECLARE_EXTENSION_FUNCTION("socket.write", SOCKET_WRITE
)
316 SocketWriteFunction();
319 ~SocketWriteFunction() override
;
322 bool Prepare() override
;
323 void AsyncWorkStart() override
;
324 void OnCompleted(int result
);
328 scoped_refptr
<net::IOBuffer
> io_buffer_
;
329 size_t io_buffer_size_
;
332 class SocketRecvFromFunction
: public SocketAsyncApiFunction
{
334 DECLARE_EXTENSION_FUNCTION("socket.recvFrom", SOCKET_RECVFROM
)
336 SocketRecvFromFunction();
339 ~SocketRecvFromFunction() override
;
342 bool Prepare() override
;
343 void AsyncWorkStart() override
;
344 void OnCompleted(int result
,
345 scoped_refptr
<net::IOBuffer
> io_buffer
,
346 const std::string
& address
,
350 scoped_ptr
<core_api::socket::RecvFrom::Params
> params_
;
353 class SocketSendToFunction
: public SocketExtensionWithDnsLookupFunction
{
355 DECLARE_EXTENSION_FUNCTION("socket.sendTo", SOCKET_SENDTO
)
357 SocketSendToFunction();
360 ~SocketSendToFunction() override
;
363 bool Prepare() override
;
364 void AsyncWorkStart() override
;
365 void OnCompleted(int result
);
367 // SocketExtensionWithDnsLookupFunction:
368 void AfterDnsLookup(int lookup_result
) override
;
374 scoped_refptr
<net::IOBuffer
> io_buffer_
;
375 size_t io_buffer_size_
;
376 std::string hostname_
;
380 class SocketSetKeepAliveFunction
: public SocketAsyncApiFunction
{
382 DECLARE_EXTENSION_FUNCTION("socket.setKeepAlive", SOCKET_SETKEEPALIVE
)
384 SocketSetKeepAliveFunction();
387 ~SocketSetKeepAliveFunction() override
;
390 bool Prepare() override
;
391 void Work() override
;
394 scoped_ptr
<core_api::socket::SetKeepAlive::Params
> params_
;
397 class SocketSetNoDelayFunction
: public SocketAsyncApiFunction
{
399 DECLARE_EXTENSION_FUNCTION("socket.setNoDelay", SOCKET_SETNODELAY
)
401 SocketSetNoDelayFunction();
404 ~SocketSetNoDelayFunction() override
;
407 bool Prepare() override
;
408 void Work() override
;
411 scoped_ptr
<core_api::socket::SetNoDelay::Params
> params_
;
414 class SocketGetInfoFunction
: public SocketAsyncApiFunction
{
416 DECLARE_EXTENSION_FUNCTION("socket.getInfo", SOCKET_GETINFO
)
418 SocketGetInfoFunction();
421 ~SocketGetInfoFunction() override
;
424 bool Prepare() override
;
425 void Work() override
;
428 scoped_ptr
<core_api::socket::GetInfo::Params
> params_
;
431 class SocketGetNetworkListFunction
: public AsyncExtensionFunction
{
433 DECLARE_EXTENSION_FUNCTION("socket.getNetworkList", SOCKET_GETNETWORKLIST
)
436 ~SocketGetNetworkListFunction() override
{}
437 bool RunAsync() override
;
440 void GetNetworkListOnFileThread();
441 void HandleGetNetworkListError();
442 void SendResponseOnUIThread(const net::NetworkInterfaceList
& interface_list
);
445 class SocketJoinGroupFunction
: public SocketAsyncApiFunction
{
447 DECLARE_EXTENSION_FUNCTION("socket.joinGroup", SOCKET_MULTICAST_JOIN_GROUP
)
449 SocketJoinGroupFunction();
452 ~SocketJoinGroupFunction() override
;
455 bool Prepare() override
;
456 void Work() override
;
459 scoped_ptr
<core_api::socket::JoinGroup::Params
> params_
;
462 class SocketLeaveGroupFunction
: public SocketAsyncApiFunction
{
464 DECLARE_EXTENSION_FUNCTION("socket.leaveGroup", SOCKET_MULTICAST_LEAVE_GROUP
)
466 SocketLeaveGroupFunction();
469 ~SocketLeaveGroupFunction() override
;
472 bool Prepare() override
;
473 void Work() override
;
476 scoped_ptr
<core_api::socket::LeaveGroup::Params
> params_
;
479 class SocketSetMulticastTimeToLiveFunction
: public SocketAsyncApiFunction
{
481 DECLARE_EXTENSION_FUNCTION("socket.setMulticastTimeToLive",
482 SOCKET_MULTICAST_SET_TIME_TO_LIVE
)
484 SocketSetMulticastTimeToLiveFunction();
487 ~SocketSetMulticastTimeToLiveFunction() override
;
490 bool Prepare() override
;
491 void Work() override
;
494 scoped_ptr
<core_api::socket::SetMulticastTimeToLive::Params
> params_
;
497 class SocketSetMulticastLoopbackModeFunction
: public SocketAsyncApiFunction
{
499 DECLARE_EXTENSION_FUNCTION("socket.setMulticastLoopbackMode",
500 SOCKET_MULTICAST_SET_LOOPBACK_MODE
)
502 SocketSetMulticastLoopbackModeFunction();
505 ~SocketSetMulticastLoopbackModeFunction() override
;
508 bool Prepare() override
;
509 void Work() override
;
512 scoped_ptr
<core_api::socket::SetMulticastLoopbackMode::Params
> params_
;
515 class SocketGetJoinedGroupsFunction
: public SocketAsyncApiFunction
{
517 DECLARE_EXTENSION_FUNCTION("socket.getJoinedGroups",
518 SOCKET_MULTICAST_GET_JOINED_GROUPS
)
520 SocketGetJoinedGroupsFunction();
523 ~SocketGetJoinedGroupsFunction() override
;
526 bool Prepare() override
;
527 void Work() override
;
530 scoped_ptr
<core_api::socket::GetJoinedGroups::Params
> params_
;
533 class SocketSecureFunction
: public SocketAsyncApiFunction
{
535 DECLARE_EXTENSION_FUNCTION("socket.secure", SOCKET_SECURE
);
536 SocketSecureFunction();
539 ~SocketSecureFunction() override
;
542 bool Prepare() override
;
543 void AsyncWorkStart() override
;
546 // Callback from TLSSocket::UpgradeSocketToTLS().
547 void TlsConnectDone(scoped_ptr
<TLSSocket
> socket
, int result
);
549 scoped_ptr
<core_api::socket::Secure::Params
> params_
;
550 scoped_refptr
<net::URLRequestContextGetter
> url_request_getter_
;
552 DISALLOW_COPY_AND_ASSIGN(SocketSecureFunction
);
555 } // namespace extensions
557 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_