blob: f8cc678a18b064a8a6c31636ccba828b4370707c [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
Alex Deymo759c2752014-03-17 21:09:36 -07005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H_
Jay Srinivasan6f6ea002012-12-14 11:26:28 -08007
Alex Vakulenko75039d72014-03-25 12:36:28 -07008#include <base/time/time.h>
Alex Deymo42432912013-07-12 20:21:15 -07009#include <gtest/gtest_prod.h> // for FRIEND_TEST
Jay Srinivasan08262882012-12-28 19:29:43 -080010
David Zeuthenb281f072014-04-02 10:20:19 -070011#include "update_engine/metrics.h"
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080012#include "update_engine/payload_state_interface.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080013#include "update_engine/prefs_interface.h"
14
15namespace chromeos_update_engine {
16
Jay Srinivasan19409b72013-04-12 19:23:36 -070017class SystemState;
18
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080019// Encapsulates all the payload state required for download. This includes the
Jay Srinivasan08262882012-12-28 19:29:43 -080020// state necessary for handling multiple URLs in Omaha response, the backoff
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080021// state, etc. All state is persisted so that we use the most recently saved
22// value when resuming the update_engine process. All state is also cached in
23// memory so that we ensure we always make progress based on last known good
24// state even when there's any issue in reading/writing from the file system.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080025class PayloadState : public PayloadStateInterface {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080026 public:
Jay Srinivasan19409b72013-04-12 19:23:36 -070027 PayloadState();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080028 virtual ~PayloadState() {}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080029
Jay Srinivasan19409b72013-04-12 19:23:36 -070030 // Initializes a payload state object using the given global system state.
31 // It performs the initial loading of all persisted state into memory and
32 // dumps the initial state for debugging purposes. Note: the other methods
33 // should be called only after calling Initialize on this object.
34 bool Initialize(SystemState* system_state);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080035
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080036 // Implementation of PayloadStateInterface methods.
37 virtual void SetResponse(const OmahaResponse& response);
38 virtual void DownloadComplete();
39 virtual void DownloadProgress(size_t count);
Chris Sosabe45bef2013-04-09 18:25:12 -070040 virtual void UpdateResumed();
Jay Srinivasan19409b72013-04-12 19:23:36 -070041 virtual void UpdateRestarted();
David Zeuthen9a017f22013-04-11 16:10:26 -070042 virtual void UpdateSucceeded();
David Zeuthena99981f2013-04-29 13:42:47 -070043 virtual void UpdateFailed(ErrorCode error);
Alex Deymo42432912013-07-12 20:21:15 -070044 virtual void ResetUpdateStatus();
Jay Srinivasan08262882012-12-28 19:29:43 -080045 virtual bool ShouldBackoffDownload();
Chris Sosaaa18e162013-06-20 13:20:30 -070046 virtual void Rollback();
Alex Deymo42432912013-07-12 20:21:15 -070047 virtual void ExpectRebootInNewVersion(const std::string& target_version_uid);
David Zeuthenbb8bdc72013-09-03 13:43:48 -070048 virtual void SetUsingP2PForDownloading(bool value);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080049
Jay Srinivasan08262882012-12-28 19:29:43 -080050 virtual inline std::string GetResponseSignature() {
51 return response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080052 }
53
Alex Deymo820cc702013-06-28 15:43:46 -070054 virtual inline int GetFullPayloadAttemptNumber() {
55 return full_payload_attempt_number_;
56 }
57
58 virtual inline int GetPayloadAttemptNumber() {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080059 return payload_attempt_number_;
60 }
61
Jay Srinivasan53173b92013-05-17 17:13:01 -070062 virtual inline std::string GetCurrentUrl() {
63 return candidate_urls_.size() ? candidate_urls_[url_index_] : "";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080064 }
65
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080066 virtual inline uint32_t GetUrlFailureCount() {
67 return url_failure_count_;
68 }
69
David Zeuthencc6f9962013-04-18 11:57:24 -070070 virtual inline uint32_t GetUrlSwitchCount() {
71 return url_switch_count_;
72 }
73
David Zeuthena573d6f2013-06-14 16:13:36 -070074 virtual inline int GetNumResponsesSeen() {
75 return num_responses_seen_;
76 }
77
Jay Srinivasan08262882012-12-28 19:29:43 -080078 virtual inline base::Time GetBackoffExpiryTime() {
79 return backoff_expiry_time_;
80 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080081
David Zeuthen9a017f22013-04-11 16:10:26 -070082 virtual base::TimeDelta GetUpdateDuration();
83
84 virtual base::TimeDelta GetUpdateDurationUptime();
85
Jay Srinivasan19409b72013-04-12 19:23:36 -070086 virtual inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) {
87 return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0;
88 }
89
90 virtual inline uint64_t GetTotalBytesDownloaded(DownloadSource source) {
91 return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0;
92 }
93
Chris Sosabe45bef2013-04-09 18:25:12 -070094 virtual inline uint32_t GetNumReboots() {
95 return num_reboots_;
96 }
97
David Zeuthene4c58bf2013-06-18 17:26:50 -070098 virtual void UpdateEngineStarted();
99
Chris Sosaaa18e162013-06-20 13:20:30 -0700100 virtual inline std::string GetRollbackVersion() {
101 return rollback_version_;
102 }
103
David Zeuthendcba8092013-08-06 12:16:35 -0700104 virtual int GetP2PNumAttempts();
105 virtual base::Time GetP2PFirstAttemptTimestamp();
106 virtual void P2PNewAttempt();
107 virtual bool P2PAttemptAllowed();
108
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700109 virtual bool GetUsingP2PForDownloading() {
110 return using_p2p_for_downloading_;
111 }
112
Jay Srinivasan08262882012-12-28 19:29:43 -0800113 private:
David Zeuthenafed4a12014-04-09 15:28:44 -0700114 enum class AttemptType {
115 kUpdate,
116 kRollback,
117 };
118
Alex Deymo42432912013-07-12 20:21:15 -0700119 friend class PayloadStateTest;
120 FRIEND_TEST(PayloadStateTest, RebootAfterUpdateFailedMetric);
121 FRIEND_TEST(PayloadStateTest, RebootAfterUpdateSucceed);
122 FRIEND_TEST(PayloadStateTest, RebootAfterCanceledUpdate);
Chris Sosab3dcdb32013-09-04 15:22:12 -0700123 FRIEND_TEST(PayloadStateTest, RollbackVersion);
Alex Deymo42432912013-07-12 20:21:15 -0700124 FRIEND_TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs);
125
David Zeuthen33bae492014-02-25 16:16:18 -0800126 // Helper called when an attempt has begun, is called by
David Zeuthenafed4a12014-04-09 15:28:44 -0700127 // UpdateResumed(), UpdateRestarted() and Rollback().
128 void AttemptStarted(AttemptType attempt_type);
David Zeuthen33bae492014-02-25 16:16:18 -0800129
Alex Deymo820cc702013-06-28 15:43:46 -0700130 // Increments the payload attempt number used for metrics.
131 void IncrementPayloadAttemptNumber();
132
Jay Srinivasan08262882012-12-28 19:29:43 -0800133 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800134 // at the time of the next update check.
Alex Deymo820cc702013-06-28 15:43:46 -0700135 void IncrementFullPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800136
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800137 // Advances the current URL index to the next available one. If all URLs have
138 // been exhausted during the current payload download attempt (as indicated
139 // by the payload attempt number), then it will increment the payload attempt
David Zeuthencc6f9962013-04-18 11:57:24 -0700140 // number and wrap around again with the first URL in the list. This also
141 // updates the URL switch count, if needed.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800142 void IncrementUrlIndex();
143
144 // Increments the failure count of the current URL. If the configured max
145 // failure count is reached for this URL, it advances the current URL index
146 // to the next URL and resets the failure count for that URL.
147 void IncrementFailureCount();
148
Jay Srinivasan08262882012-12-28 19:29:43 -0800149 // Updates the backoff expiry time exponentially based on the current
150 // payload attempt number.
151 void UpdateBackoffExpiryTime();
152
Jay Srinivasan19409b72013-04-12 19:23:36 -0700153 // Updates the value of current download source based on the current URL
154 // index. If the download source is not one of the known sources, it's set
155 // to kNumDownloadSources.
156 void UpdateCurrentDownloadSource();
157
158 // Updates the various metrics corresponding with the given number of bytes
159 // that were downloaded recently.
160 void UpdateBytesDownloaded(size_t count);
161
David Zeuthen33bae492014-02-25 16:16:18 -0800162 // Calculates the PayloadType we're using.
163 PayloadType CalculatePayloadType();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700164
David Zeuthen33bae492014-02-25 16:16:18 -0800165 // Collects and reports the various metrics related to an update attempt.
166 void CollectAndReportAttemptMetrics(ErrorCode code);
David Zeuthencc6f9962013-04-18 11:57:24 -0700167
David Zeuthen4e1d1492014-04-25 13:12:27 -0700168 // Persists values related to the UpdateEngine.Attempt.* metrics so
169 // we can identify later if an update attempt ends abnormally.
170 void PersistAttemptMetrics();
171
172 // Clears persistent state previously set using AttemptMetricsPersist().
173 void ClearPersistedAttemptMetrics();
174
175 // Checks if persistent state previously set using AttemptMetricsPersist()
176 // exists and, if so, emits it with |attempt_result| set to
177 // metrics::AttemptResult::kAbnormalTermination.
178 void ReportAndClearPersistedAttemptMetrics();
179
David Zeuthen33bae492014-02-25 16:16:18 -0800180 // Collects and reports the various metrics related to a successful update.
181 void CollectAndReportSuccessfulUpdateMetrics();
Alex Deymo820cc702013-06-28 15:43:46 -0700182
Alex Deymo42432912013-07-12 20:21:15 -0700183 // Checks if we were expecting to be running in the new version but the
184 // boot into the new version failed for some reason. If that's the case, an
185 // UMA metric is sent reporting the number of attempts the same applied
186 // payload was attempted to reboot. This function is called by UpdateAttempter
187 // every time the update engine starts and there's no reboot pending.
188 void ReportFailedBootIfNeeded();
189
Jay Srinivasan08262882012-12-28 19:29:43 -0800190 // Resets all the persisted state values which are maintained relative to the
191 // current response signature. The response signature itself is not reset.
192 void ResetPersistedState();
193
Jay Srinivasan19409b72013-04-12 19:23:36 -0700194 // Resets the appropriate state related to download sources that need to be
195 // reset on a new update.
196 void ResetDownloadSourcesOnNewUpdate();
197
Chris Sosab3dcdb32013-09-04 15:22:12 -0700198 // Returns the persisted value from prefs_ for the given key. It also
199 // validates that the value returned is non-negative.
200 int64_t GetPersistedValue(const std::string& key);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700201
Jay Srinivasan08262882012-12-28 19:29:43 -0800202 // Calculates the response "signature", which is basically a string composed
203 // of the subset of the fields in the current response that affect the
204 // behavior of the PayloadState.
205 std::string CalculateResponseSignature();
206
207 // Initializes the current response signature from the persisted state.
208 void LoadResponseSignature();
209
210 // Sets the response signature to the given value. Also persists the value
211 // being set so that we resume from the save value in case of a process
212 // restart.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700213 void SetResponseSignature(const std::string& response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800214
215 // Initializes the payload attempt number from the persisted state.
216 void LoadPayloadAttemptNumber();
217
Alex Deymo820cc702013-06-28 15:43:46 -0700218 // Initializes the payload attempt number for full payloads from the persisted
219 // state.
220 void LoadFullPayloadAttemptNumber();
221
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800222 // Sets the payload attempt number to the given value. Also persists the
223 // value being set so that we resume from the same value in case of a process
224 // restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700225 void SetPayloadAttemptNumber(int payload_attempt_number);
226
227 // Sets the payload attempt number for full updates to the given value. Also
228 // persists the value being set so that we resume from the same value in case
229 // of a process restart.
230 void SetFullPayloadAttemptNumber(int payload_attempt_number);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800231
232 // Initializes the current URL index from the persisted state.
233 void LoadUrlIndex();
234
235 // Sets the current URL index to the given value. Also persists the value
236 // being set so that we resume from the same value in case of a process
237 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800238 void SetUrlIndex(uint32_t url_index);
239
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800240 // Initializes the current URL's failure count from the persisted stae.
241 void LoadUrlFailureCount();
242
243 // Sets the current URL's failure count to the given value. Also persists the
244 // value being set so that we resume from the same value in case of a process
245 // restart.
246 void SetUrlFailureCount(uint32_t url_failure_count);
247
David Zeuthencc6f9962013-04-18 11:57:24 -0700248 // Sets |url_switch_count_| to the given value and persists the value.
249 void SetUrlSwitchCount(uint32_t url_switch_count);
250
251 // Initializes |url_switch_count_| from the persisted stae.
252 void LoadUrlSwitchCount();
253
Jay Srinivasan08262882012-12-28 19:29:43 -0800254 // Initializes the backoff expiry time from the persisted state.
255 void LoadBackoffExpiryTime();
256
257 // Sets the backoff expiry time to the given value. Also persists the value
258 // being set so that we resume from the same value in case of a process
259 // restart.
260 void SetBackoffExpiryTime(const base::Time& new_time);
261
David Zeuthen9a017f22013-04-11 16:10:26 -0700262 // Initializes |update_timestamp_start_| from the persisted state.
263 void LoadUpdateTimestampStart();
264
265 // Sets |update_timestamp_start_| to the given value and persists the value.
266 void SetUpdateTimestampStart(const base::Time& value);
267
268 // Sets |update_timestamp_end_| to the given value. This is not persisted
269 // as it happens at the end of the update process where state is deleted
270 // anyway.
271 void SetUpdateTimestampEnd(const base::Time& value);
272
273 // Initializes |update_duration_uptime_| from the persisted state.
274 void LoadUpdateDurationUptime();
275
276 // Helper method used in SetUpdateDurationUptime() and
277 // CalculateUpdateDurationUptime().
278 void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
279 const base::Time& timestamp,
280 bool use_logging);
281
282 // Sets |update_duration_uptime_| to the given value and persists
283 // the value and sets |update_duration_uptime_timestamp_| to the
284 // current monotonic time.
285 void SetUpdateDurationUptime(const base::TimeDelta& value);
286
287 // Adds the difference between current monotonic time and
288 // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
289 // sets |update_duration_uptime_timestamp_| to current monotonic time.
290 void CalculateUpdateDurationUptime();
291
Jay Srinivasan19409b72013-04-12 19:23:36 -0700292 // Returns the full key for a download source given the prefix.
293 std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
294
295 // Loads the number of bytes that have been currently downloaded through the
296 // previous attempts from the persisted state for the given source. It's
297 // reset to 0 everytime we begin a full update and is continued from previous
298 // attempt if we're resuming the update.
299 void LoadCurrentBytesDownloaded(DownloadSource source);
300
301 // Sets the number of bytes that have been currently downloaded for the
302 // given source. This value is also persisted.
303 void SetCurrentBytesDownloaded(DownloadSource source,
304 uint64_t current_bytes_downloaded,
305 bool log);
306
307 // Loads the total number of bytes that have been downloaded (since the last
308 // successful update) from the persisted state for the given source. It's
309 // reset to 0 everytime we successfully apply an update and counts the bytes
310 // downloaded for both successful and failed attempts since then.
311 void LoadTotalBytesDownloaded(DownloadSource source);
312
313 // Sets the total number of bytes that have been downloaded so far for the
314 // given source. This value is also persisted.
315 void SetTotalBytesDownloaded(DownloadSource source,
316 uint64_t total_bytes_downloaded,
317 bool log);
318
Chris Sosaaa18e162013-06-20 13:20:30 -0700319 // Loads the blacklisted version from our prefs file.
320 void LoadRollbackVersion();
321
322 // Blacklists this version from getting AU'd to until we receive a new update
323 // response.
324 void SetRollbackVersion(const std::string& rollback_version);
325
326 // Clears any blacklisted version.
327 void ResetRollbackVersion();
328
Jay Srinivasan53173b92013-05-17 17:13:01 -0700329 inline uint32_t GetUrlIndex() {
330 return url_index_;
331 }
332
333 // Computes the list of candidate URLs from the total list of payload URLs in
334 // the Omaha response.
335 void ComputeCandidateUrls();
336
David Zeuthena573d6f2013-06-14 16:13:36 -0700337 // Sets |num_responses_seen_| and persist it to disk.
338 void SetNumResponsesSeen(int num_responses_seen);
339
340 // Initializes |num_responses_seen_| from persisted state.
341 void LoadNumResponsesSeen();
342
Alex Deymob33b0f02013-08-08 21:10:02 -0700343 // Reports metric conveying how many times updates were abandoned since
344 // the last update was applied. The difference between this metric and the
345 // previous ReportUpdatesAbandonedCountMetric() one is that this metric is
346 // reported every time an update is abandoned, as oposed to the mentioned
347 // metric that is reported once the new update was applied.
348 void ReportUpdatesAbandonedEventCountMetric();
349
Jay Srinivasan19409b72013-04-12 19:23:36 -0700350 // The global state of the system.
351 SystemState* system_state_;
352
Chris Sosabe45bef2013-04-09 18:25:12 -0700353 // Initializes |num_reboots_| from the persisted state.
354 void LoadNumReboots();
355
356 // Sets |num_reboots| for the update attempt. Also persists the
357 // value being set so that we resume from the same value in case of a process
358 // restart.
359 void SetNumReboots(uint32_t num_reboots);
360
361 // Checks to see if the device rebooted since the last call and if so
362 // increments num_reboots.
363 void UpdateNumReboots();
364
David Zeuthene4c58bf2013-06-18 17:26:50 -0700365 // Writes the current wall-clock time to the kPrefsSystemUpdatedMarker
366 // state variable.
367 void CreateSystemUpdatedMarkerFile();
368
369 // Called at program startup if the device booted into a new update.
370 // The |time_to_reboot| parameter contains the (wall-clock) duration
371 // from when the update successfully completed (the value written
372 // into the kPrefsSystemUpdatedMarker state variable) until the device
373 // was booted into the update (current wall-clock time).
374 void BootedIntoUpdate(base::TimeDelta time_to_reboot);
375
David Zeuthendcba8092013-08-06 12:16:35 -0700376 // Loads the |kPrefsP2PFirstAttemptTimestamp| state variable from disk
377 // into |p2p_first_attempt_timestamp_|.
378 void LoadP2PFirstAttemptTimestamp();
379
380 // Loads the |kPrefsP2PNumAttempts| state variable into |p2p_num_attempts_|.
381 void LoadP2PNumAttempts();
382
383 // Sets the |kPrefsP2PNumAttempts| state variable to |value|.
384 void SetP2PNumAttempts(int value);
385
386 // Sets the |kPrefsP2PFirstAttemptTimestamp| state variable to |time|.
387 void SetP2PFirstAttemptTimestamp(const base::Time& time);
388
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800389 // Interface object with which we read/write persisted state. This must
390 // be set by calling the Initialize method before calling any other method.
391 PrefsInterface* prefs_;
392
Chris Sosaaa18e162013-06-20 13:20:30 -0700393 // Interface object with which we read/write persisted state. This must
394 // be set by calling the Initialize method before calling any other method.
395 // This object persists across powerwashes.
396 PrefsInterface* powerwash_safe_prefs_;
397
Jay Srinivasan08262882012-12-28 19:29:43 -0800398 // This is the current response object from Omaha.
399 OmahaResponse response_;
400
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700401 // Whether p2p is being used for downloading as set with the
402 // SetUsingP2PForDownloading() method.
403 bool using_p2p_for_downloading_;
404
Jay Srinivasan08262882012-12-28 19:29:43 -0800405 // This stores a "signature" of the current response. The signature here
406 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800407 // this value is persisted so we resume from the same value in case of a
408 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800409 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800410
Alex Deymo820cc702013-06-28 15:43:46 -0700411 // The number of times we've tried to download the payload. This is
412 // incremented each time we download the payload successsfully or when we
413 // exhaust all failure limits for all URLs and are about to wrap around back
414 // to the first URL. Each update to this value is persisted so we resume from
415 // the same value in case of a process restart.
416 int payload_attempt_number_;
417
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800418 // The number of times we've tried to download the payload in full. This is
419 // incremented each time we download the payload in full successsfully or
420 // when we exhaust all failure limits for all URLs and are about to wrap
421 // around back to the first URL. Each update to this value is persisted so
422 // we resume from the same value in case of a process restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700423 int full_payload_attempt_number_;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800424
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800425 // The index of the current URL. This type is different from the one in the
426 // accessor methods because PrefsInterface supports only int64_t but we want
427 // to provide a stronger abstraction of uint32_t. Each update to this value
428 // is persisted so we resume from the same value in case of a process
429 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800430 int64_t url_index_;
431
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800432 // The count of failures encountered in the current attempt to download using
433 // the current URL (specified by url_index_). Each update to this value is
434 // persisted so we resume from the same value in case of a process restart.
435 int64_t url_failure_count_;
436
David Zeuthencc6f9962013-04-18 11:57:24 -0700437 // The number of times we've switched URLs.
438 int32_t url_switch_count_;
439
Jay Srinivasan19409b72013-04-12 19:23:36 -0700440 // The current download source based on the current URL. This value is
441 // not persisted as it can be recomputed everytime we update the URL.
442 // We're storing this so as not to recompute this on every few bytes of
443 // data we read from the socket.
444 DownloadSource current_download_source_;
445
David Zeuthena573d6f2013-06-14 16:13:36 -0700446 // The number of different Omaha responses seen. Increases every time
447 // a new response is seen. Resets to 0 only when the system has been
448 // successfully updated.
449 int num_responses_seen_;
450
Chris Sosabe45bef2013-04-09 18:25:12 -0700451 // The number of system reboots during an update attempt. Technically since
452 // we don't go out of our way to not update it when not attempting an update,
453 // also records the number of reboots before the next update attempt starts.
454 uint32_t num_reboots_;
455
Jay Srinivasan08262882012-12-28 19:29:43 -0800456 // The timestamp until which we've to wait before attempting to download the
457 // payload again, so as to backoff repeated downloads.
458 base::Time backoff_expiry_time_;
459
David Zeuthen9a017f22013-04-11 16:10:26 -0700460 // The most recently calculated value of the update duration.
461 base::TimeDelta update_duration_current_;
462
463 // The point in time (wall-clock) that the update was started.
464 base::Time update_timestamp_start_;
465
466 // The point in time (wall-clock) that the update ended. If the update
467 // is still in progress, this is set to the Epoch (e.g. 0).
468 base::Time update_timestamp_end_;
469
470 // The update duration uptime
471 base::TimeDelta update_duration_uptime_;
472
473 // The monotonic time when |update_duration_uptime_| was last set
474 base::Time update_duration_uptime_timestamp_;
475
Jay Srinivasan19409b72013-04-12 19:23:36 -0700476 // The number of bytes that have been downloaded for each source for each new
477 // update attempt. If we resume an update, we'll continue from the previous
478 // value, but if we get a new response or if the previous attempt failed,
479 // we'll reset this to 0 to start afresh. Each update to this value is
480 // persisted so we resume from the same value in case of a process restart.
481 // The extra index in the array is to no-op accidental access in case the
482 // return value from GetCurrentDownloadSource is used without validation.
483 uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
484
485 // The number of bytes that have been downloaded for each source since the
486 // the last successful update. This is used to compute the overhead we incur.
487 // Each update to this value is persisted so we resume from the same value in
488 // case of a process restart.
489 // The extra index in the array is to no-op accidental access in case the
490 // return value from GetCurrentDownloadSource is used without validation.
491 uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
492
David Zeuthen9a017f22013-04-11 16:10:26 -0700493 // A small timespan used when comparing wall-clock times for coping
494 // with the fact that clocks drift and consequently are adjusted
495 // (either forwards or backwards) via NTP.
496 static const base::TimeDelta kDurationSlack;
497
Jay Srinivasan53173b92013-05-17 17:13:01 -0700498 // The ordered list of the subset of payload URL candidates which are
499 // allowed as per device policy.
500 std::vector<std::string> candidate_urls_;
501
Chris Sosaaa18e162013-06-20 13:20:30 -0700502 // This stores a blacklisted version set as part of rollback. When we rollback
503 // we store the version of the os from which we are rolling back from in order
504 // to guarantee that we do not re-update to it on the next au attempt after
505 // reboot.
506 std::string rollback_version_;
507
David Zeuthendcba8092013-08-06 12:16:35 -0700508 // The cached value of |kPrefsP2PFirstAttemptTimestamp|.
509 base::Time p2p_first_attempt_timestamp_;
510
511 // The cached value of |kPrefsP2PNumAttempts|.
512 int p2p_num_attempts_;
513
David Zeuthen33bae492014-02-25 16:16:18 -0800514 // The number of bytes downloaded per attempt.
515 int64_t attempt_num_bytes_downloaded_;
516
517 // The boot time when the attempt was started.
518 base::Time attempt_start_time_boot_;
519
520 // The monotonic time when the attempt was started.
521 base::Time attempt_start_time_monotonic_;
522
David Zeuthenb281f072014-04-02 10:20:19 -0700523 // The connection type when the attempt started.
524 metrics::ConnectionType attempt_connection_type_;
525
David Zeuthenafed4a12014-04-09 15:28:44 -0700526 // Whether we're currently rolling back.
527 AttemptType attempt_type_;
528
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800529 DISALLOW_COPY_AND_ASSIGN(PayloadState);
530};
531
532} // namespace chromeos_update_engine
533
Alex Deymo759c2752014-03-17 21:09:36 -0700534#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H_