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