Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / socket / socket_api.h
blob5e983ab7d322ccdb1c16979a9cdabda864fee72d
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 net::HostPortPair& host_port_pair);
151 virtual void AfterDnsLookup(int lookup_result) = 0;
153 net::AddressList addresses_;
155 private:
156 void OnDnsLookup(int resolve_result);
158 // Weak pointer to the resource context.
159 content::ResourceContext* resource_context_;
162 class SocketCreateFunction : public SocketAsyncApiFunction {
163 public:
164 DECLARE_EXTENSION_FUNCTION("socket.create", SOCKET_CREATE)
166 SocketCreateFunction();
168 protected:
169 ~SocketCreateFunction() override;
171 // AsyncApiFunction:
172 bool Prepare() override;
173 void Work() override;
175 private:
176 FRIEND_TEST_ALL_PREFIXES(SocketUnitTest, Create);
177 enum SocketType { kSocketTypeInvalid = -1, kSocketTypeTCP, kSocketTypeUDP };
179 scoped_ptr<api::socket::Create::Params> params_;
180 SocketType socket_type_;
183 class SocketDestroyFunction : public SocketAsyncApiFunction {
184 public:
185 DECLARE_EXTENSION_FUNCTION("socket.destroy", SOCKET_DESTROY)
187 protected:
188 ~SocketDestroyFunction() override {}
190 // AsyncApiFunction:
191 bool Prepare() override;
192 void Work() override;
194 private:
195 int socket_id_;
198 class SocketConnectFunction : public SocketExtensionWithDnsLookupFunction {
199 public:
200 DECLARE_EXTENSION_FUNCTION("socket.connect", SOCKET_CONNECT)
202 SocketConnectFunction();
204 protected:
205 ~SocketConnectFunction() override;
207 // AsyncApiFunction:
208 bool Prepare() override;
209 void AsyncWorkStart() override;
211 // SocketExtensionWithDnsLookupFunction:
212 void AfterDnsLookup(int lookup_result) override;
214 private:
215 void StartConnect();
216 void OnConnect(int result);
218 int socket_id_;
219 std::string hostname_;
220 uint16 port_;
223 class SocketDisconnectFunction : public SocketAsyncApiFunction {
224 public:
225 DECLARE_EXTENSION_FUNCTION("socket.disconnect", SOCKET_DISCONNECT)
227 protected:
228 ~SocketDisconnectFunction() override {}
230 // AsyncApiFunction:
231 bool Prepare() override;
232 void Work() override;
234 private:
235 int socket_id_;
238 class SocketBindFunction : public SocketAsyncApiFunction {
239 public:
240 DECLARE_EXTENSION_FUNCTION("socket.bind", SOCKET_BIND)
242 protected:
243 ~SocketBindFunction() override {}
245 // AsyncApiFunction:
246 bool Prepare() override;
247 void AsyncWorkStart() override;
249 private:
250 int socket_id_;
251 std::string address_;
252 uint16 port_;
255 class SocketListenFunction : public SocketAsyncApiFunction {
256 public:
257 DECLARE_EXTENSION_FUNCTION("socket.listen", SOCKET_LISTEN)
259 SocketListenFunction();
261 protected:
262 ~SocketListenFunction() override;
264 // AsyncApiFunction:
265 bool Prepare() override;
266 void AsyncWorkStart() override;
268 private:
269 scoped_ptr<api::socket::Listen::Params> params_;
272 class SocketAcceptFunction : public SocketAsyncApiFunction {
273 public:
274 DECLARE_EXTENSION_FUNCTION("socket.accept", SOCKET_ACCEPT)
276 SocketAcceptFunction();
278 protected:
279 ~SocketAcceptFunction() override;
281 // AsyncApiFunction:
282 bool Prepare() override;
283 void AsyncWorkStart() override;
285 private:
286 void OnAccept(int result_code, net::TCPClientSocket* socket);
288 scoped_ptr<api::socket::Accept::Params> params_;
291 class SocketReadFunction : public SocketAsyncApiFunction {
292 public:
293 DECLARE_EXTENSION_FUNCTION("socket.read", SOCKET_READ)
295 SocketReadFunction();
297 protected:
298 ~SocketReadFunction() override;
300 // AsyncApiFunction:
301 bool Prepare() override;
302 void AsyncWorkStart() override;
303 void OnCompleted(int result, scoped_refptr<net::IOBuffer> io_buffer);
305 private:
306 scoped_ptr<api::socket::Read::Params> params_;
309 class SocketWriteFunction : public SocketAsyncApiFunction {
310 public:
311 DECLARE_EXTENSION_FUNCTION("socket.write", SOCKET_WRITE)
313 SocketWriteFunction();
315 protected:
316 ~SocketWriteFunction() override;
318 // AsyncApiFunction:
319 bool Prepare() override;
320 void AsyncWorkStart() override;
321 void OnCompleted(int result);
323 private:
324 int socket_id_;
325 scoped_refptr<net::IOBuffer> io_buffer_;
326 size_t io_buffer_size_;
329 class SocketRecvFromFunction : public SocketAsyncApiFunction {
330 public:
331 DECLARE_EXTENSION_FUNCTION("socket.recvFrom", SOCKET_RECVFROM)
333 SocketRecvFromFunction();
335 protected:
336 ~SocketRecvFromFunction() override;
338 // AsyncApiFunction
339 bool Prepare() override;
340 void AsyncWorkStart() override;
341 void OnCompleted(int result,
342 scoped_refptr<net::IOBuffer> io_buffer,
343 const std::string& address,
344 uint16 port);
346 private:
347 scoped_ptr<api::socket::RecvFrom::Params> params_;
350 class SocketSendToFunction : public SocketExtensionWithDnsLookupFunction {
351 public:
352 DECLARE_EXTENSION_FUNCTION("socket.sendTo", SOCKET_SENDTO)
354 SocketSendToFunction();
356 protected:
357 ~SocketSendToFunction() override;
359 // AsyncApiFunction:
360 bool Prepare() override;
361 void AsyncWorkStart() override;
362 void OnCompleted(int result);
364 // SocketExtensionWithDnsLookupFunction:
365 void AfterDnsLookup(int lookup_result) override;
367 private:
368 void StartSendTo();
370 int socket_id_;
371 scoped_refptr<net::IOBuffer> io_buffer_;
372 size_t io_buffer_size_;
373 std::string hostname_;
374 uint16 port_;
377 class SocketSetKeepAliveFunction : public SocketAsyncApiFunction {
378 public:
379 DECLARE_EXTENSION_FUNCTION("socket.setKeepAlive", SOCKET_SETKEEPALIVE)
381 SocketSetKeepAliveFunction();
383 protected:
384 ~SocketSetKeepAliveFunction() override;
386 // AsyncApiFunction:
387 bool Prepare() override;
388 void Work() override;
390 private:
391 scoped_ptr<api::socket::SetKeepAlive::Params> params_;
394 class SocketSetNoDelayFunction : public SocketAsyncApiFunction {
395 public:
396 DECLARE_EXTENSION_FUNCTION("socket.setNoDelay", SOCKET_SETNODELAY)
398 SocketSetNoDelayFunction();
400 protected:
401 ~SocketSetNoDelayFunction() override;
403 // AsyncApiFunction:
404 bool Prepare() override;
405 void Work() override;
407 private:
408 scoped_ptr<api::socket::SetNoDelay::Params> params_;
411 class SocketGetInfoFunction : public SocketAsyncApiFunction {
412 public:
413 DECLARE_EXTENSION_FUNCTION("socket.getInfo", SOCKET_GETINFO)
415 SocketGetInfoFunction();
417 protected:
418 ~SocketGetInfoFunction() override;
420 // AsyncApiFunction:
421 bool Prepare() override;
422 void Work() override;
424 private:
425 scoped_ptr<api::socket::GetInfo::Params> params_;
428 class SocketGetNetworkListFunction : public AsyncExtensionFunction {
429 public:
430 DECLARE_EXTENSION_FUNCTION("socket.getNetworkList", SOCKET_GETNETWORKLIST)
432 protected:
433 ~SocketGetNetworkListFunction() override {}
434 bool RunAsync() override;
436 private:
437 void GetNetworkListOnFileThread();
438 void HandleGetNetworkListError();
439 void SendResponseOnUIThread(const net::NetworkInterfaceList& interface_list);
442 class SocketJoinGroupFunction : public SocketAsyncApiFunction {
443 public:
444 DECLARE_EXTENSION_FUNCTION("socket.joinGroup", SOCKET_MULTICAST_JOIN_GROUP)
446 SocketJoinGroupFunction();
448 protected:
449 ~SocketJoinGroupFunction() override;
451 // AsyncApiFunction
452 bool Prepare() override;
453 void Work() override;
455 private:
456 scoped_ptr<api::socket::JoinGroup::Params> params_;
459 class SocketLeaveGroupFunction : public SocketAsyncApiFunction {
460 public:
461 DECLARE_EXTENSION_FUNCTION("socket.leaveGroup", SOCKET_MULTICAST_LEAVE_GROUP)
463 SocketLeaveGroupFunction();
465 protected:
466 ~SocketLeaveGroupFunction() override;
468 // AsyncApiFunction
469 bool Prepare() override;
470 void Work() override;
472 private:
473 scoped_ptr<api::socket::LeaveGroup::Params> params_;
476 class SocketSetMulticastTimeToLiveFunction : public SocketAsyncApiFunction {
477 public:
478 DECLARE_EXTENSION_FUNCTION("socket.setMulticastTimeToLive",
479 SOCKET_MULTICAST_SET_TIME_TO_LIVE)
481 SocketSetMulticastTimeToLiveFunction();
483 protected:
484 ~SocketSetMulticastTimeToLiveFunction() override;
486 // AsyncApiFunction
487 bool Prepare() override;
488 void Work() override;
490 private:
491 scoped_ptr<api::socket::SetMulticastTimeToLive::Params> params_;
494 class SocketSetMulticastLoopbackModeFunction : public SocketAsyncApiFunction {
495 public:
496 DECLARE_EXTENSION_FUNCTION("socket.setMulticastLoopbackMode",
497 SOCKET_MULTICAST_SET_LOOPBACK_MODE)
499 SocketSetMulticastLoopbackModeFunction();
501 protected:
502 ~SocketSetMulticastLoopbackModeFunction() override;
504 // AsyncApiFunction
505 bool Prepare() override;
506 void Work() override;
508 private:
509 scoped_ptr<api::socket::SetMulticastLoopbackMode::Params> params_;
512 class SocketGetJoinedGroupsFunction : public SocketAsyncApiFunction {
513 public:
514 DECLARE_EXTENSION_FUNCTION("socket.getJoinedGroups",
515 SOCKET_MULTICAST_GET_JOINED_GROUPS)
517 SocketGetJoinedGroupsFunction();
519 protected:
520 ~SocketGetJoinedGroupsFunction() override;
522 // AsyncApiFunction
523 bool Prepare() override;
524 void Work() override;
526 private:
527 scoped_ptr<api::socket::GetJoinedGroups::Params> params_;
530 class SocketSecureFunction : public SocketAsyncApiFunction {
531 public:
532 DECLARE_EXTENSION_FUNCTION("socket.secure", SOCKET_SECURE);
533 SocketSecureFunction();
535 protected:
536 ~SocketSecureFunction() override;
538 // AsyncApiFunction
539 bool Prepare() override;
540 void AsyncWorkStart() override;
542 private:
543 // Callback from TLSSocket::UpgradeSocketToTLS().
544 void TlsConnectDone(scoped_ptr<TLSSocket> socket, int result);
546 scoped_ptr<api::socket::Secure::Params> params_;
547 scoped_refptr<net::URLRequestContextGetter> url_request_getter_;
549 DISALLOW_COPY_AND_ASSIGN(SocketSecureFunction);
552 } // namespace extensions
554 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_