OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_ |
6 #define EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_ | 6 #define EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "extensions/browser/api/api_resource_manager.h" | 12 #include "extensions/browser/api/api_resource_manager.h" |
13 #include "extensions/browser/api/async_api_function.h" | 13 #include "extensions/browser/api/async_api_function.h" |
14 #include "extensions/browser/extension_function.h" | 14 #include "extensions/browser/extension_function.h" |
15 #include "extensions/common/api/socket.h" | 15 #include "extensions/common/api/socket.h" |
16 #include "net/base/address_list.h" | 16 #include "net/base/address_list.h" |
17 #include "net/dns/host_resolver.h" | 17 #include "net/dns/host_resolver.h" |
18 #include "net/socket/tcp_client_socket.h" | 18 #include "net/socket/tcp_client_socket.h" |
19 | 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 class URLRequestContextGetter; |
| 28 class SSLClientSocket; |
27 } | 29 } |
28 | 30 |
29 namespace extensions { | 31 namespace extensions { |
30 | 32 class TLSSocket; |
31 class Socket; | 33 class Socket; |
32 | 34 |
33 // A simple interface to ApiResourceManager<Socket> or derived class. The goal | 35 // 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 | 36 // 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 | 37 // of ApiResourceManager<> depending on the type of socket (old version in |
36 // "socket" namespace vs new version in "socket.xxx" namespaces). | 38 // "socket" namespace vs new version in "socket.xxx" namespaces). |
37 class SocketResourceManagerInterface { | 39 class SocketResourceManagerInterface { |
38 public: | 40 public: |
39 virtual ~SocketResourceManagerInterface() {} | 41 virtual ~SocketResourceManagerInterface() {} |
40 | 42 |
41 virtual bool SetBrowserContext(content::BrowserContext* context) = 0; | 43 virtual bool SetBrowserContext(content::BrowserContext* context) = 0; |
42 virtual int Add(Socket* socket) = 0; | 44 virtual int Add(Socket* socket) = 0; |
43 virtual Socket* Get(const std::string& extension_id, int api_resource_id) = 0; | 45 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; | 46 virtual void Remove(const std::string& extension_id, int api_resource_id) = 0; |
| 47 virtual void Replace(const std::string& extension_id, |
| 48 int api_resource_id, |
| 49 Socket* socket) = 0; |
45 virtual base::hash_set<int>* GetResourceIds( | 50 virtual base::hash_set<int>* GetResourceIds( |
46 const std::string& extension_id) = 0; | 51 const std::string& extension_id) = 0; |
47 }; | 52 }; |
48 | 53 |
49 // Implementation of SocketResourceManagerInterface using an | 54 // Implementation of SocketResourceManagerInterface using an |
50 // ApiResourceManager<T> instance (where T derives from Socket). | 55 // ApiResourceManager<T> instance (where T derives from Socket). |
51 template <typename T> | 56 template <typename T> |
52 class SocketResourceManager : public SocketResourceManagerInterface { | 57 class SocketResourceManager : public SocketResourceManagerInterface { |
53 public: | 58 public: |
54 SocketResourceManager() : manager_(NULL) {} | 59 SocketResourceManager() : manager_(NULL) {} |
(...skipping 11 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 Replace(const std::string& extension_id, |
| 82 int api_resource_id, |
| 83 Socket* socket) OVERRIDE { |
| 84 manager_->Replace(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(const std::string& extension_id) | 92 virtual base::hash_set<int>* GetResourceIds(const std::string& extension_id) |
82 OVERRIDE { | 93 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 ReplaceSocket(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 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 protected: | 507 protected: |
496 virtual ~SocketGetJoinedGroupsFunction(); | 508 virtual ~SocketGetJoinedGroupsFunction(); |
497 | 509 |
498 // AsyncApiFunction | 510 // AsyncApiFunction |
499 virtual bool Prepare() OVERRIDE; | 511 virtual bool Prepare() OVERRIDE; |
500 virtual void Work() OVERRIDE; | 512 virtual void Work() OVERRIDE; |
501 | 513 |
502 private: | 514 private: |
503 scoped_ptr<core_api::socket::GetJoinedGroups::Params> params_; | 515 scoped_ptr<core_api::socket::GetJoinedGroups::Params> params_; |
504 }; | 516 }; |
| 517 |
| 518 class SocketSecureFunction : public SocketAsyncApiFunction { |
| 519 public: |
| 520 DECLARE_EXTENSION_FUNCTION("socket.secure", SOCKET_SECURE); |
| 521 SocketSecureFunction(); |
| 522 |
| 523 protected: |
| 524 virtual ~SocketSecureFunction(); |
| 525 |
| 526 // AsyncApiFunction |
| 527 virtual bool Prepare() OVERRIDE; |
| 528 virtual void AsyncWorkStart() OVERRIDE; |
| 529 |
| 530 private: |
| 531 // Callback from TLSSocket::UpgradeSocketToTLS(). |
| 532 void TlsConnectDone(scoped_ptr<TLSSocket> socket, int result); |
| 533 |
| 534 scoped_ptr<core_api::socket::Secure::Params> params_; |
| 535 scoped_refptr<net::URLRequestContextGetter> url_request_getter_; |
| 536 |
| 537 DISALLOW_COPY_AND_ASSIGN(SocketSecureFunction); |
| 538 }; |
| 539 |
505 } // namespace extensions | 540 } // namespace extensions |
506 | 541 |
507 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_ | 542 #endif // EXTENSIONS_BROWSER_API_SOCKET_SOCKET_API_H_ |
OLD | NEW |