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); | 31 const FilePath::CharType* kFilePathConfig = FILE_PATH_LITERAL(_PATH_RESCONF); |
| 28 const FilePath::CharType* kFilePathHosts = FILE_PATH_LITERAL("/etc/hosts"); | 32 const FilePath::CharType* kFilePathHosts = FILE_PATH_LITERAL("/etc/hosts"); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 bool success_; | 82 bool success_; |
| 79 }; | 83 }; |
| 80 | 84 |
| 81 } // namespace | 85 } // namespace |
| 82 | 86 |
| 83 namespace internal { | 87 namespace internal { |
| 84 | 88 |
| 85 DnsConfigServicePosix::DnsConfigServicePosix() | 89 DnsConfigServicePosix::DnsConfigServicePosix() |
| 86 : config_watcher_(new FilePathWatcherWrapper()), | 90 : config_watcher_(new FilePathWatcherWrapper()), |
| 87 hosts_watcher_(new FilePathWatcherWrapper()) { | 91 hosts_watcher_(new FilePathWatcherWrapper()) { |
| 92 #if defined(OS_MACOSX) | |
| 93 notify_watcher_.reset(new NotifyWatcherMac()); | |
| 94 #endif | |
| 88 config_reader_ = new ConfigReader( | 95 config_reader_ = new ConfigReader( |
| 89 base::Bind(&DnsConfigServicePosix::OnConfigRead, | 96 base::Bind(&DnsConfigServicePosix::OnConfigRead, |
| 90 base::Unretained(this))); | 97 base::Unretained(this))); |
| 91 hosts_reader_ = new DnsHostsReader( | 98 hosts_reader_ = new DnsHostsReader( |
| 92 FilePath(kFilePathHosts), | 99 FilePath(kFilePathHosts), |
| 93 base::Bind(&DnsConfigServicePosix::OnHostsRead, | 100 base::Bind(&DnsConfigServicePosix::OnHostsRead, |
| 94 base::Unretained(this))); | 101 base::Unretained(this))); |
| 95 } | 102 } |
| 96 | 103 |
| 97 DnsConfigServicePosix::~DnsConfigServicePosix() { | 104 DnsConfigServicePosix::~DnsConfigServicePosix() { |
| 98 config_reader_->Cancel(); | 105 config_reader_->Cancel(); |
| 99 hosts_reader_->Cancel(); | 106 hosts_reader_->Cancel(); |
| 100 } | 107 } |
| 101 | 108 |
| 102 void DnsConfigServicePosix::Watch(const CallbackType& callback) { | 109 void DnsConfigServicePosix::Watch(const CallbackType& callback) { |
| 103 DCHECK(CalledOnValidThread()); | 110 DCHECK(CalledOnValidThread()); |
| 104 DCHECK(!callback.is_null()); | 111 DCHECK(!callback.is_null()); |
| 105 set_callback(callback); | 112 set_callback(callback); |
| 106 | 113 |
| 114 base::Callback<void(bool succeeded)> config_callback = | |
| 115 base::Bind(&DnsConfigServicePosix::OnConfigChanged, | |
| 116 base::Unretained(this)); | |
| 117 | |
| 107 // Even if watchers fail, we keep the other one as it provides useful signals. | 118 // Even if watchers fail, we keep the other one as it provides useful signals. |
| 108 if (config_watcher_->Watch( | 119 #if defined(OS_MACOSX) |
| 109 FilePath(kFilePathConfig), | 120 // TODO(szym): Use dns_configuration_notify_key() from dnsinfo.h? |
|
Mark Mentovai
2012/04/17 16:27:43
Yes. Don’t hard-code.
(Unless you have a good rea
szym
2012/04/17 19:39:31
#include <dnsinfo.h> does not work on my Mac. It s
Mark Mentovai
2012/04/17 19:47:39
szym wrote:
| |
| 110 base::Bind(&DnsConfigServicePosix::OnConfigChanged, | 121 // TODO(szym): Remove |config_watcher_| and use |notify_watcher_| exclusively. |
|
Mark Mentovai
2012/04/17 16:27:43
…implying that both should just be different imple
szym
2012/04/17 19:39:31
That makes sense. I'm quite confident NotifyWatche
| |
| 111 base::Unretained(this)))) { | 122 const char* kDnsNotifyKey = "com.apple.system.SystemConfiguration." |
| 123 "dns_configuration"; | |
|
Mark Mentovai
2012/04/17 16:27:43
If you were keeping this, which you aren’t, it’d b
| |
| 124 if (notify_watcher_->Watch(kDnsNotifyKey, config_callback) && | |
| 125 config_watcher_->Watch(FilePath(kFilePathConfig), config_callback)) { | |
| 126 #else | |
| 127 if (config_watcher_->Watch(FilePath(kFilePathConfig), config_callback)) { | |
| 128 #endif | |
| 112 OnConfigChanged(true); | 129 OnConfigChanged(true); |
| 113 } else { | 130 } else { |
| 114 OnConfigChanged(false); | 131 OnConfigChanged(false); |
| 115 } | 132 } |
| 116 | 133 |
| 117 if (hosts_watcher_->Watch( | 134 if (hosts_watcher_->Watch( |
| 118 FilePath(kFilePathHosts), | 135 FilePath(kFilePathHosts), |
| 119 base::Bind(&DnsConfigServicePosix::OnHostsChanged, | 136 base::Bind(&DnsConfigServicePosix::OnHostsChanged, |
| 120 base::Unretained(this)))) { | 137 base::Unretained(this)))) { |
| 121 OnHostsChanged(true); | 138 OnHostsChanged(true); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 #endif // !defined(OS_ANDROID) | 216 #endif // !defined(OS_ANDROID) |
| 200 | 217 |
| 201 } // namespace internal | 218 } // namespace internal |
| 202 | 219 |
| 203 // static | 220 // static |
| 204 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { | 221 scoped_ptr<DnsConfigService> DnsConfigService::CreateSystemService() { |
| 205 return scoped_ptr<DnsConfigService>(new internal::DnsConfigServicePosix()); | 222 return scoped_ptr<DnsConfigService>(new internal::DnsConfigServicePosix()); |
| 206 } | 223 } |
| 207 | 224 |
| 208 } // namespace net | 225 } // namespace net |
| OLD | NEW |