blob: 8d30f7f1d1c403a0e1353b9d6759e01d7689c967 [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"
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/utils.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070028#include "update_engine/cros/omaha_request_params.h"
Alex Deymo63784a52014-05-28 10:46:14 -070029#include "update_engine/update_manager/generic_variables.h"
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070030#include "update_engine/update_manager/variable.h"
Alex Deymobd04b142014-03-18 15:00:05 -070031
32using std::string;
Alex Deymobd04b142014-03-18 15:00:05 -070033
Alex Deymo63784a52014-05-28 10:46:14 -070034namespace chromeos_update_manager {
Alex Deymobd04b142014-03-18 15:00:05 -070035
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070036namespace {
37
Alex Deymo857ded12016-05-12 19:06:21 -070038// The maximum number of consecutive failures before returning the default
39// constructor value for T instead of failure.
40const int kRetryPollVariableMaxRetry = 5;
41
42// The polling interval to be used whenever GetValue() returns an error.
43const int kRetryPollVariableRetryIntervalSeconds = 5 * 60;
44
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070045// The RetryPollVariable variable is a polling variable that allows the function
46// returning the value to fail a few times and shortens the polling rate when
47// that happens.
48template <typename T>
49class RetryPollVariable : public Variable<T> {
50 public:
51 RetryPollVariable(const string& name,
52 const base::TimeDelta poll_interval,
53 base::Callback<bool(T* res)> func)
54 : Variable<T>(name, poll_interval),
55 func_(func),
56 base_interval_(poll_interval) {
Alex Deymo857ded12016-05-12 19:06:21 -070057 DCHECK_LT(kRetryPollVariableRetryIntervalSeconds,
58 base_interval_.InSeconds());
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070059 }
60
61 protected:
62 // Variable override.
63 const T* GetValue(base::TimeDelta /* timeout */,
64 string* /* errmsg */) override {
65 std::unique_ptr<T> result(new T());
66 if (!func_.Run(result.get())) {
Alex Deymo857ded12016-05-12 19:06:21 -070067 if (failed_attempts_ >= kRetryPollVariableMaxRetry) {
Miriam Polzer213e2be2020-05-29 10:25:09 +020068 // Give up on the retries and set back the desired polling interval.
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070069 this->SetPollInterval(base_interval_);
Miriam Polzer213e2be2020-05-29 10:25:09 +020070 // Release the result instead of returning a |nullptr| to indicate that
71 // the result could not be fetched.
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070072 return result.release();
73 }
74 this->SetPollInterval(
Alex Deymo857ded12016-05-12 19:06:21 -070075 base::TimeDelta::FromSeconds(kRetryPollVariableRetryIntervalSeconds));
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070076 failed_attempts_++;
77 return nullptr;
78 }
79 failed_attempts_ = 0;
80 this->SetPollInterval(base_interval_);
81 return result.release();
82 }
83
84 private:
85 // The function to be called, stored as a base::Callback.
86 base::Callback<bool(T*)> func_;
87
88 // The desired polling interval when |func_| works and returns true.
89 base::TimeDelta base_interval_;
90
91 // The number of consecutive failed attempts made.
92 int failed_attempts_ = 0;
93
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070094 DISALLOW_COPY_AND_ASSIGN(RetryPollVariable);
95};
96
97} // namespace
98
Alex Deymo42c30c32014-04-24 18:41:18 -070099bool RealSystemProvider::Init() {
Amin Hassani4b717432019-01-14 16:24:20 -0800100 var_is_normal_boot_mode_.reset(new ConstCopyVariable<bool>(
Miriam Polzer8db52492020-09-17 14:06:46 +0200101 "is_normal_boot_mode", system_state_->hardware()->IsNormalBootMode()));
Alex Deymobd04b142014-03-18 15:00:05 -0700102
Amin Hassani4b717432019-01-14 16:24:20 -0800103 var_is_official_build_.reset(new ConstCopyVariable<bool>(
Miriam Polzer8db52492020-09-17 14:06:46 +0200104 "is_official_build", system_state_->hardware()->IsOfficialBuild()));
Alex Deymobd04b142014-03-18 15:00:05 -0700105
Amin Hassani4b717432019-01-14 16:24:20 -0800106 var_is_oobe_complete_.reset(new CallCopyVariable<bool>(
107 "is_oobe_complete",
108 base::Bind(&chromeos_update_engine::HardwareInterface::IsOOBEComplete,
Miriam Polzer8db52492020-09-17 14:06:46 +0200109 base::Unretained(system_state_->hardware()),
Amin Hassani4b717432019-01-14 16:24:20 -0800110 nullptr)));
Gilad Arnold48e13612014-05-16 10:18:05 -0700111
Amin Hassani4b717432019-01-14 16:24:20 -0800112 var_num_slots_.reset(new ConstCopyVariable<unsigned int>(
Miriam Polzer8db52492020-09-17 14:06:46 +0200113 "num_slots", system_state_->boot_control()->GetNumSlots()));
Gilad Arnoldbfc44f72014-07-09 14:41:39 -0700114
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700115 var_kiosk_required_platform_version_.reset(new RetryPollVariable<string>(
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800116 "kiosk_required_platform_version",
117 base::TimeDelta::FromHours(5), // Same as Chrome's CWS poll.
118 base::Bind(&RealSystemProvider::GetKioskAppRequiredPlatformVersion,
119 base::Unretained(this))));
120
Miriam Polzer8db52492020-09-17 14:06:46 +0200121 var_chromeos_version_.reset(new ConstCopyVariable<base::Version>(
122 "chromeos_version",
123 base::Version(system_state_->request_params()->app_version())));
124
Alex Deymobd04b142014-03-18 15:00:05 -0700125 return true;
126}
127
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700128bool RealSystemProvider::GetKioskAppRequiredPlatformVersion(
129 string* required_platform_version) {
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800130 brillo::ErrorPtr error;
Daniel Erat04df23a2018-03-29 17:55:35 -0700131 if (!kiosk_app_proxy_->GetRequiredPlatformVersion(required_platform_version,
132 &error)) {
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800133 LOG(WARNING) << "Failed to get kiosk required platform version";
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700134 required_platform_version->clear();
135 return false;
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800136 }
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800137
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700138 return true;
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800139}
140
Alex Deymo63784a52014-05-28 10:46:14 -0700141} // namespace chromeos_update_manager