OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "net/dns/dns_config_watcher.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" |
| 11 #include "base/callback.h" |
| 12 #include "base/file_path.h" |
| 13 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "net/base/network_change_notifier.h" |
| 16 #include "net/dns/file_path_watcher_wrapper.h" |
| 17 |
| 18 #if defined(OS_MACOSX) |
| 19 #include "net/dns/notify_watcher_mac.h" |
| 20 #endif |
| 21 |
| 22 namespace net { |
| 23 namespace internal { |
| 24 |
| 25 namespace { |
| 26 |
| 27 const FilePath::CharType* kFilePathHosts = FILE_PATH_LITERAL("/etc/hosts"); |
| 28 |
| 29 #if defined(OS_MACOSX) |
| 30 // From 10.7.3 configd-395.10/dnsinfo/dnsinfo.h |
| 31 static const char* kDnsNotifyKey = |
| 32 "com.apple.system.SystemConfiguration.dns_configuration"; |
| 33 |
| 34 class ConfigWatcher { |
| 35 public: |
| 36 bool Watch(const base::Callback<void(bool succeeded)>& callback) { |
| 37 return watcher_.Watch(kDnsNotifyKey, callback); |
| 38 } |
| 39 private: |
| 40 NotifyWatcherMac watcher_; |
| 41 }; |
| 42 #else |
| 43 |
| 44 #ifndef _PATH_RESCONF // Normally defined in <resolv.h> |
| 45 #define _PATH_RESCONF "/etc/resolv.conf" |
| 46 #endif |
| 47 |
| 48 static const FilePath::CharType* kFilePathConfig = |
| 49 FILE_PATH_LITERAL(_PATH_RESCONF); |
| 50 |
| 51 class ConfigWatcher { |
| 52 public: |
| 53 bool Watch(const base::Callback<void(bool succeeded)>& callback) { |
| 54 return watcher_.Watch(FilePath(kFilePathConfig), callback); |
| 55 } |
| 56 private: |
| 57 FilePathWatcherWrapper watcher_; |
| 58 }; |
| 59 #endif |
| 60 |
| 61 } // namespace |
| 62 |
| 63 class DnsConfigWatcher::Core { |
| 64 public: |
| 65 Core() {} |
| 66 ~Core() {} |
| 67 |
| 68 bool Watch() { |
| 69 bool success = true; |
| 70 if (!config_watcher_.Watch(base::Bind(&Core::OnConfigChanged, |
| 71 base::Unretained(this)))) { |
| 72 LOG(ERROR) << "DNS config watch failed to start."; |
| 73 success = false; |
| 74 } |
| 75 if (!hosts_watcher_.Watch(FilePath(kFilePathHosts), |
| 76 base::Bind(&Core::OnHostsChanged, |
| 77 base::Unretained(this)))) { |
| 78 LOG(ERROR) << "DNS hosts watch failed to start."; |
| 79 success = false; |
| 80 } |
| 81 return success; |
| 82 } |
| 83 |
| 84 private: |
| 85 void OnConfigChanged(bool succeeded) { |
| 86 if (succeeded) { |
| 87 NetworkChangeNotifier::NotifyObserversOfDNSChange( |
| 88 NetworkChangeNotifier::CHANGE_DNS_SETTINGS); |
| 89 } else { |
| 90 LOG(ERROR) << "DNS config watch failed."; |
| 91 NetworkChangeNotifier::NotifyObserversOfDNSChange( |
| 92 NetworkChangeNotifier::CHANGE_DNS_WATCH_FAILED); |
| 93 } |
| 94 } |
| 95 |
| 96 void OnHostsChanged(bool succeeded) { |
| 97 if (succeeded) { |
| 98 NetworkChangeNotifier::NotifyObserversOfDNSChange( |
| 99 NetworkChangeNotifier::CHANGE_DNS_HOSTS); |
| 100 } else { |
| 101 LOG(ERROR) << "DNS hosts watch failed."; |
| 102 NetworkChangeNotifier::NotifyObserversOfDNSChange( |
| 103 NetworkChangeNotifier::CHANGE_DNS_WATCH_FAILED); |
| 104 } |
| 105 } |
| 106 |
| 107 ConfigWatcher config_watcher_; |
| 108 FilePathWatcherWrapper hosts_watcher_; |
| 109 |
| 110 DISALLOW_COPY_AND_ASSIGN(Core); |
| 111 }; |
| 112 |
| 113 DnsConfigWatcher::DnsConfigWatcher() {} |
| 114 |
| 115 DnsConfigWatcher::~DnsConfigWatcher() {} |
| 116 |
| 117 void DnsConfigWatcher::Init() { |
| 118 core_.reset(new Core()); |
| 119 if (core_->Watch()) { |
| 120 NetworkChangeNotifier::NotifyObserversOfDNSChange( |
| 121 NetworkChangeNotifier::CHANGE_DNS_WATCH_STARTED); |
| 122 } |
| 123 // TODO(szym): re-start watcher if that makes sense. http://crbug.com/116139 |
| 124 } |
| 125 |
| 126 void DnsConfigWatcher::CleanUp() { |
| 127 core_.reset(); |
| 128 } |
| 129 |
| 130 } // namespace internal |
| 131 } // namespace net |
OLD | NEW |