blob: eb9d6553c08c95475854ed0875b91c29ee684888 [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);
43 virtual void UpdateFailed(ActionExitCode error);
Jay Srinivasan08262882012-12-28 19:29:43 -080044 virtual bool ShouldBackoffDownload();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080045
Jay Srinivasan08262882012-12-28 19:29:43 -080046 virtual inline std::string GetResponseSignature() {
47 return response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080048 }
49
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080050 virtual inline uint32_t GetPayloadAttemptNumber() {
51 return payload_attempt_number_;
52 }
53
54 virtual inline uint32_t GetUrlIndex() {
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080055 return url_index_;
56 }
57
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080058 virtual inline uint32_t GetUrlFailureCount() {
59 return url_failure_count_;
60 }
61
Jay Srinivasan08262882012-12-28 19:29:43 -080062 virtual inline base::Time GetBackoffExpiryTime() {
63 return backoff_expiry_time_;
64 }
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080065
Jay Srinivasan08262882012-12-28 19:29:43 -080066 private:
67 // Increments the payload attempt number which governs the backoff behavior
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080068 // at the time of the next update check.
69 void IncrementPayloadAttemptNumber();
Jay Srinivasan6f6ea002012-12-14 11:26:28 -080070
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -080071 // Advances the current URL index to the next available one. If all URLs have
72 // been exhausted during the current payload download attempt (as indicated
73 // by the payload attempt number), then it will increment the payload attempt
74 // number and wrap around again with the first URL in the list.
75 void IncrementUrlIndex();
76
77 // Increments the failure count of the current URL. If the configured max
78 // failure count is reached for this URL, it advances the current URL index
79 // to the next URL and resets the failure count for that URL.
80 void IncrementFailureCount();
81
Jay Srinivasan08262882012-12-28 19:29:43 -080082 // Updates the backoff expiry time exponentially based on the current
83 // payload attempt number.
84 void UpdateBackoffExpiryTime();
85
86 // Resets all the persisted state values which are maintained relative to the
87 // current response signature. The response signature itself is not reset.
88 void ResetPersistedState();
89
90 // Calculates the response "signature", which is basically a string composed
91 // of the subset of the fields in the current response that affect the
92 // behavior of the PayloadState.
93 std::string CalculateResponseSignature();
94
95 // Initializes the current response signature from the persisted state.
96 void LoadResponseSignature();
97
98 // Sets the response signature to the given value. Also persists the value
99 // being set so that we resume from the save value in case of a process
100 // restart.
101 void SetResponseSignature(std::string response_signature);
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800102
103 // Initializes the payload attempt number from the persisted state.
104 void LoadPayloadAttemptNumber();
105
106 // Sets the payload attempt number to the given value. Also persists the
107 // value being set so that we resume from the same value in case of a process
108 // restart.
109 void SetPayloadAttemptNumber(uint32_t payload_attempt_number);
110
111 // Initializes the current URL index from the persisted state.
112 void LoadUrlIndex();
113
114 // Sets the current URL index to the given value. Also persists the value
115 // being set so that we resume from the same value in case of a process
116 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800117 void SetUrlIndex(uint32_t url_index);
118
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800119 // Initializes the current URL's failure count from the persisted stae.
120 void LoadUrlFailureCount();
121
122 // Sets the current URL's failure count to the given value. Also persists the
123 // value being set so that we resume from the same value in case of a process
124 // restart.
125 void SetUrlFailureCount(uint32_t url_failure_count);
126
Jay Srinivasan08262882012-12-28 19:29:43 -0800127 // Initializes the backoff expiry time from the persisted state.
128 void LoadBackoffExpiryTime();
129
130 // Sets the backoff expiry time to the given value. Also persists the value
131 // being set so that we resume from the same value in case of a process
132 // restart.
133 void SetBackoffExpiryTime(const base::Time& new_time);
134
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800135 // Interface object with which we read/write persisted state. This must
136 // be set by calling the Initialize method before calling any other method.
137 PrefsInterface* prefs_;
138
Jay Srinivasan08262882012-12-28 19:29:43 -0800139 // This is the current response object from Omaha.
140 OmahaResponse response_;
141
142 // This stores a "signature" of the current response. The signature here
143 // refers to a subset of the current response from Omaha. Each update to
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800144 // this value is persisted so we resume from the same value in case of a
145 // process restart.
Jay Srinivasan08262882012-12-28 19:29:43 -0800146 std::string response_signature_;
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800147
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800148 // The number of times we've tried to download the payload in full. This is
149 // incremented each time we download the payload in full successsfully or
150 // when we exhaust all failure limits for all URLs and are about to wrap
151 // around back to the first URL. Each update to this value is persisted so
152 // we resume from the same value in case of a process restart.
153 uint32_t payload_attempt_number_;
154
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800155 // The index of the current URL. This type is different from the one in the
156 // accessor methods because PrefsInterface supports only int64_t but we want
157 // to provide a stronger abstraction of uint32_t. Each update to this value
158 // is persisted so we resume from the same value in case of a process
159 // restart.
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800160 int64_t url_index_;
161
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800162 // The count of failures encountered in the current attempt to download using
163 // the current URL (specified by url_index_). Each update to this value is
164 // persisted so we resume from the same value in case of a process restart.
165 int64_t url_failure_count_;
166
Jay Srinivasan08262882012-12-28 19:29:43 -0800167 // The timestamp until which we've to wait before attempting to download the
168 // payload again, so as to backoff repeated downloads.
169 base::Time backoff_expiry_time_;
170
171 // Returns the number of URLs in the current response.
172 // Note: This value will be 0 if this method is called before we receive
173 // the first valid Omaha response in this process.
174 uint32_t GetNumUrls() {
175 return response_.payload_urls.size();
176 }
Jay Srinivasan2b5a0f02012-12-19 17:25:56 -0800177
Jay Srinivasan6f6ea002012-12-14 11:26:28 -0800178 DISALLOW_COPY_AND_ASSIGN(PayloadState);
179};
180
181} // namespace chromeos_update_engine
182
183#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__