blob: bc4bf0dd047d23de3a60e4d92bd18a37b4bb5c76 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2012 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080016
Gilad Arnoldcf175a02014-07-10 16:48:47 -070017#ifndef UPDATE_ENGINE_PAYLOAD_STATE_H_
18#define UPDATE_ENGINE_PAYLOAD_STATE_H_
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080019
Tianjie Xu90aaa102017-10-10 17:39:03 -070020#include <algorithm>
Alex Vakulenkod2779df2014-06-16 13:19:00 -070021#include <string>
22#include <vector>
23
Alex Vakulenko75039d72014-03-25 12:36:28 -070024#include <base/time/time.h>
Alex Deymo42432912013-07-12 20:21:15 -070025#include <gtest/gtest_prod.h> // for FRIEND_TEST
Jay Srinivasan08262882012-12-28 19:29:43 -080026
Jae Hoon Kim5e8e30b2020-05-06 14:59:06 -070027#include "update_engine/common/excluder_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/prefs_interface.h"
Tianjie Xu282aa1f2017-09-05 13:42:45 -070029#include "update_engine/metrics_constants.h"
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080030#include "update_engine/payload_state_interface.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080031
32namespace chromeos_update_engine {
33
Jay Srinivasan19409b72013-04-12 19:23:36 -070034class SystemState;
35
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080036// Encapsulates all the payload state required for download. This includes the
Jay Srinivasan08262882012-12-28 19:29:43 -080037// state necessary for handling multiple URLs in Omaha response, the backoff
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080038// state, etc. All state is persisted so that we use the most recently saved
39// value when resuming the update_engine process. All state is also cached in
40// memory so that we ensure we always make progress based on last known good
41// state even when there's any issue in reading/writing from the file system.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080042class PayloadState : public PayloadStateInterface {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080043 public:
Jay Srinivasan19409b72013-04-12 19:23:36 -070044 PayloadState();
Alex Deymo610277e2014-11-11 21:18:11 -080045 ~PayloadState() override {}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080046
Jay Srinivasan19409b72013-04-12 19:23:36 -070047 // Initializes a payload state object using the given global system state.
48 // It performs the initial loading of all persisted state into memory and
49 // dumps the initial state for debugging purposes. Note: the other methods
50 // should be called only after calling Initialize on this object.
51 bool Initialize(SystemState* system_state);
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080052
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080053 // Implementation of PayloadStateInterface methods.
Alex Deymo610277e2014-11-11 21:18:11 -080054 void SetResponse(const OmahaResponse& response) override;
55 void DownloadComplete() override;
56 void DownloadProgress(size_t count) override;
57 void UpdateResumed() override;
58 void UpdateRestarted() override;
59 void UpdateSucceeded() override;
60 void UpdateFailed(ErrorCode error) override;
61 void ResetUpdateStatus() override;
62 bool ShouldBackoffDownload() override;
63 void Rollback() override;
64 void ExpectRebootInNewVersion(const std::string& target_version_uid) override;
65 void SetUsingP2PForDownloading(bool value) override;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080066
Gilad Arnold74b5f552014-10-07 08:17:16 -070067 void SetUsingP2PForSharing(bool value) override {
68 using_p2p_for_sharing_ = value;
69 }
70
Alex Deymo610277e2014-11-11 21:18:11 -080071 inline std::string GetResponseSignature() override {
Jay Srinivasan08262882012-12-28 19:29:43 -080072 return response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080073 }
74
Alex Deymo610277e2014-11-11 21:18:11 -080075 inline int GetFullPayloadAttemptNumber() override {
Alex Deymo820cc702013-06-28 15:43:46 -070076 return full_payload_attempt_number_;
77 }
78
Alex Deymo610277e2014-11-11 21:18:11 -080079 inline int GetPayloadAttemptNumber() override {
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080080 return payload_attempt_number_;
81 }
82
Alex Deymo610277e2014-11-11 21:18:11 -080083 inline std::string GetCurrentUrl() override {
Amin Hassani91502232018-04-13 10:31:52 -070084 return (payload_index_ < candidate_urls_.size() &&
85 url_index_ < candidate_urls_[payload_index_].size())
Sen Jiang0affc2c2017-02-10 15:55:05 -080086 ? candidate_urls_[payload_index_][url_index_]
87 : "";
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080088 }
89
Amin Hassani7cc8bb02019-01-14 16:29:47 -080090 inline uint32_t GetUrlFailureCount() override { return url_failure_count_; }
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080091
Amin Hassani7cc8bb02019-01-14 16:29:47 -080092 inline uint32_t GetUrlSwitchCount() override { return url_switch_count_; }
David Zeuthencc6f9962013-04-18 11:57:24 -070093
Amin Hassani7cc8bb02019-01-14 16:29:47 -080094 inline int GetNumResponsesSeen() override { return num_responses_seen_; }
David Zeuthena573d6f2013-06-14 16:13:36 -070095
Alex Deymo610277e2014-11-11 21:18:11 -080096 inline base::Time GetBackoffExpiryTime() override {
Jay Srinivasan08262882012-12-28 19:29:43 -080097 return backoff_expiry_time_;
98 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080099
Alex Deymo610277e2014-11-11 21:18:11 -0800100 base::TimeDelta GetUpdateDuration() override;
David Zeuthen9a017f22013-04-11 16:10:26 -0700101
Alex Deymo610277e2014-11-11 21:18:11 -0800102 base::TimeDelta GetUpdateDurationUptime() override;
David Zeuthen9a017f22013-04-11 16:10:26 -0700103
Alex Deymo610277e2014-11-11 21:18:11 -0800104 inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) override {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700105 return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0;
106 }
107
Alex Deymo610277e2014-11-11 21:18:11 -0800108 inline uint64_t GetTotalBytesDownloaded(DownloadSource source) override {
Jay Srinivasan19409b72013-04-12 19:23:36 -0700109 return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0;
110 }
111
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800112 inline uint32_t GetNumReboots() override { return num_reboots_; }
Chris Sosabe45bef2013-04-09 18:25:12 -0700113
Alex Deymo610277e2014-11-11 21:18:11 -0800114 void UpdateEngineStarted() override;
David Zeuthene4c58bf2013-06-18 17:26:50 -0700115
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200116 inline bool GetRollbackHappened() override { return rollback_happened_; }
117
118 void SetRollbackHappened(bool rollback_happened) override;
119
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800120 inline std::string GetRollbackVersion() override { return rollback_version_; }
Chris Sosaaa18e162013-06-20 13:20:30 -0700121
Alex Deymo610277e2014-11-11 21:18:11 -0800122 int GetP2PNumAttempts() override;
123 base::Time GetP2PFirstAttemptTimestamp() override;
124 void P2PNewAttempt() override;
125 bool P2PAttemptAllowed() override;
David Zeuthendcba8092013-08-06 12:16:35 -0700126
Gilad Arnold74b5f552014-10-07 08:17:16 -0700127 bool GetUsingP2PForDownloading() const override {
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700128 return using_p2p_for_downloading_;
129 }
130
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800131 bool GetUsingP2PForSharing() const override { return using_p2p_for_sharing_; }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700132
Gilad Arnold519cfc72014-10-02 10:34:54 -0700133 base::TimeDelta GetScatteringWaitPeriod() override {
134 return scattering_wait_period_;
135 }
136
137 void SetScatteringWaitPeriod(base::TimeDelta wait_period) override;
138
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700139 void SetStagingWaitPeriod(base::TimeDelta wait_period) override;
140
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800141 void SetP2PUrl(const std::string& url) override { p2p_url_ = url; }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700142
Amin Hassani7cc8bb02019-01-14 16:29:47 -0800143 std::string GetP2PUrl() const override { return p2p_url_; }
Gilad Arnold74b5f552014-10-07 08:17:16 -0700144
Sen Jiang5ae865b2017-04-18 14:24:40 -0700145 bool NextPayload() override;
Sen Jiang6c736682017-03-10 15:01:36 -0800146
Jay Srinivasan08262882012-12-28 19:29:43 -0800147 private:
David Zeuthenafed4a12014-04-09 15:28:44 -0700148 enum class AttemptType {
149 kUpdate,
150 kRollback,
151 };
152
Alex Deymo42432912013-07-12 20:21:15 -0700153 friend class PayloadStateTest;
154 FRIEND_TEST(PayloadStateTest, RebootAfterUpdateFailedMetric);
155 FRIEND_TEST(PayloadStateTest, RebootAfterUpdateSucceed);
156 FRIEND_TEST(PayloadStateTest, RebootAfterCanceledUpdate);
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200157 FRIEND_TEST(PayloadStateTest, RollbackHappened);
Chris Sosab3dcdb32013-09-04 15:22:12 -0700158 FRIEND_TEST(PayloadStateTest, RollbackVersion);
Alex Deymo42432912013-07-12 20:21:15 -0700159 FRIEND_TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs);
Jae Hoon Kima3210e62020-05-07 11:32:44 -0700160 FRIEND_TEST(PayloadStateTest, NextPayloadResetsUrlIndex);
Alex Deymo42432912013-07-12 20:21:15 -0700161
David Zeuthen33bae492014-02-25 16:16:18 -0800162 // Helper called when an attempt has begun, is called by
David Zeuthenafed4a12014-04-09 15:28:44 -0700163 // UpdateResumed(), UpdateRestarted() and Rollback().
164 void AttemptStarted(AttemptType attempt_type);
David Zeuthen33bae492014-02-25 16:16:18 -0800165
Alex Deymo820cc702013-06-28 15:43:46 -0700166 // Increments the payload attempt number used for metrics.
167 void IncrementPayloadAttemptNumber();
168
Jay Srinivasan08262882012-12-28 19:29:43 -0800169 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800170 // at the time of the next update check.
Alex Deymo820cc702013-06-28 15:43:46 -0700171 void IncrementFullPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800172
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800173 // Advances the current URL index to the next available one. If all URLs have
174 // been exhausted during the current payload download attempt (as indicated
175 // by the payload attempt number), then it will increment the payload attempt
David Zeuthencc6f9962013-04-18 11:57:24 -0700176 // number and wrap around again with the first URL in the list. This also
177 // updates the URL switch count, if needed.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800178 void IncrementUrlIndex();
179
180 // Increments the failure count of the current URL. If the configured max
181 // failure count is reached for this URL, it advances the current URL index
182 // to the next URL and resets the failure count for that URL.
183 void IncrementFailureCount();
184
Jay Srinivasan08262882012-12-28 19:29:43 -0800185 // Updates the backoff expiry time exponentially based on the current
186 // payload attempt number.
187 void UpdateBackoffExpiryTime();
188
Jay Srinivasan19409b72013-04-12 19:23:36 -0700189 // Updates the value of current download source based on the current URL
190 // index. If the download source is not one of the known sources, it's set
191 // to kNumDownloadSources.
192 void UpdateCurrentDownloadSource();
193
194 // Updates the various metrics corresponding with the given number of bytes
195 // that were downloaded recently.
196 void UpdateBytesDownloaded(size_t count);
197
David Zeuthen33bae492014-02-25 16:16:18 -0800198 // Calculates the PayloadType we're using.
199 PayloadType CalculatePayloadType();
Jay Srinivasan19409b72013-04-12 19:23:36 -0700200
David Zeuthen33bae492014-02-25 16:16:18 -0800201 // Collects and reports the various metrics related to an update attempt.
202 void CollectAndReportAttemptMetrics(ErrorCode code);
David Zeuthencc6f9962013-04-18 11:57:24 -0700203
David Zeuthen4e1d1492014-04-25 13:12:27 -0700204 // Persists values related to the UpdateEngine.Attempt.* metrics so
205 // we can identify later if an update attempt ends abnormally.
206 void PersistAttemptMetrics();
207
208 // Clears persistent state previously set using AttemptMetricsPersist().
209 void ClearPersistedAttemptMetrics();
210
211 // Checks if persistent state previously set using AttemptMetricsPersist()
212 // exists and, if so, emits it with |attempt_result| set to
213 // metrics::AttemptResult::kAbnormalTermination.
214 void ReportAndClearPersistedAttemptMetrics();
215
David Zeuthen33bae492014-02-25 16:16:18 -0800216 // Collects and reports the various metrics related to a successful update.
217 void CollectAndReportSuccessfulUpdateMetrics();
Alex Deymo820cc702013-06-28 15:43:46 -0700218
Alex Deymo42432912013-07-12 20:21:15 -0700219 // Checks if we were expecting to be running in the new version but the
220 // boot into the new version failed for some reason. If that's the case, an
221 // UMA metric is sent reporting the number of attempts the same applied
222 // payload was attempted to reboot. This function is called by UpdateAttempter
223 // every time the update engine starts and there's no reboot pending.
224 void ReportFailedBootIfNeeded();
225
Jay Srinivasan08262882012-12-28 19:29:43 -0800226 // Resets all the persisted state values which are maintained relative to the
227 // current response signature. The response signature itself is not reset.
228 void ResetPersistedState();
229
Jay Srinivasan19409b72013-04-12 19:23:36 -0700230 // Resets the appropriate state related to download sources that need to be
231 // reset on a new update.
232 void ResetDownloadSourcesOnNewUpdate();
233
Jay Srinivasan08262882012-12-28 19:29:43 -0800234 // Calculates the response "signature", which is basically a string composed
235 // of the subset of the fields in the current response that affect the
236 // behavior of the PayloadState.
237 std::string CalculateResponseSignature();
238
239 // Initializes the current response signature from the persisted state.
240 void LoadResponseSignature();
241
242 // Sets the response signature to the given value. Also persists the value
243 // being set so that we resume from the save value in case of a process
244 // restart.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700245 void SetResponseSignature(const std::string& response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800246
247 // Initializes the payload attempt number from the persisted state.
248 void LoadPayloadAttemptNumber();
249
Alex Deymo820cc702013-06-28 15:43:46 -0700250 // Initializes the payload attempt number for full payloads from the persisted
251 // state.
252 void LoadFullPayloadAttemptNumber();
253
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800254 // Sets the payload attempt number to the given value. Also persists the
255 // value being set so that we resume from the same value in case of a process
256 // restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700257 void SetPayloadAttemptNumber(int payload_attempt_number);
258
259 // Sets the payload attempt number for full updates to the given value. Also
260 // persists the value being set so that we resume from the same value in case
261 // of a process restart.
262 void SetFullPayloadAttemptNumber(int payload_attempt_number);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800263
Sen Jiang5ae865b2017-04-18 14:24:40 -0700264 // Sets the current payload index to the given value. Also persists the value
265 // being set so that we resume from the same value in case of a process
266 // restart.
267 void SetPayloadIndex(size_t payload_index);
268
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800269 // Initializes the current URL index from the persisted state.
270 void LoadUrlIndex();
271
272 // Sets the current URL index to the given value. Also persists the value
273 // being set so that we resume from the same value in case of a process
274 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800275 void SetUrlIndex(uint32_t url_index);
276
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800277 // Initializes the current URL's failure count from the persisted stae.
278 void LoadUrlFailureCount();
279
280 // Sets the current URL's failure count to the given value. Also persists the
281 // value being set so that we resume from the same value in case of a process
282 // restart.
283 void SetUrlFailureCount(uint32_t url_failure_count);
284
David Zeuthencc6f9962013-04-18 11:57:24 -0700285 // Sets |url_switch_count_| to the given value and persists the value.
286 void SetUrlSwitchCount(uint32_t url_switch_count);
287
288 // Initializes |url_switch_count_| from the persisted stae.
289 void LoadUrlSwitchCount();
290
Jay Srinivasan08262882012-12-28 19:29:43 -0800291 // Initializes the backoff expiry time from the persisted state.
292 void LoadBackoffExpiryTime();
293
294 // Sets the backoff expiry time to the given value. Also persists the value
295 // being set so that we resume from the same value in case of a process
296 // restart.
297 void SetBackoffExpiryTime(const base::Time& new_time);
298
David Zeuthen9a017f22013-04-11 16:10:26 -0700299 // Initializes |update_timestamp_start_| from the persisted state.
300 void LoadUpdateTimestampStart();
301
302 // Sets |update_timestamp_start_| to the given value and persists the value.
303 void SetUpdateTimestampStart(const base::Time& value);
304
305 // Sets |update_timestamp_end_| to the given value. This is not persisted
306 // as it happens at the end of the update process where state is deleted
307 // anyway.
308 void SetUpdateTimestampEnd(const base::Time& value);
309
310 // Initializes |update_duration_uptime_| from the persisted state.
311 void LoadUpdateDurationUptime();
312
313 // Helper method used in SetUpdateDurationUptime() and
314 // CalculateUpdateDurationUptime().
315 void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
316 const base::Time& timestamp,
317 bool use_logging);
318
319 // Sets |update_duration_uptime_| to the given value and persists
320 // the value and sets |update_duration_uptime_timestamp_| to the
321 // current monotonic time.
322 void SetUpdateDurationUptime(const base::TimeDelta& value);
323
324 // Adds the difference between current monotonic time and
325 // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
326 // sets |update_duration_uptime_timestamp_| to current monotonic time.
327 void CalculateUpdateDurationUptime();
328
Jay Srinivasan19409b72013-04-12 19:23:36 -0700329 // Returns the full key for a download source given the prefix.
330 std::string GetPrefsKey(const std::string& prefix, DownloadSource source);
331
332 // Loads the number of bytes that have been currently downloaded through the
333 // previous attempts from the persisted state for the given source. It's
Sen Jiang771f6482018-04-04 17:59:10 -0700334 // reset to 0 every time we begin a full update and is continued from previous
Jay Srinivasan19409b72013-04-12 19:23:36 -0700335 // attempt if we're resuming the update.
336 void LoadCurrentBytesDownloaded(DownloadSource source);
337
338 // Sets the number of bytes that have been currently downloaded for the
339 // given source. This value is also persisted.
340 void SetCurrentBytesDownloaded(DownloadSource source,
341 uint64_t current_bytes_downloaded,
342 bool log);
343
344 // Loads the total number of bytes that have been downloaded (since the last
345 // successful update) from the persisted state for the given source. It's
Sen Jiang771f6482018-04-04 17:59:10 -0700346 // reset to 0 every time we successfully apply an update and counts the bytes
Jay Srinivasan19409b72013-04-12 19:23:36 -0700347 // downloaded for both successful and failed attempts since then.
348 void LoadTotalBytesDownloaded(DownloadSource source);
349
350 // Sets the total number of bytes that have been downloaded so far for the
351 // given source. This value is also persisted.
352 void SetTotalBytesDownloaded(DownloadSource source,
353 uint64_t total_bytes_downloaded,
354 bool log);
355
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200356 // Loads whether rollback has happened on this device since the last update
357 // check where policy was available. This info is preserved over powerwash.
358 void LoadRollbackHappened();
359
Chris Sosaaa18e162013-06-20 13:20:30 -0700360 // Loads the blacklisted version from our prefs file.
361 void LoadRollbackVersion();
362
363 // Blacklists this version from getting AU'd to until we receive a new update
364 // response.
365 void SetRollbackVersion(const std::string& rollback_version);
366
367 // Clears any blacklisted version.
368 void ResetRollbackVersion();
369
Jay Srinivasan53173b92013-05-17 17:13:01 -0700370 inline uint32_t GetUrlIndex() {
Amin Hassani91502232018-04-13 10:31:52 -0700371 return (url_index_ != 0 && payload_index_ < candidate_urls_.size())
372 ? std::min(candidate_urls_[payload_index_].size() - 1,
373 url_index_)
374 : 0;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700375 }
376
377 // Computes the list of candidate URLs from the total list of payload URLs in
378 // the Omaha response.
379 void ComputeCandidateUrls();
380
David Zeuthena573d6f2013-06-14 16:13:36 -0700381 // Sets |num_responses_seen_| and persist it to disk.
382 void SetNumResponsesSeen(int num_responses_seen);
383
384 // Initializes |num_responses_seen_| from persisted state.
385 void LoadNumResponsesSeen();
386
Chris Sosabe45bef2013-04-09 18:25:12 -0700387 // Initializes |num_reboots_| from the persisted state.
388 void LoadNumReboots();
389
390 // Sets |num_reboots| for the update attempt. Also persists the
391 // value being set so that we resume from the same value in case of a process
392 // restart.
393 void SetNumReboots(uint32_t num_reboots);
394
395 // Checks to see if the device rebooted since the last call and if so
396 // increments num_reboots.
397 void UpdateNumReboots();
398
David Zeuthendcba8092013-08-06 12:16:35 -0700399 // Loads the |kPrefsP2PFirstAttemptTimestamp| state variable from disk
400 // into |p2p_first_attempt_timestamp_|.
401 void LoadP2PFirstAttemptTimestamp();
402
403 // Loads the |kPrefsP2PNumAttempts| state variable into |p2p_num_attempts_|.
404 void LoadP2PNumAttempts();
405
406 // Sets the |kPrefsP2PNumAttempts| state variable to |value|.
407 void SetP2PNumAttempts(int value);
408
409 // Sets the |kPrefsP2PFirstAttemptTimestamp| state variable to |time|.
410 void SetP2PFirstAttemptTimestamp(const base::Time& time);
411
Gilad Arnold519cfc72014-10-02 10:34:54 -0700412 // Loads the persisted scattering wallclock-based wait period.
413 void LoadScatteringWaitPeriod();
414
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700415 // Loads the persisted staging wallclock-based wait period.
416 void LoadStagingWaitPeriod();
417
Sen Jiang0affc2c2017-02-10 15:55:05 -0800418 // Get the total size of all payloads.
419 int64_t GetPayloadSize();
420
Gilad Arnold6e15aac2014-10-02 10:34:14 -0700421 // The global state of the system.
422 SystemState* system_state_;
423
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800424 // Interface object with which we read/write persisted state. This must
425 // be set by calling the Initialize method before calling any other method.
426 PrefsInterface* prefs_;
427
Chris Sosaaa18e162013-06-20 13:20:30 -0700428 // Interface object with which we read/write persisted state. This must
429 // be set by calling the Initialize method before calling any other method.
430 // This object persists across powerwashes.
431 PrefsInterface* powerwash_safe_prefs_;
432
Jae Hoon Kim5e8e30b2020-05-06 14:59:06 -0700433 // Interface object with which we determine exclusion decisions for
434 // payloads/partitions during the update. This must be set by calling the
435 // Initialize method before calling any other method.
436 ExcluderInterface* excluder_;
437
Jay Srinivasan08262882012-12-28 19:29:43 -0800438 // This is the current response object from Omaha.
439 OmahaResponse response_;
440
Gilad Arnold74b5f552014-10-07 08:17:16 -0700441 // Whether P2P is being used for downloading and sharing.
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700442 bool using_p2p_for_downloading_;
Gilad Arnold74b5f552014-10-07 08:17:16 -0700443 bool using_p2p_for_sharing_;
444
445 // Stores the P2P download URL, if one is used.
446 std::string p2p_url_;
447
448 // The cached value of |kPrefsP2PFirstAttemptTimestamp|.
449 base::Time p2p_first_attempt_timestamp_;
450
451 // The cached value of |kPrefsP2PNumAttempts|.
452 int p2p_num_attempts_;
David Zeuthenbb8bdc72013-09-03 13:43:48 -0700453
Jay Srinivasan08262882012-12-28 19:29:43 -0800454 // This stores a "signature" of the current response. The signature here
455 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800456 // this value is persisted so we resume from the same value in case of a
457 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800458 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800459
Alex Deymo820cc702013-06-28 15:43:46 -0700460 // The number of times we've tried to download the payload. This is
461 // incremented each time we download the payload successsfully or when we
462 // exhaust all failure limits for all URLs and are about to wrap around back
463 // to the first URL. Each update to this value is persisted so we resume from
464 // the same value in case of a process restart.
465 int payload_attempt_number_;
466
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800467 // The number of times we've tried to download the payload in full. This is
468 // incremented each time we download the payload in full successsfully or
469 // when we exhaust all failure limits for all URLs and are about to wrap
470 // around back to the first URL. Each update to this value is persisted so
471 // we resume from the same value in case of a process restart.
Alex Deymo820cc702013-06-28 15:43:46 -0700472 int full_payload_attempt_number_;
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800473
Sen Jiang0affc2c2017-02-10 15:55:05 -0800474 // The index of the current payload.
475 size_t payload_index_ = 0;
476
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800477 // The index of the current URL. This type is different from the one in the
478 // accessor methods because PrefsInterface supports only int64_t but we want
479 // to provide a stronger abstraction of uint32_t. Each update to this value
480 // is persisted so we resume from the same value in case of a process
481 // restart.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800482 size_t url_index_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800483
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800484 // The count of failures encountered in the current attempt to download using
485 // the current URL (specified by url_index_). Each update to this value is
486 // persisted so we resume from the same value in case of a process restart.
487 int64_t url_failure_count_;
488
David Zeuthencc6f9962013-04-18 11:57:24 -0700489 // The number of times we've switched URLs.
490 int32_t url_switch_count_;
491
Jay Srinivasan19409b72013-04-12 19:23:36 -0700492 // The current download source based on the current URL. This value is
Sen Jiang771f6482018-04-04 17:59:10 -0700493 // not persisted as it can be recomputed every time we update the URL.
Jay Srinivasan19409b72013-04-12 19:23:36 -0700494 // We're storing this so as not to recompute this on every few bytes of
495 // data we read from the socket.
496 DownloadSource current_download_source_;
497
David Zeuthena573d6f2013-06-14 16:13:36 -0700498 // The number of different Omaha responses seen. Increases every time
499 // a new response is seen. Resets to 0 only when the system has been
500 // successfully updated.
501 int num_responses_seen_;
502
Chris Sosabe45bef2013-04-09 18:25:12 -0700503 // The number of system reboots during an update attempt. Technically since
504 // we don't go out of our way to not update it when not attempting an update,
505 // also records the number of reboots before the next update attempt starts.
506 uint32_t num_reboots_;
507
Jay Srinivasan08262882012-12-28 19:29:43 -0800508 // The timestamp until which we've to wait before attempting to download the
509 // payload again, so as to backoff repeated downloads.
510 base::Time backoff_expiry_time_;
511
David Zeuthen9a017f22013-04-11 16:10:26 -0700512 // The most recently calculated value of the update duration.
513 base::TimeDelta update_duration_current_;
514
515 // The point in time (wall-clock) that the update was started.
516 base::Time update_timestamp_start_;
517
518 // The point in time (wall-clock) that the update ended. If the update
519 // is still in progress, this is set to the Epoch (e.g. 0).
520 base::Time update_timestamp_end_;
521
522 // The update duration uptime
523 base::TimeDelta update_duration_uptime_;
524
525 // The monotonic time when |update_duration_uptime_| was last set
526 base::Time update_duration_uptime_timestamp_;
527
Jay Srinivasan19409b72013-04-12 19:23:36 -0700528 // The number of bytes that have been downloaded for each source for each new
529 // update attempt. If we resume an update, we'll continue from the previous
530 // value, but if we get a new response or if the previous attempt failed,
531 // we'll reset this to 0 to start afresh. Each update to this value is
532 // persisted so we resume from the same value in case of a process restart.
533 // The extra index in the array is to no-op accidental access in case the
534 // return value from GetCurrentDownloadSource is used without validation.
535 uint64_t current_bytes_downloaded_[kNumDownloadSources + 1];
536
537 // The number of bytes that have been downloaded for each source since the
538 // the last successful update. This is used to compute the overhead we incur.
539 // Each update to this value is persisted so we resume from the same value in
540 // case of a process restart.
541 // The extra index in the array is to no-op accidental access in case the
542 // return value from GetCurrentDownloadSource is used without validation.
543 uint64_t total_bytes_downloaded_[kNumDownloadSources + 1];
544
David Zeuthen9a017f22013-04-11 16:10:26 -0700545 // A small timespan used when comparing wall-clock times for coping
546 // with the fact that clocks drift and consequently are adjusted
547 // (either forwards or backwards) via NTP.
548 static const base::TimeDelta kDurationSlack;
549
Jay Srinivasan53173b92013-05-17 17:13:01 -0700550 // The ordered list of the subset of payload URL candidates which are
551 // allowed as per device policy.
Sen Jiang0affc2c2017-02-10 15:55:05 -0800552 std::vector<std::vector<std::string>> candidate_urls_;
Jay Srinivasan53173b92013-05-17 17:13:01 -0700553
Marton Hunyadye58bddb2018-04-10 20:27:26 +0200554 // This stores whether rollback has happened since the last time device policy
555 // was available during update check. When this is set, we're preventing
556 // forced updates to avoid update-rollback loops.
557 bool rollback_happened_;
558
Chris Sosaaa18e162013-06-20 13:20:30 -0700559 // This stores a blacklisted version set as part of rollback. When we rollback
560 // we store the version of the os from which we are rolling back from in order
561 // to guarantee that we do not re-update to it on the next au attempt after
562 // reboot.
563 std::string rollback_version_;
564
David Zeuthen33bae492014-02-25 16:16:18 -0800565 // The number of bytes downloaded per attempt.
566 int64_t attempt_num_bytes_downloaded_;
567
568 // The boot time when the attempt was started.
569 base::Time attempt_start_time_boot_;
570
571 // The monotonic time when the attempt was started.
572 base::Time attempt_start_time_monotonic_;
573
David Zeuthenb281f072014-04-02 10:20:19 -0700574 // The connection type when the attempt started.
575 metrics::ConnectionType attempt_connection_type_;
576
David Zeuthenafed4a12014-04-09 15:28:44 -0700577 // Whether we're currently rolling back.
578 AttemptType attempt_type_;
579
Gilad Arnold519cfc72014-10-02 10:34:54 -0700580 // The current scattering wallclock-based wait period.
581 base::TimeDelta scattering_wait_period_;
582
Adolfo Victoriad3a1e352018-07-16 11:40:47 -0700583 // The current staging wallclock-based wait period.
584 base::TimeDelta staging_wait_period_;
585
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800586 DISALLOW_COPY_AND_ASSIGN(PayloadState);
587};
588
589} // namespace chromeos_update_engine
590
Gilad Arnoldcf175a02014-07-10 16:48:47 -0700591#endif // UPDATE_ENGINE_PAYLOAD_STATE_H_