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" |
26 #include "base/win/registry.h" | 27 #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::NonThreadSafe { | 295 class RegistryWatcher : public base::win::ObjectWatcher::Delegate, |
| 296 public base::NonThreadSafe { |
296 public: | 297 public: |
297 typedef base::Callback<void(bool succeeded)> CallbackType; | 298 typedef base::Callback<void(bool succeeded)> CallbackType; |
298 RegistryWatcher() {} | 299 RegistryWatcher() {} |
299 | 300 |
300 bool Watch(const wchar_t* key, const CallbackType& callback) { | 301 bool Watch(const wchar_t* key, const CallbackType& callback) { |
301 DCHECK(CalledOnValidThread()); | 302 DCHECK(CalledOnValidThread()); |
302 DCHECK(!callback.is_null()); | 303 DCHECK(!callback.is_null()); |
303 DCHECK(callback_.is_null()); | 304 DCHECK(callback_.is_null()); |
304 callback_ = callback; | 305 callback_ = callback; |
305 if (key_.Open(HKEY_LOCAL_MACHINE, key, KEY_NOTIFY) != ERROR_SUCCESS) | 306 if (key_.Open(HKEY_LOCAL_MACHINE, key, KEY_NOTIFY) != ERROR_SUCCESS) |
306 return false; | 307 return false; |
307 | 308 if (key_.StartWatching() != ERROR_SUCCESS) |
308 return key_.StartWatching(base::Bind(&RegistryWatcher::OnObjectSignaled, | 309 return false; |
309 base::Unretained(this))); | 310 if (!watcher_.StartWatching(key_.watch_event(), this)) |
| 311 return false; |
| 312 return true; |
310 } | 313 } |
311 | 314 |
312 void OnObjectSignaled() { | 315 virtual void OnObjectSignaled(HANDLE object) override { |
313 // TODO(vadimt): Remove ScopedProfile below once crbug.com/418183 is fixed. | 316 // TODO(vadimt): Remove ScopedProfile below once crbug.com/418183 is fixed. |
314 tracked_objects::ScopedProfile tracking_profile( | 317 tracked_objects::ScopedProfile tracking_profile( |
315 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 318 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
316 "RegistryWatcher_OnObjectSignaled")); | 319 "RegistryWatcher_OnObjectSignaled")); |
317 | 320 |
318 DCHECK(CalledOnValidThread()); | 321 DCHECK(CalledOnValidThread()); |
319 DCHECK(!callback_.is_null()); | 322 bool succeeded = (key_.StartWatching() == ERROR_SUCCESS) && |
320 if (key_.StartWatching(base::Bind(&RegistryWatcher::OnObjectSignaled, | 323 watcher_.StartWatching(key_.watch_event(), this); |
321 base::Unretained(this)))) { | 324 if (!succeeded && key_.Valid()) { |
322 callback_.Run(true); | 325 watcher_.StopWatching(); |
323 } else { | 326 key_.StopWatching(); |
324 key_.Close(); | 327 key_.Close(); |
325 callback_.Run(false); | |
326 } | 328 } |
| 329 if (!callback_.is_null()) |
| 330 callback_.Run(succeeded); |
327 } | 331 } |
328 | 332 |
329 private: | 333 private: |
330 CallbackType callback_; | 334 CallbackType callback_; |
331 base::win::RegKey key_; | 335 base::win::RegKey key_; |
| 336 base::win::ObjectWatcher watcher_; |
332 | 337 |
333 DISALLOW_COPY_AND_ASSIGN(RegistryWatcher); | 338 DISALLOW_COPY_AND_ASSIGN(RegistryWatcher); |
334 }; | 339 }; |
335 | 340 |
336 // Returns true iff |address| is DNS address from IPv6 stateless discovery, | 341 // Returns true iff |address| is DNS address from IPv6 stateless discovery, |
337 // i.e., matches fec0:0:0:ffff::{1,2,3}. | 342 // i.e., matches fec0:0:0:ffff::{1,2,3}. |
338 // http://tools.ietf.org/html/draft-ietf-ipngwg-dns-discovery | 343 // http://tools.ietf.org/html/draft-ietf-ipngwg-dns-discovery |
339 bool IsStatelessDiscoveryAddress(const IPAddressNumber& address) { | 344 bool IsStatelessDiscoveryAddress(const IPAddressNumber& address) { |
340 if (address.size() != kIPv6AddressSize) | 345 if (address.size() != kIPv6AddressSize) |
341 return false; | 346 return false; |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
728 bool DnsConfigServiceWin::StartWatching() { | 733 bool DnsConfigServiceWin::StartWatching() { |
729 // TODO(szym): re-start watcher if that makes sense. http://crbug.com/116139 | 734 // TODO(szym): re-start watcher if that makes sense. http://crbug.com/116139 |
730 watcher_.reset(new Watcher(this)); | 735 watcher_.reset(new Watcher(this)); |
731 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus", DNS_CONFIG_WATCH_STARTED, | 736 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus", DNS_CONFIG_WATCH_STARTED, |
732 DNS_CONFIG_WATCH_MAX); | 737 DNS_CONFIG_WATCH_MAX); |
733 return watcher_->Watch(); | 738 return watcher_->Watch(); |
734 } | 739 } |
735 | 740 |
736 void DnsConfigServiceWin::OnConfigChanged(bool succeeded) { | 741 void DnsConfigServiceWin::OnConfigChanged(bool succeeded) { |
737 InvalidateConfig(); | 742 InvalidateConfig(); |
738 config_reader_->WorkNow(); | 743 if (succeeded) { |
739 if (!succeeded) { | 744 config_reader_->WorkNow(); |
| 745 } else { |
740 LOG(ERROR) << "DNS config watch failed."; | 746 LOG(ERROR) << "DNS config watch failed."; |
741 set_watch_failed(true); | 747 set_watch_failed(true); |
742 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus", | 748 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus", |
743 DNS_CONFIG_WATCH_FAILED_CONFIG, | 749 DNS_CONFIG_WATCH_FAILED_CONFIG, |
744 DNS_CONFIG_WATCH_MAX); | 750 DNS_CONFIG_WATCH_MAX); |
745 } | 751 } |
746 } | 752 } |
747 | 753 |
748 void DnsConfigServiceWin::OnHostsChanged(bool succeeded) { | 754 void DnsConfigServiceWin::OnHostsChanged(bool succeeded) { |
749 InvalidateHosts(); | 755 InvalidateHosts(); |
750 if (succeeded) { | 756 if (succeeded) { |
751 hosts_reader_->WorkNow(); | 757 hosts_reader_->WorkNow(); |
752 } else { | 758 } else { |
753 LOG(ERROR) << "DNS hosts watch failed."; | 759 LOG(ERROR) << "DNS hosts watch failed."; |
754 set_watch_failed(true); | 760 set_watch_failed(true); |
755 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus", | 761 UMA_HISTOGRAM_ENUMERATION("AsyncDNS.WatchStatus", |
756 DNS_CONFIG_WATCH_FAILED_HOSTS, | 762 DNS_CONFIG_WATCH_FAILED_HOSTS, |
757 DNS_CONFIG_WATCH_MAX); | 763 DNS_CONFIG_WATCH_MAX); |
758 } | 764 } |
759 } | 765 } |
760 | 766 |
761 } // namespace internal | 767 } // namespace internal |
762 | 768 |
763 // static | 769 // static |
764 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { | 770 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { |
765 return scoped_ptr<DnsConfigService>(new internal::DnsConfigServiceWin()); | 771 return scoped_ptr<DnsConfigService>(new internal::DnsConfigServiceWin()); |
766 } | 772 } |
767 | 773 |
768 } // namespace net | 774 } // namespace net |
OLD | NEW |