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