blob: fde6bfa94dd340be7614d3d4116434a0aabd5882 [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>
Alex Deymobd04b142014-03-18 15:00:05 -070023
Alex Deymo39910dc2015-11-09 17:04:30 -080024#include "update_engine/common/utils.h"
Xiyuan Xia6e30bc52016-02-24 15:35:42 -080025#include "update_engine/libcros_proxy.h"
Alex Deymo63784a52014-05-28 10:46:14 -070026#include "update_engine/update_manager/generic_variables.h"
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070027#include "update_engine/update_manager/variable.h"
Alex Deymobd04b142014-03-18 15:00:05 -070028
29using std::string;
Alex Deymobd04b142014-03-18 15:00:05 -070030
Alex Deymo63784a52014-05-28 10:46:14 -070031namespace chromeos_update_manager {
Alex Deymobd04b142014-03-18 15:00:05 -070032
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070033namespace {
34
Alex Deymo857ded12016-05-12 19:06:21 -070035// The maximum number of consecutive failures before returning the default
36// constructor value for T instead of failure.
37const int kRetryPollVariableMaxRetry = 5;
38
39// The polling interval to be used whenever GetValue() returns an error.
40const int kRetryPollVariableRetryIntervalSeconds = 5 * 60;
41
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070042// The RetryPollVariable variable is a polling variable that allows the function
43// returning the value to fail a few times and shortens the polling rate when
44// that happens.
45template <typename T>
46class RetryPollVariable : public Variable<T> {
47 public:
48 RetryPollVariable(const string& name,
49 const base::TimeDelta poll_interval,
50 base::Callback<bool(T* res)> func)
51 : Variable<T>(name, poll_interval),
52 func_(func),
53 base_interval_(poll_interval) {
Alex Deymo857ded12016-05-12 19:06:21 -070054 DCHECK_LT(kRetryPollVariableRetryIntervalSeconds,
55 base_interval_.InSeconds());
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070056 }
57
58 protected:
59 // Variable override.
60 const T* GetValue(base::TimeDelta /* timeout */,
61 string* /* errmsg */) override {
62 std::unique_ptr<T> result(new T());
63 if (!func_.Run(result.get())) {
Alex Deymo857ded12016-05-12 19:06:21 -070064 if (failed_attempts_ >= kRetryPollVariableMaxRetry) {
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070065 // Give up on the retries, set back the desired polling interval and
66 // return the default.
67 this->SetPollInterval(base_interval_);
68 return result.release();
69 }
70 this->SetPollInterval(
Alex Deymo857ded12016-05-12 19:06:21 -070071 base::TimeDelta::FromSeconds(kRetryPollVariableRetryIntervalSeconds));
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070072 failed_attempts_++;
73 return nullptr;
74 }
75 failed_attempts_ = 0;
76 this->SetPollInterval(base_interval_);
77 return result.release();
78 }
79
80 private:
81 // The function to be called, stored as a base::Callback.
82 base::Callback<bool(T*)> func_;
83
84 // The desired polling interval when |func_| works and returns true.
85 base::TimeDelta base_interval_;
86
87 // The number of consecutive failed attempts made.
88 int failed_attempts_ = 0;
89
Xiyuan Xiaed9bd922016-04-07 14:45:16 -070090 DISALLOW_COPY_AND_ASSIGN(RetryPollVariable);
91};
92
93} // namespace
94
Alex Deymo42c30c32014-04-24 18:41:18 -070095bool RealSystemProvider::Init() {
David Zeuthen21716e22014-04-23 15:42:05 -070096 var_is_normal_boot_mode_.reset(
Alex Deymobd04b142014-03-18 15:00:05 -070097 new ConstCopyVariable<bool>("is_normal_boot_mode",
Alex Deymo40d86b22015-09-03 22:27:10 -070098 hardware_->IsNormalBootMode()));
Alex Deymobd04b142014-03-18 15:00:05 -070099
David Zeuthen21716e22014-04-23 15:42:05 -0700100 var_is_official_build_.reset(
Gilad Arnold48e13612014-05-16 10:18:05 -0700101 new ConstCopyVariable<bool>("is_official_build",
Alex Deymo40d86b22015-09-03 22:27:10 -0700102 hardware_->IsOfficialBuild()));
Alex Deymobd04b142014-03-18 15:00:05 -0700103
Gilad Arnold48e13612014-05-16 10:18:05 -0700104 var_is_oobe_complete_.reset(
105 new CallCopyVariable<bool>(
106 "is_oobe_complete",
107 base::Bind(&chromeos_update_engine::HardwareInterface::IsOOBEComplete,
108 base::Unretained(hardware_), nullptr)));
109
Alex Deymo763e7db2015-08-27 21:08:08 -0700110 var_num_slots_.reset(
111 new ConstCopyVariable<unsigned int>(
112 "num_slots", boot_control_->GetNumSlots()));
Gilad Arnoldbfc44f72014-07-09 14:41:39 -0700113
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700114 var_kiosk_required_platform_version_.reset(new RetryPollVariable<string>(
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800115 "kiosk_required_platform_version",
116 base::TimeDelta::FromHours(5), // Same as Chrome's CWS poll.
117 base::Bind(&RealSystemProvider::GetKioskAppRequiredPlatformVersion,
118 base::Unretained(this))));
119
Alex Deymobd04b142014-03-18 15:00:05 -0700120 return true;
121}
122
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700123bool RealSystemProvider::GetKioskAppRequiredPlatformVersion(
124 string* required_platform_version) {
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800125#if USE_LIBCROS
126 brillo::ErrorPtr error;
127 if (!libcros_proxy_->service_interface_proxy()
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700128 ->GetKioskAppRequiredPlatformVersion(required_platform_version,
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800129 &error)) {
130 LOG(WARNING) << "Failed to get kiosk required platform version";
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700131 required_platform_version->clear();
132 return false;
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800133 }
134#endif
135
Xiyuan Xiaed9bd922016-04-07 14:45:16 -0700136 return true;
Xiyuan Xia6e30bc52016-02-24 15:35:42 -0800137}
138
Alex Deymo63784a52014-05-28 10:46:14 -0700139} // namespace chromeos_update_manager