blob: 18b155b3c36eb3e297f6c9d74f185a3c866e44c8 [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>
Amin Hassani9b66aa62019-03-14 14:13:30 -070024#include <dlcservice/proto_bindings/dlcservice.pb.h>
Casey Dahlina93cd532016-01-14 16:55:11 -080025#include <update_engine/dbus-constants.h>
26
27#include "update_engine/update_status_utils.h"
28
Casey Dahlina93cd532016-01-14 16:55:11 -080029using dbus::Bus;
30using org::chromium::UpdateEngineInterfaceProxy;
31using std::string;
Amin Hassanic80d2d82019-06-21 17:43:32 -070032using std::unique_ptr;
Xiaochu Liu88d90382018-08-29 16:09:11 -070033using std::vector;
Casey Dahlina93cd532016-01-14 16:55:11 -080034
35namespace update_engine {
Amin Hassanic80d2d82019-06-21 17:43:32 -070036
37unique_ptr<UpdateEngineClient> UpdateEngineClient::CreateInstance() {
38 auto ret = std::make_unique<internal::DBusUpdateEngineClient>();
39 if (!ret->Init()) {
40 ret.reset();
41 }
42 return ret;
43}
44
Casey Dahlina93cd532016-01-14 16:55:11 -080045namespace internal {
46
Amin Hassanieb463ee2019-06-20 19:23:03 -070047namespace {
48// This converts the status from Protobuf |StatusResult| to The internal
49// |UpdateEngineStatus| struct.
Amin Hassania4417432019-07-08 12:52:40 -070050void ConvertToUpdateEngineStatus(const StatusResult& status,
Amin Hassanieb463ee2019-06-20 19:23:03 -070051 UpdateEngineStatus* out_status) {
52 out_status->last_checked_time = status.last_checked_time();
53 out_status->progress = status.progress();
54 out_status->new_version = status.new_version();
55 out_status->new_size_bytes = status.new_size();
Amin Hassania4417432019-07-08 12:52:40 -070056 out_status->status = static_cast<UpdateStatus>(status.current_operation());
Amin Hassani9be122e2019-08-29 09:20:12 -070057 out_status->is_enterprise_rollback = status.is_enterprise_rollback();
Jae Hoon Kim2f78c1c2019-07-25 13:20:43 -070058 out_status->is_install = status.is_install();
Jae Hoon Kim051627a2019-09-03 12:56:32 -070059 out_status->eol_date = status.eol_date();
Amin Hassanieb463ee2019-06-20 19:23:03 -070060}
61} // namespace
62
Casey Dahlina93cd532016-01-14 16:55:11 -080063bool DBusUpdateEngineClient::Init() {
64 Bus::Options options;
65 options.bus_type = Bus::SYSTEM;
66 scoped_refptr<Bus> bus{new Bus{options}};
67
68 if (!bus->Connect())
69 return false;
70
71 proxy_.reset(new UpdateEngineInterfaceProxy{bus});
72 return true;
73}
74
75bool DBusUpdateEngineClient::AttemptUpdate(const string& in_app_version,
76 const string& in_omaha_url,
77 bool at_user_request) {
78 return proxy_->AttemptUpdateWithFlags(
79 in_app_version,
80 in_omaha_url,
Amin Hassani6bb001f2018-02-26 14:33:02 -080081 (at_user_request)
82 ? 0
83 : update_engine::UpdateAttemptFlags::kFlagNonInteractive,
Casey Dahlina93cd532016-01-14 16:55:11 -080084 nullptr);
85}
86
Amin Hassani9b66aa62019-03-14 14:13:30 -070087bool DBusUpdateEngineClient::AttemptInstall(const string& omaha_url,
88 const vector<string>& dlc_ids) {
Xiaochu Liu88d90382018-08-29 16:09:11 -070089 // Convert parameters into protobuf.
Amin Hassani9b66aa62019-03-14 14:13:30 -070090 dlcservice::DlcModuleList dlc_parameters;
Xiaochu Liu88d90382018-08-29 16:09:11 -070091 dlc_parameters.set_omaha_url(omaha_url);
Amin Hassani9b66aa62019-03-14 14:13:30 -070092 for (const auto& dlc_id : dlc_ids) {
93 dlcservice::DlcModuleInfo* dlc_module_info =
94 dlc_parameters.add_dlc_module_infos();
95 dlc_module_info->set_dlc_id(dlc_id);
Xiaochu Liu88d90382018-08-29 16:09:11 -070096 }
Amin Hassani3bab1772019-06-21 14:58:25 -070097 return proxy_->AttemptInstall(dlc_parameters,
98 nullptr /* brillo::ErrorPtr* */);
Xiaochu Liu88d90382018-08-29 16:09:11 -070099}
100
Amin Hassanieb463ee2019-06-20 19:23:03 -0700101bool DBusUpdateEngineClient::GetStatus(UpdateEngineStatus* out_status) const {
102 StatusResult status;
103 if (!proxy_->GetStatusAdvanced(&status, nullptr)) {
104 return false;
105 }
106
Amin Hassania4417432019-07-08 12:52:40 -0700107 ConvertToUpdateEngineStatus(status, out_status);
108 return true;
Casey Dahlina93cd532016-01-14 16:55:11 -0800109}
110
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700111bool DBusUpdateEngineClient::SetCohortHint(const string& cohort_hint) {
112 return proxy_->SetCohortHint(cohort_hint, nullptr);
113}
114
115bool DBusUpdateEngineClient::GetCohortHint(string* cohort_hint) const {
116 return proxy_->GetCohortHint(cohort_hint, nullptr);
117}
118
Casey Dahlina93cd532016-01-14 16:55:11 -0800119bool DBusUpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
120 return proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
121}
122
123bool DBusUpdateEngineClient::GetUpdateOverCellularPermission(
124 bool* allowed) const {
125 return proxy_->GetUpdateOverCellularPermission(allowed, nullptr);
126}
127
128bool DBusUpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
129 return proxy_->SetP2PUpdatePermission(enabled, nullptr);
130}
131
132bool DBusUpdateEngineClient::GetP2PUpdatePermission(bool* enabled) const {
133 return proxy_->GetP2PUpdatePermission(enabled, nullptr);
134}
135
136bool DBusUpdateEngineClient::Rollback(bool powerwash) {
137 return proxy_->AttemptRollback(powerwash, nullptr);
138}
139
140bool DBusUpdateEngineClient::GetRollbackPartition(
141 string* rollback_partition) const {
142 return proxy_->GetRollbackPartition(rollback_partition, nullptr);
143}
144
145bool DBusUpdateEngineClient::GetPrevVersion(string* prev_version) const {
146 return proxy_->GetPrevVersion(prev_version, nullptr);
147}
148
149void DBusUpdateEngineClient::RebootIfNeeded() {
150 bool ret = proxy_->RebootIfNeeded(nullptr);
151 if (!ret) {
152 // Reboot error code doesn't necessarily mean that a reboot
153 // failed. For example, D-Bus may be shutdown before we receive the
154 // result.
155 LOG(INFO) << "RebootIfNeeded() failure ignored.";
156 }
157}
158
159bool DBusUpdateEngineClient::ResetStatus() {
160 return proxy_->ResetStatus(nullptr);
161}
162
Alex Deymo492eaf52016-02-02 12:05:02 -0800163void DBusUpdateEngineClient::DBusStatusHandlersRegistered(
Amin Hassaniaefaab22019-01-14 16:20:16 -0800164 const string& interface, const string& signal_name, bool success) const {
Casey Dahlina93cd532016-01-14 16:55:11 -0800165 if (!success) {
Casey Dahlina715f7b2016-01-29 16:38:51 -0800166 for (auto handler : handlers_) {
Amin Hassaniaefaab22019-01-14 16:20:16 -0800167 handler->IPCError("Could not connect to" + signal_name + " on " +
168 interface);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800169 }
170 } else {
171 StatusUpdateHandlersRegistered(nullptr);
Casey Dahlina93cd532016-01-14 16:55:11 -0800172 }
Casey Dahlina715f7b2016-01-29 16:38:51 -0800173}
Casey Dahlina93cd532016-01-14 16:55:11 -0800174
Casey Dahlina715f7b2016-01-29 16:38:51 -0800175void DBusUpdateEngineClient::StatusUpdateHandlersRegistered(
176 StatusUpdateHandler* handler) const {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700177 UpdateEngineStatus status;
178 if (!GetStatus(&status)) {
Casey Dahlina715f7b2016-01-29 16:38:51 -0800179 handler->IPCError("Could not query current status");
Casey Dahlina93cd532016-01-14 16:55:11 -0800180 return;
181 }
182
Alex Deymo492eaf52016-02-02 12:05:02 -0800183 std::vector<update_engine::StatusUpdateHandler*> just_handler = {handler};
184 for (auto h : handler ? just_handler : handlers_) {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700185 h->HandleStatusUpdate(status);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800186 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800187}
188
Casey Dahlina715f7b2016-01-29 16:38:51 -0800189void DBusUpdateEngineClient::RunStatusUpdateHandlers(
Amin Hassanieb463ee2019-06-20 19:23:03 -0700190 const StatusResult& status) {
191 UpdateEngineStatus ue_status;
192 ConvertToUpdateEngineStatus(status, &ue_status);
Casey Dahlina93cd532016-01-14 16:55:11 -0800193
Casey Dahlina715f7b2016-01-29 16:38:51 -0800194 for (auto handler : handlers_) {
Amin Hassanieb463ee2019-06-20 19:23:03 -0700195 handler->HandleStatusUpdate(ue_status);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800196 }
197}
198
199bool DBusUpdateEngineClient::UnregisterStatusUpdateHandler(
200 StatusUpdateHandler* handler) {
Alex Deymob3fa53b2016-04-18 19:57:58 -0700201 auto it = std::find(handlers_.begin(), handlers_.end(), handler);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800202 if (it != handlers_.end()) {
203 handlers_.erase(it);
204 return true;
205 }
206
207 return false;
Casey Dahlina93cd532016-01-14 16:55:11 -0800208}
209
Casey Dahlin40892492016-01-25 16:55:28 -0800210bool DBusUpdateEngineClient::RegisterStatusUpdateHandler(
Casey Dahlina93cd532016-01-14 16:55:11 -0800211 StatusUpdateHandler* handler) {
212 if (!base::MessageLoopForIO::current()) {
213 LOG(FATAL) << "Cannot get UpdateEngineClient outside of message loop.";
Casey Dahlin40892492016-01-25 16:55:28 -0800214 return false;
Casey Dahlina93cd532016-01-14 16:55:11 -0800215 }
216
Casey Dahlina715f7b2016-01-29 16:38:51 -0800217 handlers_.push_back(handler);
218
219 if (dbus_handler_registered_) {
220 StatusUpdateHandlersRegistered(handler);
221 return true;
222 }
223
Amin Hassanieb463ee2019-06-20 19:23:03 -0700224 proxy_->RegisterStatusUpdateAdvancedSignalHandler(
Casey Dahlina715f7b2016-01-29 16:38:51 -0800225 base::Bind(&DBusUpdateEngineClient::RunStatusUpdateHandlers,
226 base::Unretained(this)),
Alex Deymo492eaf52016-02-02 12:05:02 -0800227 base::Bind(&DBusUpdateEngineClient::DBusStatusHandlersRegistered,
228 base::Unretained(this)));
Casey Dahlina715f7b2016-01-29 16:38:51 -0800229
230 dbus_handler_registered_ = true;
Casey Dahlin40892492016-01-25 16:55:28 -0800231
232 return true;
Casey Dahlina93cd532016-01-14 16:55:11 -0800233}
234
235bool DBusUpdateEngineClient::SetTargetChannel(const string& in_target_channel,
236 bool allow_powerwash) {
237 return proxy_->SetChannel(in_target_channel, allow_powerwash, nullptr);
238}
239
240bool DBusUpdateEngineClient::GetTargetChannel(string* out_channel) const {
241 return proxy_->GetChannel(false, // Get the target channel.
242 out_channel,
243 nullptr);
244}
245
246bool DBusUpdateEngineClient::GetChannel(string* out_channel) const {
247 return proxy_->GetChannel(true, // Get the current channel.
248 out_channel,
249 nullptr);
250}
251
Shuqian Zhao29971732016-02-05 11:29:32 -0800252bool DBusUpdateEngineClient::GetLastAttemptError(
253 int32_t* last_attempt_error) const {
254 return proxy_->GetLastAttemptError(last_attempt_error, nullptr);
255}
256
Casey Dahlina93cd532016-01-14 16:55:11 -0800257} // namespace internal
258} // namespace update_engine