diff options
author | szym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-03 00:17:29 +0000 |
---|---|---|
committer | szym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-09-03 00:17:29 +0000 |
commit | 6998a66de5bb5889f5a31732236bdc9205ca200b (patch) | |
tree | bcc42aff5c80379b344fc71742287442d8c19cec /net/dns/dns_config_service.h | |
parent | db8c36868a4e462a466f30adfe3b3ab1bb78daa6 (diff) | |
download | chromium_src-6998a66de5bb5889f5a31732236bdc9205ca200b.zip chromium_src-6998a66de5bb5889f5a31732236bdc9205ca200b.tar.gz chromium_src-6998a66de5bb5889f5a31732236bdc9205ca200b.tar.bz2 |
Refactoring and further work on DnsConfigService
- Isolate WatchingFileReader for reusability and testability
- DnsHosts and ParseHosts to parse /etc/hosts
BUG=90881
TEST=./net_unittests --gtest_filter='DnsConfig*:DnsHosts*:WatchingFileReader*'
Review URL: http://codereview.chromium.org/7826010
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@99484 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/dns/dns_config_service.h')
-rw-r--r-- | net/dns/dns_config_service.h | 67 |
1 files changed, 56 insertions, 11 deletions
diff --git a/net/dns/dns_config_service.h b/net/dns/dns_config_service.h index 535dcc1..6ceeaa6 100644 --- a/net/dns/dns_config_service.h +++ b/net/dns/dns_config_service.h @@ -6,16 +6,20 @@ #define NET_DNS_DNS_CONFIG_SERVICE_H_ #pragma once +#include <map> #include <string> #include <vector> +#include "base/gtest_prod_util.h" +#include "base/observer_list.h" #include "base/time.h" +#include "net/base/ip_endpoint.h" // win requires size of IPEndPoint #include "net/base/net_export.h" +#include "net/dns/dns_hosts.h" +#include "net/dns/watching_file_reader.h" namespace net { -class IPEndPoint; - // DnsConfig stores configuration of the system resolver. struct NET_EXPORT_PRIVATE DnsConfig { DnsConfig(); @@ -23,7 +27,9 @@ struct NET_EXPORT_PRIVATE DnsConfig { bool Equals(const DnsConfig& d) const; - bool Valid() const { + bool EqualsIgnoreHosts(const DnsConfig& d) const; + + bool IsValid() const { return !nameservers.empty(); } @@ -33,9 +39,12 @@ struct NET_EXPORT_PRIVATE DnsConfig { // is less than |ndots|. std::vector<std::string> search; + DnsHosts hosts; + // Resolver options; see man resolv.conf. // TODO(szym): use |ndots| and |search| to determine the sequence of FQDNs // to query given a specific name. + // TODO(szym): implement DNS Devolution for windows // Minimum number of dots before global resolution precedes |search|. int ndots; @@ -49,9 +58,11 @@ struct NET_EXPORT_PRIVATE DnsConfig { bool edns0; }; + // Service for watching when the system DNS settings have changed. // Depending on the platform, watches files in /etc/ or win registry. -class NET_EXPORT_PRIVATE DnsConfigService { +class NET_EXPORT_PRIVATE DnsConfigService + : NON_EXPORTED_BASE(public base::NonThreadSafe) { public: // Callback interface for the client. The observer is called on the same // thread as Watch(). Observer must outlive the service. @@ -59,30 +70,64 @@ class NET_EXPORT_PRIVATE DnsConfigService { public: virtual ~Observer() {} - // Called only when |dns_config| is different from the last check. + // Called only when |dns_config| is different from the last call. virtual void OnConfigChanged(const DnsConfig& dns_config) = 0; }; // Creates the platform-specific DnsConfigService. static DnsConfigService* CreateSystemService(); - DnsConfigService() {} - virtual ~DnsConfigService() {} + DnsConfigService(); + virtual ~DnsConfigService(); // Immediately starts watching system configuration for changes and attempts // to read the configuration. For some platform implementations, the current // thread must have an IO loop (for base::files::FilePathWatcher). - virtual void Watch() = 0; + virtual void Watch() {} // If a config is available, |observer| will immediately be called with // OnConfigChanged. - virtual void AddObserver(Observer* observer) = 0; - virtual void RemoveObserver(Observer* observer) = 0; - + void AddObserver(Observer* observer); + void RemoveObserver(Observer* observer); + + protected: + FRIEND_TEST_ALL_PREFIXES(DnsConfigServiceTest, NotifyOnChange); + friend class DnsHostsReader; + // To be called with new config. |config|.hosts is ignored. + virtual void OnConfigRead(const DnsConfig& config); + // To be called with new hosts. Rest of the config is assumed unchanged. + virtual void OnHostsRead(const DnsHosts& hosts); + + DnsConfig dns_config_; + // True after first OnConfigRead and OnHostsRead; indicate complete config. + bool have_config_; + bool have_hosts_; + + ObserverList<Observer> observers_; private: DISALLOW_COPY_AND_ASSIGN(DnsConfigService); }; +// A WatchingFileReader that reads a HOSTS file and notifies +// DnsConfigService::OnHostsRead(). +class DnsHostsReader : public WatchingFileReader { + public: + explicit DnsHostsReader(DnsConfigService* service); + + virtual void DoRead() OVERRIDE; + virtual void OnReadFinished() OVERRIDE; + + private: + virtual ~DnsHostsReader(); + + DnsConfigService* service_; + // Written in DoRead, read in OnReadFinished, no locking necessary. + DnsHosts dns_hosts_; + bool success_; + + DISALLOW_COPY_AND_ASSIGN(DnsHostsReader); +}; + } // namespace net #endif // NET_DNS_DNS_CONFIG_SERVICE_H_ |