| 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" | |
| 11 #include "chrome/browser/extensions/api/api_resource_manager.h" | 10 #include "chrome/browser/extensions/api/api_resource_manager.h" |
| 12 #include "chrome/common/extensions/api/socket.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" | 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 content { |
| 23 class BrowserContext; |
| 24 } |
| 25 |
| 22 namespace net { | 26 namespace net { |
| 23 class IOBuffer; | 27 class IOBuffer; |
| 24 } | 28 } |
| 25 | 29 |
| 26 namespace extensions { | 30 namespace extensions { |
| 27 | 31 |
| 28 class Socket; | 32 class Socket; |
| 29 | 33 |
| 30 // A simple interface to ApiResourceManager<Socket> or derived class. The goal | 34 // 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 | 35 // 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 | 36 // of ApiResourceManager<> depending on the type of socket (old version in |
| 33 // "socket" namespace vs new version in "socket.xxx" namespaces). | 37 // "socket" namespace vs new version in "socket.xxx" namespaces). |
| 34 class SocketResourceManagerInterface { | 38 class SocketResourceManagerInterface { |
| 35 public: | 39 public: |
| 36 virtual ~SocketResourceManagerInterface() {} | 40 virtual ~SocketResourceManagerInterface() {} |
| 37 | 41 |
| 38 virtual bool SetProfile(Profile* profile) = 0; | 42 virtual bool SetBrowserContext(content::BrowserContext* context) = 0; |
| 39 virtual int Add(Socket *socket) = 0; | 43 virtual int Add(Socket *socket) = 0; |
| 40 virtual Socket* Get(const std::string& extension_id, | 44 virtual Socket* Get(const std::string& extension_id, |
| 41 int api_resource_id) = 0; | 45 int api_resource_id) = 0; |
| 42 virtual void Remove(const std::string& extension_id, | 46 virtual void Remove(const std::string& extension_id, |
| 43 int api_resource_id) = 0; | 47 int api_resource_id) = 0; |
| 44 virtual base::hash_set<int>* GetResourceIds( | 48 virtual base::hash_set<int>* GetResourceIds( |
| 45 const std::string& extension_id) = 0; | 49 const std::string& extension_id) = 0; |
| 46 }; | 50 }; |
| 47 | 51 |
| 48 // Implementation of SocketResourceManagerInterface using an | 52 // Implementation of SocketResourceManagerInterface using an |
| 49 // ApiResourceManager<T> instance (where T derives from Socket). | 53 // ApiResourceManager<T> instance (where T derives from Socket). |
| 50 template<typename T> | 54 template<typename T> |
| 51 class SocketResourceManager : public SocketResourceManagerInterface { | 55 class SocketResourceManager : public SocketResourceManagerInterface { |
| 52 public: | 56 public: |
| 53 SocketResourceManager() | 57 SocketResourceManager() |
| 54 : manager_(NULL) { | 58 : manager_(NULL) { |
| 55 } | 59 } |
| 56 | 60 |
| 57 virtual bool SetProfile(Profile* profile) OVERRIDE { | 61 virtual bool SetBrowserContext(content::BrowserContext* context) OVERRIDE { |
| 58 manager_ = ApiResourceManager<T>::Get(profile); | 62 manager_ = ApiResourceManager<T>::Get(context); |
| 59 DCHECK(manager_) << "There is no socket manager. " | 63 DCHECK(manager_) << "There is no socket manager. " |
| 60 "If this assertion is failing during a test, then it is likely that " | 64 "If this assertion is failing during a test, then it is likely that " |
| 61 "TestExtensionSystem is failing to provide an instance of " | 65 "TestExtensionSystem is failing to provide an instance of " |
| 62 "ApiResourceManager<Socket>."; | 66 "ApiResourceManager<Socket>."; |
| 63 return manager_ != NULL; | 67 return manager_ != NULL; |
| 64 } | 68 } |
| 65 | 69 |
| 66 virtual int Add(Socket *socket) OVERRIDE { | 70 virtual int Add(Socket *socket) OVERRIDE { |
| 67 // Note: Cast needed here, because "T" may be a subclass of "Socket". | 71 // Note: Cast needed here, because "T" may be a subclass of "Socket". |
| 68 return manager_->Add(static_cast<T*>(socket)); | 72 return manager_->Add(static_cast<T*>(socket)); |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 // AsyncApiFunction | 504 // AsyncApiFunction |
| 501 virtual bool Prepare() OVERRIDE; | 505 virtual bool Prepare() OVERRIDE; |
| 502 virtual void Work() OVERRIDE; | 506 virtual void Work() OVERRIDE; |
| 503 | 507 |
| 504 private: | 508 private: |
| 505 scoped_ptr<api::socket::GetJoinedGroups::Params> params_; | 509 scoped_ptr<api::socket::GetJoinedGroups::Params> params_; |
| 506 }; | 510 }; |
| 507 } // namespace extensions | 511 } // namespace extensions |
| 508 | 512 |
| 509 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ | 513 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ |
| OLD | NEW |