blob: 56e32fd52c5bc1905dc89808bcddf2f1450309fc [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
Alex Vakulenkod2779df2014-06-16 13:19:00 -070020#include <string>
21#include <vector>
22
Alex Vakulenko75039d72014-03-25 12:36:28 -070023#include <base/time/time.h>
Alex Deymo42432912013-07-12 20:21:15 -070024#include <gtest/gtest_prod.h> // for FRIEND_TEST
Jay Srinivasan08262882012-12-28 19:29:43 -080025
Alex Deymo39910dc2015-11-09 17:04:30 -080026#include "update_engine/common/prefs_interface.h"
David Zeuthenb281f072014-04-02 10:20:19 -070027#include "update_engine/metrics.h"
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080028#include "update_engine/payload_state_interface.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080029
30namespace chromeos_update_engine {
31
Jay Srinivasan19409b72013-04-12 19:23:36 -070032class SystemState;
33
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080034// Encapsulates all the payload state required for download. This includes the
Jay Srinivasan08262882012-12-28 19:29:43 -080035// state necessary for handling multiple URLs in Omaha response, the backoff
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080036// state, etc. All state is persisted so that we use the most recently saved
37// value when resuming the update_engine process. All state is also cached in
38// memory so that we ensure we always make progress based on last known good
39// state even when there's any issue in reading/writing from the file system.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080040class PayloadState : public PayloadStateInterface {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080041 public:
Jay Srinivasan19409b72013-04-12 19:23:36 -070042 PayloadState();
Alex Deymo610277e2014-11-11 21:18:11 -080043 ~PayloadState() override {}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080044
Jay Srinivasan19409b72013-04-12 19:23:36 -070045 // Initializes a payload state object using the given global system state.
46 // It performs the initial loading of all persisted state into memory and
47 // dumps the initial state for debugging purposes. Note: the other methods
48 // should be called only after calling Initialize on this object.
49 bool Initialize(SystemState* system_state);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080050
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080051 // Implementation of PayloadStateInterface methods.
Alex Deymo610277e2014-11-11 21:18:11 -080052 void SetResponse(const OmahaResponse& response) override;
53 void DownloadComplete() override;
54 void DownloadProgress(size_t count) override;
55 void UpdateResumed() override;
56 void UpdateRestarted() override;
57 void UpdateSucceeded() override;
58 void UpdateFailed(ErrorCode error) override;
59 void ResetUpdateStatus() override;
60 bool ShouldBackoffDownload() override;
61 void Rollback() override;
62 void ExpectRebootInNewVersion(const std::string& target_version_uid) override;
63 void SetUsingP2PForDownloading(bool value) override;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080064
Gilad Arnold74b5f552014-10-07 08:17:16 -070065 void SetUsingP2PForSharing(bool value) override {
66 using_p2p_for_sharing_ = value;
67 }
68
Alex Deymo610277e2014-11-11 21:18:11 -080069 inline std::string GetResponseSignature() override {
Jay Srinivasan08262882012-12-28 19:29:43 -080070 return response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080071 }
72
Alex Deymo610277e2014-11-11 21:18:11 -080073 inline int GetFullPayloadAttemptNumber() override {
Alex Deymo820cc702013-06-28 15:43:46 -070074 return full_payload_attempt_number_;
75 }
76
Alex Deymo610277e2014-11-11 21:18:11 -080077 inline int GetPayloadAttemptNumber() override {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080078 return payload_attempt_number_;
79 }
80
Alex Deymo610277e2014-11-11 21:18:11 -080081 inline std::string GetCurrentUrl() override {
Sen Jiang0affc2c2017-02-10 15:55:05 -080082 return candidate_urls_.size() && candidate_urls_[payload_index_].size()
83 ? candidate_urls_[payload_index_][url_index_]
84 : "";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080085 }
86
Alex Deymo610277e2014-11-11 21:18:11 -080087 inline uint32_t GetUrlFailureCount() override {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080088 return url_failure_count_;
89 }
90
Alex Deymo610277e2014-11-11 21:18:11 -080091 inline uint32_t GetUrlSwitchCount() override {
David Zeuthencc6f9962013-04-18 11:57:24 -070092 return url_switch_count_;
93 }
94
Alex Deymo610277e2014-11-11 21:18:11 -080095 inline int GetNumResponsesSeen() override {
David Zeuthena573d6f2013-06-14 16:13:36 -070096 return num_responses_seen_;
97 }
98
Alex Deymo610277e2014-11-11 21:18:11 -080099 inline base::Time GetBackoffExpiryTime() override {
Jay Srinivasan08262882012-12-28 19:29:43 -0800100 return backoff_expiry_time_;
101 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800102
Alex Deymo610277e2014-11-11 21:18:11 -0800103 base::TimeDelta GetUpdateDuration() override;
David Zeuthen9a017f22013-04-11 16:10:26 -0700104
Alex Deymo610277e2014-11-11 21:18:11 -0800105 base::TimeDelta GetUpdateDurationUptime() override;
David Zeuthen9a017f22013-04-11 16:10:26 -0700106
Alex Deymo610277e2014-11-11 21:18:11 -0800107 inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) override {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700108 return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0;
109 }
110
Alex Deymo610277e2014-11-11 21:18:11 -0800111 inline uint64_t GetTotalBytesDownloaded(DownloadSource source) override {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700112 return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0;
113 }
114
Alex Deymo610277e2014-11-11 21:18:11 -0800115 inline uint32_t GetNumReboots() override {
Chris Sosabe45bef2013-04-09 18:25:12 -0700116 return num_reboots_;
117 }
118
Alex Deymo610277e2014-11-11 21:18:11 -0800119 void UpdateEngineStarted() override;
David Zeuthene4c58bf2013-06-18 17:26:50 -0700120
Alex Deymo610277e2014-11-11 21:18:11 -0800121 inline std::string GetRollbackVersion() override {
Chris Sosaaa18e162013-06-20 13:20:30 -0700122 return rollback_version_;
123 }
124
Alex Deymo610277e2014-11-11 21:18:11 -0800125 int GetP2PNumAttempts() override;
126 base::Time GetP2PFirstAttemptTimestamp() override;
127 void P2PNewAttempt() override;
128 bool P2PAttemptAllowed() override;
David Zeuthendcba8092013-08-06 12:16:35 -0700129
Gilad Arnold74b5f552014-10-07 08:17:16 -0700130 bool GetUsingP2PForDownloading() const override {
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700131 return using_p2p_for_downloading_;
132 }
133
Gilad Arnold74b5f552014-10-07 08:17:16 -0700134 bool GetUsingP2PForSharing() const override {
135 return using_p2p_for_sharing_;
136 }
137
Gilad Arnold519cfc72014-10-02 10:34:54 -0700138 base::TimeDelta GetScatteringWaitPeriod() override {
139 return scattering_wait_period_;
140 }
141
142 void SetScatteringWaitPeriod(base::TimeDelta wait_period) override;
143
Gilad Arnold74b5f552014-10-07 08:17:16 -0700144 void SetP2PUrl(const std::string& url) override {
145 p2p_url_ = url;
146 }
147
148 std::string GetP2PUrl() const override {
149 return p2p_url_;
150 }
151
Shuqian Zhao29971732016-02-05 11:29:32 -0800152 inline ErrorCode GetAttemptErrorCode() const override {
153 return attempt_error_code_;
154 }
155
Sen Jiang6c736682017-03-10 15:01:36 -0800156 bool NextPayload() override {
157 if (payload_index_ + 1 >= candidate_urls_.size())
158 return false;
159 payload_index_++;
160 url_index_ = 0;
161 return true;
162 }
163
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);
Chris Sosab3dcdb32013-09-04 15:22:12 -0700174 FRIEND_TEST(PayloadStateTest, RollbackVersion);
Alex Deymo42432912013-07-12 20:21:15 -0700175 FRIEND_TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs);
176
David Zeuthen33bae492014-02-25 16:16:18 -0800177 // Helper called when an attempt has begun, is called by
David Zeuthenafed4a12014-04-09 15:28:44 -0700178 // UpdateResumed(), UpdateRestarted() and Rollback().
179 void AttemptStarted(AttemptType attempt_type);
David Zeuthen33bae492014-02-25 16:16:18 -0800180
Alex Deymo820cc702013-06-28 15:43:46 -0700181 // Increments the payload attempt number used for metrics.
182 void IncrementPayloadAttemptNumber();
183
Jay Srinivasan08262882012-12-28 19:29:43 -0800184 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800185 // at the time of the next update check.
Alex Deymo820cc702013-06-28 15:43:46 -0700186 void IncrementFullPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800187
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800188 // Advances the current URL index to the next available one. If all URLs have
189 // been exhausted during the current payload download attempt (as indicated
190 // by the payload attempt number), then it will increment the payload attempt
David Zeuthencc6f9962013-04-18 11:57:24 -0700191 // number and wrap around again with the first URL in the list. This also
192 // updates the URL switch count, if needed.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800193 void IncrementUrlIndex();
194
195 // Increments the failure count of the current URL. If the configured max
196 // failure count is reached for this URL, it advances the current URL index
197 // to the next URL and resets the failure count for that URL.
198 void IncrementFailureCount();
199
Jay Srinivasan08262882012-12-28 19:29:43 -0800200 // Updates the backoff expiry time exponentially based on the current
201 // payload attempt number.
202 void UpdateBackoffExpiryTime();
203
Jay Srinivasan19409b72013-04-12 19:23:36 -0700204 // Updates the value of current download source based on the current URL
205 // index. If the download source is not one of the known sources, it's set
206 // to kNumDownloadSources.
207 void UpdateCurrentDownloadSource();
208
209 // Updates the various metrics corresponding with the given number of bytes
210 // that were downloaded recently.
211 void UpdateBytesDownloaded(size_t count);
212
David Zeuthen33bae492014-02-25 16:16:18 -0800213 // Calculates the PayloadType we're using.
214 PayloadType CalculatePayloadType();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700215
David Zeuthen33bae492014-02-25 16:16:18 -0800216 // Collects and reports the various metrics related to an update attempt.
217 void CollectAndReportAttemptMetrics(ErrorCode code);
David Zeuthencc6f9962013-04-18 11:57:24 -0700218
David Zeuthen4e1d1492014-04-25 13:12:27 -0700219 // Persists values related to the UpdateEngine.Attempt.* metrics so
220 // we can identify later if an update attempt ends abnormally.
221 void PersistAttemptMetrics();
222
223 // Clears persistent state previously set using AttemptMetricsPersist().
224 void ClearPersistedAttemptMetrics();
225
226 // Checks if persistent state previously set using AttemptMetricsPersist()
227 // exists and, if so, emits it with |attempt_result| set to
228 // metrics::AttemptResult::kAbnormalTermination.
229 void ReportAndClearPersistedAttemptMetrics();
230
David Zeuthen33bae492014-02-25 16:16:18 -0800231 // Collects and reports the various metrics related to a successful update.
232 void CollectAndReportSuccessfulUpdateMetrics();
Alex Deymo820cc702013-06-28 15:43:46 -0700233
Alex Deymo42432912013-07-12 20:21:15 -0700234 // Checks if we were expecting to be running in the new version but the
235 // boot into the new version failed for some reason. If that's the case, an
236 // UMA metric is sent reporting the number of attempts the same applied
237 // payload was attempted to reboot. This function is called by UpdateAttempter
238 // every time the update engine starts and there's no reboot pending.
239 void ReportFailedBootIfNeeded();
240
Jay Srinivasan08262882012-12-28 19:29:43 -0800241 // Resets all the persisted state values which are maintained relative to the
242 // current response signature. The response signature itself is not reset.
243 void ResetPersistedState();
244
Jay Srinivasan19409b72013-04-12 19:23:36 -0700245 // Resets the appropriate state related to download sources that need to be
246 // reset on a new update.
247 void ResetDownloadSourcesOnNewUpdate();
248
Chris Sosab3dcdb32013-09-04 15:22:12 -0700249 // Returns the persisted value from prefs_ for the given key. It also
250 // validates that the value returned is non-negative.
251 int64_t GetPersistedValue(const std::string& key);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700252
Jay Srinivasan08262882012-12-28 19:29:43 -0800253 // Calculates the response "signature", which is basically a string composed
254 // of the subset of the fields in the current response that affect the
255 // behavior of the PayloadState.
256 std::string CalculateResponseSignature();
257
258 // Initializes the current response signature from the persisted state.
259 void LoadResponseSignature();
260
261 // Sets the response signature to the given value. Also persists the value
262 // being set so that we resume from the save value in case of a process
263 // restart.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700264 void SetResponseSignature(const std::string& response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800265
266 // Initializes the payload attempt number from the persisted state.
267 void LoadPayloadAttemptNumber();
268
Alex Deymo820cc702013-06-28 15:43:46 -0700269 // Initializes the payload attempt number for full payloads from the persisted
270 // state.
271 void LoadFullPayloadAttemptNumber();
272
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800273 // Sets the payload attempt number to the given value. Also persists the
274 // value being set so that we resume from the same value in case of a process
275 // restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700276 void SetPayloadAttemptNumber(int payload_attempt_number);
277
278 // Sets the payload attempt number for full updates to the given value. Also
279 // persists the value being set so that we resume from the same value in case
280 // of a process restart.
281 void SetFullPayloadAttemptNumber(int payload_attempt_number);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800282
283 // Initializes the current URL index from the persisted state.
284 void LoadUrlIndex();
285
286 // Sets the current URL index to the given value. Also persists the value
287 // being set so that we resume from the same value in case of a process
288 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800289 void SetUrlIndex(uint32_t url_index);
290
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800291 // Initializes the current URL's failure count from the persisted stae.
292 void LoadUrlFailureCount();
293
294 // Sets the current URL's failure count to the given value. Also persists the
295 // value being set so that we resume from the same value in case of a process
296 // restart.
297 void SetUrlFailureCount(uint32_t url_failure_count);
298
David Zeuthencc6f9962013-04-18 11:57:24 -0700299 // Sets |url_switch_count_| to the given value and persists the value.
300 void SetUrlSwitchCount(uint32_t url_switch_count);
301
302 // Initializes |url_switch_count_| from the persisted stae.
303 void LoadUrlSwitchCount();
304
Jay Srinivasan08262882012-12-28 19:29:43 -0800305 // Initializes the backoff expiry time from the persisted state.
306 void LoadBackoffExpiryTime();
307
308 // Sets the backoff expiry time to the given value. Also persists the value
309 // being set so that we resume from the same value in case of a process
310 // restart.
311 void SetBackoffExpiryTime(const base::Time& new_time);
312
David Zeuthen9a017f22013-04-11 16:10:26 -0700313 // Initializes |update_timestamp_start_| from the persisted state.
314 void LoadUpdateTimestampStart();
315
316 // Sets |update_timestamp_start_| to the given value and persists the value.
317 void SetUpdateTimestampStart(const base::Time& value);
318
319 // Sets |update_timestamp_end_| to the given value. This is not persisted
320 // as it happens at the end of the update process where state is deleted
321 // anyway.
322 void SetUpdateTimestampEnd(const base::Time& value);
323
324 // Initializes |update_duration_uptime_| from the persisted state.
325 void LoadUpdateDurationUptime();
326
327 // Helper method used in SetUpdateDurationUptime() and
328 // CalculateUpdateDurationUptime().
329 void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
330 const base::Time& timestamp,
331 bool use_logging);
332
333 // Sets |update_duration_uptime_| to the given value and persists
334 // the value and sets |update_duration_uptime_timestamp_| to the
335 // current monotonic time.
336 void SetUpdateDurationUptime(const base::TimeDelta& value);
337
338 // Adds the difference between current monotonic time and
339 // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
340 // sets |update_duration_uptime_timestamp_| to current monotonic time.
341 void CalculateUpdateDurationUptime();
342
Jay Srinivasan19409b72013-04-12 19:23:36 -0700343 // Returns the full key for a download source given the prefix.
344 std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
345
346 // Loads the number of bytes that have been currently downloaded through the
347 // previous attempts from the persisted state for the given source. It's
348 // reset to 0 everytime we begin a full update and is continued from previous
349 // attempt if we're resuming the update.
350 void LoadCurrentBytesDownloaded(DownloadSource source);
351
352 // Sets the number of bytes that have been currently downloaded for the
353 // given source. This value is also persisted.
354 void SetCurrentBytesDownloaded(DownloadSource source,
355 uint64_t current_bytes_downloaded,
356 bool log);
357
358 // Loads the total number of bytes that have been downloaded (since the last
359 // successful update) from the persisted state for the given source. It's
360 // reset to 0 everytime we successfully apply an update and counts the bytes
361 // downloaded for both successful and failed attempts since then.
362 void LoadTotalBytesDownloaded(DownloadSource source);
363
364 // Sets the total number of bytes that have been downloaded so far for the
365 // given source. This value is also persisted.
366 void SetTotalBytesDownloaded(DownloadSource source,
367 uint64_t total_bytes_downloaded,
368 bool log);
369
Chris Sosaaa18e162013-06-20 13:20:30 -0700370 // Loads the blacklisted version from our prefs file.
371 void LoadRollbackVersion();
372
373 // Blacklists this version from getting AU'd to until we receive a new update
374 // response.
375 void SetRollbackVersion(const std::string& rollback_version);
376
377 // Clears any blacklisted version.
378 void ResetRollbackVersion();
379
Jay Srinivasan53173b92013-05-17 17:13:01 -0700380 inline uint32_t GetUrlIndex() {
Sen Jiang0affc2c2017-02-10 15:55:05 -0800381 return url_index_ ? std::min(candidate_urls_[payload_index_].size() - 1,
382 url_index_)
383 : 0;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700384 }
385
386 // Computes the list of candidate URLs from the total list of payload URLs in
387 // the Omaha response.
388 void ComputeCandidateUrls();
389
David Zeuthena573d6f2013-06-14 16:13:36 -0700390 // Sets |num_responses_seen_| and persist it to disk.
391 void SetNumResponsesSeen(int num_responses_seen);
392
393 // Initializes |num_responses_seen_| from persisted state.
394 void LoadNumResponsesSeen();
395
Chris Sosabe45bef2013-04-09 18:25:12 -0700396 // Initializes |num_reboots_| from the persisted state.
397 void LoadNumReboots();
398
399 // Sets |num_reboots| for the update attempt. Also persists the
400 // value being set so that we resume from the same value in case of a process
401 // restart.
402 void SetNumReboots(uint32_t num_reboots);
403
404 // Checks to see if the device rebooted since the last call and if so
405 // increments num_reboots.
406 void UpdateNumReboots();
407
David Zeuthene4c58bf2013-06-18 17:26:50 -0700408 // Writes the current wall-clock time to the kPrefsSystemUpdatedMarker
409 // state variable.
410 void CreateSystemUpdatedMarkerFile();
411
412 // Called at program startup if the device booted into a new update.
413 // The |time_to_reboot| parameter contains the (wall-clock) duration
414 // from when the update successfully completed (the value written
415 // into the kPrefsSystemUpdatedMarker state variable) until the device
416 // was booted into the update (current wall-clock time).
417 void BootedIntoUpdate(base::TimeDelta time_to_reboot);
418
David Zeuthendcba8092013-08-06 12:16:35 -0700419 // Loads the |kPrefsP2PFirstAttemptTimestamp| state variable from disk
420 // into |p2p_first_attempt_timestamp_|.
421 void LoadP2PFirstAttemptTimestamp();
422
423 // Loads the |kPrefsP2PNumAttempts| state variable into |p2p_num_attempts_|.
424 void LoadP2PNumAttempts();
425
426 // Sets the |kPrefsP2PNumAttempts| state variable to |value|.
427 void SetP2PNumAttempts(int value);
428
429 // Sets the |kPrefsP2PFirstAttemptTimestamp| state variable to |time|.
430 void SetP2PFirstAttemptTimestamp(const base::Time& time);
431
Gilad Arnold519cfc72014-10-02 10:34:54 -0700432 // Loads the persisted scattering wallclock-based wait period.
433 void LoadScatteringWaitPeriod();
434
Sen Jiang0affc2c2017-02-10 15:55:05 -0800435 // Get the total size of all payloads.
436 int64_t GetPayloadSize();
437
Gilad Arnold6e15aac2014-10-02 10:34:14 -0700438 // The global state of the system.
439 SystemState* system_state_;
440
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800441 // Interface object with which we read/write persisted state. This must
442 // be set by calling the Initialize method before calling any other method.
443 PrefsInterface* prefs_;
444
Chris Sosaaa18e162013-06-20 13:20:30 -0700445 // Interface object with which we read/write persisted state. This must
446 // be set by calling the Initialize method before calling any other method.
447 // This object persists across powerwashes.
448 PrefsInterface* powerwash_safe_prefs_;
449
Jay Srinivasan08262882012-12-28 19:29:43 -0800450 // This is the current response object from Omaha.
451 OmahaResponse response_;
452
Gilad Arnold74b5f552014-10-07 08:17:16 -0700453 // Whether P2P is being used for downloading and sharing.
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700454 bool using_p2p_for_downloading_;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700455 bool using_p2p_for_sharing_;
456
457 // Stores the P2P download URL, if one is used.
458 std::string p2p_url_;
459
460 // The cached value of |kPrefsP2PFirstAttemptTimestamp|.
461 base::Time p2p_first_attempt_timestamp_;
462
463 // The cached value of |kPrefsP2PNumAttempts|.
464 int p2p_num_attempts_;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700465
Jay Srinivasan08262882012-12-28 19:29:43 -0800466 // This stores a "signature" of the current response. The signature here
467 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800468 // this value is persisted so we resume from the same value in case of a
469 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800470 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800471
Alex Deymo820cc702013-06-28 15:43:46 -0700472 // The number of times we've tried to download the payload. This is
473 // incremented each time we download the payload successsfully or when we
474 // exhaust all failure limits for all URLs and are about to wrap around back
475 // to the first URL. Each update to this value is persisted so we resume from
476 // the same value in case of a process restart.
477 int payload_attempt_number_;
478
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800479 // The number of times we've tried to download the payload in full. This is
480 // incremented each time we download the payload in full successsfully or
481 // when we exhaust all failure limits for all URLs and are about to wrap
482 // around back to the first URL. Each update to this value is persisted so
483 // we resume from the same value in case of a process restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700484 int full_payload_attempt_number_;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800485
Sen Jiang0affc2c2017-02-10 15:55:05 -0800486 // The index of the current payload.
487 size_t payload_index_ = 0;
488
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800489 // The index of the current URL. This type is different from the one in the
490 // accessor methods because PrefsInterface supports only int64_t but we want
491 // to provide a stronger abstraction of uint32_t. Each update to this value
492 // is persisted so we resume from the same value in case of a process
493 // restart.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800494 size_t url_index_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800495
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800496 // The count of failures encountered in the current attempt to download using
497 // the current URL (specified by url_index_). Each update to this value is
498 // persisted so we resume from the same value in case of a process restart.
499 int64_t url_failure_count_;
500
David Zeuthencc6f9962013-04-18 11:57:24 -0700501 // The number of times we've switched URLs.
502 int32_t url_switch_count_;
503
Jay Srinivasan19409b72013-04-12 19:23:36 -0700504 // The current download source based on the current URL. This value is
505 // not persisted as it can be recomputed everytime we update the URL.
506 // We're storing this so as not to recompute this on every few bytes of
507 // data we read from the socket.
508 DownloadSource current_download_source_;
509
David Zeuthena573d6f2013-06-14 16:13:36 -0700510 // The number of different Omaha responses seen. Increases every time
511 // a new response is seen. Resets to 0 only when the system has been
512 // successfully updated.
513 int num_responses_seen_;
514
Chris Sosabe45bef2013-04-09 18:25:12 -0700515 // The number of system reboots during an update attempt. Technically since
516 // we don't go out of our way to not update it when not attempting an update,
517 // also records the number of reboots before the next update attempt starts.
518 uint32_t num_reboots_;
519
Jay Srinivasan08262882012-12-28 19:29:43 -0800520 // The timestamp until which we've to wait before attempting to download the
521 // payload again, so as to backoff repeated downloads.
522 base::Time backoff_expiry_time_;
523
David Zeuthen9a017f22013-04-11 16:10:26 -0700524 // The most recently calculated value of the update duration.
525 base::TimeDelta update_duration_current_;
526
527 // The point in time (wall-clock) that the update was started.
528 base::Time update_timestamp_start_;
529
530 // The point in time (wall-clock) that the update ended. If the update
531 // is still in progress, this is set to the Epoch (e.g. 0).
532 base::Time update_timestamp_end_;
533
534 // The update duration uptime
535 base::TimeDelta update_duration_uptime_;
536
537 // The monotonic time when |update_duration_uptime_| was last set
538 base::Time update_duration_uptime_timestamp_;
539
Jay Srinivasan19409b72013-04-12 19:23:36 -0700540 // The number of bytes that have been downloaded for each source for each new
541 // update attempt. If we resume an update, we'll continue from the previous
542 // value, but if we get a new response or if the previous attempt failed,
543 // we'll reset this to 0 to start afresh. Each update to this value is
544 // persisted so we resume from the same value in case of a process restart.
545 // The extra index in the array is to no-op accidental access in case the
546 // return value from GetCurrentDownloadSource is used without validation.
547 uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
548
549 // The number of bytes that have been downloaded for each source since the
550 // the last successful update. This is used to compute the overhead we incur.
551 // Each update to this value is persisted so we resume from the same value in
552 // case of a process restart.
553 // The extra index in the array is to no-op accidental access in case the
554 // return value from GetCurrentDownloadSource is used without validation.
555 uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
556
David Zeuthen9a017f22013-04-11 16:10:26 -0700557 // A small timespan used when comparing wall-clock times for coping
558 // with the fact that clocks drift and consequently are adjusted
559 // (either forwards or backwards) via NTP.
560 static const base::TimeDelta kDurationSlack;
561
Jay Srinivasan53173b92013-05-17 17:13:01 -0700562 // The ordered list of the subset of payload URL candidates which are
563 // allowed as per device policy.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800564 std::vector<std::vector<std::string>> candidate_urls_;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700565
Chris Sosaaa18e162013-06-20 13:20:30 -0700566 // This stores a blacklisted version set as part of rollback. When we rollback
567 // we store the version of the os from which we are rolling back from in order
568 // to guarantee that we do not re-update to it on the next au attempt after
569 // reboot.
570 std::string rollback_version_;
571
David Zeuthen33bae492014-02-25 16:16:18 -0800572 // The number of bytes downloaded per attempt.
573 int64_t attempt_num_bytes_downloaded_;
574
575 // The boot time when the attempt was started.
576 base::Time attempt_start_time_boot_;
577
578 // The monotonic time when the attempt was started.
579 base::Time attempt_start_time_monotonic_;
580
David Zeuthenb281f072014-04-02 10:20:19 -0700581 // The connection type when the attempt started.
582 metrics::ConnectionType attempt_connection_type_;
583
Shuqian Zhao29971732016-02-05 11:29:32 -0800584 // The attempt error code when the attempt finished.
585 ErrorCode attempt_error_code_;
586
David Zeuthenafed4a12014-04-09 15:28:44 -0700587 // Whether we're currently rolling back.
588 AttemptType attempt_type_;
589
Gilad Arnold519cfc72014-10-02 10:34:54 -0700590 // The current scattering wallclock-based wait period.
591 base::TimeDelta scattering_wait_period_;
592
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800593 DISALLOW_COPY_AND_ASSIGN(PayloadState);
594};
595
596} // namespace chromeos_update_engine
597
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700598#endif // UPDATE_ENGINE_PAYLOAD_STATE_H_