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