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..f715e8613e76a5a5e2fc554364c7cdc67e0da20e 100644 |
--- a/net/base/network_change_notifier_linux.cc |
+++ b/net/base/network_change_notifier_linux.cc |
@@ -7,19 +7,49 @@ |
#include <errno.h> |
#include <sys/socket.h> |
+#include "base/bind.h" |
+#include "base/callback_old.h" |
#include "base/compiler_specific.h" |
#include "base/eintr_wrapper.h" |
+#include "base/file_util.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(Callback0::Type* callback) |
+ : callback_enabled(true) { |
+ callback_.reset(callback); |
eroman
2011/08/11 01:57:55
nit: I suggest moving this into the initializer li
Craig
2011/08/15 17:10:56
Done.
|
+ } |
+ virtual ~DNSWatchDelegate() {} |
+ void DisableCallback() { |
+ callback_enabled = false; |
eroman
2011/08/11 01:57:55
Not sure the bool is necessary, could do callback_
Craig
2011/08/15 17:10:56
Done.
|
+ } |
+ // FilePathWatcher::Delegate interface |
+ virtual void OnFilePathChanged(const FilePath& path); |
eroman
2011/08/11 01:57:55
nit: Can you add OVERRIDE annotation?
Craig
2011/08/15 17:10:56
Done.
|
+ private: |
eroman
2011/08/11 01:57:55
[optional]: I suggest hooking OnFilePathError() an
Craig
2011/08/15 17:10:56
Done. At the moment FilePathWatcher on linux won't
|
+ scoped_ptr<Callback0::Type> callback_; |
+ bool callback_enabled; |
+ DISALLOW_COPY_AND_ASSIGN(DNSWatchDelegate); |
+}; |
+ |
+void DNSWatchDelegate::OnFilePathChanged(const FilePath& path) { |
+ // Calls NetworkChangeNotifier::NotifyObserversOfDNSChange(). |
+ if (callback_enabled) |
+ callback_->Run(); |
+} |
+ |
} // namespace |
class NetworkChangeNotifierLinux::Thread |
@@ -42,6 +72,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 +92,44 @@ 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<DNSWatchDelegate> 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(NewCallback(this, |
+ &NetworkChangeNotifierLinux::Thread::NotifyObserversOfDNSChange)); |
+ FilePath resolver(FILE_PATH_LITERAL("/etc/resolv.conf")); |
+ FilePath tmppath; |
+ // On systems where /etc/resolv.conf is a symlink, we have to normalize |
+ // the path and watch that until FilePathWatcher copes with this. See |
+ // crbug.com/91561. Note that there are some corner cases this workaround |
+ // won't catch. |
+ if (file_util::NormalizeFilePath(resolver, &tmppath)) |
+ resolver = tmppath; |
+ else |
+ LOG(ERROR) << "Cannot normalize /etc/resolv.conf"; |
+ if (!resolv_file_watcher_->Watch(resolver, 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; |
@@ -84,6 +145,9 @@ void NetworkChangeNotifierLinux::Thread::CleanUp() { |
netlink_fd_ = kInvalidSocket; |
netlink_watcher_.StopWatchingFileDescriptor(); |
} |
+ // If DNSWatchDelegate outlives us, make sure it won't |
+ // try to call into us during destruction. |
+ file_watcher_delegate_->DisableCallback(); |
eroman
2011/08/11 01:57:55
I think it would be sufficient to reset the file w
Craig
2011/08/15 17:10:56
Done. Much cleaner. I've killed the DisableCallbac
|
} |
void NetworkChangeNotifierLinux::Thread::OnFileCanReadWithoutBlocking(int fd) { |