blob: 30ad78c6e6d38940dd3dc119e29cfd6812e73d99 [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
Qijiang Fan45061b62020-06-24 15:47:47 +090019#include <base/message_loop/message_loop_current.h>
Casey Dahlina93cd532016-01-14 16:55:11 -080020
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();
Miriam Polzer0cf1acb2020-04-29 17:39:51 +020059 out_status->will_powerwash_after_reboot =
60 status.will_powerwash_after_reboot();
Amin Hassanieb463ee2019-06-20 19:23:03 -070061}
62} // namespace
63
Casey Dahlina93cd532016-01-14 16:55:11 -080064bool DBusUpdateEngineClient::Init() {
65 Bus::Options options;
66 options.bus_type = Bus::SYSTEM;
67 scoped_refptr<Bus> bus{new Bus{options}};
68
69 if (!bus->Connect())
70 return false;
71
72 proxy_.reset(new UpdateEngineInterfaceProxy{bus});
73 return true;
74}
75
76bool DBusUpdateEngineClient::AttemptUpdate(const string& in_app_version,
77 const string& in_omaha_url,
78 bool at_user_request) {
79 return proxy_->AttemptUpdateWithFlags(
80 in_app_version,
81 in_omaha_url,
Amin Hassani6bb001f2018-02-26 14:33:02 -080082 (at_user_request)
83 ? 0
84 : update_engine::UpdateAttemptFlags::kFlagNonInteractive,
Casey Dahlina93cd532016-01-14 16:55:11 -080085 nullptr);
86}
87
Amin Hassani9b66aa62019-03-14 14:13:30 -070088bool DBusUpdateEngineClient::AttemptInstall(const string& omaha_url,
89 const vector<string>& dlc_ids) {
Amin Hassanifab175a2020-04-17 11:20:50 -070090 return proxy_->AttemptInstall(omaha_url, dlc_ids, nullptr);
Xiaochu Liu88d90382018-08-29 16:09:11 -070091}
92
Andrewa8d7df32020-03-15 20:10:01 -070093bool DBusUpdateEngineClient::SetDlcActiveValue(bool is_active,
94 const std::string& dlc_id) {
95 return proxy_->SetDlcActiveValue(is_active, dlc_id, /*error=*/nullptr);
96}
97
Amin Hassanieb463ee2019-06-20 19:23:03 -070098bool DBusUpdateEngineClient::GetStatus(UpdateEngineStatus* out_status) const {
99 StatusResult status;
100 if (!proxy_->GetStatusAdvanced(&status, nullptr)) {
101 return false;
102 }
103
Amin Hassania4417432019-07-08 12:52:40 -0700104 ConvertToUpdateEngineStatus(status, out_status);
105 return true;
Casey Dahlina93cd532016-01-14 16:55:11 -0800106}
107
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700108bool DBusUpdateEngineClient::SetCohortHint(const string& cohort_hint) {
109 return proxy_->SetCohortHint(cohort_hint, nullptr);
110}
111
112bool DBusUpdateEngineClient::GetCohortHint(string* cohort_hint) const {
113 return proxy_->GetCohortHint(cohort_hint, nullptr);
114}
115
Casey Dahlina93cd532016-01-14 16:55:11 -0800116bool DBusUpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
117 return proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
118}
119
120bool DBusUpdateEngineClient::GetUpdateOverCellularPermission(
121 bool* allowed) const {
122 return proxy_->GetUpdateOverCellularPermission(allowed, nullptr);
123}
124
125bool DBusUpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
126 return proxy_->SetP2PUpdatePermission(enabled, nullptr);
127}
128
129bool DBusUpdateEngineClient::GetP2PUpdatePermission(bool* enabled) const {
130 return proxy_->GetP2PUpdatePermission(enabled, nullptr);
131}
132
133bool DBusUpdateEngineClient::Rollback(bool powerwash) {
134 return proxy_->AttemptRollback(powerwash, nullptr);
135}
136
137bool DBusUpdateEngineClient::GetRollbackPartition(
138 string* rollback_partition) const {
139 return proxy_->GetRollbackPartition(rollback_partition, nullptr);
140}
141
142bool DBusUpdateEngineClient::GetPrevVersion(string* prev_version) const {
143 return proxy_->GetPrevVersion(prev_version, nullptr);
144}
145
146void DBusUpdateEngineClient::RebootIfNeeded() {
147 bool ret = proxy_->RebootIfNeeded(nullptr);
148 if (!ret) {
149 // Reboot error code doesn't necessarily mean that a reboot
150 // failed. For example, D-Bus may be shutdown before we receive the
151 // result.
152 LOG(INFO) << "RebootIfNeeded() failure ignored.";
153 }
154}
155
156bool DBusUpdateEngineClient::ResetStatus() {
157 return proxy_->ResetStatus(nullptr);
158}
159
Alex Deymo492eaf52016-02-02 12:05:02 -0800160void DBusUpdateEngineClient::DBusStatusHandlersRegistered(
Amin Hassaniaefaab22019-01-14 16:20:16 -0800161 const string& interface, const string& signal_name, bool success) const {
Casey Dahlina93cd532016-01-14 16:55:11 -0800162 if (!success) {
Casey Dahlina715f7b2016-01-29 16:38:51 -0800163 for (auto handler : handlers_) {
Amin Hassaniaefaab22019-01-14 16:20:16 -0800164 handler->IPCError("Could not connect to" + signal_name + " on " +
165 interface);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800166 }
167 } else {
168 StatusUpdateHandlersRegistered(nullptr);
Casey Dahlina93cd532016-01-14 16:55:11 -0800169 }
Casey Dahlina715f7b2016-01-29 16:38:51 -0800170}
Casey Dahlina93cd532016-01-14 16:55:11 -0800171
Casey Dahlina715f7b2016-01-29 16:38:51 -0800172void DBusUpdateEngineClient::StatusUpdateHandlersRegistered(
173 StatusUpdateHandler* handler) const {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700174 UpdateEngineStatus status;
175 if (!GetStatus(&status)) {
Casey Dahlina715f7b2016-01-29 16:38:51 -0800176 handler->IPCError("Could not query current status");
Casey Dahlina93cd532016-01-14 16:55:11 -0800177 return;
178 }
179
Alex Deymo492eaf52016-02-02 12:05:02 -0800180 std::vector<update_engine::StatusUpdateHandler*> just_handler = {handler};
181 for (auto h : handler ? just_handler : handlers_) {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700182 h->HandleStatusUpdate(status);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800183 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800184}
185
Casey Dahlina715f7b2016-01-29 16:38:51 -0800186void DBusUpdateEngineClient::RunStatusUpdateHandlers(
Amin Hassanieb463ee2019-06-20 19:23:03 -0700187 const StatusResult& status) {
188 UpdateEngineStatus ue_status;
189 ConvertToUpdateEngineStatus(status, &ue_status);
Casey Dahlina93cd532016-01-14 16:55:11 -0800190
Casey Dahlina715f7b2016-01-29 16:38:51 -0800191 for (auto handler : handlers_) {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700192 handler->HandleStatusUpdate(ue_status);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800193 }
194}
195
196bool DBusUpdateEngineClient::UnregisterStatusUpdateHandler(
197 StatusUpdateHandler* handler) {
Alex Deymob3fa53b2016-04-18 19:57:58 -0700198 auto it = std::find(handlers_.begin(), handlers_.end(), handler);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800199 if (it != handlers_.end()) {
200 handlers_.erase(it);
201 return true;
202 }
203
204 return false;
Casey Dahlina93cd532016-01-14 16:55:11 -0800205}
206
Casey Dahlin40892492016-01-25 16:55:28 -0800207bool DBusUpdateEngineClient::RegisterStatusUpdateHandler(
Casey Dahlina93cd532016-01-14 16:55:11 -0800208 StatusUpdateHandler* handler) {
hscham00b6aa22020-02-20 12:32:06 +0900209 if (!base::MessageLoopCurrent::IsSet()) {
Casey Dahlina93cd532016-01-14 16:55:11 -0800210 LOG(FATAL) << "Cannot get UpdateEngineClient outside of message loop.";
Casey Dahlin40892492016-01-25 16:55:28 -0800211 return false;
Casey Dahlina93cd532016-01-14 16:55:11 -0800212 }
213
Casey Dahlina715f7b2016-01-29 16:38:51 -0800214 handlers_.push_back(handler);
215
216 if (dbus_handler_registered_) {
217 StatusUpdateHandlersRegistered(handler);
218 return true;
219 }
220
Amin Hassanieb463ee2019-06-20 19:23:03 -0700221 proxy_->RegisterStatusUpdateAdvancedSignalHandler(
Casey Dahlina715f7b2016-01-29 16:38:51 -0800222 base::Bind(&DBusUpdateEngineClient::RunStatusUpdateHandlers,
223 base::Unretained(this)),
Alex Deymo492eaf52016-02-02 12:05:02 -0800224 base::Bind(&DBusUpdateEngineClient::DBusStatusHandlersRegistered,
225 base::Unretained(this)));
Casey Dahlina715f7b2016-01-29 16:38:51 -0800226
227 dbus_handler_registered_ = true;
Casey Dahlin40892492016-01-25 16:55:28 -0800228
229 return true;
Casey Dahlina93cd532016-01-14 16:55:11 -0800230}
231
232bool DBusUpdateEngineClient::SetTargetChannel(const string& in_target_channel,
233 bool allow_powerwash) {
234 return proxy_->SetChannel(in_target_channel, allow_powerwash, nullptr);
235}
236
237bool DBusUpdateEngineClient::GetTargetChannel(string* out_channel) const {
238 return proxy_->GetChannel(false, // Get the target channel.
239 out_channel,
240 nullptr);
241}
242
243bool DBusUpdateEngineClient::GetChannel(string* out_channel) const {
244 return proxy_->GetChannel(true, // Get the current channel.
245 out_channel,
246 nullptr);
247}
248
Shuqian Zhao29971732016-02-05 11:29:32 -0800249bool DBusUpdateEngineClient::GetLastAttemptError(
250 int32_t* last_attempt_error) const {
251 return proxy_->GetLastAttemptError(last_attempt_error, nullptr);
252}
253
Casey Dahlina93cd532016-01-14 16:55:11 -0800254} // namespace internal
255} // namespace update_engine