Chromium Code Reviews| Index: net/dns/dns_config_service_posix.h |
| diff --git a/net/dns/dns_config_service_posix.h b/net/dns/dns_config_service_posix.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..aa10a34818c45f4775080a0846b97473384f73d8 |
| --- /dev/null |
| +++ b/net/dns/dns_config_service_posix.h |
| @@ -0,0 +1,112 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <resolv.h> |
| + |
| +#include "base/compiler_specific.h" |
| +#include "base/files/file_path_watcher.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/observer_list.h" |
| +#include "base/scoped_ptr.h" |
| +#include "net/dns/dns_config_service.h" |
| + |
| +namespace net { |
| + |
| +// Fills in |dns_config| from |res|. Exposed for tests. |
| +bool ConvertResToConfig(const struct __res_state& res, DnsConfig* dns_config); |
| + |
| +// Allows mocking res_ninit. |
| +struct ResolverLib { |
|
cbentzel
2011/08/15 18:16:34
struct's are intended for buckets-of-bits only. On
cbentzel
2011/08/15 18:16:34
You may also want to make this listed as NonThread
szym
2011/08/15 22:02:01
Done.
|
| + virtual ~ResolverLib() {} |
| + virtual int ninit(res_state statp) { |
| + return ::res_ninit(statp); |
| + } |
| + virtual void nclose(res_state statp) { |
| + return ::res_nclose(statp); |
| + } |
|
cbentzel
2011/08/15 18:16:34
DISALLOW_COPY_AND_ASSIGN
szym
2011/08/15 22:02:01
Done.
|
| +}; |
| + |
| +// Allows mocking FilePathWatcher |
| +class FilePathWatcherShim { |
|
cbentzel
2011/08/15 18:16:34
I'm wondering if these should be nested classes of
szym
2011/08/15 22:02:01
Done.
|
| + public: |
| + FilePathWatcherShim(); |
| + virtual ~FilePathWatcherShim() {} |
| + |
| + virtual bool Watch( |
| + const FilePath& path, |
| + base::files::FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT; |
| + private: |
| + scoped_ptr<base::files::FilePathWatcher> watcher_; |
|
cbentzel
2011/08/15 18:16:34
DISALLOW_COPY_AND_ASSIGN
szym
2011/08/15 22:02:01
Done.
|
| +}; |
| + |
| +struct FilePathWatcherFactory { |
|
cbentzel
2011/08/15 18:16:34
Ditto here.
szym
2011/08/15 22:02:01
Done.
|
| + virtual ~FilePathWatcherFactory() {} |
| + virtual FilePathWatcherShim* CreateFilePathWatcher() { |
| + return new FilePathWatcherShim(); |
| + } |
| +}; |
| + |
| +class WatcherDelegate; |
| + |
| +class DnsConfigServicePosix : public DnsConfigService { |
|
cbentzel
2011/08/15 18:16:34
Could this inherit from NonThreadSafe [from base/t
szym
2011/08/15 22:02:01
Done.
|
| + public: |
| + DnsConfigServicePosix(); |
| + virtual ~DnsConfigServicePosix(); |
| + |
| + virtual void AddObserver(Observer* observer) OVERRIDE { |
|
cbentzel
2011/08/15 18:16:34
Don't inline virtual methods. There's a number of
cbentzel
2011/08/15 18:16:34
Thanks for adding OVERRIDE.
szym
2011/08/15 22:02:01
Done. Although only in .h files.
|
| + observers_.AddObserver(observer); |
| + if (have_config_) { |
|
cbentzel
2011/08/15 18:16:34
Good idea, cool.
|
| + observer->OnConfigChanged(dns_config_); |
| + } |
| + } |
| + |
| + virtual void RemoveObserver(Observer* observer) OVERRIDE { |
| + observers_.RemoveObserver(observer); |
| + } |
| + |
| + virtual void Watch() OVERRIDE; |
| + |
| + // Takes ownership of |lib|. Must be set before Watch. |
| + void set_resolver_lib(ResolverLib* lib) { |
| + DCHECK(!watcher_delegate_.get()); |
| + resolver_lib_.reset(lib); |
| + } |
| + |
| + // Takes ownership of |factory|. Must be set before Watch. |
| + void set_watcher_factory(FilePathWatcherFactory* factory) { |
| + DCHECK(!watcher_delegate_.get()); |
| + watcher_factory_.reset(factory); |
| + } |
| + |
| + private: |
| + friend class WatcherDelegate; |
| + |
| + void OnConfigRead(const DnsConfig& config) { |
| + if (!config.Equals(dns_config_)) { |
| + dns_config_ = config; |
| + have_config_ = true; |
| + FOR_EACH_OBSERVER(Observer, observers_, OnConfigChanged(config)); |
| + } |
| + } |
| + |
| + // Configure a FilePathWatcher and install watcher_delegate_. Executed each |
| + // time FilePathWatcher fails. |
| + void StartWatch(); |
| + |
| + DnsConfig dns_config_; |
| + // True after first OnConfigChanged, that is, dns_config_ is valid. |
| + bool have_config_; |
| + |
| + scoped_ptr<ResolverLib> resolver_lib_; |
| + scoped_ptr<FilePathWatcherFactory> watcher_factory_; |
| + scoped_ptr<FilePathWatcherShim> resolv_file_watcher_; |
| + scoped_refptr<WatcherDelegate> watcher_delegate_; |
| + ObserverList<Observer> observers_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DnsConfigServicePosix); |
| +}; |
| + |
| + |
|
cbentzel
2011/08/15 18:16:34
Nit: extra new line
szym
2011/08/15 22:02:01
Done.
|
| +} // namespace net |
| + |