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