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