blob: a900071ab4ff586038213878b599ce0bef7481fa [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
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/utils.h"
Alex Deymo63784a52014-05-28 10:46:14 -070028#include "update_engine/update_manager/generic_variables.h"
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070029#include "update_engine/update_manager/variable.h"
Alex Deymobd04b142014-03-18 15:00:05 -070030
31using std::string;
Alex Deymobd04b142014-03-18 15:00:05 -070032
Alex Deymo63784a52014-05-28 10:46:14 -070033namespace chromeos_update_manager {
Alex Deymobd04b142014-03-18 15:00:05 -070034
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070035namespace {
36
Alex Deymo857ded12016-05-12 19:06:21 -070037// The maximum number of consecutive failures before returning the default
38// constructor value for T instead of failure.
39const int kRetryPollVariableMaxRetry = 5;
40
41// The polling interval to be used whenever GetValue() returns an error.
42const int kRetryPollVariableRetryIntervalSeconds = 5 * 60;
43
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070044// The RetryPollVariable variable is a polling variable that allows the function
45// returning the value to fail a few times and shortens the polling rate when
46// that happens.
47template <typename T>
48class RetryPollVariable : public Variable<T> {
49 public:
50 RetryPollVariable(const string& name,
51 const base::TimeDelta poll_interval,
52 base::Callback<bool(T* res)> func)
53 : Variable<T>(name, poll_interval),
54 func_(func),
55 base_interval_(poll_interval) {
Alex Deymo857ded12016-05-12 19:06:21 -070056 DCHECK_LT(kRetryPollVariableRetryIntervalSeconds,
57 base_interval_.InSeconds());
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070058 }
59
60 protected:
61 // Variable override.
62 const T* GetValue(base::TimeDelta /* timeout */,
63 string* /* errmsg */) override {
64 std::unique_ptr<T> result(new T());
65 if (!func_.Run(result.get())) {
Alex Deymo857ded12016-05-12 19:06:21 -070066 if (failed_attempts_ >= kRetryPollVariableMaxRetry) {
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070067 // Give up on the retries, set back the desired polling interval and
68 // return the default.
69 this->SetPollInterval(base_interval_);
70 return result.release();
71 }
72 this->SetPollInterval(
Alex Deymo857ded12016-05-12 19:06:21 -070073 base::TimeDelta::FromSeconds(kRetryPollVariableRetryIntervalSeconds));
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070074 failed_attempts_++;
75 return nullptr;
76 }
77 failed_attempts_ = 0;
78 this->SetPollInterval(base_interval_);
79 return result.release();
80 }
81
82 private:
83 // The function to be called, stored as a base::Callback.
84 base::Callback<bool(T*)> func_;
85
86 // The desired polling interval when |func_| works and returns true.
87 base::TimeDelta base_interval_;
88
89 // The number of consecutive failed attempts made.
90 int failed_attempts_ = 0;
91
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070092 DISALLOW_COPY_AND_ASSIGN(RetryPollVariable);
93};
94
95} // namespace
96
Alex Deymo42c30c32014-04-24 18:41:18 -070097bool RealSystemProvider::Init() {
Amin Hassani4b717432019-01-14 16:24:20 -080098 var_is_normal_boot_mode_.reset(new ConstCopyVariable<bool>(
99 "is_normal_boot_mode", hardware_->IsNormalBootMode()));
Alex Deymobd04b142014-03-18 15:00:05 -0700100
Amin Hassani4b717432019-01-14 16:24:20 -0800101 var_is_official_build_.reset(new ConstCopyVariable<bool>(
102 "is_official_build", hardware_->IsOfficialBuild()));
Alex Deymobd04b142014-03-18 15:00:05 -0700103
Amin Hassani4b717432019-01-14 16:24:20 -0800104 var_is_oobe_complete_.reset(new CallCopyVariable<bool>(
105 "is_oobe_complete",
106 base::Bind(&chromeos_update_engine::HardwareInterface::IsOOBEComplete,
107 base::Unretained(hardware_),
108 nullptr)));
Gilad Arnold48e13612014-05-16 10:18:05 -0700109
Amin Hassani4b717432019-01-14 16:24:20 -0800110 var_num_slots_.reset(new ConstCopyVariable<unsigned int>(
111 "num_slots", boot_control_->GetNumSlots()));
Gilad Arnoldbfc44f72014-07-09 14:41:39 -0700112
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700113 var_kiosk_required_platform_version_.reset(new RetryPollVariable<string>(
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800114 "kiosk_required_platform_version",
115 base::TimeDelta::FromHours(5), // Same as Chrome's CWS poll.
116 base::Bind(&RealSystemProvider::GetKioskAppRequiredPlatformVersion,
117 base::Unretained(this))));
118
Alex Deymobd04b142014-03-18 15:00:05 -0700119 return true;
120}
121
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700122bool RealSystemProvider::GetKioskAppRequiredPlatformVersion(
123 string* required_platform_version) {
Amin Hassani47186292017-08-01 15:03:08 -0700124#if USE_CHROME_KIOSK_APP
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800125 brillo::ErrorPtr error;
Daniel Erat04df23a2018-03-29 17:55:35 -0700126 if (!kiosk_app_proxy_->GetRequiredPlatformVersion(required_platform_version,
127 &error)) {
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800128 LOG(WARNING) << "Failed to get kiosk required platform version";
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700129 required_platform_version->clear();
130 return false;
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800131 }
Amin Hassani47186292017-08-01 15:03:08 -0700132#endif // USE_CHROME_KIOSK_APP
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800133
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700134 return true;
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800135}
136
Alex Deymo63784a52014-05-28 10:46:14 -0700137} // namespace chromeos_update_manager