[email protected] | a273088 | 2012-01-21 00:56:27 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 5 | #include "net/dns/host_resolver_impl.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 6 | |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 7 | #if defined(OS_WIN) |
| 8 | #include <Winsock2.h> |
| 9 | #elif defined(OS_POSIX) |
| 10 | #include <netdb.h> |
| 11 | #endif |
| 12 | |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 13 | #include <cmath> |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 14 | #include <utility> |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 15 | #include <vector> |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 16 | |
[email protected] | 33152acc | 2011-10-20 23:37:12 | [diff] [blame] | 17 | #include "base/bind.h" |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 18 | #include "base/bind_helpers.h" |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 19 | #include "base/callback.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 20 | #include "base/compiler_specific.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 21 | #include "base/debug/debugger.h" |
| 22 | #include "base/debug/stack_trace.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame^] | 23 | #include "base/macros.h" |
olli.raula | a21e9eb7 | 2015-12-17 08:18:11 | [diff] [blame] | 24 | #include "base/memory/scoped_ptr.h" |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 25 | #include "base/metrics/field_trial.h" |
davidben | ad6fc44 | 2015-05-18 20:59:13 | [diff] [blame] | 26 | #include "base/metrics/histogram_macros.h" |
| 27 | #include "base/metrics/sparse_histogram.h" |
eroman | e186724 | 2015-04-23 23:07:44 | [diff] [blame] | 28 | #include "base/profiler/scoped_tracker.h" |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 29 | #include "base/single_thread_task_runner.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 30 | #include "base/stl_util.h" |
[email protected] | be528af | 2013-06-11 07:39:48 | [diff] [blame] | 31 | #include "base/strings/string_util.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 32 | #include "base/strings/utf_string_conversions.h" |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 33 | #include "base/thread_task_runner_handle.h" |
[email protected] | ac9ba8fe | 2010-12-30 18:08:36 | [diff] [blame] | 34 | #include "base/threading/worker_pool.h" |
[email protected] | 66e96c4 | 2013-06-28 15:20:31 | [diff] [blame] | 35 | #include "base/time/time.h" |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 36 | #include "base/values.h" |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 37 | #include "net/base/address_family.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 38 | #include "net/base/address_list.h" |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 39 | #include "net/base/host_port_pair.h" |
[email protected] | 1c7cf3f8 | 2014-08-07 21:33:48 | [diff] [blame] | 40 | #include "net/base/ip_endpoint.h" |
[email protected] | 2bb0444 | 2010-08-18 18:01:15 | [diff] [blame] | 41 | #include "net/base/net_errors.h" |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 42 | #include "net/base/net_util.h" |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 43 | #include "net/dns/address_sorter.h" |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 44 | #include "net/dns/dns_client.h" |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 45 | #include "net/dns/dns_config_service.h" |
| 46 | #include "net/dns/dns_protocol.h" |
tfarina | 47598f04 | 2015-10-07 23:08:35 | [diff] [blame] | 47 | #include "net/dns/dns_reloader.h" |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 48 | #include "net/dns/dns_response.h" |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 49 | #include "net/dns/dns_transaction.h" |
tfarina | 77021d6 | 2015-10-11 20:19:03 | [diff] [blame] | 50 | #include "net/dns/dns_util.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 51 | #include "net/dns/host_resolver_proc.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 52 | #include "net/log/net_log.h" |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 53 | #include "net/socket/client_socket_factory.h" |
| 54 | #include "net/udp/datagram_client_socket.h" |
pauljensen | 370f1c7 | 2015-02-17 16:59:14 | [diff] [blame] | 55 | #include "url/url_canon_ip.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 56 | |
| 57 | #if defined(OS_WIN) |
| 58 | #include "net/base/winsock_init.h" |
| 59 | #endif |
| 60 | |
| 61 | namespace net { |
| 62 | |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 63 | namespace { |
| 64 | |
[email protected] | 6e78dfb | 2011-07-28 21:34:47 | [diff] [blame] | 65 | // Limit the size of hostnames that will be resolved to combat issues in |
| 66 | // some platform's resolvers. |
| 67 | const size_t kMaxHostLength = 4096; |
| 68 | |
[email protected] | a273088 | 2012-01-21 00:56:27 | [diff] [blame] | 69 | // Default TTL for successful resolutions with ProcTask. |
| 70 | const unsigned kCacheEntryTTLSeconds = 60; |
| 71 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 72 | // Default TTL for unsuccessful resolutions with ProcTask. |
| 73 | const unsigned kNegativeCacheEntryTTLSeconds = 0; |
| 74 | |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 75 | // Minimum TTL for successful resolutions with DnsTask. |
| 76 | const unsigned kMinimumTTLSeconds = kCacheEntryTTLSeconds; |
| 77 | |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 78 | // Time between IPv6 probes, i.e. for how long results of each IPv6 probe are |
| 79 | // cached. |
| 80 | const int kIPv6ProbePeriodMs = 1000; |
| 81 | |
| 82 | // Google DNS address used for IPv6 probes. |
| 83 | const uint8_t kIPv6ProbeAddress[] = |
| 84 | { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, |
| 85 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 }; |
| 86 | |
[email protected] | 24f4bab | 2010-10-15 01:27:11 | [diff] [blame] | 87 | // We use a separate histogram name for each platform to facilitate the |
| 88 | // display of error codes by their symbolic name (since each platform has |
| 89 | // different mappings). |
| 90 | const char kOSErrorsForGetAddrinfoHistogramName[] = |
| 91 | #if defined(OS_WIN) |
| 92 | "Net.OSErrorsForGetAddrinfo_Win"; |
| 93 | #elif defined(OS_MACOSX) |
| 94 | "Net.OSErrorsForGetAddrinfo_Mac"; |
| 95 | #elif defined(OS_LINUX) |
| 96 | "Net.OSErrorsForGetAddrinfo_Linux"; |
| 97 | #else |
| 98 | "Net.OSErrorsForGetAddrinfo"; |
| 99 | #endif |
| 100 | |
[email protected] | c89b244 | 2011-05-26 14:28:27 | [diff] [blame] | 101 | // Gets a list of the likely error codes that getaddrinfo() can return |
| 102 | // (non-exhaustive). These are the error codes that we will track via |
| 103 | // a histogram. |
| 104 | std::vector<int> GetAllGetAddrinfoOSErrors() { |
| 105 | int os_errors[] = { |
| 106 | #if defined(OS_POSIX) |
[email protected] | 23f77116 | 2011-06-02 18:37:51 | [diff] [blame] | 107 | #if !defined(OS_FREEBSD) |
[email protected] | 3958899 | 2011-07-11 19:54:37 | [diff] [blame] | 108 | #if !defined(OS_ANDROID) |
[email protected] | c48aef9 | 2011-11-22 23:41:45 | [diff] [blame] | 109 | // EAI_ADDRFAMILY has been declared obsolete in Android's and |
| 110 | // FreeBSD's netdb.h. |
[email protected] | c89b244 | 2011-05-26 14:28:27 | [diff] [blame] | 111 | EAI_ADDRFAMILY, |
[email protected] | 3958899 | 2011-07-11 19:54:37 | [diff] [blame] | 112 | #endif |
[email protected] | c48aef9 | 2011-11-22 23:41:45 | [diff] [blame] | 113 | // EAI_NODATA has been declared obsolete in FreeBSD's netdb.h. |
[email protected] | 23f77116 | 2011-06-02 18:37:51 | [diff] [blame] | 114 | EAI_NODATA, |
| 115 | #endif |
[email protected] | c89b244 | 2011-05-26 14:28:27 | [diff] [blame] | 116 | EAI_AGAIN, |
| 117 | EAI_BADFLAGS, |
| 118 | EAI_FAIL, |
| 119 | EAI_FAMILY, |
| 120 | EAI_MEMORY, |
[email protected] | c89b244 | 2011-05-26 14:28:27 | [diff] [blame] | 121 | EAI_NONAME, |
| 122 | EAI_SERVICE, |
| 123 | EAI_SOCKTYPE, |
| 124 | EAI_SYSTEM, |
| 125 | #elif defined(OS_WIN) |
| 126 | // See: http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx |
| 127 | WSA_NOT_ENOUGH_MEMORY, |
| 128 | WSAEAFNOSUPPORT, |
| 129 | WSAEINVAL, |
| 130 | WSAESOCKTNOSUPPORT, |
| 131 | WSAHOST_NOT_FOUND, |
| 132 | WSANO_DATA, |
| 133 | WSANO_RECOVERY, |
| 134 | WSANOTINITIALISED, |
| 135 | WSATRY_AGAIN, |
| 136 | WSATYPE_NOT_FOUND, |
| 137 | // The following are not in doc, but might be to appearing in results :-(. |
| 138 | WSA_INVALID_HANDLE, |
| 139 | #endif |
| 140 | }; |
| 141 | |
| 142 | // Ensure all errors are positive, as histogram only tracks positive values. |
| 143 | for (size_t i = 0; i < arraysize(os_errors); ++i) { |
| 144 | os_errors[i] = std::abs(os_errors[i]); |
| 145 | } |
| 146 | |
| 147 | return base::CustomHistogram::ArrayToCustomRanges(os_errors, |
| 148 | arraysize(os_errors)); |
| 149 | } |
| 150 | |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 151 | enum DnsResolveStatus { |
| 152 | RESOLVE_STATUS_DNS_SUCCESS = 0, |
| 153 | RESOLVE_STATUS_PROC_SUCCESS, |
| 154 | RESOLVE_STATUS_FAIL, |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 155 | RESOLVE_STATUS_SUSPECT_NETBIOS, |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 156 | RESOLVE_STATUS_MAX |
| 157 | }; |
| 158 | |
eroman | 91dd360 | 2015-03-26 03:46:33 | [diff] [blame] | 159 | // ICANN uses this localhost address to indicate a name collision. |
| 160 | // |
| 161 | // The policy in Chromium is to fail host resolving if it resolves to |
| 162 | // this special address. |
| 163 | // |
| 164 | // Not however that IP literals are exempt from this policy, so it is still |
| 165 | // possible to navigate to http://127.0.53.53/ directly. |
| 166 | // |
| 167 | // For more details: https://www.icann.org/news/announcement-2-2014-08-01-en |
| 168 | const unsigned char kIcanNameCollisionIp[] = {127, 0, 53, 53}; |
| 169 | |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 170 | void UmaAsyncDnsResolveStatus(DnsResolveStatus result) { |
| 171 | UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ResolveStatus", |
| 172 | result, |
| 173 | RESOLVE_STATUS_MAX); |
| 174 | } |
| 175 | |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 176 | bool ResemblesNetBIOSName(const std::string& hostname) { |
| 177 | return (hostname.size() < 16) && (hostname.find('.') == std::string::npos); |
| 178 | } |
| 179 | |
| 180 | // True if |hostname| ends with either ".local" or ".local.". |
| 181 | bool ResemblesMulticastDNSName(const std::string& hostname) { |
| 182 | DCHECK(!hostname.empty()); |
| 183 | const char kSuffix[] = ".local."; |
| 184 | const size_t kSuffixLen = sizeof(kSuffix) - 1; |
| 185 | const size_t kSuffixLenTrimmed = kSuffixLen - 1; |
| 186 | if (hostname[hostname.size() - 1] == '.') { |
| 187 | return hostname.size() > kSuffixLen && |
| 188 | !hostname.compare(hostname.size() - kSuffixLen, kSuffixLen, kSuffix); |
| 189 | } |
| 190 | return hostname.size() > kSuffixLenTrimmed && |
| 191 | !hostname.compare(hostname.size() - kSuffixLenTrimmed, kSuffixLenTrimmed, |
| 192 | kSuffix, kSuffixLenTrimmed); |
| 193 | } |
| 194 | |
[email protected] | 34e6136 | 2013-07-24 20:41:56 | [diff] [blame] | 195 | // Attempts to connect a UDP socket to |dest|:53. |
[email protected] | 2b74a2f | 2013-07-23 19:37:38 | [diff] [blame] | 196 | bool IsGloballyReachable(const IPAddressNumber& dest, |
| 197 | const BoundNetLog& net_log) { |
eroman | 0eb394e | 2015-04-30 23:25:46 | [diff] [blame] | 198 | // TODO(eroman): Remove ScopedTracker below once crbug.com/455942 is fixed. |
| 199 | tracked_objects::ScopedTracker tracking_profile_1( |
eroman | 2734a9d | 2015-05-05 19:24:38 | [diff] [blame] | 200 | FROM_HERE_WITH_EXPLICIT_FUNCTION("455942 IsGloballyReachable")); |
eroman | 0eb394e | 2015-04-30 23:25:46 | [diff] [blame] | 201 | |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 202 | scoped_ptr<DatagramClientSocket> socket( |
| 203 | ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket( |
| 204 | DatagramSocket::DEFAULT_BIND, |
| 205 | RandIntCallback(), |
[email protected] | 2b74a2f | 2013-07-23 19:37:38 | [diff] [blame] | 206 | net_log.net_log(), |
| 207 | net_log.source())); |
[email protected] | 34e6136 | 2013-07-24 20:41:56 | [diff] [blame] | 208 | int rv = socket->Connect(IPEndPoint(dest, 53)); |
[email protected] | e905172 | 2013-04-12 21:58:18 | [diff] [blame] | 209 | if (rv != OK) |
| 210 | return false; |
| 211 | IPEndPoint endpoint; |
| 212 | rv = socket->GetLocalAddress(&endpoint); |
| 213 | if (rv != OK) |
| 214 | return false; |
[email protected] | 1c7cf3f8 | 2014-08-07 21:33:48 | [diff] [blame] | 215 | DCHECK_EQ(ADDRESS_FAMILY_IPV6, endpoint.GetFamily()); |
[email protected] | e905172 | 2013-04-12 21:58:18 | [diff] [blame] | 216 | const IPAddressNumber& address = endpoint.address(); |
| 217 | bool is_link_local = (address[0] == 0xFE) && ((address[1] & 0xC0) == 0x80); |
| 218 | if (is_link_local) |
| 219 | return false; |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame^] | 220 | const uint8_t kTeredoPrefix[] = {0x20, 0x01, 0, 0}; |
[email protected] | e905172 | 2013-04-12 21:58:18 | [diff] [blame] | 221 | bool is_teredo = std::equal(kTeredoPrefix, |
| 222 | kTeredoPrefix + arraysize(kTeredoPrefix), |
| 223 | address.begin()); |
| 224 | if (is_teredo) |
| 225 | return false; |
| 226 | return true; |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 227 | } |
| 228 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 229 | // Provide a common macro to simplify code and readability. We must use a |
| 230 | // macro as the underlying HISTOGRAM macro creates static variables. |
| 231 | #define DNS_HISTOGRAM(name, time) UMA_HISTOGRAM_CUSTOM_TIMES(name, time, \ |
| 232 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromHours(1), 100) |
| 233 | |
| 234 | // A macro to simplify code and readability. |
| 235 | #define DNS_HISTOGRAM_BY_PRIORITY(basename, priority, time) \ |
| 236 | do { \ |
| 237 | switch (priority) { \ |
| 238 | case HIGHEST: DNS_HISTOGRAM(basename "_HIGHEST", time); break; \ |
| 239 | case MEDIUM: DNS_HISTOGRAM(basename "_MEDIUM", time); break; \ |
| 240 | case LOW: DNS_HISTOGRAM(basename "_LOW", time); break; \ |
| 241 | case LOWEST: DNS_HISTOGRAM(basename "_LOWEST", time); break; \ |
| 242 | case IDLE: DNS_HISTOGRAM(basename "_IDLE", time); break; \ |
| 243 | default: NOTREACHED(); break; \ |
| 244 | } \ |
| 245 | DNS_HISTOGRAM(basename, time); \ |
| 246 | } while (0) |
| 247 | |
| 248 | // Record time from Request creation until a valid DNS response. |
| 249 | void RecordTotalTime(bool had_dns_config, |
| 250 | bool speculative, |
| 251 | base::TimeDelta duration) { |
| 252 | if (had_dns_config) { |
| 253 | if (speculative) { |
| 254 | DNS_HISTOGRAM("AsyncDNS.TotalTime_speculative", duration); |
| 255 | } else { |
| 256 | DNS_HISTOGRAM("AsyncDNS.TotalTime", duration); |
| 257 | } |
| 258 | } else { |
| 259 | if (speculative) { |
| 260 | DNS_HISTOGRAM("DNS.TotalTime_speculative", duration); |
| 261 | } else { |
| 262 | DNS_HISTOGRAM("DNS.TotalTime", duration); |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 267 | void RecordTTL(base::TimeDelta ttl) { |
| 268 | UMA_HISTOGRAM_CUSTOM_TIMES("AsyncDNS.TTL", ttl, |
| 269 | base::TimeDelta::FromSeconds(1), |
| 270 | base::TimeDelta::FromDays(1), 100); |
| 271 | } |
| 272 | |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 273 | bool ConfigureAsyncDnsNoFallbackFieldTrial() { |
| 274 | const bool kDefault = false; |
| 275 | |
| 276 | // Configure the AsyncDns field trial as follows: |
| 277 | // groups AsyncDnsNoFallbackA and AsyncDnsNoFallbackB: return true, |
| 278 | // groups AsyncDnsA and AsyncDnsB: return false, |
| 279 | // groups SystemDnsA and SystemDnsB: return false, |
| 280 | // otherwise (trial absent): return default. |
| 281 | std::string group_name = base::FieldTrialList::FindFullName("AsyncDns"); |
brettw | 3a2c690 | 2015-07-06 19:43:29 | [diff] [blame] | 282 | if (!group_name.empty()) { |
| 283 | return base::StartsWith(group_name, "AsyncDnsNoFallback", |
| 284 | base::CompareCase::INSENSITIVE_ASCII); |
| 285 | } |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 286 | return kDefault; |
| 287 | } |
| 288 | |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 289 | //----------------------------------------------------------------------------- |
| 290 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame^] | 291 | AddressList EnsurePortOnAddressList(const AddressList& list, uint16_t port) { |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 292 | if (list.empty() || list.front().port() == port) |
| 293 | return list; |
| 294 | return AddressList::CopyWithPort(list, port); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 295 | } |
| 296 | |
[email protected] | ec666ab2 | 2013-04-17 20:05:59 | [diff] [blame] | 297 | // Returns true if |addresses| contains only IPv4 loopback addresses. |
| 298 | bool IsAllIPv4Loopback(const AddressList& addresses) { |
| 299 | for (unsigned i = 0; i < addresses.size(); ++i) { |
| 300 | const IPAddressNumber& address = addresses[i].address(); |
| 301 | switch (addresses[i].GetFamily()) { |
| 302 | case ADDRESS_FAMILY_IPV4: |
| 303 | if (address[0] != 127) |
| 304 | return false; |
| 305 | break; |
| 306 | case ADDRESS_FAMILY_IPV6: |
| 307 | return false; |
| 308 | default: |
| 309 | NOTREACHED(); |
| 310 | return false; |
| 311 | } |
| 312 | } |
| 313 | return true; |
| 314 | } |
| 315 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 316 | // Creates NetLog parameters when the resolve failed. |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 317 | scoped_ptr<base::Value> NetLogProcTaskFailedCallback( |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame^] | 318 | uint32_t attempt_number, |
eroman | 001c374 | 2015-04-23 03:11:17 | [diff] [blame] | 319 | int net_error, |
| 320 | int os_error, |
| 321 | NetLogCaptureMode /* capture_mode */) { |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 322 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 323 | if (attempt_number) |
| 324 | dict->SetInteger("attempt_number", attempt_number); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 325 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 326 | dict->SetInteger("net_error", net_error); |
[email protected] | 1302488 | 2011-05-18 23:19:16 | [diff] [blame] | 327 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 328 | if (os_error) { |
| 329 | dict->SetInteger("os_error", os_error); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 330 | #if defined(OS_POSIX) |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 331 | dict->SetString("os_error_string", gai_strerror(os_error)); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 332 | #elif defined(OS_WIN) |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 333 | // Map the error code to a human-readable string. |
| 334 | LPWSTR error_string = NULL; |
Peter Kasting | be940e9 | 2014-11-20 23:14:08 | [diff] [blame] | 335 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
| 336 | 0, // Use the internal message table. |
| 337 | os_error, |
| 338 | 0, // Use default language. |
| 339 | (LPWSTR)&error_string, |
| 340 | 0, // Buffer size. |
| 341 | 0); // Arguments (unused). |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 342 | dict->SetString("os_error_string", base::WideToUTF8(error_string)); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 343 | LocalFree(error_string); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 344 | #endif |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 345 | } |
| 346 | |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 347 | return dict.Pass(); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 348 | } |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 349 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 350 | // Creates NetLog parameters when the DnsTask failed. |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 351 | scoped_ptr<base::Value> NetLogDnsTaskFailedCallback( |
| 352 | int net_error, |
| 353 | int dns_error, |
| 354 | NetLogCaptureMode /* capture_mode */) { |
| 355 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 356 | dict->SetInteger("net_error", net_error); |
| 357 | if (dns_error) |
| 358 | dict->SetInteger("dns_error", dns_error); |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 359 | return dict.Pass(); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 360 | }; |
| 361 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 362 | // Creates NetLog parameters containing the information in a RequestInfo object, |
| 363 | // along with the associated NetLog::Source. |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 364 | scoped_ptr<base::Value> NetLogRequestInfoCallback( |
| 365 | const HostResolver::RequestInfo* info, |
| 366 | NetLogCaptureMode /* capture_mode */) { |
| 367 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 368 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 369 | dict->SetString("host", info->host_port_pair().ToString()); |
| 370 | dict->SetInteger("address_family", |
| 371 | static_cast<int>(info->address_family())); |
| 372 | dict->SetBoolean("allow_cached_response", info->allow_cached_response()); |
| 373 | dict->SetBoolean("is_speculative", info->is_speculative()); |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 374 | return dict.Pass(); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 375 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 376 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 377 | // Creates NetLog parameters for the creation of a HostResolverImpl::Job. |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 378 | scoped_ptr<base::Value> NetLogJobCreationCallback( |
| 379 | const NetLog::Source& source, |
| 380 | const std::string* host, |
| 381 | NetLogCaptureMode /* capture_mode */) { |
| 382 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 383 | source.AddToEventParameters(dict.get()); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 384 | dict->SetString("host", *host); |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 385 | return dict.Pass(); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 386 | } |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 387 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 388 | // Creates NetLog parameters for HOST_RESOLVER_IMPL_JOB_ATTACH/DETACH events. |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 389 | scoped_ptr<base::Value> NetLogJobAttachCallback( |
| 390 | const NetLog::Source& source, |
| 391 | RequestPriority priority, |
| 392 | NetLogCaptureMode /* capture_mode */) { |
| 393 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 394 | source.AddToEventParameters(dict.get()); |
[email protected] | 3b04d1f2 | 2013-10-16 00:23:56 | [diff] [blame] | 395 | dict->SetString("priority", RequestPriorityToString(priority)); |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 396 | return dict.Pass(); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 397 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 398 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 399 | // Creates NetLog parameters for the DNS_CONFIG_CHANGED event. |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 400 | scoped_ptr<base::Value> NetLogDnsConfigCallback( |
| 401 | const DnsConfig* config, |
| 402 | NetLogCaptureMode /* capture_mode */) { |
| 403 | return make_scoped_ptr(config->ToValue()); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 404 | } |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 405 | |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 406 | scoped_ptr<base::Value> NetLogIPv6AvailableCallback( |
| 407 | bool ipv6_available, |
| 408 | bool cached, |
| 409 | NetLogCaptureMode /* capture_mode */) { |
| 410 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 411 | dict->SetBoolean("ipv6_available", ipv6_available); |
| 412 | dict->SetBoolean("cached", cached); |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 413 | return dict.Pass(); |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 414 | } |
| 415 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 416 | // The logging routines are defined here because some requests are resolved |
| 417 | // without a Request object. |
| 418 | |
| 419 | // Logs when a request has just been started. |
| 420 | void LogStartRequest(const BoundNetLog& source_net_log, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 421 | const HostResolver::RequestInfo& info) { |
| 422 | source_net_log.BeginEvent( |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 423 | NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 424 | base::Bind(&NetLogRequestInfoCallback, &info)); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | // Logs when a request has just completed (before its callback is run). |
| 428 | void LogFinishRequest(const BoundNetLog& source_net_log, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 429 | const HostResolver::RequestInfo& info, |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 430 | int net_error) { |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 431 | source_net_log.EndEventWithNetErrorCode( |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 432 | NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, net_error); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | // Logs when a request has been cancelled. |
| 436 | void LogCancelRequest(const BoundNetLog& source_net_log, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 437 | const HostResolverImpl::RequestInfo& info) { |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 438 | source_net_log.AddEvent(NetLog::TYPE_CANCELLED); |
| 439 | source_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 440 | } |
| 441 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 442 | //----------------------------------------------------------------------------- |
| 443 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 444 | // Keeps track of the highest priority. |
| 445 | class PriorityTracker { |
| 446 | public: |
[email protected] | 8c98d00 | 2012-07-18 19:02:27 | [diff] [blame] | 447 | explicit PriorityTracker(RequestPriority initial_priority) |
| 448 | : highest_priority_(initial_priority), total_count_(0) { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 449 | memset(counts_, 0, sizeof(counts_)); |
| 450 | } |
| 451 | |
| 452 | RequestPriority highest_priority() const { |
| 453 | return highest_priority_; |
| 454 | } |
| 455 | |
| 456 | size_t total_count() const { |
| 457 | return total_count_; |
| 458 | } |
| 459 | |
| 460 | void Add(RequestPriority req_priority) { |
| 461 | ++total_count_; |
| 462 | ++counts_[req_priority]; |
[email protected] | 31ae7ab | 2012-04-24 21:09:05 | [diff] [blame] | 463 | if (highest_priority_ < req_priority) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 464 | highest_priority_ = req_priority; |
| 465 | } |
| 466 | |
| 467 | void Remove(RequestPriority req_priority) { |
| 468 | DCHECK_GT(total_count_, 0u); |
| 469 | DCHECK_GT(counts_[req_priority], 0u); |
| 470 | --total_count_; |
| 471 | --counts_[req_priority]; |
| 472 | size_t i; |
[email protected] | 31ae7ab | 2012-04-24 21:09:05 | [diff] [blame] | 473 | for (i = highest_priority_; i > MINIMUM_PRIORITY && !counts_[i]; --i); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 474 | highest_priority_ = static_cast<RequestPriority>(i); |
| 475 | |
[email protected] | 31ae7ab | 2012-04-24 21:09:05 | [diff] [blame] | 476 | // In absence of requests, default to MINIMUM_PRIORITY. |
| 477 | if (total_count_ == 0) |
| 478 | DCHECK_EQ(MINIMUM_PRIORITY, highest_priority_); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | private: |
| 482 | RequestPriority highest_priority_; |
| 483 | size_t total_count_; |
| 484 | size_t counts_[NUM_PRIORITIES]; |
| 485 | }; |
| 486 | |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 487 | } // namespace |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 488 | |
| 489 | //----------------------------------------------------------------------------- |
| 490 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 491 | const unsigned HostResolverImpl::kMaximumDnsFailures = 16; |
| 492 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 493 | // Holds the data for a request that could not be completed synchronously. |
| 494 | // It is owned by a Job. Canceled Requests are only marked as canceled rather |
| 495 | // than removed from the Job's |requests_| list. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 496 | class HostResolverImpl::Request { |
| 497 | public: |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 498 | Request(const BoundNetLog& source_net_log, |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 499 | const RequestInfo& info, |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 500 | RequestPriority priority, |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 501 | const CompletionCallback& callback, |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 502 | AddressList* addresses) |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 503 | : source_net_log_(source_net_log), |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 504 | info_(info), |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 505 | priority_(priority), |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 506 | job_(NULL), |
| 507 | callback_(callback), |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 508 | addresses_(addresses), |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 509 | request_time_(base::TimeTicks::Now()) {} |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 510 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 511 | // Mark the request as canceled. |
| 512 | void MarkAsCanceled() { |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 513 | job_ = NULL; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 514 | addresses_ = NULL; |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 515 | callback_.Reset(); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 516 | } |
| 517 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 518 | bool was_canceled() const { |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 519 | return callback_.is_null(); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | void set_job(Job* job) { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 523 | DCHECK(job); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 524 | // Identify which job the request is waiting on. |
| 525 | job_ = job; |
| 526 | } |
| 527 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 528 | // Prepare final AddressList and call completion callback. |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 529 | void OnComplete(int error, const AddressList& addr_list) { |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 530 | DCHECK(!was_canceled()); |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 531 | if (error == OK) |
| 532 | *addresses_ = EnsurePortOnAddressList(addr_list, info_.port()); |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 533 | CompletionCallback callback = callback_; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 534 | MarkAsCanceled(); |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 535 | callback.Run(error); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 536 | } |
| 537 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 538 | Job* job() const { |
| 539 | return job_; |
| 540 | } |
| 541 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 542 | // NetLog for the source, passed in HostResolver::Resolve. |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 543 | const BoundNetLog& source_net_log() { |
| 544 | return source_net_log_; |
| 545 | } |
| 546 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 547 | const RequestInfo& info() const { |
| 548 | return info_; |
| 549 | } |
| 550 | |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 551 | RequestPriority priority() const { return priority_; } |
| 552 | |
| 553 | base::TimeTicks request_time() const { return request_time_; } |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 554 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 555 | private: |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 556 | const BoundNetLog source_net_log_; |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 557 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 558 | // The request info that started the request. |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 559 | const RequestInfo info_; |
| 560 | |
| 561 | // TODO(akalin): Support reprioritization. |
| 562 | const RequestPriority priority_; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 563 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 564 | // The resolve job that this request is dependent on. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 565 | Job* job_; |
| 566 | |
| 567 | // The user's callback to invoke when the request completes. |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 568 | CompletionCallback callback_; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 569 | |
| 570 | // The address list to save result into. |
| 571 | AddressList* addresses_; |
| 572 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 573 | const base::TimeTicks request_time_; |
| 574 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 575 | DISALLOW_COPY_AND_ASSIGN(Request); |
| 576 | }; |
| 577 | |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 578 | //------------------------------------------------------------------------------ |
| 579 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 580 | // Calls HostResolverProc on the WorkerPool. Performs retries if necessary. |
| 581 | // |
| 582 | // Whenever we try to resolve the host, we post a delayed task to check if host |
| 583 | // resolution (OnLookupComplete) is completed or not. If the original attempt |
| 584 | // hasn't completed, then we start another attempt for host resolution. We take |
| 585 | // the results from the first attempt that finishes and ignore the results from |
| 586 | // all other attempts. |
| 587 | // |
| 588 | // TODO(szym): Move to separate source file for testing and mocking. |
| 589 | // |
| 590 | class HostResolverImpl::ProcTask |
| 591 | : public base::RefCountedThreadSafe<HostResolverImpl::ProcTask> { |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 592 | public: |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 593 | typedef base::Callback<void(int net_error, |
| 594 | const AddressList& addr_list)> Callback; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 595 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 596 | ProcTask(const Key& key, |
| 597 | const ProcTaskParams& params, |
| 598 | const Callback& callback, |
| 599 | const BoundNetLog& job_net_log) |
| 600 | : key_(key), |
| 601 | params_(params), |
| 602 | callback_(callback), |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 603 | task_runner_(base::ThreadTaskRunnerHandle::Get()), |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 604 | attempt_number_(0), |
| 605 | completed_attempt_number_(0), |
| 606 | completed_attempt_error_(ERR_UNEXPECTED), |
| 607 | had_non_speculative_request_(false), |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 608 | net_log_(job_net_log) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 609 | if (!params_.resolver_proc.get()) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 610 | params_.resolver_proc = HostResolverProc::GetDefault(); |
| 611 | // If default is unset, use the system proc. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 612 | if (!params_.resolver_proc.get()) |
[email protected] | 1ee9afa1 | 2013-04-16 14:18:06 | [diff] [blame] | 613 | params_.resolver_proc = new SystemHostResolverProc(); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 614 | } |
| 615 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 616 | void Start() { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 617 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 4da911f | 2012-06-14 19:45:20 | [diff] [blame] | 618 | net_log_.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 619 | StartLookupAttempt(); |
| 620 | } |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 621 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 622 | // Cancels this ProcTask. It will be orphaned. Any outstanding resolve |
| 623 | // attempts running on worker threads will continue running. Only once all the |
| 624 | // attempts complete will the final reference to this ProcTask be released. |
| 625 | void Cancel() { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 626 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 627 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 628 | if (was_canceled() || was_completed()) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 629 | return; |
| 630 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 631 | callback_.Reset(); |
[email protected] | 4da911f | 2012-06-14 19:45:20 | [diff] [blame] | 632 | net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | void set_had_non_speculative_request() { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 636 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 637 | had_non_speculative_request_ = true; |
| 638 | } |
| 639 | |
| 640 | bool was_canceled() const { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 641 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 642 | return callback_.is_null(); |
| 643 | } |
| 644 | |
| 645 | bool was_completed() const { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 646 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 647 | return completed_attempt_number_ > 0; |
| 648 | } |
| 649 | |
| 650 | private: |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 651 | friend class base::RefCountedThreadSafe<ProcTask>; |
| 652 | ~ProcTask() {} |
| 653 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 654 | void StartLookupAttempt() { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 655 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 656 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 657 | ++attempt_number_; |
| 658 | // Dispatch the lookup attempt to a worker thread. |
| 659 | if (!base::WorkerPool::PostTask( |
| 660 | FROM_HERE, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 661 | base::Bind(&ProcTask::DoLookup, this, start_time, attempt_number_), |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 662 | true)) { |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 663 | NOTREACHED(); |
| 664 | |
| 665 | // Since we could be running within Resolve() right now, we can't just |
| 666 | // call OnLookupComplete(). Instead we must wait until Resolve() has |
| 667 | // returned (IO_PENDING). |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 668 | task_runner_->PostTask(FROM_HERE, |
| 669 | base::Bind(&ProcTask::OnLookupComplete, |
| 670 | this, |
| 671 | AddressList(), |
| 672 | start_time, |
| 673 | attempt_number_, |
| 674 | ERR_UNEXPECTED, |
| 675 | 0)); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 676 | return; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 677 | } |
[email protected] | 1302488 | 2011-05-18 23:19:16 | [diff] [blame] | 678 | |
tfarina | 5e24b24 | 2015-10-27 13:11:28 | [diff] [blame] | 679 | net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_STARTED, |
| 680 | NetLog::IntCallback("attempt_number", attempt_number_)); |
[email protected] | 1302488 | 2011-05-18 23:19:16 | [diff] [blame] | 681 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 682 | // If we don't get the results within a given time, RetryIfNotComplete |
| 683 | // will start a new attempt on a different worker thread if none of our |
| 684 | // outstanding attempts have completed yet. |
| 685 | if (attempt_number_ <= params_.max_retry_attempts) { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 686 | task_runner_->PostDelayedTask( |
[email protected] | 06ef6d9 | 2011-05-19 04:24:58 | [diff] [blame] | 687 | FROM_HERE, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 688 | base::Bind(&ProcTask::RetryIfNotComplete, this), |
[email protected] | 7e56010 | 2012-03-08 20:58:42 | [diff] [blame] | 689 | params_.unresponsive_delay); |
[email protected] | 06ef6d9 | 2011-05-19 04:24:58 | [diff] [blame] | 690 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 691 | } |
| 692 | |
[email protected] | 6c710ee | 2010-05-07 07:51:16 | [diff] [blame] | 693 | // WARNING: This code runs inside a worker pool. The shutdown code cannot |
| 694 | // wait for it to finish, so we must be very careful here about using other |
| 695 | // objects (like MessageLoops, Singletons, etc). During shutdown these objects |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 696 | // may no longer exist. Multiple DoLookups() could be running in parallel, so |
| 697 | // any state inside of |this| must not mutate . |
| 698 | void DoLookup(const base::TimeTicks& start_time, |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame^] | 699 | const uint32_t attempt_number) { |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 700 | AddressList results; |
| 701 | int os_error = 0; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 702 | // Running on the worker thread |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 703 | int error = params_.resolver_proc->Resolve(key_.hostname, |
| 704 | key_.address_family, |
| 705 | key_.host_resolver_flags, |
| 706 | &results, |
| 707 | &os_error); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 708 | |
eroman | 91dd360 | 2015-03-26 03:46:33 | [diff] [blame] | 709 | // Fail the resolution if the result contains 127.0.53.53. See the comment |
| 710 | // block of kIcanNameCollisionIp for details on why. |
| 711 | for (const auto& it : results) { |
| 712 | const IPAddressNumber& cur = it.address(); |
| 713 | if (cur.size() == arraysize(kIcanNameCollisionIp) && |
| 714 | 0 == memcmp(&cur.front(), kIcanNameCollisionIp, cur.size())) { |
| 715 | error = ERR_ICANN_NAME_COLLISION; |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 720 | task_runner_->PostTask(FROM_HERE, |
| 721 | base::Bind(&ProcTask::OnLookupComplete, |
| 722 | this, |
| 723 | results, |
| 724 | start_time, |
| 725 | attempt_number, |
| 726 | error, |
| 727 | os_error)); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 728 | } |
| 729 | |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 730 | // Makes next attempt if DoLookup() has not finished (runs on task runner |
| 731 | // thread). |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 732 | void RetryIfNotComplete() { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 733 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 734 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 735 | if (was_completed() || was_canceled()) |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 736 | return; |
| 737 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 738 | params_.unresponsive_delay *= params_.retry_factor; |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 739 | StartLookupAttempt(); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 740 | } |
| 741 | |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 742 | // Callback for when DoLookup() completes (runs on task runner thread). |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 743 | void OnLookupComplete(const AddressList& results, |
| 744 | const base::TimeTicks& start_time, |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame^] | 745 | const uint32_t attempt_number, |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 746 | int error, |
| 747 | const int os_error) { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 748 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 49b70b22 | 2013-05-07 21:24:23 | [diff] [blame] | 749 | // If results are empty, we should return an error. |
| 750 | bool empty_list_on_ok = (error == OK && results.empty()); |
| 751 | UMA_HISTOGRAM_BOOLEAN("DNS.EmptyAddressListAndNoError", empty_list_on_ok); |
| 752 | if (empty_list_on_ok) |
| 753 | error = ERR_NAME_NOT_RESOLVED; |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 754 | |
| 755 | bool was_retry_attempt = attempt_number > 1; |
| 756 | |
[email protected] | 2d3b776 | 2010-10-09 00:35:47 | [diff] [blame] | 757 | // Ideally the following code would be part of host_resolver_proc.cc, |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 758 | // however it isn't safe to call NetworkChangeNotifier from worker threads. |
| 759 | // So we do it here on the IO thread instead. |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 760 | if (error != OK && NetworkChangeNotifier::IsOffline()) |
| 761 | error = ERR_INTERNET_DISCONNECTED; |
[email protected] | 2d3b776 | 2010-10-09 00:35:47 | [diff] [blame] | 762 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 763 | // If this is the first attempt that is finishing later, then record data |
| 764 | // for the first attempt. Won't contaminate with retry attempt's data. |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 765 | if (!was_retry_attempt) |
| 766 | RecordPerformanceHistograms(start_time, error, os_error); |
| 767 | |
| 768 | RecordAttemptHistograms(start_time, attempt_number, error, os_error); |
[email protected] | f2d8c421 | 2010-02-02 00:56:35 | [diff] [blame] | 769 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 770 | if (was_canceled()) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 771 | return; |
| 772 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 773 | NetLog::ParametersCallback net_log_callback; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 774 | if (error != OK) { |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 775 | net_log_callback = base::Bind(&NetLogProcTaskFailedCallback, |
| 776 | attempt_number, |
| 777 | error, |
| 778 | os_error); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 779 | } else { |
tfarina | 5e24b24 | 2015-10-27 13:11:28 | [diff] [blame] | 780 | net_log_callback = NetLog::IntCallback("attempt_number", attempt_number); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 781 | } |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 782 | net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_FINISHED, |
| 783 | net_log_callback); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 784 | |
| 785 | if (was_completed()) |
| 786 | return; |
| 787 | |
| 788 | // Copy the results from the first worker thread that resolves the host. |
| 789 | results_ = results; |
| 790 | completed_attempt_number_ = attempt_number; |
| 791 | completed_attempt_error_ = error; |
| 792 | |
[email protected] | e87b8b51 | 2011-06-14 22:12:52 | [diff] [blame] | 793 | if (was_retry_attempt) { |
| 794 | // If retry attempt finishes before 1st attempt, then get stats on how |
| 795 | // much time is saved by having spawned an extra attempt. |
| 796 | retry_attempt_finished_time_ = base::TimeTicks::Now(); |
| 797 | } |
| 798 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 799 | if (error != OK) { |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 800 | net_log_callback = base::Bind(&NetLogProcTaskFailedCallback, |
| 801 | 0, error, os_error); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 802 | } else { |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 803 | net_log_callback = results_.CreateNetLogCallback(); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 804 | } |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 805 | net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK, |
| 806 | net_log_callback); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 807 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 808 | callback_.Run(error, results_); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 809 | } |
| 810 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 811 | void RecordPerformanceHistograms(const base::TimeTicks& start_time, |
| 812 | const int error, |
| 813 | const int os_error) const { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 814 | DCHECK(task_runner_->BelongsToCurrentThread()); |
asvitkine | c0fb802 | 2014-08-26 04:39:35 | [diff] [blame] | 815 | enum Category { // Used in UMA_HISTOGRAM_ENUMERATION. |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 816 | RESOLVE_SUCCESS, |
| 817 | RESOLVE_FAIL, |
| 818 | RESOLVE_SPECULATIVE_SUCCESS, |
| 819 | RESOLVE_SPECULATIVE_FAIL, |
| 820 | RESOLVE_MAX, // Bounding value. |
| 821 | }; |
| 822 | int category = RESOLVE_MAX; // Illegal value for later DCHECK only. |
| 823 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 824 | base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
| 825 | if (error == OK) { |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 826 | if (had_non_speculative_request_) { |
| 827 | category = RESOLVE_SUCCESS; |
| 828 | DNS_HISTOGRAM("DNS.ResolveSuccess", duration); |
| 829 | } else { |
| 830 | category = RESOLVE_SPECULATIVE_SUCCESS; |
| 831 | DNS_HISTOGRAM("DNS.ResolveSpeculativeSuccess", duration); |
| 832 | } |
[email protected] | 7e96d79 | 2011-06-10 17:08:23 | [diff] [blame] | 833 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 834 | // Log DNS lookups based on |address_family|. This will help us determine |
[email protected] | 7e96d79 | 2011-06-10 17:08:23 | [diff] [blame] | 835 | // if IPv4 or IPv4/6 lookups are faster or slower. |
| 836 | switch(key_.address_family) { |
| 837 | case ADDRESS_FAMILY_IPV4: |
| 838 | DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV4", duration); |
| 839 | break; |
| 840 | case ADDRESS_FAMILY_IPV6: |
| 841 | DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV6", duration); |
| 842 | break; |
| 843 | case ADDRESS_FAMILY_UNSPECIFIED: |
| 844 | DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_UNSPEC", duration); |
| 845 | break; |
| 846 | } |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 847 | } else { |
| 848 | if (had_non_speculative_request_) { |
| 849 | category = RESOLVE_FAIL; |
| 850 | DNS_HISTOGRAM("DNS.ResolveFail", duration); |
| 851 | } else { |
| 852 | category = RESOLVE_SPECULATIVE_FAIL; |
| 853 | DNS_HISTOGRAM("DNS.ResolveSpeculativeFail", duration); |
| 854 | } |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 855 | // Log DNS lookups based on |address_family|. This will help us determine |
[email protected] | 7e96d79 | 2011-06-10 17:08:23 | [diff] [blame] | 856 | // if IPv4 or IPv4/6 lookups are faster or slower. |
| 857 | switch(key_.address_family) { |
| 858 | case ADDRESS_FAMILY_IPV4: |
| 859 | DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV4", duration); |
| 860 | break; |
| 861 | case ADDRESS_FAMILY_IPV6: |
| 862 | DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV6", duration); |
| 863 | break; |
| 864 | case ADDRESS_FAMILY_UNSPECIFIED: |
| 865 | DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_UNSPEC", duration); |
| 866 | break; |
| 867 | } |
[email protected] | c833e32 | 2010-10-16 23:51:36 | [diff] [blame] | 868 | UMA_HISTOGRAM_CUSTOM_ENUMERATION(kOSErrorsForGetAddrinfoHistogramName, |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 869 | std::abs(os_error), |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 870 | GetAllGetAddrinfoOSErrors()); |
| 871 | } |
[email protected] | 051b6ab | 2010-10-18 16:50:46 | [diff] [blame] | 872 | DCHECK_LT(category, static_cast<int>(RESOLVE_MAX)); // Be sure it was set. |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 873 | |
| 874 | UMA_HISTOGRAM_ENUMERATION("DNS.ResolveCategory", category, RESOLVE_MAX); |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 875 | } |
| 876 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 877 | void RecordAttemptHistograms(const base::TimeTicks& start_time, |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame^] | 878 | const uint32_t attempt_number, |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 879 | const int error, |
| 880 | const int os_error) const { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 881 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 882 | bool first_attempt_to_complete = |
| 883 | completed_attempt_number_ == attempt_number; |
[email protected] | e87b8b51 | 2011-06-14 22:12:52 | [diff] [blame] | 884 | bool is_first_attempt = (attempt_number == 1); |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 885 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 886 | if (first_attempt_to_complete) { |
| 887 | // If this was first attempt to complete, then record the resolution |
| 888 | // status of the attempt. |
| 889 | if (completed_attempt_error_ == OK) { |
| 890 | UMA_HISTOGRAM_ENUMERATION( |
| 891 | "DNS.AttemptFirstSuccess", attempt_number, 100); |
| 892 | } else { |
| 893 | UMA_HISTOGRAM_ENUMERATION( |
| 894 | "DNS.AttemptFirstFailure", attempt_number, 100); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | if (error == OK) |
| 899 | UMA_HISTOGRAM_ENUMERATION("DNS.AttemptSuccess", attempt_number, 100); |
| 900 | else |
| 901 | UMA_HISTOGRAM_ENUMERATION("DNS.AttemptFailure", attempt_number, 100); |
| 902 | |
[email protected] | e87b8b51 | 2011-06-14 22:12:52 | [diff] [blame] | 903 | // If first attempt didn't finish before retry attempt, then calculate stats |
| 904 | // on how much time is saved by having spawned an extra attempt. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 905 | if (!first_attempt_to_complete && is_first_attempt && !was_canceled()) { |
[email protected] | e87b8b51 | 2011-06-14 22:12:52 | [diff] [blame] | 906 | DNS_HISTOGRAM("DNS.AttemptTimeSavedByRetry", |
| 907 | base::TimeTicks::Now() - retry_attempt_finished_time_); |
| 908 | } |
| 909 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 910 | if (was_canceled() || !first_attempt_to_complete) { |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 911 | // Count those attempts which completed after the job was already canceled |
| 912 | // OR after the job was already completed by an earlier attempt (so in |
| 913 | // effect). |
| 914 | UMA_HISTOGRAM_ENUMERATION("DNS.AttemptDiscarded", attempt_number, 100); |
| 915 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 916 | // Record if job is canceled. |
| 917 | if (was_canceled()) |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 918 | UMA_HISTOGRAM_ENUMERATION("DNS.AttemptCancelled", attempt_number, 100); |
| 919 | } |
| 920 | |
| 921 | base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
| 922 | if (error == OK) |
| 923 | DNS_HISTOGRAM("DNS.AttemptSuccessDuration", duration); |
| 924 | else |
| 925 | DNS_HISTOGRAM("DNS.AttemptFailDuration", duration); |
| 926 | } |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 927 | |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 928 | // Set on the task runner thread, read on the worker thread. |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 929 | Key key_; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 930 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 931 | // Holds an owning reference to the HostResolverProc that we are going to use. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 932 | // This may not be the current resolver procedure by the time we call |
| 933 | // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning |
| 934 | // reference ensures that it remains valid until we are done. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 935 | ProcTaskParams params_; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 936 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 937 | // The listener to the results of this ProcTask. |
| 938 | Callback callback_; |
| 939 | |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 940 | // Used to post ourselves onto the task runner thread. |
| 941 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 942 | |
| 943 | // Keeps track of the number of attempts we have made so far to resolve the |
| 944 | // host. Whenever we start an attempt to resolve the host, we increase this |
| 945 | // number. |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame^] | 946 | uint32_t attempt_number_; |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 947 | |
| 948 | // The index of the attempt which finished first (or 0 if the job is still in |
| 949 | // progress). |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame^] | 950 | uint32_t completed_attempt_number_; |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 951 | |
| 952 | // The result (a net error code) from the first attempt to complete. |
| 953 | int completed_attempt_error_; |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 954 | |
[email protected] | e87b8b51 | 2011-06-14 22:12:52 | [diff] [blame] | 955 | // The time when retry attempt was finished. |
| 956 | base::TimeTicks retry_attempt_finished_time_; |
| 957 | |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 958 | // True if a non-speculative request was ever attached to this job |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 959 | // (regardless of whether or not it was later canceled. |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 960 | // This boolean is used for histogramming the duration of jobs used to |
| 961 | // service non-speculative requests. |
| 962 | bool had_non_speculative_request_; |
| 963 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 964 | AddressList results_; |
| 965 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 966 | BoundNetLog net_log_; |
| 967 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 968 | DISALLOW_COPY_AND_ASSIGN(ProcTask); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 969 | }; |
| 970 | |
| 971 | //----------------------------------------------------------------------------- |
| 972 | |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 973 | // Wraps a call to HaveOnlyLoopbackAddresses to be executed on the WorkerPool as |
| 974 | // it takes 40-100ms and should not block initialization. |
| 975 | class HostResolverImpl::LoopbackProbeJob { |
| 976 | public: |
| 977 | explicit LoopbackProbeJob(const base::WeakPtr<HostResolverImpl>& resolver) |
| 978 | : resolver_(resolver), |
| 979 | result_(false) { |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 980 | DCHECK(resolver.get()); |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 981 | const bool kIsSlow = true; |
| 982 | base::WorkerPool::PostTaskAndReply( |
| 983 | FROM_HERE, |
| 984 | base::Bind(&LoopbackProbeJob::DoProbe, base::Unretained(this)), |
| 985 | base::Bind(&LoopbackProbeJob::OnProbeComplete, base::Owned(this)), |
| 986 | kIsSlow); |
| 987 | } |
| 988 | |
| 989 | virtual ~LoopbackProbeJob() {} |
| 990 | |
| 991 | private: |
| 992 | // Runs on worker thread. |
| 993 | void DoProbe() { |
| 994 | result_ = HaveOnlyLoopbackAddresses(); |
| 995 | } |
| 996 | |
| 997 | void OnProbeComplete() { |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 998 | if (!resolver_.get()) |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 999 | return; |
| 1000 | resolver_->SetHaveOnlyLoopbackAddresses(result_); |
| 1001 | } |
| 1002 | |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame] | 1003 | // Used/set only on task runner thread. |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1004 | base::WeakPtr<HostResolverImpl> resolver_; |
| 1005 | |
| 1006 | bool result_; |
| 1007 | |
| 1008 | DISALLOW_COPY_AND_ASSIGN(LoopbackProbeJob); |
| 1009 | }; |
| 1010 | |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 1011 | //----------------------------------------------------------------------------- |
| 1012 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1013 | // Resolves the hostname using DnsTransaction. |
| 1014 | // TODO(szym): This could be moved to separate source file as well. |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1015 | class HostResolverImpl::DnsTask : public base::SupportsWeakPtr<DnsTask> { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1016 | public: |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1017 | class Delegate { |
| 1018 | public: |
| 1019 | virtual void OnDnsTaskComplete(base::TimeTicks start_time, |
| 1020 | int net_error, |
| 1021 | const AddressList& addr_list, |
| 1022 | base::TimeDelta ttl) = 0; |
| 1023 | |
| 1024 | // Called when the first of two jobs succeeds. If the first completed |
| 1025 | // transaction fails, this is not called. Also not called when the DnsTask |
| 1026 | // only needs to run one transaction. |
| 1027 | virtual void OnFirstDnsTransactionComplete() = 0; |
| 1028 | |
| 1029 | protected: |
| 1030 | Delegate() {} |
| 1031 | virtual ~Delegate() {} |
| 1032 | }; |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1033 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1034 | DnsTask(DnsClient* client, |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1035 | const Key& key, |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1036 | Delegate* delegate, |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1037 | const BoundNetLog& job_net_log) |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1038 | : client_(client), |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1039 | key_(key), |
| 1040 | delegate_(delegate), |
| 1041 | net_log_(job_net_log), |
| 1042 | num_completed_transactions_(0), |
| 1043 | task_start_time_(base::TimeTicks::Now()) { |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1044 | DCHECK(client); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1045 | DCHECK(delegate_); |
[email protected] | 1affed6 | 2013-08-21 03:24:50 | [diff] [blame] | 1046 | } |
| 1047 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1048 | bool needs_two_transactions() const { |
| 1049 | return key_.address_family == ADDRESS_FAMILY_UNSPECIFIED; |
| 1050 | } |
| 1051 | |
| 1052 | bool needs_another_transaction() const { |
| 1053 | return needs_two_transactions() && !transaction_aaaa_; |
| 1054 | } |
| 1055 | |
| 1056 | void StartFirstTransaction() { |
| 1057 | DCHECK_EQ(0u, num_completed_transactions_); |
[email protected] | 70c04ab | 2013-08-22 16:05:12 | [diff] [blame] | 1058 | net_log_.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1059 | if (key_.address_family == ADDRESS_FAMILY_IPV6) { |
| 1060 | StartAAAA(); |
| 1061 | } else { |
| 1062 | StartA(); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | void StartSecondTransaction() { |
| 1067 | DCHECK(needs_two_transactions()); |
| 1068 | StartAAAA(); |
[email protected] | 70c04ab | 2013-08-22 16:05:12 | [diff] [blame] | 1069 | } |
| 1070 | |
| 1071 | private: |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1072 | void StartA() { |
| 1073 | DCHECK(!transaction_a_); |
| 1074 | DCHECK_NE(ADDRESS_FAMILY_IPV6, key_.address_family); |
| 1075 | transaction_a_ = CreateTransaction(ADDRESS_FAMILY_IPV4); |
| 1076 | transaction_a_->Start(); |
| 1077 | } |
| 1078 | |
| 1079 | void StartAAAA() { |
| 1080 | DCHECK(!transaction_aaaa_); |
| 1081 | DCHECK_NE(ADDRESS_FAMILY_IPV4, key_.address_family); |
| 1082 | transaction_aaaa_ = CreateTransaction(ADDRESS_FAMILY_IPV6); |
| 1083 | transaction_aaaa_->Start(); |
| 1084 | } |
| 1085 | |
| 1086 | scoped_ptr<DnsTransaction> CreateTransaction(AddressFamily family) { |
| 1087 | DCHECK_NE(ADDRESS_FAMILY_UNSPECIFIED, family); |
| 1088 | return client_->GetTransactionFactory()->CreateTransaction( |
| 1089 | key_.hostname, |
| 1090 | family == ADDRESS_FAMILY_IPV6 ? dns_protocol::kTypeAAAA : |
| 1091 | dns_protocol::kTypeA, |
| 1092 | base::Bind(&DnsTask::OnTransactionComplete, base::Unretained(this), |
| 1093 | base::TimeTicks::Now()), |
| 1094 | net_log_); |
| 1095 | } |
| 1096 | |
| 1097 | void OnTransactionComplete(const base::TimeTicks& start_time, |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1098 | DnsTransaction* transaction, |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1099 | int net_error, |
| 1100 | const DnsResponse* response) { |
[email protected] | add7653 | 2012-03-30 14:47:47 | [diff] [blame] | 1101 | DCHECK(transaction); |
[email protected] | 02cd698 | 2013-01-10 20:12:51 | [diff] [blame] | 1102 | base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1103 | if (net_error != OK) { |
[email protected] | 02cd698 | 2013-01-10 20:12:51 | [diff] [blame] | 1104 | DNS_HISTOGRAM("AsyncDNS.TransactionFailure", duration); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1105 | OnFailure(net_error, DnsResponse::DNS_PARSE_OK); |
| 1106 | return; |
[email protected] | 6c41190 | 2012-08-14 22:36:36 | [diff] [blame] | 1107 | } |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1108 | |
[email protected] | 02cd698 | 2013-01-10 20:12:51 | [diff] [blame] | 1109 | DNS_HISTOGRAM("AsyncDNS.TransactionSuccess", duration); |
| 1110 | switch (transaction->GetType()) { |
| 1111 | case dns_protocol::kTypeA: |
| 1112 | DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_A", duration); |
| 1113 | break; |
| 1114 | case dns_protocol::kTypeAAAA: |
| 1115 | DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_AAAA", duration); |
| 1116 | break; |
| 1117 | } |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1118 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1119 | AddressList addr_list; |
| 1120 | base::TimeDelta ttl; |
| 1121 | DnsResponse::Result result = response->ParseToAddressList(&addr_list, &ttl); |
| 1122 | UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ParseToAddressList", |
| 1123 | result, |
| 1124 | DnsResponse::DNS_PARSE_RESULT_MAX); |
| 1125 | if (result != DnsResponse::DNS_PARSE_OK) { |
| 1126 | // Fail even if the other query succeeds. |
| 1127 | OnFailure(ERR_DNS_MALFORMED_RESPONSE, result); |
| 1128 | return; |
| 1129 | } |
| 1130 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1131 | ++num_completed_transactions_; |
| 1132 | if (num_completed_transactions_ == 1) { |
| 1133 | ttl_ = ttl; |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1134 | } else { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1135 | ttl_ = std::min(ttl_, ttl); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1136 | } |
| 1137 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1138 | if (transaction->GetType() == dns_protocol::kTypeA) { |
| 1139 | DCHECK_EQ(transaction_a_.get(), transaction); |
| 1140 | // Place IPv4 addresses after IPv6. |
| 1141 | addr_list_.insert(addr_list_.end(), addr_list.begin(), addr_list.end()); |
| 1142 | } else { |
| 1143 | DCHECK_EQ(transaction_aaaa_.get(), transaction); |
| 1144 | // Place IPv6 addresses before IPv4. |
| 1145 | addr_list_.insert(addr_list_.begin(), addr_list.begin(), addr_list.end()); |
| 1146 | } |
| 1147 | |
| 1148 | if (needs_two_transactions() && num_completed_transactions_ == 1) { |
| 1149 | // No need to repeat the suffix search. |
| 1150 | key_.hostname = transaction->GetHostname(); |
| 1151 | delegate_->OnFirstDnsTransactionComplete(); |
| 1152 | return; |
| 1153 | } |
| 1154 | |
| 1155 | if (addr_list_.empty()) { |
[email protected] | 70c04ab | 2013-08-22 16:05:12 | [diff] [blame] | 1156 | // TODO(szym): Don't fallback to ProcTask in this case. |
| 1157 | OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1158 | return; |
| 1159 | } |
| 1160 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1161 | // If there are multiple addresses, and at least one is IPv6, need to sort |
| 1162 | // them. Note that IPv6 addresses are always put before IPv4 ones, so it's |
| 1163 | // sufficient to just check the family of the first address. |
| 1164 | if (addr_list_.size() > 1 && |
| 1165 | addr_list_[0].GetFamily() == ADDRESS_FAMILY_IPV6) { |
| 1166 | // Sort addresses if needed. Sort could complete synchronously. |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1167 | client_->GetAddressSorter()->Sort( |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1168 | addr_list_, |
[email protected] | 4589a3a | 2012-09-20 20:57:07 | [diff] [blame] | 1169 | base::Bind(&DnsTask::OnSortComplete, |
| 1170 | AsWeakPtr(), |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1171 | base::TimeTicks::Now())); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1172 | } else { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1173 | OnSuccess(addr_list_); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | void OnSortComplete(base::TimeTicks start_time, |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1178 | bool success, |
| 1179 | const AddressList& addr_list) { |
| 1180 | if (!success) { |
| 1181 | DNS_HISTOGRAM("AsyncDNS.SortFailure", |
| 1182 | base::TimeTicks::Now() - start_time); |
| 1183 | OnFailure(ERR_DNS_SORT_ERROR, DnsResponse::DNS_PARSE_OK); |
| 1184 | return; |
| 1185 | } |
| 1186 | |
| 1187 | DNS_HISTOGRAM("AsyncDNS.SortSuccess", |
| 1188 | base::TimeTicks::Now() - start_time); |
| 1189 | |
| 1190 | // AddressSorter prunes unusable destinations. |
| 1191 | if (addr_list.empty()) { |
| 1192 | LOG(WARNING) << "Address list empty after RFC3484 sort"; |
| 1193 | OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK); |
| 1194 | return; |
| 1195 | } |
| 1196 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1197 | OnSuccess(addr_list); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | void OnFailure(int net_error, DnsResponse::Result result) { |
| 1201 | DCHECK_NE(OK, net_error); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1202 | net_log_.EndEvent( |
| 1203 | NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK, |
| 1204 | base::Bind(&NetLogDnsTaskFailedCallback, net_error, result)); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1205 | delegate_->OnDnsTaskComplete(task_start_time_, net_error, AddressList(), |
| 1206 | base::TimeDelta()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1207 | } |
| 1208 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1209 | void OnSuccess(const AddressList& addr_list) { |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1210 | net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK, |
| 1211 | addr_list.CreateNetLogCallback()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1212 | delegate_->OnDnsTaskComplete(task_start_time_, OK, addr_list, ttl_); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1213 | } |
| 1214 | |
| 1215 | DnsClient* client_; |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1216 | Key key_; |
| 1217 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1218 | // The listener to the results of this DnsTask. |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1219 | Delegate* delegate_; |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1220 | const BoundNetLog net_log_; |
| 1221 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1222 | scoped_ptr<DnsTransaction> transaction_a_; |
| 1223 | scoped_ptr<DnsTransaction> transaction_aaaa_; |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1224 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1225 | unsigned num_completed_transactions_; |
| 1226 | |
| 1227 | // These are updated as each transaction completes. |
| 1228 | base::TimeDelta ttl_; |
| 1229 | // IPv6 addresses must appear first in the list. |
| 1230 | AddressList addr_list_; |
| 1231 | |
| 1232 | base::TimeTicks task_start_time_; |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1233 | |
| 1234 | DISALLOW_COPY_AND_ASSIGN(DnsTask); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1235 | }; |
| 1236 | |
| 1237 | //----------------------------------------------------------------------------- |
| 1238 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1239 | // Aggregates all Requests for the same Key. Dispatched via PriorityDispatch. |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1240 | class HostResolverImpl::Job : public PrioritizedDispatcher::Job, |
| 1241 | public HostResolverImpl::DnsTask::Delegate { |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1242 | public: |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1243 | // Creates new job for |key| where |request_net_log| is bound to the |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1244 | // request that spawned it. |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1245 | Job(const base::WeakPtr<HostResolverImpl>& resolver, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1246 | const Key& key, |
[email protected] | 8c98d00 | 2012-07-18 19:02:27 | [diff] [blame] | 1247 | RequestPriority priority, |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1248 | const BoundNetLog& source_net_log) |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1249 | : resolver_(resolver), |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1250 | key_(key), |
[email protected] | 8c98d00 | 2012-07-18 19:02:27 | [diff] [blame] | 1251 | priority_tracker_(priority), |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1252 | had_non_speculative_request_(false), |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1253 | had_dns_config_(false), |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1254 | num_occupied_job_slots_(0), |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 1255 | dns_task_error_(OK), |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1256 | creation_time_(base::TimeTicks::Now()), |
| 1257 | priority_change_time_(creation_time_), |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1258 | net_log_(BoundNetLog::Make(source_net_log.net_log(), |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1259 | NetLog::SOURCE_HOST_RESOLVER_IMPL_JOB)) { |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1260 | source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CREATE_JOB); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1261 | |
| 1262 | net_log_.BeginEvent( |
| 1263 | NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1264 | base::Bind(&NetLogJobCreationCallback, |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1265 | source_net_log.source(), |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1266 | &key_.hostname)); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1267 | } |
| 1268 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1269 | ~Job() override { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1270 | if (is_running()) { |
| 1271 | // |resolver_| was destroyed with this Job still in flight. |
| 1272 | // Clean-up, record in the log, but don't run any callbacks. |
| 1273 | if (is_proc_running()) { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1274 | proc_task_->Cancel(); |
| 1275 | proc_task_ = NULL; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1276 | } |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1277 | // Clean up now for nice NetLog. |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1278 | KillDnsTask(); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1279 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, |
| 1280 | ERR_ABORTED); |
| 1281 | } else if (is_queued()) { |
[email protected] | 57a48d3 | 2012-03-03 00:04:55 | [diff] [blame] | 1282 | // |resolver_| was destroyed without running this Job. |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1283 | // TODO(szym): is there any benefit in having this distinction? |
[email protected] | 4da911f | 2012-06-14 19:45:20 | [diff] [blame] | 1284 | net_log_.AddEvent(NetLog::TYPE_CANCELLED); |
| 1285 | net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1286 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1287 | // else CompleteRequests logged EndEvent. |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1288 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1289 | // Log any remaining Requests as cancelled. |
olli.raula | 526b098 | 2015-11-18 12:44:04 | [diff] [blame] | 1290 | for (const scoped_ptr<Request>& req : requests_) { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1291 | if (req->was_canceled()) |
| 1292 | continue; |
| 1293 | DCHECK_EQ(this, req->job()); |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1294 | LogCancelRequest(req->source_net_log(), req->info()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1295 | } |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1296 | } |
| 1297 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1298 | // Add this job to the dispatcher. If "at_head" is true, adds at the front |
| 1299 | // of the queue. |
| 1300 | void Schedule(bool at_head) { |
| 1301 | DCHECK(!is_queued()); |
| 1302 | PrioritizedDispatcher::Handle handle; |
| 1303 | if (!at_head) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1304 | handle = resolver_->dispatcher_->Add(this, priority()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1305 | } else { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1306 | handle = resolver_->dispatcher_->AddAtHead(this, priority()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1307 | } |
| 1308 | // The dispatcher could have started |this| in the above call to Add, which |
| 1309 | // could have called Schedule again. In that case |handle| will be null, |
| 1310 | // but |handle_| may have been set by the other nested call to Schedule. |
| 1311 | if (!handle.is_null()) { |
| 1312 | DCHECK(handle_.is_null()); |
| 1313 | handle_ = handle; |
| 1314 | } |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1315 | } |
| 1316 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1317 | void AddRequest(scoped_ptr<Request> req) { |
estark | d1bc206e | 2015-06-20 00:44:39 | [diff] [blame] | 1318 | DCHECK_EQ(key_.hostname, req->info().hostname()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1319 | |
| 1320 | req->set_job(this); |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 1321 | priority_tracker_.Add(req->priority()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1322 | |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1323 | req->source_net_log().AddEvent( |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1324 | NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH, |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1325 | net_log_.source().ToEventParametersCallback()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1326 | |
| 1327 | net_log_.AddEvent( |
| 1328 | NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_ATTACH, |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1329 | base::Bind(&NetLogJobAttachCallback, |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1330 | req->source_net_log().source(), |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1331 | priority())); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1332 | |
| 1333 | // TODO(szym): Check if this is still needed. |
| 1334 | if (!req->info().is_speculative()) { |
| 1335 | had_non_speculative_request_ = true; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1336 | if (proc_task_.get()) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1337 | proc_task_->set_had_non_speculative_request(); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1338 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1339 | |
hari.singh1 | bc615d387 | 2015-05-27 04:26:45 | [diff] [blame] | 1340 | requests_.push_back(req.Pass()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1341 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1342 | UpdatePriority(); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1343 | } |
| 1344 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1345 | // Marks |req| as cancelled. If it was the last active Request, also finishes |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1346 | // this Job, marking it as cancelled, and deletes it. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1347 | void CancelRequest(Request* req) { |
| 1348 | DCHECK_EQ(key_.hostname, req->info().hostname()); |
| 1349 | DCHECK(!req->was_canceled()); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1350 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1351 | // Don't remove it from |requests_| just mark it canceled. |
| 1352 | req->MarkAsCanceled(); |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1353 | LogCancelRequest(req->source_net_log(), req->info()); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1354 | |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 1355 | priority_tracker_.Remove(req->priority()); |
| 1356 | net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_DETACH, |
| 1357 | base::Bind(&NetLogJobAttachCallback, |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1358 | req->source_net_log().source(), |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 1359 | priority())); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1360 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1361 | if (num_active_requests() > 0) { |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1362 | UpdatePriority(); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1363 | } else { |
| 1364 | // If we were called from a Request's callback within CompleteRequests, |
| 1365 | // that Request could not have been cancelled, so num_active_requests() |
| 1366 | // could not be 0. Therefore, we are not in CompleteRequests(). |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1367 | CompleteRequestsWithError(OK /* cancelled */); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1368 | } |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1369 | } |
| 1370 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 1371 | // Called from AbortAllInProgressJobs. Completes all requests and destroys |
| 1372 | // the job. This currently assumes the abort is due to a network change. |
olli.raula | a21e9eb7 | 2015-12-17 08:18:11 | [diff] [blame] | 1373 | // TODO This should not delete |this|. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1374 | void Abort() { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1375 | DCHECK(is_running()); |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 1376 | CompleteRequestsWithError(ERR_NETWORK_CHANGED); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1377 | } |
| 1378 | |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 1379 | // If DnsTask present, abort it and fall back to ProcTask. |
| 1380 | void AbortDnsTask() { |
| 1381 | if (dns_task_) { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1382 | KillDnsTask(); |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 1383 | dns_task_error_ = OK; |
| 1384 | StartProcTask(); |
| 1385 | } |
| 1386 | } |
| 1387 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1388 | // Called by HostResolverImpl when this job is evicted due to queue overflow. |
| 1389 | // Completes all requests and destroys the job. |
| 1390 | void OnEvicted() { |
| 1391 | DCHECK(!is_running()); |
| 1392 | DCHECK(is_queued()); |
| 1393 | handle_.Reset(); |
| 1394 | |
[email protected] | 4da911f | 2012-06-14 19:45:20 | [diff] [blame] | 1395 | net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_EVICTED); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1396 | |
| 1397 | // This signals to CompleteRequests that this job never ran. |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1398 | CompleteRequestsWithError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1399 | } |
| 1400 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1401 | // Attempts to serve the job from HOSTS. Returns true if succeeded and |
| 1402 | // this Job was destroyed. |
| 1403 | bool ServeFromHosts() { |
| 1404 | DCHECK_GT(num_active_requests(), 0u); |
| 1405 | AddressList addr_list; |
| 1406 | if (resolver_->ServeFromHosts(key(), |
[email protected] | 3cb676a1 | 2012-06-30 15:46:03 | [diff] [blame] | 1407 | requests_.front()->info(), |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1408 | &addr_list)) { |
| 1409 | // This will destroy the Job. |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1410 | CompleteRequests( |
| 1411 | HostCache::Entry(OK, MakeAddressListForRequest(addr_list)), |
| 1412 | base::TimeDelta()); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1413 | return true; |
| 1414 | } |
| 1415 | return false; |
| 1416 | } |
| 1417 | |
tanay.c | 6ddd5f6f | 2015-08-25 06:34:57 | [diff] [blame] | 1418 | const Key& key() const { return key_; } |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 1419 | |
| 1420 | bool is_queued() const { |
| 1421 | return !handle_.is_null(); |
| 1422 | } |
| 1423 | |
| 1424 | bool is_running() const { |
| 1425 | return is_dns_running() || is_proc_running(); |
| 1426 | } |
| 1427 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1428 | private: |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1429 | void KillDnsTask() { |
| 1430 | if (dns_task_) { |
| 1431 | ReduceToOneJobSlot(); |
| 1432 | dns_task_.reset(); |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | // Reduce the number of job slots occupied and queued in the dispatcher |
| 1437 | // to one. If the second Job slot is queued in the dispatcher, cancels the |
| 1438 | // queued job. Otherwise, the second Job has been started by the |
| 1439 | // PrioritizedDispatcher, so signals it is complete. |
| 1440 | void ReduceToOneJobSlot() { |
| 1441 | DCHECK_GE(num_occupied_job_slots_, 1u); |
| 1442 | if (is_queued()) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1443 | resolver_->dispatcher_->Cancel(handle_); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1444 | handle_.Reset(); |
| 1445 | } else if (num_occupied_job_slots_ > 1) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1446 | resolver_->dispatcher_->OnJobFinished(); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1447 | --num_occupied_job_slots_; |
| 1448 | } |
| 1449 | DCHECK_EQ(1u, num_occupied_job_slots_); |
| 1450 | } |
| 1451 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1452 | void UpdatePriority() { |
| 1453 | if (is_queued()) { |
| 1454 | if (priority() != static_cast<RequestPriority>(handle_.priority())) |
| 1455 | priority_change_time_ = base::TimeTicks::Now(); |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1456 | handle_ = resolver_->dispatcher_->ChangePriority(handle_, priority()); |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1457 | } |
| 1458 | } |
| 1459 | |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1460 | AddressList MakeAddressListForRequest(const AddressList& list) const { |
| 1461 | if (requests_.empty()) |
| 1462 | return list; |
| 1463 | return AddressList::CopyWithPort(list, requests_.front()->info().port()); |
| 1464 | } |
| 1465 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1466 | // PriorityDispatch::Job: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1467 | void Start() override { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1468 | DCHECK_LE(num_occupied_job_slots_, 1u); |
| 1469 | |
[email protected] | 70c04ab | 2013-08-22 16:05:12 | [diff] [blame] | 1470 | handle_.Reset(); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1471 | ++num_occupied_job_slots_; |
| 1472 | |
| 1473 | if (num_occupied_job_slots_ == 2) { |
| 1474 | StartSecondDnsTransaction(); |
| 1475 | return; |
| 1476 | } |
| 1477 | |
| 1478 | DCHECK(!is_running()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1479 | |
[email protected] | 4da911f | 2012-06-14 19:45:20 | [diff] [blame] | 1480 | net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_STARTED); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1481 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1482 | had_dns_config_ = resolver_->HaveDnsConfig(); |
| 1483 | |
| 1484 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1485 | base::TimeDelta queue_time = now - creation_time_; |
| 1486 | base::TimeDelta queue_time_after_change = now - priority_change_time_; |
| 1487 | |
| 1488 | if (had_dns_config_) { |
| 1489 | DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTime", priority(), |
| 1490 | queue_time); |
| 1491 | DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTimeAfterChange", priority(), |
| 1492 | queue_time_after_change); |
| 1493 | } else { |
| 1494 | DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTime", priority(), queue_time); |
| 1495 | DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTimeAfterChange", priority(), |
| 1496 | queue_time_after_change); |
| 1497 | } |
| 1498 | |
[email protected] | 443714fad | 2013-09-19 04:52:01 | [diff] [blame] | 1499 | bool system_only = |
| 1500 | (key_.host_resolver_flags & HOST_RESOLVER_SYSTEM_ONLY) != 0; |
| 1501 | |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 1502 | // Caution: Job::Start must not complete synchronously. |
[email protected] | 443714fad | 2013-09-19 04:52:01 | [diff] [blame] | 1503 | if (!system_only && had_dns_config_ && |
| 1504 | !ResemblesMulticastDNSName(key_.hostname)) { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1505 | StartDnsTask(); |
| 1506 | } else { |
| 1507 | StartProcTask(); |
| 1508 | } |
| 1509 | } |
| 1510 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1511 | // TODO(szym): Since DnsTransaction does not consume threads, we can increase |
| 1512 | // the limits on |dispatcher_|. But in order to keep the number of WorkerPool |
| 1513 | // threads low, we will need to use an "inner" PrioritizedDispatcher with |
| 1514 | // tighter limits. |
| 1515 | void StartProcTask() { |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1516 | DCHECK(!is_dns_running()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1517 | proc_task_ = new ProcTask( |
| 1518 | key_, |
| 1519 | resolver_->proc_params_, |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1520 | base::Bind(&Job::OnProcTaskComplete, base::Unretained(this), |
| 1521 | base::TimeTicks::Now()), |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1522 | net_log_); |
| 1523 | |
| 1524 | if (had_non_speculative_request_) |
| 1525 | proc_task_->set_had_non_speculative_request(); |
| 1526 | // Start() could be called from within Resolve(), hence it must NOT directly |
| 1527 | // call OnProcTaskComplete, for example, on synchronous failure. |
| 1528 | proc_task_->Start(); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1529 | } |
| 1530 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1531 | // Called by ProcTask when it completes. |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1532 | void OnProcTaskComplete(base::TimeTicks start_time, |
| 1533 | int net_error, |
| 1534 | const AddressList& addr_list) { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1535 | DCHECK(is_proc_running()); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1536 | |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 1537 | if (!resolver_->resolved_known_ipv6_hostname_ && |
| 1538 | net_error == OK && |
| 1539 | key_.address_family == ADDRESS_FAMILY_UNSPECIFIED) { |
| 1540 | if (key_.hostname == "www.google.com") { |
| 1541 | resolver_->resolved_known_ipv6_hostname_ = true; |
| 1542 | bool got_ipv6_address = false; |
| 1543 | for (size_t i = 0; i < addr_list.size(); ++i) { |
[email protected] | 5134c22a | 2013-08-06 18:09:02 | [diff] [blame] | 1544 | if (addr_list[i].GetFamily() == ADDRESS_FAMILY_IPV6) { |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 1545 | got_ipv6_address = true; |
[email protected] | 5134c22a | 2013-08-06 18:09:02 | [diff] [blame] | 1546 | break; |
| 1547 | } |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 1548 | } |
| 1549 | UMA_HISTOGRAM_BOOLEAN("Net.UnspecResolvedIPv6", got_ipv6_address); |
| 1550 | } |
| 1551 | } |
| 1552 | |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 1553 | if (dns_task_error_ != OK) { |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1554 | base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1555 | if (net_error == OK) { |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1556 | DNS_HISTOGRAM("AsyncDNS.FallbackSuccess", duration); |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 1557 | if ((dns_task_error_ == ERR_NAME_NOT_RESOLVED) && |
| 1558 | ResemblesNetBIOSName(key_.hostname)) { |
| 1559 | UmaAsyncDnsResolveStatus(RESOLVE_STATUS_SUSPECT_NETBIOS); |
| 1560 | } else { |
| 1561 | UmaAsyncDnsResolveStatus(RESOLVE_STATUS_PROC_SUCCESS); |
| 1562 | } |
davidben | ad6fc44 | 2015-05-18 20:59:13 | [diff] [blame] | 1563 | UMA_HISTOGRAM_SPARSE_SLOWLY("AsyncDNS.ResolveError", |
| 1564 | std::abs(dns_task_error_)); |
[email protected] | 1ffdda8 | 2012-12-12 23:04:22 | [diff] [blame] | 1565 | resolver_->OnDnsTaskResolve(dns_task_error_); |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1566 | } else { |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1567 | DNS_HISTOGRAM("AsyncDNS.FallbackFail", duration); |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1568 | UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL); |
| 1569 | } |
| 1570 | } |
| 1571 | |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1572 | base::TimeDelta ttl = |
| 1573 | base::TimeDelta::FromSeconds(kNegativeCacheEntryTTLSeconds); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1574 | if (net_error == OK) |
| 1575 | ttl = base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1576 | |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1577 | // Don't store the |ttl| in cache since it's not obtained from the server. |
| 1578 | CompleteRequests( |
| 1579 | HostCache::Entry(net_error, MakeAddressListForRequest(addr_list)), |
| 1580 | ttl); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | void StartDnsTask() { |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1584 | DCHECK(resolver_->HaveDnsConfig()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1585 | dns_task_.reset(new DnsTask(resolver_->dns_client_.get(), key_, this, |
| 1586 | net_log_)); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1587 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1588 | dns_task_->StartFirstTransaction(); |
| 1589 | // Schedule a second transaction, if needed. |
| 1590 | if (dns_task_->needs_two_transactions()) |
| 1591 | Schedule(true); |
| 1592 | } |
| 1593 | |
| 1594 | void StartSecondDnsTransaction() { |
| 1595 | DCHECK(dns_task_->needs_two_transactions()); |
| 1596 | dns_task_->StartSecondTransaction(); |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | // Called if DnsTask fails. It is posted from StartDnsTask, so Job may be |
| 1600 | // deleted before this callback. In this case dns_task is deleted as well, |
| 1601 | // so we use it as indicator whether Job is still valid. |
| 1602 | void OnDnsTaskFailure(const base::WeakPtr<DnsTask>& dns_task, |
| 1603 | base::TimeDelta duration, |
| 1604 | int net_error) { |
| 1605 | DNS_HISTOGRAM("AsyncDNS.ResolveFail", duration); |
| 1606 | |
| 1607 | if (dns_task == NULL) |
| 1608 | return; |
| 1609 | |
| 1610 | dns_task_error_ = net_error; |
| 1611 | |
| 1612 | // TODO(szym): Run ServeFromHosts now if nsswitch.conf says so. |
| 1613 | // http://crbug.com/117655 |
| 1614 | |
| 1615 | // TODO(szym): Some net errors indicate lack of connectivity. Starting |
| 1616 | // ProcTask in that case is a waste of time. |
| 1617 | if (resolver_->fallback_to_proctask_) { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1618 | KillDnsTask(); |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 1619 | StartProcTask(); |
| 1620 | } else { |
| 1621 | UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL); |
| 1622 | CompleteRequestsWithError(net_error); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1623 | } |
| 1624 | } |
| 1625 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1626 | |
| 1627 | // HostResolverImpl::DnsTask::Delegate implementation: |
| 1628 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1629 | void OnDnsTaskComplete(base::TimeTicks start_time, |
| 1630 | int net_error, |
| 1631 | const AddressList& addr_list, |
| 1632 | base::TimeDelta ttl) override { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1633 | DCHECK(is_dns_running()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1634 | |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1635 | base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1636 | if (net_error != OK) { |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 1637 | OnDnsTaskFailure(dns_task_->AsWeakPtr(), duration, net_error); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1638 | return; |
| 1639 | } |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1640 | DNS_HISTOGRAM("AsyncDNS.ResolveSuccess", duration); |
[email protected] | 02cd698 | 2013-01-10 20:12:51 | [diff] [blame] | 1641 | // Log DNS lookups based on |address_family|. |
| 1642 | switch(key_.address_family) { |
| 1643 | case ADDRESS_FAMILY_IPV4: |
| 1644 | DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV4", duration); |
| 1645 | break; |
| 1646 | case ADDRESS_FAMILY_IPV6: |
| 1647 | DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV6", duration); |
| 1648 | break; |
| 1649 | case ADDRESS_FAMILY_UNSPECIFIED: |
| 1650 | DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_UNSPEC", duration); |
| 1651 | break; |
| 1652 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1653 | |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1654 | UmaAsyncDnsResolveStatus(RESOLVE_STATUS_DNS_SUCCESS); |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1655 | RecordTTL(ttl); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1656 | |
[email protected] | 1ffdda8 | 2012-12-12 23:04:22 | [diff] [blame] | 1657 | resolver_->OnDnsTaskResolve(OK); |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 1658 | |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1659 | base::TimeDelta bounded_ttl = |
| 1660 | std::max(ttl, base::TimeDelta::FromSeconds(kMinimumTTLSeconds)); |
| 1661 | |
| 1662 | CompleteRequests( |
| 1663 | HostCache::Entry(net_error, MakeAddressListForRequest(addr_list), ttl), |
| 1664 | bounded_ttl); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1665 | } |
| 1666 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1667 | void OnFirstDnsTransactionComplete() override { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1668 | DCHECK(dns_task_->needs_two_transactions()); |
| 1669 | DCHECK_EQ(dns_task_->needs_another_transaction(), is_queued()); |
| 1670 | // No longer need to occupy two dispatcher slots. |
| 1671 | ReduceToOneJobSlot(); |
| 1672 | |
| 1673 | // We already have a job slot at the dispatcher, so if the second |
| 1674 | // transaction hasn't started, reuse it now instead of waiting in the queue |
| 1675 | // for the second slot. |
| 1676 | if (dns_task_->needs_another_transaction()) |
| 1677 | dns_task_->StartSecondTransaction(); |
| 1678 | } |
| 1679 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1680 | // Performs Job's last rites. Completes all Requests. Deletes this. |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1681 | void CompleteRequests(const HostCache::Entry& entry, |
| 1682 | base::TimeDelta ttl) { |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 1683 | CHECK(resolver_.get()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1684 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1685 | // This job must be removed from resolver's |jobs_| now to make room for a |
| 1686 | // new job with the same key in case one of the OnComplete callbacks decides |
| 1687 | // to spawn one. Consequently, the job deletes itself when CompleteRequests |
| 1688 | // is done. |
| 1689 | scoped_ptr<Job> self_deleter(this); |
| 1690 | |
| 1691 | resolver_->RemoveJob(this); |
| 1692 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1693 | if (is_running()) { |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1694 | if (is_proc_running()) { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1695 | DCHECK(!is_queued()); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1696 | proc_task_->Cancel(); |
| 1697 | proc_task_ = NULL; |
| 1698 | } |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1699 | KillDnsTask(); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1700 | |
| 1701 | // Signal dispatcher that a slot has opened. |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1702 | resolver_->dispatcher_->OnJobFinished(); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1703 | } else if (is_queued()) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1704 | resolver_->dispatcher_->Cancel(handle_); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1705 | handle_.Reset(); |
| 1706 | } |
| 1707 | |
| 1708 | if (num_active_requests() == 0) { |
[email protected] | 4da911f | 2012-06-14 19:45:20 | [diff] [blame] | 1709 | net_log_.AddEvent(NetLog::TYPE_CANCELLED); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1710 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, |
| 1711 | OK); |
| 1712 | return; |
| 1713 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1714 | |
| 1715 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1716 | entry.error); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1717 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1718 | DCHECK(!requests_.empty()); |
| 1719 | |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1720 | if (entry.error == OK) { |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 1721 | // Record this histogram here, when we know the system has a valid DNS |
| 1722 | // configuration. |
[email protected] | 539df6c | 2012-06-19 21:21:29 | [diff] [blame] | 1723 | UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig", |
| 1724 | resolver_->received_dns_config_); |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 1725 | } |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1726 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 1727 | bool did_complete = (entry.error != ERR_NETWORK_CHANGED) && |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1728 | (entry.error != ERR_HOST_RESOLVER_QUEUE_TOO_LARGE); |
| 1729 | if (did_complete) |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1730 | resolver_->CacheResult(key_, entry, ttl); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1731 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1732 | // Complete all of the requests that were attached to the job. |
olli.raula | 526b098 | 2015-11-18 12:44:04 | [diff] [blame] | 1733 | for (const scoped_ptr<Request>& req : requests_) { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1734 | if (req->was_canceled()) |
| 1735 | continue; |
| 1736 | |
| 1737 | DCHECK_EQ(this, req->job()); |
| 1738 | // Update the net log and notify registered observers. |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1739 | LogFinishRequest(req->source_net_log(), req->info(), entry.error); |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1740 | if (did_complete) { |
| 1741 | // Record effective total time from creation to completion. |
| 1742 | RecordTotalTime(had_dns_config_, req->info().is_speculative(), |
| 1743 | base::TimeTicks::Now() - req->request_time()); |
| 1744 | } |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1745 | req->OnComplete(entry.error, entry.addrlist); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1746 | |
| 1747 | // Check if the resolver was destroyed as a result of running the |
| 1748 | // callback. If it was, we could continue, but we choose to bail. |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 1749 | if (!resolver_.get()) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1750 | return; |
| 1751 | } |
| 1752 | } |
| 1753 | |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1754 | // Convenience wrapper for CompleteRequests in case of failure. |
| 1755 | void CompleteRequestsWithError(int net_error) { |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1756 | CompleteRequests(HostCache::Entry(net_error, AddressList()), |
| 1757 | base::TimeDelta()); |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1758 | } |
| 1759 | |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 1760 | RequestPriority priority() const { |
| 1761 | return priority_tracker_.highest_priority(); |
| 1762 | } |
| 1763 | |
| 1764 | // Number of non-canceled requests in |requests_|. |
| 1765 | size_t num_active_requests() const { |
| 1766 | return priority_tracker_.total_count(); |
| 1767 | } |
| 1768 | |
| 1769 | bool is_dns_running() const { |
| 1770 | return dns_task_.get() != NULL; |
| 1771 | } |
| 1772 | |
| 1773 | bool is_proc_running() const { |
| 1774 | return proc_task_.get() != NULL; |
| 1775 | } |
| 1776 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1777 | base::WeakPtr<HostResolverImpl> resolver_; |
| 1778 | |
| 1779 | Key key_; |
| 1780 | |
| 1781 | // Tracks the highest priority across |requests_|. |
| 1782 | PriorityTracker priority_tracker_; |
| 1783 | |
| 1784 | bool had_non_speculative_request_; |
| 1785 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1786 | // Distinguishes measurements taken while DnsClient was fully configured. |
| 1787 | bool had_dns_config_; |
| 1788 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1789 | // Number of slots occupied by this Job in resolver's PrioritizedDispatcher. |
| 1790 | unsigned num_occupied_job_slots_; |
| 1791 | |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 1792 | // Result of DnsTask. |
| 1793 | int dns_task_error_; |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1794 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1795 | const base::TimeTicks creation_time_; |
| 1796 | base::TimeTicks priority_change_time_; |
| 1797 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1798 | BoundNetLog net_log_; |
| 1799 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1800 | // Resolves the host using a HostResolverProc. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1801 | scoped_refptr<ProcTask> proc_task_; |
| 1802 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1803 | // Resolves the host using a DnsTransaction. |
| 1804 | scoped_ptr<DnsTask> dns_task_; |
| 1805 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1806 | // All Requests waiting for the result of this Job. Some can be canceled. |
olli.raula | 526b098 | 2015-11-18 12:44:04 | [diff] [blame] | 1807 | std::vector<scoped_ptr<Request>> requests_; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1808 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1809 | // A handle used in |HostResolverImpl::dispatcher_|. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1810 | PrioritizedDispatcher::Handle handle_; |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1811 | }; |
| 1812 | |
| 1813 | //----------------------------------------------------------------------------- |
| 1814 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1815 | HostResolverImpl::ProcTaskParams::ProcTaskParams( |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 1816 | HostResolverProc* resolver_proc, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1817 | size_t max_retry_attempts) |
| 1818 | : resolver_proc(resolver_proc), |
| 1819 | max_retry_attempts(max_retry_attempts), |
| 1820 | unresponsive_delay(base::TimeDelta::FromMilliseconds(6000)), |
| 1821 | retry_factor(2) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1822 | // Maximum of 4 retry attempts for host resolution. |
| 1823 | static const size_t kDefaultMaxRetryAttempts = 4u; |
| 1824 | if (max_retry_attempts == HostResolver::kDefaultRetryAttempts) |
| 1825 | max_retry_attempts = kDefaultMaxRetryAttempts; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | HostResolverImpl::ProcTaskParams::~ProcTaskParams() {} |
| 1829 | |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1830 | HostResolverImpl::HostResolverImpl(const Options& options, NetLog* net_log) |
| 1831 | : max_queued_jobs_(0), |
| 1832 | proc_params_(NULL, options.max_retry_attempts), |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 1833 | net_log_(net_log), |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 1834 | received_dns_config_(false), |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 1835 | num_dns_failures_(0), |
[email protected] | c9fa8f31 | 2013-09-17 12:24:52 | [diff] [blame] | 1836 | use_local_ipv6_(false), |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 1837 | last_ipv6_probe_result_(true), |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 1838 | resolved_known_ipv6_hostname_(false), |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 1839 | additional_resolver_flags_(0), |
[email protected] | 0a30cf51 | 2014-05-27 20:55:18 | [diff] [blame] | 1840 | fallback_to_proctask_(true), |
| 1841 | weak_ptr_factory_(this), |
| 1842 | probe_weak_ptr_factory_(this) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1843 | if (options.enable_caching) |
| 1844 | cache_ = HostCache::CreateDefaultCache(); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1845 | |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1846 | PrioritizedDispatcher::Limits job_limits = options.GetDispatcherLimits(); |
| 1847 | dispatcher_.reset(new PrioritizedDispatcher(job_limits)); |
| 1848 | max_queued_jobs_ = job_limits.total_jobs * 100u; |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1849 | |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1850 | DCHECK_GE(dispatcher_->num_priorities(), static_cast<size_t>(NUM_PRIORITIES)); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1851 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1852 | #if defined(OS_WIN) |
| 1853 | EnsureWinsockInit(); |
| 1854 | #endif |
[email protected] | 7c466e9 | 2013-07-20 01:44:48 | [diff] [blame] | 1855 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1856 | new LoopbackProbeJob(weak_ptr_factory_.GetWeakPtr()); |
[email protected] | 2f3bc65c | 2010-07-23 17:47:10 | [diff] [blame] | 1857 | #endif |
[email protected] | 232a581 | 2011-03-04 22:42:08 | [diff] [blame] | 1858 | NetworkChangeNotifier::AddIPAddressObserver(this); |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 1859 | NetworkChangeNotifier::AddDNSObserver(this); |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 1860 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \ |
| 1861 | !defined(OS_ANDROID) |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 1862 | EnsureDnsReloaderInit(); |
[email protected] | 46018c9d | 2011-09-06 03:42:34 | [diff] [blame] | 1863 | #endif |
[email protected] | 2ac22db | 2012-11-28 19:50:04 | [diff] [blame] | 1864 | |
[email protected] | 2ac22db | 2012-11-28 19:50:04 | [diff] [blame] | 1865 | { |
| 1866 | DnsConfig dns_config; |
| 1867 | NetworkChangeNotifier::GetDnsConfig(&dns_config); |
| 1868 | received_dns_config_ = dns_config.IsValid(); |
[email protected] | c9fa8f31 | 2013-09-17 12:24:52 | [diff] [blame] | 1869 | // Conservatively assume local IPv6 is needed when DnsConfig is not valid. |
| 1870 | use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6; |
[email protected] | 2ac22db | 2012-11-28 19:50:04 | [diff] [blame] | 1871 | } |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 1872 | |
| 1873 | fallback_to_proctask_ = !ConfigureAsyncDnsNoFallbackFieldTrial(); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | HostResolverImpl::~HostResolverImpl() { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1877 | // Prevent the dispatcher from starting new jobs. |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1878 | dispatcher_->SetLimitsToZero(); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1879 | // It's now safe for Jobs to call KillDsnTask on destruction, because |
| 1880 | // OnJobComplete will not start any new jobs. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1881 | STLDeleteValues(&jobs_); |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 1882 | |
[email protected] | 232a581 | 2011-03-04 22:42:08 | [diff] [blame] | 1883 | NetworkChangeNotifier::RemoveIPAddressObserver(this); |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 1884 | NetworkChangeNotifier::RemoveDNSObserver(this); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1885 | } |
| 1886 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1887 | void HostResolverImpl::SetMaxQueuedJobs(size_t value) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1888 | DCHECK_EQ(0u, dispatcher_->num_queued_jobs()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1889 | DCHECK_GT(value, 0u); |
| 1890 | max_queued_jobs_ = value; |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 1891 | } |
| 1892 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 1893 | int HostResolverImpl::Resolve(const RequestInfo& info, |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 1894 | RequestPriority priority, |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1895 | AddressList* addresses, |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 1896 | const CompletionCallback& callback, |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 1897 | RequestHandle* out_req, |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1898 | const BoundNetLog& source_net_log) { |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 1899 | DCHECK(addresses); |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 1900 | DCHECK(CalledOnValidThread()); |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 1901 | DCHECK_EQ(false, callback.is_null()); |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 1902 | |
[email protected] | e806cd7 | 2013-05-17 02:08:43 | [diff] [blame] | 1903 | // Check that the caller supplied a valid hostname to resolve. |
| 1904 | std::string labeled_hostname; |
| 1905 | if (!DNSDomainFromDot(info.hostname(), &labeled_hostname)) |
| 1906 | return ERR_NAME_NOT_RESOLVED; |
| 1907 | |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1908 | LogStartRequest(source_net_log, info); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1909 | |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 1910 | IPAddressNumber ip_number; |
| 1911 | IPAddressNumber* ip_number_ptr = nullptr; |
| 1912 | if (ParseIPLiteralToNumber(info.hostname(), &ip_number)) |
| 1913 | ip_number_ptr = &ip_number; |
| 1914 | |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 1915 | // Build a key that identifies the request in the cache and in the |
| 1916 | // outstanding jobs map. |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 1917 | Key key = GetEffectiveKeyForRequest(info, ip_number_ptr, source_net_log); |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 1918 | |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 1919 | int rv = ResolveHelper(key, info, ip_number_ptr, addresses, source_net_log); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 1920 | if (rv != ERR_DNS_CACHE_MISS) { |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1921 | LogFinishRequest(source_net_log, info, rv); |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1922 | RecordTotalTime(HaveDnsConfig(), info.is_speculative(), base::TimeDelta()); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 1923 | return rv; |
[email protected] | 3836871 | 2011-03-02 08:09:40 | [diff] [blame] | 1924 | } |
| 1925 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1926 | // Next we need to attach our request to a "job". This job is responsible for |
| 1927 | // calling "getaddrinfo(hostname)" on a worker thread. |
| 1928 | |
| 1929 | JobMap::iterator jobit = jobs_.find(key); |
| 1930 | Job* job; |
| 1931 | if (jobit == jobs_.end()) { |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 1932 | job = |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1933 | new Job(weak_ptr_factory_.GetWeakPtr(), key, priority, source_net_log); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1934 | job->Schedule(false); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1935 | |
| 1936 | // Check for queue overflow. |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1937 | if (dispatcher_->num_queued_jobs() > max_queued_jobs_) { |
| 1938 | Job* evicted = static_cast<Job*>(dispatcher_->EvictOldestLowest()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1939 | DCHECK(evicted); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1940 | evicted->OnEvicted(); // Deletes |evicted|. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1941 | if (evicted == job) { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1942 | rv = ERR_HOST_RESOLVER_QUEUE_TOO_LARGE; |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1943 | LogFinishRequest(source_net_log, info, rv); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1944 | return rv; |
| 1945 | } |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1946 | } |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1947 | jobs_.insert(jobit, std::make_pair(key, job)); |
| 1948 | } else { |
| 1949 | job = jobit->second; |
| 1950 | } |
| 1951 | |
| 1952 | // Can't complete synchronously. Create and attach request. |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 1953 | scoped_ptr<Request> req(new Request( |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1954 | source_net_log, info, priority, callback, addresses)); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1955 | if (out_req) |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1956 | *out_req = reinterpret_cast<RequestHandle>(req.get()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1957 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1958 | job->AddRequest(req.Pass()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1959 | // Completion happens during Job::CompleteRequests(). |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1960 | return ERR_IO_PENDING; |
| 1961 | } |
| 1962 | |
[email protected] | 287d7c2 | 2011-11-15 17:34:25 | [diff] [blame] | 1963 | int HostResolverImpl::ResolveHelper(const Key& key, |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 1964 | const RequestInfo& info, |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 1965 | const IPAddressNumber* ip_number, |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 1966 | AddressList* addresses, |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1967 | const BoundNetLog& source_net_log) { |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 1968 | // The result of |getaddrinfo| for empty hosts is inconsistent across systems. |
| 1969 | // On Windows it gives the default interface's address, whereas on Linux it |
| 1970 | // gives an error. We will make it fail on all platforms for consistency. |
| 1971 | if (info.hostname().empty() || info.hostname().size() > kMaxHostLength) |
| 1972 | return ERR_NAME_NOT_RESOLVED; |
| 1973 | |
| 1974 | int net_error = ERR_UNEXPECTED; |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 1975 | if (ResolveAsIP(key, info, ip_number, &net_error, addresses)) |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 1976 | return net_error; |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1977 | if (ServeFromCache(key, info, &net_error, addresses)) { |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1978 | source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1979 | return net_error; |
| 1980 | } |
| 1981 | // TODO(szym): Do not do this if nsswitch.conf instructs not to. |
| 1982 | // http://crbug.com/117655 |
| 1983 | if (ServeFromHosts(key, info, addresses)) { |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1984 | source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_HOSTS_HIT); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1985 | return OK; |
| 1986 | } |
estark | d1bc206e | 2015-06-20 00:44:39 | [diff] [blame] | 1987 | |
| 1988 | if (ServeLocalhost(key, info, addresses)) |
| 1989 | return OK; |
| 1990 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1991 | return ERR_DNS_CACHE_MISS; |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | int HostResolverImpl::ResolveFromCache(const RequestInfo& info, |
| 1995 | AddressList* addresses, |
| 1996 | const BoundNetLog& source_net_log) { |
| 1997 | DCHECK(CalledOnValidThread()); |
| 1998 | DCHECK(addresses); |
| 1999 | |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2000 | // Update the net log and notify registered observers. |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 2001 | LogStartRequest(source_net_log, info); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2002 | |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2003 | IPAddressNumber ip_number; |
| 2004 | IPAddressNumber* ip_number_ptr = nullptr; |
| 2005 | if (ParseIPLiteralToNumber(info.hostname(), &ip_number)) |
| 2006 | ip_number_ptr = &ip_number; |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2007 | |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2008 | Key key = GetEffectiveKeyForRequest(info, ip_number_ptr, source_net_log); |
| 2009 | |
| 2010 | int rv = ResolveHelper(key, info, ip_number_ptr, addresses, source_net_log); |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 2011 | LogFinishRequest(source_net_log, info, rv); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2012 | return rv; |
| 2013 | } |
| 2014 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2015 | void HostResolverImpl::CancelRequest(RequestHandle req_handle) { |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 2016 | DCHECK(CalledOnValidThread()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2017 | Request* req = reinterpret_cast<Request*>(req_handle); |
| 2018 | DCHECK(req); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2019 | Job* job = req->job(); |
| 2020 | DCHECK(job); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2021 | job->CancelRequest(req); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2022 | } |
| 2023 | |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2024 | void HostResolverImpl::SetDnsClientEnabled(bool enabled) { |
| 2025 | DCHECK(CalledOnValidThread()); |
| 2026 | #if defined(ENABLE_BUILT_IN_DNS) |
| 2027 | if (enabled && !dns_client_) { |
| 2028 | SetDnsClient(DnsClient::CreateClient(net_log_)); |
| 2029 | } else if (!enabled && dns_client_) { |
| 2030 | SetDnsClient(scoped_ptr<DnsClient>()); |
| 2031 | } |
| 2032 | #endif |
| 2033 | } |
| 2034 | |
[email protected] | 489d1a8 | 2011-10-12 03:09:11 | [diff] [blame] | 2035 | HostCache* HostResolverImpl::GetHostCache() { |
| 2036 | return cache_.get(); |
| 2037 | } |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2038 | |
[email protected] | 17e9203 | 2012-03-29 00:56:24 | [diff] [blame] | 2039 | base::Value* HostResolverImpl::GetDnsConfigAsValue() const { |
| 2040 | // Check if async DNS is disabled. |
| 2041 | if (!dns_client_.get()) |
| 2042 | return NULL; |
| 2043 | |
| 2044 | // Check if async DNS is enabled, but we currently have no configuration |
| 2045 | // for it. |
| 2046 | const DnsConfig* dns_config = dns_client_->GetConfig(); |
| 2047 | if (dns_config == NULL) |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 2048 | return new base::DictionaryValue(); |
[email protected] | 17e9203 | 2012-03-29 00:56:24 | [diff] [blame] | 2049 | |
| 2050 | return dns_config->ToValue(); |
| 2051 | } |
| 2052 | |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2053 | bool HostResolverImpl::ResolveAsIP(const Key& key, |
| 2054 | const RequestInfo& info, |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2055 | const IPAddressNumber* ip_number, |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2056 | int* net_error, |
| 2057 | AddressList* addresses) { |
| 2058 | DCHECK(addresses); |
| 2059 | DCHECK(net_error); |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2060 | if (ip_number == nullptr) |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2061 | return false; |
| 2062 | |
| 2063 | DCHECK_EQ(key.host_resolver_flags & |
| 2064 | ~(HOST_RESOLVER_CANONNAME | HOST_RESOLVER_LOOPBACK_ONLY | |
| 2065 | HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6), |
| 2066 | 0) << " Unhandled flag"; |
[email protected] | 1c7cf3f8 | 2014-08-07 21:33:48 | [diff] [blame] | 2067 | |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2068 | *net_error = OK; |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2069 | AddressFamily family = GetAddressFamily(*ip_number); |
cbentzel | 1906f87 | 2015-06-05 16:25:25 | [diff] [blame] | 2070 | if (key.address_family != ADDRESS_FAMILY_UNSPECIFIED && |
| 2071 | key.address_family != family) { |
[email protected] | 1c7cf3f8 | 2014-08-07 21:33:48 | [diff] [blame] | 2072 | // Don't return IPv6 addresses for IPv4 queries, and vice versa. |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2073 | *net_error = ERR_NAME_NOT_RESOLVED; |
| 2074 | } else { |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2075 | *addresses = AddressList::CreateFromIPAddress(*ip_number, info.port()); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 2076 | if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME) |
| 2077 | addresses->SetDefaultCanonicalName(); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2078 | } |
| 2079 | return true; |
| 2080 | } |
| 2081 | |
| 2082 | bool HostResolverImpl::ServeFromCache(const Key& key, |
| 2083 | const RequestInfo& info, |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2084 | int* net_error, |
| 2085 | AddressList* addresses) { |
| 2086 | DCHECK(addresses); |
| 2087 | DCHECK(net_error); |
| 2088 | if (!info.allow_cached_response() || !cache_.get()) |
| 2089 | return false; |
| 2090 | |
[email protected] | 407a30ab | 2012-08-15 17:16:10 | [diff] [blame] | 2091 | const HostCache::Entry* cache_entry = cache_->Lookup( |
| 2092 | key, base::TimeTicks::Now()); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2093 | if (!cache_entry) |
| 2094 | return false; |
| 2095 | |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2096 | *net_error = cache_entry->error; |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 2097 | if (*net_error == OK) { |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 2098 | if (cache_entry->has_ttl()) |
| 2099 | RecordTTL(cache_entry->ttl); |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 2100 | *addresses = EnsurePortOnAddressList(cache_entry->addrlist, info.port()); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 2101 | } |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2102 | return true; |
| 2103 | } |
| 2104 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2105 | bool HostResolverImpl::ServeFromHosts(const Key& key, |
| 2106 | const RequestInfo& info, |
| 2107 | AddressList* addresses) { |
| 2108 | DCHECK(addresses); |
| 2109 | if (!HaveDnsConfig()) |
| 2110 | return false; |
[email protected] | 05a79d4 | 2013-03-28 07:30:09 | [diff] [blame] | 2111 | addresses->clear(); |
| 2112 | |
[email protected] | cb50762 | 2012-03-23 16:17:06 | [diff] [blame] | 2113 | // HOSTS lookups are case-insensitive. |
brettw | 8e2106d | 2015-08-11 19:30:22 | [diff] [blame] | 2114 | std::string hostname = base::ToLowerASCII(key.hostname); |
[email protected] | cb50762 | 2012-03-23 16:17:06 | [diff] [blame] | 2115 | |
[email protected] | 05a79d4 | 2013-03-28 07:30:09 | [diff] [blame] | 2116 | const DnsHosts& hosts = dns_client_->GetConfig()->hosts; |
| 2117 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2118 | // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations |
| 2119 | // (glibc and c-ares) return the first matching line. We have more |
| 2120 | // flexibility, but lose implicit ordering. |
[email protected] | 05a79d4 | 2013-03-28 07:30:09 | [diff] [blame] | 2121 | // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if |
| 2122 | // necessary. |
| 2123 | if (key.address_family == ADDRESS_FAMILY_IPV6 || |
| 2124 | key.address_family == ADDRESS_FAMILY_UNSPECIFIED) { |
| 2125 | DnsHosts::const_iterator it = hosts.find( |
| 2126 | DnsHostsKey(hostname, ADDRESS_FAMILY_IPV6)); |
| 2127 | if (it != hosts.end()) |
| 2128 | addresses->push_back(IPEndPoint(it->second, info.port())); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2129 | } |
| 2130 | |
[email protected] | 05a79d4 | 2013-03-28 07:30:09 | [diff] [blame] | 2131 | if (key.address_family == ADDRESS_FAMILY_IPV4 || |
| 2132 | key.address_family == ADDRESS_FAMILY_UNSPECIFIED) { |
| 2133 | DnsHosts::const_iterator it = hosts.find( |
| 2134 | DnsHostsKey(hostname, ADDRESS_FAMILY_IPV4)); |
| 2135 | if (it != hosts.end()) |
| 2136 | addresses->push_back(IPEndPoint(it->second, info.port())); |
| 2137 | } |
| 2138 | |
[email protected] | ec666ab2 | 2013-04-17 20:05:59 | [diff] [blame] | 2139 | // If got only loopback addresses and the family was restricted, resolve |
| 2140 | // again, without restrictions. See SystemHostResolverCall for rationale. |
| 2141 | if ((key.host_resolver_flags & |
| 2142 | HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) && |
| 2143 | IsAllIPv4Loopback(*addresses)) { |
| 2144 | Key new_key(key); |
| 2145 | new_key.address_family = ADDRESS_FAMILY_UNSPECIFIED; |
| 2146 | new_key.host_resolver_flags &= |
| 2147 | ~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; |
| 2148 | return ServeFromHosts(new_key, info, addresses); |
| 2149 | } |
[email protected] | 05a79d4 | 2013-03-28 07:30:09 | [diff] [blame] | 2150 | return !addresses->empty(); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2151 | } |
| 2152 | |
estark | d1bc206e | 2015-06-20 00:44:39 | [diff] [blame] | 2153 | bool HostResolverImpl::ServeLocalhost(const Key& key, |
| 2154 | const RequestInfo& info, |
| 2155 | AddressList* addresses) { |
| 2156 | AddressList resolved_addresses; |
| 2157 | if (!ResolveLocalHostname(key.hostname, info.port(), &resolved_addresses)) |
| 2158 | return false; |
| 2159 | |
| 2160 | addresses->clear(); |
| 2161 | |
| 2162 | for (const auto& address : resolved_addresses) { |
| 2163 | // Include the address if: |
| 2164 | // - caller didn't specify an address family, or |
| 2165 | // - caller specifically asked for the address family of this address, or |
| 2166 | // - this is an IPv6 address and caller specifically asked for IPv4 due |
| 2167 | // to lack of detected IPv6 support. (See SystemHostResolverCall for |
| 2168 | // rationale). |
| 2169 | if (key.address_family == ADDRESS_FAMILY_UNSPECIFIED || |
| 2170 | key.address_family == address.GetFamily() || |
| 2171 | (address.GetFamily() == ADDRESS_FAMILY_IPV6 && |
| 2172 | key.address_family == ADDRESS_FAMILY_IPV4 && |
| 2173 | (key.host_resolver_flags & |
| 2174 | HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6))) { |
| 2175 | addresses->push_back(address); |
| 2176 | } |
| 2177 | } |
| 2178 | |
| 2179 | return true; |
| 2180 | } |
| 2181 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 2182 | void HostResolverImpl::CacheResult(const Key& key, |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 2183 | const HostCache::Entry& entry, |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 2184 | base::TimeDelta ttl) { |
| 2185 | if (cache_.get()) |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 2186 | cache_->Set(key, entry, base::TimeTicks::Now(), ttl); |
[email protected] | ef4c40c | 2010-09-01 14:42:03 | [diff] [blame] | 2187 | } |
| 2188 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2189 | void HostResolverImpl::RemoveJob(Job* job) { |
| 2190 | DCHECK(job); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 2191 | JobMap::iterator it = jobs_.find(job->key()); |
| 2192 | if (it != jobs_.end() && it->second == job) |
| 2193 | jobs_.erase(it); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2194 | } |
| 2195 | |
[email protected] | 9936a786 | 2012-10-26 04:44:02 | [diff] [blame] | 2196 | void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result) { |
| 2197 | if (result) { |
| 2198 | additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY; |
| 2199 | } else { |
| 2200 | additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY; |
| 2201 | } |
| 2202 | } |
| 2203 | |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 2204 | HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest( |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2205 | const RequestInfo& info, |
| 2206 | const IPAddressNumber* ip_number, |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 2207 | const BoundNetLog& net_log) { |
[email protected] | eaf3a3b | 2010-09-03 20:34:27 | [diff] [blame] | 2208 | HostResolverFlags effective_flags = |
| 2209 | info.host_resolver_flags() | additional_resolver_flags_; |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 2210 | AddressFamily effective_address_family = info.address_family(); |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 2211 | |
| 2212 | if (info.address_family() == ADDRESS_FAMILY_UNSPECIFIED) { |
cbentzel | 1906f87 | 2015-06-05 16:25:25 | [diff] [blame] | 2213 | if (!use_local_ipv6_ && |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2214 | // When resolving IPv4 literals, there's no need to probe for IPv6. |
| 2215 | // When resolving IPv6 literals, there's no benefit to artificially |
| 2216 | // limiting our resolution based on a probe. Prior logic ensures |
| 2217 | // that this query is UNSPECIFIED (see info.address_family() |
cbentzel | 1906f87 | 2015-06-05 16:25:25 | [diff] [blame] | 2218 | // check above) so the code requesting the resolution should be amenable |
| 2219 | // to receiving a IPv6 resolution. |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2220 | ip_number == nullptr) { |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 2221 | if (!IsIPv6Reachable(net_log)) { |
[email protected] | ac0b52e | 2013-04-21 01:26:16 | [diff] [blame] | 2222 | effective_address_family = ADDRESS_FAMILY_IPV4; |
| 2223 | effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; |
| 2224 | } |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 2225 | } |
| 2226 | } |
| 2227 | |
estark | d1bc206e | 2015-06-20 00:44:39 | [diff] [blame] | 2228 | return Key(info.hostname(), effective_address_family, effective_flags); |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 2229 | } |
| 2230 | |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 2231 | bool HostResolverImpl::IsIPv6Reachable(const BoundNetLog& net_log) { |
| 2232 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2233 | bool cached = true; |
| 2234 | if ((now - last_ipv6_probe_time_).InMilliseconds() > kIPv6ProbePeriodMs) { |
| 2235 | IPAddressNumber address(kIPv6ProbeAddress, |
| 2236 | kIPv6ProbeAddress + arraysize(kIPv6ProbeAddress)); |
| 2237 | last_ipv6_probe_result_ = IsGloballyReachable(address, net_log); |
| 2238 | last_ipv6_probe_time_ = now; |
| 2239 | cached = false; |
| 2240 | } |
| 2241 | net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_IPV6_REACHABILITY_CHECK, |
| 2242 | base::Bind(&NetLogIPv6AvailableCallback, |
| 2243 | last_ipv6_probe_result_, cached)); |
| 2244 | return last_ipv6_probe_result_; |
| 2245 | } |
| 2246 | |
[email protected] | 35ddc28 | 2010-09-21 23:42:06 | [diff] [blame] | 2247 | void HostResolverImpl::AbortAllInProgressJobs() { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2248 | // In Abort, a Request callback could spawn new Jobs with matching keys, so |
| 2249 | // first collect and remove all running jobs from |jobs_|. |
olli.raula | a21e9eb7 | 2015-12-17 08:18:11 | [diff] [blame] | 2250 | std::vector<scoped_ptr<Job>> jobs_to_abort; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2251 | for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ) { |
| 2252 | Job* job = it->second; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2253 | if (job->is_running()) { |
olli.raula | a21e9eb7 | 2015-12-17 08:18:11 | [diff] [blame] | 2254 | jobs_to_abort.push_back(make_scoped_ptr(job)); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2255 | jobs_.erase(it++); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2256 | } else { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2257 | DCHECK(job->is_queued()); |
| 2258 | ++it; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2259 | } |
[email protected] | ef4c40c | 2010-09-01 14:42:03 | [diff] [blame] | 2260 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2261 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2262 | // Pause the dispatcher so it won't start any new dispatcher jobs while |
| 2263 | // aborting the old ones. This is needed so that it won't start the second |
| 2264 | // DnsTransaction for a job in |jobs_to_abort| if the DnsConfig just became |
| 2265 | // invalid. |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 2266 | PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits(); |
| 2267 | dispatcher_->SetLimits( |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2268 | PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0)); |
[email protected] | 70c04ab | 2013-08-22 16:05:12 | [diff] [blame] | 2269 | |
[email protected] | 57a48d3 | 2012-03-03 00:04:55 | [diff] [blame] | 2270 | // Life check to bail once |this| is deleted. |
[email protected] | 4589a3a | 2012-09-20 20:57:07 | [diff] [blame] | 2271 | base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr(); |
[email protected] | 57a48d3 | 2012-03-03 00:04:55 | [diff] [blame] | 2272 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 2273 | // Then Abort them. |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 2274 | for (size_t i = 0; self.get() && i < jobs_to_abort.size(); ++i) { |
[email protected] | 57a48d3 | 2012-03-03 00:04:55 | [diff] [blame] | 2275 | jobs_to_abort[i]->Abort(); |
olli.raula | a21e9eb7 | 2015-12-17 08:18:11 | [diff] [blame] | 2276 | ignore_result(jobs_to_abort[i].release()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2277 | } |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2278 | |
| 2279 | if (self) |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 2280 | dispatcher_->SetLimits(limits); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | void HostResolverImpl::AbortDnsTasks() { |
| 2284 | // Pause the dispatcher so it won't start any new dispatcher jobs while |
| 2285 | // aborting the old ones. This is needed so that it won't start the second |
| 2286 | // DnsTransaction for a job if the DnsConfig just changed. |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 2287 | PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits(); |
| 2288 | dispatcher_->SetLimits( |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2289 | PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0)); |
| 2290 | |
| 2291 | for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ++it) |
| 2292 | it->second->AbortDnsTask(); |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 2293 | dispatcher_->SetLimits(limits); |
[email protected] | ef4c40c | 2010-09-01 14:42:03 | [diff] [blame] | 2294 | } |
| 2295 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2296 | void HostResolverImpl::TryServingAllJobsFromHosts() { |
| 2297 | if (!HaveDnsConfig()) |
| 2298 | return; |
| 2299 | |
| 2300 | // TODO(szym): Do not do this if nsswitch.conf instructs not to. |
| 2301 | // http://crbug.com/117655 |
| 2302 | |
| 2303 | // Life check to bail once |this| is deleted. |
[email protected] | 4589a3a | 2012-09-20 20:57:07 | [diff] [blame] | 2304 | base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr(); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2305 | |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 2306 | for (JobMap::iterator it = jobs_.begin(); self.get() && it != jobs_.end();) { |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2307 | Job* job = it->second; |
| 2308 | ++it; |
| 2309 | // This could remove |job| from |jobs_|, but iterator will remain valid. |
| 2310 | job->ServeFromHosts(); |
| 2311 | } |
| 2312 | } |
| 2313 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 2314 | void HostResolverImpl::OnIPAddressChanged() { |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 2315 | resolved_known_ipv6_hostname_ = false; |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 2316 | last_ipv6_probe_time_ = base::TimeTicks(); |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 2317 | // Abandon all ProbeJobs. |
| 2318 | probe_weak_ptr_factory_.InvalidateWeakPtrs(); |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 2319 | if (cache_.get()) |
| 2320 | cache_->clear(); |
[email protected] | 7c466e9 | 2013-07-20 01:44:48 | [diff] [blame] | 2321 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 2322 | new LoopbackProbeJob(probe_weak_ptr_factory_.GetWeakPtr()); |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 2323 | #endif |
| 2324 | AbortAllInProgressJobs(); |
| 2325 | // |this| may be deleted inside AbortAllInProgressJobs(). |
| 2326 | } |
| 2327 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2328 | void HostResolverImpl::OnInitialDNSConfigRead() { |
| 2329 | UpdateDNSConfig(false); |
| 2330 | } |
| 2331 | |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 2332 | void HostResolverImpl::OnDNSChanged() { |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2333 | UpdateDNSConfig(true); |
| 2334 | } |
| 2335 | |
| 2336 | void HostResolverImpl::UpdateDNSConfig(bool config_changed) { |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 2337 | DnsConfig dns_config; |
| 2338 | NetworkChangeNotifier::GetDnsConfig(&dns_config); |
[email protected] | ec666ab2 | 2013-04-17 20:05:59 | [diff] [blame] | 2339 | |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 2340 | if (net_log_) { |
| 2341 | net_log_->AddGlobalEntry( |
| 2342 | NetLog::TYPE_DNS_CONFIG_CHANGED, |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 2343 | base::Bind(&NetLogDnsConfigCallback, &dns_config)); |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 2344 | } |
| 2345 | |
[email protected] | 01b3b9d | 2012-08-13 16:18:14 | [diff] [blame] | 2346 | // TODO(szym): Remove once http://crbug.com/137914 is resolved. |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 2347 | received_dns_config_ = dns_config.IsValid(); |
[email protected] | c9fa8f31 | 2013-09-17 12:24:52 | [diff] [blame] | 2348 | // Conservatively assume local IPv6 is needed when DnsConfig is not valid. |
| 2349 | use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6; |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2350 | |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2351 | num_dns_failures_ = 0; |
| 2352 | |
[email protected] | 01b3b9d | 2012-08-13 16:18:14 | [diff] [blame] | 2353 | // We want a new DnsSession in place, before we Abort running Jobs, so that |
| 2354 | // the newly started jobs use the new config. |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2355 | if (dns_client_.get()) { |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 2356 | dns_client_->SetConfig(dns_config); |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2357 | if (dns_client_->GetConfig()) { |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2358 | UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2359 | // If we just switched DnsClients, restart jobs using new resolver. |
| 2360 | // TODO(pauljensen): Is this necessary? |
| 2361 | config_changed = true; |
| 2362 | } |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2363 | } |
[email protected] | 01b3b9d | 2012-08-13 16:18:14 | [diff] [blame] | 2364 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2365 | if (config_changed) { |
| 2366 | // If the DNS server has changed, existing cached info could be wrong so we |
| 2367 | // have to drop our internal cache :( Note that OS level DNS caches, such |
| 2368 | // as NSCD's cache should be dropped automatically by the OS when |
| 2369 | // resolv.conf changes so we don't need to do anything to clear that cache. |
| 2370 | if (cache_.get()) |
| 2371 | cache_->clear(); |
[email protected] | 01b3b9d | 2012-08-13 16:18:14 | [diff] [blame] | 2372 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2373 | // Life check to bail once |this| is deleted. |
| 2374 | base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr(); |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2375 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2376 | // Existing jobs will have been sent to the original server so they need to |
| 2377 | // be aborted. |
| 2378 | AbortAllInProgressJobs(); |
[email protected] | 01b3b9d | 2012-08-13 16:18:14 | [diff] [blame] | 2379 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2380 | // |this| may be deleted inside AbortAllInProgressJobs(). |
| 2381 | if (self.get()) |
| 2382 | TryServingAllJobsFromHosts(); |
| 2383 | } |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | bool HostResolverImpl::HaveDnsConfig() const { |
[email protected] | 32b1dbcf | 2013-01-26 03:48:25 | [diff] [blame] | 2387 | // Use DnsClient only if it's fully configured and there is no override by |
| 2388 | // ScopedDefaultHostResolverProc. |
| 2389 | // The alternative is to use NetworkChangeNotifier to override DnsConfig, |
| 2390 | // but that would introduce construction order requirements for NCN and SDHRP. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2391 | return (dns_client_.get() != NULL) && (dns_client_->GetConfig() != NULL) && |
| 2392 | !(proc_params_.resolver_proc.get() == NULL && |
[email protected] | 32b1dbcf | 2013-01-26 03:48:25 | [diff] [blame] | 2393 | HostResolverProc::GetDefault() != NULL); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2394 | } |
| 2395 | |
[email protected] | 1ffdda8 | 2012-12-12 23:04:22 | [diff] [blame] | 2396 | void HostResolverImpl::OnDnsTaskResolve(int net_error) { |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2397 | DCHECK(dns_client_); |
[email protected] | 1ffdda8 | 2012-12-12 23:04:22 | [diff] [blame] | 2398 | if (net_error == OK) { |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2399 | num_dns_failures_ = 0; |
| 2400 | return; |
| 2401 | } |
| 2402 | ++num_dns_failures_; |
| 2403 | if (num_dns_failures_ < kMaximumDnsFailures) |
| 2404 | return; |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2405 | |
| 2406 | // Disable DnsClient until the next DNS change. Must be done before aborting |
| 2407 | // DnsTasks, since doing so may start new jobs. |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2408 | dns_client_->SetConfig(DnsConfig()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2409 | |
| 2410 | // Switch jobs with active DnsTasks over to using ProcTasks. |
| 2411 | AbortDnsTasks(); |
| 2412 | |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2413 | UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", false); |
davidben | ad6fc44 | 2015-05-18 20:59:13 | [diff] [blame] | 2414 | UMA_HISTOGRAM_SPARSE_SLOWLY("AsyncDNS.DnsClientDisabledReason", |
| 2415 | std::abs(net_error)); |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2416 | } |
| 2417 | |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2418 | void HostResolverImpl::SetDnsClient(scoped_ptr<DnsClient> dns_client) { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2419 | // DnsClient and config must be updated before aborting DnsTasks, since doing |
| 2420 | // so may start new jobs. |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2421 | dns_client_ = dns_client.Pass(); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2422 | if (dns_client_ && !dns_client_->GetConfig() && |
| 2423 | num_dns_failures_ < kMaximumDnsFailures) { |
| 2424 | DnsConfig dns_config; |
| 2425 | NetworkChangeNotifier::GetDnsConfig(&dns_config); |
| 2426 | dns_client_->SetConfig(dns_config); |
| 2427 | num_dns_failures_ = 0; |
| 2428 | if (dns_client_->GetConfig()) |
| 2429 | UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2430 | } |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2431 | |
| 2432 | AbortDnsTasks(); |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2433 | } |
| 2434 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2435 | } // namespace net |