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 3 // found in the LICENSE file. 4 // 5 // ClientSocketPoolManager manages access to all ClientSocketPools. It's a 6 // simple container for all of them. Most importantly, it handles the lifetime 7 // and destruction order properly. 8 9 #ifndef NET_SOCKET_CLIENT_SOCKET_POOL_MANAGER_H_ 10 #define NET_SOCKET_CLIENT_SOCKET_POOL_MANAGER_H_ 11 #pragma once 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-inl.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" 22 #include "net/socket/client_socket_pool_histograms.h" 23 24 class Value; 25 26 namespace net { 27 28 class BoundNetLog; 29 class CertVerifier; 30 class ClientSocketFactory; 31 class ClientSocketHandle; 32 class ClientSocketPoolHistograms; 33 class DnsCertProvenanceChecker; 34 class DnsRRResolver; 35 class HttpNetworkSession; 36 class HostPortPair; 37 class HttpProxyClientSocketPool; 38 class HostResolver; 39 class NetLog; 40 class ProxyInfo; 41 class ProxyService; 42 class SOCKSClientSocketPool; 43 class SSLClientSocketPool; 44 class SSLConfigService; 45 class SSLHostInfoFactory; 46 class TransportClientSocketPool; 47 48 struct HttpRequestInfo; 49 struct SSLConfig; 50 51 namespace internal { 52 53 // A helper class for auto-deleting Values in the destructor. 54 template <typename Key, typename Value> 55 class OwnedPoolMap : public std::map<Key, Value> { 56 public: 57 OwnedPoolMap() { 58 COMPILE_ASSERT(base::is_pointer<Value>::value, 59 value_must_be_a_pointer); 60 } 61 62 ~OwnedPoolMap() { 63 STLDeleteValues(this); 64 } 65 }; 66 67 } // namespace internal 68 69 class ClientSocketPoolManager : public base::NonThreadSafe, 70 public CertDatabase::Observer { 71 public: 72 ClientSocketPoolManager(NetLog* net_log, 73 ClientSocketFactory* socket_factory, 74 HostResolver* host_resolver, 75 CertVerifier* cert_verifier, 76 DnsRRResolver* dnsrr_resolver, 77 DnsCertProvenanceChecker* dns_cert_checker, 78 SSLHostInfoFactory* ssl_host_info_factory, 79 ProxyService* proxy_service, 80 SSLConfigService* ssl_config_service); 81 ~ClientSocketPoolManager(); 82 83 void FlushSocketPools(); 84 void CloseIdleSockets(); 85 86 TransportClientSocketPool* transport_socket_pool() { 87 return transport_socket_pool_.get(); 88 } 89 90 SSLClientSocketPool* ssl_socket_pool() { return ssl_socket_pool_.get(); } 91 92 SOCKSClientSocketPool* GetSocketPoolForSOCKSProxy( 93 const HostPortPair& socks_proxy); 94 95 HttpProxyClientSocketPool* GetSocketPoolForHTTPProxy( 96 const HostPortPair& http_proxy); 97 98 SSLClientSocketPool* GetSocketPoolForSSLWithProxy( 99 const HostPortPair& proxy_server); 100 101 static int max_sockets_per_group(); 102 static void set_max_sockets_per_group(int socket_count); 103 static void set_max_sockets_per_proxy_server(int socket_count); 104 105 // A helper method that uses the passed in proxy information to initialize a 106 // ClientSocketHandle with the relevant socket pool. Use this method for 107 // HTTP/HTTPS requests. |ssl_config_for_origin| is only used if the request 108 // uses SSL and |ssl_config_for_proxy| is used if the proxy server is HTTPS. 109 static int InitSocketHandleForHttpRequest( 110 const HttpRequestInfo& request_info, 111 HttpNetworkSession* session, 112 const ProxyInfo& proxy_info, 113 bool force_spdy_over_ssl, 114 bool want_spdy_over_npn, 115 const SSLConfig& ssl_config_for_origin, 116 const SSLConfig& ssl_config_for_proxy, 117 const BoundNetLog& net_log, 118 ClientSocketHandle* socket_handle, 119 CompletionCallback* callback); 120 121 // A helper method that uses the passed in proxy information to initialize a 122 // ClientSocketHandle with the relevant socket pool. Use this method for 123 // a raw socket connection to a host-port pair (that needs to tunnel through 124 // the proxies). 125 static int InitSocketHandleForRawConnect( 126 const HostPortPair& host_port_pair, 127 HttpNetworkSession* session, 128 const ProxyInfo& proxy_info, 129 const SSLConfig& ssl_config_for_origin, 130 const SSLConfig& ssl_config_for_proxy, 131 const BoundNetLog& net_log, 132 ClientSocketHandle* socket_handle, 133 CompletionCallback* callback); 134 135 // Similar to InitSocketHandleForHttpRequest except that it initiates the 136 // desired number of preconnect streams from the relevant socket pool. 137 static int PreconnectSocketsForHttpRequest( 138 const HttpRequestInfo& request_info, 139 HttpNetworkSession* session, 140 const ProxyInfo& proxy_info, 141 bool force_spdy_over_ssl, 142 bool want_spdy_over_npn, 143 const SSLConfig& ssl_config_for_origin, 144 const SSLConfig& ssl_config_for_proxy, 145 const BoundNetLog& net_log, 146 int num_preconnect_streams); 147 148 // Creates a Value summary of the state of the socket pools. The caller is 149 // responsible for deleting the returned value. 150 Value* SocketPoolInfoToValue() const; 151 152 // CertDatabase::Observer methods: 153 virtual void OnUserCertAdded(const X509Certificate* cert); 154 virtual void OnCertTrustChanged(const X509Certificate* cert); 155 156 private: 157 friend class HttpNetworkSessionPeer; 158 159 typedef internal::OwnedPoolMap<HostPortPair, TransportClientSocketPool*> 160 TransportSocketPoolMap; 161 typedef internal::OwnedPoolMap<HostPortPair, SOCKSClientSocketPool*> 162 SOCKSSocketPoolMap; 163 typedef internal::OwnedPoolMap<HostPortPair, HttpProxyClientSocketPool*> 164 HTTPProxySocketPoolMap; 165 typedef internal::OwnedPoolMap<HostPortPair, SSLClientSocketPool*> 166 SSLSocketPoolMap; 167 168 NetLog* const net_log_; 169 ClientSocketFactory* const socket_factory_; 170 HostResolver* const host_resolver_; 171 CertVerifier* const cert_verifier_; 172 DnsRRResolver* const dnsrr_resolver_; 173 DnsCertProvenanceChecker* const dns_cert_checker_; 174 SSLHostInfoFactory* const ssl_host_info_factory_; 175 const scoped_refptr<ProxyService> proxy_service_; 176 const scoped_refptr<SSLConfigService> ssl_config_service_; 177 178 // Note: this ordering is important. 179 180 ClientSocketPoolHistograms transport_pool_histograms_; 181 scoped_ptr<TransportClientSocketPool> transport_socket_pool_; 182 183 ClientSocketPoolHistograms ssl_pool_histograms_; 184 scoped_ptr<SSLClientSocketPool> ssl_socket_pool_; 185 186 ClientSocketPoolHistograms transport_for_socks_pool_histograms_; 187 TransportSocketPoolMap transport_socket_pools_for_socks_proxies_; 188 189 ClientSocketPoolHistograms socks_pool_histograms_; 190 SOCKSSocketPoolMap socks_socket_pools_; 191 192 ClientSocketPoolHistograms transport_for_http_proxy_pool_histograms_; 193 TransportSocketPoolMap transport_socket_pools_for_http_proxies_; 194 195 ClientSocketPoolHistograms transport_for_https_proxy_pool_histograms_; 196 TransportSocketPoolMap transport_socket_pools_for_https_proxies_; 197 198 ClientSocketPoolHistograms ssl_for_https_proxy_pool_histograms_; 199 SSLSocketPoolMap ssl_socket_pools_for_https_proxies_; 200 201 ClientSocketPoolHistograms http_proxy_pool_histograms_; 202 HTTPProxySocketPoolMap http_proxy_socket_pools_; 203 204 ClientSocketPoolHistograms ssl_socket_pool_for_proxies_histograms_; 205 SSLSocketPoolMap ssl_socket_pools_for_proxies_; 206 207 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolManager); 208 }; 209 210 } // namespace net 211 212 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_MANAGER_H_ 213