Add testing/scripts/OWNERS
[chromium-blink-merge.git] / extensions / browser / api / socket / socket_api.h
blob8ceee16fe18514ded67d561a65ce4f785e28c7e8
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 namespace content {
21 class BrowserContext;
22 class ResourceContext;
25 namespace net {
26 class IOBuffer;
27 class URLRequestContextGetter;
28 class SSLClientSocket;
31 namespace extensions {
32 class TLSSocket;
33 class Socket;
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 {
40 public:
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,
48 int api_resource_id,
49 Socket* socket) = 0;
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).
56 template <typename T>
57 class SocketResourceManager : public SocketResourceManagerInterface {
58 public:
59 SocketResourceManager() : manager_(NULL) {}
61 virtual bool SetBrowserContext(content::BrowserContext* context) override {
62 manager_ = ApiResourceManager<T>::Get(context);
63 DCHECK(manager_)
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,
82 int api_resource_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)
93 override {
94 return manager_->GetResourceIds(extension_id);
97 private:
98 ApiResourceManager<T>* manager_;
101 class SocketAsyncApiFunction : public AsyncApiFunction {
102 public:
103 SocketAsyncApiFunction();
105 protected:
106 ~SocketAsyncApiFunction() override;
108 // AsyncApiFunction:
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();
121 private:
122 scoped_ptr<SocketResourceManagerInterface> manager_;
125 class SocketExtensionWithDnsLookupFunction : public SocketAsyncApiFunction {
126 protected:
127 SocketExtensionWithDnsLookupFunction();
128 ~SocketExtensionWithDnsLookupFunction() override;
130 // AsyncApiFunction:
131 bool PrePrepare() override;
133 void StartDnsLookup(const std::string& hostname);
134 virtual void AfterDnsLookup(int lookup_result) = 0;
136 std::string resolved_address_;
138 private:
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 {
149 public:
150 DECLARE_EXTENSION_FUNCTION("socket.create", SOCKET_CREATE)
152 SocketCreateFunction();
154 protected:
155 ~SocketCreateFunction() override;
157 // AsyncApiFunction:
158 bool Prepare() override;
159 void Work() override;
161 private:
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 {
170 public:
171 DECLARE_EXTENSION_FUNCTION("socket.destroy", SOCKET_DESTROY)
173 protected:
174 ~SocketDestroyFunction() override {}
176 // AsyncApiFunction:
177 bool Prepare() override;
178 void Work() override;
180 private:
181 int socket_id_;
184 class SocketConnectFunction : public SocketExtensionWithDnsLookupFunction {
185 public:
186 DECLARE_EXTENSION_FUNCTION("socket.connect", SOCKET_CONNECT)
188 SocketConnectFunction();
190 protected:
191 ~SocketConnectFunction() override;
193 // AsyncApiFunction:
194 bool Prepare() override;
195 void AsyncWorkStart() override;
197 // SocketExtensionWithDnsLookupFunction:
198 void AfterDnsLookup(int lookup_result) override;
200 private:
201 void StartConnect();
202 void OnConnect(int result);
204 int socket_id_;
205 std::string hostname_;
206 int port_;
209 class SocketDisconnectFunction : public SocketAsyncApiFunction {
210 public:
211 DECLARE_EXTENSION_FUNCTION("socket.disconnect", SOCKET_DISCONNECT)
213 protected:
214 ~SocketDisconnectFunction() override {}
216 // AsyncApiFunction:
217 bool Prepare() override;
218 void Work() override;
220 private:
221 int socket_id_;
224 class SocketBindFunction : public SocketAsyncApiFunction {
225 public:
226 DECLARE_EXTENSION_FUNCTION("socket.bind", SOCKET_BIND)
228 protected:
229 ~SocketBindFunction() override {}
231 // AsyncApiFunction:
232 bool Prepare() override;
233 void Work() override;
235 private:
236 int socket_id_;
237 std::string address_;
238 int port_;
241 class SocketListenFunction : public SocketAsyncApiFunction {
242 public:
243 DECLARE_EXTENSION_FUNCTION("socket.listen", SOCKET_LISTEN)
245 SocketListenFunction();
247 protected:
248 ~SocketListenFunction() override;
250 // AsyncApiFunction:
251 bool Prepare() override;
252 void Work() override;
254 private:
255 scoped_ptr<core_api::socket::Listen::Params> params_;
258 class SocketAcceptFunction : public SocketAsyncApiFunction {
259 public:
260 DECLARE_EXTENSION_FUNCTION("socket.accept", SOCKET_ACCEPT)
262 SocketAcceptFunction();
264 protected:
265 ~SocketAcceptFunction() override;
267 // AsyncApiFunction:
268 bool Prepare() override;
269 void AsyncWorkStart() override;
271 private:
272 void OnAccept(int result_code, net::TCPClientSocket* socket);
274 scoped_ptr<core_api::socket::Accept::Params> params_;
277 class SocketReadFunction : public SocketAsyncApiFunction {
278 public:
279 DECLARE_EXTENSION_FUNCTION("socket.read", SOCKET_READ)
281 SocketReadFunction();
283 protected:
284 ~SocketReadFunction() override;
286 // AsyncApiFunction:
287 bool Prepare() override;
288 void AsyncWorkStart() override;
289 void OnCompleted(int result, scoped_refptr<net::IOBuffer> io_buffer);
291 private:
292 scoped_ptr<core_api::socket::Read::Params> params_;
295 class SocketWriteFunction : public SocketAsyncApiFunction {
296 public:
297 DECLARE_EXTENSION_FUNCTION("socket.write", SOCKET_WRITE)
299 SocketWriteFunction();
301 protected:
302 ~SocketWriteFunction() override;
304 // AsyncApiFunction:
305 bool Prepare() override;
306 void AsyncWorkStart() override;
307 void OnCompleted(int result);
309 private:
310 int socket_id_;
311 scoped_refptr<net::IOBuffer> io_buffer_;
312 size_t io_buffer_size_;
315 class SocketRecvFromFunction : public SocketAsyncApiFunction {
316 public:
317 DECLARE_EXTENSION_FUNCTION("socket.recvFrom", SOCKET_RECVFROM)
319 SocketRecvFromFunction();
321 protected:
322 ~SocketRecvFromFunction() override;
324 // AsyncApiFunction
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,
330 int port);
332 private:
333 scoped_ptr<core_api::socket::RecvFrom::Params> params_;
336 class SocketSendToFunction : public SocketExtensionWithDnsLookupFunction {
337 public:
338 DECLARE_EXTENSION_FUNCTION("socket.sendTo", SOCKET_SENDTO)
340 SocketSendToFunction();
342 protected:
343 ~SocketSendToFunction() override;
345 // AsyncApiFunction:
346 bool Prepare() override;
347 void AsyncWorkStart() override;
348 void OnCompleted(int result);
350 // SocketExtensionWithDnsLookupFunction:
351 void AfterDnsLookup(int lookup_result) override;
353 private:
354 void StartSendTo();
356 int socket_id_;
357 scoped_refptr<net::IOBuffer> io_buffer_;
358 size_t io_buffer_size_;
359 std::string hostname_;
360 int port_;
363 class SocketSetKeepAliveFunction : public SocketAsyncApiFunction {
364 public:
365 DECLARE_EXTENSION_FUNCTION("socket.setKeepAlive", SOCKET_SETKEEPALIVE)
367 SocketSetKeepAliveFunction();
369 protected:
370 ~SocketSetKeepAliveFunction() override;
372 // AsyncApiFunction:
373 bool Prepare() override;
374 void Work() override;
376 private:
377 scoped_ptr<core_api::socket::SetKeepAlive::Params> params_;
380 class SocketSetNoDelayFunction : public SocketAsyncApiFunction {
381 public:
382 DECLARE_EXTENSION_FUNCTION("socket.setNoDelay", SOCKET_SETNODELAY)
384 SocketSetNoDelayFunction();
386 protected:
387 ~SocketSetNoDelayFunction() override;
389 // AsyncApiFunction:
390 bool Prepare() override;
391 void Work() override;
393 private:
394 scoped_ptr<core_api::socket::SetNoDelay::Params> params_;
397 class SocketGetInfoFunction : public SocketAsyncApiFunction {
398 public:
399 DECLARE_EXTENSION_FUNCTION("socket.getInfo", SOCKET_GETINFO)
401 SocketGetInfoFunction();
403 protected:
404 ~SocketGetInfoFunction() override;
406 // AsyncApiFunction:
407 bool Prepare() override;
408 void Work() override;
410 private:
411 scoped_ptr<core_api::socket::GetInfo::Params> params_;
414 class SocketGetNetworkListFunction : public AsyncExtensionFunction {
415 public:
416 DECLARE_EXTENSION_FUNCTION("socket.getNetworkList", SOCKET_GETNETWORKLIST)
418 protected:
419 ~SocketGetNetworkListFunction() override {}
420 bool RunAsync() override;
422 private:
423 void GetNetworkListOnFileThread();
424 void HandleGetNetworkListError();
425 void SendResponseOnUIThread(const net::NetworkInterfaceList& interface_list);
428 class SocketJoinGroupFunction : public SocketAsyncApiFunction {
429 public:
430 DECLARE_EXTENSION_FUNCTION("socket.joinGroup", SOCKET_MULTICAST_JOIN_GROUP)
432 SocketJoinGroupFunction();
434 protected:
435 ~SocketJoinGroupFunction() override;
437 // AsyncApiFunction
438 bool Prepare() override;
439 void Work() override;
441 private:
442 scoped_ptr<core_api::socket::JoinGroup::Params> params_;
445 class SocketLeaveGroupFunction : public SocketAsyncApiFunction {
446 public:
447 DECLARE_EXTENSION_FUNCTION("socket.leaveGroup", SOCKET_MULTICAST_LEAVE_GROUP)
449 SocketLeaveGroupFunction();
451 protected:
452 ~SocketLeaveGroupFunction() override;
454 // AsyncApiFunction
455 bool Prepare() override;
456 void Work() override;
458 private:
459 scoped_ptr<core_api::socket::LeaveGroup::Params> params_;
462 class SocketSetMulticastTimeToLiveFunction : public SocketAsyncApiFunction {
463 public:
464 DECLARE_EXTENSION_FUNCTION("socket.setMulticastTimeToLive",
465 SOCKET_MULTICAST_SET_TIME_TO_LIVE)
467 SocketSetMulticastTimeToLiveFunction();
469 protected:
470 ~SocketSetMulticastTimeToLiveFunction() override;
472 // AsyncApiFunction
473 bool Prepare() override;
474 void Work() override;
476 private:
477 scoped_ptr<core_api::socket::SetMulticastTimeToLive::Params> params_;
480 class SocketSetMulticastLoopbackModeFunction : public SocketAsyncApiFunction {
481 public:
482 DECLARE_EXTENSION_FUNCTION("socket.setMulticastLoopbackMode",
483 SOCKET_MULTICAST_SET_LOOPBACK_MODE)
485 SocketSetMulticastLoopbackModeFunction();
487 protected:
488 ~SocketSetMulticastLoopbackModeFunction() override;
490 // AsyncApiFunction
491 bool Prepare() override;
492 void Work() override;
494 private:
495 scoped_ptr<core_api::socket::SetMulticastLoopbackMode::Params> params_;
498 class SocketGetJoinedGroupsFunction : public SocketAsyncApiFunction {
499 public:
500 DECLARE_EXTENSION_FUNCTION("socket.getJoinedGroups",
501 SOCKET_MULTICAST_GET_JOINED_GROUPS)
503 SocketGetJoinedGroupsFunction();
505 protected:
506 ~SocketGetJoinedGroupsFunction() override;
508 // AsyncApiFunction
509 bool Prepare() override;
510 void Work() override;
512 private:
513 scoped_ptr<core_api::socket::GetJoinedGroups::Params> params_;
516 class SocketSecureFunction : public SocketAsyncApiFunction {
517 public:
518 DECLARE_EXTENSION_FUNCTION("socket.secure", SOCKET_SECURE);
519 SocketSecureFunction();
521 protected:
522 ~SocketSecureFunction() override;
524 // AsyncApiFunction
525 bool Prepare() override;
526 void AsyncWorkStart() override;
528 private:
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_