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