Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1 | // 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 Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 8 | #include <base/time.h> |
| 9 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 10 | #include "update_engine/payload_state_interface.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 11 | #include "update_engine/prefs_interface.h" |
| 12 | |
| 13 | namespace chromeos_update_engine { |
| 14 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 15 | // Encapsulates all the payload state required for download. This includes the |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 16 | // state necessary for handling multiple URLs in Omaha response, the backoff |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 17 | // 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 Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 21 | class PayloadState : public PayloadStateInterface { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 22 | public: |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 23 | PayloadState() |
| 24 | : prefs_(NULL), |
| 25 | payload_attempt_number_(0), |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 26 | url_index_(0), |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 27 | url_failure_count_(0) {} |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 28 | |
| 29 | virtual ~PayloadState() {} |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 30 | |
| 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 Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 38 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 39 | // 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 Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 44 | virtual bool ShouldBackoffDownload(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 45 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 46 | virtual inline std::string GetResponseSignature() { |
| 47 | return response_signature_; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 50 | virtual inline uint32_t GetPayloadAttemptNumber() { |
| 51 | return payload_attempt_number_; |
| 52 | } |
| 53 | |
| 54 | virtual inline uint32_t GetUrlIndex() { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 55 | return url_index_; |
| 56 | } |
| 57 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 58 | virtual inline uint32_t GetUrlFailureCount() { |
| 59 | return url_failure_count_; |
| 60 | } |
| 61 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 62 | virtual inline base::Time GetBackoffExpiryTime() { |
| 63 | return backoff_expiry_time_; |
| 64 | } |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 65 | |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 66 | private: |
| 67 | // Increments the payload attempt number which governs the backoff behavior |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 68 | // at the time of the next update check. |
| 69 | void IncrementPayloadAttemptNumber(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 70 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 71 | // 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 Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 82 | // 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 Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 102 | |
| 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 Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 117 | void SetUrlIndex(uint32_t url_index); |
| 118 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 119 | // 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 Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 127 | // 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 Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 135 | // 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 Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 139 | // 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 Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 144 | // this value is persisted so we resume from the same value in case of a |
| 145 | // process restart. |
Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 146 | std::string response_signature_; |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 147 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 148 | // 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 Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 155 | // 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 Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 160 | int64_t url_index_; |
| 161 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 162 | // 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 Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 167 | // 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 Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 177 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 178 | DISALLOW_COPY_AND_ASSIGN(PayloadState); |
| 179 | }; |
| 180 | |
| 181 | } // namespace chromeos_update_engine |
| 182 | |
| 183 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__ |