| 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" | 10 #include "chrome/browser/net/dns_probe_job.h" |
| 11 #include "chrome/common/net/net_error_info.h" | 11 #include "chrome/common/net/net_error_info.h" |
| 12 #include "net/base/ip_endpoint.h" | 12 #include "net/base/ip_endpoint.h" |
| 13 #include "net/base/net_util.h" | 13 #include "net/base/net_util.h" |
| 14 #include "net/base/network_change_notifier.h" |
| 14 #include "net/dns/dns_client.h" | 15 #include "net/dns/dns_client.h" |
| 15 #include "net/dns/dns_config_service.h" | 16 #include "net/dns/dns_config_service.h" |
| 16 #include "net/dns/dns_protocol.h" | 17 #include "net/dns/dns_protocol.h" |
| 17 | 18 |
| 18 using base::FieldTrialList; | 19 using base::FieldTrialList; |
| 19 using base::StringToInt; | 20 using base::StringToInt; |
| 20 using chrome_common_net::DnsProbeResult; | 21 using chrome_common_net::DnsProbeStatus; |
| 21 using net::DnsClient; | 22 using net::DnsClient; |
| 22 using net::DnsConfig; | 23 using net::DnsConfig; |
| 23 using net::IPAddressNumber; | 24 using net::IPAddressNumber; |
| 24 using net::IPEndPoint; | 25 using net::IPEndPoint; |
| 25 using net::ParseIPLiteralToNumber; | 26 using net::ParseIPLiteralToNumber; |
| 26 using net::NetworkChangeNotifier; | 27 using net::NetworkChangeNotifier; |
| 27 | 28 |
| 28 namespace chrome_browser_net { | 29 namespace chrome_browser_net { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 bool IsLocalhost(const IPAddressNumber& ip) { | 66 bool IsLocalhost(const IPAddressNumber& ip) { |
| 66 return (ip.size() == net::kIPv4AddressSize) | 67 return (ip.size() == net::kIPv4AddressSize) |
| 67 && (ip[0] == 127) && (ip[1] == 0) && (ip[2] == 0) && (ip[3] == 1); | 68 && (ip[0] == 127) && (ip[1] == 0) && (ip[2] == 0) && (ip[3] == 1); |
| 68 } | 69 } |
| 69 | 70 |
| 70 // The maximum number of nameservers counted in histograms. | 71 // The maximum number of nameservers counted in histograms. |
| 71 const int kNameserverCountMax = 10; | 72 const int kNameserverCountMax = 10; |
| 72 | 73 |
| 73 } // namespace | 74 } // namespace |
| 74 | 75 |
| 75 DnsProbeService::DnsProbeService() | 76 class DnsProbeServiceImpl |
| 76 : system_result_(DnsProbeJob::SERVERS_UNKNOWN), | 77 : public DnsProbeService, |
| 78 net::NetworkChangeNotifier::IPAddressObserver { |
| 79 public: |
| 80 DnsProbeServiceImpl(scoped_ptr<DnsProbeService::JobFactory> job_factory); |
| 81 virtual ~DnsProbeServiceImpl(); |
| 82 |
| 83 virtual void ProbeDns(const CallbackType& callback) OVERRIDE; |
| 84 |
| 85 virtual void ExpireResultForTesting() OVERRIDE { ExpireResult(); } |
| 86 |
| 87 // NetworkChangeNotifier::IPAddressObserver implementation: |
| 88 virtual void OnIPAddressChanged() OVERRIDE; |
| 89 |
| 90 protected: |
| 91 void ExpireResult(); |
| 92 |
| 93 private: |
| 94 enum State { |
| 95 STATE_NO_RESULTS, |
| 96 STATE_PROBE_RUNNING, |
| 97 STATE_RESULTS_CACHED, |
| 98 }; |
| 99 |
| 100 void StartProbes(); |
| 101 void OnProbesComplete(); |
| 102 void CallCallbacks(); |
| 103 |
| 104 void OnProbeJobComplete(DnsProbeJob* job, DnsProbeJob::Result result); |
| 105 chrome_common_net::DnsProbeStatus EvaluateResults() const; |
| 106 void HistogramProbes() const; |
| 107 |
| 108 bool ResultsExpired(); |
| 109 |
| 110 scoped_ptr<DnsProbeService::JobFactory> job_factory_; |
| 111 scoped_ptr<DnsProbeJob> system_job_; |
| 112 scoped_ptr<DnsProbeJob> public_job_; |
| 113 DnsProbeJob::Result system_result_; |
| 114 DnsProbeJob::Result public_result_; |
| 115 std::vector<CallbackType> callbacks_; |
| 116 State state_; |
| 117 chrome_common_net::DnsProbeStatus result_; |
| 118 base::Time probe_start_time_; |
| 119 |
| 120 DISALLOW_COPY_AND_ASSIGN(DnsProbeServiceImpl); |
| 121 }; |
| 122 |
| 123 class DefaultDnsProbeJobFactory : public DnsProbeService::JobFactory { |
| 124 public: |
| 125 DefaultDnsProbeJobFactory(); |
| 126 |
| 127 virtual scoped_ptr<DnsProbeJob> CreateSystemJob( |
| 128 const DnsProbeJob::CallbackType& job_callback) OVERRIDE; |
| 129 virtual scoped_ptr<DnsProbeJob> CreatePublicJob( |
| 130 const DnsProbeJob::CallbackType& job_callback) OVERRIDE; |
| 131 |
| 132 private: |
| 133 void GetSystemDnsConfig(net::DnsConfig* config); |
| 134 void GetPublicDnsConfig(net::DnsConfig* config); |
| 135 |
| 136 scoped_ptr<DnsProbeJob> CreateProbeJob( |
| 137 const net::DnsConfig& dns_config, |
| 138 const DnsProbeJob::CallbackType& job_callback); |
| 139 |
| 140 // How many DNS request attempts the probe jobs will make before giving up |
| 141 // (Overrides the attempts field in the system DnsConfig.) |
| 142 const int dns_attempts_; |
| 143 |
| 144 DISALLOW_COPY_AND_ASSIGN(DefaultDnsProbeJobFactory); |
| 145 }; |
| 146 |
| 147 DnsProbeService* DnsProbeService::CreateDefault() { |
| 148 return new DnsProbeServiceImpl( |
| 149 scoped_ptr<DnsProbeService::JobFactory>( |
| 150 new DefaultDnsProbeJobFactory())); |
| 151 } |
| 152 |
| 153 DnsProbeService* DnsProbeService::CreateDefaultWithJobFactory( |
| 154 scoped_ptr<DnsProbeService::JobFactory> job_factory) { |
| 155 return new DnsProbeServiceImpl(job_factory.Pass()); |
| 156 } |
| 157 |
| 158 DnsProbeServiceImpl::DnsProbeServiceImpl( |
| 159 scoped_ptr<DnsProbeService::JobFactory> job_factory) |
| 160 : DnsProbeService(), |
| 161 job_factory_(job_factory.Pass()), |
| 162 system_result_(DnsProbeJob::SERVERS_UNKNOWN), |
| 77 public_result_(DnsProbeJob::SERVERS_UNKNOWN), | 163 public_result_(DnsProbeJob::SERVERS_UNKNOWN), |
| 78 state_(STATE_NO_RESULTS), | 164 state_(STATE_NO_RESULTS), |
| 79 result_(chrome_common_net::DNS_PROBE_UNKNOWN), | 165 result_(chrome_common_net::DNS_PROBE_MAX) { |
| 80 dns_attempts_(GetAttemptsFromFieldTrial()) { | |
| 81 NetworkChangeNotifier::AddIPAddressObserver(this); | 166 NetworkChangeNotifier::AddIPAddressObserver(this); |
| 82 } | 167 } |
| 83 | 168 |
| 84 DnsProbeService::~DnsProbeService() { | 169 DnsProbeServiceImpl::~DnsProbeServiceImpl() { |
| 85 NetworkChangeNotifier::RemoveIPAddressObserver(this); | 170 NetworkChangeNotifier::RemoveIPAddressObserver(this); |
| 86 } | 171 } |
| 87 | 172 |
| 88 void DnsProbeService::ProbeDns(const DnsProbeService::CallbackType& callback) { | 173 void DnsProbeServiceImpl::ProbeDns( |
| 174 const DnsProbeService::CallbackType& callback) { |
| 89 callbacks_.push_back(callback); | 175 callbacks_.push_back(callback); |
| 90 | 176 |
| 91 if (state_ == STATE_RESULTS_CACHED && ResultsExpired()) | 177 if (state_ == STATE_RESULTS_CACHED && ResultsExpired()) |
| 92 ExpireResults(); | 178 ExpireResult(); |
| 93 | 179 |
| 94 switch (state_) { | 180 switch (state_) { |
| 95 case STATE_NO_RESULTS: | 181 case STATE_NO_RESULTS: |
| 96 StartProbes(); | 182 StartProbes(); |
| 97 break; | 183 break; |
| 98 case STATE_RESULTS_CACHED: | 184 case STATE_RESULTS_CACHED: |
| 99 CallCallbacks(); | 185 CallCallbacks(); |
| 100 break; | 186 break; |
| 101 case STATE_PROBE_RUNNING: | 187 case STATE_PROBE_RUNNING: |
| 102 // do nothing; probe is already running, and will call the callback | 188 // do nothing; probe is already running, and will call the callback |
| 103 break; | 189 break; |
| 104 } | 190 } |
| 105 } | 191 } |
| 106 | 192 |
| 107 scoped_ptr<DnsProbeJob> DnsProbeService::CreateSystemProbeJob( | 193 void DnsProbeServiceImpl::OnIPAddressChanged() { |
| 108 const DnsProbeJob::CallbackType& job_callback) { | 194 if (state_ == STATE_RESULTS_CACHED) |
| 109 DnsConfig system_config; | 195 ExpireResult(); |
| 110 GetSystemDnsConfig(&system_config); | |
| 111 return CreateProbeJob(system_config, job_callback); | |
| 112 } | 196 } |
| 113 | 197 |
| 114 scoped_ptr<DnsProbeJob> DnsProbeService::CreatePublicProbeJob( | 198 void DnsProbeServiceImpl::ExpireResult() { |
| 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_); | 199 DCHECK_EQ(STATE_RESULTS_CACHED, state_); |
| 128 | 200 |
| 129 state_ = STATE_NO_RESULTS; | 201 state_ = STATE_NO_RESULTS; |
| 130 result_ = chrome_common_net::DNS_PROBE_UNKNOWN; | 202 result_ = chrome_common_net::DNS_PROBE_MAX; |
| 131 } | 203 } |
| 132 | 204 |
| 133 void DnsProbeService::StartProbes() { | 205 void DnsProbeServiceImpl::StartProbes() { |
| 134 DCHECK_NE(STATE_PROBE_RUNNING, state_); | 206 DCHECK_NE(STATE_PROBE_RUNNING, state_); |
| 135 DCHECK(!system_job_.get()); | 207 DCHECK(!system_job_.get()); |
| 136 DCHECK(!public_job_.get()); | 208 DCHECK(!public_job_.get()); |
| 137 | 209 |
| 138 DnsProbeJob::CallbackType job_callback = | 210 DnsProbeJob::CallbackType job_callback = |
| 139 base::Bind(&DnsProbeService::OnProbeJobComplete, | 211 base::Bind(&DnsProbeServiceImpl::OnProbeJobComplete, |
| 140 base::Unretained(this)); | 212 base::Unretained(this)); |
| 141 | 213 |
| 142 // TODO(ttuttle): Do we want to keep explicit flags for "job done"? | 214 // TODO(ttuttle): Do we want to keep explicit flags for "job done"? |
| 143 // Or maybe DnsProbeJob should have a "finished" flag? | 215 // Or maybe DnsProbeJob should have a "finished" flag? |
| 144 system_result_ = DnsProbeJob::SERVERS_UNKNOWN; | 216 system_result_ = DnsProbeJob::SERVERS_UNKNOWN; |
| 145 public_result_ = DnsProbeJob::SERVERS_UNKNOWN; | 217 public_result_ = DnsProbeJob::SERVERS_UNKNOWN; |
| 146 | 218 |
| 147 system_job_ = CreateSystemProbeJob(job_callback); | 219 system_job_ = job_factory_->CreateSystemJob(job_callback); |
| 148 public_job_ = CreatePublicProbeJob(job_callback); | 220 public_job_ = job_factory_->CreatePublicJob(job_callback); |
| 149 | 221 |
| 150 // If we can't create one or both jobs, fail the probe immediately. | 222 // If we can't create one or both jobs, fail the probe immediately. |
| 151 if (!system_job_.get() || !public_job_.get()) { | 223 if (!system_job_.get() || !public_job_.get()) { |
| 152 system_job_.reset(); | 224 system_job_.reset(); |
| 153 public_job_.reset(); | 225 public_job_.reset(); |
| 154 state_ = STATE_RESULTS_CACHED; | 226 state_ = STATE_RESULTS_CACHED; |
| 155 // TODO(ttuttle): Should this be BAD_CONFIG? Currently I think it only | 227 // TODO(ttuttle): Should this be BAD_CONFIG? Currently I think it only |
| 156 // happens when the system DnsConfig has no servers. | 228 // happens when the system DnsConfig has no servers. |
| 157 result_ = chrome_common_net::DNS_PROBE_UNKNOWN; | 229 result_ = chrome_common_net::DNS_PROBE_FINISHED_UNKNOWN; |
| 158 CallCallbacks(); | 230 CallCallbacks(); |
| 159 return; | 231 return; |
| 160 } | 232 } |
| 161 | 233 |
| 162 state_ = STATE_PROBE_RUNNING; | 234 state_ = STATE_PROBE_RUNNING; |
| 163 probe_start_time_ = base::Time::Now(); | 235 probe_start_time_ = base::Time::Now(); |
| 164 } | 236 } |
| 165 | 237 |
| 166 void DnsProbeService::OnProbesComplete() { | 238 void DnsProbeServiceImpl::OnProbesComplete() { |
| 167 DCHECK_EQ(STATE_PROBE_RUNNING, state_); | 239 DCHECK_EQ(STATE_PROBE_RUNNING, state_); |
| 168 | 240 |
| 169 state_ = STATE_RESULTS_CACHED; | 241 state_ = STATE_RESULTS_CACHED; |
| 170 result_ = EvaluateResults(); | 242 result_ = EvaluateResults(); |
| 171 | 243 |
| 172 HistogramProbes(); | 244 HistogramProbes(); |
| 173 | 245 |
| 174 CallCallbacks(); | 246 CallCallbacks(); |
| 175 } | 247 } |
| 176 | 248 |
| 177 void DnsProbeService::HistogramProbes() const { | 249 void DnsProbeServiceImpl::HistogramProbes() const { |
| 178 const DnsProbeResult kMaxResult = chrome_common_net::DNS_PROBE_MAX; | 250 const DnsProbeStatus kMaxStatus = chrome_common_net::DNS_PROBE_MAX; |
| 179 | 251 |
| 180 DCHECK_EQ(STATE_RESULTS_CACHED, state_); | 252 DCHECK_EQ(STATE_RESULTS_CACHED, state_); |
| 181 DCHECK_NE(kMaxResult, result_); | 253 DCHECK(chrome_common_net::DnsProbeStatusIsFinished(result_)); |
| 182 | 254 |
| 183 base::TimeDelta elapsed = base::Time::Now() - probe_start_time_; | 255 base::TimeDelta elapsed = base::Time::Now() - probe_start_time_; |
| 184 | 256 |
| 185 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Probe.Result", result_, kMaxResult); | 257 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Status", result_, kMaxStatus); |
| 186 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.Elapsed", elapsed); | 258 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed", elapsed); |
| 187 | 259 |
| 188 if (NetworkChangeNotifier::IsOffline()) { | 260 if (NetworkChangeNotifier::IsOffline()) { |
| 189 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Probe.NcnOffline.Result", | 261 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Status_NcnOffline", |
| 190 result_, kMaxResult); | 262 result_, kMaxStatus); |
| 191 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.NcnOffline.Elapsed", elapsed); | 263 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_NcnOffline", elapsed); |
| 192 } else { | 264 } else { |
| 193 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Probe.NcnOnline.Result", | 265 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Status_NcnOnline", |
| 194 result_, kMaxResult); | 266 result_, kMaxStatus); |
| 195 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.NcnOnline.Elapsed", elapsed); | 267 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_NcnOnline", elapsed); |
| 196 } | 268 } |
| 197 | 269 |
| 198 switch (result_) { | 270 switch (result_) { |
| 199 case chrome_common_net::DNS_PROBE_UNKNOWN: | 271 case chrome_common_net::DNS_PROBE_FINISHED_UNKNOWN: |
| 200 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.ResultUnknown.Elapsed", | 272 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_Unknown", |
| 201 elapsed); | 273 elapsed); |
| 202 break; | 274 break; |
| 203 case chrome_common_net::DNS_PROBE_NO_INTERNET: | 275 case chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET: |
| 204 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.ResultNoInternet.Elapsed", | 276 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_NoInternet", |
| 205 elapsed); | 277 elapsed); |
| 206 break; | 278 break; |
| 207 case chrome_common_net::DNS_PROBE_BAD_CONFIG: | 279 case chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG: |
| 208 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.ResultBadConfig.Elapsed", | 280 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_BadConfig", |
| 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); | 281 elapsed); |
| 227 break; | 282 break; |
| 283 case chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN: |
| 284 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_Nxdomain", |
| 285 elapsed); |
| 286 break; |
| 287 |
| 288 // These aren't actually results. |
| 289 case chrome_common_net::DNS_PROBE_POSSIBLE: |
| 290 case chrome_common_net::DNS_PROBE_NOT_RUN: |
| 291 case chrome_common_net::DNS_PROBE_STARTED: |
| 228 case chrome_common_net::DNS_PROBE_MAX: | 292 case chrome_common_net::DNS_PROBE_MAX: |
| 229 NOTREACHED(); | 293 NOTREACHED(); |
| 230 break; | 294 break; |
| 231 } | 295 } |
| 232 } | 296 } |
| 233 | 297 |
| 234 DnsProbeResult DnsProbeService::EvaluateResults() const { | 298 DnsProbeStatus DnsProbeServiceImpl::EvaluateResults() const { |
| 235 DCHECK_NE(DnsProbeJob::SERVERS_UNKNOWN, system_result_); | 299 DCHECK_NE(DnsProbeJob::SERVERS_UNKNOWN, system_result_); |
| 236 DCHECK_NE(DnsProbeJob::SERVERS_UNKNOWN, public_result_); | 300 DCHECK_NE(DnsProbeJob::SERVERS_UNKNOWN, public_result_); |
| 237 | 301 |
| 238 // If the system DNS is working, assume the domain doesn't exist. | 302 // If the system DNS is working, assume the domain doesn't exist. |
| 239 if (system_result_ == DnsProbeJob::SERVERS_CORRECT) | 303 if (system_result_ == DnsProbeJob::SERVERS_CORRECT) |
| 240 return chrome_common_net::DNS_PROBE_NXDOMAIN; | 304 return chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN; |
| 241 | 305 |
| 242 // If the system DNS is not working but another public server is, assume the | 306 // 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). | 307 // DNS config is bad (or perhaps the DNS servers are down or broken). |
| 244 if (public_result_ == DnsProbeJob::SERVERS_CORRECT) | 308 if (public_result_ == DnsProbeJob::SERVERS_CORRECT) |
| 245 return chrome_common_net::DNS_PROBE_BAD_CONFIG; | 309 return chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG; |
| 246 | 310 |
| 247 // If the system DNS is not working and another public server is unreachable, | 311 // 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 | 312 // 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.) | 313 // router on the LAN, so it may be reachable but returning errors.) |
| 250 if (public_result_ == DnsProbeJob::SERVERS_UNREACHABLE) | 314 if (public_result_ == DnsProbeJob::SERVERS_UNREACHABLE) |
| 251 return chrome_common_net::DNS_PROBE_NO_INTERNET; | 315 return chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET; |
| 252 | 316 |
| 253 // Otherwise: the system DNS is not working and another public server is | 317 // 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; | 318 // responding but with errors or incorrect results. This is an awkward case; |
| 255 // an invasive captive portal or a restrictive firewall may be intercepting | 319 // 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 | 320 // or rewriting DNS traffic, or the public server may itself be failing or |
| 257 // down. | 321 // down. |
| 258 return chrome_common_net::DNS_PROBE_UNKNOWN; | 322 return chrome_common_net::DNS_PROBE_FINISHED_UNKNOWN; |
| 259 } | 323 } |
| 260 | 324 |
| 261 void DnsProbeService::CallCallbacks() { | 325 void DnsProbeServiceImpl::CallCallbacks() { |
| 262 DCHECK_EQ(STATE_RESULTS_CACHED, state_); | 326 DCHECK_EQ(STATE_RESULTS_CACHED, state_); |
| 327 DCHECK(chrome_common_net::DnsProbeStatusIsFinished(result_)); |
| 263 DCHECK(!callbacks_.empty()); | 328 DCHECK(!callbacks_.empty()); |
| 264 | 329 |
| 265 std::vector<CallbackType> callbacks = callbacks_; | 330 std::vector<CallbackType> callbacks = callbacks_; |
| 266 callbacks_.clear(); | 331 callbacks_.clear(); |
| 267 | 332 |
| 268 for (std::vector<CallbackType>::const_iterator i = callbacks.begin(); | 333 for (std::vector<CallbackType>::const_iterator i = callbacks.begin(); |
| 269 i != callbacks.end(); ++i) { | 334 i != callbacks.end(); ++i) { |
| 270 i->Run(result_); | 335 i->Run(result_); |
| 271 } | 336 } |
| 272 } | 337 } |
| 273 | 338 |
| 274 scoped_ptr<DnsProbeJob> DnsProbeService::CreateProbeJob( | 339 void DnsProbeServiceImpl::OnProbeJobComplete(DnsProbeJob* job, |
| 275 const DnsConfig& dns_config, | 340 DnsProbeJob::Result result) { |
| 276 const DnsProbeJob::CallbackType& job_callback) { | |
| 277 if (!dns_config.IsValid()) | |
| 278 return scoped_ptr<DnsProbeJob>(NULL); | |
| 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_); | 341 DCHECK_EQ(STATE_PROBE_RUNNING, state_); |
| 288 | 342 |
| 289 if (job == system_job_.get()) { | 343 if (job == system_job_.get()) { |
| 290 system_result_ = result; | 344 system_result_ = result; |
| 291 system_job_.reset(); | 345 system_job_.reset(); |
| 292 } else if (job == public_job_.get()) { | 346 } else if (job == public_job_.get()) { |
| 293 public_result_ = result; | 347 public_result_ = result; |
| 294 public_job_.reset(); | 348 public_job_.reset(); |
| 295 } else { | 349 } else { |
| 296 NOTREACHED(); | 350 NOTREACHED(); |
| 297 return; | 351 return; |
| 298 } | 352 } |
| 299 | 353 |
| 300 if (system_result_ != DnsProbeJob::SERVERS_UNKNOWN && | 354 if (system_result_ != DnsProbeJob::SERVERS_UNKNOWN && |
| 301 public_result_ != DnsProbeJob::SERVERS_UNKNOWN) { | 355 public_result_ != DnsProbeJob::SERVERS_UNKNOWN) { |
| 302 OnProbesComplete(); | 356 OnProbesComplete(); |
| 303 } | 357 } |
| 304 } | 358 } |
| 305 | 359 |
| 306 void DnsProbeService::GetSystemDnsConfig(DnsConfig* config) { | 360 bool DnsProbeServiceImpl::ResultsExpired() { |
| 361 const base::TimeDelta kMaxResultAge = |
| 362 base::TimeDelta::FromMilliseconds(kMaxResultAgeMs); |
| 363 return base::Time::Now() - probe_start_time_ > kMaxResultAge; |
| 364 } |
| 365 |
| 366 DefaultDnsProbeJobFactory::DefaultDnsProbeJobFactory() |
| 367 : dns_attempts_(GetAttemptsFromFieldTrial()) { |
| 368 } |
| 369 |
| 370 scoped_ptr<DnsProbeJob> DefaultDnsProbeJobFactory::CreateSystemJob( |
| 371 const DnsProbeJob::CallbackType& job_callback) { |
| 372 DnsConfig system_config; |
| 373 GetSystemDnsConfig(&system_config); |
| 374 return CreateProbeJob(system_config, job_callback); |
| 375 } |
| 376 |
| 377 scoped_ptr<DnsProbeJob> DefaultDnsProbeJobFactory::CreatePublicJob( |
| 378 const DnsProbeJob::CallbackType& job_callback) { |
| 379 DnsConfig public_config; |
| 380 GetPublicDnsConfig(&public_config); |
| 381 return CreateProbeJob(public_config, job_callback); |
| 382 } |
| 383 |
| 384 void DefaultDnsProbeJobFactory::GetSystemDnsConfig(DnsConfig* config) { |
| 307 NetworkChangeNotifier::GetDnsConfig(config); | 385 NetworkChangeNotifier::GetDnsConfig(config); |
| 308 | 386 |
| 309 // DNS probes don't need or want the suffix search list populated | 387 // DNS probes don't need or want the suffix search list populated |
| 310 config->search.clear(); | 388 config->search.clear(); |
| 311 | 389 |
| 312 if (dns_attempts_ != kAttemptsUseDefault) | 390 if (dns_attempts_ != kAttemptsUseDefault) |
| 313 config->attempts = dns_attempts_; | 391 config->attempts = dns_attempts_; |
| 314 | 392 |
| 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. | 393 // Disable port randomization. |
| 322 config->randomize_ports = false; | 394 config->randomize_ports = false; |
| 323 } | 395 } |
| 324 | 396 |
| 325 void DnsProbeService::GetPublicDnsConfig(DnsConfig* config) { | 397 void DefaultDnsProbeJobFactory::GetPublicDnsConfig(DnsConfig* config) { |
| 326 *config = DnsConfig(); | 398 *config = DnsConfig(); |
| 327 | 399 |
| 328 config->nameservers.push_back(MakeDnsEndPoint(kPublicDnsPrimary)); | 400 config->nameservers.push_back(MakeDnsEndPoint(kPublicDnsPrimary)); |
| 329 config->nameservers.push_back(MakeDnsEndPoint(kPublicDnsSecondary)); | 401 config->nameservers.push_back(MakeDnsEndPoint(kPublicDnsSecondary)); |
| 330 | 402 |
| 331 if (dns_attempts_ != kAttemptsUseDefault) | 403 if (dns_attempts_ != kAttemptsUseDefault) |
| 332 config->attempts = dns_attempts_; | 404 config->attempts = dns_attempts_; |
| 333 | 405 |
| 334 // Disable port randomization. | 406 // Disable port randomization. |
| 335 config->randomize_ports = false; | 407 config->randomize_ports = false; |
| 336 } | 408 } |
| 337 | 409 |
| 338 bool DnsProbeService::ResultsExpired() { | 410 scoped_ptr<DnsProbeJob> DefaultDnsProbeJobFactory::CreateProbeJob( |
| 339 const base::TimeDelta kMaxResultAge = | 411 const DnsConfig& dns_config, |
| 340 base::TimeDelta::FromMilliseconds(kMaxResultAgeMs); | 412 const DnsProbeJob::CallbackType& job_callback) { |
| 341 return base::Time::Now() - probe_start_time_ > kMaxResultAge; | 413 if (!dns_config.IsValid()) |
| 414 return scoped_ptr<DnsProbeJob>(NULL); |
| 415 |
| 416 scoped_ptr<DnsClient> dns_client(DnsClient::CreateClient(NULL)); |
| 417 dns_client->SetConfig(dns_config); |
| 418 return DnsProbeJob::CreateJob(dns_client.Pass(), job_callback, NULL); |
| 342 } | 419 } |
| 343 | 420 |
| 344 } // namespace chrome_browser_net | 421 } // namespace chrome_browser_net |
| OLD | NEW |