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 virtual ~SocketAsyncApiFunction();
109 virtual bool PrePrepare() OVERRIDE;
110 virtual 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 virtual ~SocketExtensionWithDnsLookupFunction();
131 virtual 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 virtual ~SocketCreateFunction();
158 virtual bool Prepare() OVERRIDE;
159 virtual 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 virtual ~SocketDestroyFunction() {}
177 virtual bool Prepare() OVERRIDE;
178 virtual void Work() OVERRIDE;
184 class SocketConnectFunction : public SocketExtensionWithDnsLookupFunction {
186 DECLARE_EXTENSION_FUNCTION("socket.connect", SOCKET_CONNECT)
188 SocketConnectFunction();
191 virtual ~SocketConnectFunction();
194 virtual bool Prepare() OVERRIDE;
195 virtual void AsyncWorkStart() OVERRIDE;
197 // SocketExtensionWithDnsLookupFunction:
198 virtual void AfterDnsLookup(int lookup_result) OVERRIDE;
202 void OnConnect(int result);
205 std::string hostname_;
210 class SocketDisconnectFunction : public SocketAsyncApiFunction {
212 DECLARE_EXTENSION_FUNCTION("socket.disconnect", SOCKET_DISCONNECT)
215 virtual ~SocketDisconnectFunction() {}
218 virtual bool Prepare() OVERRIDE;
219 virtual void Work() OVERRIDE;
225 class SocketBindFunction : public SocketAsyncApiFunction {
227 DECLARE_EXTENSION_FUNCTION("socket.bind", SOCKET_BIND)
230 virtual ~SocketBindFunction() {}
233 virtual bool Prepare() OVERRIDE;
234 virtual void Work() OVERRIDE;
238 std::string address_;
242 class SocketListenFunction : public SocketAsyncApiFunction {
244 DECLARE_EXTENSION_FUNCTION("socket.listen", SOCKET_LISTEN)
246 SocketListenFunction();
249 virtual ~SocketListenFunction();
252 virtual bool Prepare() OVERRIDE;
253 virtual void Work() OVERRIDE;
256 scoped_ptr<core_api::socket::Listen::Params> params_;
259 class SocketAcceptFunction : public SocketAsyncApiFunction {
261 DECLARE_EXTENSION_FUNCTION("socket.accept", SOCKET_ACCEPT)
263 SocketAcceptFunction();
266 virtual ~SocketAcceptFunction();
269 virtual bool Prepare() OVERRIDE;
270 virtual void AsyncWorkStart() OVERRIDE;
273 void OnAccept(int result_code, net::TCPClientSocket* socket);
275 scoped_ptr<core_api::socket::Accept::Params> params_;
278 class SocketReadFunction : public SocketAsyncApiFunction {
280 DECLARE_EXTENSION_FUNCTION("socket.read", SOCKET_READ)
282 SocketReadFunction();
285 virtual ~SocketReadFunction();
288 virtual bool Prepare() OVERRIDE;
289 virtual void AsyncWorkStart() OVERRIDE;
290 void OnCompleted(int result, scoped_refptr<net::IOBuffer> io_buffer);
293 scoped_ptr<core_api::socket::Read::Params> params_;
296 class SocketWriteFunction : public SocketAsyncApiFunction {
298 DECLARE_EXTENSION_FUNCTION("socket.write", SOCKET_WRITE)
300 SocketWriteFunction();
303 virtual ~SocketWriteFunction();
306 virtual bool Prepare() OVERRIDE;
307 virtual void AsyncWorkStart() OVERRIDE;
308 void OnCompleted(int result);
312 scoped_refptr<net::IOBuffer> io_buffer_;
313 size_t io_buffer_size_;
316 class SocketRecvFromFunction : public SocketAsyncApiFunction {
318 DECLARE_EXTENSION_FUNCTION("socket.recvFrom", SOCKET_RECVFROM)
320 SocketRecvFromFunction();
323 virtual ~SocketRecvFromFunction();
326 virtual bool Prepare() OVERRIDE;
327 virtual void AsyncWorkStart() OVERRIDE;
328 void OnCompleted(int result,
329 scoped_refptr<net::IOBuffer> io_buffer,
330 const std::string& address,
334 scoped_ptr<core_api::socket::RecvFrom::Params> params_;
337 class SocketSendToFunction : public SocketExtensionWithDnsLookupFunction {
339 DECLARE_EXTENSION_FUNCTION("socket.sendTo", SOCKET_SENDTO)
341 SocketSendToFunction();
344 virtual ~SocketSendToFunction();
347 virtual bool Prepare() OVERRIDE;
348 virtual void AsyncWorkStart() OVERRIDE;
349 void OnCompleted(int result);
351 // SocketExtensionWithDnsLookupFunction:
352 virtual void AfterDnsLookup(int lookup_result) OVERRIDE;
358 scoped_refptr<net::IOBuffer> io_buffer_;
359 size_t io_buffer_size_;
360 std::string hostname_;
365 class SocketSetKeepAliveFunction : public SocketAsyncApiFunction {
367 DECLARE_EXTENSION_FUNCTION("socket.setKeepAlive", SOCKET_SETKEEPALIVE)
369 SocketSetKeepAliveFunction();
372 virtual ~SocketSetKeepAliveFunction();
375 virtual bool Prepare() OVERRIDE;
376 virtual void Work() OVERRIDE;
379 scoped_ptr<core_api::socket::SetKeepAlive::Params> params_;
382 class SocketSetNoDelayFunction : public SocketAsyncApiFunction {
384 DECLARE_EXTENSION_FUNCTION("socket.setNoDelay", SOCKET_SETNODELAY)
386 SocketSetNoDelayFunction();
389 virtual ~SocketSetNoDelayFunction();
392 virtual bool Prepare() OVERRIDE;
393 virtual void Work() OVERRIDE;
396 scoped_ptr<core_api::socket::SetNoDelay::Params> params_;
399 class SocketGetInfoFunction : public SocketAsyncApiFunction {
401 DECLARE_EXTENSION_FUNCTION("socket.getInfo", SOCKET_GETINFO)
403 SocketGetInfoFunction();
406 virtual ~SocketGetInfoFunction();
409 virtual bool Prepare() OVERRIDE;
410 virtual void Work() OVERRIDE;
413 scoped_ptr<core_api::socket::GetInfo::Params> params_;
416 class SocketGetNetworkListFunction : public AsyncExtensionFunction {
418 DECLARE_EXTENSION_FUNCTION("socket.getNetworkList", SOCKET_GETNETWORKLIST)
421 virtual ~SocketGetNetworkListFunction() {}
422 virtual bool RunAsync() OVERRIDE;
425 void GetNetworkListOnFileThread();
426 void HandleGetNetworkListError();
427 void SendResponseOnUIThread(const net::NetworkInterfaceList& interface_list);
430 class SocketJoinGroupFunction : public SocketAsyncApiFunction {
432 DECLARE_EXTENSION_FUNCTION("socket.joinGroup", SOCKET_MULTICAST_JOIN_GROUP)
434 SocketJoinGroupFunction();
437 virtual ~SocketJoinGroupFunction();
440 virtual bool Prepare() OVERRIDE;
441 virtual void Work() OVERRIDE;
444 scoped_ptr<core_api::socket::JoinGroup::Params> params_;
447 class SocketLeaveGroupFunction : public SocketAsyncApiFunction {
449 DECLARE_EXTENSION_FUNCTION("socket.leaveGroup", SOCKET_MULTICAST_LEAVE_GROUP)
451 SocketLeaveGroupFunction();
454 virtual ~SocketLeaveGroupFunction();
457 virtual bool Prepare() OVERRIDE;
458 virtual void Work() OVERRIDE;
461 scoped_ptr<core_api::socket::LeaveGroup::Params> params_;
464 class SocketSetMulticastTimeToLiveFunction : public SocketAsyncApiFunction {
466 DECLARE_EXTENSION_FUNCTION("socket.setMulticastTimeToLive",
467 SOCKET_MULTICAST_SET_TIME_TO_LIVE)
469 SocketSetMulticastTimeToLiveFunction();
472 virtual ~SocketSetMulticastTimeToLiveFunction();
475 virtual bool Prepare() OVERRIDE;
476 virtual void Work() OVERRIDE;
479 scoped_ptr<core_api::socket::SetMulticastTimeToLive::Params> params_;
482 class SocketSetMulticastLoopbackModeFunction : public SocketAsyncApiFunction {
484 DECLARE_EXTENSION_FUNCTION("socket.setMulticastLoopbackMode",
485 SOCKET_MULTICAST_SET_LOOPBACK_MODE)
487 SocketSetMulticastLoopbackModeFunction();
490 virtual ~SocketSetMulticastLoopbackModeFunction();
493 virtual bool Prepare() OVERRIDE;
494 virtual void Work() OVERRIDE;
497 scoped_ptr<core_api::socket::SetMulticastLoopbackMode::Params> params_;
500 class SocketGetJoinedGroupsFunction : public SocketAsyncApiFunction {
502 DECLARE_EXTENSION_FUNCTION("socket.getJoinedGroups",
503 SOCKET_MULTICAST_GET_JOINED_GROUPS)
505 SocketGetJoinedGroupsFunction();
508 virtual ~SocketGetJoinedGroupsFunction();
511 virtual bool Prepare() OVERRIDE;
512 virtual void Work() OVERRIDE;
515 scoped_ptr<core_api::socket::GetJoinedGroups::Params> params_;
518 class SocketSecureFunction : public SocketAsyncApiFunction {
520 DECLARE_EXTENSION_FUNCTION("socket.secure", SOCKET_SECURE);
521 SocketSecureFunction();
524 virtual ~SocketSecureFunction();
527 virtual bool Prepare() OVERRIDE;
528 virtual void AsyncWorkStart() OVERRIDE;
531 // Callback from TLSSocket::UpgradeSocketToTLS().
532 void TlsConnectDone(scoped_ptr<TLSSocket> socket, int result);
534 scoped_ptr<core_api::socket::Secure::Params> params_;
535 scoped_refptr<net::URLRequestContextGetter> url_request_getter_;
537 DISALLOW_COPY_AND_ASSIGN(SocketSecureFunction);
540 } // namespace extensions
542 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_