blob: 34397f338e31dafebde9e239be4111b630c621c1 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2014 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//
Alex Deymobd04b142014-03-18 15:00:05 -070016
Alex Deymo63784a52014-05-28 10:46:14 -070017#include "update_engine/update_manager/real_system_provider.h"
Alex Deymobd04b142014-03-18 15:00:05 -070018
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070019#include <base/bind.h>
20#include <base/callback.h>
Alex Deymobd04b142014-03-18 15:00:05 -070021#include <base/logging.h>
Gilad Arnold48e13612014-05-16 10:18:05 -070022#include <base/time/time.h>
Daniel Erat04df23a2018-03-29 17:55:35 -070023#include <kiosk-app/dbus-proxies.h>
Alex Deymobd04b142014-03-18 15:00:05 -070024
Miriam Polzer8db52492020-09-17 14:06:46 +020025#include "update_engine/common/boot_control_interface.h"
26#include "update_engine/common/hardware_interface.h"
Amin Hassani538bd592020-11-04 20:46:08 -080027#include "update_engine/common/system_state.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/utils.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070029#include "update_engine/cros/omaha_request_params.h"
Alex Deymo63784a52014-05-28 10:46:14 -070030#include "update_engine/update_manager/generic_variables.h"
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070031#include "update_engine/update_manager/variable.h"
Alex Deymobd04b142014-03-18 15:00:05 -070032
Amin Hassani538bd592020-11-04 20:46:08 -080033using chromeos_update_engine::SystemState;
Alex Deymobd04b142014-03-18 15:00:05 -070034using std::string;
Alex Deymobd04b142014-03-18 15:00:05 -070035
Alex Deymo63784a52014-05-28 10:46:14 -070036namespace chromeos_update_manager {
Alex Deymobd04b142014-03-18 15:00:05 -070037
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070038namespace {
39
Alex Deymo857ded12016-05-12 19:06:21 -070040// The maximum number of consecutive failures before returning the default
41// constructor value for T instead of failure.
42const int kRetryPollVariableMaxRetry = 5;
43
44// The polling interval to be used whenever GetValue() returns an error.
45const int kRetryPollVariableRetryIntervalSeconds = 5 * 60;
46
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070047// The RetryPollVariable variable is a polling variable that allows the function
48// returning the value to fail a few times and shortens the polling rate when
49// that happens.
50template <typename T>
51class RetryPollVariable : public Variable<T> {
52 public:
53 RetryPollVariable(const string& name,
54 const base::TimeDelta poll_interval,
55 base::Callback<bool(T* res)> func)
56 : Variable<T>(name, poll_interval),
57 func_(func),
58 base_interval_(poll_interval) {
Alex Deymo857ded12016-05-12 19:06:21 -070059 DCHECK_LT(kRetryPollVariableRetryIntervalSeconds,
60 base_interval_.InSeconds());
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070061 }
62
63 protected:
64 // Variable override.
65 const T* GetValue(base::TimeDelta /* timeout */,
66 string* /* errmsg */) override {
67 std::unique_ptr<T> result(new T());
68 if (!func_.Run(result.get())) {
Alex Deymo857ded12016-05-12 19:06:21 -070069 if (failed_attempts_ >= kRetryPollVariableMaxRetry) {
Miriam Polzer213e2be2020-05-29 10:25:09 +020070 // Give up on the retries and set back the desired polling interval.
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070071 this->SetPollInterval(base_interval_);
Miriam Polzer213e2be2020-05-29 10:25:09 +020072 // Release the result instead of returning a |nullptr| to indicate that
73 // the result could not be fetched.
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070074 return result.release();
75 }
76 this->SetPollInterval(
Alex Deymo857ded12016-05-12 19:06:21 -070077 base::TimeDelta::FromSeconds(kRetryPollVariableRetryIntervalSeconds));
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070078 failed_attempts_++;
79 return nullptr;
80 }
81 failed_attempts_ = 0;
82 this->SetPollInterval(base_interval_);
83 return result.release();
84 }
85
86 private:
87 // The function to be called, stored as a base::Callback.
88 base::Callback<bool(T*)> func_;
89
90 // The desired polling interval when |func_| works and returns true.
91 base::TimeDelta base_interval_;
92
93 // The number of consecutive failed attempts made.
94 int failed_attempts_ = 0;
95
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070096 DISALLOW_COPY_AND_ASSIGN(RetryPollVariable);
97};
98
99} // namespace
100
Alex Deymo42c30c32014-04-24 18:41:18 -0700101bool RealSystemProvider::Init() {
Amin Hassani4b717432019-01-14 16:24:20 -0800102 var_is_normal_boot_mode_.reset(new ConstCopyVariable<bool>(
Amin Hassani538bd592020-11-04 20:46:08 -0800103 "is_normal_boot_mode",
104 SystemState::Get()->hardware()->IsNormalBootMode()));
Alex Deymobd04b142014-03-18 15:00:05 -0700105
Amin Hassani4b717432019-01-14 16:24:20 -0800106 var_is_official_build_.reset(new ConstCopyVariable<bool>(
Amin Hassani538bd592020-11-04 20:46:08 -0800107 "is_official_build", SystemState::Get()->hardware()->IsOfficialBuild()));
Alex Deymobd04b142014-03-18 15:00:05 -0700108
Amin Hassani4b717432019-01-14 16:24:20 -0800109 var_is_oobe_complete_.reset(new CallCopyVariable<bool>(
110 "is_oobe_complete",
111 base::Bind(&chromeos_update_engine::HardwareInterface::IsOOBEComplete,
Amin Hassani538bd592020-11-04 20:46:08 -0800112 base::Unretained(SystemState::Get()->hardware()),
Amin Hassani4b717432019-01-14 16:24:20 -0800113 nullptr)));
Gilad Arnold48e13612014-05-16 10:18:05 -0700114
Amin Hassani4b717432019-01-14 16:24:20 -0800115 var_num_slots_.reset(new ConstCopyVariable<unsigned int>(
Amin Hassani538bd592020-11-04 20:46:08 -0800116 "num_slots", SystemState::Get()->boot_control()->GetNumSlots()));
Gilad Arnoldbfc44f72014-07-09 14:41:39 -0700117
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700118 var_kiosk_required_platform_version_.reset(new RetryPollVariable<string>(
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800119 "kiosk_required_platform_version",
120 base::TimeDelta::FromHours(5), // Same as Chrome's CWS poll.
121 base::Bind(&RealSystemProvider::GetKioskAppRequiredPlatformVersion,
122 base::Unretained(this))));
123
Miriam Polzer8db52492020-09-17 14:06:46 +0200124 var_chromeos_version_.reset(new ConstCopyVariable<base::Version>(
125 "chromeos_version",
Amin Hassani538bd592020-11-04 20:46:08 -0800126 base::Version(SystemState::Get()->request_params()->app_version())));
Miriam Polzer8db52492020-09-17 14:06:46 +0200127
Alex Deymobd04b142014-03-18 15:00:05 -0700128 return true;
129}
130
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700131bool RealSystemProvider::GetKioskAppRequiredPlatformVersion(
132 string* required_platform_version) {
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800133 brillo::ErrorPtr error;
Daniel Erat04df23a2018-03-29 17:55:35 -0700134 if (!kiosk_app_proxy_->GetRequiredPlatformVersion(required_platform_version,
135 &error)) {
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800136 LOG(WARNING) << "Failed to get kiosk required platform version";
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700137 required_platform_version->clear();
138 return false;
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800139 }
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800140
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700141 return true;
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800142}
143
Alex Deymo63784a52014-05-28 10:46:14 -0700144} // namespace chromeos_update_manager