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