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