blob: 0868a100714df93865dfab1b0ac0751d53970c85 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 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//
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080016
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_PAYLOAD_STATE_H_
18#define UPDATE_ENGINE_PAYLOAD_STATE_H_
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080019
Tianjie Xu90aaa102017-10-10 17:39:03 -070020#include <algorithm>
Alex Vakulenkod2779df2014-06-16 13:19:00 -070021#include <string>
22#include <vector>
23
Alex Vakulenko75039d72014-03-25 12:36:28 -070024#include <base/time/time.h>
Alex Deymo42432912013-07-12 20:21:15 -070025#include <gtest/gtest_prod.h> // for FRIEND_TEST
Jay Srinivasan08262882012-12-28 19:29:43 -080026
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/prefs_interface.h"
Tianjie Xu282aa1f2017-09-05 13:42:45 -070028#include "update_engine/metrics_constants.h"
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080029#include "update_engine/payload_state_interface.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080030
31namespace chromeos_update_engine {
32
Jay Srinivasan19409b72013-04-12 19:23:36 -070033class SystemState;
34
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080035// Encapsulates all the payload state required for download. This includes the
Jay Srinivasan08262882012-12-28 19:29:43 -080036// state necessary for handling multiple URLs in Omaha response, the backoff
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080037// state, etc. All state is persisted so that we use the most recently saved
38// value when resuming the update_engine process. All state is also cached in
39// memory so that we ensure we always make progress based on last known good
40// state even when there's any issue in reading/writing from the file system.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080041class PayloadState : public PayloadStateInterface {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080042 public:
Jay Srinivasan19409b72013-04-12 19:23:36 -070043 PayloadState();
Alex Deymo610277e2014-11-11 21:18:11 -080044 ~PayloadState() override {}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080045
Jay Srinivasan19409b72013-04-12 19:23:36 -070046 // Initializes a payload state object using the given global system state.
47 // It performs the initial loading of all persisted state into memory and
48 // dumps the initial state for debugging purposes. Note: the other methods
49 // should be called only after calling Initialize on this object.
50 bool Initialize(SystemState* system_state);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080051
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080052 // Implementation of PayloadStateInterface methods.
Alex Deymo610277e2014-11-11 21:18:11 -080053 void SetResponse(const OmahaResponse& response) override;
54 void DownloadComplete() override;
55 void DownloadProgress(size_t count) override;
56 void UpdateResumed() override;
57 void UpdateRestarted() override;
58 void UpdateSucceeded() override;
59 void UpdateFailed(ErrorCode error) override;
60 void ResetUpdateStatus() override;
61 bool ShouldBackoffDownload() override;
62 void Rollback() override;
63 void ExpectRebootInNewVersion(const std::string& target_version_uid) override;
64 void SetUsingP2PForDownloading(bool value) override;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080065
Gilad Arnold74b5f552014-10-07 08:17:16 -070066 void SetUsingP2PForSharing(bool value) override {
67 using_p2p_for_sharing_ = value;
68 }
69
Alex Deymo610277e2014-11-11 21:18:11 -080070 inline std::string GetResponseSignature() override {
Jay Srinivasan08262882012-12-28 19:29:43 -080071 return response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080072 }
73
Alex Deymo610277e2014-11-11 21:18:11 -080074 inline int GetFullPayloadAttemptNumber() override {
Alex Deymo820cc702013-06-28 15:43:46 -070075 return full_payload_attempt_number_;
76 }
77
Alex Deymo610277e2014-11-11 21:18:11 -080078 inline int GetPayloadAttemptNumber() override {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080079 return payload_attempt_number_;
80 }
81
Alex Deymo610277e2014-11-11 21:18:11 -080082 inline std::string GetCurrentUrl() override {
Amin Hassani91502232018-04-13 10:31:52 -070083 return (payload_index_ < candidate_urls_.size() &&
84 url_index_ < candidate_urls_[payload_index_].size())
Sen Jiang0affc2c2017-02-10 15:55:05 -080085 ? candidate_urls_[payload_index_][url_index_]
86 : "";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080087 }
88
Alex Deymo610277e2014-11-11 21:18:11 -080089 inline uint32_t GetUrlFailureCount() override {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080090 return url_failure_count_;
91 }
92
Alex Deymo610277e2014-11-11 21:18:11 -080093 inline uint32_t GetUrlSwitchCount() override {
David Zeuthencc6f9962013-04-18 11:57:24 -070094 return url_switch_count_;
95 }
96
Alex Deymo610277e2014-11-11 21:18:11 -080097 inline int GetNumResponsesSeen() override {
David Zeuthena573d6f2013-06-14 16:13:36 -070098 return num_responses_seen_;
99 }
100
Alex Deymo610277e2014-11-11 21:18:11 -0800101 inline base::Time GetBackoffExpiryTime() override {
Jay Srinivasan08262882012-12-28 19:29:43 -0800102 return backoff_expiry_time_;
103 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800104
Alex Deymo610277e2014-11-11 21:18:11 -0800105 base::TimeDelta GetUpdateDuration() override;
David Zeuthen9a017f22013-04-11 16:10:26 -0700106
Alex Deymo610277e2014-11-11 21:18:11 -0800107 base::TimeDelta GetUpdateDurationUptime() override;
David Zeuthen9a017f22013-04-11 16:10:26 -0700108
Alex Deymo610277e2014-11-11 21:18:11 -0800109 inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) override {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700110 return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0;
111 }
112
Alex Deymo610277e2014-11-11 21:18:11 -0800113 inline uint64_t GetTotalBytesDownloaded(DownloadSource source) override {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700114 return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0;
115 }
116
Alex Deymo610277e2014-11-11 21:18:11 -0800117 inline uint32_t GetNumReboots() override {
Chris Sosabe45bef2013-04-09 18:25:12 -0700118 return num_reboots_;
119 }
120
Alex Deymo610277e2014-11-11 21:18:11 -0800121 void UpdateEngineStarted() override;
David Zeuthene4c58bf2013-06-18 17:26:50 -0700122
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200123 inline bool GetRollbackHappened() override { return rollback_happened_; }
124
125 void SetRollbackHappened(bool rollback_happened) override;
126
Alex Deymo610277e2014-11-11 21:18:11 -0800127 inline std::string GetRollbackVersion() override {
Chris Sosaaa18e162013-06-20 13:20:30 -0700128 return rollback_version_;
129 }
130
Alex Deymo610277e2014-11-11 21:18:11 -0800131 int GetP2PNumAttempts() override;
132 base::Time GetP2PFirstAttemptTimestamp() override;
133 void P2PNewAttempt() override;
134 bool P2PAttemptAllowed() override;
David Zeuthendcba8092013-08-06 12:16:35 -0700135
Gilad Arnold74b5f552014-10-07 08:17:16 -0700136 bool GetUsingP2PForDownloading() const override {
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700137 return using_p2p_for_downloading_;
138 }
139
Gilad Arnold74b5f552014-10-07 08:17:16 -0700140 bool GetUsingP2PForSharing() const override {
141 return using_p2p_for_sharing_;
142 }
143
Gilad Arnold519cfc72014-10-02 10:34:54 -0700144 base::TimeDelta GetScatteringWaitPeriod() override {
145 return scattering_wait_period_;
146 }
147
148 void SetScatteringWaitPeriod(base::TimeDelta wait_period) override;
149
Gilad Arnold74b5f552014-10-07 08:17:16 -0700150 void SetP2PUrl(const std::string& url) override {
151 p2p_url_ = url;
152 }
153
154 std::string GetP2PUrl() const override {
155 return p2p_url_;
156 }
157
Shuqian Zhao29971732016-02-05 11:29:32 -0800158 inline ErrorCode GetAttemptErrorCode() const override {
159 return attempt_error_code_;
160 }
161
Sen Jiang5ae865b2017-04-18 14:24:40 -0700162 bool NextPayload() override;
Sen Jiang6c736682017-03-10 15:01:36 -0800163
Jay Srinivasan08262882012-12-28 19:29:43 -0800164 private:
David Zeuthenafed4a12014-04-09 15:28:44 -0700165 enum class AttemptType {
166 kUpdate,
167 kRollback,
168 };
169
Alex Deymo42432912013-07-12 20:21:15 -0700170 friend class PayloadStateTest;
171 FRIEND_TEST(PayloadStateTest, RebootAfterUpdateFailedMetric);
172 FRIEND_TEST(PayloadStateTest, RebootAfterUpdateSucceed);
173 FRIEND_TEST(PayloadStateTest, RebootAfterCanceledUpdate);
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200174 FRIEND_TEST(PayloadStateTest, RollbackHappened);
Chris Sosab3dcdb32013-09-04 15:22:12 -0700175 FRIEND_TEST(PayloadStateTest, RollbackVersion);
Alex Deymo42432912013-07-12 20:21:15 -0700176 FRIEND_TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs);
177
David Zeuthen33bae492014-02-25 16:16:18 -0800178 // Helper called when an attempt has begun, is called by
David Zeuthenafed4a12014-04-09 15:28:44 -0700179 // UpdateResumed(), UpdateRestarted() and Rollback().
180 void AttemptStarted(AttemptType attempt_type);
David Zeuthen33bae492014-02-25 16:16:18 -0800181
Alex Deymo820cc702013-06-28 15:43:46 -0700182 // Increments the payload attempt number used for metrics.
183 void IncrementPayloadAttemptNumber();
184
Jay Srinivasan08262882012-12-28 19:29:43 -0800185 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800186 // at the time of the next update check.
Alex Deymo820cc702013-06-28 15:43:46 -0700187 void IncrementFullPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800188
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800189 // Advances the current URL index to the next available one. If all URLs have
190 // been exhausted during the current payload download attempt (as indicated
191 // by the payload attempt number), then it will increment the payload attempt
David Zeuthencc6f9962013-04-18 11:57:24 -0700192 // number and wrap around again with the first URL in the list. This also
193 // updates the URL switch count, if needed.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800194 void IncrementUrlIndex();
195
196 // Increments the failure count of the current URL. If the configured max
197 // failure count is reached for this URL, it advances the current URL index
198 // to the next URL and resets the failure count for that URL.
199 void IncrementFailureCount();
200
Jay Srinivasan08262882012-12-28 19:29:43 -0800201 // Updates the backoff expiry time exponentially based on the current
202 // payload attempt number.
203 void UpdateBackoffExpiryTime();
204
Jay Srinivasan19409b72013-04-12 19:23:36 -0700205 // Updates the value of current download source based on the current URL
206 // index. If the download source is not one of the known sources, it's set
207 // to kNumDownloadSources.
208 void UpdateCurrentDownloadSource();
209
210 // Updates the various metrics corresponding with the given number of bytes
211 // that were downloaded recently.
212 void UpdateBytesDownloaded(size_t count);
213
David Zeuthen33bae492014-02-25 16:16:18 -0800214 // Calculates the PayloadType we're using.
215 PayloadType CalculatePayloadType();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700216
David Zeuthen33bae492014-02-25 16:16:18 -0800217 // Collects and reports the various metrics related to an update attempt.
218 void CollectAndReportAttemptMetrics(ErrorCode code);
David Zeuthencc6f9962013-04-18 11:57:24 -0700219
David Zeuthen4e1d1492014-04-25 13:12:27 -0700220 // Persists values related to the UpdateEngine.Attempt.* metrics so
221 // we can identify later if an update attempt ends abnormally.
222 void PersistAttemptMetrics();
223
224 // Clears persistent state previously set using AttemptMetricsPersist().
225 void ClearPersistedAttemptMetrics();
226
227 // Checks if persistent state previously set using AttemptMetricsPersist()
228 // exists and, if so, emits it with |attempt_result| set to
229 // metrics::AttemptResult::kAbnormalTermination.
230 void ReportAndClearPersistedAttemptMetrics();
231
David Zeuthen33bae492014-02-25 16:16:18 -0800232 // Collects and reports the various metrics related to a successful update.
233 void CollectAndReportSuccessfulUpdateMetrics();
Alex Deymo820cc702013-06-28 15:43:46 -0700234
Alex Deymo42432912013-07-12 20:21:15 -0700235 // Checks if we were expecting to be running in the new version but the
236 // boot into the new version failed for some reason. If that's the case, an
237 // UMA metric is sent reporting the number of attempts the same applied
238 // payload was attempted to reboot. This function is called by UpdateAttempter
239 // every time the update engine starts and there's no reboot pending.
240 void ReportFailedBootIfNeeded();
241
Jay Srinivasan08262882012-12-28 19:29:43 -0800242 // Resets all the persisted state values which are maintained relative to the
243 // current response signature. The response signature itself is not reset.
244 void ResetPersistedState();
245
Jay Srinivasan19409b72013-04-12 19:23:36 -0700246 // Resets the appropriate state related to download sources that need to be
247 // reset on a new update.
248 void ResetDownloadSourcesOnNewUpdate();
249
Jay Srinivasan08262882012-12-28 19:29:43 -0800250 // Calculates the response "signature", which is basically a string composed
251 // of the subset of the fields in the current response that affect the
252 // behavior of the PayloadState.
253 std::string CalculateResponseSignature();
254
255 // Initializes the current response signature from the persisted state.
256 void LoadResponseSignature();
257
258 // Sets the response signature to the given value. Also persists the value
259 // being set so that we resume from the save value in case of a process
260 // restart.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700261 void SetResponseSignature(const std::string& response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800262
263 // Initializes the payload attempt number from the persisted state.
264 void LoadPayloadAttemptNumber();
265
Alex Deymo820cc702013-06-28 15:43:46 -0700266 // Initializes the payload attempt number for full payloads from the persisted
267 // state.
268 void LoadFullPayloadAttemptNumber();
269
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800270 // Sets the payload attempt number to the given value. Also persists the
271 // value being set so that we resume from the same value in case of a process
272 // restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700273 void SetPayloadAttemptNumber(int payload_attempt_number);
274
275 // Sets the payload attempt number for full updates to the given value. Also
276 // persists the value being set so that we resume from the same value in case
277 // of a process restart.
278 void SetFullPayloadAttemptNumber(int payload_attempt_number);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800279
Sen Jiang5ae865b2017-04-18 14:24:40 -0700280 // Sets the current payload index to the given value. Also persists the value
281 // being set so that we resume from the same value in case of a process
282 // restart.
283 void SetPayloadIndex(size_t payload_index);
284
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800285 // Initializes the current URL index from the persisted state.
286 void LoadUrlIndex();
287
288 // Sets the current URL index to the given value. Also persists the value
289 // being set so that we resume from the same value in case of a process
290 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800291 void SetUrlIndex(uint32_t url_index);
292
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800293 // Initializes the current URL's failure count from the persisted stae.
294 void LoadUrlFailureCount();
295
296 // Sets the current URL's failure count to the given value. Also persists the
297 // value being set so that we resume from the same value in case of a process
298 // restart.
299 void SetUrlFailureCount(uint32_t url_failure_count);
300
David Zeuthencc6f9962013-04-18 11:57:24 -0700301 // Sets |url_switch_count_| to the given value and persists the value.
302 void SetUrlSwitchCount(uint32_t url_switch_count);
303
304 // Initializes |url_switch_count_| from the persisted stae.
305 void LoadUrlSwitchCount();
306
Jay Srinivasan08262882012-12-28 19:29:43 -0800307 // Initializes the backoff expiry time from the persisted state.
308 void LoadBackoffExpiryTime();
309
310 // Sets the backoff expiry time to the given value. Also persists the value
311 // being set so that we resume from the same value in case of a process
312 // restart.
313 void SetBackoffExpiryTime(const base::Time& new_time);
314
David Zeuthen9a017f22013-04-11 16:10:26 -0700315 // Initializes |update_timestamp_start_| from the persisted state.
316 void LoadUpdateTimestampStart();
317
318 // Sets |update_timestamp_start_| to the given value and persists the value.
319 void SetUpdateTimestampStart(const base::Time& value);
320
321 // Sets |update_timestamp_end_| to the given value. This is not persisted
322 // as it happens at the end of the update process where state is deleted
323 // anyway.
324 void SetUpdateTimestampEnd(const base::Time& value);
325
326 // Initializes |update_duration_uptime_| from the persisted state.
327 void LoadUpdateDurationUptime();
328
329 // Helper method used in SetUpdateDurationUptime() and
330 // CalculateUpdateDurationUptime().
331 void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
332 const base::Time& timestamp,
333 bool use_logging);
334
335 // Sets |update_duration_uptime_| to the given value and persists
336 // the value and sets |update_duration_uptime_timestamp_| to the
337 // current monotonic time.
338 void SetUpdateDurationUptime(const base::TimeDelta& value);
339
340 // Adds the difference between current monotonic time and
341 // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
342 // sets |update_duration_uptime_timestamp_| to current monotonic time.
343 void CalculateUpdateDurationUptime();
344
Jay Srinivasan19409b72013-04-12 19:23:36 -0700345 // Returns the full key for a download source given the prefix.
346 std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
347
348 // Loads the number of bytes that have been currently downloaded through the
349 // previous attempts from the persisted state for the given source. It's
350 // reset to 0 everytime we begin a full update and is continued from previous
351 // attempt if we're resuming the update.
352 void LoadCurrentBytesDownloaded(DownloadSource source);
353
354 // Sets the number of bytes that have been currently downloaded for the
355 // given source. This value is also persisted.
356 void SetCurrentBytesDownloaded(DownloadSource source,
357 uint64_t current_bytes_downloaded,
358 bool log);
359
360 // Loads the total number of bytes that have been downloaded (since the last
361 // successful update) from the persisted state for the given source. It's
362 // reset to 0 everytime we successfully apply an update and counts the bytes
363 // downloaded for both successful and failed attempts since then.
364 void LoadTotalBytesDownloaded(DownloadSource source);
365
366 // Sets the total number of bytes that have been downloaded so far for the
367 // given source. This value is also persisted.
368 void SetTotalBytesDownloaded(DownloadSource source,
369 uint64_t total_bytes_downloaded,
370 bool log);
371
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200372 // Loads whether rollback has happened on this device since the last update
373 // check where policy was available. This info is preserved over powerwash.
374 void LoadRollbackHappened();
375
Chris Sosaaa18e162013-06-20 13:20:30 -0700376 // Loads the blacklisted version from our prefs file.
377 void LoadRollbackVersion();
378
379 // Blacklists this version from getting AU'd to until we receive a new update
380 // response.
381 void SetRollbackVersion(const std::string& rollback_version);
382
383 // Clears any blacklisted version.
384 void ResetRollbackVersion();
385
Jay Srinivasan53173b92013-05-17 17:13:01 -0700386 inline uint32_t GetUrlIndex() {
Amin Hassani91502232018-04-13 10:31:52 -0700387 return (url_index_ != 0 && payload_index_ < candidate_urls_.size())
388 ? std::min(candidate_urls_[payload_index_].size() - 1,
389 url_index_)
390 : 0;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700391 }
392
393 // Computes the list of candidate URLs from the total list of payload URLs in
394 // the Omaha response.
395 void ComputeCandidateUrls();
396
David Zeuthena573d6f2013-06-14 16:13:36 -0700397 // Sets |num_responses_seen_| and persist it to disk.
398 void SetNumResponsesSeen(int num_responses_seen);
399
400 // Initializes |num_responses_seen_| from persisted state.
401 void LoadNumResponsesSeen();
402
Chris Sosabe45bef2013-04-09 18:25:12 -0700403 // Initializes |num_reboots_| from the persisted state.
404 void LoadNumReboots();
405
406 // Sets |num_reboots| for the update attempt. Also persists the
407 // value being set so that we resume from the same value in case of a process
408 // restart.
409 void SetNumReboots(uint32_t num_reboots);
410
411 // Checks to see if the device rebooted since the last call and if so
412 // increments num_reboots.
413 void UpdateNumReboots();
414
David Zeuthene4c58bf2013-06-18 17:26:50 -0700415
David Zeuthene4c58bf2013-06-18 17:26:50 -0700416
David Zeuthendcba8092013-08-06 12:16:35 -0700417 // Loads the |kPrefsP2PFirstAttemptTimestamp| state variable from disk
418 // into |p2p_first_attempt_timestamp_|.
419 void LoadP2PFirstAttemptTimestamp();
420
421 // Loads the |kPrefsP2PNumAttempts| state variable into |p2p_num_attempts_|.
422 void LoadP2PNumAttempts();
423
424 // Sets the |kPrefsP2PNumAttempts| state variable to |value|.
425 void SetP2PNumAttempts(int value);
426
427 // Sets the |kPrefsP2PFirstAttemptTimestamp| state variable to |time|.
428 void SetP2PFirstAttemptTimestamp(const base::Time& time);
429
Gilad Arnold519cfc72014-10-02 10:34:54 -0700430 // Loads the persisted scattering wallclock-based wait period.
431 void LoadScatteringWaitPeriod();
432
Sen Jiang0affc2c2017-02-10 15:55:05 -0800433 // Get the total size of all payloads.
434 int64_t GetPayloadSize();
435
Gilad Arnold6e15aac2014-10-02 10:34:14 -0700436 // The global state of the system.
437 SystemState* system_state_;
438
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800439 // Interface object with which we read/write persisted state. This must
440 // be set by calling the Initialize method before calling any other method.
441 PrefsInterface* prefs_;
442
Chris Sosaaa18e162013-06-20 13:20:30 -0700443 // Interface object with which we read/write persisted state. This must
444 // be set by calling the Initialize method before calling any other method.
445 // This object persists across powerwashes.
446 PrefsInterface* powerwash_safe_prefs_;
447
Jay Srinivasan08262882012-12-28 19:29:43 -0800448 // This is the current response object from Omaha.
449 OmahaResponse response_;
450
Gilad Arnold74b5f552014-10-07 08:17:16 -0700451 // Whether P2P is being used for downloading and sharing.
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700452 bool using_p2p_for_downloading_;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700453 bool using_p2p_for_sharing_;
454
455 // Stores the P2P download URL, if one is used.
456 std::string p2p_url_;
457
458 // The cached value of |kPrefsP2PFirstAttemptTimestamp|.
459 base::Time p2p_first_attempt_timestamp_;
460
461 // The cached value of |kPrefsP2PNumAttempts|.
462 int p2p_num_attempts_;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700463
Jay Srinivasan08262882012-12-28 19:29:43 -0800464 // This stores a "signature" of the current response. The signature here
465 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800466 // this value is persisted so we resume from the same value in case of a
467 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800468 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800469
Alex Deymo820cc702013-06-28 15:43:46 -0700470 // The number of times we've tried to download the payload. This is
471 // incremented each time we download the payload successsfully or when we
472 // exhaust all failure limits for all URLs and are about to wrap around back
473 // to the first URL. Each update to this value is persisted so we resume from
474 // the same value in case of a process restart.
475 int payload_attempt_number_;
476
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800477 // The number of times we've tried to download the payload in full. This is
478 // incremented each time we download the payload in full successsfully or
479 // when we exhaust all failure limits for all URLs and are about to wrap
480 // around back to the first URL. Each update to this value is persisted so
481 // we resume from the same value in case of a process restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700482 int full_payload_attempt_number_;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800483
Sen Jiang0affc2c2017-02-10 15:55:05 -0800484 // The index of the current payload.
485 size_t payload_index_ = 0;
486
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800487 // The index of the current URL. This type is different from the one in the
488 // accessor methods because PrefsInterface supports only int64_t but we want
489 // to provide a stronger abstraction of uint32_t. Each update to this value
490 // is persisted so we resume from the same value in case of a process
491 // restart.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800492 size_t url_index_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800493
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800494 // The count of failures encountered in the current attempt to download using
495 // the current URL (specified by url_index_). Each update to this value is
496 // persisted so we resume from the same value in case of a process restart.
497 int64_t url_failure_count_;
498
David Zeuthencc6f9962013-04-18 11:57:24 -0700499 // The number of times we've switched URLs.
500 int32_t url_switch_count_;
501
Jay Srinivasan19409b72013-04-12 19:23:36 -0700502 // The current download source based on the current URL. This value is
503 // not persisted as it can be recomputed everytime we update the URL.
504 // We're storing this so as not to recompute this on every few bytes of
505 // data we read from the socket.
506 DownloadSource current_download_source_;
507
David Zeuthena573d6f2013-06-14 16:13:36 -0700508 // The number of different Omaha responses seen. Increases every time
509 // a new response is seen. Resets to 0 only when the system has been
510 // successfully updated.
511 int num_responses_seen_;
512
Chris Sosabe45bef2013-04-09 18:25:12 -0700513 // The number of system reboots during an update attempt. Technically since
514 // we don't go out of our way to not update it when not attempting an update,
515 // also records the number of reboots before the next update attempt starts.
516 uint32_t num_reboots_;
517
Jay Srinivasan08262882012-12-28 19:29:43 -0800518 // The timestamp until which we've to wait before attempting to download the
519 // payload again, so as to backoff repeated downloads.
520 base::Time backoff_expiry_time_;
521
David Zeuthen9a017f22013-04-11 16:10:26 -0700522 // The most recently calculated value of the update duration.
523 base::TimeDelta update_duration_current_;
524
525 // The point in time (wall-clock) that the update was started.
526 base::Time update_timestamp_start_;
527
528 // The point in time (wall-clock) that the update ended. If the update
529 // is still in progress, this is set to the Epoch (e.g. 0).
530 base::Time update_timestamp_end_;
531
532 // The update duration uptime
533 base::TimeDelta update_duration_uptime_;
534
535 // The monotonic time when |update_duration_uptime_| was last set
536 base::Time update_duration_uptime_timestamp_;
537
Jay Srinivasan19409b72013-04-12 19:23:36 -0700538 // The number of bytes that have been downloaded for each source for each new
539 // update attempt. If we resume an update, we'll continue from the previous
540 // value, but if we get a new response or if the previous attempt failed,
541 // we'll reset this to 0 to start afresh. Each update to this value is
542 // persisted so we resume from the same value in case of a process restart.
543 // The extra index in the array is to no-op accidental access in case the
544 // return value from GetCurrentDownloadSource is used without validation.
545 uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
546
547 // The number of bytes that have been downloaded for each source since the
548 // the last successful update. This is used to compute the overhead we incur.
549 // Each update to this value is persisted so we resume from the same value in
550 // case of a process restart.
551 // The extra index in the array is to no-op accidental access in case the
552 // return value from GetCurrentDownloadSource is used without validation.
553 uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
554
David Zeuthen9a017f22013-04-11 16:10:26 -0700555 // A small timespan used when comparing wall-clock times for coping
556 // with the fact that clocks drift and consequently are adjusted
557 // (either forwards or backwards) via NTP.
558 static const base::TimeDelta kDurationSlack;
559
Jay Srinivasan53173b92013-05-17 17:13:01 -0700560 // The ordered list of the subset of payload URL candidates which are
561 // allowed as per device policy.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800562 std::vector<std::vector<std::string>> candidate_urls_;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700563
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200564 // This stores whether rollback has happened since the last time device policy
565 // was available during update check. When this is set, we're preventing
566 // forced updates to avoid update-rollback loops.
567 bool rollback_happened_;
568
Chris Sosaaa18e162013-06-20 13:20:30 -0700569 // This stores a blacklisted version set as part of rollback. When we rollback
570 // we store the version of the os from which we are rolling back from in order
571 // to guarantee that we do not re-update to it on the next au attempt after
572 // reboot.
573 std::string rollback_version_;
574
David Zeuthen33bae492014-02-25 16:16:18 -0800575 // The number of bytes downloaded per attempt.
576 int64_t attempt_num_bytes_downloaded_;
577
578 // The boot time when the attempt was started.
579 base::Time attempt_start_time_boot_;
580
581 // The monotonic time when the attempt was started.
582 base::Time attempt_start_time_monotonic_;
583
David Zeuthenb281f072014-04-02 10:20:19 -0700584 // The connection type when the attempt started.
585 metrics::ConnectionType attempt_connection_type_;
586
Shuqian Zhao29971732016-02-05 11:29:32 -0800587 // The attempt error code when the attempt finished.
588 ErrorCode attempt_error_code_;
589
David Zeuthenafed4a12014-04-09 15:28:44 -0700590 // Whether we're currently rolling back.
591 AttemptType attempt_type_;
592
Gilad Arnold519cfc72014-10-02 10:34:54 -0700593 // The current scattering wallclock-based wait period.
594 base::TimeDelta scattering_wait_period_;
595
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800596 DISALLOW_COPY_AND_ASSIGN(PayloadState);
597};
598
599} // namespace chromeos_update_engine
600
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700601#endif // UPDATE_ENGINE_PAYLOAD_STATE_H_