client_dbus.cc revision 194414134a954d921c8063610304c4fd21f19df2
116daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley//
216daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley// Copyright (C) 2015 The Android Open Source Project
316daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley//
416daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley// Licensed under the Apache License, Version 2.0 (the "License");
516daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley// you may not use this file except in compliance with the License.
616daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley// You may obtain a copy of the License at
716daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley//
816daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley//      http://www.apache.org/licenses/LICENSE-2.0
916daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley//
1016daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley// Unless required by applicable law or agreed to in writing, software
1116daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley// distributed under the License is distributed on an "AS IS" BASIS,
1216daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1316daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley// See the License for the specific language governing permissions and
1416daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley// limitations under the License.
1516daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley//
1616daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley
1716daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley#include "update_engine/client_library/client_impl.h"
1816daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley
19194414134a954d921c8063610304c4fd21f19df2Casey Dahlin#include <base/message_loop/message_loop.h>
20194414134a954d921c8063610304c4fd21f19df2Casey Dahlin
2116daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley#include <dbus/bus.h>
2216daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley#include <update_engine/dbus-constants.h>
2316daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley
2416daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley#include "update_engine/update_status_utils.h"
2516daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley
2616daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wileyusing chromeos_update_engine::StringToUpdateStatus;
27194414134a954d921c8063610304c4fd21f19df2Casey Dahlinusing std::string;
2816daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wileyusing dbus::Bus;
2916daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wileyusing org::chromium::UpdateEngineInterfaceProxy;
3016daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley
3116daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wileynamespace update_engine {
3216daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wileynamespace internal {
3316daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley
34194414134a954d921c8063610304c4fd21f19df2Casey Dahlinbool UpdateEngineClientImpl::Init() {
3516daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley  Bus::Options options;
3616daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley  options.bus_type = Bus::SYSTEM;
3716daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley  scoped_refptr<Bus> bus{new Bus{options}};
38194414134a954d921c8063610304c4fd21f19df2Casey Dahlin
39194414134a954d921c8063610304c4fd21f19df2Casey Dahlin  if (!bus->Connect()) return false;
40194414134a954d921c8063610304c4fd21f19df2Casey Dahlin
4116daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley  proxy_.reset(new UpdateEngineInterfaceProxy{bus});
42194414134a954d921c8063610304c4fd21f19df2Casey Dahlin  return true;
4316daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley}
4416daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley
4516daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wileybool UpdateEngineClientImpl::AttemptUpdate(const string& in_app_version,
4616daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley                                           const string& in_omaha_url,
4716daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley                                           bool at_user_request) {
4816daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley  return proxy_->AttemptUpdateWithFlags(
4997c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin      in_app_version, in_omaha_url,
5097c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin      (at_user_request) ? 0 : kAttemptUpdateFlagNonInteractive, nullptr);
5116daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley}
5216daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley
5316daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wileybool UpdateEngineClientImpl::GetStatus(int64_t* out_last_checked_time,
5416daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley                                       double* out_progress,
5516daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley                                       UpdateStatus* out_update_status,
5616daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley                                       string* out_new_version,
57194414134a954d921c8063610304c4fd21f19df2Casey Dahlin                                       int64_t* out_new_size) const {
5816daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley  string status_as_string;
5997c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  const bool success =
6097c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin      proxy_->GetStatus(out_last_checked_time, out_progress, &status_as_string,
6197c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin                        out_new_version, out_new_size, nullptr);
6216daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley  if (!success) {
6316daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley    return false;
6416daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley  }
6516daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley
6616daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley  return StringToUpdateStatus(status_as_string, out_update_status);
6716daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley}
6816daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley
69ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlinbool UpdateEngineClientImpl::SetUpdateOverCellularPermission(bool allowed) {
70ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin  return proxy_->SetUpdateOverCellularPermission(allowed, nullptr);
71ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin}
72ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin
73194414134a954d921c8063610304c4fd21f19df2Casey Dahlinbool UpdateEngineClientImpl::GetUpdateOverCellularPermission(bool* allowed) const {
74ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin  return proxy_->GetUpdateOverCellularPermission(allowed, nullptr);
75ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin}
76ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin
77ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlinbool UpdateEngineClientImpl::SetP2PUpdatePermission(bool enabled) {
78ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin  return proxy_->SetP2PUpdatePermission(enabled, nullptr);
79ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin}
80ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin
81194414134a954d921c8063610304c4fd21f19df2Casey Dahlinbool UpdateEngineClientImpl::GetP2PUpdatePermission(bool* enabled) const {
82ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin  return proxy_->GetP2PUpdatePermission(enabled, nullptr);
83ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin}
84ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin
85ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlinbool UpdateEngineClientImpl::Rollback(bool powerwash) {
86ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin  return proxy_->AttemptRollback(powerwash, nullptr);
87ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin}
88ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin
89194414134a954d921c8063610304c4fd21f19df2Casey Dahlinbool UpdateEngineClientImpl::GetRollbackPartition(string* rollback_partition) const {
90ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin  return proxy_->GetRollbackPartition(rollback_partition, nullptr);
91ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin}
92ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin
93194414134a954d921c8063610304c4fd21f19df2Casey Dahlinbool UpdateEngineClientImpl::GetPrevVersion(string* prev_version) const {
94ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin  return proxy_->GetPrevVersion(prev_version, nullptr);
95ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin}
96ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin
97ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlinvoid UpdateEngineClientImpl::RebootIfNeeded() {
98ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin  bool ret = proxy_->RebootIfNeeded(nullptr);
99ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin  if (!ret) {
100ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin    // Reboot error code doesn't necessarily mean that a reboot
101ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin    // failed. For example, D-Bus may be shutdown before we receive the
102ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin    // result.
103ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin    LOG(INFO) << "RebootIfNeeded() failure ignored.";
104ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin  }
105ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin}
106ef361136cea1e423dc462d2b347923ae60b2cf08Casey Dahlin
107e844c1a5604765bbddafa7e05f1143a27f136747Casey Dahlinbool UpdateEngineClientImpl::ResetStatus() {
108e844c1a5604765bbddafa7e05f1143a27f136747Casey Dahlin  return proxy_->ResetStatus(nullptr);
109e844c1a5604765bbddafa7e05f1143a27f136747Casey Dahlin}
110e844c1a5604765bbddafa7e05f1143a27f136747Casey Dahlin
11197c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlinvoid UpdateEngineClientImpl::StatusUpdateHandlerRegistered(
11297c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin    StatusUpdateHandler* handler, const std::string& interface,
113194414134a954d921c8063610304c4fd21f19df2Casey Dahlin    const std::string& signal_name, bool success) const {
11497c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  if (!success) {
11597c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin    handler->IPCError("Could not connect to" + signal_name);
11697c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin    return;
11797c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  }
11897c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin
11997c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  int64_t last_checked_time;
12097c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  double progress;
12197c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  UpdateStatus update_status;
12297c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  string new_version;
12397c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  int64_t new_size;
12497c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin
12597c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  if (GetStatus(&last_checked_time, &progress, &update_status, &new_version,
12697c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin                &new_size)) {
12797c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin    handler->HandleStatusUpdate(last_checked_time, progress, update_status,
12897c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin                                new_version, new_size);
12997c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin    return;
13097c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  }
13197c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin
13297c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  handler->IPCError("Could not query current status");
13397c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin}
13497c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin
13597c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlinvoid UpdateEngineClientImpl::RunStatusUpdateHandler(
13697c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin    StatusUpdateHandler* h, int64_t last_checked_time, double progress,
13797c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin    const std::string& current_operation, const std::string& new_version,
13897c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin    int64_t new_size) {
13997c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  UpdateStatus status;
14097c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  StringToUpdateStatus(current_operation, &status);
14197c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin
14297c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  h->HandleStatusUpdate(last_checked_time, progress, status, new_version,
14397c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin                        new_size);
14497c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin}
14597c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin
14697c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlinvoid UpdateEngineClientImpl::RegisterStatusUpdateHandler(
14797c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin    StatusUpdateHandler* handler) {
148194414134a954d921c8063610304c4fd21f19df2Casey Dahlin  if (!base::MessageLoopForIO::current()) {
149194414134a954d921c8063610304c4fd21f19df2Casey Dahlin    LOG(FATAL) << "Cannot get UpdateEngineClient outside of message loop.";
150194414134a954d921c8063610304c4fd21f19df2Casey Dahlin    return;
151194414134a954d921c8063610304c4fd21f19df2Casey Dahlin  }
152194414134a954d921c8063610304c4fd21f19df2Casey Dahlin
15397c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  proxy_->RegisterStatusUpdateSignalHandler(
15497c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin      base::Bind(&UpdateEngineClientImpl::RunStatusUpdateHandler,
15597c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin                 base::Unretained(this), base::Unretained(handler)),
15697c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin      base::Bind(&UpdateEngineClientImpl::StatusUpdateHandlerRegistered,
15797c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin                 base::Unretained(this), base::Unretained(handler)));
15897c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin}
15997c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin
16087ab88e35fac5d3ded1c70c8911d307ec47a7db9Casey Dahlinbool UpdateEngineClientImpl::SetTargetChannel(const string& in_target_channel,
16187ab88e35fac5d3ded1c70c8911d307ec47a7db9Casey Dahlin                                              bool allow_powerwash) {
16297c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  return proxy_->SetChannel(in_target_channel, allow_powerwash, nullptr);
16316daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley}
16416daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley
165194414134a954d921c8063610304c4fd21f19df2Casey Dahlinbool UpdateEngineClientImpl::GetTargetChannel(string* out_channel) const {
16697c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  return proxy_->GetChannel(false,  // Get the target channel.
16797c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin                            out_channel, nullptr);
16816daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley}
16916daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley
170194414134a954d921c8063610304c4fd21f19df2Casey Dahlinbool UpdateEngineClientImpl::GetChannel(string* out_channel) const {
17197c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin  return proxy_->GetChannel(true,  // Get the current channel.
17297c870518ce1149b32966d98ebfe93f3eb5d1fefCasey Dahlin                            out_channel, nullptr);
17316daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley}
17416daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley
17516daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley}  // namespace internal
17616daa08470beb5021b85618f1b3ee214d89e59a1Christopher Wiley}  // namespace update_engine
177