blob: 5ca519a16b0b418f4e0bcf9e1086c3642a1bc7e9 [file] [log] [blame]
Casey Dahlina93cd532016-01-14 16:55:11 -08001//
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 Hassanic80d2d82019-06-21 17:43:32 -070021#include <memory>
22
Casey Dahlina93cd532016-01-14 16:55:11 -080023#include <dbus/bus.h>
24#include <update_engine/dbus-constants.h>
25
26#include "update_engine/update_status_utils.h"
27
Casey Dahlina93cd532016-01-14 16:55:11 -080028using dbus::Bus;
29using org::chromium::UpdateEngineInterfaceProxy;
30using std::string;
Amin Hassanic80d2d82019-06-21 17:43:32 -070031using std::unique_ptr;
Xiaochu Liu88d90382018-08-29 16:09:11 -070032using std::vector;
Casey Dahlina93cd532016-01-14 16:55:11 -080033
34namespace update_engine {
Amin Hassanic80d2d82019-06-21 17:43:32 -070035
36unique_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 Dahlina93cd532016-01-14 16:55:11 -080044namespace internal {
45
Amin Hassanieb463ee2019-06-20 19:23:03 -070046namespace {
47// This converts the status from Protobuf |StatusResult| to The internal
48// |UpdateEngineStatus| struct.
Amin Hassania4417432019-07-08 12:52:40 -070049void ConvertToUpdateEngineStatus(const StatusResult& status,
Amin Hassanieb463ee2019-06-20 19:23:03 -070050 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 Hassania4417432019-07-08 12:52:40 -070055 out_status->status = static_cast<UpdateStatus>(status.current_operation());
Amin Hassani9be122e2019-08-29 09:20:12 -070056 out_status->is_enterprise_rollback = status.is_enterprise_rollback();
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -070057 out_status->is_install = status.is_install();
Jae Hoon Kim051627a2019-09-03 12:56:32 -070058 out_status->eol_date = status.eol_date();
Amin Hassanieb463ee2019-06-20 19:23:03 -070059}
60} // namespace
61
Casey Dahlina93cd532016-01-14 16:55:11 -080062bool DBusUpdateEngineClient::Init() {
63 Bus::Options options;
64 options.bus_type = Bus::SYSTEM;
65 scoped_refptr<Bus> bus{new Bus{options}};
66
67 if (!bus->Connect())
68 return false;
69
70 proxy_.reset(new UpdateEngineInterfaceProxy{bus});
71 return true;
72}
73
74bool DBusUpdateEngineClient::AttemptUpdate(const string& in_app_version,
75 const string& in_omaha_url,
76 bool at_user_request) {
77 return proxy_->AttemptUpdateWithFlags(
78 in_app_version,
79 in_omaha_url,
Amin Hassani6bb001f2018-02-26 14:33:02 -080080 (at_user_request)
81 ? 0
82 : update_engine::UpdateAttemptFlags::kFlagNonInteractive,
Casey Dahlina93cd532016-01-14 16:55:11 -080083 nullptr);
84}
85
Amin Hassani9b66aa62019-03-14 14:13:30 -070086bool DBusUpdateEngineClient::AttemptInstall(const string& omaha_url,
87 const vector<string>& dlc_ids) {
Amin Hassanifab175a2020-04-17 11:20:50 -070088 return proxy_->AttemptInstall(omaha_url, dlc_ids, nullptr);
Xiaochu Liu88d90382018-08-29 16:09:11 -070089}
90
Andrewa8d7df32020-03-15 20:10:01 -070091bool DBusUpdateEngineClient::SetDlcActiveValue(bool is_active,
92 const std::string& dlc_id) {
93 return proxy_->SetDlcActiveValue(is_active, dlc_id, /*error=*/nullptr);
94}
95
Amin Hassanieb463ee2019-06-20 19:23:03 -070096bool DBusUpdateEngineClient::GetStatus(UpdateEngineStatus* out_status) const {
97 StatusResult status;
98 if (!proxy_->GetStatusAdvanced(&status, nullptr)) {
99 return false;
100 }
101
Amin Hassania4417432019-07-08 12:52:40 -0700102 ConvertToUpdateEngineStatus(status, out_status);
103 return true;
Casey Dahlina93cd532016-01-14 16:55:11 -0800104}
105
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700106bool DBusUpdateEngineClient::SetCohortHint(const string& cohort_hint) {
107 return proxy_->SetCohortHint(cohort_hint, nullptr);
108}
109
110bool DBusUpdateEngineClient::GetCohortHint(string* cohort_hint) const {
111 return proxy_->GetCohortHint(cohort_hint, nullptr);
112}
113
Casey Dahlina93cd532016-01-14 16:55:11 -0800114bool DBusUpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
115 return proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
116}
117
118bool DBusUpdateEngineClient::GetUpdateOverCellularPermission(
119 bool* allowed) const {
120 return proxy_->GetUpdateOverCellularPermission(allowed, nullptr);
121}
122
123bool DBusUpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
124 return proxy_->SetP2PUpdatePermission(enabled, nullptr);
125}
126
127bool DBusUpdateEngineClient::GetP2PUpdatePermission(bool* enabled) const {
128 return proxy_->GetP2PUpdatePermission(enabled, nullptr);
129}
130
131bool DBusUpdateEngineClient::Rollback(bool powerwash) {
132 return proxy_->AttemptRollback(powerwash, nullptr);
133}
134
135bool DBusUpdateEngineClient::GetRollbackPartition(
136 string* rollback_partition) const {
137 return proxy_->GetRollbackPartition(rollback_partition, nullptr);
138}
139
140bool DBusUpdateEngineClient::GetPrevVersion(string* prev_version) const {
141 return proxy_->GetPrevVersion(prev_version, nullptr);
142}
143
144void DBusUpdateEngineClient::RebootIfNeeded() {
145 bool ret = proxy_->RebootIfNeeded(nullptr);
146 if (!ret) {
147 // Reboot error code doesn't necessarily mean that a reboot
148 // failed. For example, D-Bus may be shutdown before we receive the
149 // result.
150 LOG(INFO) << "RebootIfNeeded() failure ignored.";
151 }
152}
153
154bool DBusUpdateEngineClient::ResetStatus() {
155 return proxy_->ResetStatus(nullptr);
156}
157
Alex Deymo492eaf52016-02-02 12:05:02 -0800158void DBusUpdateEngineClient::DBusStatusHandlersRegistered(
Amin Hassaniaefaab22019-01-14 16:20:16 -0800159 const string& interface, const string& signal_name, bool success) const {
Casey Dahlina93cd532016-01-14 16:55:11 -0800160 if (!success) {
Casey Dahlina715f7b2016-01-29 16:38:51 -0800161 for (auto handler : handlers_) {
Amin Hassaniaefaab22019-01-14 16:20:16 -0800162 handler->IPCError("Could not connect to" + signal_name + " on " +
163 interface);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800164 }
165 } else {
166 StatusUpdateHandlersRegistered(nullptr);
Casey Dahlina93cd532016-01-14 16:55:11 -0800167 }
Casey Dahlina715f7b2016-01-29 16:38:51 -0800168}
Casey Dahlina93cd532016-01-14 16:55:11 -0800169
Casey Dahlina715f7b2016-01-29 16:38:51 -0800170void DBusUpdateEngineClient::StatusUpdateHandlersRegistered(
171 StatusUpdateHandler* handler) const {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700172 UpdateEngineStatus status;
173 if (!GetStatus(&status)) {
Casey Dahlina715f7b2016-01-29 16:38:51 -0800174 handler->IPCError("Could not query current status");
Casey Dahlina93cd532016-01-14 16:55:11 -0800175 return;
176 }
177
Alex Deymo492eaf52016-02-02 12:05:02 -0800178 std::vector<update_engine::StatusUpdateHandler*> just_handler = {handler};
179 for (auto h : handler ? just_handler : handlers_) {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700180 h->HandleStatusUpdate(status);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800181 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800182}
183
Casey Dahlina715f7b2016-01-29 16:38:51 -0800184void DBusUpdateEngineClient::RunStatusUpdateHandlers(
Amin Hassanieb463ee2019-06-20 19:23:03 -0700185 const StatusResult& status) {
186 UpdateEngineStatus ue_status;
187 ConvertToUpdateEngineStatus(status, &ue_status);
Casey Dahlina93cd532016-01-14 16:55:11 -0800188
Casey Dahlina715f7b2016-01-29 16:38:51 -0800189 for (auto handler : handlers_) {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700190 handler->HandleStatusUpdate(ue_status);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800191 }
192}
193
194bool DBusUpdateEngineClient::UnregisterStatusUpdateHandler(
195 StatusUpdateHandler* handler) {
Alex Deymob3fa53b2016-04-18 19:57:58 -0700196 auto it = std::find(handlers_.begin(), handlers_.end(), handler);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800197 if (it != handlers_.end()) {
198 handlers_.erase(it);
199 return true;
200 }
201
202 return false;
Casey Dahlina93cd532016-01-14 16:55:11 -0800203}
204
Casey Dahlin40892492016-01-25 16:55:28 -0800205bool DBusUpdateEngineClient::RegisterStatusUpdateHandler(
Casey Dahlina93cd532016-01-14 16:55:11 -0800206 StatusUpdateHandler* handler) {
hscham00b6aa22020-02-20 12:32:06 +0900207 if (!base::MessageLoopCurrent::IsSet()) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800208 LOG(FATAL) << "Cannot get UpdateEngineClient outside of message loop.";
Casey Dahlin40892492016-01-25 16:55:28 -0800209 return false;
Casey Dahlina93cd532016-01-14 16:55:11 -0800210 }
211
Casey Dahlina715f7b2016-01-29 16:38:51 -0800212 handlers_.push_back(handler);
213
214 if (dbus_handler_registered_) {
215 StatusUpdateHandlersRegistered(handler);
216 return true;
217 }
218
Amin Hassanieb463ee2019-06-20 19:23:03 -0700219 proxy_->RegisterStatusUpdateAdvancedSignalHandler(
Casey Dahlina715f7b2016-01-29 16:38:51 -0800220 base::Bind(&DBusUpdateEngineClient::RunStatusUpdateHandlers,
221 base::Unretained(this)),
Alex Deymo492eaf52016-02-02 12:05:02 -0800222 base::Bind(&DBusUpdateEngineClient::DBusStatusHandlersRegistered,
223 base::Unretained(this)));
Casey Dahlina715f7b2016-01-29 16:38:51 -0800224
225 dbus_handler_registered_ = true;
Casey Dahlin40892492016-01-25 16:55:28 -0800226
227 return true;
Casey Dahlina93cd532016-01-14 16:55:11 -0800228}
229
230bool DBusUpdateEngineClient::SetTargetChannel(const string& in_target_channel,
231 bool allow_powerwash) {
232 return proxy_->SetChannel(in_target_channel, allow_powerwash, nullptr);
233}
234
235bool DBusUpdateEngineClient::GetTargetChannel(string* out_channel) const {
236 return proxy_->GetChannel(false, // Get the target channel.
237 out_channel,
238 nullptr);
239}
240
241bool DBusUpdateEngineClient::GetChannel(string* out_channel) const {
242 return proxy_->GetChannel(true, // Get the current channel.
243 out_channel,
244 nullptr);
245}
246
Shuqian Zhao29971732016-02-05 11:29:32 -0800247bool DBusUpdateEngineClient::GetLastAttemptError(
248 int32_t* last_attempt_error) const {
249 return proxy_->GetLastAttemptError(last_attempt_error, nullptr);
250}
251
Casey Dahlina93cd532016-01-14 16:55:11 -0800252} // namespace internal
253} // namespace update_engine