update_attempter_android.cc revision 90aaa109e38f147ec4bfc772439d9949f1b237c0
15e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo//
25e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// Copyright (C) 2016 The Android Open Source Project
35e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo//
45e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// Licensed under the Apache License, Version 2.0 (the "License");
55e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// you may not use this file except in compliance with the License.
65e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// You may obtain a copy of the License at
75e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo//
85e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo//      http://www.apache.org/licenses/LICENSE-2.0
95e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo//
105e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// Unless required by applicable law or agreed to in writing, software
115e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// distributed under the License is distributed on an "AS IS" BASIS,
125e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// See the License for the specific language governing permissions and
145e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// limitations under the License.
155e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo//
165e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
175e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/update_attempter_android.h"
185e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
195e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <algorithm>
20218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo#include <map>
2190aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu#include <memory>
225e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <utility>
235e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu#include <android-base/properties.h>
255e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <base/bind.h>
265e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <base/logging.h>
27218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo#include <base/strings/string_number_conversions.h>
285e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <brillo/bind_lambda.h>
292703ef4466066d64d8021904e233b120f38c0272Sen Jiang#include <brillo/data_encoding.h>
305e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <brillo/message_loops/message_loop.h>
31218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo#include <brillo/strings/string_utils.h>
325e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
335e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/common/constants.h"
342c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo#include "update_engine/common/file_fetcher.h"
355e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/common/utils.h"
3603a4de7dc77b058987fc0670f4f67ba10ff31bc8Alex Deymo#include "update_engine/daemon_state_interface.h"
371b66114d367d1b85b4397bb559e274dbd0ebcd31Tianjie Xu#include "update_engine/metrics_reporter_android.h"
381b66114d367d1b85b4397bb559e274dbd0ebcd31Tianjie Xu#include "update_engine/metrics_utils.h"
3987792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo#include "update_engine/network_selector.h"
405e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/payload_consumer/download_action.h"
415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/payload_consumer/filesystem_verifier_action.h"
425e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/payload_consumer/postinstall_runner_action.h"
433b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo#include "update_engine/update_status_utils.h"
445e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4514c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#ifndef _UE_SIDELOAD
4614c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo// Do not include support for external HTTP(s) urls when building
4714c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo// update_engine_sideload.
4814c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#include "update_engine/libcurl_http_fetcher.h"
4914c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#endif
5014c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo
515e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing base::Bind;
5290aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xuusing base::Time;
535e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing base::TimeDelta;
545e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing base::TimeTicks;
555e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing std::shared_ptr;
565e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing std::string;
575e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing std::vector;
587f92e2b7060cb35719acf96ef96a4e273e165bc5Aaron Woodusing update_engine::UpdateEngineStatus;
595e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
605e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymonamespace chromeos_update_engine {
615e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
625e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymonamespace {
635e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
640d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo// Minimum threshold to broadcast an status update in progress and time.
650d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymoconst double kBroadcastThresholdProgress = 0.01;  // 1%
660d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymoconst int kBroadcastThresholdSeconds = 10;
670d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo
685e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymoconst char* const kErrorDomain = "update_engine";
695e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// TODO(deymo): Convert the different errors to a numeric value to report them
705e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// back on the service error.
715e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymoconst char* const kGenericError = "generic_error";
725e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
735e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// Log and set the error on the passed ErrorPtr.
745e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool LogAndSetError(brillo::ErrorPtr* error,
755e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                    const tracked_objects::Location& location,
765e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                    const string& reason) {
775e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  brillo::Error::AddTo(error, location, kErrorDomain, kGenericError, reason);
785e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  LOG(ERROR) << "Replying with failure: " << location.ToString() << ": "
795e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo             << reason;
805e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  return false;
815e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
825e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
835e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}  // namespace
845e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
855e3ea278d9e771be3b51bd72985ad582678baddcAlex DeymoUpdateAttempterAndroid::UpdateAttempterAndroid(
8603a4de7dc77b058987fc0670f4f67ba10ff31bc8Alex Deymo    DaemonStateInterface* daemon_state,
875e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    PrefsInterface* prefs,
885e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    BootControlInterface* boot_control,
895e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    HardwareInterface* hardware)
905e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    : daemon_state_(daemon_state),
915e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      prefs_(prefs),
925e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      boot_control_(boot_control),
935e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      hardware_(hardware),
941b66114d367d1b85b4397bb559e274dbd0ebcd31Tianjie Xu      processor_(new ActionProcessor()),
9590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      clock_(new Clock()),
961b66114d367d1b85b4397bb559e274dbd0ebcd31Tianjie Xu      metrics_reporter_(new MetricsReporterAndroid()) {
9787792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo  network_selector_ = network::CreateNetworkSelector();
985e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
995e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1005e3ea278d9e771be3b51bd72985ad582678baddcAlex DeymoUpdateAttempterAndroid::~UpdateAttempterAndroid() {
1015e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Release ourselves as the ActionProcessor's delegate to prevent
1025e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // re-scheduling the updates due to the processing stopped.
1035e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  processor_->set_delegate(nullptr);
1045e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
1055e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1065e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::Init() {
1075e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // In case of update_engine restart without a reboot we need to restore the
1085e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // reboot needed state.
10990aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  if (UpdateCompletedOnThisBoot()) {
1100e061aebc651e041b6d1d8a3a91834feb87e0d62Alex Deymo    SetStatusAndNotify(UpdateStatus::UPDATED_NEED_REBOOT);
11190aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  } else {
1120e061aebc651e041b6d1d8a3a91834feb87e0d62Alex Deymo    SetStatusAndNotify(UpdateStatus::IDLE);
11390aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    UpdatePrefsAndReportUpdateMetricsOnReboot();
11490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  }
1155e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
1165e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1175e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::ApplyPayload(
1185e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    const string& payload_url,
1195e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    int64_t payload_offset,
1205e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    int64_t payload_size,
1215e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    const vector<string>& key_value_pair_headers,
1225e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    brillo::ErrorPtr* error) {
1235e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (status_ == UpdateStatus::UPDATED_NEED_REBOOT) {
1245e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return LogAndSetError(
1255e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo        error, FROM_HERE, "An update already applied, waiting for reboot");
1265e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
1275e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (ongoing_update_) {
1285e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return LogAndSetError(
1295e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo        error, FROM_HERE, "Already processing an update, cancel it first.");
1305e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
1315e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  DCHECK(status_ == UpdateStatus::IDLE);
1325e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
133218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  std::map<string, string> headers;
134218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  for (const string& key_value_pair : key_value_pair_headers) {
135218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    string key;
136218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    string value;
137218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    if (!brillo::string_utils::SplitAtFirst(
138218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo            key_value_pair, "=", &key, &value, false)) {
139218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo      return LogAndSetError(
140218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo          error, FROM_HERE, "Passed invalid header: " + key_value_pair);
141218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    }
142218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    if (!headers.emplace(key, value).second)
143218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo      return LogAndSetError(error, FROM_HERE, "Passed repeated key: " + key);
144218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  }
145218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo
146218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  // Unique identifier for the payload. An empty string means that the payload
147218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  // can't be resumed.
148218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  string payload_id = (headers[kPayloadPropertyFileHash] +
149218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo                       headers[kPayloadPropertyMetadataHash]);
1505e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1515e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Setup the InstallPlan based on the request.
1525e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_ = InstallPlan();
1535e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1545e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.download_url = payload_url;
1555e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.version = "";
1560fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo  base_offset_ = payload_offset;
1570affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang  InstallPlan::Payload payload;
1580affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang  payload.size = payload_size;
1590affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang  if (!payload.size) {
160218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    if (!base::StringToUint64(headers[kPayloadPropertyFileSize],
1610affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang                              &payload.size)) {
1620affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang      payload.size = 0;
163218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    }
164218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  }
1652703ef4466066d64d8021904e233b120f38c0272Sen Jiang  if (!brillo::data_encoding::Base64Decode(headers[kPayloadPropertyFileHash],
1660affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang                                           &payload.hash)) {
1672703ef4466066d64d8021904e233b120f38c0272Sen Jiang    LOG(WARNING) << "Unable to decode base64 file hash: "
1682703ef4466066d64d8021904e233b120f38c0272Sen Jiang                 << headers[kPayloadPropertyFileHash];
1692703ef4466066d64d8021904e233b120f38c0272Sen Jiang  }
170218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  if (!base::StringToUint64(headers[kPayloadPropertyMetadataSize],
1710affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang                            &payload.metadata_size)) {
1720affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang    payload.metadata_size = 0;
173218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  }
174cdd5206c6507fa5c70344c269053d30777f7d674Sen Jiang  // The |payload.type| is not used anymore since minor_version 3.
175cdd5206c6507fa5c70344c269053d30777f7d674Sen Jiang  payload.type = InstallPayloadType::kUnknown;
1760affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang  install_plan_.payloads.push_back(payload);
1770affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang
1785e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // The |public_key_rsa| key would override the public key stored on disk.
1795e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.public_key_rsa = "";
1805e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1815e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.hash_checks_mandatory = hardware_->IsOfficialBuild();
1825e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.is_resume = !payload_id.empty() &&
1835e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                            DeltaPerformer::CanResumeUpdate(prefs_, payload_id);
1845e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (!install_plan_.is_resume) {
1855e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    if (!DeltaPerformer::ResetUpdateProgress(prefs_, false)) {
1865e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      LOG(WARNING) << "Unable to reset the update progress.";
1875e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    }
1885e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    if (!prefs_->SetString(kPrefsUpdateCheckResponseHash, payload_id)) {
1895e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      LOG(WARNING) << "Unable to save the update check response hash.";
1905e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    }
1915e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
1925e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.source_slot = boot_control_->GetCurrentSlot();
1935e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0;
194fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo
195fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo  int data_wipe = 0;
196fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo  install_plan_.powerwash_required =
197fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo      base::StringToInt(headers[kPayloadPropertyPowerwash], &data_wipe) &&
198fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo      data_wipe != 0;
1995e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
20087792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo  NetworkId network_id = kDefaultNetworkId;
20187792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo  if (!headers[kPayloadPropertyNetworkId].empty()) {
20287792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo    if (!base::StringToUint64(headers[kPayloadPropertyNetworkId],
20387792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo                              &network_id)) {
20487792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo      return LogAndSetError(
20587792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo          error,
20687792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo          FROM_HERE,
20787792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo          "Invalid network_id: " + headers[kPayloadPropertyNetworkId]);
20887792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo    }
20987792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo    if (!network_selector_->SetProcessNetwork(network_id)) {
210cbd37c68ca69ceec25a9609c0c36fa30ee707f6dSen Jiang      return LogAndSetError(
211cbd37c68ca69ceec25a9609c0c36fa30ee707f6dSen Jiang          error,
212cbd37c68ca69ceec25a9609c0c36fa30ee707f6dSen Jiang          FROM_HERE,
213cbd37c68ca69ceec25a9609c0c36fa30ee707f6dSen Jiang          "Unable to set network_id: " + headers[kPayloadPropertyNetworkId]);
21487792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo    }
21587792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo  }
21687792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo
2175e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  LOG(INFO) << "Using this install plan:";
2185e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.Dump();
2195e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2202c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo  BuildUpdateActions(payload_url);
221fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo  // Setup extra headers.
222fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo  HttpFetcher* fetcher = download_action_->http_fetcher();
223fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo  if (!headers[kPayloadPropertyAuthorization].empty())
224fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo    fetcher->SetHeader("Authorization", headers[kPayloadPropertyAuthorization]);
225fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo  if (!headers[kPayloadPropertyUserAgent].empty())
226fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo    fetcher->SetHeader("User-Agent", headers[kPayloadPropertyUserAgent]);
227fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo
2285e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE);
229f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  ongoing_update_ = true;
2305e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2315e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Just in case we didn't update boot flags yet, make sure they're updated
2325e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // before any update processing starts. This will start the update process.
2335e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  UpdateBootFlags();
23490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu
23590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  UpdatePrefsOnUpdateStart(install_plan_.is_resume);
23690aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  // TODO(xunchang) report the metrics for unresumable updates
23790aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu
2385e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  return true;
2395e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
2405e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::SuspendUpdate(brillo::ErrorPtr* error) {
242f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  if (!ongoing_update_)
243f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo    return LogAndSetError(error, FROM_HERE, "No ongoing update to suspend.");
244f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  processor_->SuspendProcessing();
245f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  return true;
2465e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
2475e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2485e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::ResumeUpdate(brillo::ErrorPtr* error) {
249f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  if (!ongoing_update_)
250f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo    return LogAndSetError(error, FROM_HERE, "No ongoing update to resume.");
251f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  processor_->ResumeProcessing();
252f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  return true;
2535e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
2545e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2555e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::CancelUpdate(brillo::ErrorPtr* error) {
256f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  if (!ongoing_update_)
2575e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return LogAndSetError(error, FROM_HERE, "No ongoing update to cancel.");
258f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  processor_->StopProcessing();
259f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  return true;
2605e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
2615e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2623b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymobool UpdateAttempterAndroid::ResetStatus(brillo::ErrorPtr* error) {
2633b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo  LOG(INFO) << "Attempting to reset state from "
2643b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo            << UpdateStatusToString(status_) << " to UpdateStatus::IDLE";
2653b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2663b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo  switch (status_) {
2673b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo    case UpdateStatus::IDLE:
2683b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      return true;
2693b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2703b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo    case UpdateStatus::UPDATED_NEED_REBOOT:  {
2713b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      // Remove the reboot marker so that if the machine is rebooted
2723b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      // after resetting to idle state, it doesn't go back to
2733b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      // UpdateStatus::UPDATED_NEED_REBOOT state.
2743b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      bool ret_value = prefs_->Delete(kPrefsUpdateCompletedOnBootId);
27590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      ClearMetricsPrefs();
2763b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2773b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      // Update the boot flags so the current slot has higher priority.
2783b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      if (!boot_control_->SetActiveBootSlot(boot_control_->GetCurrentSlot()))
2793b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo        ret_value = false;
2803b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2815259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo      // Mark the current slot as successful again, since marking it as active
2825259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo      // may reset the successful bit. We ignore the result of whether marking
2835259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo      // the current slot as successful worked.
2845259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo      if (!boot_control_->MarkBootSuccessfulAsync(Bind([](bool successful){})))
2855259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo        ret_value = false;
2865259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo
2873b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      if (!ret_value) {
2883b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo        return LogAndSetError(
2893b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo            error,
2903b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo            FROM_HERE,
2913b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo            "Failed to reset the status to ");
2923b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      }
2933b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2943b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      SetStatusAndNotify(UpdateStatus::IDLE);
2953b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      LOG(INFO) << "Reset status successful";
2963b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      return true;
2973b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo    }
2983b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2993b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo    default:
3003b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      return LogAndSetError(
3013b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo          error,
3023b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo          FROM_HERE,
3033b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo          "Reset not allowed in this state. Cancel the ongoing update first");
3043b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo  }
3053b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo}
3063b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
3075e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::ProcessingDone(const ActionProcessor* processor,
3085e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                            ErrorCode code) {
3095e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  LOG(INFO) << "Processing Done.";
3105e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3115990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo  switch (code) {
3125990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kSuccess:
3135990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      // Update succeeded.
3145990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      WriteUpdateCompletedMarker();
3155990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
3165990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      DeltaPerformer::ResetUpdateProgress(prefs_, false);
3175990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo
3185990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      LOG(INFO) << "Update successfully applied, waiting to reboot.";
3195990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      break;
3205990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo
3215990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kFilesystemCopierError:
3225990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kNewRootfsVerificationError:
3235990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kNewKernelVerificationError:
3245990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kFilesystemVerifierError:
3255990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kDownloadStateInitializationError:
3265990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      // Reset the ongoing update for these errors so it starts from the
3275990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      // beginning next time.
3285990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      DeltaPerformer::ResetUpdateProgress(prefs_, false);
3295990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      LOG(INFO) << "Resetting update progress.";
3305990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      break;
3315e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3325990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    default:
3335990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      // Ignore all other error codes.
3345990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      break;
33503a4de7dc77b058987fc0670f4f67ba10ff31bc8Alex Deymo  }
3365e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3375e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  TerminateUpdateAndNotify(code);
3385e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3395e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3405e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::ProcessingStopped(
3415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    const ActionProcessor* processor) {
3425e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  TerminateUpdateAndNotify(ErrorCode::kUserCanceled);
3435e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3445e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3455e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::ActionCompleted(ActionProcessor* processor,
3465e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                             AbstractAction* action,
3475e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                             ErrorCode code) {
3485e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Reset download progress regardless of whether or not the download
3495e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // action succeeded.
3505e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  const string type = action->Type();
3515e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (type == DownloadAction::StaticType()) {
3520d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo    download_progress_ = 0;
3535e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3545e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (code != ErrorCode::kSuccess) {
3555e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    // If an action failed, the ActionProcessor will cancel the whole thing.
3565e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return;
3575e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3585e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (type == DownloadAction::StaticType()) {
3595e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    SetStatusAndNotify(UpdateStatus::FINALIZING);
3605e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3615e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3625e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3635e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::BytesReceived(uint64_t bytes_progressed,
3645e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                           uint64_t bytes_received,
3655e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                           uint64_t total) {
3660d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  double progress = 0;
3675e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (total)
3685e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    progress = static_cast<double>(bytes_received) / static_cast<double>(total);
3690d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  if (status_ != UpdateStatus::DOWNLOADING || bytes_received == total) {
3705e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    download_progress_ = progress;
3715e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    SetStatusAndNotify(UpdateStatus::DOWNLOADING);
3720d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  } else {
3730d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo    ProgressUpdate(progress);
3745e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3755e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3765e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3775e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::ShouldCancel(ErrorCode* cancel_reason) {
3785e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // TODO(deymo): Notify the DownloadAction that it should cancel the update
3795e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // download.
3805e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  return false;
3815e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3825e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3835e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::DownloadComplete() {
3845e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Nothing needs to be done when the download completes.
3855e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3865e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3870d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymovoid UpdateAttempterAndroid::ProgressUpdate(double progress) {
3880d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  // Self throttle based on progress. Also send notifications if progress is
3890d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  // too slow.
3900d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  if (progress == 1.0 ||
3910d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo      progress - download_progress_ >= kBroadcastThresholdProgress ||
3920d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo      TimeTicks::Now() - last_notify_time_ >=
3930d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo          TimeDelta::FromSeconds(kBroadcastThresholdSeconds)) {
3940d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo    download_progress_ = progress;
3950d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo    SetStatusAndNotify(status_);
3960d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  }
3970d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo}
3980d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo
3995e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::UpdateBootFlags() {
4005e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (updated_boot_flags_) {
4015e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    LOG(INFO) << "Already updated boot flags. Skipping.";
4025e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    CompleteUpdateBootFlags(true);
4035e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return;
4045e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
4055e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // This is purely best effort.
4065e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  LOG(INFO) << "Marking booted slot as good.";
4075e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (!boot_control_->MarkBootSuccessfulAsync(
4085e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo          Bind(&UpdateAttempterAndroid::CompleteUpdateBootFlags,
4095e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo               base::Unretained(this)))) {
4105e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    LOG(ERROR) << "Failed to mark current boot as successful.";
4115e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    CompleteUpdateBootFlags(false);
4125e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
4135e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
4145e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4155e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::CompleteUpdateBootFlags(bool successful) {
4165e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  updated_boot_flags_ = true;
4175e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  ScheduleProcessingStart();
4185e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
4195e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4205e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::ScheduleProcessingStart() {
4215e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  LOG(INFO) << "Scheduling an action processor start.";
4225e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  brillo::MessageLoop::current()->PostTask(
423f1cf34849efdc0158fb1987ba74a51f25016c5bdLuis Hector Chavez      FROM_HERE,
424f1cf34849efdc0158fb1987ba74a51f25016c5bdLuis Hector Chavez      Bind([](ActionProcessor* processor) { processor->StartProcessing(); },
425f1cf34849efdc0158fb1987ba74a51f25016c5bdLuis Hector Chavez           base::Unretained(processor_.get())));
4265e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
4275e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4285e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::TerminateUpdateAndNotify(ErrorCode error_code) {
4295e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (status_ == UpdateStatus::IDLE) {
4305e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    LOG(ERROR) << "No ongoing update, but TerminatedUpdate() called.";
4315e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return;
4325e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
4335e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4340d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  download_progress_ = 0;
4355e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  actions_.clear();
4365e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  UpdateStatus new_status =
4375e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      (error_code == ErrorCode::kSuccess ? UpdateStatus::UPDATED_NEED_REBOOT
4385e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                         : UpdateStatus::IDLE);
4395e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  SetStatusAndNotify(new_status);
4405e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  ongoing_update_ = false;
4415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4425e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  for (auto observer : daemon_state_->service_observers())
4435e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    observer->SendPayloadApplicationComplete(error_code);
4441b66114d367d1b85b4397bb559e274dbd0ebcd31Tianjie Xu
44590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  CollectAndReportUpdateMetricsOnUpdateFinished(error_code);
44690aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  ClearMetricsPrefs();
44790aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  if (error_code == ErrorCode::kSuccess) {
44890aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    metrics_utils::SetSystemUpdatedMarker(clock_.get(), prefs_);
44990aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  }
4505e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
4515e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4525e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::SetStatusAndNotify(UpdateStatus status) {
4535e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  status_ = status;
4542d1c87bbd55878c5b4dd93e97a8cc28e715137ecSen Jiang  size_t payload_size =
4552d1c87bbd55878c5b4dd93e97a8cc28e715137ecSen Jiang      install_plan_.payloads.empty() ? 0 : install_plan_.payloads[0].size;
4567f92e2b7060cb35719acf96ef96a4e273e165bc5Aaron Wood  UpdateEngineStatus status_to_send = {.status = status_,
4577f92e2b7060cb35719acf96ef96a4e273e165bc5Aaron Wood                                       .progress = download_progress_,
4587f92e2b7060cb35719acf96ef96a4e273e165bc5Aaron Wood                                       .new_size_bytes = payload_size};
4597f92e2b7060cb35719acf96ef96a4e273e165bc5Aaron Wood
4605e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  for (auto observer : daemon_state_->service_observers()) {
4617f92e2b7060cb35719acf96ef96a4e273e165bc5Aaron Wood    observer->SendStatusUpdate(status_to_send);
4625e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
4635e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  last_notify_time_ = TimeTicks::Now();
4645e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
4655e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4662c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymovoid UpdateAttempterAndroid::BuildUpdateActions(const string& url) {
4675e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  CHECK(!processor_->IsRunning());
4685e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  processor_->set_delegate(this);
4695e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4705e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Actions:
4715e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  shared_ptr<InstallPlanAction> install_plan_action(
4725e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      new InstallPlanAction(install_plan_));
4735e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4742c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo  HttpFetcher* download_fetcher = nullptr;
4752c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo  if (FileFetcher::SupportedUrl(url)) {
4762c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo    DLOG(INFO) << "Using FileFetcher for file URL.";
4772c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo    download_fetcher = new FileFetcher();
4782c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo  } else {
47914c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#ifdef _UE_SIDELOAD
48014c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo    LOG(FATAL) << "Unsupported sideload URI: " << url;
48114c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#else
4822c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo    LibcurlHttpFetcher* libcurl_fetcher =
4832c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo        new LibcurlHttpFetcher(&proxy_resolver_, hardware_);
4842c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo    libcurl_fetcher->set_server_to_check(ServerToCheck::kDownload);
4852c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo    download_fetcher = libcurl_fetcher;
48614c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#endif  // _UE_SIDELOAD
4872c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo  }
4885ae865b431cc041feed82f1db28a38e99dbd631fSen Jiang  shared_ptr<DownloadAction> download_action(
4895ae865b431cc041feed82f1db28a38e99dbd631fSen Jiang      new DownloadAction(prefs_,
4905ae865b431cc041feed82f1db28a38e99dbd631fSen Jiang                         boot_control_,
4915ae865b431cc041feed82f1db28a38e99dbd631fSen Jiang                         hardware_,
4925ae865b431cc041feed82f1db28a38e99dbd631fSen Jiang                         nullptr,             // system_state, not used.
4935ae865b431cc041feed82f1db28a38e99dbd631fSen Jiang                         download_fetcher));  // passes ownership
494fef85fd9ece49941db274559a938fe8b2c5157bfSen Jiang  shared_ptr<FilesystemVerifierAction> filesystem_verifier_action(
495e6e4bb929acd73e57b68a30d1e3a33d76607aec3Sen Jiang      new FilesystemVerifierAction());
4965e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4975e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
498fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo      new PostinstallRunnerAction(boot_control_, hardware_));
4995e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
5005e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  download_action->set_delegate(this);
5015ae865b431cc041feed82f1db28a38e99dbd631fSen Jiang  download_action->set_base_offset(base_offset_);
5025e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  download_action_ = download_action;
503b6eef73e20736b4b6a43611fa732d8bba72ff7ffAlex Deymo  postinstall_runner_action->set_delegate(this);
5045e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
5055e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  actions_.push_back(shared_ptr<AbstractAction>(install_plan_action));
5065e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  actions_.push_back(shared_ptr<AbstractAction>(download_action));
507fef85fd9ece49941db274559a938fe8b2c5157bfSen Jiang  actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
5085e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
5095e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
5105e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Bond them together. We have to use the leaf-types when calling
5115e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // BondActions().
5125e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  BondActions(install_plan_action.get(), download_action.get());
513fef85fd9ece49941db274559a938fe8b2c5157bfSen Jiang  BondActions(download_action.get(), filesystem_verifier_action.get());
514fef85fd9ece49941db274559a938fe8b2c5157bfSen Jiang  BondActions(filesystem_verifier_action.get(),
5155e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo              postinstall_runner_action.get());
5165e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
5175e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Enqueue the actions.
5185e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  for (const shared_ptr<AbstractAction>& action : actions_)
5195e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    processor_->EnqueueAction(action.get());
5205e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
5215e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
5225e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::WriteUpdateCompletedMarker() {
5235e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  string boot_id;
5245e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id));
5255e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  prefs_->SetString(kPrefsUpdateCompletedOnBootId, boot_id);
5265e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  return true;
5275e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
5285e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
5295e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::UpdateCompletedOnThisBoot() {
5305e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // In case of an update_engine restart without a reboot, we stored the boot_id
5315e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // when the update was completed by setting a pref, so we can check whether
5325e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // the last update was on this boot or a previous one.
5335e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  string boot_id;
5345e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id));
5355e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
5365e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  string update_completed_on_boot_id;
5375e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  return (prefs_->Exists(kPrefsUpdateCompletedOnBootId) &&
5385e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo          prefs_->GetString(kPrefsUpdateCompletedOnBootId,
5395e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                            &update_completed_on_boot_id) &&
5405e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo          update_completed_on_boot_id == boot_id);
5415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
5425e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
54390aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu// Collect and report the android metrics when we terminate the update.
54490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xuvoid UpdateAttempterAndroid::CollectAndReportUpdateMetricsOnUpdateFinished(
54590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    ErrorCode error_code) {
54690aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  int64_t attempt_number =
54790aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      metrics_utils::GetPersistedValue(kPrefsPayloadAttemptNumber, prefs_);
54890aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  PayloadType payload_type = kPayloadTypeFull;
54990aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  int64_t payload_size = 0;
55090aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  for (const auto& p : install_plan_.payloads) {
55190aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    if (p.type == InstallPayloadType::kDelta)
55290aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      payload_type = kPayloadTypeDelta;
55390aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    payload_size += p.size;
55490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  }
55590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu
55690aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  metrics::AttemptResult attempt_result =
55790aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      metrics_utils::GetAttemptResult(error_code);
55890aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  Time attempt_start_time = Time::FromInternalValue(
55990aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      metrics_utils::GetPersistedValue(kPrefsUpdateTimestampStart, prefs_));
56090aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  TimeDelta duration_uptime = clock_->GetMonotonicTime() - attempt_start_time;
56190aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu
56290aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  metrics_reporter_->ReportUpdateAttemptMetrics(
56390aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      nullptr,  // system_state
56490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      static_cast<int>(attempt_number),
56590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      payload_type,
56690aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      TimeDelta(),
56790aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      duration_uptime,
56890aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      payload_size,
56990aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      attempt_result,
57090aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      error_code);
57190aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu
57290aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  if (error_code == ErrorCode::kSuccess) {
57390aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    int64_t reboot_count =
57490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu        metrics_utils::GetPersistedValue(kPrefsNumReboots, prefs_);
57590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    string build_version;
57690aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    prefs_->GetString(kPrefsPreviousVersion, &build_version);
57790aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    metrics_reporter_->ReportSuccessfulUpdateMetrics(
57890aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu        static_cast<int>(attempt_number),
57990aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu        0,  // update abandoned count
58090aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu        payload_type,
58190aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu        payload_size,
58290aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu        nullptr,  // num bytes downloaded
58390aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu        0,        // download overhead percentage
58490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu        duration_uptime,
58590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu        static_cast<int>(reboot_count),
58690aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu        0);  // url_switch_count
58790aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  }
58890aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu}
58990aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu
59090aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xuvoid UpdateAttempterAndroid::UpdatePrefsAndReportUpdateMetricsOnReboot() {
59190aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  string current_boot_id;
59290aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  TEST_AND_RETURN(utils::GetBootId(&current_boot_id));
59390aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  // Example: [ro.build.version.incremental]: [4292972]
59490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  string current_version =
59590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      android::base::GetProperty("ro.build.version.incremental", "");
59690aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  TEST_AND_RETURN(!current_version.empty());
59790aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu
59890aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  // If there's no record of previous version (e.g. due to a data wipe), we
59990aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  // save the info of current boot and skip the metrics report.
60090aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  if (!prefs_->Exists(kPrefsPreviousVersion)) {
60190aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    prefs_->SetString(kPrefsBootId, current_boot_id);
60290aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    prefs_->SetString(kPrefsPreviousVersion, current_version);
60390aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    ClearMetricsPrefs();
60490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    return;
60590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  }
60690aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  string previous_version;
60790aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  // update_engine restarted under the same build.
60890aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  // TODO(xunchang) identify and report rollback by checking UpdateMarker.
60990aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  if (prefs_->GetString(kPrefsPreviousVersion, &previous_version) &&
61090aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      previous_version == current_version) {
61190aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    string last_boot_id;
61290aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    bool is_reboot = prefs_->Exists(kPrefsBootId) &&
61390aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu                     (prefs_->GetString(kPrefsBootId, &last_boot_id) &&
61490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu                      last_boot_id != current_boot_id);
61590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    // Increment the reboot number if |kPrefsNumReboots| exists. That pref is
61690aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    // set when we start a new update.
61790aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    if (is_reboot && prefs_->Exists(kPrefsNumReboots)) {
61890aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      prefs_->SetString(kPrefsBootId, current_boot_id);
61990aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      int64_t reboot_count =
62090aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu          metrics_utils::GetPersistedValue(kPrefsNumReboots, prefs_);
62190aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      metrics_utils::SetNumReboots(reboot_count + 1, prefs_);
62290aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    }
62390aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    return;
62490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  }
62590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu
62690aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  // Now that the build version changes, report the update metrics.
62790aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  // TODO(xunchang) check the build version is larger than the previous one.
62890aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  prefs_->SetString(kPrefsBootId, current_boot_id);
62990aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  prefs_->SetString(kPrefsPreviousVersion, current_version);
63090aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu
63190aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  bool previous_attempt_exists = prefs_->Exists(kPrefsPayloadAttemptNumber);
63290aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  // |kPrefsPayloadAttemptNumber| should be cleared upon successful update.
63390aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  if (previous_attempt_exists) {
63490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    metrics_reporter_->ReportAbnormallyTerminatedUpdateAttemptMetrics();
63590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  }
63690aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu
63790aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  metrics_utils::LoadAndReportTimeToReboot(
63890aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu      metrics_reporter_.get(), prefs_, clock_.get());
63990aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  ClearMetricsPrefs();
64090aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu}
64190aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu
64290aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu// Save the update start time. Reset the reboot count and attempt number if the
64390aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu// update isn't a resume; otherwise increment the attempt number.
64490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xuvoid UpdateAttempterAndroid::UpdatePrefsOnUpdateStart(bool is_resume) {
64590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  if (!is_resume) {
64690aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    metrics_utils::SetNumReboots(0, prefs_);
64790aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    metrics_utils::SetPayloadAttemptNumber(1, prefs_);
64890aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  } else {
64990aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    int64_t attempt_number =
65090aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu        metrics_utils::GetPersistedValue(kPrefsPayloadAttemptNumber, prefs_);
65190aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu    metrics_utils::SetPayloadAttemptNumber(attempt_number + 1, prefs_);
65290aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  }
65390aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  Time update_start_time = clock_->GetMonotonicTime();
65490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  metrics_utils::SetUpdateTimestampStart(update_start_time, prefs_);
65590aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu}
65690aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu
65790aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xuvoid UpdateAttempterAndroid::ClearMetricsPrefs() {
65890aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  CHECK(prefs_);
65990aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  prefs_->Delete(kPrefsNumReboots);
66090aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  prefs_->Delete(kPrefsPayloadAttemptNumber);
66190aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  prefs_->Delete(kPrefsSystemUpdatedMarker);
66290aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu  prefs_->Delete(kPrefsUpdateTimestampStart);
66390aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu}
66490aaa109e38f147ec4bfc772439d9949f1b237c0Tianjie Xu
6655e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}  // namespace chromeos_update_engine
666