aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn L Chen <zuan@chromium.org>2019-09-09 19:45:47 +0800
committerchrome-bot <chrome-bot@chromium.org>2019-10-16 00:02:49 -0700
commit119b83c4bff5e1ae2f7ed60e1ef810b52ea9fd51 (patch)
tree70d923a7cbfc0a492ff7480205482482f46aba8c
parent4c0df2f307eb2f9b2c91766ca87849537e56252c (diff)
downloadplatform_external_libbrillo-119b83c4bff5e1ae2f7ed60e1ef810b52ea9fd51.tar.gz
platform_external_libbrillo-119b83c4bff5e1ae2f7ed60e1ef810b52ea9fd51.tar.bz2
platform_external_libbrillo-119b83c4bff5e1ae2f7ed60e1ef810b52ea9fd51.zip
libbrillo: Add dbus_utils::IntrospectableInterfaceHelper
dbus_utils::IntrospectableInterfaceHelper is a helper class that is designed to work with chromeos-dbus-bindings tool to create Introspectable DBus interface, a DBus standard interface. BUG=none TEST=manually with the following CL && FEATURES=test emerge-$BOARD libbrillo Change-Id: I261e86de5b6a9c60c1f31c50dab68b142dd37aa2 Reviewed-on: https://chromium-review.googlesource.com/1797974 Tested-by: John L Chen <zuan@chromium.org> Commit-Ready: John L Chen <zuan@chromium.org> Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org> Reviewed-by: Eric Caruso <ejcaruso@chromium.org> Cr-Mirrored-From: https://chromium.googlesource.com/chromiumos/platform2 Cr-Mirrored-Commit: ca8cf7eaff618d97069beed3bfc1815eca94c8fd
-rw-r--r--BUILD.gn1
-rw-r--r--brillo/dbus/introspectable_helper.cc81
-rw-r--r--brillo/dbus/introspectable_helper.h68
3 files changed, 150 insertions, 0 deletions
diff --git a/BUILD.gn b/BUILD.gn
index dca735b..14922d5 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -100,6 +100,7 @@ libbrillo_sublibs = [
"brillo/dbus/dbus_signal.cc",
"brillo/dbus/exported_object_manager.cc",
"brillo/dbus/exported_property_set.cc",
+ "brillo/dbus/introspectable_helper.cc",
"brillo/dbus/utils.cc",
]
}
diff --git a/brillo/dbus/introspectable_helper.cc b/brillo/dbus/introspectable_helper.cc
new file mode 100644
index 0000000..68ec78c
--- /dev/null
+++ b/brillo/dbus/introspectable_helper.cc
@@ -0,0 +1,81 @@
+// Copyright 2019 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.
+
+#include <brillo/dbus/introspectable_helper.h>
+
+#include <memory>
+
+#include <base/bind.h>
+#include <dbus/dbus-shared.h>
+
+namespace brillo {
+namespace dbus_utils {
+
+using base::Bind;
+using std::string;
+using std::unique_ptr;
+
+void IntrospectableInterfaceHelper::AddInterfaceXml(string xml) {
+ interface_xmls.push_back(xml);
+}
+
+void IntrospectableInterfaceHelper::RegisterWithDBusObject(DBusObject* object) {
+ DBusInterface* itf = object->AddOrGetInterface(DBUS_INTERFACE_INTROSPECTABLE);
+
+ itf->AddMethodHandler("Introspect", GetHandler());
+}
+
+IntrospectableInterfaceHelper::IntrospectCallback
+IntrospectableInterfaceHelper::GetHandler() {
+ return Bind(
+ [](const string& xml, StringResponse response) { response->Return(xml); },
+ GetXmlString());
+}
+
+string IntrospectableInterfaceHelper::GetXmlString() {
+ constexpr const char header[] =
+ "<!DOCTYPE node PUBLIC "
+ "\"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
+ "\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
+ "\n"
+ "<node>\n"
+ " <interface name=\"org.freedesktop.DBus.Introspectable\">\n"
+ " <method name=\"Introspect\">\n"
+ " <arg name=\"data\" direction=\"out\" type=\"s\"/>\n"
+ " </method>\n"
+ " </interface>\n"
+ " <interface name=\"org.freedesktop.DBus.Properties\">\n"
+ " <method name=\"Get\">\n"
+ " <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"propname\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"value\" direction=\"out\" type=\"v\"/>\n"
+ " </method>\n"
+ " <method name=\"Set\">\n"
+ " <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"propname\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"value\" direction=\"in\" type=\"v\"/>\n"
+ " </method>\n"
+ " <method name=\"GetAll\">\n"
+ " <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n"
+ " <arg name=\"props\" direction=\"out\" type=\"a{sv}\"/>\n"
+ " </method>\n"
+ " </interface>\n";
+ constexpr const char footer[] = "</node>\n";
+
+ size_t result_len = strlen(header) + strlen(footer);
+ for (const string& xml : interface_xmls) {
+ result_len += xml.size();
+ }
+
+ string result = header;
+ result.reserve(result_len + 1); // +1 for null terminator
+ for (const string& xml : interface_xmls) {
+ result.append(xml);
+ }
+ result.append(footer);
+ return result;
+}
+
+} // namespace dbus_utils
+} // namespace brillo
diff --git a/brillo/dbus/introspectable_helper.h b/brillo/dbus/introspectable_helper.h
new file mode 100644
index 0000000..e1a398f
--- /dev/null
+++ b/brillo/dbus/introspectable_helper.h
@@ -0,0 +1,68 @@
+// Copyright 2019 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 LIBBRILLO_BRILLO_DBUS_INTROSPECTABLE_HELPER_H_
+#define LIBBRILLO_BRILLO_DBUS_INTROSPECTABLE_HELPER_H_
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <brillo/brillo_export.h>
+#include <brillo/dbus/dbus_method_response.h>
+#include <brillo/dbus/dbus_object.h>
+
+namespace brillo {
+namespace dbus_utils {
+
+// Note that brillo/dbus/dbus_object.h include files that include this file, so
+// we'll need this forward declaration.
+// class DBusObject;
+
+// This is a helper class that is used for creating the DBus Introspectable
+// Interface. Each of the interfaces that is exported under a DBus Object will
+// add its dbus interface introspection XML to this class, and then the user of
+// this class will call RegisterWithDBusObject on the DBus object. Then this
+// class can be freed. Note that this class is usually used in conjunction with
+// the chromeos-dbus-bindings tool. Simply pass the string returned by
+// GetIntrospectionXML() of the generated adaptor. Usage example:
+// {
+// IntrospectableInterfaceHelper helper;
+// helper.AddInterfaceXML("<interface...> ...</interface>");
+// helper.AddInterfaceXML("<interface...> ...</interface>");
+// helper.AddInterfaceXML(XXXAdaptor::GetIntrospect());
+// helper.RegisterWithDBusObject(object);
+// }
+class BRILLO_EXPORT IntrospectableInterfaceHelper {
+ public:
+ IntrospectableInterfaceHelper() = default;
+
+ // Add the Introspection XML for an interface to this class. The |xml| string
+ // should contain an interface XML tag and its content.
+ void AddInterfaceXml(std::string xml);
+
+ // Register the Introspectable Interface with a DBus object. Note that this
+ // class can be freed after registering with DBus object.
+ void RegisterWithDBusObject(DBusObject* object);
+
+ private:
+ // Internal alias for convenience.
+ using StringResponse = std::unique_ptr<DBusMethodResponse<std::string>>;
+ using IntrospectCallback = base::Callback<void(StringResponse)>;
+
+ // Create the method handler for Introspect method call.
+ IntrospectCallback GetHandler();
+
+ // Get the complete introspection XML.
+ std::string GetXmlString();
+
+ // Stores the list of introspection XMLs for each of the interfaces that was
+ // added to this class.
+ std::vector<std::string> interface_xmls;
+};
+
+} // namespace dbus_utils
+} // namespace brillo
+
+#endif // LIBBRILLO_BRILLO_DBUS_INTROSPECTABLE_HELPER_H_