blob: 186301fb1e0e2090bf314f65d93079f8078122f1 [file] [log] [blame]
Christopher Wiley16daa082015-10-01 17:18:40 -07001//
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_impl.h"
18
Casey Dahlin19441412016-01-07 14:56:40 -080019#include <base/message_loop/message_loop.h>
20
Christopher Wiley16daa082015-10-01 17:18:40 -070021#include <dbus/bus.h>
22#include <update_engine/dbus-constants.h>
23
24#include "update_engine/update_status_utils.h"
25
26using chromeos_update_engine::StringToUpdateStatus;
Casey Dahlin19441412016-01-07 14:56:40 -080027using std::string;
Christopher Wiley16daa082015-10-01 17:18:40 -070028using dbus::Bus;
29using org::chromium::UpdateEngineInterfaceProxy;
Christopher Wiley16daa082015-10-01 17:18:40 -070030
31namespace update_engine {
32namespace internal {
33
Casey Dahlin19441412016-01-07 14:56:40 -080034bool UpdateEngineClientImpl::Init() {
Christopher Wiley16daa082015-10-01 17:18:40 -070035 Bus::Options options;
36 options.bus_type = Bus::SYSTEM;
37 scoped_refptr<Bus> bus{new Bus{options}};
Casey Dahlin19441412016-01-07 14:56:40 -080038
39 if (!bus->Connect()) return false;
40
Christopher Wiley16daa082015-10-01 17:18:40 -070041 proxy_.reset(new UpdateEngineInterfaceProxy{bus});
Casey Dahlin19441412016-01-07 14:56:40 -080042 return true;
Christopher Wiley16daa082015-10-01 17:18:40 -070043}
44
45bool UpdateEngineClientImpl::AttemptUpdate(const string& in_app_version,
46 const string& in_omaha_url,
47 bool at_user_request) {
48 return proxy_->AttemptUpdateWithFlags(
Casey Dahlin97c87052016-01-06 14:33:55 -080049 in_app_version, in_omaha_url,
50 (at_user_request) ? 0 : kAttemptUpdateFlagNonInteractive, nullptr);
Christopher Wiley16daa082015-10-01 17:18:40 -070051}
52
53bool UpdateEngineClientImpl::GetStatus(int64_t* out_last_checked_time,
54 double* out_progress,
55 UpdateStatus* out_update_status,
56 string* out_new_version,
Casey Dahlin19441412016-01-07 14:56:40 -080057 int64_t* out_new_size) const {
Christopher Wiley16daa082015-10-01 17:18:40 -070058 string status_as_string;
Casey Dahlin97c87052016-01-06 14:33:55 -080059 const bool success =
60 proxy_->GetStatus(out_last_checked_time, out_progress, &status_as_string,
61 out_new_version, out_new_size, nullptr);
Christopher Wiley16daa082015-10-01 17:18:40 -070062 if (!success) {
63 return false;
64 }
65
66 return StringToUpdateStatus(status_as_string, out_update_status);
67}
68
Casey Dahlinef361132015-12-17 13:02:37 -080069bool UpdateEngineClientImpl::SetUpdateOverCellularPermission(bool allowed) {
70 return proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
71}
72
Casey Dahlin19441412016-01-07 14:56:40 -080073bool UpdateEngineClientImpl::GetUpdateOverCellularPermission(bool* allowed) const {
Casey Dahlinef361132015-12-17 13:02:37 -080074 return proxy_->GetUpdateOverCellularPermission(allowed, nullptr);
75}
76
77bool UpdateEngineClientImpl::SetP2PUpdatePermission(bool enabled) {
78 return proxy_->SetP2PUpdatePermission(enabled, nullptr);
79}
80
Casey Dahlin19441412016-01-07 14:56:40 -080081bool UpdateEngineClientImpl::GetP2PUpdatePermission(bool* enabled) const {
Casey Dahlinef361132015-12-17 13:02:37 -080082 return proxy_->GetP2PUpdatePermission(enabled, nullptr);
83}
84
85bool UpdateEngineClientImpl::Rollback(bool powerwash) {
86 return proxy_->AttemptRollback(powerwash, nullptr);
87}
88
Casey Dahlin19441412016-01-07 14:56:40 -080089bool UpdateEngineClientImpl::GetRollbackPartition(string* rollback_partition) const {
Casey Dahlinef361132015-12-17 13:02:37 -080090 return proxy_->GetRollbackPartition(rollback_partition, nullptr);
91}
92
Casey Dahlin19441412016-01-07 14:56:40 -080093bool UpdateEngineClientImpl::GetPrevVersion(string* prev_version) const {
Casey Dahlinef361132015-12-17 13:02:37 -080094 return proxy_->GetPrevVersion(prev_version, nullptr);
95}
96
97void UpdateEngineClientImpl::RebootIfNeeded() {
98 bool ret = proxy_->RebootIfNeeded(nullptr);
99 if (!ret) {
100 // Reboot error code doesn't necessarily mean that a reboot
101 // failed. For example, D-Bus may be shutdown before we receive the
102 // result.
103 LOG(INFO) << "RebootIfNeeded() failure ignored.";
104 }
105}
106
Casey Dahline844c1a2015-12-16 14:30:58 -0800107bool UpdateEngineClientImpl::ResetStatus() {
108 return proxy_->ResetStatus(nullptr);
109}
110
Casey Dahlin97c87052016-01-06 14:33:55 -0800111void UpdateEngineClientImpl::StatusUpdateHandlerRegistered(
112 StatusUpdateHandler* handler, const std::string& interface,
Casey Dahlin19441412016-01-07 14:56:40 -0800113 const std::string& signal_name, bool success) const {
Casey Dahlin97c87052016-01-06 14:33:55 -0800114 if (!success) {
115 handler->IPCError("Could not connect to" + signal_name);
116 return;
117 }
118
119 int64_t last_checked_time;
120 double progress;
121 UpdateStatus update_status;
122 string new_version;
123 int64_t new_size;
124
125 if (GetStatus(&last_checked_time, &progress, &update_status, &new_version,
126 &new_size)) {
127 handler->HandleStatusUpdate(last_checked_time, progress, update_status,
128 new_version, new_size);
129 return;
130 }
131
132 handler->IPCError("Could not query current status");
133}
134
135void UpdateEngineClientImpl::RunStatusUpdateHandler(
136 StatusUpdateHandler* h, int64_t last_checked_time, double progress,
137 const std::string& current_operation, const std::string& new_version,
138 int64_t new_size) {
139 UpdateStatus status;
140 StringToUpdateStatus(current_operation, &status);
141
142 h->HandleStatusUpdate(last_checked_time, progress, status, new_version,
143 new_size);
144}
145
146void UpdateEngineClientImpl::RegisterStatusUpdateHandler(
147 StatusUpdateHandler* handler) {
Casey Dahlin19441412016-01-07 14:56:40 -0800148 if (!base::MessageLoopForIO::current()) {
149 LOG(FATAL) << "Cannot get UpdateEngineClient outside of message loop.";
150 return;
151 }
152
Casey Dahlin97c87052016-01-06 14:33:55 -0800153 proxy_->RegisterStatusUpdateSignalHandler(
154 base::Bind(&UpdateEngineClientImpl::RunStatusUpdateHandler,
155 base::Unretained(this), base::Unretained(handler)),
156 base::Bind(&UpdateEngineClientImpl::StatusUpdateHandlerRegistered,
157 base::Unretained(this), base::Unretained(handler)));
158}
159
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800160bool UpdateEngineClientImpl::SetTargetChannel(const string& in_target_channel,
161 bool allow_powerwash) {
Casey Dahlin97c87052016-01-06 14:33:55 -0800162 return proxy_->SetChannel(in_target_channel, allow_powerwash, nullptr);
Christopher Wiley16daa082015-10-01 17:18:40 -0700163}
164
Casey Dahlin19441412016-01-07 14:56:40 -0800165bool UpdateEngineClientImpl::GetTargetChannel(string* out_channel) const {
Casey Dahlin97c87052016-01-06 14:33:55 -0800166 return proxy_->GetChannel(false, // Get the target channel.
167 out_channel, nullptr);
Christopher Wiley16daa082015-10-01 17:18:40 -0700168}
169
Casey Dahlin19441412016-01-07 14:56:40 -0800170bool UpdateEngineClientImpl::GetChannel(string* out_channel) const {
Casey Dahlin97c87052016-01-06 14:33:55 -0800171 return proxy_->GetChannel(true, // Get the current channel.
172 out_channel, nullptr);
Christopher Wiley16daa082015-10-01 17:18:40 -0700173}
174
175} // namespace internal
176} // namespace update_engine