blob: 66cce6edd35adaed39966186ed476ffa6c8cbe07 [file] [log] [blame]
Peter Qiu0e92d1e2015-11-20 10:11:36 -08001//
2// Copyright (C) 2014 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#include "apmanager/dbus/service_dbus_adaptor.h"
18
Peter Qiu2ea547f2015-11-20 14:18:11 -080019#include <base/bind.h>
Peter Qiu0e92d1e2015-11-20 10:11:36 -080020#include <base/strings/stringprintf.h>
21#include <dbus_bindings/org.chromium.apmanager.Manager.h>
22
23#include "apmanager/service.h"
24
25using brillo::dbus_utils::ExportedObjectManager;
26using brillo::dbus_utils::DBusMethodResponse;
27using brillo::dbus_utils::DBusObject;
28using org::chromium::apmanager::ManagerAdaptor;
29using std::string;
30
31namespace apmanager {
32
33ServiceDBusAdaptor::ServiceDBusAdaptor(
34 const scoped_refptr<dbus::Bus>& bus,
35 ExportedObjectManager* object_manager,
36 Service* service)
37 : adaptor_(this),
38 object_path_(
39 base::StringPrintf("%s/services/%d",
40 ManagerAdaptor::GetObjectPath().value().c_str(),
41 service->identifier())),
42 dbus_object_(object_manager, bus, object_path_),
43 service_(service) {
Peter Qiu2ea547f2015-11-20 14:18:11 -080044 // Need to initialize Config property with a valid path before registering
45 // to the bus.
46 SetConfig(service_->config());
Peter Qiu0e92d1e2015-11-20 10:11:36 -080047 // Register D-Bus object.
48 adaptor_.RegisterWithDBusObject(&dbus_object_);
49 dbus_object_.RegisterAndBlock();
50}
51
52ServiceDBusAdaptor::~ServiceDBusAdaptor() {}
53
54void ServiceDBusAdaptor::Start(
55 std::unique_ptr<DBusMethodResponse<>> response) {
Peter Qiu2ea547f2015-11-20 14:18:11 -080056 service_->Start(
57 base::Bind(&ServiceDBusAdaptor::OnStartCompleted,
58 base::Unretained(this),
59 base::Passed(&response)));
Peter Qiu0e92d1e2015-11-20 10:11:36 -080060}
61
Peter Qiu2ea547f2015-11-20 14:18:11 -080062bool ServiceDBusAdaptor::Stop(brillo::ErrorPtr* dbus_error) {
63 Error error;
64 service_->Stop(&error);
65 return !error.ToDBusError(dbus_error);
Peter Qiu0e92d1e2015-11-20 10:11:36 -080066}
67
68RPCObjectIdentifier ServiceDBusAdaptor::GetRpcObjectIdentifier() {
69 return object_path_;
70}
71
72void ServiceDBusAdaptor::SetConfig(Config* config) {
73 adaptor_.SetConfig(config->adaptor()->GetRpcObjectIdentifier());
74}
75
76void ServiceDBusAdaptor::SetState(const string& state) {
77 adaptor_.SetState(state);
78}
79
Peter Qiu2ea547f2015-11-20 14:18:11 -080080void ServiceDBusAdaptor::OnStartCompleted(
81 std::unique_ptr<DBusMethodResponse<>> response, const Error& error) {
82 brillo::ErrorPtr dbus_error;
83 if (error.ToDBusError(&dbus_error)) {
84 response->ReplyWithError(dbus_error.get());
85 } else {
86 response->Return();
87 }
88}
89
Peter Qiu0e92d1e2015-11-20 10:11:36 -080090} // namespace apmanager