1 // Copyright 2012 The Chromium Authors 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 12 #include <vector> 13 14 #include "base/values.h" 15 #include "net/base/completion_once_callback.h" 16 #include "net/base/net_export.h" 17 #include "net/base/request_priority.h" 18 #include "net/dns/public/secure_dns_policy.h" 19 #include "net/http/http_network_session.h" 20 #include "net/socket/client_socket_pool.h" 21 #include "net/ssl/ssl_config.h" 22 #include "url/scheme_host_port.h" 23 24 namespace net { 25 26 class ClientSocketHandle; 27 class NetLogWithSource; 28 class NetworkAnonymizationKey; 29 class ProxyInfo; 30 class ProxyChain; 31 32 constexpr int kDefaultMaxSocketsPerProxyChain = 32; 33 34 class NET_EXPORT_PRIVATE ClientSocketPoolManager { 35 public: 36 ClientSocketPoolManager(); 37 virtual ~ClientSocketPoolManager(); 38 39 // The setter methods below affect only newly created socket pools after the 40 // methods are called. Normally they should be called at program startup 41 // before any ClientSocketPoolManagerImpl is created. 42 static int max_sockets_per_pool(HttpNetworkSession::SocketPoolType pool_type); 43 static void set_max_sockets_per_pool( 44 HttpNetworkSession::SocketPoolType pool_type, 45 int socket_count); 46 47 static int max_sockets_per_group( 48 HttpNetworkSession::SocketPoolType pool_type); 49 static void set_max_sockets_per_group( 50 HttpNetworkSession::SocketPoolType pool_type, 51 int socket_count); 52 53 static int max_sockets_per_proxy_chain( 54 HttpNetworkSession::SocketPoolType pool_type); 55 static void set_max_sockets_per_proxy_chain( 56 HttpNetworkSession::SocketPoolType pool_type, 57 int socket_count); 58 59 static base::TimeDelta unused_idle_socket_timeout( 60 HttpNetworkSession::SocketPoolType pool_type); 61 62 // The |net_error| is returned to clients of pending socket requests, while 63 // |reason| is logged at the socket layer. 64 virtual void FlushSocketPoolsWithError(int net_error, 65 const char* net_log_reason_utf8) = 0; 66 virtual void CloseIdleSockets(const char* net_log_reason_utf8) = 0; 67 68 // Returns the socket pool for the specified ProxyChain (Which may be 69 // ProxyChain::Direct()). 70 virtual ClientSocketPool* GetSocketPool(const ProxyChain& proxy_chain) = 0; 71 72 // Creates a Value summary of the state of the socket pools. 73 virtual base::Value SocketPoolInfoToValue() const = 0; 74 }; 75 76 // A helper method that uses the passed in proxy information to initialize a 77 // ClientSocketHandle with the relevant socket pool. Use this method for 78 // HTTP/HTTPS requests. `allowed_bad_certs` is only used if the request 79 // uses SSL. `resolution_callback` will be invoked after the the hostname is 80 // resolved. If `resolution_callback` does not return OK, then the connection 81 // will be aborted with that value. 82 int InitSocketHandleForHttpRequest( 83 url::SchemeHostPort endpoint, 84 int request_load_flags, 85 RequestPriority request_priority, 86 HttpNetworkSession* session, 87 const ProxyInfo& proxy_info, 88 const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs, 89 PrivacyMode privacy_mode, 90 NetworkAnonymizationKey network_anonymization_key, 91 SecureDnsPolicy secure_dns_policy, 92 const SocketTag& socket_tag, 93 const NetLogWithSource& net_log, 94 ClientSocketHandle* socket_handle, 95 CompletionOnceCallback callback, 96 const ClientSocketPool::ProxyAuthCallback& proxy_auth_callback); 97 98 // A helper method that uses the passed in proxy information to initialize a 99 // ClientSocketHandle with the relevant socket pool. Use this method for 100 // HTTP/HTTPS requests for WebSocket handshake. 101 // `ssl_config_for_origin` is only used if the request uses SSL. 102 // `resolution_callback` will be invoked after the the hostname is resolved. If 103 // `resolution_callback` does not return OK, then the connection will be aborted 104 // with that value. This function uses WEBSOCKET_SOCKET_POOL socket pools. 105 int InitSocketHandleForWebSocketRequest( 106 url::SchemeHostPort endpoint, 107 int request_load_flags, 108 RequestPriority request_priority, 109 HttpNetworkSession* session, 110 const ProxyInfo& proxy_info, 111 const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs, 112 PrivacyMode privacy_mode, 113 NetworkAnonymizationKey network_anonymization_key, 114 const NetLogWithSource& net_log, 115 ClientSocketHandle* socket_handle, 116 CompletionOnceCallback callback, 117 const ClientSocketPool::ProxyAuthCallback& proxy_auth_callback); 118 119 // Similar to InitSocketHandleForHttpRequest except that it initiates the 120 // desired number of preconnect streams from the relevant socket pool. 121 int PreconnectSocketsForHttpRequest( 122 url::SchemeHostPort endpoint, 123 int request_load_flags, 124 RequestPriority request_priority, 125 HttpNetworkSession* session, 126 const ProxyInfo& proxy_info, 127 const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs, 128 PrivacyMode privacy_mode, 129 NetworkAnonymizationKey network_anonymization_key, 130 SecureDnsPolicy secure_dns_policy, 131 const NetLogWithSource& net_log, 132 int num_preconnect_streams, 133 CompletionOnceCallback callback); 134 135 } // namespace net 136 137 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_MANAGER_H_ 138