blob: 099abc15f90169fb852a7dfbcceb52f869488712 [file] [log] [blame]
Christopher Wileya4915c42014-03-27 14:45:37 -07001// Copyright 2014 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
Christopher Wileya4915c42014-03-27 14:45:37 -07005#include "buffet/dbus_utils.h"
6
Christopher Wiley94d65972014-08-07 15:47:24 -07007#include <string>
8
Christopher Wiley90016242014-04-01 17:33:29 -07009#include <base/bind.h>
Alex Vakulenko5841c302014-07-23 10:49:49 -070010#include <base/logging.h>
Christopher Wiley90016242014-04-01 17:33:29 -070011
Christopher Wileya4915c42014-03-27 14:45:37 -070012namespace buffet {
13
14namespace dbus_utils {
15
Christopher Wiley90016242014-04-01 17:33:29 -070016namespace {
17
18// Passes |method_call| to |handler| and passes the response to
19// |response_sender|. If |handler| returns NULL, an empty response is created
20// and sent.
21void HandleSynchronousDBusMethodCall(
22 base::Callback<scoped_ptr<dbus::Response>(dbus::MethodCall*)> handler,
23 dbus::MethodCall* method_call,
24 dbus::ExportedObject::ResponseSender response_sender) {
25 auto response = handler.Run(method_call);
26 if (!response)
27 response = dbus::Response::FromMethodCall(method_call);
28
29 response_sender.Run(response.Pass());
30}
31
32} // namespace
33
Alex Vakulenkob3aac252014-05-07 17:35:24 -070034scoped_ptr<dbus::Response> GetDBusError(dbus::MethodCall* method_call,
Alex Vakulenkoaf23b322014-05-08 16:25:45 -070035 const Error* error) {
Alex Vakulenkob3aac252014-05-07 17:35:24 -070036 std::string message;
37 while (error) {
38 // Format error string as "domain/code:message".
39 if (!message.empty())
40 message += ';';
41 message += error->GetDomain() + '/' + error->GetCode() + ':' +
42 error->GetMessage();
43 error = error->GetInnerError();
44 }
45 scoped_ptr<dbus::ErrorResponse> resp(dbus::ErrorResponse::FromMethodCall(
46 method_call, "org.freedesktop.DBus.Error.Failed", message));
47 return scoped_ptr<dbus::Response>(resp.release());
48}
49
Christopher Wiley90016242014-04-01 17:33:29 -070050dbus::ExportedObject::MethodCallCallback GetExportableDBusMethod(
51 base::Callback<scoped_ptr<dbus::Response>(dbus::MethodCall*)> handler) {
52 return base::Bind(&HandleSynchronousDBusMethodCall, handler);
53}
54
Christopher Wileya4915c42014-03-27 14:45:37 -070055} // namespace dbus_utils
56
57} // namespace buffet