Chromium Code Reviews| OLD | NEW | 
|---|---|
| 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 CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ | 
| 6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ | 
| 7 | 7 | 
| 8 #include "base/gtest_prod_util.h" | 8 #include "base/gtest_prod_util.h" | 
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" | 
| 10 #include "chrome/browser/extensions/api/api_function.h" | 10 #include "chrome/browser/extensions/api/api_function.h" | 
| 11 #include "chrome/browser/extensions/api/api_resource_manager.h" | 11 #include "chrome/browser/extensions/api/api_resource_manager.h" | 
| 12 #include "chrome/common/extensions/api/socket.h" | 12 #include "chrome/common/extensions/api/socket.h" | 
| 13 #include "extensions/browser/extension_function.h" | 13 #include "extensions/browser/extension_function.h" | 
| 14 #include "net/base/address_list.h" | 14 #include "net/base/address_list.h" | 
| 15 #include "net/dns/host_resolver.h" | 15 #include "net/dns/host_resolver.h" | 
| 16 #include "net/socket/tcp_client_socket.h" | 16 #include "net/socket/tcp_client_socket.h" | 
| 17 | 17 | 
| 18 #include <string> | 18 #include <string> | 
| 19 | 19 | 
| 20 class IOThread; | 20 class IOThread; | 
| 21 | 21 | 
| 22 namespace net { | 22 namespace net { | 
| 23 class IOBuffer; | 23 class IOBuffer; | 
| 24 class URLRequestContextGetter; | |
| 25 class SSLClientSocket; | |
| 24 } | 26 } | 
| 25 | 27 | 
| 26 namespace extensions { | 28 namespace extensions { | 
| 27 | 29 class TLSSocket; | 
| 28 class Socket; | 30 class Socket; | 
| 29 | 31 | 
| 30 // A simple interface to ApiResourceManager<Socket> or derived class. The goal | 32 // A simple interface to ApiResourceManager<Socket> or derived class. The goal | 
| 31 // of this interface is to allow Socket API functions to use distinct instances | 33 // of this interface is to allow Socket API functions to use distinct instances | 
| 32 // of ApiResourceManager<> depending on the type of socket (old version in | 34 // of ApiResourceManager<> depending on the type of socket (old version in | 
| 33 // "socket" namespace vs new version in "socket.xxx" namespaces). | 35 // "socket" namespace vs new version in "socket.xxx" namespaces). | 
| 34 class SocketResourceManagerInterface { | 36 class SocketResourceManagerInterface { | 
| 35 public: | 37 public: | 
| 36 virtual ~SocketResourceManagerInterface() {} | 38 virtual ~SocketResourceManagerInterface() {} | 
| 37 | 39 | 
| 38 virtual bool SetProfile(Profile* profile) = 0; | 40 virtual bool SetProfile(Profile* profile) = 0; | 
| 39 virtual int Add(Socket *socket) = 0; | 41 virtual int Add(Socket *socket) = 0; | 
| 40 virtual Socket* Get(const std::string& extension_id, | 42 virtual Socket* Get(const std::string& extension_id, | 
| 41 int api_resource_id) = 0; | 43 int api_resource_id) = 0; | 
| 42 virtual void Remove(const std::string& extension_id, | 44 virtual void Remove(const std::string& extension_id, | 
| 43 int api_resource_id) = 0; | 45 int api_resource_id) = 0; | 
| 46 virtual void Set(const std::string& extension_id, | |
| 47 int api_resource_id, | |
| 48 Socket* socket) = 0; | |
| 44 virtual base::hash_set<int>* GetResourceIds( | 49 virtual base::hash_set<int>* GetResourceIds( | 
| 45 const std::string& extension_id) = 0; | 50 const std::string& extension_id) = 0; | 
| 46 }; | 51 }; | 
| 47 | 52 | 
| 48 // Implementation of SocketResourceManagerInterface using an | 53 // Implementation of SocketResourceManagerInterface using an | 
| 49 // ApiResourceManager<T> instance (where T derives from Socket). | 54 // ApiResourceManager<T> instance (where T derives from Socket). | 
| 50 template<typename T> | 55 template<typename T> | 
| 51 class SocketResourceManager : public SocketResourceManagerInterface { | 56 class SocketResourceManager : public SocketResourceManagerInterface { | 
| 52 public: | 57 public: | 
| 53 SocketResourceManager() | 58 SocketResourceManager() | 
| (...skipping 12 matching lines...) Expand all Loading... | |
| 66 virtual int Add(Socket *socket) OVERRIDE { | 71 virtual int Add(Socket *socket) OVERRIDE { | 
| 67 // Note: Cast needed here, because "T" may be a subclass of "Socket". | 72 // Note: Cast needed here, because "T" may be a subclass of "Socket". | 
| 68 return manager_->Add(static_cast<T*>(socket)); | 73 return manager_->Add(static_cast<T*>(socket)); | 
| 69 } | 74 } | 
| 70 | 75 | 
| 71 virtual Socket* Get(const std::string& extension_id, | 76 virtual Socket* Get(const std::string& extension_id, | 
| 72 int api_resource_id) OVERRIDE { | 77 int api_resource_id) OVERRIDE { | 
| 73 return manager_->Get(extension_id, api_resource_id); | 78 return manager_->Get(extension_id, api_resource_id); | 
| 74 } | 79 } | 
| 75 | 80 | 
| 81 virtual void Set(const std::string& extension_id, | |
| 82 int api_resource_id, | |
| 83 Socket* socket) OVERRIDE { | |
| 84 manager_->Set(extension_id, api_resource_id, static_cast<T*>(socket)); | |
| 85 } | |
| 86 | |
| 76 virtual void Remove(const std::string& extension_id, | 87 virtual void Remove(const std::string& extension_id, | 
| 77 int api_resource_id) OVERRIDE { | 88 int api_resource_id) OVERRIDE { | 
| 78 manager_->Remove(extension_id, api_resource_id); | 89 manager_->Remove(extension_id, api_resource_id); | 
| 79 } | 90 } | 
| 80 | 91 | 
| 81 virtual base::hash_set<int>* GetResourceIds( | 92 virtual base::hash_set<int>* GetResourceIds( | 
| 82 const std::string& extension_id) OVERRIDE { | 93 const std::string& extension_id) OVERRIDE { | 
| 83 return manager_->GetResourceIds(extension_id); | 94 return manager_->GetResourceIds(extension_id); | 
| 84 } | 95 } | 
| 85 | 96 | 
| (...skipping 10 matching lines...) Expand all Loading... | |
| 96 | 107 | 
| 97 // AsyncApiFunction: | 108 // AsyncApiFunction: | 
| 98 virtual bool PrePrepare() OVERRIDE; | 109 virtual bool PrePrepare() OVERRIDE; | 
| 99 virtual bool Respond() OVERRIDE; | 110 virtual bool Respond() OVERRIDE; | 
| 100 | 111 | 
| 101 virtual scoped_ptr<SocketResourceManagerInterface> | 112 virtual scoped_ptr<SocketResourceManagerInterface> | 
| 102 CreateSocketResourceManager(); | 113 CreateSocketResourceManager(); | 
| 103 | 114 | 
| 104 int AddSocket(Socket* socket); | 115 int AddSocket(Socket* socket); | 
| 105 Socket* GetSocket(int api_resource_id); | 116 Socket* GetSocket(int api_resource_id); | 
| 117 void SetSocket(int api_resource_id, Socket* socket); | |
| 106 void RemoveSocket(int api_resource_id); | 118 void RemoveSocket(int api_resource_id); | 
| 107 base::hash_set<int>* GetSocketIds(); | 119 base::hash_set<int>* GetSocketIds(); | 
| 108 | 120 | 
| 109 private: | 121 private: | 
| 110 scoped_ptr<SocketResourceManagerInterface> manager_; | 122 scoped_ptr<SocketResourceManagerInterface> manager_; | 
| 111 }; | 123 }; | 
| 112 | 124 | 
| 113 class SocketExtensionWithDnsLookupFunction : public SocketAsyncApiFunction { | 125 class SocketExtensionWithDnsLookupFunction : public SocketAsyncApiFunction { | 
| 114 protected: | 126 protected: | 
| 115 SocketExtensionWithDnsLookupFunction(); | 127 SocketExtensionWithDnsLookupFunction(); | 
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 497 protected: | 509 protected: | 
| 498 virtual ~SocketGetJoinedGroupsFunction(); | 510 virtual ~SocketGetJoinedGroupsFunction(); | 
| 499 | 511 | 
| 500 // AsyncApiFunction | 512 // AsyncApiFunction | 
| 501 virtual bool Prepare() OVERRIDE; | 513 virtual bool Prepare() OVERRIDE; | 
| 502 virtual void Work() OVERRIDE; | 514 virtual void Work() OVERRIDE; | 
| 503 | 515 | 
| 504 private: | 516 private: | 
| 505 scoped_ptr<api::socket::GetJoinedGroups::Params> params_; | 517 scoped_ptr<api::socket::GetJoinedGroups::Params> params_; | 
| 506 }; | 518 }; | 
| 519 | |
| 520 class SocketSecureFunction : public SocketAsyncApiFunction { | |
| 521 public: | |
| 522 DECLARE_EXTENSION_FUNCTION("socket.secure", SOCKET_SECURE); | |
| 523 SocketSecureFunction(); | |
| 524 | |
| 525 protected: | |
| 526 virtual ~SocketSecureFunction(); | |
| 527 | |
| 528 // AsyncApiFunction | |
| 529 virtual bool Prepare() OVERRIDE; | |
| 530 virtual void AsyncWorkStart() OVERRIDE; | |
| 531 | |
| 532 private: | |
| 533 // Callback from TLSSocket::SecureTCPSocket(). | |
| 534 void TlsConnectDone(scoped_ptr<TLSSocket> socket, int result); | |
| 535 | |
| 536 scoped_ptr<api::socket::Secure::Params> params_; | |
| 537 net::URLRequestContextGetter* url_request_getter_; | |
| 
 
Ryan Sleevi
2014/02/04 22:28:14
use scoped_refptr<> when passing between threads.
 
lally
2014/02/11 22:05:35
Done.
 
 | |
| 538 | |
| 539 DISALLOW_COPY_AND_ASSIGN(SocketSecureFunction); | |
| 540 }; | |
| 541 | |
| 507 } // namespace extensions | 542 } // namespace extensions | 
| 508 | 543 | 
| 509 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ | 544 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ | 
| OLD | NEW |