OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef NET_DNS_DNS_CONFIG_SERVICE_POSIX_H_ |
| 6 #define NET_DNS_DNS_CONFIG_SERVICE_POSIX_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <resolv.h> |
| 10 |
| 11 #include "base/compiler_specific.h" |
| 12 #include "base/files/file_path_watcher.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/observer_list.h" |
| 15 #include "base/scoped_ptr.h" |
| 16 #include "base/threading/non_thread_safe.h" |
| 17 #include "net/base/net_export.h" |
| 18 #include "net/dns/dns_config_service.h" |
| 19 |
| 20 namespace net { |
| 21 |
| 22 class NET_EXPORT_PRIVATE DnsConfigServicePosix : |
| 23 public NON_EXPORTED_BASE(DnsConfigService), |
| 24 public NON_EXPORTED_BASE(base::NonThreadSafe) { |
| 25 public: |
| 26 class ResolverLib; |
| 27 class FilePathWatcherShim; |
| 28 class FilePathWatcherFactory; |
| 29 class WatcherDelegate; |
| 30 |
| 31 DnsConfigServicePosix(); |
| 32 virtual ~DnsConfigServicePosix(); |
| 33 |
| 34 virtual void AddObserver(Observer* observer) OVERRIDE; |
| 35 virtual void RemoveObserver(Observer* observer) OVERRIDE; |
| 36 virtual void Watch() OVERRIDE; |
| 37 |
| 38 // Takes ownership of |lib|. Must be set before Watch. |
| 39 void set_resolver_lib(ResolverLib* lib) { |
| 40 DCHECK(!watcher_delegate_.get()); |
| 41 resolver_lib_.reset(lib); |
| 42 } |
| 43 |
| 44 // Takes ownership of |factory|. Must be set before Watch. |
| 45 void set_watcher_factory(FilePathWatcherFactory* factory) { |
| 46 DCHECK(!watcher_delegate_.get()); |
| 47 watcher_factory_.reset(factory); |
| 48 } |
| 49 |
| 50 private: |
| 51 void OnConfigRead(const DnsConfig& config); |
| 52 |
| 53 // Configure a FilePathWatcher and install watcher_delegate_. Executed each |
| 54 // time FilePathWatcher fails. |
| 55 void StartWatch(); |
| 56 |
| 57 DnsConfig dns_config_; |
| 58 // True after first OnConfigChanged, that is, dns_config_ is valid. |
| 59 bool have_config_; |
| 60 |
| 61 scoped_ptr<ResolverLib> resolver_lib_; |
| 62 scoped_ptr<FilePathWatcherFactory> watcher_factory_; |
| 63 scoped_ptr<FilePathWatcherShim> resolv_file_watcher_; |
| 64 scoped_refptr<WatcherDelegate> watcher_delegate_; |
| 65 ObserverList<Observer> observers_; |
| 66 |
| 67 DISALLOW_COPY_AND_ASSIGN(DnsConfigServicePosix); |
| 68 }; |
| 69 |
| 70 |
| 71 // Allows mocking res_ninit. |
| 72 class NET_EXPORT_PRIVATE DnsConfigServicePosix::ResolverLib : |
| 73 public NON_EXPORTED_BASE(base::NonThreadSafe) { |
| 74 public: |
| 75 ResolverLib() {} |
| 76 virtual ~ResolverLib() {} |
| 77 virtual int ninit(res_state statp) { |
| 78 return ::res_ninit(statp); |
| 79 } |
| 80 virtual void nclose(res_state statp) { |
| 81 return ::res_nclose(statp); |
| 82 } |
| 83 private: |
| 84 DISALLOW_COPY_AND_ASSIGN(ResolverLib); |
| 85 }; |
| 86 |
| 87 // Allows mocking FilePathWatcher |
| 88 class NET_EXPORT_PRIVATE DnsConfigServicePosix::FilePathWatcherShim : |
| 89 public NON_EXPORTED_BASE(base::NonThreadSafe) { |
| 90 public: |
| 91 FilePathWatcherShim() : watcher_(new base::files::FilePathWatcher()) {} |
| 92 virtual ~FilePathWatcherShim() {} |
| 93 |
| 94 virtual bool Watch( |
| 95 const FilePath& path, |
| 96 base::files::FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT { |
| 97 return watcher_->Watch(path, delegate); |
| 98 } |
| 99 private: |
| 100 scoped_ptr<base::files::FilePathWatcher> watcher_; |
| 101 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherShim); |
| 102 }; |
| 103 |
| 104 class NET_EXPORT_PRIVATE DnsConfigServicePosix::FilePathWatcherFactory : |
| 105 public NON_EXPORTED_BASE(base::NonThreadSafe) { |
| 106 public: |
| 107 FilePathWatcherFactory() {} |
| 108 virtual ~FilePathWatcherFactory() {} |
| 109 virtual FilePathWatcherShim* CreateFilePathWatcher() { |
| 110 return new FilePathWatcherShim(); |
| 111 } |
| 112 private: |
| 113 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherFactory); |
| 114 }; |
| 115 |
| 116 // Fills in |dns_config| from |res|. Exposed for tests. |
| 117 bool NET_EXPORT_PRIVATE ConvertResToConfig(const struct __res_state& res, |
| 118 DnsConfig* dns_config); |
| 119 |
| 120 } // namespace net |
| 121 |
| 122 #endif // NET_DNS_DNS_CONFIG_SERVICE_POSIX_H_ |
OLD | NEW |