blob: 247f8f047bcd4c9b7c00adbf956ce6ab74c8e5eb [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
19#include <dbus/bus.h>
20#include <update_engine/dbus-constants.h>
21
22#include "update_engine/update_status_utils.h"
23
24using chromeos_update_engine::StringToUpdateStatus;
25using dbus::Bus;
26using org::chromium::UpdateEngineInterfaceProxy;
27using std::string;
28
29namespace update_engine {
30namespace internal {
31
32UpdateEngineClientImpl::UpdateEngineClientImpl() {
33 Bus::Options options;
34 options.bus_type = Bus::SYSTEM;
35 scoped_refptr<Bus> bus{new Bus{options}};
36 proxy_.reset(new UpdateEngineInterfaceProxy{bus});
37}
38
39bool UpdateEngineClientImpl::AttemptUpdate(const string& in_app_version,
40 const string& in_omaha_url,
41 bool at_user_request) {
42 return proxy_->AttemptUpdateWithFlags(
Casey Dahlin97c87052016-01-06 14:33:55 -080043 in_app_version, in_omaha_url,
44 (at_user_request) ? 0 : kAttemptUpdateFlagNonInteractive, nullptr);
Christopher Wiley16daa082015-10-01 17:18:40 -070045}
46
47bool UpdateEngineClientImpl::GetStatus(int64_t* out_last_checked_time,
48 double* out_progress,
49 UpdateStatus* out_update_status,
50 string* out_new_version,
51 int64_t* out_new_size) {
52 string status_as_string;
Casey Dahlin97c87052016-01-06 14:33:55 -080053 const bool success =
54 proxy_->GetStatus(out_last_checked_time, out_progress, &status_as_string,
55 out_new_version, out_new_size, nullptr);
Christopher Wiley16daa082015-10-01 17:18:40 -070056 if (!success) {
57 return false;
58 }
59
60 return StringToUpdateStatus(status_as_string, out_update_status);
61}
62
Casey Dahlinef361132015-12-17 13:02:37 -080063bool UpdateEngineClientImpl::SetUpdateOverCellularPermission(bool allowed) {
64 return proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
65}
66
Casey Dahlin97c87052016-01-06 14:33:55 -080067bool UpdateEngineClientImpl::GetUpdateOverCellularPermission(bool* allowed) {
Casey Dahlinef361132015-12-17 13:02:37 -080068 return proxy_->GetUpdateOverCellularPermission(allowed, nullptr);
69}
70
71bool UpdateEngineClientImpl::SetP2PUpdatePermission(bool enabled) {
72 return proxy_->SetP2PUpdatePermission(enabled, nullptr);
73}
74
75bool UpdateEngineClientImpl::GetP2PUpdatePermission(bool* enabled) {
76 return proxy_->GetP2PUpdatePermission(enabled, nullptr);
77}
78
79bool UpdateEngineClientImpl::Rollback(bool powerwash) {
80 return proxy_->AttemptRollback(powerwash, nullptr);
81}
82
83bool UpdateEngineClientImpl::GetRollbackPartition(string* rollback_partition) {
84 return proxy_->GetRollbackPartition(rollback_partition, nullptr);
85}
86
87bool UpdateEngineClientImpl::GetPrevVersion(string* prev_version) {
88 return proxy_->GetPrevVersion(prev_version, nullptr);
89}
90
91void UpdateEngineClientImpl::RebootIfNeeded() {
92 bool ret = proxy_->RebootIfNeeded(nullptr);
93 if (!ret) {
94 // Reboot error code doesn't necessarily mean that a reboot
95 // failed. For example, D-Bus may be shutdown before we receive the
96 // result.
97 LOG(INFO) << "RebootIfNeeded() failure ignored.";
98 }
99}
100
Casey Dahline844c1a2015-12-16 14:30:58 -0800101bool UpdateEngineClientImpl::ResetStatus() {
102 return proxy_->ResetStatus(nullptr);
103}
104
Casey Dahlin97c87052016-01-06 14:33:55 -0800105void UpdateEngineClientImpl::StatusUpdateHandlerRegistered(
106 StatusUpdateHandler* handler, const std::string& interface,
107 const std::string& signal_name, bool success) {
108 if (!success) {
109 handler->IPCError("Could not connect to" + signal_name);
110 return;
111 }
112
113 int64_t last_checked_time;
114 double progress;
115 UpdateStatus update_status;
116 string new_version;
117 int64_t new_size;
118
119 if (GetStatus(&last_checked_time, &progress, &update_status, &new_version,
120 &new_size)) {
121 handler->HandleStatusUpdate(last_checked_time, progress, update_status,
122 new_version, new_size);
123 return;
124 }
125
126 handler->IPCError("Could not query current status");
127}
128
129void UpdateEngineClientImpl::RunStatusUpdateHandler(
130 StatusUpdateHandler* h, int64_t last_checked_time, double progress,
131 const std::string& current_operation, const std::string& new_version,
132 int64_t new_size) {
133 UpdateStatus status;
134 StringToUpdateStatus(current_operation, &status);
135
136 h->HandleStatusUpdate(last_checked_time, progress, status, new_version,
137 new_size);
138}
139
140void UpdateEngineClientImpl::RegisterStatusUpdateHandler(
141 StatusUpdateHandler* handler) {
142 proxy_->RegisterStatusUpdateSignalHandler(
143 base::Bind(&UpdateEngineClientImpl::RunStatusUpdateHandler,
144 base::Unretained(this), base::Unretained(handler)),
145 base::Bind(&UpdateEngineClientImpl::StatusUpdateHandlerRegistered,
146 base::Unretained(this), base::Unretained(handler)));
147}
148
Casey Dahlin87ab88e2015-12-16 17:58:05 -0800149bool UpdateEngineClientImpl::SetTargetChannel(const string& in_target_channel,
150 bool allow_powerwash) {
Casey Dahlin97c87052016-01-06 14:33:55 -0800151 return proxy_->SetChannel(in_target_channel, allow_powerwash, nullptr);
Christopher Wiley16daa082015-10-01 17:18:40 -0700152}
153
154bool UpdateEngineClientImpl::GetTargetChannel(string* out_channel) {
Casey Dahlin97c87052016-01-06 14:33:55 -0800155 return proxy_->GetChannel(false, // Get the target channel.
156 out_channel, nullptr);
Christopher Wiley16daa082015-10-01 17:18:40 -0700157}
158
159bool UpdateEngineClientImpl::GetChannel(string* out_channel) {
Casey Dahlin97c87052016-01-06 14:33:55 -0800160 return proxy_->GetChannel(true, // Get the current channel.
161 out_channel, nullptr);
Christopher Wiley16daa082015-10-01 17:18:40 -0700162}
163
164} // namespace internal
165} // namespace update_engine