Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / api / socket / socket_api.h
blob9c26c258b49677d1f10369277c33e1e98c7a3415
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_
8 #include <string>
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"
23 #endif // OS_CHROMEOS
25 namespace content {
26 class BrowserContext;
27 class ResourceContext;
30 namespace net {
31 class IOBuffer;
32 class URLRequestContextGetter;
33 class SSLClientSocket;
36 namespace extensions {
37 class Socket;
38 class TLSSocket;
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 {
45 public:
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,
53 int api_resource_id,
54 Socket* socket) = 0;
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).
61 template <typename T>
62 class SocketResourceManager : public SocketResourceManagerInterface {
63 public:
64 SocketResourceManager() : manager_(NULL) {}
66 bool SetBrowserContext(content::BrowserContext* context) override {
67 manager_ = ApiResourceManager<T>::Get(context);
68 DCHECK(manager_)
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,
86 int api_resource_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);
100 private:
101 ApiResourceManager<T>* manager_;
104 class SocketAsyncApiFunction : public AsyncApiFunction {
105 public:
106 SocketAsyncApiFunction();
108 protected:
109 ~SocketAsyncApiFunction() override;
111 // AsyncApiFunction:
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,
126 int socket_id,
127 Socket* socket);
129 private:
130 #if defined(OS_CHROMEOS)
131 void OpenFirewallHoleOnUIThread(AppFirewallHole::PortType type,
132 uint16_t port,
133 int socket_id);
134 void OnFirewallHoleOpened(
135 int socket_id,
136 scoped_ptr<AppFirewallHole, content::BrowserThread::DeleteOnUIThread>
137 hole);
138 #endif // OS_CHROMEOS
140 scoped_ptr<SocketResourceManagerInterface> manager_;
143 class SocketExtensionWithDnsLookupFunction : public SocketAsyncApiFunction {
144 protected:
145 SocketExtensionWithDnsLookupFunction();
146 ~SocketExtensionWithDnsLookupFunction() override;
148 // AsyncApiFunction:
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_;
156 private:
157 void OnDnsLookup(int resolve_result);
159 // Weak pointer to the resource context.
160 content::ResourceContext* resource_context_;
163 class SocketCreateFunction : public SocketAsyncApiFunction {
164 public:
165 DECLARE_EXTENSION_FUNCTION("socket.create", SOCKET_CREATE)
167 SocketCreateFunction();
169 protected:
170 ~SocketCreateFunction() override;
172 // AsyncApiFunction:
173 bool Prepare() override;
174 void Work() override;
176 private:
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 {
185 public:
186 DECLARE_EXTENSION_FUNCTION("socket.destroy", SOCKET_DESTROY)
188 protected:
189 ~SocketDestroyFunction() override {}
191 // AsyncApiFunction:
192 bool Prepare() override;
193 void Work() override;
195 private:
196 int socket_id_;
199 class SocketConnectFunction : public SocketExtensionWithDnsLookupFunction {
200 public:
201 DECLARE_EXTENSION_FUNCTION("socket.connect", SOCKET_CONNECT)
203 SocketConnectFunction();
205 protected:
206 ~SocketConnectFunction() override;
208 // AsyncApiFunction:
209 bool Prepare() override;
210 void AsyncWorkStart() override;
212 // SocketExtensionWithDnsLookupFunction:
213 void AfterDnsLookup(int lookup_result) override;
215 private:
216 void StartConnect();
217 void OnConnect(int result);
219 int socket_id_;
220 std::string hostname_;
221 uint16 port_;
224 class SocketDisconnectFunction : public SocketAsyncApiFunction {
225 public:
226 DECLARE_EXTENSION_FUNCTION("socket.disconnect", SOCKET_DISCONNECT)
228 protected:
229 ~SocketDisconnectFunction() override {}
231 // AsyncApiFunction:
232 bool Prepare() override;
233 void Work() override;
235 private:
236 int socket_id_;
239 class SocketBindFunction : public SocketAsyncApiFunction {
240 public:
241 DECLARE_EXTENSION_FUNCTION("socket.bind", SOCKET_BIND)
243 protected:
244 ~SocketBindFunction() override {}
246 // AsyncApiFunction:
247 bool Prepare() override;
248 void AsyncWorkStart() override;
250 private:
251 int socket_id_;
252 std::string address_;
253 uint16 port_;
256 class SocketListenFunction : public SocketAsyncApiFunction {
257 public:
258 DECLARE_EXTENSION_FUNCTION("socket.listen", SOCKET_LISTEN)
260 SocketListenFunction();
262 protected:
263 ~SocketListenFunction() override;
265 // AsyncApiFunction:
266 bool Prepare() override;
267 void AsyncWorkStart() override;
269 private:
270 scoped_ptr<api::socket::Listen::Params> params_;
273 class SocketAcceptFunction : public SocketAsyncApiFunction {
274 public:
275 DECLARE_EXTENSION_FUNCTION("socket.accept", SOCKET_ACCEPT)
277 SocketAcceptFunction();
279 protected:
280 ~SocketAcceptFunction() override;
282 // AsyncApiFunction:
283 bool Prepare() override;
284 void AsyncWorkStart() override;
286 private:
287 void OnAccept(int result_code, net::TCPClientSocket* socket);
289 scoped_ptr<api::socket::Accept::Params> params_;
292 class SocketReadFunction : public SocketAsyncApiFunction {
293 public:
294 DECLARE_EXTENSION_FUNCTION("socket.read", SOCKET_READ)
296 SocketReadFunction();
298 protected:
299 ~SocketReadFunction() override;
301 // AsyncApiFunction:
302 bool Prepare() override;
303 void AsyncWorkStart() override;
304 void OnCompleted(int result, scoped_refptr<net::IOBuffer> io_buffer);
306 private:
307 scoped_ptr<api::socket::Read::Params> params_;
310 class SocketWriteFunction : public SocketAsyncApiFunction {
311 public:
312 DECLARE_EXTENSION_FUNCTION("socket.write", SOCKET_WRITE)
314 SocketWriteFunction();
316 protected:
317 ~SocketWriteFunction() override;
319 // AsyncApiFunction:
320 bool Prepare() override;
321 void AsyncWorkStart() override;
322 void OnCompleted(int result);
324 private:
325 int socket_id_;
326 scoped_refptr<net::IOBuffer> io_buffer_;
327 size_t io_buffer_size_;
330 class SocketRecvFromFunction : public SocketAsyncApiFunction {
331 public:
332 DECLARE_EXTENSION_FUNCTION("socket.recvFrom", SOCKET_RECVFROM)
334 SocketRecvFromFunction();
336 protected:
337 ~SocketRecvFromFunction() override;
339 // AsyncApiFunction
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,
345 uint16 port);
347 private:
348 scoped_ptr<api::socket::RecvFrom::Params> params_;
351 class SocketSendToFunction : public SocketExtensionWithDnsLookupFunction {
352 public:
353 DECLARE_EXTENSION_FUNCTION("socket.sendTo", SOCKET_SENDTO)
355 SocketSendToFunction();
357 protected:
358 ~SocketSendToFunction() override;
360 // AsyncApiFunction:
361 bool Prepare() override;
362 void AsyncWorkStart() override;
363 void OnCompleted(int result);
365 // SocketExtensionWithDnsLookupFunction:
366 void AfterDnsLookup(int lookup_result) override;
368 private:
369 void StartSendTo();
371 int socket_id_;
372 scoped_refptr<net::IOBuffer> io_buffer_;
373 size_t io_buffer_size_;
374 std::string hostname_;
375 uint16 port_;
378 class SocketSetKeepAliveFunction : public SocketAsyncApiFunction {
379 public:
380 DECLARE_EXTENSION_FUNCTION("socket.setKeepAlive", SOCKET_SETKEEPALIVE)
382 SocketSetKeepAliveFunction();
384 protected:
385 ~SocketSetKeepAliveFunction() override;
387 // AsyncApiFunction:
388 bool Prepare() override;
389 void Work() override;
391 private:
392 scoped_ptr<api::socket::SetKeepAlive::Params> params_;
395 class SocketSetNoDelayFunction : public SocketAsyncApiFunction {
396 public:
397 DECLARE_EXTENSION_FUNCTION("socket.setNoDelay", SOCKET_SETNODELAY)
399 SocketSetNoDelayFunction();
401 protected:
402 ~SocketSetNoDelayFunction() override;
404 // AsyncApiFunction:
405 bool Prepare() override;
406 void Work() override;
408 private:
409 scoped_ptr<api::socket::SetNoDelay::Params> params_;
412 class SocketGetInfoFunction : public SocketAsyncApiFunction {
413 public:
414 DECLARE_EXTENSION_FUNCTION("socket.getInfo", SOCKET_GETINFO)
416 SocketGetInfoFunction();
418 protected:
419 ~SocketGetInfoFunction() override;
421 // AsyncApiFunction:
422 bool Prepare() override;
423 void Work() override;
425 private:
426 scoped_ptr<api::socket::GetInfo::Params> params_;
429 class SocketGetNetworkListFunction : public AsyncExtensionFunction {
430 public:
431 DECLARE_EXTENSION_FUNCTION("socket.getNetworkList", SOCKET_GETNETWORKLIST)
433 protected:
434 ~SocketGetNetworkListFunction() override {}
435 bool RunAsync() override;
437 private:
438 void GetNetworkListOnFileThread();
439 void HandleGetNetworkListError();
440 void SendResponseOnUIThread(const net::NetworkInterfaceList& interface_list);
443 class SocketJoinGroupFunction : public SocketAsyncApiFunction {
444 public:
445 DECLARE_EXTENSION_FUNCTION("socket.joinGroup", SOCKET_MULTICAST_JOIN_GROUP)
447 SocketJoinGroupFunction();
449 protected:
450 ~SocketJoinGroupFunction() override;
452 // AsyncApiFunction
453 bool Prepare() override;
454 void Work() override;
456 private:
457 scoped_ptr<api::socket::JoinGroup::Params> params_;
460 class SocketLeaveGroupFunction : public SocketAsyncApiFunction {
461 public:
462 DECLARE_EXTENSION_FUNCTION("socket.leaveGroup", SOCKET_MULTICAST_LEAVE_GROUP)
464 SocketLeaveGroupFunction();
466 protected:
467 ~SocketLeaveGroupFunction() override;
469 // AsyncApiFunction
470 bool Prepare() override;
471 void Work() override;
473 private:
474 scoped_ptr<api::socket::LeaveGroup::Params> params_;
477 class SocketSetMulticastTimeToLiveFunction : public SocketAsyncApiFunction {
478 public:
479 DECLARE_EXTENSION_FUNCTION("socket.setMulticastTimeToLive",
480 SOCKET_MULTICAST_SET_TIME_TO_LIVE)
482 SocketSetMulticastTimeToLiveFunction();
484 protected:
485 ~SocketSetMulticastTimeToLiveFunction() override;
487 // AsyncApiFunction
488 bool Prepare() override;
489 void Work() override;
491 private:
492 scoped_ptr<api::socket::SetMulticastTimeToLive::Params> params_;
495 class SocketSetMulticastLoopbackModeFunction : public SocketAsyncApiFunction {
496 public:
497 DECLARE_EXTENSION_FUNCTION("socket.setMulticastLoopbackMode",
498 SOCKET_MULTICAST_SET_LOOPBACK_MODE)
500 SocketSetMulticastLoopbackModeFunction();
502 protected:
503 ~SocketSetMulticastLoopbackModeFunction() override;
505 // AsyncApiFunction
506 bool Prepare() override;
507 void Work() override;
509 private:
510 scoped_ptr<api::socket::SetMulticastLoopbackMode::Params> params_;
513 class SocketGetJoinedGroupsFunction : public SocketAsyncApiFunction {
514 public:
515 DECLARE_EXTENSION_FUNCTION("socket.getJoinedGroups",
516 SOCKET_MULTICAST_GET_JOINED_GROUPS)
518 SocketGetJoinedGroupsFunction();
520 protected:
521 ~SocketGetJoinedGroupsFunction() override;
523 // AsyncApiFunction
524 bool Prepare() override;
525 void Work() override;
527 private:
528 scoped_ptr<api::socket::GetJoinedGroups::Params> params_;
531 class SocketSecureFunction : public SocketAsyncApiFunction {
532 public:
533 DECLARE_EXTENSION_FUNCTION("socket.secure", SOCKET_SECURE);
534 SocketSecureFunction();
536 protected:
537 ~SocketSecureFunction() override;
539 // AsyncApiFunction
540 bool Prepare() override;
541 void AsyncWorkStart() override;
543 private:
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_