blob: 5b76332ab721f1ce75dea89b2b8f248fc08d811e [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//
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070016
Alex Deymo63784a52014-05-28 10:46:14 -070017#include "update_engine/update_manager/real_updater_provider.h"
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070018
19#include <inttypes.h>
20
Amin Hassani03277de2020-07-28 12:32:49 -070021#include <algorithm>
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070022#include <string>
23
Gilad Arnold44dc3bf2014-07-18 23:39:38 -070024#include <base/bind.h>
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070025#include <base/strings/stringprintf.h>
Alex Deymoc7ab6162014-04-25 18:32:50 -070026#include <base/time/time.h>
Alex Deymod6deb1d2015-08-28 15:54:37 -070027#include <update_engine/dbus-constants.h>
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070028
Aaron Wood7f92e2b2017-08-28 14:51:21 -070029#include "update_engine/client_library/include/update_engine/update_status.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080030#include "update_engine/common/prefs.h"
Amin Hassani538bd592020-11-04 20:46:08 -080031#include "update_engine/common/system_state.h"
Amin Hassaniec7bc112020-10-29 16:47:58 -070032#include "update_engine/cros/omaha_request_params.h"
33#include "update_engine/cros/update_attempter.h"
Aaron Wood7f92e2b2017-08-28 14:51:21 -070034#include "update_engine/update_status_utils.h"
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070035
36using base::StringPrintf;
37using base::Time;
38using base::TimeDelta;
39using chromeos_update_engine::OmahaRequestParams;
40using chromeos_update_engine::SystemState;
41using std::string;
Aaron Woodbf5a2522017-10-04 10:58:36 -070042using update_engine::UpdateAttemptFlags;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070043using update_engine::UpdateEngineStatus;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070044
Alex Deymo63784a52014-05-28 10:46:14 -070045namespace chromeos_update_manager {
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070046
47// A templated base class for all update related variables. Provides uniform
48// construction and a system state handle.
Amin Hassani4b717432019-01-14 16:24:20 -080049template <typename T>
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070050class UpdaterVariableBase : public Variable<T> {
51 public:
Amin Hassani538bd592020-11-04 20:46:08 -080052 UpdaterVariableBase(const string& name, VariableMode mode)
53 : Variable<T>(name, mode) {}
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070054};
55
56// Helper class for issuing a GetStatus() to the UpdateAttempter.
57class GetStatusHelper {
58 public:
Amin Hassani538bd592020-11-04 20:46:08 -080059 explicit GetStatusHelper(string* errmsg) {
60 is_success_ = SystemState::Get()->update_attempter()->GetStatus(
61 &update_engine_status_);
Aaron Wood7f92e2b2017-08-28 14:51:21 -070062 if (!is_success_ && errmsg) {
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070063 *errmsg = "Failed to get a status update from the update engine";
Aaron Wood7f92e2b2017-08-28 14:51:21 -070064 }
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070065 }
66
67 inline bool is_success() { return is_success_; }
Aaron Wood7f92e2b2017-08-28 14:51:21 -070068 inline int64_t last_checked_time() {
Aaron Wood795c5b42017-12-05 16:06:13 -080069 return update_engine_status_.last_checked_time;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070070 }
71 inline double progress() { return update_engine_status_.progress; }
72 inline const string update_status() {
73 return chromeos_update_engine::UpdateStatusToString(
74 update_engine_status_.status);
75 }
76 inline const string& new_version() {
77 return update_engine_status_.new_version;
78 }
79 inline uint64_t payload_size() {
80 return update_engine_status_.new_size_bytes;
81 }
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070082
83 private:
84 bool is_success_;
Aaron Wood7f92e2b2017-08-28 14:51:21 -070085 UpdateEngineStatus update_engine_status_;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070086};
87
88// A variable reporting the time when a last update check was issued.
89class LastCheckedTimeVariable : public UpdaterVariableBase<Time> {
90 public:
Amin Hassani538bd592020-11-04 20:46:08 -080091 explicit LastCheckedTimeVariable(const string& name)
92 : UpdaterVariableBase<Time>(name, kVariableModePoll) {}
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070093
94 private:
Alex Vakulenko157fe302014-08-11 15:59:58 -070095 const Time* GetValue(TimeDelta /* timeout */, string* errmsg) override {
Amin Hassani538bd592020-11-04 20:46:08 -080096 GetStatusHelper raw(errmsg);
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070097 if (!raw.is_success())
Alex Vakulenko88b591f2014-08-28 16:48:57 -070098 return nullptr;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070099
100 return new Time(Time::FromTimeT(raw.last_checked_time()));
101 }
102
103 DISALLOW_COPY_AND_ASSIGN(LastCheckedTimeVariable);
104};
105
106// A variable reporting the update (download) progress as a decimal fraction
107// between 0.0 and 1.0.
108class ProgressVariable : public UpdaterVariableBase<double> {
109 public:
Amin Hassani538bd592020-11-04 20:46:08 -0800110 explicit ProgressVariable(const string& name)
111 : UpdaterVariableBase<double>(name, kVariableModePoll) {}
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700112
113 private:
Alex Vakulenko157fe302014-08-11 15:59:58 -0700114 const double* GetValue(TimeDelta /* timeout */, string* errmsg) override {
Amin Hassani538bd592020-11-04 20:46:08 -0800115 GetStatusHelper raw(errmsg);
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700116 if (!raw.is_success())
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700117 return nullptr;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700118
119 if (raw.progress() < 0.0 || raw.progress() > 1.0) {
120 if (errmsg) {
Amin Hassani4b717432019-01-14 16:24:20 -0800121 *errmsg =
122 StringPrintf("Invalid progress value received: %f", raw.progress());
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700123 }
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700124 return nullptr;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700125 }
126
127 return new double(raw.progress());
128 }
129
130 DISALLOW_COPY_AND_ASSIGN(ProgressVariable);
131};
132
133// A variable reporting the stage in which the update process is.
134class StageVariable : public UpdaterVariableBase<Stage> {
135 public:
Amin Hassani538bd592020-11-04 20:46:08 -0800136 explicit StageVariable(const string& name)
137 : UpdaterVariableBase<Stage>(name, kVariableModePoll) {}
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700138
139 private:
140 struct CurrOpStrToStage {
141 const char* str;
142 Stage stage;
143 };
144 static const CurrOpStrToStage curr_op_str_to_stage[];
145
146 // Note: the method is defined outside the class so arraysize can work.
Alex Vakulenko157fe302014-08-11 15:59:58 -0700147 const Stage* GetValue(TimeDelta /* timeout */, string* errmsg) override;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700148
149 DISALLOW_COPY_AND_ASSIGN(StageVariable);
150};
151
152const StageVariable::CurrOpStrToStage StageVariable::curr_op_str_to_stage[] = {
Amin Hassani4b717432019-01-14 16:24:20 -0800153 {update_engine::kUpdateStatusIdle, Stage::kIdle},
154 {update_engine::kUpdateStatusCheckingForUpdate, Stage::kCheckingForUpdate},
155 {update_engine::kUpdateStatusUpdateAvailable, Stage::kUpdateAvailable},
156 {update_engine::kUpdateStatusDownloading, Stage::kDownloading},
157 {update_engine::kUpdateStatusVerifying, Stage::kVerifying},
158 {update_engine::kUpdateStatusFinalizing, Stage::kFinalizing},
159 {update_engine::kUpdateStatusUpdatedNeedReboot, Stage::kUpdatedNeedReboot},
160 {update_engine::kUpdateStatusReportingErrorEvent,
161 Stage::kReportingErrorEvent},
162 {update_engine::kUpdateStatusAttemptingRollback,
163 Stage::kAttemptingRollback},
Amin Hassani70a90f52020-09-15 15:30:09 -0700164 {update_engine::kUpdateStatusCleanupPreviousUpdate,
165 Stage::kCleanupPreviousUpdate},
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700166};
167
Amin Hassani4b717432019-01-14 16:24:20 -0800168const Stage* StageVariable::GetValue(TimeDelta /* timeout */, string* errmsg) {
Amin Hassani538bd592020-11-04 20:46:08 -0800169 GetStatusHelper raw(errmsg);
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700170 if (!raw.is_success())
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700171 return nullptr;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700172
173 for (auto& key_val : curr_op_str_to_stage)
174 if (raw.update_status() == key_val.str)
175 return new Stage(key_val.stage);
176
177 if (errmsg)
178 *errmsg = string("Unknown update status: ") + raw.update_status();
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700179 return nullptr;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700180}
181
182// A variable reporting the version number that an update is updating to.
183class NewVersionVariable : public UpdaterVariableBase<string> {
184 public:
Amin Hassani538bd592020-11-04 20:46:08 -0800185 explicit NewVersionVariable(const string& name)
186 : UpdaterVariableBase<string>(name, kVariableModePoll) {}
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700187
188 private:
Alex Vakulenko157fe302014-08-11 15:59:58 -0700189 const string* GetValue(TimeDelta /* timeout */, string* errmsg) override {
Amin Hassani538bd592020-11-04 20:46:08 -0800190 GetStatusHelper raw(errmsg);
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700191 if (!raw.is_success())
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700192 return nullptr;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700193
194 return new string(raw.new_version());
195 }
196
197 DISALLOW_COPY_AND_ASSIGN(NewVersionVariable);
198};
199
200// A variable reporting the size of the update being processed in bytes.
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700201class PayloadSizeVariable : public UpdaterVariableBase<uint64_t> {
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700202 public:
Amin Hassani538bd592020-11-04 20:46:08 -0800203 explicit PayloadSizeVariable(const string& name)
204 : UpdaterVariableBase<uint64_t>(name, kVariableModePoll) {}
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700205
206 private:
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700207 const uint64_t* GetValue(TimeDelta /* timeout */, string* errmsg) override {
Amin Hassani538bd592020-11-04 20:46:08 -0800208 GetStatusHelper raw(errmsg);
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700209 if (!raw.is_success())
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700210 return nullptr;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700211
Aaron Wood7f92e2b2017-08-28 14:51:21 -0700212 return new uint64_t(raw.payload_size());
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700213 }
214
215 DISALLOW_COPY_AND_ASSIGN(PayloadSizeVariable);
216};
217
218// A variable reporting the point in time an update last completed in the
219// current boot cycle.
220//
221// TODO(garnold) In general, both the current boottime and wallclock time
222// readings should come from the time provider and be moderated by the
223// evaluation context, so that they are uniform throughout the evaluation of a
224// policy request.
225class UpdateCompletedTimeVariable : public UpdaterVariableBase<Time> {
226 public:
Amin Hassani538bd592020-11-04 20:46:08 -0800227 explicit UpdateCompletedTimeVariable(const string& name)
228 : UpdaterVariableBase<Time>(name, kVariableModePoll) {}
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700229
230 private:
Alex Vakulenko157fe302014-08-11 15:59:58 -0700231 const Time* GetValue(TimeDelta /* timeout */, string* errmsg) override {
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700232 Time update_boottime;
Amin Hassani538bd592020-11-04 20:46:08 -0800233 if (!SystemState::Get()->update_attempter()->GetBootTimeAtUpdate(
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700234 &update_boottime)) {
235 if (errmsg)
236 *errmsg = "Update completed time could not be read";
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700237 return nullptr;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700238 }
239
Amin Hassani0468a762020-11-17 23:53:48 -0800240 const auto* clock = SystemState::Get()->clock();
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700241 Time curr_boottime = clock->GetBootTime();
242 if (curr_boottime < update_boottime) {
243 if (errmsg)
244 *errmsg = "Update completed time more recent than current time";
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700245 return nullptr;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700246 }
247 TimeDelta duration_since_update = curr_boottime - update_boottime;
248 return new Time(clock->GetWallclockTime() - duration_since_update);
249 }
250
251 DISALLOW_COPY_AND_ASSIGN(UpdateCompletedTimeVariable);
252};
253
254// Variables reporting the current image channel.
255class CurrChannelVariable : public UpdaterVariableBase<string> {
256 public:
Amin Hassani538bd592020-11-04 20:46:08 -0800257 explicit CurrChannelVariable(const string& name)
258 : UpdaterVariableBase<string>(name, kVariableModePoll) {}
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700259
260 private:
Alex Vakulenko157fe302014-08-11 15:59:58 -0700261 const string* GetValue(TimeDelta /* timeout */, string* errmsg) override {
Amin Hassani538bd592020-11-04 20:46:08 -0800262 OmahaRequestParams* request_params = SystemState::Get()->request_params();
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700263 string channel = request_params->current_channel();
264 if (channel.empty()) {
265 if (errmsg)
266 *errmsg = "No current channel";
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700267 return nullptr;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700268 }
269 return new string(channel);
270 }
271
272 DISALLOW_COPY_AND_ASSIGN(CurrChannelVariable);
273};
274
275// Variables reporting the new image channel.
276class NewChannelVariable : public UpdaterVariableBase<string> {
277 public:
Amin Hassani538bd592020-11-04 20:46:08 -0800278 explicit NewChannelVariable(const string& name)
279 : UpdaterVariableBase<string>(name, kVariableModePoll) {}
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700280
281 private:
Alex Vakulenko157fe302014-08-11 15:59:58 -0700282 const string* GetValue(TimeDelta /* timeout */, string* errmsg) override {
Amin Hassani538bd592020-11-04 20:46:08 -0800283 OmahaRequestParams* request_params = SystemState::Get()->request_params();
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700284 string channel = request_params->target_channel();
285 if (channel.empty()) {
286 if (errmsg)
287 *errmsg = "No new channel";
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700288 return nullptr;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700289 }
290 return new string(channel);
291 }
292
293 DISALLOW_COPY_AND_ASSIGN(NewChannelVariable);
294};
295
296// A variable class for reading Boolean prefs values.
Alex Deymod6f60072015-10-12 12:22:27 -0700297class BooleanPrefVariable
298 : public AsyncCopyVariable<bool>,
299 public chromeos_update_engine::PrefsInterface::ObserverInterface {
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700300 public:
Alex Deymod6f60072015-10-12 12:22:27 -0700301 BooleanPrefVariable(const string& name,
Alex Deymod6f60072015-10-12 12:22:27 -0700302 const char* key,
303 bool default_val)
304 : AsyncCopyVariable<bool>(name),
Alex Deymod6f60072015-10-12 12:22:27 -0700305 key_(key),
306 default_val_(default_val) {
Amin Hassani90e9f192020-11-18 14:20:56 -0800307 SystemState::Get()->prefs()->AddObserver(key, this);
Alex Deymod6f60072015-10-12 12:22:27 -0700308 OnPrefSet(key);
309 }
Amin Hassani90e9f192020-11-18 14:20:56 -0800310 ~BooleanPrefVariable() {
311 SystemState::Get()->prefs()->RemoveObserver(key_, this);
312 }
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700313
314 private:
Alex Deymod6f60072015-10-12 12:22:27 -0700315 // Reads the actual value from the Prefs instance and updates the Variable
316 // value.
317 void OnPrefSet(const string& key) override {
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700318 bool result = default_val_;
Amin Hassani90e9f192020-11-18 14:20:56 -0800319 auto* prefs = SystemState::Get()->prefs();
320 if (prefs->Exists(key_) && !prefs->GetBoolean(key_, &result))
Alex Deymod6f60072015-10-12 12:22:27 -0700321 result = default_val_;
322 // AsyncCopyVariable will take care of values that didn't change.
323 SetValue(result);
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700324 }
325
Amin Hassani4b717432019-01-14 16:24:20 -0800326 void OnPrefDeleted(const string& key) override { SetValue(default_val_); }
Alex Deymod6f60072015-10-12 12:22:27 -0700327
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700328 // The Boolean preference key and default value.
329 const char* const key_;
330 const bool default_val_;
331
332 DISALLOW_COPY_AND_ASSIGN(BooleanPrefVariable);
333};
334
Gilad Arnolda6dab942014-04-25 11:46:03 -0700335// A variable returning the number of consecutive failed update checks.
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700336class ConsecutiveFailedUpdateChecksVariable
337 : public UpdaterVariableBase<unsigned int> {
Gilad Arnolda6dab942014-04-25 11:46:03 -0700338 public:
Amin Hassani538bd592020-11-04 20:46:08 -0800339 explicit ConsecutiveFailedUpdateChecksVariable(const string& name)
340 : UpdaterVariableBase<unsigned int>(name, kVariableModePoll) {}
Gilad Arnolda6dab942014-04-25 11:46:03 -0700341
342 private:
Alex Vakulenko157fe302014-08-11 15:59:58 -0700343 const unsigned int* GetValue(TimeDelta /* timeout */,
344 string* /* errmsg */) override {
Amin Hassani538bd592020-11-04 20:46:08 -0800345 // NOLINTNEXTLINE(readability/casting)
346 return new unsigned int(SystemState::Get()
347 ->update_attempter()
348 ->consecutive_failed_update_checks());
Gilad Arnolda6dab942014-04-25 11:46:03 -0700349 }
350
351 DISALLOW_COPY_AND_ASSIGN(ConsecutiveFailedUpdateChecksVariable);
352};
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700353
Gilad Arnolda0258a52014-07-10 16:21:19 -0700354// A variable returning the server-dictated poll interval.
355class ServerDictatedPollIntervalVariable
356 : public UpdaterVariableBase<unsigned int> {
357 public:
Amin Hassani538bd592020-11-04 20:46:08 -0800358 explicit ServerDictatedPollIntervalVariable(const string& name)
359 : UpdaterVariableBase<unsigned int>(name, kVariableModePoll) {}
Gilad Arnolda0258a52014-07-10 16:21:19 -0700360
361 private:
Alex Vakulenko157fe302014-08-11 15:59:58 -0700362 const unsigned int* GetValue(TimeDelta /* timeout */,
363 string* /* errmsg */) override {
Amin Hassani538bd592020-11-04 20:46:08 -0800364 // NOLINTNEXTLINE(readability/casting)
365 return new unsigned int(SystemState::Get()
366 ->update_attempter()
367 ->server_dictated_poll_interval());
Gilad Arnolda0258a52014-07-10 16:21:19 -0700368 }
369
370 DISALLOW_COPY_AND_ASSIGN(ServerDictatedPollIntervalVariable);
371};
372
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700373// An async variable that tracks changes to forced update requests.
374class ForcedUpdateRequestedVariable
375 : public UpdaterVariableBase<UpdateRequestStatus> {
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700376 public:
Amin Hassani538bd592020-11-04 20:46:08 -0800377 explicit ForcedUpdateRequestedVariable(const string& name)
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700378 : UpdaterVariableBase<UpdateRequestStatus>::UpdaterVariableBase(
Amin Hassani538bd592020-11-04 20:46:08 -0800379 name, kVariableModeAsync) {
380 SystemState::Get()->update_attempter()->set_forced_update_pending_callback(
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700381 new base::Callback<void(bool, bool)>( // NOLINT(readability/function)
382 base::Bind(&ForcedUpdateRequestedVariable::Reset,
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700383 base::Unretained(this))));
384 }
385
386 private:
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700387 const UpdateRequestStatus* GetValue(TimeDelta /* timeout */,
388 string* /* errmsg */) override {
389 return new UpdateRequestStatus(update_request_status_);
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700390 }
391
Amin Hassanied37d682018-04-06 13:22:00 -0700392 void Reset(bool forced_update_requested, bool interactive) {
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700393 UpdateRequestStatus new_value = UpdateRequestStatus::kNone;
394 if (forced_update_requested)
Amin Hassanied37d682018-04-06 13:22:00 -0700395 new_value = (interactive ? UpdateRequestStatus::kInteractive
396 : UpdateRequestStatus::kPeriodic);
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700397 if (update_request_status_ != new_value) {
398 update_request_status_ = new_value;
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700399 NotifyValueChanged();
400 }
401 }
402
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700403 UpdateRequestStatus update_request_status_ = UpdateRequestStatus::kNone;
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700404
Gilad Arnoldec7f9162014-07-15 13:24:46 -0700405 DISALLOW_COPY_AND_ASSIGN(ForcedUpdateRequestedVariable);
Gilad Arnold44dc3bf2014-07-18 23:39:38 -0700406};
407
Aaron Woodbf5a2522017-10-04 10:58:36 -0700408// A variable returning the current update restrictions that are in effect.
409class UpdateRestrictionsVariable
410 : public UpdaterVariableBase<UpdateRestrictions> {
411 public:
Amin Hassani538bd592020-11-04 20:46:08 -0800412 explicit UpdateRestrictionsVariable(const string& name)
413 : UpdaterVariableBase<UpdateRestrictions>(name, kVariableModePoll) {}
Aaron Woodbf5a2522017-10-04 10:58:36 -0700414
415 private:
416 const UpdateRestrictions* GetValue(TimeDelta /* timeout */,
417 string* /* errmsg */) override {
418 UpdateAttemptFlags attempt_flags =
Amin Hassani538bd592020-11-04 20:46:08 -0800419 SystemState::Get()->update_attempter()->GetCurrentUpdateAttemptFlags();
Aaron Woodbf5a2522017-10-04 10:58:36 -0700420 UpdateRestrictions restriction_flags = UpdateRestrictions::kNone;
421 // Don't blindly copy the whole value, test and set bits that should
422 // transfer from one set of flags to the other.
423 if (attempt_flags & UpdateAttemptFlags::kFlagRestrictDownload) {
424 restriction_flags = static_cast<UpdateRestrictions>(
425 restriction_flags | UpdateRestrictions::kRestrictDownloading);
426 }
427
428 return new UpdateRestrictions(restriction_flags);
429 }
430
431 DISALLOW_COPY_AND_ASSIGN(UpdateRestrictionsVariable);
432};
433
Amin Hassani03277de2020-07-28 12:32:49 -0700434// A variable class for reading timeout interval prefs value.
435class TestUpdateCheckIntervalTimeoutVariable : public Variable<int64_t> {
436 public:
Amin Hassani90e9f192020-11-18 14:20:56 -0800437 explicit TestUpdateCheckIntervalTimeoutVariable(const string& name)
438 : Variable<int64_t>(name, kVariableModePoll), read_count_(0) {
Amin Hassani03277de2020-07-28 12:32:49 -0700439 SetMissingOk();
440 }
441 ~TestUpdateCheckIntervalTimeoutVariable() = default;
442
443 private:
444 const int64_t* GetValue(TimeDelta /* timeout */,
445 string* /* errmsg */) override {
446 auto key = chromeos_update_engine::kPrefsTestUpdateCheckIntervalTimeout;
Amin Hassani90e9f192020-11-18 14:20:56 -0800447 auto* prefs = SystemState::Get()->prefs();
Amin Hassani03277de2020-07-28 12:32:49 -0700448 int64_t result;
Amin Hassani90e9f192020-11-18 14:20:56 -0800449 if (prefs->Exists(key) && prefs->GetInt64(key, &result)) {
Amin Hassani03277de2020-07-28 12:32:49 -0700450 // This specific value is used for testing only. So it should not be kept
451 // around and should be deleted after a few reads.
Amin Hassani7ad016b2020-09-03 14:19:13 -0700452 if (++read_count_ > 5)
Amin Hassani90e9f192020-11-18 14:20:56 -0800453 prefs->Delete(key);
Amin Hassani03277de2020-07-28 12:32:49 -0700454
455 // Limit the timeout interval to 10 minutes so it is not abused if it is
456 // seen on official images.
457 return new int64_t(std::min(result, static_cast<int64_t>(10 * 60)));
458 }
459 return nullptr;
460 }
461
Amin Hassani03277de2020-07-28 12:32:49 -0700462 // Counts how many times this variable is read. This is used to delete the
463 // underlying file defining the variable after a certain number of reads in
464 // order to prevent any abuse of this variable.
465 int read_count_;
466
467 DISALLOW_COPY_AND_ASSIGN(TestUpdateCheckIntervalTimeoutVariable);
468};
469
Gilad Arnoldae47a9a2014-03-26 12:16:47 -0700470// RealUpdaterProvider methods.
471
Amin Hassani538bd592020-11-04 20:46:08 -0800472RealUpdaterProvider::RealUpdaterProvider()
473 : var_updater_started_time_(
474 "updater_started_time",
475 SystemState::Get()->clock()->GetWallclockTime()),
476 var_last_checked_time_(new LastCheckedTimeVariable("last_checked_time")),
477 var_update_completed_time_(
478 new UpdateCompletedTimeVariable("update_completed_time")),
479 var_progress_(new ProgressVariable("progress")),
480 var_stage_(new StageVariable("stage")),
481 var_new_version_(new NewVersionVariable("new_version")),
482 var_payload_size_(new PayloadSizeVariable("payload_size")),
483 var_curr_channel_(new CurrChannelVariable("curr_channel")),
484 var_new_channel_(new NewChannelVariable("new_channel")),
Amin Hassani90e9f192020-11-18 14:20:56 -0800485 var_p2p_enabled_(new BooleanPrefVariable(
486 "p2p_enabled", chromeos_update_engine::kPrefsP2PEnabled, false)),
Aaron Woodbf5a2522017-10-04 10:58:36 -0700487 var_cellular_enabled_(new BooleanPrefVariable(
488 "cellular_enabled",
Aaron Woodbf5a2522017-10-04 10:58:36 -0700489 chromeos_update_engine::kPrefsUpdateOverCellularPermission,
490 false)),
491 var_consecutive_failed_update_checks_(
492 new ConsecutiveFailedUpdateChecksVariable(
Amin Hassani538bd592020-11-04 20:46:08 -0800493 "consecutive_failed_update_checks")),
Aaron Woodbf5a2522017-10-04 10:58:36 -0700494 var_server_dictated_poll_interval_(new ServerDictatedPollIntervalVariable(
Amin Hassani538bd592020-11-04 20:46:08 -0800495 "server_dictated_poll_interval")),
496 var_forced_update_requested_(
497 new ForcedUpdateRequestedVariable("forced_update_requested")),
Amin Hassani03277de2020-07-28 12:32:49 -0700498 var_update_restrictions_(
Amin Hassani538bd592020-11-04 20:46:08 -0800499 new UpdateRestrictionsVariable("update_restrictions")),
Amin Hassani03277de2020-07-28 12:32:49 -0700500 var_test_update_check_interval_timeout_(
501 new TestUpdateCheckIntervalTimeoutVariable(
Amin Hassani90e9f192020-11-18 14:20:56 -0800502 "test_update_check_interval_timeout")) {}
Alex Deymo63784a52014-05-28 10:46:14 -0700503} // namespace chromeos_update_manager