OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ | |
7 | |
8 #include "base/gtest_prod_util.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "chrome/common/extensions/api/socket.h" | |
11 #include "extensions/browser/api/api_resource_manager.h" | |
12 #include "extensions/browser/api/async_api_function.h" | |
13 #include "extensions/browser/extension_function.h" | |
14 #include "net/base/address_list.h" | |
15 #include "net/dns/host_resolver.h" | |
16 #include "net/socket/tcp_client_socket.h" | |
17 | |
18 #include <string> | |
19 | |
20 namespace content { | |
21 class BrowserContext; | |
22 class ResourceContext; | |
23 } | |
24 | |
25 namespace net { | |
26 class IOBuffer; | |
27 } | |
28 | |
29 namespace extensions { | |
30 | |
31 class Socket; | |
32 | |
33 // A simple interface to ApiResourceManager<Socket> or derived class. The goal | |
34 // of this interface is to allow Socket API functions to use distinct instances | |
35 // of ApiResourceManager<> depending on the type of socket (old version in | |
36 // "socket" namespace vs new version in "socket.xxx" namespaces). | |
37 class SocketResourceManagerInterface { | |
38 public: | |
39 virtual ~SocketResourceManagerInterface() {} | |
40 | |
41 virtual bool SetBrowserContext(content::BrowserContext* context) = 0; | |
42 virtual int Add(Socket* socket) = 0; | |
43 virtual Socket* Get(const std::string& extension_id, int api_resource_id) = 0; | |
44 virtual void Remove(const std::string& extension_id, int api_resource_id) = 0; | |
45 virtual base::hash_set<int>* GetResourceIds( | |
46 const std::string& extension_id) = 0; | |
47 }; | |
48 | |
49 // Implementation of SocketResourceManagerInterface using an | |
50 // ApiResourceManager<T> instance (where T derives from Socket). | |
51 template <typename T> | |
52 class SocketResourceManager : public SocketResourceManagerInterface { | |
53 public: | |
54 SocketResourceManager() : manager_(NULL) {} | |
55 | |
56 virtual bool SetBrowserContext(content::BrowserContext* context) OVERRIDE { | |
57 manager_ = ApiResourceManager<T>::Get(context); | |
58 DCHECK(manager_) | |
59 << "There is no socket manager. " | |
60 "If this assertion is failing during a test, then it is likely that " | |
61 "TestExtensionSystem is failing to provide an instance of " | |
62 "ApiResourceManager<Socket>."; | |
63 return manager_ != NULL; | |
64 } | |
65 | |
66 virtual int Add(Socket* socket) OVERRIDE { | |
67 // Note: Cast needed here, because "T" may be a subclass of "Socket". | |
68 return manager_->Add(static_cast<T*>(socket)); | |
69 } | |
70 | |
71 virtual Socket* Get(const std::string& extension_id, | |
72 int api_resource_id) OVERRIDE { | |
73 return manager_->Get(extension_id, api_resource_id); | |
74 } | |
75 | |
76 virtual void Remove(const std::string& extension_id, | |
77 int api_resource_id) OVERRIDE { | |
78 manager_->Remove(extension_id, api_resource_id); | |
79 } | |
80 | |
81 virtual base::hash_set<int>* GetResourceIds(const std::string& extension_id) | |
82 OVERRIDE { | |
83 return manager_->GetResourceIds(extension_id); | |
84 } | |
85 | |
86 private: | |
87 ApiResourceManager<T>* manager_; | |
88 }; | |
89 | |
90 class SocketAsyncApiFunction : public AsyncApiFunction { | |
91 public: | |
92 SocketAsyncApiFunction(); | |
93 | |
94 protected: | |
95 virtual ~SocketAsyncApiFunction(); | |
96 | |
97 // AsyncApiFunction: | |
98 virtual bool PrePrepare() OVERRIDE; | |
99 virtual bool Respond() OVERRIDE; | |
100 | |
101 virtual scoped_ptr<SocketResourceManagerInterface> | |
102 CreateSocketResourceManager(); | |
103 | |
104 int AddSocket(Socket* socket); | |
105 Socket* GetSocket(int api_resource_id); | |
106 void RemoveSocket(int api_resource_id); | |
107 base::hash_set<int>* GetSocketIds(); | |
108 | |
109 private: | |
110 scoped_ptr<SocketResourceManagerInterface> manager_; | |
111 }; | |
112 | |
113 class SocketExtensionWithDnsLookupFunction : public SocketAsyncApiFunction { | |
114 protected: | |
115 SocketExtensionWithDnsLookupFunction(); | |
116 virtual ~SocketExtensionWithDnsLookupFunction(); | |
117 | |
118 // AsyncApiFunction: | |
119 virtual bool PrePrepare() OVERRIDE; | |
120 | |
121 void StartDnsLookup(const std::string& hostname); | |
122 virtual void AfterDnsLookup(int lookup_result) = 0; | |
123 | |
124 std::string resolved_address_; | |
125 | |
126 private: | |
127 void OnDnsLookup(int resolve_result); | |
128 | |
129 // Weak pointer to the resource context. | |
130 content::ResourceContext* resource_context_; | |
131 | |
132 scoped_ptr<net::HostResolver::RequestHandle> request_handle_; | |
133 scoped_ptr<net::AddressList> addresses_; | |
134 }; | |
135 | |
136 class SocketCreateFunction : public SocketAsyncApiFunction { | |
137 public: | |
138 DECLARE_EXTENSION_FUNCTION("socket.create", SOCKET_CREATE) | |
139 | |
140 SocketCreateFunction(); | |
141 | |
142 protected: | |
143 virtual ~SocketCreateFunction(); | |
144 | |
145 // AsyncApiFunction: | |
146 virtual bool Prepare() OVERRIDE; | |
147 virtual void Work() OVERRIDE; | |
148 | |
149 private: | |
150 FRIEND_TEST_ALL_PREFIXES(SocketUnitTest, Create); | |
151 enum SocketType { kSocketTypeInvalid = -1, kSocketTypeTCP, kSocketTypeUDP }; | |
152 | |
153 scoped_ptr<api::socket::Create::Params> params_; | |
154 SocketType socket_type_; | |
155 }; | |
156 | |
157 class SocketDestroyFunction : public SocketAsyncApiFunction { | |
158 public: | |
159 DECLARE_EXTENSION_FUNCTION("socket.destroy", SOCKET_DESTROY) | |
160 | |
161 protected: | |
162 virtual ~SocketDestroyFunction() {} | |
163 | |
164 // AsyncApiFunction: | |
165 virtual bool Prepare() OVERRIDE; | |
166 virtual void Work() OVERRIDE; | |
167 | |
168 private: | |
169 int socket_id_; | |
170 }; | |
171 | |
172 class SocketConnectFunction : public SocketExtensionWithDnsLookupFunction { | |
173 public: | |
174 DECLARE_EXTENSION_FUNCTION("socket.connect", SOCKET_CONNECT) | |
175 | |
176 SocketConnectFunction(); | |
177 | |
178 protected: | |
179 virtual ~SocketConnectFunction(); | |
180 | |
181 // AsyncApiFunction: | |
182 virtual bool Prepare() OVERRIDE; | |
183 virtual void AsyncWorkStart() OVERRIDE; | |
184 | |
185 // SocketExtensionWithDnsLookupFunction: | |
186 virtual void AfterDnsLookup(int lookup_result) OVERRIDE; | |
187 | |
188 private: | |
189 void StartConnect(); | |
190 void OnConnect(int result); | |
191 | |
192 int socket_id_; | |
193 std::string hostname_; | |
194 int port_; | |
195 Socket* socket_; | |
196 }; | |
197 | |
198 class SocketDisconnectFunction : public SocketAsyncApiFunction { | |
199 public: | |
200 DECLARE_EXTENSION_FUNCTION("socket.disconnect", SOCKET_DISCONNECT) | |
201 | |
202 protected: | |
203 virtual ~SocketDisconnectFunction() {} | |
204 | |
205 // AsyncApiFunction: | |
206 virtual bool Prepare() OVERRIDE; | |
207 virtual void Work() OVERRIDE; | |
208 | |
209 private: | |
210 int socket_id_; | |
211 }; | |
212 | |
213 class SocketBindFunction : public SocketAsyncApiFunction { | |
214 public: | |
215 DECLARE_EXTENSION_FUNCTION("socket.bind", SOCKET_BIND) | |
216 | |
217 protected: | |
218 virtual ~SocketBindFunction() {} | |
219 | |
220 // AsyncApiFunction: | |
221 virtual bool Prepare() OVERRIDE; | |
222 virtual void Work() OVERRIDE; | |
223 | |
224 private: | |
225 int socket_id_; | |
226 std::string address_; | |
227 int port_; | |
228 }; | |
229 | |
230 class SocketListenFunction : public SocketAsyncApiFunction { | |
231 public: | |
232 DECLARE_EXTENSION_FUNCTION("socket.listen", SOCKET_LISTEN) | |
233 | |
234 SocketListenFunction(); | |
235 | |
236 protected: | |
237 virtual ~SocketListenFunction(); | |
238 | |
239 // AsyncApiFunction: | |
240 virtual bool Prepare() OVERRIDE; | |
241 virtual void Work() OVERRIDE; | |
242 | |
243 private: | |
244 scoped_ptr<api::socket::Listen::Params> params_; | |
245 }; | |
246 | |
247 class SocketAcceptFunction : public SocketAsyncApiFunction { | |
248 public: | |
249 DECLARE_EXTENSION_FUNCTION("socket.accept", SOCKET_ACCEPT) | |
250 | |
251 SocketAcceptFunction(); | |
252 | |
253 protected: | |
254 virtual ~SocketAcceptFunction(); | |
255 | |
256 // AsyncApiFunction: | |
257 virtual bool Prepare() OVERRIDE; | |
258 virtual void AsyncWorkStart() OVERRIDE; | |
259 | |
260 private: | |
261 void OnAccept(int result_code, net::TCPClientSocket* socket); | |
262 scoped_ptr<api::socket::Accept::Params> params_; | |
263 }; | |
264 | |
265 class SocketReadFunction : public SocketAsyncApiFunction { | |
266 public: | |
267 DECLARE_EXTENSION_FUNCTION("socket.read", SOCKET_READ) | |
268 | |
269 SocketReadFunction(); | |
270 | |
271 protected: | |
272 virtual ~SocketReadFunction(); | |
273 | |
274 // AsyncApiFunction: | |
275 virtual bool Prepare() OVERRIDE; | |
276 virtual void AsyncWorkStart() OVERRIDE; | |
277 void OnCompleted(int result, scoped_refptr<net::IOBuffer> io_buffer); | |
278 | |
279 private: | |
280 scoped_ptr<api::socket::Read::Params> params_; | |
281 }; | |
282 | |
283 class SocketWriteFunction : public SocketAsyncApiFunction { | |
284 public: | |
285 DECLARE_EXTENSION_FUNCTION("socket.write", SOCKET_WRITE) | |
286 | |
287 SocketWriteFunction(); | |
288 | |
289 protected: | |
290 virtual ~SocketWriteFunction(); | |
291 | |
292 // AsyncApiFunction: | |
293 virtual bool Prepare() OVERRIDE; | |
294 virtual void AsyncWorkStart() OVERRIDE; | |
295 void OnCompleted(int result); | |
296 | |
297 private: | |
298 int socket_id_; | |
299 scoped_refptr<net::IOBuffer> io_buffer_; | |
300 size_t io_buffer_size_; | |
301 }; | |
302 | |
303 class SocketRecvFromFunction : public SocketAsyncApiFunction { | |
304 public: | |
305 DECLARE_EXTENSION_FUNCTION("socket.recvFrom", SOCKET_RECVFROM) | |
306 | |
307 SocketRecvFromFunction(); | |
308 | |
309 protected: | |
310 virtual ~SocketRecvFromFunction(); | |
311 | |
312 // AsyncApiFunction | |
313 virtual bool Prepare() OVERRIDE; | |
314 virtual void AsyncWorkStart() OVERRIDE; | |
315 void OnCompleted(int result, | |
316 scoped_refptr<net::IOBuffer> io_buffer, | |
317 const std::string& address, | |
318 int port); | |
319 | |
320 private: | |
321 scoped_ptr<api::socket::RecvFrom::Params> params_; | |
322 }; | |
323 | |
324 class SocketSendToFunction : public SocketExtensionWithDnsLookupFunction { | |
325 public: | |
326 DECLARE_EXTENSION_FUNCTION("socket.sendTo", SOCKET_SENDTO) | |
327 | |
328 SocketSendToFunction(); | |
329 | |
330 protected: | |
331 virtual ~SocketSendToFunction(); | |
332 | |
333 // AsyncApiFunction: | |
334 virtual bool Prepare() OVERRIDE; | |
335 virtual void AsyncWorkStart() OVERRIDE; | |
336 void OnCompleted(int result); | |
337 | |
338 // SocketExtensionWithDnsLookupFunction: | |
339 virtual void AfterDnsLookup(int lookup_result) OVERRIDE; | |
340 | |
341 private: | |
342 void StartSendTo(); | |
343 | |
344 int socket_id_; | |
345 scoped_refptr<net::IOBuffer> io_buffer_; | |
346 size_t io_buffer_size_; | |
347 std::string hostname_; | |
348 int port_; | |
349 Socket* socket_; | |
350 }; | |
351 | |
352 class SocketSetKeepAliveFunction : public SocketAsyncApiFunction { | |
353 public: | |
354 DECLARE_EXTENSION_FUNCTION("socket.setKeepAlive", SOCKET_SETKEEPALIVE) | |
355 | |
356 SocketSetKeepAliveFunction(); | |
357 | |
358 protected: | |
359 virtual ~SocketSetKeepAliveFunction(); | |
360 | |
361 // AsyncApiFunction: | |
362 virtual bool Prepare() OVERRIDE; | |
363 virtual void Work() OVERRIDE; | |
364 | |
365 private: | |
366 scoped_ptr<api::socket::SetKeepAlive::Params> params_; | |
367 }; | |
368 | |
369 class SocketSetNoDelayFunction : public SocketAsyncApiFunction { | |
370 public: | |
371 DECLARE_EXTENSION_FUNCTION("socket.setNoDelay", SOCKET_SETNODELAY) | |
372 | |
373 SocketSetNoDelayFunction(); | |
374 | |
375 protected: | |
376 virtual ~SocketSetNoDelayFunction(); | |
377 | |
378 // AsyncApiFunction: | |
379 virtual bool Prepare() OVERRIDE; | |
380 virtual void Work() OVERRIDE; | |
381 | |
382 private: | |
383 scoped_ptr<api::socket::SetNoDelay::Params> params_; | |
384 }; | |
385 | |
386 class SocketGetInfoFunction : public SocketAsyncApiFunction { | |
387 public: | |
388 DECLARE_EXTENSION_FUNCTION("socket.getInfo", SOCKET_GETINFO) | |
389 | |
390 SocketGetInfoFunction(); | |
391 | |
392 protected: | |
393 virtual ~SocketGetInfoFunction(); | |
394 | |
395 // AsyncApiFunction: | |
396 virtual bool Prepare() OVERRIDE; | |
397 virtual void Work() OVERRIDE; | |
398 | |
399 private: | |
400 scoped_ptr<api::socket::GetInfo::Params> params_; | |
401 }; | |
402 | |
403 class SocketGetNetworkListFunction : public AsyncExtensionFunction { | |
404 public: | |
405 DECLARE_EXTENSION_FUNCTION("socket.getNetworkList", SOCKET_GETNETWORKLIST) | |
406 | |
407 protected: | |
408 virtual ~SocketGetNetworkListFunction() {} | |
409 virtual bool RunImpl() OVERRIDE; | |
410 | |
411 private: | |
412 void GetNetworkListOnFileThread(); | |
413 void HandleGetNetworkListError(); | |
414 void SendResponseOnUIThread(const net::NetworkInterfaceList& interface_list); | |
415 }; | |
416 | |
417 class SocketJoinGroupFunction : public SocketAsyncApiFunction { | |
418 public: | |
419 DECLARE_EXTENSION_FUNCTION("socket.joinGroup", SOCKET_MULTICAST_JOIN_GROUP) | |
420 | |
421 SocketJoinGroupFunction(); | |
422 | |
423 protected: | |
424 virtual ~SocketJoinGroupFunction(); | |
425 | |
426 // AsyncApiFunction | |
427 virtual bool Prepare() OVERRIDE; | |
428 virtual void Work() OVERRIDE; | |
429 | |
430 private: | |
431 scoped_ptr<api::socket::JoinGroup::Params> params_; | |
432 }; | |
433 | |
434 class SocketLeaveGroupFunction : public SocketAsyncApiFunction { | |
435 public: | |
436 DECLARE_EXTENSION_FUNCTION("socket.leaveGroup", SOCKET_MULTICAST_LEAVE_GROUP) | |
437 | |
438 SocketLeaveGroupFunction(); | |
439 | |
440 protected: | |
441 virtual ~SocketLeaveGroupFunction(); | |
442 | |
443 // AsyncApiFunction | |
444 virtual bool Prepare() OVERRIDE; | |
445 virtual void Work() OVERRIDE; | |
446 | |
447 private: | |
448 scoped_ptr<api::socket::LeaveGroup::Params> params_; | |
449 }; | |
450 | |
451 class SocketSetMulticastTimeToLiveFunction : public SocketAsyncApiFunction { | |
452 public: | |
453 DECLARE_EXTENSION_FUNCTION("socket.setMulticastTimeToLive", | |
454 SOCKET_MULTICAST_SET_TIME_TO_LIVE) | |
455 | |
456 SocketSetMulticastTimeToLiveFunction(); | |
457 | |
458 protected: | |
459 virtual ~SocketSetMulticastTimeToLiveFunction(); | |
460 | |
461 // AsyncApiFunction | |
462 virtual bool Prepare() OVERRIDE; | |
463 virtual void Work() OVERRIDE; | |
464 | |
465 private: | |
466 scoped_ptr<api::socket::SetMulticastTimeToLive::Params> params_; | |
467 }; | |
468 | |
469 class SocketSetMulticastLoopbackModeFunction : public SocketAsyncApiFunction { | |
470 public: | |
471 DECLARE_EXTENSION_FUNCTION("socket.setMulticastLoopbackMode", | |
472 SOCKET_MULTICAST_SET_LOOPBACK_MODE) | |
473 | |
474 SocketSetMulticastLoopbackModeFunction(); | |
475 | |
476 protected: | |
477 virtual ~SocketSetMulticastLoopbackModeFunction(); | |
478 | |
479 // AsyncApiFunction | |
480 virtual bool Prepare() OVERRIDE; | |
481 virtual void Work() OVERRIDE; | |
482 | |
483 private: | |
484 scoped_ptr<api::socket::SetMulticastLoopbackMode::Params> params_; | |
485 }; | |
486 | |
487 class SocketGetJoinedGroupsFunction : public SocketAsyncApiFunction { | |
488 public: | |
489 DECLARE_EXTENSION_FUNCTION("socket.getJoinedGroups", | |
490 SOCKET_MULTICAST_GET_JOINED_GROUPS) | |
491 | |
492 SocketGetJoinedGroupsFunction(); | |
493 | |
494 protected: | |
495 virtual ~SocketGetJoinedGroupsFunction(); | |
496 | |
497 // AsyncApiFunction | |
498 virtual bool Prepare() OVERRIDE; | |
499 virtual void Work() OVERRIDE; | |
500 | |
501 private: | |
502 scoped_ptr<api::socket::GetJoinedGroups::Params> params_; | |
503 }; | |
504 } // namespace extensions | |
505 | |
506 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ | |
OLD | NEW |