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
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 "net/base/completion_callback.h"
13#include "net/base/net_export.h"
14#include "net/base/request_priority.h"
15#include "net/http/http_network_session.h"
16
17class GURL;
18
19namespace base {
20class Value;
21}
22
23namespace net {
24
25typedef base::Callback<int(const AddressList&, const BoundNetLog& net_log)>
26OnHostResolutionCallback;
27
28class BoundNetLog;
29class ClientSocketHandle;
30class HostPortPair;
31class HttpNetworkSession;
32class HttpProxyClientSocketPool;
33class HttpRequestHeaders;
34class ProxyInfo;
35class TransportClientSocketPool;
36class SOCKSClientSocketPool;
37class SSLClientSocketPool;
38
39struct SSLConfig;
40
41// This should rather be a simple constant but Windows shared libs doesn't
42// really offer much flexiblity in exporting contants.
43enum DefaultMaxValues { kDefaultMaxSocketsPerProxyServer = 32 };
44
45class NET_EXPORT_PRIVATE ClientSocketPoolManager {
46 public:
47  ClientSocketPoolManager();
48  virtual ~ClientSocketPoolManager();
49
50  // The setter methods below affect only newly created socket pools after the
51  // methods are called. Normally they should be called at program startup
52  // before any ClientSocketPoolManagerImpl is created.
53  static int max_sockets_per_pool(HttpNetworkSession::SocketPoolType pool_type);
54  static void set_max_sockets_per_pool(
55      HttpNetworkSession::SocketPoolType pool_type,
56      int socket_count);
57
58  static int max_sockets_per_group(
59      HttpNetworkSession::SocketPoolType pool_type);
60  static void set_max_sockets_per_group(
61      HttpNetworkSession::SocketPoolType pool_type,
62      int socket_count);
63
64  static int max_sockets_per_proxy_server(
65      HttpNetworkSession::SocketPoolType pool_type);
66  static void set_max_sockets_per_proxy_server(
67      HttpNetworkSession::SocketPoolType pool_type,
68      int socket_count);
69
70  virtual void FlushSocketPoolsWithError(int error) = 0;
71  virtual void CloseIdleSockets() = 0;
72  virtual TransportClientSocketPool* GetTransportSocketPool() = 0;
73  virtual SSLClientSocketPool* GetSSLSocketPool() = 0;
74  virtual SOCKSClientSocketPool* GetSocketPoolForSOCKSProxy(
75      const HostPortPair& socks_proxy) = 0;
76  virtual HttpProxyClientSocketPool* GetSocketPoolForHTTPProxy(
77      const HostPortPair& http_proxy) = 0;
78  virtual SSLClientSocketPool* GetSocketPoolForSSLWithProxy(
79      const HostPortPair& proxy_server) = 0;
80  // Creates a Value summary of the state of the socket pools. The caller is
81  // responsible for deleting the returned value.
82  virtual base::Value* SocketPoolInfoToValue() const = 0;
83};
84
85// A helper method that uses the passed in proxy information to initialize a
86// ClientSocketHandle with the relevant socket pool. Use this method for
87// HTTP/HTTPS requests. |ssl_config_for_origin| is only used if the request
88// uses SSL and |ssl_config_for_proxy| is used if the proxy server is HTTPS.
89// |resolution_callback| will be invoked after the the hostname is
90// resolved.  If |resolution_callback| does not return OK, then the
91// connection will be aborted with that value.
92int InitSocketHandleForHttpRequest(
93    const GURL& request_url,
94    const HttpRequestHeaders& request_extra_headers,
95    int request_load_flags,
96    RequestPriority request_priority,
97    HttpNetworkSession* session,
98    const ProxyInfo& proxy_info,
99    bool force_spdy_over_ssl,
100    bool want_spdy_over_npn,
101    const SSLConfig& ssl_config_for_origin,
102    const SSLConfig& ssl_config_for_proxy,
103    PrivacyMode privacy_mode,
104    const BoundNetLog& net_log,
105    ClientSocketHandle* socket_handle,
106    const OnHostResolutionCallback& resolution_callback,
107    const CompletionCallback& callback);
108
109// A helper method that uses the passed in proxy information to initialize a
110// ClientSocketHandle with the relevant socket pool. Use this method for
111// HTTP/HTTPS requests for WebSocket handshake.
112// |ssl_config_for_origin| is only used if the request
113// uses SSL and |ssl_config_for_proxy| is used if the proxy server is HTTPS.
114// |resolution_callback| will be invoked after the the hostname is
115// resolved.  If |resolution_callback| does not return OK, then the
116// connection will be aborted with that value.
117// This function uses WEBSOCKET_SOCKET_POOL socket pools.
118int InitSocketHandleForWebSocketRequest(
119    const GURL& request_url,
120    const HttpRequestHeaders& request_extra_headers,
121    int request_load_flags,
122    RequestPriority request_priority,
123    HttpNetworkSession* session,
124    const ProxyInfo& proxy_info,
125    bool force_spdy_over_ssl,
126    bool want_spdy_over_npn,
127    const SSLConfig& ssl_config_for_origin,
128    const SSLConfig& ssl_config_for_proxy,
129    PrivacyMode privacy_mode,
130    const BoundNetLog& net_log,
131    ClientSocketHandle* socket_handle,
132    const OnHostResolutionCallback& resolution_callback,
133    const CompletionCallback& callback);
134
135// A helper method that uses the passed in proxy information to initialize a
136// ClientSocketHandle with the relevant socket pool. Use this method for
137// a raw socket connection to a host-port pair (that needs to tunnel through
138// the proxies).
139NET_EXPORT int InitSocketHandleForRawConnect(
140    const HostPortPair& host_port_pair,
141    HttpNetworkSession* session,
142    const ProxyInfo& proxy_info,
143    const SSLConfig& ssl_config_for_origin,
144    const SSLConfig& ssl_config_for_proxy,
145    PrivacyMode privacy_mode,
146    const BoundNetLog& net_log,
147    ClientSocketHandle* socket_handle,
148    const CompletionCallback& callback);
149
150// A helper method that uses the passed in proxy information to initialize a
151// ClientSocketHandle with the relevant socket pool. Use this method for
152// a raw socket connection with TLS negotiation to a host-port pair (that needs
153// to tunnel through the proxies).
154NET_EXPORT int InitSocketHandleForTlsConnect(
155    const HostPortPair& host_port_pair,
156    HttpNetworkSession* session,
157    const ProxyInfo& proxy_info,
158    const SSLConfig& ssl_config_for_origin,
159    const SSLConfig& ssl_config_for_proxy,
160    PrivacyMode privacy_mode,
161    const BoundNetLog& net_log,
162    ClientSocketHandle* socket_handle,
163    const CompletionCallback& callback);
164
165// Similar to InitSocketHandleForHttpRequest except that it initiates the
166// desired number of preconnect streams from the relevant socket pool.
167int PreconnectSocketsForHttpRequest(
168    const GURL& request_url,
169    const HttpRequestHeaders& request_extra_headers,
170    int request_load_flags,
171    RequestPriority request_priority,
172    HttpNetworkSession* session,
173    const ProxyInfo& proxy_info,
174    bool force_spdy_over_ssl,
175    bool want_spdy_over_npn,
176    const SSLConfig& ssl_config_for_origin,
177    const SSLConfig& ssl_config_for_proxy,
178    PrivacyMode privacy_mode,
179    const BoundNetLog& net_log,
180    int num_preconnect_streams);
181
182}  // namespace net
183
184#endif  // NET_SOCKET_CLIENT_SOCKET_POOL_MANAGER_H_
185