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

Side by Side Diff: net/dns/dns_config_service.cc

Issue 10873018: [net] Move DnsConfigService to NetworkChangeNotifier. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add comment Created 8 years, 3 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 | Annotate | Revision Log
« no previous file with comments | « net/dns/dns_config_service.h ('k') | net/dns/dns_config_service_posix.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "net/dns/dns_config_service.h" 5 #include "net/dns/dns_config_service.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/metrics/histogram.h" 8 #include "base/metrics/histogram.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "net/base/ip_endpoint.h" 10 #include "net/base/ip_endpoint.h"
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 dict->SetInteger("attempts", attempts); 68 dict->SetInteger("attempts", attempts);
69 dict->SetBoolean("rotate", rotate); 69 dict->SetBoolean("rotate", rotate);
70 dict->SetBoolean("edns0", edns0); 70 dict->SetBoolean("edns0", edns0);
71 dict->SetInteger("num_hosts", hosts.size()); 71 dict->SetInteger("num_hosts", hosts.size());
72 72
73 return dict; 73 return dict;
74 } 74 }
75 75
76 76
77 DnsConfigService::DnsConfigService() 77 DnsConfigService::DnsConfigService()
78 : have_config_(false), 78 : watch_failed_(false),
79 have_config_(false),
79 have_hosts_(false), 80 have_hosts_(false),
80 need_update_(false), 81 need_update_(false),
81 last_sent_empty_(true) {} 82 last_sent_empty_(true) {}
82 83
83 DnsConfigService::~DnsConfigService() { 84 DnsConfigService::~DnsConfigService() {
84 // Must always clean up.
85 NetworkChangeNotifier::RemoveDNSObserver(this);
86 } 85 }
87 86
88 void DnsConfigService::Read(const CallbackType& callback) { 87 void DnsConfigService::ReadConfig(const CallbackType& callback) {
89 DCHECK(CalledOnValidThread()); 88 DCHECK(CalledOnValidThread());
90 DCHECK(!callback.is_null()); 89 DCHECK(!callback.is_null());
91 DCHECK(callback_.is_null()); 90 DCHECK(callback_.is_null());
92 callback_ = callback; 91 callback_ = callback;
93 OnDNSChanged(NetworkChangeNotifier::CHANGE_DNS_WATCH_STARTED); 92 ReadNow();
94 } 93 }
95 94
96 void DnsConfigService::Watch(const CallbackType& callback) { 95 void DnsConfigService::WatchConfig(const CallbackType& callback) {
97 DCHECK(CalledOnValidThread()); 96 DCHECK(CalledOnValidThread());
98 DCHECK(!callback.is_null()); 97 DCHECK(!callback.is_null());
99 DCHECK(callback_.is_null()); 98 DCHECK(callback_.is_null());
100 NetworkChangeNotifier::AddDNSObserver(this);
101 callback_ = callback; 99 callback_ = callback;
102 if (NetworkChangeNotifier::IsWatchingDNS()) 100 watch_failed_ = !StartWatching();
103 OnDNSChanged(NetworkChangeNotifier::CHANGE_DNS_WATCH_STARTED); 101 ReadNow();
104 // else: Wait until signal before reading.
105 } 102 }
106 103
107 void DnsConfigService::InvalidateConfig() { 104 void DnsConfigService::InvalidateConfig() {
108 DCHECK(CalledOnValidThread()); 105 DCHECK(CalledOnValidThread());
109 base::TimeTicks now = base::TimeTicks::Now(); 106 base::TimeTicks now = base::TimeTicks::Now();
110 if (!last_invalidate_config_time_.is_null()) { 107 if (!last_invalidate_config_time_.is_null()) {
111 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.ConfigNotifyInterval", 108 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.ConfigNotifyInterval",
112 now - last_invalidate_config_time_); 109 now - last_invalidate_config_time_);
113 } 110 }
114 last_invalidate_config_time_ = now; 111 last_invalidate_config_time_ = now;
(...skipping 27 matching lines...) Expand all
142 need_update_ = true; 139 need_update_ = true;
143 changed = true; 140 changed = true;
144 } 141 }
145 if (!changed && !last_sent_empty_time_.is_null()) { 142 if (!changed && !last_sent_empty_time_.is_null()) {
146 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedConfigInterval", 143 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedConfigInterval",
147 base::TimeTicks::Now() - last_sent_empty_time_); 144 base::TimeTicks::Now() - last_sent_empty_time_);
148 } 145 }
149 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.ConfigChange", changed); 146 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.ConfigChange", changed);
150 147
151 have_config_ = true; 148 have_config_ = true;
152 if (have_hosts_) 149 if (have_hosts_ || watch_failed_)
153 OnCompleteConfig(); 150 OnCompleteConfig();
154 } 151 }
155 152
156 void DnsConfigService::OnHostsRead(const DnsHosts& hosts) { 153 void DnsConfigService::OnHostsRead(const DnsHosts& hosts) {
157 DCHECK(CalledOnValidThread()); 154 DCHECK(CalledOnValidThread());
158 155
159 bool changed = false; 156 bool changed = false;
160 if (hosts != dns_config_.hosts) { 157 if (hosts != dns_config_.hosts) {
161 dns_config_.hosts = hosts; 158 dns_config_.hosts = hosts;
162 need_update_ = true; 159 need_update_ = true;
163 changed = true; 160 changed = true;
164 } 161 }
165 if (!changed && !last_sent_empty_time_.is_null()) { 162 if (!changed && !last_sent_empty_time_.is_null()) {
166 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedHostsInterval", 163 UMA_HISTOGRAM_LONG_TIMES("AsyncDNS.UnchangedHostsInterval",
167 base::TimeTicks::Now() - last_sent_empty_time_); 164 base::TimeTicks::Now() - last_sent_empty_time_);
168 } 165 }
169 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HostsChange", changed); 166 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HostsChange", changed);
170 167
171 have_hosts_ = true; 168 have_hosts_ = true;
172 if (have_config_) 169 if (have_config_ || watch_failed_)
173 OnCompleteConfig(); 170 OnCompleteConfig();
174 } 171 }
175 172
176 void DnsConfigService::StartTimer() { 173 void DnsConfigService::StartTimer() {
177 DCHECK(CalledOnValidThread()); 174 DCHECK(CalledOnValidThread());
178 if (last_sent_empty_) { 175 if (last_sent_empty_) {
179 DCHECK(!timer_.IsRunning()); 176 DCHECK(!timer_.IsRunning());
180 return; // No need to withdraw again. 177 return; // No need to withdraw again.
181 } 178 }
182 timer_.Stop(); 179 timer_.Stop();
(...skipping 26 matching lines...) Expand all
209 last_sent_empty_time_ = base::TimeTicks::Now(); 206 last_sent_empty_time_ = base::TimeTicks::Now();
210 callback_.Run(DnsConfig()); 207 callback_.Run(DnsConfig());
211 } 208 }
212 209
213 void DnsConfigService::OnCompleteConfig() { 210 void DnsConfigService::OnCompleteConfig() {
214 timer_.Stop(); 211 timer_.Stop();
215 if (!need_update_) 212 if (!need_update_)
216 return; 213 return;
217 need_update_ = false; 214 need_update_ = false;
218 last_sent_empty_ = false; 215 last_sent_empty_ = false;
219 callback_.Run(dns_config_); 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_);
221 }
220 } 222 }
221 223
222 } // namespace net 224 } // namespace net
223 225
OLDNEW
« no previous file with comments | « net/dns/dns_config_service.h ('k') | net/dns/dns_config_service_posix.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698