Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: extensions/browser/api/socket/socket_api.h

Issue 183893041: Move sockets APIs out of src/chrome (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ 5 #ifndef EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ 6 #define EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_
7
8 #include <string>
7 9
8 #include "base/gtest_prod_util.h" 10 #include "base/gtest_prod_util.h"
9 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
10 #include "chrome/browser/extensions/api/api_resource_manager.h" 12 #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/api/async_api_function.h"
13 #include "extensions/browser/extension_function.h" 14 #include "extensions/browser/extension_function.h"
15 #include "extensions/common/api/socket.h"
14 #include "net/base/address_list.h" 16 #include "net/base/address_list.h"
15 #include "net/dns/host_resolver.h" 17 #include "net/dns/host_resolver.h"
16 #include "net/socket/tcp_client_socket.h" 18 #include "net/socket/tcp_client_socket.h"
17 19
18 #include <string>
19
20 namespace content { 20 namespace content {
21 class BrowserContext; 21 class BrowserContext;
22 class ResourceContext; 22 class ResourceContext;
23 } 23 }
24 24
25 namespace net { 25 namespace net {
26 class IOBuffer; 26 class IOBuffer;
27 } 27 }
28 28
29 namespace extensions { 29 namespace extensions {
30 30
31 class Socket; 31 class Socket;
32 32
33 // A simple interface to ApiResourceManager<Socket> or derived class. The goal 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 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 35 // of ApiResourceManager<> depending on the type of socket (old version in
36 // "socket" namespace vs new version in "socket.xxx" namespaces). 36 // "socket" namespace vs new version in "socket.xxx" namespaces).
37 class SocketResourceManagerInterface { 37 class SocketResourceManagerInterface {
38 public: 38 public:
39 virtual ~SocketResourceManagerInterface() {} 39 virtual ~SocketResourceManagerInterface() {}
40 40
41 virtual bool SetBrowserContext(content::BrowserContext* context) = 0; 41 virtual bool SetBrowserContext(content::BrowserContext* context) = 0;
42 virtual int Add(Socket *socket) = 0; 42 virtual int Add(Socket* socket) = 0;
43 virtual Socket* Get(const std::string& extension_id, 43 virtual Socket* Get(const std::string& extension_id, int api_resource_id) = 0;
44 int api_resource_id) = 0; 44 virtual void Remove(const std::string& extension_id, 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( 45 virtual base::hash_set<int>* GetResourceIds(
48 const std::string& extension_id) = 0; 46 const std::string& extension_id) = 0;
49 }; 47 };
50 48
51 // Implementation of SocketResourceManagerInterface using an 49 // Implementation of SocketResourceManagerInterface using an
52 // ApiResourceManager<T> instance (where T derives from Socket). 50 // ApiResourceManager<T> instance (where T derives from Socket).
53 template<typename T> 51 template <typename T>
54 class SocketResourceManager : public SocketResourceManagerInterface { 52 class SocketResourceManager : public SocketResourceManagerInterface {
55 public: 53 public:
56 SocketResourceManager() 54 SocketResourceManager() : manager_(NULL) {}
57 : manager_(NULL) {
58 }
59 55
60 virtual bool SetBrowserContext(content::BrowserContext* context) OVERRIDE { 56 virtual bool SetBrowserContext(content::BrowserContext* context) OVERRIDE {
61 manager_ = ApiResourceManager<T>::Get(context); 57 manager_ = ApiResourceManager<T>::Get(context);
62 DCHECK(manager_) << "There is no socket manager. " 58 DCHECK(manager_)
63 "If this assertion is failing during a test, then it is likely that " 59 << "There is no socket manager. "
64 "TestExtensionSystem is failing to provide an instance of " 60 "If this assertion is failing during a test, then it is likely that "
65 "ApiResourceManager<Socket>."; 61 "TestExtensionSystem is failing to provide an instance of "
62 "ApiResourceManager<Socket>.";
66 return manager_ != NULL; 63 return manager_ != NULL;
67 } 64 }
68 65
69 virtual int Add(Socket *socket) OVERRIDE { 66 virtual int Add(Socket* socket) OVERRIDE {
70 // Note: Cast needed here, because "T" may be a subclass of "Socket". 67 // Note: Cast needed here, because "T" may be a subclass of "Socket".
71 return manager_->Add(static_cast<T*>(socket)); 68 return manager_->Add(static_cast<T*>(socket));
72 } 69 }
73 70
74 virtual Socket* Get(const std::string& extension_id, 71 virtual Socket* Get(const std::string& extension_id,
75 int api_resource_id) OVERRIDE { 72 int api_resource_id) OVERRIDE {
76 return manager_->Get(extension_id, api_resource_id); 73 return manager_->Get(extension_id, api_resource_id);
77 } 74 }
78 75
79 virtual void Remove(const std::string& extension_id, 76 virtual void Remove(const std::string& extension_id,
80 int api_resource_id) OVERRIDE { 77 int api_resource_id) OVERRIDE {
81 manager_->Remove(extension_id, api_resource_id); 78 manager_->Remove(extension_id, api_resource_id);
82 } 79 }
83 80
84 virtual base::hash_set<int>* GetResourceIds( 81 virtual base::hash_set<int>* GetResourceIds(const std::string& extension_id)
85 const std::string& extension_id) OVERRIDE { 82 OVERRIDE {
86 return manager_->GetResourceIds(extension_id); 83 return manager_->GetResourceIds(extension_id);
87 } 84 }
88 85
89 private: 86 private:
90 ApiResourceManager<T>* manager_; 87 ApiResourceManager<T>* manager_;
91 }; 88 };
92 89
93 class SocketAsyncApiFunction : public AsyncApiFunction { 90 class SocketAsyncApiFunction : public AsyncApiFunction {
94 public: 91 public:
95 SocketAsyncApiFunction(); 92 SocketAsyncApiFunction();
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 141
145 protected: 142 protected:
146 virtual ~SocketCreateFunction(); 143 virtual ~SocketCreateFunction();
147 144
148 // AsyncApiFunction: 145 // AsyncApiFunction:
149 virtual bool Prepare() OVERRIDE; 146 virtual bool Prepare() OVERRIDE;
150 virtual void Work() OVERRIDE; 147 virtual void Work() OVERRIDE;
151 148
152 private: 149 private:
153 FRIEND_TEST_ALL_PREFIXES(SocketUnitTest, Create); 150 FRIEND_TEST_ALL_PREFIXES(SocketUnitTest, Create);
154 enum SocketType { 151 enum SocketType { kSocketTypeInvalid = -1, kSocketTypeTCP, kSocketTypeUDP };
155 kSocketTypeInvalid = -1,
156 kSocketTypeTCP,
157 kSocketTypeUDP
158 };
159 152
160 scoped_ptr<api::socket::Create::Params> params_; 153 scoped_ptr<core_api::socket::Create::Params> params_;
161 SocketType socket_type_; 154 SocketType socket_type_;
162 }; 155 };
163 156
164 class SocketDestroyFunction : public SocketAsyncApiFunction { 157 class SocketDestroyFunction : public SocketAsyncApiFunction {
165 public: 158 public:
166 DECLARE_EXTENSION_FUNCTION("socket.destroy", SOCKET_DESTROY) 159 DECLARE_EXTENSION_FUNCTION("socket.destroy", SOCKET_DESTROY)
167 160
168 protected: 161 protected:
169 virtual ~SocketDestroyFunction() {} 162 virtual ~SocketDestroyFunction() {}
170 163
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 SocketListenFunction(); 234 SocketListenFunction();
242 235
243 protected: 236 protected:
244 virtual ~SocketListenFunction(); 237 virtual ~SocketListenFunction();
245 238
246 // AsyncApiFunction: 239 // AsyncApiFunction:
247 virtual bool Prepare() OVERRIDE; 240 virtual bool Prepare() OVERRIDE;
248 virtual void Work() OVERRIDE; 241 virtual void Work() OVERRIDE;
249 242
250 private: 243 private:
251 scoped_ptr<api::socket::Listen::Params> params_; 244 scoped_ptr<core_api::socket::Listen::Params> params_;
252 }; 245 };
253 246
254 class SocketAcceptFunction : public SocketAsyncApiFunction { 247 class SocketAcceptFunction : public SocketAsyncApiFunction {
255 public: 248 public:
256 DECLARE_EXTENSION_FUNCTION("socket.accept", SOCKET_ACCEPT) 249 DECLARE_EXTENSION_FUNCTION("socket.accept", SOCKET_ACCEPT)
257 250
258 SocketAcceptFunction(); 251 SocketAcceptFunction();
259 252
260 protected: 253 protected:
261 virtual ~SocketAcceptFunction(); 254 virtual ~SocketAcceptFunction();
262 255
263 // AsyncApiFunction: 256 // AsyncApiFunction:
264 virtual bool Prepare() OVERRIDE; 257 virtual bool Prepare() OVERRIDE;
265 virtual void AsyncWorkStart() OVERRIDE; 258 virtual void AsyncWorkStart() OVERRIDE;
266 259
267 private: 260 private:
268 void OnAccept(int result_code, net::TCPClientSocket *socket); 261 void OnAccept(int result_code, net::TCPClientSocket* socket);
269 scoped_ptr<api::socket::Accept::Params> params_; 262 scoped_ptr<core_api::socket::Accept::Params> params_;
270 }; 263 };
271 264
272 class SocketReadFunction : public SocketAsyncApiFunction { 265 class SocketReadFunction : public SocketAsyncApiFunction {
273 public: 266 public:
274 DECLARE_EXTENSION_FUNCTION("socket.read", SOCKET_READ) 267 DECLARE_EXTENSION_FUNCTION("socket.read", SOCKET_READ)
275 268
276 SocketReadFunction(); 269 SocketReadFunction();
277 270
278 protected: 271 protected:
279 virtual ~SocketReadFunction(); 272 virtual ~SocketReadFunction();
280 273
281 // AsyncApiFunction: 274 // AsyncApiFunction:
282 virtual bool Prepare() OVERRIDE; 275 virtual bool Prepare() OVERRIDE;
283 virtual void AsyncWorkStart() OVERRIDE; 276 virtual void AsyncWorkStart() OVERRIDE;
284 void OnCompleted(int result, scoped_refptr<net::IOBuffer> io_buffer); 277 void OnCompleted(int result, scoped_refptr<net::IOBuffer> io_buffer);
285 278
286 private: 279 private:
287 scoped_ptr<api::socket::Read::Params> params_; 280 scoped_ptr<core_api::socket::Read::Params> params_;
288 }; 281 };
289 282
290 class SocketWriteFunction : public SocketAsyncApiFunction { 283 class SocketWriteFunction : public SocketAsyncApiFunction {
291 public: 284 public:
292 DECLARE_EXTENSION_FUNCTION("socket.write", SOCKET_WRITE) 285 DECLARE_EXTENSION_FUNCTION("socket.write", SOCKET_WRITE)
293 286
294 SocketWriteFunction(); 287 SocketWriteFunction();
295 288
296 protected: 289 protected:
297 virtual ~SocketWriteFunction(); 290 virtual ~SocketWriteFunction();
(...skipping 20 matching lines...) Expand all
318 311
319 // AsyncApiFunction 312 // AsyncApiFunction
320 virtual bool Prepare() OVERRIDE; 313 virtual bool Prepare() OVERRIDE;
321 virtual void AsyncWorkStart() OVERRIDE; 314 virtual void AsyncWorkStart() OVERRIDE;
322 void OnCompleted(int result, 315 void OnCompleted(int result,
323 scoped_refptr<net::IOBuffer> io_buffer, 316 scoped_refptr<net::IOBuffer> io_buffer,
324 const std::string& address, 317 const std::string& address,
325 int port); 318 int port);
326 319
327 private: 320 private:
328 scoped_ptr<api::socket::RecvFrom::Params> params_; 321 scoped_ptr<core_api::socket::RecvFrom::Params> params_;
329 }; 322 };
330 323
331 class SocketSendToFunction : public SocketExtensionWithDnsLookupFunction { 324 class SocketSendToFunction : public SocketExtensionWithDnsLookupFunction {
332 public: 325 public:
333 DECLARE_EXTENSION_FUNCTION("socket.sendTo", SOCKET_SENDTO) 326 DECLARE_EXTENSION_FUNCTION("socket.sendTo", SOCKET_SENDTO)
334 327
335 SocketSendToFunction(); 328 SocketSendToFunction();
336 329
337 protected: 330 protected:
338 virtual ~SocketSendToFunction(); 331 virtual ~SocketSendToFunction();
(...skipping 24 matching lines...) Expand all
363 SocketSetKeepAliveFunction(); 356 SocketSetKeepAliveFunction();
364 357
365 protected: 358 protected:
366 virtual ~SocketSetKeepAliveFunction(); 359 virtual ~SocketSetKeepAliveFunction();
367 360
368 // AsyncApiFunction: 361 // AsyncApiFunction:
369 virtual bool Prepare() OVERRIDE; 362 virtual bool Prepare() OVERRIDE;
370 virtual void Work() OVERRIDE; 363 virtual void Work() OVERRIDE;
371 364
372 private: 365 private:
373 scoped_ptr<api::socket::SetKeepAlive::Params> params_; 366 scoped_ptr<core_api::socket::SetKeepAlive::Params> params_;
374 }; 367 };
375 368
376 class SocketSetNoDelayFunction : public SocketAsyncApiFunction { 369 class SocketSetNoDelayFunction : public SocketAsyncApiFunction {
377 public: 370 public:
378 DECLARE_EXTENSION_FUNCTION("socket.setNoDelay", SOCKET_SETNODELAY) 371 DECLARE_EXTENSION_FUNCTION("socket.setNoDelay", SOCKET_SETNODELAY)
379 372
380 SocketSetNoDelayFunction(); 373 SocketSetNoDelayFunction();
381 374
382 protected: 375 protected:
383 virtual ~SocketSetNoDelayFunction(); 376 virtual ~SocketSetNoDelayFunction();
384 377
385 // AsyncApiFunction: 378 // AsyncApiFunction:
386 virtual bool Prepare() OVERRIDE; 379 virtual bool Prepare() OVERRIDE;
387 virtual void Work() OVERRIDE; 380 virtual void Work() OVERRIDE;
388 381
389 private: 382 private:
390 scoped_ptr<api::socket::SetNoDelay::Params> params_; 383 scoped_ptr<core_api::socket::SetNoDelay::Params> params_;
391 }; 384 };
392 385
393 class SocketGetInfoFunction : public SocketAsyncApiFunction { 386 class SocketGetInfoFunction : public SocketAsyncApiFunction {
394 public: 387 public:
395 DECLARE_EXTENSION_FUNCTION("socket.getInfo", SOCKET_GETINFO) 388 DECLARE_EXTENSION_FUNCTION("socket.getInfo", SOCKET_GETINFO)
396 389
397 SocketGetInfoFunction(); 390 SocketGetInfoFunction();
398 391
399 protected: 392 protected:
400 virtual ~SocketGetInfoFunction(); 393 virtual ~SocketGetInfoFunction();
401 394
402 // AsyncApiFunction: 395 // AsyncApiFunction:
403 virtual bool Prepare() OVERRIDE; 396 virtual bool Prepare() OVERRIDE;
404 virtual void Work() OVERRIDE; 397 virtual void Work() OVERRIDE;
405 398
406 private: 399 private:
407 scoped_ptr<api::socket::GetInfo::Params> params_; 400 scoped_ptr<core_api::socket::GetInfo::Params> params_;
408 }; 401 };
409 402
410 class SocketGetNetworkListFunction : public AsyncExtensionFunction { 403 class SocketGetNetworkListFunction : public AsyncExtensionFunction {
411 public: 404 public:
412 DECLARE_EXTENSION_FUNCTION("socket.getNetworkList", SOCKET_GETNETWORKLIST) 405 DECLARE_EXTENSION_FUNCTION("socket.getNetworkList", SOCKET_GETNETWORKLIST)
413 406
414 protected: 407 protected:
415 virtual ~SocketGetNetworkListFunction() {} 408 virtual ~SocketGetNetworkListFunction() {}
416 virtual bool RunImpl() OVERRIDE; 409 virtual bool RunImpl() OVERRIDE;
417 410
(...skipping 10 matching lines...) Expand all
428 SocketJoinGroupFunction(); 421 SocketJoinGroupFunction();
429 422
430 protected: 423 protected:
431 virtual ~SocketJoinGroupFunction(); 424 virtual ~SocketJoinGroupFunction();
432 425
433 // AsyncApiFunction 426 // AsyncApiFunction
434 virtual bool Prepare() OVERRIDE; 427 virtual bool Prepare() OVERRIDE;
435 virtual void Work() OVERRIDE; 428 virtual void Work() OVERRIDE;
436 429
437 private: 430 private:
438 scoped_ptr<api::socket::JoinGroup::Params> params_; 431 scoped_ptr<core_api::socket::JoinGroup::Params> params_;
439 }; 432 };
440 433
441 class SocketLeaveGroupFunction : public SocketAsyncApiFunction { 434 class SocketLeaveGroupFunction : public SocketAsyncApiFunction {
442 public: 435 public:
443 DECLARE_EXTENSION_FUNCTION("socket.leaveGroup", SOCKET_MULTICAST_LEAVE_GROUP) 436 DECLARE_EXTENSION_FUNCTION("socket.leaveGroup", SOCKET_MULTICAST_LEAVE_GROUP)
444 437
445 SocketLeaveGroupFunction(); 438 SocketLeaveGroupFunction();
446 439
447 protected: 440 protected:
448 virtual ~SocketLeaveGroupFunction(); 441 virtual ~SocketLeaveGroupFunction();
449 442
450 // AsyncApiFunction 443 // AsyncApiFunction
451 virtual bool Prepare() OVERRIDE; 444 virtual bool Prepare() OVERRIDE;
452 virtual void Work() OVERRIDE; 445 virtual void Work() OVERRIDE;
453 446
454 private: 447 private:
455 scoped_ptr<api::socket::LeaveGroup::Params> params_; 448 scoped_ptr<core_api::socket::LeaveGroup::Params> params_;
456 }; 449 };
457 450
458 class SocketSetMulticastTimeToLiveFunction : public SocketAsyncApiFunction { 451 class SocketSetMulticastTimeToLiveFunction : public SocketAsyncApiFunction {
459 public: 452 public:
460 DECLARE_EXTENSION_FUNCTION("socket.setMulticastTimeToLive", 453 DECLARE_EXTENSION_FUNCTION("socket.setMulticastTimeToLive",
461 SOCKET_MULTICAST_SET_TIME_TO_LIVE) 454 SOCKET_MULTICAST_SET_TIME_TO_LIVE)
462 455
463 SocketSetMulticastTimeToLiveFunction(); 456 SocketSetMulticastTimeToLiveFunction();
464 457
465 protected: 458 protected:
466 virtual ~SocketSetMulticastTimeToLiveFunction(); 459 virtual ~SocketSetMulticastTimeToLiveFunction();
467 460
468 // AsyncApiFunction 461 // AsyncApiFunction
469 virtual bool Prepare() OVERRIDE; 462 virtual bool Prepare() OVERRIDE;
470 virtual void Work() OVERRIDE; 463 virtual void Work() OVERRIDE;
471 464
472 private: 465 private:
473 scoped_ptr<api::socket::SetMulticastTimeToLive::Params> params_; 466 scoped_ptr<core_api::socket::SetMulticastTimeToLive::Params> params_;
474 }; 467 };
475 468
476 class SocketSetMulticastLoopbackModeFunction : public SocketAsyncApiFunction { 469 class SocketSetMulticastLoopbackModeFunction : public SocketAsyncApiFunction {
477 public: 470 public:
478 DECLARE_EXTENSION_FUNCTION("socket.setMulticastLoopbackMode", 471 DECLARE_EXTENSION_FUNCTION("socket.setMulticastLoopbackMode",
479 SOCKET_MULTICAST_SET_LOOPBACK_MODE) 472 SOCKET_MULTICAST_SET_LOOPBACK_MODE)
480 473
481 SocketSetMulticastLoopbackModeFunction(); 474 SocketSetMulticastLoopbackModeFunction();
482 475
483 protected: 476 protected:
484 virtual ~SocketSetMulticastLoopbackModeFunction(); 477 virtual ~SocketSetMulticastLoopbackModeFunction();
485 478
486 // AsyncApiFunction 479 // AsyncApiFunction
487 virtual bool Prepare() OVERRIDE; 480 virtual bool Prepare() OVERRIDE;
488 virtual void Work() OVERRIDE; 481 virtual void Work() OVERRIDE;
489 482
490 private: 483 private:
491 scoped_ptr<api::socket::SetMulticastLoopbackMode::Params> params_; 484 scoped_ptr<core_api::socket::SetMulticastLoopbackMode::Params> params_;
492 }; 485 };
493 486
494 class SocketGetJoinedGroupsFunction : public SocketAsyncApiFunction { 487 class SocketGetJoinedGroupsFunction : public SocketAsyncApiFunction {
495 public: 488 public:
496 DECLARE_EXTENSION_FUNCTION("socket.getJoinedGroups", 489 DECLARE_EXTENSION_FUNCTION("socket.getJoinedGroups",
497 SOCKET_MULTICAST_GET_JOINED_GROUPS) 490 SOCKET_MULTICAST_GET_JOINED_GROUPS)
498 491
499 SocketGetJoinedGroupsFunction(); 492 SocketGetJoinedGroupsFunction();
500 493
501 protected: 494 protected:
502 virtual ~SocketGetJoinedGroupsFunction(); 495 virtual ~SocketGetJoinedGroupsFunction();
503 496
504 // AsyncApiFunction 497 // AsyncApiFunction
505 virtual bool Prepare() OVERRIDE; 498 virtual bool Prepare() OVERRIDE;
506 virtual void Work() OVERRIDE; 499 virtual void Work() OVERRIDE;
507 500
508 private: 501 private:
509 scoped_ptr<api::socket::GetJoinedGroups::Params> params_; 502 scoped_ptr<core_api::socket::GetJoinedGroups::Params> params_;
510 }; 503 };
511 } // namespace extensions 504 } // namespace extensions
512 505
513 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ 506 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698