Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(276)

Side by Side Diff: net/dns/dns_config_watcher_posix.cc

Issue 10873018: [net] Move DnsConfigService to NetworkChangeNotifier. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add comment Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/dns/dns_config_watcher.h ('k') | net/dns/dns_config_watcher_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "net/dns/dns_config_watcher.h"
6
7 #include <string>
8
9 #include "base/basictypes.h"
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/files/file_path_watcher.h"
13 #include "base/file_path.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "net/base/network_change_notifier.h"
17
18 #if defined(OS_MACOSX)
19 #include "net/dns/notify_watcher_mac.h"
20 #endif
21
22 namespace net {
23 namespace internal {
24
25 namespace {
26
27 const FilePath::CharType* kFilePathHosts = FILE_PATH_LITERAL("/etc/hosts");
28
29 #if defined(OS_MACOSX)
30 // From 10.7.3 configd-395.10/dnsinfo/dnsinfo.h
31 static const char* kDnsNotifyKey =
32 "com.apple.system.SystemConfiguration.dns_configuration";
33
34 class ConfigWatcher {
35 public:
36 bool Watch(const base::Callback<void(bool succeeded)>& callback) {
37 return watcher_.Watch(kDnsNotifyKey, callback);
38 }
39
40 private:
41 NotifyWatcherMac watcher_;
42 };
43 #else
44
45 #ifndef _PATH_RESCONF // Normally defined in <resolv.h>
46 #define _PATH_RESCONF "/etc/resolv.conf"
47 #endif
48
49 static const FilePath::CharType* kFilePathConfig =
50 FILE_PATH_LITERAL(_PATH_RESCONF);
51
52 class ConfigWatcher {
53 public:
54 typedef base::Callback<void(bool succeeded)> CallbackType;
55
56 bool Watch(const CallbackType& callback) {
57 callback_ = callback;
58 return watcher_.Watch(FilePath(kFilePathConfig),
59 base::Bind(&ConfigWatcher::OnCallback,
60 base::Unretained(this)));
61 }
62
63 private:
64 void OnCallback(const FilePath& path, bool error) {
65 callback_.Run(!error);
66 }
67
68 base::files::FilePathWatcher watcher_;
69 CallbackType callback_;
70 };
71 #endif
72
73 } // namespace
74
75 class DnsConfigWatcher::Core {
76 public:
77 Core() {}
78 ~Core() {}
79
80 bool Watch() {
81 bool success = true;
82 if (!config_watcher_.Watch(base::Bind(&Core::OnConfigChanged,
83 base::Unretained(this)))) {
84 LOG(ERROR) << "DNS config watch failed to start.";
85 success = false;
86 }
87 if (!hosts_watcher_.Watch(FilePath(kFilePathHosts),
88 base::Bind(&Core::OnHostsChanged,
89 base::Unretained(this)))) {
90 LOG(ERROR) << "DNS hosts watch failed to start.";
91 success = false;
92 }
93 return success;
94 }
95
96 private:
97 void OnConfigChanged(bool succeeded) {
98 if (succeeded) {
99 NetworkChangeNotifier::NotifyObserversOfDNSChange(
100 NetworkChangeNotifier::CHANGE_DNS_SETTINGS);
101 } else {
102 LOG(ERROR) << "DNS config watch failed.";
103 NetworkChangeNotifier::NotifyObserversOfDNSChange(
104 NetworkChangeNotifier::CHANGE_DNS_WATCH_FAILED);
105 }
106 }
107
108 void OnHostsChanged(const FilePath& path, bool error) {
109 if (!error) {
110 NetworkChangeNotifier::NotifyObserversOfDNSChange(
111 NetworkChangeNotifier::CHANGE_DNS_HOSTS);
112 } else {
113 LOG(ERROR) << "DNS hosts watch failed.";
114 NetworkChangeNotifier::NotifyObserversOfDNSChange(
115 NetworkChangeNotifier::CHANGE_DNS_WATCH_FAILED);
116 }
117 }
118
119 ConfigWatcher config_watcher_;
120 base::files::FilePathWatcher hosts_watcher_;
121
122 DISALLOW_COPY_AND_ASSIGN(Core);
123 };
124
125 DnsConfigWatcher::DnsConfigWatcher() {}
126
127 DnsConfigWatcher::~DnsConfigWatcher() {}
128
129 void DnsConfigWatcher::Init() {
130 core_.reset(new Core());
131 if (core_->Watch()) {
132 NetworkChangeNotifier::NotifyObserversOfDNSChange(
133 NetworkChangeNotifier::CHANGE_DNS_WATCH_STARTED);
134 }
135 // TODO(szym): re-start watcher if that makes sense. http://crbug.com/116139
136 }
137
138 void DnsConfigWatcher::CleanUp() {
139 core_.reset();
140 }
141
142 } // namespace internal
143 } // namespace net
OLDNEW
« no previous file with comments | « net/dns/dns_config_watcher.h ('k') | net/dns/dns_config_watcher_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698