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