-
Notifications
You must be signed in to change notification settings - Fork 19
/
real_system_provider.cc
144 lines (119 loc) · 4.95 KB
/
real_system_provider.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "update_engine/update_manager/real_system_provider.h"
#include <base/bind.h>
#include <base/callback.h>
#include <base/logging.h>
#include <base/time/time.h>
#include <kiosk-app/dbus-proxies.h>
#include "update_engine/common/boot_control_interface.h"
#include "update_engine/common/hardware_interface.h"
#include "update_engine/common/system_state.h"
#include "update_engine/common/utils.h"
#include "update_engine/cros/omaha_request_params.h"
#include "update_engine/update_manager/generic_variables.h"
#include "update_engine/update_manager/variable.h"
using chromeos_update_engine::SystemState;
using std::string;
namespace chromeos_update_manager {
namespace {
// The maximum number of consecutive failures before returning the default
// constructor value for T instead of failure.
const int kRetryPollVariableMaxRetry = 5;
// The polling interval to be used whenever GetValue() returns an error.
const int kRetryPollVariableRetryIntervalSeconds = 5 * 60;
// The RetryPollVariable variable is a polling variable that allows the function
// returning the value to fail a few times and shortens the polling rate when
// that happens.
template <typename T>
class RetryPollVariable : public Variable<T> {
public:
RetryPollVariable(const string& name,
const base::TimeDelta poll_interval,
base::Callback<bool(T* res)> func)
: Variable<T>(name, poll_interval),
func_(func),
base_interval_(poll_interval) {
DCHECK_LT(kRetryPollVariableRetryIntervalSeconds,
base_interval_.InSeconds());
}
protected:
// Variable override.
const T* GetValue(base::TimeDelta /* timeout */,
string* /* errmsg */) override {
std::unique_ptr<T> result(new T());
if (!func_.Run(result.get())) {
if (failed_attempts_ >= kRetryPollVariableMaxRetry) {
// Give up on the retries and set back the desired polling interval.
this->SetPollInterval(base_interval_);
// Release the result instead of returning a |nullptr| to indicate that
// the result could not be fetched.
return result.release();
}
this->SetPollInterval(
base::TimeDelta::FromSeconds(kRetryPollVariableRetryIntervalSeconds));
failed_attempts_++;
return nullptr;
}
failed_attempts_ = 0;
this->SetPollInterval(base_interval_);
return result.release();
}
private:
// The function to be called, stored as a base::Callback.
base::Callback<bool(T*)> func_;
// The desired polling interval when |func_| works and returns true.
base::TimeDelta base_interval_;
// The number of consecutive failed attempts made.
int failed_attempts_ = 0;
DISALLOW_COPY_AND_ASSIGN(RetryPollVariable);
};
} // namespace
bool RealSystemProvider::Init() {
var_is_normal_boot_mode_.reset(new ConstCopyVariable<bool>(
"is_normal_boot_mode",
SystemState::Get()->hardware()->IsNormalBootMode()));
var_is_official_build_.reset(new ConstCopyVariable<bool>(
"is_official_build", SystemState::Get()->hardware()->IsOfficialBuild()));
var_is_oobe_complete_.reset(new CallCopyVariable<bool>(
"is_oobe_complete",
base::Bind(&chromeos_update_engine::HardwareInterface::IsOOBEComplete,
base::Unretained(SystemState::Get()->hardware()),
nullptr)));
var_num_slots_.reset(new ConstCopyVariable<unsigned int>(
"num_slots", SystemState::Get()->boot_control()->GetNumSlots()));
var_kiosk_required_platform_version_.reset(new RetryPollVariable<string>(
"kiosk_required_platform_version",
base::TimeDelta::FromHours(5), // Same as Chrome's CWS poll.
base::Bind(&RealSystemProvider::GetKioskAppRequiredPlatformVersion,
base::Unretained(this))));
var_chromeos_version_.reset(new ConstCopyVariable<base::Version>(
"chromeos_version",
base::Version(SystemState::Get()->request_params()->app_version())));
return true;
}
bool RealSystemProvider::GetKioskAppRequiredPlatformVersion(
string* required_platform_version) {
brillo::ErrorPtr error;
if (!kiosk_app_proxy_->GetRequiredPlatformVersion(required_platform_version,
&error)) {
LOG(WARNING) << "Failed to get kiosk required platform version";
required_platform_version->clear();
return false;
}
return true;
}
} // namespace chromeos_update_manager