Add explicit |forceOnlineSignin| to user pod status
[chromium-blink-merge.git] / net / dns / dns_config_service.cc
blob661318255717d3b86d1a769c369060beecd79b04
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 : unhandled_options(false),
18 append_to_multi_label_name(true),
19 randomize_ports(false),
20 ndots(1),
21 timeout(base::TimeDelta::FromSeconds(kDnsTimeoutSeconds)),
22 attempts(2),
23 rotate(false),
24 edns0(false),
25 use_local_ipv6(false) {}
27 DnsConfig::~DnsConfig() {}
29 bool DnsConfig::Equals(const DnsConfig& d) const {
30 return EqualsIgnoreHosts(d) && (hosts == d.hosts);
33 bool DnsConfig::EqualsIgnoreHosts(const DnsConfig& d) const {
34 return (nameservers == d.nameservers) &&
35 (search == d.search) &&
36 (unhandled_options == d.unhandled_options) &&
37 (append_to_multi_label_name == d.append_to_multi_label_name) &&
38 (ndots == d.ndots) &&
39 (timeout == d.timeout) &&
40 (attempts == d.attempts) &&
41 (rotate == d.rotate) &&
42 (edns0 == d.edns0) &&
43 (use_local_ipv6 == d.use_local_ipv6);
46 void DnsConfig::CopyIgnoreHosts(const DnsConfig& d) {
47 nameservers = d.nameservers;
48 search = d.search;
49 unhandled_options = d.unhandled_options;
50 append_to_multi_label_name = d.append_to_multi_label_name;
51 ndots = d.ndots;
52 timeout = d.timeout;
53 attempts = d.attempts;
54 rotate = d.rotate;
55 edns0 = d.edns0;
56 use_local_ipv6 = d.use_local_ipv6;
59 base::Value* DnsConfig::ToValue() const {
60 base::DictionaryValue* dict = new base::DictionaryValue();
62 base::ListValue* list = new base::ListValue();
63 for (size_t i = 0; i < nameservers.size(); ++i)
64 list->Append(new base::StringValue(nameservers[i].ToString()));
65 dict->Set("nameservers", list);
67 list = new base::ListValue();
68 for (size_t i = 0; i < search.size(); ++i)
69 list->Append(new base::StringValue(search[i]));
70 dict->Set("search", list);
72 dict->SetBoolean("unhandled_options", unhandled_options);
73 dict->SetBoolean("append_to_multi_label_name", append_to_multi_label_name);
74 dict->SetInteger("ndots", ndots);
75 dict->SetDouble("timeout", timeout.InSecondsF());
76 dict->SetInteger("attempts", attempts);
77 dict->SetBoolean("rotate", rotate);
78 dict->SetBoolean("edns0", edns0);
79 dict->SetBoolean("use_local_ipv6", use_local_ipv6);
80 dict->SetInteger("num_hosts", hosts.size());
82 return dict;
86 DnsConfigService::DnsConfigService()
87 : watch_failed_(false),
88 have_config_(false),
89 have_hosts_(false),
90 need_update_(false),
91 last_sent_empty_(true) {}
93 DnsConfigService::~DnsConfigService() {
96 void DnsConfigService::ReadConfig(const CallbackType& callback) {
97 DCHECK(CalledOnValidThread());
98 DCHECK(!callback.is_null());
99 DCHECK(callback_.is_null());
100 callback_ = callback;
101 ReadNow();
104 void DnsConfigService::WatchConfig(const CallbackType& callback) {
105 DCHECK(CalledOnValidThread());
106 DCHECK(!callback.is_null());
107 DCHECK(callback_.is_null());
108 callback_ = callback;
109 watch_failed_ = !StartWatching();
110 ReadNow();
113 void DnsConfigService::InvalidateConfig() {
114 DCHECK(CalledOnValidThread());
115 base::TimeTicks now = base::TimeTicks::Now();
116 if (!last_invalidate_config_time_.is_null()) {
117 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.ConfigNotifyInterval",
118 now - last_invalidate_config_time_);
120 last_invalidate_config_time_ = now;
121 if (!have_config_)
122 return;
123 have_config_ = false;
124 StartTimer();
127 void DnsConfigService::InvalidateHosts() {
128 DCHECK(CalledOnValidThread());
129 base::TimeTicks now = base::TimeTicks::Now();
130 if (!last_invalidate_hosts_time_.is_null()) {
131 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.HostsNotifyInterval",
132 now - last_invalidate_hosts_time_);
134 last_invalidate_hosts_time_ = now;
135 if (!have_hosts_)
136 return;
137 have_hosts_ = false;
138 StartTimer();
141 void DnsConfigService::OnConfigRead(const DnsConfig& config) {
142 DCHECK(CalledOnValidThread());
143 DCHECK(config.IsValid());
145 bool changed = false;
146 if (!config.EqualsIgnoreHosts(dns_config_)) {
147 dns_config_.CopyIgnoreHosts(config);
148 need_update_ = true;
149 changed = true;
151 if (!changed && !last_sent_empty_time_.is_null()) {
152 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedConfigInterval",
153 base::TimeTicks::Now() - last_sent_empty_time_);
155 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.ConfigChange", changed);
157 have_config_ = true;
158 if (have_hosts_ || watch_failed_)
159 OnCompleteConfig();
162 void DnsConfigService::OnHostsRead(const DnsHosts& hosts) {
163 DCHECK(CalledOnValidThread());
165 bool changed = false;
166 if (hosts != dns_config_.hosts) {
167 dns_config_.hosts = hosts;
168 need_update_ = true;
169 changed = true;
171 if (!changed && !last_sent_empty_time_.is_null()) {
172 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedHostsInterval",
173 base::TimeTicks::Now() - last_sent_empty_time_);
175 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HostsChange", changed);
177 have_hosts_ = true;
178 if (have_config_ || watch_failed_)
179 OnCompleteConfig();
182 void DnsConfigService::StartTimer() {
183 DCHECK(CalledOnValidThread());
184 if (last_sent_empty_) {
185 DCHECK(!timer_.IsRunning());
186 return; // No need to withdraw again.
188 timer_.Stop();
190 // Give it a short timeout to come up with a valid config. Otherwise withdraw
191 // the config from the receiver. The goal is to avoid perceivable network
192 // outage (when using the wrong config) but at the same time avoid
193 // unnecessary Job aborts in HostResolverImpl. The signals come from multiple
194 // sources so it might receive multiple events during a config change.
196 // DHCP and user-induced changes are on the order of seconds, so 150ms should
197 // not add perceivable delay. On the other hand, config readers should finish
198 // within 150ms with the rare exception of I/O block or extra large HOSTS.
199 const base::TimeDelta kTimeout = base::TimeDelta::FromMilliseconds(150);
201 timer_.Start(FROM_HERE,
202 kTimeout,
203 this,
204 &DnsConfigService::OnTimeout);
207 void DnsConfigService::OnTimeout() {
208 DCHECK(CalledOnValidThread());
209 DCHECK(!last_sent_empty_);
210 // Indicate that even if there is no change in On*Read, we will need to
211 // update the receiver when the config becomes complete.
212 need_update_ = true;
213 // Empty config is considered invalid.
214 last_sent_empty_ = true;
215 last_sent_empty_time_ = base::TimeTicks::Now();
216 callback_.Run(DnsConfig());
219 void DnsConfigService::OnCompleteConfig() {
220 timer_.Stop();
221 if (!need_update_)
222 return;
223 need_update_ = false;
224 last_sent_empty_ = false;
225 if (watch_failed_) {
226 // If a watch failed, the config may not be accurate, so report empty.
227 callback_.Run(DnsConfig());
228 } else {
229 callback_.Run(dns_config_);
233 } // namespace net