| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 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 Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 16 |  | 
| Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_PAYLOAD_STATE_H_ | 
|  | 18 | #define UPDATE_ENGINE_PAYLOAD_STATE_H_ | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 19 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 20 | #include <string> | 
|  | 21 | #include <vector> | 
|  | 22 |  | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 23 | #include <base/time/time.h> | 
| Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 24 | #include <gtest/gtest_prod.h>  // for FRIEND_TEST | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 25 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 26 | #include "update_engine/common/prefs_interface.h" | 
| David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 27 | #include "update_engine/metrics.h" | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 28 | #include "update_engine/payload_state_interface.h" | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 29 |  | 
|  | 30 | namespace chromeos_update_engine { | 
|  | 31 |  | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 32 | class SystemState; | 
|  | 33 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 34 | // Encapsulates all the payload state required for download. This includes the | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 35 | // state necessary for handling multiple URLs in Omaha response, the backoff | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 36 | // state, etc. All state is persisted so that we use the most recently saved | 
|  | 37 | // value when resuming the update_engine process. All state is also cached in | 
|  | 38 | // memory so that we ensure we always make progress based on last known good | 
|  | 39 | // 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] | 40 | class PayloadState : public PayloadStateInterface { | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 41 | public: | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 42 | PayloadState(); | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 43 | ~PayloadState() override {} | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 44 |  | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 45 | // Initializes a payload state object using the given global system state. | 
|  | 46 | // It performs the initial loading of all persisted state into memory and | 
|  | 47 | // dumps the initial state for debugging purposes.  Note: the other methods | 
|  | 48 | // should be called only after calling Initialize on this object. | 
|  | 49 | bool Initialize(SystemState* system_state); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 50 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 51 | // Implementation of PayloadStateInterface methods. | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 52 | void SetResponse(const OmahaResponse& response) override; | 
|  | 53 | void DownloadComplete() override; | 
|  | 54 | void DownloadProgress(size_t count) override; | 
|  | 55 | void UpdateResumed() override; | 
|  | 56 | void UpdateRestarted() override; | 
|  | 57 | void UpdateSucceeded() override; | 
|  | 58 | void UpdateFailed(ErrorCode error) override; | 
|  | 59 | void ResetUpdateStatus() override; | 
|  | 60 | bool ShouldBackoffDownload() override; | 
|  | 61 | void Rollback() override; | 
|  | 62 | void ExpectRebootInNewVersion(const std::string& target_version_uid) override; | 
|  | 63 | void SetUsingP2PForDownloading(bool value) override; | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 64 |  | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 65 | void SetUsingP2PForSharing(bool value) override { | 
|  | 66 | using_p2p_for_sharing_ = value; | 
|  | 67 | } | 
|  | 68 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 69 | inline std::string GetResponseSignature() override { | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 70 | return response_signature_; | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 71 | } | 
|  | 72 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 73 | inline int GetFullPayloadAttemptNumber() override { | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 74 | return full_payload_attempt_number_; | 
|  | 75 | } | 
|  | 76 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 77 | inline int GetPayloadAttemptNumber() override { | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 78 | return payload_attempt_number_; | 
|  | 79 | } | 
|  | 80 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 81 | inline std::string GetCurrentUrl() override { | 
| Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 82 | return candidate_urls_.size() ? candidate_urls_[url_index_] : ""; | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 85 | inline uint32_t GetUrlFailureCount() override { | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 86 | return url_failure_count_; | 
|  | 87 | } | 
|  | 88 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 89 | inline uint32_t GetUrlSwitchCount() override { | 
| David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 90 | return url_switch_count_; | 
|  | 91 | } | 
|  | 92 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 93 | inline int GetNumResponsesSeen() override { | 
| David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 94 | return num_responses_seen_; | 
|  | 95 | } | 
|  | 96 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 97 | inline base::Time GetBackoffExpiryTime() override { | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 98 | return backoff_expiry_time_; | 
|  | 99 | } | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 100 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 101 | base::TimeDelta GetUpdateDuration() override; | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 102 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 103 | base::TimeDelta GetUpdateDurationUptime() override; | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 104 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 105 | inline uint64_t GetCurrentBytesDownloaded(DownloadSource source) override { | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 106 | return source < kNumDownloadSources ? current_bytes_downloaded_[source] : 0; | 
|  | 107 | } | 
|  | 108 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 109 | inline uint64_t GetTotalBytesDownloaded(DownloadSource source) override { | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 110 | return source < kNumDownloadSources ? total_bytes_downloaded_[source] : 0; | 
|  | 111 | } | 
|  | 112 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 113 | inline uint32_t GetNumReboots() override { | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 114 | return num_reboots_; | 
|  | 115 | } | 
|  | 116 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 117 | void UpdateEngineStarted() override; | 
| David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 118 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 119 | inline std::string GetRollbackVersion() override { | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 120 | return rollback_version_; | 
|  | 121 | } | 
|  | 122 |  | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 123 | int GetP2PNumAttempts() override; | 
|  | 124 | base::Time GetP2PFirstAttemptTimestamp() override; | 
|  | 125 | void P2PNewAttempt() override; | 
|  | 126 | bool P2PAttemptAllowed() override; | 
| David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 127 |  | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 128 | bool GetUsingP2PForDownloading() const override { | 
| David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 129 | return using_p2p_for_downloading_; | 
|  | 130 | } | 
|  | 131 |  | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 132 | bool GetUsingP2PForSharing() const override { | 
|  | 133 | return using_p2p_for_sharing_; | 
|  | 134 | } | 
|  | 135 |  | 
| Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 136 | base::TimeDelta GetScatteringWaitPeriod() override { | 
|  | 137 | return scattering_wait_period_; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | void SetScatteringWaitPeriod(base::TimeDelta wait_period) override; | 
|  | 141 |  | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 142 | void SetP2PUrl(const std::string& url) override { | 
|  | 143 | p2p_url_ = url; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | std::string GetP2PUrl() const override { | 
|  | 147 | return p2p_url_; | 
|  | 148 | } | 
|  | 149 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 150 | private: | 
| David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 151 | enum class AttemptType { | 
|  | 152 | kUpdate, | 
|  | 153 | kRollback, | 
|  | 154 | }; | 
|  | 155 |  | 
| Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 156 | friend class PayloadStateTest; | 
|  | 157 | FRIEND_TEST(PayloadStateTest, RebootAfterUpdateFailedMetric); | 
|  | 158 | FRIEND_TEST(PayloadStateTest, RebootAfterUpdateSucceed); | 
|  | 159 | FRIEND_TEST(PayloadStateTest, RebootAfterCanceledUpdate); | 
| Chris Sosa | b3dcdb3 | 2013-09-04 15:22:12 -0700 | [diff] [blame] | 160 | FRIEND_TEST(PayloadStateTest, RollbackVersion); | 
| Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 161 | FRIEND_TEST(PayloadStateTest, UpdateSuccessWithWipedPrefs); | 
|  | 162 |  | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 163 | // Helper called when an attempt has begun, is called by | 
| David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 164 | // UpdateResumed(), UpdateRestarted() and Rollback(). | 
|  | 165 | void AttemptStarted(AttemptType attempt_type); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 166 |  | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 167 | // Increments the payload attempt number used for metrics. | 
|  | 168 | void IncrementPayloadAttemptNumber(); | 
|  | 169 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 170 | // Increments the payload attempt number which governs the backoff behavior | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 171 | // at the time of the next update check. | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 172 | void IncrementFullPayloadAttemptNumber(); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 173 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 174 | // Advances the current URL index to the next available one. If all URLs have | 
|  | 175 | // been exhausted during the current payload download attempt (as indicated | 
|  | 176 | // by the payload attempt number), then it will increment the payload attempt | 
| David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 177 | // number and wrap around again with the first URL in the list. This also | 
|  | 178 | // updates the URL switch count, if needed. | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 179 | void IncrementUrlIndex(); | 
|  | 180 |  | 
|  | 181 | // Increments the failure count of the current URL. If the configured max | 
|  | 182 | // failure count is reached for this URL, it advances the current URL index | 
|  | 183 | // to the next URL and resets the failure count for that URL. | 
|  | 184 | void IncrementFailureCount(); | 
|  | 185 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 186 | // Updates the backoff expiry time exponentially based on the current | 
|  | 187 | // payload attempt number. | 
|  | 188 | void UpdateBackoffExpiryTime(); | 
|  | 189 |  | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 190 | // Updates the value of current download source based on the current URL | 
|  | 191 | // index. If the download source is not one of the known sources, it's set | 
|  | 192 | // to kNumDownloadSources. | 
|  | 193 | void UpdateCurrentDownloadSource(); | 
|  | 194 |  | 
|  | 195 | // Updates the various metrics corresponding with the given number of bytes | 
|  | 196 | // that were downloaded recently. | 
|  | 197 | void UpdateBytesDownloaded(size_t count); | 
|  | 198 |  | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 199 | // Calculates the PayloadType we're using. | 
|  | 200 | PayloadType CalculatePayloadType(); | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 201 |  | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 202 | // Collects and reports the various metrics related to an update attempt. | 
|  | 203 | void CollectAndReportAttemptMetrics(ErrorCode code); | 
| David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 204 |  | 
| David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 205 | // Persists values related to the UpdateEngine.Attempt.* metrics so | 
|  | 206 | // we can identify later if an update attempt ends abnormally. | 
|  | 207 | void PersistAttemptMetrics(); | 
|  | 208 |  | 
|  | 209 | // Clears persistent state previously set using AttemptMetricsPersist(). | 
|  | 210 | void ClearPersistedAttemptMetrics(); | 
|  | 211 |  | 
|  | 212 | // Checks if persistent state previously set using AttemptMetricsPersist() | 
|  | 213 | // exists and, if so, emits it with |attempt_result| set to | 
|  | 214 | // metrics::AttemptResult::kAbnormalTermination. | 
|  | 215 | void ReportAndClearPersistedAttemptMetrics(); | 
|  | 216 |  | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 217 | // Collects and reports the various metrics related to a successful update. | 
|  | 218 | void CollectAndReportSuccessfulUpdateMetrics(); | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 219 |  | 
| Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 220 | // Checks if we were expecting to be running in the new version but the | 
|  | 221 | // boot into the new version failed for some reason. If that's the case, an | 
|  | 222 | // UMA metric is sent reporting the number of attempts the same applied | 
|  | 223 | // payload was attempted to reboot. This function is called by UpdateAttempter | 
|  | 224 | // every time the update engine starts and there's no reboot pending. | 
|  | 225 | void ReportFailedBootIfNeeded(); | 
|  | 226 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 227 | // Resets all the persisted state values which are maintained relative to the | 
|  | 228 | // current response signature. The response signature itself is not reset. | 
|  | 229 | void ResetPersistedState(); | 
|  | 230 |  | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 231 | // Resets the appropriate state related to download sources that need to be | 
|  | 232 | // reset on a new update. | 
|  | 233 | void ResetDownloadSourcesOnNewUpdate(); | 
|  | 234 |  | 
| Chris Sosa | b3dcdb3 | 2013-09-04 15:22:12 -0700 | [diff] [blame] | 235 | // Returns the persisted value from prefs_ for the given key. It also | 
|  | 236 | // validates that the value returned is non-negative. | 
|  | 237 | int64_t GetPersistedValue(const std::string& key); | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 238 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 239 | // Calculates the response "signature", which is basically a string composed | 
|  | 240 | // of the subset of the fields in the current response that affect the | 
|  | 241 | // behavior of the PayloadState. | 
|  | 242 | std::string CalculateResponseSignature(); | 
|  | 243 |  | 
|  | 244 | // Initializes the current response signature from the persisted state. | 
|  | 245 | void LoadResponseSignature(); | 
|  | 246 |  | 
|  | 247 | // Sets the response signature to the given value. Also persists the value | 
|  | 248 | // being set so that we resume from the save value in case of a process | 
|  | 249 | // restart. | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 250 | void SetResponseSignature(const std::string& response_signature); | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 251 |  | 
|  | 252 | // Initializes the payload attempt number from the persisted state. | 
|  | 253 | void LoadPayloadAttemptNumber(); | 
|  | 254 |  | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 255 | // Initializes the payload attempt number for full payloads from the persisted | 
|  | 256 | // state. | 
|  | 257 | void LoadFullPayloadAttemptNumber(); | 
|  | 258 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 259 | // Sets the payload attempt number to the given value. Also persists the | 
|  | 260 | // value being set so that we resume from the same value in case of a process | 
|  | 261 | // restart. | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 262 | void SetPayloadAttemptNumber(int payload_attempt_number); | 
|  | 263 |  | 
|  | 264 | // Sets the payload attempt number for full updates to the given value. Also | 
|  | 265 | // persists the value being set so that we resume from the same value in case | 
|  | 266 | // of a process restart. | 
|  | 267 | void SetFullPayloadAttemptNumber(int payload_attempt_number); | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 268 |  | 
|  | 269 | // 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 Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 275 | void SetUrlIndex(uint32_t url_index); | 
|  | 276 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 277 | // 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 Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 285 | // 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 Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 291 | // 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 Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 299 | // 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 Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 329 | // 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 | 
|  | 334 | // reset to 0 everytime we begin a full update and is continued from previous | 
|  | 335 | // 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 | 
|  | 346 | // reset to 0 everytime we successfully apply an update and counts the bytes | 
|  | 347 | // 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 |  | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 356 | // Loads the blacklisted version from our prefs file. | 
|  | 357 | void LoadRollbackVersion(); | 
|  | 358 |  | 
|  | 359 | // Blacklists this version from getting AU'd to until we receive a new update | 
|  | 360 | // response. | 
|  | 361 | void SetRollbackVersion(const std::string& rollback_version); | 
|  | 362 |  | 
|  | 363 | // Clears any blacklisted version. | 
|  | 364 | void ResetRollbackVersion(); | 
|  | 365 |  | 
| Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 366 | inline uint32_t GetUrlIndex() { | 
|  | 367 | return url_index_; | 
|  | 368 | } | 
|  | 369 |  | 
|  | 370 | // Computes the list of candidate URLs from the total list of payload URLs in | 
|  | 371 | // the Omaha response. | 
|  | 372 | void ComputeCandidateUrls(); | 
|  | 373 |  | 
| David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 374 | // Sets |num_responses_seen_| and persist it to disk. | 
|  | 375 | void SetNumResponsesSeen(int num_responses_seen); | 
|  | 376 |  | 
|  | 377 | // Initializes |num_responses_seen_| from persisted state. | 
|  | 378 | void LoadNumResponsesSeen(); | 
|  | 379 |  | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 380 | // Initializes |num_reboots_| from the persisted state. | 
|  | 381 | void LoadNumReboots(); | 
|  | 382 |  | 
|  | 383 | // Sets |num_reboots| for the update attempt. Also persists the | 
|  | 384 | // value being set so that we resume from the same value in case of a process | 
|  | 385 | // restart. | 
|  | 386 | void SetNumReboots(uint32_t num_reboots); | 
|  | 387 |  | 
|  | 388 | // Checks to see if the device rebooted since the last call and if so | 
|  | 389 | // increments num_reboots. | 
|  | 390 | void UpdateNumReboots(); | 
|  | 391 |  | 
| David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 392 | // Writes the current wall-clock time to the kPrefsSystemUpdatedMarker | 
|  | 393 | // state variable. | 
|  | 394 | void CreateSystemUpdatedMarkerFile(); | 
|  | 395 |  | 
|  | 396 | // Called at program startup if the device booted into a new update. | 
|  | 397 | // The |time_to_reboot| parameter contains the (wall-clock) duration | 
|  | 398 | // from when the update successfully completed (the value written | 
|  | 399 | // into the kPrefsSystemUpdatedMarker state variable) until the device | 
|  | 400 | // was booted into the update (current wall-clock time). | 
|  | 401 | void BootedIntoUpdate(base::TimeDelta time_to_reboot); | 
|  | 402 |  | 
| David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 403 | // Loads the |kPrefsP2PFirstAttemptTimestamp| state variable from disk | 
|  | 404 | // into |p2p_first_attempt_timestamp_|. | 
|  | 405 | void LoadP2PFirstAttemptTimestamp(); | 
|  | 406 |  | 
|  | 407 | // Loads the |kPrefsP2PNumAttempts| state variable into |p2p_num_attempts_|. | 
|  | 408 | void LoadP2PNumAttempts(); | 
|  | 409 |  | 
|  | 410 | // Sets the |kPrefsP2PNumAttempts| state variable to |value|. | 
|  | 411 | void SetP2PNumAttempts(int value); | 
|  | 412 |  | 
|  | 413 | // Sets the |kPrefsP2PFirstAttemptTimestamp| state variable to |time|. | 
|  | 414 | void SetP2PFirstAttemptTimestamp(const base::Time& time); | 
|  | 415 |  | 
| Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 416 | // Loads the persisted scattering wallclock-based wait period. | 
|  | 417 | void LoadScatteringWaitPeriod(); | 
|  | 418 |  | 
| Gilad Arnold | 6e15aac | 2014-10-02 10:34:14 -0700 | [diff] [blame] | 419 | // The global state of the system. | 
|  | 420 | SystemState* system_state_; | 
|  | 421 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 422 | // Interface object with which we read/write persisted state. This must | 
|  | 423 | // be set by calling the Initialize method before calling any other method. | 
|  | 424 | PrefsInterface* prefs_; | 
|  | 425 |  | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 426 | // Interface object with which we read/write persisted state. This must | 
|  | 427 | // be set by calling the Initialize method before calling any other method. | 
|  | 428 | // This object persists across powerwashes. | 
|  | 429 | PrefsInterface* powerwash_safe_prefs_; | 
|  | 430 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 431 | // This is the current response object from Omaha. | 
|  | 432 | OmahaResponse response_; | 
|  | 433 |  | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 434 | // Whether P2P is being used for downloading and sharing. | 
| David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 435 | bool using_p2p_for_downloading_; | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 436 | bool using_p2p_for_sharing_; | 
|  | 437 |  | 
|  | 438 | // Stores the P2P download URL, if one is used. | 
|  | 439 | std::string p2p_url_; | 
|  | 440 |  | 
|  | 441 | // The cached value of |kPrefsP2PFirstAttemptTimestamp|. | 
|  | 442 | base::Time p2p_first_attempt_timestamp_; | 
|  | 443 |  | 
|  | 444 | // The cached value of |kPrefsP2PNumAttempts|. | 
|  | 445 | int p2p_num_attempts_; | 
| David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 446 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 447 | // This stores a "signature" of the current response. The signature here | 
|  | 448 | // 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] | 449 | // this value is persisted so we resume from the same value in case of a | 
|  | 450 | // process restart. | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 451 | std::string response_signature_; | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 452 |  | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 453 | // The number of times we've tried to download the payload. This is | 
|  | 454 | // incremented each time we download the payload successsfully or when we | 
|  | 455 | // exhaust all failure limits for all URLs and are about to wrap around back | 
|  | 456 | // to the first URL.  Each update to this value is persisted so we resume from | 
|  | 457 | // the same value in case of a process restart. | 
|  | 458 | int payload_attempt_number_; | 
|  | 459 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 460 | // The number of times we've tried to download the payload in full. This is | 
|  | 461 | // incremented each time we download the payload in full successsfully or | 
|  | 462 | // when we exhaust all failure limits for all URLs and are about to wrap | 
|  | 463 | // around back to the first URL.  Each update to this value is persisted so | 
|  | 464 | // we resume from the same value in case of a process restart. | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 465 | int full_payload_attempt_number_; | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 466 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 467 | // The index of the current URL.  This type is different from the one in the | 
|  | 468 | // accessor methods because PrefsInterface supports only int64_t but we want | 
|  | 469 | // to provide a stronger abstraction of uint32_t.  Each update to this value | 
|  | 470 | // is persisted so we resume from the same value in case of a process | 
|  | 471 | // restart. | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 472 | int64_t url_index_; | 
|  | 473 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 474 | // The count of failures encountered in the current attempt to download using | 
|  | 475 | // the current URL (specified by url_index_).  Each update to this value is | 
|  | 476 | // persisted so we resume from the same value in case of a process restart. | 
|  | 477 | int64_t url_failure_count_; | 
|  | 478 |  | 
| David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 479 | // The number of times we've switched URLs. | 
|  | 480 | int32_t url_switch_count_; | 
|  | 481 |  | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 482 | // The current download source based on the current URL. This value is | 
|  | 483 | // not persisted as it can be recomputed everytime we update the URL. | 
|  | 484 | // We're storing this so as not to recompute this on every few bytes of | 
|  | 485 | // data we read from the socket. | 
|  | 486 | DownloadSource current_download_source_; | 
|  | 487 |  | 
| David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 488 | // The number of different Omaha responses seen. Increases every time | 
|  | 489 | // a new response is seen. Resets to 0 only when the system has been | 
|  | 490 | // successfully updated. | 
|  | 491 | int num_responses_seen_; | 
|  | 492 |  | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 493 | // The number of system reboots during an update attempt. Technically since | 
|  | 494 | // we don't go out of our way to not update it when not attempting an update, | 
|  | 495 | // also records the number of reboots before the next update attempt starts. | 
|  | 496 | uint32_t num_reboots_; | 
|  | 497 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 498 | // The timestamp until which we've to wait before attempting to download the | 
|  | 499 | // payload again, so as to backoff repeated downloads. | 
|  | 500 | base::Time backoff_expiry_time_; | 
|  | 501 |  | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 502 | // The most recently calculated value of the update duration. | 
|  | 503 | base::TimeDelta update_duration_current_; | 
|  | 504 |  | 
|  | 505 | // The point in time (wall-clock) that the update was started. | 
|  | 506 | base::Time update_timestamp_start_; | 
|  | 507 |  | 
|  | 508 | // The point in time (wall-clock) that the update ended. If the update | 
|  | 509 | // is still in progress, this is set to the Epoch (e.g. 0). | 
|  | 510 | base::Time update_timestamp_end_; | 
|  | 511 |  | 
|  | 512 | // The update duration uptime | 
|  | 513 | base::TimeDelta update_duration_uptime_; | 
|  | 514 |  | 
|  | 515 | // The monotonic time when |update_duration_uptime_| was last set | 
|  | 516 | base::Time update_duration_uptime_timestamp_; | 
|  | 517 |  | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 518 | // The number of bytes that have been downloaded for each source for each new | 
|  | 519 | // update attempt. If we resume an update, we'll continue from the previous | 
|  | 520 | // value, but if we get a new response or if the previous attempt failed, | 
|  | 521 | // we'll reset this to 0 to start afresh. Each update to this value is | 
|  | 522 | // persisted so we resume from the same value in case of a process restart. | 
|  | 523 | // The extra index in the array is to no-op accidental access in case the | 
|  | 524 | // return value from GetCurrentDownloadSource is used without validation. | 
|  | 525 | uint64_t current_bytes_downloaded_[kNumDownloadSources + 1]; | 
|  | 526 |  | 
|  | 527 | // The number of bytes that have been downloaded for each source since the | 
|  | 528 | // the last successful update. This is used to compute the overhead we incur. | 
|  | 529 | // Each update to this value is persisted so we resume from the same value in | 
|  | 530 | // case of a process restart. | 
|  | 531 | // The extra index in the array is to no-op accidental access in case the | 
|  | 532 | // return value from GetCurrentDownloadSource is used without validation. | 
|  | 533 | uint64_t total_bytes_downloaded_[kNumDownloadSources + 1]; | 
|  | 534 |  | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 535 | // A small timespan used when comparing wall-clock times for coping | 
|  | 536 | // with the fact that clocks drift and consequently are adjusted | 
|  | 537 | // (either forwards or backwards) via NTP. | 
|  | 538 | static const base::TimeDelta kDurationSlack; | 
|  | 539 |  | 
| Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 540 | // The ordered list of the subset of payload URL candidates which are | 
|  | 541 | // allowed as per device policy. | 
|  | 542 | std::vector<std::string> candidate_urls_; | 
|  | 543 |  | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 544 | // This stores a blacklisted version set as part of rollback. When we rollback | 
|  | 545 | // we store the version of the os from which we are rolling back from in order | 
|  | 546 | // to guarantee that we do not re-update to it on the next au attempt after | 
|  | 547 | // reboot. | 
|  | 548 | std::string rollback_version_; | 
|  | 549 |  | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 550 | // The number of bytes downloaded per attempt. | 
|  | 551 | int64_t attempt_num_bytes_downloaded_; | 
|  | 552 |  | 
|  | 553 | // The boot time when the attempt was started. | 
|  | 554 | base::Time attempt_start_time_boot_; | 
|  | 555 |  | 
|  | 556 | // The monotonic time when the attempt was started. | 
|  | 557 | base::Time attempt_start_time_monotonic_; | 
|  | 558 |  | 
| David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 559 | // The connection type when the attempt started. | 
|  | 560 | metrics::ConnectionType attempt_connection_type_; | 
|  | 561 |  | 
| David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 562 | // Whether we're currently rolling back. | 
|  | 563 | AttemptType attempt_type_; | 
|  | 564 |  | 
| Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 565 | // The current scattering wallclock-based wait period. | 
|  | 566 | base::TimeDelta scattering_wait_period_; | 
|  | 567 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 568 | DISALLOW_COPY_AND_ASSIGN(PayloadState); | 
|  | 569 | }; | 
|  | 570 |  | 
|  | 571 | }  // namespace chromeos_update_engine | 
|  | 572 |  | 
| Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 573 | #endif  // UPDATE_ENGINE_PAYLOAD_STATE_H_ |