xref: /aosp_15_r20/external/libbrillo/brillo/dbus/dbus_method_response.cc (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 #include <brillo/dbus/dbus_method_response.h>
6*1a96fba6SXin Li 
7*1a96fba6SXin Li #include <brillo/dbus/utils.h>
8*1a96fba6SXin Li 
9*1a96fba6SXin Li namespace brillo {
10*1a96fba6SXin Li namespace dbus_utils {
11*1a96fba6SXin Li 
DBusMethodResponseBase(dbus::MethodCall * method_call,ResponseSender sender)12*1a96fba6SXin Li DBusMethodResponseBase::DBusMethodResponseBase(dbus::MethodCall* method_call,
13*1a96fba6SXin Li                                                ResponseSender sender)
14*1a96fba6SXin Li     : sender_(sender), method_call_(method_call) {
15*1a96fba6SXin Li }
16*1a96fba6SXin Li 
~DBusMethodResponseBase()17*1a96fba6SXin Li DBusMethodResponseBase::~DBusMethodResponseBase() {
18*1a96fba6SXin Li   if (method_call_) {
19*1a96fba6SXin Li     // Response hasn't been sent by the handler. Abort the call.
20*1a96fba6SXin Li     Abort();
21*1a96fba6SXin Li   }
22*1a96fba6SXin Li }
23*1a96fba6SXin Li 
ReplyWithError(const brillo::Error * error)24*1a96fba6SXin Li void DBusMethodResponseBase::ReplyWithError(const brillo::Error* error) {
25*1a96fba6SXin Li   CheckCanSendResponse();
26*1a96fba6SXin Li   auto response = GetDBusError(method_call_, error);
27*1a96fba6SXin Li   SendRawResponse(std::move(response));
28*1a96fba6SXin Li }
29*1a96fba6SXin Li 
ReplyWithError(const base::Location & location,const std::string & error_domain,const std::string & error_code,const std::string & error_message)30*1a96fba6SXin Li void DBusMethodResponseBase::ReplyWithError(
31*1a96fba6SXin Li     const base::Location& location,
32*1a96fba6SXin Li     const std::string& error_domain,
33*1a96fba6SXin Li     const std::string& error_code,
34*1a96fba6SXin Li     const std::string& error_message) {
35*1a96fba6SXin Li   ErrorPtr error;
36*1a96fba6SXin Li   Error::AddTo(&error, location, error_domain, error_code, error_message);
37*1a96fba6SXin Li   ReplyWithError(error.get());
38*1a96fba6SXin Li }
39*1a96fba6SXin Li 
Abort()40*1a96fba6SXin Li void DBusMethodResponseBase::Abort() {
41*1a96fba6SXin Li   SendRawResponse(std::unique_ptr<dbus::Response>());
42*1a96fba6SXin Li }
43*1a96fba6SXin Li 
SendRawResponse(std::unique_ptr<dbus::Response> response)44*1a96fba6SXin Li void DBusMethodResponseBase::SendRawResponse(
45*1a96fba6SXin Li     std::unique_ptr<dbus::Response> response) {
46*1a96fba6SXin Li   CheckCanSendResponse();
47*1a96fba6SXin Li   method_call_ = nullptr;  // Mark response as sent.
48*1a96fba6SXin Li   sender_.Run(std::move(response));
49*1a96fba6SXin Li }
50*1a96fba6SXin Li 
51*1a96fba6SXin Li std::unique_ptr<dbus::Response>
CreateCustomResponse() const52*1a96fba6SXin Li DBusMethodResponseBase::CreateCustomResponse() const {
53*1a96fba6SXin Li   return dbus::Response::FromMethodCall(method_call_);
54*1a96fba6SXin Li }
55*1a96fba6SXin Li 
IsResponseSent() const56*1a96fba6SXin Li bool DBusMethodResponseBase::IsResponseSent() const {
57*1a96fba6SXin Li   return (method_call_ == nullptr);
58*1a96fba6SXin Li }
59*1a96fba6SXin Li 
CheckCanSendResponse() const60*1a96fba6SXin Li void DBusMethodResponseBase::CheckCanSendResponse() const {
61*1a96fba6SXin Li   CHECK(method_call_) << "Response already sent";
62*1a96fba6SXin Li }
63*1a96fba6SXin Li 
64*1a96fba6SXin Li }  // namespace dbus_utils
65*1a96fba6SXin Li }  // namespace brillo
66