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