blob: 34f2e536f88bc6ffcf038fb1f37413f6a2124c63 [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
Jay Srinivasan08262882012-12-28 19:29:43 -0800116 private:
David Zeuthenafed4a12014-04-09 15:28:44 -0700117 enum class AttemptType {
118 kUpdate,
119 kRollback,
120 };
121
Alex Deymo42432912013-07-12 20:21:15 -0700122 friend class PayloadStateTest;
123 FRIEND_TEST(PayloadStateTest, RebootAfterUpdateFailedMetric);
124 FRIEND_TEST(PayloadStateTest, RebootAfterUpdateSucceed);
125 FRIEND_TEST(PayloadStateTest, RebootAfterCanceledUpdate);
Chris Sosab3dcdb32013-09-04 15:22:12 -0700126 FRIEND_TEST(PayloadStateTest, RollbackVersion);
Alex Deymo42432912013-07-12 20:21:15 -0700127 FRIEND_TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs);
128
David Zeuthen33bae492014-02-25 16:16:18 -0800129 // Helper called when an attempt has begun, is called by
David Zeuthenafed4a12014-04-09 15:28:44 -0700130 // UpdateResumed(), UpdateRestarted() and Rollback().
131 void AttemptStarted(AttemptType attempt_type);
David Zeuthen33bae492014-02-25 16:16:18 -0800132
Alex Deymo820cc702013-06-28 15:43:46 -0700133 // Increments the payload attempt number used for metrics.
134 void IncrementPayloadAttemptNumber();
135
Jay Srinivasan08262882012-12-28 19:29:43 -0800136 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800137 // at the time of the next update check.
Alex Deymo820cc702013-06-28 15:43:46 -0700138 void IncrementFullPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800139
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800140 // Advances the current URL index to the next available one. If all URLs have
141 // been exhausted during the current payload download attempt (as indicated
142 // by the payload attempt number), then it will increment the payload attempt
David Zeuthencc6f9962013-04-18 11:57:24 -0700143 // number and wrap around again with the first URL in the list. This also
144 // updates the URL switch count, if needed.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800145 void IncrementUrlIndex();
146
147 // Increments the failure count of the current URL. If the configured max
148 // failure count is reached for this URL, it advances the current URL index
149 // to the next URL and resets the failure count for that URL.
150 void IncrementFailureCount();
151
Jay Srinivasan08262882012-12-28 19:29:43 -0800152 // Updates the backoff expiry time exponentially based on the current
153 // payload attempt number.
154 void UpdateBackoffExpiryTime();
155
Jay Srinivasan19409b72013-04-12 19:23:36 -0700156 // Updates the value of current download source based on the current URL
157 // index. If the download source is not one of the known sources, it's set
158 // to kNumDownloadSources.
159 void UpdateCurrentDownloadSource();
160
161 // Updates the various metrics corresponding with the given number of bytes
162 // that were downloaded recently.
163 void UpdateBytesDownloaded(size_t count);
164
David Zeuthen33bae492014-02-25 16:16:18 -0800165 // Calculates the PayloadType we're using.
166 PayloadType CalculatePayloadType();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700167
David Zeuthen33bae492014-02-25 16:16:18 -0800168 // Collects and reports the various metrics related to an update attempt.
169 void CollectAndReportAttemptMetrics(ErrorCode code);
David Zeuthencc6f9962013-04-18 11:57:24 -0700170
David Zeuthen4e1d1492014-04-25 13:12:27 -0700171 // Persists values related to the UpdateEngine.Attempt.* metrics so
172 // we can identify later if an update attempt ends abnormally.
173 void PersistAttemptMetrics();
174
175 // Clears persistent state previously set using AttemptMetricsPersist().
176 void ClearPersistedAttemptMetrics();
177
178 // Checks if persistent state previously set using AttemptMetricsPersist()
179 // exists and, if so, emits it with |attempt_result| set to
180 // metrics::AttemptResult::kAbnormalTermination.
181 void ReportAndClearPersistedAttemptMetrics();
182
David Zeuthen33bae492014-02-25 16:16:18 -0800183 // Collects and reports the various metrics related to a successful update.
184 void CollectAndReportSuccessfulUpdateMetrics();
Alex Deymo820cc702013-06-28 15:43:46 -0700185
Alex Deymo42432912013-07-12 20:21:15 -0700186 // Checks if we were expecting to be running in the new version but the
187 // boot into the new version failed for some reason. If that's the case, an
188 // UMA metric is sent reporting the number of attempts the same applied
189 // payload was attempted to reboot. This function is called by UpdateAttempter
190 // every time the update engine starts and there's no reboot pending.
191 void ReportFailedBootIfNeeded();
192
Jay Srinivasan08262882012-12-28 19:29:43 -0800193 // Resets all the persisted state values which are maintained relative to the
194 // current response signature. The response signature itself is not reset.
195 void ResetPersistedState();
196
Jay Srinivasan19409b72013-04-12 19:23:36 -0700197 // Resets the appropriate state related to download sources that need to be
198 // reset on a new update.
199 void ResetDownloadSourcesOnNewUpdate();
200
Chris Sosab3dcdb32013-09-04 15:22:12 -0700201 // Returns the persisted value from prefs_ for the given key. It also
202 // validates that the value returned is non-negative.
203 int64_t GetPersistedValue(const std::string& key);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700204
Jay Srinivasan08262882012-12-28 19:29:43 -0800205 // Calculates the response "signature", which is basically a string composed
206 // of the subset of the fields in the current response that affect the
207 // behavior of the PayloadState.
208 std::string CalculateResponseSignature();
209
210 // Initializes the current response signature from the persisted state.
211 void LoadResponseSignature();
212
213 // Sets the response signature to the given value. Also persists the value
214 // being set so that we resume from the save value in case of a process
215 // restart.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700216 void SetResponseSignature(const std::string& response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800217
218 // Initializes the payload attempt number from the persisted state.
219 void LoadPayloadAttemptNumber();
220
Alex Deymo820cc702013-06-28 15:43:46 -0700221 // Initializes the payload attempt number for full payloads from the persisted
222 // state.
223 void LoadFullPayloadAttemptNumber();
224
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800225 // Sets the payload attempt number to the given value. Also persists the
226 // value being set so that we resume from the same value in case of a process
227 // restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700228 void SetPayloadAttemptNumber(int payload_attempt_number);
229
230 // Sets the payload attempt number for full updates to the given value. Also
231 // persists the value being set so that we resume from the same value in case
232 // of a process restart.
233 void SetFullPayloadAttemptNumber(int payload_attempt_number);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800234
235 // Initializes the current URL index from the persisted state.
236 void LoadUrlIndex();
237
238 // Sets the current URL index to the given value. Also persists the value
239 // being set so that we resume from the same value in case of a process
240 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800241 void SetUrlIndex(uint32_t url_index);
242
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800243 // Initializes the current URL's failure count from the persisted stae.
244 void LoadUrlFailureCount();
245
246 // Sets the current URL's failure count to the given value. Also persists the
247 // value being set so that we resume from the same value in case of a process
248 // restart.
249 void SetUrlFailureCount(uint32_t url_failure_count);
250
David Zeuthencc6f9962013-04-18 11:57:24 -0700251 // Sets |url_switch_count_| to the given value and persists the value.
252 void SetUrlSwitchCount(uint32_t url_switch_count);
253
254 // Initializes |url_switch_count_| from the persisted stae.
255 void LoadUrlSwitchCount();
256
Jay Srinivasan08262882012-12-28 19:29:43 -0800257 // Initializes the backoff expiry time from the persisted state.
258 void LoadBackoffExpiryTime();
259
260 // Sets the backoff expiry time to the given value. Also persists the value
261 // being set so that we resume from the same value in case of a process
262 // restart.
263 void SetBackoffExpiryTime(const base::Time& new_time);
264
David Zeuthen9a017f22013-04-11 16:10:26 -0700265 // Initializes |update_timestamp_start_| from the persisted state.
266 void LoadUpdateTimestampStart();
267
268 // Sets |update_timestamp_start_| to the given value and persists the value.
269 void SetUpdateTimestampStart(const base::Time& value);
270
271 // Sets |update_timestamp_end_| to the given value. This is not persisted
272 // as it happens at the end of the update process where state is deleted
273 // anyway.
274 void SetUpdateTimestampEnd(const base::Time& value);
275
276 // Initializes |update_duration_uptime_| from the persisted state.
277 void LoadUpdateDurationUptime();
278
279 // Helper method used in SetUpdateDurationUptime() and
280 // CalculateUpdateDurationUptime().
281 void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
282 const base::Time& timestamp,
283 bool use_logging);
284
285 // Sets |update_duration_uptime_| to the given value and persists
286 // the value and sets |update_duration_uptime_timestamp_| to the
287 // current monotonic time.
288 void SetUpdateDurationUptime(const base::TimeDelta& value);
289
290 // Adds the difference between current monotonic time and
291 // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
292 // sets |update_duration_uptime_timestamp_| to current monotonic time.
293 void CalculateUpdateDurationUptime();
294
Jay Srinivasan19409b72013-04-12 19:23:36 -0700295 // Returns the full key for a download source given the prefix.
296 std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
297
298 // Loads the number of bytes that have been currently downloaded through the
299 // previous attempts from the persisted state for the given source. It's
300 // reset to 0 everytime we begin a full update and is continued from previous
301 // attempt if we're resuming the update.
302 void LoadCurrentBytesDownloaded(DownloadSource source);
303
304 // Sets the number of bytes that have been currently downloaded for the
305 // given source. This value is also persisted.
306 void SetCurrentBytesDownloaded(DownloadSource source,
307 uint64_t current_bytes_downloaded,
308 bool log);
309
310 // Loads the total number of bytes that have been downloaded (since the last
311 // successful update) from the persisted state for the given source. It's
312 // reset to 0 everytime we successfully apply an update and counts the bytes
313 // downloaded for both successful and failed attempts since then.
314 void LoadTotalBytesDownloaded(DownloadSource source);
315
316 // Sets the total number of bytes that have been downloaded so far for the
317 // given source. This value is also persisted.
318 void SetTotalBytesDownloaded(DownloadSource source,
319 uint64_t total_bytes_downloaded,
320 bool log);
321
Chris Sosaaa18e162013-06-20 13:20:30 -0700322 // Loads the blacklisted version from our prefs file.
323 void LoadRollbackVersion();
324
325 // Blacklists this version from getting AU'd to until we receive a new update
326 // response.
327 void SetRollbackVersion(const std::string& rollback_version);
328
329 // Clears any blacklisted version.
330 void ResetRollbackVersion();
331
Jay Srinivasan53173b92013-05-17 17:13:01 -0700332 inline uint32_t GetUrlIndex() {
333 return url_index_;
334 }
335
336 // Computes the list of candidate URLs from the total list of payload URLs in
337 // the Omaha response.
338 void ComputeCandidateUrls();
339
David Zeuthena573d6f2013-06-14 16:13:36 -0700340 // Sets |num_responses_seen_| and persist it to disk.
341 void SetNumResponsesSeen(int num_responses_seen);
342
343 // Initializes |num_responses_seen_| from persisted state.
344 void LoadNumResponsesSeen();
345
Alex Deymob33b0f02013-08-08 21:10:02 -0700346 // Reports metric conveying how many times updates were abandoned since
347 // the last update was applied. The difference between this metric and the
348 // previous ReportUpdatesAbandonedCountMetric() one is that this metric is
349 // reported every time an update is abandoned, as oposed to the mentioned
350 // metric that is reported once the new update was applied.
351 void ReportUpdatesAbandonedEventCountMetric();
352
Jay Srinivasan19409b72013-04-12 19:23:36 -0700353 // The global state of the system.
354 SystemState* system_state_;
355
Chris Sosabe45bef2013-04-09 18:25:12 -0700356 // Initializes |num_reboots_| from the persisted state.
357 void LoadNumReboots();
358
359 // Sets |num_reboots| for the update attempt. Also persists the
360 // value being set so that we resume from the same value in case of a process
361 // restart.
362 void SetNumReboots(uint32_t num_reboots);
363
364 // Checks to see if the device rebooted since the last call and if so
365 // increments num_reboots.
366 void UpdateNumReboots();
367
David Zeuthene4c58bf2013-06-18 17:26:50 -0700368 // Writes the current wall-clock time to the kPrefsSystemUpdatedMarker
369 // state variable.
370 void CreateSystemUpdatedMarkerFile();
371
372 // Called at program startup if the device booted into a new update.
373 // The |time_to_reboot| parameter contains the (wall-clock) duration
374 // from when the update successfully completed (the value written
375 // into the kPrefsSystemUpdatedMarker state variable) until the device
376 // was booted into the update (current wall-clock time).
377 void BootedIntoUpdate(base::TimeDelta time_to_reboot);
378
David Zeuthendcba8092013-08-06 12:16:35 -0700379 // Loads the |kPrefsP2PFirstAttemptTimestamp| state variable from disk
380 // into |p2p_first_attempt_timestamp_|.
381 void LoadP2PFirstAttemptTimestamp();
382
383 // Loads the |kPrefsP2PNumAttempts| state variable into |p2p_num_attempts_|.
384 void LoadP2PNumAttempts();
385
386 // Sets the |kPrefsP2PNumAttempts| state variable to |value|.
387 void SetP2PNumAttempts(int value);
388
389 // Sets the |kPrefsP2PFirstAttemptTimestamp| state variable to |time|.
390 void SetP2PFirstAttemptTimestamp(const base::Time& time);
391
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800392 // Interface object with which we read/write persisted state. This must
393 // be set by calling the Initialize method before calling any other method.
394 PrefsInterface* prefs_;
395
Chris Sosaaa18e162013-06-20 13:20:30 -0700396 // Interface object with which we read/write persisted state. This must
397 // be set by calling the Initialize method before calling any other method.
398 // This object persists across powerwashes.
399 PrefsInterface* powerwash_safe_prefs_;
400
Jay Srinivasan08262882012-12-28 19:29:43 -0800401 // This is the current response object from Omaha.
402 OmahaResponse response_;
403
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700404 // Whether p2p is being used for downloading as set with the
405 // SetUsingP2PForDownloading() method.
406 bool using_p2p_for_downloading_;
407
Jay Srinivasan08262882012-12-28 19:29:43 -0800408 // This stores a "signature" of the current response. The signature here
409 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800410 // this value is persisted so we resume from the same value in case of a
411 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800412 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800413
Alex Deymo820cc702013-06-28 15:43:46 -0700414 // The number of times we've tried to download the payload. This is
415 // incremented each time we download the payload successsfully or when we
416 // exhaust all failure limits for all URLs and are about to wrap around back
417 // to the first URL. Each update to this value is persisted so we resume from
418 // the same value in case of a process restart.
419 int payload_attempt_number_;
420
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800421 // The number of times we've tried to download the payload in full. This is
422 // incremented each time we download the payload in full successsfully or
423 // when we exhaust all failure limits for all URLs and are about to wrap
424 // around back to the first URL. Each update to this value is persisted so
425 // we resume from the same value in case of a process restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700426 int full_payload_attempt_number_;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800427
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800428 // The index of the current URL. This type is different from the one in the
429 // accessor methods because PrefsInterface supports only int64_t but we want
430 // to provide a stronger abstraction of uint32_t. Each update to this value
431 // is persisted so we resume from the same value in case of a process
432 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800433 int64_t url_index_;
434
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800435 // The count of failures encountered in the current attempt to download using
436 // the current URL (specified by url_index_). Each update to this value is
437 // persisted so we resume from the same value in case of a process restart.
438 int64_t url_failure_count_;
439
David Zeuthencc6f9962013-04-18 11:57:24 -0700440 // The number of times we've switched URLs.
441 int32_t url_switch_count_;
442
Jay Srinivasan19409b72013-04-12 19:23:36 -0700443 // The current download source based on the current URL. This value is
444 // not persisted as it can be recomputed everytime we update the URL.
445 // We're storing this so as not to recompute this on every few bytes of
446 // data we read from the socket.
447 DownloadSource current_download_source_;
448
David Zeuthena573d6f2013-06-14 16:13:36 -0700449 // The number of different Omaha responses seen. Increases every time
450 // a new response is seen. Resets to 0 only when the system has been
451 // successfully updated.
452 int num_responses_seen_;
453
Chris Sosabe45bef2013-04-09 18:25:12 -0700454 // The number of system reboots during an update attempt. Technically since
455 // we don't go out of our way to not update it when not attempting an update,
456 // also records the number of reboots before the next update attempt starts.
457 uint32_t num_reboots_;
458
Jay Srinivasan08262882012-12-28 19:29:43 -0800459 // The timestamp until which we've to wait before attempting to download the
460 // payload again, so as to backoff repeated downloads.
461 base::Time backoff_expiry_time_;
462
David Zeuthen9a017f22013-04-11 16:10:26 -0700463 // The most recently calculated value of the update duration.
464 base::TimeDelta update_duration_current_;
465
466 // The point in time (wall-clock) that the update was started.
467 base::Time update_timestamp_start_;
468
469 // The point in time (wall-clock) that the update ended. If the update
470 // is still in progress, this is set to the Epoch (e.g. 0).
471 base::Time update_timestamp_end_;
472
473 // The update duration uptime
474 base::TimeDelta update_duration_uptime_;
475
476 // The monotonic time when |update_duration_uptime_| was last set
477 base::Time update_duration_uptime_timestamp_;
478
Jay Srinivasan19409b72013-04-12 19:23:36 -0700479 // The number of bytes that have been downloaded for each source for each new
480 // update attempt. If we resume an update, we'll continue from the previous
481 // value, but if we get a new response or if the previous attempt failed,
482 // we'll reset this to 0 to start afresh. Each update to this value is
483 // persisted so we resume from the same value in case of a process restart.
484 // The extra index in the array is to no-op accidental access in case the
485 // return value from GetCurrentDownloadSource is used without validation.
486 uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
487
488 // The number of bytes that have been downloaded for each source since the
489 // the last successful update. This is used to compute the overhead we incur.
490 // Each update to this value is persisted so we resume from the same value in
491 // case of a process restart.
492 // The extra index in the array is to no-op accidental access in case the
493 // return value from GetCurrentDownloadSource is used without validation.
494 uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
495
David Zeuthen9a017f22013-04-11 16:10:26 -0700496 // A small timespan used when comparing wall-clock times for coping
497 // with the fact that clocks drift and consequently are adjusted
498 // (either forwards or backwards) via NTP.
499 static const base::TimeDelta kDurationSlack;
500
Jay Srinivasan53173b92013-05-17 17:13:01 -0700501 // The ordered list of the subset of payload URL candidates which are
502 // allowed as per device policy.
503 std::vector<std::string> candidate_urls_;
504
Chris Sosaaa18e162013-06-20 13:20:30 -0700505 // This stores a blacklisted version set as part of rollback. When we rollback
506 // we store the version of the os from which we are rolling back from in order
507 // to guarantee that we do not re-update to it on the next au attempt after
508 // reboot.
509 std::string rollback_version_;
510
David Zeuthendcba8092013-08-06 12:16:35 -0700511 // The cached value of |kPrefsP2PFirstAttemptTimestamp|.
512 base::Time p2p_first_attempt_timestamp_;
513
514 // The cached value of |kPrefsP2PNumAttempts|.
515 int p2p_num_attempts_;
516
David Zeuthen33bae492014-02-25 16:16:18 -0800517 // The number of bytes downloaded per attempt.
518 int64_t attempt_num_bytes_downloaded_;
519
520 // The boot time when the attempt was started.
521 base::Time attempt_start_time_boot_;
522
523 // The monotonic time when the attempt was started.
524 base::Time attempt_start_time_monotonic_;
525
David Zeuthenb281f072014-04-02 10:20:19 -0700526 // The connection type when the attempt started.
527 metrics::ConnectionType attempt_connection_type_;
528
David Zeuthenafed4a12014-04-09 15:28:44 -0700529 // Whether we're currently rolling back.
530 AttemptType attempt_type_;
531
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800532 DISALLOW_COPY_AND_ASSIGN(PayloadState);
533};
534
535} // namespace chromeos_update_engine
536
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700537#endif // UPDATE_ENGINE_PAYLOAD_STATE_H_