1 // Copyright 2019 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <brillo/dbus/introspectable_helper.h> 6 7 #include <memory> 8 9 #include <base/bind.h> 10 #include <dbus/dbus-shared.h> 11 12 namespace brillo { 13 namespace dbus_utils { 14 15 using base::Bind; 16 using std::string; 17 using std::unique_ptr; 18 AddInterfaceXml(string xml)19void IntrospectableInterfaceHelper::AddInterfaceXml(string xml) { 20 interface_xmls.push_back(xml); 21 } 22 RegisterWithDBusObject(DBusObject * object)23void IntrospectableInterfaceHelper::RegisterWithDBusObject(DBusObject* object) { 24 DBusInterface* itf = object->AddOrGetInterface(DBUS_INTERFACE_INTROSPECTABLE); 25 26 itf->AddMethodHandler("Introspect", GetHandler()); 27 } 28 29 IntrospectableInterfaceHelper::IntrospectCallback GetHandler()30IntrospectableInterfaceHelper::GetHandler() { 31 return Bind( 32 [](const string& xml, StringResponse response) { response->Return(xml); }, 33 GetXmlString()); 34 } 35 GetXmlString()36string IntrospectableInterfaceHelper::GetXmlString() { 37 constexpr const char header[] = 38 "<!DOCTYPE node PUBLIC " 39 "\"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n" 40 "\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n" 41 "\n" 42 "<node>\n" 43 " <interface name=\"org.freedesktop.DBus.Introspectable\">\n" 44 " <method name=\"Introspect\">\n" 45 " <arg name=\"data\" direction=\"out\" type=\"s\"/>\n" 46 " </method>\n" 47 " </interface>\n" 48 " <interface name=\"org.freedesktop.DBus.Properties\">\n" 49 " <method name=\"Get\">\n" 50 " <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n" 51 " <arg name=\"propname\" direction=\"in\" type=\"s\"/>\n" 52 " <arg name=\"value\" direction=\"out\" type=\"v\"/>\n" 53 " </method>\n" 54 " <method name=\"Set\">\n" 55 " <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n" 56 " <arg name=\"propname\" direction=\"in\" type=\"s\"/>\n" 57 " <arg name=\"value\" direction=\"in\" type=\"v\"/>\n" 58 " </method>\n" 59 " <method name=\"GetAll\">\n" 60 " <arg name=\"interface\" direction=\"in\" type=\"s\"/>\n" 61 " <arg name=\"props\" direction=\"out\" type=\"a{sv}\"/>\n" 62 " </method>\n" 63 " </interface>\n"; 64 constexpr const char footer[] = "</node>\n"; 65 66 size_t result_len = strlen(header) + strlen(footer); 67 for (const string& xml : interface_xmls) { 68 result_len += xml.size(); 69 } 70 71 string result = header; 72 result.reserve(result_len + 1); // +1 for null terminator 73 for (const string& xml : interface_xmls) { 74 result.append(xml); 75 } 76 result.append(footer); 77 return result; 78 } 79 80 } // namespace dbus_utils 81 } // namespace brillo 82