blob: 07ff22decc3a498a981ba2e6a82850c27e89edfa [file] [log] [blame]
Christopher Wiley9e1eda92015-11-16 15:23:37 -08001//
2// Copyright (C) 2015 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 "update_engine/binder_service.h"
18
Casey Dahlin40892492016-01-25 16:55:28 -080019#include <base/bind.h>
20
21#include <binderwrapper/binder_wrapper.h>
22
Casey Dahlina93cd532016-01-14 16:55:11 -080023#include <utils/String16.h>
24#include <utils/StrongPointer.h>
25
Christopher Wiley9e1eda92015-11-16 15:23:37 -080026using android::String16;
Casey Dahlina93cd532016-01-14 16:55:11 -080027using android::String8;
Christopher Wiley09af8812015-11-19 08:12:33 -080028using android::binder::Status;
Casey Dahlina93cd532016-01-14 16:55:11 -080029using android::brillo::IUpdateEngineStatusCallback;
30using android::brillo::ParcelableUpdateEngineStatus;
31using android::sp;
32using brillo::ErrorPtr;
33using std::string;
Christopher Wiley9e1eda92015-11-16 15:23:37 -080034
35namespace chromeos_update_engine {
36
Casey Dahlina93cd532016-01-14 16:55:11 -080037namespace {
38string NormalString(const String16& in) {
39 return string{String8{in}.string()};
Tao Baoe78e3fb2016-01-04 17:57:53 -080040}
41
Casey Dahlina93cd532016-01-14 16:55:11 -080042Status ToStatus(ErrorPtr* error) {
43 return Status::fromServiceSpecificError(
44 1, String8{error->get()->GetMessage().c_str()});
45}
46} // namespace
47
48template<typename... Parameters, typename... Arguments>
49Status BinderUpdateEngineService::CallCommonHandler(
50 bool (UpdateEngineService::*Handler)(ErrorPtr*, Parameters...),
51 Arguments... arguments) {
52 ErrorPtr error;
53 if (((common_.get())->*Handler)(&error, arguments...)) return Status::ok();
54 return ToStatus(&error);
Christopher Wiley9e1eda92015-11-16 15:23:37 -080055}
56
Casey Dahlina93cd532016-01-14 16:55:11 -080057Status BinderUpdateEngineService::AttemptUpdate(const String16& app_version,
58 const String16& omaha_url,
59 int flags) {
60 return CallCommonHandler(
61 &UpdateEngineService::AttemptUpdate, NormalString(app_version),
62 NormalString(omaha_url), flags);
Christopher Wiley9e1eda92015-11-16 15:23:37 -080063}
64
Casey Dahlina93cd532016-01-14 16:55:11 -080065Status BinderUpdateEngineService::AttemptRollback(bool powerwash) {
66 return CallCommonHandler(&UpdateEngineService::AttemptRollback, powerwash);
Christopher Wiley9e1eda92015-11-16 15:23:37 -080067}
68
Casey Dahlina93cd532016-01-14 16:55:11 -080069Status BinderUpdateEngineService::CanRollback(bool* out_can_rollback) {
70 return CallCommonHandler(&UpdateEngineService::CanRollback,
71 out_can_rollback);
72}
73
74Status BinderUpdateEngineService::ResetStatus() {
75 return CallCommonHandler(&UpdateEngineService::ResetStatus);
76}
77
78Status BinderUpdateEngineService::GetStatus(
79 ParcelableUpdateEngineStatus* status) {
80 string current_op;
81 string new_version;
82
83 auto ret = CallCommonHandler(&UpdateEngineService::GetStatus,
84 &status->last_checked_time_,
85 &status->progress_,
86 &current_op,
87 &new_version,
88 &status->new_size_);
89
90 if (ret.isOk()) {
91 status->current_operation_ = String16{current_op.c_str()};
92 status->new_version_ = String16{new_version.c_str()};
93 }
94
95 return ret;
96}
97
98Status BinderUpdateEngineService::RebootIfNeeded() {
99 return CallCommonHandler(&UpdateEngineService::RebootIfNeeded);
100}
101
102Status BinderUpdateEngineService::SetChannel(const String16& target_channel,
103 bool powerwash) {
104 return CallCommonHandler(&UpdateEngineService::SetChannel,
105 NormalString(target_channel), powerwash);
106}
107
108Status BinderUpdateEngineService::GetChannel(bool get_current_channel,
109 String16* out_channel) {
110 string channel_string;
111 auto ret = CallCommonHandler(&UpdateEngineService::GetChannel,
112 get_current_channel,
113 &channel_string);
114
115 *out_channel = String16(channel_string.c_str());
116 return ret;
117}
118
119Status BinderUpdateEngineService::SetP2PUpdatePermission(bool enabled) {
120 return CallCommonHandler(&UpdateEngineService::SetP2PUpdatePermission,
121 enabled);
122}
123
124Status BinderUpdateEngineService::GetP2PUpdatePermission(
125 bool* out_p2p_permission) {
126 return CallCommonHandler(&UpdateEngineService::GetP2PUpdatePermission,
127 out_p2p_permission);
128}
129
130Status BinderUpdateEngineService::SetUpdateOverCellularPermission(
131 bool enabled) {
132 return CallCommonHandler(
133 &UpdateEngineService::SetUpdateOverCellularPermission, enabled);
134}
135
136Status BinderUpdateEngineService::GetUpdateOverCellularPermission(
137 bool* out_cellular_permission) {
138 return CallCommonHandler(
139 &UpdateEngineService::GetUpdateOverCellularPermission,
140 out_cellular_permission);
141}
142
143Status BinderUpdateEngineService::GetDurationSinceUpdate(
144 int64_t* out_duration) {
145 return CallCommonHandler(&UpdateEngineService::GetDurationSinceUpdate,
146 out_duration);
147}
148
149Status BinderUpdateEngineService::GetPrevVersion(String16* out_prev_version) {
150 string version_string;
151 auto ret = CallCommonHandler(&UpdateEngineService::GetPrevVersion,
152 &version_string);
153
154 *out_prev_version = String16(version_string.c_str());
155 return ret;
156}
157
158Status BinderUpdateEngineService::GetRollbackPartition(
159 String16* out_rollback_partition) {
160 string partition_string;
161 auto ret = CallCommonHandler(&UpdateEngineService::GetRollbackPartition,
162 &partition_string);
163
164 if (ret.isOk()) {
165 *out_rollback_partition = String16(partition_string.c_str());
166 }
167
168 return ret;
169}
170
171Status BinderUpdateEngineService::RegisterStatusCallback(
172 const sp<IUpdateEngineStatusCallback>& callback) {
Casey Dahlin40892492016-01-25 16:55:28 -0800173 callbacks_.emplace_back(callback);
174
175 auto binder_wrapper = android::BinderWrapper::Get();
176
177 binder_wrapper->RegisterForDeathNotifications(
178 IUpdateEngineStatusCallback::asBinder(callback),
179 base::Bind(&BinderUpdateEngineService::UnregisterStatusCallback,
180 base::Unretained(this), base::Unretained(callback.get())));
181
Christopher Wiley09af8812015-11-19 08:12:33 -0800182 return Status::ok();
Christopher Wiley9e1eda92015-11-16 15:23:37 -0800183}
184
Casey Dahlin40892492016-01-25 16:55:28 -0800185void BinderUpdateEngineService::UnregisterStatusCallback(
186 IUpdateEngineStatusCallback* callback) {
187 auto it = callbacks_.begin();
188
189 for (; it != callbacks_.end() && it->get() != callback; it++)
190 ;
191
192 if (it == callbacks_.end()) {
193 LOG(ERROR) << "Got death notification for unknown callback.";
194 return;
195 }
196
197 LOG(INFO) << "Erasing orphan callback";
198 callbacks_.erase(it);
199}
200
201void BinderUpdateEngineService::SendStatusUpdate(
202 int64_t in_last_checked_time, double in_progress,
203 const std::string& in_current_operation, const std::string& in_new_version,
204 int64_t in_new_size) {
205 for (auto& callback : callbacks_) {
206 callback->HandleStatusUpdate(in_last_checked_time, in_progress,
207 String16{in_current_operation.c_str()},
208 String16{in_new_version.c_str()}, in_new_size);
209 }
210}
211
Christopher Wiley9e1eda92015-11-16 15:23:37 -0800212} // namespace chromeos_update_engine