// 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 #include #include #include 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[] = "\n" "\n" "\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n"; constexpr const char footer[] = "\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