blob: 809ad13a82369b8fe1d864caacf3a7f9bb0d9b2a [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
29using chromeos_update_engine::StringToUpdateStatus;
30using dbus::Bus;
31using org::chromium::UpdateEngineInterfaceProxy;
32using std::string;
Amin Hassanic80d2d82019-06-21 17:43:32 -070033using std::unique_ptr;
Xiaochu Liu88d90382018-08-29 16:09:11 -070034using std::vector;
Casey Dahlina93cd532016-01-14 16:55:11 -080035
36namespace update_engine {
Amin Hassanic80d2d82019-06-21 17:43:32 -070037
38unique_ptr<UpdateEngineClient> UpdateEngineClient::CreateInstance() {
39 auto ret = std::make_unique<internal::DBusUpdateEngineClient>();
40 if (!ret->Init()) {
41 ret.reset();
42 }
43 return ret;
44}
45
Casey Dahlina93cd532016-01-14 16:55:11 -080046namespace internal {
47
48bool DBusUpdateEngineClient::Init() {
49 Bus::Options options;
50 options.bus_type = Bus::SYSTEM;
51 scoped_refptr<Bus> bus{new Bus{options}};
52
53 if (!bus->Connect())
54 return false;
55
56 proxy_.reset(new UpdateEngineInterfaceProxy{bus});
57 return true;
58}
59
60bool DBusUpdateEngineClient::AttemptUpdate(const string& in_app_version,
61 const string& in_omaha_url,
62 bool at_user_request) {
63 return proxy_->AttemptUpdateWithFlags(
64 in_app_version,
65 in_omaha_url,
Amin Hassani6bb001f2018-02-26 14:33:02 -080066 (at_user_request)
67 ? 0
68 : update_engine::UpdateAttemptFlags::kFlagNonInteractive,
Casey Dahlina93cd532016-01-14 16:55:11 -080069 nullptr);
70}
71
Amin Hassani9b66aa62019-03-14 14:13:30 -070072bool DBusUpdateEngineClient::AttemptInstall(const string& omaha_url,
73 const vector<string>& dlc_ids) {
Xiaochu Liu88d90382018-08-29 16:09:11 -070074 // Convert parameters into protobuf.
Amin Hassani9b66aa62019-03-14 14:13:30 -070075 dlcservice::DlcModuleList dlc_parameters;
Xiaochu Liu88d90382018-08-29 16:09:11 -070076 dlc_parameters.set_omaha_url(omaha_url);
Amin Hassani9b66aa62019-03-14 14:13:30 -070077 for (const auto& dlc_id : dlc_ids) {
78 dlcservice::DlcModuleInfo* dlc_module_info =
79 dlc_parameters.add_dlc_module_infos();
80 dlc_module_info->set_dlc_id(dlc_id);
Xiaochu Liu88d90382018-08-29 16:09:11 -070081 }
82 string dlc_request;
83 if (dlc_parameters.SerializeToString(&dlc_request)) {
84 return proxy_->AttemptInstall(dlc_request, nullptr /* brillo::ErrorPtr* */);
85 } else {
86 LOG(ERROR) << "Fail to serialize a protobuf to a string.";
87 return false;
88 }
89}
90
Casey Dahlina93cd532016-01-14 16:55:11 -080091bool DBusUpdateEngineClient::GetStatus(int64_t* out_last_checked_time,
92 double* out_progress,
93 UpdateStatus* out_update_status,
94 string* out_new_version,
95 int64_t* out_new_size) const {
96 string status_as_string;
97 const bool success = proxy_->GetStatus(out_last_checked_time,
98 out_progress,
99 &status_as_string,
100 out_new_version,
101 out_new_size,
102 nullptr);
103 if (!success) {
104 return false;
105 }
106
107 return StringToUpdateStatus(status_as_string, out_update_status);
108}
109
Alex Deymo5b5fa8b2016-10-06 15:40:49 -0700110bool DBusUpdateEngineClient::SetCohortHint(const string& cohort_hint) {
111 return proxy_->SetCohortHint(cohort_hint, nullptr);
112}
113
114bool DBusUpdateEngineClient::GetCohortHint(string* cohort_hint) const {
115 return proxy_->GetCohortHint(cohort_hint, nullptr);
116}
117
Casey Dahlina93cd532016-01-14 16:55:11 -0800118bool DBusUpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
119 return proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
120}
121
122bool DBusUpdateEngineClient::GetUpdateOverCellularPermission(
123 bool* allowed) const {
124 return proxy_->GetUpdateOverCellularPermission(allowed, nullptr);
125}
126
127bool DBusUpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
128 return proxy_->SetP2PUpdatePermission(enabled, nullptr);
129}
130
131bool DBusUpdateEngineClient::GetP2PUpdatePermission(bool* enabled) const {
132 return proxy_->GetP2PUpdatePermission(enabled, nullptr);
133}
134
135bool DBusUpdateEngineClient::Rollback(bool powerwash) {
136 return proxy_->AttemptRollback(powerwash, nullptr);
137}
138
139bool DBusUpdateEngineClient::GetRollbackPartition(
140 string* rollback_partition) const {
141 return proxy_->GetRollbackPartition(rollback_partition, nullptr);
142}
143
144bool DBusUpdateEngineClient::GetPrevVersion(string* prev_version) const {
145 return proxy_->GetPrevVersion(prev_version, nullptr);
146}
147
148void DBusUpdateEngineClient::RebootIfNeeded() {
149 bool ret = proxy_->RebootIfNeeded(nullptr);
150 if (!ret) {
151 // Reboot error code doesn't necessarily mean that a reboot
152 // failed. For example, D-Bus may be shutdown before we receive the
153 // result.
154 LOG(INFO) << "RebootIfNeeded() failure ignored.";
155 }
156}
157
158bool DBusUpdateEngineClient::ResetStatus() {
159 return proxy_->ResetStatus(nullptr);
160}
161
Alex Deymo492eaf52016-02-02 12:05:02 -0800162void DBusUpdateEngineClient::DBusStatusHandlersRegistered(
Amin Hassaniaefaab22019-01-14 16:20:16 -0800163 const string& interface, const string& signal_name, bool success) const {
Casey Dahlina93cd532016-01-14 16:55:11 -0800164 if (!success) {
Casey Dahlina715f7b2016-01-29 16:38:51 -0800165 for (auto handler : handlers_) {
Amin Hassaniaefaab22019-01-14 16:20:16 -0800166 handler->IPCError("Could not connect to" + signal_name + " on " +
167 interface);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800168 }
169 } else {
170 StatusUpdateHandlersRegistered(nullptr);
Casey Dahlina93cd532016-01-14 16:55:11 -0800171 }
Casey Dahlina715f7b2016-01-29 16:38:51 -0800172}
Casey Dahlina93cd532016-01-14 16:55:11 -0800173
Casey Dahlina715f7b2016-01-29 16:38:51 -0800174void DBusUpdateEngineClient::StatusUpdateHandlersRegistered(
175 StatusUpdateHandler* handler) const {
Casey Dahlina93cd532016-01-14 16:55:11 -0800176 int64_t last_checked_time;
177 double progress;
178 UpdateStatus update_status;
179 string new_version;
180 int64_t new_size;
181
Casey Dahlina715f7b2016-01-29 16:38:51 -0800182 if (!GetStatus(&last_checked_time,
183 &progress,
184 &update_status,
185 &new_version,
186 &new_size)) {
187 handler->IPCError("Could not query current status");
Casey Dahlina93cd532016-01-14 16:55:11 -0800188 return;
189 }
190
Alex Deymo492eaf52016-02-02 12:05:02 -0800191 std::vector<update_engine::StatusUpdateHandler*> just_handler = {handler};
192 for (auto h : handler ? just_handler : handlers_) {
Casey Dahlina715f7b2016-01-29 16:38:51 -0800193 h->HandleStatusUpdate(
194 last_checked_time, progress, update_status, new_version, new_size);
195 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800196}
197
Casey Dahlina715f7b2016-01-29 16:38:51 -0800198void DBusUpdateEngineClient::RunStatusUpdateHandlers(
Casey Dahlina93cd532016-01-14 16:55:11 -0800199 int64_t last_checked_time,
200 double progress,
201 const string& current_operation,
202 const string& new_version,
203 int64_t new_size) {
204 UpdateStatus status;
205 StringToUpdateStatus(current_operation, &status);
206
Casey Dahlina715f7b2016-01-29 16:38:51 -0800207 for (auto handler : handlers_) {
208 handler->HandleStatusUpdate(
209 last_checked_time, progress, status, new_version, new_size);
210 }
211}
212
213bool DBusUpdateEngineClient::UnregisterStatusUpdateHandler(
214 StatusUpdateHandler* handler) {
Alex Deymob3fa53b2016-04-18 19:57:58 -0700215 auto it = std::find(handlers_.begin(), handlers_.end(), handler);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800216 if (it != handlers_.end()) {
217 handlers_.erase(it);
218 return true;
219 }
220
221 return false;
Casey Dahlina93cd532016-01-14 16:55:11 -0800222}
223
Casey Dahlin40892492016-01-25 16:55:28 -0800224bool DBusUpdateEngineClient::RegisterStatusUpdateHandler(
Casey Dahlina93cd532016-01-14 16:55:11 -0800225 StatusUpdateHandler* handler) {
226 if (!base::MessageLoopForIO::current()) {
227 LOG(FATAL) << "Cannot get UpdateEngineClient outside of message loop.";
Casey Dahlin40892492016-01-25 16:55:28 -0800228 return false;
Casey Dahlina93cd532016-01-14 16:55:11 -0800229 }
230
Casey Dahlina715f7b2016-01-29 16:38:51 -0800231 handlers_.push_back(handler);
232
233 if (dbus_handler_registered_) {
234 StatusUpdateHandlersRegistered(handler);
235 return true;
236 }
237
Casey Dahlina93cd532016-01-14 16:55:11 -0800238 proxy_->RegisterStatusUpdateSignalHandler(
Casey Dahlina715f7b2016-01-29 16:38:51 -0800239 base::Bind(&DBusUpdateEngineClient::RunStatusUpdateHandlers,
240 base::Unretained(this)),
Alex Deymo492eaf52016-02-02 12:05:02 -0800241 base::Bind(&DBusUpdateEngineClient::DBusStatusHandlersRegistered,
242 base::Unretained(this)));
Casey Dahlina715f7b2016-01-29 16:38:51 -0800243
244 dbus_handler_registered_ = true;
Casey Dahlin40892492016-01-25 16:55:28 -0800245
246 return true;
Casey Dahlina93cd532016-01-14 16:55:11 -0800247}
248
249bool DBusUpdateEngineClient::SetTargetChannel(const string& in_target_channel,
250 bool allow_powerwash) {
251 return proxy_->SetChannel(in_target_channel, allow_powerwash, nullptr);
252}
253
254bool DBusUpdateEngineClient::GetTargetChannel(string* out_channel) const {
255 return proxy_->GetChannel(false, // Get the target channel.
256 out_channel,
257 nullptr);
258}
259
260bool DBusUpdateEngineClient::GetChannel(string* out_channel) const {
261 return proxy_->GetChannel(true, // Get the current channel.
262 out_channel,
263 nullptr);
264}
265
Shuqian Zhao29971732016-02-05 11:29:32 -0800266bool DBusUpdateEngineClient::GetLastAttemptError(
267 int32_t* last_attempt_error) const {
268 return proxy_->GetLastAttemptError(last_attempt_error, nullptr);
269}
270
Alex Deymob3fa53b2016-04-18 19:57:58 -0700271bool DBusUpdateEngineClient::GetEolStatus(int32_t* eol_status) const {
272 return proxy_->GetEolStatus(eol_status, nullptr);
273}
274
Casey Dahlina93cd532016-01-14 16:55:11 -0800275} // namespace internal
276} // namespace update_engine