| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/dns/host_resolver_impl.h" | 5 #include "net/dns/host_resolver_impl.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 dict->SetInteger("attempt_number", attempt_number); | 334 dict->SetInteger("attempt_number", attempt_number); |
| 335 | 335 |
| 336 dict->SetInteger("net_error", net_error); | 336 dict->SetInteger("net_error", net_error); |
| 337 | 337 |
| 338 if (os_error) { | 338 if (os_error) { |
| 339 dict->SetInteger("os_error", os_error); | 339 dict->SetInteger("os_error", os_error); |
| 340 #if defined(OS_POSIX) | 340 #if defined(OS_POSIX) |
| 341 dict->SetString("os_error_string", gai_strerror(os_error)); | 341 dict->SetString("os_error_string", gai_strerror(os_error)); |
| 342 #elif defined(OS_WIN) | 342 #elif defined(OS_WIN) |
| 343 // Map the error code to a human-readable string. | 343 // Map the error code to a human-readable string. |
| 344 LPWSTR error_string = NULL; | 344 LPWSTR error_string = nullptr; |
| 345 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | 345 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
| 346 0, // Use the internal message table. | 346 0, // Use the internal message table. |
| 347 os_error, | 347 os_error, |
| 348 0, // Use default language. | 348 0, // Use default language. |
| 349 (LPWSTR)&error_string, | 349 (LPWSTR)&error_string, |
| 350 0, // Buffer size. | 350 0, // Buffer size. |
| 351 0); // Arguments (unused). | 351 0); // Arguments (unused). |
| 352 dict->SetString("os_error_string", base::WideToUTF8(error_string)); | 352 dict->SetString("os_error_string", base::WideToUTF8(error_string)); |
| 353 LocalFree(error_string); | 353 LocalFree(error_string); |
| 354 #endif | 354 #endif |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 class HostResolverImpl::Request { | 531 class HostResolverImpl::Request { |
| 532 public: | 532 public: |
| 533 Request(const BoundNetLog& source_net_log, | 533 Request(const BoundNetLog& source_net_log, |
| 534 const RequestInfo& info, | 534 const RequestInfo& info, |
| 535 RequestPriority priority, | 535 RequestPriority priority, |
| 536 const CompletionCallback& callback, | 536 const CompletionCallback& callback, |
| 537 AddressList* addresses) | 537 AddressList* addresses) |
| 538 : source_net_log_(source_net_log), | 538 : source_net_log_(source_net_log), |
| 539 info_(info), | 539 info_(info), |
| 540 priority_(priority), | 540 priority_(priority), |
| 541 job_(NULL), | 541 job_(nullptr), |
| 542 callback_(callback), | 542 callback_(callback), |
| 543 addresses_(addresses), | 543 addresses_(addresses), |
| 544 request_time_(base::TimeTicks::Now()) {} | 544 request_time_(base::TimeTicks::Now()) {} |
| 545 | 545 |
| 546 // Mark the request as canceled. | 546 // Mark the request as canceled. |
| 547 void MarkAsCanceled() { | 547 void MarkAsCanceled() { |
| 548 job_ = NULL; | 548 job_ = nullptr; |
| 549 addresses_ = NULL; | 549 addresses_ = nullptr; |
| 550 callback_.Reset(); | 550 callback_.Reset(); |
| 551 } | 551 } |
| 552 | 552 |
| 553 bool was_canceled() const { | 553 bool was_canceled() const { |
| 554 return callback_.is_null(); | 554 return callback_.is_null(); |
| 555 } | 555 } |
| 556 | 556 |
| 557 void set_job(Job* job) { | 557 void set_job(Job* job) { |
| 558 DCHECK(job); | 558 DCHECK(job); |
| 559 // Identify which job the request is waiting on. | 559 // Identify which job the request is waiting on. |
| (...skipping 743 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1303 source_net_log.source(), | 1303 source_net_log.source(), |
| 1304 &key_.hostname)); | 1304 &key_.hostname)); |
| 1305 } | 1305 } |
| 1306 | 1306 |
| 1307 ~Job() override { | 1307 ~Job() override { |
| 1308 if (is_running()) { | 1308 if (is_running()) { |
| 1309 // |resolver_| was destroyed with this Job still in flight. | 1309 // |resolver_| was destroyed with this Job still in flight. |
| 1310 // Clean-up, record in the log, but don't run any callbacks. | 1310 // Clean-up, record in the log, but don't run any callbacks. |
| 1311 if (is_proc_running()) { | 1311 if (is_proc_running()) { |
| 1312 proc_task_->Cancel(); | 1312 proc_task_->Cancel(); |
| 1313 proc_task_ = NULL; | 1313 proc_task_ = nullptr; |
| 1314 } | 1314 } |
| 1315 // Clean up now for nice NetLog. | 1315 // Clean up now for nice NetLog. |
| 1316 KillDnsTask(); | 1316 KillDnsTask(); |
| 1317 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, | 1317 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, |
| 1318 ERR_ABORTED); | 1318 ERR_ABORTED); |
| 1319 } else if (is_queued()) { | 1319 } else if (is_queued()) { |
| 1320 // |resolver_| was destroyed without running this Job. | 1320 // |resolver_| was destroyed without running this Job. |
| 1321 // TODO(szym): is there any benefit in having this distinction? | 1321 // TODO(szym): is there any benefit in having this distinction? |
| 1322 net_log_.AddEvent(NetLog::TYPE_CANCELLED); | 1322 net_log_.AddEvent(NetLog::TYPE_CANCELLED); |
| 1323 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB); | 1323 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB); |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1644 } | 1644 } |
| 1645 | 1645 |
| 1646 // Called if DnsTask fails. It is posted from StartDnsTask, so Job may be | 1646 // Called if DnsTask fails. It is posted from StartDnsTask, so Job may be |
| 1647 // deleted before this callback. In this case dns_task is deleted as well, | 1647 // deleted before this callback. In this case dns_task is deleted as well, |
| 1648 // so we use it as indicator whether Job is still valid. | 1648 // so we use it as indicator whether Job is still valid. |
| 1649 void OnDnsTaskFailure(const base::WeakPtr<DnsTask>& dns_task, | 1649 void OnDnsTaskFailure(const base::WeakPtr<DnsTask>& dns_task, |
| 1650 base::TimeDelta duration, | 1650 base::TimeDelta duration, |
| 1651 int net_error) { | 1651 int net_error) { |
| 1652 DNS_HISTOGRAM("AsyncDNS.ResolveFail", duration); | 1652 DNS_HISTOGRAM("AsyncDNS.ResolveFail", duration); |
| 1653 | 1653 |
| 1654 if (dns_task == NULL) | 1654 if (!dns_task) |
| 1655 return; | 1655 return; |
| 1656 | 1656 |
| 1657 dns_task_error_ = net_error; | 1657 dns_task_error_ = net_error; |
| 1658 | 1658 |
| 1659 // TODO(szym): Run ServeFromHosts now if nsswitch.conf says so. | 1659 // TODO(szym): Run ServeFromHosts now if nsswitch.conf says so. |
| 1660 // http://crbug.com/117655 | 1660 // http://crbug.com/117655 |
| 1661 | 1661 |
| 1662 // TODO(szym): Some net errors indicate lack of connectivity. Starting | 1662 // TODO(szym): Some net errors indicate lack of connectivity. Starting |
| 1663 // ProcTask in that case is a waste of time. | 1663 // ProcTask in that case is a waste of time. |
| 1664 if (resolver_->fallback_to_proctask_) { | 1664 if (resolver_->fallback_to_proctask_) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1734 // to spawn one. Consequently, the job deletes itself when CompleteRequests | 1734 // to spawn one. Consequently, the job deletes itself when CompleteRequests |
| 1735 // is done. | 1735 // is done. |
| 1736 std::unique_ptr<Job> self_deleter(this); | 1736 std::unique_ptr<Job> self_deleter(this); |
| 1737 | 1737 |
| 1738 resolver_->RemoveJob(this); | 1738 resolver_->RemoveJob(this); |
| 1739 | 1739 |
| 1740 if (is_running()) { | 1740 if (is_running()) { |
| 1741 if (is_proc_running()) { | 1741 if (is_proc_running()) { |
| 1742 DCHECK(!is_queued()); | 1742 DCHECK(!is_queued()); |
| 1743 proc_task_->Cancel(); | 1743 proc_task_->Cancel(); |
| 1744 proc_task_ = NULL; | 1744 proc_task_ = nullptr; |
| 1745 } | 1745 } |
| 1746 KillDnsTask(); | 1746 KillDnsTask(); |
| 1747 | 1747 |
| 1748 // Signal dispatcher that a slot has opened. | 1748 // Signal dispatcher that a slot has opened. |
| 1749 resolver_->dispatcher_->OnJobFinished(); | 1749 resolver_->dispatcher_->OnJobFinished(); |
| 1750 } else if (is_queued()) { | 1750 } else if (is_queued()) { |
| 1751 resolver_->dispatcher_->Cancel(handle_); | 1751 resolver_->dispatcher_->Cancel(handle_); |
| 1752 handle_.Reset(); | 1752 handle_.Reset(); |
| 1753 } | 1753 } |
| 1754 | 1754 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1806 | 1806 |
| 1807 RequestPriority priority() const { | 1807 RequestPriority priority() const { |
| 1808 return priority_tracker_.highest_priority(); | 1808 return priority_tracker_.highest_priority(); |
| 1809 } | 1809 } |
| 1810 | 1810 |
| 1811 // Number of non-canceled requests in |requests_|. | 1811 // Number of non-canceled requests in |requests_|. |
| 1812 size_t num_active_requests() const { | 1812 size_t num_active_requests() const { |
| 1813 return priority_tracker_.total_count(); | 1813 return priority_tracker_.total_count(); |
| 1814 } | 1814 } |
| 1815 | 1815 |
| 1816 bool is_dns_running() const { | 1816 bool is_dns_running() const { return !!dns_task_; } |
| 1817 return dns_task_.get() != NULL; | |
| 1818 } | |
| 1819 | 1817 |
| 1820 bool is_proc_running() const { | 1818 bool is_proc_running() const { return !!proc_task_; } |
| 1821 return proc_task_.get() != NULL; | |
| 1822 } | |
| 1823 | 1819 |
| 1824 base::WeakPtr<HostResolverImpl> resolver_; | 1820 base::WeakPtr<HostResolverImpl> resolver_; |
| 1825 | 1821 |
| 1826 Key key_; | 1822 Key key_; |
| 1827 | 1823 |
| 1828 // Tracks the highest priority across |requests_|. | 1824 // Tracks the highest priority across |requests_|. |
| 1829 PriorityTracker priority_tracker_; | 1825 PriorityTracker priority_tracker_; |
| 1830 | 1826 |
| 1831 // Task runner where the HostResolverProc is invoked. | 1827 // Task runner where the HostResolverProc is invoked. |
| 1832 scoped_refptr<base::TaskRunner> worker_task_runner_; | 1828 scoped_refptr<base::TaskRunner> worker_task_runner_; |
| (...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2137 } | 2133 } |
| 2138 | 2134 |
| 2139 std::unique_ptr<base::Value> HostResolverImpl::GetDnsConfigAsValue() const { | 2135 std::unique_ptr<base::Value> HostResolverImpl::GetDnsConfigAsValue() const { |
| 2140 // Check if async DNS is disabled. | 2136 // Check if async DNS is disabled. |
| 2141 if (!dns_client_.get()) | 2137 if (!dns_client_.get()) |
| 2142 return nullptr; | 2138 return nullptr; |
| 2143 | 2139 |
| 2144 // Check if async DNS is enabled, but we currently have no configuration | 2140 // Check if async DNS is enabled, but we currently have no configuration |
| 2145 // for it. | 2141 // for it. |
| 2146 const DnsConfig* dns_config = dns_client_->GetConfig(); | 2142 const DnsConfig* dns_config = dns_client_->GetConfig(); |
| 2147 if (dns_config == NULL) | 2143 if (!dns_config) |
| 2148 return base::WrapUnique(new base::DictionaryValue()); | 2144 return base::WrapUnique(new base::DictionaryValue()); |
| 2149 | 2145 |
| 2150 return dns_config->ToValue(); | 2146 return dns_config->ToValue(); |
| 2151 } | 2147 } |
| 2152 | 2148 |
| 2153 int HostResolverImpl::ResolveStaleFromCache( | 2149 int HostResolverImpl::ResolveStaleFromCache( |
| 2154 const RequestInfo& info, | 2150 const RequestInfo& info, |
| 2155 AddressList* addresses, | 2151 AddressList* addresses, |
| 2156 HostCache::EntryStaleness* stale_info, | 2152 HostCache::EntryStaleness* stale_info, |
| 2157 const BoundNetLog& source_net_log) { | 2153 const BoundNetLog& source_net_log) { |
| (...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2512 if (self.get()) | 2508 if (self.get()) |
| 2513 TryServingAllJobsFromHosts(); | 2509 TryServingAllJobsFromHosts(); |
| 2514 } | 2510 } |
| 2515 } | 2511 } |
| 2516 | 2512 |
| 2517 bool HostResolverImpl::HaveDnsConfig() const { | 2513 bool HostResolverImpl::HaveDnsConfig() const { |
| 2518 // Use DnsClient only if it's fully configured and there is no override by | 2514 // Use DnsClient only if it's fully configured and there is no override by |
| 2519 // ScopedDefaultHostResolverProc. | 2515 // ScopedDefaultHostResolverProc. |
| 2520 // The alternative is to use NetworkChangeNotifier to override DnsConfig, | 2516 // The alternative is to use NetworkChangeNotifier to override DnsConfig, |
| 2521 // but that would introduce construction order requirements for NCN and SDHRP. | 2517 // but that would introduce construction order requirements for NCN and SDHRP. |
| 2522 return (dns_client_.get() != NULL) && (dns_client_->GetConfig() != NULL) && | 2518 return dns_client_ && dns_client_->GetConfig() && |
| 2523 !(proc_params_.resolver_proc.get() == NULL && | 2519 (proc_params_.resolver_proc || !HostResolverProc::GetDefault()); |
| 2524 HostResolverProc::GetDefault() != NULL); | |
| 2525 } | 2520 } |
| 2526 | 2521 |
| 2527 void HostResolverImpl::OnDnsTaskResolve(int net_error) { | 2522 void HostResolverImpl::OnDnsTaskResolve(int net_error) { |
| 2528 DCHECK(dns_client_); | 2523 DCHECK(dns_client_); |
| 2529 if (net_error == OK) { | 2524 if (net_error == OK) { |
| 2530 num_dns_failures_ = 0; | 2525 num_dns_failures_ = 0; |
| 2531 return; | 2526 return; |
| 2532 } | 2527 } |
| 2533 ++num_dns_failures_; | 2528 ++num_dns_failures_; |
| 2534 if (num_dns_failures_ < kMaximumDnsFailures) | 2529 if (num_dns_failures_ < kMaximumDnsFailures) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 2557 dns_client_->SetConfig(dns_config); | 2552 dns_client_->SetConfig(dns_config); |
| 2558 num_dns_failures_ = 0; | 2553 num_dns_failures_ = 0; |
| 2559 if (dns_client_->GetConfig()) | 2554 if (dns_client_->GetConfig()) |
| 2560 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); | 2555 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); |
| 2561 } | 2556 } |
| 2562 | 2557 |
| 2563 AbortDnsTasks(); | 2558 AbortDnsTasks(); |
| 2564 } | 2559 } |
| 2565 | 2560 |
| 2566 } // namespace net | 2561 } // namespace net |
| OLD | NEW |