| 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/base/host_resolver_impl.h" | 5 #include "net/base/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 1251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1262 } else { | 1262 } else { |
| 1263 // If we were called from a Request's callback within CompleteRequests, | 1263 // If we were called from a Request's callback within CompleteRequests, |
| 1264 // that Request could not have been cancelled, so num_active_requests() | 1264 // that Request could not have been cancelled, so num_active_requests() |
| 1265 // could not be 0. Therefore, we are not in CompleteRequests(). | 1265 // could not be 0. Therefore, we are not in CompleteRequests(). |
| 1266 CompleteRequestsWithError(OK /* cancelled */); | 1266 CompleteRequestsWithError(OK /* cancelled */); |
| 1267 } | 1267 } |
| 1268 } | 1268 } |
| 1269 | 1269 |
| 1270 // Called from AbortAllInProgressJobs. Completes all requests as aborted | 1270 // Called from AbortAllInProgressJobs. Completes all requests as aborted |
| 1271 // and destroys the job. | 1271 // and destroys the job. |
| 1272 void Abort() { | 1272 void Abort(int error) { |
| 1273 DCHECK(is_running()); | 1273 DCHECK(is_running()); |
| 1274 CompleteRequestsWithError(ERR_ABORTED); | 1274 CompleteRequestsWithError(error); |
| 1275 } | 1275 } |
| 1276 | 1276 |
| 1277 // If DnsTask present, abort it and fall back to ProcTask. | 1277 // If DnsTask present, abort it and fall back to ProcTask. |
| 1278 void AbortDnsTask() { | 1278 void AbortDnsTask() { |
| 1279 if (dns_task_) { | 1279 if (dns_task_) { |
| 1280 dns_task_.reset(); | 1280 dns_task_.reset(); |
| 1281 dns_task_error_ = OK; | 1281 dns_task_error_ = OK; |
| 1282 StartProcTask(); | 1282 StartProcTask(); |
| 1283 } | 1283 } |
| 1284 } | 1284 } |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1525 DCHECK(!requests_.empty()); | 1525 DCHECK(!requests_.empty()); |
| 1526 | 1526 |
| 1527 if (entry.error == OK) { | 1527 if (entry.error == OK) { |
| 1528 // Record this histogram here, when we know the system has a valid DNS | 1528 // Record this histogram here, when we know the system has a valid DNS |
| 1529 // configuration. | 1529 // configuration. |
| 1530 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig", | 1530 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig", |
| 1531 resolver_->received_dns_config_); | 1531 resolver_->received_dns_config_); |
| 1532 } | 1532 } |
| 1533 | 1533 |
| 1534 bool did_complete = (entry.error != ERR_ABORTED) && | 1534 bool did_complete = (entry.error != ERR_ABORTED) && |
| 1535 (entry.error != ERR_NETWORK_CHANGED) && |
| 1535 (entry.error != ERR_HOST_RESOLVER_QUEUE_TOO_LARGE); | 1536 (entry.error != ERR_HOST_RESOLVER_QUEUE_TOO_LARGE); |
| 1536 if (did_complete) | 1537 if (did_complete) |
| 1537 resolver_->CacheResult(key_, entry, ttl); | 1538 resolver_->CacheResult(key_, entry, ttl); |
| 1538 | 1539 |
| 1539 // Complete all of the requests that were attached to the job. | 1540 // Complete all of the requests that were attached to the job. |
| 1540 for (RequestsList::const_iterator it = requests_.begin(); | 1541 for (RequestsList::const_iterator it = requests_.begin(); |
| 1541 it != requests_.end(); ++it) { | 1542 it != requests_.end(); ++it) { |
| 1542 Request* req = *it; | 1543 Request* req = *it; |
| 1543 | 1544 |
| 1544 if (req->was_canceled()) | 1545 if (req->was_canceled()) |
| (...skipping 480 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2025 AddressFamily effective_address_family = info.address_family(); | 2026 AddressFamily effective_address_family = info.address_family(); |
| 2026 if (effective_address_family == ADDRESS_FAMILY_UNSPECIFIED && | 2027 if (effective_address_family == ADDRESS_FAMILY_UNSPECIFIED && |
| 2027 default_address_family_ != ADDRESS_FAMILY_UNSPECIFIED) { | 2028 default_address_family_ != ADDRESS_FAMILY_UNSPECIFIED) { |
| 2028 effective_address_family = default_address_family_; | 2029 effective_address_family = default_address_family_; |
| 2029 if (ipv6_probe_monitoring_) | 2030 if (ipv6_probe_monitoring_) |
| 2030 effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; | 2031 effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; |
| 2031 } | 2032 } |
| 2032 return Key(info.hostname(), effective_address_family, effective_flags); | 2033 return Key(info.hostname(), effective_address_family, effective_flags); |
| 2033 } | 2034 } |
| 2034 | 2035 |
| 2035 void HostResolverImpl::AbortAllInProgressJobs() { | 2036 void HostResolverImpl::AbortAllInProgressJobs(int error) { |
| 2036 // In Abort, a Request callback could spawn new Jobs with matching keys, so | 2037 // In Abort, a Request callback could spawn new Jobs with matching keys, so |
| 2037 // first collect and remove all running jobs from |jobs_|. | 2038 // first collect and remove all running jobs from |jobs_|. |
| 2038 ScopedVector<Job> jobs_to_abort; | 2039 ScopedVector<Job> jobs_to_abort; |
| 2039 for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ) { | 2040 for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ) { |
| 2040 Job* job = it->second; | 2041 Job* job = it->second; |
| 2041 if (job->is_running()) { | 2042 if (job->is_running()) { |
| 2042 jobs_to_abort.push_back(job); | 2043 jobs_to_abort.push_back(job); |
| 2043 jobs_.erase(it++); | 2044 jobs_.erase(it++); |
| 2044 } else { | 2045 } else { |
| 2045 DCHECK(job->is_queued()); | 2046 DCHECK(job->is_queued()); |
| 2046 ++it; | 2047 ++it; |
| 2047 } | 2048 } |
| 2048 } | 2049 } |
| 2049 | 2050 |
| 2050 // Check if no dispatcher slots leaked out. | 2051 // Check if no dispatcher slots leaked out. |
| 2051 DCHECK_EQ(dispatcher_.num_running_jobs(), jobs_to_abort.size()); | 2052 DCHECK_EQ(dispatcher_.num_running_jobs(), jobs_to_abort.size()); |
| 2052 | 2053 |
| 2053 // Life check to bail once |this| is deleted. | 2054 // Life check to bail once |this| is deleted. |
| 2054 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr(); | 2055 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr(); |
| 2055 | 2056 |
| 2056 // Then Abort them. | 2057 // Then Abort them. |
| 2057 for (size_t i = 0; self && i < jobs_to_abort.size(); ++i) { | 2058 for (size_t i = 0; self && i < jobs_to_abort.size(); ++i) { |
| 2058 jobs_to_abort[i]->Abort(); | 2059 jobs_to_abort[i]->Abort(error); |
| 2059 jobs_to_abort[i] = NULL; | 2060 jobs_to_abort[i] = NULL; |
| 2060 } | 2061 } |
| 2061 } | 2062 } |
| 2062 | 2063 |
| 2063 void HostResolverImpl::TryServingAllJobsFromHosts() { | 2064 void HostResolverImpl::TryServingAllJobsFromHosts() { |
| 2064 if (!HaveDnsConfig()) | 2065 if (!HaveDnsConfig()) |
| 2065 return; | 2066 return; |
| 2066 | 2067 |
| 2067 // TODO(szym): Do not do this if nsswitch.conf instructs not to. | 2068 // TODO(szym): Do not do this if nsswitch.conf instructs not to. |
| 2068 // http://crbug.com/117655 | 2069 // http://crbug.com/117655 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 2081 void HostResolverImpl::OnIPAddressChanged() { | 2082 void HostResolverImpl::OnIPAddressChanged() { |
| 2082 // Abandon all ProbeJobs. | 2083 // Abandon all ProbeJobs. |
| 2083 probe_weak_ptr_factory_.InvalidateWeakPtrs(); | 2084 probe_weak_ptr_factory_.InvalidateWeakPtrs(); |
| 2084 if (cache_.get()) | 2085 if (cache_.get()) |
| 2085 cache_->clear(); | 2086 cache_->clear(); |
| 2086 if (ipv6_probe_monitoring_) | 2087 if (ipv6_probe_monitoring_) |
| 2087 new IPv6ProbeJob(probe_weak_ptr_factory_.GetWeakPtr(), net_log_); | 2088 new IPv6ProbeJob(probe_weak_ptr_factory_.GetWeakPtr(), net_log_); |
| 2088 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 2089 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 2089 new LoopbackProbeJob(probe_weak_ptr_factory_.GetWeakPtr()); | 2090 new LoopbackProbeJob(probe_weak_ptr_factory_.GetWeakPtr()); |
| 2090 #endif | 2091 #endif |
| 2091 AbortAllInProgressJobs(); | 2092 AbortAllInProgressJobs(ERR_NETWORK_CHANGED); |
| 2092 // |this| may be deleted inside AbortAllInProgressJobs(). | 2093 // |this| may be deleted inside AbortAllInProgressJobs(). |
| 2093 } | 2094 } |
| 2094 | 2095 |
| 2095 void HostResolverImpl::OnDNSChanged() { | 2096 void HostResolverImpl::OnDNSChanged() { |
| 2096 DnsConfig dns_config; | 2097 DnsConfig dns_config; |
| 2097 NetworkChangeNotifier::GetDnsConfig(&dns_config); | 2098 NetworkChangeNotifier::GetDnsConfig(&dns_config); |
| 2098 if (net_log_) { | 2099 if (net_log_) { |
| 2099 net_log_->AddGlobalEntry( | 2100 net_log_->AddGlobalEntry( |
| 2100 NetLog::TYPE_DNS_CONFIG_CHANGED, | 2101 NetLog::TYPE_DNS_CONFIG_CHANGED, |
| 2101 base::Bind(&NetLogDnsConfigCallback, &dns_config)); | 2102 base::Bind(&NetLogDnsConfigCallback, &dns_config)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 2119 // as NSCD's cache should be dropped automatically by the OS when | 2120 // as NSCD's cache should be dropped automatically by the OS when |
| 2120 // resolv.conf changes so we don't need to do anything to clear that cache. | 2121 // resolv.conf changes so we don't need to do anything to clear that cache. |
| 2121 if (cache_.get()) | 2122 if (cache_.get()) |
| 2122 cache_->clear(); | 2123 cache_->clear(); |
| 2123 | 2124 |
| 2124 // Life check to bail once |this| is deleted. | 2125 // Life check to bail once |this| is deleted. |
| 2125 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr(); | 2126 base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr(); |
| 2126 | 2127 |
| 2127 // Existing jobs will have been sent to the original server so they need to | 2128 // Existing jobs will have been sent to the original server so they need to |
| 2128 // be aborted. | 2129 // be aborted. |
| 2129 AbortAllInProgressJobs(); | 2130 AbortAllInProgressJobs(ERR_NETWORK_CHANGED); |
| 2130 | 2131 |
| 2131 // |this| may be deleted inside AbortAllInProgressJobs(). | 2132 // |this| may be deleted inside AbortAllInProgressJobs(). |
| 2132 if (self) | 2133 if (self) |
| 2133 TryServingAllJobsFromHosts(); | 2134 TryServingAllJobsFromHosts(); |
| 2134 } | 2135 } |
| 2135 | 2136 |
| 2136 bool HostResolverImpl::HaveDnsConfig() const { | 2137 bool HostResolverImpl::HaveDnsConfig() const { |
| 2137 return (dns_client_.get() != NULL) && (dns_client_->GetConfig() != NULL); | 2138 return (dns_client_.get() != NULL) && (dns_client_->GetConfig() != NULL); |
| 2138 } | 2139 } |
| 2139 | 2140 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 2165 } | 2166 } |
| 2166 DnsConfig dns_config; | 2167 DnsConfig dns_config; |
| 2167 NetworkChangeNotifier::GetDnsConfig(&dns_config); | 2168 NetworkChangeNotifier::GetDnsConfig(&dns_config); |
| 2168 dns_client_->SetConfig(dns_config); | 2169 dns_client_->SetConfig(dns_config); |
| 2169 num_dns_failures_ = 0; | 2170 num_dns_failures_ = 0; |
| 2170 if (dns_config.IsValid()) | 2171 if (dns_config.IsValid()) |
| 2171 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); | 2172 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); |
| 2172 } | 2173 } |
| 2173 | 2174 |
| 2174 } // namespace net | 2175 } // namespace net |
| OLD | NEW |