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