| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This implementation of NetworkChangeNotifier's offline state detection | 5 // This implementation of NetworkChangeNotifier's offline state detection |
| 6 // depends on D-Bus and NetworkManager, and is known to work on at least | 6 // depends on D-Bus and NetworkManager, and is known to work on at least |
| 7 // GNOME version 2.30. If D-Bus or NetworkManager are unavailable, this | 7 // GNOME version 2.30. If D-Bus or NetworkManager are unavailable, this |
| 8 // implementation will always behave as if it is online. | 8 // implementation will always behave as if it is online. |
| 9 | 9 |
| 10 #include "net/base/network_change_notifier_linux.h" | 10 #include "net/base/network_change_notifier_linux.h" |
| 11 | 11 |
| 12 #include <errno.h> | 12 #include <errno.h> |
| 13 #include <sys/socket.h> | 13 #include <sys/socket.h> |
| 14 | 14 |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/bind_helpers.h" | 16 #include "base/bind_helpers.h" |
| 17 #include "base/callback.h" | 17 #include "base/callback.h" |
| 18 #include "base/compiler_specific.h" | 18 #include "base/compiler_specific.h" |
| 19 #include "base/eintr_wrapper.h" | 19 #include "base/eintr_wrapper.h" |
| 20 #include "base/file_util.h" | 20 #include "base/file_util.h" |
| 21 #include "base/files/file_path_watcher.h" | 21 #include "base/files/file_path_watcher.h" |
| 22 #include "base/memory/weak_ptr.h" | 22 #include "base/memory/weak_ptr.h" |
| 23 #include "base/synchronization/lock.h" | 23 #include "base/synchronization/lock.h" |
| 24 #include "base/synchronization/waitable_event.h" | 24 #include "base/synchronization/waitable_event.h" |
| 25 #include "base/threading/platform_thread.h" | 25 #include "base/threading/platform_thread.h" |
| 26 #include "base/threading/thread.h" | 26 #include "base/threading/thread.h" |
| 27 #include "base/threading/thread_restrictions.h" |
| 27 #include "dbus/bus.h" | 28 #include "dbus/bus.h" |
| 28 #include "dbus/message.h" | 29 #include "dbus/message.h" |
| 29 #include "dbus/object_proxy.h" | 30 #include "dbus/object_proxy.h" |
| 30 #include "net/base/net_errors.h" | 31 #include "net/base/net_errors.h" |
| 31 #include "net/base/network_change_notifier_netlink_linux.h" | 32 #include "net/base/network_change_notifier_netlink_linux.h" |
| 32 | 33 |
| 33 using ::base::files::FilePathWatcher; | 34 using ::base::files::FilePathWatcher; |
| 34 | 35 |
| 35 namespace net { | 36 namespace net { |
| 36 | 37 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 56 NM_STATE_UNKNOWN = 0, | 57 NM_STATE_UNKNOWN = 0, |
| 57 NM_STATE_ASLEEP = 10, | 58 NM_STATE_ASLEEP = 10, |
| 58 NM_STATE_DISCONNECTED = 20, | 59 NM_STATE_DISCONNECTED = 20, |
| 59 NM_STATE_DISCONNECTING = 30, | 60 NM_STATE_DISCONNECTING = 30, |
| 60 NM_STATE_CONNECTING = 40, | 61 NM_STATE_CONNECTING = 40, |
| 61 NM_STATE_CONNECTED_LOCAL = 50, | 62 NM_STATE_CONNECTED_LOCAL = 50, |
| 62 NM_STATE_CONNECTED_SITE = 60, | 63 NM_STATE_CONNECTED_SITE = 60, |
| 63 NM_STATE_CONNECTED_GLOBAL = 70 | 64 NM_STATE_CONNECTED_GLOBAL = 70 |
| 64 }; | 65 }; |
| 65 | 66 |
| 67 class DNSWatchDelegate : public FilePathWatcher::Delegate { |
| 68 public: |
| 69 explicit DNSWatchDelegate(const base::Closure& callback) |
| 70 : callback_(callback) {} |
| 71 virtual ~DNSWatchDelegate() {} |
| 72 // FilePathWatcher::Delegate interface |
| 73 virtual void OnFilePathChanged(const FilePath& path) OVERRIDE; |
| 74 virtual void OnFilePathError(const FilePath& path) OVERRIDE; |
| 75 private: |
| 76 base::Closure callback_; |
| 77 DISALLOW_COPY_AND_ASSIGN(DNSWatchDelegate); |
| 78 }; |
| 79 |
| 80 void DNSWatchDelegate::OnFilePathChanged(const FilePath& path) { |
| 81 // Calls NetworkChangeNotifier::NotifyObserversOfDNSChange(). |
| 82 callback_.Run(); |
| 83 } |
| 84 |
| 85 void DNSWatchDelegate::OnFilePathError(const FilePath& path) { |
| 86 LOG(ERROR) << "DNSWatchDelegate::OnFilePathError for " << path.value(); |
| 87 } |
| 88 |
| 89 } // namespace |
| 90 |
| 66 // A wrapper around NetworkManager's D-Bus API. | 91 // A wrapper around NetworkManager's D-Bus API. |
| 67 class NetworkManagerApi { | 92 class NetworkManagerApi { |
| 68 public: | 93 public: |
| 69 NetworkManagerApi(const base::Closure& notification_callback, dbus::Bus* bus) | 94 NetworkManagerApi(const base::Closure& notification_callback, dbus::Bus* bus) |
| 70 : is_offline_(false), | 95 : is_offline_(false), |
| 71 offline_state_initialized_(true /*manual_reset*/, false), | 96 offline_state_initialized_(true /*manual_reset*/, false), |
| 72 notification_callback_(notification_callback), | 97 notification_callback_(notification_callback), |
| 73 helper_thread_id_(base::kInvalidThreadId), | 98 helper_thread_id_(base::kInvalidThreadId), |
| 74 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)), | 99 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)), |
| 75 system_bus_(bus) { } | 100 system_bus_(bus) { } |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 case NM_STATE_ASLEEP: | 250 case NM_STATE_ASLEEP: |
| 226 // Networking disabled or no devices on system | 251 // Networking disabled or no devices on system |
| 227 return true; | 252 return true; |
| 228 default: | 253 default: |
| 229 // Unknown status | 254 // Unknown status |
| 230 return false; | 255 return false; |
| 231 } | 256 } |
| 232 } | 257 } |
| 233 | 258 |
| 234 bool NetworkManagerApi::IsCurrentlyOffline() { | 259 bool NetworkManagerApi::IsCurrentlyOffline() { |
| 260 // http://crbug.com/125097 |
| 261 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 235 offline_state_initialized_.Wait(); | 262 offline_state_initialized_.Wait(); |
| 236 base::AutoLock lock(is_offline_lock_); | 263 base::AutoLock lock(is_offline_lock_); |
| 237 return is_offline_; | 264 return is_offline_; |
| 238 } | 265 } |
| 239 | 266 |
| 240 class DNSWatchDelegate : public FilePathWatcher::Delegate { | |
| 241 public: | |
| 242 explicit DNSWatchDelegate(const base::Closure& callback) | |
| 243 : callback_(callback) {} | |
| 244 virtual ~DNSWatchDelegate() {} | |
| 245 // FilePathWatcher::Delegate interface | |
| 246 virtual void OnFilePathChanged(const FilePath& path) OVERRIDE; | |
| 247 virtual void OnFilePathError(const FilePath& path) OVERRIDE; | |
| 248 private: | |
| 249 base::Closure callback_; | |
| 250 DISALLOW_COPY_AND_ASSIGN(DNSWatchDelegate); | |
| 251 }; | |
| 252 | |
| 253 void DNSWatchDelegate::OnFilePathChanged(const FilePath& path) { | |
| 254 // Calls NetworkChangeNotifier::NotifyObserversOfDNSChange(). | |
| 255 callback_.Run(); | |
| 256 } | |
| 257 | |
| 258 void DNSWatchDelegate::OnFilePathError(const FilePath& path) { | |
| 259 LOG(ERROR) << "DNSWatchDelegate::OnFilePathError for " << path.value(); | |
| 260 } | |
| 261 | |
| 262 } // namespace | |
| 263 | |
| 264 class NetworkChangeNotifierLinux::Thread | 267 class NetworkChangeNotifierLinux::Thread |
| 265 : public base::Thread, public MessageLoopForIO::Watcher { | 268 : public base::Thread, public MessageLoopForIO::Watcher { |
| 266 public: | 269 public: |
| 267 explicit Thread(dbus::Bus* bus); | 270 explicit Thread(dbus::Bus* bus); |
| 268 virtual ~Thread(); | 271 virtual ~Thread(); |
| 269 | 272 |
| 270 // MessageLoopForIO::Watcher: | 273 // MessageLoopForIO::Watcher: |
| 271 virtual void OnFileCanReadWithoutBlocking(int fd); | 274 virtual void OnFileCanReadWithoutBlocking(int fd); |
| 272 virtual void OnFileCanWriteWithoutBlocking(int /* fd */); | 275 virtual void OnFileCanWriteWithoutBlocking(int /* fd */); |
| 273 | 276 |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 // Stopping from here allows us to sanity- check that the notifier | 440 // Stopping from here allows us to sanity- check that the notifier |
| 438 // thread shut down properly. | 441 // thread shut down properly. |
| 439 notifier_thread_->Stop(); | 442 notifier_thread_->Stop(); |
| 440 } | 443 } |
| 441 | 444 |
| 442 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { | 445 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { |
| 443 return notifier_thread_->IsCurrentlyOffline(); | 446 return notifier_thread_->IsCurrentlyOffline(); |
| 444 } | 447 } |
| 445 | 448 |
| 446 } // namespace net | 449 } // namespace net |
| OLD | NEW |