Christopher Wiley | 9e1eda9 | 2015-11-16 15:23:37 -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.h" |
| 18 | |
Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 19 | #include <base/bind.h> |
| 20 | |
| 21 | #include <binderwrapper/binder_wrapper.h> |
| 22 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 23 | #include <utils/String16.h> |
| 24 | #include <utils/StrongPointer.h> |
| 25 | |
Christopher Wiley | 9e1eda9 | 2015-11-16 15:23:37 -0800 | [diff] [blame] | 26 | using android::String16; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 27 | using android::String8; |
Christopher Wiley | 09af881 | 2015-11-19 08:12:33 -0800 | [diff] [blame] | 28 | using android::binder::Status; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 29 | using android::brillo::IUpdateEngineStatusCallback; |
| 30 | using android::brillo::ParcelableUpdateEngineStatus; |
| 31 | using android::sp; |
| 32 | using brillo::ErrorPtr; |
| 33 | using std::string; |
Christopher Wiley | 9e1eda9 | 2015-11-16 15:23:37 -0800 | [diff] [blame] | 34 | |
| 35 | namespace chromeos_update_engine { |
| 36 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 37 | namespace { |
| 38 | string NormalString(const String16& in) { |
| 39 | return string{String8{in}.string()}; |
Tao Bao | e78e3fb | 2016-01-04 17:57:53 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 42 | Status ToStatus(ErrorPtr* error) { |
| 43 | return Status::fromServiceSpecificError( |
| 44 | 1, String8{error->get()->GetMessage().c_str()}); |
| 45 | } |
| 46 | } // namespace |
| 47 | |
| 48 | template<typename... Parameters, typename... Arguments> |
| 49 | Status 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 Wiley | 9e1eda9 | 2015-11-16 15:23:37 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 57 | Status 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 Wiley | 9e1eda9 | 2015-11-16 15:23:37 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 65 | Status BinderUpdateEngineService::AttemptRollback(bool powerwash) { |
| 66 | return CallCommonHandler(&UpdateEngineService::AttemptRollback, powerwash); |
Christopher Wiley | 9e1eda9 | 2015-11-16 15:23:37 -0800 | [diff] [blame] | 67 | } |
| 68 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 69 | Status BinderUpdateEngineService::CanRollback(bool* out_can_rollback) { |
| 70 | return CallCommonHandler(&UpdateEngineService::CanRollback, |
| 71 | out_can_rollback); |
| 72 | } |
| 73 | |
| 74 | Status BinderUpdateEngineService::ResetStatus() { |
| 75 | return CallCommonHandler(&UpdateEngineService::ResetStatus); |
| 76 | } |
| 77 | |
| 78 | Status 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 | ¤t_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 | |
| 98 | Status BinderUpdateEngineService::RebootIfNeeded() { |
| 99 | return CallCommonHandler(&UpdateEngineService::RebootIfNeeded); |
| 100 | } |
| 101 | |
| 102 | Status BinderUpdateEngineService::SetChannel(const String16& target_channel, |
| 103 | bool powerwash) { |
| 104 | return CallCommonHandler(&UpdateEngineService::SetChannel, |
| 105 | NormalString(target_channel), powerwash); |
| 106 | } |
| 107 | |
| 108 | Status 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 | |
| 119 | Status BinderUpdateEngineService::SetP2PUpdatePermission(bool enabled) { |
| 120 | return CallCommonHandler(&UpdateEngineService::SetP2PUpdatePermission, |
| 121 | enabled); |
| 122 | } |
| 123 | |
| 124 | Status BinderUpdateEngineService::GetP2PUpdatePermission( |
| 125 | bool* out_p2p_permission) { |
| 126 | return CallCommonHandler(&UpdateEngineService::GetP2PUpdatePermission, |
| 127 | out_p2p_permission); |
| 128 | } |
| 129 | |
| 130 | Status BinderUpdateEngineService::SetUpdateOverCellularPermission( |
| 131 | bool enabled) { |
| 132 | return CallCommonHandler( |
| 133 | &UpdateEngineService::SetUpdateOverCellularPermission, enabled); |
| 134 | } |
| 135 | |
| 136 | Status BinderUpdateEngineService::GetUpdateOverCellularPermission( |
| 137 | bool* out_cellular_permission) { |
| 138 | return CallCommonHandler( |
| 139 | &UpdateEngineService::GetUpdateOverCellularPermission, |
| 140 | out_cellular_permission); |
| 141 | } |
| 142 | |
| 143 | Status BinderUpdateEngineService::GetDurationSinceUpdate( |
| 144 | int64_t* out_duration) { |
| 145 | return CallCommonHandler(&UpdateEngineService::GetDurationSinceUpdate, |
| 146 | out_duration); |
| 147 | } |
| 148 | |
| 149 | Status 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 | |
| 158 | Status 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 | |
| 171 | Status BinderUpdateEngineService::RegisterStatusCallback( |
| 172 | const sp<IUpdateEngineStatusCallback>& callback) { |
Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 173 | 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 Wiley | 09af881 | 2015-11-19 08:12:33 -0800 | [diff] [blame] | 182 | return Status::ok(); |
Christopher Wiley | 9e1eda9 | 2015-11-16 15:23:37 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 185 | void 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 | |
| 201 | void 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 Wiley | 9e1eda9 | 2015-11-16 15:23:37 -0800 | [diff] [blame] | 212 | } // namespace chromeos_update_engine |