blob: 9b5bc02ec0e2129a51f0a72d49c983fb9cbcb8a3 [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>
Amin Hassani47186292017-08-01 15:03:08 -070023#if USE_CHROME_KIOSK_APP
Daniel Erat04df23a2018-03-29 17:55:35 -070024#include <kiosk-app/dbus-proxies.h>
Amin Hassani47186292017-08-01 15:03:08 -070025#endif // USE_CHROME_KIOSK_APP
Alex Deymobd04b142014-03-18 15:00:05 -070026
Miriam Polzer8db52492020-09-17 14:06:46 +020027#include "update_engine/common/boot_control_interface.h"
28#include "update_engine/common/hardware_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/utils.h"
Miriam Polzer8db52492020-09-17 14:06:46 +020030#include "update_engine/omaha_request_params.h"
Alex Deymo63784a52014-05-28 10:46:14 -070031#include "update_engine/update_manager/generic_variables.h"
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070032#include "update_engine/update_manager/variable.h"
Alex Deymobd04b142014-03-18 15:00:05 -070033
34using 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>(
Miriam Polzer8db52492020-09-17 14:06:46 +0200103 "is_normal_boot_mode", system_state_->hardware()->IsNormalBootMode()));
Alex Deymobd04b142014-03-18 15:00:05 -0700104
Amin Hassani4b717432019-01-14 16:24:20 -0800105 var_is_official_build_.reset(new ConstCopyVariable<bool>(
Miriam Polzer8db52492020-09-17 14:06:46 +0200106 "is_official_build", system_state_->hardware()->IsOfficialBuild()));
Alex Deymobd04b142014-03-18 15:00:05 -0700107
Amin Hassani4b717432019-01-14 16:24:20 -0800108 var_is_oobe_complete_.reset(new CallCopyVariable<bool>(
109 "is_oobe_complete",
110 base::Bind(&chromeos_update_engine::HardwareInterface::IsOOBEComplete,
Miriam Polzer8db52492020-09-17 14:06:46 +0200111 base::Unretained(system_state_->hardware()),
Amin Hassani4b717432019-01-14 16:24:20 -0800112 nullptr)));
Gilad Arnold48e13612014-05-16 10:18:05 -0700113
Amin Hassani4b717432019-01-14 16:24:20 -0800114 var_num_slots_.reset(new ConstCopyVariable<unsigned int>(
Miriam Polzer8db52492020-09-17 14:06:46 +0200115 "num_slots", system_state_->boot_control()->GetNumSlots()));
Gilad Arnoldbfc44f72014-07-09 14:41:39 -0700116
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700117 var_kiosk_required_platform_version_.reset(new RetryPollVariable<string>(
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800118 "kiosk_required_platform_version",
119 base::TimeDelta::FromHours(5), // Same as Chrome's CWS poll.
120 base::Bind(&RealSystemProvider::GetKioskAppRequiredPlatformVersion,
121 base::Unretained(this))));
122
Miriam Polzer8db52492020-09-17 14:06:46 +0200123 var_chromeos_version_.reset(new ConstCopyVariable<base::Version>(
124 "chromeos_version",
125 base::Version(system_state_->request_params()->app_version())));
126
Alex Deymobd04b142014-03-18 15:00:05 -0700127 return true;
128}
129
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700130bool RealSystemProvider::GetKioskAppRequiredPlatformVersion(
131 string* required_platform_version) {
Amin Hassani47186292017-08-01 15:03:08 -0700132#if USE_CHROME_KIOSK_APP
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 }
Amin Hassani47186292017-08-01 15:03:08 -0700140#endif // USE_CHROME_KIOSK_APP
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800141
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700142 return true;
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800143}
144
Alex Deymo63784a52014-05-28 10:46:14 -0700145} // namespace chromeos_update_manager