Chromium Code Reviews| 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" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 system_bus_(bus) { } | 100 system_bus_(bus) { } |
| 101 | 101 |
| 102 ~NetworkManagerApi() { } | 102 ~NetworkManagerApi() { } |
| 103 | 103 |
| 104 // Should be called on a helper thread which must be of type IO. | 104 // Should be called on a helper thread which must be of type IO. |
| 105 void Init(); | 105 void Init(); |
| 106 | 106 |
| 107 // Must be called by the helper thread's CleanUp() method. | 107 // Must be called by the helper thread's CleanUp() method. |
| 108 void CleanUp(); | 108 void CleanUp(); |
| 109 | 109 |
| 110 // Implementation of NetworkChangeNotifierLinux::IsCurrentlyOffline(). | 110 // Implementation of NetworkChangeNotifierLinux::GetCurrentConnectionType(). |
| 111 // Safe to call from any thread, but will block until Init() has completed. | 111 // Safe to call from any thread, but will block until Init() has completed. |
| 112 bool IsCurrentlyOffline(); | 112 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType(); |
| 113 | 113 |
| 114 private: | 114 private: |
| 115 // Callbacks for D-Bus API. | 115 // Callbacks for D-Bus API. |
| 116 void OnInitialResponse(dbus::Response* response) { | 116 void OnInitialResponse(dbus::Response* response) { |
| 117 HandleResponse(response); | 117 HandleResponse(response); |
| 118 offline_state_initialized_.Signal(); | 118 offline_state_initialized_.Signal(); |
| 119 } | 119 } |
| 120 | 120 |
| 121 void OnSignaled(dbus::Signal* signal); | 121 void OnSignaled(dbus::Signal* signal); |
| 122 | 122 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 249 case NM_LEGACY_STATE_ASLEEP: | 249 case NM_LEGACY_STATE_ASLEEP: |
| 250 case NM_STATE_ASLEEP: | 250 case NM_STATE_ASLEEP: |
| 251 // Networking disabled or no devices on system | 251 // Networking disabled or no devices on system |
| 252 return true; | 252 return true; |
| 253 default: | 253 default: |
| 254 // Unknown status | 254 // Unknown status |
| 255 return false; | 255 return false; |
| 256 } | 256 } |
| 257 } | 257 } |
| 258 | 258 |
| 259 bool NetworkManagerApi::IsCurrentlyOffline() { | 259 NetworkChangeNotifier::ConnectionType |
| 260 NetworkManagerApi::GetCurrentConnectionType() { | |
| 260 // http://crbug.com/125097 | 261 // http://crbug.com/125097 |
| 261 base::ThreadRestrictions::ScopedAllowWait allow_wait; | 262 base::ThreadRestrictions::ScopedAllowWait allow_wait; |
| 262 offline_state_initialized_.Wait(); | 263 offline_state_initialized_.Wait(); |
| 263 base::AutoLock lock(is_offline_lock_); | 264 base::AutoLock lock(is_offline_lock_); |
| 264 return is_offline_; | 265 // TODO: Return something more detailed than CONNECTION_UNKNOWN. |
|
wtc
2012/05/11 01:35:05
Nit: please add 'droger' after TODO. This is reco
| |
| 266 return is_offline_ ? NetworkChangeNotifier::CONNECTION_NONE : | |
| 267 NetworkChangeNotifier::CONNECTION_UNKNOWN; | |
| 265 } | 268 } |
| 266 | 269 |
| 267 class NetworkChangeNotifierLinux::Thread | 270 class NetworkChangeNotifierLinux::Thread |
| 268 : public base::Thread, public MessageLoopForIO::Watcher { | 271 : public base::Thread, public MessageLoopForIO::Watcher { |
| 269 public: | 272 public: |
| 270 explicit Thread(dbus::Bus* bus); | 273 explicit Thread(dbus::Bus* bus); |
| 271 virtual ~Thread(); | 274 virtual ~Thread(); |
| 272 | 275 |
| 273 // MessageLoopForIO::Watcher: | 276 // MessageLoopForIO::Watcher: |
| 274 virtual void OnFileCanReadWithoutBlocking(int fd); | 277 virtual void OnFileCanReadWithoutBlocking(int fd); |
| 275 virtual void OnFileCanWriteWithoutBlocking(int /* fd */); | 278 virtual void OnFileCanWriteWithoutBlocking(int /* fd */); |
| 276 | 279 |
| 277 // Plumbing for NetworkChangeNotifier::IsCurrentlyOffline. | 280 // Plumbing for NetworkChangeNotifier::GetCurrentConnectionType. |
| 278 // Safe to call from any thread. | 281 // Safe to call from any thread. |
| 279 bool IsCurrentlyOffline() { | 282 NetworkChangeNotifier::ConnectionType GetCurrentConnectionType() { |
| 280 return network_manager_api_.IsCurrentlyOffline(); | 283 return network_manager_api_.GetCurrentConnectionType(); |
| 281 } | 284 } |
| 282 | 285 |
| 283 protected: | 286 protected: |
| 284 // base::Thread | 287 // base::Thread |
| 285 virtual void Init(); | 288 virtual void Init(); |
| 286 virtual void CleanUp(); | 289 virtual void CleanUp(); |
| 287 | 290 |
| 288 private: | 291 private: |
| 289 // Starts listening for netlink messages. Also handles the messages if there | 292 // Starts listening for netlink messages. Also handles the messages if there |
| 290 // are any available on the netlink socket. | 293 // are any available on the netlink socket. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 301 | 304 |
| 302 // Technically only needed for ChromeOS, but it's ugly to #ifdef out. | 305 // Technically only needed for ChromeOS, but it's ugly to #ifdef out. |
| 303 base::WeakPtrFactory<Thread> ptr_factory_; | 306 base::WeakPtrFactory<Thread> ptr_factory_; |
| 304 | 307 |
| 305 // Used to watch for changes to /etc/resolv.conf and /etc/hosts. | 308 // Used to watch for changes to /etc/resolv.conf and /etc/hosts. |
| 306 scoped_ptr<base::files::FilePathWatcher> resolv_file_watcher_; | 309 scoped_ptr<base::files::FilePathWatcher> resolv_file_watcher_; |
| 307 scoped_ptr<base::files::FilePathWatcher> hosts_file_watcher_; | 310 scoped_ptr<base::files::FilePathWatcher> hosts_file_watcher_; |
| 308 scoped_refptr<DNSWatchDelegate> resolv_watcher_delegate_; | 311 scoped_refptr<DNSWatchDelegate> resolv_watcher_delegate_; |
| 309 scoped_refptr<DNSWatchDelegate> hosts_watcher_delegate_; | 312 scoped_refptr<DNSWatchDelegate> hosts_watcher_delegate_; |
| 310 | 313 |
| 311 // Used to detect online/offline state changes. | 314 // Used to detect connection state changes. |
|
wtc
2012/05/11 01:35:05
state => type
| |
| 312 NetworkManagerApi network_manager_api_; | 315 NetworkManagerApi network_manager_api_; |
| 313 | 316 |
| 314 DISALLOW_COPY_AND_ASSIGN(Thread); | 317 DISALLOW_COPY_AND_ASSIGN(Thread); |
| 315 }; | 318 }; |
| 316 | 319 |
| 317 NetworkChangeNotifierLinux::Thread::Thread(dbus::Bus* bus) | 320 NetworkChangeNotifierLinux::Thread::Thread(dbus::Bus* bus) |
| 318 : base::Thread("NetworkChangeNotifier"), | 321 : base::Thread("NetworkChangeNotifier"), |
| 319 netlink_fd_(kInvalidSocket), | 322 netlink_fd_(kInvalidSocket), |
| 320 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)), | 323 ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)), |
| 321 network_manager_api_( | 324 network_manager_api_( |
| 322 base::Bind(&NetworkChangeNotifier | 325 base::Bind(&NetworkChangeNotifier |
| 323 ::NotifyObserversOfOnlineStateChange), | 326 ::NotifyObserversOfConnectionTypeChange), |
| 324 bus) { | 327 bus) { |
| 325 } | 328 } |
| 326 | 329 |
| 327 NetworkChangeNotifierLinux::Thread::~Thread() { | 330 NetworkChangeNotifierLinux::Thread::~Thread() { |
| 328 DCHECK(!Thread::IsRunning()); | 331 DCHECK(!Thread::IsRunning()); |
| 329 } | 332 } |
| 330 | 333 |
| 331 void NetworkChangeNotifierLinux::Thread::Init() { | 334 void NetworkChangeNotifierLinux::Thread::Init() { |
| 332 resolv_file_watcher_.reset(new FilePathWatcher); | 335 resolv_file_watcher_.reset(new FilePathWatcher); |
| 333 hosts_file_watcher_.reset(new FilePathWatcher); | 336 hosts_file_watcher_.reset(new FilePathWatcher); |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 435 base::Thread::Options thread_options(MessageLoop::TYPE_IO, 0); | 438 base::Thread::Options thread_options(MessageLoop::TYPE_IO, 0); |
| 436 notifier_thread_->StartWithOptions(thread_options); | 439 notifier_thread_->StartWithOptions(thread_options); |
| 437 } | 440 } |
| 438 | 441 |
| 439 NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() { | 442 NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() { |
| 440 // Stopping from here allows us to sanity- check that the notifier | 443 // Stopping from here allows us to sanity- check that the notifier |
| 441 // thread shut down properly. | 444 // thread shut down properly. |
| 442 notifier_thread_->Stop(); | 445 notifier_thread_->Stop(); |
| 443 } | 446 } |
| 444 | 447 |
| 445 bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { | 448 NetworkChangeNotifier::ConnectionType |
| 446 return notifier_thread_->IsCurrentlyOffline(); | 449 NetworkChangeNotifierLinux::GetCurrentConnectionType() const { |
| 450 return notifier_thread_->GetCurrentConnectionType(); | |
| 447 } | 451 } |
| 448 | 452 |
| 449 } // namespace net | 453 } // namespace net |
| OLD | NEW |