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 | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame^] | 8 | #include "update_engine/payload_state_interface.h" |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 9 | #include "update_engine/prefs_interface.h" |
| 10 | |
| 11 | namespace chromeos_update_engine { |
| 12 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 13 | // Encapsulates all the payload state required for download. This includes the |
| 14 | // state necessary for handling multiple URLs in Omaha response, the back-off |
| 15 | // state, etc. All state is persisted so that we use the most recently saved |
| 16 | // value when resuming the update_engine process. All state is also cached in |
| 17 | // memory so that we ensure we always make progress based on last known good |
| 18 | // 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^] | 19 | class PayloadState : public PayloadStateInterface { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 20 | public: |
| 21 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame^] | 22 | PayloadState() |
| 23 | : prefs_(NULL), |
| 24 | payload_attempt_number_(0), |
| 25 | num_urls_(0), |
| 26 | url_index_(0), |
| 27 | url_failure_count_(0), |
| 28 | max_failure_count_per_url_(0) {} |
| 29 | |
| 30 | virtual ~PayloadState() {} |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 31 | |
| 32 | // Initializes a payload state object using |prefs| for storing the |
| 33 | // persisted state. It also performs the initial loading of all persisted |
| 34 | // state into memory and dumps the initial state for debugging purposes. |
| 35 | // Note: the other methods should be called only after calling Initialize |
| 36 | // on this object. |
| 37 | bool Initialize(PrefsInterface* prefs); |
| 38 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 39 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame^] | 40 | // Implementation of PayloadStateInterface methods. |
| 41 | virtual void SetResponse(const OmahaResponse& response); |
| 42 | virtual void DownloadComplete(); |
| 43 | virtual void DownloadProgress(size_t count); |
| 44 | virtual void UpdateFailed(ActionExitCode error); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 45 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame^] | 46 | virtual inline std::string GetResponse() { |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 47 | return response_; |
| 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 | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 62 | private: |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame^] | 63 | // Logs the current payload state. |
| 64 | void LogPayloadState(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 65 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame^] | 66 | // Increments the payload attempt number which governs the back-off behavior |
| 67 | // at the time of the next update check. |
| 68 | void IncrementPayloadAttemptNumber(); |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 69 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame^] | 70 | // Advances the current URL index to the next available one. If all URLs have |
| 71 | // been exhausted during the current payload download attempt (as indicated |
| 72 | // by the payload attempt number), then it will increment the payload attempt |
| 73 | // number and wrap around again with the first URL in the list. |
| 74 | void IncrementUrlIndex(); |
| 75 | |
| 76 | // Increments the failure count of the current URL. If the configured max |
| 77 | // failure count is reached for this URL, it advances the current URL index |
| 78 | // to the next URL and resets the failure count for that URL. |
| 79 | void IncrementFailureCount(); |
| 80 | |
| 81 | // Initializes the current response from the persisted state. |
| 82 | void LoadResponse(); |
| 83 | |
| 84 | // Initializes the payload attempt number from the persisted state. |
| 85 | void LoadPayloadAttemptNumber(); |
| 86 | |
| 87 | // Sets the payload attempt number to the given value. Also persists the |
| 88 | // value being set so that we resume from the same value in case of a process |
| 89 | // restart. |
| 90 | void SetPayloadAttemptNumber(uint32_t payload_attempt_number); |
| 91 | |
| 92 | // Initializes the current URL index from the persisted state. |
| 93 | void LoadUrlIndex(); |
| 94 | |
| 95 | // Sets the current URL index to the given value. Also persists the value |
| 96 | // being set so that we resume from the same value in case of a process |
| 97 | // restart. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 98 | void SetUrlIndex(uint32_t url_index); |
| 99 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame^] | 100 | // Initializes the current URL's failure count from the persisted stae. |
| 101 | void LoadUrlFailureCount(); |
| 102 | |
| 103 | // Sets the current URL's failure count to the given value. Also persists the |
| 104 | // value being set so that we resume from the same value in case of a process |
| 105 | // restart. |
| 106 | void SetUrlFailureCount(uint32_t url_failure_count); |
| 107 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 108 | // Interface object with which we read/write persisted state. This must |
| 109 | // be set by calling the Initialize method before calling any other method. |
| 110 | PrefsInterface* prefs_; |
| 111 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame^] | 112 | // This stores a subset of the current response from Omaha. Each update to |
| 113 | // this value is persisted so we resume from the same value in case of a |
| 114 | // process restart. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 115 | std::string response_; |
| 116 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame^] | 117 | // The number of times we've tried to download the payload in full. This is |
| 118 | // incremented each time we download the payload in full successsfully or |
| 119 | // when we exhaust all failure limits for all URLs and are about to wrap |
| 120 | // around back to the first URL. Each update to this value is persisted so |
| 121 | // we resume from the same value in case of a process restart. |
| 122 | uint32_t payload_attempt_number_; |
| 123 | |
| 124 | // The number of urls in the current response. This value is not persisted, |
| 125 | // as we will always get a response from Omaha before we need this value. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 126 | uint32_t num_urls_; |
| 127 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame^] | 128 | // The index of the current URL. This type is different from the one in the |
| 129 | // accessor methods because PrefsInterface supports only int64_t but we want |
| 130 | // to provide a stronger abstraction of uint32_t. Each update to this value |
| 131 | // is persisted so we resume from the same value in case of a process |
| 132 | // restart. |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 133 | int64_t url_index_; |
| 134 | |
Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame^] | 135 | // The count of failures encountered in the current attempt to download using |
| 136 | // the current URL (specified by url_index_). Each update to this value is |
| 137 | // persisted so we resume from the same value in case of a process restart. |
| 138 | int64_t url_failure_count_; |
| 139 | |
| 140 | // The max failure count per url configured in the current response. This |
| 141 | // value is not persisted, as we will always get a response from Omaha before |
| 142 | // we need this value. |
| 143 | uint32_t max_failure_count_per_url_; |
| 144 | |
Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 145 | DISALLOW_COPY_AND_ASSIGN(PayloadState); |
| 146 | }; |
| 147 | |
| 148 | } // namespace chromeos_update_engine |
| 149 | |
| 150 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_PAYLOAD_STATE_H__ |