Chromium Code Reviews| 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 // 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" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 system_bus_(bus) { } | 75 system_bus_(bus) { } |
| 76 | 76 |
| 77 ~NetworkManagerApi() { } | 77 ~NetworkManagerApi() { } |
| 78 | 78 |
| 79 // Should be called on a helper thread which must be of type IO. | 79 // Should be called on a helper thread which must be of type IO. |
| 80 void Init(); | 80 void Init(); |
| 81 | 81 |
| 82 // Must be called by the helper thread's CleanUp() method. | 82 // Must be called by the helper thread's CleanUp() method. |
| 83 void CleanUp(); | 83 void CleanUp(); |
| 84 | 84 |
| 85 // Implementation of NetworkChangeNotifierLinux::IsCurrentlyOffline(). | 85 // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionState(). |
| 86 // Safe to call from any thread, but will block until Init() has completed. | 86 // Safe to call from any thread, but will block until Init() has completed. |
| 87 bool IsCurrentlyOffline(); | 87 NetworkChangeNotifier::ConnectionState GetCurrentConnectionState(); |
| 88 | 88 |
| 89 private: | 89 private: |
| 90 // Callbacks for D-Bus API. | 90 // Callbacks for D-Bus API. |
| 91 void OnInitialResponse(dbus::Response* response) { | 91 void OnInitialResponse(dbus::Response* response) { |
| 92 HandleResponse(response); | 92 HandleResponse(response); |
| 93 offline_state_initialized_.Signal(); | 93 offline_state_initialized_.Signal(); |
| 94 } | 94 } |
| 95 | 95 |
| 96 void OnSignaled(dbus::Signal* signal); | 96 void OnSignaled(dbus::Signal* signal); |
| 97 | 97 |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 case NM_LEGACY_STATE_ASLEEP: | 223 case NM_LEGACY_STATE_ASLEEP: |
| 224 case NM_STATE_ASLEEP: | 224 case NM_STATE_ASLEEP: |
| 225 // Networking disabled or no devices on system | 225 // Networking disabled or no devices on system |
| 226 return true; | 226 return true; |
| 227 default: | 227 default: |
| 228 // Unknown status | 228 // Unknown status |
| 229 return false; | 229 return false; |
| 230 } | 230 } |
| 231 } | 231 } |
| 232 | 232 |
| 233 bool NetworkManagerApi::IsCurrentlyOffline() { | 233 NetworkChangeNotifier::ConnectionState |
| 234 NetworkManagerApi::GetCurrentConnectionState() { | |
| 234 offline_state_initialized_.Wait(); | 235 offline_state_initialized_.Wait(); |
| 235 base::AutoLock lock(is_offline_lock_); | 236 base::AutoLock lock(is_offline_lock_); |
| 236 return is_offline_; | 237 return is_offline_ ? NetworkChangeNotifier::NONE : |
| 238 NetworkChangeNotifier::ETHERNET; | |
|
joth
2012/01/10 10:48:35
again, seems like UNKNOWN would be a better fit?
| |
| 237 } | 239 } |
| 238 | 240 |
| 239 class DNSWatchDelegate : public FilePathWatcher::Delegate { | 241 class DNSWatchDelegate : public FilePathWatcher::Delegate { |
| 240 public: | 242 public: |
| 241 explicit DNSWatchDelegate(const base::Closure& callback) | 243 explicit DNSWatchDelegate(const base::Closure& callback) |
| 242 : callback_(callback) {} | 244 : callback_(callback) {} |
| 243 virtual ~DNSWatchDelegate() {} | 245 virtual ~DNSWatchDelegate() {} |
| 244 // FilePathWatcher::Delegate interface | 246 // FilePathWatcher::Delegate interface |
| 245 virtual void OnFilePathChanged(const FilePath& path) OVERRIDE; | 247 virtual void OnFilePathChanged(const FilePath& path) OVERRIDE; |
| 246 virtual void OnFilePathError(const FilePath& path) OVERRIDE; | 248 virtual void OnFilePathError(const FilePath& path) OVERRIDE; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 263 class NetworkChangeNotifierLinux::Thread | 265 class NetworkChangeNotifierLinux::Thread |
| 264 : public base::Thread, public MessageLoopForIO::Watcher { | 266 : public base::Thread, public MessageLoopForIO::Watcher { |
| 265 public: | 267 public: |
| 266 explicit Thread(dbus::Bus* bus); | 268 explicit Thread(dbus::Bus* bus); |
| 267 virtual ~Thread(); | 269 virtual ~Thread(); |
| 268 | 270 |
| 269 // MessageLoopForIO::Watcher: | 271 // MessageLoopForIO::Watcher: |
| 270 virtual void OnFileCanReadWithoutBlocking(int fd); | 272 virtual void OnFileCanReadWithoutBlocking(int fd); |
| 271 virtual void OnFileCanWriteWithoutBlocking(int /* fd */); | 273 virtual void OnFileCanWriteWithoutBlocking(int /* fd */); |
| 272 | 274 |
| 273 // Plumbing for NetworkChangeNotifier::IsCurrentlyOffline. | 275 // Plumbing for NetworkChangeNotifier::GetCurrentConnectionState. |
| 274 // Safe to call from any thread. | 276 // Safe to call from any thread. |
| 275 bool IsCurrentlyOffline() { | 277 NetworkChangeNotifier::ConnectionState GetCurrentConnectionState() { |
| 276 return network_manager_api_.IsCurrentlyOffline(); | 278 return network_manager_api_.GetCurrentConnectionState(); |
| 277 } | 279 } |
| 278 | 280 |
| 279 protected: | 281 protected: |
| 280 // base::Thread | 282 // base::Thread |
| 281 virtual void Init(); | 283 virtual void Init(); |
| 282 virtual void CleanUp(); | 284 virtual void CleanUp(); |
| 283 | 285 |
| 284 private: | 286 private: |
| 285 void NotifyObserversOfIPAddressChange() { | 287 void NotifyObserversOfIPAddressChange() { |
| 286 NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); | 288 NetworkChangeNotifier::NotifyObserversOfIPAddressChange(); |
| 287 } | 289 } |
| 288 | 290 |
| 289 static void NotifyObserversOfDNSChange() { | 291 static void NotifyObserversOfDNSChange() { |
| 290 NetworkChangeNotifier::NotifyObserversOfDNSChange(); | 292 NetworkChangeNotifier::NotifyObserversOfDNSChange(); |
| 291 } | 293 } |
| 292 | 294 |
| 293 static void NotifyObserversOfOnlineStateChange() { | 295 static void NotifyObserversOfConnectionStateChange() { |
| 294 NetworkChangeNotifier::NotifyObserversOfOnlineStateChange(); | 296 NetworkChangeNotifier::NotifyObserversOfConnectionStateChange(); |
| 295 } | 297 } |
| 296 | 298 |
| 297 // Starts listening for netlink messages. Also handles the messages if there | 299 // Starts listening for netlink messages. Also handles the messages if there |
| 298 // are any available on the netlink socket. | 300 // are any available on the netlink socket. |
| 299 void ListenForNotifications(); | 301 void ListenForNotifications(); |
| 300 | 302 |
| 301 // Attempts to read from the netlink socket into |buf| of length |len|. | 303 // Attempts to read from the netlink socket into |buf| of length |len|. |
| 302 // Returns the bytes read on synchronous success and ERR_IO_PENDING if the | 304 // Returns the bytes read on synchronous success and ERR_IO_PENDING if the |
| 303 // recv() would block. Otherwise, it returns a net error code. | 305 // recv() would block. Otherwise, it returns a net error code. |
| 304 int ReadNotificationMessage(char* buf, size_t len); | 306 int ReadNotificationMessage(char* buf, size_t len); |
| 305 | 307 |
| 306 // The netlink socket descriptor. | 308 // The netlink socket descriptor. |
| 307 int netlink_fd_; | 309 int netlink_fd_; |
| 308 MessageLoopForIO::FileDescriptorWatcher netlink_watcher_; | 310 MessageLoopForIO::FileDescriptorWatcher netlink_watcher_; |
| 309 | 311 |
| 310 // Technically only needed for ChromeOS, but it's ugly to #ifdef out. | 312 // Technically only needed for ChromeOS, but it's ugly to #ifdef out. |
| 311 base::WeakPtrFactory<Thread> ptr_factory_; | 313 base::WeakPtrFactory<Thread> ptr_factory_; |
| 312 | 314 |
| 313 // Used to watch for changes to /etc/resolv.conf and /etc/hosts. | 315 // Used to watch for changes to /etc/resolv.conf and /etc/hosts. |
| 314 scoped_ptr<base::files::FilePathWatcher> resolv_file_watcher_; | 316 scoped_ptr<base::files::FilePathWatcher> resolv_file_watcher_; |
| 315 scoped_ptr<base::files::FilePathWatcher> hosts_file_watcher_; | 317 scoped_ptr<base::files::FilePathWatcher> hosts_file_watcher_; |
| 316 scoped_refptr<DNSWatchDelegate> file_watcher_delegate_; | 318 scoped_refptr<DNSWatchDelegate> file_watcher_delegate_; |
| 317 | 319 |
| 318 // Used to detect online/offline state changes. | 320 // Used to detect connection state changes. |
| 319 NetworkManagerApi network_manager_api_; | 321 NetworkManagerApi network_manager_api_; |
| 320 | 322 |
| 321 DISALLOW_COPY_AND_ASSIGN(Thread); | 323 DISALLOW_COPY_AND_ASSIGN(Thread); |
| 322 }; | 324 }; |
| 323 | 325 |
| 324 NetworkChangeNotifierLinux::Thread::Thread(dbus::Bus* bus) | 326 NetworkChangeNotifierLinux::Thread::Thread(dbus::Bus* bus) |
| 325 : base::Thread("NetworkChangeNotifier"), | 327 : base::Thread("NetworkChangeNotifier"), |
| 326 netlink_fd_(kInvalidSocket), | 328 netlink_fd_(kInvalidSocket), |
| 327 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)), | 329 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)), |
| 328 network_manager_api_( | 330 network_manager_api_( |
| 329 base::Bind(&NetworkChangeNotifierLinux::Thread | 331 base::Bind(&NetworkChangeNotifierLinux::Thread |
| 330 ::NotifyObserversOfOnlineStateChange), | 332 ::NotifyObserversOfConnectionStateChange), |
| 331 bus) { | 333 bus) { |
| 332 } | 334 } |
| 333 | 335 |
| 334 NetworkChangeNotifierLinux::Thread::~Thread() { | 336 NetworkChangeNotifierLinux::Thread::~Thread() { |
| 335 DCHECK(!Thread::IsRunning()); | 337 DCHECK(!Thread::IsRunning()); |
| 336 } | 338 } |
| 337 | 339 |
| 338 void NetworkChangeNotifierLinux::Thread::Init() { | 340 void NetworkChangeNotifierLinux::Thread::Init() { |
| 339 resolv_file_watcher_.reset(new FilePathWatcher); | 341 resolv_file_watcher_.reset(new FilePathWatcher); |
| 340 hosts_file_watcher_.reset(new FilePathWatcher); | 342 hosts_file_watcher_.reset(new FilePathWatcher); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 452 base::Thread::Options thread_options(MessageLoop::TYPE_IO, 0); | 454 base::Thread::Options thread_options(MessageLoop::TYPE_IO, 0); |
| 453 notifier_thread_->StartWithOptions(thread_options); | 455 notifier_thread_->StartWithOptions(thread_options); |
| 454 } | 456 } |
| 455 | 457 |
| 456 NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() { | 458 NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() { |
| 457 // Stopping from here allows us to sanity- check that the notifier | 459 // Stopping from here allows us to sanity- check that the notifier |
| 458 // thread shut down properly. | 460 // thread shut down properly. |
| 459 notifier_thread_->Stop(); | 461 notifier_thread_->Stop(); |
| 460 } | 462 } |
| 461 | 463 |
| 462 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { | 464 NetworkChangeNotifier::ConnectionState |
| 463 return notifier_thread_->IsCurrentlyOffline(); | 465 NetworkChangeNotifierMac::GetCurrentConnectionState() const { |
| 466 return notifier_thread_->GetCurrentConnectionState(); | |
| 464 } | 467 } |
| 465 | 468 |
| 466 } // namespace net | 469 } // namespace net |
| OLD | NEW |