Chromium Code Reviews| Index: net/base/network_change_notifier_linux.cc |
| diff --git a/net/base/network_change_notifier_linux.cc b/net/base/network_change_notifier_linux.cc |
| index 77d80432366fe3b5842757fd14c8c8684880ac68..c3dd71fe75aeb752619767bba69db3c579ac8951 100644 |
| --- a/net/base/network_change_notifier_linux.cc |
| +++ b/net/base/network_change_notifier_linux.cc |
| @@ -15,7 +15,13 @@ |
| #include "base/file_util.h" |
| #include "base/files/file_path_watcher.h" |
| #include "base/memory/weak_ptr.h" |
| +#include "base/synchronization/lock.h" |
| +#include "base/synchronization/waitable_event.h" |
| +#include "base/threading/platform_thread.h" |
| #include "base/threading/thread.h" |
| +#include "dbus/bus.h" |
| +#include "dbus/message.h" |
| +#include "dbus/object_proxy.h" |
| #include "net/base/net_errors.h" |
| #include "net/base/network_change_notifier_netlink_linux.h" |
| @@ -27,6 +33,192 @@ namespace { |
| const int kInvalidSocket = -1; |
| +const char kNetworkManagerServiceName[] = "org.freedesktop.NetworkManager"; |
| +const char kNetworkManagerPath[] = "/org/freedesktop/NetworkManager"; |
| +const char kNetworkManagerInterface[] = "org.freedesktop.NetworkManager"; |
| + |
| +// http://projects.gnome.org/NetworkManager/developers/spec-08.html#type-NM_STATE |
| +enum { |
| + NM_LEGACY_STATE_UNKNOWN = 0, |
| + NM_LEGACY_STATE_ASLEEP = 1, |
| + NM_LEGACY_STATE_CONNECTING = 2, |
| + NM_LEGACY_STATE_CONNECTED = 3, |
| + NM_LEGACY_STATE_DISCONNECTED = 4 |
| +}; |
| + |
| +// http://projects.gnome.org/NetworkManager/developers/migrating-to-09/spec.html#type-NM_STATE |
| +enum { |
| + NM_STATE_UNKNOWN = 0, |
| + NM_STATE_ASLEEP = 10, |
| + NM_STATE_DISCONNECTED = 20, |
| + NM_STATE_DISCONNECTING = 30, |
| + NM_STATE_CONNECTING = 40, |
| + NM_STATE_CONNECTED_LOCAL = 50, |
| + NM_STATE_CONNECTED_SITE = 60, |
| + NM_STATE_CONNECTED_GLOBAL = 70 |
| +}; |
| + |
| +// A wrapper around NetworkManager's D-Bus API. |
| +class NetworkManagerApi { |
| + public: |
| + explicit NetworkManagerApi(const base::Closure& notification_callback) |
| + : is_offline_(false), |
| + offline_state_initialized_(true /*manual_reset*/, false), |
| + notification_callback_(notification_callback), |
| + helper_thread_id_(base::kInvalidThreadId), |
| + ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)) { } |
| + |
| + // For testing. |
| + NetworkManagerApi(const base::Closure& notification_callback, dbus::Bus* bus) |
| + : is_offline_(false), |
| + offline_state_initialized_(true /*manual_reset*/, false), |
| + notification_callback_(notification_callback), |
| + helper_thread_id_(base::kInvalidThreadId), |
| + ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)), |
| + system_bus_(bus) { } |
| + |
| + ~NetworkManagerApi() { } |
| + |
| + // Should be called on a helper thread which must be of type IO. |
| + void Init(); |
| + |
| + // Must be called by the helper thread's CleanUp() method. |
| + void CleanUp(); |
| + |
| + // Implementation of NetworkChangeNotifierLinux::IsCurrentlyOffline(). |
| + // Safe to call from any thread, but will block until Init() has completed. |
| + bool IsCurrentlyOffline(); |
| + |
| + private: |
| + // Callbacks for D-Bus API. |
| + void OnStateChanged(dbus::Message* message); |
|
satorux1
2011/10/26 17:37:09
We usually have a blank line between functions.
adamk
2011/10/26 18:35:52
Done.
|
| + void OnResponse(dbus::Response* response) { |
| + OnStateChanged(response); |
| + offline_state_initialized_.Signal(); |
| + } |
| + void OnSignaled(dbus::Signal* signal) { |
| + OnStateChanged(signal); |
| + } |
| + void OnConnected(const std::string&, const std::string&, bool success) { |
| + DLOG_IF(WARNING, !success) << "Failed to set up offline state detection"; |
| + } |
| + |
| + // Converts a NetworkManager state uint to a bool. |
| + static bool StateIsOffline(uint32 state); |
| + |
| + bool is_offline_; |
| + base::Lock is_offline_lock_; |
| + base::WaitableEvent offline_state_initialized_; |
| + |
| + base::Closure notification_callback_; |
| + |
| + base::PlatformThreadId helper_thread_id_; |
| + |
| + base::WeakPtrFactory<NetworkManagerApi> ptr_factory_; |
| + |
| + scoped_refptr<dbus::Bus> system_bus_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(NetworkManagerApi); |
| +}; |
| + |
| +void NetworkManagerApi::Init() { |
| + // D-Bus requires an IO MessageLoop. |
| + DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_IO); |
| + helper_thread_id_ = base::PlatformThread::CurrentId(); |
| + |
| + if (!system_bus_) { |
| + dbus::Bus::Options options; |
| + options.bus_type = dbus::Bus::SYSTEM; |
| + options.connection_type = dbus::Bus::PRIVATE; |
| + system_bus_ = new dbus::Bus(options); |
| + } |
| + |
| + dbus::ObjectProxy* proxy = |
| + system_bus_->GetObjectProxy(kNetworkManagerServiceName, |
| + kNetworkManagerPath); |
| + |
| + // Get the initial state asynchronously. |
| + dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get"); |
| + dbus::MessageWriter builder(&method_call); |
| + builder.AppendString(kNetworkManagerInterface); |
| + builder.AppendString("State"); |
| + proxy->CallMethod( |
| + &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| + base::Bind(&NetworkManagerApi::OnResponse, ptr_factory_.GetWeakPtr())); |
| + |
| + // And sign up for notifications. |
| + proxy->ConnectToSignal( |
| + kNetworkManagerInterface, |
| + "StateChanged", |
| + base::Bind(&NetworkManagerApi::OnSignaled, ptr_factory_.GetWeakPtr()), |
| + base::Bind(&NetworkManagerApi::OnConnected, ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void NetworkManagerApi::CleanUp() { |
| + DCHECK_EQ(helper_thread_id_, base::PlatformThread::CurrentId()); |
| + ptr_factory_.InvalidateWeakPtrs(); |
| +} |
| + |
| +void NetworkManagerApi::OnStateChanged(dbus::Message* message) { |
| + DCHECK_EQ(helper_thread_id_, base::PlatformThread::CurrentId()); |
| + if (!message) { |
| + DLOG(WARNING) << "No response received for initial state request"; |
| + return; |
| + } |
| + dbus::MessageReader reader(message); |
| + uint32 state = 0; |
| + if (!reader.HasMoreData() || !reader.PopUint32(&state)) { |
| + DLOG(WARNING) << "Unexpected response for NetworkManager State request: " |
| + << message->ToString(); |
| + return; |
| + } |
| + bool new_is_offline = StateIsOffline(state); |
| + { |
| + base::AutoLock lock(is_offline_lock_); |
| + if (is_offline_ != new_is_offline) |
| + is_offline_ = new_is_offline; |
| + else |
| + return; |
| + } |
| + if (offline_state_initialized_.IsSignaled()) |
| + notification_callback_.Run(); |
| +} |
| + |
| +bool NetworkManagerApi::StateIsOffline(uint32 state) { |
| + switch (state) { |
| + case NM_LEGACY_STATE_CONNECTED: |
| + case NM_STATE_CONNECTED_SITE: |
| + case NM_STATE_CONNECTED_GLOBAL: |
| + // Definitely connected |
| + return false; |
| + case NM_LEGACY_STATE_DISCONNECTED: |
| + case NM_STATE_DISCONNECTED: |
| + // Definitely disconnected |
| + return true; |
| + case NM_STATE_CONNECTED_LOCAL: |
| + // Local networking only; I'm treating this as offline (keybuk) |
| + return true; |
| + case NM_LEGACY_STATE_CONNECTING: |
| + case NM_STATE_DISCONNECTING: |
| + case NM_STATE_CONNECTING: |
| + // In-flight change to connection status currently underway |
| + return true; |
| + case NM_LEGACY_STATE_ASLEEP: |
| + case NM_STATE_ASLEEP: |
| + // Networking disabled or no devices on system |
| + return true; |
| + default: |
| + // Unknown status |
| + return false; |
| + } |
| +} |
| + |
| +bool NetworkManagerApi::IsCurrentlyOffline() { |
| + offline_state_initialized_.Wait(); |
| + base::AutoLock lock(is_offline_lock_); |
| + return is_offline_; |
| +} |
| + |
| class DNSWatchDelegate : public FilePathWatcher::Delegate { |
| public: |
| explicit DNSWatchDelegate(const base::Closure& callback) |
| @@ -55,12 +247,19 @@ class NetworkChangeNotifierLinux::Thread |
| : public base::Thread, public MessageLoopForIO::Watcher { |
| public: |
| Thread(); |
| + explicit Thread(dbus::Bus* bus); |
|
satorux1
2011/10/26 17:37:09
Function overloading is prohibited per our C++ sty
adamk
2011/10/26 18:35:52
Done, and got rid of the extra constructor on Netw
|
| virtual ~Thread(); |
| // MessageLoopForIO::Watcher: |
| virtual void OnFileCanReadWithoutBlocking(int fd); |
| virtual void OnFileCanWriteWithoutBlocking(int /* fd */); |
| + // Plumbing for NetworkChangeNotifier::IsCurrentlyOffline. |
| + // Safe to call from any thread. |
| + bool IsCurrentlyOffline() { |
| + return network_manager_api_.IsCurrentlyOffline(); |
| + } |
| + |
| protected: |
| // base::Thread |
| virtual void Init(); |
| @@ -75,6 +274,10 @@ class NetworkChangeNotifierLinux::Thread |
| NetworkChangeNotifier::NotifyObserversOfDNSChange(); |
| } |
| + static void NotifyObserversOfOnlineStateChange() { |
| + NetworkChangeNotifier::NotifyObserversOfOnlineStateChange(); |
| + } |
| + |
| // Starts listening for netlink messages. Also handles the messages if there |
| // are any available on the netlink socket. |
| void ListenForNotifications(); |
| @@ -96,13 +299,29 @@ class NetworkChangeNotifierLinux::Thread |
| scoped_ptr<base::files::FilePathWatcher> hosts_file_watcher_; |
| scoped_refptr<DNSWatchDelegate> file_watcher_delegate_; |
| + // Used to detect online/offline state changes. |
| + NetworkManagerApi network_manager_api_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(Thread); |
| }; |
| NetworkChangeNotifierLinux::Thread::Thread() |
| : base::Thread("NetworkChangeNotifier"), |
| netlink_fd_(kInvalidSocket), |
| - ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)) { |
| + ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)), |
| + network_manager_api_( |
| + base::Bind(&NetworkChangeNotifierLinux::Thread |
| + ::NotifyObserversOfOnlineStateChange)) { |
| +} |
| + |
| +NetworkChangeNotifierLinux::Thread::Thread(dbus::Bus* bus) |
| + : base::Thread("NetworkChangeNotifier"), |
| + netlink_fd_(kInvalidSocket), |
| + ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)), |
| + network_manager_api_( |
| + base::Bind(&NetworkChangeNotifierLinux::Thread |
| + ::NotifyObserversOfOnlineStateChange), |
| + bus) { |
| } |
| NetworkChangeNotifierLinux::Thread::~Thread() {} |
| @@ -128,6 +347,8 @@ void NetworkChangeNotifierLinux::Thread::Init() { |
| return; |
| } |
| ListenForNotifications(); |
| + |
| + network_manager_api_.Init(); |
| } |
| void NetworkChangeNotifierLinux::Thread::CleanUp() { |
| @@ -141,6 +362,8 @@ void NetworkChangeNotifierLinux::Thread::CleanUp() { |
| // into us via the delegate during destruction. |
| resolv_file_watcher_.reset(); |
| hosts_file_watcher_.reset(); |
| + |
| + network_manager_api_.CleanUp(); |
| } |
| void NetworkChangeNotifierLinux::Thread::OnFileCanReadWithoutBlocking(int fd) { |
| @@ -213,6 +436,13 @@ NetworkChangeNotifierLinux::NetworkChangeNotifierLinux() |
| notifier_thread_->StartWithOptions(thread_options); |
| } |
| +// Used only for testing, injects a dbus::Bus into the helper thread. |
| +NetworkChangeNotifierLinux::NetworkChangeNotifierLinux(dbus::Bus* bus) |
| + : notifier_thread_(new Thread(bus)) { |
| + base::Thread::Options thread_options(MessageLoop::TYPE_IO, 0); |
| + notifier_thread_->StartWithOptions(thread_options); |
| +} |
| + |
| NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() { |
| // We don't need to explicitly Stop(), but doing so allows us to sanity- |
| // check that the notifier thread shut down properly. |
| @@ -220,8 +450,7 @@ NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() { |
| } |
| bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const { |
| - // TODO(eroman): http://crbug.com/53473 |
| - return false; |
| + return notifier_thread_->IsCurrentlyOffline(); |
| } |
| } // namespace net |