blob: 0557d0afe655511d9fe7c1fb0a62c592f1eb96cd [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
5#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__
7
Jay Srinivasan08262882012-12-28 19:29:43 -08008#include <base/time.h>
9
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080010#include "update_engine/payload_state_interface.h"
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080011#include "update_engine/prefs_interface.h"
12
13namespace chromeos_update_engine {
14
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080015// Encapsulates all the payload state required for download. This includes the
Jay Srinivasan08262882012-12-28 19:29:43 -080016// state necessary for handling multiple URLs in Omaha response, the backoff
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080017// state, etc. All state is persisted so that we use the most recently saved
18// value when resuming the update_engine process. All state is also cached in
19// memory so that we ensure we always make progress based on last known good
20// state even when there's any issue in reading/writing from the file system.
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080021class PayloadState : public PayloadStateInterface {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080022 public:
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080023 PayloadState()
24 : prefs_(NULL),
25 payload_attempt_number_(0),
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080026 url_index_(0),
Jay Srinivasan08262882012-12-28 19:29:43 -080027 url_failure_count_(0) {}
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080028
29 virtual ~PayloadState() {}
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080030
31 // Initializes a payload state object using |prefs| for storing the
32 // persisted state. It also performs the initial loading of all persisted
33 // state into memory and dumps the initial state for debugging purposes.
34 // Note: the other methods should be called only after calling Initialize
35 // on this object.
36 bool Initialize(PrefsInterface* prefs);
37
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);
David Zeuthen9a017f22013-04-11 16:10:26 -070043 virtual void UpdateSucceeded();
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080044 virtual void UpdateFailed(ActionExitCode error);
Jay Srinivasan08262882012-12-28 19:29:43 -080045 virtual bool ShouldBackoffDownload();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080046
Jay Srinivasan08262882012-12-28 19:29:43 -080047 virtual inline std::string GetResponseSignature() {
48 return response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080049 }
50
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080051 virtual inline uint32_t GetPayloadAttemptNumber() {
52 return payload_attempt_number_;
53 }
54
55 virtual inline uint32_t GetUrlIndex() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080056 return url_index_;
57 }
58
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080059 virtual inline uint32_t GetUrlFailureCount() {
60 return url_failure_count_;
61 }
62
Jay Srinivasan08262882012-12-28 19:29:43 -080063 virtual inline base::Time GetBackoffExpiryTime() {
64 return backoff_expiry_time_;
65 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080066
David Zeuthen9a017f22013-04-11 16:10:26 -070067 virtual base::TimeDelta GetUpdateDuration();
68
69 virtual base::TimeDelta GetUpdateDurationUptime();
70
Jay Srinivasan08262882012-12-28 19:29:43 -080071 private:
72 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080073 // at the time of the next update check.
74 void IncrementPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080075
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080076 // Advances the current URL index to the next available one. If all URLs have
77 // been exhausted during the current payload download attempt (as indicated
78 // by the payload attempt number), then it will increment the payload attempt
79 // number and wrap around again with the first URL in the list.
80 void IncrementUrlIndex();
81
82 // Increments the failure count of the current URL. If the configured max
83 // failure count is reached for this URL, it advances the current URL index
84 // to the next URL and resets the failure count for that URL.
85 void IncrementFailureCount();
86
Jay Srinivasan08262882012-12-28 19:29:43 -080087 // Updates the backoff expiry time exponentially based on the current
88 // payload attempt number.
89 void UpdateBackoffExpiryTime();
90
91 // Resets all the persisted state values which are maintained relative to the
92 // current response signature. The response signature itself is not reset.
93 void ResetPersistedState();
94
95 // Calculates the response "signature", which is basically a string composed
96 // of the subset of the fields in the current response that affect the
97 // behavior of the PayloadState.
98 std::string CalculateResponseSignature();
99
100 // Initializes the current response signature from the persisted state.
101 void LoadResponseSignature();
102
103 // Sets the response signature to the given value. Also persists the value
104 // being set so that we resume from the save value in case of a process
105 // restart.
106 void SetResponseSignature(std::string response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800107
108 // Initializes the payload attempt number from the persisted state.
109 void LoadPayloadAttemptNumber();
110
111 // Sets the payload attempt number to the given value. Also persists the
112 // value being set so that we resume from the same value in case of a process
113 // restart.
114 void SetPayloadAttemptNumber(uint32_t payload_attempt_number);
115
116 // Initializes the current URL index from the persisted state.
117 void LoadUrlIndex();
118
119 // Sets the current URL index to the given value. Also persists the value
120 // being set so that we resume from the same value in case of a process
121 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800122 void SetUrlIndex(uint32_t url_index);
123
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800124 // Initializes the current URL's failure count from the persisted stae.
125 void LoadUrlFailureCount();
126
127 // Sets the current URL's failure count to the given value. Also persists the
128 // value being set so that we resume from the same value in case of a process
129 // restart.
130 void SetUrlFailureCount(uint32_t url_failure_count);
131
Jay Srinivasan08262882012-12-28 19:29:43 -0800132 // Initializes the backoff expiry time from the persisted state.
133 void LoadBackoffExpiryTime();
134
135 // Sets the backoff expiry time to the given value. Also persists the value
136 // being set so that we resume from the same value in case of a process
137 // restart.
138 void SetBackoffExpiryTime(const base::Time& new_time);
139
David Zeuthen9a017f22013-04-11 16:10:26 -0700140 // Initializes |update_timestamp_start_| from the persisted state.
141 void LoadUpdateTimestampStart();
142
143 // Sets |update_timestamp_start_| to the given value and persists the value.
144 void SetUpdateTimestampStart(const base::Time& value);
145
146 // Sets |update_timestamp_end_| to the given value. This is not persisted
147 // as it happens at the end of the update process where state is deleted
148 // anyway.
149 void SetUpdateTimestampEnd(const base::Time& value);
150
151 // Initializes |update_duration_uptime_| from the persisted state.
152 void LoadUpdateDurationUptime();
153
154 // Helper method used in SetUpdateDurationUptime() and
155 // CalculateUpdateDurationUptime().
156 void SetUpdateDurationUptimeExtended(const base::TimeDelta& value,
157 const base::Time& timestamp,
158 bool use_logging);
159
160 // Sets |update_duration_uptime_| to the given value and persists
161 // the value and sets |update_duration_uptime_timestamp_| to the
162 // current monotonic time.
163 void SetUpdateDurationUptime(const base::TimeDelta& value);
164
165 // Adds the difference between current monotonic time and
166 // |update_duration_uptime_timestamp_| to |update_duration_uptime_| and
167 // sets |update_duration_uptime_timestamp_| to current monotonic time.
168 void CalculateUpdateDurationUptime();
169
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800170 // Interface object with which we read/write persisted state. This must
171 // be set by calling the Initialize method before calling any other method.
172 PrefsInterface* prefs_;
173
Jay Srinivasan08262882012-12-28 19:29:43 -0800174 // This is the current response object from Omaha.
175 OmahaResponse response_;
176
177 // This stores a "signature" of the current response. The signature here
178 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800179 // this value is persisted so we resume from the same value in case of a
180 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800181 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800182
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800183 // The number of times we've tried to download the payload in full. This is
184 // incremented each time we download the payload in full successsfully or
185 // when we exhaust all failure limits for all URLs and are about to wrap
186 // around back to the first URL. Each update to this value is persisted so
187 // we resume from the same value in case of a process restart.
188 uint32_t payload_attempt_number_;
189
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800190 // The index of the current URL. This type is different from the one in the
191 // accessor methods because PrefsInterface supports only int64_t but we want
192 // to provide a stronger abstraction of uint32_t. Each update to this value
193 // is persisted so we resume from the same value in case of a process
194 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800195 int64_t url_index_;
196
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800197 // The count of failures encountered in the current attempt to download using
198 // the current URL (specified by url_index_). Each update to this value is
199 // persisted so we resume from the same value in case of a process restart.
200 int64_t url_failure_count_;
201
Jay Srinivasan08262882012-12-28 19:29:43 -0800202 // The timestamp until which we've to wait before attempting to download the
203 // payload again, so as to backoff repeated downloads.
204 base::Time backoff_expiry_time_;
205
David Zeuthen9a017f22013-04-11 16:10:26 -0700206 // The most recently calculated value of the update duration.
207 base::TimeDelta update_duration_current_;
208
209 // The point in time (wall-clock) that the update was started.
210 base::Time update_timestamp_start_;
211
212 // The point in time (wall-clock) that the update ended. If the update
213 // is still in progress, this is set to the Epoch (e.g. 0).
214 base::Time update_timestamp_end_;
215
216 // The update duration uptime
217 base::TimeDelta update_duration_uptime_;
218
219 // The monotonic time when |update_duration_uptime_| was last set
220 base::Time update_duration_uptime_timestamp_;
221
Jay Srinivasan08262882012-12-28 19:29:43 -0800222 // Returns the number of URLs in the current response.
223 // Note: This value will be 0 if this method is called before we receive
224 // the first valid Omaha response in this process.
225 uint32_t GetNumUrls() {
226 return response_.payload_urls.size();
227 }
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800228
David Zeuthen9a017f22013-04-11 16:10:26 -0700229 // A small timespan used when comparing wall-clock times for coping
230 // with the fact that clocks drift and consequently are adjusted
231 // (either forwards or backwards) via NTP.
232 static const base::TimeDelta kDurationSlack;
233
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800234 DISALLOW_COPY_AND_ASSIGN(PayloadState);
235};
236
237} // namespace chromeos_update_engine
238
239#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__