blob: 7def7992b0d456d90f070fce929fb10764f9db9b [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();
Alex Deymo610277e2014-11-11 21:18:11 -080031 ~PayloadState() override {}
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.
Alex Deymo610277e2014-11-11 21:18:11 -080040 void SetResponse(const OmahaResponse& response) override;
41 void DownloadComplete() override;
42 void DownloadProgress(size_t count) override;
43 void UpdateResumed() override;
44 void UpdateRestarted() override;
45 void UpdateSucceeded() override;
46 void UpdateFailed(ErrorCode error) override;
47 void ResetUpdateStatus() override;
48 bool ShouldBackoffDownload() override;
49 void Rollback() override;
50 void ExpectRebootInNewVersion(const std::string& target_version_uid) override;
51 void SetUsingP2PForDownloading(bool value) override;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080052
Gilad Arnold74b5f552014-10-07 08:17:16 -070053 void SetUsingP2PForSharing(bool value) override {
54 using_p2p_for_sharing_ = value;
55 }
56
Alex Deymo610277e2014-11-11 21:18:11 -080057 inline std::string GetResponseSignature() override {
Jay Srinivasan08262882012-12-28 19:29:43 -080058 return response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080059 }
60
Alex Deymo610277e2014-11-11 21:18:11 -080061 inline int GetFullPayloadAttemptNumber() override {
Alex Deymo820cc702013-06-28 15:43:46 -070062 return full_payload_attempt_number_;
63 }
64
Alex Deymo610277e2014-11-11 21:18:11 -080065 inline int GetPayloadAttemptNumber() override {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080066 return payload_attempt_number_;
67 }
68
Alex Deymo610277e2014-11-11 21:18:11 -080069 inline std::string GetCurrentUrl() override {
Jay Srinivasan53173b92013-05-17 17:13:01 -070070 return candidate_urls_.size() ? candidate_urls_[url_index_] : "";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080071 }
72
Alex Deymo610277e2014-11-11 21:18:11 -080073 inline uint32_t GetUrlFailureCount() override {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080074 return url_failure_count_;
75 }
76
Alex Deymo610277e2014-11-11 21:18:11 -080077 inline uint32_t GetUrlSwitchCount() override {
David Zeuthencc6f9962013-04-18 11:57:24 -070078 return url_switch_count_;
79 }
80
Alex Deymo610277e2014-11-11 21:18:11 -080081 inline int GetNumResponsesSeen() override {
David Zeuthena573d6f2013-06-14 16:13:36 -070082 return num_responses_seen_;
83 }
84
Alex Deymo610277e2014-11-11 21:18:11 -080085 inline base::Time GetBackoffExpiryTime() override {
Jay Srinivasan08262882012-12-28 19:29:43 -080086 return backoff_expiry_time_;
87 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080088
Alex Deymo610277e2014-11-11 21:18:11 -080089 base::TimeDelta GetUpdateDuration() override;
David Zeuthen9a017f22013-04-11 16:10:26 -070090
Alex Deymo610277e2014-11-11 21:18:11 -080091 base::TimeDelta GetUpdateDurationUptime() override;
David Zeuthen9a017f22013-04-11 16:10:26 -070092
Alex Deymo610277e2014-11-11 21:18:11 -080093 inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) override {
Jay Srinivasan19409b72013-04-12 19:23:36 -070094 return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0;
95 }
96
Alex Deymo610277e2014-11-11 21:18:11 -080097 inline uint64_t GetTotalBytesDownloaded(DownloadSource source) override {
Jay Srinivasan19409b72013-04-12 19:23:36 -070098 return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0;
99 }
100
Alex Deymo610277e2014-11-11 21:18:11 -0800101 inline uint32_t GetNumReboots() override {
Chris Sosabe45bef2013-04-09 18:25:12 -0700102 return num_reboots_;
103 }
104
Alex Deymo610277e2014-11-11 21:18:11 -0800105 void UpdateEngineStarted() override;
David Zeuthene4c58bf2013-06-18 17:26:50 -0700106
Alex Deymo610277e2014-11-11 21:18:11 -0800107 inline std::string GetRollbackVersion() override {
Chris Sosaaa18e162013-06-20 13:20:30 -0700108 return rollback_version_;
109 }
110
Alex Deymo610277e2014-11-11 21:18:11 -0800111 int GetP2PNumAttempts() override;
112 base::Time GetP2PFirstAttemptTimestamp() override;
113 void P2PNewAttempt() override;
114 bool P2PAttemptAllowed() override;
David Zeuthendcba8092013-08-06 12:16:35 -0700115
Gilad Arnold74b5f552014-10-07 08:17:16 -0700116 bool GetUsingP2PForDownloading() const override {
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700117 return using_p2p_for_downloading_;
118 }
119
Gilad Arnold74b5f552014-10-07 08:17:16 -0700120 bool GetUsingP2PForSharing() const override {
121 return using_p2p_for_sharing_;
122 }
123
Gilad Arnold519cfc72014-10-02 10:34:54 -0700124 base::TimeDelta GetScatteringWaitPeriod() override {
125 return scattering_wait_period_;
126 }
127
128 void SetScatteringWaitPeriod(base::TimeDelta wait_period) override;
129
Gilad Arnold74b5f552014-10-07 08:17:16 -0700130 void SetP2PUrl(const std::string& url) override {
131 p2p_url_ = url;
132 }
133
134 std::string GetP2PUrl() const override {
135 return p2p_url_;
136 }
137
Jay Srinivasan08262882012-12-28 19:29:43 -0800138 private:
David Zeuthenafed4a12014-04-09 15:28:44 -0700139 enum class AttemptType {
140 kUpdate,
141 kRollback,
142 };
143
Alex Deymo42432912013-07-12 20:21:15 -0700144 friend class PayloadStateTest;
145 FRIEND_TEST(PayloadStateTest, RebootAfterUpdateFailedMetric);
146 FRIEND_TEST(PayloadStateTest, RebootAfterUpdateSucceed);
147 FRIEND_TEST(PayloadStateTest, RebootAfterCanceledUpdate);
Chris Sosab3dcdb32013-09-04 15:22:12 -0700148 FRIEND_TEST(PayloadStateTest, RollbackVersion);
Alex Deymo42432912013-07-12 20:21:15 -0700149 FRIEND_TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs);
150
David Zeuthen33bae492014-02-25 16:16:18 -0800151 // Helper called when an attempt has begun, is called by
David Zeuthenafed4a12014-04-09 15:28:44 -0700152 // UpdateResumed(), UpdateRestarted() and Rollback().
153 void AttemptStarted(AttemptType attempt_type);
David Zeuthen33bae492014-02-25 16:16:18 -0800154
Alex Deymo820cc702013-06-28 15:43:46 -0700155 // Increments the payload attempt number used for metrics.
156 void IncrementPayloadAttemptNumber();
157
Jay Srinivasan08262882012-12-28 19:29:43 -0800158 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800159 // at the time of the next update check.
Alex Deymo820cc702013-06-28 15:43:46 -0700160 void IncrementFullPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800161
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800162 // Advances the current URL index to the next available one. If all URLs have
163 // been exhausted during the current payload download attempt (as indicated
164 // by the payload attempt number), then it will increment the payload attempt
David Zeuthencc6f9962013-04-18 11:57:24 -0700165 // number and wrap around again with the first URL in the list. This also
166 // updates the URL switch count, if needed.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800167 void IncrementUrlIndex();
168
169 // Increments the failure count of the current URL. If the configured max
170 // failure count is reached for this URL, it advances the current URL index
171 // to the next URL and resets the failure count for that URL.
172 void IncrementFailureCount();
173
Jay Srinivasan08262882012-12-28 19:29:43 -0800174 // Updates the backoff expiry time exponentially based on the current
175 // payload attempt number.
176 void UpdateBackoffExpiryTime();
177
Jay Srinivasan19409b72013-04-12 19:23:36 -0700178 // Updates the value of current download source based on the current URL
179 // index. If the download source is not one of the known sources, it's set
180 // to kNumDownloadSources.
181 void UpdateCurrentDownloadSource();
182
183 // Updates the various metrics corresponding with the given number of bytes
184 // that were downloaded recently.
185 void UpdateBytesDownloaded(size_t count);
186
David Zeuthen33bae492014-02-25 16:16:18 -0800187 // Calculates the PayloadType we're using.
188 PayloadType CalculatePayloadType();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700189
David Zeuthen33bae492014-02-25 16:16:18 -0800190 // Collects and reports the various metrics related to an update attempt.
191 void CollectAndReportAttemptMetrics(ErrorCode code);
David Zeuthencc6f9962013-04-18 11:57:24 -0700192
David Zeuthen4e1d1492014-04-25 13:12:27 -0700193 // Persists values related to the UpdateEngine.Attempt.* metrics so
194 // we can identify later if an update attempt ends abnormally.
195 void PersistAttemptMetrics();
196
197 // Clears persistent state previously set using AttemptMetricsPersist().
198 void ClearPersistedAttemptMetrics();
199
200 // Checks if persistent state previously set using AttemptMetricsPersist()
201 // exists and, if so, emits it with |attempt_result| set to
202 // metrics::AttemptResult::kAbnormalTermination.
203 void ReportAndClearPersistedAttemptMetrics();
204
David Zeuthen33bae492014-02-25 16:16:18 -0800205 // Collects and reports the various metrics related to a successful update.
206 void CollectAndReportSuccessfulUpdateMetrics();
Alex Deymo820cc702013-06-28 15:43:46 -0700207
Alex Deymo42432912013-07-12 20:21:15 -0700208 // Checks if we were expecting to be running in the new version but the
209 // boot into the new version failed for some reason. If that's the case, an
210 // UMA metric is sent reporting the number of attempts the same applied
211 // payload was attempted to reboot. This function is called by UpdateAttempter
212 // every time the update engine starts and there's no reboot pending.
213 void ReportFailedBootIfNeeded();
214
Jay Srinivasan08262882012-12-28 19:29:43 -0800215 // Resets all the persisted state values which are maintained relative to the
216 // current response signature. The response signature itself is not reset.
217 void ResetPersistedState();
218
Jay Srinivasan19409b72013-04-12 19:23:36 -0700219 // Resets the appropriate state related to download sources that need to be
220 // reset on a new update.
221 void ResetDownloadSourcesOnNewUpdate();
222
Chris Sosab3dcdb32013-09-04 15:22:12 -0700223 // Returns the persisted value from prefs_ for the given key. It also
224 // validates that the value returned is non-negative.
225 int64_t GetPersistedValue(const std::string& key);
Jay Srinivasan19409b72013-04-12 19:23:36 -0700226
Jay Srinivasan08262882012-12-28 19:29:43 -0800227 // Calculates the response "signature", which is basically a string composed
228 // of the subset of the fields in the current response that affect the
229 // behavior of the PayloadState.
230 std::string CalculateResponseSignature();
231
232 // Initializes the current response signature from the persisted state.
233 void LoadResponseSignature();
234
235 // Sets the response signature to the given value. Also persists the value
236 // being set so that we resume from the save value in case of a process
237 // restart.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700238 void SetResponseSignature(const std::string& response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800239
240 // Initializes the payload attempt number from the persisted state.
241 void LoadPayloadAttemptNumber();
242
Alex Deymo820cc702013-06-28 15:43:46 -0700243 // Initializes the payload attempt number for full payloads from the persisted
244 // state.
245 void LoadFullPayloadAttemptNumber();
246
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800247 // Sets the payload attempt number to the given value. Also persists the
248 // value being set so that we resume from the same value in case of a process
249 // restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700250 void SetPayloadAttemptNumber(int payload_attempt_number);
251
252 // Sets the payload attempt number for full updates to the given value. Also
253 // persists the value being set so that we resume from the same value in case
254 // of a process restart.
255 void SetFullPayloadAttemptNumber(int payload_attempt_number);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800256
257 // Initializes the current URL index from the persisted state.
258 void LoadUrlIndex();
259
260 // Sets the current URL index 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.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800263 void SetUrlIndex(uint32_t url_index);
264
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800265 // Initializes the current URL's failure count from the persisted stae.
266 void LoadUrlFailureCount();
267
268 // Sets the current URL's failure count to the given value. Also persists the
269 // value being set so that we resume from the same value in case of a process
270 // restart.
271 void SetUrlFailureCount(uint32_t url_failure_count);
272
David Zeuthencc6f9962013-04-18 11:57:24 -0700273 // Sets |url_switch_count_| to the given value and persists the value.
274 void SetUrlSwitchCount(uint32_t url_switch_count);
275
276 // Initializes |url_switch_count_| from the persisted stae.
277 void LoadUrlSwitchCount();
278
Jay Srinivasan08262882012-12-28 19:29:43 -0800279 // Initializes the backoff expiry time from the persisted state.
280 void LoadBackoffExpiryTime();
281
282 // Sets the backoff expiry time to the given value. Also persists the value
283 // being set so that we resume from the same value in case of a process
284 // restart.
285 void SetBackoffExpiryTime(const base::Time& new_time);
286
David Zeuthen9a017f22013-04-11 16:10:26 -0700287 // Initializes |update_timestamp_start_| from the persisted state.
288 void LoadUpdateTimestampStart();
289
290 // Sets |update_timestamp_start_| to the given value and persists the value.
291 void SetUpdateTimestampStart(const base::Time& value);
292
293 // Sets |update_timestamp_end_| to the given value. This is not persisted
294 // as it happens at the end of the update process where state is deleted
295 // anyway.
296 void SetUpdateTimestampEnd(const base::Time& value);
297
298 // Initializes |update_duration_uptime_| from the persisted state.
299 void LoadUpdateDurationUptime();
300
301 // Helper method used in SetUpdateDurationUptime() and
302 // CalculateUpdateDurationUptime().
303 void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
304 const base::Time& timestamp,
305 bool use_logging);
306
307 // Sets |update_duration_uptime_| to the given value and persists
308 // the value and sets |update_duration_uptime_timestamp_| to the
309 // current monotonic time.
310 void SetUpdateDurationUptime(const base::TimeDelta& value);
311
312 // Adds the difference between current monotonic time and
313 // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
314 // sets |update_duration_uptime_timestamp_| to current monotonic time.
315 void CalculateUpdateDurationUptime();
316
Jay Srinivasan19409b72013-04-12 19:23:36 -0700317 // Returns the full key for a download source given the prefix.
318 std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
319
320 // Loads the number of bytes that have been currently downloaded through the
321 // previous attempts from the persisted state for the given source. It's
322 // reset to 0 everytime we begin a full update and is continued from previous
323 // attempt if we're resuming the update.
324 void LoadCurrentBytesDownloaded(DownloadSource source);
325
326 // Sets the number of bytes that have been currently downloaded for the
327 // given source. This value is also persisted.
328 void SetCurrentBytesDownloaded(DownloadSource source,
329 uint64_t current_bytes_downloaded,
330 bool log);
331
332 // Loads the total number of bytes that have been downloaded (since the last
333 // successful update) from the persisted state for the given source. It's
334 // reset to 0 everytime we successfully apply an update and counts the bytes
335 // downloaded for both successful and failed attempts since then.
336 void LoadTotalBytesDownloaded(DownloadSource source);
337
338 // Sets the total number of bytes that have been downloaded so far for the
339 // given source. This value is also persisted.
340 void SetTotalBytesDownloaded(DownloadSource source,
341 uint64_t total_bytes_downloaded,
342 bool log);
343
Chris Sosaaa18e162013-06-20 13:20:30 -0700344 // Loads the blacklisted version from our prefs file.
345 void LoadRollbackVersion();
346
347 // Blacklists this version from getting AU'd to until we receive a new update
348 // response.
349 void SetRollbackVersion(const std::string& rollback_version);
350
351 // Clears any blacklisted version.
352 void ResetRollbackVersion();
353
Jay Srinivasan53173b92013-05-17 17:13:01 -0700354 inline uint32_t GetUrlIndex() {
355 return url_index_;
356 }
357
358 // Computes the list of candidate URLs from the total list of payload URLs in
359 // the Omaha response.
360 void ComputeCandidateUrls();
361
David Zeuthena573d6f2013-06-14 16:13:36 -0700362 // Sets |num_responses_seen_| and persist it to disk.
363 void SetNumResponsesSeen(int num_responses_seen);
364
365 // Initializes |num_responses_seen_| from persisted state.
366 void LoadNumResponsesSeen();
367
Alex Deymob33b0f02013-08-08 21:10:02 -0700368 // Reports metric conveying how many times updates were abandoned since
369 // the last update was applied. The difference between this metric and the
370 // previous ReportUpdatesAbandonedCountMetric() one is that this metric is
371 // reported every time an update is abandoned, as oposed to the mentioned
372 // metric that is reported once the new update was applied.
373 void ReportUpdatesAbandonedEventCountMetric();
374
Chris Sosabe45bef2013-04-09 18:25:12 -0700375 // Initializes |num_reboots_| from the persisted state.
376 void LoadNumReboots();
377
378 // Sets |num_reboots| for the update attempt. Also persists the
379 // value being set so that we resume from the same value in case of a process
380 // restart.
381 void SetNumReboots(uint32_t num_reboots);
382
383 // Checks to see if the device rebooted since the last call and if so
384 // increments num_reboots.
385 void UpdateNumReboots();
386
David Zeuthene4c58bf2013-06-18 17:26:50 -0700387 // Writes the current wall-clock time to the kPrefsSystemUpdatedMarker
388 // state variable.
389 void CreateSystemUpdatedMarkerFile();
390
391 // Called at program startup if the device booted into a new update.
392 // The |time_to_reboot| parameter contains the (wall-clock) duration
393 // from when the update successfully completed (the value written
394 // into the kPrefsSystemUpdatedMarker state variable) until the device
395 // was booted into the update (current wall-clock time).
396 void BootedIntoUpdate(base::TimeDelta time_to_reboot);
397
David Zeuthendcba8092013-08-06 12:16:35 -0700398 // Loads the |kPrefsP2PFirstAttemptTimestamp| state variable from disk
399 // into |p2p_first_attempt_timestamp_|.
400 void LoadP2PFirstAttemptTimestamp();
401
402 // Loads the |kPrefsP2PNumAttempts| state variable into |p2p_num_attempts_|.
403 void LoadP2PNumAttempts();
404
405 // Sets the |kPrefsP2PNumAttempts| state variable to |value|.
406 void SetP2PNumAttempts(int value);
407
408 // Sets the |kPrefsP2PFirstAttemptTimestamp| state variable to |time|.
409 void SetP2PFirstAttemptTimestamp(const base::Time& time);
410
Gilad Arnold519cfc72014-10-02 10:34:54 -0700411 // Loads the persisted scattering wallclock-based wait period.
412 void LoadScatteringWaitPeriod();
413
Gilad Arnold6e15aac2014-10-02 10:34:14 -0700414 // The global state of the system.
415 SystemState* system_state_;
416
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800417 // Interface object with which we read/write persisted state. This must
418 // be set by calling the Initialize method before calling any other method.
419 PrefsInterface* prefs_;
420
Chris Sosaaa18e162013-06-20 13:20:30 -0700421 // Interface object with which we read/write persisted state. This must
422 // be set by calling the Initialize method before calling any other method.
423 // This object persists across powerwashes.
424 PrefsInterface* powerwash_safe_prefs_;
425
Jay Srinivasan08262882012-12-28 19:29:43 -0800426 // This is the current response object from Omaha.
427 OmahaResponse response_;
428
Gilad Arnold74b5f552014-10-07 08:17:16 -0700429 // Whether P2P is being used for downloading and sharing.
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700430 bool using_p2p_for_downloading_;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700431 bool using_p2p_for_sharing_;
432
433 // Stores the P2P download URL, if one is used.
434 std::string p2p_url_;
435
436 // The cached value of |kPrefsP2PFirstAttemptTimestamp|.
437 base::Time p2p_first_attempt_timestamp_;
438
439 // The cached value of |kPrefsP2PNumAttempts|.
440 int p2p_num_attempts_;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700441
Jay Srinivasan08262882012-12-28 19:29:43 -0800442 // This stores a "signature" of the current response. The signature here
443 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800444 // this value is persisted so we resume from the same value in case of a
445 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800446 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800447
Alex Deymo820cc702013-06-28 15:43:46 -0700448 // The number of times we've tried to download the payload. This is
449 // incremented each time we download the payload successsfully or when we
450 // exhaust all failure limits for all URLs and are about to wrap around back
451 // to the first URL. Each update to this value is persisted so we resume from
452 // the same value in case of a process restart.
453 int payload_attempt_number_;
454
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800455 // The number of times we've tried to download the payload in full. This is
456 // incremented each time we download the payload in full successsfully or
457 // when we exhaust all failure limits for all URLs and are about to wrap
458 // around back to the first URL. Each update to this value is persisted so
459 // we resume from the same value in case of a process restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700460 int full_payload_attempt_number_;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800461
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800462 // The index of the current URL. This type is different from the one in the
463 // accessor methods because PrefsInterface supports only int64_t but we want
464 // to provide a stronger abstraction of uint32_t. Each update to this value
465 // is persisted so we resume from the same value in case of a process
466 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800467 int64_t url_index_;
468
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800469 // The count of failures encountered in the current attempt to download using
470 // the current URL (specified by url_index_). Each update to this value is
471 // persisted so we resume from the same value in case of a process restart.
472 int64_t url_failure_count_;
473
David Zeuthencc6f9962013-04-18 11:57:24 -0700474 // The number of times we've switched URLs.
475 int32_t url_switch_count_;
476
Jay Srinivasan19409b72013-04-12 19:23:36 -0700477 // The current download source based on the current URL. This value is
478 // not persisted as it can be recomputed everytime we update the URL.
479 // We're storing this so as not to recompute this on every few bytes of
480 // data we read from the socket.
481 DownloadSource current_download_source_;
482
David Zeuthena573d6f2013-06-14 16:13:36 -0700483 // The number of different Omaha responses seen. Increases every time
484 // a new response is seen. Resets to 0 only when the system has been
485 // successfully updated.
486 int num_responses_seen_;
487
Chris Sosabe45bef2013-04-09 18:25:12 -0700488 // The number of system reboots during an update attempt. Technically since
489 // we don't go out of our way to not update it when not attempting an update,
490 // also records the number of reboots before the next update attempt starts.
491 uint32_t num_reboots_;
492
Jay Srinivasan08262882012-12-28 19:29:43 -0800493 // The timestamp until which we've to wait before attempting to download the
494 // payload again, so as to backoff repeated downloads.
495 base::Time backoff_expiry_time_;
496
David Zeuthen9a017f22013-04-11 16:10:26 -0700497 // The most recently calculated value of the update duration.
498 base::TimeDelta update_duration_current_;
499
500 // The point in time (wall-clock) that the update was started.
501 base::Time update_timestamp_start_;
502
503 // The point in time (wall-clock) that the update ended. If the update
504 // is still in progress, this is set to the Epoch (e.g. 0).
505 base::Time update_timestamp_end_;
506
507 // The update duration uptime
508 base::TimeDelta update_duration_uptime_;
509
510 // The monotonic time when |update_duration_uptime_| was last set
511 base::Time update_duration_uptime_timestamp_;
512
Jay Srinivasan19409b72013-04-12 19:23:36 -0700513 // The number of bytes that have been downloaded for each source for each new
514 // update attempt. If we resume an update, we'll continue from the previous
515 // value, but if we get a new response or if the previous attempt failed,
516 // we'll reset this to 0 to start afresh. Each update to this value is
517 // persisted so we resume from the same value in case of a process restart.
518 // The extra index in the array is to no-op accidental access in case the
519 // return value from GetCurrentDownloadSource is used without validation.
520 uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
521
522 // The number of bytes that have been downloaded for each source since the
523 // the last successful update. This is used to compute the overhead we incur.
524 // Each update to this value is persisted so we resume from the same value in
525 // case of a process restart.
526 // The extra index in the array is to no-op accidental access in case the
527 // return value from GetCurrentDownloadSource is used without validation.
528 uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
529
David Zeuthen9a017f22013-04-11 16:10:26 -0700530 // A small timespan used when comparing wall-clock times for coping
531 // with the fact that clocks drift and consequently are adjusted
532 // (either forwards or backwards) via NTP.
533 static const base::TimeDelta kDurationSlack;
534
Jay Srinivasan53173b92013-05-17 17:13:01 -0700535 // The ordered list of the subset of payload URL candidates which are
536 // allowed as per device policy.
537 std::vector<std::string> candidate_urls_;
538
Chris Sosaaa18e162013-06-20 13:20:30 -0700539 // This stores a blacklisted version set as part of rollback. When we rollback
540 // we store the version of the os from which we are rolling back from in order
541 // to guarantee that we do not re-update to it on the next au attempt after
542 // reboot.
543 std::string rollback_version_;
544
David Zeuthen33bae492014-02-25 16:16:18 -0800545 // The number of bytes downloaded per attempt.
546 int64_t attempt_num_bytes_downloaded_;
547
548 // The boot time when the attempt was started.
549 base::Time attempt_start_time_boot_;
550
551 // The monotonic time when the attempt was started.
552 base::Time attempt_start_time_monotonic_;
553
David Zeuthenb281f072014-04-02 10:20:19 -0700554 // The connection type when the attempt started.
555 metrics::ConnectionType attempt_connection_type_;
556
David Zeuthenafed4a12014-04-09 15:28:44 -0700557 // Whether we're currently rolling back.
558 AttemptType attempt_type_;
559
Gilad Arnold519cfc72014-10-02 10:34:54 -0700560 // The current scattering wallclock-based wait period.
561 base::TimeDelta scattering_wait_period_;
562
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800563 DISALLOW_COPY_AND_ASSIGN(PayloadState);
564};
565
566} // namespace chromeos_update_engine
567
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700568#endif // UPDATE_ENGINE_PAYLOAD_STATE_H_