aboutsummaryrefslogtreecommitdiffstats
path: root/brillo/dbus/dbus_signal_handler.h
diff options
context:
space:
mode:
authorAlex Vakulenko <avakulenko@google.com>2015-10-12 15:21:28 -0700
committerAlex Vakulenko <avakulenko@google.com>2015-10-13 16:10:03 -0700
commit9ed0cab99f18acb3570a35e9408f24355f6b8324 (patch)
tree60e3b4c2822b812b3218489a9a6d835df1e8ca6e /brillo/dbus/dbus_signal_handler.h
parenteabfe23a51c91a103042793ac2d5c28170994e1f (diff)
downloadplatform_external_libbrillo-9ed0cab99f18acb3570a35e9408f24355f6b8324.tar.gz
platform_external_libbrillo-9ed0cab99f18acb3570a35e9408f24355f6b8324.tar.bz2
platform_external_libbrillo-9ed0cab99f18acb3570a35e9408f24355f6b8324.zip
Move chromeos symbols into brillo namespace
And move the include files into "brillo" directory instead of "chromeos" BUG: 24872993 TEST=built aosp and brillo and unit tests pass on dragonoboard Change-Id: Ieb979d1ebd3152921d36cd15acbd6247f02aae69
Diffstat (limited to 'brillo/dbus/dbus_signal_handler.h')
-rw-r--r--brillo/dbus/dbus_signal_handler.h68
1 files changed, 68 insertions, 0 deletions
diff --git a/brillo/dbus/dbus_signal_handler.h b/brillo/dbus/dbus_signal_handler.h
new file mode 100644
index 0000000..dfa1e78
--- /dev/null
+++ b/brillo/dbus/dbus_signal_handler.h
@@ -0,0 +1,68 @@
+// Copyright 2014 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef LIBCHROMEOS_BRILLO_DBUS_DBUS_SIGNAL_HANDLER_H_
+#define LIBCHROMEOS_BRILLO_DBUS_DBUS_SIGNAL_HANDLER_H_
+
+#include <string>
+
+#include <brillo/bind_lambda.h>
+#include <brillo/dbus/dbus_param_reader.h>
+#include <dbus/message.h>
+#include <dbus/object_proxy.h>
+
+namespace brillo {
+namespace dbus_utils {
+
+// brillo::dbus_utils::ConnectToSignal() is a helper function similar to
+// dbus::ObjectProxy::ConnectToSignal() but the |signal_callback| is an actual
+// C++ signal handler with expected signal parameters as native method args.
+//
+// brillo::dbus_utils::ConnectToSignal() actually registers a stub signal
+// handler with D-Bus which has a standard signature that matches
+// dbus::ObjectProxy::SignalCallback.
+//
+// When a D-Bus signal is emitted, the stub handler is invoked, which unpacks
+// the expected parameters from dbus::Signal message and then calls
+// |signal_callback| with unpacked arguments.
+//
+// If the signal message doesn't contain correct number or types of arguments,
+// an error message is logged to the system log and the signal is ignored
+// (|signal_callback| is not invoked).
+template<typename... Args>
+void ConnectToSignal(
+ dbus::ObjectProxy* object_proxy,
+ const std::string& interface_name,
+ const std::string& signal_name,
+ base::Callback<void(Args...)> signal_callback,
+ dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
+ // DBusParamReader::Invoke() needs a functor object, not a base::Callback.
+ // Wrap the callback with lambda so we can redirect the call.
+ auto signal_callback_wrapper = [signal_callback](const Args&... args) {
+ if (!signal_callback.is_null()) {
+ signal_callback.Run(args...);
+ }
+ };
+
+ // Raw signal handler stub method. When called, unpacks the signal arguments
+ // from |signal| message buffer and redirects the call to
+ // |signal_callback_wrapper| which, in turn, would call the user-provided
+ // |signal_callback|.
+ auto dbus_signal_callback = [signal_callback_wrapper](dbus::Signal* signal) {
+ dbus::MessageReader reader(signal);
+ DBusParamReader<false, Args...>::Invoke(
+ signal_callback_wrapper, &reader, nullptr);
+ };
+
+ // Register our stub handler with D-Bus ObjectProxy.
+ object_proxy->ConnectToSignal(interface_name,
+ signal_name,
+ base::Bind(dbus_signal_callback),
+ on_connected_callback);
+}
+
+} // namespace dbus_utils
+} // namespace brillo
+
+#endif // LIBCHROMEOS_BRILLO_DBUS_DBUS_SIGNAL_HANDLER_H_