Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(296)

Side by Side Diff: net/dns/host_resolver_impl.cc

Issue 238433003: Provide Shill IP Address to myIpAddress() (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase + feedback Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/dns/host_resolver_impl.h ('k') | net/dns/host_resolver_impl_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <Winsock2.h> 8 #include <Winsock2.h>
9 #elif defined(OS_POSIX) 9 #elif defined(OS_POSIX)
10 #include <netdb.h> 10 #include <netdb.h>
(...skipping 1240 matching lines...) Expand 10 before | Expand all | Expand 10 after
1251 req->info()); 1251 req->info());
1252 } 1252 }
1253 } 1253 }
1254 1254
1255 // Add this job to the dispatcher. If "at_head" is true, adds at the front 1255 // Add this job to the dispatcher. If "at_head" is true, adds at the front
1256 // of the queue. 1256 // of the queue.
1257 void Schedule(bool at_head) { 1257 void Schedule(bool at_head) {
1258 DCHECK(!is_queued()); 1258 DCHECK(!is_queued());
1259 PrioritizedDispatcher::Handle handle; 1259 PrioritizedDispatcher::Handle handle;
1260 if (!at_head) { 1260 if (!at_head) {
1261 handle = resolver_->dispatcher_.Add(this, priority()); 1261 handle = resolver_->dispatcher_->Add(this, priority());
1262 } else { 1262 } else {
1263 handle = resolver_->dispatcher_.AddAtHead(this, priority()); 1263 handle = resolver_->dispatcher_->AddAtHead(this, priority());
1264 } 1264 }
1265 // The dispatcher could have started |this| in the above call to Add, which 1265 // The dispatcher could have started |this| in the above call to Add, which
1266 // could have called Schedule again. In that case |handle| will be null, 1266 // could have called Schedule again. In that case |handle| will be null,
1267 // but |handle_| may have been set by the other nested call to Schedule. 1267 // but |handle_| may have been set by the other nested call to Schedule.
1268 if (!handle.is_null()) { 1268 if (!handle.is_null()) {
1269 DCHECK(handle_.is_null()); 1269 DCHECK(handle_.is_null());
1270 handle_ = handle; 1270 handle_ = handle;
1271 } 1271 }
1272 } 1272 }
1273 1273
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
1392 } 1392 }
1393 } 1393 }
1394 1394
1395 // Reduce the number of job slots occupied and queued in the dispatcher 1395 // Reduce the number of job slots occupied and queued in the dispatcher
1396 // to one. If the second Job slot is queued in the dispatcher, cancels the 1396 // to one. If the second Job slot is queued in the dispatcher, cancels the
1397 // queued job. Otherwise, the second Job has been started by the 1397 // queued job. Otherwise, the second Job has been started by the
1398 // PrioritizedDispatcher, so signals it is complete. 1398 // PrioritizedDispatcher, so signals it is complete.
1399 void ReduceToOneJobSlot() { 1399 void ReduceToOneJobSlot() {
1400 DCHECK_GE(num_occupied_job_slots_, 1u); 1400 DCHECK_GE(num_occupied_job_slots_, 1u);
1401 if (is_queued()) { 1401 if (is_queued()) {
1402 resolver_->dispatcher_.Cancel(handle_); 1402 resolver_->dispatcher_->Cancel(handle_);
1403 handle_.Reset(); 1403 handle_.Reset();
1404 } else if (num_occupied_job_slots_ > 1) { 1404 } else if (num_occupied_job_slots_ > 1) {
1405 resolver_->dispatcher_.OnJobFinished(); 1405 resolver_->dispatcher_->OnJobFinished();
1406 --num_occupied_job_slots_; 1406 --num_occupied_job_slots_;
1407 } 1407 }
1408 DCHECK_EQ(1u, num_occupied_job_slots_); 1408 DCHECK_EQ(1u, num_occupied_job_slots_);
1409 } 1409 }
1410 1410
1411 void UpdatePriority() { 1411 void UpdatePriority() {
1412 if (is_queued()) { 1412 if (is_queued()) {
1413 if (priority() != static_cast<RequestPriority>(handle_.priority())) 1413 if (priority() != static_cast<RequestPriority>(handle_.priority()))
1414 priority_change_time_ = base::TimeTicks::Now(); 1414 priority_change_time_ = base::TimeTicks::Now();
1415 handle_ = resolver_->dispatcher_.ChangePriority(handle_, priority()); 1415 handle_ = resolver_->dispatcher_->ChangePriority(handle_, priority());
1416 } 1416 }
1417 } 1417 }
1418 1418
1419 AddressList MakeAddressListForRequest(const AddressList& list) const { 1419 AddressList MakeAddressListForRequest(const AddressList& list) const {
1420 if (requests_.empty()) 1420 if (requests_.empty())
1421 return list; 1421 return list;
1422 return AddressList::CopyWithPort(list, requests_.front()->info().port()); 1422 return AddressList::CopyWithPort(list, requests_.front()->info().port());
1423 } 1423 }
1424 1424
1425 // PriorityDispatch::Job: 1425 // PriorityDispatch::Job:
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
1652 1652
1653 if (is_running()) { 1653 if (is_running()) {
1654 if (is_proc_running()) { 1654 if (is_proc_running()) {
1655 DCHECK(!is_queued()); 1655 DCHECK(!is_queued());
1656 proc_task_->Cancel(); 1656 proc_task_->Cancel();
1657 proc_task_ = NULL; 1657 proc_task_ = NULL;
1658 } 1658 }
1659 KillDnsTask(); 1659 KillDnsTask();
1660 1660
1661 // Signal dispatcher that a slot has opened. 1661 // Signal dispatcher that a slot has opened.
1662 resolver_->dispatcher_.OnJobFinished(); 1662 resolver_->dispatcher_->OnJobFinished();
1663 } else if (is_queued()) { 1663 } else if (is_queued()) {
1664 resolver_->dispatcher_.Cancel(handle_); 1664 resolver_->dispatcher_->Cancel(handle_);
1665 handle_.Reset(); 1665 handle_.Reset();
1666 } 1666 }
1667 1667
1668 if (num_active_requests() == 0) { 1668 if (num_active_requests() == 0) {
1669 net_log_.AddEvent(NetLog::TYPE_CANCELLED); 1669 net_log_.AddEvent(NetLog::TYPE_CANCELLED);
1670 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, 1670 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
1671 OK); 1671 OK);
1672 return; 1672 return;
1673 } 1673 }
1674 1674
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
1776 1776
1777 //----------------------------------------------------------------------------- 1777 //-----------------------------------------------------------------------------
1778 1778
1779 HostResolverImpl::ProcTaskParams::ProcTaskParams( 1779 HostResolverImpl::ProcTaskParams::ProcTaskParams(
1780 HostResolverProc* resolver_proc, 1780 HostResolverProc* resolver_proc,
1781 size_t max_retry_attempts) 1781 size_t max_retry_attempts)
1782 : resolver_proc(resolver_proc), 1782 : resolver_proc(resolver_proc),
1783 max_retry_attempts(max_retry_attempts), 1783 max_retry_attempts(max_retry_attempts),
1784 unresponsive_delay(base::TimeDelta::FromMilliseconds(6000)), 1784 unresponsive_delay(base::TimeDelta::FromMilliseconds(6000)),
1785 retry_factor(2) { 1785 retry_factor(2) {
1786 // Maximum of 4 retry attempts for host resolution.
1787 static const size_t kDefaultMaxRetryAttempts = 4u;
1788 if (max_retry_attempts == HostResolver::kDefaultRetryAttempts)
1789 max_retry_attempts = kDefaultMaxRetryAttempts;
1786 } 1790 }
1787 1791
1788 HostResolverImpl::ProcTaskParams::~ProcTaskParams() {} 1792 HostResolverImpl::ProcTaskParams::~ProcTaskParams() {}
1789 1793
1790 HostResolverImpl::HostResolverImpl( 1794 HostResolverImpl::HostResolverImpl(const Options& options, NetLog* net_log)
1791 scoped_ptr<HostCache> cache, 1795 : max_queued_jobs_(0),
1792 const PrioritizedDispatcher::Limits& job_limits, 1796 proc_params_(NULL, options.max_retry_attempts),
1793 const ProcTaskParams& proc_params,
1794 NetLog* net_log)
1795 : cache_(cache.Pass()),
1796 dispatcher_(job_limits),
1797 max_queued_jobs_(job_limits.total_jobs * 100u),
1798 proc_params_(proc_params),
1799 net_log_(net_log), 1797 net_log_(net_log),
1800 default_address_family_(ADDRESS_FAMILY_UNSPECIFIED), 1798 default_address_family_(ADDRESS_FAMILY_UNSPECIFIED),
1801 received_dns_config_(false), 1799 received_dns_config_(false),
1802 num_dns_failures_(0), 1800 num_dns_failures_(0),
1803 probe_ipv6_support_(true), 1801 probe_ipv6_support_(true),
1804 use_local_ipv6_(false), 1802 use_local_ipv6_(false),
1805 resolved_known_ipv6_hostname_(false), 1803 resolved_known_ipv6_hostname_(false),
1806 additional_resolver_flags_(0), 1804 additional_resolver_flags_(0),
1807 fallback_to_proctask_(true), 1805 fallback_to_proctask_(true),
1808 weak_ptr_factory_(this), 1806 weak_ptr_factory_(this),
1809 probe_weak_ptr_factory_(this) { 1807 probe_weak_ptr_factory_(this) {
1808 if (options.enable_caching)
1809 cache_ = HostCache::CreateDefaultCache();
1810 1810
1811 DCHECK_GE(dispatcher_.num_priorities(), static_cast<size_t>(NUM_PRIORITIES)); 1811 PrioritizedDispatcher::Limits job_limits = options.GetDispatcherLimits();
1812 dispatcher_.reset(new PrioritizedDispatcher(job_limits));
1813 max_queued_jobs_ = job_limits.total_jobs * 100u;
1812 1814
1813 // Maximum of 4 retry attempts for host resolution. 1815 DCHECK_GE(dispatcher_->num_priorities(), static_cast<size_t>(NUM_PRIORITIES));
1814 static const size_t kDefaultMaxRetryAttempts = 4u;
1815
1816 if (proc_params_.max_retry_attempts == HostResolver::kDefaultRetryAttempts)
1817 proc_params_.max_retry_attempts = kDefaultMaxRetryAttempts;
1818 1816
1819 #if defined(OS_WIN) 1817 #if defined(OS_WIN)
1820 EnsureWinsockInit(); 1818 EnsureWinsockInit();
1821 #endif 1819 #endif
1822 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) 1820 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
1823 new LoopbackProbeJob(weak_ptr_factory_.GetWeakPtr()); 1821 new LoopbackProbeJob(weak_ptr_factory_.GetWeakPtr());
1824 #endif 1822 #endif
1825 NetworkChangeNotifier::AddIPAddressObserver(this); 1823 NetworkChangeNotifier::AddIPAddressObserver(this);
1826 NetworkChangeNotifier::AddDNSObserver(this); 1824 NetworkChangeNotifier::AddDNSObserver(this);
1827 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \ 1825 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \
1828 !defined(OS_ANDROID) 1826 !defined(OS_ANDROID)
1829 EnsureDnsReloaderInit(); 1827 EnsureDnsReloaderInit();
1830 #endif 1828 #endif
1831 1829
1832 { 1830 {
1833 DnsConfig dns_config; 1831 DnsConfig dns_config;
1834 NetworkChangeNotifier::GetDnsConfig(&dns_config); 1832 NetworkChangeNotifier::GetDnsConfig(&dns_config);
1835 received_dns_config_ = dns_config.IsValid(); 1833 received_dns_config_ = dns_config.IsValid();
1836 // Conservatively assume local IPv6 is needed when DnsConfig is not valid. 1834 // Conservatively assume local IPv6 is needed when DnsConfig is not valid.
1837 use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6; 1835 use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6;
1838 } 1836 }
1839 1837
1840 fallback_to_proctask_ = !ConfigureAsyncDnsNoFallbackFieldTrial(); 1838 fallback_to_proctask_ = !ConfigureAsyncDnsNoFallbackFieldTrial();
1841 } 1839 }
1842 1840
1843 HostResolverImpl::~HostResolverImpl() { 1841 HostResolverImpl::~HostResolverImpl() {
1844 // Prevent the dispatcher from starting new jobs. 1842 // Prevent the dispatcher from starting new jobs.
1845 dispatcher_.SetLimitsToZero(); 1843 dispatcher_->SetLimitsToZero();
1846 // It's now safe for Jobs to call KillDsnTask on destruction, because 1844 // It's now safe for Jobs to call KillDsnTask on destruction, because
1847 // OnJobComplete will not start any new jobs. 1845 // OnJobComplete will not start any new jobs.
1848 STLDeleteValues(&jobs_); 1846 STLDeleteValues(&jobs_);
1849 1847
1850 NetworkChangeNotifier::RemoveIPAddressObserver(this); 1848 NetworkChangeNotifier::RemoveIPAddressObserver(this);
1851 NetworkChangeNotifier::RemoveDNSObserver(this); 1849 NetworkChangeNotifier::RemoveDNSObserver(this);
1852 } 1850 }
1853 1851
1854 void HostResolverImpl::SetMaxQueuedJobs(size_t value) { 1852 void HostResolverImpl::SetMaxQueuedJobs(size_t value) {
1855 DCHECK_EQ(0u, dispatcher_.num_queued_jobs()); 1853 DCHECK_EQ(0u, dispatcher_->num_queued_jobs());
1856 DCHECK_GT(value, 0u); 1854 DCHECK_GT(value, 0u);
1857 max_queued_jobs_ = value; 1855 max_queued_jobs_ = value;
1858 } 1856 }
1859 1857
1860 int HostResolverImpl::Resolve(const RequestInfo& info, 1858 int HostResolverImpl::Resolve(const RequestInfo& info,
1861 RequestPriority priority, 1859 RequestPriority priority,
1862 AddressList* addresses, 1860 AddressList* addresses,
1863 const CompletionCallback& callback, 1861 const CompletionCallback& callback,
1864 RequestHandle* out_req, 1862 RequestHandle* out_req,
1865 const BoundNetLog& source_net_log) { 1863 const BoundNetLog& source_net_log) {
(...skipping 27 matching lines...) Expand all
1893 // calling "getaddrinfo(hostname)" on a worker thread. 1891 // calling "getaddrinfo(hostname)" on a worker thread.
1894 1892
1895 JobMap::iterator jobit = jobs_.find(key); 1893 JobMap::iterator jobit = jobs_.find(key);
1896 Job* job; 1894 Job* job;
1897 if (jobit == jobs_.end()) { 1895 if (jobit == jobs_.end()) {
1898 job = 1896 job =
1899 new Job(weak_ptr_factory_.GetWeakPtr(), key, priority, request_net_log); 1897 new Job(weak_ptr_factory_.GetWeakPtr(), key, priority, request_net_log);
1900 job->Schedule(false); 1898 job->Schedule(false);
1901 1899
1902 // Check for queue overflow. 1900 // Check for queue overflow.
1903 if (dispatcher_.num_queued_jobs() > max_queued_jobs_) { 1901 if (dispatcher_->num_queued_jobs() > max_queued_jobs_) {
1904 Job* evicted = static_cast<Job*>(dispatcher_.EvictOldestLowest()); 1902 Job* evicted = static_cast<Job*>(dispatcher_->EvictOldestLowest());
1905 DCHECK(evicted); 1903 DCHECK(evicted);
1906 evicted->OnEvicted(); // Deletes |evicted|. 1904 evicted->OnEvicted(); // Deletes |evicted|.
1907 if (evicted == job) { 1905 if (evicted == job) {
1908 rv = ERR_HOST_RESOLVER_QUEUE_TOO_LARGE; 1906 rv = ERR_HOST_RESOLVER_QUEUE_TOO_LARGE;
1909 LogFinishRequest(source_net_log, request_net_log, info, rv); 1907 LogFinishRequest(source_net_log, request_net_log, info, rv);
1910 return rv; 1908 return rv;
1911 } 1909 }
1912 } 1910 }
1913 jobs_.insert(jobit, std::make_pair(key, job)); 1911 jobs_.insert(jobit, std::make_pair(key, job));
1914 } else { 1912 } else {
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
2196 } else { 2194 } else {
2197 DCHECK(job->is_queued()); 2195 DCHECK(job->is_queued());
2198 ++it; 2196 ++it;
2199 } 2197 }
2200 } 2198 }
2201 2199
2202 // Pause the dispatcher so it won't start any new dispatcher jobs while 2200 // Pause the dispatcher so it won't start any new dispatcher jobs while
2203 // aborting the old ones. This is needed so that it won't start the second 2201 // aborting the old ones. This is needed so that it won't start the second
2204 // DnsTransaction for a job in |jobs_to_abort| if the DnsConfig just became 2202 // DnsTransaction for a job in |jobs_to_abort| if the DnsConfig just became
2205 // invalid. 2203 // invalid.
2206 PrioritizedDispatcher::Limits limits = dispatcher_.GetLimits(); 2204 PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
2207 dispatcher_.SetLimits( 2205 dispatcher_->SetLimits(
2208 PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0)); 2206 PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
2209 2207
2210 // Life check to bail once |this| is deleted. 2208 // Life check to bail once |this| is deleted.
2211 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr(); 2209 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr();
2212 2210
2213 // Then Abort them. 2211 // Then Abort them.
2214 for (size_t i = 0; self.get() && i < jobs_to_abort.size(); ++i) { 2212 for (size_t i = 0; self.get() && i < jobs_to_abort.size(); ++i) {
2215 jobs_to_abort[i]->Abort(); 2213 jobs_to_abort[i]->Abort();
2216 jobs_to_abort[i] = NULL; 2214 jobs_to_abort[i] = NULL;
2217 } 2215 }
2218 2216
2219 if (self) 2217 if (self)
2220 dispatcher_.SetLimits(limits); 2218 dispatcher_->SetLimits(limits);
2221 } 2219 }
2222 2220
2223 void HostResolverImpl::AbortDnsTasks() { 2221 void HostResolverImpl::AbortDnsTasks() {
2224 // Pause the dispatcher so it won't start any new dispatcher jobs while 2222 // Pause the dispatcher so it won't start any new dispatcher jobs while
2225 // aborting the old ones. This is needed so that it won't start the second 2223 // aborting the old ones. This is needed so that it won't start the second
2226 // DnsTransaction for a job if the DnsConfig just changed. 2224 // DnsTransaction for a job if the DnsConfig just changed.
2227 PrioritizedDispatcher::Limits limits = dispatcher_.GetLimits(); 2225 PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits();
2228 dispatcher_.SetLimits( 2226 dispatcher_->SetLimits(
2229 PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0)); 2227 PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0));
2230 2228
2231 for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ++it) 2229 for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ++it)
2232 it->second->AbortDnsTask(); 2230 it->second->AbortDnsTask();
2233 dispatcher_.SetLimits(limits); 2231 dispatcher_->SetLimits(limits);
2234 } 2232 }
2235 2233
2236 void HostResolverImpl::TryServingAllJobsFromHosts() { 2234 void HostResolverImpl::TryServingAllJobsFromHosts() {
2237 if (!HaveDnsConfig()) 2235 if (!HaveDnsConfig())
2238 return; 2236 return;
2239 2237
2240 // TODO(szym): Do not do this if nsswitch.conf instructs not to. 2238 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
2241 // http://crbug.com/117655 2239 // http://crbug.com/117655
2242 2240
2243 // Life check to bail once |this| is deleted. 2241 // Life check to bail once |this| is deleted.
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
2352 dns_client_->SetConfig(dns_config); 2350 dns_client_->SetConfig(dns_config);
2353 num_dns_failures_ = 0; 2351 num_dns_failures_ = 0;
2354 if (dns_client_->GetConfig()) 2352 if (dns_client_->GetConfig())
2355 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); 2353 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
2356 } 2354 }
2357 2355
2358 AbortDnsTasks(); 2356 AbortDnsTasks();
2359 } 2357 }
2360 2358
2361 } // namespace net 2359 } // namespace net
OLDNEW
« no previous file with comments | « net/dns/host_resolver_impl.h ('k') | net/dns/host_resolver_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698