We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / extensions / browser / api / socket / socket_api.h
bloba3e4e0ead7872f5f4a7cdf7c22ec19c06e96fc36
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 virtual 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 virtual 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 virtual Socket* Get(const std::string& extension_id,
81 int api_resource_id) override {
82 return manager_->Get(extension_id, api_resource_id);
85 virtual 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 virtual void Remove(const std::string& extension_id,
92 int api_resource_id) override {
93 manager_->Remove(extension_id, api_resource_id);
96 virtual base::hash_set<int>* GetResourceIds(const std::string& extension_id)
97 override {
98 return manager_->GetResourceIds(extension_id);
101 private:
102 ApiResourceManager<T>* manager_;
105 class SocketAsyncApiFunction : public AsyncApiFunction {
106 public:
107 SocketAsyncApiFunction();
109 protected:
110 ~SocketAsyncApiFunction() override;
112 // AsyncApiFunction:
113 bool PrePrepare() override;
114 bool Respond() override;
116 virtual scoped_ptr<SocketResourceManagerInterface>
117 CreateSocketResourceManager();
119 int AddSocket(Socket* socket);
120 Socket* GetSocket(int api_resource_id);
121 void ReplaceSocket(int api_resource_id, Socket* socket);
122 void RemoveSocket(int api_resource_id);
123 base::hash_set<int>* GetSocketIds();
125 // A no-op outside of Chrome OS.
126 void OpenFirewallHole(const std::string& address,
127 int socket_id,
128 Socket* socket);
130 private:
131 #if defined(OS_CHROMEOS)
132 void OpenFirewallHoleOnUIThread(AppFirewallHole::PortType type,
133 uint16_t port,
134 int socket_id);
135 void OnFirewallHoleOpened(
136 int socket_id,
137 scoped_ptr<AppFirewallHole, content::BrowserThread::DeleteOnUIThread>
138 hole);
139 #endif // OS_CHROMEOS
141 scoped_ptr<SocketResourceManagerInterface> manager_;
144 class SocketExtensionWithDnsLookupFunction : public SocketAsyncApiFunction {
145 protected:
146 SocketExtensionWithDnsLookupFunction();
147 ~SocketExtensionWithDnsLookupFunction() override;
149 // AsyncApiFunction:
150 bool PrePrepare() override;
152 void StartDnsLookup(const std::string& hostname);
153 virtual void AfterDnsLookup(int lookup_result) = 0;
155 std::string resolved_address_;
157 private:
158 void OnDnsLookup(int resolve_result);
160 // Weak pointer to the resource context.
161 content::ResourceContext* resource_context_;
163 scoped_ptr<net::HostResolver::RequestHandle> request_handle_;
164 scoped_ptr<net::AddressList> addresses_;
167 class SocketCreateFunction : public SocketAsyncApiFunction {
168 public:
169 DECLARE_EXTENSION_FUNCTION("socket.create", SOCKET_CREATE)
171 SocketCreateFunction();
173 protected:
174 ~SocketCreateFunction() override;
176 // AsyncApiFunction:
177 bool Prepare() override;
178 void Work() override;
180 private:
181 FRIEND_TEST_ALL_PREFIXES(SocketUnitTest, Create);
182 enum SocketType { kSocketTypeInvalid = -1, kSocketTypeTCP, kSocketTypeUDP };
184 scoped_ptr<core_api::socket::Create::Params> params_;
185 SocketType socket_type_;
188 class SocketDestroyFunction : public SocketAsyncApiFunction {
189 public:
190 DECLARE_EXTENSION_FUNCTION("socket.destroy", SOCKET_DESTROY)
192 protected:
193 ~SocketDestroyFunction() override {}
195 // AsyncApiFunction:
196 bool Prepare() override;
197 void Work() override;
199 private:
200 int socket_id_;
203 class SocketConnectFunction : public SocketExtensionWithDnsLookupFunction {
204 public:
205 DECLARE_EXTENSION_FUNCTION("socket.connect", SOCKET_CONNECT)
207 SocketConnectFunction();
209 protected:
210 ~SocketConnectFunction() override;
212 // AsyncApiFunction:
213 bool Prepare() override;
214 void AsyncWorkStart() override;
216 // SocketExtensionWithDnsLookupFunction:
217 void AfterDnsLookup(int lookup_result) override;
219 private:
220 void StartConnect();
221 void OnConnect(int result);
223 int socket_id_;
224 std::string hostname_;
225 uint16 port_;
228 class SocketDisconnectFunction : public SocketAsyncApiFunction {
229 public:
230 DECLARE_EXTENSION_FUNCTION("socket.disconnect", SOCKET_DISCONNECT)
232 protected:
233 ~SocketDisconnectFunction() override {}
235 // AsyncApiFunction:
236 bool Prepare() override;
237 void Work() override;
239 private:
240 int socket_id_;
243 class SocketBindFunction : public SocketAsyncApiFunction {
244 public:
245 DECLARE_EXTENSION_FUNCTION("socket.bind", SOCKET_BIND)
247 protected:
248 ~SocketBindFunction() override {}
250 // AsyncApiFunction:
251 bool Prepare() override;
252 void AsyncWorkStart() override;
254 private:
255 int socket_id_;
256 std::string address_;
257 uint16 port_;
260 class SocketListenFunction : public SocketAsyncApiFunction {
261 public:
262 DECLARE_EXTENSION_FUNCTION("socket.listen", SOCKET_LISTEN)
264 SocketListenFunction();
266 protected:
267 ~SocketListenFunction() override;
269 // AsyncApiFunction:
270 bool Prepare() override;
271 void AsyncWorkStart() override;
273 private:
274 scoped_ptr<core_api::socket::Listen::Params> params_;
277 class SocketAcceptFunction : public SocketAsyncApiFunction {
278 public:
279 DECLARE_EXTENSION_FUNCTION("socket.accept", SOCKET_ACCEPT)
281 SocketAcceptFunction();
283 protected:
284 ~SocketAcceptFunction() override;
286 // AsyncApiFunction:
287 bool Prepare() override;
288 void AsyncWorkStart() override;
290 private:
291 void OnAccept(int result_code, net::TCPClientSocket* socket);
293 scoped_ptr<core_api::socket::Accept::Params> params_;
296 class SocketReadFunction : public SocketAsyncApiFunction {
297 public:
298 DECLARE_EXTENSION_FUNCTION("socket.read", SOCKET_READ)
300 SocketReadFunction();
302 protected:
303 ~SocketReadFunction() override;
305 // AsyncApiFunction:
306 bool Prepare() override;
307 void AsyncWorkStart() override;
308 void OnCompleted(int result, scoped_refptr<net::IOBuffer> io_buffer);
310 private:
311 scoped_ptr<core_api::socket::Read::Params> params_;
314 class SocketWriteFunction : public SocketAsyncApiFunction {
315 public:
316 DECLARE_EXTENSION_FUNCTION("socket.write", SOCKET_WRITE)
318 SocketWriteFunction();
320 protected:
321 ~SocketWriteFunction() override;
323 // AsyncApiFunction:
324 bool Prepare() override;
325 void AsyncWorkStart() override;
326 void OnCompleted(int result);
328 private:
329 int socket_id_;
330 scoped_refptr<net::IOBuffer> io_buffer_;
331 size_t io_buffer_size_;
334 class SocketRecvFromFunction : public SocketAsyncApiFunction {
335 public:
336 DECLARE_EXTENSION_FUNCTION("socket.recvFrom", SOCKET_RECVFROM)
338 SocketRecvFromFunction();
340 protected:
341 ~SocketRecvFromFunction() override;
343 // AsyncApiFunction
344 bool Prepare() override;
345 void AsyncWorkStart() override;
346 void OnCompleted(int result,
347 scoped_refptr<net::IOBuffer> io_buffer,
348 const std::string& address,
349 uint16 port);
351 private:
352 scoped_ptr<core_api::socket::RecvFrom::Params> params_;
355 class SocketSendToFunction : public SocketExtensionWithDnsLookupFunction {
356 public:
357 DECLARE_EXTENSION_FUNCTION("socket.sendTo", SOCKET_SENDTO)
359 SocketSendToFunction();
361 protected:
362 ~SocketSendToFunction() override;
364 // AsyncApiFunction:
365 bool Prepare() override;
366 void AsyncWorkStart() override;
367 void OnCompleted(int result);
369 // SocketExtensionWithDnsLookupFunction:
370 void AfterDnsLookup(int lookup_result) override;
372 private:
373 void StartSendTo();
375 int socket_id_;
376 scoped_refptr<net::IOBuffer> io_buffer_;
377 size_t io_buffer_size_;
378 std::string hostname_;
379 uint16 port_;
382 class SocketSetKeepAliveFunction : public SocketAsyncApiFunction {
383 public:
384 DECLARE_EXTENSION_FUNCTION("socket.setKeepAlive", SOCKET_SETKEEPALIVE)
386 SocketSetKeepAliveFunction();
388 protected:
389 ~SocketSetKeepAliveFunction() override;
391 // AsyncApiFunction:
392 bool Prepare() override;
393 void Work() override;
395 private:
396 scoped_ptr<core_api::socket::SetKeepAlive::Params> params_;
399 class SocketSetNoDelayFunction : public SocketAsyncApiFunction {
400 public:
401 DECLARE_EXTENSION_FUNCTION("socket.setNoDelay", SOCKET_SETNODELAY)
403 SocketSetNoDelayFunction();
405 protected:
406 ~SocketSetNoDelayFunction() override;
408 // AsyncApiFunction:
409 bool Prepare() override;
410 void Work() override;
412 private:
413 scoped_ptr<core_api::socket::SetNoDelay::Params> params_;
416 class SocketGetInfoFunction : public SocketAsyncApiFunction {
417 public:
418 DECLARE_EXTENSION_FUNCTION("socket.getInfo", SOCKET_GETINFO)
420 SocketGetInfoFunction();
422 protected:
423 ~SocketGetInfoFunction() override;
425 // AsyncApiFunction:
426 bool Prepare() override;
427 void Work() override;
429 private:
430 scoped_ptr<core_api::socket::GetInfo::Params> params_;
433 class SocketGetNetworkListFunction : public AsyncExtensionFunction {
434 public:
435 DECLARE_EXTENSION_FUNCTION("socket.getNetworkList", SOCKET_GETNETWORKLIST)
437 protected:
438 ~SocketGetNetworkListFunction() override {}
439 bool RunAsync() override;
441 private:
442 void GetNetworkListOnFileThread();
443 void HandleGetNetworkListError();
444 void SendResponseOnUIThread(const net::NetworkInterfaceList& interface_list);
447 class SocketJoinGroupFunction : public SocketAsyncApiFunction {
448 public:
449 DECLARE_EXTENSION_FUNCTION("socket.joinGroup", SOCKET_MULTICAST_JOIN_GROUP)
451 SocketJoinGroupFunction();
453 protected:
454 ~SocketJoinGroupFunction() override;
456 // AsyncApiFunction
457 bool Prepare() override;
458 void Work() override;
460 private:
461 scoped_ptr<core_api::socket::JoinGroup::Params> params_;
464 class SocketLeaveGroupFunction : public SocketAsyncApiFunction {
465 public:
466 DECLARE_EXTENSION_FUNCTION("socket.leaveGroup", SOCKET_MULTICAST_LEAVE_GROUP)
468 SocketLeaveGroupFunction();
470 protected:
471 ~SocketLeaveGroupFunction() override;
473 // AsyncApiFunction
474 bool Prepare() override;
475 void Work() override;
477 private:
478 scoped_ptr<core_api::socket::LeaveGroup::Params> params_;
481 class SocketSetMulticastTimeToLiveFunction : public SocketAsyncApiFunction {
482 public:
483 DECLARE_EXTENSION_FUNCTION("socket.setMulticastTimeToLive",
484 SOCKET_MULTICAST_SET_TIME_TO_LIVE)
486 SocketSetMulticastTimeToLiveFunction();
488 protected:
489 ~SocketSetMulticastTimeToLiveFunction() override;
491 // AsyncApiFunction
492 bool Prepare() override;
493 void Work() override;
495 private:
496 scoped_ptr<core_api::socket::SetMulticastTimeToLive::Params> params_;
499 class SocketSetMulticastLoopbackModeFunction : public SocketAsyncApiFunction {
500 public:
501 DECLARE_EXTENSION_FUNCTION("socket.setMulticastLoopbackMode",
502 SOCKET_MULTICAST_SET_LOOPBACK_MODE)
504 SocketSetMulticastLoopbackModeFunction();
506 protected:
507 ~SocketSetMulticastLoopbackModeFunction() override;
509 // AsyncApiFunction
510 bool Prepare() override;
511 void Work() override;
513 private:
514 scoped_ptr<core_api::socket::SetMulticastLoopbackMode::Params> params_;
517 class SocketGetJoinedGroupsFunction : public SocketAsyncApiFunction {
518 public:
519 DECLARE_EXTENSION_FUNCTION("socket.getJoinedGroups",
520 SOCKET_MULTICAST_GET_JOINED_GROUPS)
522 SocketGetJoinedGroupsFunction();
524 protected:
525 ~SocketGetJoinedGroupsFunction() override;
527 // AsyncApiFunction
528 bool Prepare() override;
529 void Work() override;
531 private:
532 scoped_ptr<core_api::socket::GetJoinedGroups::Params> params_;
535 class SocketSecureFunction : public SocketAsyncApiFunction {
536 public:
537 DECLARE_EXTENSION_FUNCTION("socket.secure", SOCKET_SECURE);
538 SocketSecureFunction();
540 protected:
541 ~SocketSecureFunction() override;
543 // AsyncApiFunction
544 bool Prepare() override;
545 void AsyncWorkStart() override;
547 private:
548 // Callback from TLSSocket::UpgradeSocketToTLS().
549 void TlsConnectDone(scoped_ptr<TLSSocket> socket, int result);
551 scoped_ptr<core_api::socket::Secure::Params> params_;
552 scoped_refptr<net::URLRequestContextGetter> url_request_getter_;
554 DISALLOW_COPY_AND_ASSIGN(SocketSecureFunction);
557 } // namespace extensions
559 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_