Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / extensions / browser / api / socket / socket_api.h
blobd6e473678b9aac230911d10d17a25da76d2da9fb
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/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"
22 #endif // OS_CHROMEOS
24 namespace content {
25 class BrowserContext;
26 class ResourceContext;
29 namespace net {
30 class IOBuffer;
31 class URLRequestContextGetter;
32 class SSLClientSocket;
35 namespace extensions {
36 class Socket;
37 class TLSSocket;
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 {
44 public:
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,
52 int api_resource_id,
53 Socket* socket) = 0;
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).
60 template <typename T>
61 class SocketResourceManager : public SocketResourceManagerInterface {
62 public:
63 SocketResourceManager() : manager_(NULL) {}
65 bool SetBrowserContext(content::BrowserContext* context) override {
66 manager_ = ApiResourceManager<T>::Get(context);
67 DCHECK(manager_)
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,
85 int api_resource_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);
99 private:
100 ApiResourceManager<T>* manager_;
103 class SocketAsyncApiFunction : public AsyncApiFunction {
104 public:
105 SocketAsyncApiFunction();
107 protected:
108 ~SocketAsyncApiFunction() override;
110 // AsyncApiFunction:
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,
125 int socket_id,
126 Socket* socket);
128 private:
129 #if defined(OS_CHROMEOS)
130 void OpenFirewallHoleOnUIThread(AppFirewallHole::PortType type,
131 uint16_t port,
132 int socket_id);
133 void OnFirewallHoleOpened(
134 int socket_id,
135 scoped_ptr<AppFirewallHole, content::BrowserThread::DeleteOnUIThread>
136 hole);
137 #endif // OS_CHROMEOS
139 scoped_ptr<SocketResourceManagerInterface> manager_;
142 class SocketExtensionWithDnsLookupFunction : public SocketAsyncApiFunction {
143 protected:
144 SocketExtensionWithDnsLookupFunction();
145 ~SocketExtensionWithDnsLookupFunction() override;
147 // AsyncApiFunction:
148 bool PrePrepare() override;
150 void StartDnsLookup(const std::string& hostname);
151 virtual void AfterDnsLookup(int lookup_result) = 0;
153 std::string resolved_address_;
155 private:
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 {
166 public:
167 DECLARE_EXTENSION_FUNCTION("socket.create", SOCKET_CREATE)
169 SocketCreateFunction();
171 protected:
172 ~SocketCreateFunction() override;
174 // AsyncApiFunction:
175 bool Prepare() override;
176 void Work() override;
178 private:
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 {
187 public:
188 DECLARE_EXTENSION_FUNCTION("socket.destroy", SOCKET_DESTROY)
190 protected:
191 ~SocketDestroyFunction() override {}
193 // AsyncApiFunction:
194 bool Prepare() override;
195 void Work() override;
197 private:
198 int socket_id_;
201 class SocketConnectFunction : public SocketExtensionWithDnsLookupFunction {
202 public:
203 DECLARE_EXTENSION_FUNCTION("socket.connect", SOCKET_CONNECT)
205 SocketConnectFunction();
207 protected:
208 ~SocketConnectFunction() override;
210 // AsyncApiFunction:
211 bool Prepare() override;
212 void AsyncWorkStart() override;
214 // SocketExtensionWithDnsLookupFunction:
215 void AfterDnsLookup(int lookup_result) override;
217 private:
218 void StartConnect();
219 void OnConnect(int result);
221 int socket_id_;
222 std::string hostname_;
223 uint16 port_;
226 class SocketDisconnectFunction : public SocketAsyncApiFunction {
227 public:
228 DECLARE_EXTENSION_FUNCTION("socket.disconnect", SOCKET_DISCONNECT)
230 protected:
231 ~SocketDisconnectFunction() override {}
233 // AsyncApiFunction:
234 bool Prepare() override;
235 void Work() override;
237 private:
238 int socket_id_;
241 class SocketBindFunction : public SocketAsyncApiFunction {
242 public:
243 DECLARE_EXTENSION_FUNCTION("socket.bind", SOCKET_BIND)
245 protected:
246 ~SocketBindFunction() override {}
248 // AsyncApiFunction:
249 bool Prepare() override;
250 void AsyncWorkStart() override;
252 private:
253 int socket_id_;
254 std::string address_;
255 uint16 port_;
258 class SocketListenFunction : public SocketAsyncApiFunction {
259 public:
260 DECLARE_EXTENSION_FUNCTION("socket.listen", SOCKET_LISTEN)
262 SocketListenFunction();
264 protected:
265 ~SocketListenFunction() override;
267 // AsyncApiFunction:
268 bool Prepare() override;
269 void AsyncWorkStart() override;
271 private:
272 scoped_ptr<core_api::socket::Listen::Params> params_;
275 class SocketAcceptFunction : public SocketAsyncApiFunction {
276 public:
277 DECLARE_EXTENSION_FUNCTION("socket.accept", SOCKET_ACCEPT)
279 SocketAcceptFunction();
281 protected:
282 ~SocketAcceptFunction() override;
284 // AsyncApiFunction:
285 bool Prepare() override;
286 void AsyncWorkStart() override;
288 private:
289 void OnAccept(int result_code, net::TCPClientSocket* socket);
291 scoped_ptr<core_api::socket::Accept::Params> params_;
294 class SocketReadFunction : public SocketAsyncApiFunction {
295 public:
296 DECLARE_EXTENSION_FUNCTION("socket.read", SOCKET_READ)
298 SocketReadFunction();
300 protected:
301 ~SocketReadFunction() override;
303 // AsyncApiFunction:
304 bool Prepare() override;
305 void AsyncWorkStart() override;
306 void OnCompleted(int result, scoped_refptr<net::IOBuffer> io_buffer);
308 private:
309 scoped_ptr<core_api::socket::Read::Params> params_;
312 class SocketWriteFunction : public SocketAsyncApiFunction {
313 public:
314 DECLARE_EXTENSION_FUNCTION("socket.write", SOCKET_WRITE)
316 SocketWriteFunction();
318 protected:
319 ~SocketWriteFunction() override;
321 // AsyncApiFunction:
322 bool Prepare() override;
323 void AsyncWorkStart() override;
324 void OnCompleted(int result);
326 private:
327 int socket_id_;
328 scoped_refptr<net::IOBuffer> io_buffer_;
329 size_t io_buffer_size_;
332 class SocketRecvFromFunction : public SocketAsyncApiFunction {
333 public:
334 DECLARE_EXTENSION_FUNCTION("socket.recvFrom", SOCKET_RECVFROM)
336 SocketRecvFromFunction();
338 protected:
339 ~SocketRecvFromFunction() override;
341 // AsyncApiFunction
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,
347 uint16 port);
349 private:
350 scoped_ptr<core_api::socket::RecvFrom::Params> params_;
353 class SocketSendToFunction : public SocketExtensionWithDnsLookupFunction {
354 public:
355 DECLARE_EXTENSION_FUNCTION("socket.sendTo", SOCKET_SENDTO)
357 SocketSendToFunction();
359 protected:
360 ~SocketSendToFunction() override;
362 // AsyncApiFunction:
363 bool Prepare() override;
364 void AsyncWorkStart() override;
365 void OnCompleted(int result);
367 // SocketExtensionWithDnsLookupFunction:
368 void AfterDnsLookup(int lookup_result) override;
370 private:
371 void StartSendTo();
373 int socket_id_;
374 scoped_refptr<net::IOBuffer> io_buffer_;
375 size_t io_buffer_size_;
376 std::string hostname_;
377 uint16 port_;
380 class SocketSetKeepAliveFunction : public SocketAsyncApiFunction {
381 public:
382 DECLARE_EXTENSION_FUNCTION("socket.setKeepAlive", SOCKET_SETKEEPALIVE)
384 SocketSetKeepAliveFunction();
386 protected:
387 ~SocketSetKeepAliveFunction() override;
389 // AsyncApiFunction:
390 bool Prepare() override;
391 void Work() override;
393 private:
394 scoped_ptr<core_api::socket::SetKeepAlive::Params> params_;
397 class SocketSetNoDelayFunction : public SocketAsyncApiFunction {
398 public:
399 DECLARE_EXTENSION_FUNCTION("socket.setNoDelay", SOCKET_SETNODELAY)
401 SocketSetNoDelayFunction();
403 protected:
404 ~SocketSetNoDelayFunction() override;
406 // AsyncApiFunction:
407 bool Prepare() override;
408 void Work() override;
410 private:
411 scoped_ptr<core_api::socket::SetNoDelay::Params> params_;
414 class SocketGetInfoFunction : public SocketAsyncApiFunction {
415 public:
416 DECLARE_EXTENSION_FUNCTION("socket.getInfo", SOCKET_GETINFO)
418 SocketGetInfoFunction();
420 protected:
421 ~SocketGetInfoFunction() override;
423 // AsyncApiFunction:
424 bool Prepare() override;
425 void Work() override;
427 private:
428 scoped_ptr<core_api::socket::GetInfo::Params> params_;
431 class SocketGetNetworkListFunction : public AsyncExtensionFunction {
432 public:
433 DECLARE_EXTENSION_FUNCTION("socket.getNetworkList", SOCKET_GETNETWORKLIST)
435 protected:
436 ~SocketGetNetworkListFunction() override {}
437 bool RunAsync() override;
439 private:
440 void GetNetworkListOnFileThread();
441 void HandleGetNetworkListError();
442 void SendResponseOnUIThread(const net::NetworkInterfaceList& interface_list);
445 class SocketJoinGroupFunction : public SocketAsyncApiFunction {
446 public:
447 DECLARE_EXTENSION_FUNCTION("socket.joinGroup", SOCKET_MULTICAST_JOIN_GROUP)
449 SocketJoinGroupFunction();
451 protected:
452 ~SocketJoinGroupFunction() override;
454 // AsyncApiFunction
455 bool Prepare() override;
456 void Work() override;
458 private:
459 scoped_ptr<core_api::socket::JoinGroup::Params> params_;
462 class SocketLeaveGroupFunction : public SocketAsyncApiFunction {
463 public:
464 DECLARE_EXTENSION_FUNCTION("socket.leaveGroup", SOCKET_MULTICAST_LEAVE_GROUP)
466 SocketLeaveGroupFunction();
468 protected:
469 ~SocketLeaveGroupFunction() override;
471 // AsyncApiFunction
472 bool Prepare() override;
473 void Work() override;
475 private:
476 scoped_ptr<core_api::socket::LeaveGroup::Params> params_;
479 class SocketSetMulticastTimeToLiveFunction : public SocketAsyncApiFunction {
480 public:
481 DECLARE_EXTENSION_FUNCTION("socket.setMulticastTimeToLive",
482 SOCKET_MULTICAST_SET_TIME_TO_LIVE)
484 SocketSetMulticastTimeToLiveFunction();
486 protected:
487 ~SocketSetMulticastTimeToLiveFunction() override;
489 // AsyncApiFunction
490 bool Prepare() override;
491 void Work() override;
493 private:
494 scoped_ptr<core_api::socket::SetMulticastTimeToLive::Params> params_;
497 class SocketSetMulticastLoopbackModeFunction : public SocketAsyncApiFunction {
498 public:
499 DECLARE_EXTENSION_FUNCTION("socket.setMulticastLoopbackMode",
500 SOCKET_MULTICAST_SET_LOOPBACK_MODE)
502 SocketSetMulticastLoopbackModeFunction();
504 protected:
505 ~SocketSetMulticastLoopbackModeFunction() override;
507 // AsyncApiFunction
508 bool Prepare() override;
509 void Work() override;
511 private:
512 scoped_ptr<core_api::socket::SetMulticastLoopbackMode::Params> params_;
515 class SocketGetJoinedGroupsFunction : public SocketAsyncApiFunction {
516 public:
517 DECLARE_EXTENSION_FUNCTION("socket.getJoinedGroups",
518 SOCKET_MULTICAST_GET_JOINED_GROUPS)
520 SocketGetJoinedGroupsFunction();
522 protected:
523 ~SocketGetJoinedGroupsFunction() override;
525 // AsyncApiFunction
526 bool Prepare() override;
527 void Work() override;
529 private:
530 scoped_ptr<core_api::socket::GetJoinedGroups::Params> params_;
533 class SocketSecureFunction : public SocketAsyncApiFunction {
534 public:
535 DECLARE_EXTENSION_FUNCTION("socket.secure", SOCKET_SECURE);
536 SocketSecureFunction();
538 protected:
539 ~SocketSecureFunction() override;
541 // AsyncApiFunction
542 bool Prepare() override;
543 void AsyncWorkStart() override;
545 private:
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_