OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/base/network_change_notifier_linux.h" | 5 #include "net/base/network_change_notifier_linux.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
9 | 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/callback.h" |
10 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
11 #include "base/eintr_wrapper.h" | 13 #include "base/eintr_wrapper.h" |
| 14 #include "base/files/file_path_watcher.h" |
12 #include "base/task.h" | 15 #include "base/task.h" |
13 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
14 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
15 #include "net/base/network_change_notifier_netlink_linux.h" | 18 #include "net/base/network_change_notifier_netlink_linux.h" |
16 | 19 |
| 20 using ::base::files::FilePathWatcher; |
| 21 |
17 namespace net { | 22 namespace net { |
18 | 23 |
19 namespace { | 24 namespace { |
20 | 25 |
21 const int kInvalidSocket = -1; | 26 const int kInvalidSocket = -1; |
22 | 27 |
| 28 class DNSWatchDelegate : public FilePathWatcher::Delegate { |
| 29 public: |
| 30 explicit DNSWatchDelegate(const base::Callback<void(void)>& no_ref_cb) |
| 31 : no_ref_cb_(no_ref_cb) {} |
| 32 virtual ~DNSWatchDelegate() {} |
| 33 // FilePathWatcher::Delegate interface |
| 34 virtual void OnFilePathChanged(const FilePath& path); |
| 35 private: |
| 36 base::Callback<void(void)> no_ref_cb_; |
| 37 DISALLOW_COPY_AND_ASSIGN(DNSWatchDelegate); |
| 38 }; |
| 39 |
| 40 void DNSWatchDelegate::OnFilePathChanged(const FilePath& path) { |
| 41 // Calls NetworkChangeNotifier::NotifyObserversOfDNSChange(). |
| 42 no_ref_cb_.Run(); |
| 43 } |
| 44 |
23 } // namespace | 45 } // namespace |
24 | 46 |
25 class NetworkChangeNotifierLinux::Thread | 47 class NetworkChangeNotifierLinux::Thread |
26 : public base::Thread, public MessageLoopForIO::Watcher { | 48 : public base::Thread, public MessageLoopForIO::Watcher { |
27 public: | 49 public: |
28 Thread(); | 50 Thread(); |
29 virtual ~Thread(); | 51 virtual ~Thread(); |
30 | 52 |
31 // MessageLoopForIO::Watcher: | 53 // MessageLoopForIO::Watcher: |
32 virtual void OnFileCanReadWithoutBlocking(int fd); | 54 virtual void OnFileCanReadWithoutBlocking(int fd); |
33 virtual void OnFileCanWriteWithoutBlocking(int /* fd */); | 55 virtual void OnFileCanWriteWithoutBlocking(int /* fd */); |
34 | 56 |
35 protected: | 57 protected: |
36 // base::Thread | 58 // base::Thread |
37 virtual void Init(); | 59 virtual void Init(); |
38 virtual void CleanUp(); | 60 virtual void CleanUp(); |
39 | 61 |
40 private: | 62 private: |
41 void NotifyObserversOfIPAddressChange() { | 63 void NotifyObserversOfIPAddressChange() { |
42 NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); | 64 NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); |
43 } | 65 } |
44 | 66 |
| 67 void NotifyObserversOfDNSChange() { |
| 68 NetworkChangeNotifier::NotifyObserversOfDNSChange(); |
| 69 } |
| 70 |
45 // Starts listening for netlink messages. Also handles the messages if there | 71 // Starts listening for netlink messages. Also handles the messages if there |
46 // are any available on the netlink socket. | 72 // are any available on the netlink socket. |
47 void ListenForNotifications(); | 73 void ListenForNotifications(); |
48 | 74 |
49 // Attempts to read from the netlink socket into |buf| of length |len|. | 75 // Attempts to read from the netlink socket into |buf| of length |len|. |
50 // Returns the bytes read on synchronous success and ERR_IO_PENDING if the | 76 // Returns the bytes read on synchronous success and ERR_IO_PENDING if the |
51 // recv() would block. Otherwise, it returns a net error code. | 77 // recv() would block. Otherwise, it returns a net error code. |
52 int ReadNotificationMessage(char* buf, size_t len); | 78 int ReadNotificationMessage(char* buf, size_t len); |
53 | 79 |
54 // The netlink socket descriptor. | 80 // The netlink socket descriptor. |
55 int netlink_fd_; | 81 int netlink_fd_; |
56 MessageLoopForIO::FileDescriptorWatcher netlink_watcher_; | 82 MessageLoopForIO::FileDescriptorWatcher netlink_watcher_; |
57 | 83 |
58 // Technically only needed for ChromeOS, but it's ugly to #ifdef out. | 84 // Technically only needed for ChromeOS, but it's ugly to #ifdef out. |
59 ScopedRunnableMethodFactory<Thread> method_factory_; | 85 ScopedRunnableMethodFactory<Thread> method_factory_; |
60 | 86 |
| 87 // Used to watch for changes to /etc/resolv.conf and /etc/hosts. |
| 88 scoped_ptr<base::files::FilePathWatcher> resolv_file_watcher_; |
| 89 scoped_ptr<base::files::FilePathWatcher> hosts_file_watcher_; |
| 90 scoped_refptr<base::files::FilePathWatcher::Delegate> file_watcher_delegate_; |
| 91 |
61 DISALLOW_COPY_AND_ASSIGN(Thread); | 92 DISALLOW_COPY_AND_ASSIGN(Thread); |
62 }; | 93 }; |
63 | 94 |
64 NetworkChangeNotifierLinux::Thread::Thread() | 95 NetworkChangeNotifierLinux::Thread::Thread() |
65 : base::Thread("NetworkChangeNotifier"), | 96 : base::Thread("NetworkChangeNotifier"), |
66 netlink_fd_(kInvalidSocket), | 97 netlink_fd_(kInvalidSocket), |
67 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} | 98 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| 99 } |
68 | 100 |
69 NetworkChangeNotifierLinux::Thread::~Thread() {} | 101 NetworkChangeNotifierLinux::Thread::~Thread() {} |
70 | 102 |
71 void NetworkChangeNotifierLinux::Thread::Init() { | 103 void NetworkChangeNotifierLinux::Thread::Init() { |
| 104 resolv_file_watcher_.reset(new FilePathWatcher); |
| 105 hosts_file_watcher_.reset(new FilePathWatcher); |
| 106 file_watcher_delegate_ = new DNSWatchDelegate(base::Bind( |
| 107 &NetworkChangeNotifierLinux::Thread::NotifyObserversOfDNSChange, |
| 108 base::Unretained(this))); |
| 109 if (!resolv_file_watcher_->Watch( |
| 110 FilePath(FILE_PATH_LITERAL("/etc/resolv.conf")), |
| 111 file_watcher_delegate_.get())) { |
| 112 LOG(ERROR) << "Failed to setup watch for /etc/resolv.conf"; |
| 113 } |
| 114 if (!hosts_file_watcher_->Watch(FilePath(FILE_PATH_LITERAL("/etc/hosts")), |
| 115 file_watcher_delegate_.get())) { |
| 116 LOG(ERROR) << "Failed to setup watch for /etc/hosts"; |
| 117 } |
72 netlink_fd_ = InitializeNetlinkSocket(); | 118 netlink_fd_ = InitializeNetlinkSocket(); |
73 if (netlink_fd_ < 0) { | 119 if (netlink_fd_ < 0) { |
74 netlink_fd_ = kInvalidSocket; | 120 netlink_fd_ = kInvalidSocket; |
75 return; | 121 return; |
76 } | 122 } |
77 ListenForNotifications(); | 123 ListenForNotifications(); |
78 } | 124 } |
79 | 125 |
80 void NetworkChangeNotifierLinux::Thread::CleanUp() { | 126 void NetworkChangeNotifierLinux::Thread::CleanUp() { |
81 if (netlink_fd_ != kInvalidSocket) { | 127 if (netlink_fd_ != kInvalidSocket) { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 // check that the notifier thread shut down properly. | 206 // check that the notifier thread shut down properly. |
161 notifier_thread_->Stop(); | 207 notifier_thread_->Stop(); |
162 } | 208 } |
163 | 209 |
164 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { | 210 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { |
165 // TODO(eroman): http://crbug.com/53473 | 211 // TODO(eroman): http://crbug.com/53473 |
166 return false; | 212 return false; |
167 } | 213 } |
168 | 214 |
169 } // namespace net | 215 } // namespace net |
OLD | NEW |