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