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 #include "net/socket/ssl_client_socket_pool.h"
8 #include "base/bind_helpers.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/metrics/histogram.h"
11 #include "base/metrics/sparse_histogram.h"
12 #include "base/stl_util.h"
13 #include "base/values.h"
14 #include "net/base/host_port_pair.h"
15 #include "net/base/net_errors.h"
16 #include "net/http/http_proxy_client_socket.h"
17 #include "net/http/http_proxy_client_socket_pool.h"
18 #include "net/socket/client_socket_factory.h"
19 #include "net/socket/client_socket_handle.h"
20 #include "net/socket/socks_client_socket_pool.h"
21 #include "net/socket/ssl_client_socket.h"
22 #include "net/socket/transport_client_socket_pool.h"
23 #include "net/ssl/ssl_cert_request_info.h"
24 #include "net/ssl/ssl_connection_status_flags.h"
25 #include "net/ssl/ssl_info.h"
29 SSLSocketParams::SSLSocketParams(
30 const scoped_refptr
<TransportSocketParams
>& direct_params
,
31 const scoped_refptr
<SOCKSSocketParams
>& socks_proxy_params
,
32 const scoped_refptr
<HttpProxySocketParams
>& http_proxy_params
,
33 const HostPortPair
& host_and_port
,
34 const SSLConfig
& ssl_config
,
35 PrivacyMode privacy_mode
,
37 bool force_spdy_over_ssl
,
38 bool want_spdy_over_npn
)
39 : direct_params_(direct_params
),
40 socks_proxy_params_(socks_proxy_params
),
41 http_proxy_params_(http_proxy_params
),
42 host_and_port_(host_and_port
),
43 ssl_config_(ssl_config
),
44 privacy_mode_(privacy_mode
),
45 load_flags_(load_flags
),
46 force_spdy_over_ssl_(force_spdy_over_ssl
),
47 want_spdy_over_npn_(want_spdy_over_npn
),
48 ignore_limits_(false) {
50 DCHECK(!socks_proxy_params_
);
51 DCHECK(!http_proxy_params_
);
52 ignore_limits_
= direct_params_
->ignore_limits();
53 } else if (socks_proxy_params_
) {
54 DCHECK(!http_proxy_params_
);
55 ignore_limits_
= socks_proxy_params_
->ignore_limits();
57 DCHECK(http_proxy_params_
);
58 ignore_limits_
= http_proxy_params_
->ignore_limits();
62 SSLSocketParams::~SSLSocketParams() {}
64 SSLSocketParams::ConnectionType
SSLSocketParams::GetConnectionType() const {
66 DCHECK(!socks_proxy_params_
);
67 DCHECK(!http_proxy_params_
);
71 if (socks_proxy_params_
) {
72 DCHECK(!http_proxy_params_
);
76 DCHECK(http_proxy_params_
);
80 const scoped_refptr
<TransportSocketParams
>&
81 SSLSocketParams::GetDirectConnectionParams() const {
82 DCHECK_EQ(GetConnectionType(), DIRECT
);
83 return direct_params_
;
86 const scoped_refptr
<SOCKSSocketParams
>&
87 SSLSocketParams::GetSocksProxyConnectionParams() const {
88 DCHECK_EQ(GetConnectionType(), SOCKS_PROXY
);
89 return socks_proxy_params_
;
92 const scoped_refptr
<HttpProxySocketParams
>&
93 SSLSocketParams::GetHttpProxyConnectionParams() const {
94 DCHECK_EQ(GetConnectionType(), HTTP_PROXY
);
95 return http_proxy_params_
;
98 SSLConnectJobMessenger::SocketAndCallback::SocketAndCallback(
99 SSLClientSocket
* ssl_socket
,
100 const base::Closure
& job_resumption_callback
)
101 : socket(ssl_socket
), callback(job_resumption_callback
) {
104 SSLConnectJobMessenger::SocketAndCallback::~SocketAndCallback() {
107 SSLConnectJobMessenger::SSLConnectJobMessenger(
108 const base::Closure
& messenger_finished_callback
)
109 : messenger_finished_callback_(messenger_finished_callback
),
110 weak_factory_(this) {
113 SSLConnectJobMessenger::~SSLConnectJobMessenger() {
116 void SSLConnectJobMessenger::RemovePendingSocket(SSLClientSocket
* ssl_socket
) {
117 // Sockets do not need to be removed from connecting_sockets_ because
118 // OnSSLHandshakeCompleted will do this.
119 for (SSLPendingSocketsAndCallbacks::iterator it
=
120 pending_sockets_and_callbacks_
.begin();
121 it
!= pending_sockets_and_callbacks_
.end();
123 if (it
->socket
== ssl_socket
) {
124 pending_sockets_and_callbacks_
.erase(it
);
130 bool SSLConnectJobMessenger::CanProceed(SSLClientSocket
* ssl_socket
) {
131 // If there are no connecting sockets, allow the connection to proceed.
132 return connecting_sockets_
.empty();
135 void SSLConnectJobMessenger::MonitorConnectionResult(
136 SSLClientSocket
* ssl_socket
) {
137 connecting_sockets_
.push_back(ssl_socket
);
138 ssl_socket
->SetHandshakeCompletionCallback(
139 base::Bind(&SSLConnectJobMessenger::OnSSLHandshakeCompleted
,
140 weak_factory_
.GetWeakPtr()));
143 void SSLConnectJobMessenger::AddPendingSocket(SSLClientSocket
* ssl_socket
,
144 const base::Closure
& callback
) {
145 DCHECK(!connecting_sockets_
.empty());
146 pending_sockets_and_callbacks_
.push_back(
147 SocketAndCallback(ssl_socket
, callback
));
150 void SSLConnectJobMessenger::OnSSLHandshakeCompleted() {
151 connecting_sockets_
.clear();
152 SSLPendingSocketsAndCallbacks temp_list
;
153 temp_list
.swap(pending_sockets_and_callbacks_
);
154 base::Closure messenger_finished_callback
= messenger_finished_callback_
;
155 messenger_finished_callback
.Run();
156 RunAllCallbacks(temp_list
);
159 void SSLConnectJobMessenger::RunAllCallbacks(
160 const SSLPendingSocketsAndCallbacks
& pending_sockets_and_callbacks
) {
161 for (std::vector
<SocketAndCallback
>::const_iterator it
=
162 pending_sockets_and_callbacks
.begin();
163 it
!= pending_sockets_and_callbacks
.end();
169 // Timeout for the SSL handshake portion of the connect.
170 static const int kSSLHandshakeTimeoutInSeconds
= 30;
172 SSLConnectJob::SSLConnectJob(const std::string
& group_name
,
173 RequestPriority priority
,
174 const scoped_refptr
<SSLSocketParams
>& params
,
175 const base::TimeDelta
& timeout_duration
,
176 TransportClientSocketPool
* transport_pool
,
177 SOCKSClientSocketPool
* socks_pool
,
178 HttpProxyClientSocketPool
* http_proxy_pool
,
179 ClientSocketFactory
* client_socket_factory
,
180 HostResolver
* host_resolver
,
181 const SSLClientSocketContext
& context
,
182 const GetMessengerCallback
& get_messenger_callback
,
185 : ConnectJob(group_name
,
189 BoundNetLog::Make(net_log
, NetLog::SOURCE_CONNECT_JOB
)),
191 transport_pool_(transport_pool
),
192 socks_pool_(socks_pool
),
193 http_proxy_pool_(http_proxy_pool
),
194 client_socket_factory_(client_socket_factory
),
195 host_resolver_(host_resolver
),
196 context_(context
.cert_verifier
,
197 context
.channel_id_service
,
198 context
.transport_security_state
,
199 context
.cert_transparency_verifier
,
200 (params
->privacy_mode() == PRIVACY_MODE_ENABLED
201 ? "pm/" + context
.ssl_session_cache_shard
202 : context
.ssl_session_cache_shard
)),
204 base::Bind(&SSLConnectJob::OnIOComplete
, base::Unretained(this))),
206 get_messenger_callback_(get_messenger_callback
),
207 weak_factory_(this) {
210 SSLConnectJob::~SSLConnectJob() {
211 if (ssl_socket_
.get() && messenger_
)
212 messenger_
->RemovePendingSocket(ssl_socket_
.get());
215 LoadState
SSLConnectJob::GetLoadState() const {
216 switch (next_state_
) {
217 case STATE_TUNNEL_CONNECT_COMPLETE
:
218 if (transport_socket_handle_
->socket())
219 return LOAD_STATE_ESTABLISHING_PROXY_TUNNEL
;
220 // else, fall through.
221 case STATE_TRANSPORT_CONNECT
:
222 case STATE_TRANSPORT_CONNECT_COMPLETE
:
223 case STATE_SOCKS_CONNECT
:
224 case STATE_SOCKS_CONNECT_COMPLETE
:
225 case STATE_TUNNEL_CONNECT
:
226 return transport_socket_handle_
->GetLoadState();
227 case STATE_CREATE_SSL_SOCKET
:
228 case STATE_CHECK_FOR_RESUME
:
229 case STATE_SSL_CONNECT
:
230 case STATE_SSL_CONNECT_COMPLETE
:
231 return LOAD_STATE_SSL_HANDSHAKE
;
234 return LOAD_STATE_IDLE
;
238 void SSLConnectJob::GetAdditionalErrorState(ClientSocketHandle
* handle
) {
239 // Headers in |error_response_info_| indicate a proxy tunnel setup
240 // problem. See DoTunnelConnectComplete.
241 if (error_response_info_
.headers
.get()) {
242 handle
->set_pending_http_proxy_connection(
243 transport_socket_handle_
.release());
245 handle
->set_ssl_error_response_info(error_response_info_
);
246 if (!connect_timing_
.ssl_start
.is_null())
247 handle
->set_is_ssl_error(true);
250 void SSLConnectJob::OnIOComplete(int result
) {
251 int rv
= DoLoop(result
);
252 if (rv
!= ERR_IO_PENDING
)
253 NotifyDelegateOfCompletion(rv
); // Deletes |this|.
256 int SSLConnectJob::DoLoop(int result
) {
257 DCHECK_NE(next_state_
, STATE_NONE
);
261 State state
= next_state_
;
262 next_state_
= STATE_NONE
;
264 case STATE_TRANSPORT_CONNECT
:
266 rv
= DoTransportConnect();
268 case STATE_TRANSPORT_CONNECT_COMPLETE
:
269 rv
= DoTransportConnectComplete(rv
);
271 case STATE_SOCKS_CONNECT
:
273 rv
= DoSOCKSConnect();
275 case STATE_SOCKS_CONNECT_COMPLETE
:
276 rv
= DoSOCKSConnectComplete(rv
);
278 case STATE_TUNNEL_CONNECT
:
280 rv
= DoTunnelConnect();
282 case STATE_TUNNEL_CONNECT_COMPLETE
:
283 rv
= DoTunnelConnectComplete(rv
);
285 case STATE_CREATE_SSL_SOCKET
:
286 rv
= DoCreateSSLSocket();
288 case STATE_CHECK_FOR_RESUME
:
289 rv
= DoCheckForResume();
291 case STATE_SSL_CONNECT
:
295 case STATE_SSL_CONNECT_COMPLETE
:
296 rv
= DoSSLConnectComplete(rv
);
299 NOTREACHED() << "bad state";
303 } while (rv
!= ERR_IO_PENDING
&& next_state_
!= STATE_NONE
);
308 int SSLConnectJob::DoTransportConnect() {
309 DCHECK(transport_pool_
);
311 next_state_
= STATE_TRANSPORT_CONNECT_COMPLETE
;
312 transport_socket_handle_
.reset(new ClientSocketHandle());
313 scoped_refptr
<TransportSocketParams
> direct_params
=
314 params_
->GetDirectConnectionParams();
315 return transport_socket_handle_
->Init(group_name(),
323 int SSLConnectJob::DoTransportConnectComplete(int result
) {
325 next_state_
= STATE_CREATE_SSL_SOCKET
;
330 int SSLConnectJob::DoSOCKSConnect() {
332 next_state_
= STATE_SOCKS_CONNECT_COMPLETE
;
333 transport_socket_handle_
.reset(new ClientSocketHandle());
334 scoped_refptr
<SOCKSSocketParams
> socks_proxy_params
=
335 params_
->GetSocksProxyConnectionParams();
336 return transport_socket_handle_
->Init(group_name(),
344 int SSLConnectJob::DoSOCKSConnectComplete(int result
) {
346 next_state_
= STATE_CREATE_SSL_SOCKET
;
351 int SSLConnectJob::DoTunnelConnect() {
352 DCHECK(http_proxy_pool_
);
353 next_state_
= STATE_TUNNEL_CONNECT_COMPLETE
;
355 transport_socket_handle_
.reset(new ClientSocketHandle());
356 scoped_refptr
<HttpProxySocketParams
> http_proxy_params
=
357 params_
->GetHttpProxyConnectionParams();
358 return transport_socket_handle_
->Init(group_name(),
366 int SSLConnectJob::DoTunnelConnectComplete(int result
) {
367 // Extract the information needed to prompt for appropriate proxy
368 // authentication so that when ClientSocketPoolBaseHelper calls
369 // |GetAdditionalErrorState|, we can easily set the state.
370 if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
371 error_response_info_
= transport_socket_handle_
->ssl_error_response_info();
372 } else if (result
== ERR_PROXY_AUTH_REQUESTED
||
373 result
== ERR_HTTPS_PROXY_TUNNEL_RESPONSE
) {
374 StreamSocket
* socket
= transport_socket_handle_
->socket();
375 HttpProxyClientSocket
* tunnel_socket
=
376 static_cast<HttpProxyClientSocket
*>(socket
);
377 error_response_info_
= *tunnel_socket
->GetConnectResponseInfo();
381 next_state_
= STATE_CREATE_SSL_SOCKET
;
385 int SSLConnectJob::DoCreateSSLSocket() {
386 next_state_
= STATE_CHECK_FOR_RESUME
;
388 // Reset the timeout to just the time allowed for the SSL handshake.
389 ResetTimer(base::TimeDelta::FromSeconds(kSSLHandshakeTimeoutInSeconds
));
391 // If the handle has a fresh socket, get its connect start and DNS times.
392 // This should always be the case.
393 const LoadTimingInfo::ConnectTiming
& socket_connect_timing
=
394 transport_socket_handle_
->connect_timing();
395 if (!transport_socket_handle_
->is_reused() &&
396 !socket_connect_timing
.connect_start
.is_null()) {
397 // Overwriting |connect_start| serves two purposes - it adjusts timing so
398 // |connect_start| doesn't include dns times, and it adjusts the time so
399 // as not to include time spent waiting for an idle socket.
400 connect_timing_
.connect_start
= socket_connect_timing
.connect_start
;
401 connect_timing_
.dns_start
= socket_connect_timing
.dns_start
;
402 connect_timing_
.dns_end
= socket_connect_timing
.dns_end
;
405 ssl_socket_
= client_socket_factory_
->CreateSSLClientSocket(
406 transport_socket_handle_
.Pass(),
407 params_
->host_and_port(),
408 params_
->ssl_config(),
411 if (!ssl_socket_
->InSessionCache())
412 messenger_
= get_messenger_callback_
.Run(ssl_socket_
->GetSessionCacheKey());
417 int SSLConnectJob::DoCheckForResume() {
418 next_state_
= STATE_SSL_CONNECT
;
423 if (messenger_
->CanProceed(ssl_socket_
.get())) {
424 messenger_
->MonitorConnectionResult(ssl_socket_
.get());
425 // The SSLConnectJob no longer needs access to the messenger after this
431 messenger_
->AddPendingSocket(ssl_socket_
.get(),
432 base::Bind(&SSLConnectJob::ResumeSSLConnection
,
433 weak_factory_
.GetWeakPtr()));
435 return ERR_IO_PENDING
;
438 int SSLConnectJob::DoSSLConnect() {
439 next_state_
= STATE_SSL_CONNECT_COMPLETE
;
441 connect_timing_
.ssl_start
= base::TimeTicks::Now();
443 return ssl_socket_
->Connect(io_callback_
);
446 int SSLConnectJob::DoSSLConnectComplete(int result
) {
447 connect_timing_
.ssl_end
= base::TimeTicks::Now();
449 SSLClientSocket::NextProtoStatus status
=
450 SSLClientSocket::kNextProtoUnsupported
;
452 // GetNextProto will fail and and trigger a NOTREACHED if we pass in a socket
453 // that hasn't had SSL_ImportFD called on it. If we get a certificate error
454 // here, then we know that we called SSL_ImportFD.
455 if (result
== OK
|| IsCertificateError(result
))
456 status
= ssl_socket_
->GetNextProto(&proto
);
458 // If we want spdy over npn, make sure it succeeded.
459 if (status
== SSLClientSocket::kNextProtoNegotiated
) {
460 ssl_socket_
->set_was_npn_negotiated(true);
461 NextProto protocol_negotiated
=
462 SSLClientSocket::NextProtoFromString(proto
);
463 ssl_socket_
->set_protocol_negotiated(protocol_negotiated
);
464 // If we negotiated a SPDY version, it must have been present in
465 // SSLConfig::next_protos.
466 // TODO(mbelshe): Verify this.
467 if (protocol_negotiated
>= kProtoSPDYMinimumVersion
&&
468 protocol_negotiated
<= kProtoSPDYMaximumVersion
) {
469 ssl_socket_
->set_was_spdy_negotiated(true);
472 if (params_
->want_spdy_over_npn() && !ssl_socket_
->was_spdy_negotiated())
473 return ERR_NPN_NEGOTIATION_FAILED
;
475 // Spdy might be turned on by default, or it might be over npn.
476 bool using_spdy
= params_
->force_spdy_over_ssl() ||
477 params_
->want_spdy_over_npn();
480 ssl_socket_
->IgnoreCertError(result
, params_
->load_flags())) {
481 DCHECK(!connect_timing_
.ssl_start
.is_null());
482 base::TimeDelta connect_duration
=
483 connect_timing_
.ssl_end
- connect_timing_
.ssl_start
;
485 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SpdyConnectionLatency_2",
487 base::TimeDelta::FromMilliseconds(1),
488 base::TimeDelta::FromMinutes(1),
491 #if defined(SPDY_PROXY_AUTH_ORIGIN)
492 bool using_data_reduction_proxy
= params_
->host_and_port().Equals(
493 HostPortPair::FromURL(GURL(SPDY_PROXY_AUTH_ORIGIN
)));
494 if (using_data_reduction_proxy
) {
495 UMA_HISTOGRAM_CUSTOM_TIMES(
496 "Net.SSL_Connection_Latency_DataReductionProxy",
498 base::TimeDelta::FromMilliseconds(1),
499 base::TimeDelta::FromMinutes(1),
504 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_2",
506 base::TimeDelta::FromMilliseconds(1),
507 base::TimeDelta::FromMinutes(1),
511 ssl_socket_
->GetSSLInfo(&ssl_info
);
513 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSL_CipherSuite",
514 SSLConnectionStatusToCipherSuite(
515 ssl_info
.connection_status
));
517 UMA_HISTOGRAM_BOOLEAN(
518 "Net.RenegotiationExtensionSupported",
519 (ssl_info
.connection_status
&
520 SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION
) == 0);
522 if (ssl_info
.handshake_type
== SSLInfo::HANDSHAKE_RESUME
) {
523 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Resume_Handshake",
525 base::TimeDelta::FromMilliseconds(1),
526 base::TimeDelta::FromMinutes(1),
528 } else if (ssl_info
.handshake_type
== SSLInfo::HANDSHAKE_FULL
) {
529 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Full_Handshake",
531 base::TimeDelta::FromMilliseconds(1),
532 base::TimeDelta::FromMinutes(1),
536 const std::string
& host
= params_
->host_and_port().host();
538 host
== "google.com" ||
539 (host
.size() > 11 && host
.rfind(".google.com") == host
.size() - 11);
541 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google2",
543 base::TimeDelta::FromMilliseconds(1),
544 base::TimeDelta::FromMinutes(1),
546 if (ssl_info
.handshake_type
== SSLInfo::HANDSHAKE_RESUME
) {
547 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google_"
550 base::TimeDelta::FromMilliseconds(1),
551 base::TimeDelta::FromMinutes(1),
553 } else if (ssl_info
.handshake_type
== SSLInfo::HANDSHAKE_FULL
) {
554 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google_"
557 base::TimeDelta::FromMilliseconds(1),
558 base::TimeDelta::FromMinutes(1),
564 if (result
== OK
|| IsCertificateError(result
)) {
565 SetSocket(ssl_socket_
.PassAs
<StreamSocket
>());
566 } else if (result
== ERR_SSL_CLIENT_AUTH_CERT_NEEDED
) {
567 error_response_info_
.cert_request_info
= new SSLCertRequestInfo
;
568 ssl_socket_
->GetSSLCertRequestInfo(
569 error_response_info_
.cert_request_info
.get());
575 void SSLConnectJob::ResumeSSLConnection() {
576 DCHECK_EQ(next_state_
, STATE_SSL_CONNECT
);
581 SSLConnectJob::State
SSLConnectJob::GetInitialState(
582 SSLSocketParams::ConnectionType connection_type
) {
583 switch (connection_type
) {
584 case SSLSocketParams::DIRECT
:
585 return STATE_TRANSPORT_CONNECT
;
586 case SSLSocketParams::HTTP_PROXY
:
587 return STATE_TUNNEL_CONNECT
;
588 case SSLSocketParams::SOCKS_PROXY
:
589 return STATE_SOCKS_CONNECT
;
595 int SSLConnectJob::ConnectInternal() {
596 next_state_
= GetInitialState(params_
->GetConnectionType());
600 SSLClientSocketPool::SSLConnectJobFactory::SSLConnectJobFactory(
601 TransportClientSocketPool
* transport_pool
,
602 SOCKSClientSocketPool
* socks_pool
,
603 HttpProxyClientSocketPool
* http_proxy_pool
,
604 ClientSocketFactory
* client_socket_factory
,
605 HostResolver
* host_resolver
,
606 const SSLClientSocketContext
& context
,
607 const SSLConnectJob::GetMessengerCallback
& get_messenger_callback
,
609 : transport_pool_(transport_pool
),
610 socks_pool_(socks_pool
),
611 http_proxy_pool_(http_proxy_pool
),
612 client_socket_factory_(client_socket_factory
),
613 host_resolver_(host_resolver
),
615 get_messenger_callback_(get_messenger_callback
),
617 base::TimeDelta max_transport_timeout
= base::TimeDelta();
618 base::TimeDelta pool_timeout
;
620 max_transport_timeout
= transport_pool_
->ConnectionTimeout();
622 pool_timeout
= socks_pool_
->ConnectionTimeout();
623 if (pool_timeout
> max_transport_timeout
)
624 max_transport_timeout
= pool_timeout
;
626 if (http_proxy_pool_
) {
627 pool_timeout
= http_proxy_pool_
->ConnectionTimeout();
628 if (pool_timeout
> max_transport_timeout
)
629 max_transport_timeout
= pool_timeout
;
631 timeout_
= max_transport_timeout
+
632 base::TimeDelta::FromSeconds(kSSLHandshakeTimeoutInSeconds
);
635 SSLClientSocketPool::SSLConnectJobFactory::~SSLConnectJobFactory() {
638 SSLClientSocketPool::SSLClientSocketPool(
640 int max_sockets_per_group
,
641 ClientSocketPoolHistograms
* histograms
,
642 HostResolver
* host_resolver
,
643 CertVerifier
* cert_verifier
,
644 ChannelIDService
* channel_id_service
,
645 TransportSecurityState
* transport_security_state
,
646 CTVerifier
* cert_transparency_verifier
,
647 const std::string
& ssl_session_cache_shard
,
648 ClientSocketFactory
* client_socket_factory
,
649 TransportClientSocketPool
* transport_pool
,
650 SOCKSClientSocketPool
* socks_pool
,
651 HttpProxyClientSocketPool
* http_proxy_pool
,
652 SSLConfigService
* ssl_config_service
,
653 bool enable_ssl_connect_job_waiting
,
655 : transport_pool_(transport_pool
),
656 socks_pool_(socks_pool
),
657 http_proxy_pool_(http_proxy_pool
),
660 max_sockets_per_group
,
662 ClientSocketPool::unused_idle_socket_timeout(),
663 ClientSocketPool::used_idle_socket_timeout(),
664 new SSLConnectJobFactory(
668 client_socket_factory
,
670 SSLClientSocketContext(cert_verifier
,
672 transport_security_state
,
673 cert_transparency_verifier
,
674 ssl_session_cache_shard
),
676 &SSLClientSocketPool::GetOrCreateSSLConnectJobMessenger
,
677 base::Unretained(this)),
679 ssl_config_service_(ssl_config_service
),
680 enable_ssl_connect_job_waiting_(enable_ssl_connect_job_waiting
) {
681 if (ssl_config_service_
.get())
682 ssl_config_service_
->AddObserver(this);
684 base_
.AddLowerLayeredPool(transport_pool_
);
686 base_
.AddLowerLayeredPool(socks_pool_
);
687 if (http_proxy_pool_
)
688 base_
.AddLowerLayeredPool(http_proxy_pool_
);
691 SSLClientSocketPool::~SSLClientSocketPool() {
692 STLDeleteContainerPairSecondPointers(messenger_map_
.begin(),
693 messenger_map_
.end());
694 if (ssl_config_service_
.get())
695 ssl_config_service_
->RemoveObserver(this);
698 scoped_ptr
<ConnectJob
> SSLClientSocketPool::SSLConnectJobFactory::NewConnectJob(
699 const std::string
& group_name
,
700 const PoolBase::Request
& request
,
701 ConnectJob::Delegate
* delegate
) const {
702 return scoped_ptr
<ConnectJob
>(new SSLConnectJob(group_name
,
709 client_socket_factory_
,
712 get_messenger_callback_
,
717 base::TimeDelta
SSLClientSocketPool::SSLConnectJobFactory::ConnectionTimeout()
722 int SSLClientSocketPool::RequestSocket(const std::string
& group_name
,
723 const void* socket_params
,
724 RequestPriority priority
,
725 ClientSocketHandle
* handle
,
726 const CompletionCallback
& callback
,
727 const BoundNetLog
& net_log
) {
728 const scoped_refptr
<SSLSocketParams
>* casted_socket_params
=
729 static_cast<const scoped_refptr
<SSLSocketParams
>*>(socket_params
);
731 return base_
.RequestSocket(group_name
, *casted_socket_params
, priority
,
732 handle
, callback
, net_log
);
735 void SSLClientSocketPool::RequestSockets(
736 const std::string
& group_name
,
739 const BoundNetLog
& net_log
) {
740 const scoped_refptr
<SSLSocketParams
>* casted_params
=
741 static_cast<const scoped_refptr
<SSLSocketParams
>*>(params
);
743 base_
.RequestSockets(group_name
, *casted_params
, num_sockets
, net_log
);
746 void SSLClientSocketPool::CancelRequest(const std::string
& group_name
,
747 ClientSocketHandle
* handle
) {
748 base_
.CancelRequest(group_name
, handle
);
751 void SSLClientSocketPool::ReleaseSocket(const std::string
& group_name
,
752 scoped_ptr
<StreamSocket
> socket
,
754 base_
.ReleaseSocket(group_name
, socket
.Pass(), id
);
757 void SSLClientSocketPool::FlushWithError(int error
) {
758 base_
.FlushWithError(error
);
761 void SSLClientSocketPool::CloseIdleSockets() {
762 base_
.CloseIdleSockets();
765 int SSLClientSocketPool::IdleSocketCount() const {
766 return base_
.idle_socket_count();
769 int SSLClientSocketPool::IdleSocketCountInGroup(
770 const std::string
& group_name
) const {
771 return base_
.IdleSocketCountInGroup(group_name
);
774 LoadState
SSLClientSocketPool::GetLoadState(
775 const std::string
& group_name
, const ClientSocketHandle
* handle
) const {
776 return base_
.GetLoadState(group_name
, handle
);
779 base::DictionaryValue
* SSLClientSocketPool::GetInfoAsValue(
780 const std::string
& name
,
781 const std::string
& type
,
782 bool include_nested_pools
) const {
783 base::DictionaryValue
* dict
= base_
.GetInfoAsValue(name
, type
);
784 if (include_nested_pools
) {
785 base::ListValue
* list
= new base::ListValue();
786 if (transport_pool_
) {
787 list
->Append(transport_pool_
->GetInfoAsValue("transport_socket_pool",
788 "transport_socket_pool",
792 list
->Append(socks_pool_
->GetInfoAsValue("socks_pool",
796 if (http_proxy_pool_
) {
797 list
->Append(http_proxy_pool_
->GetInfoAsValue("http_proxy_pool",
801 dict
->Set("nested_pools", list
);
806 base::TimeDelta
SSLClientSocketPool::ConnectionTimeout() const {
807 return base_
.ConnectionTimeout();
810 ClientSocketPoolHistograms
* SSLClientSocketPool::histograms() const {
811 return base_
.histograms();
814 bool SSLClientSocketPool::IsStalled() const {
815 return base_
.IsStalled();
818 void SSLClientSocketPool::AddHigherLayeredPool(HigherLayeredPool
* higher_pool
) {
819 base_
.AddHigherLayeredPool(higher_pool
);
822 void SSLClientSocketPool::RemoveHigherLayeredPool(
823 HigherLayeredPool
* higher_pool
) {
824 base_
.RemoveHigherLayeredPool(higher_pool
);
827 bool SSLClientSocketPool::CloseOneIdleConnection() {
828 if (base_
.CloseOneIdleSocket())
830 return base_
.CloseOneIdleConnectionInHigherLayeredPool();
833 SSLConnectJobMessenger
* SSLClientSocketPool::GetOrCreateSSLConnectJobMessenger(
834 const std::string
& cache_key
) {
835 if (!enable_ssl_connect_job_waiting_
)
837 MessengerMap::const_iterator it
= messenger_map_
.find(cache_key
);
838 if (it
== messenger_map_
.end()) {
839 std::pair
<MessengerMap::iterator
, bool> iter
=
840 messenger_map_
.insert(MessengerMap::value_type(
842 new SSLConnectJobMessenger(
843 base::Bind(&SSLClientSocketPool::DeleteSSLConnectJobMessenger
,
844 base::Unretained(this),
851 void SSLClientSocketPool::DeleteSSLConnectJobMessenger(
852 const std::string
& cache_key
) {
853 MessengerMap::iterator it
= messenger_map_
.find(cache_key
);
854 CHECK(it
!= messenger_map_
.end());
856 messenger_map_
.erase(it
);
859 void SSLClientSocketPool::OnSSLConfigChanged() {
860 FlushWithError(ERR_NETWORK_CHANGED
);