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