| 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_posix.h" | 5 #include "net/dns/dns_config_service_posix.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "net/base/ip_endpoint.h" | 14 #include "net/base/ip_endpoint.h" |
| 15 #include "net/base/net_util.h" | 15 #include "net/base/net_util.h" |
| 16 #include "net/dns/file_path_watcher_wrapper.h" | 16 #include "net/dns/file_path_watcher_wrapper.h" |
| 17 #include "net/dns/serial_worker.h" | 17 #include "net/dns/serial_worker.h" |
| 18 | 18 |
| 19 #if defined(OS_MACOSX) |
| 20 #include "net/dns/notify_watcher_mac.h" |
| 21 #endif |
| 22 |
| 19 #ifndef _PATH_RESCONF // Normally defined in <resolv.h> | 23 #ifndef _PATH_RESCONF // Normally defined in <resolv.h> |
| 20 #define _PATH_RESCONF "/etc/resolv.conf" | 24 #define _PATH_RESCONF "/etc/resolv.conf" |
| 21 #endif | 25 #endif |
| 22 | 26 |
| 23 namespace net { | 27 namespace net { |
| 24 | 28 |
| 25 namespace { | 29 namespace { |
| 26 | 30 |
| 27 const FilePath::CharType* kFilePathConfig = FILE_PATH_LITERAL(_PATH_RESCONF); | |
| 28 const FilePath::CharType* kFilePathHosts = FILE_PATH_LITERAL("/etc/hosts"); | 31 const FilePath::CharType* kFilePathHosts = FILE_PATH_LITERAL("/etc/hosts"); |
| 29 | 32 |
| 30 // A SerialWorker that uses libresolv to initialize res_state and converts | 33 // A SerialWorker that uses libresolv to initialize res_state and converts |
| 31 // it to DnsConfig. | 34 // it to DnsConfig. |
| 32 class ConfigReader : public SerialWorker { | 35 class ConfigReader : public SerialWorker { |
| 33 public: | 36 public: |
| 34 typedef base::Callback<void(const DnsConfig& config)> CallbackType; | 37 typedef base::Callback<void(const DnsConfig& config)> CallbackType; |
| 35 explicit ConfigReader(const CallbackType& callback) | 38 explicit ConfigReader(const CallbackType& callback) |
| 36 : callback_(callback), | 39 : callback_(callback), |
| 37 success_(false) {} | 40 success_(false) {} |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 CallbackType callback_; | 78 CallbackType callback_; |
| 76 // Written in DoWork, read in OnWorkFinished, no locking necessary. | 79 // Written in DoWork, read in OnWorkFinished, no locking necessary. |
| 77 DnsConfig dns_config_; | 80 DnsConfig dns_config_; |
| 78 bool success_; | 81 bool success_; |
| 79 }; | 82 }; |
| 80 | 83 |
| 81 } // namespace | 84 } // namespace |
| 82 | 85 |
| 83 namespace internal { | 86 namespace internal { |
| 84 | 87 |
| 88 #if defined(OS_MACOSX) |
| 89 // From 10.7.3 configd-395.10/dnsinfo/dnsinfo.h |
| 90 static const char* kDnsNotifyKey = |
| 91 "com.apple.system.SystemConfiguration.dns_configuration"; |
| 92 |
| 93 class DnsConfigServicePosix::ConfigWatcher : public NotifyWatcherMac { |
| 94 public: |
| 95 bool Watch(const base::Callback<void(bool succeeded)>& callback) { |
| 96 return NotifyWatcherMac::Watch(kDnsNotifyKey, callback); |
| 97 } |
| 98 }; |
| 99 #else |
| 100 static const FilePath::CharType* kFilePathConfig = |
| 101 FILE_PATH_LITERAL(_PATH_RESCONF); |
| 102 |
| 103 class DnsConfigServicePosix::ConfigWatcher : public FilePathWatcherWrapper { |
| 104 public: |
| 105 bool Watch(const base::Callback<void(bool succeeded)>& callback) { |
| 106 return FilePathWatcherWrapper::Watch(FilePath(kFilePathConfig), callback); |
| 107 } |
| 108 }; |
| 109 #endif |
| 110 |
| 85 DnsConfigServicePosix::DnsConfigServicePosix() | 111 DnsConfigServicePosix::DnsConfigServicePosix() |
| 86 : config_watcher_(new FilePathWatcherWrapper()), | 112 : config_watcher_(new ConfigWatcher()), |
| 87 hosts_watcher_(new FilePathWatcherWrapper()) { | 113 hosts_watcher_(new FilePathWatcherWrapper()) { |
| 88 config_reader_ = new ConfigReader( | 114 config_reader_ = new ConfigReader( |
| 89 base::Bind(&DnsConfigServicePosix::OnConfigRead, | 115 base::Bind(&DnsConfigServicePosix::OnConfigRead, |
| 90 base::Unretained(this))); | 116 base::Unretained(this))); |
| 91 hosts_reader_ = new DnsHostsReader( | 117 hosts_reader_ = new DnsHostsReader( |
| 92 FilePath(kFilePathHosts), | 118 FilePath(kFilePathHosts), |
| 93 base::Bind(&DnsConfigServicePosix::OnHostsRead, | 119 base::Bind(&DnsConfigServicePosix::OnHostsRead, |
| 94 base::Unretained(this))); | 120 base::Unretained(this))); |
| 95 } | 121 } |
| 96 | 122 |
| 97 DnsConfigServicePosix::~DnsConfigServicePosix() { | 123 DnsConfigServicePosix::~DnsConfigServicePosix() { |
| 98 config_reader_->Cancel(); | 124 config_reader_->Cancel(); |
| 99 hosts_reader_->Cancel(); | 125 hosts_reader_->Cancel(); |
| 100 } | 126 } |
| 101 | 127 |
| 102 void DnsConfigServicePosix::Watch(const CallbackType& callback) { | 128 void DnsConfigServicePosix::Watch(const CallbackType& callback) { |
| 103 DCHECK(CalledOnValidThread()); | 129 DCHECK(CalledOnValidThread()); |
| 104 DCHECK(!callback.is_null()); | 130 DCHECK(!callback.is_null()); |
| 105 set_callback(callback); | 131 set_callback(callback); |
| 106 | 132 |
| 107 // Even if watchers fail, we keep the other one as it provides useful signals. | 133 // Even if watchers fail, we keep the other one as it provides useful signals. |
| 108 if (config_watcher_->Watch( | 134 if (config_watcher_->Watch( |
| 109 FilePath(kFilePathConfig), | 135 base::Bind(&DnsConfigServicePosix::OnConfigChanged, |
| 110 base::Bind(&DnsConfigServicePosix::OnConfigChanged, | 136 base::Unretained(this)))) { |
| 111 base::Unretained(this)))) { | |
| 112 OnConfigChanged(true); | 137 OnConfigChanged(true); |
| 113 } else { | 138 } else { |
| 114 OnConfigChanged(false); | 139 OnConfigChanged(false); |
| 115 } | 140 } |
| 116 | 141 |
| 117 if (hosts_watcher_->Watch( | 142 if (hosts_watcher_->Watch( |
| 118 FilePath(kFilePathHosts), | 143 FilePath(kFilePathHosts), |
| 119 base::Bind(&DnsConfigServicePosix::OnHostsChanged, | 144 base::Bind(&DnsConfigServicePosix::OnHostsChanged, |
| 120 base::Unretained(this)))) { | 145 base::Unretained(this)))) { |
| 121 OnHostsChanged(true); | 146 OnHostsChanged(true); |
| 122 } else { | 147 } else { |
| 123 OnHostsChanged(false); | 148 OnHostsChanged(false); |
| 124 } | 149 } |
| 125 } | 150 } |
| 126 | 151 |
| 127 void DnsConfigServicePosix::OnConfigChanged(bool watch_succeeded) { | 152 void DnsConfigServicePosix::OnConfigChanged(bool watch_succeeded) { |
| 128 InvalidateConfig(); | 153 InvalidateConfig(); |
| 129 // We don't trust a config that we cannot watch in the future. | 154 // We don't trust a config that we cannot watch in the future. |
| 130 // TODO(szym): re-start watcher if that makes sense. http://crbug.com/116139 | 155 // TODO(szym): re-start watcher if that makes sense. http://crbug.com/116139 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 #endif // !defined(OS_ANDROID) | 224 #endif // !defined(OS_ANDROID) |
| 200 | 225 |
| 201 } // namespace internal | 226 } // namespace internal |
| 202 | 227 |
| 203 // static | 228 // static |
| 204 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { | 229 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { |
| 205 return scoped_ptr<DnsConfigService>(new internal::DnsConfigServicePosix()); | 230 return scoped_ptr<DnsConfigService>(new internal::DnsConfigServicePosix()); |
| 206 } | 231 } |
| 207 | 232 |
| 208 } // namespace net | 233 } // namespace net |
| OLD | NEW |