| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // ClientSocketPoolManager manages access to all ClientSocketPools. It's a | 5 // ClientSocketPoolManager manages access to all ClientSocketPools. It's a |
| 6 // simple container for all of them. Most importantly, it handles the lifetime | 6 // simple container for all of them. Most importantly, it handles the lifetime |
| 7 // and destruction order properly. | 7 // and destruction order properly. |
| 8 | 8 |
| 9 #ifndef NET_SOCKET_CLIENT_SOCKET_POOL_MANAGER_H_ | 9 #ifndef NET_SOCKET_CLIENT_SOCKET_POOL_MANAGER_H_ |
| 10 #define NET_SOCKET_CLIENT_SOCKET_POOL_MANAGER_H_ | 10 #define NET_SOCKET_CLIENT_SOCKET_POOL_MANAGER_H_ |
| 11 #pragma once | 11 #pragma once |
| 12 | 12 |
| 13 #include <map> | |
| 14 #include "base/basictypes.h" | |
| 15 #include "base/memory/ref_counted.h" | |
| 16 #include "base/memory/scoped_ptr.h" | |
| 17 #include "base/stl_util.h" | |
| 18 #include "base/template_util.h" | |
| 19 #include "base/threading/non_thread_safe.h" | |
| 20 #include "net/base/cert_database.h" | |
| 21 #include "net/base/completion_callback.h" | 13 #include "net/base/completion_callback.h" |
| 22 #include "net/base/net_export.h" | 14 #include "net/base/net_export.h" |
| 23 #include "net/base/request_priority.h" | 15 #include "net/base/request_priority.h" |
| 24 #include "net/socket/client_socket_pool_histograms.h" | |
| 25 | 16 |
| 26 class GURL; | 17 class GURL; |
| 27 | 18 |
| 28 namespace base { | 19 namespace base { |
| 29 class Value; | 20 class Value; |
| 30 } | 21 } |
| 31 | 22 |
| 32 namespace net { | 23 namespace net { |
| 33 | 24 |
| 34 class BoundNetLog; | 25 class BoundNetLog; |
| 35 class CertVerifier; | |
| 36 class ClientSocketFactory; | |
| 37 class ClientSocketHandle; | 26 class ClientSocketHandle; |
| 38 class ClientSocketPoolHistograms; | 27 class HostPortPair; |
| 39 class DnsCertProvenanceChecker; | |
| 40 class DnsRRResolver; | |
| 41 class HttpNetworkSession; | 28 class HttpNetworkSession; |
| 29 class HttpProxyClientSocketPool; |
| 42 class HttpRequestHeaders; | 30 class HttpRequestHeaders; |
| 43 class HostPortPair; | |
| 44 class HttpProxyClientSocketPool; | |
| 45 class HostResolver; | |
| 46 class NetLog; | |
| 47 class OriginBoundCertService; | |
| 48 class ProxyInfo; | 31 class ProxyInfo; |
| 49 class ProxyService; | 32 class TransportClientSocketPool; |
| 50 class SOCKSClientSocketPool; | 33 class SOCKSClientSocketPool; |
| 51 class SSLClientSocketPool; | 34 class SSLClientSocketPool; |
| 52 class SSLConfigService; | |
| 53 class SSLHostInfoFactory; | |
| 54 class TransportClientSocketPool; | |
| 55 | 35 |
| 56 struct SSLConfig; | 36 struct SSLConfig; |
| 57 | 37 |
| 58 // This should rather be a simple constant but Windows shared libs doesn't | 38 // This should rather be a simple constant but Windows shared libs doesn't |
| 59 // really offer much flexiblity in exporting contants. | 39 // really offer much flexiblity in exporting contants. |
| 60 enum DefaultMaxValues { kDefaultMaxSocketsPerProxyServer = 32 }; | 40 enum DefaultMaxValues { kDefaultMaxSocketsPerProxyServer = 32 }; |
| 61 | 41 |
| 62 namespace internal { | 42 class NET_EXPORT_PRIVATE ClientSocketPoolManager { |
| 63 | |
| 64 // A helper class for auto-deleting Values in the destructor. | |
| 65 template <typename Key, typename Value> | |
| 66 class OwnedPoolMap : public std::map<Key, Value> { | |
| 67 public: | 43 public: |
| 68 OwnedPoolMap() { | 44 ClientSocketPoolManager(); |
| 69 COMPILE_ASSERT(base::is_pointer<Value>::value, | |
| 70 value_must_be_a_pointer); | |
| 71 } | |
| 72 | |
| 73 ~OwnedPoolMap() { | |
| 74 STLDeleteValues(this); | |
| 75 } | |
| 76 }; | |
| 77 | |
| 78 } // namespace internal | |
| 79 | |
| 80 class ClientSocketPoolManager : public base::NonThreadSafe, | |
| 81 public CertDatabase::Observer { | |
| 82 public: | |
| 83 ClientSocketPoolManager(NetLog* net_log, | |
| 84 ClientSocketFactory* socket_factory, | |
| 85 HostResolver* host_resolver, | |
| 86 CertVerifier* cert_verifier, | |
| 87 OriginBoundCertService* origin_bound_cert_service, | |
| 88 DnsRRResolver* dnsrr_resolver, | |
| 89 DnsCertProvenanceChecker* dns_cert_checker, | |
| 90 SSLHostInfoFactory* ssl_host_info_factory, | |
| 91 ProxyService* proxy_service, | |
| 92 SSLConfigService* ssl_config_service); | |
| 93 virtual ~ClientSocketPoolManager(); | 45 virtual ~ClientSocketPoolManager(); |
| 94 | 46 |
| 95 void FlushSocketPools(); | |
| 96 void CloseIdleSockets(); | |
| 97 | |
| 98 TransportClientSocketPool* transport_socket_pool() { | |
| 99 return transport_socket_pool_.get(); | |
| 100 } | |
| 101 | |
| 102 SSLClientSocketPool* ssl_socket_pool() { return ssl_socket_pool_.get(); } | |
| 103 | |
| 104 SOCKSClientSocketPool* GetSocketPoolForSOCKSProxy( | |
| 105 const HostPortPair& socks_proxy); | |
| 106 | |
| 107 HttpProxyClientSocketPool* GetSocketPoolForHTTPProxy( | |
| 108 const HostPortPair& http_proxy); | |
| 109 | |
| 110 SSLClientSocketPool* GetSocketPoolForSSLWithProxy( | |
| 111 const HostPortPair& proxy_server); | |
| 112 | |
| 113 // The setter methods below affect only newly created socket pools after the | 47 // The setter methods below affect only newly created socket pools after the |
| 114 // methods are called. Normally they should be called at program startup | 48 // methods are called. Normally they should be called at program startup |
| 115 // before any ClientSocketPoolManager is created. | 49 // before any ClientSocketPoolManagerImpl is created. |
| 116 NET_EXPORT static void set_max_sockets_per_pool(int socket_count); | 50 static int max_sockets_per_pool(); |
| 117 NET_EXPORT static int max_sockets_per_group(); | 51 static void set_max_sockets_per_pool(int socket_count); |
| 118 NET_EXPORT static void set_max_sockets_per_group(int socket_count); | |
| 119 NET_EXPORT static void set_max_sockets_per_proxy_server(int socket_count); | |
| 120 | 52 |
| 121 // A helper method that uses the passed in proxy information to initialize a | 53 static int max_sockets_per_group(); |
| 122 // ClientSocketHandle with the relevant socket pool. Use this method for | 54 static void set_max_sockets_per_group(int socket_count); |
| 123 // HTTP/HTTPS requests. |ssl_config_for_origin| is only used if the request | |
| 124 // uses SSL and |ssl_config_for_proxy| is used if the proxy server is HTTPS. | |
| 125 static int InitSocketHandleForHttpRequest( | |
| 126 const GURL& request_url, | |
| 127 const HttpRequestHeaders& request_extra_headers, | |
| 128 int request_load_flags, | |
| 129 RequestPriority request_priority, | |
| 130 HttpNetworkSession* session, | |
| 131 const ProxyInfo& proxy_info, | |
| 132 bool force_spdy_over_ssl, | |
| 133 bool want_spdy_over_npn, | |
| 134 const SSLConfig& ssl_config_for_origin, | |
| 135 const SSLConfig& ssl_config_for_proxy, | |
| 136 const BoundNetLog& net_log, | |
| 137 ClientSocketHandle* socket_handle, | |
| 138 OldCompletionCallback* callback); | |
| 139 | 55 |
| 140 // A helper method that uses the passed in proxy information to initialize a | 56 static int max_sockets_per_proxy_server(); |
| 141 // ClientSocketHandle with the relevant socket pool. Use this method for | 57 static void set_max_sockets_per_proxy_server(int socket_count); |
| 142 // a raw socket connection to a host-port pair (that needs to tunnel through | |
| 143 // the proxies). | |
| 144 NET_EXPORT static int InitSocketHandleForRawConnect( | |
| 145 const HostPortPair& host_port_pair, | |
| 146 HttpNetworkSession* session, | |
| 147 const ProxyInfo& proxy_info, | |
| 148 const SSLConfig& ssl_config_for_origin, | |
| 149 const SSLConfig& ssl_config_for_proxy, | |
| 150 const BoundNetLog& net_log, | |
| 151 ClientSocketHandle* socket_handle, | |
| 152 OldCompletionCallback* callback); | |
| 153 | 58 |
| 154 // Similar to InitSocketHandleForHttpRequest except that it initiates the | 59 virtual void FlushSocketPools() = 0; |
| 155 // desired number of preconnect streams from the relevant socket pool. | 60 virtual void CloseIdleSockets() = 0; |
| 156 static int PreconnectSocketsForHttpRequest( | 61 virtual TransportClientSocketPool* GetTransportSocketPool() = 0; |
| 157 const GURL& request_url, | 62 virtual SSLClientSocketPool* GetSSLSocketPool() = 0; |
| 158 const HttpRequestHeaders& request_extra_headers, | 63 virtual SOCKSClientSocketPool* GetSocketPoolForSOCKSProxy( |
| 159 int request_load_flags, | 64 const HostPortPair& socks_proxy) = 0; |
| 160 RequestPriority request_priority, | 65 virtual HttpProxyClientSocketPool* GetSocketPoolForHTTPProxy( |
| 161 HttpNetworkSession* session, | 66 const HostPortPair& http_proxy) = 0; |
| 162 const ProxyInfo& proxy_info, | 67 virtual SSLClientSocketPool* GetSocketPoolForSSLWithProxy( |
| 163 bool force_spdy_over_ssl, | 68 const HostPortPair& proxy_server) = 0; |
| 164 bool want_spdy_over_npn, | |
| 165 const SSLConfig& ssl_config_for_origin, | |
| 166 const SSLConfig& ssl_config_for_proxy, | |
| 167 const BoundNetLog& net_log, | |
| 168 int num_preconnect_streams); | |
| 169 | |
| 170 // Creates a Value summary of the state of the socket pools. The caller is | 69 // Creates a Value summary of the state of the socket pools. The caller is |
| 171 // responsible for deleting the returned value. | 70 // responsible for deleting the returned value. |
| 172 base::Value* SocketPoolInfoToValue() const; | 71 virtual base::Value* SocketPoolInfoToValue() const = 0; |
| 72 }; |
| 173 | 73 |
| 174 // CertDatabase::Observer methods: | 74 // A helper method that uses the passed in proxy information to initialize a |
| 175 virtual void OnUserCertAdded(const X509Certificate* cert) OVERRIDE; | 75 // ClientSocketHandle with the relevant socket pool. Use this method for |
| 176 virtual void OnCertTrustChanged(const X509Certificate* cert) OVERRIDE; | 76 // HTTP/HTTPS requests. |ssl_config_for_origin| is only used if the request |
| 77 // uses SSL and |ssl_config_for_proxy| is used if the proxy server is HTTPS. |
| 78 int InitSocketHandleForHttpRequest( |
| 79 const GURL& request_url, |
| 80 const HttpRequestHeaders& request_extra_headers, |
| 81 int request_load_flags, |
| 82 RequestPriority request_priority, |
| 83 HttpNetworkSession* session, |
| 84 const ProxyInfo& proxy_info, |
| 85 bool force_spdy_over_ssl, |
| 86 bool want_spdy_over_npn, |
| 87 const SSLConfig& ssl_config_for_origin, |
| 88 const SSLConfig& ssl_config_for_proxy, |
| 89 const BoundNetLog& net_log, |
| 90 ClientSocketHandle* socket_handle, |
| 91 OldCompletionCallback* callback); |
| 177 | 92 |
| 178 private: | 93 // A helper method that uses the passed in proxy information to initialize a |
| 179 friend class HttpNetworkSessionPeer; | 94 // ClientSocketHandle with the relevant socket pool. Use this method for |
| 95 // a raw socket connection to a host-port pair (that needs to tunnel through |
| 96 // the proxies). |
| 97 NET_EXPORT int InitSocketHandleForRawConnect( |
| 98 const HostPortPair& host_port_pair, |
| 99 HttpNetworkSession* session, |
| 100 const ProxyInfo& proxy_info, |
| 101 const SSLConfig& ssl_config_for_origin, |
| 102 const SSLConfig& ssl_config_for_proxy, |
| 103 const BoundNetLog& net_log, |
| 104 ClientSocketHandle* socket_handle, |
| 105 OldCompletionCallback* callback); |
| 180 | 106 |
| 181 typedef internal::OwnedPoolMap<HostPortPair, TransportClientSocketPool*> | 107 // Similar to InitSocketHandleForHttpRequest except that it initiates the |
| 182 TransportSocketPoolMap; | 108 // desired number of preconnect streams from the relevant socket pool. |
| 183 typedef internal::OwnedPoolMap<HostPortPair, SOCKSClientSocketPool*> | 109 int PreconnectSocketsForHttpRequest( |
| 184 SOCKSSocketPoolMap; | 110 const GURL& request_url, |
| 185 typedef internal::OwnedPoolMap<HostPortPair, HttpProxyClientSocketPool*> | 111 const HttpRequestHeaders& request_extra_headers, |
| 186 HTTPProxySocketPoolMap; | 112 int request_load_flags, |
| 187 typedef internal::OwnedPoolMap<HostPortPair, SSLClientSocketPool*> | 113 RequestPriority request_priority, |
| 188 SSLSocketPoolMap; | 114 HttpNetworkSession* session, |
| 189 | 115 const ProxyInfo& proxy_info, |
| 190 NetLog* const net_log_; | 116 bool force_spdy_over_ssl, |
| 191 ClientSocketFactory* const socket_factory_; | 117 bool want_spdy_over_npn, |
| 192 HostResolver* const host_resolver_; | 118 const SSLConfig& ssl_config_for_origin, |
| 193 CertVerifier* const cert_verifier_; | 119 const SSLConfig& ssl_config_for_proxy, |
| 194 OriginBoundCertService* const origin_bound_cert_service_; | 120 const BoundNetLog& net_log, |
| 195 DnsRRResolver* const dnsrr_resolver_; | 121 int num_preconnect_streams); |
| 196 DnsCertProvenanceChecker* const dns_cert_checker_; | |
| 197 SSLHostInfoFactory* const ssl_host_info_factory_; | |
| 198 ProxyService* const proxy_service_; | |
| 199 const scoped_refptr<SSLConfigService> ssl_config_service_; | |
| 200 | |
| 201 // Note: this ordering is important. | |
| 202 | |
| 203 ClientSocketPoolHistograms transport_pool_histograms_; | |
| 204 scoped_ptr<TransportClientSocketPool> transport_socket_pool_; | |
| 205 | |
| 206 ClientSocketPoolHistograms ssl_pool_histograms_; | |
| 207 scoped_ptr<SSLClientSocketPool> ssl_socket_pool_; | |
| 208 | |
| 209 ClientSocketPoolHistograms transport_for_socks_pool_histograms_; | |
| 210 TransportSocketPoolMap transport_socket_pools_for_socks_proxies_; | |
| 211 | |
| 212 ClientSocketPoolHistograms socks_pool_histograms_; | |
| 213 SOCKSSocketPoolMap socks_socket_pools_; | |
| 214 | |
| 215 ClientSocketPoolHistograms transport_for_http_proxy_pool_histograms_; | |
| 216 TransportSocketPoolMap transport_socket_pools_for_http_proxies_; | |
| 217 | |
| 218 ClientSocketPoolHistograms transport_for_https_proxy_pool_histograms_; | |
| 219 TransportSocketPoolMap transport_socket_pools_for_https_proxies_; | |
| 220 | |
| 221 ClientSocketPoolHistograms ssl_for_https_proxy_pool_histograms_; | |
| 222 SSLSocketPoolMap ssl_socket_pools_for_https_proxies_; | |
| 223 | |
| 224 ClientSocketPoolHistograms http_proxy_pool_histograms_; | |
| 225 HTTPProxySocketPoolMap http_proxy_socket_pools_; | |
| 226 | |
| 227 ClientSocketPoolHistograms ssl_socket_pool_for_proxies_histograms_; | |
| 228 SSLSocketPoolMap ssl_socket_pools_for_proxies_; | |
| 229 | |
| 230 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolManager); | |
| 231 }; | |
| 232 | 122 |
| 233 } // namespace net | 123 } // namespace net |
| 234 | 124 |
| 235 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_MANAGER_H_ | 125 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_MANAGER_H_ |
| OLD | NEW |