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 "net/dns/dns_config_service_win.h" | 5 #include "net/dns/dns_config_service_win.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/files/file_path_watcher.h" | 14 #include "base/files/file_path_watcher.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
18 #include "base/profiler/scoped_profile.h" | 18 #include "base/profiler/scoped_profile.h" |
19 #include "base/strings/string_split.h" | 19 #include "base/strings/string_split.h" |
20 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
22 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
23 #include "base/threading/non_thread_safe.h" | 23 #include "base/threading/non_thread_safe.h" |
24 #include "base/threading/thread_restrictions.h" | 24 #include "base/threading/thread_restrictions.h" |
25 #include "base/time/time.h" | 25 #include "base/time/time.h" |
26 #include "base/win/object_watcher.h" | |
27 #include "base/win/registry.h" | 26 #include "base/win/registry.h" |
27 #include "base/win/scoped_handle.h" | |
28 #include "base/win/windows_version.h" | 28 #include "base/win/windows_version.h" |
29 #include "net/base/net_util.h" | 29 #include "net/base/net_util.h" |
30 #include "net/base/network_change_notifier.h" | 30 #include "net/base/network_change_notifier.h" |
31 #include "net/dns/dns_hosts.h" | 31 #include "net/dns/dns_hosts.h" |
32 #include "net/dns/dns_protocol.h" | 32 #include "net/dns/dns_protocol.h" |
33 #include "net/dns/serial_worker.h" | 33 #include "net/dns/serial_worker.h" |
34 #include "url/url_canon.h" | 34 #include "url/url_canon.h" |
35 | 35 |
36 #pragma comment(lib, "iphlpapi.lib") | 36 #pragma comment(lib, "iphlpapi.lib") |
37 | 37 |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
285 } else if (!have_ipv6 && (ipe.GetFamily() == ADDRESS_FAMILY_IPV6)) { | 285 } else if (!have_ipv6 && (ipe.GetFamily() == ADDRESS_FAMILY_IPV6)) { |
286 have_ipv6 = true; | 286 have_ipv6 = true; |
287 (*hosts)[DnsHostsKey(localname, ADDRESS_FAMILY_IPV6)] = ipe.address(); | 287 (*hosts)[DnsHostsKey(localname, ADDRESS_FAMILY_IPV6)] = ipe.address(); |
288 } | 288 } |
289 } | 289 } |
290 } | 290 } |
291 return HOSTS_PARSE_WIN_OK; | 291 return HOSTS_PARSE_WIN_OK; |
292 } | 292 } |
293 | 293 |
294 // Watches a single registry key for changes. | 294 // Watches a single registry key for changes. |
295 class RegistryWatcher : public base::win::ObjectWatcher::Delegate, | 295 class RegistryWatcher : public base::NonThreadSafe { |
296 public base::NonThreadSafe { | |
297 public: | 296 public: |
298 typedef base::Callback<void(bool succeeded)> CallbackType; | 297 typedef base::Callback<void(bool succeeded)> CallbackType; |
299 RegistryWatcher() {} | 298 RegistryWatcher() {} |
300 | 299 |
301 bool Watch(const wchar_t* key, const CallbackType& callback) { | 300 bool Watch(const wchar_t* key, const CallbackType& callback) { |
302 DCHECK(CalledOnValidThread()); | 301 DCHECK(CalledOnValidThread()); |
303 DCHECK(!callback.is_null()); | 302 DCHECK(!callback.is_null()); |
304 DCHECK(callback_.is_null()); | 303 DCHECK(callback_.is_null()); |
305 callback_ = callback; | 304 callback_ = callback; |
306 if (key_.Open(HKEY_LOCAL_MACHINE, key, KEY_NOTIFY) != ERROR_SUCCESS) | 305 if (key_.Open(HKEY_LOCAL_MACHINE, key, KEY_NOTIFY) != ERROR_SUCCESS) |
307 return false; | 306 return false; |
308 if (key_.StartWatching() != ERROR_SUCCESS) | 307 |
309 return false; | 308 return key_.StartWatching(base::Bind(&RegistryWatcher::OnObjectSignaled, |
310 if (!watcher_.StartWatching(key_.watch_event(), this)) | 309 base::Unretained(this))); |
311 return false; | |
312 return true; | |
313 } | 310 } |
314 | 311 |
315 virtual void OnObjectSignaled(HANDLE object) OVERRIDE { | 312 void OnObjectSignaled() { |
316 // TODO(vadimt): Remove ScopedProfile below once crbug.com/418183 is fixed. | 313 // TODO(vadimt): Remove ScopedProfile below once crbug.com/418183 is fixed. |
317 tracked_objects::ScopedProfile tracking_profile( | 314 tracked_objects::ScopedProfile tracking_profile( |
318 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 315 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
319 "RegistryWatcher_OnObjectSignaled")); | 316 "RegistryWatcher_OnObjectSignaled")); |
320 | 317 |
321 DCHECK(CalledOnValidThread()); | 318 DCHECK(CalledOnValidThread()); |
322 bool succeeded = (key_.StartWatching() == ERROR_SUCCESS) && | 319 DCHECK(!callback_.is_null()); |
323 watcher_.StartWatching(key_.watch_event(), this); | 320 if (key_.StartWatching(base::Bind(&RegistryWatcher::OnObjectSignaled, |
324 if (!succeeded && key_.Valid()) { | 321 base::Unretained(this)))) { |
325 watcher_.StopWatching(); | 322 callback_.Run(true); |
326 key_.StopWatching(); | 323 } else { |
327 key_.Close(); | 324 key_.Close(); |
325 callback_.Run(false); | |
328 } | 326 } |
329 if (!callback_.is_null()) | |
330 callback_.Run(succeeded); | |
331 } | 327 } |
332 | 328 |
333 private: | 329 private: |
334 CallbackType callback_; | 330 CallbackType callback_; |
335 base::win::RegKey key_; | 331 base::win::RegKey key_; |
336 base::win::ObjectWatcher watcher_; | |
337 | 332 |
338 DISALLOW_COPY_AND_ASSIGN(RegistryWatcher); | 333 DISALLOW_COPY_AND_ASSIGN(RegistryWatcher); |
339 }; | 334 }; |
340 | 335 |
341 // Returns true iff |address| is DNS address from IPv6 stateless discovery, | 336 // Returns true iff |address| is DNS address from IPv6 stateless discovery, |
342 // i.e., matches fec0:0:0:ffff::{1,2,3}. | 337 // i.e., matches fec0:0:0:ffff::{1,2,3}. |
343 // http://tools.ietf.org/html/draft-ietf-ipngwg-dns-discovery | 338 // http://tools.ietf.org/html/draft-ietf-ipngwg-dns-discovery |
344 bool IsStatelessDiscoveryAddress(const IPAddressNumber& address) { | 339 bool IsStatelessDiscoveryAddress(const IPAddressNumber& address) { |
345 if (address.size() != kIPv6AddressSize) | 340 if (address.size() != kIPv6AddressSize) |
346 return false; | 341 return false; |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
733 bool DnsConfigServiceWin::StartWatching() { | 728 bool DnsConfigServiceWin::StartWatching() { |
734 // TODO(szym): re-start watcher if that makes sense. http://crbug.com/116139 | 729 // TODO(szym): re-start watcher if that makes sense. http://crbug.com/116139 |
735 watcher_.reset(new Watcher(this)); | 730 watcher_.reset(new Watcher(this)); |
736 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus", DNS_CONFIG_WATCH_STARTED, | 731 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus", DNS_CONFIG_WATCH_STARTED, |
737 DNS_CONFIG_WATCH_MAX); | 732 DNS_CONFIG_WATCH_MAX); |
738 return watcher_->Watch(); | 733 return watcher_->Watch(); |
739 } | 734 } |
740 | 735 |
741 void DnsConfigServiceWin::OnConfigChanged(bool succeeded) { | 736 void DnsConfigServiceWin::OnConfigChanged(bool succeeded) { |
742 InvalidateConfig(); | 737 InvalidateConfig(); |
743 if (succeeded) { | 738 config_reader_->WorkNow(); |
744 config_reader_->WorkNow(); | 739 if (!succeeded) { |
eroman
2014/10/10 21:34:35
What is the reason for this change?
rvargas (doing something else)
2014/10/10 22:22:05
|succeeded| means that the next notification was s
| |
745 } else { | |
746 LOG(ERROR) << "DNS config watch failed."; | 740 LOG(ERROR) << "DNS config watch failed."; |
747 set_watch_failed(true); | 741 set_watch_failed(true); |
748 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus", | 742 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus", |
749 DNS_CONFIG_WATCH_FAILED_CONFIG, | 743 DNS_CONFIG_WATCH_FAILED_CONFIG, |
750 DNS_CONFIG_WATCH_MAX); | 744 DNS_CONFIG_WATCH_MAX); |
751 } | 745 } |
752 } | 746 } |
753 | 747 |
754 void DnsConfigServiceWin::OnHostsChanged(bool succeeded) { | 748 void DnsConfigServiceWin::OnHostsChanged(bool succeeded) { |
755 InvalidateHosts(); | 749 InvalidateHosts(); |
756 if (succeeded) { | 750 if (succeeded) { |
757 hosts_reader_->WorkNow(); | 751 hosts_reader_->WorkNow(); |
758 } else { | 752 } else { |
759 LOG(ERROR) << "DNS hosts watch failed."; | 753 LOG(ERROR) << "DNS hosts watch failed."; |
760 set_watch_failed(true); | 754 set_watch_failed(true); |
761 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus", | 755 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus", |
762 DNS_CONFIG_WATCH_FAILED_HOSTS, | 756 DNS_CONFIG_WATCH_FAILED_HOSTS, |
763 DNS_CONFIG_WATCH_MAX); | 757 DNS_CONFIG_WATCH_MAX); |
764 } | 758 } |
765 } | 759 } |
766 | 760 |
767 } // namespace internal | 761 } // namespace internal |
768 | 762 |
769 // static | 763 // static |
770 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { | 764 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { |
771 return scoped_ptr<DnsConfigService>(new internal::DnsConfigServiceWin()); | 765 return scoped_ptr<DnsConfigService>(new internal::DnsConfigServiceWin()); |
772 } | 766 } |
773 | 767 |
774 } // namespace net | 768 } // namespace net |
OLD | NEW |