1 //
2 // Copyright (C) 2012 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/dbus_service.h"
18 
19 #include <string>
20 #include <vector>
21 
22 #include <update_engine/dbus-constants.h>
23 
24 #include "update_engine/dbus_connection.h"
25 #include "update_engine/proto_bindings/update_engine.pb.h"
26 #include "update_engine/update_status_utils.h"
27 
28 namespace chromeos_update_engine {
29 
30 using brillo::ErrorPtr;
31 using chromeos_update_engine::UpdateEngineService;
32 using std::string;
33 using std::vector;
34 using update_engine::Operation;
35 using update_engine::StatusResult;
36 using update_engine::UpdateEngineStatus;
37 
38 namespace {
39 // Converts the internal |UpdateEngineStatus| to the protobuf |StatusResult|.
ConvertToStatusResult(const UpdateEngineStatus & ue_status,StatusResult * out_status)40 void ConvertToStatusResult(const UpdateEngineStatus& ue_status,
41                            StatusResult* out_status) {
42   out_status->set_last_checked_time(ue_status.last_checked_time);
43   out_status->set_progress(ue_status.progress);
44   out_status->set_current_operation(static_cast<Operation>(ue_status.status));
45   out_status->set_new_version(ue_status.new_version);
46   out_status->set_new_size(ue_status.new_size_bytes);
47   out_status->set_is_enterprise_rollback(ue_status.is_enterprise_rollback);
48   out_status->set_is_install(ue_status.is_install);
49   out_status->set_eol_date(ue_status.eol_date);
50   out_status->set_will_powerwash_after_reboot(
51       ue_status.will_powerwash_after_reboot);
52 }
53 }  // namespace
54 
DBusUpdateEngineService(SystemState * system_state)55 DBusUpdateEngineService::DBusUpdateEngineService(SystemState* system_state)
56     : common_(new UpdateEngineService{system_state}) {}
57 
58 // org::chromium::UpdateEngineInterfaceInterface methods implementation.
59 
AttemptUpdate(ErrorPtr * error,const string & in_app_version,const string & in_omaha_url)60 bool DBusUpdateEngineService::AttemptUpdate(ErrorPtr* error,
61                                             const string& in_app_version,
62                                             const string& in_omaha_url) {
63   return AttemptUpdateWithFlags(
64       error, in_app_version, in_omaha_url, 0 /* no flags */);
65 }
66 
AttemptUpdateWithFlags(ErrorPtr * error,const string & in_app_version,const string & in_omaha_url,int32_t in_flags_as_int)67 bool DBusUpdateEngineService::AttemptUpdateWithFlags(
68     ErrorPtr* error,
69     const string& in_app_version,
70     const string& in_omaha_url,
71     int32_t in_flags_as_int) {
72   update_engine::AttemptUpdateFlags flags =
73       static_cast<update_engine::AttemptUpdateFlags>(in_flags_as_int);
74   bool interactive = !(flags & update_engine::kAttemptUpdateFlagNonInteractive);
75   bool result;
76   return common_->AttemptUpdate(
77       error,
78       in_app_version,
79       in_omaha_url,
80       interactive ? 0 : update_engine::UpdateAttemptFlags::kFlagNonInteractive,
81       &result);
82 }
83 
AttemptInstall(ErrorPtr * error,const string & in_omaha_url,const vector<string> & dlc_ids)84 bool DBusUpdateEngineService::AttemptInstall(ErrorPtr* error,
85                                              const string& in_omaha_url,
86                                              const vector<string>& dlc_ids) {
87   return common_->AttemptInstall(error, in_omaha_url, dlc_ids);
88 }
89 
AttemptRollback(ErrorPtr * error,bool in_powerwash)90 bool DBusUpdateEngineService::AttemptRollback(ErrorPtr* error,
91                                               bool in_powerwash) {
92   return common_->AttemptRollback(error, in_powerwash);
93 }
94 
CanRollback(ErrorPtr * error,bool * out_can_rollback)95 bool DBusUpdateEngineService::CanRollback(ErrorPtr* error,
96                                           bool* out_can_rollback) {
97   return common_->CanRollback(error, out_can_rollback);
98 }
99 
ResetStatus(ErrorPtr * error)100 bool DBusUpdateEngineService::ResetStatus(ErrorPtr* error) {
101   return common_->ResetStatus(error);
102 }
103 
SetDlcActiveValue(brillo::ErrorPtr * error,bool is_active,const string & dlc_id)104 bool DBusUpdateEngineService::SetDlcActiveValue(brillo::ErrorPtr* error,
105                                                 bool is_active,
106                                                 const string& dlc_id) {
107   return common_->SetDlcActiveValue(error, is_active, dlc_id);
108 }
109 
GetStatusAdvanced(ErrorPtr * error,StatusResult * out_status)110 bool DBusUpdateEngineService::GetStatusAdvanced(ErrorPtr* error,
111                                                 StatusResult* out_status) {
112   UpdateEngineStatus status;
113   if (!common_->GetStatus(error, &status)) {
114     return false;
115   }
116 
117   ConvertToStatusResult(status, out_status);
118   return true;
119 }
120 
RebootIfNeeded(ErrorPtr * error)121 bool DBusUpdateEngineService::RebootIfNeeded(ErrorPtr* error) {
122   return common_->RebootIfNeeded(error);
123 }
124 
SetChannel(ErrorPtr * error,const string & in_target_channel,bool in_is_powerwash_allowed)125 bool DBusUpdateEngineService::SetChannel(ErrorPtr* error,
126                                          const string& in_target_channel,
127                                          bool in_is_powerwash_allowed) {
128   return common_->SetChannel(error, in_target_channel, in_is_powerwash_allowed);
129 }
130 
GetChannel(ErrorPtr * error,bool in_get_current_channel,string * out_channel)131 bool DBusUpdateEngineService::GetChannel(ErrorPtr* error,
132                                          bool in_get_current_channel,
133                                          string* out_channel) {
134   return common_->GetChannel(error, in_get_current_channel, out_channel);
135 }
136 
GetCohortHint(ErrorPtr * error,string * out_cohort_hint)137 bool DBusUpdateEngineService::GetCohortHint(ErrorPtr* error,
138                                             string* out_cohort_hint) {
139   return common_->GetCohortHint(error, out_cohort_hint);
140 }
141 
SetCohortHint(ErrorPtr * error,const string & in_cohort_hint)142 bool DBusUpdateEngineService::SetCohortHint(ErrorPtr* error,
143                                             const string& in_cohort_hint) {
144   return common_->SetCohortHint(error, in_cohort_hint);
145 }
146 
SetP2PUpdatePermission(ErrorPtr * error,bool in_enabled)147 bool DBusUpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
148                                                      bool in_enabled) {
149   return common_->SetP2PUpdatePermission(error, in_enabled);
150 }
151 
GetP2PUpdatePermission(ErrorPtr * error,bool * out_enabled)152 bool DBusUpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
153                                                      bool* out_enabled) {
154   return common_->GetP2PUpdatePermission(error, out_enabled);
155 }
156 
SetUpdateOverCellularPermission(ErrorPtr * error,bool in_allowed)157 bool DBusUpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
158                                                               bool in_allowed) {
159   return common_->SetUpdateOverCellularPermission(error, in_allowed);
160 }
161 
SetUpdateOverCellularTarget(brillo::ErrorPtr * error,const std::string & target_version,int64_t target_size)162 bool DBusUpdateEngineService::SetUpdateOverCellularTarget(
163     brillo::ErrorPtr* error,
164     const std::string& target_version,
165     int64_t target_size) {
166   return common_->SetUpdateOverCellularTarget(
167       error, target_version, target_size);
168 }
169 
GetUpdateOverCellularPermission(ErrorPtr * error,bool * out_allowed)170 bool DBusUpdateEngineService::GetUpdateOverCellularPermission(
171     ErrorPtr* error, bool* out_allowed) {
172   return common_->GetUpdateOverCellularPermission(error, out_allowed);
173 }
174 
GetDurationSinceUpdate(ErrorPtr * error,int64_t * out_usec_wallclock)175 bool DBusUpdateEngineService::GetDurationSinceUpdate(
176     ErrorPtr* error, int64_t* out_usec_wallclock) {
177   return common_->GetDurationSinceUpdate(error, out_usec_wallclock);
178 }
179 
GetPrevVersion(ErrorPtr * error,string * out_prev_version)180 bool DBusUpdateEngineService::GetPrevVersion(ErrorPtr* error,
181                                              string* out_prev_version) {
182   return common_->GetPrevVersion(error, out_prev_version);
183 }
184 
GetRollbackPartition(ErrorPtr * error,string * out_rollback_partition_name)185 bool DBusUpdateEngineService::GetRollbackPartition(
186     ErrorPtr* error, string* out_rollback_partition_name) {
187   return common_->GetRollbackPartition(error, out_rollback_partition_name);
188 }
189 
GetLastAttemptError(ErrorPtr * error,int32_t * out_last_attempt_error)190 bool DBusUpdateEngineService::GetLastAttemptError(
191     ErrorPtr* error, int32_t* out_last_attempt_error) {
192   return common_->GetLastAttemptError(error, out_last_attempt_error);
193 }
194 
UpdateEngineAdaptor(SystemState * system_state)195 UpdateEngineAdaptor::UpdateEngineAdaptor(SystemState* system_state)
196     : org::chromium::UpdateEngineInterfaceAdaptor(&dbus_service_),
197       bus_(DBusConnection::Get()->GetDBus()),
198       dbus_service_(system_state),
199       dbus_object_(nullptr,
200                    bus_,
201                    dbus::ObjectPath(update_engine::kUpdateEngineServicePath)) {}
202 
RegisterAsync(const base::Callback<void (bool)> & completion_callback)203 void UpdateEngineAdaptor::RegisterAsync(
204     const base::Callback<void(bool)>& completion_callback) {
205   RegisterWithDBusObject(&dbus_object_);
206   dbus_object_.RegisterAsync(completion_callback);
207 }
208 
RequestOwnership()209 bool UpdateEngineAdaptor::RequestOwnership() {
210   return bus_->RequestOwnershipAndBlock(update_engine::kUpdateEngineServiceName,
211                                         dbus::Bus::REQUIRE_PRIMARY);
212 }
213 
SendStatusUpdate(const UpdateEngineStatus & update_engine_status)214 void UpdateEngineAdaptor::SendStatusUpdate(
215     const UpdateEngineStatus& update_engine_status) {
216   StatusResult status;
217   ConvertToStatusResult(update_engine_status, &status);
218 
219   // Send |StatusUpdateAdvanced| signal.
220   SendStatusUpdateAdvancedSignal(status);
221 }
222 
223 }  // namespace chromeos_update_engine
224