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