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