update_attempter_android.cc revision cbd37c68ca69ceec25a9609c0c36fa30ee707f6d
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>
215e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <utility>
225e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
235e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <base/bind.h>
245e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <base/logging.h>
25218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo#include <base/strings/string_number_conversions.h>
265e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <brillo/bind_lambda.h>
272703ef4466066d64d8021904e233b120f38c0272Sen Jiang#include <brillo/data_encoding.h>
285e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include <brillo/message_loops/message_loop.h>
29218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo#include <brillo/strings/string_utils.h>
305e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
315e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/common/constants.h"
322c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo#include "update_engine/common/file_fetcher.h"
335e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/common/utils.h"
3403a4de7dc77b058987fc0670f4f67ba10ff31bc8Alex Deymo#include "update_engine/daemon_state_interface.h"
3587792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo#include "update_engine/network_selector.h"
365e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/payload_consumer/download_action.h"
375e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/payload_consumer/filesystem_verifier_action.h"
385e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/payload_consumer/postinstall_runner_action.h"
393b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo#include "update_engine/update_status_utils.h"
405e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4114c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#ifndef _UE_SIDELOAD
4214c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo// Do not include support for external HTTP(s) urls when building
4314c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo// update_engine_sideload.
4414c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#include "update_engine/libcurl_http_fetcher.h"
4514c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#endif
4614c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo
475e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing base::Bind;
485e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing base::TimeDelta;
495e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing base::TimeTicks;
505e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing std::shared_ptr;
515e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing std::string;
525e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing std::vector;
535e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
545e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymonamespace chromeos_update_engine {
555e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
565e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymonamespace {
575e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
580d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo// Minimum threshold to broadcast an status update in progress and time.
590d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymoconst double kBroadcastThresholdProgress = 0.01;  // 1%
600d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymoconst int kBroadcastThresholdSeconds = 10;
610d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo
625e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymoconst char* const kErrorDomain = "update_engine";
635e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// TODO(deymo): Convert the different errors to a numeric value to report them
645e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// back on the service error.
655e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymoconst char* const kGenericError = "generic_error";
665e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
675e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// Log and set the error on the passed ErrorPtr.
685e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool LogAndSetError(brillo::ErrorPtr* error,
695e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                    const tracked_objects::Location& location,
705e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                    const string& reason) {
715e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  brillo::Error::AddTo(error, location, kErrorDomain, kGenericError, reason);
725e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  LOG(ERROR) << "Replying with failure: " << location.ToString() << ": "
735e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo             << reason;
745e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  return false;
755e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
765e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
775e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}  // namespace
785e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
795e3ea278d9e771be3b51bd72985ad582678baddcAlex DeymoUpdateAttempterAndroid::UpdateAttempterAndroid(
8003a4de7dc77b058987fc0670f4f67ba10ff31bc8Alex Deymo    DaemonStateInterface* daemon_state,
815e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    PrefsInterface* prefs,
825e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    BootControlInterface* boot_control,
835e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    HardwareInterface* hardware)
845e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    : daemon_state_(daemon_state),
855e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      prefs_(prefs),
865e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      boot_control_(boot_control),
875e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      hardware_(hardware),
885e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      processor_(new ActionProcessor()) {
8987792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo  network_selector_ = network::CreateNetworkSelector();
905e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
915e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
925e3ea278d9e771be3b51bd72985ad582678baddcAlex DeymoUpdateAttempterAndroid::~UpdateAttempterAndroid() {
935e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Release ourselves as the ActionProcessor's delegate to prevent
945e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // re-scheduling the updates due to the processing stopped.
955e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  processor_->set_delegate(nullptr);
965e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
975e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
985e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::Init() {
995e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // In case of update_engine restart without a reboot we need to restore the
1005e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // reboot needed state.
1015e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (UpdateCompletedOnThisBoot())
1020e061aebc651e041b6d1d8a3a91834feb87e0d62Alex Deymo    SetStatusAndNotify(UpdateStatus::UPDATED_NEED_REBOOT);
1035e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  else
1040e061aebc651e041b6d1d8a3a91834feb87e0d62Alex Deymo    SetStatusAndNotify(UpdateStatus::IDLE);
1055e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
1065e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1075e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::ApplyPayload(
1085e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    const string& payload_url,
1095e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    int64_t payload_offset,
1105e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    int64_t payload_size,
1115e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    const vector<string>& key_value_pair_headers,
1125e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    brillo::ErrorPtr* error) {
1135e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (status_ == UpdateStatus::UPDATED_NEED_REBOOT) {
1145e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return LogAndSetError(
1155e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo        error, FROM_HERE, "An update already applied, waiting for reboot");
1165e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
1175e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (ongoing_update_) {
1185e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return LogAndSetError(
1195e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo        error, FROM_HERE, "Already processing an update, cancel it first.");
1205e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
1215e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  DCHECK(status_ == UpdateStatus::IDLE);
1225e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
123218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  std::map<string, string> headers;
124218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  for (const string& key_value_pair : key_value_pair_headers) {
125218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    string key;
126218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    string value;
127218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    if (!brillo::string_utils::SplitAtFirst(
128218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo            key_value_pair, "=", &key, &value, false)) {
129218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo      return LogAndSetError(
130218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo          error, FROM_HERE, "Passed invalid header: " + key_value_pair);
131218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    }
132218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    if (!headers.emplace(key, value).second)
133218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo      return LogAndSetError(error, FROM_HERE, "Passed repeated key: " + key);
134218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  }
135218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo
136218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  // Unique identifier for the payload. An empty string means that the payload
137218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  // can't be resumed.
138218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  string payload_id = (headers[kPayloadPropertyFileHash] +
139218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo                       headers[kPayloadPropertyMetadataHash]);
1405e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Setup the InstallPlan based on the request.
1425e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_ = InstallPlan();
1435e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1445e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.download_url = payload_url;
1455e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.version = "";
1460fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo  base_offset_ = payload_offset;
1470affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang  InstallPlan::Payload payload;
1480affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang  payload.size = payload_size;
1490affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang  if (!payload.size) {
150218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    if (!base::StringToUint64(headers[kPayloadPropertyFileSize],
1510affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang                              &payload.size)) {
1520affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang      payload.size = 0;
153218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    }
154218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  }
1552703ef4466066d64d8021904e233b120f38c0272Sen Jiang  if (!brillo::data_encoding::Base64Decode(headers[kPayloadPropertyFileHash],
1560affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang                                           &payload.hash)) {
1572703ef4466066d64d8021904e233b120f38c0272Sen Jiang    LOG(WARNING) << "Unable to decode base64 file hash: "
1582703ef4466066d64d8021904e233b120f38c0272Sen Jiang                 << headers[kPayloadPropertyFileHash];
1592703ef4466066d64d8021904e233b120f38c0272Sen Jiang  }
160218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  if (!base::StringToUint64(headers[kPayloadPropertyMetadataSize],
1610affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang                            &payload.metadata_size)) {
1620affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang    payload.metadata_size = 0;
163218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  }
164cdd5206c6507fa5c70344c269053d30777f7d674Sen Jiang  // The |payload.type| is not used anymore since minor_version 3.
165cdd5206c6507fa5c70344c269053d30777f7d674Sen Jiang  payload.type = InstallPayloadType::kUnknown;
1660affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang  install_plan_.payloads.push_back(payload);
1670affc2c099fac47b999ba39f8f1bacb418e65a97Sen Jiang
1685e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // The |public_key_rsa| key would override the public key stored on disk.
1695e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.public_key_rsa = "";
1705e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1715e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.hash_checks_mandatory = hardware_->IsOfficialBuild();
1725e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.is_resume = !payload_id.empty() &&
1735e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                            DeltaPerformer::CanResumeUpdate(prefs_, payload_id);
1745e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (!install_plan_.is_resume) {
1755e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    if (!DeltaPerformer::ResetUpdateProgress(prefs_, false)) {
1765e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      LOG(WARNING) << "Unable to reset the update progress.";
1775e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    }
1785e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    if (!prefs_->SetString(kPrefsUpdateCheckResponseHash, payload_id)) {
1795e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      LOG(WARNING) << "Unable to save the update check response hash.";
1805e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    }
1815e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
1825e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.source_slot = boot_control_->GetCurrentSlot();
1835e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0;
184fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo
185fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo  int data_wipe = 0;
186fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo  install_plan_.powerwash_required =
187fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo      base::StringToInt(headers[kPayloadPropertyPowerwash], &data_wipe) &&
188fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo      data_wipe != 0;
1895e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
19087792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo  NetworkId network_id = kDefaultNetworkId;
19187792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo  if (!headers[kPayloadPropertyNetworkId].empty()) {
19287792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo    if (!base::StringToUint64(headers[kPayloadPropertyNetworkId],
19387792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo                              &network_id)) {
19487792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo      return LogAndSetError(
19587792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo          error,
19687792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo          FROM_HERE,
19787792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo          "Invalid network_id: " + headers[kPayloadPropertyNetworkId]);
19887792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo    }
19987792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo    if (!network_selector_->SetProcessNetwork(network_id)) {
200cbd37c68ca69ceec25a9609c0c36fa30ee707f6dSen Jiang      return LogAndSetError(
201cbd37c68ca69ceec25a9609c0c36fa30ee707f6dSen Jiang          error,
202cbd37c68ca69ceec25a9609c0c36fa30ee707f6dSen Jiang          FROM_HERE,
203cbd37c68ca69ceec25a9609c0c36fa30ee707f6dSen Jiang          "Unable to set network_id: " + headers[kPayloadPropertyNetworkId]);
20487792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo    }
20587792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo  }
20687792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo
2075e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  LOG(INFO) << "Using this install plan:";
2085e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.Dump();
2095e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2102c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo  BuildUpdateActions(payload_url);
211fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo  // Setup extra headers.
212fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo  HttpFetcher* fetcher = download_action_->http_fetcher();
213fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo  if (!headers[kPayloadPropertyAuthorization].empty())
214fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo    fetcher->SetHeader("Authorization", headers[kPayloadPropertyAuthorization]);
215fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo  if (!headers[kPayloadPropertyUserAgent].empty())
216fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo    fetcher->SetHeader("User-Agent", headers[kPayloadPropertyUserAgent]);
217fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo
2185e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE);
219f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  ongoing_update_ = true;
2205e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2215e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Just in case we didn't update boot flags yet, make sure they're updated
2225e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // before any update processing starts. This will start the update process.
2235e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  UpdateBootFlags();
2245e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  return true;
2255e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
2265e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2275e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::SuspendUpdate(brillo::ErrorPtr* error) {
228f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  if (!ongoing_update_)
229f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo    return LogAndSetError(error, FROM_HERE, "No ongoing update to suspend.");
230f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  processor_->SuspendProcessing();
231f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  return true;
2325e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
2335e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2345e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::ResumeUpdate(brillo::ErrorPtr* error) {
235f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  if (!ongoing_update_)
236f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo    return LogAndSetError(error, FROM_HERE, "No ongoing update to resume.");
237f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  processor_->ResumeProcessing();
238f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  return true;
2395e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
2405e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::CancelUpdate(brillo::ErrorPtr* error) {
242f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  if (!ongoing_update_)
2435e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return LogAndSetError(error, FROM_HERE, "No ongoing update to cancel.");
244f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  processor_->StopProcessing();
245f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  return true;
2465e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
2475e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2483b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymobool UpdateAttempterAndroid::ResetStatus(brillo::ErrorPtr* error) {
2493b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo  LOG(INFO) << "Attempting to reset state from "
2503b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo            << UpdateStatusToString(status_) << " to UpdateStatus::IDLE";
2513b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2523b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo  switch (status_) {
2533b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo    case UpdateStatus::IDLE:
2543b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      return true;
2553b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2563b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo    case UpdateStatus::UPDATED_NEED_REBOOT:  {
2573b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      // Remove the reboot marker so that if the machine is rebooted
2583b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      // after resetting to idle state, it doesn't go back to
2593b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      // UpdateStatus::UPDATED_NEED_REBOOT state.
2603b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      bool ret_value = prefs_->Delete(kPrefsUpdateCompletedOnBootId);
2613b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2623b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      // Update the boot flags so the current slot has higher priority.
2633b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      if (!boot_control_->SetActiveBootSlot(boot_control_->GetCurrentSlot()))
2643b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo        ret_value = false;
2653b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2665259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo      // Mark the current slot as successful again, since marking it as active
2675259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo      // may reset the successful bit. We ignore the result of whether marking
2685259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo      // the current slot as successful worked.
2695259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo      if (!boot_control_->MarkBootSuccessfulAsync(Bind([](bool successful){})))
2705259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo        ret_value = false;
2715259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo
2723b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      if (!ret_value) {
2733b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo        return LogAndSetError(
2743b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo            error,
2753b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo            FROM_HERE,
2763b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo            "Failed to reset the status to ");
2773b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      }
2783b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2793b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      SetStatusAndNotify(UpdateStatus::IDLE);
2803b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      LOG(INFO) << "Reset status successful";
2813b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      return true;
2823b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo    }
2833b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2843b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo    default:
2853b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      return LogAndSetError(
2863b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo          error,
2873b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo          FROM_HERE,
2883b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo          "Reset not allowed in this state. Cancel the ongoing update first");
2893b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo  }
2903b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo}
2913b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2925e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::ProcessingDone(const ActionProcessor* processor,
2935e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                            ErrorCode code) {
2945e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  LOG(INFO) << "Processing Done.";
2955e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2965990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo  switch (code) {
2975990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kSuccess:
2985990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      // Update succeeded.
2995990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      WriteUpdateCompletedMarker();
3005990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
3015990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      DeltaPerformer::ResetUpdateProgress(prefs_, false);
3025990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo
3035990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      LOG(INFO) << "Update successfully applied, waiting to reboot.";
3045990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      break;
3055990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo
3065990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kFilesystemCopierError:
3075990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kNewRootfsVerificationError:
3085990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kNewKernelVerificationError:
3095990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kFilesystemVerifierError:
3105990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kDownloadStateInitializationError:
3115990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      // Reset the ongoing update for these errors so it starts from the
3125990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      // beginning next time.
3135990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      DeltaPerformer::ResetUpdateProgress(prefs_, false);
3145990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      LOG(INFO) << "Resetting update progress.";
3155990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      break;
3165e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3175990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    default:
3185990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      // Ignore all other error codes.
3195990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      break;
32003a4de7dc77b058987fc0670f4f67ba10ff31bc8Alex Deymo  }
3215e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3225e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  TerminateUpdateAndNotify(code);
3235e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3245e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3255e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::ProcessingStopped(
3265e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    const ActionProcessor* processor) {
3275e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  TerminateUpdateAndNotify(ErrorCode::kUserCanceled);
3285e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3295e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3305e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::ActionCompleted(ActionProcessor* processor,
3315e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                             AbstractAction* action,
3325e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                             ErrorCode code) {
3335e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Reset download progress regardless of whether or not the download
3345e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // action succeeded.
3355e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  const string type = action->Type();
3365e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (type == DownloadAction::StaticType()) {
3370d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo    download_progress_ = 0;
3385e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3395e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (code != ErrorCode::kSuccess) {
3405e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    // If an action failed, the ActionProcessor will cancel the whole thing.
3415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return;
3425e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3435e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (type == DownloadAction::StaticType()) {
3445e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    SetStatusAndNotify(UpdateStatus::FINALIZING);
3455e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3465e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3475e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3485e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::BytesReceived(uint64_t bytes_progressed,
3495e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                           uint64_t bytes_received,
3505e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                           uint64_t total) {
3510d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  double progress = 0;
3525e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (total)
3535e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    progress = static_cast<double>(bytes_received) / static_cast<double>(total);
3540d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  if (status_ != UpdateStatus::DOWNLOADING || bytes_received == total) {
3555e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    download_progress_ = progress;
3565e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    SetStatusAndNotify(UpdateStatus::DOWNLOADING);
3570d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  } else {
3580d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo    ProgressUpdate(progress);
3595e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3605e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3615e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3625e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::ShouldCancel(ErrorCode* cancel_reason) {
3635e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // TODO(deymo): Notify the DownloadAction that it should cancel the update
3645e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // download.
3655e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  return false;
3665e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3675e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3685e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::DownloadComplete() {
3695e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Nothing needs to be done when the download completes.
3705e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3715e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3720d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymovoid UpdateAttempterAndroid::ProgressUpdate(double progress) {
3730d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  // Self throttle based on progress. Also send notifications if progress is
3740d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  // too slow.
3750d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  if (progress == 1.0 ||
3760d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo      progress - download_progress_ >= kBroadcastThresholdProgress ||
3770d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo      TimeTicks::Now() - last_notify_time_ >=
3780d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo          TimeDelta::FromSeconds(kBroadcastThresholdSeconds)) {
3790d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo    download_progress_ = progress;
3800d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo    SetStatusAndNotify(status_);
3810d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  }
3820d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo}
3830d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo
3845e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::UpdateBootFlags() {
3855e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (updated_boot_flags_) {
3865e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    LOG(INFO) << "Already updated boot flags. Skipping.";
3875e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    CompleteUpdateBootFlags(true);
3885e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return;
3895e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3905e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // This is purely best effort.
3915e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  LOG(INFO) << "Marking booted slot as good.";
3925e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (!boot_control_->MarkBootSuccessfulAsync(
3935e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo          Bind(&UpdateAttempterAndroid::CompleteUpdateBootFlags,
3945e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo               base::Unretained(this)))) {
3955e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    LOG(ERROR) << "Failed to mark current boot as successful.";
3965e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    CompleteUpdateBootFlags(false);
3975e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3985e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3995e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4005e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::CompleteUpdateBootFlags(bool successful) {
4015e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  updated_boot_flags_ = true;
4025e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  ScheduleProcessingStart();
4035e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
4045e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4055e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::ScheduleProcessingStart() {
4065e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  LOG(INFO) << "Scheduling an action processor start.";
4075e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  brillo::MessageLoop::current()->PostTask(
408f1cf34849efdc0158fb1987ba74a51f25016c5bdLuis Hector Chavez      FROM_HERE,
409f1cf34849efdc0158fb1987ba74a51f25016c5bdLuis Hector Chavez      Bind([](ActionProcessor* processor) { processor->StartProcessing(); },
410f1cf34849efdc0158fb1987ba74a51f25016c5bdLuis Hector Chavez           base::Unretained(processor_.get())));
4115e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
4125e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4135e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::TerminateUpdateAndNotify(ErrorCode error_code) {
4145e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (status_ == UpdateStatus::IDLE) {
4155e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    LOG(ERROR) << "No ongoing update, but TerminatedUpdate() called.";
4165e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return;
4175e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
4185e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4190d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  download_progress_ = 0;
4205e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  actions_.clear();
4215e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  UpdateStatus new_status =
4225e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      (error_code == ErrorCode::kSuccess ? UpdateStatus::UPDATED_NEED_REBOOT
4235e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                         : UpdateStatus::IDLE);
4245e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  SetStatusAndNotify(new_status);
4255e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  ongoing_update_ = false;
4265e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4275e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  for (auto observer : daemon_state_->service_observers())
4285e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    observer->SendPayloadApplicationComplete(error_code);
4295e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
4305e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4315e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::SetStatusAndNotify(UpdateStatus status) {
4325e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  status_ = status;
4332d1c87bbd55878c5b4dd93e97a8cc28e715137ecSen Jiang  size_t payload_size =
4342d1c87bbd55878c5b4dd93e97a8cc28e715137ecSen Jiang      install_plan_.payloads.empty() ? 0 : install_plan_.payloads[0].size;
4355e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  for (auto observer : daemon_state_->service_observers()) {
4365e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    observer->SendStatusUpdate(
4372d1c87bbd55878c5b4dd93e97a8cc28e715137ecSen Jiang        0, download_progress_, status_, "", payload_size);
4385e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
4395e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  last_notify_time_ = TimeTicks::Now();
4405e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
4415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4422c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymovoid UpdateAttempterAndroid::BuildUpdateActions(const string& url) {
4435e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  CHECK(!processor_->IsRunning());
4445e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  processor_->set_delegate(this);
4455e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4465e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Actions:
4475e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  shared_ptr<InstallPlanAction> install_plan_action(
4485e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      new InstallPlanAction(install_plan_));
4495e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4502c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo  HttpFetcher* download_fetcher = nullptr;
4512c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo  if (FileFetcher::SupportedUrl(url)) {
4522c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo    DLOG(INFO) << "Using FileFetcher for file URL.";
4532c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo    download_fetcher = new FileFetcher();
4542c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo  } else {
45514c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#ifdef _UE_SIDELOAD
45614c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo    LOG(FATAL) << "Unsupported sideload URI: " << url;
45714c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#else
4582c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo    LibcurlHttpFetcher* libcurl_fetcher =
4592c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo        new LibcurlHttpFetcher(&proxy_resolver_, hardware_);
4602c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo    libcurl_fetcher->set_server_to_check(ServerToCheck::kDownload);
4612c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo    download_fetcher = libcurl_fetcher;
46214c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#endif  // _UE_SIDELOAD
4632c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo  }
4645ae865b431cc041feed82f1db28a38e99dbd631fSen Jiang  shared_ptr<DownloadAction> download_action(
4655ae865b431cc041feed82f1db28a38e99dbd631fSen Jiang      new DownloadAction(prefs_,
4665ae865b431cc041feed82f1db28a38e99dbd631fSen Jiang                         boot_control_,
4675ae865b431cc041feed82f1db28a38e99dbd631fSen Jiang                         hardware_,
4685ae865b431cc041feed82f1db28a38e99dbd631fSen Jiang                         nullptr,             // system_state, not used.
4695ae865b431cc041feed82f1db28a38e99dbd631fSen Jiang                         download_fetcher));  // passes ownership
470fef85fd9ece49941db274559a938fe8b2c5157bfSen Jiang  shared_ptr<FilesystemVerifierAction> filesystem_verifier_action(
471e6e4bb929acd73e57b68a30d1e3a33d76607aec3Sen Jiang      new FilesystemVerifierAction());
4725e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4735e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
474fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo      new PostinstallRunnerAction(boot_control_, hardware_));
4755e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4765e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  download_action->set_delegate(this);
4775ae865b431cc041feed82f1db28a38e99dbd631fSen Jiang  download_action->set_base_offset(base_offset_);
4785e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  download_action_ = download_action;
479b6eef73e20736b4b6a43611fa732d8bba72ff7ffAlex Deymo  postinstall_runner_action->set_delegate(this);
4805e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4815e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  actions_.push_back(shared_ptr<AbstractAction>(install_plan_action));
4825e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  actions_.push_back(shared_ptr<AbstractAction>(download_action));
483fef85fd9ece49941db274559a938fe8b2c5157bfSen Jiang  actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
4845e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
4855e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4865e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Bond them together. We have to use the leaf-types when calling
4875e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // BondActions().
4885e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  BondActions(install_plan_action.get(), download_action.get());
489fef85fd9ece49941db274559a938fe8b2c5157bfSen Jiang  BondActions(download_action.get(), filesystem_verifier_action.get());
490fef85fd9ece49941db274559a938fe8b2c5157bfSen Jiang  BondActions(filesystem_verifier_action.get(),
4915e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo              postinstall_runner_action.get());
4925e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4935e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Enqueue the actions.
4945e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  for (const shared_ptr<AbstractAction>& action : actions_)
4955e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    processor_->EnqueueAction(action.get());
4965e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
4975e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4985e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::WriteUpdateCompletedMarker() {
4995e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  string boot_id;
5005e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id));
5015e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  prefs_->SetString(kPrefsUpdateCompletedOnBootId, boot_id);
5025e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  return true;
5035e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
5045e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
5055e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::UpdateCompletedOnThisBoot() {
5065e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // In case of an update_engine restart without a reboot, we stored the boot_id
5075e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // when the update was completed by setting a pref, so we can check whether
5085e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // the last update was on this boot or a previous one.
5095e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  string boot_id;
5105e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id));
5115e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
5125e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  string update_completed_on_boot_id;
5135e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  return (prefs_->Exists(kPrefsUpdateCompletedOnBootId) &&
5145e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo          prefs_->GetString(kPrefsUpdateCompletedOnBootId,
5155e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                            &update_completed_on_boot_id) &&
5165e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo          update_completed_on_boot_id == boot_id);
5175e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
5185e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
5195e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}  // namespace chromeos_update_engine
520