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_enabled(true) { | |
33 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.
| |
34 } | |
35 virtual ~DNSWatchDelegate() {} | |
36 void DisableCallback() { | |
37 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.
| |
38 } | |
39 // FilePathWatcher::Delegate interface | |
40 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.
| |
41 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
| |
42 scoped_ptr<Callback0::Type> callback_; | |
43 bool callback_enabled; | |
44 DISALLOW_COPY_AND_ASSIGN(DNSWatchDelegate); | |
45 }; | |
46 | |
47 void DNSWatchDelegate::OnFilePathChanged(const FilePath& path) { | |
48 // Calls NetworkChangeNotifier::NotifyObserversOfDNSChange(). | |
49 if (callback_enabled) | |
50 callback_->Run(); | |
51 } | |
52 | |
23 } // namespace | 53 } // namespace |
24 | 54 |
25 class NetworkChangeNotifierLinux::Thread | 55 class NetworkChangeNotifierLinux::Thread |
26 : public base::Thread, public MessageLoopForIO::Watcher { | 56 : public base::Thread, public MessageLoopForIO::Watcher { |
27 public: | 57 public: |
28 Thread(); | 58 Thread(); |
29 virtual ~Thread(); | 59 virtual ~Thread(); |
30 | 60 |
31 // MessageLoopForIO::Watcher: | 61 // MessageLoopForIO::Watcher: |
32 virtual void OnFileCanReadWithoutBlocking(int fd); | 62 virtual void OnFileCanReadWithoutBlocking(int fd); |
33 virtual void OnFileCanWriteWithoutBlocking(int /* fd */); | 63 virtual void OnFileCanWriteWithoutBlocking(int /* fd */); |
34 | 64 |
35 protected: | 65 protected: |
36 // base::Thread | 66 // base::Thread |
37 virtual void Init(); | 67 virtual void Init(); |
38 virtual void CleanUp(); | 68 virtual void CleanUp(); |
39 | 69 |
40 private: | 70 private: |
41 void NotifyObserversOfIPAddressChange() { | 71 void NotifyObserversOfIPAddressChange() { |
42 NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); | 72 NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); |
43 } | 73 } |
44 | 74 |
75 void NotifyObserversOfDNSChange() { | |
76 NetworkChangeNotifier::NotifyObserversOfDNSChange(); | |
77 } | |
78 | |
45 // Starts listening for netlink messages. Also handles the messages if there | 79 // Starts listening for netlink messages. Also handles the messages if there |
46 // are any available on the netlink socket. | 80 // are any available on the netlink socket. |
47 void ListenForNotifications(); | 81 void ListenForNotifications(); |
48 | 82 |
49 // Attempts to read from the netlink socket into |buf| of length |len|. | 83 // 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 | 84 // Returns the bytes read on synchronous success and ERR_IO_PENDING if the |
51 // recv() would block. Otherwise, it returns a net error code. | 85 // recv() would block. Otherwise, it returns a net error code. |
52 int ReadNotificationMessage(char* buf, size_t len); | 86 int ReadNotificationMessage(char* buf, size_t len); |
53 | 87 |
54 // The netlink socket descriptor. | 88 // The netlink socket descriptor. |
55 int netlink_fd_; | 89 int netlink_fd_; |
56 MessageLoopForIO::FileDescriptorWatcher netlink_watcher_; | 90 MessageLoopForIO::FileDescriptorWatcher netlink_watcher_; |
57 | 91 |
58 // Technically only needed for ChromeOS, but it's ugly to #ifdef out. | 92 // Technically only needed for ChromeOS, but it's ugly to #ifdef out. |
59 ScopedRunnableMethodFactory<Thread> method_factory_; | 93 ScopedRunnableMethodFactory<Thread> method_factory_; |
60 | 94 |
95 // Used to watch for changes to /etc/resolv.conf and /etc/hosts. | |
96 scoped_ptr<base::files::FilePathWatcher> resolv_file_watcher_; | |
97 scoped_ptr<base::files::FilePathWatcher> hosts_file_watcher_; | |
98 scoped_refptr<DNSWatchDelegate> file_watcher_delegate_; | |
99 | |
61 DISALLOW_COPY_AND_ASSIGN(Thread); | 100 DISALLOW_COPY_AND_ASSIGN(Thread); |
62 }; | 101 }; |
63 | 102 |
64 NetworkChangeNotifierLinux::Thread::Thread() | 103 NetworkChangeNotifierLinux::Thread::Thread() |
65 : base::Thread("NetworkChangeNotifier"), | 104 : base::Thread("NetworkChangeNotifier"), |
66 netlink_fd_(kInvalidSocket), | 105 netlink_fd_(kInvalidSocket), |
67 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} | 106 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
107 } | |
68 | 108 |
69 NetworkChangeNotifierLinux::Thread::~Thread() {} | 109 NetworkChangeNotifierLinux::Thread::~Thread() {} |
70 | 110 |
71 void NetworkChangeNotifierLinux::Thread::Init() { | 111 void NetworkChangeNotifierLinux::Thread::Init() { |
112 resolv_file_watcher_.reset(new FilePathWatcher); | |
113 hosts_file_watcher_.reset(new FilePathWatcher); | |
114 file_watcher_delegate_ = new DNSWatchDelegate(NewCallback(this, | |
115 &NetworkChangeNotifierLinux::Thread::NotifyObserversOfDNSChange)); | |
116 FilePath resolver(FILE_PATH_LITERAL("/etc/resolv.conf")); | |
117 FilePath tmppath; | |
118 // On systems where /etc/resolv.conf is a symlink, we have to normalize | |
119 // the path and watch that until FilePathWatcher copes with this. See | |
120 // crbug.com/91561. Note that there are some corner cases this workaround | |
121 // won't catch. | |
122 if (file_util::NormalizeFilePath(resolver, &tmppath)) | |
123 resolver = tmppath; | |
124 else | |
125 LOG(ERROR) << "Cannot normalize /etc/resolv.conf"; | |
126 if (!resolv_file_watcher_->Watch(resolver, file_watcher_delegate_.get())) { | |
127 LOG(ERROR) << "Failed to setup watch for /etc/resolv.conf"; | |
128 } | |
129 if (!hosts_file_watcher_->Watch(FilePath(FILE_PATH_LITERAL("/etc/hosts")), | |
130 file_watcher_delegate_.get())) { | |
131 LOG(ERROR) << "Failed to setup watch for /etc/hosts"; | |
132 } | |
72 netlink_fd_ = InitializeNetlinkSocket(); | 133 netlink_fd_ = InitializeNetlinkSocket(); |
73 if (netlink_fd_ < 0) { | 134 if (netlink_fd_ < 0) { |
74 netlink_fd_ = kInvalidSocket; | 135 netlink_fd_ = kInvalidSocket; |
75 return; | 136 return; |
76 } | 137 } |
77 ListenForNotifications(); | 138 ListenForNotifications(); |
78 } | 139 } |
79 | 140 |
80 void NetworkChangeNotifierLinux::Thread::CleanUp() { | 141 void NetworkChangeNotifierLinux::Thread::CleanUp() { |
81 if (netlink_fd_ != kInvalidSocket) { | 142 if (netlink_fd_ != kInvalidSocket) { |
82 if (HANDLE_EINTR(close(netlink_fd_)) != 0) | 143 if (HANDLE_EINTR(close(netlink_fd_)) != 0) |
83 PLOG(ERROR) << "Failed to close socket"; | 144 PLOG(ERROR) << "Failed to close socket"; |
84 netlink_fd_ = kInvalidSocket; | 145 netlink_fd_ = kInvalidSocket; |
85 netlink_watcher_.StopWatchingFileDescriptor(); | 146 netlink_watcher_.StopWatchingFileDescriptor(); |
86 } | 147 } |
148 // If DNSWatchDelegate outlives us, make sure it won't | |
149 // try to call into us during destruction. | |
150 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
| |
87 } | 151 } |
88 | 152 |
89 void NetworkChangeNotifierLinux::Thread::OnFileCanReadWithoutBlocking(int fd) { | 153 void NetworkChangeNotifierLinux::Thread::OnFileCanReadWithoutBlocking(int fd) { |
90 DCHECK_EQ(fd, netlink_fd_); | 154 DCHECK_EQ(fd, netlink_fd_); |
91 ListenForNotifications(); | 155 ListenForNotifications(); |
92 } | 156 } |
93 | 157 |
94 void NetworkChangeNotifierLinux::Thread::OnFileCanWriteWithoutBlocking( | 158 void NetworkChangeNotifierLinux::Thread::OnFileCanWriteWithoutBlocking( |
95 int /* fd */) { | 159 int /* fd */) { |
96 NOTREACHED(); | 160 NOTREACHED(); |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
160 // check that the notifier thread shut down properly. | 224 // check that the notifier thread shut down properly. |
161 notifier_thread_->Stop(); | 225 notifier_thread_->Stop(); |
162 } | 226 } |
163 | 227 |
164 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { | 228 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { |
165 // TODO(eroman): http://crbug.com/53473 | 229 // TODO(eroman): http://crbug.com/53473 |
166 return false; | 230 return false; |
167 } | 231 } |
168 | 232 |
169 } // namespace net | 233 } // namespace net |
OLD | NEW |