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 #include <resolv.h> | |
6 | |
7 #include "base/compiler_specific.h" | |
8 #include "base/files/file_path_watcher.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/observer_list.h" | |
11 #include "base/scoped_ptr.h" | |
12 #include "net/dns/dns_config_service.h" | |
13 | |
14 namespace net { | |
15 | |
16 // Fills in |dns_config| from |res|. Exposed for tests. | |
17 bool ConvertResToConfig(const struct __res_state& res, DnsConfig* dns_config); | |
18 | |
19 // Allows mocking res_ninit. | |
20 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.
| |
21 virtual ~ResolverLib() {} | |
22 virtual int ninit(res_state statp) { | |
23 return ::res_ninit(statp); | |
24 } | |
25 virtual void nclose(res_state statp) { | |
26 return ::res_nclose(statp); | |
27 } | |
cbentzel
2011/08/15 18:16:34
DISALLOW_COPY_AND_ASSIGN
szym
2011/08/15 22:02:01
Done.
| |
28 }; | |
29 | |
30 // Allows mocking FilePathWatcher | |
31 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.
| |
32 public: | |
33 FilePathWatcherShim(); | |
34 virtual ~FilePathWatcherShim() {} | |
35 | |
36 virtual bool Watch( | |
37 const FilePath& path, | |
38 base::files::FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT; | |
39 private: | |
40 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.
| |
41 }; | |
42 | |
43 struct FilePathWatcherFactory { | |
cbentzel
2011/08/15 18:16:34
Ditto here.
szym
2011/08/15 22:02:01
Done.
| |
44 virtual ~FilePathWatcherFactory() {} | |
45 virtual FilePathWatcherShim* CreateFilePathWatcher() { | |
46 return new FilePathWatcherShim(); | |
47 } | |
48 }; | |
49 | |
50 class WatcherDelegate; | |
51 | |
52 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.
| |
53 public: | |
54 DnsConfigServicePosix(); | |
55 virtual ~DnsConfigServicePosix(); | |
56 | |
57 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.
| |
58 observers_.AddObserver(observer); | |
59 if (have_config_) { | |
cbentzel
2011/08/15 18:16:34
Good idea, cool.
| |
60 observer->OnConfigChanged(dns_config_); | |
61 } | |
62 } | |
63 | |
64 virtual void RemoveObserver(Observer* observer) OVERRIDE { | |
65 observers_.RemoveObserver(observer); | |
66 } | |
67 | |
68 virtual void Watch() OVERRIDE; | |
69 | |
70 // Takes ownership of |lib|. Must be set before Watch. | |
71 void set_resolver_lib(ResolverLib* lib) { | |
72 DCHECK(!watcher_delegate_.get()); | |
73 resolver_lib_.reset(lib); | |
74 } | |
75 | |
76 // Takes ownership of |factory|. Must be set before Watch. | |
77 void set_watcher_factory(FilePathWatcherFactory* factory) { | |
78 DCHECK(!watcher_delegate_.get()); | |
79 watcher_factory_.reset(factory); | |
80 } | |
81 | |
82 private: | |
83 friend class WatcherDelegate; | |
84 | |
85 void OnConfigRead(const DnsConfig& config) { | |
86 if (!config.Equals(dns_config_)) { | |
87 dns_config_ = config; | |
88 have_config_ = true; | |
89 FOR_EACH_OBSERVER(Observer, observers_, OnConfigChanged(config)); | |
90 } | |
91 } | |
92 | |
93 // Configure a FilePathWatcher and install watcher_delegate_. Executed each | |
94 // time FilePathWatcher fails. | |
95 void StartWatch(); | |
96 | |
97 DnsConfig dns_config_; | |
98 // True after first OnConfigChanged, that is, dns_config_ is valid. | |
99 bool have_config_; | |
100 | |
101 scoped_ptr<ResolverLib> resolver_lib_; | |
102 scoped_ptr<FilePathWatcherFactory> watcher_factory_; | |
103 scoped_ptr<FilePathWatcherShim> resolv_file_watcher_; | |
104 scoped_refptr<WatcherDelegate> watcher_delegate_; | |
105 ObserverList<Observer> observers_; | |
106 | |
107 DISALLOW_COPY_AND_ASSIGN(DnsConfigServicePosix); | |
108 }; | |
109 | |
110 | |
cbentzel
2011/08/15 18:16:34
Nit: extra new line
szym
2011/08/15 22:02:01
Done.
| |
111 } // namespace net | |
112 | |
OLD | NEW |