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