Fix build.
[chromium-blink-merge.git] / net / socket / ssl_client_socket_pool.cc
blob56df1d85d09bd587d9d334be5d072dec029f6d6c
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"
7 #include "base/bind.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"
27 namespace net {
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,
36 int load_flags,
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) {
49 if (direct_params_.get()) {
50 DCHECK(!socks_proxy_params_.get());
51 DCHECK(!http_proxy_params_.get());
52 ignore_limits_ = direct_params_->ignore_limits();
53 } else if (socks_proxy_params_.get()) {
54 DCHECK(!http_proxy_params_.get());
55 ignore_limits_ = socks_proxy_params_->ignore_limits();
56 } else {
57 DCHECK(http_proxy_params_.get());
58 ignore_limits_ = http_proxy_params_->ignore_limits();
62 SSLSocketParams::~SSLSocketParams() {}
64 SSLSocketParams::ConnectionType SSLSocketParams::GetConnectionType() const {
65 if (direct_params_.get()) {
66 DCHECK(!socks_proxy_params_.get());
67 DCHECK(!http_proxy_params_.get());
68 return DIRECT;
71 if (socks_proxy_params_.get()) {
72 DCHECK(!http_proxy_params_.get());
73 return SOCKS_PROXY;
76 DCHECK(http_proxy_params_.get());
77 return HTTP_PROXY;
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();
122 ++it) {
123 if (it->socket == ssl_socket) {
124 pending_sockets_and_callbacks_.erase(it);
125 return;
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();
164 ++it) {
165 it->callback.Run();
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,
183 Delegate* delegate,
184 NetLog* net_log)
185 : ConnectJob(group_name,
186 timeout_duration,
187 priority,
188 delegate,
189 BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)),
190 params_(params),
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)),
203 io_callback_(
204 base::Bind(&SSLConnectJob::OnIOComplete, base::Unretained(this))),
205 messenger_(NULL),
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;
232 default:
233 NOTREACHED();
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);
259 int rv = result;
260 do {
261 State state = next_state_;
262 next_state_ = STATE_NONE;
263 switch (state) {
264 case STATE_TRANSPORT_CONNECT:
265 DCHECK_EQ(OK, rv);
266 rv = DoTransportConnect();
267 break;
268 case STATE_TRANSPORT_CONNECT_COMPLETE:
269 rv = DoTransportConnectComplete(rv);
270 break;
271 case STATE_SOCKS_CONNECT:
272 DCHECK_EQ(OK, rv);
273 rv = DoSOCKSConnect();
274 break;
275 case STATE_SOCKS_CONNECT_COMPLETE:
276 rv = DoSOCKSConnectComplete(rv);
277 break;
278 case STATE_TUNNEL_CONNECT:
279 DCHECK_EQ(OK, rv);
280 rv = DoTunnelConnect();
281 break;
282 case STATE_TUNNEL_CONNECT_COMPLETE:
283 rv = DoTunnelConnectComplete(rv);
284 break;
285 case STATE_CREATE_SSL_SOCKET:
286 rv = DoCreateSSLSocket();
287 break;
288 case STATE_CHECK_FOR_RESUME:
289 rv = DoCheckForResume();
290 break;
291 case STATE_SSL_CONNECT:
292 DCHECK_EQ(OK, rv);
293 rv = DoSSLConnect();
294 break;
295 case STATE_SSL_CONNECT_COMPLETE:
296 rv = DoSSLConnectComplete(rv);
297 break;
298 default:
299 NOTREACHED() << "bad state";
300 rv = ERR_FAILED;
301 break;
303 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
305 return rv;
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(),
316 direct_params,
317 priority(),
318 io_callback_,
319 transport_pool_,
320 net_log());
323 int SSLConnectJob::DoTransportConnectComplete(int result) {
324 if (result == OK)
325 next_state_ = STATE_CREATE_SSL_SOCKET;
327 return result;
330 int SSLConnectJob::DoSOCKSConnect() {
331 DCHECK(socks_pool_);
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(),
337 socks_proxy_params,
338 priority(),
339 io_callback_,
340 socks_pool_,
341 net_log());
344 int SSLConnectJob::DoSOCKSConnectComplete(int result) {
345 if (result == OK)
346 next_state_ = STATE_CREATE_SSL_SOCKET;
348 return result;
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(),
359 http_proxy_params,
360 priority(),
361 io_callback_,
362 http_proxy_pool_,
363 net_log());
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();
379 if (result < 0)
380 return result;
381 next_state_ = STATE_CREATE_SSL_SOCKET;
382 return result;
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(),
409 context_);
411 if (!ssl_socket_->InSessionCache())
412 messenger_ = get_messenger_callback_.Run(ssl_socket_->GetSessionCacheKey());
414 return OK;
417 int SSLConnectJob::DoCheckForResume() {
418 next_state_ = STATE_SSL_CONNECT;
420 if (!messenger_)
421 return OK;
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
426 // point.
427 messenger_ = NULL;
428 return OK;
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;
451 std::string proto;
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);
457 ssl_socket_->RecordNegotiationExtension();
460 // If we want spdy over npn, make sure it succeeded.
461 if (status == SSLClientSocket::kNextProtoNegotiated) {
462 ssl_socket_->set_was_npn_negotiated(true);
463 NextProto protocol_negotiated =
464 SSLClientSocket::NextProtoFromString(proto);
465 ssl_socket_->set_protocol_negotiated(protocol_negotiated);
466 // If we negotiated a SPDY version, it must have been present in
467 // SSLConfig::next_protos.
468 // TODO(mbelshe): Verify this.
469 if (protocol_negotiated >= kProtoSPDYMinimumVersion &&
470 protocol_negotiated <= kProtoSPDYMaximumVersion) {
471 ssl_socket_->set_was_spdy_negotiated(true);
474 if (params_->want_spdy_over_npn() && !ssl_socket_->was_spdy_negotiated())
475 return ERR_NPN_NEGOTIATION_FAILED;
477 // Spdy might be turned on by default, or it might be over npn.
478 bool using_spdy = params_->force_spdy_over_ssl() ||
479 params_->want_spdy_over_npn();
481 if (result == OK ||
482 ssl_socket_->IgnoreCertError(result, params_->load_flags())) {
483 DCHECK(!connect_timing_.ssl_start.is_null());
484 base::TimeDelta connect_duration =
485 connect_timing_.ssl_end - connect_timing_.ssl_start;
486 if (using_spdy) {
487 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SpdyConnectionLatency_2",
488 connect_duration,
489 base::TimeDelta::FromMilliseconds(1),
490 base::TimeDelta::FromMinutes(1),
491 100);
494 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_2",
495 connect_duration,
496 base::TimeDelta::FromMilliseconds(1),
497 base::TimeDelta::FromMinutes(1),
498 100);
500 SSLInfo ssl_info;
501 ssl_socket_->GetSSLInfo(&ssl_info);
503 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.SSL_CipherSuite",
504 SSLConnectionStatusToCipherSuite(
505 ssl_info.connection_status));
507 UMA_HISTOGRAM_BOOLEAN(
508 "Net.RenegotiationExtensionSupported",
509 (ssl_info.connection_status &
510 SSL_CONNECTION_NO_RENEGOTIATION_EXTENSION) == 0);
512 if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_RESUME) {
513 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Resume_Handshake",
514 connect_duration,
515 base::TimeDelta::FromMilliseconds(1),
516 base::TimeDelta::FromMinutes(1),
517 100);
518 } else if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_FULL) {
519 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Full_Handshake",
520 connect_duration,
521 base::TimeDelta::FromMilliseconds(1),
522 base::TimeDelta::FromMinutes(1),
523 100);
526 const std::string& host = params_->host_and_port().host();
527 bool is_google =
528 host == "google.com" ||
529 (host.size() > 11 && host.rfind(".google.com") == host.size() - 11);
530 if (is_google) {
531 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google2",
532 connect_duration,
533 base::TimeDelta::FromMilliseconds(1),
534 base::TimeDelta::FromMinutes(1),
535 100);
536 if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_RESUME) {
537 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google_"
538 "Resume_Handshake",
539 connect_duration,
540 base::TimeDelta::FromMilliseconds(1),
541 base::TimeDelta::FromMinutes(1),
542 100);
543 } else if (ssl_info.handshake_type == SSLInfo::HANDSHAKE_FULL) {
544 UMA_HISTOGRAM_CUSTOM_TIMES("Net.SSL_Connection_Latency_Google_"
545 "Full_Handshake",
546 connect_duration,
547 base::TimeDelta::FromMilliseconds(1),
548 base::TimeDelta::FromMinutes(1),
549 100);
554 if (result == OK || IsCertificateError(result)) {
555 SetSocket(ssl_socket_.Pass());
556 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
557 error_response_info_.cert_request_info = new SSLCertRequestInfo;
558 ssl_socket_->GetSSLCertRequestInfo(
559 error_response_info_.cert_request_info.get());
562 return result;
565 void SSLConnectJob::ResumeSSLConnection() {
566 DCHECK_EQ(next_state_, STATE_SSL_CONNECT);
567 messenger_ = NULL;
568 OnIOComplete(OK);
571 SSLConnectJob::State SSLConnectJob::GetInitialState(
572 SSLSocketParams::ConnectionType connection_type) {
573 switch (connection_type) {
574 case SSLSocketParams::DIRECT:
575 return STATE_TRANSPORT_CONNECT;
576 case SSLSocketParams::HTTP_PROXY:
577 return STATE_TUNNEL_CONNECT;
578 case SSLSocketParams::SOCKS_PROXY:
579 return STATE_SOCKS_CONNECT;
581 NOTREACHED();
582 return STATE_NONE;
585 int SSLConnectJob::ConnectInternal() {
586 next_state_ = GetInitialState(params_->GetConnectionType());
587 return DoLoop(OK);
590 SSLClientSocketPool::SSLConnectJobFactory::SSLConnectJobFactory(
591 TransportClientSocketPool* transport_pool,
592 SOCKSClientSocketPool* socks_pool,
593 HttpProxyClientSocketPool* http_proxy_pool,
594 ClientSocketFactory* client_socket_factory,
595 HostResolver* host_resolver,
596 const SSLClientSocketContext& context,
597 const SSLConnectJob::GetMessengerCallback& get_messenger_callback,
598 NetLog* net_log)
599 : transport_pool_(transport_pool),
600 socks_pool_(socks_pool),
601 http_proxy_pool_(http_proxy_pool),
602 client_socket_factory_(client_socket_factory),
603 host_resolver_(host_resolver),
604 context_(context),
605 get_messenger_callback_(get_messenger_callback),
606 net_log_(net_log) {
607 base::TimeDelta max_transport_timeout = base::TimeDelta();
608 base::TimeDelta pool_timeout;
609 if (transport_pool_)
610 max_transport_timeout = transport_pool_->ConnectionTimeout();
611 if (socks_pool_) {
612 pool_timeout = socks_pool_->ConnectionTimeout();
613 if (pool_timeout > max_transport_timeout)
614 max_transport_timeout = pool_timeout;
616 if (http_proxy_pool_) {
617 pool_timeout = http_proxy_pool_->ConnectionTimeout();
618 if (pool_timeout > max_transport_timeout)
619 max_transport_timeout = pool_timeout;
621 timeout_ = max_transport_timeout +
622 base::TimeDelta::FromSeconds(kSSLHandshakeTimeoutInSeconds);
625 SSLClientSocketPool::SSLConnectJobFactory::~SSLConnectJobFactory() {
628 SSLClientSocketPool::SSLClientSocketPool(
629 int max_sockets,
630 int max_sockets_per_group,
631 ClientSocketPoolHistograms* histograms,
632 HostResolver* host_resolver,
633 CertVerifier* cert_verifier,
634 ChannelIDService* channel_id_service,
635 TransportSecurityState* transport_security_state,
636 CTVerifier* cert_transparency_verifier,
637 const std::string& ssl_session_cache_shard,
638 ClientSocketFactory* client_socket_factory,
639 TransportClientSocketPool* transport_pool,
640 SOCKSClientSocketPool* socks_pool,
641 HttpProxyClientSocketPool* http_proxy_pool,
642 SSLConfigService* ssl_config_service,
643 bool enable_ssl_connect_job_waiting,
644 NetLog* net_log)
645 : transport_pool_(transport_pool),
646 socks_pool_(socks_pool),
647 http_proxy_pool_(http_proxy_pool),
648 base_(this,
649 max_sockets,
650 max_sockets_per_group,
651 histograms,
652 ClientSocketPool::unused_idle_socket_timeout(),
653 ClientSocketPool::used_idle_socket_timeout(),
654 new SSLConnectJobFactory(
655 transport_pool,
656 socks_pool,
657 http_proxy_pool,
658 client_socket_factory,
659 host_resolver,
660 SSLClientSocketContext(cert_verifier,
661 channel_id_service,
662 transport_security_state,
663 cert_transparency_verifier,
664 ssl_session_cache_shard),
665 base::Bind(
666 &SSLClientSocketPool::GetOrCreateSSLConnectJobMessenger,
667 base::Unretained(this)),
668 net_log)),
669 ssl_config_service_(ssl_config_service),
670 enable_ssl_connect_job_waiting_(enable_ssl_connect_job_waiting) {
671 if (ssl_config_service_.get())
672 ssl_config_service_->AddObserver(this);
673 if (transport_pool_)
674 base_.AddLowerLayeredPool(transport_pool_);
675 if (socks_pool_)
676 base_.AddLowerLayeredPool(socks_pool_);
677 if (http_proxy_pool_)
678 base_.AddLowerLayeredPool(http_proxy_pool_);
681 SSLClientSocketPool::~SSLClientSocketPool() {
682 STLDeleteContainerPairSecondPointers(messenger_map_.begin(),
683 messenger_map_.end());
684 if (ssl_config_service_.get())
685 ssl_config_service_->RemoveObserver(this);
688 scoped_ptr<ConnectJob> SSLClientSocketPool::SSLConnectJobFactory::NewConnectJob(
689 const std::string& group_name,
690 const PoolBase::Request& request,
691 ConnectJob::Delegate* delegate) const {
692 return scoped_ptr<ConnectJob>(new SSLConnectJob(group_name,
693 request.priority(),
694 request.params(),
695 ConnectionTimeout(),
696 transport_pool_,
697 socks_pool_,
698 http_proxy_pool_,
699 client_socket_factory_,
700 host_resolver_,
701 context_,
702 get_messenger_callback_,
703 delegate,
704 net_log_));
707 base::TimeDelta SSLClientSocketPool::SSLConnectJobFactory::ConnectionTimeout()
708 const {
709 return timeout_;
712 int SSLClientSocketPool::RequestSocket(const std::string& group_name,
713 const void* socket_params,
714 RequestPriority priority,
715 ClientSocketHandle* handle,
716 const CompletionCallback& callback,
717 const BoundNetLog& net_log) {
718 const scoped_refptr<SSLSocketParams>* casted_socket_params =
719 static_cast<const scoped_refptr<SSLSocketParams>*>(socket_params);
721 return base_.RequestSocket(group_name, *casted_socket_params, priority,
722 handle, callback, net_log);
725 void SSLClientSocketPool::RequestSockets(
726 const std::string& group_name,
727 const void* params,
728 int num_sockets,
729 const BoundNetLog& net_log) {
730 const scoped_refptr<SSLSocketParams>* casted_params =
731 static_cast<const scoped_refptr<SSLSocketParams>*>(params);
733 base_.RequestSockets(group_name, *casted_params, num_sockets, net_log);
736 void SSLClientSocketPool::CancelRequest(const std::string& group_name,
737 ClientSocketHandle* handle) {
738 base_.CancelRequest(group_name, handle);
741 void SSLClientSocketPool::ReleaseSocket(const std::string& group_name,
742 scoped_ptr<StreamSocket> socket,
743 int id) {
744 base_.ReleaseSocket(group_name, socket.Pass(), id);
747 void SSLClientSocketPool::FlushWithError(int error) {
748 base_.FlushWithError(error);
751 void SSLClientSocketPool::CloseIdleSockets() {
752 base_.CloseIdleSockets();
755 int SSLClientSocketPool::IdleSocketCount() const {
756 return base_.idle_socket_count();
759 int SSLClientSocketPool::IdleSocketCountInGroup(
760 const std::string& group_name) const {
761 return base_.IdleSocketCountInGroup(group_name);
764 LoadState SSLClientSocketPool::GetLoadState(
765 const std::string& group_name, const ClientSocketHandle* handle) const {
766 return base_.GetLoadState(group_name, handle);
769 base::DictionaryValue* SSLClientSocketPool::GetInfoAsValue(
770 const std::string& name,
771 const std::string& type,
772 bool include_nested_pools) const {
773 base::DictionaryValue* dict = base_.GetInfoAsValue(name, type);
774 if (include_nested_pools) {
775 base::ListValue* list = new base::ListValue();
776 if (transport_pool_) {
777 list->Append(transport_pool_->GetInfoAsValue("transport_socket_pool",
778 "transport_socket_pool",
779 false));
781 if (socks_pool_) {
782 list->Append(socks_pool_->GetInfoAsValue("socks_pool",
783 "socks_pool",
784 true));
786 if (http_proxy_pool_) {
787 list->Append(http_proxy_pool_->GetInfoAsValue("http_proxy_pool",
788 "http_proxy_pool",
789 true));
791 dict->Set("nested_pools", list);
793 return dict;
796 base::TimeDelta SSLClientSocketPool::ConnectionTimeout() const {
797 return base_.ConnectionTimeout();
800 ClientSocketPoolHistograms* SSLClientSocketPool::histograms() const {
801 return base_.histograms();
804 bool SSLClientSocketPool::IsStalled() const {
805 return base_.IsStalled();
808 void SSLClientSocketPool::AddHigherLayeredPool(HigherLayeredPool* higher_pool) {
809 base_.AddHigherLayeredPool(higher_pool);
812 void SSLClientSocketPool::RemoveHigherLayeredPool(
813 HigherLayeredPool* higher_pool) {
814 base_.RemoveHigherLayeredPool(higher_pool);
817 bool SSLClientSocketPool::CloseOneIdleConnection() {
818 if (base_.CloseOneIdleSocket())
819 return true;
820 return base_.CloseOneIdleConnectionInHigherLayeredPool();
823 SSLConnectJobMessenger* SSLClientSocketPool::GetOrCreateSSLConnectJobMessenger(
824 const std::string& cache_key) {
825 if (!enable_ssl_connect_job_waiting_)
826 return NULL;
827 MessengerMap::const_iterator it = messenger_map_.find(cache_key);
828 if (it == messenger_map_.end()) {
829 std::pair<MessengerMap::iterator, bool> iter =
830 messenger_map_.insert(MessengerMap::value_type(
831 cache_key,
832 new SSLConnectJobMessenger(
833 base::Bind(&SSLClientSocketPool::DeleteSSLConnectJobMessenger,
834 base::Unretained(this),
835 cache_key))));
836 it = iter.first;
838 return it->second;
841 void SSLClientSocketPool::DeleteSSLConnectJobMessenger(
842 const std::string& cache_key) {
843 MessengerMap::iterator it = messenger_map_.find(cache_key);
844 CHECK(it != messenger_map_.end());
845 delete it->second;
846 messenger_map_.erase(it);
849 void SSLClientSocketPool::OnSSLConfigChanged() {
850 FlushWithError(ERR_NETWORK_CHANGED);
853 } // namespace net