Index: chromeos/dbus/dbus.cc |
diff --git a/chromeos/dbus/dbus.cc b/chromeos/dbus/dbus.cc |
index 33ef997185c69748e5c88a6f9749bb5150a50cd6..f06293812b1fb3b864f20ad3acc751000e55a923 100644 |
--- a/chromeos/dbus/dbus.cc |
+++ b/chromeos/dbus/dbus.cc |
@@ -4,10 +4,10 @@ |
#include "chromeos/dbus/dbus.h" |
-#include <dbus/dbus.h> |
-#include <dbus/dbus-glib-bindings.h> |
-#include <dbus/dbus-glib-lowlevel.h> |
+#include <base/stringprintf.h> |
Daniel Erat
2011/02/10 23:07:24
nit: alphabetize these
(technically, i think that
glotov
2011/02/11 16:36:52
Done.
|
#include <base/logging.h> |
+#include <dbus/dbus-glib-bindings.h> |
+#include <dbus/dbus.h> |
namespace chromeos { |
namespace dbus { |
@@ -256,5 +256,68 @@ void SendSignalWithNoArgumentsToSystemBus(const char* path, |
::dbus_message_unref(signal); |
} |
+void MonitorSignal::StartMonitoring(const char* interface, |
+ const char* signal) { |
+ DCHECK(!interface_) << "StartMonitoring() must be called only once"; |
+ interface_ = interface; |
+ signal_ = signal; |
+ |
+ // Snoop on D-Bus messages so we can get notified about brightness changes. |
Daniel Erat
2011/02/10 23:07:24
remove the part about brightness here
glotov
2011/02/11 16:36:52
Done.
|
+ DBusConnection* dbus_conn = dbus_g_connection_get_connection( |
+ GetSystemBusConnection().g_connection()); |
+ DCHECK(dbus_conn); |
+ |
+ DBusError error; |
+ dbus_error_init(&error); |
+ dbus_bus_add_match(dbus_conn, GetDBusMatchString().c_str(), &error); |
+ if (dbus_error_is_set(&error)) { |
+ LOG(DFATAL) << "Got error while adding D-Bus match rule: " << error.name |
+ << " (" << error.message << ")"; |
+ } |
+ |
+ if (!dbus_connection_add_filter(dbus_conn, |
+ &MonitorSignal::FilterDBusMessage, |
+ this, // user_data |
+ NULL)) { // free_data_function |
+ LOG(DFATAL) << "Unable to add D-Bus filter"; |
+ } |
+} |
+ |
+MonitorSignal::~MonitorSignal() { |
+ DBusConnection* dbus_conn = dbus_g_connection_get_connection( |
+ dbus::GetSystemBusConnection().g_connection()); |
+ DCHECK(dbus_conn); |
+ |
+ dbus_connection_remove_filter(dbus_conn, |
+ &MonitorSignal::FilterDBusMessage, |
+ this); |
+ |
+ DBusError error; |
+ dbus_error_init(&error); |
+ dbus_bus_remove_match(dbus_conn, GetDBusMatchString().c_str(), &error); |
Daniel Erat
2011/02/10 23:07:24
i think that you'll segfault right now if someone
glotov
2011/02/11 16:36:52
Done.
|
+ if (dbus_error_is_set(&error)) { |
+ LOG(DFATAL) << "Got error while removing D-Bus match rule: " << error.name |
+ << " (" << error.message << ")"; |
+ } |
+} |
+ |
+std::string MonitorSignal::GetDBusMatchString() const { |
+ return StringPrintf("type='signal', interface='%s', member='%s'", |
+ interface_, signal_); |
+} |
+ |
+/* static */ |
+DBusHandlerResult MonitorSignal::FilterDBusMessage(DBusConnection* dbus_conn, |
+ DBusMessage* message, |
+ void* data) { |
+ MonitorSignal* self = static_cast<MonitorSignal*>(data); |
+ if (dbus_message_is_signal(message, self->interface_, self->signal_)) { |
+ self->OnSignal(message); |
+ return DBUS_HANDLER_RESULT_HANDLED; |
+ } else { |
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
+ } |
+} |
+ |
} // namespace dbus |
} // namespace chromeos |