xref: /aosp_15_r20/external/libbrillo/brillo/dbus/introspectable_helper.h (revision 1a96fba65179ea7d3f56207137718607415c5953)
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 #ifndef LIBBRILLO_BRILLO_DBUS_INTROSPECTABLE_HELPER_H_
6 #define LIBBRILLO_BRILLO_DBUS_INTROSPECTABLE_HELPER_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include <brillo/brillo_export.h>
13 #include <brillo/dbus/dbus_method_response.h>
14 #include <brillo/dbus/dbus_object.h>
15 
16 namespace brillo {
17 namespace dbus_utils {
18 
19 // Note that brillo/dbus/dbus_object.h include files that include this file, so
20 // we'll need this forward declaration.
21 // class DBusObject;
22 
23 // This is a helper class that is used for creating the DBus Introspectable
24 // Interface. Each of the interfaces that is exported under a DBus Object will
25 // add its dbus interface introspection XML to this class, and then the user of
26 // this class will call RegisterWithDBusObject on the DBus object. Then this
27 // class can be freed. Note that this class is usually used in conjunction with
28 // the chromeos-dbus-bindings tool. Simply pass the string returned by
29 // GetIntrospectionXML() of the generated adaptor. Usage example:
30 // {
31 //   IntrospectableInterfaceHelper helper;
32 //   helper.AddInterfaceXML("<interface...> ...</interface>");
33 //   helper.AddInterfaceXML("<interface...> ...</interface>");
34 //   helper.AddInterfaceXML(XXXAdaptor::GetIntrospect());
35 //   helper.RegisterWithDBusObject(object);
36 // }
37 class BRILLO_EXPORT IntrospectableInterfaceHelper {
38  public:
39   IntrospectableInterfaceHelper() = default;
40 
41   // Add the Introspection XML for an interface to this class. The |xml| string
42   // should contain an interface XML tag and its content.
43   void AddInterfaceXml(std::string xml);
44 
45   // Register the Introspectable Interface with a DBus object. Note that this
46   // class can be freed after registering with DBus object.
47   void RegisterWithDBusObject(DBusObject* object);
48 
49  private:
50   // Internal alias for convenience.
51   using StringResponse = std::unique_ptr<DBusMethodResponse<std::string>>;
52   using IntrospectCallback = base::Callback<void(StringResponse)>;
53 
54   // Create the method handler for Introspect method call.
55   IntrospectCallback GetHandler();
56 
57   // Get the complete introspection XML.
58   std::string GetXmlString();
59 
60   // Stores the list of introspection XMLs for each of the interfaces that was
61   // added to this class.
62   std::vector<std::string> interface_xmls;
63 };
64 
65 }  // namespace dbus_utils
66 }  // namespace brillo
67 
68 #endif  // LIBBRILLO_BRILLO_DBUS_INTROSPECTABLE_HELPER_H_
69