Chromium Code Reviews| 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 // Normally defined in dnsinfo.h in configd. | |
|
Mark Mentovai
2012/04/17 19:53:54
You can do this:
extern "C" {
// From 10.7.3 con
szym
2012/04/17 20:14:31
I wish this worked. Unfortunately, it won't link (
Mark Mentovai
2012/04/17 20:22:20
szym wrote:
| |
| 90 const char* kDnsNotifyKey = | |
|
Mark Mentovai
2012/04/17 19:53:54
If you’re sticking with a const char*, make it sta
| |
| 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 const FilePath::CharType* kFilePathConfig = FILE_PATH_LITERAL(_PATH_RESCONF); | |
|
Mark Mentovai
2012/04/17 19:53:54
This, too, can be static or in an anonymous namesp
| |
| 101 | |
| 102 class DnsConfigServicePosix::ConfigWatcher : public FilePathWatcherWrapper { | |
| 103 public: | |
| 104 bool Watch(const base::Callback<void(bool succeeded)>& callback) { | |
| 105 return FilePathWatcherWrapper:Watch(FilePath(kFilePathConfig), callback); | |
| 106 } | |
| 107 }; | |
| 108 #endif | |
| 109 | |
| 85 DnsConfigServicePosix::DnsConfigServicePosix() | 110 DnsConfigServicePosix::DnsConfigServicePosix() |
| 86 : config_watcher_(new FilePathWatcherWrapper()), | 111 : config_watcher_(new ConfigWatcher()), |
| 87 hosts_watcher_(new FilePathWatcherWrapper()) { | 112 hosts_watcher_(new FilePathWatcherWrapper()) { |
| 88 config_reader_ = new ConfigReader( | 113 config_reader_ = new ConfigReader( |
| 89 base::Bind(&DnsConfigServicePosix::OnConfigRead, | 114 base::Bind(&DnsConfigServicePosix::OnConfigRead, |
| 90 base::Unretained(this))); | 115 base::Unretained(this))); |
| 91 hosts_reader_ = new DnsHostsReader( | 116 hosts_reader_ = new DnsHostsReader( |
| 92 FilePath(kFilePathHosts), | 117 FilePath(kFilePathHosts), |
| 93 base::Bind(&DnsConfigServicePosix::OnHostsRead, | 118 base::Bind(&DnsConfigServicePosix::OnHostsRead, |
| 94 base::Unretained(this))); | 119 base::Unretained(this))); |
| 95 } | 120 } |
| 96 | 121 |
| 97 DnsConfigServicePosix::~DnsConfigServicePosix() { | 122 DnsConfigServicePosix::~DnsConfigServicePosix() { |
| 98 config_reader_->Cancel(); | 123 config_reader_->Cancel(); |
| 99 hosts_reader_->Cancel(); | 124 hosts_reader_->Cancel(); |
| 100 } | 125 } |
| 101 | 126 |
| 102 void DnsConfigServicePosix::Watch(const CallbackType& callback) { | 127 void DnsConfigServicePosix::Watch(const CallbackType& callback) { |
| 103 DCHECK(CalledOnValidThread()); | 128 DCHECK(CalledOnValidThread()); |
| 104 DCHECK(!callback.is_null()); | 129 DCHECK(!callback.is_null()); |
| 105 set_callback(callback); | 130 set_callback(callback); |
| 106 | 131 |
| 107 // Even if watchers fail, we keep the other one as it provides useful signals. | 132 // Even if watchers fail, we keep the other one as it provides useful signals. |
| 108 if (config_watcher_->Watch( | 133 if (config_watcher_->Watch( |
| 109 FilePath(kFilePathConfig), | 134 base::Bind(&DnsConfigServicePosix::OnConfigChanged, |
| 110 base::Bind(&DnsConfigServicePosix::OnConfigChanged, | 135 base::Unretained(this)))) { |
| 111 base::Unretained(this)))) { | |
| 112 OnConfigChanged(true); | 136 OnConfigChanged(true); |
| 113 } else { | 137 } else { |
| 114 OnConfigChanged(false); | 138 OnConfigChanged(false); |
| 115 } | 139 } |
| 116 | 140 |
| 117 if (hosts_watcher_->Watch( | 141 if (hosts_watcher_->Watch( |
| 118 FilePath(kFilePathHosts), | 142 FilePath(kFilePathHosts), |
| 119 base::Bind(&DnsConfigServicePosix::OnHostsChanged, | 143 base::Bind(&DnsConfigServicePosix::OnHostsChanged, |
| 120 base::Unretained(this)))) { | 144 base::Unretained(this)))) { |
| 121 OnHostsChanged(true); | 145 OnHostsChanged(true); |
| 122 } else { | 146 } else { |
| 123 OnHostsChanged(false); | 147 OnHostsChanged(false); |
| 124 } | 148 } |
| 125 } | 149 } |
| 126 | 150 |
| 127 void DnsConfigServicePosix::OnConfigChanged(bool watch_succeeded) { | 151 void DnsConfigServicePosix::OnConfigChanged(bool watch_succeeded) { |
| 128 InvalidateConfig(); | 152 InvalidateConfig(); |
| 129 // We don't trust a config that we cannot watch in the future. | 153 // 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 | 154 // 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) | 223 #endif // !defined(OS_ANDROID) |
| 200 | 224 |
| 201 } // namespace internal | 225 } // namespace internal |
| 202 | 226 |
| 203 // static | 227 // static |
| 204 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { | 228 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { |
| 205 return scoped_ptr<DnsConfigService>(new internal::DnsConfigServicePosix()); | 229 return scoped_ptr<DnsConfigService>(new internal::DnsConfigServicePosix()); |
| 206 } | 230 } |
| 207 | 231 |
| 208 } // namespace net | 232 } // namespace net |
| OLD | NEW |