1 //
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
19 #include <base/bind.h>
20 #include <base/strings/stringprintf.h>
21 #include <dbus_bindings/org.chromium.apmanager.Manager.h>
22
23 #include "apmanager/service.h"
24
25 using brillo::dbus_utils::ExportedObjectManager;
26 using brillo::dbus_utils::DBusMethodResponse;
27 using brillo::dbus_utils::DBusObject;
28 using org::chromium::apmanager::ManagerAdaptor;
29 using std::string;
30
31 namespace apmanager {
32
ServiceDBusAdaptor(const scoped_refptr<dbus::Bus> & bus,ExportedObjectManager * object_manager,Service * service)33 ServiceDBusAdaptor::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) {
44 // Need to initialize Config property with a valid path before registering
45 // to the bus.
46 SetConfig(service_->config());
47 // Register D-Bus object.
48 adaptor_.RegisterWithDBusObject(&dbus_object_);
49 dbus_object_.RegisterAndBlock();
50 }
51
~ServiceDBusAdaptor()52 ServiceDBusAdaptor::~ServiceDBusAdaptor() {}
53
Start(std::unique_ptr<DBusMethodResponse<>> response)54 void ServiceDBusAdaptor::Start(
55 std::unique_ptr<DBusMethodResponse<>> response) {
56 service_->Start(
57 base::Bind(&ServiceDBusAdaptor::OnStartCompleted,
58 base::Unretained(this),
59 base::Passed(&response)));
60 }
61
Stop(brillo::ErrorPtr * dbus_error)62 bool ServiceDBusAdaptor::Stop(brillo::ErrorPtr* dbus_error) {
63 Error error;
64 service_->Stop(&error);
65 return !error.ToDBusError(dbus_error);
66 }
67
GetRpcObjectIdentifier()68 RPCObjectIdentifier ServiceDBusAdaptor::GetRpcObjectIdentifier() {
69 return object_path_;
70 }
71
SetConfig(Config * config)72 void ServiceDBusAdaptor::SetConfig(Config* config) {
73 adaptor_.SetConfig(config->adaptor()->GetRpcObjectIdentifier());
74 }
75
SetState(const string & state)76 void ServiceDBusAdaptor::SetState(const string& state) {
77 adaptor_.SetState(state);
78 }
79
OnStartCompleted(std::unique_ptr<DBusMethodResponse<>> response,const Error & error)80 void 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
90 } // namespace apmanager
91