summaryrefslogtreecommitdiff
path: root/chromeos/dbus/dbus_method_response.cc
blob: 25d0b2710b44933d44235ffa74e40bf66f6db4b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2014 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 <chromeos/dbus/dbus_method_response.h>

#include <chromeos/dbus/utils.h>

namespace chromeos {
namespace dbus_utils {

DBusMethodResponseBase::DBusMethodResponseBase(dbus::MethodCall* method_call,
                                               ResponseSender sender)
    : sender_(sender), method_call_(method_call) {
}

DBusMethodResponseBase::~DBusMethodResponseBase() {
  if (method_call_) {
    // Response hasn't been sent by the handler. Abort the call.
    Abort();
  }
}

void DBusMethodResponseBase::ReplyWithError(const chromeos::Error* error) {
  CheckCanSendResponse();
  auto response = GetDBusError(method_call_, error);
  SendRawResponse(std::move(response));
}

void DBusMethodResponseBase::ReplyWithError(
    const tracked_objects::Location& location,
    const std::string& error_domain,
    const std::string& error_code,
    const std::string& error_message) {
  ErrorPtr error;
  Error::AddTo(&error, location, error_domain, error_code, error_message);
  ReplyWithError(error.get());
}

void DBusMethodResponseBase::Abort() {
  SendRawResponse(std::unique_ptr<dbus::Response>());
}

void DBusMethodResponseBase::SendRawResponse(
    std::unique_ptr<dbus::Response> response) {
  CheckCanSendResponse();
  method_call_ = nullptr;  // Mark response as sent.
  sender_.Run(scoped_ptr<dbus::Response>{response.release()});
}

std::unique_ptr<dbus::Response>
DBusMethodResponseBase::CreateCustomResponse() const {
  return std::unique_ptr<dbus::Response>{
      dbus::Response::FromMethodCall(method_call_).release()};
}

bool DBusMethodResponseBase::IsResponseSent() const {
  return (method_call_ == nullptr);
}

void DBusMethodResponseBase::CheckCanSendResponse() const {
  CHECK(method_call_) << "Response already sent";
}

}  // namespace dbus_utils
}  // namespace chromeos