Index: net/base/network_change_notifier_linux.cc |
diff --git a/net/base/network_change_notifier_linux.cc b/net/base/network_change_notifier_linux.cc |
index 3937de0ec3da8db7067c2ae07c0a3c68027d2791..20c8b0a9a4143779a28a58d63d1ee6118efea3af 100644 |
--- a/net/base/network_change_notifier_linux.cc |
+++ b/net/base/network_change_notifier_linux.cc |
@@ -7,19 +7,41 @@ |
#include <errno.h> |
#include <sys/socket.h> |
+#include "base/bind.h" |
+#include "base/callback.h" |
#include "base/compiler_specific.h" |
#include "base/eintr_wrapper.h" |
+#include "base/files/file_path_watcher.h" |
#include "base/task.h" |
#include "base/threading/thread.h" |
#include "net/base/net_errors.h" |
#include "net/base/network_change_notifier_netlink_linux.h" |
+using ::base::files::FilePathWatcher; |
+ |
namespace net { |
namespace { |
const int kInvalidSocket = -1; |
+class DNSWatchDelegate : public FilePathWatcher::Delegate { |
+ public: |
+ explicit DNSWatchDelegate(const base::Callback<void(void)>& no_ref_cb) |
+ : no_ref_cb_(no_ref_cb) {} |
+ virtual ~DNSWatchDelegate() {} |
+ // FilePathWatcher::Delegate interface |
+ virtual void OnFilePathChanged(const FilePath& path); |
+ private: |
+ base::Callback<void(void)> no_ref_cb_; |
+ DISALLOW_COPY_AND_ASSIGN(DNSWatchDelegate); |
+}; |
+ |
+void DNSWatchDelegate::OnFilePathChanged(const FilePath& path) { |
+ // Calls NetworkChangeNotifier::NotifyObserversOfDNSChange(). |
+ no_ref_cb_.Run(); |
+} |
+ |
} // namespace |
class NetworkChangeNotifierLinux::Thread |
@@ -42,6 +64,10 @@ class NetworkChangeNotifierLinux::Thread |
NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); |
} |
+ void NotifyObserversOfDNSChange() { |
+ NetworkChangeNotifier::NotifyObserversOfDNSChange(); |
+ } |
+ |
// Starts listening for netlink messages. Also handles the messages if there |
// are any available on the netlink socket. |
void ListenForNotifications(); |
@@ -58,17 +84,37 @@ class NetworkChangeNotifierLinux::Thread |
// Technically only needed for ChromeOS, but it's ugly to #ifdef out. |
ScopedRunnableMethodFactory<Thread> method_factory_; |
+ // Used to watch for changes to /etc/resolv.conf and /etc/hosts. |
+ scoped_ptr<base::files::FilePathWatcher> resolv_file_watcher_; |
+ scoped_ptr<base::files::FilePathWatcher> hosts_file_watcher_; |
+ scoped_refptr<base::files::FilePathWatcher::Delegate> file_watcher_delegate_; |
+ |
DISALLOW_COPY_AND_ASSIGN(Thread); |
}; |
NetworkChangeNotifierLinux::Thread::Thread() |
: base::Thread("NetworkChangeNotifier"), |
netlink_fd_(kInvalidSocket), |
- ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} |
+ ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
+} |
NetworkChangeNotifierLinux::Thread::~Thread() {} |
void NetworkChangeNotifierLinux::Thread::Init() { |
+ resolv_file_watcher_.reset(new FilePathWatcher); |
+ hosts_file_watcher_.reset(new FilePathWatcher); |
+ file_watcher_delegate_ = new DNSWatchDelegate(base::Bind( |
+ &NetworkChangeNotifierLinux::Thread::NotifyObserversOfDNSChange, |
+ base::Unretained(this))); |
+ if (!resolv_file_watcher_->Watch( |
+ FilePath(FILE_PATH_LITERAL("/etc/resolv.conf")), |
+ file_watcher_delegate_.get())) { |
+ LOG(ERROR) << "Failed to setup watch for /etc/resolv.conf"; |
+ } |
+ if (!hosts_file_watcher_->Watch(FilePath(FILE_PATH_LITERAL("/etc/hosts")), |
+ file_watcher_delegate_.get())) { |
+ LOG(ERROR) << "Failed to setup watch for /etc/hosts"; |
+ } |
netlink_fd_ = InitializeNetlinkSocket(); |
if (netlink_fd_ < 0) { |
netlink_fd_ = kInvalidSocket; |