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.
5 #ifndef NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_
6 #define NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "net/base/privacy_mode.h"
14 #include "net/http/http_response_info.h"
15 #include "net/socket/client_socket_pool.h"
16 #include "net/socket/client_socket_pool_base.h"
17 #include "net/socket/ssl_client_socket.h"
18 #include "net/ssl/ssl_config_service.h"
22 class CertPolicyEnforcer
;
24 class ClientSocketFactory
;
25 class ConnectJobFactory
;
28 class HttpProxyClientSocketPool
;
29 class HttpProxySocketParams
;
30 class SOCKSClientSocketPool
;
31 class SOCKSSocketParams
;
32 class SSLClientSocket
;
33 class TransportClientSocketPool
;
34 class TransportSecurityState
;
35 class TransportSocketParams
;
37 class NET_EXPORT_PRIVATE SSLSocketParams
38 : public base::RefCounted
<SSLSocketParams
> {
40 enum ConnectionType
{ DIRECT
, SOCKS_PROXY
, HTTP_PROXY
};
42 // Exactly one of |direct_params|, |socks_proxy_params|, and
43 // |http_proxy_params| must be non-NULL.
45 const scoped_refptr
<TransportSocketParams
>& direct_params
,
46 const scoped_refptr
<SOCKSSocketParams
>& socks_proxy_params
,
47 const scoped_refptr
<HttpProxySocketParams
>& http_proxy_params
,
48 const HostPortPair
& host_and_port
,
49 const SSLConfig
& ssl_config
,
50 PrivacyMode privacy_mode
,
52 bool force_spdy_over_ssl
,
53 bool want_spdy_over_npn
);
55 // Returns the type of the underlying connection.
56 ConnectionType
GetConnectionType() const;
58 // Must be called only when GetConnectionType() returns DIRECT.
59 const scoped_refptr
<TransportSocketParams
>&
60 GetDirectConnectionParams() const;
62 // Must be called only when GetConnectionType() returns SOCKS_PROXY.
63 const scoped_refptr
<SOCKSSocketParams
>&
64 GetSocksProxyConnectionParams() const;
66 // Must be called only when GetConnectionType() returns HTTP_PROXY.
67 const scoped_refptr
<HttpProxySocketParams
>&
68 GetHttpProxyConnectionParams() const;
70 const HostPortPair
& host_and_port() const { return host_and_port_
; }
71 const SSLConfig
& ssl_config() const { return ssl_config_
; }
72 PrivacyMode
privacy_mode() const { return privacy_mode_
; }
73 int load_flags() const { return load_flags_
; }
74 bool force_spdy_over_ssl() const { return force_spdy_over_ssl_
; }
75 bool want_spdy_over_npn() const { return want_spdy_over_npn_
; }
76 bool ignore_limits() const { return ignore_limits_
; }
79 friend class base::RefCounted
<SSLSocketParams
>;
82 const scoped_refptr
<TransportSocketParams
> direct_params_
;
83 const scoped_refptr
<SOCKSSocketParams
> socks_proxy_params_
;
84 const scoped_refptr
<HttpProxySocketParams
> http_proxy_params_
;
85 const HostPortPair host_and_port_
;
86 const SSLConfig ssl_config_
;
87 const PrivacyMode privacy_mode_
;
88 const int load_flags_
;
89 const bool force_spdy_over_ssl_
;
90 const bool want_spdy_over_npn_
;
93 DISALLOW_COPY_AND_ASSIGN(SSLSocketParams
);
96 // SSLConnectJob handles the SSL handshake after setting up the underlying
97 // connection as specified in the params.
98 class SSLConnectJob
: public ConnectJob
{
100 // Note: the SSLConnectJob does not own |messenger| so it must outlive the
102 SSLConnectJob(const std::string
& group_name
,
103 RequestPriority priority
,
104 const scoped_refptr
<SSLSocketParams
>& params
,
105 const base::TimeDelta
& timeout_duration
,
106 TransportClientSocketPool
* transport_pool
,
107 SOCKSClientSocketPool
* socks_pool
,
108 HttpProxyClientSocketPool
* http_proxy_pool
,
109 ClientSocketFactory
* client_socket_factory
,
110 const SSLClientSocketContext
& context
,
113 ~SSLConnectJob() override
;
115 // ConnectJob methods.
116 LoadState
GetLoadState() const override
;
118 void GetAdditionalErrorState(ClientSocketHandle
* handle
) override
;
122 STATE_TRANSPORT_CONNECT
,
123 STATE_TRANSPORT_CONNECT_COMPLETE
,
125 STATE_SOCKS_CONNECT_COMPLETE
,
126 STATE_TUNNEL_CONNECT
,
127 STATE_TUNNEL_CONNECT_COMPLETE
,
129 STATE_SSL_CONNECT_COMPLETE
,
133 void OnIOComplete(int result
);
135 // Runs the state transition loop.
136 int DoLoop(int result
);
138 int DoTransportConnect();
139 int DoTransportConnectComplete(int result
);
140 int DoSOCKSConnect();
141 int DoSOCKSConnectComplete(int result
);
142 int DoTunnelConnect();
143 int DoTunnelConnectComplete(int result
);
145 int DoSSLConnectComplete(int result
);
147 // Returns the initial state for the state machine based on the
148 // |connection_type|.
149 static State
GetInitialState(SSLSocketParams::ConnectionType connection_type
);
151 // Starts the SSL connection process. Returns OK on success and
152 // ERR_IO_PENDING if it cannot immediately service the request.
153 // Otherwise, it returns a net error code.
154 int ConnectInternal() override
;
156 scoped_refptr
<SSLSocketParams
> params_
;
157 TransportClientSocketPool
* const transport_pool_
;
158 SOCKSClientSocketPool
* const socks_pool_
;
159 HttpProxyClientSocketPool
* const http_proxy_pool_
;
160 ClientSocketFactory
* const client_socket_factory_
;
162 const SSLClientSocketContext context_
;
165 CompletionCallback callback_
;
166 scoped_ptr
<ClientSocketHandle
> transport_socket_handle_
;
167 scoped_ptr
<SSLClientSocket
> ssl_socket_
;
169 HttpResponseInfo error_response_info_
;
171 DISALLOW_COPY_AND_ASSIGN(SSLConnectJob
);
174 class NET_EXPORT_PRIVATE SSLClientSocketPool
175 : public ClientSocketPool
,
176 public HigherLayeredPool
,
177 public SSLConfigService::Observer
{
179 typedef SSLSocketParams SocketParams
;
181 // Only the pools that will be used are required. i.e. if you never
182 // try to create an SSL over SOCKS socket, |socks_pool| may be NULL.
183 SSLClientSocketPool(int max_sockets
,
184 int max_sockets_per_group
,
185 CertVerifier
* cert_verifier
,
186 ChannelIDService
* channel_id_service
,
187 TransportSecurityState
* transport_security_state
,
188 CTVerifier
* cert_transparency_verifier
,
189 CertPolicyEnforcer
* cert_policy_enforcer
,
190 const std::string
& ssl_session_cache_shard
,
191 ClientSocketFactory
* client_socket_factory
,
192 TransportClientSocketPool
* transport_pool
,
193 SOCKSClientSocketPool
* socks_pool
,
194 HttpProxyClientSocketPool
* http_proxy_pool
,
195 SSLConfigService
* ssl_config_service
,
198 ~SSLClientSocketPool() override
;
200 // ClientSocketPool implementation.
201 int RequestSocket(const std::string
& group_name
,
202 const void* connect_params
,
203 RequestPriority priority
,
204 ClientSocketHandle
* handle
,
205 const CompletionCallback
& callback
,
206 const BoundNetLog
& net_log
) override
;
208 void RequestSockets(const std::string
& group_name
,
211 const BoundNetLog
& net_log
) override
;
213 void CancelRequest(const std::string
& group_name
,
214 ClientSocketHandle
* handle
) override
;
216 void ReleaseSocket(const std::string
& group_name
,
217 scoped_ptr
<StreamSocket
> socket
,
220 void FlushWithError(int error
) override
;
222 void CloseIdleSockets() override
;
224 int IdleSocketCount() const override
;
226 int IdleSocketCountInGroup(const std::string
& group_name
) const override
;
228 LoadState
GetLoadState(const std::string
& group_name
,
229 const ClientSocketHandle
* handle
) const override
;
231 base::DictionaryValue
* GetInfoAsValue(
232 const std::string
& name
,
233 const std::string
& type
,
234 bool include_nested_pools
) const override
;
236 base::TimeDelta
ConnectionTimeout() const override
;
238 // LowerLayeredPool implementation.
239 bool IsStalled() const override
;
241 void AddHigherLayeredPool(HigherLayeredPool
* higher_pool
) override
;
243 void RemoveHigherLayeredPool(HigherLayeredPool
* higher_pool
) override
;
245 // HigherLayeredPool implementation.
246 bool CloseOneIdleConnection() override
;
249 typedef ClientSocketPoolBase
<SSLSocketParams
> PoolBase
;
251 // SSLConfigService::Observer implementation.
253 // When the user changes the SSL config, we flush all idle sockets so they
254 // won't get re-used.
255 void OnSSLConfigChanged() override
;
257 class SSLConnectJobFactory
: public PoolBase::ConnectJobFactory
{
259 SSLConnectJobFactory(
260 TransportClientSocketPool
* transport_pool
,
261 SOCKSClientSocketPool
* socks_pool
,
262 HttpProxyClientSocketPool
* http_proxy_pool
,
263 ClientSocketFactory
* client_socket_factory
,
264 const SSLClientSocketContext
& context
,
267 ~SSLConnectJobFactory() override
;
269 // ClientSocketPoolBase::ConnectJobFactory methods.
270 scoped_ptr
<ConnectJob
> NewConnectJob(
271 const std::string
& group_name
,
272 const PoolBase::Request
& request
,
273 ConnectJob::Delegate
* delegate
) const override
;
275 base::TimeDelta
ConnectionTimeout() const override
;
278 TransportClientSocketPool
* const transport_pool_
;
279 SOCKSClientSocketPool
* const socks_pool_
;
280 HttpProxyClientSocketPool
* const http_proxy_pool_
;
281 ClientSocketFactory
* const client_socket_factory_
;
282 const SSLClientSocketContext context_
;
283 base::TimeDelta timeout_
;
286 DISALLOW_COPY_AND_ASSIGN(SSLConnectJobFactory
);
289 TransportClientSocketPool
* const transport_pool_
;
290 SOCKSClientSocketPool
* const socks_pool_
;
291 HttpProxyClientSocketPool
* const http_proxy_pool_
;
293 const scoped_refptr
<SSLConfigService
> ssl_config_service_
;
295 DISALLOW_COPY_AND_ASSIGN(SSLClientSocketPool
);
300 #endif // NET_SOCKET_SSL_CLIENT_SOCKET_POOL_H_