Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 1 | // |
| 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 | |
| 28 | using android::String16; |
| 29 | using android::String8; |
| 30 | using android::binder::Status; |
| 31 | using android::brillo::IUpdateEngineStatusCallback; |
| 32 | using android::brillo::ParcelableUpdateEngineStatus; |
| 33 | using android::sp; |
| 34 | using brillo::ErrorPtr; |
| 35 | using std::string; |
| 36 | |
| 37 | namespace chromeos_update_engine { |
| 38 | |
| 39 | namespace { |
| 40 | string NormalString(const String16& in) { |
| 41 | return string{String8{in}.string()}; |
| 42 | } |
| 43 | |
| 44 | Status ToStatus(ErrorPtr* error) { |
| 45 | return Status::fromServiceSpecificError( |
| 46 | 1, String8{error->get()->GetMessage().c_str()}); |
| 47 | } |
| 48 | } // namespace |
| 49 | |
| 50 | template <typename... Parameters, typename... Arguments> |
| 51 | Status 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 | |
| 60 | Status 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 | |
| 68 | Status BinderUpdateEngineBrilloService::AttemptRollback(bool powerwash) { |
| 69 | return CallCommonHandler(&UpdateEngineService::AttemptRollback, powerwash); |
| 70 | } |
| 71 | |
| 72 | Status BinderUpdateEngineBrilloService::CanRollback(bool* out_can_rollback) { |
| 73 | return CallCommonHandler(&UpdateEngineService::CanRollback, out_can_rollback); |
| 74 | } |
| 75 | |
| 76 | Status BinderUpdateEngineBrilloService::ResetStatus() { |
| 77 | return CallCommonHandler(&UpdateEngineService::ResetStatus); |
| 78 | } |
| 79 | |
| 80 | Status 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 | ¤t_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 | |
| 100 | Status BinderUpdateEngineBrilloService::RebootIfNeeded() { |
| 101 | return CallCommonHandler(&UpdateEngineService::RebootIfNeeded); |
| 102 | } |
| 103 | |
| 104 | Status BinderUpdateEngineBrilloService::SetChannel( |
| 105 | const String16& target_channel, bool powerwash) { |
| 106 | return CallCommonHandler(&UpdateEngineService::SetChannel, |
| 107 | NormalString(target_channel), |
| 108 | powerwash); |
| 109 | } |
| 110 | |
| 111 | Status 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 | |
Alex Deymo | 5b5fa8b | 2016-10-06 15:40:49 -0700 | [diff] [blame] | 121 | Status BinderUpdateEngineBrilloService::SetCohortHint( |
| 122 | const String16& in_cohort_hint) { |
| 123 | return CallCommonHandler(&UpdateEngineService::SetCohortHint, |
| 124 | NormalString(in_cohort_hint)); |
| 125 | } |
| 126 | |
| 127 | Status BinderUpdateEngineBrilloService::GetCohortHint( |
| 128 | String16* out_cohort_hint) { |
| 129 | string cohort_hint; |
| 130 | auto ret = |
| 131 | CallCommonHandler(&UpdateEngineService::GetCohortHint, &cohort_hint); |
| 132 | |
| 133 | *out_cohort_hint = String16(cohort_hint.c_str()); |
| 134 | return ret; |
| 135 | } |
| 136 | |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 137 | Status BinderUpdateEngineBrilloService::SetP2PUpdatePermission(bool enabled) { |
| 138 | return CallCommonHandler(&UpdateEngineService::SetP2PUpdatePermission, |
| 139 | enabled); |
| 140 | } |
| 141 | |
| 142 | Status BinderUpdateEngineBrilloService::GetP2PUpdatePermission( |
| 143 | bool* out_p2p_permission) { |
| 144 | return CallCommonHandler(&UpdateEngineService::GetP2PUpdatePermission, |
| 145 | out_p2p_permission); |
| 146 | } |
| 147 | |
| 148 | Status BinderUpdateEngineBrilloService::SetUpdateOverCellularPermission( |
| 149 | bool enabled) { |
| 150 | return CallCommonHandler( |
| 151 | &UpdateEngineService::SetUpdateOverCellularPermission, enabled); |
| 152 | } |
| 153 | |
Weidong Guo | 4b0d603 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 154 | Status BinderUpdateEngineBrilloService::SetUpdateOverCellularTarget( |
| 155 | const String16& target_version, |
| 156 | int64_t target_size) { |
| 157 | return CallCommonHandler( |
| 158 | &UpdateEngineService::SetUpdateOverCellularTarget, |
| 159 | NormalString(target_version), target_size); |
| 160 | } |
| 161 | |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 162 | Status BinderUpdateEngineBrilloService::GetUpdateOverCellularPermission( |
| 163 | bool* out_cellular_permission) { |
| 164 | return CallCommonHandler( |
| 165 | &UpdateEngineService::GetUpdateOverCellularPermission, |
| 166 | out_cellular_permission); |
| 167 | } |
| 168 | |
| 169 | Status BinderUpdateEngineBrilloService::GetDurationSinceUpdate( |
| 170 | int64_t* out_duration) { |
| 171 | return CallCommonHandler(&UpdateEngineService::GetDurationSinceUpdate, |
| 172 | out_duration); |
| 173 | } |
| 174 | |
| 175 | Status BinderUpdateEngineBrilloService::GetPrevVersion( |
| 176 | String16* out_prev_version) { |
| 177 | string version_string; |
| 178 | auto ret = |
| 179 | CallCommonHandler(&UpdateEngineService::GetPrevVersion, &version_string); |
| 180 | |
| 181 | *out_prev_version = String16(version_string.c_str()); |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | Status BinderUpdateEngineBrilloService::GetRollbackPartition( |
| 186 | String16* out_rollback_partition) { |
| 187 | string partition_string; |
| 188 | auto ret = CallCommonHandler(&UpdateEngineService::GetRollbackPartition, |
| 189 | &partition_string); |
| 190 | |
| 191 | if (ret.isOk()) { |
| 192 | *out_rollback_partition = String16(partition_string.c_str()); |
| 193 | } |
| 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | Status BinderUpdateEngineBrilloService::RegisterStatusCallback( |
| 199 | const sp<IUpdateEngineStatusCallback>& callback) { |
| 200 | callbacks_.emplace_back(callback); |
| 201 | |
| 202 | auto binder_wrapper = android::BinderWrapper::Get(); |
| 203 | |
| 204 | binder_wrapper->RegisterForDeathNotifications( |
| 205 | IUpdateEngineStatusCallback::asBinder(callback), |
| 206 | base::Bind(&BinderUpdateEngineBrilloService::UnregisterStatusCallback, |
| 207 | base::Unretained(this), |
| 208 | base::Unretained(callback.get()))); |
| 209 | |
| 210 | return Status::ok(); |
| 211 | } |
| 212 | |
Shuqian Zhao | 2997173 | 2016-02-05 11:29:32 -0800 | [diff] [blame] | 213 | Status BinderUpdateEngineBrilloService::GetLastAttemptError( |
| 214 | int* out_last_attempt_error) { |
| 215 | return CallCommonHandler(&UpdateEngineService::GetLastAttemptError, |
| 216 | out_last_attempt_error); |
| 217 | } |
| 218 | |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 219 | Status BinderUpdateEngineBrilloService::GetEolStatus(int* out_eol_status) { |
| 220 | return CallCommonHandler(&UpdateEngineService::GetEolStatus, out_eol_status); |
| 221 | } |
| 222 | |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 223 | void BinderUpdateEngineBrilloService::UnregisterStatusCallback( |
| 224 | IUpdateEngineStatusCallback* callback) { |
| 225 | auto it = callbacks_.begin(); |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 226 | while (it != callbacks_.end() && it->get() != callback) |
| 227 | it++; |
Alex Deymo | fa78f14 | 2016-01-26 21:36:16 -0800 | [diff] [blame] | 228 | |
| 229 | if (it == callbacks_.end()) { |
| 230 | LOG(ERROR) << "Got death notification for unknown callback."; |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | LOG(INFO) << "Erasing orphan callback"; |
| 235 | callbacks_.erase(it); |
| 236 | } |
| 237 | |
| 238 | void BinderUpdateEngineBrilloService::SendStatusUpdate( |
| 239 | int64_t last_checked_time, |
| 240 | double progress, |
| 241 | update_engine::UpdateStatus status, |
| 242 | const string& new_version, |
| 243 | int64_t new_size) { |
| 244 | const string str_status = UpdateStatusToString(status); |
| 245 | for (auto& callback : callbacks_) { |
| 246 | callback->HandleStatusUpdate(last_checked_time, |
| 247 | progress, |
| 248 | String16{str_status.c_str()}, |
| 249 | String16{new_version.c_str()}, |
| 250 | new_size); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | } // namespace chromeos_update_engine |