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