blob: fdf7e86b2c7293020c2ee352d0fa7221bf6b0ae2 [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 Erate5f6f252017-04-20 12:09:58 -060024#include <libcros/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() {
David Zeuthen21716e22014-04-23 15:42:05 -070098 var_is_normal_boot_mode_.reset(
Alex Deymobd04b142014-03-18 15:00:05 -070099 new ConstCopyVariable<bool>("is_normal_boot_mode",
Alex Deymo40d86b22015-09-03 22:27:10 -0700100 hardware_->IsNormalBootMode()));
Alex Deymobd04b142014-03-18 15:00:05 -0700101
David Zeuthen21716e22014-04-23 15:42:05 -0700102 var_is_official_build_.reset(
Gilad Arnold48e13612014-05-16 10:18:05 -0700103 new ConstCopyVariable<bool>("is_official_build",
Alex Deymo40d86b22015-09-03 22:27:10 -0700104 hardware_->IsOfficialBuild()));
Alex Deymobd04b142014-03-18 15:00:05 -0700105
Gilad Arnold48e13612014-05-16 10:18:05 -0700106 var_is_oobe_complete_.reset(
107 new CallCopyVariable<bool>(
108 "is_oobe_complete",
109 base::Bind(&chromeos_update_engine::HardwareInterface::IsOOBEComplete,
110 base::Unretained(hardware_), nullptr)));
111
Alex Deymo763e7db2015-08-27 21:08:08 -0700112 var_num_slots_.reset(
113 new ConstCopyVariable<unsigned int>(
114 "num_slots", boot_control_->GetNumSlots()));
Gilad Arnoldbfc44f72014-07-09 14:41:39 -0700115
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700116 var_kiosk_required_platform_version_.reset(new RetryPollVariable<string>(
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800117 "kiosk_required_platform_version",
118 base::TimeDelta::FromHours(5), // Same as Chrome's CWS poll.
119 base::Bind(&RealSystemProvider::GetKioskAppRequiredPlatformVersion,
120 base::Unretained(this))));
121
Alex Deymobd04b142014-03-18 15:00:05 -0700122 return true;
123}
124
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700125bool RealSystemProvider::GetKioskAppRequiredPlatformVersion(
126 string* required_platform_version) {
Amin Hassani47186292017-08-01 15:03:08 -0700127#if USE_CHROME_KIOSK_APP
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800128 brillo::ErrorPtr error;
Daniel Erate5f6f252017-04-20 12:09:58 -0600129 if (!libcros_proxy_->GetKioskAppRequiredPlatformVersion(
130 required_platform_version, &error)) {
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800131 LOG(WARNING) << "Failed to get kiosk required platform version";
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700132 required_platform_version->clear();
133 return false;
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800134 }
Amin Hassani47186292017-08-01 15:03:08 -0700135#endif // USE_CHROME_KIOSK_APP
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800136
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700137 return true;
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800138}
139
Alex Deymo63784a52014-05-28 10:46:14 -0700140} // namespace chromeos_update_manager