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_old.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/file_util.h" |
| 15 #include "base/files/file_path_watcher.h" |
12 #include "base/task.h" | 16 #include "base/task.h" |
13 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
14 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
15 #include "net/base/network_change_notifier_netlink_linux.h" | 19 #include "net/base/network_change_notifier_netlink_linux.h" |
16 | 20 |
| 21 using ::base::files::FilePathWatcher; |
| 22 |
17 namespace net { | 23 namespace net { |
18 | 24 |
19 namespace { | 25 namespace { |
20 | 26 |
21 const int kInvalidSocket = -1; | 27 const int kInvalidSocket = -1; |
22 | 28 |
| 29 class DNSWatchDelegate : public FilePathWatcher::Delegate { |
| 30 public: |
| 31 explicit DNSWatchDelegate(Callback0::Type* callback) |
| 32 : callback_(callback) {} |
| 33 virtual ~DNSWatchDelegate() {} |
| 34 // FilePathWatcher::Delegate interface |
| 35 virtual void OnFilePathChanged(const FilePath& path) OVERRIDE; |
| 36 virtual void OnFilePathError(const FilePath& path) OVERRIDE; |
| 37 private: |
| 38 scoped_ptr<Callback0::Type> callback_; |
| 39 DISALLOW_COPY_AND_ASSIGN(DNSWatchDelegate); |
| 40 }; |
| 41 |
| 42 void DNSWatchDelegate::OnFilePathChanged(const FilePath& path) { |
| 43 // Calls NetworkChangeNotifier::NotifyObserversOfDNSChange(). |
| 44 if (callback_.get()) |
| 45 callback_->Run(); |
| 46 } |
| 47 |
| 48 void DNSWatchDelegate::OnFilePathError(const FilePath& path) { |
| 49 LOG(ERROR) << "DNSWatchDelegate::OnFilePathError for " << path.value(); |
| 50 } |
| 51 |
23 } // namespace | 52 } // namespace |
24 | 53 |
25 class NetworkChangeNotifierLinux::Thread | 54 class NetworkChangeNotifierLinux::Thread |
26 : public base::Thread, public MessageLoopForIO::Watcher { | 55 : public base::Thread, public MessageLoopForIO::Watcher { |
27 public: | 56 public: |
28 Thread(); | 57 Thread(); |
29 virtual ~Thread(); | 58 virtual ~Thread(); |
30 | 59 |
31 // MessageLoopForIO::Watcher: | 60 // MessageLoopForIO::Watcher: |
32 virtual void OnFileCanReadWithoutBlocking(int fd); | 61 virtual void OnFileCanReadWithoutBlocking(int fd); |
33 virtual void OnFileCanWriteWithoutBlocking(int /* fd */); | 62 virtual void OnFileCanWriteWithoutBlocking(int /* fd */); |
34 | 63 |
35 protected: | 64 protected: |
36 // base::Thread | 65 // base::Thread |
37 virtual void Init(); | 66 virtual void Init(); |
38 virtual void CleanUp(); | 67 virtual void CleanUp(); |
39 | 68 |
40 private: | 69 private: |
41 void NotifyObserversOfIPAddressChange() { | 70 void NotifyObserversOfIPAddressChange() { |
42 NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); | 71 NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); |
43 } | 72 } |
44 | 73 |
| 74 void NotifyObserversOfDNSChange() { |
| 75 NetworkChangeNotifier::NotifyObserversOfDNSChange(); |
| 76 } |
| 77 |
45 // Starts listening for netlink messages. Also handles the messages if there | 78 // Starts listening for netlink messages. Also handles the messages if there |
46 // are any available on the netlink socket. | 79 // are any available on the netlink socket. |
47 void ListenForNotifications(); | 80 void ListenForNotifications(); |
48 | 81 |
49 // Attempts to read from the netlink socket into |buf| of length |len|. | 82 // 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 | 83 // Returns the bytes read on synchronous success and ERR_IO_PENDING if the |
51 // recv() would block. Otherwise, it returns a net error code. | 84 // recv() would block. Otherwise, it returns a net error code. |
52 int ReadNotificationMessage(char* buf, size_t len); | 85 int ReadNotificationMessage(char* buf, size_t len); |
53 | 86 |
54 // The netlink socket descriptor. | 87 // The netlink socket descriptor. |
55 int netlink_fd_; | 88 int netlink_fd_; |
56 MessageLoopForIO::FileDescriptorWatcher netlink_watcher_; | 89 MessageLoopForIO::FileDescriptorWatcher netlink_watcher_; |
57 | 90 |
58 // Technically only needed for ChromeOS, but it's ugly to #ifdef out. | 91 // Technically only needed for ChromeOS, but it's ugly to #ifdef out. |
59 ScopedRunnableMethodFactory<Thread> method_factory_; | 92 ScopedRunnableMethodFactory<Thread> method_factory_; |
60 | 93 |
| 94 // Used to watch for changes to /etc/resolv.conf and /etc/hosts. |
| 95 scoped_ptr<base::files::FilePathWatcher> resolv_file_watcher_; |
| 96 scoped_ptr<base::files::FilePathWatcher> hosts_file_watcher_; |
| 97 scoped_refptr<DNSWatchDelegate> file_watcher_delegate_; |
| 98 |
61 DISALLOW_COPY_AND_ASSIGN(Thread); | 99 DISALLOW_COPY_AND_ASSIGN(Thread); |
62 }; | 100 }; |
63 | 101 |
64 NetworkChangeNotifierLinux::Thread::Thread() | 102 NetworkChangeNotifierLinux::Thread::Thread() |
65 : base::Thread("NetworkChangeNotifier"), | 103 : base::Thread("NetworkChangeNotifier"), |
66 netlink_fd_(kInvalidSocket), | 104 netlink_fd_(kInvalidSocket), |
67 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} | 105 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| 106 } |
68 | 107 |
69 NetworkChangeNotifierLinux::Thread::~Thread() {} | 108 NetworkChangeNotifierLinux::Thread::~Thread() {} |
70 | 109 |
71 void NetworkChangeNotifierLinux::Thread::Init() { | 110 void NetworkChangeNotifierLinux::Thread::Init() { |
| 111 resolv_file_watcher_.reset(new FilePathWatcher); |
| 112 hosts_file_watcher_.reset(new FilePathWatcher); |
| 113 file_watcher_delegate_ = new DNSWatchDelegate(NewCallback(this, |
| 114 &NetworkChangeNotifierLinux::Thread::NotifyObserversOfDNSChange)); |
| 115 if (!resolv_file_watcher_->Watch( |
| 116 FilePath(FILE_PATH_LITERAL("/etc/resolv.conf")), |
| 117 file_watcher_delegate_.get())) { |
| 118 LOG(ERROR) << "Failed to setup watch for /etc/resolv.conf"; |
| 119 } |
| 120 if (!hosts_file_watcher_->Watch(FilePath(FILE_PATH_LITERAL("/etc/hosts")), |
| 121 file_watcher_delegate_.get())) { |
| 122 LOG(ERROR) << "Failed to setup watch for /etc/hosts"; |
| 123 } |
72 netlink_fd_ = InitializeNetlinkSocket(); | 124 netlink_fd_ = InitializeNetlinkSocket(); |
73 if (netlink_fd_ < 0) { | 125 if (netlink_fd_ < 0) { |
74 netlink_fd_ = kInvalidSocket; | 126 netlink_fd_ = kInvalidSocket; |
75 return; | 127 return; |
76 } | 128 } |
77 ListenForNotifications(); | 129 ListenForNotifications(); |
78 } | 130 } |
79 | 131 |
80 void NetworkChangeNotifierLinux::Thread::CleanUp() { | 132 void NetworkChangeNotifierLinux::Thread::CleanUp() { |
81 if (netlink_fd_ != kInvalidSocket) { | 133 if (netlink_fd_ != kInvalidSocket) { |
82 if (HANDLE_EINTR(close(netlink_fd_)) != 0) | 134 if (HANDLE_EINTR(close(netlink_fd_)) != 0) |
83 PLOG(ERROR) << "Failed to close socket"; | 135 PLOG(ERROR) << "Failed to close socket"; |
84 netlink_fd_ = kInvalidSocket; | 136 netlink_fd_ = kInvalidSocket; |
85 netlink_watcher_.StopWatchingFileDescriptor(); | 137 netlink_watcher_.StopWatchingFileDescriptor(); |
86 } | 138 } |
| 139 // Kill watchers early to make sure they won't try to call |
| 140 // into us via the delegate during destruction. |
| 141 resolv_file_watcher_.reset(); |
| 142 hosts_file_watcher_.reset(); |
87 } | 143 } |
88 | 144 |
89 void NetworkChangeNotifierLinux::Thread::OnFileCanReadWithoutBlocking(int fd) { | 145 void NetworkChangeNotifierLinux::Thread::OnFileCanReadWithoutBlocking(int fd) { |
90 DCHECK_EQ(fd, netlink_fd_); | 146 DCHECK_EQ(fd, netlink_fd_); |
91 ListenForNotifications(); | 147 ListenForNotifications(); |
92 } | 148 } |
93 | 149 |
94 void NetworkChangeNotifierLinux::Thread::OnFileCanWriteWithoutBlocking( | 150 void NetworkChangeNotifierLinux::Thread::OnFileCanWriteWithoutBlocking( |
95 int /* fd */) { | 151 int /* fd */) { |
96 NOTREACHED(); | 152 NOTREACHED(); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 // check that the notifier thread shut down properly. | 216 // check that the notifier thread shut down properly. |
161 notifier_thread_->Stop(); | 217 notifier_thread_->Stop(); |
162 } | 218 } |
163 | 219 |
164 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { | 220 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { |
165 // TODO(eroman): http://crbug.com/53473 | 221 // TODO(eroman): http://crbug.com/53473 |
166 return false; | 222 return false; |
167 } | 223 } |
168 | 224 |
169 } // namespace net | 225 } // namespace net |
OLD | NEW |