blob: 3ffb0886ed99b0781660678d80f089720336d5ed [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
21#include <dbus/bus.h>
Amin Hassani9b66aa62019-03-14 14:13:30 -070022#include <dlcservice/proto_bindings/dlcservice.pb.h>
Casey Dahlina93cd532016-01-14 16:55:11 -080023#include <update_engine/dbus-constants.h>
24
25#include "update_engine/update_status_utils.h"
26
27using chromeos_update_engine::StringToUpdateStatus;
28using dbus::Bus;
29using org::chromium::UpdateEngineInterfaceProxy;
30using std::string;
Xiaochu Liu88d90382018-08-29 16:09:11 -070031using std::vector;
Casey Dahlina93cd532016-01-14 16:55:11 -080032
33namespace update_engine {
34namespace internal {
35
36bool 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
48bool 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 Hassani6bb001f2018-02-26 14:33:02 -080054 (at_user_request)
55 ? 0
56 : update_engine::UpdateAttemptFlags::kFlagNonInteractive,
Casey Dahlina93cd532016-01-14 16:55:11 -080057 nullptr);
58}
59
Amin Hassani9b66aa62019-03-14 14:13:30 -070060bool DBusUpdateEngineClient::AttemptInstall(const string& omaha_url,
61 const vector<string>& dlc_ids) {
Xiaochu Liu88d90382018-08-29 16:09:11 -070062 // Convert parameters into protobuf.
Amin Hassani9b66aa62019-03-14 14:13:30 -070063 dlcservice::DlcModuleList dlc_parameters;
Xiaochu Liu88d90382018-08-29 16:09:11 -070064 dlc_parameters.set_omaha_url(omaha_url);
Amin Hassani9b66aa62019-03-14 14:13:30 -070065 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 Liu88d90382018-08-29 16:09:11 -070069 }
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 Dahlina93cd532016-01-14 16:55:11 -080079bool 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 Deymo5b5fa8b2016-10-06 15:40:49 -070098bool DBusUpdateEngineClient::SetCohortHint(const string& cohort_hint) {
99 return proxy_->SetCohortHint(cohort_hint, nullptr);
100}
101
102bool DBusUpdateEngineClient::GetCohortHint(string* cohort_hint) const {
103 return proxy_->GetCohortHint(cohort_hint, nullptr);
104}
105
Casey Dahlina93cd532016-01-14 16:55:11 -0800106bool DBusUpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
107 return proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
108}
109
110bool DBusUpdateEngineClient::GetUpdateOverCellularPermission(
111 bool* allowed) const {
112 return proxy_->GetUpdateOverCellularPermission(allowed, nullptr);
113}
114
115bool DBusUpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
116 return proxy_->SetP2PUpdatePermission(enabled, nullptr);
117}
118
119bool DBusUpdateEngineClient::GetP2PUpdatePermission(bool* enabled) const {
120 return proxy_->GetP2PUpdatePermission(enabled, nullptr);
121}
122
123bool DBusUpdateEngineClient::Rollback(bool powerwash) {
124 return proxy_->AttemptRollback(powerwash, nullptr);
125}
126
127bool DBusUpdateEngineClient::GetRollbackPartition(
128 string* rollback_partition) const {
129 return proxy_->GetRollbackPartition(rollback_partition, nullptr);
130}
131
132bool DBusUpdateEngineClient::GetPrevVersion(string* prev_version) const {
133 return proxy_->GetPrevVersion(prev_version, nullptr);
134}
135
136void 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
146bool DBusUpdateEngineClient::ResetStatus() {
147 return proxy_->ResetStatus(nullptr);
148}
149
Alex Deymo492eaf52016-02-02 12:05:02 -0800150void DBusUpdateEngineClient::DBusStatusHandlersRegistered(
Amin Hassaniaefaab22019-01-14 16:20:16 -0800151 const string& interface, const string& signal_name, bool success) const {
Casey Dahlina93cd532016-01-14 16:55:11 -0800152 if (!success) {
Casey Dahlina715f7b2016-01-29 16:38:51 -0800153 for (auto handler : handlers_) {
Amin Hassaniaefaab22019-01-14 16:20:16 -0800154 handler->IPCError("Could not connect to" + signal_name + " on " +
155 interface);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800156 }
157 } else {
158 StatusUpdateHandlersRegistered(nullptr);
Casey Dahlina93cd532016-01-14 16:55:11 -0800159 }
Casey Dahlina715f7b2016-01-29 16:38:51 -0800160}
Casey Dahlina93cd532016-01-14 16:55:11 -0800161
Casey Dahlina715f7b2016-01-29 16:38:51 -0800162void DBusUpdateEngineClient::StatusUpdateHandlersRegistered(
163 StatusUpdateHandler* handler) const {
Casey Dahlina93cd532016-01-14 16:55:11 -0800164 int64_t last_checked_time;
165 double progress;
166 UpdateStatus update_status;
167 string new_version;
168 int64_t new_size;
169
Casey Dahlina715f7b2016-01-29 16:38:51 -0800170 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 Dahlina93cd532016-01-14 16:55:11 -0800176 return;
177 }
178
Alex Deymo492eaf52016-02-02 12:05:02 -0800179 std::vector<update_engine::StatusUpdateHandler*> just_handler = {handler};
180 for (auto h : handler ? just_handler : handlers_) {
Casey Dahlina715f7b2016-01-29 16:38:51 -0800181 h->HandleStatusUpdate(
182 last_checked_time, progress, update_status, new_version, new_size);
183 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800184}
185
Casey Dahlina715f7b2016-01-29 16:38:51 -0800186void DBusUpdateEngineClient::RunStatusUpdateHandlers(
Casey Dahlina93cd532016-01-14 16:55:11 -0800187 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 Dahlina715f7b2016-01-29 16:38:51 -0800195 for (auto handler : handlers_) {
196 handler->HandleStatusUpdate(
197 last_checked_time, progress, status, new_version, new_size);
198 }
199}
200
201bool DBusUpdateEngineClient::UnregisterStatusUpdateHandler(
202 StatusUpdateHandler* handler) {
Alex Deymob3fa53b2016-04-18 19:57:58 -0700203 auto it = std::find(handlers_.begin(), handlers_.end(), handler);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800204 if (it != handlers_.end()) {
205 handlers_.erase(it);
206 return true;
207 }
208
209 return false;
Casey Dahlina93cd532016-01-14 16:55:11 -0800210}
211
Casey Dahlin40892492016-01-25 16:55:28 -0800212bool DBusUpdateEngineClient::RegisterStatusUpdateHandler(
Casey Dahlina93cd532016-01-14 16:55:11 -0800213 StatusUpdateHandler* handler) {
214 if (!base::MessageLoopForIO::current()) {
215 LOG(FATAL) << "Cannot get UpdateEngineClient outside of message loop.";
Casey Dahlin40892492016-01-25 16:55:28 -0800216 return false;
Casey Dahlina93cd532016-01-14 16:55:11 -0800217 }
218
Casey Dahlina715f7b2016-01-29 16:38:51 -0800219 handlers_.push_back(handler);
220
221 if (dbus_handler_registered_) {
222 StatusUpdateHandlersRegistered(handler);
223 return true;
224 }
225
Casey Dahlina93cd532016-01-14 16:55:11 -0800226 proxy_->RegisterStatusUpdateSignalHandler(
Casey Dahlina715f7b2016-01-29 16:38:51 -0800227 base::Bind(&DBusUpdateEngineClient::RunStatusUpdateHandlers,
228 base::Unretained(this)),
Alex Deymo492eaf52016-02-02 12:05:02 -0800229 base::Bind(&DBusUpdateEngineClient::DBusStatusHandlersRegistered,
230 base::Unretained(this)));
Casey Dahlina715f7b2016-01-29 16:38:51 -0800231
232 dbus_handler_registered_ = true;
Casey Dahlin40892492016-01-25 16:55:28 -0800233
234 return true;
Casey Dahlina93cd532016-01-14 16:55:11 -0800235}
236
237bool DBusUpdateEngineClient::SetTargetChannel(const string& in_target_channel,
238 bool allow_powerwash) {
239 return proxy_->SetChannel(in_target_channel, allow_powerwash, nullptr);
240}
241
242bool DBusUpdateEngineClient::GetTargetChannel(string* out_channel) const {
243 return proxy_->GetChannel(false, // Get the target channel.
244 out_channel,
245 nullptr);
246}
247
248bool DBusUpdateEngineClient::GetChannel(string* out_channel) const {
249 return proxy_->GetChannel(true, // Get the current channel.
250 out_channel,
251 nullptr);
252}
253
Shuqian Zhao29971732016-02-05 11:29:32 -0800254bool DBusUpdateEngineClient::GetLastAttemptError(
255 int32_t* last_attempt_error) const {
256 return proxy_->GetLastAttemptError(last_attempt_error, nullptr);
257}
258
Alex Deymob3fa53b2016-04-18 19:57:58 -0700259bool DBusUpdateEngineClient::GetEolStatus(int32_t* eol_status) const {
260 return proxy_->GetEolStatus(eol_status, nullptr);
261}
262
Casey Dahlina93cd532016-01-14 16:55:11 -0800263} // namespace internal
264} // namespace update_engine