Peter Qiu | 0e92d1e | 2015-11-20 10:11:36 -0800 | [diff] [blame] | 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 | |
Peter Qiu | 2ea547f | 2015-11-20 14:18:11 -0800 | [diff] [blame] | 19 | #include <base/bind.h> |
Peter Qiu | 0e92d1e | 2015-11-20 10:11:36 -0800 | [diff] [blame] | 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 | |
| 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) { |
Peter Qiu | 2ea547f | 2015-11-20 14:18:11 -0800 | [diff] [blame] | 44 | // Need to initialize Config property with a valid path before registering |
| 45 | // to the bus. |
| 46 | SetConfig(service_->config()); |
Peter Qiu | 0e92d1e | 2015-11-20 10:11:36 -0800 | [diff] [blame] | 47 | // Register D-Bus object. |
| 48 | adaptor_.RegisterWithDBusObject(&dbus_object_); |
| 49 | dbus_object_.RegisterAndBlock(); |
| 50 | } |
| 51 | |
| 52 | ServiceDBusAdaptor::~ServiceDBusAdaptor() {} |
| 53 | |
| 54 | void ServiceDBusAdaptor::Start( |
| 55 | std::unique_ptr<DBusMethodResponse<>> response) { |
Peter Qiu | 2ea547f | 2015-11-20 14:18:11 -0800 | [diff] [blame] | 56 | service_->Start( |
| 57 | base::Bind(&ServiceDBusAdaptor::OnStartCompleted, |
| 58 | base::Unretained(this), |
| 59 | base::Passed(&response))); |
Peter Qiu | 0e92d1e | 2015-11-20 10:11:36 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Peter Qiu | 2ea547f | 2015-11-20 14:18:11 -0800 | [diff] [blame] | 62 | bool ServiceDBusAdaptor::Stop(brillo::ErrorPtr* dbus_error) { |
| 63 | Error error; |
| 64 | service_->Stop(&error); |
| 65 | return !error.ToDBusError(dbus_error); |
Peter Qiu | 0e92d1e | 2015-11-20 10:11:36 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | RPCObjectIdentifier ServiceDBusAdaptor::GetRpcObjectIdentifier() { |
| 69 | return object_path_; |
| 70 | } |
| 71 | |
| 72 | void ServiceDBusAdaptor::SetConfig(Config* config) { |
| 73 | adaptor_.SetConfig(config->adaptor()->GetRpcObjectIdentifier()); |
| 74 | } |
| 75 | |
| 76 | void ServiceDBusAdaptor::SetState(const string& state) { |
| 77 | adaptor_.SetState(state); |
| 78 | } |
| 79 | |
Peter Qiu | 2ea547f | 2015-11-20 14:18:11 -0800 | [diff] [blame] | 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 | |
Peter Qiu | 0e92d1e | 2015-11-20 10:11:36 -0800 | [diff] [blame] | 90 | } // namespace apmanager |