Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Unified Diff: net/base/network_change_notifier_linux.cc

Issue 8249008: Offline state detection for linux, using new D-Bus library. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Responded to comments Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | net/net.gyp » ('j') | net/net.gyp » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 45ee139fc28b38e0d1c4eb1c1706199307788238..f4f06c5335e099f24e3fc60c4062bdbffb1c29e4 100644
--- a/net/base/network_change_notifier_linux.cc
+++ b/net/base/network_change_notifier_linux.cc
@@ -8,13 +8,21 @@
#include <sys/socket.h>
#include "base/bind.h"
+#include "base/callback.h"
#include "base/callback_old.h"
#include "base/compiler_specific.h"
#include "base/eintr_wrapper.h"
#include "base/file_util.h"
#include "base/files/file_path_watcher.h"
+#include "base/memory/weak_ptr.h"
+#include "base/synchronization/condition_variable.h"
+#include "base/synchronization/lock.h"
#include "base/task.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"
@@ -26,6 +34,208 @@ 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:
+ typedef base::Callback<void(void)> NotificationCallback;
+
+ explicit NetworkManagerApi(NotificationCallback notification_callback)
+ : online_state_(UNINITIALIZED),
+ initial_state_cv_(&online_state_lock_),
+ notification_callback_(notification_callback),
+ helper_thread_id_(base::kInvalidThreadId),
satorux1 2011/10/13 06:58:05 Not sure if it helps, but saving the origin thread
adamk 2011/10/13 18:14:06 Nothing should run on the origin thread except the
+ ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)) { }
+
+ ~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:
+ enum OnlineState {
+ UNINITIALIZED = -1,
+ OFFLINE = 0,
+ ONLINE = 1
+ };
+
+ void NotifyObserversOfOnlineStateChange() {
+ notification_callback_.Run();
+ }
+
+ // Callbacks for D-Bus API.
+ void OnStateChanged(dbus::Signal* signal);
+ void OnConnected(const std::string&, const std::string&, bool success) {
+ LOG_IF(WARNING, !success) << "Failed to set up offline state detection";
+ }
+
+ // Synchronously gets the current state from the NetworkManager.
+ // Should be called on a helper thread.
+ uint32 GetCurrentState(dbus::ObjectProxy* proxy);
+
+ // Converts a NetworkManager state uint to our enum.
+ // Always returns either ONLINE or OFFLINE.
+ static OnlineState TranslateState(uint32 state);
+
+ OnlineState online_state_;
+ base::Lock online_state_lock_;
+ base::ConditionVariable initial_state_cv_;
+
+ NotificationCallback notification_callback_;
+
+ scoped_refptr<dbus::Bus> system_bus_;
+
+ base::PlatformThreadId helper_thread_id_;
+
+ base::WeakPtrFactory<NetworkManagerApi> ptr_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(NetworkManagerApi);
+};
+
+void NetworkManagerApi::Init() {
+ DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_IO);
satorux1 2011/10/13 06:58:05 You might want to add a comment why TYPE_IO matter
adamk 2011/10/13 18:14:06 Done.
+ helper_thread_id_ = base::PlatformThread::CurrentId();
+
+ 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);
+ OnlineState online_state = TranslateState(GetCurrentState(proxy));
+ {
+ base::AutoLock lock(online_state_lock_);
+ online_state_ = online_state;
+ initial_state_cv_.Signal();
+ }
+
+ proxy->ConnectToSignal(
+ kNetworkManagerInterface,
+ "StateChanged",
+ base::Bind(&NetworkManagerApi::OnStateChanged, 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::Signal* signal) {
+ DCHECK_EQ(helper_thread_id_, base::PlatformThread::CurrentId());
+ DCHECK(signal);
+ dbus::MessageReader reader(signal);
+ uint32 state;
+ if (!reader.PopUint32(&state)) {
+ LOG(WARNING) << "Unexpected response for NetworkManager State request: "
+ << signal->ToString();
+ return;
+ }
+ OnlineState new_online_state = TranslateState(state);
+ {
+ base::AutoLock lock(online_state_lock_);
+ if (new_online_state != online_state_)
+ online_state_ = new_online_state;
+ else
+ return;
+ }
+ // Can't notify while we hold the lock
+ NotifyObserversOfOnlineStateChange();
+}
+
+uint32 NetworkManagerApi::GetCurrentState(dbus::ObjectProxy* proxy) {
+ DCHECK_EQ(helper_thread_id_, base::PlatformThread::CurrentId());
+ dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get");
+ dbus::MessageWriter builder(&method_call);
+ builder.AppendString(kNetworkManagerInterface);
+ builder.AppendString("State");
+ scoped_ptr<dbus::Response> response(
+ proxy->CallMethodAndBlock(&method_call,
+ dbus::ObjectProxy::TIMEOUT_USE_DEFAULT));
+ if (!response.get())
+ return NM_STATE_UNKNOWN;
+
+ dbus::MessageReader reader(response.get());
+ uint32 state;
+ if (!reader.PopUint32(&state)) {
+ LOG(WARNING) << "Unexpected response for NetworkManager State request: "
+ << response->ToString();
+ return NM_STATE_UNKNOWN;
+ }
+
+ return state;
+}
+
+NetworkManagerApi::OnlineState NetworkManagerApi::TranslateState(uint32 state) {
+ switch (state) {
+ case NM_LEGACY_STATE_CONNECTED:
+ case NM_STATE_CONNECTED_SITE:
+ case NM_STATE_CONNECTED_GLOBAL:
+ // Definitely connected
+ return ONLINE;
+ case NM_LEGACY_STATE_DISCONNECTED:
+ case NM_STATE_DISCONNECTED:
+ // Definitely disconnected
+ return OFFLINE;
+ case NM_STATE_CONNECTED_LOCAL:
+ // Local networking only; I'm treating this as offline (keybuk)
+ return OFFLINE;
+ case NM_LEGACY_STATE_CONNECTING:
+ case NM_STATE_DISCONNECTING:
+ case NM_STATE_CONNECTING:
+ // In-flight change to connection status currently underway
+ return OFFLINE;
+ case NM_LEGACY_STATE_ASLEEP:
+ case NM_STATE_ASLEEP:
+ // Networking disabled or no devices on system
+ return OFFLINE;
+ default:
+ // Unknown status
+ return ONLINE;
+ }
+}
+
+bool NetworkManagerApi::IsCurrentlyOffline() {
+ base::AutoLock lock(online_state_lock_);
+ while (online_state_ == UNINITIALIZED) {
+ initial_state_cv_.Wait();
+ }
+ return online_state_ == OFFLINE;
+}
+
class DNSWatchDelegate : public FilePathWatcher::Delegate {
public:
explicit DNSWatchDelegate(Callback0::Type* callback)
@@ -61,6 +271,12 @@ class NetworkChangeNotifierLinux::Thread
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 +291,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 +316,19 @@ 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(method_factory_(this)) {
+ ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)),
+ network_manager_api_(
+ base::Bind(&NetworkChangeNotifierLinux::Thread
+ ::NotifyObserversOfOnlineStateChange)) {
}
NetworkChangeNotifierLinux::Thread::~Thread() {}
@@ -127,6 +353,8 @@ void NetworkChangeNotifierLinux::Thread::Init() {
return;
}
ListenForNotifications();
+
+ network_manager_api_.Init();
}
void NetworkChangeNotifierLinux::Thread::CleanUp() {
@@ -140,6 +368,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) {
@@ -218,8 +448,7 @@ NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() {
}
bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const {
- // TODO(eroman): http://crbug.com/53473
- return false;
+ return notifier_thread_->IsCurrentlyOffline();
}
} // namespace net
« no previous file with comments | « no previous file | net/net.gyp » ('j') | net/net.gyp » ('J')

Powered by Google App Engine
This is Rietveld 408576698