Chromium Code Reviews| 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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 523 class HostResolverImpl::Request { | 523 class HostResolverImpl::Request { |
| 524 public: | 524 public: |
| 525 Request(const BoundNetLog& source_net_log, | 525 Request(const BoundNetLog& source_net_log, |
| 526 const RequestInfo& info, | 526 const RequestInfo& info, |
| 527 RequestPriority priority, | 527 RequestPriority priority, |
| 528 const CompletionCallback& callback, | 528 const CompletionCallback& callback, |
| 529 AddressList* addresses) | 529 AddressList* addresses) |
| 530 : source_net_log_(source_net_log), | 530 : source_net_log_(source_net_log), |
| 531 info_(info), | 531 info_(info), |
| 532 priority_(priority), | 532 priority_(priority), |
| 533 job_(NULL), | 533 job_(nullptr), |
| 534 callback_(callback), | 534 callback_(callback), |
| 535 addresses_(addresses), | 535 addresses_(addresses), |
| 536 request_time_(base::TimeTicks::Now()) {} | 536 request_time_(base::TimeTicks::Now()) {} |
| 537 | 537 |
| 538 // Mark the request as canceled. | 538 // Mark the request as canceled. |
| 539 void MarkAsCanceled() { | 539 void MarkAsCanceled() { |
| 540 job_ = NULL; | 540 job_ = nullptr; |
| 541 addresses_ = NULL; | 541 addresses_ = nullptr; |
| 542 callback_.Reset(); | 542 callback_.Reset(); |
| 543 } | 543 } |
| 544 | 544 |
| 545 bool was_canceled() const { | 545 bool was_canceled() const { |
| 546 return callback_.is_null(); | 546 return callback_.is_null(); |
| 547 } | 547 } |
| 548 | 548 |
| 549 void set_job(Job* job) { | 549 void set_job(Job* job) { |
| 550 DCHECK(job); | 550 DCHECK(job); |
| 551 // Identify which job the request is waiting on. | 551 // Identify which job the request is waiting on. |
| (...skipping 743 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1295 source_net_log.source(), | 1295 source_net_log.source(), |
| 1296 &key_.hostname)); | 1296 &key_.hostname)); |
| 1297 } | 1297 } |
| 1298 | 1298 |
| 1299 ~Job() override { | 1299 ~Job() override { |
| 1300 if (is_running()) { | 1300 if (is_running()) { |
| 1301 // |resolver_| was destroyed with this Job still in flight. | 1301 // |resolver_| was destroyed with this Job still in flight. |
| 1302 // Clean-up, record in the log, but don't run any callbacks. | 1302 // Clean-up, record in the log, but don't run any callbacks. |
| 1303 if (is_proc_running()) { | 1303 if (is_proc_running()) { |
| 1304 proc_task_->Cancel(); | 1304 proc_task_->Cancel(); |
| 1305 proc_task_ = NULL; | 1305 proc_task_ = nullptr; |
| 1306 } | 1306 } |
| 1307 // Clean up now for nice NetLog. | 1307 // Clean up now for nice NetLog. |
| 1308 KillDnsTask(); | 1308 KillDnsTask(); |
| 1309 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, | 1309 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, |
| 1310 ERR_ABORTED); | 1310 ERR_ABORTED); |
| 1311 } else if (is_queued()) { | 1311 } else if (is_queued()) { |
| 1312 // |resolver_| was destroyed without running this Job. | 1312 // |resolver_| was destroyed without running this Job. |
| 1313 // TODO(szym): is there any benefit in having this distinction? | 1313 // TODO(szym): is there any benefit in having this distinction? |
| 1314 net_log_.AddEvent(NetLog::TYPE_CANCELLED); | 1314 net_log_.AddEvent(NetLog::TYPE_CANCELLED); |
| 1315 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB); | 1315 net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB); |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1636 } | 1636 } |
| 1637 | 1637 |
| 1638 // Called if DnsTask fails. It is posted from StartDnsTask, so Job may be | 1638 // Called if DnsTask fails. It is posted from StartDnsTask, so Job may be |
| 1639 // deleted before this callback. In this case dns_task is deleted as well, | 1639 // deleted before this callback. In this case dns_task is deleted as well, |
| 1640 // so we use it as indicator whether Job is still valid. | 1640 // so we use it as indicator whether Job is still valid. |
| 1641 void OnDnsTaskFailure(const base::WeakPtr<DnsTask>& dns_task, | 1641 void OnDnsTaskFailure(const base::WeakPtr<DnsTask>& dns_task, |
| 1642 base::TimeDelta duration, | 1642 base::TimeDelta duration, |
| 1643 int net_error) { | 1643 int net_error) { |
| 1644 DNS_HISTOGRAM("AsyncDNS.ResolveFail", duration); | 1644 DNS_HISTOGRAM("AsyncDNS.ResolveFail", duration); |
| 1645 | 1645 |
| 1646 if (dns_task == NULL) | 1646 if (!dns_task) |
| 1647 return; | 1647 return; |
| 1648 | 1648 |
| 1649 dns_task_error_ = net_error; | 1649 dns_task_error_ = net_error; |
| 1650 | 1650 |
| 1651 // TODO(szym): Run ServeFromHosts now if nsswitch.conf says so. | 1651 // TODO(szym): Run ServeFromHosts now if nsswitch.conf says so. |
| 1652 // http://crbug.com/117655 | 1652 // http://crbug.com/117655 |
| 1653 | 1653 |
| 1654 // TODO(szym): Some net errors indicate lack of connectivity. Starting | 1654 // TODO(szym): Some net errors indicate lack of connectivity. Starting |
| 1655 // ProcTask in that case is a waste of time. | 1655 // ProcTask in that case is a waste of time. |
| 1656 if (resolver_->fallback_to_proctask_) { | 1656 if (resolver_->fallback_to_proctask_) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1726 // to spawn one. Consequently, the job deletes itself when CompleteRequests | 1726 // to spawn one. Consequently, the job deletes itself when CompleteRequests |
| 1727 // is done. | 1727 // is done. |
| 1728 std::unique_ptr<Job> self_deleter(this); | 1728 std::unique_ptr<Job> self_deleter(this); |
| 1729 | 1729 |
| 1730 resolver_->RemoveJob(this); | 1730 resolver_->RemoveJob(this); |
| 1731 | 1731 |
| 1732 if (is_running()) { | 1732 if (is_running()) { |
| 1733 if (is_proc_running()) { | 1733 if (is_proc_running()) { |
| 1734 DCHECK(!is_queued()); | 1734 DCHECK(!is_queued()); |
| 1735 proc_task_->Cancel(); | 1735 proc_task_->Cancel(); |
| 1736 proc_task_ = NULL; | 1736 proc_task_ = nullptr; |
| 1737 } | 1737 } |
| 1738 KillDnsTask(); | 1738 KillDnsTask(); |
| 1739 | 1739 |
| 1740 // Signal dispatcher that a slot has opened. | 1740 // Signal dispatcher that a slot has opened. |
| 1741 resolver_->dispatcher_->OnJobFinished(); | 1741 resolver_->dispatcher_->OnJobFinished(); |
| 1742 } else if (is_queued()) { | 1742 } else if (is_queued()) { |
| 1743 resolver_->dispatcher_->Cancel(handle_); | 1743 resolver_->dispatcher_->Cancel(handle_); |
| 1744 handle_.Reset(); | 1744 handle_.Reset(); |
| 1745 } | 1745 } |
| 1746 | 1746 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1798 | 1798 |
| 1799 RequestPriority priority() const { | 1799 RequestPriority priority() const { |
| 1800 return priority_tracker_.highest_priority(); | 1800 return priority_tracker_.highest_priority(); |
| 1801 } | 1801 } |
| 1802 | 1802 |
| 1803 // Number of non-canceled requests in |requests_|. | 1803 // Number of non-canceled requests in |requests_|. |
| 1804 size_t num_active_requests() const { | 1804 size_t num_active_requests() const { |
| 1805 return priority_tracker_.total_count(); | 1805 return priority_tracker_.total_count(); |
| 1806 } | 1806 } |
| 1807 | 1807 |
| 1808 bool is_dns_running() const { | 1808 bool is_dns_running() const { return !!dns_task_; } |
|
Nico
2016/06/02 21:31:02
.
| |
| 1809 return dns_task_.get() != NULL; | |
| 1810 } | |
| 1811 | 1809 |
| 1812 bool is_proc_running() const { | 1810 bool is_proc_running() const { return !!proc_task_; } |
|
Nico
2016/06/02 21:31:02
.
| |
| 1813 return proc_task_.get() != NULL; | |
| 1814 } | |
| 1815 | 1811 |
| 1816 base::WeakPtr<HostResolverImpl> resolver_; | 1812 base::WeakPtr<HostResolverImpl> resolver_; |
| 1817 | 1813 |
| 1818 Key key_; | 1814 Key key_; |
| 1819 | 1815 |
| 1820 // Tracks the highest priority across |requests_|. | 1816 // Tracks the highest priority across |requests_|. |
| 1821 PriorityTracker priority_tracker_; | 1817 PriorityTracker priority_tracker_; |
| 1822 | 1818 |
| 1823 // Task runner where the HostResolverProc is invoked. | 1819 // Task runner where the HostResolverProc is invoked. |
| 1824 scoped_refptr<base::TaskRunner> worker_task_runner_; | 1820 scoped_refptr<base::TaskRunner> worker_task_runner_; |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2115 } | 2111 } |
| 2116 | 2112 |
| 2117 std::unique_ptr<base::Value> HostResolverImpl::GetDnsConfigAsValue() const { | 2113 std::unique_ptr<base::Value> HostResolverImpl::GetDnsConfigAsValue() const { |
| 2118 // Check if async DNS is disabled. | 2114 // Check if async DNS is disabled. |
| 2119 if (!dns_client_.get()) | 2115 if (!dns_client_.get()) |
| 2120 return nullptr; | 2116 return nullptr; |
| 2121 | 2117 |
| 2122 // Check if async DNS is enabled, but we currently have no configuration | 2118 // Check if async DNS is enabled, but we currently have no configuration |
| 2123 // for it. | 2119 // for it. |
| 2124 const DnsConfig* dns_config = dns_client_->GetConfig(); | 2120 const DnsConfig* dns_config = dns_client_->GetConfig(); |
| 2125 if (dns_config == NULL) | 2121 if (!dns_config) |
| 2126 return base::WrapUnique(new base::DictionaryValue()); | 2122 return base::WrapUnique(new base::DictionaryValue()); |
| 2127 | 2123 |
| 2128 return dns_config->ToValue(); | 2124 return dns_config->ToValue(); |
| 2129 } | 2125 } |
| 2130 | 2126 |
| 2131 bool HostResolverImpl::ResolveAsIP(const Key& key, | 2127 bool HostResolverImpl::ResolveAsIP(const Key& key, |
| 2132 const RequestInfo& info, | 2128 const RequestInfo& info, |
| 2133 const IPAddress* ip_address, | 2129 const IPAddress* ip_address, |
| 2134 int* net_error, | 2130 int* net_error, |
| 2135 AddressList* addresses) { | 2131 AddressList* addresses) { |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2459 if (self.get()) | 2455 if (self.get()) |
| 2460 TryServingAllJobsFromHosts(); | 2456 TryServingAllJobsFromHosts(); |
| 2461 } | 2457 } |
| 2462 } | 2458 } |
| 2463 | 2459 |
| 2464 bool HostResolverImpl::HaveDnsConfig() const { | 2460 bool HostResolverImpl::HaveDnsConfig() const { |
| 2465 // Use DnsClient only if it's fully configured and there is no override by | 2461 // Use DnsClient only if it's fully configured and there is no override by |
| 2466 // ScopedDefaultHostResolverProc. | 2462 // ScopedDefaultHostResolverProc. |
| 2467 // The alternative is to use NetworkChangeNotifier to override DnsConfig, | 2463 // The alternative is to use NetworkChangeNotifier to override DnsConfig, |
| 2468 // but that would introduce construction order requirements for NCN and SDHRP. | 2464 // but that would introduce construction order requirements for NCN and SDHRP. |
| 2469 return (dns_client_.get() != NULL) && (dns_client_->GetConfig() != NULL) && | 2465 return dns_client_ && dns_client_->GetConfig() && |
| 2470 !(proc_params_.resolver_proc.get() == NULL && | 2466 (proc_params_.resolver_proc || !HostResolverProc::GetDefault()); |
| 2471 HostResolverProc::GetDefault() != NULL); | |
| 2472 } | 2467 } |
| 2473 | 2468 |
| 2474 void HostResolverImpl::OnDnsTaskResolve(int net_error) { | 2469 void HostResolverImpl::OnDnsTaskResolve(int net_error) { |
| 2475 DCHECK(dns_client_); | 2470 DCHECK(dns_client_); |
| 2476 if (net_error == OK) { | 2471 if (net_error == OK) { |
| 2477 num_dns_failures_ = 0; | 2472 num_dns_failures_ = 0; |
| 2478 return; | 2473 return; |
| 2479 } | 2474 } |
| 2480 ++num_dns_failures_; | 2475 ++num_dns_failures_; |
| 2481 if (num_dns_failures_ < kMaximumDnsFailures) | 2476 if (num_dns_failures_ < kMaximumDnsFailures) |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 2504 dns_client_->SetConfig(dns_config); | 2499 dns_client_->SetConfig(dns_config); |
| 2505 num_dns_failures_ = 0; | 2500 num_dns_failures_ = 0; |
| 2506 if (dns_client_->GetConfig()) | 2501 if (dns_client_->GetConfig()) |
| 2507 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); | 2502 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); |
| 2508 } | 2503 } |
| 2509 | 2504 |
| 2510 AbortDnsTasks(); | 2505 AbortDnsTasks(); |
| 2511 } | 2506 } |
| 2512 | 2507 |
| 2513 } // namespace net | 2508 } // namespace net |
| OLD | NEW |