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

Side by Side Diff: chrome/browser/net/dns_probe_service.cc

Issue 13270005: Display DNS probe results. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: ...actually add the unittests Created 7 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
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 "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_runner.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
30 namespace { 29 namespace {
31 30
32 // How long the DnsProbeService will cache the probe result for. 31 // 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 32 // If it's older than this and we get a probe request, the service expires it
34 // and starts a new probe. 33 // and starts a new probe.
35 const int kMaxResultAgeMs = 5000; 34 const int kMaxResultAgeMs = 5000;
36 35
37 // The public DNS servers used by the DnsProbeService to verify internet 36 // The public DNS servers used by the DnsProbeService to verify internet
38 // connectivity. 37 // connectivity.
39 const char kPublicDnsPrimary[] = "8.8.8.8"; 38 const char kPublicDnsPrimary[] = "8.8.8.8";
(...skipping 15 matching lines...) Expand all
55 if (group == "" || group == "default") 54 if (group == "" || group == "default")
56 return kAttemptsUseDefault; 55 return kAttemptsUseDefault;
57 56
58 int attempts; 57 int attempts;
59 if (!StringToInt(group, &attempts)) 58 if (!StringToInt(group, &attempts))
60 return kAttemptsUseDefault; 59 return kAttemptsUseDefault;
61 60
62 return attempts; 61 return attempts;
63 } 62 }
64 63
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 64 } // namespace
74 65
75 DnsProbeService::DnsProbeService() 66 DnsProbeService::DnsProbeService()
76 : system_result_(DnsProbeJob::SERVERS_UNKNOWN), 67 : state_(STATE_NO_RESULTS),
77 public_result_(DnsProbeJob::SERVERS_UNKNOWN), 68 system_result_(DnsProbeRunner::UNKNOWN),
78 state_(STATE_NO_RESULTS), 69 public_result_(DnsProbeRunner::UNKNOWN),
79 result_(chrome_common_net::DNS_PROBE_UNKNOWN), 70 result_(chrome_common_net::DNS_PROBE_MAX) {
80 dns_attempts_(GetAttemptsFromFieldTrial()) { 71 NetworkChangeNotifier::AddDNSObserver(this);
81 NetworkChangeNotifier::AddIPAddressObserver(this);
82 } 72 }
83 73
84 DnsProbeService::~DnsProbeService() { 74 DnsProbeService::~DnsProbeService() {
85 NetworkChangeNotifier::RemoveIPAddressObserver(this); 75 NetworkChangeNotifier::RemoveDNSObserver(this);
86 } 76 }
87 77
88 void DnsProbeService::ProbeDns(const DnsProbeService::CallbackType& callback) { 78 void DnsProbeService::ProbeDns(const DnsProbeService::ProbeCallback& callback) {
89 callbacks_.push_back(callback); 79 pending_callbacks_.push_back(callback);
90 80
91 if (state_ == STATE_RESULTS_CACHED && ResultsExpired()) 81 if (state_ == STATE_RESULTS_CACHED && ResultsExpired())
92 ExpireResults(); 82 ExpireResult();
93 83
94 switch (state_) { 84 switch (state_) {
95 case STATE_NO_RESULTS: 85 case STATE_NO_RESULTS:
96 StartProbes(); 86 StartProbes();
97 break; 87 break;
98 case STATE_RESULTS_CACHED: 88 case STATE_RESULTS_CACHED:
99 CallCallbacks(); 89 CallCallbacks();
100 break; 90 break;
101 case STATE_PROBE_RUNNING: 91 case STATE_PROBE_RUNNING:
102 // do nothing; probe is already running, and will call the callback 92 // do nothing; probe is already running, and will call the callback
103 break; 93 break;
104 } 94 }
105 } 95 }
106 96
107 scoped_ptr<DnsProbeJob> DnsProbeService::CreateSystemProbeJob( 97 void DnsProbeService::ExpireResultForTesting() {
108 const DnsProbeJob::CallbackType& job_callback) { 98 ExpireResult();
109 DnsConfig system_config;
110 GetSystemDnsConfig(&system_config);
111 return CreateProbeJob(system_config, job_callback);
112 } 99 }
113 100
114 scoped_ptr<DnsProbeJob> DnsProbeService::CreatePublicProbeJob( 101 DnsProbeRunner* DnsProbeService::GetProbeRunnerForTesting() {
115 const DnsProbeJob::CallbackType& job_callback) { 102 return &probe_runner_;
116 DnsConfig public_config;
117 GetPublicDnsConfig(&public_config);
118 return CreateProbeJob(public_config, job_callback);
119 } 103 }
120 104
121 void DnsProbeService::OnIPAddressChanged() { 105 void DnsProbeService::OnDNSChanged() {
122 if (state_ == STATE_RESULTS_CACHED) 106 ExpireResult();
123 ExpireResults();
124 } 107 }
125 108
126 void DnsProbeService::ExpireResults() { 109 void DnsProbeService::ExpireResult() {
127 DCHECK_EQ(STATE_RESULTS_CACHED, state_); 110 if (state_ == STATE_RESULTS_CACHED) {
128 111 state_ = STATE_NO_RESULTS;
129 state_ = STATE_NO_RESULTS; 112 result_ = chrome_common_net::DNS_PROBE_MAX;
130 result_ = chrome_common_net::DNS_PROBE_UNKNOWN; 113 }
131 } 114 }
132 115
133 void DnsProbeService::StartProbes() { 116 void DnsProbeService::StartProbes() {
134 DCHECK_NE(STATE_PROBE_RUNNING, state_); 117 DCHECK_NE(STATE_PROBE_RUNNING, state_);
135 DCHECK(!system_job_.get());
136 DCHECK(!public_job_.get());
137 118
138 DnsProbeJob::CallbackType job_callback = 119 const DnsProbeRunner::ProbeCallback probe_callback =
139 base::Bind(&DnsProbeService::OnProbeJobComplete, 120 base::Bind(&DnsProbeService::OnProbeComplete, base::Unretained(this));
140 base::Unretained(this));
141 121
142 // TODO(ttuttle): Do we want to keep explicit flags for "job done"? 122 system_done_ = false;
143 // Or maybe DnsProbeJob should have a "finished" flag? 123 public_done_ = false;
144 system_result_ = DnsProbeJob::SERVERS_UNKNOWN; 124 system_result_ = DnsProbeRunner::UNKNOWN;
145 public_result_ = DnsProbeJob::SERVERS_UNKNOWN; 125 public_result_ = DnsProbeRunner::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 126
162 state_ = STATE_PROBE_RUNNING; 127 state_ = STATE_PROBE_RUNNING;
163 probe_start_time_ = base::Time::Now(); 128 probe_start_time_ = base::Time::Now();
129
130 probe_runner_.RunProbe(DnsProbeRunner::SYSTEM, probe_callback);
131 probe_runner_.RunProbe(DnsProbeRunner::PUBLIC, probe_callback);
164 } 132 }
165 133
166 void DnsProbeService::OnProbesComplete() { 134 void DnsProbeService::OnProbesComplete() {
167 DCHECK_EQ(STATE_PROBE_RUNNING, state_); 135 DCHECK_EQ(STATE_PROBE_RUNNING, state_);
168 136
169 state_ = STATE_RESULTS_CACHED; 137 state_ = STATE_RESULTS_CACHED;
170 result_ = EvaluateResults(); 138 result_ = EvaluateResults();
171 139
172 HistogramProbes(); 140 HistogramProbes();
173 141
174 CallCallbacks(); 142 CallCallbacks();
175 } 143 }
176 144
145 // TODO(ttuttle): Make sure we're not changing result histogram mappings going
146 // from DnsProbeJob to DnsProbeRunner.
177 void DnsProbeService::HistogramProbes() const { 147 void DnsProbeService::HistogramProbes() const {
178 const DnsProbeResult kMaxResult = chrome_common_net::DNS_PROBE_MAX; 148 const DnsProbeStatus kMaxStatus = chrome_common_net::DNS_PROBE_MAX;
179 149
180 DCHECK_EQ(STATE_RESULTS_CACHED, state_); 150 DCHECK_EQ(STATE_RESULTS_CACHED, state_);
181 DCHECK_NE(kMaxResult, result_); 151 DCHECK(chrome_common_net::DnsProbeStatusIsFinished(result_));
182 152
183 base::TimeDelta elapsed = base::Time::Now() - probe_start_time_; 153 base::TimeDelta elapsed = base::Time::Now() - probe_start_time_;
184 154
185 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Probe.Result", result_, kMaxResult); 155 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Status", result_, kMaxStatus);
186 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.Elapsed", elapsed); 156 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed", elapsed);
187 157
188 if (NetworkChangeNotifier::IsOffline()) { 158 if (NetworkChangeNotifier::IsOffline()) {
189 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Probe.NcnOffline.Result", 159 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Status_NcnOffline",
190 result_, kMaxResult); 160 result_, kMaxStatus);
191 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.NcnOffline.Elapsed", elapsed); 161 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_NcnOffline", elapsed);
192 } else { 162 } else {
193 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Probe.NcnOnline.Result", 163 UMA_HISTOGRAM_ENUMERATION("DnsProbe.Status_NcnOnline",
194 result_, kMaxResult); 164 result_, kMaxStatus);
195 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.NcnOnline.Elapsed", elapsed); 165 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_NcnOnline", elapsed);
196 } 166 }
197 167
198 switch (result_) { 168 switch (result_) {
199 case chrome_common_net::DNS_PROBE_UNKNOWN: 169 case chrome_common_net::DNS_PROBE_FINISHED_UNKNOWN:
200 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.ResultUnknown.Elapsed", 170 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_Unknown",
201 elapsed); 171 elapsed);
202 break; 172 break;
203 case chrome_common_net::DNS_PROBE_NO_INTERNET: 173 case chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET:
204 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.ResultNoInternet.Elapsed", 174 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_NoInternet",
205 elapsed); 175 elapsed);
206 break; 176 break;
207 case chrome_common_net::DNS_PROBE_BAD_CONFIG: 177 case chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG:
208 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Probe.ResultBadConfig.Elapsed", 178 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); 179 elapsed);
227 break; 180 break;
181 case chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN:
182 UMA_HISTOGRAM_MEDIUM_TIMES("DnsProbe.Elapsed_Nxdomain",
183 elapsed);
184 break;
185
186 // These aren't actually results.
187 case chrome_common_net::DNS_PROBE_POSSIBLE:
188 case chrome_common_net::DNS_PROBE_NOT_RUN:
189 case chrome_common_net::DNS_PROBE_STARTED:
228 case chrome_common_net::DNS_PROBE_MAX: 190 case chrome_common_net::DNS_PROBE_MAX:
229 NOTREACHED(); 191 NOTREACHED();
230 break; 192 break;
231 } 193 }
232 } 194 }
233 195
234 DnsProbeResult DnsProbeService::EvaluateResults() const { 196 DnsProbeStatus DnsProbeService::EvaluateResults() const {
235 DCHECK_NE(DnsProbeJob::SERVERS_UNKNOWN, system_result_); 197 DCHECK_NE(DnsProbeRunner::UNKNOWN, system_result_);
236 DCHECK_NE(DnsProbeJob::SERVERS_UNKNOWN, public_result_); 198 DCHECK_NE(DnsProbeRunner::UNKNOWN, public_result_);
237 199
238 // If the system DNS is working, assume the domain doesn't exist. 200 // If the system DNS is working, assume the domain doesn't exist.
239 if (system_result_ == DnsProbeJob::SERVERS_CORRECT) 201 if (system_result_ == DnsProbeRunner::CORRECT)
240 return chrome_common_net::DNS_PROBE_NXDOMAIN; 202 return chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN;
241 203
242 // If the system DNS is not working but another public server is, assume the 204 // 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). 205 // DNS config is bad (or perhaps the DNS servers are down or broken).
244 if (public_result_ == DnsProbeJob::SERVERS_CORRECT) 206 if (public_result_ == DnsProbeRunner::CORRECT)
245 return chrome_common_net::DNS_PROBE_BAD_CONFIG; 207 return chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG;
246 208
247 // If the system DNS is not working and another public server is unreachable, 209 // 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 210 // 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.) 211 // router on the LAN, so it may be reachable but returning errors.)
250 if (public_result_ == DnsProbeJob::SERVERS_UNREACHABLE) 212 if (public_result_ == DnsProbeRunner::UNREACHABLE)
251 return chrome_common_net::DNS_PROBE_NO_INTERNET; 213 return chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET;
252 214
253 // Otherwise: the system DNS is not working and another public server is 215 // 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; 216 // responding but with errors or incorrect results. This is an awkward case;
255 // an invasive captive portal or a restrictive firewall may be intercepting 217 // 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 218 // or rewriting DNS traffic, or the public server may itself be failing or
257 // down. 219 // down.
258 return chrome_common_net::DNS_PROBE_UNKNOWN; 220 return chrome_common_net::DNS_PROBE_FINISHED_UNKNOWN;
259 } 221 }
260 222
261 void DnsProbeService::CallCallbacks() { 223 void DnsProbeService::CallCallbacks() {
262 DCHECK_EQ(STATE_RESULTS_CACHED, state_); 224 DCHECK_EQ(STATE_RESULTS_CACHED, state_);
263 DCHECK(!callbacks_.empty()); 225 DCHECK(chrome_common_net::DnsProbeStatusIsFinished(result_));
226 DCHECK(!pending_callbacks_.empty());
264 227
265 std::vector<CallbackType> callbacks = callbacks_; 228 std::vector<ProbeCallback> callbacks = pending_callbacks_;
266 callbacks_.clear(); 229 pending_callbacks_.clear();
267 230
268 for (std::vector<CallbackType>::const_iterator i = callbacks.begin(); 231 for (std::vector<ProbeCallback>::const_iterator i = callbacks.begin();
269 i != callbacks.end(); ++i) { 232 i != callbacks.end(); ++i) {
270 i->Run(result_); 233 i->Run(result_);
271 } 234 }
272 } 235 }
273 236
274 scoped_ptr<DnsProbeJob> DnsProbeService::CreateProbeJob( 237 void DnsProbeService::OnProbeComplete(DnsProbeRunner::Type type,
275 const DnsConfig& dns_config, 238 DnsProbeRunner::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_); 239 DCHECK_EQ(STATE_PROBE_RUNNING, state_);
288 240
289 if (job == system_job_.get()) { 241 switch (type) {
242 case DnsProbeRunner::SYSTEM:
243 DCHECK(!system_done_);
290 system_result_ = result; 244 system_result_ = result;
291 system_job_.reset(); 245 system_done_ = true;
292 } else if (job == public_job_.get()) { 246 break;
247 case DnsProbeRunner::PUBLIC:
248 DCHECK(!public_done_);
293 public_result_ = result; 249 public_result_ = result;
294 public_job_.reset(); 250 public_done_ = true;
295 } else {
296 NOTREACHED();
297 return;
298 } 251 }
299 252
300 if (system_result_ != DnsProbeJob::SERVERS_UNKNOWN && 253 if (system_done_ && public_done_) {
301 public_result_ != DnsProbeJob::SERVERS_UNKNOWN) {
302 OnProbesComplete(); 254 OnProbesComplete();
303 } 255 }
304 } 256 }
305 257
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() { 258 bool DnsProbeService::ResultsExpired() {
339 const base::TimeDelta kMaxResultAge = 259 const base::TimeDelta kMaxResultAge =
340 base::TimeDelta::FromMilliseconds(kMaxResultAgeMs); 260 base::TimeDelta::FromMilliseconds(kMaxResultAgeMs);
341 return base::Time::Now() - probe_start_time_ > kMaxResultAge; 261 return base::Time::Now() - probe_start_time_ > kMaxResultAge;
342 } 262 }
343
344 } // namespace chrome_browser_net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698