blob: bfe2cf0b8ee9a0cb4a0ae89bed875c30acc492e9 [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
Amin Hassani7cc8bb02019-01-14 16:29:47 -080089 inline uint32_t GetUrlFailureCount() override { return url_failure_count_; }
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080090
Amin Hassani7cc8bb02019-01-14 16:29:47 -080091 inline uint32_t GetUrlSwitchCount() override { return url_switch_count_; }
David Zeuthencc6f9962013-04-18 11:57:24 -070092
Amin Hassani7cc8bb02019-01-14 16:29:47 -080093 inline int GetNumResponsesSeen() override { return num_responses_seen_; }
David Zeuthena573d6f2013-06-14 16:13:36 -070094
Alex Deymo610277e2014-11-11 21:18:11 -080095 inline base::Time GetBackoffExpiryTime() override {
Jay Srinivasan08262882012-12-28 19:29:43 -080096 return backoff_expiry_time_;
97 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080098
Alex Deymo610277e2014-11-11 21:18:11 -080099 base::TimeDelta GetUpdateDuration() override;
David Zeuthen9a017f22013-04-11 16:10:26 -0700100
Alex Deymo610277e2014-11-11 21:18:11 -0800101 base::TimeDelta GetUpdateDurationUptime() override;
David Zeuthen9a017f22013-04-11 16:10:26 -0700102
Alex Deymo610277e2014-11-11 21:18:11 -0800103 inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) override {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700104 return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0;
105 }
106
Alex Deymo610277e2014-11-11 21:18:11 -0800107 inline uint64_t GetTotalBytesDownloaded(DownloadSource source) override {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700108 return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0;
109 }
110
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800111 inline uint32_t GetNumReboots() override { return num_reboots_; }
Chris Sosabe45bef2013-04-09 18:25:12 -0700112
Alex Deymo610277e2014-11-11 21:18:11 -0800113 void UpdateEngineStarted() override;
David Zeuthene4c58bf2013-06-18 17:26:50 -0700114
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200115 inline bool GetRollbackHappened() override { return rollback_happened_; }
116
117 void SetRollbackHappened(bool rollback_happened) override;
118
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800119 inline std::string GetRollbackVersion() override { return rollback_version_; }
Chris Sosaaa18e162013-06-20 13:20:30 -0700120
Alex Deymo610277e2014-11-11 21:18:11 -0800121 int GetP2PNumAttempts() override;
122 base::Time GetP2PFirstAttemptTimestamp() override;
123 void P2PNewAttempt() override;
124 bool P2PAttemptAllowed() override;
David Zeuthendcba8092013-08-06 12:16:35 -0700125
Gilad Arnold74b5f552014-10-07 08:17:16 -0700126 bool GetUsingP2PForDownloading() const override {
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700127 return using_p2p_for_downloading_;
128 }
129
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800130 bool GetUsingP2PForSharing() const override { return using_p2p_for_sharing_; }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700131
Gilad Arnold519cfc72014-10-02 10:34:54 -0700132 base::TimeDelta GetScatteringWaitPeriod() override {
133 return scattering_wait_period_;
134 }
135
136 void SetScatteringWaitPeriod(base::TimeDelta wait_period) override;
137
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700138 void SetStagingWaitPeriod(base::TimeDelta wait_period) override;
139
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800140 void SetP2PUrl(const std::string& url) override { p2p_url_ = url; }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700141
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800142 std::string GetP2PUrl() const override { return p2p_url_; }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700143
Sen Jiang5ae865b2017-04-18 14:24:40 -0700144 bool NextPayload() override;
Sen Jiang6c736682017-03-10 15:01:36 -0800145
Jay Srinivasan08262882012-12-28 19:29:43 -0800146 private:
David Zeuthenafed4a12014-04-09 15:28:44 -0700147 enum class AttemptType {
148 kUpdate,
149 kRollback,
150 };
151
Alex Deymo42432912013-07-12 20:21:15 -0700152 friend class PayloadStateTest;
153 FRIEND_TEST(PayloadStateTest, RebootAfterUpdateFailedMetric);
154 FRIEND_TEST(PayloadStateTest, RebootAfterUpdateSucceed);
155 FRIEND_TEST(PayloadStateTest, RebootAfterCanceledUpdate);
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200156 FRIEND_TEST(PayloadStateTest, RollbackHappened);
Chris Sosab3dcdb32013-09-04 15:22:12 -0700157 FRIEND_TEST(PayloadStateTest, RollbackVersion);
Alex Deymo42432912013-07-12 20:21:15 -0700158 FRIEND_TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs);
Jae Hoon Kima3210e62020-05-07 11:32:44 -0700159 FRIEND_TEST(PayloadStateTest, NextPayloadResetsUrlIndex);
Alex Deymo42432912013-07-12 20:21:15 -0700160
David Zeuthen33bae492014-02-25 16:16:18 -0800161 // Helper called when an attempt has begun, is called by
David Zeuthenafed4a12014-04-09 15:28:44 -0700162 // UpdateResumed(), UpdateRestarted() and Rollback().
163 void AttemptStarted(AttemptType attempt_type);
David Zeuthen33bae492014-02-25 16:16:18 -0800164
Alex Deymo820cc702013-06-28 15:43:46 -0700165 // Increments the payload attempt number used for metrics.
166 void IncrementPayloadAttemptNumber();
167
Jay Srinivasan08262882012-12-28 19:29:43 -0800168 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800169 // at the time of the next update check.
Alex Deymo820cc702013-06-28 15:43:46 -0700170 void IncrementFullPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800171
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800172 // Advances the current URL index to the next available one. If all URLs have
173 // been exhausted during the current payload download attempt (as indicated
174 // by the payload attempt number), then it will increment the payload attempt
David Zeuthencc6f9962013-04-18 11:57:24 -0700175 // number and wrap around again with the first URL in the list. This also
176 // updates the URL switch count, if needed.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800177 void IncrementUrlIndex();
178
179 // Increments the failure count of the current URL. If the configured max
180 // failure count is reached for this URL, it advances the current URL index
181 // to the next URL and resets the failure count for that URL.
182 void IncrementFailureCount();
183
Jay Srinivasan08262882012-12-28 19:29:43 -0800184 // Updates the backoff expiry time exponentially based on the current
185 // payload attempt number.
186 void UpdateBackoffExpiryTime();
187
Jay Srinivasan19409b72013-04-12 19:23:36 -0700188 // Updates the value of current download source based on the current URL
189 // index. If the download source is not one of the known sources, it's set
190 // to kNumDownloadSources.
191 void UpdateCurrentDownloadSource();
192
193 // Updates the various metrics corresponding with the given number of bytes
194 // that were downloaded recently.
195 void UpdateBytesDownloaded(size_t count);
196
David Zeuthen33bae492014-02-25 16:16:18 -0800197 // Calculates the PayloadType we're using.
198 PayloadType CalculatePayloadType();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700199
David Zeuthen33bae492014-02-25 16:16:18 -0800200 // Collects and reports the various metrics related to an update attempt.
201 void CollectAndReportAttemptMetrics(ErrorCode code);
David Zeuthencc6f9962013-04-18 11:57:24 -0700202
David Zeuthen4e1d1492014-04-25 13:12:27 -0700203 // Persists values related to the UpdateEngine.Attempt.* metrics so
204 // we can identify later if an update attempt ends abnormally.
205 void PersistAttemptMetrics();
206
207 // Clears persistent state previously set using AttemptMetricsPersist().
208 void ClearPersistedAttemptMetrics();
209
210 // Checks if persistent state previously set using AttemptMetricsPersist()
211 // exists and, if so, emits it with |attempt_result| set to
212 // metrics::AttemptResult::kAbnormalTermination.
213 void ReportAndClearPersistedAttemptMetrics();
214
David Zeuthen33bae492014-02-25 16:16:18 -0800215 // Collects and reports the various metrics related to a successful update.
216 void CollectAndReportSuccessfulUpdateMetrics();
Alex Deymo820cc702013-06-28 15:43:46 -0700217
Alex Deymo42432912013-07-12 20:21:15 -0700218 // Checks if we were expecting to be running in the new version but the
219 // boot into the new version failed for some reason. If that's the case, an
220 // UMA metric is sent reporting the number of attempts the same applied
221 // payload was attempted to reboot. This function is called by UpdateAttempter
222 // every time the update engine starts and there's no reboot pending.
223 void ReportFailedBootIfNeeded();
224
Jay Srinivasan08262882012-12-28 19:29:43 -0800225 // Resets all the persisted state values which are maintained relative to the
226 // current response signature. The response signature itself is not reset.
227 void ResetPersistedState();
228
Jay Srinivasan19409b72013-04-12 19:23:36 -0700229 // Resets the appropriate state related to download sources that need to be
230 // reset on a new update.
231 void ResetDownloadSourcesOnNewUpdate();
232
Jay Srinivasan08262882012-12-28 19:29:43 -0800233 // Calculates the response "signature", which is basically a string composed
234 // of the subset of the fields in the current response that affect the
235 // behavior of the PayloadState.
236 std::string CalculateResponseSignature();
237
238 // Initializes the current response signature from the persisted state.
239 void LoadResponseSignature();
240
241 // Sets the response signature to the given value. Also persists the value
242 // being set so that we resume from the save value in case of a process
243 // restart.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700244 void SetResponseSignature(const std::string& response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800245
246 // Initializes the payload attempt number from the persisted state.
247 void LoadPayloadAttemptNumber();
248
Alex Deymo820cc702013-06-28 15:43:46 -0700249 // Initializes the payload attempt number for full payloads from the persisted
250 // state.
251 void LoadFullPayloadAttemptNumber();
252
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800253 // Sets the payload attempt number to the given value. Also persists the
254 // value being set so that we resume from the same value in case of a process
255 // restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700256 void SetPayloadAttemptNumber(int payload_attempt_number);
257
258 // Sets the payload attempt number for full updates to the given value. Also
259 // persists the value being set so that we resume from the same value in case
260 // of a process restart.
261 void SetFullPayloadAttemptNumber(int payload_attempt_number);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800262
Sen Jiang5ae865b2017-04-18 14:24:40 -0700263 // Sets the current payload index to the given value. Also persists the value
264 // being set so that we resume from the same value in case of a process
265 // restart.
266 void SetPayloadIndex(size_t payload_index);
267
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800268 // Initializes the current URL index from the persisted state.
269 void LoadUrlIndex();
270
271 // Sets the current URL index to the given value. Also persists the value
272 // being set so that we resume from the same value in case of a process
273 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800274 void SetUrlIndex(uint32_t url_index);
275
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800276 // Initializes the current URL's failure count from the persisted stae.
277 void LoadUrlFailureCount();
278
279 // Sets the current URL's failure count to the given value. Also persists the
280 // value being set so that we resume from the same value in case of a process
281 // restart.
282 void SetUrlFailureCount(uint32_t url_failure_count);
283
David Zeuthencc6f9962013-04-18 11:57:24 -0700284 // Sets |url_switch_count_| to the given value and persists the value.
285 void SetUrlSwitchCount(uint32_t url_switch_count);
286
287 // Initializes |url_switch_count_| from the persisted stae.
288 void LoadUrlSwitchCount();
289
Jay Srinivasan08262882012-12-28 19:29:43 -0800290 // Initializes the backoff expiry time from the persisted state.
291 void LoadBackoffExpiryTime();
292
293 // Sets the backoff expiry time to the given value. Also persists the value
294 // being set so that we resume from the same value in case of a process
295 // restart.
296 void SetBackoffExpiryTime(const base::Time& new_time);
297
David Zeuthen9a017f22013-04-11 16:10:26 -0700298 // Initializes |update_timestamp_start_| from the persisted state.
299 void LoadUpdateTimestampStart();
300
301 // Sets |update_timestamp_start_| to the given value and persists the value.
302 void SetUpdateTimestampStart(const base::Time& value);
303
304 // Sets |update_timestamp_end_| to the given value. This is not persisted
305 // as it happens at the end of the update process where state is deleted
306 // anyway.
307 void SetUpdateTimestampEnd(const base::Time& value);
308
309 // Initializes |update_duration_uptime_| from the persisted state.
310 void LoadUpdateDurationUptime();
311
312 // Helper method used in SetUpdateDurationUptime() and
313 // CalculateUpdateDurationUptime().
314 void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
315 const base::Time& timestamp,
316 bool use_logging);
317
318 // Sets |update_duration_uptime_| to the given value and persists
319 // the value and sets |update_duration_uptime_timestamp_| to the
320 // current monotonic time.
321 void SetUpdateDurationUptime(const base::TimeDelta& value);
322
323 // Adds the difference between current monotonic time and
324 // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
325 // sets |update_duration_uptime_timestamp_| to current monotonic time.
326 void CalculateUpdateDurationUptime();
327
Jay Srinivasan19409b72013-04-12 19:23:36 -0700328 // Returns the full key for a download source given the prefix.
329 std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
330
331 // Loads the number of bytes that have been currently downloaded through the
332 // previous attempts from the persisted state for the given source. It's
Sen Jiang771f6482018-04-04 17:59:10 -0700333 // reset to 0 every time we begin a full update and is continued from previous
Jay Srinivasan19409b72013-04-12 19:23:36 -0700334 // attempt if we're resuming the update.
335 void LoadCurrentBytesDownloaded(DownloadSource source);
336
337 // Sets the number of bytes that have been currently downloaded for the
338 // given source. This value is also persisted.
339 void SetCurrentBytesDownloaded(DownloadSource source,
340 uint64_t current_bytes_downloaded,
341 bool log);
342
343 // Loads the total number of bytes that have been downloaded (since the last
344 // successful update) from the persisted state for the given source. It's
Sen Jiang771f6482018-04-04 17:59:10 -0700345 // reset to 0 every time we successfully apply an update and counts the bytes
Jay Srinivasan19409b72013-04-12 19:23:36 -0700346 // downloaded for both successful and failed attempts since then.
347 void LoadTotalBytesDownloaded(DownloadSource source);
348
349 // Sets the total number of bytes that have been downloaded so far for the
350 // given source. This value is also persisted.
351 void SetTotalBytesDownloaded(DownloadSource source,
352 uint64_t total_bytes_downloaded,
353 bool log);
354
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200355 // Loads whether rollback has happened on this device since the last update
356 // check where policy was available. This info is preserved over powerwash.
357 void LoadRollbackHappened();
358
Chris Sosaaa18e162013-06-20 13:20:30 -0700359 // Loads the blacklisted version from our prefs file.
360 void LoadRollbackVersion();
361
362 // Blacklists this version from getting AU'd to until we receive a new update
363 // response.
364 void SetRollbackVersion(const std::string& rollback_version);
365
366 // Clears any blacklisted version.
367 void ResetRollbackVersion();
368
Jay Srinivasan53173b92013-05-17 17:13:01 -0700369 inline uint32_t GetUrlIndex() {
Amin Hassani91502232018-04-13 10:31:52 -0700370 return (url_index_ != 0 && payload_index_ < candidate_urls_.size())
371 ? std::min(candidate_urls_[payload_index_].size() - 1,
372 url_index_)
373 : 0;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700374 }
375
376 // Computes the list of candidate URLs from the total list of payload URLs in
377 // the Omaha response.
378 void ComputeCandidateUrls();
379
David Zeuthena573d6f2013-06-14 16:13:36 -0700380 // Sets |num_responses_seen_| and persist it to disk.
381 void SetNumResponsesSeen(int num_responses_seen);
382
383 // Initializes |num_responses_seen_| from persisted state.
384 void LoadNumResponsesSeen();
385
Chris Sosabe45bef2013-04-09 18:25:12 -0700386 // Initializes |num_reboots_| from the persisted state.
387 void LoadNumReboots();
388
389 // Sets |num_reboots| for the update attempt. Also persists the
390 // value being set so that we resume from the same value in case of a process
391 // restart.
392 void SetNumReboots(uint32_t num_reboots);
393
394 // Checks to see if the device rebooted since the last call and if so
395 // increments num_reboots.
396 void UpdateNumReboots();
397
David Zeuthendcba8092013-08-06 12:16:35 -0700398 // Loads the |kPrefsP2PFirstAttemptTimestamp| state variable from disk
399 // into |p2p_first_attempt_timestamp_|.
400 void LoadP2PFirstAttemptTimestamp();
401
402 // Loads the |kPrefsP2PNumAttempts| state variable into |p2p_num_attempts_|.
403 void LoadP2PNumAttempts();
404
405 // Sets the |kPrefsP2PNumAttempts| state variable to |value|.
406 void SetP2PNumAttempts(int value);
407
408 // Sets the |kPrefsP2PFirstAttemptTimestamp| state variable to |time|.
409 void SetP2PFirstAttemptTimestamp(const base::Time& time);
410
Gilad Arnold519cfc72014-10-02 10:34:54 -0700411 // Loads the persisted scattering wallclock-based wait period.
412 void LoadScatteringWaitPeriod();
413
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700414 // Loads the persisted staging wallclock-based wait period.
415 void LoadStagingWaitPeriod();
416
Sen Jiang0affc2c2017-02-10 15:55:05 -0800417 // Get the total size of all payloads.
418 int64_t GetPayloadSize();
419
Gilad Arnold6e15aac2014-10-02 10:34:14 -0700420 // The global state of the system.
421 SystemState* system_state_;
422
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800423 // Interface object with which we read/write persisted state. This must
424 // be set by calling the Initialize method before calling any other method.
425 PrefsInterface* prefs_;
426
Chris Sosaaa18e162013-06-20 13:20:30 -0700427 // Interface object with which we read/write persisted state. This must
428 // be set by calling the Initialize method before calling any other method.
429 // This object persists across powerwashes.
430 PrefsInterface* powerwash_safe_prefs_;
431
Jay Srinivasan08262882012-12-28 19:29:43 -0800432 // This is the current response object from Omaha.
433 OmahaResponse response_;
434
Gilad Arnold74b5f552014-10-07 08:17:16 -0700435 // Whether P2P is being used for downloading and sharing.
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700436 bool using_p2p_for_downloading_;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700437 bool using_p2p_for_sharing_;
438
439 // Stores the P2P download URL, if one is used.
440 std::string p2p_url_;
441
442 // The cached value of |kPrefsP2PFirstAttemptTimestamp|.
443 base::Time p2p_first_attempt_timestamp_;
444
445 // The cached value of |kPrefsP2PNumAttempts|.
446 int p2p_num_attempts_;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700447
Jay Srinivasan08262882012-12-28 19:29:43 -0800448 // This stores a "signature" of the current response. The signature here
449 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800450 // this value is persisted so we resume from the same value in case of a
451 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800452 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800453
Alex Deymo820cc702013-06-28 15:43:46 -0700454 // The number of times we've tried to download the payload. This is
455 // incremented each time we download the payload successsfully or when we
456 // exhaust all failure limits for all URLs and are about to wrap around back
457 // to the first URL. Each update to this value is persisted so we resume from
458 // the same value in case of a process restart.
459 int payload_attempt_number_;
460
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800461 // The number of times we've tried to download the payload in full. This is
462 // incremented each time we download the payload in full successsfully or
463 // when we exhaust all failure limits for all URLs and are about to wrap
464 // around back to the first URL. Each update to this value is persisted so
465 // we resume from the same value in case of a process restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700466 int full_payload_attempt_number_;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800467
Sen Jiang0affc2c2017-02-10 15:55:05 -0800468 // The index of the current payload.
469 size_t payload_index_ = 0;
470
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800471 // The index of the current URL. This type is different from the one in the
472 // accessor methods because PrefsInterface supports only int64_t but we want
473 // to provide a stronger abstraction of uint32_t. Each update to this value
474 // is persisted so we resume from the same value in case of a process
475 // restart.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800476 size_t url_index_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800477
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800478 // The count of failures encountered in the current attempt to download using
479 // the current URL (specified by url_index_). Each update to this value is
480 // persisted so we resume from the same value in case of a process restart.
481 int64_t url_failure_count_;
482
David Zeuthencc6f9962013-04-18 11:57:24 -0700483 // The number of times we've switched URLs.
484 int32_t url_switch_count_;
485
Jay Srinivasan19409b72013-04-12 19:23:36 -0700486 // The current download source based on the current URL. This value is
Sen Jiang771f6482018-04-04 17:59:10 -0700487 // not persisted as it can be recomputed every time we update the URL.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700488 // We're storing this so as not to recompute this on every few bytes of
489 // data we read from the socket.
490 DownloadSource current_download_source_;
491
David Zeuthena573d6f2013-06-14 16:13:36 -0700492 // The number of different Omaha responses seen. Increases every time
493 // a new response is seen. Resets to 0 only when the system has been
494 // successfully updated.
495 int num_responses_seen_;
496
Chris Sosabe45bef2013-04-09 18:25:12 -0700497 // The number of system reboots during an update attempt. Technically since
498 // we don't go out of our way to not update it when not attempting an update,
499 // also records the number of reboots before the next update attempt starts.
500 uint32_t num_reboots_;
501
Jay Srinivasan08262882012-12-28 19:29:43 -0800502 // The timestamp until which we've to wait before attempting to download the
503 // payload again, so as to backoff repeated downloads.
504 base::Time backoff_expiry_time_;
505
David Zeuthen9a017f22013-04-11 16:10:26 -0700506 // The most recently calculated value of the update duration.
507 base::TimeDelta update_duration_current_;
508
509 // The point in time (wall-clock) that the update was started.
510 base::Time update_timestamp_start_;
511
512 // The point in time (wall-clock) that the update ended. If the update
513 // is still in progress, this is set to the Epoch (e.g. 0).
514 base::Time update_timestamp_end_;
515
516 // The update duration uptime
517 base::TimeDelta update_duration_uptime_;
518
519 // The monotonic time when |update_duration_uptime_| was last set
520 base::Time update_duration_uptime_timestamp_;
521
Jay Srinivasan19409b72013-04-12 19:23:36 -0700522 // The number of bytes that have been downloaded for each source for each new
523 // update attempt. If we resume an update, we'll continue from the previous
524 // value, but if we get a new response or if the previous attempt failed,
525 // we'll reset this to 0 to start afresh. Each update to this value is
526 // persisted so we resume from the same value in case of a process restart.
527 // The extra index in the array is to no-op accidental access in case the
528 // return value from GetCurrentDownloadSource is used without validation.
529 uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
530
531 // The number of bytes that have been downloaded for each source since the
532 // the last successful update. This is used to compute the overhead we incur.
533 // Each update to this value is persisted so we resume from the same value in
534 // case of a process restart.
535 // The extra index in the array is to no-op accidental access in case the
536 // return value from GetCurrentDownloadSource is used without validation.
537 uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
538
David Zeuthen9a017f22013-04-11 16:10:26 -0700539 // A small timespan used when comparing wall-clock times for coping
540 // with the fact that clocks drift and consequently are adjusted
541 // (either forwards or backwards) via NTP.
542 static const base::TimeDelta kDurationSlack;
543
Jay Srinivasan53173b92013-05-17 17:13:01 -0700544 // The ordered list of the subset of payload URL candidates which are
545 // allowed as per device policy.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800546 std::vector<std::vector<std::string>> candidate_urls_;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700547
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200548 // This stores whether rollback has happened since the last time device policy
549 // was available during update check. When this is set, we're preventing
550 // forced updates to avoid update-rollback loops.
551 bool rollback_happened_;
552
Chris Sosaaa18e162013-06-20 13:20:30 -0700553 // This stores a blacklisted version set as part of rollback. When we rollback
554 // we store the version of the os from which we are rolling back from in order
555 // to guarantee that we do not re-update to it on the next au attempt after
556 // reboot.
557 std::string rollback_version_;
558
David Zeuthen33bae492014-02-25 16:16:18 -0800559 // The number of bytes downloaded per attempt.
560 int64_t attempt_num_bytes_downloaded_;
561
562 // The boot time when the attempt was started.
563 base::Time attempt_start_time_boot_;
564
565 // The monotonic time when the attempt was started.
566 base::Time attempt_start_time_monotonic_;
567
David Zeuthenb281f072014-04-02 10:20:19 -0700568 // The connection type when the attempt started.
569 metrics::ConnectionType attempt_connection_type_;
570
David Zeuthenafed4a12014-04-09 15:28:44 -0700571 // Whether we're currently rolling back.
572 AttemptType attempt_type_;
573
Gilad Arnold519cfc72014-10-02 10:34:54 -0700574 // The current scattering wallclock-based wait period.
575 base::TimeDelta scattering_wait_period_;
576
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700577 // The current staging wallclock-based wait period.
578 base::TimeDelta staging_wait_period_;
579
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800580 DISALLOW_COPY_AND_ASSIGN(PayloadState);
581};
582
583} // namespace chromeos_update_engine
584
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700585#endif // UPDATE_ENGINE_PAYLOAD_STATE_H_