| 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 "chrome/browser/net/dns_probe_service.h" | 5 #include "chrome/browser/net/dns_probe_service.h" |
| 6 | 6 |
| 7 #include "base/metrics/field_trial.h" | 7 #include "base/metrics/field_trial.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "chrome/browser/net/dns_probe_job.h" | |
| 11 #include "chrome/common/net/net_error_info.h" | |
| 12 #include "net/base/ip_endpoint.h" | 10 #include "net/base/ip_endpoint.h" |
| 13 #include "net/base/net_util.h" | 11 #include "net/base/net_util.h" |
| 14 #include "net/dns/dns_client.h" | 12 #include "net/dns/dns_client.h" |
| 15 #include "net/dns/dns_config_service.h" | 13 #include "net/dns/dns_config_service.h" |
| 16 #include "net/dns/dns_protocol.h" | 14 #include "net/dns/dns_protocol.h" |
| 17 | 15 |
| 18 using base::FieldTrialList; | 16 using base::FieldTrialList; |
| 19 using base::StringToInt; | 17 using base::StringToInt; |
| 20 using chrome_common_net::DnsProbeResult; | 18 using chrome_common_net::DnsProbeStatus; |
| 21 using net::DnsClient; | 19 using net::DnsClient; |
| 22 using net::DnsConfig; | 20 using net::DnsConfig; |
| 23 using net::IPAddressNumber; | 21 using net::IPAddressNumber; |
| 24 using net::IPEndPoint; | 22 using net::IPEndPoint; |
| 25 using net::ParseIPLiteralToNumber; | 23 using net::ParseIPLiteralToNumber; |
| 26 using net::NetworkChangeNotifier; | 24 using net::NetworkChangeNotifier; |
| 27 | 25 |
| 28 namespace chrome_browser_net { | 26 namespace chrome_browser_net { |
| 29 | 27 |
| 30 namespace { | 28 namespace { |
| 31 | 29 |
| 32 // How long the DnsProbeService will cache the probe result for. | 30 // How long the DnsProbeService will cache the probe result for. |
| 33 // If it's older than this and we get a probe request, the service expires it | 31 // If it's older than this and we get a probe request, the service expires it |
| 34 // and starts a new probe. | 32 // and starts a new probe. |
| 35 const int kMaxResultAgeMs = 5000; | 33 const int kMaxResultAgeMs = 5000; |
| 36 | 34 |
| 37 // The public DNS servers used by the DnsProbeService to verify internet | 35 // The public DNS servers used by the DnsProbeService to verify internet |
| 38 // connectivity. | 36 // connectivity. |
| 39 const char kPublicDnsPrimary[] = "8.8.8.8"; | 37 const char kGooglePublicDns1[] = "8.8.8.8"; |
| 40 const char kPublicDnsSecondary[] = "8.8.4.4"; | 38 const char kGooglePublicDns2[] = "8.8.4.4"; |
| 41 | 39 |
| 42 IPEndPoint MakeDnsEndPoint(const std::string& dns_ip_literal) { | 40 IPEndPoint MakeDnsEndPoint(const std::string& dns_ip_literal) { |
| 43 IPAddressNumber dns_ip_number; | 41 IPAddressNumber dns_ip_number; |
| 44 bool rv = ParseIPLiteralToNumber(dns_ip_literal, &dns_ip_number); | 42 bool rv = ParseIPLiteralToNumber(dns_ip_literal, &dns_ip_number); |
| 45 DCHECK(rv); | 43 DCHECK(rv); |
| 46 return IPEndPoint(dns_ip_number, net::dns_protocol::kDefaultPort); | 44 return IPEndPoint(dns_ip_number, net::dns_protocol::kDefaultPort); |
| 47 } | 45 } |
| 48 | 46 |
| 49 const int kAttemptsUseDefault = -1; | 47 DnsProbeStatus EvaluateResults(DnsProbeRunner::Result system_result, |
| 48 DnsProbeRunner::Result public_result) { |
| 49 // If the system DNS is working, assume the domain doesn't exist. |
| 50 if (system_result == DnsProbeRunner::CORRECT) |
| 51 return chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN; |
| 50 | 52 |
| 51 const char kAttemptsFieldTrialName[] = "DnsProbe-Attempts"; | 53 // If the system DNS is not working but another public server is, assume the |
| 54 // DNS config is bad (or perhaps the DNS servers are down or broken). |
| 55 if (public_result == DnsProbeRunner::CORRECT) |
| 56 return chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG; |
| 52 | 57 |
| 53 int GetAttemptsFromFieldTrial() { | 58 // If the system DNS is not working and another public server is unreachable, |
| 54 std::string group = FieldTrialList::FindFullName(kAttemptsFieldTrialName); | 59 // assume the internet connection is down (note that system DNS may be a |
| 55 if (group == "" || group == "default") | 60 // router on the LAN, so it may be reachable but returning errors.) |
| 56 return kAttemptsUseDefault; | 61 if (public_result == DnsProbeRunner::UNREACHABLE) |
| 62 return chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET; |
| 57 | 63 |
| 58 int attempts; | 64 // Otherwise: the system DNS is not working and another public server is |
| 59 if (!StringToInt(group, &attempts)) | 65 // responding but with errors or incorrect results. This is an awkward case; |
| 60 return kAttemptsUseDefault; | 66 // an invasive captive portal or a restrictive firewall may be intercepting |
| 61 | 67 // or rewriting DNS traffic, or the public server may itself be failing or |
| 62 return attempts; | 68 // down. |
| 69 return chrome_common_net::DNS_PROBE_FINISHED_UNKNOWN; |
| 63 } | 70 } |
| 64 | 71 |
| 65 bool IsLocalhost(const IPAddressNumber& ip) { | 72 // TODO(ttuttle): Make sure we're not changing result histogram mappings going |
| 66 return (ip.size() == net::kIPv4AddressSize) | 73 // from DnsProbeJob to DnsProbeRunner. |
| 67 && (ip[0] == 127) && (ip[1] == 0) && (ip[2] == 0) && (ip[3] == 1); | 74 void HistogramProbe(DnsProbeStatus result, base::TimeDelta elapsed) { |
| 75 DCHECK(chrome_common_net::DnsProbeStatusIsFinished(result)); |
| 76 |
| 77 const DnsProbeStatus kMaxStatus = chrome_common_net::DNS_PROBE_MAX; |
| 78 |
| 79 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Status", result, kMaxStatus); |
| 80 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed", elapsed); |
| 81 |
| 82 if (NetworkChangeNotifier::IsOffline()) { |
| 83 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Status_NcnOffline", |
| 84 result, kMaxStatus); |
| 85 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_NcnOffline", elapsed); |
| 86 } else { |
| 87 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Status_NcnOnline", |
| 88 result, kMaxStatus); |
| 89 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_NcnOnline", elapsed); |
| 90 } |
| 91 |
| 92 switch (result) { |
| 93 case chrome_common_net::DNS_PROBE_FINISHED_UNKNOWN: |
| 94 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_Unknown", |
| 95 elapsed); |
| 96 break; |
| 97 case chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET: |
| 98 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_NoInternet", |
| 99 elapsed); |
| 100 break; |
| 101 case chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG: |
| 102 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_BadConfig", |
| 103 elapsed); |
| 104 break; |
| 105 case chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN: |
| 106 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_Nxdomain", |
| 107 elapsed); |
| 108 break; |
| 109 |
| 110 // These aren't actually results. |
| 111 case chrome_common_net::DNS_PROBE_POSSIBLE: |
| 112 case chrome_common_net::DNS_PROBE_NOT_RUN: |
| 113 case chrome_common_net::DNS_PROBE_STARTED: |
| 114 case chrome_common_net::DNS_PROBE_MAX: |
| 115 NOTREACHED(); |
| 116 break; |
| 117 } |
| 68 } | 118 } |
| 69 | 119 |
| 70 // The maximum number of nameservers counted in histograms. | |
| 71 const int kNameserverCountMax = 10; | |
| 72 | |
| 73 } // namespace | 120 } // namespace |
| 74 | 121 |
| 75 DnsProbeService::DnsProbeService() | 122 DnsProbeService::DnsProbeService() |
| 76 : system_result_(DnsProbeJob::SERVERS_UNKNOWN), | 123 : state_(STATE_NO_RESULTS) { |
| 77 public_result_(DnsProbeJob::SERVERS_UNKNOWN), | 124 NetworkChangeNotifier::AddDNSObserver(this); |
| 78 state_(STATE_NO_RESULTS), | 125 SetSystemClientToCurrentConfig(); |
| 79 result_(chrome_common_net::DNS_PROBE_UNKNOWN), | 126 SetPublicClientToGooglePublicDns(); |
| 80 dns_attempts_(GetAttemptsFromFieldTrial()) { | |
| 81 NetworkChangeNotifier::AddIPAddressObserver(this); | |
| 82 } | 127 } |
| 83 | 128 |
| 84 DnsProbeService::~DnsProbeService() { | 129 DnsProbeService::~DnsProbeService() { |
| 85 NetworkChangeNotifier::RemoveIPAddressObserver(this); | 130 NetworkChangeNotifier::RemoveDNSObserver(this); |
| 86 } | 131 } |
| 87 | 132 |
| 88 void DnsProbeService::ProbeDns(const DnsProbeService::CallbackType& callback) { | 133 void DnsProbeService::ProbeDns(const DnsProbeService::ProbeCallback& callback) { |
| 89 callbacks_.push_back(callback); | 134 pending_callbacks_.push_back(callback); |
| 90 | 135 |
| 91 if (state_ == STATE_RESULTS_CACHED && ResultsExpired()) | 136 if (CachedResultIsExpired()) |
| 92 ExpireResults(); | 137 ClearCachedResult(); |
| 93 | 138 |
| 94 switch (state_) { | 139 switch (state_) { |
| 95 case STATE_NO_RESULTS: | 140 case STATE_NO_RESULTS: |
| 96 StartProbes(); | 141 StartProbes(); |
| 97 break; | 142 break; |
| 98 case STATE_RESULTS_CACHED: | 143 case STATE_RESULTS_CACHED: |
| 99 CallCallbacks(); | 144 CallCallbacks(); |
| 100 break; | 145 break; |
| 101 case STATE_PROBE_RUNNING: | 146 case STATE_PROBE_RUNNING: |
| 102 // do nothing; probe is already running, and will call the callback | 147 // do nothing; probe is already running, and will call the callback |
| 103 break; | 148 break; |
| 104 } | 149 } |
| 105 } | 150 } |
| 106 | 151 |
| 107 scoped_ptr<DnsProbeJob> DnsProbeService::CreateSystemProbeJob( | 152 void DnsProbeService::OnDNSChanged() { |
| 108 const DnsProbeJob::CallbackType& job_callback) { | 153 ClearCachedResult(); |
| 109 DnsConfig system_config; | 154 SetSystemClientToCurrentConfig(); |
| 110 GetSystemDnsConfig(&system_config); | |
| 111 return CreateProbeJob(system_config, job_callback); | |
| 112 } | 155 } |
| 113 | 156 |
| 114 scoped_ptr<DnsProbeJob> DnsProbeService::CreatePublicProbeJob( | 157 void DnsProbeService::SetSystemClientForTesting( |
| 115 const DnsProbeJob::CallbackType& job_callback) { | 158 scoped_ptr<DnsClient> system_client) { |
| 116 DnsConfig public_config; | 159 system_runner_.SetClient(system_client.Pass()); |
| 117 GetPublicDnsConfig(&public_config); | |
| 118 return CreateProbeJob(public_config, job_callback); | |
| 119 } | 160 } |
| 120 | 161 |
| 121 void DnsProbeService::OnIPAddressChanged() { | 162 void DnsProbeService::SetPublicClientForTesting( |
| 122 if (state_ == STATE_RESULTS_CACHED) | 163 scoped_ptr<DnsClient> public_client) { |
| 123 ExpireResults(); | 164 public_runner_.SetClient(public_client.Pass()); |
| 124 } | 165 } |
| 125 | 166 |
| 126 void DnsProbeService::ExpireResults() { | 167 void DnsProbeService::ClearCachedResultForTesting() { |
| 127 DCHECK_EQ(STATE_RESULTS_CACHED, state_); | 168 ClearCachedResult(); |
| 169 } |
| 128 | 170 |
| 129 state_ = STATE_NO_RESULTS; | 171 void DnsProbeService::SetSystemClientToCurrentConfig() { |
| 130 result_ = chrome_common_net::DNS_PROBE_UNKNOWN; | 172 DnsConfig system_config; |
| 173 NetworkChangeNotifier::GetDnsConfig(&system_config); |
| 174 system_config.search.clear(); |
| 175 system_config.attempts = 1; |
| 176 system_config.randomize_ports = false; |
| 177 |
| 178 scoped_ptr<DnsClient> system_client(DnsClient::CreateClient(NULL)); |
| 179 system_client->SetConfig(system_config); |
| 180 |
| 181 system_runner_.SetClient(system_client.Pass()); |
| 182 } |
| 183 |
| 184 void DnsProbeService::SetPublicClientToGooglePublicDns() { |
| 185 DnsConfig public_config; |
| 186 public_config.nameservers.push_back(MakeDnsEndPoint(kGooglePublicDns1)); |
| 187 public_config.nameservers.push_back(MakeDnsEndPoint(kGooglePublicDns1)); |
| 188 public_config.attempts = 1; |
| 189 public_config.randomize_ports = false; |
| 190 |
| 191 scoped_ptr<DnsClient> public_client(DnsClient::CreateClient(NULL)); |
| 192 public_client->SetConfig(public_config); |
| 193 |
| 194 public_runner_.SetClient(public_client.Pass()); |
| 131 } | 195 } |
| 132 | 196 |
| 133 void DnsProbeService::StartProbes() { | 197 void DnsProbeService::StartProbes() { |
| 134 DCHECK_NE(STATE_PROBE_RUNNING, state_); | 198 DCHECK_EQ(STATE_NO_RESULTS, state_); |
| 135 DCHECK(!system_job_.get()); | |
| 136 DCHECK(!public_job_.get()); | |
| 137 | 199 |
| 138 DnsProbeJob::CallbackType job_callback = | 200 DCHECK(!system_runner_.IsRunning()); |
| 139 base::Bind(&DnsProbeService::OnProbeJobComplete, | 201 DCHECK(!public_runner_.IsRunning()); |
| 140 base::Unretained(this)); | |
| 141 | 202 |
| 142 // TODO(ttuttle): Do we want to keep explicit flags for "job done"? | 203 const base::Closure callback = base::Bind(&DnsProbeService::OnProbeComplete, |
| 143 // Or maybe DnsProbeJob should have a "finished" flag? | 204 base::Unretained(this)); |
| 144 system_result_ = DnsProbeJob::SERVERS_UNKNOWN; | 205 system_runner_.RunProbe(callback); |
| 145 public_result_ = DnsProbeJob::SERVERS_UNKNOWN; | 206 public_runner_.RunProbe(callback); |
| 207 probe_start_time_ = base::Time::Now(); |
| 208 state_ = STATE_PROBE_RUNNING; |
| 146 | 209 |
| 147 system_job_ = CreateSystemProbeJob(job_callback); | 210 DCHECK(system_runner_.IsRunning()); |
| 148 public_job_ = CreatePublicProbeJob(job_callback); | 211 DCHECK(public_runner_.IsRunning()); |
| 149 | |
| 150 // If we can't create one or both jobs, fail the probe immediately. | |
| 151 if (!system_job_.get() || !public_job_.get()) { | |
| 152 system_job_.reset(); | |
| 153 public_job_.reset(); | |
| 154 state_ = STATE_RESULTS_CACHED; | |
| 155 // TODO(ttuttle): Should this be BAD_CONFIG? Currently I think it only | |
| 156 // happens when the system DnsConfig has no servers. | |
| 157 result_ = chrome_common_net::DNS_PROBE_UNKNOWN; | |
| 158 CallCallbacks(); | |
| 159 return; | |
| 160 } | |
| 161 | |
| 162 state_ = STATE_PROBE_RUNNING; | |
| 163 probe_start_time_ = base::Time::Now(); | |
| 164 } | 212 } |
| 165 | 213 |
| 166 void DnsProbeService::OnProbesComplete() { | 214 void DnsProbeService::OnProbeComplete() { |
| 167 DCHECK_EQ(STATE_PROBE_RUNNING, state_); | 215 DCHECK_EQ(STATE_PROBE_RUNNING, state_); |
| 168 | 216 |
| 217 if (system_runner_.IsRunning() || public_runner_.IsRunning()) |
| 218 return; |
| 219 |
| 220 cached_result_ = EvaluateResults(system_runner_.result(), |
| 221 public_runner_.result()); |
| 169 state_ = STATE_RESULTS_CACHED; | 222 state_ = STATE_RESULTS_CACHED; |
| 170 result_ = EvaluateResults(); | |
| 171 | 223 |
| 172 HistogramProbes(); | 224 HistogramProbe(cached_result_, base::Time::Now() - probe_start_time_); |
| 173 | 225 |
| 174 CallCallbacks(); | 226 CallCallbacks(); |
| 175 } | 227 } |
| 176 | 228 |
| 177 void DnsProbeService::HistogramProbes() const { | 229 void DnsProbeService::CallCallbacks() { |
| 178 const DnsProbeResult kMaxResult = chrome_common_net::DNS_PROBE_MAX; | 230 DCHECK_EQ(STATE_RESULTS_CACHED, state_); |
| 231 DCHECK(chrome_common_net::DnsProbeStatusIsFinished(cached_result_)); |
| 232 DCHECK(!pending_callbacks_.empty()); |
| 179 | 233 |
| 180 DCHECK_EQ(STATE_RESULTS_CACHED, state_); | 234 std::vector<ProbeCallback> callbacks = pending_callbacks_; |
| 181 DCHECK_NE(kMaxResult, result_); | 235 pending_callbacks_.clear(); |
| 182 | 236 |
| 183 base::TimeDelta elapsed = base::Time::Now() - probe_start_time_; | 237 for (std::vector<ProbeCallback>::const_iterator i = callbacks.begin(); |
| 184 | 238 i != callbacks.end(); ++i) { |
| 185 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Probe.Result", result_, kMaxResult); | 239 i->Run(cached_result_); |
| 186 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.Elapsed", elapsed); | |
| 187 | |
| 188 if (NetworkChangeNotifier::IsOffline()) { | |
| 189 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Probe.NcnOffline.Result", | |
| 190 result_, kMaxResult); | |
| 191 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.NcnOffline.Elapsed", elapsed); | |
| 192 } else { | |
| 193 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Probe.NcnOnline.Result", | |
| 194 result_, kMaxResult); | |
| 195 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.NcnOnline.Elapsed", elapsed); | |
| 196 } | |
| 197 | |
| 198 switch (result_) { | |
| 199 case chrome_common_net::DNS_PROBE_UNKNOWN: | |
| 200 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.ResultUnknown.Elapsed", | |
| 201 elapsed); | |
| 202 break; | |
| 203 case chrome_common_net::DNS_PROBE_NO_INTERNET: | |
| 204 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.ResultNoInternet.Elapsed", | |
| 205 elapsed); | |
| 206 break; | |
| 207 case chrome_common_net::DNS_PROBE_BAD_CONFIG: | |
| 208 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.ResultBadConfig.Elapsed", | |
| 209 elapsed); | |
| 210 | |
| 211 // Histogram some extra data to see why BAD_CONFIG is happening. | |
| 212 UMA_HISTOGRAM_ENUMERATION( | |
| 213 "DnsProbe.Probe.ResultBadConfig.SystemJobResult", | |
| 214 system_result_, | |
| 215 DnsProbeJob::MAX_RESULT); | |
| 216 UMA_HISTOGRAM_CUSTOM_COUNTS( | |
| 217 "DnsProbe.Probe.ResultBadConfig.SystemNameserverCount", | |
| 218 system_nameserver_count_, | |
| 219 0, kNameserverCountMax, kNameserverCountMax + 1); | |
| 220 UMA_HISTOGRAM_BOOLEAN( | |
| 221 "DnsProbe.Probe.ResultBadConfig.SystemIsLocalhost", | |
| 222 system_is_localhost_); | |
| 223 break; | |
| 224 case chrome_common_net::DNS_PROBE_NXDOMAIN: | |
| 225 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.ResultNxdomain.Elapsed", | |
| 226 elapsed); | |
| 227 break; | |
| 228 case chrome_common_net::DNS_PROBE_MAX: | |
| 229 NOTREACHED(); | |
| 230 break; | |
| 231 } | 240 } |
| 232 } | 241 } |
| 233 | 242 |
| 234 DnsProbeResult DnsProbeService::EvaluateResults() const { | 243 void DnsProbeService::ClearCachedResult() { |
| 235 DCHECK_NE(DnsProbeJob::SERVERS_UNKNOWN, system_result_); | 244 if (state_ == STATE_RESULTS_CACHED) { |
| 236 DCHECK_NE(DnsProbeJob::SERVERS_UNKNOWN, public_result_); | 245 state_ = STATE_NO_RESULTS; |
| 237 | 246 cached_result_ = chrome_common_net::DNS_PROBE_MAX; |
| 238 // If the system DNS is working, assume the domain doesn't exist. | |
| 239 if (system_result_ == DnsProbeJob::SERVERS_CORRECT) | |
| 240 return chrome_common_net::DNS_PROBE_NXDOMAIN; | |
| 241 | |
| 242 // If the system DNS is not working but another public server is, assume the | |
| 243 // DNS config is bad (or perhaps the DNS servers are down or broken). | |
| 244 if (public_result_ == DnsProbeJob::SERVERS_CORRECT) | |
| 245 return chrome_common_net::DNS_PROBE_BAD_CONFIG; | |
| 246 | |
| 247 // If the system DNS is not working and another public server is unreachable, | |
| 248 // assume the internet connection is down (note that system DNS may be a | |
| 249 // router on the LAN, so it may be reachable but returning errors.) | |
| 250 if (public_result_ == DnsProbeJob::SERVERS_UNREACHABLE) | |
| 251 return chrome_common_net::DNS_PROBE_NO_INTERNET; | |
| 252 | |
| 253 // Otherwise: the system DNS is not working and another public server is | |
| 254 // responding but with errors or incorrect results. This is an awkward case; | |
| 255 // an invasive captive portal or a restrictive firewall may be intercepting | |
| 256 // or rewriting DNS traffic, or the public server may itself be failing or | |
| 257 // down. | |
| 258 return chrome_common_net::DNS_PROBE_UNKNOWN; | |
| 259 } | |
| 260 | |
| 261 void DnsProbeService::CallCallbacks() { | |
| 262 DCHECK_EQ(STATE_RESULTS_CACHED, state_); | |
| 263 DCHECK(!callbacks_.empty()); | |
| 264 | |
| 265 std::vector<CallbackType> callbacks = callbacks_; | |
| 266 callbacks_.clear(); | |
| 267 | |
| 268 for (std::vector<CallbackType>::const_iterator i = callbacks.begin(); | |
| 269 i != callbacks.end(); ++i) { | |
| 270 i->Run(result_); | |
| 271 } | 247 } |
| 272 } | 248 } |
| 273 | 249 |
| 274 scoped_ptr<DnsProbeJob> DnsProbeService::CreateProbeJob( | 250 bool DnsProbeService::CachedResultIsExpired() const { |
| 275 const DnsConfig& dns_config, | 251 if (state_ != STATE_RESULTS_CACHED) |
| 276 const DnsProbeJob::CallbackType& job_callback) { | 252 return false; |
| 277 if (!dns_config.IsValid()) | |
| 278 return scoped_ptr<DnsProbeJob>(); | |
| 279 | 253 |
| 280 scoped_ptr<DnsClient> dns_client(DnsClient::CreateClient(NULL)); | |
| 281 dns_client->SetConfig(dns_config); | |
| 282 return DnsProbeJob::CreateJob(dns_client.Pass(), job_callback, NULL); | |
| 283 } | |
| 284 | |
| 285 void DnsProbeService::OnProbeJobComplete(DnsProbeJob* job, | |
| 286 DnsProbeJob::Result result) { | |
| 287 DCHECK_EQ(STATE_PROBE_RUNNING, state_); | |
| 288 | |
| 289 if (job == system_job_.get()) { | |
| 290 system_result_ = result; | |
| 291 system_job_.reset(); | |
| 292 } else if (job == public_job_.get()) { | |
| 293 public_result_ = result; | |
| 294 public_job_.reset(); | |
| 295 } else { | |
| 296 NOTREACHED(); | |
| 297 return; | |
| 298 } | |
| 299 | |
| 300 if (system_result_ != DnsProbeJob::SERVERS_UNKNOWN && | |
| 301 public_result_ != DnsProbeJob::SERVERS_UNKNOWN) { | |
| 302 OnProbesComplete(); | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 void DnsProbeService::GetSystemDnsConfig(DnsConfig* config) { | |
| 307 NetworkChangeNotifier::GetDnsConfig(config); | |
| 308 | |
| 309 // DNS probes don't need or want the suffix search list populated | |
| 310 config->search.clear(); | |
| 311 | |
| 312 if (dns_attempts_ != kAttemptsUseDefault) | |
| 313 config->attempts = dns_attempts_; | |
| 314 | |
| 315 // Take notes in case the config turns out to be bad, so we can histogram | |
| 316 // some useful data. | |
| 317 system_nameserver_count_ = config->nameservers.size(); | |
| 318 system_is_localhost_ = (system_nameserver_count_ == 1) | |
| 319 && IsLocalhost(config->nameservers[0].address()); | |
| 320 | |
| 321 // Disable port randomization. | |
| 322 config->randomize_ports = false; | |
| 323 } | |
| 324 | |
| 325 void DnsProbeService::GetPublicDnsConfig(DnsConfig* config) { | |
| 326 *config = DnsConfig(); | |
| 327 | |
| 328 config->nameservers.push_back(MakeDnsEndPoint(kPublicDnsPrimary)); | |
| 329 config->nameservers.push_back(MakeDnsEndPoint(kPublicDnsSecondary)); | |
| 330 | |
| 331 if (dns_attempts_ != kAttemptsUseDefault) | |
| 332 config->attempts = dns_attempts_; | |
| 333 | |
| 334 // Disable port randomization. | |
| 335 config->randomize_ports = false; | |
| 336 } | |
| 337 | |
| 338 bool DnsProbeService::ResultsExpired() { | |
| 339 const base::TimeDelta kMaxResultAge = | 254 const base::TimeDelta kMaxResultAge = |
| 340 base::TimeDelta::FromMilliseconds(kMaxResultAgeMs); | 255 base::TimeDelta::FromMilliseconds(kMaxResultAgeMs); |
| 341 return base::Time::Now() - probe_start_time_ > kMaxResultAge; | 256 return base::Time::Now() - probe_start_time_ > kMaxResultAge; |
| 342 } | 257 } |
| 343 | 258 |
| 344 } // namespace chrome_browser_net | 259 } // namespace chrome_browser_net |
| OLD | NEW |