xref: /aosp_15_r20/external/libbrillo/brillo/dbus/utils.h (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright 2014 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_UTILS_H_
6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_DBUS_UTILS_H_
7*1a96fba6SXin Li 
8*1a96fba6SXin Li #include <memory>
9*1a96fba6SXin Li #include <string>
10*1a96fba6SXin Li 
11*1a96fba6SXin Li #include <brillo/brillo_export.h>
12*1a96fba6SXin Li #include <brillo/errors/error.h>
13*1a96fba6SXin Li #include <dbus/exported_object.h>
14*1a96fba6SXin Li #include <dbus/message.h>
15*1a96fba6SXin Li #include <dbus/scoped_dbus_error.h>
16*1a96fba6SXin Li 
17*1a96fba6SXin Li namespace brillo {
18*1a96fba6SXin Li namespace dbus_utils {
19*1a96fba6SXin Li 
20*1a96fba6SXin Li // A helper function to create a D-Bus error response object as unique_ptr<>.
21*1a96fba6SXin Li BRILLO_EXPORT std::unique_ptr<::dbus::Response> CreateDBusErrorResponse(
22*1a96fba6SXin Li     ::dbus::MethodCall* method_call,
23*1a96fba6SXin Li     const std::string& error_name,
24*1a96fba6SXin Li     const std::string& error_message);
25*1a96fba6SXin Li 
26*1a96fba6SXin Li // Create a D-Bus error response object from brillo::Error. If the last
27*1a96fba6SXin Li // error in the error chain belongs to "dbus" error domain, its error code
28*1a96fba6SXin Li // and message are directly translated to D-Bus error code and message.
29*1a96fba6SXin Li // Any inner errors are formatted as "domain/code:message" string and appended
30*1a96fba6SXin Li // to the D-Bus error message, delimited by semi-colons.
31*1a96fba6SXin Li BRILLO_EXPORT std::unique_ptr<::dbus::Response> GetDBusError(
32*1a96fba6SXin Li     ::dbus::MethodCall* method_call, const brillo::Error* error);
33*1a96fba6SXin Li 
34*1a96fba6SXin Li // AddDBusError() is the opposite of GetDBusError(). It de-serializes the Error
35*1a96fba6SXin Li // object received over D-Bus.
36*1a96fba6SXin Li BRILLO_EXPORT void AddDBusError(brillo::ErrorPtr* error,
37*1a96fba6SXin Li                                 const std::string& dbus_error_name,
38*1a96fba6SXin Li                                 const std::string& dbus_error_message);
39*1a96fba6SXin Li 
40*1a96fba6SXin Li }  // namespace dbus_utils
41*1a96fba6SXin Li }  // namespace brillo
42*1a96fba6SXin Li 
43*1a96fba6SXin Li #endif  // LIBBRILLO_BRILLO_DBUS_UTILS_H_
44