15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/socket/transport_client_socket_pool.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/lazy_instance.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
12ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "base/message_loop/message_loop.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/metrics/histogram.h"
145e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)#include "base/strings/string_util.h"
154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/synchronization/lock.h"
16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/ip_endpoint.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_errors.h"
20eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "net/base/net_log.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/socket/client_socket_factory.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/socket/client_socket_handle.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/socket/client_socket_pool_base.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/socket/socket_net_log_params.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/socket/tcp_client_socket.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using base::TimeDelta;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(willchan): Base this off RTT instead of statically setting it. Note we
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// choose a timeout that is different from the backup connect job timer so they
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// don't synchronize.
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdochconst int TransportConnectJobHelper::kIPv6FallbackTimerInMs = 300;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns true iff all addresses in |list| are in the IPv6 family.
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool AddressListOnlyContainsIPv6(const AddressList& list) {
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!list.empty());
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (AddressList::const_iterator iter = list.begin(); iter != list.end();
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       ++iter) {
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (iter->GetFamily() != ADDRESS_FAMILY_IPV6)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// This lock protects |g_last_connect_time|.
524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)static base::LazyInstance<base::Lock>::Leaky
534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    g_last_connect_time_lock = LAZY_INSTANCE_INITIALIZER;
544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// |g_last_connect_time| has the last time a connect() call is made.
564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)static base::LazyInstance<base::TimeTicks>::Leaky
574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    g_last_connect_time = LAZY_INSTANCE_INITIALIZER;
584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TransportSocketParams::TransportSocketParams(
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const HostPortPair& host_port_pair,
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool disable_resolver_cache,
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool ignore_limits,
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const OnHostResolutionCallback& host_resolution_callback,
641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    CombineConnectAndWritePolicy combine_connect_and_write_if_supported)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : destination_(host_port_pair),
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ignore_limits_(ignore_limits),
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      host_resolution_callback_(host_resolution_callback),
681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      combine_connect_and_write_(combine_connect_and_write_if_supported) {
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (disable_resolver_cache)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    destination_.set_allow_cached_response(false);
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // combine_connect_and_write currently translates to TCP FastOpen.
721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Enable TCP FastOpen if user wants it.
731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (combine_connect_and_write_ == COMBINE_CONNECT_AND_WRITE_DEFAULT) {
741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    IsTCPFastOpenUserEnabled() ? combine_connect_and_write_ =
751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        COMBINE_CONNECT_AND_WRITE_DESIRED :
761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        COMBINE_CONNECT_AND_WRITE_PROHIBITED;
771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)TransportSocketParams::~TransportSocketParams() {}
813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TransportConnectJobs will time out after this many seconds.  Note this is
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the total time, including both host resolution and TCP connect() times.
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(eroman): The use of this constant needs to be re-evaluated. The time
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// needed for TCPClientSocketXXX::Connect() can be arbitrarily long, since
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the address list may contain many alternatives, and most of those may
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// timeout. Even worse, the per-connect timeout threshold varies greatly
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// between systems (anywhere from 20 seconds to 190 seconds).
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// See comment #12 at http://crbug.com/23364 for specifics.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const int kTransportConnectJobTimeoutInSeconds = 240;  // 4 minutes.
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
93116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTransportConnectJobHelper::TransportConnectJobHelper(
94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const scoped_refptr<TransportSocketParams>& params,
95116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    ClientSocketFactory* client_socket_factory,
96116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    HostResolver* host_resolver,
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    LoadTimingInfo::ConnectTiming* connect_timing)
98116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : params_(params),
99116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      client_socket_factory_(client_socket_factory),
100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      resolver_(host_resolver),
101116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      next_state_(STATE_NONE),
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      connect_timing_(connect_timing) {}
103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
104116680a4aac90f2aa7413d9095a592090648e557Ben MurdochTransportConnectJobHelper::~TransportConnectJobHelper() {}
105116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
106116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint TransportConnectJobHelper::DoResolveHost(RequestPriority priority,
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                             const BoundNetLog& net_log) {
108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  next_state_ = STATE_RESOLVE_HOST_COMPLETE;
109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  connect_timing_->dns_start = base::TimeTicks::Now();
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return resolver_.Resolve(
112116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      params_->destination(), priority, &addresses_, on_io_complete_, net_log);
113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
114116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
115116680a4aac90f2aa7413d9095a592090648e557Ben Murdochint TransportConnectJobHelper::DoResolveHostComplete(
116116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    int result,
117116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const BoundNetLog& net_log) {
118116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  connect_timing_->dns_end = base::TimeTicks::Now();
119116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Overwrite connection start time, since for connections that do not go
120116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // through proxies, |connect_start| should not include dns lookup time.
121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  connect_timing_->connect_start = connect_timing_->dns_end;
122116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
123116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (result == OK) {
124116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    // Invoke callback, and abort if it fails.
125116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (!params_->host_resolution_callback().is_null())
126116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      result = params_->host_resolution_callback().Run(addresses_, net_log);
127116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
128116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (result == OK)
129116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      next_state_ = STATE_TRANSPORT_CONNECT;
130116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
131116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return result;
132116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
133116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
134116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbase::TimeDelta TransportConnectJobHelper::HistogramDuration(
135116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    ConnectionLatencyHistogram race_result) {
136116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DCHECK(!connect_timing_->connect_start.is_null());
137116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DCHECK(!connect_timing_->dns_start.is_null());
138116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::TimeTicks now = base::TimeTicks::Now();
139116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::TimeDelta total_duration = now - connect_timing_->dns_start;
140116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  UMA_HISTOGRAM_CUSTOM_TIMES("Net.DNS_Resolution_And_TCP_Connection_Latency2",
141116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                             total_duration,
142116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                             base::TimeDelta::FromMilliseconds(1),
143116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                             base::TimeDelta::FromMinutes(10),
144116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                             100);
145116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
146116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::TimeDelta connect_duration = now - connect_timing_->connect_start;
147116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency",
148116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                             connect_duration,
149116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                             base::TimeDelta::FromMilliseconds(1),
150116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                             base::TimeDelta::FromMinutes(10),
151116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                             100);
152116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
153116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  switch (race_result) {
154116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    case CONNECTION_LATENCY_IPV4_WINS_RACE:
155116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv4_Wins_Race",
156116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 connect_duration,
157116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 base::TimeDelta::FromMilliseconds(1),
158116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 base::TimeDelta::FromMinutes(10),
159116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 100);
160116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      break;
161116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
162116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    case CONNECTION_LATENCY_IPV4_NO_RACE:
163116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv4_No_Race",
164116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 connect_duration,
165116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 base::TimeDelta::FromMilliseconds(1),
166116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 base::TimeDelta::FromMinutes(10),
167116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 100);
168116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      break;
169116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
170116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    case CONNECTION_LATENCY_IPV6_RACEABLE:
171116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv6_Raceable",
172116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 connect_duration,
173116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 base::TimeDelta::FromMilliseconds(1),
174116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 base::TimeDelta::FromMinutes(10),
175116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 100);
176116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      break;
177116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
178116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    case CONNECTION_LATENCY_IPV6_SOLO:
179116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      UMA_HISTOGRAM_CUSTOM_TIMES("Net.TCP_Connection_Latency_IPv6_Solo",
180116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 connect_duration,
181116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 base::TimeDelta::FromMilliseconds(1),
182116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 base::TimeDelta::FromMinutes(10),
183116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                 100);
184116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      break;
185116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
186116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    default:
187116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      NOTREACHED();
188116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      break;
189116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
190116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
191116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return connect_duration;
192116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
193116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TransportConnectJob::TransportConnectJob(
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& group_name,
1963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    RequestPriority priority,
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const scoped_refptr<TransportSocketParams>& params,
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    base::TimeDelta timeout_duration,
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ClientSocketFactory* client_socket_factory,
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HostResolver* host_resolver,
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Delegate* delegate,
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NetLog* net_log)
2033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    : ConnectJob(group_name, timeout_duration, priority, delegate,
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 BoundNetLog::Make(net_log, NetLog::SOURCE_CONNECT_JOB)),
205116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      helper_(params, client_socket_factory, host_resolver, &connect_timing_),
206f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      interval_between_connects_(CONNECT_INTERVAL_GT_20MS) {
207116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  helper_.SetOnIOComplete(this);
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TransportConnectJob::~TransportConnectJob() {
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We don't worry about cancelling the host resolution and TCP connect, since
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ~SingleRequestHostResolver and ~StreamSocket will take care of it.
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LoadState TransportConnectJob::GetLoadState() const {
216116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  switch (helper_.next_state()) {
217116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    case TransportConnectJobHelper::STATE_RESOLVE_HOST:
218116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    case TransportConnectJobHelper::STATE_RESOLVE_HOST_COMPLETE:
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return LOAD_STATE_RESOLVING_HOST;
220116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    case TransportConnectJobHelper::STATE_TRANSPORT_CONNECT:
221116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    case TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE:
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return LOAD_STATE_CONNECTING;
223116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    case TransportConnectJobHelper::STATE_NONE:
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return LOAD_STATE_IDLE;
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
226d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  NOTREACHED();
227d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  return LOAD_STATE_IDLE;
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TransportConnectJob::MakeAddressListStartWithIPv4(AddressList* list) {
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (AddressList::iterator i = list->begin(); i != list->end(); ++i) {
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (i->GetFamily() == ADDRESS_FAMILY_IPV4) {
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::rotate(list->begin(), i, list->end());
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TransportConnectJob::DoResolveHost() {
241116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return helper_.DoResolveHost(priority(), net_log());
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TransportConnectJob::DoResolveHostComplete(int result) {
245116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return helper_.DoResolveHostComplete(result, net_log());
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TransportConnectJob::DoTransportConnect() {
2494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  base::TimeTicks now = base::TimeTicks::Now();
2504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  base::TimeTicks last_connect_time;
2514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  {
2524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    base::AutoLock lock(g_last_connect_time_lock.Get());
2534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    last_connect_time = g_last_connect_time.Get();
2544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    *g_last_connect_time.Pointer() = now;
2554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
256f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (last_connect_time.is_null()) {
257f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    interval_between_connects_ = CONNECT_INTERVAL_GT_20MS;
2584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  } else {
259f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    int64 interval = (now - last_connect_time).InMilliseconds();
260f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (interval <= 10)
261f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      interval_between_connects_ = CONNECT_INTERVAL_LE_10MS;
262f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    else if (interval <= 20)
263f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      interval_between_connects_ = CONNECT_INTERVAL_LE_20MS;
264f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    else
265f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      interval_between_connects_ = CONNECT_INTERVAL_GT_20MS;
2664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
2674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
268116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  helper_.set_next_state(
269116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE);
270116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  transport_socket_ =
271116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      helper_.client_socket_factory()->CreateTransportClientSocket(
272116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          helper_.addresses(), net_log().net_log(), net_log().source());
2731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // If the list contains IPv6 and IPv4 addresses, the first address will
2751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // be IPv6, and the IPv4 addresses will be tried as fallback addresses,
2761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // per "Happy Eyeballs" (RFC 6555).
2771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  bool try_ipv6_connect_with_ipv4_fallback =
278116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      helper_.addresses().front().GetFamily() == ADDRESS_FAMILY_IPV6 &&
2791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      !AddressListOnlyContainsIPv6(helper_.addresses());
2801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Enable TCP FastOpen if indicated by transport socket params.
2821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Note: We currently do not turn on TCP FastOpen for destinations where
2831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // we try a TCP connect over IPv6 with fallback to IPv4.
2841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!try_ipv6_connect_with_ipv4_fallback &&
2851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      helper_.params()->combine_connect_and_write() ==
2861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          TransportSocketParams::COMBINE_CONNECT_AND_WRITE_DESIRED) {
2871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    transport_socket_->EnableTCPFastOpenIfSupported();
2881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
2891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  int rv = transport_socket_->Connect(helper_.on_io_complete());
2911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (rv == ERR_IO_PENDING && try_ipv6_connect_with_ipv4_fallback) {
292116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    fallback_timer_.Start(
293116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        FROM_HERE,
294116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        base::TimeDelta::FromMilliseconds(
295116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            TransportConnectJobHelper::kIPv6FallbackTimerInMs),
296116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        this,
297116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        &TransportConnectJob::DoIPv6FallbackTransportConnect);
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return rv;
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TransportConnectJob::DoTransportConnectComplete(int result) {
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (result == OK) {
304116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    bool is_ipv4 =
305116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        helper_.addresses().front().GetFamily() == ADDRESS_FAMILY_IPV4;
306116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    TransportConnectJobHelper::ConnectionLatencyHistogram race_result =
307116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        TransportConnectJobHelper::CONNECTION_LATENCY_UNKNOWN;
308116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (is_ipv4) {
309116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      race_result = TransportConnectJobHelper::CONNECTION_LATENCY_IPV4_NO_RACE;
310116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    } else {
311116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      if (AddressListOnlyContainsIPv6(helper_.addresses())) {
312116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        race_result = TransportConnectJobHelper::CONNECTION_LATENCY_IPV6_SOLO;
313116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      } else {
314116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        race_result =
315116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            TransportConnectJobHelper::CONNECTION_LATENCY_IPV6_RACEABLE;
316116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      }
317116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    }
318116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    base::TimeDelta connect_duration = helper_.HistogramDuration(race_result);
319f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    switch (interval_between_connects_) {
320f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      case CONNECT_INTERVAL_LE_10MS:
321f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        UMA_HISTOGRAM_CUSTOM_TIMES(
322f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            "Net.TCP_Connection_Latency_Interval_LessThanOrEqual_10ms",
323f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            connect_duration,
324f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            base::TimeDelta::FromMilliseconds(1),
325f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            base::TimeDelta::FromMinutes(10),
326f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            100);
327f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        break;
328f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      case CONNECT_INTERVAL_LE_20MS:
329f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        UMA_HISTOGRAM_CUSTOM_TIMES(
330f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            "Net.TCP_Connection_Latency_Interval_LessThanOrEqual_20ms",
331f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            connect_duration,
332f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            base::TimeDelta::FromMilliseconds(1),
333f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            base::TimeDelta::FromMinutes(10),
334f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            100);
335f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        break;
336f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      case CONNECT_INTERVAL_GT_20MS:
337f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        UMA_HISTOGRAM_CUSTOM_TIMES(
338f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            "Net.TCP_Connection_Latency_Interval_GreaterThan_20ms",
339f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            connect_duration,
340f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            base::TimeDelta::FromMilliseconds(1),
341f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            base::TimeDelta::FromMinutes(10),
342f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)            100);
343f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        break;
344f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      default:
345f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        NOTREACHED();
346f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        break;
3474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    }
3484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
3493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    SetSocket(transport_socket_.Pass());
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    fallback_timer_.Stop();
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Be a bit paranoid and kill off the fallback members to prevent reuse.
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    fallback_transport_socket_.reset();
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    fallback_addresses_.reset();
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result;
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TransportConnectJob::DoIPv6FallbackTransportConnect() {
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The timer should only fire while we're waiting for the main connect to
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // succeed.
363116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (helper_.next_state() !=
364116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE) {
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!fallback_transport_socket_.get());
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!fallback_addresses_.get());
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
372116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  fallback_addresses_.reset(new AddressList(helper_.addresses()));
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MakeAddressListStartWithIPv4(fallback_addresses_.get());
3743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  fallback_transport_socket_ =
375116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      helper_.client_socket_factory()->CreateTransportClientSocket(
3763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          *fallback_addresses_, net_log().net_log(), net_log().source());
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fallback_connect_start_time_ = base::TimeTicks::Now();
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int rv = fallback_transport_socket_->Connect(
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          &TransportConnectJob::DoIPv6FallbackTransportConnectComplete,
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          base::Unretained(this)));
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (rv != ERR_IO_PENDING)
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DoIPv6FallbackTransportConnectComplete(rv);
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TransportConnectJob::DoIPv6FallbackTransportConnectComplete(int result) {
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This should only happen when we're waiting for the main connect to succeed.
388116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (helper_.next_state() !=
389116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      TransportConnectJobHelper::STATE_TRANSPORT_CONNECT_COMPLETE) {
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED();
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK_NE(ERR_IO_PENDING, result);
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(fallback_transport_socket_.get());
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(fallback_addresses_.get());
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (result == OK) {
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK(!fallback_connect_start_time_.is_null());
400116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    connect_timing_.connect_start = fallback_connect_start_time_;
401116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    helper_.HistogramDuration(
402116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        TransportConnectJobHelper::CONNECTION_LATENCY_IPV4_WINS_RACE);
4033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    SetSocket(fallback_transport_socket_.Pass());
404116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    helper_.set_next_state(TransportConnectJobHelper::STATE_NONE);
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    transport_socket_.reset();
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } else {
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Be a bit paranoid and kill off the fallback members to prevent reuse.
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    fallback_transport_socket_.reset();
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    fallback_addresses_.reset();
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NotifyDelegateOfCompletion(result);  // Deletes |this|
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TransportConnectJob::ConnectInternal() {
415116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return helper_.DoConnectInternal(this);
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)scoped_ptr<ConnectJob>
41903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)TransportClientSocketPool::TransportConnectJobFactory::NewConnectJob(
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& group_name,
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const PoolBase::Request& request,
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ConnectJob::Delegate* delegate) const {
4233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return scoped_ptr<ConnectJob>(
4243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      new TransportConnectJob(group_name,
4253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                              request.priority(),
4263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                              request.params(),
4273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                              ConnectionTimeout(),
4283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                              client_socket_factory_,
4293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                              host_resolver_,
4303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                              delegate,
4313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                              net_log_));
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::TimeDelta
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    TransportClientSocketPool::TransportConnectJobFactory::ConnectionTimeout()
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const {
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return base::TimeDelta::FromSeconds(kTransportConnectJobTimeoutInSeconds);
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TransportClientSocketPool::TransportClientSocketPool(
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int max_sockets,
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int max_sockets_per_group,
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ClientSocketPoolHistograms* histograms,
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HostResolver* host_resolver,
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ClientSocketFactory* client_socket_factory,
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NetLog* net_log)
4473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    : base_(NULL, max_sockets, max_sockets_per_group, histograms,
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            ClientSocketPool::unused_idle_socket_timeout(),
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            ClientSocketPool::used_idle_socket_timeout(),
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            new TransportConnectJobFactory(client_socket_factory,
4513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                           host_resolver, net_log)) {
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base_.EnableConnectBackupJobs();
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TransportClientSocketPool::~TransportClientSocketPool() {}
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TransportClientSocketPool::RequestSocket(
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& group_name,
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const void* params,
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    RequestPriority priority,
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ClientSocketHandle* handle,
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const CompletionCallback& callback,
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const BoundNetLog& net_log) {
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const scoped_refptr<TransportSocketParams>* casted_params =
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      static_cast<const scoped_refptr<TransportSocketParams>*>(params);
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
467116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  NetLogTcpClientSocketPoolRequestedSocket(net_log, casted_params);
468116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
469116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return base_.RequestSocket(group_name, *casted_params, priority, handle,
470116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                             callback, net_log);
471116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
472116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
473116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid TransportClientSocketPool::NetLogTcpClientSocketPoolRequestedSocket(
474116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const BoundNetLog& net_log,
475116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const scoped_refptr<TransportSocketParams>* casted_params) {
476a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (net_log.IsLogging()) {
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO(eroman): Split out the host and port parameters.
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net_log.AddEvent(
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NetLog::TYPE_TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKET,
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        CreateNetLogHostPortPairCallback(
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            &casted_params->get()->destination().host_port_pair()));
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TransportClientSocketPool::RequestSockets(
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& group_name,
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const void* params,
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int num_sockets,
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const BoundNetLog& net_log) {
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const scoped_refptr<TransportSocketParams>* casted_params =
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      static_cast<const scoped_refptr<TransportSocketParams>*>(params);
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
493a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (net_log.IsLogging()) {
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // TODO(eroman): Split out the host and port parameters.
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net_log.AddEvent(
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        NetLog::TYPE_TCP_CLIENT_SOCKET_POOL_REQUESTED_SOCKETS,
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        CreateNetLogHostPortPairCallback(
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            &casted_params->get()->destination().host_port_pair()));
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base_.RequestSockets(group_name, *casted_params, num_sockets, net_log);
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TransportClientSocketPool::CancelRequest(
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& group_name,
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ClientSocketHandle* handle) {
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base_.CancelRequest(group_name, handle);
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TransportClientSocketPool::ReleaseSocket(
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& group_name,
5123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    scoped_ptr<StreamSocket> socket,
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int id) {
5143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base_.ReleaseSocket(group_name, socket.Pass(), id);
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TransportClientSocketPool::FlushWithError(int error) {
5182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base_.FlushWithError(error);
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TransportClientSocketPool::CloseIdleSockets() {
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base_.CloseIdleSockets();
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TransportClientSocketPool::IdleSocketCount() const {
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return base_.idle_socket_count();
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TransportClientSocketPool::IdleSocketCountInGroup(
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& group_name) const {
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return base_.IdleSocketCountInGroup(group_name);
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)LoadState TransportClientSocketPool::GetLoadState(
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& group_name, const ClientSocketHandle* handle) const {
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return base_.GetLoadState(group_name, handle);
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)base::DictionaryValue* TransportClientSocketPool::GetInfoAsValue(
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& name,
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& type,
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool include_nested_pools) const {
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return base_.GetInfoAsValue(name, type);
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)base::TimeDelta TransportClientSocketPool::ConnectionTimeout() const {
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return base_.ConnectionTimeout();
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ClientSocketPoolHistograms* TransportClientSocketPool::histograms() const {
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return base_.histograms();
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)bool TransportClientSocketPool::IsStalled() const {
5553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return base_.IsStalled();
5563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
5573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
5583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void TransportClientSocketPool::AddHigherLayeredPool(
5593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    HigherLayeredPool* higher_pool) {
5603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base_.AddHigherLayeredPool(higher_pool);
5613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
5623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
5633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void TransportClientSocketPool::RemoveHigherLayeredPool(
5643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    HigherLayeredPool* higher_pool) {
5653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base_.RemoveHigherLayeredPool(higher_pool);
5663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
5673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace net
569