| 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 |  | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #include "update_engine/cros/payload_state.h" | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 18 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 19 | #include <algorithm> | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 20 | #include <string> | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 21 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 22 | #include <base/logging.h> | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 23 | #include <base/strings/string_util.h> | 
|  | 24 | #include <base/strings/stringprintf.h> | 
| Alex Deymo | a259179 | 2015-11-17 00:39:40 -0300 | [diff] [blame] | 25 | #include <metrics/metrics_library.h> | 
| Gilad Arnold | 1f84723 | 2014-04-07 12:07:49 -0700 | [diff] [blame] | 26 | #include <policy/device_policy.h> | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 27 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 28 | #include "update_engine/common/clock.h" | 
|  | 29 | #include "update_engine/common/constants.h" | 
| Alex Deymo | e88e9fe | 2016-02-03 16:38:00 -0800 | [diff] [blame] | 30 | #include "update_engine/common/error_code_utils.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 31 | #include "update_engine/common/hardware_interface.h" | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 32 | #include "update_engine/common/metrics_reporter_interface.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 33 | #include "update_engine/common/prefs.h" | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 34 | #include "update_engine/common/system_state.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 35 | #include "update_engine/common/utils.h" | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 36 | #include "update_engine/cros/connection_manager_interface.h" | 
|  | 37 | #include "update_engine/cros/omaha_request_params.h" | 
|  | 38 | #include "update_engine/cros/update_attempter.h" | 
| Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 39 | #include "update_engine/metrics_utils.h" | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 40 | #include "update_engine/payload_consumer/install_plan.h" | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 41 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 42 | using base::Time; | 
|  | 43 | using base::TimeDelta; | 
|  | 44 | using std::min; | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 45 | using std::string; | 
|  | 46 |  | 
|  | 47 | namespace chromeos_update_engine { | 
|  | 48 |  | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 49 | using metrics_utils::GetPersistedValue; | 
|  | 50 |  | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 51 | const TimeDelta PayloadState::kDurationSlack = TimeDelta::FromSeconds(600); | 
|  | 52 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 53 | // We want to upperbound backoffs to 16 days | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 54 | static const int kMaxBackoffDays = 16; | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 55 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 56 | // We want to randomize retry attempts after the backoff by +/- 6 hours. | 
|  | 57 | static const uint32_t kMaxBackoffFuzzMinutes = 12 * 60; | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 58 |  | 
| Colin Howes | 0e452c9 | 2018-11-02 13:18:44 -0700 | [diff] [blame] | 59 | // Limit persisting current update duration uptime to once per second | 
|  | 60 | static const uint64_t kUptimeResolution = 1; | 
|  | 61 |  | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 62 | PayloadState::PayloadState() | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 63 | : prefs_(nullptr), | 
| Jae Hoon Kim | 5e8e30b | 2020-05-06 14:59:06 -0700 | [diff] [blame] | 64 | powerwash_safe_prefs_(nullptr), | 
|  | 65 | excluder_(nullptr), | 
| David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 66 | using_p2p_for_downloading_(false), | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 67 | p2p_num_attempts_(0), | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 68 | payload_attempt_number_(0), | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 69 | full_payload_attempt_number_(0), | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 70 | url_index_(0), | 
| David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 71 | url_failure_count_(0), | 
| David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 72 | url_switch_count_(0), | 
| Marton Hunyady | e58bddb | 2018-04-10 20:27:26 +0200 | [diff] [blame] | 73 | rollback_happened_(false), | 
| David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 74 | attempt_num_bytes_downloaded_(0), | 
|  | 75 | attempt_connection_type_(metrics::ConnectionType::kUnknown), | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 76 | attempt_type_(AttemptType::kUpdate) { | 
|  | 77 | for (int i = 0; i <= kNumDownloadSources; i++) | 
|  | 78 | total_bytes_downloaded_[i] = current_bytes_downloaded_[i] = 0; | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 79 | } | 
|  | 80 |  | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 81 | bool PayloadState::Initialize() { | 
|  | 82 | prefs_ = SystemState::Get()->prefs(); | 
|  | 83 | powerwash_safe_prefs_ = SystemState::Get()->powerwash_safe_prefs(); | 
|  | 84 | excluder_ = SystemState::Get()->update_attempter()->GetExcluder(); | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 85 | LoadResponseSignature(); | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 86 | LoadPayloadAttemptNumber(); | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 87 | LoadFullPayloadAttemptNumber(); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 88 | LoadUrlIndex(); | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 89 | LoadUrlFailureCount(); | 
| David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 90 | LoadUrlSwitchCount(); | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 91 | LoadBackoffExpiryTime(); | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 92 | LoadUpdateTimestampStart(); | 
|  | 93 | // The LoadUpdateDurationUptime() method relies on LoadUpdateTimestampStart() | 
|  | 94 | // being called before it. Don't reorder. | 
|  | 95 | LoadUpdateDurationUptime(); | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 96 | for (int i = 0; i < kNumDownloadSources; i++) { | 
|  | 97 | DownloadSource source = static_cast<DownloadSource>(i); | 
|  | 98 | LoadCurrentBytesDownloaded(source); | 
|  | 99 | LoadTotalBytesDownloaded(source); | 
|  | 100 | } | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 101 | LoadNumReboots(); | 
| David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 102 | LoadNumResponsesSeen(); | 
| Marton Hunyady | e58bddb | 2018-04-10 20:27:26 +0200 | [diff] [blame] | 103 | LoadRollbackHappened(); | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 104 | LoadRollbackVersion(); | 
| David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 105 | LoadP2PFirstAttemptTimestamp(); | 
|  | 106 | LoadP2PNumAttempts(); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 107 | return true; | 
|  | 108 | } | 
|  | 109 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 110 | void PayloadState::SetResponse(const OmahaResponse& omaha_response) { | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 111 | // Always store the latest response. | 
|  | 112 | response_ = omaha_response; | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 113 |  | 
| Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 114 | // Compute the candidate URLs first as they are used to calculate the | 
|  | 115 | // response signature so that a change in enterprise policy for | 
|  | 116 | // HTTP downloads being enabled or not could be honored as soon as the | 
|  | 117 | // next update check happens. | 
|  | 118 | ComputeCandidateUrls(); | 
|  | 119 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 120 | // Check if the "signature" of this response (i.e. the fields we care about) | 
|  | 121 | // has changed. | 
|  | 122 | string new_response_signature = CalculateResponseSignature(); | 
|  | 123 | bool has_response_changed = (response_signature_ != new_response_signature); | 
|  | 124 |  | 
|  | 125 | // If the response has changed, we should persist the new signature and | 
|  | 126 | // clear away all the existing state. | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 127 | if (has_response_changed) { | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 128 | LOG(INFO) << "Resetting all persisted state as this is a new response"; | 
| David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 129 | SetNumResponsesSeen(num_responses_seen_ + 1); | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 130 | SetResponseSignature(new_response_signature); | 
|  | 131 | ResetPersistedState(); | 
|  | 132 | return; | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 133 | } | 
|  | 134 |  | 
| Sen Jiang | 97eba34 | 2017-05-22 14:34:11 -0700 | [diff] [blame] | 135 | // Always start from payload index 0, even for resume, to download partition | 
|  | 136 | // info from previous payloads. | 
|  | 137 | payload_index_ = 0; | 
|  | 138 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 139 | // This is the earliest point at which we can validate whether the URL index | 
|  | 140 | // we loaded from the persisted state is a valid value. If the response | 
|  | 141 | // hasn't changed but the URL index is invalid, it's indicative of some | 
|  | 142 | // tampering of the persisted state. | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 143 | if (payload_index_ >= candidate_urls_.size() || | 
|  | 144 | url_index_ >= candidate_urls_[payload_index_].size()) { | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 145 | LOG(INFO) << "Resetting all payload state as the url index seems to have " | 
|  | 146 | "been tampered with"; | 
|  | 147 | ResetPersistedState(); | 
|  | 148 | return; | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 149 | } | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 150 |  | 
|  | 151 | // Update the current download source which depends on the latest value of | 
|  | 152 | // the response. | 
|  | 153 | UpdateCurrentDownloadSource(); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
| David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 156 | void PayloadState::SetUsingP2PForDownloading(bool value) { | 
|  | 157 | using_p2p_for_downloading_ = value; | 
|  | 158 | // Update the current download source which depends on whether we are | 
|  | 159 | // using p2p or not. | 
|  | 160 | UpdateCurrentDownloadSource(); | 
|  | 161 | } | 
|  | 162 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 163 | void PayloadState::DownloadComplete() { | 
|  | 164 | LOG(INFO) << "Payload downloaded successfully"; | 
|  | 165 | IncrementPayloadAttemptNumber(); | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 166 | IncrementFullPayloadAttemptNumber(); | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 167 | } | 
|  | 168 |  | 
|  | 169 | void PayloadState::DownloadProgress(size_t count) { | 
|  | 170 | if (count == 0) | 
|  | 171 | return; | 
|  | 172 |  | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 173 | CalculateUpdateDurationUptime(); | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 174 | UpdateBytesDownloaded(count); | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 175 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 176 | // We've received non-zero bytes from a recent download operation.  Since our | 
|  | 177 | // URL failure count is meant to penalize a URL only for consecutive | 
|  | 178 | // failures, downloading bytes successfully means we should reset the failure | 
|  | 179 | // count (as we know at least that the URL is working). In future, we can | 
|  | 180 | // design this to be more sophisticated to check for more intelligent failure | 
|  | 181 | // patterns, but right now, even 1 byte downloaded will mark the URL to be | 
|  | 182 | // good unless it hits 10 (or configured number of) consecutive failures | 
|  | 183 | // again. | 
|  | 184 |  | 
|  | 185 | if (GetUrlFailureCount() == 0) | 
|  | 186 | return; | 
|  | 187 |  | 
|  | 188 | LOG(INFO) << "Resetting failure count of Url" << GetUrlIndex() | 
|  | 189 | << " to 0 as we received " << count << " bytes successfully"; | 
|  | 190 | SetUrlFailureCount(0); | 
|  | 191 | } | 
|  | 192 |  | 
| David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 193 | void PayloadState::AttemptStarted(AttemptType attempt_type) { | 
| David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 194 | // Flush previous state from abnormal attempt failure, if any. | 
|  | 195 | ReportAndClearPersistedAttemptMetrics(); | 
|  | 196 |  | 
| David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 197 | attempt_type_ = attempt_type; | 
|  | 198 |  | 
| Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 199 | const auto* clock = SystemState::Get()->clock(); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 200 | attempt_start_time_boot_ = clock->GetBootTime(); | 
|  | 201 | attempt_start_time_monotonic_ = clock->GetMonotonicTime(); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 202 | attempt_num_bytes_downloaded_ = 0; | 
| David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 203 |  | 
|  | 204 | metrics::ConnectionType type; | 
| Sen Jiang | 255e22b | 2016-05-20 16:15:29 -0700 | [diff] [blame] | 205 | ConnectionType network_connection_type; | 
|  | 206 | ConnectionTethering tethering; | 
| Alex Deymo | f6ee016 | 2015-07-31 12:35:22 -0700 | [diff] [blame] | 207 | ConnectionManagerInterface* connection_manager = | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 208 | SystemState::Get()->connection_manager(); | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 209 | if (!connection_manager->GetConnectionProperties(&network_connection_type, | 
| David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 210 | &tethering)) { | 
|  | 211 | LOG(ERROR) << "Failed to determine connection type."; | 
|  | 212 | type = metrics::ConnectionType::kUnknown; | 
|  | 213 | } else { | 
| Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 214 | type = metrics_utils::GetConnectionType(network_connection_type, tethering); | 
| David Zeuthen | b281f07 | 2014-04-02 10:20:19 -0700 | [diff] [blame] | 215 | } | 
|  | 216 | attempt_connection_type_ = type; | 
| David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 217 |  | 
|  | 218 | if (attempt_type == AttemptType::kUpdate) | 
|  | 219 | PersistAttemptMetrics(); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 220 | } | 
|  | 221 |  | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 222 | void PayloadState::UpdateResumed() { | 
|  | 223 | LOG(INFO) << "Resuming an update that was previously started."; | 
|  | 224 | UpdateNumReboots(); | 
| David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 225 | AttemptStarted(AttemptType::kUpdate); | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 226 | } | 
|  | 227 |  | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 228 | void PayloadState::UpdateRestarted() { | 
|  | 229 | LOG(INFO) << "Starting a new update"; | 
|  | 230 | ResetDownloadSourcesOnNewUpdate(); | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 231 | SetNumReboots(0); | 
| David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 232 | AttemptStarted(AttemptType::kUpdate); | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 233 | } | 
|  | 234 |  | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 235 | void PayloadState::UpdateSucceeded() { | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 236 | // Send the relevant metrics that are tracked in this class to UMA. | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 237 | CalculateUpdateDurationUptime(); | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 238 | SetUpdateTimestampEnd(SystemState::Get()->clock()->GetWallclockTime()); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 239 |  | 
| David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 240 | switch (attempt_type_) { | 
|  | 241 | case AttemptType::kUpdate: | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 242 | CollectAndReportAttemptMetrics(ErrorCode::kSuccess); | 
| David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 243 | CollectAndReportSuccessfulUpdateMetrics(); | 
| David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 244 | ClearPersistedAttemptMetrics(); | 
| David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 245 | break; | 
|  | 246 |  | 
|  | 247 | case AttemptType::kRollback: | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 248 | SystemState::Get()->metrics_reporter()->ReportRollbackMetrics( | 
| Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 249 | metrics::RollbackResult::kSuccess); | 
| David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 250 | break; | 
| David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 251 | } | 
| David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 252 |  | 
|  | 253 | // Reset the number of responses seen since it counts from the last | 
|  | 254 | // successful update, e.g. now. | 
|  | 255 | SetNumResponsesSeen(0); | 
| Sen Jiang | 97eba34 | 2017-05-22 14:34:11 -0700 | [diff] [blame] | 256 | SetPayloadIndex(0); | 
| David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 257 |  | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 258 | metrics_utils::SetSystemUpdatedMarker(SystemState::Get()->clock(), prefs_); | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 259 | } | 
|  | 260 |  | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 261 | void PayloadState::UpdateFailed(ErrorCode error) { | 
|  | 262 | ErrorCode base_error = utils::GetBaseErrorCode(error); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 263 | LOG(INFO) << "Updating payload state for error code: " << base_error << " (" | 
|  | 264 | << utils::ErrorCodeToString(base_error) << ")"; | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 265 |  | 
| Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 266 | if (candidate_urls_.size() == 0) { | 
|  | 267 | // This means we got this error even before we got a valid Omaha response | 
|  | 268 | // or don't have any valid candidates in the Omaha response. | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 269 | // So we should not advance the url_index_ in such cases. | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 270 | LOG(INFO) << "Ignoring failures until we get a valid Omaha response."; | 
|  | 271 | return; | 
|  | 272 | } | 
|  | 273 |  | 
| David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 274 | switch (attempt_type_) { | 
|  | 275 | case AttemptType::kUpdate: | 
|  | 276 | CollectAndReportAttemptMetrics(base_error); | 
| David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 277 | ClearPersistedAttemptMetrics(); | 
| David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 278 | break; | 
|  | 279 |  | 
|  | 280 | case AttemptType::kRollback: | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 281 | SystemState::Get()->metrics_reporter()->ReportRollbackMetrics( | 
| Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 282 | metrics::RollbackResult::kFailed); | 
| David Zeuthen | 96197df | 2014-04-16 12:22:39 -0700 | [diff] [blame] | 283 | break; | 
|  | 284 | } | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 285 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 286 | switch (base_error) { | 
|  | 287 | // Errors which are good indicators of a problem with a particular URL or | 
|  | 288 | // the protocol used in the URL or entities in the communication channel | 
|  | 289 | // (e.g. proxies). We should try the next available URL in the next update | 
|  | 290 | // check to quickly recover from these errors. | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 291 | case ErrorCode::kPayloadHashMismatchError: | 
|  | 292 | case ErrorCode::kPayloadSizeMismatchError: | 
|  | 293 | case ErrorCode::kDownloadPayloadVerificationError: | 
|  | 294 | case ErrorCode::kDownloadPayloadPubKeyVerificationError: | 
|  | 295 | case ErrorCode::kSignedDeltaPayloadExpectedError: | 
|  | 296 | case ErrorCode::kDownloadInvalidMetadataMagicString: | 
|  | 297 | case ErrorCode::kDownloadSignatureMissingInManifest: | 
|  | 298 | case ErrorCode::kDownloadManifestParseError: | 
|  | 299 | case ErrorCode::kDownloadMetadataSignatureError: | 
|  | 300 | case ErrorCode::kDownloadMetadataSignatureVerificationError: | 
|  | 301 | case ErrorCode::kDownloadMetadataSignatureMismatch: | 
|  | 302 | case ErrorCode::kDownloadOperationHashVerificationError: | 
|  | 303 | case ErrorCode::kDownloadOperationExecutionError: | 
|  | 304 | case ErrorCode::kDownloadOperationHashMismatch: | 
|  | 305 | case ErrorCode::kDownloadInvalidMetadataSize: | 
|  | 306 | case ErrorCode::kDownloadInvalidMetadataSignature: | 
|  | 307 | case ErrorCode::kDownloadOperationHashMissingError: | 
|  | 308 | case ErrorCode::kDownloadMetadataSignatureMissingError: | 
|  | 309 | case ErrorCode::kPayloadMismatchedType: | 
|  | 310 | case ErrorCode::kUnsupportedMajorPayloadVersion: | 
|  | 311 | case ErrorCode::kUnsupportedMinorPayloadVersion: | 
| Sen Jiang | 8e768e9 | 2017-06-28 17:13:19 -0700 | [diff] [blame] | 312 | case ErrorCode::kPayloadTimestampError: | 
| Sen Jiang | 57f9180 | 2017-11-14 17:42:13 -0800 | [diff] [blame] | 313 | case ErrorCode::kVerityCalculationError: | 
| Jae Hoon Kim | 694eeb0 | 2020-06-01 14:24:08 -0700 | [diff] [blame] | 314 | ExcludeCurrentPayload(); | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 315 | IncrementUrlIndex(); | 
|  | 316 | break; | 
|  | 317 |  | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 318 | // Errors which seem to be just transient network/communication related | 
|  | 319 | // failures and do not indicate any inherent problem with the URL itself. | 
|  | 320 | // So, we should keep the current URL but just increment the | 
|  | 321 | // failure count to give it more chances. This way, while we maximize our | 
|  | 322 | // chances of downloading from the URLs that appear earlier in the | 
|  | 323 | // response (because download from a local server URL that appears earlier | 
|  | 324 | // in a response is preferable than downloading from the next URL which | 
|  | 325 | // could be a internet URL and thus could be more expensive). | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 326 |  | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 327 | case ErrorCode::kError: | 
|  | 328 | case ErrorCode::kDownloadTransferError: | 
|  | 329 | case ErrorCode::kDownloadWriteError: | 
|  | 330 | case ErrorCode::kDownloadStateInitializationError: | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 331 | case ErrorCode::kOmahaErrorInHTTPResponse:  // Aggregate for HTTP errors. | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 332 | IncrementFailureCount(); | 
|  | 333 | break; | 
|  | 334 |  | 
|  | 335 | // Errors which are not specific to a URL and hence shouldn't result in | 
|  | 336 | // the URL being penalized. This can happen in two cases: | 
|  | 337 | // 1. We haven't started downloading anything: These errors don't cost us | 
|  | 338 | // anything in terms of actual payload bytes, so we should just do the | 
|  | 339 | // regular retries at the next update check. | 
|  | 340 | // 2. We have successfully downloaded the payload: In this case, the | 
|  | 341 | // payload attempt number would have been incremented and would take care | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 342 | // of the backoff at the next update check. | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 343 | // In either case, there's no need to update URL index or failure count. | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 344 | case ErrorCode::kOmahaRequestError: | 
|  | 345 | case ErrorCode::kOmahaResponseHandlerError: | 
|  | 346 | case ErrorCode::kPostinstallRunnerError: | 
|  | 347 | case ErrorCode::kFilesystemCopierError: | 
|  | 348 | case ErrorCode::kInstallDeviceOpenError: | 
|  | 349 | case ErrorCode::kKernelDeviceOpenError: | 
|  | 350 | case ErrorCode::kDownloadNewPartitionInfoError: | 
|  | 351 | case ErrorCode::kNewRootfsVerificationError: | 
|  | 352 | case ErrorCode::kNewKernelVerificationError: | 
|  | 353 | case ErrorCode::kPostinstallBootedFromFirmwareB: | 
|  | 354 | case ErrorCode::kPostinstallFirmwareRONotUpdatable: | 
|  | 355 | case ErrorCode::kOmahaRequestEmptyResponseError: | 
|  | 356 | case ErrorCode::kOmahaRequestXMLParseError: | 
|  | 357 | case ErrorCode::kOmahaResponseInvalid: | 
|  | 358 | case ErrorCode::kOmahaUpdateIgnoredPerPolicy: | 
|  | 359 | case ErrorCode::kOmahaUpdateDeferredPerPolicy: | 
| Kevin Cernekee | 2494e28 | 2016-03-29 18:03:53 -0700 | [diff] [blame] | 360 | case ErrorCode::kNonCriticalUpdateInOOBE: | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 361 | case ErrorCode::kOmahaUpdateDeferredForBackoff: | 
|  | 362 | case ErrorCode::kPostinstallPowerwashError: | 
|  | 363 | case ErrorCode::kUpdateCanceledByChannelChange: | 
| David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 364 | case ErrorCode::kOmahaRequestXMLHasEntityDecl: | 
| Allie Wood | eb9e6d8 | 2015-04-17 13:55:30 -0700 | [diff] [blame] | 365 | case ErrorCode::kFilesystemVerifierError: | 
| Alex Deymo | 1f19dcc | 2016-02-03 09:22:17 -0800 | [diff] [blame] | 366 | case ErrorCode::kUserCanceled: | 
| Weidong Guo | 421ff33 | 2017-04-17 10:08:38 -0700 | [diff] [blame] | 367 | case ErrorCode::kOmahaUpdateIgnoredOverCellular: | 
| Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 368 | case ErrorCode::kUpdatedButNotActive: | 
| Sen Jiang | 89e24c1 | 2018-03-22 18:05:44 -0700 | [diff] [blame] | 369 | case ErrorCode::kNoUpdate: | 
| Marton Hunyady | 199152d | 2018-05-07 19:08:48 +0200 | [diff] [blame] | 370 | case ErrorCode::kRollbackNotPossible: | 
| Amin Hassani | 80f4d4c | 2018-05-16 13:34:00 -0700 | [diff] [blame] | 371 | case ErrorCode::kFirstActiveOmahaPingSentPersistenceError: | 
| Amin Hassani | d3d8421 | 2019-08-17 00:27:44 -0700 | [diff] [blame] | 372 | case ErrorCode::kInternalLibCurlError: | 
|  | 373 | case ErrorCode::kUnresolvedHostError: | 
|  | 374 | case ErrorCode::kUnresolvedHostRecovered: | 
| Yifan Hong | 46e37d4 | 2019-12-13 12:01:42 -0800 | [diff] [blame] | 375 | case ErrorCode::kNotEnoughSpace: | 
| Yifan Hong | 7727990 | 2019-12-17 16:38:21 -0800 | [diff] [blame] | 376 | case ErrorCode::kDeviceCorrupted: | 
| Jae Hoon Kim | 3e69b4c | 2020-06-16 09:23:39 -0700 | [diff] [blame] | 377 | case ErrorCode::kPackageExcludedFromUpdate: | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 378 | LOG(INFO) << "Not incrementing URL index or failure count for this error"; | 
|  | 379 | break; | 
|  | 380 |  | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 381 | case ErrorCode::kSuccess:                       // success code | 
|  | 382 | case ErrorCode::kUmaReportedMax:                // not an error code | 
|  | 383 | case ErrorCode::kOmahaRequestHTTPResponseBase:  // aggregated already | 
|  | 384 | case ErrorCode::kDevModeFlag:                   // not an error code | 
|  | 385 | case ErrorCode::kResumedFlag:                   // not an error code | 
|  | 386 | case ErrorCode::kTestImageFlag:                 // not an error code | 
|  | 387 | case ErrorCode::kTestOmahaUrlFlag:              // not an error code | 
|  | 388 | case ErrorCode::kSpecialFlags:                  // not an error code | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 389 | // These shouldn't happen. Enumerating these  explicitly here so that we | 
|  | 390 | // can let the compiler warn about new error codes that are added to | 
|  | 391 | // action_processor.h but not added here. | 
|  | 392 | LOG(WARNING) << "Unexpected error code for UpdateFailed"; | 
|  | 393 | break; | 
|  | 394 |  | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 395 | // Note: Not adding a default here so as to let the compiler warn us of | 
|  | 396 | // any new enums that were added in the .h but not listed in this switch. | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 397 | } | 
|  | 398 | } | 
|  | 399 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 400 | bool PayloadState::ShouldBackoffDownload() { | 
|  | 401 | if (response_.disable_payload_backoff) { | 
|  | 402 | LOG(INFO) << "Payload backoff logic is disabled. " | 
|  | 403 | "Can proceed with the download"; | 
|  | 404 | return false; | 
|  | 405 | } | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 406 | if (GetUsingP2PForDownloading() && !GetP2PUrl().empty()) { | 
| Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 407 | LOG(INFO) << "Payload backoff logic is disabled because download " | 
|  | 408 | << "will happen from local peer (via p2p)."; | 
|  | 409 | return false; | 
|  | 410 | } | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 411 | if (SystemState::Get()->request_params()->interactive()) { | 
| Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 412 | LOG(INFO) << "Payload backoff disabled for interactive update checks."; | 
|  | 413 | return false; | 
|  | 414 | } | 
| Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 415 | for (const auto& package : response_.packages) { | 
|  | 416 | if (package.is_delta) { | 
|  | 417 | // If delta payloads fail, we want to fallback quickly to full payloads as | 
|  | 418 | // they are more likely to succeed. Exponential backoffs would greatly | 
|  | 419 | // slow down the fallback to full payloads.  So we don't backoff for delta | 
|  | 420 | // payloads. | 
|  | 421 | LOG(INFO) << "No backoffs for delta payloads. " | 
|  | 422 | << "Can proceed with the download"; | 
|  | 423 | return false; | 
|  | 424 | } | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 425 | } | 
|  | 426 |  | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 427 | if (!SystemState::Get()->hardware()->IsOfficialBuild() && | 
| Amin Hassani | ffb6d80 | 2018-03-30 11:43:57 -0700 | [diff] [blame] | 428 | !prefs_->Exists(kPrefsNoIgnoreBackoff)) { | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 429 | // Backoffs are needed only for official builds. We do not want any delays | 
| Amin Hassani | ffb6d80 | 2018-03-30 11:43:57 -0700 | [diff] [blame] | 430 | // or update failures due to backoffs during testing or development. Unless | 
|  | 431 | // the |kPrefsNoIgnoreBackoff| is manually set. | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 432 | LOG(INFO) << "No backoffs for test/dev images. " | 
|  | 433 | << "Can proceed with the download"; | 
|  | 434 | return false; | 
|  | 435 | } | 
|  | 436 |  | 
|  | 437 | if (backoff_expiry_time_.is_null()) { | 
|  | 438 | LOG(INFO) << "No backoff expiry time has been set. " | 
|  | 439 | << "Can proceed with the download"; | 
|  | 440 | return false; | 
|  | 441 | } | 
|  | 442 |  | 
|  | 443 | if (backoff_expiry_time_ < Time::Now()) { | 
|  | 444 | LOG(INFO) << "The backoff expiry time (" | 
|  | 445 | << utils::ToString(backoff_expiry_time_) | 
|  | 446 | << ") has elapsed. Can proceed with the download"; | 
|  | 447 | return false; | 
|  | 448 | } | 
|  | 449 |  | 
|  | 450 | LOG(INFO) << "Cannot proceed with downloads as we need to backoff until " | 
|  | 451 | << utils::ToString(backoff_expiry_time_); | 
|  | 452 | return true; | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 453 | } | 
|  | 454 |  | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 455 | void PayloadState::Rollback() { | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 456 | SetRollbackVersion(SystemState::Get()->request_params()->app_version()); | 
| David Zeuthen | afed4a1 | 2014-04-09 15:28:44 -0700 | [diff] [blame] | 457 | AttemptStarted(AttemptType::kRollback); | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 458 | } | 
|  | 459 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 460 | void PayloadState::IncrementPayloadAttemptNumber() { | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 461 | // Update the payload attempt number for both payload types: full and delta. | 
|  | 462 | SetPayloadAttemptNumber(GetPayloadAttemptNumber() + 1); | 
|  | 463 | } | 
|  | 464 |  | 
|  | 465 | void PayloadState::IncrementFullPayloadAttemptNumber() { | 
| Jae Hoon Kim | 7658391 | 2020-06-23 10:24:03 -0700 | [diff] [blame] | 466 | DCHECK(payload_index_ < response_.packages.size()); | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 467 | // Update the payload attempt number for full payloads and the backoff time. | 
| Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 468 | if (response_.packages[payload_index_].is_delta) { | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 469 | LOG(INFO) << "Not incrementing payload attempt number for delta payloads"; | 
|  | 470 | return; | 
|  | 471 | } | 
|  | 472 |  | 
| Alex Deymo | 29b51d9 | 2013-07-09 15:26:24 -0700 | [diff] [blame] | 473 | LOG(INFO) << "Incrementing the full payload attempt number"; | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 474 | SetFullPayloadAttemptNumber(GetFullPayloadAttemptNumber() + 1); | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 475 | UpdateBackoffExpiryTime(); | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 476 | } | 
|  | 477 |  | 
|  | 478 | void PayloadState::IncrementUrlIndex() { | 
| Jae Hoon Kim | 7658391 | 2020-06-23 10:24:03 -0700 | [diff] [blame] | 479 | DCHECK(payload_index_ < candidate_urls_.size()); | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 480 | size_t next_url_index = url_index_ + 1; | 
| Jae Hoon Kim | a3210e6 | 2020-05-07 11:32:44 -0700 | [diff] [blame] | 481 | size_t max_url_size = candidate_urls_[payload_index_].size(); | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 482 | if (next_url_index < max_url_size) { | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 483 | LOG(INFO) << "Incrementing the URL index for next attempt"; | 
|  | 484 | SetUrlIndex(next_url_index); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 485 | } else { | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 486 | LOG(INFO) << "Resetting the current URL index (" << url_index_ << ") to " | 
|  | 487 | << "0 as we only have " << max_url_size << " candidate URL(s)"; | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 488 | SetUrlIndex(0); | 
| Alex Deymo | 29b51d9 | 2013-07-09 15:26:24 -0700 | [diff] [blame] | 489 | IncrementPayloadAttemptNumber(); | 
|  | 490 | IncrementFullPayloadAttemptNumber(); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 491 | } | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 492 |  | 
| David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 493 | // If we have multiple URLs, record that we just switched to another one | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 494 | if (max_url_size > 1) | 
| David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 495 | SetUrlSwitchCount(url_switch_count_ + 1); | 
|  | 496 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 497 | // Whenever we update the URL index, we should also clear the URL failure | 
|  | 498 | // count so we can start over fresh for the new URL. | 
|  | 499 | SetUrlFailureCount(0); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 500 | } | 
|  | 501 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 502 | void PayloadState::IncrementFailureCount() { | 
|  | 503 | uint32_t next_url_failure_count = GetUrlFailureCount() + 1; | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 504 | if (next_url_failure_count < response_.max_failure_count_per_url) { | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 505 | LOG(INFO) << "Incrementing the URL failure count"; | 
|  | 506 | SetUrlFailureCount(next_url_failure_count); | 
|  | 507 | } else { | 
|  | 508 | LOG(INFO) << "Reached max number of failures for Url" << GetUrlIndex() | 
|  | 509 | << ". Trying next available URL"; | 
| Jae Hoon Kim | 694eeb0 | 2020-06-01 14:24:08 -0700 | [diff] [blame] | 510 | ExcludeCurrentPayload(); | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 511 | IncrementUrlIndex(); | 
|  | 512 | } | 
|  | 513 | } | 
|  | 514 |  | 
| Jae Hoon Kim | 694eeb0 | 2020-06-01 14:24:08 -0700 | [diff] [blame] | 515 | void PayloadState::ExcludeCurrentPayload() { | 
| Jae Hoon Kim | 7658391 | 2020-06-23 10:24:03 -0700 | [diff] [blame] | 516 | if (payload_index_ >= response_.packages.size()) { | 
|  | 517 | LOG(INFO) << "Skipping exclusion of the current payload."; | 
|  | 518 | return; | 
|  | 519 | } | 
| Jae Hoon Kim | 694eeb0 | 2020-06-01 14:24:08 -0700 | [diff] [blame] | 520 | const auto& package = response_.packages[payload_index_]; | 
|  | 521 | if (!package.can_exclude) { | 
|  | 522 | LOG(INFO) << "Not excluding as marked non-excludable for package hash=" | 
|  | 523 | << package.hash; | 
|  | 524 | return; | 
|  | 525 | } | 
|  | 526 | auto exclusion_name = utils::GetExclusionName(GetCurrentUrl()); | 
|  | 527 | if (!excluder_->Exclude(exclusion_name)) | 
|  | 528 | LOG(WARNING) << "Failed to exclude " | 
|  | 529 | << " Package Hash=" << package.hash | 
|  | 530 | << " CurrentUrl=" << GetCurrentUrl(); | 
|  | 531 | else | 
|  | 532 | LOG(INFO) << "Excluded " | 
|  | 533 | << " Package Hash=" << package.hash | 
|  | 534 | << " CurrentUrl=" << GetCurrentUrl(); | 
|  | 535 | } | 
|  | 536 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 537 | void PayloadState::UpdateBackoffExpiryTime() { | 
|  | 538 | if (response_.disable_payload_backoff) { | 
|  | 539 | LOG(INFO) << "Resetting backoff expiry time as payload backoff is disabled"; | 
|  | 540 | SetBackoffExpiryTime(Time()); | 
|  | 541 | return; | 
|  | 542 | } | 
|  | 543 |  | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 544 | if (GetFullPayloadAttemptNumber() == 0) { | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 545 | SetBackoffExpiryTime(Time()); | 
|  | 546 | return; | 
|  | 547 | } | 
|  | 548 |  | 
|  | 549 | // Since we're doing left-shift below, make sure we don't shift more | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 550 | // than this. E.g. if int is 4-bytes, don't left-shift more than 30 bits, | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 551 | // since we don't expect value of kMaxBackoffDays to be more than 100 anyway. | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 552 | int num_days = 1;  // the value to be shifted. | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 553 | const int kMaxShifts = (sizeof(num_days) * 8) - 2; | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 554 |  | 
|  | 555 | // Normal backoff days is 2 raised to (payload_attempt_number - 1). | 
|  | 556 | // E.g. if payload_attempt_number is over 30, limit power to 30. | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 557 | int power = min(GetFullPayloadAttemptNumber() - 1, kMaxShifts); | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 558 |  | 
|  | 559 | // The number of days is the minimum of 2 raised to (payload_attempt_number | 
|  | 560 | // - 1) or kMaxBackoffDays. | 
|  | 561 | num_days = min(num_days << power, kMaxBackoffDays); | 
|  | 562 |  | 
|  | 563 | // We don't want all retries to happen exactly at the same time when | 
|  | 564 | // retrying after backoff. So add some random minutes to fuzz. | 
|  | 565 | int fuzz_minutes = utils::FuzzInt(0, kMaxBackoffFuzzMinutes); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 566 | TimeDelta next_backoff_interval = | 
|  | 567 | TimeDelta::FromDays(num_days) + TimeDelta::FromMinutes(fuzz_minutes); | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 568 | LOG(INFO) << "Incrementing the backoff expiry time by " | 
|  | 569 | << utils::FormatTimeDelta(next_backoff_interval); | 
|  | 570 | SetBackoffExpiryTime(Time::Now() + next_backoff_interval); | 
|  | 571 | } | 
|  | 572 |  | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 573 | void PayloadState::UpdateCurrentDownloadSource() { | 
|  | 574 | current_download_source_ = kNumDownloadSources; | 
|  | 575 |  | 
| David Zeuthen | bb8bdc7 | 2013-09-03 13:43:48 -0700 | [diff] [blame] | 576 | if (using_p2p_for_downloading_) { | 
|  | 577 | current_download_source_ = kDownloadSourceHttpPeer; | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 578 | } else if (payload_index_ < candidate_urls_.size() && | 
|  | 579 | candidate_urls_[payload_index_].size() != 0) { | 
|  | 580 | const string& current_url = candidate_urls_[payload_index_][GetUrlIndex()]; | 
|  | 581 | if (base::StartsWith( | 
|  | 582 | current_url, "https://", base::CompareCase::INSENSITIVE_ASCII)) { | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 583 | current_download_source_ = kDownloadSourceHttpsServer; | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 584 | } else if (base::StartsWith(current_url, | 
|  | 585 | "http://", | 
| Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 586 | base::CompareCase::INSENSITIVE_ASCII)) { | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 587 | current_download_source_ = kDownloadSourceHttpServer; | 
| Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 588 | } | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 589 | } | 
|  | 590 |  | 
|  | 591 | LOG(INFO) << "Current download source: " | 
|  | 592 | << utils::ToString(current_download_source_); | 
|  | 593 | } | 
|  | 594 |  | 
|  | 595 | void PayloadState::UpdateBytesDownloaded(size_t count) { | 
|  | 596 | SetCurrentBytesDownloaded( | 
|  | 597 | current_download_source_, | 
|  | 598 | GetCurrentBytesDownloaded(current_download_source_) + count, | 
|  | 599 | false); | 
|  | 600 | SetTotalBytesDownloaded( | 
|  | 601 | current_download_source_, | 
|  | 602 | GetTotalBytesDownloaded(current_download_source_) + count, | 
|  | 603 | false); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 604 |  | 
|  | 605 | attempt_num_bytes_downloaded_ += count; | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 606 | } | 
|  | 607 |  | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 608 | PayloadType PayloadState::CalculatePayloadType() { | 
| Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 609 | for (const auto& package : response_.packages) { | 
|  | 610 | if (package.is_delta) { | 
|  | 611 | return kPayloadTypeDelta; | 
|  | 612 | } | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 613 | } | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 614 | OmahaRequestParams* params = SystemState::Get()->request_params(); | 
| Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 615 | if (params->delta_okay()) { | 
|  | 616 | return kPayloadTypeFull; | 
|  | 617 | } | 
|  | 618 | // Full payload, delta was not allowed by request. | 
|  | 619 | return kPayloadTypeForcedFull; | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 620 | } | 
|  | 621 |  | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 622 | void PayloadState::CollectAndReportAttemptMetrics(ErrorCode code) { | 
|  | 623 | int attempt_number = GetPayloadAttemptNumber(); | 
|  | 624 |  | 
|  | 625 | PayloadType payload_type = CalculatePayloadType(); | 
|  | 626 |  | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 627 | int64_t payload_size = GetPayloadSize(); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 628 |  | 
|  | 629 | int64_t payload_bytes_downloaded = attempt_num_bytes_downloaded_; | 
|  | 630 |  | 
| Amin Hassani | 0468a76 | 2020-11-17 23:53:48 -0800 | [diff] [blame] | 631 | const auto* clock = SystemState::Get()->clock(); | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 632 | TimeDelta duration = clock->GetBootTime() - attempt_start_time_boot_; | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 633 | TimeDelta duration_uptime = | 
|  | 634 | clock->GetMonotonicTime() - attempt_start_time_monotonic_; | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 635 |  | 
|  | 636 | int64_t payload_download_speed_bps = 0; | 
|  | 637 | int64_t usec = duration_uptime.InMicroseconds(); | 
|  | 638 | if (usec > 0) { | 
|  | 639 | double sec = static_cast<double>(usec) / Time::kMicrosecondsPerSecond; | 
|  | 640 | double bps = static_cast<double>(payload_bytes_downloaded) / sec; | 
|  | 641 | payload_download_speed_bps = static_cast<int64_t>(bps); | 
|  | 642 | } | 
|  | 643 |  | 
|  | 644 | DownloadSource download_source = current_download_source_; | 
|  | 645 |  | 
|  | 646 | metrics::DownloadErrorCode payload_download_error_code = | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 647 | metrics::DownloadErrorCode::kUnset; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 648 | ErrorCode internal_error_code = ErrorCode::kSuccess; | 
| Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 649 | metrics::AttemptResult attempt_result = metrics_utils::GetAttemptResult(code); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 650 |  | 
|  | 651 | // Add additional detail to AttemptResult | 
|  | 652 | switch (attempt_result) { | 
|  | 653 | case metrics::AttemptResult::kPayloadDownloadError: | 
| Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 654 | payload_download_error_code = metrics_utils::GetDownloadErrorCode(code); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 655 | break; | 
|  | 656 |  | 
|  | 657 | case metrics::AttemptResult::kInternalError: | 
|  | 658 | internal_error_code = code; | 
|  | 659 | break; | 
|  | 660 |  | 
|  | 661 | // Explicit fall-through for cases where we do not have additional | 
|  | 662 | // detail. We avoid the default keyword to force people adding new | 
|  | 663 | // AttemptResult values to visit this code and examine whether | 
|  | 664 | // additional detail is needed. | 
|  | 665 | case metrics::AttemptResult::kUpdateSucceeded: | 
|  | 666 | case metrics::AttemptResult::kMetadataMalformed: | 
|  | 667 | case metrics::AttemptResult::kOperationMalformed: | 
|  | 668 | case metrics::AttemptResult::kOperationExecutionError: | 
|  | 669 | case metrics::AttemptResult::kMetadataVerificationFailed: | 
|  | 670 | case metrics::AttemptResult::kPayloadVerificationFailed: | 
|  | 671 | case metrics::AttemptResult::kVerificationFailed: | 
|  | 672 | case metrics::AttemptResult::kPostInstallFailed: | 
|  | 673 | case metrics::AttemptResult::kAbnormalTermination: | 
| Alex Deymo | 1f19dcc | 2016-02-03 09:22:17 -0800 | [diff] [blame] | 674 | case metrics::AttemptResult::kUpdateCanceled: | 
| Sen Jiang | fe52282 | 2017-10-31 15:14:11 -0700 | [diff] [blame] | 675 | case metrics::AttemptResult::kUpdateSucceededNotActive: | 
| Andrew | b57c16e | 2020-07-22 14:32:39 -0700 | [diff] [blame] | 676 | case metrics::AttemptResult::kUpdateSkipped: | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 677 | case metrics::AttemptResult::kNumConstants: | 
|  | 678 | case metrics::AttemptResult::kUnset: | 
|  | 679 | break; | 
|  | 680 | } | 
|  | 681 |  | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 682 | SystemState::Get()->metrics_reporter()->ReportUpdateAttemptMetrics( | 
| Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 683 | attempt_number, | 
|  | 684 | payload_type, | 
|  | 685 | duration, | 
|  | 686 | duration_uptime, | 
|  | 687 | payload_size, | 
| Tianjie Xu | 1f93d09 | 2017-10-09 12:13:29 -0700 | [diff] [blame] | 688 | attempt_result, | 
|  | 689 | internal_error_code); | 
|  | 690 |  | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 691 | SystemState::Get()->metrics_reporter()->ReportUpdateAttemptDownloadMetrics( | 
| Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 692 | payload_bytes_downloaded, | 
|  | 693 | payload_download_speed_bps, | 
|  | 694 | download_source, | 
| Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 695 | payload_download_error_code, | 
|  | 696 | attempt_connection_type_); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 697 | } | 
|  | 698 |  | 
| David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 699 | void PayloadState::PersistAttemptMetrics() { | 
|  | 700 | // TODO(zeuthen): For now we only persist whether an attempt was in | 
|  | 701 | // progress and not values/metrics related to the attempt. This | 
|  | 702 | // means that when this happens, of all the UpdateEngine.Attempt.* | 
|  | 703 | // metrics, only UpdateEngine.Attempt.Result is reported (with the | 
|  | 704 | // value |kAbnormalTermination|). In the future we might want to | 
|  | 705 | // persist more data so we can report other metrics in the | 
|  | 706 | // UpdateEngine.Attempt.* namespace when this happens. | 
|  | 707 | prefs_->SetBoolean(kPrefsAttemptInProgress, true); | 
|  | 708 | } | 
|  | 709 |  | 
|  | 710 | void PayloadState::ClearPersistedAttemptMetrics() { | 
|  | 711 | prefs_->Delete(kPrefsAttemptInProgress); | 
|  | 712 | } | 
|  | 713 |  | 
|  | 714 | void PayloadState::ReportAndClearPersistedAttemptMetrics() { | 
|  | 715 | bool attempt_in_progress = false; | 
|  | 716 | if (!prefs_->GetBoolean(kPrefsAttemptInProgress, &attempt_in_progress)) | 
|  | 717 | return; | 
|  | 718 | if (!attempt_in_progress) | 
|  | 719 | return; | 
|  | 720 |  | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 721 | SystemState::Get() | 
|  | 722 | ->metrics_reporter() | 
| Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 723 | ->ReportAbnormallyTerminatedUpdateAttemptMetrics(); | 
| David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 724 |  | 
|  | 725 | ClearPersistedAttemptMetrics(); | 
|  | 726 | } | 
|  | 727 |  | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 728 | void PayloadState::CollectAndReportSuccessfulUpdateMetrics() { | 
| Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 729 | string metric; | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 730 |  | 
|  | 731 | // Report metrics collected from all known download sources to UMA. | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 732 | int64_t total_bytes_by_source[kNumDownloadSources]; | 
|  | 733 | int64_t successful_bytes = 0; | 
|  | 734 | int64_t total_bytes = 0; | 
|  | 735 | int64_t successful_mbs = 0; | 
|  | 736 | int64_t total_mbs = 0; | 
|  | 737 |  | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 738 | for (int i = 0; i < kNumDownloadSources; i++) { | 
|  | 739 | DownloadSource source = static_cast<DownloadSource>(i); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 740 | int64_t bytes; | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 741 |  | 
| David Zeuthen | 4484860 | 2013-06-24 13:32:14 -0700 | [diff] [blame] | 742 | // Only consider this download source (and send byte counts) as | 
|  | 743 | // having been used if we downloaded a non-trivial amount of bytes | 
|  | 744 | // (e.g. at least 1 MiB) that contributed to the final success of | 
|  | 745 | // the update. Otherwise we're going to end up with a lot of | 
|  | 746 | // zero-byte events in the histogram. | 
| Jay Srinivasan | dbd9ea2 | 2013-04-22 17:45:19 -0700 | [diff] [blame] | 747 |  | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 748 | bytes = GetCurrentBytesDownloaded(source); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 749 | successful_bytes += bytes; | 
|  | 750 | successful_mbs += bytes / kNumBytesInOneMiB; | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 751 | SetCurrentBytesDownloaded(source, 0, true); | 
|  | 752 |  | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 753 | bytes = GetTotalBytesDownloaded(source); | 
|  | 754 | total_bytes_by_source[i] = bytes; | 
|  | 755 | total_bytes += bytes; | 
|  | 756 | total_mbs += bytes / kNumBytesInOneMiB; | 
|  | 757 | SetTotalBytesDownloaded(source, 0, true); | 
|  | 758 | } | 
|  | 759 |  | 
|  | 760 | int download_overhead_percentage = 0; | 
|  | 761 | if (successful_bytes > 0) { | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 762 | download_overhead_percentage = | 
|  | 763 | (total_bytes - successful_bytes) * 100ULL / successful_bytes; | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 764 | } | 
|  | 765 |  | 
|  | 766 | int url_switch_count = static_cast<int>(url_switch_count_); | 
|  | 767 |  | 
|  | 768 | int reboot_count = GetNumReboots(); | 
|  | 769 |  | 
|  | 770 | SetNumReboots(0); | 
|  | 771 |  | 
|  | 772 | TimeDelta duration = GetUpdateDuration(); | 
| Sen Jiang | 8712e96 | 2018-05-08 12:12:28 -0700 | [diff] [blame] | 773 | TimeDelta duration_uptime = GetUpdateDurationUptime(); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 774 |  | 
|  | 775 | prefs_->Delete(kPrefsUpdateTimestampStart); | 
|  | 776 | prefs_->Delete(kPrefsUpdateDurationUptime); | 
|  | 777 |  | 
|  | 778 | PayloadType payload_type = CalculatePayloadType(); | 
|  | 779 |  | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 780 | int64_t payload_size = GetPayloadSize(); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 781 |  | 
|  | 782 | int attempt_count = GetPayloadAttemptNumber(); | 
|  | 783 |  | 
|  | 784 | int updates_abandoned_count = num_responses_seen_ - 1; | 
|  | 785 |  | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 786 | SystemState::Get()->metrics_reporter()->ReportSuccessfulUpdateMetrics( | 
| Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 787 | attempt_count, | 
|  | 788 | updates_abandoned_count, | 
|  | 789 | payload_type, | 
|  | 790 | payload_size, | 
|  | 791 | total_bytes_by_source, | 
|  | 792 | download_overhead_percentage, | 
|  | 793 | duration, | 
| Sen Jiang | 8712e96 | 2018-05-08 12:12:28 -0700 | [diff] [blame] | 794 | duration_uptime, | 
| Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 795 | reboot_count, | 
|  | 796 | url_switch_count); | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 797 | } | 
|  | 798 |  | 
|  | 799 | void PayloadState::UpdateNumReboots() { | 
|  | 800 | // We only update the reboot count when the system has been detected to have | 
|  | 801 | // been rebooted. | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 802 | if (!SystemState::Get()->system_rebooted()) { | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 803 | return; | 
|  | 804 | } | 
|  | 805 |  | 
|  | 806 | SetNumReboots(GetNumReboots() + 1); | 
|  | 807 | } | 
|  | 808 |  | 
|  | 809 | void PayloadState::SetNumReboots(uint32_t num_reboots) { | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 810 | num_reboots_ = num_reboots; | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 811 | metrics_utils::SetNumReboots(num_reboots, prefs_); | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 812 | } | 
|  | 813 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 814 | void PayloadState::ResetPersistedState() { | 
|  | 815 | SetPayloadAttemptNumber(0); | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 816 | SetFullPayloadAttemptNumber(0); | 
| Sen Jiang | 97eba34 | 2017-05-22 14:34:11 -0700 | [diff] [blame] | 817 | SetPayloadIndex(0); | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 818 | SetUrlIndex(0); | 
|  | 819 | SetUrlFailureCount(0); | 
| David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 820 | SetUrlSwitchCount(0); | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 821 | UpdateBackoffExpiryTime();  // This will reset the backoff expiry time. | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 822 | SetUpdateTimestampStart(SystemState::Get()->clock()->GetWallclockTime()); | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 823 | SetUpdateTimestampEnd(Time());  // Set to null time | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 824 | SetUpdateDurationUptime(TimeDelta::FromSeconds(0)); | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 825 | ResetDownloadSourcesOnNewUpdate(); | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 826 | ResetRollbackVersion(); | 
| David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 827 | SetP2PNumAttempts(0); | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 828 | SetP2PFirstAttemptTimestamp(Time());  // Set to null time | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 829 | SetScatteringWaitPeriod(TimeDelta()); | 
| Adolfo Victoria | d3a1e35 | 2018-07-16 11:40:47 -0700 | [diff] [blame] | 830 | SetStagingWaitPeriod(TimeDelta()); | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 831 | } | 
|  | 832 |  | 
|  | 833 | void PayloadState::ResetRollbackVersion() { | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 834 | rollback_version_ = ""; | 
|  | 835 | powerwash_safe_prefs_->Delete(kPrefsRollbackVersion); | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 836 | } | 
|  | 837 |  | 
|  | 838 | void PayloadState::ResetDownloadSourcesOnNewUpdate() { | 
|  | 839 | for (int i = 0; i < kNumDownloadSources; i++) { | 
|  | 840 | DownloadSource source = static_cast<DownloadSource>(i); | 
|  | 841 | SetCurrentBytesDownloaded(source, 0, true); | 
|  | 842 | // Note: Not resetting the TotalBytesDownloaded as we want that metric | 
|  | 843 | // to count the bytes downloaded across various update attempts until | 
|  | 844 | // we have successfully applied the update. | 
|  | 845 | } | 
|  | 846 | } | 
|  | 847 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 848 | string PayloadState::CalculateResponseSignature() { | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 849 | string response_sign; | 
|  | 850 | for (size_t i = 0; i < response_.packages.size(); i++) { | 
|  | 851 | const auto& package = response_.packages[i]; | 
|  | 852 | response_sign += base::StringPrintf( | 
|  | 853 | "Payload %zu:\n" | 
|  | 854 | "  Size = %ju\n" | 
|  | 855 | "  Sha256 Hash = %s\n" | 
|  | 856 | "  Metadata Size = %ju\n" | 
|  | 857 | "  Metadata Signature = %s\n" | 
| Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 858 | "  Is Delta = %d\n" | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 859 | "  NumURLs = %zu\n", | 
|  | 860 | i, | 
|  | 861 | static_cast<uintmax_t>(package.size), | 
|  | 862 | package.hash.c_str(), | 
|  | 863 | static_cast<uintmax_t>(package.metadata_size), | 
|  | 864 | package.metadata_signature.c_str(), | 
| Sen Jiang | cdd5206 | 2017-05-18 15:33:10 -0700 | [diff] [blame] | 865 | package.is_delta, | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 866 | candidate_urls_[i].size()); | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 867 |  | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 868 | for (size_t j = 0; j < candidate_urls_[i].size(); j++) | 
|  | 869 | response_sign += base::StringPrintf( | 
|  | 870 | "  Candidate Url%zu = %s\n", j, candidate_urls_[i][j].c_str()); | 
|  | 871 | } | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 872 |  | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 873 | response_sign += base::StringPrintf( | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 874 | "Max Failure Count Per Url = %d\n" | 
|  | 875 | "Disable Payload Backoff = %d\n", | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 876 | response_.max_failure_count_per_url, | 
|  | 877 | response_.disable_payload_backoff); | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 878 | return response_sign; | 
|  | 879 | } | 
|  | 880 |  | 
|  | 881 | void PayloadState::LoadResponseSignature() { | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 882 | string stored_value; | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 883 | if (prefs_->Exists(kPrefsCurrentResponseSignature) && | 
|  | 884 | prefs_->GetString(kPrefsCurrentResponseSignature, &stored_value)) { | 
|  | 885 | SetResponseSignature(stored_value); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 886 | } | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 887 | } | 
|  | 888 |  | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 889 | void PayloadState::SetResponseSignature(const string& response_signature) { | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 890 | response_signature_ = response_signature; | 
|  | 891 | LOG(INFO) << "Current Response Signature = \n" << response_signature_; | 
|  | 892 | prefs_->SetString(kPrefsCurrentResponseSignature, response_signature_); | 
|  | 893 | } | 
|  | 894 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 895 | void PayloadState::LoadPayloadAttemptNumber() { | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 896 | SetPayloadAttemptNumber( | 
|  | 897 | GetPersistedValue(kPrefsPayloadAttemptNumber, prefs_)); | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 898 | } | 
|  | 899 |  | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 900 | void PayloadState::LoadFullPayloadAttemptNumber() { | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 901 | SetFullPayloadAttemptNumber( | 
|  | 902 | GetPersistedValue(kPrefsFullPayloadAttemptNumber, prefs_)); | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 903 | } | 
|  | 904 |  | 
|  | 905 | void PayloadState::SetPayloadAttemptNumber(int payload_attempt_number) { | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 906 | payload_attempt_number_ = payload_attempt_number; | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 907 | metrics_utils::SetPayloadAttemptNumber(payload_attempt_number, prefs_); | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 908 | } | 
|  | 909 |  | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 910 | void PayloadState::SetFullPayloadAttemptNumber( | 
|  | 911 | int full_payload_attempt_number) { | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 912 | full_payload_attempt_number_ = full_payload_attempt_number; | 
|  | 913 | LOG(INFO) << "Full Payload Attempt Number = " << full_payload_attempt_number_; | 
|  | 914 | prefs_->SetInt64(kPrefsFullPayloadAttemptNumber, | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 915 | full_payload_attempt_number_); | 
| Alex Deymo | 820cc70 | 2013-06-28 15:43:46 -0700 | [diff] [blame] | 916 | } | 
|  | 917 |  | 
| Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 918 | void PayloadState::SetPayloadIndex(size_t payload_index) { | 
| Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 919 | payload_index_ = payload_index; | 
|  | 920 | LOG(INFO) << "Payload Index = " << payload_index_; | 
|  | 921 | prefs_->SetInt64(kPrefsUpdateStatePayloadIndex, payload_index_); | 
|  | 922 | } | 
|  | 923 |  | 
|  | 924 | bool PayloadState::NextPayload() { | 
| Jae Hoon Kim | 7658391 | 2020-06-23 10:24:03 -0700 | [diff] [blame] | 925 | if (payload_index_ >= candidate_urls_.size()) | 
|  | 926 | return false; | 
|  | 927 | SetPayloadIndex(payload_index_ + 1); | 
|  | 928 | if (payload_index_ >= candidate_urls_.size()) | 
| Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 929 | return false; | 
| Jae Hoon Kim | a3210e6 | 2020-05-07 11:32:44 -0700 | [diff] [blame] | 930 | SetUrlIndex(0); | 
| Sen Jiang | 5ae865b | 2017-04-18 14:24:40 -0700 | [diff] [blame] | 931 | return true; | 
|  | 932 | } | 
|  | 933 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 934 | void PayloadState::LoadUrlIndex() { | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 935 | SetUrlIndex(GetPersistedValue(kPrefsCurrentUrlIndex, prefs_)); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 936 | } | 
|  | 937 |  | 
|  | 938 | void PayloadState::SetUrlIndex(uint32_t url_index) { | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 939 | url_index_ = url_index; | 
|  | 940 | LOG(INFO) << "Current URL Index = " << url_index_; | 
|  | 941 | prefs_->SetInt64(kPrefsCurrentUrlIndex, url_index_); | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 942 |  | 
|  | 943 | // Also update the download source, which is purely dependent on the | 
|  | 944 | // current URL index alone. | 
|  | 945 | UpdateCurrentDownloadSource(); | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 946 | } | 
|  | 947 |  | 
| Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 948 | void PayloadState::LoadScatteringWaitPeriod() { | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 949 | SetScatteringWaitPeriod(TimeDelta::FromSeconds( | 
| Adolfo Victoria | d3a1e35 | 2018-07-16 11:40:47 -0700 | [diff] [blame] | 950 | GetPersistedValue(kPrefsWallClockScatteringWaitPeriod, prefs_))); | 
| Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 951 | } | 
|  | 952 |  | 
| Alex Deymo | f329b93 | 2014-10-30 01:37:48 -0700 | [diff] [blame] | 953 | void PayloadState::SetScatteringWaitPeriod(TimeDelta wait_period) { | 
| Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 954 | scattering_wait_period_ = wait_period; | 
|  | 955 | LOG(INFO) << "Scattering Wait Period (seconds) = " | 
|  | 956 | << scattering_wait_period_.InSeconds(); | 
|  | 957 | if (scattering_wait_period_.InSeconds() > 0) { | 
| Adolfo Victoria | d3a1e35 | 2018-07-16 11:40:47 -0700 | [diff] [blame] | 958 | prefs_->SetInt64(kPrefsWallClockScatteringWaitPeriod, | 
| Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 959 | scattering_wait_period_.InSeconds()); | 
|  | 960 | } else { | 
| Adolfo Victoria | d3a1e35 | 2018-07-16 11:40:47 -0700 | [diff] [blame] | 961 | prefs_->Delete(kPrefsWallClockScatteringWaitPeriod); | 
|  | 962 | } | 
|  | 963 | } | 
|  | 964 |  | 
|  | 965 | void PayloadState::LoadStagingWaitPeriod() { | 
|  | 966 | SetStagingWaitPeriod(TimeDelta::FromSeconds( | 
|  | 967 | GetPersistedValue(kPrefsWallClockStagingWaitPeriod, prefs_))); | 
|  | 968 | } | 
|  | 969 |  | 
|  | 970 | void PayloadState::SetStagingWaitPeriod(TimeDelta wait_period) { | 
| Adolfo Victoria | d3a1e35 | 2018-07-16 11:40:47 -0700 | [diff] [blame] | 971 | staging_wait_period_ = wait_period; | 
|  | 972 | LOG(INFO) << "Staging Wait Period (days) =" << staging_wait_period_.InDays(); | 
|  | 973 | if (staging_wait_period_.InSeconds() > 0) { | 
|  | 974 | prefs_->SetInt64(kPrefsWallClockStagingWaitPeriod, | 
|  | 975 | staging_wait_period_.InSeconds()); | 
|  | 976 | } else { | 
|  | 977 | prefs_->Delete(kPrefsWallClockStagingWaitPeriod); | 
| Gilad Arnold | 519cfc7 | 2014-10-02 10:34:54 -0700 | [diff] [blame] | 978 | } | 
|  | 979 | } | 
|  | 980 |  | 
| David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 981 | void PayloadState::LoadUrlSwitchCount() { | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 982 | SetUrlSwitchCount(GetPersistedValue(kPrefsUrlSwitchCount, prefs_)); | 
| David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 983 | } | 
|  | 984 |  | 
|  | 985 | void PayloadState::SetUrlSwitchCount(uint32_t url_switch_count) { | 
| David Zeuthen | cc6f996 | 2013-04-18 11:57:24 -0700 | [diff] [blame] | 986 | url_switch_count_ = url_switch_count; | 
|  | 987 | LOG(INFO) << "URL Switch Count = " << url_switch_count_; | 
|  | 988 | prefs_->SetInt64(kPrefsUrlSwitchCount, url_switch_count_); | 
|  | 989 | } | 
|  | 990 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 991 | void PayloadState::LoadUrlFailureCount() { | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 992 | SetUrlFailureCount(GetPersistedValue(kPrefsCurrentUrlFailureCount, prefs_)); | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 993 | } | 
|  | 994 |  | 
|  | 995 | void PayloadState::SetUrlFailureCount(uint32_t url_failure_count) { | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 996 | url_failure_count_ = url_failure_count; | 
|  | 997 | LOG(INFO) << "Current URL (Url" << GetUrlIndex() | 
|  | 998 | << ")'s Failure Count = " << url_failure_count_; | 
|  | 999 | prefs_->SetInt64(kPrefsCurrentUrlFailureCount, url_failure_count_); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1000 | } | 
|  | 1001 |  | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 1002 | void PayloadState::LoadBackoffExpiryTime() { | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 1003 | int64_t stored_value; | 
|  | 1004 | if (!prefs_->Exists(kPrefsBackoffExpiryTime)) | 
|  | 1005 | return; | 
|  | 1006 |  | 
|  | 1007 | if (!prefs_->GetInt64(kPrefsBackoffExpiryTime, &stored_value)) | 
|  | 1008 | return; | 
|  | 1009 |  | 
|  | 1010 | Time stored_time = Time::FromInternalValue(stored_value); | 
|  | 1011 | if (stored_time > Time::Now() + TimeDelta::FromDays(kMaxBackoffDays)) { | 
|  | 1012 | LOG(ERROR) << "Invalid backoff expiry time (" | 
|  | 1013 | << utils::ToString(stored_time) | 
|  | 1014 | << ") in persisted state. Resetting."; | 
|  | 1015 | stored_time = Time(); | 
|  | 1016 | } | 
|  | 1017 | SetBackoffExpiryTime(stored_time); | 
|  | 1018 | } | 
|  | 1019 |  | 
|  | 1020 | void PayloadState::SetBackoffExpiryTime(const Time& new_time) { | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 1021 | backoff_expiry_time_ = new_time; | 
|  | 1022 | LOG(INFO) << "Backoff Expiry Time = " | 
|  | 1023 | << utils::ToString(backoff_expiry_time_); | 
|  | 1024 | prefs_->SetInt64(kPrefsBackoffExpiryTime, | 
|  | 1025 | backoff_expiry_time_.ToInternalValue()); | 
|  | 1026 | } | 
|  | 1027 |  | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1028 | TimeDelta PayloadState::GetUpdateDuration() { | 
| David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1029 | Time end_time = update_timestamp_end_.is_null() | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1030 | ? SystemState::Get()->clock()->GetWallclockTime() | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1031 | : update_timestamp_end_; | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1032 | return end_time - update_timestamp_start_; | 
|  | 1033 | } | 
|  | 1034 |  | 
|  | 1035 | void PayloadState::LoadUpdateTimestampStart() { | 
|  | 1036 | int64_t stored_value; | 
|  | 1037 | Time stored_time; | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1038 | Time now = SystemState::Get()->clock()->GetWallclockTime(); | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1039 |  | 
|  | 1040 | if (!prefs_->Exists(kPrefsUpdateTimestampStart)) { | 
|  | 1041 | // The preference missing is not unexpected - in that case, just | 
|  | 1042 | // use the current time as start time | 
|  | 1043 | stored_time = now; | 
|  | 1044 | } else if (!prefs_->GetInt64(kPrefsUpdateTimestampStart, &stored_value)) { | 
|  | 1045 | LOG(ERROR) << "Invalid UpdateTimestampStart value. Resetting."; | 
|  | 1046 | stored_time = now; | 
|  | 1047 | } else { | 
|  | 1048 | stored_time = Time::FromInternalValue(stored_value); | 
|  | 1049 | } | 
|  | 1050 |  | 
| Tianjie | e283ce4 | 2020-07-29 11:37:51 -0700 | [diff] [blame] | 1051 | // Validation check: If the time read from disk is in the future | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1052 | // (modulo some slack to account for possible NTP drift | 
|  | 1053 | // adjustments), something is fishy and we should report and | 
|  | 1054 | // reset. | 
|  | 1055 | TimeDelta duration_according_to_stored_time = now - stored_time; | 
|  | 1056 | if (duration_according_to_stored_time < -kDurationSlack) { | 
|  | 1057 | LOG(ERROR) << "The UpdateTimestampStart value (" | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1058 | << utils::ToString(stored_time) << ") in persisted state is " | 
| David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 1059 | << utils::FormatTimeDelta(duration_according_to_stored_time) | 
|  | 1060 | << " in the future. Resetting."; | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1061 | stored_time = now; | 
|  | 1062 | } | 
|  | 1063 |  | 
|  | 1064 | SetUpdateTimestampStart(stored_time); | 
|  | 1065 | } | 
|  | 1066 |  | 
|  | 1067 | void PayloadState::SetUpdateTimestampStart(const Time& value) { | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1068 | update_timestamp_start_ = value; | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1069 | metrics_utils::SetUpdateTimestampStart(value, prefs_); | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1070 | } | 
|  | 1071 |  | 
|  | 1072 | void PayloadState::SetUpdateTimestampEnd(const Time& value) { | 
|  | 1073 | update_timestamp_end_ = value; | 
|  | 1074 | LOG(INFO) << "Update Timestamp End = " | 
|  | 1075 | << utils::ToString(update_timestamp_end_); | 
|  | 1076 | } | 
|  | 1077 |  | 
|  | 1078 | TimeDelta PayloadState::GetUpdateDurationUptime() { | 
|  | 1079 | return update_duration_uptime_; | 
|  | 1080 | } | 
|  | 1081 |  | 
|  | 1082 | void PayloadState::LoadUpdateDurationUptime() { | 
|  | 1083 | int64_t stored_value; | 
|  | 1084 | TimeDelta stored_delta; | 
|  | 1085 |  | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1086 | if (!prefs_->Exists(kPrefsUpdateDurationUptime)) { | 
|  | 1087 | // The preference missing is not unexpected - in that case, just | 
|  | 1088 | // we'll use zero as the delta | 
|  | 1089 | } else if (!prefs_->GetInt64(kPrefsUpdateDurationUptime, &stored_value)) { | 
|  | 1090 | LOG(ERROR) << "Invalid UpdateDurationUptime value. Resetting."; | 
|  | 1091 | stored_delta = TimeDelta::FromSeconds(0); | 
|  | 1092 | } else { | 
|  | 1093 | stored_delta = TimeDelta::FromInternalValue(stored_value); | 
|  | 1094 | } | 
|  | 1095 |  | 
| Tianjie | e283ce4 | 2020-07-29 11:37:51 -0700 | [diff] [blame] | 1096 | // Validation check: Uptime can never be greater than the wall-clock | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1097 | // difference (modulo some slack). If it is, report and reset | 
|  | 1098 | // to the wall-clock difference. | 
|  | 1099 | TimeDelta diff = GetUpdateDuration() - stored_delta; | 
|  | 1100 | if (diff < -kDurationSlack) { | 
|  | 1101 | LOG(ERROR) << "The UpdateDurationUptime value (" | 
| David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 1102 | << utils::FormatTimeDelta(stored_delta) | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1103 | << ") in persisted state is " << utils::FormatTimeDelta(diff) | 
| David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 1104 | << " larger than the wall-clock delta. Resetting."; | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1105 | stored_delta = update_duration_current_; | 
|  | 1106 | } | 
|  | 1107 |  | 
|  | 1108 | SetUpdateDurationUptime(stored_delta); | 
|  | 1109 | } | 
|  | 1110 |  | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 1111 | void PayloadState::LoadNumReboots() { | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1112 | SetNumReboots(GetPersistedValue(kPrefsNumReboots, prefs_)); | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 1113 | } | 
|  | 1114 |  | 
| Marton Hunyady | e58bddb | 2018-04-10 20:27:26 +0200 | [diff] [blame] | 1115 | void PayloadState::LoadRollbackHappened() { | 
| Marton Hunyady | e58bddb | 2018-04-10 20:27:26 +0200 | [diff] [blame] | 1116 | bool rollback_happened = false; | 
|  | 1117 | powerwash_safe_prefs_->GetBoolean(kPrefsRollbackHappened, &rollback_happened); | 
|  | 1118 | SetRollbackHappened(rollback_happened); | 
|  | 1119 | } | 
|  | 1120 |  | 
|  | 1121 | void PayloadState::SetRollbackHappened(bool rollback_happened) { | 
| Marton Hunyady | e58bddb | 2018-04-10 20:27:26 +0200 | [diff] [blame] | 1122 | LOG(INFO) << "Setting rollback-happened to " << rollback_happened << "."; | 
|  | 1123 | rollback_happened_ = rollback_happened; | 
|  | 1124 | if (rollback_happened) { | 
|  | 1125 | powerwash_safe_prefs_->SetBoolean(kPrefsRollbackHappened, | 
|  | 1126 | rollback_happened); | 
|  | 1127 | } else { | 
|  | 1128 | powerwash_safe_prefs_->Delete(kPrefsRollbackHappened); | 
|  | 1129 | } | 
|  | 1130 | } | 
|  | 1131 |  | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 1132 | void PayloadState::LoadRollbackVersion() { | 
| Chris Sosa | b3dcdb3 | 2013-09-04 15:22:12 -0700 | [diff] [blame] | 1133 | string rollback_version; | 
|  | 1134 | if (powerwash_safe_prefs_->GetString(kPrefsRollbackVersion, | 
|  | 1135 | &rollback_version)) { | 
|  | 1136 | SetRollbackVersion(rollback_version); | 
|  | 1137 | } | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 1138 | } | 
|  | 1139 |  | 
|  | 1140 | void PayloadState::SetRollbackVersion(const string& rollback_version) { | 
| Andrew | cc6ab9f | 2020-06-25 07:41:40 -0700 | [diff] [blame] | 1141 | LOG(INFO) << "Excluding version " << rollback_version; | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 1142 | rollback_version_ = rollback_version; | 
|  | 1143 | powerwash_safe_prefs_->SetString(kPrefsRollbackVersion, rollback_version); | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 1144 | } | 
|  | 1145 |  | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1146 | void PayloadState::SetUpdateDurationUptimeExtended(const TimeDelta& value, | 
|  | 1147 | const Time& timestamp, | 
|  | 1148 | bool use_logging) { | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1149 | update_duration_uptime_ = value; | 
|  | 1150 | update_duration_uptime_timestamp_ = timestamp; | 
|  | 1151 | prefs_->SetInt64(kPrefsUpdateDurationUptime, | 
|  | 1152 | update_duration_uptime_.ToInternalValue()); | 
|  | 1153 | if (use_logging) { | 
|  | 1154 | LOG(INFO) << "Update Duration Uptime = " | 
| David Zeuthen | 674c318 | 2013-04-18 14:05:20 -0700 | [diff] [blame] | 1155 | << utils::FormatTimeDelta(update_duration_uptime_); | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1156 | } | 
|  | 1157 | } | 
|  | 1158 |  | 
|  | 1159 | void PayloadState::SetUpdateDurationUptime(const TimeDelta& value) { | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1160 | Time now = SystemState::Get()->clock()->GetMonotonicTime(); | 
| David Zeuthen | f413fe5 | 2013-04-22 14:04:39 -0700 | [diff] [blame] | 1161 | SetUpdateDurationUptimeExtended(value, now, true); | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1162 | } | 
|  | 1163 |  | 
|  | 1164 | void PayloadState::CalculateUpdateDurationUptime() { | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1165 | Time now = SystemState::Get()->clock()->GetMonotonicTime(); | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1166 | TimeDelta uptime_since_last_update = now - update_duration_uptime_timestamp_; | 
| Colin Howes | 0e452c9 | 2018-11-02 13:18:44 -0700 | [diff] [blame] | 1167 |  | 
|  | 1168 | if (uptime_since_last_update > TimeDelta::FromSeconds(kUptimeResolution)) { | 
|  | 1169 | TimeDelta new_uptime = update_duration_uptime_ + uptime_since_last_update; | 
|  | 1170 | // We're frequently called so avoid logging this write | 
|  | 1171 | SetUpdateDurationUptimeExtended(new_uptime, now, false); | 
|  | 1172 | } | 
| David Zeuthen | 9a017f2 | 2013-04-11 16:10:26 -0700 | [diff] [blame] | 1173 | } | 
|  | 1174 |  | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 1175 | string PayloadState::GetPrefsKey(const string& prefix, DownloadSource source) { | 
|  | 1176 | return prefix + "-from-" + utils::ToString(source); | 
|  | 1177 | } | 
|  | 1178 |  | 
|  | 1179 | void PayloadState::LoadCurrentBytesDownloaded(DownloadSource source) { | 
|  | 1180 | string key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source); | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1181 | SetCurrentBytesDownloaded(source, GetPersistedValue(key, prefs_), true); | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 1182 | } | 
|  | 1183 |  | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1184 | void PayloadState::SetCurrentBytesDownloaded(DownloadSource source, | 
|  | 1185 | uint64_t current_bytes_downloaded, | 
|  | 1186 | bool log) { | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 1187 | if (source >= kNumDownloadSources) | 
|  | 1188 | return; | 
|  | 1189 |  | 
|  | 1190 | // Update the in-memory value. | 
|  | 1191 | current_bytes_downloaded_[source] = current_bytes_downloaded; | 
|  | 1192 |  | 
|  | 1193 | string prefs_key = GetPrefsKey(kPrefsCurrentBytesDownloaded, source); | 
|  | 1194 | prefs_->SetInt64(prefs_key, current_bytes_downloaded); | 
|  | 1195 | LOG_IF(INFO, log) << "Current bytes downloaded for " | 
|  | 1196 | << utils::ToString(source) << " = " | 
|  | 1197 | << GetCurrentBytesDownloaded(source); | 
|  | 1198 | } | 
|  | 1199 |  | 
|  | 1200 | void PayloadState::LoadTotalBytesDownloaded(DownloadSource source) { | 
|  | 1201 | string key = GetPrefsKey(kPrefsTotalBytesDownloaded, source); | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1202 | SetTotalBytesDownloaded(source, GetPersistedValue(key, prefs_), true); | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 1203 | } | 
|  | 1204 |  | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1205 | void PayloadState::SetTotalBytesDownloaded(DownloadSource source, | 
|  | 1206 | uint64_t total_bytes_downloaded, | 
|  | 1207 | bool log) { | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 1208 | if (source >= kNumDownloadSources) | 
|  | 1209 | return; | 
|  | 1210 |  | 
|  | 1211 | // Update the in-memory value. | 
|  | 1212 | total_bytes_downloaded_[source] = total_bytes_downloaded; | 
|  | 1213 |  | 
|  | 1214 | // Persist. | 
|  | 1215 | string prefs_key = GetPrefsKey(kPrefsTotalBytesDownloaded, source); | 
|  | 1216 | prefs_->SetInt64(prefs_key, total_bytes_downloaded); | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1217 | LOG_IF(INFO, log) << "Total bytes downloaded for " << utils::ToString(source) | 
|  | 1218 | << " = " << GetTotalBytesDownloaded(source); | 
| Jay Srinivasan | 19409b7 | 2013-04-12 19:23:36 -0700 | [diff] [blame] | 1219 | } | 
|  | 1220 |  | 
| David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 1221 | void PayloadState::LoadNumResponsesSeen() { | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1222 | SetNumResponsesSeen(GetPersistedValue(kPrefsNumResponsesSeen, prefs_)); | 
| David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 1223 | } | 
|  | 1224 |  | 
|  | 1225 | void PayloadState::SetNumResponsesSeen(int num_responses_seen) { | 
| David Zeuthen | a573d6f | 2013-06-14 16:13:36 -0700 | [diff] [blame] | 1226 | num_responses_seen_ = num_responses_seen; | 
|  | 1227 | LOG(INFO) << "Num Responses Seen = " << num_responses_seen_; | 
|  | 1228 | prefs_->SetInt64(kPrefsNumResponsesSeen, num_responses_seen_); | 
|  | 1229 | } | 
|  | 1230 |  | 
| Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1231 | void PayloadState::ComputeCandidateUrls() { | 
| Chris Sosa | f7d8004 | 2013-08-22 16:45:17 -0700 | [diff] [blame] | 1232 | bool http_url_ok = true; | 
| Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1233 |  | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1234 | if (SystemState::Get()->hardware()->IsOfficialBuild()) { | 
|  | 1235 | const policy::DevicePolicy* policy = SystemState::Get()->device_policy(); | 
| Chris Sosa | f7d8004 | 2013-08-22 16:45:17 -0700 | [diff] [blame] | 1236 | if (policy && policy->GetHttpDownloadsEnabled(&http_url_ok) && !http_url_ok) | 
| Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1237 | LOG(INFO) << "Downloads via HTTP Url are not enabled by device policy"; | 
|  | 1238 | } else { | 
|  | 1239 | LOG(INFO) << "Allowing HTTP downloads for unofficial builds"; | 
|  | 1240 | http_url_ok = true; | 
|  | 1241 | } | 
|  | 1242 |  | 
|  | 1243 | candidate_urls_.clear(); | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 1244 | for (const auto& package : response_.packages) { | 
|  | 1245 | candidate_urls_.emplace_back(); | 
|  | 1246 | for (const string& candidate_url : package.payload_urls) { | 
|  | 1247 | if (base::StartsWith( | 
|  | 1248 | candidate_url, "http://", base::CompareCase::INSENSITIVE_ASCII) && | 
|  | 1249 | !http_url_ok) { | 
|  | 1250 | continue; | 
|  | 1251 | } | 
|  | 1252 | candidate_urls_.back().push_back(candidate_url); | 
|  | 1253 | LOG(INFO) << "Candidate Url" << (candidate_urls_.back().size() - 1) | 
|  | 1254 | << ": " << candidate_url; | 
| Alex Vakulenko | 0103c36 | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 1255 | } | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 1256 | LOG(INFO) << "Found " << candidate_urls_.back().size() << " candidate URLs " | 
|  | 1257 | << "out of " << package.payload_urls.size() | 
|  | 1258 | << " URLs supplied in package " << candidate_urls_.size() - 1; | 
| Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1259 | } | 
| Jay Srinivasan | 53173b9 | 2013-05-17 17:13:01 -0700 | [diff] [blame] | 1260 | } | 
|  | 1261 |  | 
| David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 1262 | void PayloadState::UpdateEngineStarted() { | 
| David Zeuthen | 4e1d149 | 2014-04-25 13:12:27 -0700 | [diff] [blame] | 1263 | // Flush previous state from abnormal attempt failure, if any. | 
|  | 1264 | ReportAndClearPersistedAttemptMetrics(); | 
|  | 1265 |  | 
| Alex Deymo | 569c424 | 2013-07-24 12:01:01 -0700 | [diff] [blame] | 1266 | // Avoid the UpdateEngineStarted actions if this is not the first time we | 
|  | 1267 | // run the update engine since reboot. | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1268 | if (!SystemState::Get()->system_rebooted()) | 
| Alex Deymo | 569c424 | 2013-07-24 12:01:01 -0700 | [diff] [blame] | 1269 | return; | 
|  | 1270 |  | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1271 | // Report time_to_reboot if we booted into a new update. | 
|  | 1272 | metrics_utils::LoadAndReportTimeToReboot( | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1273 | SystemState::Get()->metrics_reporter(), | 
|  | 1274 | prefs_, | 
|  | 1275 | SystemState::Get()->clock()); | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1276 | prefs_->Delete(kPrefsSystemUpdatedMarker); | 
|  | 1277 |  | 
| Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1278 | // Check if it is needed to send metrics about a failed reboot into a new | 
|  | 1279 | // version. | 
|  | 1280 | ReportFailedBootIfNeeded(); | 
|  | 1281 | } | 
|  | 1282 |  | 
|  | 1283 | void PayloadState::ReportFailedBootIfNeeded() { | 
|  | 1284 | // If the kPrefsTargetVersionInstalledFrom is present, a successfully applied | 
|  | 1285 | // payload was marked as ready immediately before the last reboot, and we | 
|  | 1286 | // need to check if such payload successfully rebooted or not. | 
|  | 1287 | if (prefs_->Exists(kPrefsTargetVersionInstalledFrom)) { | 
| Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 1288 | int64_t installed_from = 0; | 
|  | 1289 | if (!prefs_->GetInt64(kPrefsTargetVersionInstalledFrom, &installed_from)) { | 
| Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1290 | LOG(ERROR) << "Error reading TargetVersionInstalledFrom on reboot."; | 
|  | 1291 | return; | 
|  | 1292 | } | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 1293 | // Old Chrome OS devices will write 2 or 4 in this setting, with the | 
|  | 1294 | // partition number. We are now using slot numbers (0 or 1) instead, so | 
|  | 1295 | // the following comparison will not match if we are comparing an old | 
|  | 1296 | // partition number against a new slot number, which is the correct outcome | 
|  | 1297 | // since we successfully booted the new update in that case. If the boot | 
|  | 1298 | // failed, we will read this value from the same version, so it will always | 
|  | 1299 | // be compatible. | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1300 | if (installed_from == | 
|  | 1301 | SystemState::Get()->boot_control()->GetCurrentSlot()) { | 
| Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1302 | // A reboot was pending, but the chromebook is again in the same | 
|  | 1303 | // BootDevice where the update was installed from. | 
|  | 1304 | int64_t target_attempt; | 
|  | 1305 | if (!prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt)) { | 
|  | 1306 | LOG(ERROR) << "Error reading TargetVersionAttempt when " | 
|  | 1307 | "TargetVersionInstalledFrom was present."; | 
|  | 1308 | target_attempt = 1; | 
|  | 1309 | } | 
|  | 1310 |  | 
|  | 1311 | // Report the UMA metric of the current boot failure. | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1312 | SystemState::Get()->metrics_reporter()->ReportFailedUpdateCount( | 
| Tianjie Xu | 282aa1f | 2017-09-05 13:42:45 -0700 | [diff] [blame] | 1313 | target_attempt); | 
| Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1314 | } else { | 
|  | 1315 | prefs_->Delete(kPrefsTargetVersionAttempt); | 
|  | 1316 | prefs_->Delete(kPrefsTargetVersionUniqueId); | 
|  | 1317 | } | 
|  | 1318 | prefs_->Delete(kPrefsTargetVersionInstalledFrom); | 
|  | 1319 | } | 
|  | 1320 | } | 
|  | 1321 |  | 
|  | 1322 | void PayloadState::ExpectRebootInNewVersion(const string& target_version_uid) { | 
|  | 1323 | // Expect to boot into the new partition in the next reboot setting the | 
|  | 1324 | // TargetVersion* flags in the Prefs. | 
|  | 1325 | string stored_target_version_uid; | 
|  | 1326 | string target_version_id; | 
|  | 1327 | string target_partition; | 
|  | 1328 | int64_t target_attempt; | 
|  | 1329 |  | 
|  | 1330 | if (prefs_->Exists(kPrefsTargetVersionUniqueId) && | 
|  | 1331 | prefs_->GetString(kPrefsTargetVersionUniqueId, | 
|  | 1332 | &stored_target_version_uid) && | 
|  | 1333 | stored_target_version_uid == target_version_uid) { | 
|  | 1334 | if (!prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt)) | 
|  | 1335 | target_attempt = 0; | 
|  | 1336 | } else { | 
|  | 1337 | prefs_->SetString(kPrefsTargetVersionUniqueId, target_version_uid); | 
|  | 1338 | target_attempt = 0; | 
|  | 1339 | } | 
|  | 1340 | prefs_->SetInt64(kPrefsTargetVersionAttempt, target_attempt + 1); | 
|  | 1341 |  | 
| Alex Vakulenko | 4f5b144 | 2014-02-21 12:19:44 -0800 | [diff] [blame] | 1342 | prefs_->SetInt64(kPrefsTargetVersionInstalledFrom, | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1343 | SystemState::Get()->boot_control()->GetCurrentSlot()); | 
| Alex Deymo | 4243291 | 2013-07-12 20:21:15 -0700 | [diff] [blame] | 1344 | } | 
|  | 1345 |  | 
|  | 1346 | void PayloadState::ResetUpdateStatus() { | 
|  | 1347 | // Remove the TargetVersionInstalledFrom pref so that if the machine is | 
|  | 1348 | // rebooted the next boot is not flagged as failed to rebooted into the | 
|  | 1349 | // new applied payload. | 
|  | 1350 | prefs_->Delete(kPrefsTargetVersionInstalledFrom); | 
|  | 1351 |  | 
|  | 1352 | // Also decrement the attempt number if it exists. | 
|  | 1353 | int64_t target_attempt; | 
|  | 1354 | if (prefs_->GetInt64(kPrefsTargetVersionAttempt, &target_attempt)) | 
| Alex Deymo | 763e7db | 2015-08-27 21:08:08 -0700 | [diff] [blame] | 1355 | prefs_->SetInt64(kPrefsTargetVersionAttempt, target_attempt - 1); | 
| David Zeuthen | e4c58bf | 2013-06-18 17:26:50 -0700 | [diff] [blame] | 1356 | } | 
|  | 1357 |  | 
| David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1358 | int PayloadState::GetP2PNumAttempts() { | 
|  | 1359 | return p2p_num_attempts_; | 
|  | 1360 | } | 
|  | 1361 |  | 
|  | 1362 | void PayloadState::SetP2PNumAttempts(int value) { | 
|  | 1363 | p2p_num_attempts_ = value; | 
|  | 1364 | LOG(INFO) << "p2p Num Attempts = " << p2p_num_attempts_; | 
| David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1365 | prefs_->SetInt64(kPrefsP2PNumAttempts, value); | 
|  | 1366 | } | 
|  | 1367 |  | 
|  | 1368 | void PayloadState::LoadP2PNumAttempts() { | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1369 | SetP2PNumAttempts(GetPersistedValue(kPrefsP2PNumAttempts, prefs_)); | 
| David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1370 | } | 
|  | 1371 |  | 
|  | 1372 | Time PayloadState::GetP2PFirstAttemptTimestamp() { | 
|  | 1373 | return p2p_first_attempt_timestamp_; | 
|  | 1374 | } | 
|  | 1375 |  | 
|  | 1376 | void PayloadState::SetP2PFirstAttemptTimestamp(const Time& time) { | 
|  | 1377 | p2p_first_attempt_timestamp_ = time; | 
|  | 1378 | LOG(INFO) << "p2p First Attempt Timestamp = " | 
|  | 1379 | << utils::ToString(p2p_first_attempt_timestamp_); | 
| David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1380 | int64_t stored_value = time.ToInternalValue(); | 
|  | 1381 | prefs_->SetInt64(kPrefsP2PFirstAttemptTimestamp, stored_value); | 
|  | 1382 | } | 
|  | 1383 |  | 
|  | 1384 | void PayloadState::LoadP2PFirstAttemptTimestamp() { | 
| Tianjie Xu | 90aaa10 | 2017-10-10 17:39:03 -0700 | [diff] [blame] | 1385 | int64_t stored_value = | 
|  | 1386 | GetPersistedValue(kPrefsP2PFirstAttemptTimestamp, prefs_); | 
| David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1387 | Time stored_time = Time::FromInternalValue(stored_value); | 
|  | 1388 | SetP2PFirstAttemptTimestamp(stored_time); | 
|  | 1389 | } | 
|  | 1390 |  | 
|  | 1391 | void PayloadState::P2PNewAttempt() { | 
| David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1392 | // Set timestamp, if it hasn't been set already | 
|  | 1393 | if (p2p_first_attempt_timestamp_.is_null()) { | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1394 | SetP2PFirstAttemptTimestamp( | 
|  | 1395 | SystemState::Get()->clock()->GetWallclockTime()); | 
| David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1396 | } | 
|  | 1397 | // Increase number of attempts | 
|  | 1398 | SetP2PNumAttempts(GetP2PNumAttempts() + 1); | 
|  | 1399 | } | 
|  | 1400 |  | 
|  | 1401 | bool PayloadState::P2PAttemptAllowed() { | 
|  | 1402 | if (p2p_num_attempts_ > kMaxP2PAttempts) { | 
|  | 1403 | LOG(INFO) << "Number of p2p attempts is " << p2p_num_attempts_ | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1404 | << " which is greater than " << kMaxP2PAttempts | 
| David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1405 | << " - disallowing p2p."; | 
|  | 1406 | return false; | 
|  | 1407 | } | 
|  | 1408 |  | 
|  | 1409 | if (!p2p_first_attempt_timestamp_.is_null()) { | 
| Amin Hassani | 538bd59 | 2020-11-04 20:46:08 -0800 | [diff] [blame] | 1410 | Time now = SystemState::Get()->clock()->GetWallclockTime(); | 
| David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1411 | TimeDelta time_spent_attempting_p2p = now - p2p_first_attempt_timestamp_; | 
|  | 1412 | if (time_spent_attempting_p2p.InSeconds() < 0) { | 
|  | 1413 | LOG(ERROR) << "Time spent attempting p2p is negative" | 
|  | 1414 | << " - disallowing p2p."; | 
|  | 1415 | return false; | 
|  | 1416 | } | 
|  | 1417 | if (time_spent_attempting_p2p.InSeconds() > kMaxP2PAttemptTimeSeconds) { | 
|  | 1418 | LOG(INFO) << "Time spent attempting p2p is " | 
|  | 1419 | << utils::FormatTimeDelta(time_spent_attempting_p2p) | 
|  | 1420 | << " which is greater than " | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 1421 | << utils::FormatTimeDelta( | 
|  | 1422 | TimeDelta::FromSeconds(kMaxP2PAttemptTimeSeconds)) | 
| David Zeuthen | dcba809 | 2013-08-06 12:16:35 -0700 | [diff] [blame] | 1423 | << " - disallowing p2p."; | 
|  | 1424 | return false; | 
|  | 1425 | } | 
|  | 1426 | } | 
|  | 1427 |  | 
|  | 1428 | return true; | 
|  | 1429 | } | 
|  | 1430 |  | 
| Sen Jiang | 0affc2c | 2017-02-10 15:55:05 -0800 | [diff] [blame] | 1431 | int64_t PayloadState::GetPayloadSize() { | 
|  | 1432 | int64_t payload_size = 0; | 
|  | 1433 | for (const auto& package : response_.packages) | 
|  | 1434 | payload_size += package.size; | 
|  | 1435 | return payload_size; | 
|  | 1436 | } | 
|  | 1437 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1438 | }  // namespace chromeos_update_engine |