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"
22 class ResourceContext
;
27 class URLRequestContextGetter
;
28 class SSLClientSocket
;
31 namespace extensions
{
35 // A simple interface to ApiResourceManager<Socket> or derived class. The goal
36 // of this interface is to allow Socket API functions to use distinct instances
37 // of ApiResourceManager<> depending on the type of socket (old version in
38 // "socket" namespace vs new version in "socket.xxx" namespaces).
39 class SocketResourceManagerInterface
{
41 virtual ~SocketResourceManagerInterface() {}
43 virtual bool SetBrowserContext(content::BrowserContext
* context
) = 0;
44 virtual int Add(Socket
* socket
) = 0;
45 virtual Socket
* Get(const std::string
& extension_id
, int api_resource_id
) = 0;
46 virtual void Remove(const std::string
& extension_id
, int api_resource_id
) = 0;
47 virtual void Replace(const std::string
& extension_id
,
50 virtual base::hash_set
<int>* GetResourceIds(
51 const std::string
& extension_id
) = 0;
54 // Implementation of SocketResourceManagerInterface using an
55 // ApiResourceManager<T> instance (where T derives from Socket).
57 class SocketResourceManager
: public SocketResourceManagerInterface
{
59 SocketResourceManager() : manager_(NULL
) {}
61 virtual bool SetBrowserContext(content::BrowserContext
* context
) override
{
62 manager_
= ApiResourceManager
<T
>::Get(context
);
64 << "There is no socket manager. "
65 "If this assertion is failing during a test, then it is likely that "
66 "TestExtensionSystem is failing to provide an instance of "
67 "ApiResourceManager<Socket>.";
68 return manager_
!= NULL
;
71 virtual int Add(Socket
* socket
) override
{
72 // Note: Cast needed here, because "T" may be a subclass of "Socket".
73 return manager_
->Add(static_cast<T
*>(socket
));
76 virtual Socket
* Get(const std::string
& extension_id
,
77 int api_resource_id
) override
{
78 return manager_
->Get(extension_id
, api_resource_id
);
81 virtual void Replace(const std::string
& extension_id
,
83 Socket
* socket
) override
{
84 manager_
->Replace(extension_id
, api_resource_id
, static_cast<T
*>(socket
));
87 virtual void Remove(const std::string
& extension_id
,
88 int api_resource_id
) override
{
89 manager_
->Remove(extension_id
, api_resource_id
);
92 virtual base::hash_set
<int>* GetResourceIds(const std::string
& extension_id
)
94 return manager_
->GetResourceIds(extension_id
);
98 ApiResourceManager
<T
>* manager_
;
101 class SocketAsyncApiFunction
: public AsyncApiFunction
{
103 SocketAsyncApiFunction();
106 ~SocketAsyncApiFunction() override
;
109 bool PrePrepare() override
;
110 bool Respond() override
;
112 virtual scoped_ptr
<SocketResourceManagerInterface
>
113 CreateSocketResourceManager();
115 int AddSocket(Socket
* socket
);
116 Socket
* GetSocket(int api_resource_id
);
117 void ReplaceSocket(int api_resource_id
, Socket
* socket
);
118 void RemoveSocket(int api_resource_id
);
119 base::hash_set
<int>* GetSocketIds();
122 scoped_ptr
<SocketResourceManagerInterface
> manager_
;
125 class SocketExtensionWithDnsLookupFunction
: public SocketAsyncApiFunction
{
127 SocketExtensionWithDnsLookupFunction();
128 ~SocketExtensionWithDnsLookupFunction() override
;
131 bool PrePrepare() override
;
133 void StartDnsLookup(const std::string
& hostname
);
134 virtual void AfterDnsLookup(int lookup_result
) = 0;
136 std::string resolved_address_
;
139 void OnDnsLookup(int resolve_result
);
141 // Weak pointer to the resource context.
142 content::ResourceContext
* resource_context_
;
144 scoped_ptr
<net::HostResolver::RequestHandle
> request_handle_
;
145 scoped_ptr
<net::AddressList
> addresses_
;
148 class SocketCreateFunction
: public SocketAsyncApiFunction
{
150 DECLARE_EXTENSION_FUNCTION("socket.create", SOCKET_CREATE
)
152 SocketCreateFunction();
155 ~SocketCreateFunction() override
;
158 bool Prepare() override
;
159 void Work() override
;
162 FRIEND_TEST_ALL_PREFIXES(SocketUnitTest
, Create
);
163 enum SocketType
{ kSocketTypeInvalid
= -1, kSocketTypeTCP
, kSocketTypeUDP
};
165 scoped_ptr
<core_api::socket::Create::Params
> params_
;
166 SocketType socket_type_
;
169 class SocketDestroyFunction
: public SocketAsyncApiFunction
{
171 DECLARE_EXTENSION_FUNCTION("socket.destroy", SOCKET_DESTROY
)
174 ~SocketDestroyFunction() override
{}
177 bool Prepare() override
;
178 void Work() override
;
184 class SocketConnectFunction
: public SocketExtensionWithDnsLookupFunction
{
186 DECLARE_EXTENSION_FUNCTION("socket.connect", SOCKET_CONNECT
)
188 SocketConnectFunction();
191 ~SocketConnectFunction() override
;
194 bool Prepare() override
;
195 void AsyncWorkStart() override
;
197 // SocketExtensionWithDnsLookupFunction:
198 void AfterDnsLookup(int lookup_result
) override
;
202 void OnConnect(int result
);
205 std::string hostname_
;
209 class SocketDisconnectFunction
: public SocketAsyncApiFunction
{
211 DECLARE_EXTENSION_FUNCTION("socket.disconnect", SOCKET_DISCONNECT
)
214 ~SocketDisconnectFunction() override
{}
217 bool Prepare() override
;
218 void Work() override
;
224 class SocketBindFunction
: public SocketAsyncApiFunction
{
226 DECLARE_EXTENSION_FUNCTION("socket.bind", SOCKET_BIND
)
229 ~SocketBindFunction() override
{}
232 bool Prepare() override
;
233 void Work() override
;
237 std::string address_
;
241 class SocketListenFunction
: public SocketAsyncApiFunction
{
243 DECLARE_EXTENSION_FUNCTION("socket.listen", SOCKET_LISTEN
)
245 SocketListenFunction();
248 ~SocketListenFunction() override
;
251 bool Prepare() override
;
252 void Work() override
;
255 scoped_ptr
<core_api::socket::Listen::Params
> params_
;
258 class SocketAcceptFunction
: public SocketAsyncApiFunction
{
260 DECLARE_EXTENSION_FUNCTION("socket.accept", SOCKET_ACCEPT
)
262 SocketAcceptFunction();
265 ~SocketAcceptFunction() override
;
268 bool Prepare() override
;
269 void AsyncWorkStart() override
;
272 void OnAccept(int result_code
, net::TCPClientSocket
* socket
);
274 scoped_ptr
<core_api::socket::Accept::Params
> params_
;
277 class SocketReadFunction
: public SocketAsyncApiFunction
{
279 DECLARE_EXTENSION_FUNCTION("socket.read", SOCKET_READ
)
281 SocketReadFunction();
284 ~SocketReadFunction() override
;
287 bool Prepare() override
;
288 void AsyncWorkStart() override
;
289 void OnCompleted(int result
, scoped_refptr
<net::IOBuffer
> io_buffer
);
292 scoped_ptr
<core_api::socket::Read::Params
> params_
;
295 class SocketWriteFunction
: public SocketAsyncApiFunction
{
297 DECLARE_EXTENSION_FUNCTION("socket.write", SOCKET_WRITE
)
299 SocketWriteFunction();
302 ~SocketWriteFunction() override
;
305 bool Prepare() override
;
306 void AsyncWorkStart() override
;
307 void OnCompleted(int result
);
311 scoped_refptr
<net::IOBuffer
> io_buffer_
;
312 size_t io_buffer_size_
;
315 class SocketRecvFromFunction
: public SocketAsyncApiFunction
{
317 DECLARE_EXTENSION_FUNCTION("socket.recvFrom", SOCKET_RECVFROM
)
319 SocketRecvFromFunction();
322 ~SocketRecvFromFunction() override
;
325 bool Prepare() override
;
326 void AsyncWorkStart() override
;
327 void OnCompleted(int result
,
328 scoped_refptr
<net::IOBuffer
> io_buffer
,
329 const std::string
& address
,
333 scoped_ptr
<core_api::socket::RecvFrom::Params
> params_
;
336 class SocketSendToFunction
: public SocketExtensionWithDnsLookupFunction
{
338 DECLARE_EXTENSION_FUNCTION("socket.sendTo", SOCKET_SENDTO
)
340 SocketSendToFunction();
343 ~SocketSendToFunction() override
;
346 bool Prepare() override
;
347 void AsyncWorkStart() override
;
348 void OnCompleted(int result
);
350 // SocketExtensionWithDnsLookupFunction:
351 void AfterDnsLookup(int lookup_result
) override
;
357 scoped_refptr
<net::IOBuffer
> io_buffer_
;
358 size_t io_buffer_size_
;
359 std::string hostname_
;
363 class SocketSetKeepAliveFunction
: public SocketAsyncApiFunction
{
365 DECLARE_EXTENSION_FUNCTION("socket.setKeepAlive", SOCKET_SETKEEPALIVE
)
367 SocketSetKeepAliveFunction();
370 ~SocketSetKeepAliveFunction() override
;
373 bool Prepare() override
;
374 void Work() override
;
377 scoped_ptr
<core_api::socket::SetKeepAlive::Params
> params_
;
380 class SocketSetNoDelayFunction
: public SocketAsyncApiFunction
{
382 DECLARE_EXTENSION_FUNCTION("socket.setNoDelay", SOCKET_SETNODELAY
)
384 SocketSetNoDelayFunction();
387 ~SocketSetNoDelayFunction() override
;
390 bool Prepare() override
;
391 void Work() override
;
394 scoped_ptr
<core_api::socket::SetNoDelay::Params
> params_
;
397 class SocketGetInfoFunction
: public SocketAsyncApiFunction
{
399 DECLARE_EXTENSION_FUNCTION("socket.getInfo", SOCKET_GETINFO
)
401 SocketGetInfoFunction();
404 ~SocketGetInfoFunction() override
;
407 bool Prepare() override
;
408 void Work() override
;
411 scoped_ptr
<core_api::socket::GetInfo::Params
> params_
;
414 class SocketGetNetworkListFunction
: public AsyncExtensionFunction
{
416 DECLARE_EXTENSION_FUNCTION("socket.getNetworkList", SOCKET_GETNETWORKLIST
)
419 ~SocketGetNetworkListFunction() override
{}
420 bool RunAsync() override
;
423 void GetNetworkListOnFileThread();
424 void HandleGetNetworkListError();
425 void SendResponseOnUIThread(const net::NetworkInterfaceList
& interface_list
);
428 class SocketJoinGroupFunction
: public SocketAsyncApiFunction
{
430 DECLARE_EXTENSION_FUNCTION("socket.joinGroup", SOCKET_MULTICAST_JOIN_GROUP
)
432 SocketJoinGroupFunction();
435 ~SocketJoinGroupFunction() override
;
438 bool Prepare() override
;
439 void Work() override
;
442 scoped_ptr
<core_api::socket::JoinGroup::Params
> params_
;
445 class SocketLeaveGroupFunction
: public SocketAsyncApiFunction
{
447 DECLARE_EXTENSION_FUNCTION("socket.leaveGroup", SOCKET_MULTICAST_LEAVE_GROUP
)
449 SocketLeaveGroupFunction();
452 ~SocketLeaveGroupFunction() override
;
455 bool Prepare() override
;
456 void Work() override
;
459 scoped_ptr
<core_api::socket::LeaveGroup::Params
> params_
;
462 class SocketSetMulticastTimeToLiveFunction
: public SocketAsyncApiFunction
{
464 DECLARE_EXTENSION_FUNCTION("socket.setMulticastTimeToLive",
465 SOCKET_MULTICAST_SET_TIME_TO_LIVE
)
467 SocketSetMulticastTimeToLiveFunction();
470 ~SocketSetMulticastTimeToLiveFunction() override
;
473 bool Prepare() override
;
474 void Work() override
;
477 scoped_ptr
<core_api::socket::SetMulticastTimeToLive::Params
> params_
;
480 class SocketSetMulticastLoopbackModeFunction
: public SocketAsyncApiFunction
{
482 DECLARE_EXTENSION_FUNCTION("socket.setMulticastLoopbackMode",
483 SOCKET_MULTICAST_SET_LOOPBACK_MODE
)
485 SocketSetMulticastLoopbackModeFunction();
488 ~SocketSetMulticastLoopbackModeFunction() override
;
491 bool Prepare() override
;
492 void Work() override
;
495 scoped_ptr
<core_api::socket::SetMulticastLoopbackMode::Params
> params_
;
498 class SocketGetJoinedGroupsFunction
: public SocketAsyncApiFunction
{
500 DECLARE_EXTENSION_FUNCTION("socket.getJoinedGroups",
501 SOCKET_MULTICAST_GET_JOINED_GROUPS
)
503 SocketGetJoinedGroupsFunction();
506 ~SocketGetJoinedGroupsFunction() override
;
509 bool Prepare() override
;
510 void Work() override
;
513 scoped_ptr
<core_api::socket::GetJoinedGroups::Params
> params_
;
516 class SocketSecureFunction
: public SocketAsyncApiFunction
{
518 DECLARE_EXTENSION_FUNCTION("socket.secure", SOCKET_SECURE
);
519 SocketSecureFunction();
522 ~SocketSecureFunction() override
;
525 bool Prepare() override
;
526 void AsyncWorkStart() override
;
529 // Callback from TLSSocket::UpgradeSocketToTLS().
530 void TlsConnectDone(scoped_ptr
<TLSSocket
> socket
, int result
);
532 scoped_ptr
<core_api::socket::Secure::Params
> params_
;
533 scoped_refptr
<net::URLRequestContextGetter
> url_request_getter_
;
535 DISALLOW_COPY_AND_ASSIGN(SocketSecureFunction
);
538 } // namespace extensions
540 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_