blob: 6a6a16e1ff86def33ead1991f81f480ae60afe54 [file] [log] [blame]
Alex Deymofa78f142016-01-26 21:36:16 -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_brillo.h"
18
19#include <base/bind.h>
20
21#include <binderwrapper/binder_wrapper.h>
22
23#include <utils/String16.h>
24#include <utils/StrongPointer.h>
25
26#include "update_engine/update_status_utils.h"
27
28using android::String16;
29using android::String8;
30using android::binder::Status;
31using android::brillo::IUpdateEngineStatusCallback;
32using android::brillo::ParcelableUpdateEngineStatus;
33using android::sp;
34using brillo::ErrorPtr;
35using std::string;
36
37namespace chromeos_update_engine {
38
39namespace {
40string NormalString(const String16& in) {
41 return string{String8{in}.string()};
42}
43
44Status ToStatus(ErrorPtr* error) {
45 return Status::fromServiceSpecificError(
46 1, String8{error->get()->GetMessage().c_str()});
47}
48} // namespace
49
50template <typename... Parameters, typename... Arguments>
51Status BinderUpdateEngineBrilloService::CallCommonHandler(
52 bool (UpdateEngineService::*Handler)(ErrorPtr*, Parameters...),
53 Arguments... arguments) {
54 ErrorPtr error;
55 if (((common_.get())->*Handler)(&error, arguments...))
56 return Status::ok();
57 return ToStatus(&error);
58}
59
60Status BinderUpdateEngineBrilloService::AttemptUpdate(
61 const String16& app_version, const String16& omaha_url, int flags) {
62 return CallCommonHandler(&UpdateEngineService::AttemptUpdate,
63 NormalString(app_version),
64 NormalString(omaha_url),
65 flags);
66}
67
68Status BinderUpdateEngineBrilloService::AttemptRollback(bool powerwash) {
69 return CallCommonHandler(&UpdateEngineService::AttemptRollback, powerwash);
70}
71
72Status BinderUpdateEngineBrilloService::CanRollback(bool* out_can_rollback) {
73 return CallCommonHandler(&UpdateEngineService::CanRollback, out_can_rollback);
74}
75
76Status BinderUpdateEngineBrilloService::ResetStatus() {
77 return CallCommonHandler(&UpdateEngineService::ResetStatus);
78}
79
80Status BinderUpdateEngineBrilloService::GetStatus(
81 ParcelableUpdateEngineStatus* status) {
82 string current_op;
83 string new_version;
84
85 auto ret = CallCommonHandler(&UpdateEngineService::GetStatus,
86 &status->last_checked_time_,
87 &status->progress_,
88 &current_op,
89 &new_version,
90 &status->new_size_);
91
92 if (ret.isOk()) {
93 status->current_operation_ = String16{current_op.c_str()};
94 status->new_version_ = String16{new_version.c_str()};
95 }
96
97 return ret;
98}
99
100Status BinderUpdateEngineBrilloService::RebootIfNeeded() {
101 return CallCommonHandler(&UpdateEngineService::RebootIfNeeded);
102}
103
104Status BinderUpdateEngineBrilloService::SetChannel(
105 const String16& target_channel, bool powerwash) {
106 return CallCommonHandler(&UpdateEngineService::SetChannel,
107 NormalString(target_channel),
108 powerwash);
109}
110
111Status BinderUpdateEngineBrilloService::GetChannel(bool get_current_channel,
112 String16* out_channel) {
113 string channel_string;
114 auto ret = CallCommonHandler(
115 &UpdateEngineService::GetChannel, get_current_channel, &channel_string);
116
117 *out_channel = String16(channel_string.c_str());
118 return ret;
119}
120
121Status BinderUpdateEngineBrilloService::SetP2PUpdatePermission(bool enabled) {
122 return CallCommonHandler(&UpdateEngineService::SetP2PUpdatePermission,
123 enabled);
124}
125
126Status BinderUpdateEngineBrilloService::GetP2PUpdatePermission(
127 bool* out_p2p_permission) {
128 return CallCommonHandler(&UpdateEngineService::GetP2PUpdatePermission,
129 out_p2p_permission);
130}
131
132Status BinderUpdateEngineBrilloService::SetUpdateOverCellularPermission(
133 bool enabled) {
134 return CallCommonHandler(
135 &UpdateEngineService::SetUpdateOverCellularPermission, enabled);
136}
137
138Status BinderUpdateEngineBrilloService::GetUpdateOverCellularPermission(
139 bool* out_cellular_permission) {
140 return CallCommonHandler(
141 &UpdateEngineService::GetUpdateOverCellularPermission,
142 out_cellular_permission);
143}
144
145Status BinderUpdateEngineBrilloService::GetDurationSinceUpdate(
146 int64_t* out_duration) {
147 return CallCommonHandler(&UpdateEngineService::GetDurationSinceUpdate,
148 out_duration);
149}
150
151Status BinderUpdateEngineBrilloService::GetPrevVersion(
152 String16* out_prev_version) {
153 string version_string;
154 auto ret =
155 CallCommonHandler(&UpdateEngineService::GetPrevVersion, &version_string);
156
157 *out_prev_version = String16(version_string.c_str());
158 return ret;
159}
160
161Status BinderUpdateEngineBrilloService::GetRollbackPartition(
162 String16* out_rollback_partition) {
163 string partition_string;
164 auto ret = CallCommonHandler(&UpdateEngineService::GetRollbackPartition,
165 &partition_string);
166
167 if (ret.isOk()) {
168 *out_rollback_partition = String16(partition_string.c_str());
169 }
170
171 return ret;
172}
173
174Status BinderUpdateEngineBrilloService::RegisterStatusCallback(
175 const sp<IUpdateEngineStatusCallback>& callback) {
176 callbacks_.emplace_back(callback);
177
178 auto binder_wrapper = android::BinderWrapper::Get();
179
180 binder_wrapper->RegisterForDeathNotifications(
181 IUpdateEngineStatusCallback::asBinder(callback),
182 base::Bind(&BinderUpdateEngineBrilloService::UnregisterStatusCallback,
183 base::Unretained(this),
184 base::Unretained(callback.get())));
185
186 return Status::ok();
187}
188
Shuqian Zhao29971732016-02-05 11:29:32 -0800189Status BinderUpdateEngineBrilloService::GetLastAttemptError(
190 int* out_last_attempt_error) {
191 return CallCommonHandler(&UpdateEngineService::GetLastAttemptError,
192 out_last_attempt_error);
193}
194
Alex Deymofa78f142016-01-26 21:36:16 -0800195void BinderUpdateEngineBrilloService::UnregisterStatusCallback(
196 IUpdateEngineStatusCallback* callback) {
197 auto it = callbacks_.begin();
198
199 for (; it != callbacks_.end() && it->get() != callback; it++)
200 ;
201
202 if (it == callbacks_.end()) {
203 LOG(ERROR) << "Got death notification for unknown callback.";
204 return;
205 }
206
207 LOG(INFO) << "Erasing orphan callback";
208 callbacks_.erase(it);
209}
210
211void BinderUpdateEngineBrilloService::SendStatusUpdate(
212 int64_t last_checked_time,
213 double progress,
214 update_engine::UpdateStatus status,
215 const string& new_version,
216 int64_t new_size) {
217 const string str_status = UpdateStatusToString(status);
218 for (auto& callback : callbacks_) {
219 callback->HandleStatusUpdate(last_checked_time,
220 progress,
221 String16{str_status.c_str()},
222 String16{new_version.c_str()},
223 new_size);
224 }
225}
226
227} // namespace chromeos_update_engine