[email protected] | 5ea28dea | 2010-04-08 15:35:13 | [diff] [blame] | 1 | // Copyright (c) 2010 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> |
| 14 | #include <deque> |
[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] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 18 | #include "base/compiler_specific.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame^] | 19 | #include "base/debug/debugger.h" |
| 20 | #include "base/debug/stack_trace.h" |
[email protected] | f2d8c421 | 2010-02-02 00:56:35 | [diff] [blame] | 21 | #include "base/lock.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 22 | #include "base/message_loop.h" |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 23 | #include "base/metrics/field_trial.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 24 | #include "base/metrics/histogram.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 25 | #include "base/stl_util-inl.h" |
| 26 | #include "base/string_util.h" |
| 27 | #include "base/time.h" |
[email protected] | ccaff65 | 2010-07-31 06:28:20 | [diff] [blame] | 28 | #include "base/utf_string_conversions.h" |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 29 | #include "base/values.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 30 | #include "base/worker_pool.h" |
| 31 | #include "net/base/address_list.h" |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 32 | #include "net/base/address_list_net_log_param.h" |
| 33 | #include "net/base/host_port_pair.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 34 | #include "net/base/host_resolver_proc.h" |
[email protected] | 2bb0444 | 2010-08-18 18:01:15 | [diff] [blame] | 35 | #include "net/base/net_errors.h" |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 36 | #include "net/base/net_log.h" |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 37 | #include "net/base/net_util.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 38 | |
| 39 | #if defined(OS_WIN) |
| 40 | #include "net/base/winsock_init.h" |
| 41 | #endif |
| 42 | |
| 43 | namespace net { |
| 44 | |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 45 | namespace { |
| 46 | |
[email protected] | 24f4bab | 2010-10-15 01:27:11 | [diff] [blame] | 47 | // We use a separate histogram name for each platform to facilitate the |
| 48 | // display of error codes by their symbolic name (since each platform has |
| 49 | // different mappings). |
| 50 | const char kOSErrorsForGetAddrinfoHistogramName[] = |
| 51 | #if defined(OS_WIN) |
| 52 | "Net.OSErrorsForGetAddrinfo_Win"; |
| 53 | #elif defined(OS_MACOSX) |
| 54 | "Net.OSErrorsForGetAddrinfo_Mac"; |
| 55 | #elif defined(OS_LINUX) |
| 56 | "Net.OSErrorsForGetAddrinfo_Linux"; |
| 57 | #else |
| 58 | "Net.OSErrorsForGetAddrinfo"; |
| 59 | #endif |
| 60 | |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 61 | HostCache* CreateDefaultCache() { |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 62 | static const size_t kMaxHostCacheEntries = 100; |
[email protected] | 112bd46 | 2009-12-10 07:23:40 | [diff] [blame] | 63 | |
| 64 | HostCache* cache = new HostCache( |
| 65 | kMaxHostCacheEntries, |
| 66 | base::TimeDelta::FromMinutes(1), |
[email protected] | 72bceac | 2010-06-17 21:45:08 | [diff] [blame] | 67 | base::TimeDelta::FromSeconds(0)); // Disable caching of failed DNS. |
[email protected] | 112bd46 | 2009-12-10 07:23:40 | [diff] [blame] | 68 | |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 69 | return cache; |
| 70 | } |
| 71 | |
| 72 | } // anonymous namespace |
| 73 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 74 | HostResolver* CreateSystemHostResolver(size_t max_concurrent_resolves, |
| 75 | NetLog* net_log) { |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 76 | // Maximum of 50 concurrent threads. |
| 77 | // TODO(eroman): Adjust this, do some A/B experiments. |
[email protected] | 962b9821 | 2010-07-17 03:37:51 | [diff] [blame] | 78 | static const size_t kDefaultMaxJobs = 50u; |
| 79 | |
| 80 | if (max_concurrent_resolves == HostResolver::kDefaultParallelism) |
| 81 | max_concurrent_resolves = kDefaultMaxJobs; |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 82 | |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 83 | HostResolverImpl* resolver = |
[email protected] | 962b9821 | 2010-07-17 03:37:51 | [diff] [blame] | 84 | new HostResolverImpl(NULL, CreateDefaultCache(), |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 85 | max_concurrent_resolves, net_log); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 86 | |
| 87 | return resolver; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static int ResolveAddrInfo(HostResolverProc* resolver_proc, |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 91 | const std::string& host, |
| 92 | AddressFamily address_family, |
[email protected] | 5ea28dea | 2010-04-08 15:35:13 | [diff] [blame] | 93 | HostResolverFlags host_resolver_flags, |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 94 | AddressList* out, |
| 95 | int* os_error) { |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 96 | if (resolver_proc) { |
| 97 | // Use the custom procedure. |
[email protected] | 5ea28dea | 2010-04-08 15:35:13 | [diff] [blame] | 98 | return resolver_proc->Resolve(host, address_family, |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 99 | host_resolver_flags, out, os_error); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 100 | } else { |
| 101 | // Use the system procedure (getaddrinfo). |
[email protected] | 5ea28dea | 2010-04-08 15:35:13 | [diff] [blame] | 102 | return SystemHostResolverProc(host, address_family, |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 103 | host_resolver_flags, out, os_error); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 107 | // Extra parameters to attach to the NetLog when the resolve failed. |
| 108 | class HostResolveFailedParams : public NetLog::EventParameters { |
| 109 | public: |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 110 | HostResolveFailedParams(int net_error, int os_error) |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 111 | : net_error_(net_error), |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 112 | os_error_(os_error) { |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | virtual Value* ToValue() const { |
| 116 | DictionaryValue* dict = new DictionaryValue(); |
[email protected] | ccaff65 | 2010-07-31 06:28:20 | [diff] [blame] | 117 | dict->SetInteger("net_error", net_error_); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 118 | |
| 119 | if (os_error_) { |
[email protected] | ccaff65 | 2010-07-31 06:28:20 | [diff] [blame] | 120 | dict->SetInteger("os_error", os_error_); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 121 | #if defined(OS_POSIX) |
[email protected] | ccaff65 | 2010-07-31 06:28:20 | [diff] [blame] | 122 | dict->SetString("os_error_string", gai_strerror(os_error_)); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 123 | #elif defined(OS_WIN) |
| 124 | // Map the error code to a human-readable string. |
| 125 | LPWSTR error_string = NULL; |
| 126 | int size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 127 | FORMAT_MESSAGE_FROM_SYSTEM, |
| 128 | 0, // Use the internal message table. |
| 129 | os_error_, |
| 130 | 0, // Use default language. |
| 131 | (LPWSTR)&error_string, |
| 132 | 0, // Buffer size. |
| 133 | 0); // Arguments (unused). |
[email protected] | ccaff65 | 2010-07-31 06:28:20 | [diff] [blame] | 134 | dict->SetString("os_error_string", WideToUTF8(error_string)); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 135 | LocalFree(error_string); |
| 136 | #endif |
| 137 | } |
| 138 | |
| 139 | return dict; |
| 140 | } |
| 141 | |
| 142 | private: |
| 143 | const int net_error_; |
| 144 | const int os_error_; |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | // Parameters representing the information in a RequestInfo object, along with |
| 148 | // the associated NetLog::Source. |
| 149 | class RequestInfoParameters : public NetLog::EventParameters { |
| 150 | public: |
| 151 | RequestInfoParameters(const HostResolver::RequestInfo& info, |
| 152 | const NetLog::Source& source) |
| 153 | : info_(info), source_(source) {} |
| 154 | |
| 155 | virtual Value* ToValue() const { |
| 156 | DictionaryValue* dict = new DictionaryValue(); |
[email protected] | 930cc74 | 2010-09-15 22:54:10 | [diff] [blame] | 157 | dict->SetString("host", info_.host_port_pair().ToString()); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 158 | dict->SetInteger("address_family", |
| 159 | static_cast<int>(info_.address_family())); |
| 160 | dict->SetBoolean("allow_cached_response", info_.allow_cached_response()); |
| 161 | dict->SetBoolean("is_speculative", info_.is_speculative()); |
| 162 | dict->SetInteger("priority", info_.priority()); |
| 163 | |
| 164 | if (source_.is_valid()) |
| 165 | dict->Set("source_dependency", source_.ToValue()); |
| 166 | |
| 167 | return dict; |
| 168 | } |
| 169 | |
| 170 | private: |
| 171 | const HostResolver::RequestInfo info_; |
| 172 | const NetLog::Source source_; |
| 173 | }; |
| 174 | |
| 175 | // Parameters associated with the creation of a HostResolveImpl::Job. |
| 176 | class JobCreationParameters : public NetLog::EventParameters { |
| 177 | public: |
| 178 | JobCreationParameters(const std::string& host, const NetLog::Source& source) |
| 179 | : host_(host), source_(source) {} |
| 180 | |
| 181 | virtual Value* ToValue() const { |
| 182 | DictionaryValue* dict = new DictionaryValue(); |
| 183 | dict->SetString("host", host_); |
| 184 | dict->Set("source_dependency", source_.ToValue()); |
| 185 | return dict; |
| 186 | } |
| 187 | |
| 188 | private: |
| 189 | const std::string host_; |
| 190 | const NetLog::Source source_; |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | // Gets a list of the likely error codes that getaddrinfo() can return |
| 194 | // (non-exhaustive). These are the error codes that we will track via |
| 195 | // a histogram. |
| 196 | std::vector<int> GetAllGetAddrinfoOSErrors() { |
| 197 | int os_errors[] = { |
| 198 | #if defined(OS_POSIX) |
| 199 | EAI_ADDRFAMILY, |
| 200 | EAI_AGAIN, |
| 201 | EAI_BADFLAGS, |
| 202 | EAI_FAIL, |
| 203 | EAI_FAMILY, |
| 204 | EAI_MEMORY, |
| 205 | EAI_NODATA, |
| 206 | EAI_NONAME, |
| 207 | EAI_SERVICE, |
| 208 | EAI_SOCKTYPE, |
| 209 | EAI_SYSTEM, |
| 210 | #elif defined(OS_WIN) |
| 211 | // See: http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx |
| 212 | WSA_NOT_ENOUGH_MEMORY, |
| 213 | WSAEAFNOSUPPORT, |
| 214 | WSAEINVAL, |
| 215 | WSAESOCKTNOSUPPORT, |
| 216 | WSAHOST_NOT_FOUND, |
| 217 | WSANO_DATA, |
| 218 | WSANO_RECOVERY, |
| 219 | WSANOTINITIALISED, |
| 220 | WSATRY_AGAIN, |
| 221 | WSATYPE_NOT_FOUND, |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 222 | // The following are not in doc, but might be to appearing in results :-(. |
| 223 | WSA_INVALID_HANDLE, |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 224 | #endif |
| 225 | }; |
| 226 | |
| 227 | // Histogram enumerations require positive numbers. |
| 228 | std::vector<int> errors; |
| 229 | for (size_t i = 0; i < arraysize(os_errors); ++i) { |
| 230 | errors.push_back(std::abs(os_errors[i])); |
| 231 | // Also add N+1 for each error, so the bucket that contains our expected |
| 232 | // error is of size 1. That way if we get unexpected error codes, they |
| 233 | // won't fall into the same buckets as the expected ones. |
| 234 | errors.push_back(std::abs(os_errors[i]) + 1); |
| 235 | } |
| 236 | return errors; |
| 237 | } |
| 238 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 239 | //----------------------------------------------------------------------------- |
| 240 | |
| 241 | class HostResolverImpl::Request { |
| 242 | public: |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 243 | Request(const BoundNetLog& source_net_log, |
| 244 | const BoundNetLog& request_net_log, |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 245 | int id, |
| 246 | const RequestInfo& info, |
| 247 | CompletionCallback* callback, |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 248 | AddressList* addresses) |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 249 | : source_net_log_(source_net_log), |
| 250 | request_net_log_(request_net_log), |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 251 | id_(id), |
| 252 | info_(info), |
| 253 | job_(NULL), |
| 254 | callback_(callback), |
| 255 | addresses_(addresses) { |
| 256 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 257 | |
| 258 | // Mark the request as cancelled. |
| 259 | void MarkAsCancelled() { |
| 260 | job_ = NULL; |
| 261 | callback_ = NULL; |
| 262 | addresses_ = NULL; |
| 263 | } |
| 264 | |
| 265 | bool was_cancelled() const { |
| 266 | return callback_ == NULL; |
| 267 | } |
| 268 | |
| 269 | void set_job(Job* job) { |
| 270 | DCHECK(job != NULL); |
| 271 | // Identify which job the request is waiting on. |
| 272 | job_ = job; |
| 273 | } |
| 274 | |
| 275 | void OnComplete(int error, const AddressList& addrlist) { |
| 276 | if (error == OK) |
| 277 | addresses_->SetFrom(addrlist, port()); |
[email protected] | ef4c40c | 2010-09-01 14:42:03 | [diff] [blame] | 278 | CompletionCallback* callback = callback_; |
| 279 | MarkAsCancelled(); |
| 280 | callback->Run(error); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | int port() const { |
| 284 | return info_.port(); |
| 285 | } |
| 286 | |
| 287 | Job* job() const { |
| 288 | return job_; |
| 289 | } |
| 290 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 291 | const BoundNetLog& source_net_log() { |
| 292 | return source_net_log_; |
| 293 | } |
| 294 | |
| 295 | const BoundNetLog& request_net_log() { |
| 296 | return request_net_log_; |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 297 | } |
| 298 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 299 | int id() const { |
| 300 | return id_; |
| 301 | } |
| 302 | |
| 303 | const RequestInfo& info() const { |
| 304 | return info_; |
| 305 | } |
| 306 | |
| 307 | private: |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 308 | BoundNetLog source_net_log_; |
| 309 | BoundNetLog request_net_log_; |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 310 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 311 | // Unique ID for this request. Used by observers to identify requests. |
| 312 | int id_; |
| 313 | |
| 314 | // The request info that started the request. |
| 315 | RequestInfo info_; |
| 316 | |
| 317 | // The resolve job (running in worker pool) that this request is dependent on. |
| 318 | Job* job_; |
| 319 | |
| 320 | // The user's callback to invoke when the request completes. |
| 321 | CompletionCallback* callback_; |
| 322 | |
| 323 | // The address list to save result into. |
| 324 | AddressList* addresses_; |
| 325 | |
| 326 | DISALLOW_COPY_AND_ASSIGN(Request); |
| 327 | }; |
| 328 | |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 329 | //------------------------------------------------------------------------------ |
| 330 | |
| 331 | // Provide a common macro to simplify code and readability. We must use a |
| 332 | // macros as the underlying HISTOGRAM macro creates static varibles. |
| 333 | #define DNS_HISTOGRAM(name, time) UMA_HISTOGRAM_CUSTOM_TIMES(name, time, \ |
| 334 | base::TimeDelta::FromMicroseconds(1), base::TimeDelta::FromHours(1), 100) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 335 | |
| 336 | // This class represents a request to the worker pool for a "getaddrinfo()" |
| 337 | // call. |
| 338 | class HostResolverImpl::Job |
| 339 | : public base::RefCountedThreadSafe<HostResolverImpl::Job> { |
| 340 | public: |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 341 | Job(int id, |
| 342 | HostResolverImpl* resolver, |
| 343 | const Key& key, |
| 344 | const BoundNetLog& source_net_log, |
| 345 | NetLog* net_log) |
| 346 | : id_(id), |
| 347 | key_(key), |
| 348 | resolver_(resolver), |
| 349 | origin_loop_(MessageLoop::current()), |
| 350 | resolver_proc_(resolver->effective_resolver_proc()), |
| 351 | error_(OK), |
| 352 | os_error_(0), |
| 353 | had_non_speculative_request_(false), |
| 354 | net_log_(BoundNetLog::Make(net_log, |
| 355 | NetLog::SOURCE_HOST_RESOLVER_IMPL_JOB)) { |
| 356 | net_log_.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, |
| 357 | new JobCreationParameters(key.hostname, |
| 358 | source_net_log.source())); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 359 | } |
| 360 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 361 | // Attaches a request to this job. The job takes ownership of |req| and will |
| 362 | // take care to delete it. |
| 363 | void AddRequest(Request* req) { |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 364 | req->request_net_log().BeginEvent( |
| 365 | NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH, |
| 366 | new NetLogSourceParameter("source_dependency", net_log_.source())); |
| 367 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 368 | req->set_job(this); |
| 369 | requests_.push_back(req); |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 370 | |
| 371 | if (!req->info().is_speculative()) |
| 372 | had_non_speculative_request_ = true; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | // Called from origin loop. |
| 376 | void Start() { |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 377 | start_time_ = base::TimeTicks::Now(); |
| 378 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 379 | // Dispatch the job to a worker thread. |
| 380 | if (!WorkerPool::PostTask(FROM_HERE, |
| 381 | NewRunnableMethod(this, &Job::DoLookup), true)) { |
| 382 | NOTREACHED(); |
| 383 | |
| 384 | // Since we could be running within Resolve() right now, we can't just |
| 385 | // call OnLookupComplete(). Instead we must wait until Resolve() has |
| 386 | // returned (IO_PENDING). |
| 387 | error_ = ERR_UNEXPECTED; |
| 388 | MessageLoop::current()->PostTask( |
| 389 | FROM_HERE, NewRunnableMethod(this, &Job::OnLookupComplete)); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | // Cancels the current job. Callable from origin thread. |
| 394 | void Cancel() { |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 395 | net_log_.AddEvent(NetLog::TYPE_CANCELLED, NULL); |
| 396 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 397 | HostResolver* resolver = resolver_; |
| 398 | resolver_ = NULL; |
| 399 | |
| 400 | // Mark the job as cancelled, so when worker thread completes it will |
| 401 | // not try to post completion to origin loop. |
| 402 | { |
| 403 | AutoLock locked(origin_loop_lock_); |
| 404 | origin_loop_ = NULL; |
| 405 | } |
| 406 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 407 | // End here to prevent issues when a Job outlives the HostResolver that |
| 408 | // spawned it. |
| 409 | net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, NULL); |
| 410 | |
[email protected] | 1877a221 | 2009-09-18 21:09:26 | [diff] [blame] | 411 | // We will call HostResolverImpl::CancelRequest(Request*) on each one |
[email protected] | 27f99c8 | 2009-10-29 22:21:00 | [diff] [blame] | 412 | // in order to notify any observers. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 413 | for (RequestsList::const_iterator it = requests_.begin(); |
| 414 | it != requests_.end(); ++it) { |
| 415 | HostResolverImpl::Request* req = *it; |
| 416 | if (!req->was_cancelled()) |
| 417 | resolver->CancelRequest(req); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | // Called from origin thread. |
| 422 | bool was_cancelled() const { |
| 423 | return resolver_ == NULL; |
| 424 | } |
| 425 | |
| 426 | // Called from origin thread. |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 427 | const Key& key() const { |
| 428 | return key_; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 429 | } |
| 430 | |
[email protected] | a2fbebe | 2010-02-05 01:40:12 | [diff] [blame] | 431 | int id() const { |
| 432 | return id_; |
| 433 | } |
| 434 | |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 435 | base::TimeTicks start_time() const { |
| 436 | return start_time_; |
| 437 | } |
| 438 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 439 | // Called from origin thread. |
| 440 | const RequestsList& requests() const { |
| 441 | return requests_; |
| 442 | } |
| 443 | |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 444 | // Returns the first request attached to the job. |
| 445 | const Request* initial_request() const { |
| 446 | DCHECK_EQ(origin_loop_, MessageLoop::current()); |
| 447 | DCHECK(!requests_.empty()); |
| 448 | return requests_[0]; |
| 449 | } |
| 450 | |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 451 | // Returns true if |req_info| can be fulfilled by this job. |
| 452 | bool CanServiceRequest(const RequestInfo& req_info) const { |
| 453 | return key_ == resolver_->GetEffectiveKeyForRequest(req_info); |
| 454 | } |
| 455 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 456 | private: |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 457 | friend class base::RefCountedThreadSafe<HostResolverImpl::Job>; |
| 458 | |
| 459 | ~Job() { |
| 460 | // Free the requests attached to this job. |
| 461 | STLDeleteElements(&requests_); |
| 462 | } |
| 463 | |
[email protected] | 6c710ee | 2010-05-07 07:51:16 | [diff] [blame] | 464 | // WARNING: This code runs inside a worker pool. The shutdown code cannot |
| 465 | // wait for it to finish, so we must be very careful here about using other |
| 466 | // objects (like MessageLoops, Singletons, etc). During shutdown these objects |
| 467 | // may no longer exist. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 468 | void DoLookup() { |
| 469 | // Running on the worker thread |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 470 | error_ = ResolveAddrInfo(resolver_proc_, |
| 471 | key_.hostname, |
| 472 | key_.address_family, |
[email protected] | 5ea28dea | 2010-04-08 15:35:13 | [diff] [blame] | 473 | key_.host_resolver_flags, |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 474 | &results_, |
| 475 | &os_error_); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 476 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 477 | // The origin loop could go away while we are trying to post to it, so we |
| 478 | // need to call its PostTask method inside a lock. See ~HostResolver. |
| 479 | { |
| 480 | AutoLock locked(origin_loop_lock_); |
| 481 | if (origin_loop_) { |
[email protected] | 6c710ee | 2010-05-07 07:51:16 | [diff] [blame] | 482 | origin_loop_->PostTask(FROM_HERE, |
| 483 | NewRunnableMethod(this, &Job::OnLookupComplete)); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 484 | } |
| 485 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | // Callback for when DoLookup() completes (runs on origin thread). |
| 489 | void OnLookupComplete() { |
| 490 | // Should be running on origin loop. |
| 491 | // TODO(eroman): this is being hit by URLRequestTest.CancelTest*, |
| 492 | // because MessageLoop::current() == NULL. |
| 493 | //DCHECK_EQ(origin_loop_, MessageLoop::current()); |
| 494 | DCHECK(error_ || results_.head()); |
| 495 | |
[email protected] | 2d3b776 | 2010-10-09 00:35:47 | [diff] [blame] | 496 | // Ideally the following code would be part of host_resolver_proc.cc, |
| 497 | // however it isn't safe to call NetworkChangeNotifier from worker |
| 498 | // threads. So we do it here on the IO thread instead. |
[email protected] | 8ed0cab6 | 2010-10-15 04:12:45 | [diff] [blame] | 499 | if (error_ != OK && NetworkChangeNotifier::IsOffline()) |
[email protected] | 2d3b776 | 2010-10-09 00:35:47 | [diff] [blame] | 500 | error_ = ERR_INTERNET_DISCONNECTED; |
| 501 | |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 502 | RecordPerformanceHistograms(); |
[email protected] | f2d8c421 | 2010-02-02 00:56:35 | [diff] [blame] | 503 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 504 | if (was_cancelled()) |
| 505 | return; |
| 506 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 507 | scoped_refptr<NetLog::EventParameters> params; |
| 508 | if (error_ != OK) { |
| 509 | params = new HostResolveFailedParams(error_, os_error_); |
| 510 | } else { |
| 511 | params = new AddressListNetLogParam(results_); |
| 512 | } |
| 513 | |
| 514 | // End here to prevent issues when a Job outlives the HostResolver that |
| 515 | // spawned it. |
| 516 | net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, params); |
| 517 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 518 | DCHECK(!requests_.empty()); |
| 519 | |
| 520 | // Use the port number of the first request. |
| 521 | if (error_ == OK) |
| 522 | results_.SetPort(requests_[0]->port()); |
| 523 | |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 524 | resolver_->OnJobComplete(this, error_, os_error_, results_); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 525 | } |
| 526 | |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 527 | void RecordPerformanceHistograms() const { |
| 528 | enum Category { // Used in HISTOGRAM_ENUMERATION. |
| 529 | RESOLVE_SUCCESS, |
| 530 | RESOLVE_FAIL, |
| 531 | RESOLVE_SPECULATIVE_SUCCESS, |
| 532 | RESOLVE_SPECULATIVE_FAIL, |
| 533 | RESOLVE_MAX, // Bounding value. |
| 534 | }; |
| 535 | int category = RESOLVE_MAX; // Illegal value for later DCHECK only. |
| 536 | |
| 537 | base::TimeDelta duration = base::TimeTicks::Now() - start_time_; |
| 538 | if (error_ == OK) { |
| 539 | if (had_non_speculative_request_) { |
| 540 | category = RESOLVE_SUCCESS; |
| 541 | DNS_HISTOGRAM("DNS.ResolveSuccess", duration); |
| 542 | } else { |
| 543 | category = RESOLVE_SPECULATIVE_SUCCESS; |
| 544 | DNS_HISTOGRAM("DNS.ResolveSpeculativeSuccess", duration); |
| 545 | } |
| 546 | } else { |
| 547 | if (had_non_speculative_request_) { |
| 548 | category = RESOLVE_FAIL; |
| 549 | DNS_HISTOGRAM("DNS.ResolveFail", duration); |
| 550 | } else { |
| 551 | category = RESOLVE_SPECULATIVE_FAIL; |
| 552 | DNS_HISTOGRAM("DNS.ResolveSpeculativeFail", duration); |
| 553 | } |
[email protected] | c833e32 | 2010-10-16 23:51:36 | [diff] [blame] | 554 | UMA_HISTOGRAM_CUSTOM_ENUMERATION(kOSErrorsForGetAddrinfoHistogramName, |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 555 | std::abs(os_error_), |
| 556 | GetAllGetAddrinfoOSErrors()); |
| 557 | } |
[email protected] | 051b6ab | 2010-10-18 16:50:46 | [diff] [blame] | 558 | DCHECK_LT(category, static_cast<int>(RESOLVE_MAX)); // Be sure it was set. |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 559 | |
| 560 | UMA_HISTOGRAM_ENUMERATION("DNS.ResolveCategory", category, RESOLVE_MAX); |
| 561 | |
[email protected] | ecd95ae | 2010-10-20 23:58:17 | [diff] [blame] | 562 | static bool show_speculative_experiment_histograms = |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 563 | base::FieldTrialList::Find("DnsImpact") && |
| 564 | !base::FieldTrialList::Find("DnsImpact")->group_name().empty(); |
[email protected] | ecd95ae | 2010-10-20 23:58:17 | [diff] [blame] | 565 | if (show_speculative_experiment_histograms) { |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 566 | UMA_HISTOGRAM_ENUMERATION( |
| 567 | base::FieldTrial::MakeName("DNS.ResolveCategory", "DnsImpact"), |
| 568 | category, RESOLVE_MAX); |
| 569 | if (RESOLVE_SUCCESS == category) { |
| 570 | DNS_HISTOGRAM(base::FieldTrial::MakeName("DNS.ResolveSuccess", |
| 571 | "DnsImpact"), duration); |
| 572 | } |
| 573 | } |
[email protected] | ecd95ae | 2010-10-20 23:58:17 | [diff] [blame] | 574 | static bool show_parallelism_experiment_histograms = |
| 575 | base::FieldTrialList::Find("DnsParallelism") && |
| 576 | !base::FieldTrialList::Find("DnsParallelism")->group_name().empty(); |
| 577 | if (show_parallelism_experiment_histograms) { |
| 578 | UMA_HISTOGRAM_ENUMERATION( |
| 579 | base::FieldTrial::MakeName("DNS.ResolveCategory", "DnsParallelism"), |
| 580 | category, RESOLVE_MAX); |
| 581 | if (RESOLVE_SUCCESS == category) { |
| 582 | DNS_HISTOGRAM(base::FieldTrial::MakeName("DNS.ResolveSuccess", |
| 583 | "DnsParallelism"), duration); |
| 584 | } |
| 585 | } |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | |
| 589 | |
[email protected] | f2d8c421 | 2010-02-02 00:56:35 | [diff] [blame] | 590 | // Immutable. Can be read from either thread, |
| 591 | const int id_; |
| 592 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 593 | // Set on the origin thread, read on the worker thread. |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 594 | Key key_; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 595 | |
| 596 | // Only used on the origin thread (where Resolve was called). |
| 597 | HostResolverImpl* resolver_; |
| 598 | RequestsList requests_; // The requests waiting on this job. |
| 599 | |
| 600 | // Used to post ourselves onto the origin thread. |
| 601 | Lock origin_loop_lock_; |
| 602 | MessageLoop* origin_loop_; |
| 603 | |
| 604 | // Hold an owning reference to the HostResolverProc that we are going to use. |
| 605 | // This may not be the current resolver procedure by the time we call |
| 606 | // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning |
| 607 | // reference ensures that it remains valid until we are done. |
| 608 | scoped_refptr<HostResolverProc> resolver_proc_; |
| 609 | |
| 610 | // Assigned on the worker thread, read on the origin thread. |
| 611 | int error_; |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 612 | int os_error_; |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 613 | |
| 614 | // True if a non-speculative request was ever attached to this job |
| 615 | // (regardless of whether or not it was later cancelled. |
| 616 | // This boolean is used for histogramming the duration of jobs used to |
| 617 | // service non-speculative requests. |
| 618 | bool had_non_speculative_request_; |
| 619 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 620 | AddressList results_; |
| 621 | |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 622 | // The time when the job was started. |
| 623 | base::TimeTicks start_time_; |
| 624 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 625 | BoundNetLog net_log_; |
| 626 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 627 | DISALLOW_COPY_AND_ASSIGN(Job); |
| 628 | }; |
| 629 | |
| 630 | //----------------------------------------------------------------------------- |
| 631 | |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 632 | // This class represents a request to the worker pool for a "probe for IPv6 |
| 633 | // support" call. |
| 634 | class HostResolverImpl::IPv6ProbeJob |
| 635 | : public base::RefCountedThreadSafe<HostResolverImpl::IPv6ProbeJob> { |
| 636 | public: |
| 637 | explicit IPv6ProbeJob(HostResolverImpl* resolver) |
| 638 | : resolver_(resolver), |
| 639 | origin_loop_(MessageLoop::current()) { |
[email protected] | a9af711 | 2010-05-08 00:56:01 | [diff] [blame] | 640 | DCHECK(!was_cancelled()); |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | void Start() { |
[email protected] | a9af711 | 2010-05-08 00:56:01 | [diff] [blame] | 644 | if (was_cancelled()) |
| 645 | return; |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 646 | DCHECK(IsOnOriginThread()); |
[email protected] | f092e64b | 2010-03-17 00:39:18 | [diff] [blame] | 647 | const bool kIsSlow = true; |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 648 | WorkerPool::PostTask( |
[email protected] | f092e64b | 2010-03-17 00:39:18 | [diff] [blame] | 649 | FROM_HERE, NewRunnableMethod(this, &IPv6ProbeJob::DoProbe), kIsSlow); |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | // Cancels the current job. |
| 653 | void Cancel() { |
[email protected] | a9af711 | 2010-05-08 00:56:01 | [diff] [blame] | 654 | if (was_cancelled()) |
| 655 | return; |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 656 | DCHECK(IsOnOriginThread()); |
| 657 | resolver_ = NULL; // Read/write ONLY on origin thread. |
| 658 | { |
| 659 | AutoLock locked(origin_loop_lock_); |
| 660 | // Origin loop may be destroyed before we can use it! |
[email protected] | a9af711 | 2010-05-08 00:56:01 | [diff] [blame] | 661 | origin_loop_ = NULL; // Write ONLY on origin thread. |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 665 | private: |
| 666 | friend class base::RefCountedThreadSafe<HostResolverImpl::IPv6ProbeJob>; |
| 667 | |
| 668 | ~IPv6ProbeJob() { |
| 669 | } |
| 670 | |
[email protected] | a9af711 | 2010-05-08 00:56:01 | [diff] [blame] | 671 | // Should be run on |orgin_thread_|, but that may not be well defined now. |
| 672 | bool was_cancelled() const { |
| 673 | if (!resolver_ || !origin_loop_) { |
| 674 | DCHECK(!resolver_); |
| 675 | DCHECK(!origin_loop_); |
| 676 | return true; |
| 677 | } |
| 678 | return false; |
| 679 | } |
| 680 | |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 681 | // Run on worker thread. |
| 682 | void DoProbe() { |
| 683 | // Do actual testing on this thread, as it takes 40-100ms. |
| 684 | AddressFamily family = IPv6Supported() ? ADDRESS_FAMILY_UNSPECIFIED |
| 685 | : ADDRESS_FAMILY_IPV4; |
| 686 | |
| 687 | Task* reply = NewRunnableMethod(this, &IPv6ProbeJob::OnProbeComplete, |
| 688 | family); |
| 689 | |
| 690 | // The origin loop could go away while we are trying to post to it, so we |
| 691 | // need to call its PostTask method inside a lock. See ~HostResolver. |
| 692 | { |
| 693 | AutoLock locked(origin_loop_lock_); |
| 694 | if (origin_loop_) { |
| 695 | origin_loop_->PostTask(FROM_HERE, reply); |
| 696 | return; |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | // We didn't post, so delete the reply. |
| 701 | delete reply; |
| 702 | } |
| 703 | |
| 704 | // Callback for when DoProbe() completes (runs on origin thread). |
| 705 | void OnProbeComplete(AddressFamily address_family) { |
[email protected] | a9af711 | 2010-05-08 00:56:01 | [diff] [blame] | 706 | if (was_cancelled()) |
| 707 | return; |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 708 | DCHECK(IsOnOriginThread()); |
[email protected] | a9af711 | 2010-05-08 00:56:01 | [diff] [blame] | 709 | resolver_->IPv6ProbeSetDefaultAddressFamily(address_family); |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | bool IsOnOriginThread() const { |
| 713 | return !MessageLoop::current() || origin_loop_ == MessageLoop::current(); |
| 714 | } |
| 715 | |
| 716 | // Used/set only on origin thread. |
| 717 | HostResolverImpl* resolver_; |
| 718 | |
| 719 | // Used to post ourselves onto the origin thread. |
| 720 | Lock origin_loop_lock_; |
| 721 | MessageLoop* origin_loop_; |
| 722 | |
| 723 | DISALLOW_COPY_AND_ASSIGN(IPv6ProbeJob); |
| 724 | }; |
| 725 | |
| 726 | //----------------------------------------------------------------------------- |
| 727 | |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 728 | // We rely on the priority enum values being sequential having starting at 0, |
| 729 | // and increasing for lower priorities. |
| 730 | COMPILE_ASSERT(HIGHEST == 0u && |
| 731 | LOWEST > HIGHEST && |
[email protected] | c9c6f5c | 2010-07-31 01:30:03 | [diff] [blame] | 732 | IDLE > LOWEST && |
| 733 | NUM_PRIORITIES > IDLE, |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 734 | priority_indexes_incompatible); |
| 735 | |
| 736 | // JobPool contains all the information relating to queued requests, including |
| 737 | // the limits on how many jobs are allowed to be used for this category of |
| 738 | // requests. |
| 739 | class HostResolverImpl::JobPool { |
| 740 | public: |
| 741 | JobPool(size_t max_outstanding_jobs, size_t max_pending_requests) |
| 742 | : num_outstanding_jobs_(0u) { |
| 743 | SetConstraints(max_outstanding_jobs, max_pending_requests); |
| 744 | } |
| 745 | |
| 746 | ~JobPool() { |
| 747 | // Free the pending requests. |
| 748 | for (size_t i = 0; i < arraysize(pending_requests_); ++i) |
| 749 | STLDeleteElements(&pending_requests_[i]); |
| 750 | } |
| 751 | |
| 752 | // Sets the constraints for this pool. See SetPoolConstraints() for the |
| 753 | // specific meaning of these parameters. |
| 754 | void SetConstraints(size_t max_outstanding_jobs, |
| 755 | size_t max_pending_requests) { |
[email protected] | b1f031dd | 2010-03-02 23:19:33 | [diff] [blame] | 756 | CHECK_NE(max_outstanding_jobs, 0u); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 757 | max_outstanding_jobs_ = max_outstanding_jobs; |
| 758 | max_pending_requests_ = max_pending_requests; |
| 759 | } |
| 760 | |
| 761 | // Returns the number of pending requests enqueued to this pool. |
| 762 | // A pending request is one waiting to be attached to a job. |
| 763 | size_t GetNumPendingRequests() const { |
| 764 | size_t total = 0u; |
| 765 | for (size_t i = 0u; i < arraysize(pending_requests_); ++i) |
| 766 | total += pending_requests_[i].size(); |
| 767 | return total; |
| 768 | } |
| 769 | |
| 770 | bool HasPendingRequests() const { |
| 771 | return GetNumPendingRequests() > 0u; |
| 772 | } |
| 773 | |
| 774 | // Enqueues a request to this pool. As a result of enqueing this request, |
| 775 | // the queue may have reached its maximum size. In this case, a request is |
| 776 | // evicted from the queue, and returned. Otherwise returns NULL. The caller |
| 777 | // is responsible for freeing the evicted request. |
| 778 | Request* InsertPendingRequest(Request* req) { |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 779 | req->request_net_log().BeginEvent( |
| 780 | NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_POOL_QUEUE, |
| 781 | NULL); |
| 782 | |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 783 | PendingRequestsQueue& q = pending_requests_[req->info().priority()]; |
| 784 | q.push_back(req); |
| 785 | |
| 786 | // If the queue is too big, kick out the lowest priority oldest request. |
| 787 | if (GetNumPendingRequests() > max_pending_requests_) { |
| 788 | // Iterate over the queues from lowest priority to highest priority. |
| 789 | for (int i = static_cast<int>(arraysize(pending_requests_)) - 1; |
| 790 | i >= 0; --i) { |
| 791 | PendingRequestsQueue& q = pending_requests_[i]; |
| 792 | if (!q.empty()) { |
| 793 | Request* req = q.front(); |
| 794 | q.pop_front(); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 795 | req->request_net_log().AddEvent( |
| 796 | NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_POOL_QUEUE_EVICTED, NULL); |
| 797 | req->request_net_log().EndEvent( |
| 798 | NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_POOL_QUEUE, NULL); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 799 | return req; |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | return NULL; |
| 805 | } |
| 806 | |
| 807 | // Erases |req| from this container. Caller is responsible for freeing |
| 808 | // |req| afterwards. |
| 809 | void RemovePendingRequest(Request* req) { |
| 810 | PendingRequestsQueue& q = pending_requests_[req->info().priority()]; |
| 811 | PendingRequestsQueue::iterator it = std::find(q.begin(), q.end(), req); |
| 812 | DCHECK(it != q.end()); |
| 813 | q.erase(it); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 814 | req->request_net_log().EndEvent( |
| 815 | NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_POOL_QUEUE, NULL); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | // Removes and returns the highest priority pending request. |
| 819 | Request* RemoveTopPendingRequest() { |
| 820 | DCHECK(HasPendingRequests()); |
| 821 | |
| 822 | for (size_t i = 0u; i < arraysize(pending_requests_); ++i) { |
| 823 | PendingRequestsQueue& q = pending_requests_[i]; |
| 824 | if (!q.empty()) { |
| 825 | Request* req = q.front(); |
| 826 | q.pop_front(); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 827 | req->request_net_log().EndEvent( |
| 828 | NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_POOL_QUEUE, NULL); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 829 | return req; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | NOTREACHED(); |
| 834 | return NULL; |
| 835 | } |
| 836 | |
| 837 | // Keeps track of a job that was just added/removed, and belongs to this pool. |
| 838 | void AdjustNumOutstandingJobs(int offset) { |
| 839 | DCHECK(offset == 1 || (offset == -1 && num_outstanding_jobs_ > 0u)); |
| 840 | num_outstanding_jobs_ += offset; |
| 841 | } |
| 842 | |
[email protected] | 35ddc28 | 2010-09-21 23:42:06 | [diff] [blame] | 843 | void ResetNumOutstandingJobs() { |
| 844 | num_outstanding_jobs_ = 0; |
| 845 | } |
| 846 | |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 847 | // Returns true if a new job can be created for this pool. |
| 848 | bool CanCreateJob() const { |
| 849 | return num_outstanding_jobs_ + 1u <= max_outstanding_jobs_; |
| 850 | } |
| 851 | |
| 852 | // Removes any pending requests from the queue which are for the |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 853 | // same (hostname / effective address-family) as |job|, and attaches them to |
| 854 | // |job|. |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 855 | void MoveRequestsToJob(Job* job) { |
| 856 | for (size_t i = 0u; i < arraysize(pending_requests_); ++i) { |
| 857 | PendingRequestsQueue& q = pending_requests_[i]; |
| 858 | PendingRequestsQueue::iterator req_it = q.begin(); |
| 859 | while (req_it != q.end()) { |
| 860 | Request* req = *req_it; |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 861 | if (job->CanServiceRequest(req->info())) { |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 862 | // Job takes ownership of |req|. |
| 863 | job->AddRequest(req); |
| 864 | req_it = q.erase(req_it); |
| 865 | } else { |
| 866 | ++req_it; |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | private: |
| 873 | typedef std::deque<Request*> PendingRequestsQueue; |
| 874 | |
| 875 | // Maximum number of concurrent jobs allowed to be started for requests |
| 876 | // belonging to this pool. |
| 877 | size_t max_outstanding_jobs_; |
| 878 | |
| 879 | // The current number of running jobs that were started for requests |
| 880 | // belonging to this pool. |
| 881 | size_t num_outstanding_jobs_; |
| 882 | |
| 883 | // The maximum number of requests we allow to be waiting on a job, |
| 884 | // for this pool. |
| 885 | size_t max_pending_requests_; |
| 886 | |
| 887 | // The requests which are waiting to be started for this pool. |
| 888 | PendingRequestsQueue pending_requests_[NUM_PRIORITIES]; |
| 889 | }; |
| 890 | |
| 891 | //----------------------------------------------------------------------------- |
| 892 | |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 893 | HostResolverImpl::HostResolverImpl( |
| 894 | HostResolverProc* resolver_proc, |
| 895 | HostCache* cache, |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 896 | size_t max_jobs, |
| 897 | NetLog* net_log) |
[email protected] | 112bd46 | 2009-12-10 07:23:40 | [diff] [blame] | 898 | : cache_(cache), |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 899 | max_jobs_(max_jobs), |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 900 | next_request_id_(0), |
[email protected] | f2d8c421 | 2010-02-02 00:56:35 | [diff] [blame] | 901 | next_job_id_(0), |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 902 | resolver_proc_(resolver_proc), |
[email protected] | 0c779845 | 2009-10-26 17:59:51 | [diff] [blame] | 903 | default_address_family_(ADDRESS_FAMILY_UNSPECIFIED), |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 904 | shutdown_(false), |
[email protected] | 2f3bc65c | 2010-07-23 17:47:10 | [diff] [blame] | 905 | ipv6_probe_monitoring_(false), |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 906 | additional_resolver_flags_(0), |
| 907 | net_log_(net_log) { |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 908 | DCHECK_GT(max_jobs, 0u); |
| 909 | |
| 910 | // It is cumbersome to expose all of the constraints in the constructor, |
| 911 | // so we choose some defaults, which users can override later. |
| 912 | job_pools_[POOL_NORMAL] = new JobPool(max_jobs, 100u * max_jobs); |
| 913 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 914 | #if defined(OS_WIN) |
| 915 | EnsureWinsockInit(); |
| 916 | #endif |
[email protected] | 2f3bc65c | 2010-07-23 17:47:10 | [diff] [blame] | 917 | #if defined(OS_LINUX) |
| 918 | if (HaveOnlyLoopbackAddresses()) |
| 919 | additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY; |
| 920 | #endif |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 921 | NetworkChangeNotifier::AddObserver(this); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | HostResolverImpl::~HostResolverImpl() { |
| 925 | // Cancel the outstanding jobs. Those jobs may contain several attached |
| 926 | // requests, which will also be cancelled. |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 927 | DiscardIPv6ProbeJob(); |
| 928 | |
[email protected] | ef4c40c | 2010-09-01 14:42:03 | [diff] [blame] | 929 | CancelAllJobs(); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 930 | |
| 931 | // In case we are being deleted during the processing of a callback. |
| 932 | if (cur_completing_job_) |
| 933 | cur_completing_job_->Cancel(); |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 934 | |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 935 | NetworkChangeNotifier::RemoveObserver(this); |
[email protected] | 61a86c4 | 2010-04-19 22:45:53 | [diff] [blame] | 936 | |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 937 | // Delete the job pools. |
| 938 | for (size_t i = 0u; i < arraysize(job_pools_); ++i) |
| 939 | delete job_pools_[i]; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 940 | } |
| 941 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 942 | int HostResolverImpl::Resolve(const RequestInfo& info, |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 943 | AddressList* addresses, |
| 944 | CompletionCallback* callback, |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 945 | RequestHandle* out_req, |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 946 | const BoundNetLog& source_net_log) { |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 947 | DCHECK(CalledOnValidThread()); |
| 948 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 949 | if (shutdown_) |
| 950 | return ERR_UNEXPECTED; |
| 951 | |
| 952 | // Choose a unique ID number for observers to see. |
| 953 | int request_id = next_request_id_++; |
| 954 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 955 | // Make a log item for the request. |
| 956 | BoundNetLog request_net_log = BoundNetLog::Make(net_log_, |
| 957 | NetLog::SOURCE_HOST_RESOLVER_IMPL_REQUEST); |
| 958 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 959 | // Update the net log and notify registered observers. |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 960 | OnStartRequest(source_net_log, request_net_log, request_id, info); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 961 | |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 962 | // Build a key that identifies the request in the cache and in the |
| 963 | // outstanding jobs map. |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 964 | Key key = GetEffectiveKeyForRequest(info); |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 965 | |
[email protected] | eaf3a3b | 2010-09-03 20:34:27 | [diff] [blame] | 966 | // Check for IP literal. |
| 967 | IPAddressNumber ip_number; |
| 968 | if (ParseIPLiteralToNumber(info.hostname(), &ip_number)) { |
| 969 | DCHECK_EQ(key.host_resolver_flags & |
| 970 | ~(HOST_RESOLVER_CANONNAME | HOST_RESOLVER_LOOPBACK_ONLY | |
| 971 | HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6), |
| 972 | 0) << " Unhandled flag"; |
| 973 | bool ipv6_disabled = default_address_family_ == ADDRESS_FAMILY_IPV4 && |
| 974 | !ipv6_probe_monitoring_; |
| 975 | int net_error = OK; |
| 976 | if (ip_number.size() == 16 && ipv6_disabled) { |
| 977 | net_error = ERR_NAME_NOT_RESOLVED; |
| 978 | } else { |
| 979 | AddressList result(ip_number, info.port(), |
| 980 | (key.host_resolver_flags & HOST_RESOLVER_CANONNAME)); |
| 981 | *addresses = result; |
| 982 | } |
| 983 | // Update the net log and notify registered observers. |
| 984 | OnFinishRequest(source_net_log, request_net_log, request_id, info, |
| 985 | net_error, 0 /* os_error (unknown since from cache) */); |
| 986 | return net_error; |
| 987 | } |
| 988 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 989 | // If we have an unexpired cache entry, use it. |
[email protected] | 112bd46 | 2009-12-10 07:23:40 | [diff] [blame] | 990 | if (info.allow_cached_response() && cache_.get()) { |
| 991 | const HostCache::Entry* cache_entry = cache_->Lookup( |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 992 | key, base::TimeTicks::Now()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 993 | if (cache_entry) { |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 994 | request_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT, NULL); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 995 | int net_error = cache_entry->error; |
| 996 | if (net_error == OK) |
[email protected] | 112bd46 | 2009-12-10 07:23:40 | [diff] [blame] | 997 | addresses->SetFrom(cache_entry->addrlist, info.port()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 998 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 999 | // Update the net log and notify registered observers. |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1000 | OnFinishRequest(source_net_log, request_net_log, request_id, info, |
| 1001 | net_error, |
| 1002 | 0 /* os_error (unknown since from cache) */); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1003 | |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 1004 | return net_error; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | // If no callback was specified, do a synchronous resolution. |
| 1009 | if (!callback) { |
| 1010 | AddressList addrlist; |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 1011 | int os_error = 0; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1012 | int error = ResolveAddrInfo( |
[email protected] | 5ea28dea | 2010-04-08 15:35:13 | [diff] [blame] | 1013 | effective_resolver_proc(), key.hostname, key.address_family, |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 1014 | key.host_resolver_flags, &addrlist, &os_error); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1015 | if (error == OK) { |
| 1016 | addrlist.SetPort(info.port()); |
| 1017 | *addresses = addrlist; |
| 1018 | } |
| 1019 | |
| 1020 | // Write to cache. |
[email protected] | 112bd46 | 2009-12-10 07:23:40 | [diff] [blame] | 1021 | if (cache_.get()) |
| 1022 | cache_->Set(key, error, addrlist, base::TimeTicks::Now()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1023 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1024 | // Update the net log and notify registered observers. |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1025 | OnFinishRequest(source_net_log, request_net_log, request_id, info, error, |
| 1026 | os_error); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1027 | |
| 1028 | return error; |
| 1029 | } |
| 1030 | |
| 1031 | // Create a handle for this request, and pass it back to the user if they |
| 1032 | // asked for it (out_req != NULL). |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1033 | Request* req = new Request(source_net_log, request_net_log, request_id, info, |
| 1034 | callback, addresses); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1035 | if (out_req) |
| 1036 | *out_req = reinterpret_cast<RequestHandle>(req); |
| 1037 | |
| 1038 | // Next we need to attach our request to a "job". This job is responsible for |
| 1039 | // calling "getaddrinfo(hostname)" on a worker thread. |
| 1040 | scoped_refptr<Job> job; |
| 1041 | |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 1042 | // If there is already an outstanding job to resolve |key|, use |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1043 | // it. This prevents starting concurrent resolves for the same hostname. |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 1044 | job = FindOutstandingJob(key); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1045 | if (job) { |
| 1046 | job->AddRequest(req); |
| 1047 | } else { |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1048 | JobPool* pool = GetPoolForRequest(req); |
| 1049 | if (CanCreateJobForPool(*pool)) { |
| 1050 | CreateAndStartJob(req); |
| 1051 | } else { |
| 1052 | return EnqueueRequest(pool, req); |
| 1053 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1054 | } |
| 1055 | |
| 1056 | // Completion happens during OnJobComplete(Job*). |
| 1057 | return ERR_IO_PENDING; |
| 1058 | } |
| 1059 | |
| 1060 | // See OnJobComplete(Job*) for why it is important not to clean out |
| 1061 | // cancelled requests from Job::requests_. |
| 1062 | void HostResolverImpl::CancelRequest(RequestHandle req_handle) { |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 1063 | DCHECK(CalledOnValidThread()); |
[email protected] | bf0b51c | 2009-08-01 23:46:40 | [diff] [blame] | 1064 | if (shutdown_) { |
[email protected] | ff1f61b9 | 2009-08-03 23:20:23 | [diff] [blame] | 1065 | // TODO(eroman): temp hack for: http://crbug.com/18373 |
| 1066 | // Because we destroy outstanding requests during Shutdown(), |
| 1067 | // |req_handle| is already cancelled. |
[email protected] | bf0b51c | 2009-08-01 23:46:40 | [diff] [blame] | 1068 | LOG(ERROR) << "Called HostResolverImpl::CancelRequest() after Shutdown()."; |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame^] | 1069 | base::debug::StackTrace().PrintBacktrace(); |
[email protected] | bf0b51c | 2009-08-01 23:46:40 | [diff] [blame] | 1070 | return; |
| 1071 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1072 | Request* req = reinterpret_cast<Request*>(req_handle); |
| 1073 | DCHECK(req); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1074 | |
| 1075 | scoped_ptr<Request> request_deleter; // Frees at end of function. |
| 1076 | |
| 1077 | if (!req->job()) { |
| 1078 | // If the request was not attached to a job yet, it must have been |
| 1079 | // enqueued into a pool. Remove it from that pool's queue. |
| 1080 | // Otherwise if it was attached to a job, the job is responsible for |
| 1081 | // deleting it. |
| 1082 | JobPool* pool = GetPoolForRequest(req); |
| 1083 | pool->RemovePendingRequest(req); |
| 1084 | request_deleter.reset(req); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1085 | } else { |
| 1086 | req->request_net_log().EndEvent( |
| 1087 | NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH, NULL); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1088 | } |
| 1089 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1090 | // NULL out the fields of req, to mark it as cancelled. |
| 1091 | req->MarkAsCancelled(); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1092 | OnCancelRequest(req->source_net_log(), req->request_net_log(), req->id(), |
| 1093 | req->info()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1094 | } |
| 1095 | |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 1096 | void HostResolverImpl::AddObserver(HostResolver::Observer* observer) { |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 1097 | DCHECK(CalledOnValidThread()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1098 | observers_.push_back(observer); |
| 1099 | } |
| 1100 | |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 1101 | void HostResolverImpl::RemoveObserver(HostResolver::Observer* observer) { |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 1102 | DCHECK(CalledOnValidThread()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1103 | ObserversList::iterator it = |
| 1104 | std::find(observers_.begin(), observers_.end(), observer); |
| 1105 | |
| 1106 | // Observer must exist. |
| 1107 | DCHECK(it != observers_.end()); |
| 1108 | |
| 1109 | observers_.erase(it); |
| 1110 | } |
| 1111 | |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 1112 | void HostResolverImpl::SetDefaultAddressFamily(AddressFamily address_family) { |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 1113 | DCHECK(CalledOnValidThread()); |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 1114 | ipv6_probe_monitoring_ = false; |
| 1115 | DiscardIPv6ProbeJob(); |
| 1116 | default_address_family_ = address_family; |
| 1117 | } |
| 1118 | |
[email protected] | f7d310e | 2010-10-07 16:25:11 | [diff] [blame] | 1119 | AddressFamily HostResolverImpl::GetDefaultAddressFamily() const { |
| 1120 | return default_address_family_; |
| 1121 | } |
| 1122 | |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 1123 | void HostResolverImpl::ProbeIPv6Support() { |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 1124 | DCHECK(CalledOnValidThread()); |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 1125 | DCHECK(!ipv6_probe_monitoring_); |
| 1126 | ipv6_probe_monitoring_ = true; |
[email protected] | 61a86c4 | 2010-04-19 22:45:53 | [diff] [blame] | 1127 | OnIPAddressChanged(); // Give initial setup call. |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 1128 | } |
| 1129 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1130 | void HostResolverImpl::Shutdown() { |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 1131 | DCHECK(CalledOnValidThread()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1132 | |
| 1133 | // Cancel the outstanding jobs. |
[email protected] | ef4c40c | 2010-09-01 14:42:03 | [diff] [blame] | 1134 | CancelAllJobs(); |
[email protected] | 0a17551 | 2010-04-20 03:09:25 | [diff] [blame] | 1135 | DiscardIPv6ProbeJob(); |
[email protected] | be5c616 | 2010-07-27 21:12:51 | [diff] [blame] | 1136 | |
| 1137 | shutdown_ = true; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1138 | } |
| 1139 | |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1140 | void HostResolverImpl::SetPoolConstraints(JobPoolIndex pool_index, |
| 1141 | size_t max_outstanding_jobs, |
| 1142 | size_t max_pending_requests) { |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 1143 | DCHECK(CalledOnValidThread()); |
[email protected] | b1f031dd | 2010-03-02 23:19:33 | [diff] [blame] | 1144 | CHECK_GE(pool_index, 0); |
| 1145 | CHECK_LT(pool_index, POOL_COUNT); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1146 | CHECK(jobs_.empty()) << "Can only set constraints during setup"; |
| 1147 | JobPool* pool = job_pools_[pool_index]; |
| 1148 | pool->SetConstraints(max_outstanding_jobs, max_pending_requests); |
| 1149 | } |
| 1150 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1151 | void HostResolverImpl::AddOutstandingJob(Job* job) { |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 1152 | scoped_refptr<Job>& found_job = jobs_[job->key()]; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1153 | DCHECK(!found_job); |
| 1154 | found_job = job; |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1155 | |
| 1156 | JobPool* pool = GetPoolForRequest(job->initial_request()); |
| 1157 | pool->AdjustNumOutstandingJobs(1); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1158 | } |
| 1159 | |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 1160 | HostResolverImpl::Job* HostResolverImpl::FindOutstandingJob(const Key& key) { |
| 1161 | JobMap::iterator it = jobs_.find(key); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1162 | if (it != jobs_.end()) |
| 1163 | return it->second; |
| 1164 | return NULL; |
| 1165 | } |
| 1166 | |
| 1167 | void HostResolverImpl::RemoveOutstandingJob(Job* job) { |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 1168 | JobMap::iterator it = jobs_.find(job->key()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1169 | DCHECK(it != jobs_.end()); |
| 1170 | DCHECK_EQ(it->second.get(), job); |
| 1171 | jobs_.erase(it); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1172 | |
| 1173 | JobPool* pool = GetPoolForRequest(job->initial_request()); |
| 1174 | pool->AdjustNumOutstandingJobs(-1); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1175 | } |
| 1176 | |
| 1177 | void HostResolverImpl::OnJobComplete(Job* job, |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 1178 | int net_error, |
| 1179 | int os_error, |
[email protected] | 27f99c8 | 2009-10-29 22:21:00 | [diff] [blame] | 1180 | const AddressList& addrlist) { |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1181 | RemoveOutstandingJob(job); |
| 1182 | |
| 1183 | // Write result to the cache. |
[email protected] | 112bd46 | 2009-12-10 07:23:40 | [diff] [blame] | 1184 | if (cache_.get()) |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 1185 | cache_->Set(job->key(), net_error, addrlist, base::TimeTicks::Now()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1186 | |
[email protected] | ef4c40c | 2010-09-01 14:42:03 | [diff] [blame] | 1187 | OnJobCompleteInternal(job, net_error, os_error, addrlist); |
| 1188 | } |
| 1189 | |
| 1190 | void HostResolverImpl::AbortJob(Job* job) { |
| 1191 | OnJobCompleteInternal(job, ERR_ABORTED, 0 /* no os_error */, AddressList()); |
| 1192 | } |
| 1193 | |
| 1194 | void HostResolverImpl::OnJobCompleteInternal( |
| 1195 | Job* job, |
| 1196 | int net_error, |
| 1197 | int os_error, |
| 1198 | const AddressList& addrlist) { |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1199 | // Make a note that we are executing within OnJobComplete() in case the |
| 1200 | // HostResolver is deleted by a callback invocation. |
| 1201 | DCHECK(!cur_completing_job_); |
| 1202 | cur_completing_job_ = job; |
| 1203 | |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1204 | // Try to start any queued requests now that a job-slot has freed up. |
| 1205 | ProcessQueuedRequests(); |
| 1206 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1207 | // Complete all of the requests that were attached to the job. |
| 1208 | for (RequestsList::const_iterator it = job->requests().begin(); |
| 1209 | it != job->requests().end(); ++it) { |
| 1210 | Request* req = *it; |
| 1211 | if (!req->was_cancelled()) { |
| 1212 | DCHECK_EQ(job, req->job()); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1213 | req->request_net_log().EndEvent( |
| 1214 | NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH, NULL); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1215 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 1216 | // Update the net log and notify registered observers. |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1217 | OnFinishRequest(req->source_net_log(), req->request_net_log(), req->id(), |
| 1218 | req->info(), net_error, os_error); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1219 | |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 1220 | req->OnComplete(net_error, addrlist); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1221 | |
| 1222 | // Check if the job was cancelled as a result of running the callback. |
| 1223 | // (Meaning that |this| was deleted). |
| 1224 | if (job->was_cancelled()) |
| 1225 | return; |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | cur_completing_job_ = NULL; |
| 1230 | } |
| 1231 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1232 | void HostResolverImpl::OnStartRequest(const BoundNetLog& source_net_log, |
| 1233 | const BoundNetLog& request_net_log, |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 1234 | int request_id, |
| 1235 | const RequestInfo& info) { |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1236 | source_net_log.BeginEvent( |
| 1237 | NetLog::TYPE_HOST_RESOLVER_IMPL, |
| 1238 | new NetLogSourceParameter("source_dependency", request_net_log.source())); |
| 1239 | |
| 1240 | request_net_log.BeginEvent( |
| 1241 | NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, |
| 1242 | new RequestInfoParameters(info, source_net_log.source())); |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 1243 | |
| 1244 | // Notify the observers of the start. |
| 1245 | if (!observers_.empty()) { |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 1246 | for (ObserversList::iterator it = observers_.begin(); |
| 1247 | it != observers_.end(); ++it) { |
| 1248 | (*it)->OnStartResolution(request_id, info); |
| 1249 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1250 | } |
| 1251 | } |
| 1252 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1253 | void HostResolverImpl::OnFinishRequest(const BoundNetLog& source_net_log, |
| 1254 | const BoundNetLog& request_net_log, |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 1255 | int request_id, |
| 1256 | const RequestInfo& info, |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 1257 | int net_error, |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1258 | int os_error) { |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 1259 | bool was_resolved = net_error == OK; |
| 1260 | |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 1261 | // Notify the observers of the completion. |
| 1262 | if (!observers_.empty()) { |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 1263 | for (ObserversList::iterator it = observers_.begin(); |
| 1264 | it != observers_.end(); ++it) { |
| 1265 | (*it)->OnFinishResolutionWithStatus(request_id, was_resolved, info); |
| 1266 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1267 | } |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 1268 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1269 | // Log some extra parameters on failure for synchronous requests. |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 1270 | scoped_refptr<NetLog::EventParameters> params; |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1271 | if (!was_resolved) { |
| 1272 | params = new HostResolveFailedParams(net_error, os_error); |
| 1273 | } |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 1274 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1275 | request_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, params); |
| 1276 | source_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL, NULL); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1277 | } |
| 1278 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1279 | void HostResolverImpl::OnCancelRequest(const BoundNetLog& source_net_log, |
| 1280 | const BoundNetLog& request_net_log, |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 1281 | int request_id, |
| 1282 | const RequestInfo& info) { |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1283 | request_net_log.AddEvent(NetLog::TYPE_CANCELLED, NULL); |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 1284 | |
| 1285 | // Notify the observers of the cancellation. |
| 1286 | if (!observers_.empty()) { |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 1287 | for (ObserversList::iterator it = observers_.begin(); |
| 1288 | it != observers_.end(); ++it) { |
| 1289 | (*it)->OnCancelResolution(request_id, info); |
| 1290 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1291 | } |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 1292 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1293 | request_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, NULL); |
| 1294 | source_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL, NULL); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1295 | } |
| 1296 | |
[email protected] | 61a86c4 | 2010-04-19 22:45:53 | [diff] [blame] | 1297 | void HostResolverImpl::OnIPAddressChanged() { |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 1298 | if (cache_.get()) |
| 1299 | cache_->clear(); |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 1300 | if (ipv6_probe_monitoring_) { |
[email protected] | 0a17551 | 2010-04-20 03:09:25 | [diff] [blame] | 1301 | DCHECK(!shutdown_); |
| 1302 | if (shutdown_) |
| 1303 | return; |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 1304 | DiscardIPv6ProbeJob(); |
| 1305 | ipv6_probe_job_ = new IPv6ProbeJob(this); |
| 1306 | ipv6_probe_job_->Start(); |
| 1307 | } |
[email protected] | 2f3bc65c | 2010-07-23 17:47:10 | [diff] [blame] | 1308 | #if defined(OS_LINUX) |
| 1309 | if (HaveOnlyLoopbackAddresses()) { |
| 1310 | additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY; |
| 1311 | } else { |
| 1312 | additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY; |
| 1313 | } |
| 1314 | #endif |
[email protected] | 35ddc28 | 2010-09-21 23:42:06 | [diff] [blame] | 1315 | AbortAllInProgressJobs(); |
| 1316 | // |this| may be deleted inside AbortAllInProgressJobs(). |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | void HostResolverImpl::DiscardIPv6ProbeJob() { |
| 1320 | if (ipv6_probe_job_.get()) { |
| 1321 | ipv6_probe_job_->Cancel(); |
| 1322 | ipv6_probe_job_ = NULL; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | void HostResolverImpl::IPv6ProbeSetDefaultAddressFamily( |
| 1327 | AddressFamily address_family) { |
| 1328 | DCHECK(address_family == ADDRESS_FAMILY_UNSPECIFIED || |
| 1329 | address_family == ADDRESS_FAMILY_IPV4); |
[email protected] | f092e64b | 2010-03-17 00:39:18 | [diff] [blame] | 1330 | if (default_address_family_ != address_family) { |
[email protected] | b30a3f5 | 2010-10-16 01:05:46 | [diff] [blame] | 1331 | VLOG(1) << "IPv6Probe forced AddressFamily setting to " |
| 1332 | << ((address_family == ADDRESS_FAMILY_UNSPECIFIED) ? |
| 1333 | "ADDRESS_FAMILY_UNSPECIFIED" : "ADDRESS_FAMILY_IPV4"); |
[email protected] | f092e64b | 2010-03-17 00:39:18 | [diff] [blame] | 1334 | } |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 1335 | default_address_family_ = address_family; |
| 1336 | // Drop reference since the job has called us back. |
| 1337 | DiscardIPv6ProbeJob(); |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 1338 | } |
| 1339 | |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1340 | // static |
| 1341 | HostResolverImpl::JobPoolIndex HostResolverImpl::GetJobPoolIndexForRequest( |
| 1342 | const Request* req) { |
| 1343 | return POOL_NORMAL; |
| 1344 | } |
| 1345 | |
| 1346 | bool HostResolverImpl::CanCreateJobForPool(const JobPool& pool) const { |
| 1347 | DCHECK_LE(jobs_.size(), max_jobs_); |
| 1348 | |
| 1349 | // We can't create another job if it would exceed the global total. |
| 1350 | if (jobs_.size() + 1 > max_jobs_) |
| 1351 | return false; |
| 1352 | |
| 1353 | // Check whether the pool's constraints are met. |
| 1354 | return pool.CanCreateJob(); |
| 1355 | } |
| 1356 | |
| 1357 | void HostResolverImpl::ProcessQueuedRequests() { |
| 1358 | // Find the highest priority request that can be scheduled. |
| 1359 | Request* top_req = NULL; |
| 1360 | for (size_t i = 0; i < arraysize(job_pools_); ++i) { |
| 1361 | JobPool* pool = job_pools_[i]; |
| 1362 | if (pool->HasPendingRequests() && CanCreateJobForPool(*pool)) { |
| 1363 | top_req = pool->RemoveTopPendingRequest(); |
| 1364 | break; |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | if (!top_req) |
| 1369 | return; |
| 1370 | |
| 1371 | scoped_refptr<Job> job = CreateAndStartJob(top_req); |
| 1372 | |
| 1373 | // Search for any other pending request which can piggy-back off this job. |
| 1374 | for (size_t pool_i = 0; pool_i < POOL_COUNT; ++pool_i) { |
| 1375 | JobPool* pool = job_pools_[pool_i]; |
| 1376 | pool->MoveRequestsToJob(job); |
| 1377 | } |
| 1378 | } |
| 1379 | |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 1380 | HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest( |
| 1381 | const RequestInfo& info) const { |
[email protected] | eaf3a3b | 2010-09-03 20:34:27 | [diff] [blame] | 1382 | HostResolverFlags effective_flags = |
| 1383 | info.host_resolver_flags() | additional_resolver_flags_; |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 1384 | AddressFamily effective_address_family = info.address_family(); |
[email protected] | eaf3a3b | 2010-09-03 20:34:27 | [diff] [blame] | 1385 | if (effective_address_family == ADDRESS_FAMILY_UNSPECIFIED && |
| 1386 | default_address_family_ != ADDRESS_FAMILY_UNSPECIFIED) { |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 1387 | effective_address_family = default_address_family_; |
[email protected] | eaf3a3b | 2010-09-03 20:34:27 | [diff] [blame] | 1388 | if (ipv6_probe_monitoring_) |
| 1389 | effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; |
| 1390 | } |
| 1391 | return Key(info.hostname(), effective_address_family, effective_flags); |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 1392 | } |
| 1393 | |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1394 | HostResolverImpl::Job* HostResolverImpl::CreateAndStartJob(Request* req) { |
| 1395 | DCHECK(CanCreateJobForPool(*GetPoolForRequest(req))); |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 1396 | Key key = GetEffectiveKeyForRequest(req->info()); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1397 | |
| 1398 | req->request_net_log().AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CREATE_JOB, |
| 1399 | NULL); |
| 1400 | |
| 1401 | scoped_refptr<Job> job = new Job(next_job_id_++, this, key, |
| 1402 | req->request_net_log(), net_log_); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1403 | job->AddRequest(req); |
| 1404 | AddOutstandingJob(job); |
| 1405 | job->Start(); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1406 | |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1407 | return job.get(); |
| 1408 | } |
| 1409 | |
| 1410 | int HostResolverImpl::EnqueueRequest(JobPool* pool, Request* req) { |
| 1411 | scoped_ptr<Request> req_evicted_from_queue( |
| 1412 | pool->InsertPendingRequest(req)); |
| 1413 | |
| 1414 | // If the queue has become too large, we need to kick something out. |
| 1415 | if (req_evicted_from_queue.get()) { |
| 1416 | Request* r = req_evicted_from_queue.get(); |
| 1417 | int error = ERR_HOST_RESOLVER_QUEUE_TOO_LARGE; |
| 1418 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1419 | OnFinishRequest(r->source_net_log(), r->request_net_log(), r->id(), |
| 1420 | r->info(), error, |
| 1421 | 0 /* os_error (not applicable) */); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1422 | |
| 1423 | if (r == req) |
| 1424 | return error; |
| 1425 | |
| 1426 | r->OnComplete(error, AddressList()); |
| 1427 | } |
| 1428 | |
| 1429 | return ERR_IO_PENDING; |
| 1430 | } |
| 1431 | |
[email protected] | ef4c40c | 2010-09-01 14:42:03 | [diff] [blame] | 1432 | void HostResolverImpl::CancelAllJobs() { |
| 1433 | JobMap jobs; |
| 1434 | jobs.swap(jobs_); |
| 1435 | for (JobMap::iterator it = jobs.begin(); it != jobs.end(); ++it) |
| 1436 | it->second->Cancel(); |
| 1437 | } |
| 1438 | |
[email protected] | 35ddc28 | 2010-09-21 23:42:06 | [diff] [blame] | 1439 | void HostResolverImpl::AbortAllInProgressJobs() { |
| 1440 | for (size_t i = 0; i < arraysize(job_pools_); ++i) |
| 1441 | job_pools_[i]->ResetNumOutstandingJobs(); |
[email protected] | ef4c40c | 2010-09-01 14:42:03 | [diff] [blame] | 1442 | JobMap jobs; |
| 1443 | jobs.swap(jobs_); |
| 1444 | for (JobMap::iterator it = jobs.begin(); it != jobs.end(); ++it) { |
| 1445 | AbortJob(it->second); |
| 1446 | it->second->Cancel(); |
| 1447 | } |
| 1448 | } |
| 1449 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1450 | } // namespace net |