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