Enabling tests which should be fixed by r173829.
[chromium-blink-merge.git] / net / dns / dns_config_service.cc
blob1230688b999c7997f8a44267fe51de6840058441
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
3 // found in the LICENSE file.
5 #include "net/dns/dns_config_service.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9 #include "base/values.h"
10 #include "net/base/ip_endpoint.h"
12 namespace net {
14 // Default values are taken from glibc resolv.h except timeout which is set to
15 // |kDnsTimeoutSeconds|.
16 DnsConfig::DnsConfig()
17 : append_to_multi_label_name(true),
18 ndots(1),
19 timeout(base::TimeDelta::FromSeconds(kDnsTimeoutSeconds)),
20 attempts(2),
21 rotate(false),
22 edns0(false) {}
24 DnsConfig::~DnsConfig() {}
26 bool DnsConfig::Equals(const DnsConfig& d) const {
27 return EqualsIgnoreHosts(d) && (hosts == d.hosts);
30 bool DnsConfig::EqualsIgnoreHosts(const DnsConfig& d) const {
31 return (nameservers == d.nameservers) &&
32 (search == d.search) &&
33 (append_to_multi_label_name == d.append_to_multi_label_name) &&
34 (ndots == d.ndots) &&
35 (timeout == d.timeout) &&
36 (attempts == d.attempts) &&
37 (rotate == d.rotate) &&
38 (edns0 == d.edns0);
41 void DnsConfig::CopyIgnoreHosts(const DnsConfig& d) {
42 nameservers = d.nameservers;
43 search = d.search;
44 append_to_multi_label_name = d.append_to_multi_label_name;
45 ndots = d.ndots;
46 timeout = d.timeout;
47 attempts = d.attempts;
48 rotate = d.rotate;
49 edns0 = d.edns0;
52 base::Value* DnsConfig::ToValue() const {
53 DictionaryValue* dict = new DictionaryValue();
55 ListValue* list = new ListValue();
56 for (size_t i = 0; i < nameservers.size(); ++i)
57 list->Append(Value::CreateStringValue(nameservers[i].ToString()));
58 dict->Set("nameservers", list);
60 list = new ListValue();
61 for (size_t i = 0; i < search.size(); ++i)
62 list->Append(Value::CreateStringValue(search[i]));
63 dict->Set("search", list);
65 dict->SetBoolean("append_to_multi_label_name", append_to_multi_label_name);
66 dict->SetInteger("ndots", ndots);
67 dict->SetDouble("timeout", timeout.InSecondsF());
68 dict->SetInteger("attempts", attempts);
69 dict->SetBoolean("rotate", rotate);
70 dict->SetBoolean("edns0", edns0);
71 dict->SetInteger("num_hosts", hosts.size());
73 return dict;
77 DnsConfigService::DnsConfigService()
78 : watch_failed_(false),
79 have_config_(false),
80 have_hosts_(false),
81 need_update_(false),
82 last_sent_empty_(true) {}
84 DnsConfigService::~DnsConfigService() {
87 void DnsConfigService::ReadConfig(const CallbackType& callback) {
88 DCHECK(CalledOnValidThread());
89 DCHECK(!callback.is_null());
90 DCHECK(callback_.is_null());
91 callback_ = callback;
92 ReadNow();
95 void DnsConfigService::WatchConfig(const CallbackType& callback) {
96 DCHECK(CalledOnValidThread());
97 DCHECK(!callback.is_null());
98 DCHECK(callback_.is_null());
99 callback_ = callback;
100 watch_failed_ = !StartWatching();
101 ReadNow();
104 void DnsConfigService::InvalidateConfig() {
105 DCHECK(CalledOnValidThread());
106 base::TimeTicks now = base::TimeTicks::Now();
107 if (!last_invalidate_config_time_.is_null()) {
108 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.ConfigNotifyInterval",
109 now - last_invalidate_config_time_);
111 last_invalidate_config_time_ = now;
112 if (!have_config_)
113 return;
114 have_config_ = false;
115 StartTimer();
118 void DnsConfigService::InvalidateHosts() {
119 DCHECK(CalledOnValidThread());
120 base::TimeTicks now = base::TimeTicks::Now();
121 if (!last_invalidate_hosts_time_.is_null()) {
122 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.HostsNotifyInterval",
123 now - last_invalidate_hosts_time_);
125 last_invalidate_hosts_time_ = now;
126 if (!have_hosts_)
127 return;
128 have_hosts_ = false;
129 StartTimer();
132 void DnsConfigService::OnConfigRead(const DnsConfig& config) {
133 DCHECK(CalledOnValidThread());
134 DCHECK(config.IsValid());
136 bool changed = false;
137 if (!config.EqualsIgnoreHosts(dns_config_)) {
138 dns_config_.CopyIgnoreHosts(config);
139 need_update_ = true;
140 changed = true;
142 if (!changed && !last_sent_empty_time_.is_null()) {
143 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedConfigInterval",
144 base::TimeTicks::Now() - last_sent_empty_time_);
146 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.ConfigChange", changed);
148 have_config_ = true;
149 if (have_hosts_ || watch_failed_)
150 OnCompleteConfig();
153 void DnsConfigService::OnHostsRead(const DnsHosts& hosts) {
154 DCHECK(CalledOnValidThread());
156 bool changed = false;
157 if (hosts != dns_config_.hosts) {
158 dns_config_.hosts = hosts;
159 need_update_ = true;
160 changed = true;
162 if (!changed && !last_sent_empty_time_.is_null()) {
163 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedHostsInterval",
164 base::TimeTicks::Now() - last_sent_empty_time_);
166 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HostsChange", changed);
168 have_hosts_ = true;
169 if (have_config_ || watch_failed_)
170 OnCompleteConfig();
173 void DnsConfigService::StartTimer() {
174 DCHECK(CalledOnValidThread());
175 if (last_sent_empty_) {
176 DCHECK(!timer_.IsRunning());
177 return; // No need to withdraw again.
179 timer_.Stop();
181 // Give it a short timeout to come up with a valid config. Otherwise withdraw
182 // the config from the receiver. The goal is to avoid perceivable network
183 // outage (when using the wrong config) but at the same time avoid
184 // unnecessary Job aborts in HostResolverImpl. The signals come from multiple
185 // sources so it might receive multiple events during a config change.
187 // DHCP and user-induced changes are on the order of seconds, so 100ms should
188 // not add perceivable delay. On the other hand, config readers should finish
189 // within 100ms with the rare exception of I/O block or extra large HOSTS.
190 const base::TimeDelta kTimeout = base::TimeDelta::FromMilliseconds(100);
192 timer_.Start(FROM_HERE,
193 kTimeout,
194 this,
195 &DnsConfigService::OnTimeout);
198 void DnsConfigService::OnTimeout() {
199 DCHECK(CalledOnValidThread());
200 DCHECK(!last_sent_empty_);
201 // Indicate that even if there is no change in On*Read, we will need to
202 // update the receiver when the config becomes complete.
203 need_update_ = true;
204 // Empty config is considered invalid.
205 last_sent_empty_ = true;
206 last_sent_empty_time_ = base::TimeTicks::Now();
207 callback_.Run(DnsConfig());
210 void DnsConfigService::OnCompleteConfig() {
211 timer_.Stop();
212 if (!need_update_)
213 return;
214 need_update_ = false;
215 last_sent_empty_ = false;
216 if (watch_failed_) {
217 // If a watch failed, the config may not be accurate, so report empty.
218 callback_.Run(DnsConfig());
219 } else {
220 callback_.Run(dns_config_);
224 } // namespace net