diff options
Diffstat (limited to 'net/dns/dns_config_service.cc')
-rw-r--r-- | net/dns/dns_config_service.cc | 82 |
1 files changed, 81 insertions, 1 deletions
diff --git a/net/dns/dns_config_service.cc b/net/dns/dns_config_service.cc index f32f293..ed3f1d2 100644 --- a/net/dns/dns_config_service.cc +++ b/net/dns/dns_config_service.cc @@ -4,6 +4,7 @@ #include "net/dns/dns_config_service.h" +#include "base/file_util.h" #include "net/base/ip_endpoint.h" namespace net { @@ -18,7 +19,7 @@ DnsConfig::DnsConfig() DnsConfig::~DnsConfig() {} -bool DnsConfig::Equals(const DnsConfig& d) const { +bool DnsConfig::EqualsIgnoreHosts(const DnsConfig& d) const { return (nameservers == d.nameservers) && (search == d.search) && (ndots == d.ndots) && @@ -28,5 +29,84 @@ bool DnsConfig::Equals(const DnsConfig& d) const { (edns0 == d.edns0); } +bool DnsConfig::Equals(const DnsConfig& d) const { + return EqualsIgnoreHosts(d) && (hosts == d.hosts); +} + +DnsConfigService::DnsConfigService() + : have_config_(false), + have_hosts_(false) {} + +DnsConfigService::~DnsConfigService() {} + +void DnsConfigService::AddObserver(Observer* observer) { + DCHECK(CalledOnValidThread()); + observers_.AddObserver(observer); + if (have_config_ && have_hosts_) { + observer->OnConfigChanged(dns_config_); + } +} + +void DnsConfigService::RemoveObserver(Observer* observer) { + DCHECK(CalledOnValidThread()); + observers_.RemoveObserver(observer); +} + +void DnsConfigService::OnConfigRead(const DnsConfig& config) { + DCHECK(CalledOnValidThread()); + if (!config.EqualsIgnoreHosts(dns_config_)) { + DnsConfig copy = config; + copy.hosts.swap(dns_config_.hosts); + dns_config_ = copy; + have_config_ = true; + if (have_hosts_) { + FOR_EACH_OBSERVER(Observer, observers_, OnConfigChanged(dns_config_)); + } + } +} + +void DnsConfigService::OnHostsRead(const DnsHosts& hosts) { + DCHECK(CalledOnValidThread()); + if (hosts != dns_config_.hosts || !have_hosts_) { + dns_config_.hosts = hosts; + have_hosts_ = true; + if (have_config_) { + FOR_EACH_OBSERVER(Observer, observers_, OnConfigChanged(dns_config_)); + } + } +} + +DnsHostsReader::DnsHostsReader(DnsConfigService* service) + : service_(service), + success_(false) { + DCHECK(service); +} + +// Reads the contents of the file at |path| into |str| if the total length is +// less than |size|. +static bool ReadFile(const FilePath& path, int64 size, std::string* str) { + int64 sz; + if (!file_util::GetFileSize(path, &sz) || sz > size) + return false; + return file_util::ReadFileToString(path, str); +} + +void DnsHostsReader::DoRead() { + success_ = false; + std::string contents; + const int64 kMaxHostsSize = 1 << 16; + if (ReadFile(get_path(), kMaxHostsSize, &contents)) { + success_ = true; + ParseHosts(contents, &dns_hosts_); + } +} + +void DnsHostsReader::OnReadFinished() { + if (success_) + service_->OnHostsRead(dns_hosts_); +} + +DnsHostsReader::~DnsHostsReader() {} + } // namespace net |