blob: 8eb541cde29ed999a1bdc26aabd0f04ca35c8b4c [file] [log] [blame]
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gilad Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_PAYLOAD_STATE_H_
6#define UPDATE_ENGINE_PAYLOAD_STATE_H_
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08007
Alex Vakulenkod2779df2014-06-16 13:19:00 -07008#include <string>
9#include <vector>
10
Alex Vakulenko75039d72014-03-25 12:36:28 -070011#include <base/time/time.h>
Alex Deymo42432912013-07-12 20:21:15 -070012#include <gtest/gtest_prod.h> // for FRIEND_TEST
Jay Srinivasan08262882012-12-28 19:29:43 -080013
David Zeuthenb281f072014-04-02 10:20:19 -070014#include "update_engine/metrics.h"
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080015#include "update_engine/payload_state_interface.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080016#include "update_engine/prefs_interface.h"
17
18namespace chromeos_update_engine {
19
Jay Srinivasan19409b72013-04-12 19:23:36 -070020class SystemState;
21
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080022// Encapsulates all the payload state required for download. This includes the
Jay Srinivasan08262882012-12-28 19:29:43 -080023// state necessary for handling multiple URLs in Omaha response, the backoff
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080024// state, etc. All state is persisted so that we use the most recently saved
25// value when resuming the update_engine process. All state is also cached in
26// memory so that we ensure we always make progress based on last known good
27// state even when there's any issue in reading/writing from the file system.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080028class PayloadState : public PayloadStateInterface {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080029 public:
Jay Srinivasan19409b72013-04-12 19:23:36 -070030 PayloadState();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080031 virtual ~PayloadState() {}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080032
Jay Srinivasan19409b72013-04-12 19:23:36 -070033 // Initializes a payload state object using the given global system state.
34 // It performs the initial loading of all persisted state into memory and
35 // dumps the initial state for debugging purposes. Note: the other methods
36 // should be called only after calling Initialize on this object.
37 bool Initialize(SystemState* system_state);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080038
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080039 // Implementation of PayloadStateInterface methods.
40 virtual void SetResponse(const OmahaResponse& response);
41 virtual void DownloadComplete();
42 virtual void DownloadProgress(size_t count);
Chris Sosabe45bef2013-04-09 18:25:12 -070043 virtual void UpdateResumed();
Jay Srinivasan19409b72013-04-12 19:23:36 -070044 virtual void UpdateRestarted();
David Zeuthen9a017f22013-04-11 16:10:26 -070045 virtual void UpdateSucceeded();
David Zeuthena99981f2013-04-29 13:42:47 -070046 virtual void UpdateFailed(ErrorCode error);
Alex Deymo42432912013-07-12 20:21:15 -070047 virtual void ResetUpdateStatus();
Jay Srinivasan08262882012-12-28 19:29:43 -080048 virtual bool ShouldBackoffDownload();
Chris Sosaaa18e162013-06-20 13:20:30 -070049 virtual void Rollback();
Alex Deymo42432912013-07-12 20:21:15 -070050 virtual void ExpectRebootInNewVersion(const std::string& target_version_uid);
David Zeuthenbb8bdc72013-09-03 13:43:48 -070051 virtual void SetUsingP2PForDownloading(bool value);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080052
Jay Srinivasan08262882012-12-28 19:29:43 -080053 virtual inline std::string GetResponseSignature() {
54 return response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080055 }
56
Alex Deymo820cc702013-06-28 15:43:46 -070057 virtual inline int GetFullPayloadAttemptNumber() {
58 return full_payload_attempt_number_;
59 }
60
61 virtual inline int GetPayloadAttemptNumber() {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080062 return payload_attempt_number_;
63 }
64
Jay Srinivasan53173b92013-05-17 17:13:01 -070065 virtual inline std::string GetCurrentUrl() {
66 return candidate_urls_.size() ? candidate_urls_[url_index_] : "";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080067 }
68
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080069 virtual inline uint32_t GetUrlFailureCount() {
70 return url_failure_count_;
71 }
72
David Zeuthencc6f9962013-04-18 11:57:24 -070073 virtual inline uint32_t GetUrlSwitchCount() {
74 return url_switch_count_;
75 }
76
David Zeuthena573d6f2013-06-14 16:13:36 -070077 virtual inline int GetNumResponsesSeen() {
78 return num_responses_seen_;
79 }
80
Jay Srinivasan08262882012-12-28 19:29:43 -080081 virtual inline base::Time GetBackoffExpiryTime() {
82 return backoff_expiry_time_;
83 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080084
David Zeuthen9a017f22013-04-11 16:10:26 -070085 virtual base::TimeDelta GetUpdateDuration();
86
87 virtual base::TimeDelta GetUpdateDurationUptime();
88
Jay Srinivasan19409b72013-04-12 19:23:36 -070089 virtual inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) {
90 return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0;
91 }
92
93 virtual inline uint64_t GetTotalBytesDownloaded(DownloadSource source) {
94 return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0;
95 }
96
Chris Sosabe45bef2013-04-09 18:25:12 -070097 virtual inline uint32_t GetNumReboots() {
98 return num_reboots_;
99 }
100
David Zeuthene4c58bf2013-06-18 17:26:50 -0700101 virtual void UpdateEngineStarted();
102
Chris Sosaaa18e162013-06-20 13:20:30 -0700103 virtual inline std::string GetRollbackVersion() {
104 return rollback_version_;
105 }
106
David Zeuthendcba8092013-08-06 12:16:35 -0700107 virtual int GetP2PNumAttempts();
108 virtual base::Time GetP2PFirstAttemptTimestamp();
109 virtual void P2PNewAttempt();
110 virtual bool P2PAttemptAllowed();
111
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700112 virtual bool GetUsingP2PForDownloading() {
113 return using_p2p_for_downloading_;
114 }
115
Gilad Arnold519cfc72014-10-02 10:34:54 -0700116 base::TimeDelta GetScatteringWaitPeriod() override {
117 return scattering_wait_period_;
118 }
119
120 void SetScatteringWaitPeriod(base::TimeDelta wait_period) override;
121
Jay Srinivasan08262882012-12-28 19:29:43 -0800122 private:
David Zeuthenafed4a12014-04-09 15:28:44 -0700123 enum class AttemptType {
124 kUpdate,
125 kRollback,
126 };
127
Alex Deymo42432912013-07-12 20:21:15 -0700128 friend class PayloadStateTest;
129 FRIEND_TEST(PayloadStateTest, RebootAfterUpdateFailedMetric);
130 FRIEND_TEST(PayloadStateTest, RebootAfterUpdateSucceed);
131 FRIEND_TEST(PayloadStateTest, RebootAfterCanceledUpdate);
Chris Sosab3dcdb32013-09-04 15:22:12 -0700132 FRIEND_TEST(PayloadStateTest, RollbackVersion);
Alex Deymo42432912013-07-12 20:21:15 -0700133 FRIEND_TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs);
134
David Zeuthen33bae492014-02-25 16:16:18 -0800135 // Helper called when an attempt has begun, is called by
David Zeuthenafed4a12014-04-09 15:28:44 -0700136 // UpdateResumed(), UpdateRestarted() and Rollback().
137 void AttemptStarted(AttemptType attempt_type);
David Zeuthen33bae492014-02-25 16:16:18 -0800138
Alex Deymo820cc702013-06-28 15:43:46 -0700139 // Increments the payload attempt number used for metrics.
140 void IncrementPayloadAttemptNumber();
141
Jay Srinivasan08262882012-12-28 19:29:43 -0800142 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800143 // at the time of the next update check.
Alex Deymo820cc702013-06-28 15:43:46 -0700144 void IncrementFullPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800145
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800146 // Advances the current URL index to the next available one. If all URLs have
147 // been exhausted during the current payload download attempt (as indicated
148 // by the payload attempt number), then it will increment the payload attempt
David Zeuthencc6f9962013-04-18 11:57:24 -0700149 // number and wrap around again with the first URL in the list. This also
150 // updates the URL switch count, if needed.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800151 void IncrementUrlIndex();
152
153 // Increments the failure count of the current URL. If the configured max
154 // failure count is reached for this URL, it advances the current URL index
155 // to the next URL and resets the failure count for that URL.
156 void IncrementFailureCount();
157
Jay Srinivasan08262882012-12-28 19:29:43 -0800158 // Updates the backoff expiry time exponentially based on the current
159 // payload attempt number.
160 void UpdateBackoffExpiryTime();
161
Jay Srinivasan19409b72013-04-12 19:23:36 -0700162 // Updates the value of current download source based on the current URL
163 // index. If the download source is not one of the known sources, it's set
164 // to kNumDownloadSources.
165 void UpdateCurrentDownloadSource();
166
167 // Updates the various metrics corresponding with the given number of bytes
168 // that were downloaded recently.
169 void UpdateBytesDownloaded(size_t count);
170
David Zeuthen33bae492014-02-25 16:16:18 -0800171 // Calculates the PayloadType we're using.
172 PayloadType CalculatePayloadType();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700173
David Zeuthen33bae492014-02-25 16:16:18 -0800174 // Collects and reports the various metrics related to an update attempt.
175 void CollectAndReportAttemptMetrics(ErrorCode code);
David Zeuthencc6f9962013-04-18 11:57:24 -0700176
David Zeuthen4e1d1492014-04-25 13:12:27 -0700177 // Persists values related to the UpdateEngine.Attempt.* metrics so
178 // we can identify later if an update attempt ends abnormally.
179 void PersistAttemptMetrics();
180
181 // Clears persistent state previously set using AttemptMetricsPersist().
182 void ClearPersistedAttemptMetrics();
183
184 // Checks if persistent state previously set using AttemptMetricsPersist()
185 // exists and, if so, emits it with |attempt_result| set to
186 // metrics::AttemptResult::kAbnormalTermination.
187 void ReportAndClearPersistedAttemptMetrics();
188
David Zeuthen33bae492014-02-25 16:16:18 -0800189 // Collects and reports the various metrics related to a successful update.
190 void CollectAndReportSuccessfulUpdateMetrics();
Alex Deymo820cc702013-06-28 15:43:46 -0700191
Alex Deymo42432912013-07-12 20:21:15 -0700192 // Checks if we were expecting to be running in the new version but the
193 // boot into the new version failed for some reason. If that's the case, an
194 // UMA metric is sent reporting the number of attempts the same applied
195 // payload was attempted to reboot. This function is called by UpdateAttempter
196 // every time the update engine starts and there's no reboot pending.
197 void ReportFailedBootIfNeeded();
198
Jay Srinivasan08262882012-12-28 19:29:43 -0800199 // Resets all the persisted state values which are maintained relative to the
200 // current response signature. The response signature itself is not reset.
201 void ResetPersistedState();
202
Jay Srinivasan19409b72013-04-12 19:23:36 -0700203 // Resets the appropriate state related to download sources that need to be
204 // reset on a new update.
205 void ResetDownloadSourcesOnNewUpdate();
206
Chris Sosab3dcdb32013-09-04 15:22:12 -0700207 // Returns the persisted value from prefs_ for the given key. It also
208 // validates that the value returned is non-negative.
209 int64_t GetPersistedValue(const std::string& key);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700210
Jay Srinivasan08262882012-12-28 19:29:43 -0800211 // Calculates the response "signature", which is basically a string composed
212 // of the subset of the fields in the current response that affect the
213 // behavior of the PayloadState.
214 std::string CalculateResponseSignature();
215
216 // Initializes the current response signature from the persisted state.
217 void LoadResponseSignature();
218
219 // Sets the response signature to the given value. Also persists the value
220 // being set so that we resume from the save value in case of a process
221 // restart.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700222 void SetResponseSignature(const std::string& response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800223
224 // Initializes the payload attempt number from the persisted state.
225 void LoadPayloadAttemptNumber();
226
Alex Deymo820cc702013-06-28 15:43:46 -0700227 // Initializes the payload attempt number for full payloads from the persisted
228 // state.
229 void LoadFullPayloadAttemptNumber();
230
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800231 // Sets the payload attempt number to the given value. Also persists the
232 // value being set so that we resume from the same value in case of a process
233 // restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700234 void SetPayloadAttemptNumber(int payload_attempt_number);
235
236 // Sets the payload attempt number for full updates to the given value. Also
237 // persists the value being set so that we resume from the same value in case
238 // of a process restart.
239 void SetFullPayloadAttemptNumber(int payload_attempt_number);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800240
241 // Initializes the current URL index from the persisted state.
242 void LoadUrlIndex();
243
244 // Sets the current URL index to the given value. Also persists the value
245 // being set so that we resume from the same value in case of a process
246 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800247 void SetUrlIndex(uint32_t url_index);
248
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800249 // Initializes the current URL's failure count from the persisted stae.
250 void LoadUrlFailureCount();
251
252 // Sets the current URL's failure count to the given value. Also persists the
253 // value being set so that we resume from the same value in case of a process
254 // restart.
255 void SetUrlFailureCount(uint32_t url_failure_count);
256
David Zeuthencc6f9962013-04-18 11:57:24 -0700257 // Sets |url_switch_count_| to the given value and persists the value.
258 void SetUrlSwitchCount(uint32_t url_switch_count);
259
260 // Initializes |url_switch_count_| from the persisted stae.
261 void LoadUrlSwitchCount();
262
Jay Srinivasan08262882012-12-28 19:29:43 -0800263 // Initializes the backoff expiry time from the persisted state.
264 void LoadBackoffExpiryTime();
265
266 // Sets the backoff expiry time to the given value. Also persists the value
267 // being set so that we resume from the same value in case of a process
268 // restart.
269 void SetBackoffExpiryTime(const base::Time& new_time);
270
David Zeuthen9a017f22013-04-11 16:10:26 -0700271 // Initializes |update_timestamp_start_| from the persisted state.
272 void LoadUpdateTimestampStart();
273
274 // Sets |update_timestamp_start_| to the given value and persists the value.
275 void SetUpdateTimestampStart(const base::Time& value);
276
277 // Sets |update_timestamp_end_| to the given value. This is not persisted
278 // as it happens at the end of the update process where state is deleted
279 // anyway.
280 void SetUpdateTimestampEnd(const base::Time& value);
281
282 // Initializes |update_duration_uptime_| from the persisted state.
283 void LoadUpdateDurationUptime();
284
285 // Helper method used in SetUpdateDurationUptime() and
286 // CalculateUpdateDurationUptime().
287 void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
288 const base::Time& timestamp,
289 bool use_logging);
290
291 // Sets |update_duration_uptime_| to the given value and persists
292 // the value and sets |update_duration_uptime_timestamp_| to the
293 // current monotonic time.
294 void SetUpdateDurationUptime(const base::TimeDelta& value);
295
296 // Adds the difference between current monotonic time and
297 // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
298 // sets |update_duration_uptime_timestamp_| to current monotonic time.
299 void CalculateUpdateDurationUptime();
300
Jay Srinivasan19409b72013-04-12 19:23:36 -0700301 // Returns the full key for a download source given the prefix.
302 std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
303
304 // Loads the number of bytes that have been currently downloaded through the
305 // previous attempts from the persisted state for the given source. It's
306 // reset to 0 everytime we begin a full update and is continued from previous
307 // attempt if we're resuming the update.
308 void LoadCurrentBytesDownloaded(DownloadSource source);
309
310 // Sets the number of bytes that have been currently downloaded for the
311 // given source. This value is also persisted.
312 void SetCurrentBytesDownloaded(DownloadSource source,
313 uint64_t current_bytes_downloaded,
314 bool log);
315
316 // Loads the total number of bytes that have been downloaded (since the last
317 // successful update) from the persisted state for the given source. It's
318 // reset to 0 everytime we successfully apply an update and counts the bytes
319 // downloaded for both successful and failed attempts since then.
320 void LoadTotalBytesDownloaded(DownloadSource source);
321
322 // Sets the total number of bytes that have been downloaded so far for the
323 // given source. This value is also persisted.
324 void SetTotalBytesDownloaded(DownloadSource source,
325 uint64_t total_bytes_downloaded,
326 bool log);
327
Chris Sosaaa18e162013-06-20 13:20:30 -0700328 // Loads the blacklisted version from our prefs file.
329 void LoadRollbackVersion();
330
331 // Blacklists this version from getting AU'd to until we receive a new update
332 // response.
333 void SetRollbackVersion(const std::string& rollback_version);
334
335 // Clears any blacklisted version.
336 void ResetRollbackVersion();
337
Jay Srinivasan53173b92013-05-17 17:13:01 -0700338 inline uint32_t GetUrlIndex() {
339 return url_index_;
340 }
341
342 // Computes the list of candidate URLs from the total list of payload URLs in
343 // the Omaha response.
344 void ComputeCandidateUrls();
345
David Zeuthena573d6f2013-06-14 16:13:36 -0700346 // Sets |num_responses_seen_| and persist it to disk.
347 void SetNumResponsesSeen(int num_responses_seen);
348
349 // Initializes |num_responses_seen_| from persisted state.
350 void LoadNumResponsesSeen();
351
Alex Deymob33b0f02013-08-08 21:10:02 -0700352 // Reports metric conveying how many times updates were abandoned since
353 // the last update was applied. The difference between this metric and the
354 // previous ReportUpdatesAbandonedCountMetric() one is that this metric is
355 // reported every time an update is abandoned, as oposed to the mentioned
356 // metric that is reported once the new update was applied.
357 void ReportUpdatesAbandonedEventCountMetric();
358
Chris Sosabe45bef2013-04-09 18:25:12 -0700359 // Initializes |num_reboots_| from the persisted state.
360 void LoadNumReboots();
361
362 // Sets |num_reboots| for the update attempt. Also persists the
363 // value being set so that we resume from the same value in case of a process
364 // restart.
365 void SetNumReboots(uint32_t num_reboots);
366
367 // Checks to see if the device rebooted since the last call and if so
368 // increments num_reboots.
369 void UpdateNumReboots();
370
David Zeuthene4c58bf2013-06-18 17:26:50 -0700371 // Writes the current wall-clock time to the kPrefsSystemUpdatedMarker
372 // state variable.
373 void CreateSystemUpdatedMarkerFile();
374
375 // Called at program startup if the device booted into a new update.
376 // The |time_to_reboot| parameter contains the (wall-clock) duration
377 // from when the update successfully completed (the value written
378 // into the kPrefsSystemUpdatedMarker state variable) until the device
379 // was booted into the update (current wall-clock time).
380 void BootedIntoUpdate(base::TimeDelta time_to_reboot);
381
David Zeuthendcba8092013-08-06 12:16:35 -0700382 // Loads the |kPrefsP2PFirstAttemptTimestamp| state variable from disk
383 // into |p2p_first_attempt_timestamp_|.
384 void LoadP2PFirstAttemptTimestamp();
385
386 // Loads the |kPrefsP2PNumAttempts| state variable into |p2p_num_attempts_|.
387 void LoadP2PNumAttempts();
388
389 // Sets the |kPrefsP2PNumAttempts| state variable to |value|.
390 void SetP2PNumAttempts(int value);
391
392 // Sets the |kPrefsP2PFirstAttemptTimestamp| state variable to |time|.
393 void SetP2PFirstAttemptTimestamp(const base::Time& time);
394
Gilad Arnold519cfc72014-10-02 10:34:54 -0700395 // Loads the persisted scattering wallclock-based wait period.
396 void LoadScatteringWaitPeriod();
397
Gilad Arnold6e15aac2014-10-02 10:34:14 -0700398 // The global state of the system.
399 SystemState* system_state_;
400
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800401 // Interface object with which we read/write persisted state. This must
402 // be set by calling the Initialize method before calling any other method.
403 PrefsInterface* prefs_;
404
Chris Sosaaa18e162013-06-20 13:20:30 -0700405 // Interface object with which we read/write persisted state. This must
406 // be set by calling the Initialize method before calling any other method.
407 // This object persists across powerwashes.
408 PrefsInterface* powerwash_safe_prefs_;
409
Jay Srinivasan08262882012-12-28 19:29:43 -0800410 // This is the current response object from Omaha.
411 OmahaResponse response_;
412
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700413 // Whether p2p is being used for downloading as set with the
414 // SetUsingP2PForDownloading() method.
415 bool using_p2p_for_downloading_;
416
Jay Srinivasan08262882012-12-28 19:29:43 -0800417 // This stores a "signature" of the current response. The signature here
418 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800419 // this value is persisted so we resume from the same value in case of a
420 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800421 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800422
Alex Deymo820cc702013-06-28 15:43:46 -0700423 // The number of times we've tried to download the payload. This is
424 // incremented each time we download the payload successsfully or when we
425 // exhaust all failure limits for all URLs and are about to wrap around back
426 // to the first URL. Each update to this value is persisted so we resume from
427 // the same value in case of a process restart.
428 int payload_attempt_number_;
429
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800430 // The number of times we've tried to download the payload in full. This is
431 // incremented each time we download the payload in full successsfully or
432 // when we exhaust all failure limits for all URLs and are about to wrap
433 // around back to the first URL. Each update to this value is persisted so
434 // we resume from the same value in case of a process restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700435 int full_payload_attempt_number_;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800436
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800437 // The index of the current URL. This type is different from the one in the
438 // accessor methods because PrefsInterface supports only int64_t but we want
439 // to provide a stronger abstraction of uint32_t. Each update to this value
440 // is persisted so we resume from the same value in case of a process
441 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800442 int64_t url_index_;
443
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800444 // The count of failures encountered in the current attempt to download using
445 // the current URL (specified by url_index_). Each update to this value is
446 // persisted so we resume from the same value in case of a process restart.
447 int64_t url_failure_count_;
448
David Zeuthencc6f9962013-04-18 11:57:24 -0700449 // The number of times we've switched URLs.
450 int32_t url_switch_count_;
451
Jay Srinivasan19409b72013-04-12 19:23:36 -0700452 // The current download source based on the current URL. This value is
453 // not persisted as it can be recomputed everytime we update the URL.
454 // We're storing this so as not to recompute this on every few bytes of
455 // data we read from the socket.
456 DownloadSource current_download_source_;
457
David Zeuthena573d6f2013-06-14 16:13:36 -0700458 // The number of different Omaha responses seen. Increases every time
459 // a new response is seen. Resets to 0 only when the system has been
460 // successfully updated.
461 int num_responses_seen_;
462
Chris Sosabe45bef2013-04-09 18:25:12 -0700463 // The number of system reboots during an update attempt. Technically since
464 // we don't go out of our way to not update it when not attempting an update,
465 // also records the number of reboots before the next update attempt starts.
466 uint32_t num_reboots_;
467
Jay Srinivasan08262882012-12-28 19:29:43 -0800468 // The timestamp until which we've to wait before attempting to download the
469 // payload again, so as to backoff repeated downloads.
470 base::Time backoff_expiry_time_;
471
David Zeuthen9a017f22013-04-11 16:10:26 -0700472 // The most recently calculated value of the update duration.
473 base::TimeDelta update_duration_current_;
474
475 // The point in time (wall-clock) that the update was started.
476 base::Time update_timestamp_start_;
477
478 // The point in time (wall-clock) that the update ended. If the update
479 // is still in progress, this is set to the Epoch (e.g. 0).
480 base::Time update_timestamp_end_;
481
482 // The update duration uptime
483 base::TimeDelta update_duration_uptime_;
484
485 // The monotonic time when |update_duration_uptime_| was last set
486 base::Time update_duration_uptime_timestamp_;
487
Jay Srinivasan19409b72013-04-12 19:23:36 -0700488 // The number of bytes that have been downloaded for each source for each new
489 // update attempt. If we resume an update, we'll continue from the previous
490 // value, but if we get a new response or if the previous attempt failed,
491 // we'll reset this to 0 to start afresh. Each update to this value is
492 // persisted so we resume from the same value in case of a process restart.
493 // The extra index in the array is to no-op accidental access in case the
494 // return value from GetCurrentDownloadSource is used without validation.
495 uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
496
497 // The number of bytes that have been downloaded for each source since the
498 // the last successful update. This is used to compute the overhead we incur.
499 // Each update to this value is persisted so we resume from the same value in
500 // case of a process restart.
501 // The extra index in the array is to no-op accidental access in case the
502 // return value from GetCurrentDownloadSource is used without validation.
503 uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
504
David Zeuthen9a017f22013-04-11 16:10:26 -0700505 // A small timespan used when comparing wall-clock times for coping
506 // with the fact that clocks drift and consequently are adjusted
507 // (either forwards or backwards) via NTP.
508 static const base::TimeDelta kDurationSlack;
509
Jay Srinivasan53173b92013-05-17 17:13:01 -0700510 // The ordered list of the subset of payload URL candidates which are
511 // allowed as per device policy.
512 std::vector<std::string> candidate_urls_;
513
Chris Sosaaa18e162013-06-20 13:20:30 -0700514 // This stores a blacklisted version set as part of rollback. When we rollback
515 // we store the version of the os from which we are rolling back from in order
516 // to guarantee that we do not re-update to it on the next au attempt after
517 // reboot.
518 std::string rollback_version_;
519
David Zeuthendcba8092013-08-06 12:16:35 -0700520 // The cached value of |kPrefsP2PFirstAttemptTimestamp|.
521 base::Time p2p_first_attempt_timestamp_;
522
523 // The cached value of |kPrefsP2PNumAttempts|.
524 int p2p_num_attempts_;
525
David Zeuthen33bae492014-02-25 16:16:18 -0800526 // The number of bytes downloaded per attempt.
527 int64_t attempt_num_bytes_downloaded_;
528
529 // The boot time when the attempt was started.
530 base::Time attempt_start_time_boot_;
531
532 // The monotonic time when the attempt was started.
533 base::Time attempt_start_time_monotonic_;
534
David Zeuthenb281f072014-04-02 10:20:19 -0700535 // The connection type when the attempt started.
536 metrics::ConnectionType attempt_connection_type_;
537
David Zeuthenafed4a12014-04-09 15:28:44 -0700538 // Whether we're currently rolling back.
539 AttemptType attempt_type_;
540
Gilad Arnold519cfc72014-10-02 10:34:54 -0700541 // The current scattering wallclock-based wait period.
542 base::TimeDelta scattering_wait_period_;
543
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800544 DISALLOW_COPY_AND_ASSIGN(PayloadState);
545};
546
547} // namespace chromeos_update_engine
548
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700549#endif // UPDATE_ENGINE_PAYLOAD_STATE_H_