Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -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/client_library/client_dbus.h" |
| 18 | |
| 19 | #include <base/message_loop/message_loop.h> |
| 20 | |
Amin Hassani | c80d2d8 | 2019-06-21 17:43:32 -0700 | [diff] [blame] | 21 | #include <memory> |
| 22 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 23 | #include <dbus/bus.h> |
Amin Hassani | 9b66aa6 | 2019-03-14 14:13:30 -0700 | [diff] [blame] | 24 | #include <dlcservice/proto_bindings/dlcservice.pb.h> |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 25 | #include <update_engine/dbus-constants.h> |
| 26 | |
| 27 | #include "update_engine/update_status_utils.h" |
| 28 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 29 | using dbus::Bus; |
| 30 | using org::chromium::UpdateEngineInterfaceProxy; |
| 31 | using std::string; |
Amin Hassani | c80d2d8 | 2019-06-21 17:43:32 -0700 | [diff] [blame] | 32 | using std::unique_ptr; |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 33 | using std::vector; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 34 | |
| 35 | namespace update_engine { |
Amin Hassani | c80d2d8 | 2019-06-21 17:43:32 -0700 | [diff] [blame] | 36 | |
| 37 | unique_ptr<UpdateEngineClient> UpdateEngineClient::CreateInstance() { |
| 38 | auto ret = std::make_unique<internal::DBusUpdateEngineClient>(); |
| 39 | if (!ret->Init()) { |
| 40 | ret.reset(); |
| 41 | } |
| 42 | return ret; |
| 43 | } |
| 44 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 45 | namespace internal { |
| 46 | |
Amin Hassani | eb463ee | 2019-06-20 19:23:03 -0700 | [diff] [blame] | 47 | namespace { |
| 48 | // This converts the status from Protobuf |StatusResult| to The internal |
| 49 | // |UpdateEngineStatus| struct. |
Amin Hassani | a441743 | 2019-07-08 12:52:40 -0700 | [diff] [blame] | 50 | void ConvertToUpdateEngineStatus(const StatusResult& status, |
Amin Hassani | eb463ee | 2019-06-20 19:23:03 -0700 | [diff] [blame] | 51 | UpdateEngineStatus* out_status) { |
| 52 | out_status->last_checked_time = status.last_checked_time(); |
| 53 | out_status->progress = status.progress(); |
| 54 | out_status->new_version = status.new_version(); |
| 55 | out_status->new_size_bytes = status.new_size(); |
Amin Hassani | a441743 | 2019-07-08 12:52:40 -0700 | [diff] [blame] | 56 | out_status->status = static_cast<UpdateStatus>(status.current_operation()); |
Amin Hassani | 9be122e | 2019-08-29 09:20:12 -0700 | [diff] [blame] | 57 | out_status->is_enterprise_rollback = status.is_enterprise_rollback(); |
Jae Hoon Kim | 2f78c1c | 2019-07-25 13:20:43 -0700 | [diff] [blame] | 58 | out_status->is_install = status.is_install(); |
Jae Hoon Kim | 051627a | 2019-09-03 12:56:32 -0700 | [diff] [blame] | 59 | out_status->eol_date = status.eol_date(); |
Amin Hassani | eb463ee | 2019-06-20 19:23:03 -0700 | [diff] [blame] | 60 | } |
| 61 | } // namespace |
| 62 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 63 | bool DBusUpdateEngineClient::Init() { |
| 64 | Bus::Options options; |
| 65 | options.bus_type = Bus::SYSTEM; |
| 66 | scoped_refptr<Bus> bus{new Bus{options}}; |
| 67 | |
| 68 | if (!bus->Connect()) |
| 69 | return false; |
| 70 | |
| 71 | proxy_.reset(new UpdateEngineInterfaceProxy{bus}); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | bool DBusUpdateEngineClient::AttemptUpdate(const string& in_app_version, |
| 76 | const string& in_omaha_url, |
| 77 | bool at_user_request) { |
| 78 | return proxy_->AttemptUpdateWithFlags( |
| 79 | in_app_version, |
| 80 | in_omaha_url, |
Amin Hassani | 6bb001f | 2018-02-26 14:33:02 -0800 | [diff] [blame] | 81 | (at_user_request) |
| 82 | ? 0 |
| 83 | : update_engine::UpdateAttemptFlags::kFlagNonInteractive, |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 84 | nullptr); |
| 85 | } |
| 86 | |
Amin Hassani | 9b66aa6 | 2019-03-14 14:13:30 -0700 | [diff] [blame] | 87 | bool DBusUpdateEngineClient::AttemptInstall(const string& omaha_url, |
| 88 | const vector<string>& dlc_ids) { |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 89 | // Convert parameters into protobuf. |
Amin Hassani | 9b66aa6 | 2019-03-14 14:13:30 -0700 | [diff] [blame] | 90 | dlcservice::DlcModuleList dlc_parameters; |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 91 | dlc_parameters.set_omaha_url(omaha_url); |
Amin Hassani | 9b66aa6 | 2019-03-14 14:13:30 -0700 | [diff] [blame] | 92 | for (const auto& dlc_id : dlc_ids) { |
| 93 | dlcservice::DlcModuleInfo* dlc_module_info = |
| 94 | dlc_parameters.add_dlc_module_infos(); |
| 95 | dlc_module_info->set_dlc_id(dlc_id); |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 96 | } |
Amin Hassani | 3bab177 | 2019-06-21 14:58:25 -0700 | [diff] [blame] | 97 | return proxy_->AttemptInstall(dlc_parameters, |
| 98 | nullptr /* brillo::ErrorPtr* */); |
Xiaochu Liu | 88d9038 | 2018-08-29 16:09:11 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Andrew | a8d7df3 | 2020-03-15 20:10:01 -0700 | [diff] [blame] | 101 | bool DBusUpdateEngineClient::SetDlcActiveValue(bool is_active, |
| 102 | const std::string& dlc_id) { |
| 103 | return proxy_->SetDlcActiveValue(is_active, dlc_id, /*error=*/nullptr); |
| 104 | } |
| 105 | |
Amin Hassani | eb463ee | 2019-06-20 19:23:03 -0700 | [diff] [blame] | 106 | bool DBusUpdateEngineClient::GetStatus(UpdateEngineStatus* out_status) const { |
| 107 | StatusResult status; |
| 108 | if (!proxy_->GetStatusAdvanced(&status, nullptr)) { |
| 109 | return false; |
| 110 | } |
| 111 | |
Amin Hassani | a441743 | 2019-07-08 12:52:40 -0700 | [diff] [blame] | 112 | ConvertToUpdateEngineStatus(status, out_status); |
| 113 | return true; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Alex Deymo | 5b5fa8b | 2016-10-06 15:40:49 -0700 | [diff] [blame] | 116 | bool DBusUpdateEngineClient::SetCohortHint(const string& cohort_hint) { |
| 117 | return proxy_->SetCohortHint(cohort_hint, nullptr); |
| 118 | } |
| 119 | |
| 120 | bool DBusUpdateEngineClient::GetCohortHint(string* cohort_hint) const { |
| 121 | return proxy_->GetCohortHint(cohort_hint, nullptr); |
| 122 | } |
| 123 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 124 | bool DBusUpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) { |
| 125 | return proxy_->SetUpdateOverCellularPermission(allowed, nullptr); |
| 126 | } |
| 127 | |
| 128 | bool DBusUpdateEngineClient::GetUpdateOverCellularPermission( |
| 129 | bool* allowed) const { |
| 130 | return proxy_->GetUpdateOverCellularPermission(allowed, nullptr); |
| 131 | } |
| 132 | |
| 133 | bool DBusUpdateEngineClient::SetP2PUpdatePermission(bool enabled) { |
| 134 | return proxy_->SetP2PUpdatePermission(enabled, nullptr); |
| 135 | } |
| 136 | |
| 137 | bool DBusUpdateEngineClient::GetP2PUpdatePermission(bool* enabled) const { |
| 138 | return proxy_->GetP2PUpdatePermission(enabled, nullptr); |
| 139 | } |
| 140 | |
| 141 | bool DBusUpdateEngineClient::Rollback(bool powerwash) { |
| 142 | return proxy_->AttemptRollback(powerwash, nullptr); |
| 143 | } |
| 144 | |
| 145 | bool DBusUpdateEngineClient::GetRollbackPartition( |
| 146 | string* rollback_partition) const { |
| 147 | return proxy_->GetRollbackPartition(rollback_partition, nullptr); |
| 148 | } |
| 149 | |
| 150 | bool DBusUpdateEngineClient::GetPrevVersion(string* prev_version) const { |
| 151 | return proxy_->GetPrevVersion(prev_version, nullptr); |
| 152 | } |
| 153 | |
| 154 | void DBusUpdateEngineClient::RebootIfNeeded() { |
| 155 | bool ret = proxy_->RebootIfNeeded(nullptr); |
| 156 | if (!ret) { |
| 157 | // Reboot error code doesn't necessarily mean that a reboot |
| 158 | // failed. For example, D-Bus may be shutdown before we receive the |
| 159 | // result. |
| 160 | LOG(INFO) << "RebootIfNeeded() failure ignored."; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | bool DBusUpdateEngineClient::ResetStatus() { |
| 165 | return proxy_->ResetStatus(nullptr); |
| 166 | } |
| 167 | |
Alex Deymo | 492eaf5 | 2016-02-02 12:05:02 -0800 | [diff] [blame] | 168 | void DBusUpdateEngineClient::DBusStatusHandlersRegistered( |
Amin Hassani | aefaab2 | 2019-01-14 16:20:16 -0800 | [diff] [blame] | 169 | const string& interface, const string& signal_name, bool success) const { |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 170 | if (!success) { |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 171 | for (auto handler : handlers_) { |
Amin Hassani | aefaab2 | 2019-01-14 16:20:16 -0800 | [diff] [blame] | 172 | handler->IPCError("Could not connect to" + signal_name + " on " + |
| 173 | interface); |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 174 | } |
| 175 | } else { |
| 176 | StatusUpdateHandlersRegistered(nullptr); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 177 | } |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 178 | } |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 179 | |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 180 | void DBusUpdateEngineClient::StatusUpdateHandlersRegistered( |
| 181 | StatusUpdateHandler* handler) const { |
Amin Hassani | eb463ee | 2019-06-20 19:23:03 -0700 | [diff] [blame] | 182 | UpdateEngineStatus status; |
| 183 | if (!GetStatus(&status)) { |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 184 | handler->IPCError("Could not query current status"); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
Alex Deymo | 492eaf5 | 2016-02-02 12:05:02 -0800 | [diff] [blame] | 188 | std::vector<update_engine::StatusUpdateHandler*> just_handler = {handler}; |
| 189 | for (auto h : handler ? just_handler : handlers_) { |
Amin Hassani | eb463ee | 2019-06-20 19:23:03 -0700 | [diff] [blame] | 190 | h->HandleStatusUpdate(status); |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 191 | } |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 194 | void DBusUpdateEngineClient::RunStatusUpdateHandlers( |
Amin Hassani | eb463ee | 2019-06-20 19:23:03 -0700 | [diff] [blame] | 195 | const StatusResult& status) { |
| 196 | UpdateEngineStatus ue_status; |
| 197 | ConvertToUpdateEngineStatus(status, &ue_status); |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 198 | |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 199 | for (auto handler : handlers_) { |
Amin Hassani | eb463ee | 2019-06-20 19:23:03 -0700 | [diff] [blame] | 200 | handler->HandleStatusUpdate(ue_status); |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | bool DBusUpdateEngineClient::UnregisterStatusUpdateHandler( |
| 205 | StatusUpdateHandler* handler) { |
Alex Deymo | b3fa53b | 2016-04-18 19:57:58 -0700 | [diff] [blame] | 206 | auto it = std::find(handlers_.begin(), handlers_.end(), handler); |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 207 | if (it != handlers_.end()) { |
| 208 | handlers_.erase(it); |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | return false; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 215 | bool DBusUpdateEngineClient::RegisterStatusUpdateHandler( |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 216 | StatusUpdateHandler* handler) { |
hscham | 00b6aa2 | 2020-02-20 12:32:06 +0900 | [diff] [blame^] | 217 | if (!base::MessageLoopCurrent::IsSet()) { |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 218 | LOG(FATAL) << "Cannot get UpdateEngineClient outside of message loop."; |
Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 219 | return false; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 222 | handlers_.push_back(handler); |
| 223 | |
| 224 | if (dbus_handler_registered_) { |
| 225 | StatusUpdateHandlersRegistered(handler); |
| 226 | return true; |
| 227 | } |
| 228 | |
Amin Hassani | eb463ee | 2019-06-20 19:23:03 -0700 | [diff] [blame] | 229 | proxy_->RegisterStatusUpdateAdvancedSignalHandler( |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 230 | base::Bind(&DBusUpdateEngineClient::RunStatusUpdateHandlers, |
| 231 | base::Unretained(this)), |
Alex Deymo | 492eaf5 | 2016-02-02 12:05:02 -0800 | [diff] [blame] | 232 | base::Bind(&DBusUpdateEngineClient::DBusStatusHandlersRegistered, |
| 233 | base::Unretained(this))); |
Casey Dahlin | a715f7b | 2016-01-29 16:38:51 -0800 | [diff] [blame] | 234 | |
| 235 | dbus_handler_registered_ = true; |
Casey Dahlin | 4089249 | 2016-01-25 16:55:28 -0800 | [diff] [blame] | 236 | |
| 237 | return true; |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | bool DBusUpdateEngineClient::SetTargetChannel(const string& in_target_channel, |
| 241 | bool allow_powerwash) { |
| 242 | return proxy_->SetChannel(in_target_channel, allow_powerwash, nullptr); |
| 243 | } |
| 244 | |
| 245 | bool DBusUpdateEngineClient::GetTargetChannel(string* out_channel) const { |
| 246 | return proxy_->GetChannel(false, // Get the target channel. |
| 247 | out_channel, |
| 248 | nullptr); |
| 249 | } |
| 250 | |
| 251 | bool DBusUpdateEngineClient::GetChannel(string* out_channel) const { |
| 252 | return proxy_->GetChannel(true, // Get the current channel. |
| 253 | out_channel, |
| 254 | nullptr); |
| 255 | } |
| 256 | |
Shuqian Zhao | 2997173 | 2016-02-05 11:29:32 -0800 | [diff] [blame] | 257 | bool DBusUpdateEngineClient::GetLastAttemptError( |
| 258 | int32_t* last_attempt_error) const { |
| 259 | return proxy_->GetLastAttemptError(last_attempt_error, nullptr); |
| 260 | } |
| 261 | |
Casey Dahlin | a93cd53 | 2016-01-14 16:55:11 -0800 | [diff] [blame] | 262 | } // namespace internal |
| 263 | } // namespace update_engine |