diff options
author | Sonny Sasaka <sonnysasaka@chromium.org> | 2018-02-27 00:22:54 -0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-03-02 13:25:17 -0800 |
commit | 037e08dae3b6c1b288ac35b35245c7d8bed84a58 (patch) | |
tree | 46e4776e36f8c1661668366375a6919f9ce3e6a8 /brillo | |
parent | f122936f24dd1f536ac1ec33415d2e929d2ff4aa (diff) | |
download | platform_external_libbrillo-037e08dae3b6c1b288ac35b35245c7d8bed84a58.tar.gz platform_external_libbrillo-037e08dae3b6c1b288ac35b35245c7d8bed84a58.tar.bz2 platform_external_libbrillo-037e08dae3b6c1b288ac35b35245c7d8bed84a58.zip |
libbrillo: Support custom D-Bus property handlers
This CL is to allow clients of DBusObject to implement their own custom
property method handlers (GetAll/Get/Set). Possible use cases include:
* handling Set method call to be forwarded to another D-Bus service
* handling Set method call to trigger some operation other than setting
the property value itself.
* handling Get method call to return a value which is a function of
other properties' values.
BUG=chromium:812468
TEST=Tested with Bluetooth daemon that forwards Set method call.
Change-Id: I8294dc46885b2105adceb9c144f4d603185e49ff
Reviewed-on: https://chromium-review.googlesource.com/939263
Commit-Ready: Sonny Sasaka <sonnysasaka@chromium.org>
Tested-by: Sonny Sasaka <sonnysasaka@chromium.org>
Reviewed-by: Ben Chan <benchan@chromium.org>
Diffstat (limited to 'brillo')
-rw-r--r-- | brillo/dbus/dbus_object.cc | 46 | ||||
-rw-r--r-- | brillo/dbus/dbus_object.h | 16 |
2 files changed, 49 insertions, 13 deletions
diff --git a/brillo/dbus/dbus_object.cc b/brillo/dbus/dbus_object.cc index 5172dc6..15bcddd 100644 --- a/brillo/dbus/dbus_object.cc +++ b/brillo/dbus/dbus_object.cc @@ -16,6 +16,23 @@ namespace brillo { namespace dbus_utils { +namespace { + +void SetupDefaultPropertyHandlers(DBusInterface* prop_interface, + ExportedPropertySet* property_set) { + prop_interface->AddSimpleMethodHandler(dbus::kPropertiesGetAll, + base::Unretained(property_set), + &ExportedPropertySet::HandleGetAll); + prop_interface->AddSimpleMethodHandlerWithError( + dbus::kPropertiesGet, base::Unretained(property_set), + &ExportedPropertySet::HandleGet); + prop_interface->AddSimpleMethodHandlerWithError( + dbus::kPropertiesSet, base::Unretained(property_set), + &ExportedPropertySet::HandleSet); +} + +} // namespace + ////////////////////////////////////////////////////////////////////////////// DBusInterface::DBusInterface(DBusObject* dbus_object, @@ -152,7 +169,21 @@ void DBusInterface::AddSignalImpl( DBusObject::DBusObject(ExportedObjectManager* object_manager, const scoped_refptr<dbus::Bus>& bus, const dbus::ObjectPath& object_path) - : property_set_(bus.get()), bus_(bus), object_path_(object_path) { + : DBusObject::DBusObject(object_manager, + bus, + object_path, + base::Bind(&SetupDefaultPropertyHandlers)) {} + +DBusObject::DBusObject( + ExportedObjectManager* object_manager, + const scoped_refptr<dbus::Bus>& bus, + const dbus::ObjectPath& object_path, + PropertyHandlerSetupCallback property_handler_setup_callback) + : property_set_(bus.get()), + bus_(bus), + object_path_(object_path), + property_handler_setup_callback_( + std::move(property_handler_setup_callback)) { if (object_manager) object_manager_ = object_manager->AsWeakPtr(); } @@ -249,18 +280,7 @@ bool DBusObject::SendSignal(dbus::Signal* signal) { void DBusObject::RegisterPropertiesInterface() { DBusInterface* prop_interface = AddOrGetInterface(dbus::kPropertiesInterface); - prop_interface->AddSimpleMethodHandler( - dbus::kPropertiesGetAll, - base::Unretained(&property_set_), - &ExportedPropertySet::HandleGetAll); - prop_interface->AddSimpleMethodHandlerWithError( - dbus::kPropertiesGet, - base::Unretained(&property_set_), - &ExportedPropertySet::HandleGet); - prop_interface->AddSimpleMethodHandlerWithError( - dbus::kPropertiesSet, - base::Unretained(&property_set_), - &ExportedPropertySet::HandleSet); + property_handler_setup_callback_.Run(prop_interface, &property_set_); property_set_.OnPropertiesInterfaceExported(prop_interface); } diff --git a/brillo/dbus/dbus_object.h b/brillo/dbus/dbus_object.h index e7c59e3..48f09ff 100644 --- a/brillo/dbus/dbus_object.h +++ b/brillo/dbus/dbus_object.h @@ -507,6 +507,9 @@ class BRILLO_EXPORT DBusInterface final { // by this object. class BRILLO_EXPORT DBusObject { public: + using PropertyHandlerSetupCallback = base::Callback<void( + DBusInterface* prop_interface, ExportedPropertySet* property_set)>; + // object_manager - ExportedObjectManager instance that notifies D-Bus // listeners of a new interface being claimed and property // changes on those interfaces. @@ -514,6 +517,17 @@ class BRILLO_EXPORT DBusObject { DBusObject(ExportedObjectManager* object_manager, const scoped_refptr<dbus::Bus>& bus, const dbus::ObjectPath& object_path); + + // property_handler_setup_callback - To be called when setting up property + // method handlers. Clients can register + // their own custom property method handlers + // (GetAll/Get/Set) by passing in this + // callback. + DBusObject(ExportedObjectManager* object_manager, + const scoped_refptr<dbus::Bus>& bus, + const dbus::ObjectPath& object_path, + PropertyHandlerSetupCallback property_handler_setup_callback); + virtual ~DBusObject(); // Returns an proxy handler for the interface |interface_name|. If the @@ -571,6 +585,8 @@ class BRILLO_EXPORT DBusObject { dbus::ObjectPath object_path_; // D-Bus object instance once this object is successfully exported. dbus::ExportedObject* exported_object_ = nullptr; // weak; owned by |bus_|. + // Sets up property method handlers. + PropertyHandlerSetupCallback property_handler_setup_callback_; friend class DBusInterface; DISALLOW_COPY_AND_ASSIGN(DBusObject); |