blob: 2257d2e9ab4bdc1a5d3f6c70c5acf83ea8a93a32 [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>
22#include <update_engine/dbus-constants.h>
Xiaochu Liu88d90382018-08-29 16:09:11 -070023#include <update_engine/proto_bindings/update_engine.pb.h>
Casey Dahlina93cd532016-01-14 16:55:11 -080024
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
Xiaochu Liu88d90382018-08-29 16:09:11 -070060bool DBusUpdateEngineClient::AttemptInstall(const string& omaha_url,
61 const vector<string>& dlc_ids) {
62 // Convert parameters into protobuf.
63 chromeos_update_engine::DlcParameters dlc_parameters;
64 dlc_parameters.set_omaha_url(omaha_url);
65 for (const auto& dlc_id : dlc_ids) {
66 chromeos_update_engine::DlcInfo* dlc_info = dlc_parameters.add_dlc_infos();
67 dlc_info->set_dlc_id(dlc_id);
68 }
69 string dlc_request;
70 if (dlc_parameters.SerializeToString(&dlc_request)) {
71 return proxy_->AttemptInstall(dlc_request, nullptr /* brillo::ErrorPtr* */);
72 } else {
73 LOG(ERROR) << "Fail to serialize a protobuf to a string.";
74 return false;
75 }
76}
77
Casey Dahlina93cd532016-01-14 16:55:11 -080078bool DBusUpdateEngineClient::GetStatus(int64_t* out_last_checked_time,
79 double* out_progress,
80 UpdateStatus* out_update_status,
81 string* out_new_version,
82 int64_t* out_new_size) const {
83 string status_as_string;
84 const bool success = proxy_->GetStatus(out_last_checked_time,
85 out_progress,
86 &status_as_string,
87 out_new_version,
88 out_new_size,
89 nullptr);
90 if (!success) {
91 return false;
92 }
93
94 return StringToUpdateStatus(status_as_string, out_update_status);
95}
96
Alex Deymo5b5fa8b2016-10-06 15:40:49 -070097bool DBusUpdateEngineClient::SetCohortHint(const string& cohort_hint) {
98 return proxy_->SetCohortHint(cohort_hint, nullptr);
99}
100
101bool DBusUpdateEngineClient::GetCohortHint(string* cohort_hint) const {
102 return proxy_->GetCohortHint(cohort_hint, nullptr);
103}
104
Casey Dahlina93cd532016-01-14 16:55:11 -0800105bool DBusUpdateEngineClient::SetUpdateOverCellularPermission(bool allowed) {
106 return proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
107}
108
109bool DBusUpdateEngineClient::GetUpdateOverCellularPermission(
110 bool* allowed) const {
111 return proxy_->GetUpdateOverCellularPermission(allowed, nullptr);
112}
113
114bool DBusUpdateEngineClient::SetP2PUpdatePermission(bool enabled) {
115 return proxy_->SetP2PUpdatePermission(enabled, nullptr);
116}
117
118bool DBusUpdateEngineClient::GetP2PUpdatePermission(bool* enabled) const {
119 return proxy_->GetP2PUpdatePermission(enabled, nullptr);
120}
121
122bool DBusUpdateEngineClient::Rollback(bool powerwash) {
123 return proxy_->AttemptRollback(powerwash, nullptr);
124}
125
126bool DBusUpdateEngineClient::GetRollbackPartition(
127 string* rollback_partition) const {
128 return proxy_->GetRollbackPartition(rollback_partition, nullptr);
129}
130
131bool DBusUpdateEngineClient::GetPrevVersion(string* prev_version) const {
132 return proxy_->GetPrevVersion(prev_version, nullptr);
133}
134
135void DBusUpdateEngineClient::RebootIfNeeded() {
136 bool ret = proxy_->RebootIfNeeded(nullptr);
137 if (!ret) {
138 // Reboot error code doesn't necessarily mean that a reboot
139 // failed. For example, D-Bus may be shutdown before we receive the
140 // result.
141 LOG(INFO) << "RebootIfNeeded() failure ignored.";
142 }
143}
144
145bool DBusUpdateEngineClient::ResetStatus() {
146 return proxy_->ResetStatus(nullptr);
147}
148
Alex Deymo492eaf52016-02-02 12:05:02 -0800149void DBusUpdateEngineClient::DBusStatusHandlersRegistered(
Casey Dahlina93cd532016-01-14 16:55:11 -0800150 const string& interface,
151 const string& signal_name,
152 bool success) const {
153 if (!success) {
Casey Dahlina715f7b2016-01-29 16:38:51 -0800154 for (auto handler : handlers_) {
155 handler->IPCError("Could not connect to" + signal_name +
156 " on " + interface);
157 }
158 } else {
159 StatusUpdateHandlersRegistered(nullptr);
Casey Dahlina93cd532016-01-14 16:55:11 -0800160 }
Casey Dahlina715f7b2016-01-29 16:38:51 -0800161}
Casey Dahlina93cd532016-01-14 16:55:11 -0800162
Casey Dahlina715f7b2016-01-29 16:38:51 -0800163void DBusUpdateEngineClient::StatusUpdateHandlersRegistered(
164 StatusUpdateHandler* handler) const {
Casey Dahlina93cd532016-01-14 16:55:11 -0800165 int64_t last_checked_time;
166 double progress;
167 UpdateStatus update_status;
168 string new_version;
169 int64_t new_size;
170
Casey Dahlina715f7b2016-01-29 16:38:51 -0800171 if (!GetStatus(&last_checked_time,
172 &progress,
173 &update_status,
174 &new_version,
175 &new_size)) {
176 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_) {
Casey Dahlina715f7b2016-01-29 16:38:51 -0800182 h->HandleStatusUpdate(
183 last_checked_time, progress, update_status, new_version, new_size);
184 }
Casey Dahlina93cd532016-01-14 16:55:11 -0800185}
186
Casey Dahlina715f7b2016-01-29 16:38:51 -0800187void DBusUpdateEngineClient::RunStatusUpdateHandlers(
Casey Dahlina93cd532016-01-14 16:55:11 -0800188 int64_t last_checked_time,
189 double progress,
190 const string& current_operation,
191 const string& new_version,
192 int64_t new_size) {
193 UpdateStatus status;
194 StringToUpdateStatus(current_operation, &status);
195
Casey Dahlina715f7b2016-01-29 16:38:51 -0800196 for (auto handler : handlers_) {
197 handler->HandleStatusUpdate(
198 last_checked_time, progress, status, new_version, new_size);
199 }
200}
201
202bool DBusUpdateEngineClient::UnregisterStatusUpdateHandler(
203 StatusUpdateHandler* handler) {
Alex Deymob3fa53b2016-04-18 19:57:58 -0700204 auto it = std::find(handlers_.begin(), handlers_.end(), handler);
Casey Dahlina715f7b2016-01-29 16:38:51 -0800205 if (it != handlers_.end()) {
206 handlers_.erase(it);
207 return true;
208 }
209
210 return false;
Casey Dahlina93cd532016-01-14 16:55:11 -0800211}
212
Casey Dahlin40892492016-01-25 16:55:28 -0800213bool DBusUpdateEngineClient::RegisterStatusUpdateHandler(
Casey Dahlina93cd532016-01-14 16:55:11 -0800214 StatusUpdateHandler* handler) {
215 if (!base::MessageLoopForIO::current()) {
216 LOG(FATAL) << "Cannot get UpdateEngineClient outside of message loop.";
Casey Dahlin40892492016-01-25 16:55:28 -0800217 return false;
Casey Dahlina93cd532016-01-14 16:55:11 -0800218 }
219
Casey Dahlina715f7b2016-01-29 16:38:51 -0800220 handlers_.push_back(handler);
221
222 if (dbus_handler_registered_) {
223 StatusUpdateHandlersRegistered(handler);
224 return true;
225 }
226
Casey Dahlina93cd532016-01-14 16:55:11 -0800227 proxy_->RegisterStatusUpdateSignalHandler(
Casey Dahlina715f7b2016-01-29 16:38:51 -0800228 base::Bind(&DBusUpdateEngineClient::RunStatusUpdateHandlers,
229 base::Unretained(this)),
Alex Deymo492eaf52016-02-02 12:05:02 -0800230 base::Bind(&DBusUpdateEngineClient::DBusStatusHandlersRegistered,
231 base::Unretained(this)));
Casey Dahlina715f7b2016-01-29 16:38:51 -0800232
233 dbus_handler_registered_ = true;
Casey Dahlin40892492016-01-25 16:55:28 -0800234
235 return true;
Casey Dahlina93cd532016-01-14 16:55:11 -0800236}
237
238bool DBusUpdateEngineClient::SetTargetChannel(const string& in_target_channel,
239 bool allow_powerwash) {
240 return proxy_->SetChannel(in_target_channel, allow_powerwash, nullptr);
241}
242
243bool DBusUpdateEngineClient::GetTargetChannel(string* out_channel) const {
244 return proxy_->GetChannel(false, // Get the target channel.
245 out_channel,
246 nullptr);
247}
248
249bool DBusUpdateEngineClient::GetChannel(string* out_channel) const {
250 return proxy_->GetChannel(true, // Get the current channel.
251 out_channel,
252 nullptr);
253}
254
Shuqian Zhao29971732016-02-05 11:29:32 -0800255bool DBusUpdateEngineClient::GetLastAttemptError(
256 int32_t* last_attempt_error) const {
257 return proxy_->GetLastAttemptError(last_attempt_error, nullptr);
258}
259
Alex Deymob3fa53b2016-04-18 19:57:58 -0700260bool DBusUpdateEngineClient::GetEolStatus(int32_t* eol_status) const {
261 return proxy_->GetEolStatus(eol_status, nullptr);
262}
263
Casey Dahlina93cd532016-01-14 16:55:11 -0800264} // namespace internal
265} // namespace update_engine