update_attempter_android.cc revision 2703ef4466066d64d8021904e233b120f38c0272
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/multi_range_http_fetcher.h"
345e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/common/utils.h"
3503a4de7dc77b058987fc0670f4f67ba10ff31bc8Alex Deymo#include "update_engine/daemon_state_interface.h"
3687792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo#include "update_engine/network_selector.h"
375e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/payload_consumer/download_action.h"
385e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/payload_consumer/filesystem_verifier_action.h"
395e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo#include "update_engine/payload_consumer/postinstall_runner_action.h"
403b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo#include "update_engine/update_status_utils.h"
415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4214c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#ifndef _UE_SIDELOAD
4314c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo// Do not include support for external HTTP(s) urls when building
4414c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo// update_engine_sideload.
4514c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#include "update_engine/libcurl_http_fetcher.h"
4614c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#endif
4714c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo
485e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing base::Bind;
495e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing base::TimeDelta;
505e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing base::TimeTicks;
515e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing std::shared_ptr;
525e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing std::string;
535e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymousing std::vector;
545e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
555e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymonamespace chromeos_update_engine {
565e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
575e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymonamespace {
585e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
590d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo// Minimum threshold to broadcast an status update in progress and time.
600d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymoconst double kBroadcastThresholdProgress = 0.01;  // 1%
610d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymoconst int kBroadcastThresholdSeconds = 10;
620d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo
635e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymoconst char* const kErrorDomain = "update_engine";
645e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// TODO(deymo): Convert the different errors to a numeric value to report them
655e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// back on the service error.
665e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymoconst char* const kGenericError = "generic_error";
675e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
685e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo// Log and set the error on the passed ErrorPtr.
695e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool LogAndSetError(brillo::ErrorPtr* error,
705e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                    const tracked_objects::Location& location,
715e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                    const string& reason) {
725e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  brillo::Error::AddTo(error, location, kErrorDomain, kGenericError, reason);
735e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  LOG(ERROR) << "Replying with failure: " << location.ToString() << ": "
745e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo             << reason;
755e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  return false;
765e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
775e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
785e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}  // namespace
795e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
805e3ea278d9e771be3b51bd72985ad582678baddcAlex DeymoUpdateAttempterAndroid::UpdateAttempterAndroid(
8103a4de7dc77b058987fc0670f4f67ba10ff31bc8Alex Deymo    DaemonStateInterface* daemon_state,
825e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    PrefsInterface* prefs,
835e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    BootControlInterface* boot_control,
845e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    HardwareInterface* hardware)
855e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    : daemon_state_(daemon_state),
865e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      prefs_(prefs),
875e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      boot_control_(boot_control),
885e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      hardware_(hardware),
895e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      processor_(new ActionProcessor()) {
9087792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo  network_selector_ = network::CreateNetworkSelector();
915e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
925e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
935e3ea278d9e771be3b51bd72985ad582678baddcAlex DeymoUpdateAttempterAndroid::~UpdateAttempterAndroid() {
945e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Release ourselves as the ActionProcessor's delegate to prevent
955e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // re-scheduling the updates due to the processing stopped.
965e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  processor_->set_delegate(nullptr);
975e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
985e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
995e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::Init() {
1005e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // In case of update_engine restart without a reboot we need to restore the
1015e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // reboot needed state.
1025e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (UpdateCompletedOnThisBoot())
1030e061aebc651e041b6d1d8a3a91834feb87e0d62Alex Deymo    SetStatusAndNotify(UpdateStatus::UPDATED_NEED_REBOOT);
1045e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  else
1050e061aebc651e041b6d1d8a3a91834feb87e0d62Alex Deymo    SetStatusAndNotify(UpdateStatus::IDLE);
1065e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
1075e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1085e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::ApplyPayload(
1095e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    const string& payload_url,
1105e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    int64_t payload_offset,
1115e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    int64_t payload_size,
1125e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    const vector<string>& key_value_pair_headers,
1135e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    brillo::ErrorPtr* error) {
1145e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (status_ == UpdateStatus::UPDATED_NEED_REBOOT) {
1155e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return LogAndSetError(
1165e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo        error, FROM_HERE, "An update already applied, waiting for reboot");
1175e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
1185e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (ongoing_update_) {
1195e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return LogAndSetError(
1205e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo        error, FROM_HERE, "Already processing an update, cancel it first.");
1215e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
1225e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  DCHECK(status_ == UpdateStatus::IDLE);
1235e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
124218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  std::map<string, string> headers;
125218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  for (const string& key_value_pair : key_value_pair_headers) {
126218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    string key;
127218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    string value;
128218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    if (!brillo::string_utils::SplitAtFirst(
129218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo            key_value_pair, "=", &key, &value, false)) {
130218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo      return LogAndSetError(
131218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo          error, FROM_HERE, "Passed invalid header: " + key_value_pair);
132218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    }
133218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    if (!headers.emplace(key, value).second)
134218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo      return LogAndSetError(error, FROM_HERE, "Passed repeated key: " + key);
135218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  }
136218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo
137218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  // Unique identifier for the payload. An empty string means that the payload
138218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  // can't be resumed.
139218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  string payload_id = (headers[kPayloadPropertyFileHash] +
140218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo                       headers[kPayloadPropertyMetadataHash]);
1415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1425e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Setup the InstallPlan based on the request.
1435e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_ = InstallPlan();
1445e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1455e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.download_url = payload_url;
1465e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.version = "";
1470fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo  base_offset_ = payload_offset;
148218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  install_plan_.payload_size = payload_size;
149218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  if (!install_plan_.payload_size) {
150218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    if (!base::StringToUint64(headers[kPayloadPropertyFileSize],
151218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo                              &install_plan_.payload_size)) {
152218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo      install_plan_.payload_size = 0;
153218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    }
154218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  }
1552703ef4466066d64d8021904e233b120f38c0272Sen Jiang  if (!brillo::data_encoding::Base64Decode(headers[kPayloadPropertyFileHash],
1562703ef4466066d64d8021904e233b120f38c0272Sen Jiang                                           &install_plan_.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],
161218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo                            &install_plan_.metadata_size)) {
162218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo    install_plan_.metadata_size = 0;
163218397ff42cbef9f1b95fdfe30e8e82145ba9908Alex Deymo  }
1645e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.metadata_signature = "";
1655e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // The |public_key_rsa| key would override the public key stored on disk.
1665e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.public_key_rsa = "";
1675e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
1685e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.hash_checks_mandatory = hardware_->IsOfficialBuild();
1695e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.is_resume = !payload_id.empty() &&
1705e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                            DeltaPerformer::CanResumeUpdate(prefs_, payload_id);
1715e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (!install_plan_.is_resume) {
1725e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    if (!DeltaPerformer::ResetUpdateProgress(prefs_, false)) {
1735e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      LOG(WARNING) << "Unable to reset the update progress.";
1745e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    }
1755e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    if (!prefs_->SetString(kPrefsUpdateCheckResponseHash, payload_id)) {
1765e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      LOG(WARNING) << "Unable to save the update check response hash.";
1775e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    }
1785e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
17964d9878470aa7b388e971862181daf6260851602Alex Deymo  // The |payload_type| is not used anymore since minor_version 3.
18064d9878470aa7b388e971862181daf6260851602Alex Deymo  install_plan_.payload_type = InstallPayloadType::kUnknown;
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)) {
20087792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo      LOG(WARNING) << "Unable to set network_id, continuing with the update.";
20187792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo    }
20287792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo  }
20387792ea818a63f2d246bbba2b7429b85ee8d669fAlex Deymo
2045e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  LOG(INFO) << "Using this install plan:";
2055e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  install_plan_.Dump();
2065e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2072c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo  BuildUpdateActions(payload_url);
2085e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  SetupDownload();
209fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo  // Setup extra headers.
210fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo  HttpFetcher* fetcher = download_action_->http_fetcher();
211fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo  if (!headers[kPayloadPropertyAuthorization].empty())
212fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo    fetcher->SetHeader("Authorization", headers[kPayloadPropertyAuthorization]);
213fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo  if (!headers[kPayloadPropertyUserAgent].empty())
214fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo    fetcher->SetHeader("User-Agent", headers[kPayloadPropertyUserAgent]);
215fdd6dec9c4be2fbd667cf874c4cc6f4ffecaeef9Alex Deymo
2165e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE);
217f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  ongoing_update_ = true;
2185e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2195e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Just in case we didn't update boot flags yet, make sure they're updated
2205e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // before any update processing starts. This will start the update process.
2215e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  UpdateBootFlags();
2225e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  return true;
2235e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
2245e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2255e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::SuspendUpdate(brillo::ErrorPtr* error) {
226f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  if (!ongoing_update_)
227f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo    return LogAndSetError(error, FROM_HERE, "No ongoing update to suspend.");
228f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  processor_->SuspendProcessing();
229f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  return true;
2305e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
2315e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2325e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::ResumeUpdate(brillo::ErrorPtr* error) {
233f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  if (!ongoing_update_)
234f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo    return LogAndSetError(error, FROM_HERE, "No ongoing update to resume.");
235f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  processor_->ResumeProcessing();
236f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  return true;
2375e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
2385e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2395e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::CancelUpdate(brillo::ErrorPtr* error) {
240f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  if (!ongoing_update_)
2415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return LogAndSetError(error, FROM_HERE, "No ongoing update to cancel.");
242f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  processor_->StopProcessing();
243f28585764e91b7c25a7c2856ff645c8bb22d64a9Alex Deymo  return true;
2445e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
2455e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2463b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymobool UpdateAttempterAndroid::ResetStatus(brillo::ErrorPtr* error) {
2473b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo  LOG(INFO) << "Attempting to reset state from "
2483b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo            << UpdateStatusToString(status_) << " to UpdateStatus::IDLE";
2493b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2503b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo  switch (status_) {
2513b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo    case UpdateStatus::IDLE:
2523b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      return true;
2533b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2543b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo    case UpdateStatus::UPDATED_NEED_REBOOT:  {
2553b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      // Remove the reboot marker so that if the machine is rebooted
2563b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      // after resetting to idle state, it doesn't go back to
2573b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      // UpdateStatus::UPDATED_NEED_REBOOT state.
2583b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      bool ret_value = prefs_->Delete(kPrefsUpdateCompletedOnBootId);
2593b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2603b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      // Update the boot flags so the current slot has higher priority.
2613b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      if (!boot_control_->SetActiveBootSlot(boot_control_->GetCurrentSlot()))
2623b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo        ret_value = false;
2633b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2645259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo      // Mark the current slot as successful again, since marking it as active
2655259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo      // may reset the successful bit. We ignore the result of whether marking
2665259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo      // the current slot as successful worked.
2675259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo      if (!boot_control_->MarkBootSuccessfulAsync(Bind([](bool successful){})))
2685259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo        ret_value = false;
2695259033d23938ce3ce20e4a9b599be0eec113610Alex Deymo
2703b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      if (!ret_value) {
2713b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo        return LogAndSetError(
2723b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo            error,
2733b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo            FROM_HERE,
2743b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo            "Failed to reset the status to ");
2753b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      }
2763b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2773b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      SetStatusAndNotify(UpdateStatus::IDLE);
2783b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      LOG(INFO) << "Reset status successful";
2793b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      return true;
2803b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo    }
2813b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2823b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo    default:
2833b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo      return LogAndSetError(
2843b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo          error,
2853b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo          FROM_HERE,
2863b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo          "Reset not allowed in this state. Cancel the ongoing update first");
2873b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo  }
2883b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo}
2893b678db2e7fd4baa38c6d6b8bb8334f7d88b8682Alex Deymo
2905e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::ProcessingDone(const ActionProcessor* processor,
2915e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                            ErrorCode code) {
2925e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  LOG(INFO) << "Processing Done.";
2935e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
2945990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo  switch (code) {
2955990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kSuccess:
2965990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      // Update succeeded.
2975990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      WriteUpdateCompletedMarker();
2985990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
2995990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      DeltaPerformer::ResetUpdateProgress(prefs_, false);
3005990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo
3015990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      LOG(INFO) << "Update successfully applied, waiting to reboot.";
3025990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      break;
3035990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo
3045990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kFilesystemCopierError:
3055990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kNewRootfsVerificationError:
3065990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kNewKernelVerificationError:
3075990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kFilesystemVerifierError:
3085990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    case ErrorCode::kDownloadStateInitializationError:
3095990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      // Reset the ongoing update for these errors so it starts from the
3105990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      // beginning next time.
3115990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      DeltaPerformer::ResetUpdateProgress(prefs_, false);
3125990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      LOG(INFO) << "Resetting update progress.";
3135990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      break;
3145e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3155990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo    default:
3165990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      // Ignore all other error codes.
3175990bf33b50beec0caf55a97f5ca87608ccbc694Alex Deymo      break;
31803a4de7dc77b058987fc0670f4f67ba10ff31bc8Alex Deymo  }
3195e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3205e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  TerminateUpdateAndNotify(code);
3215e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3225e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3235e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::ProcessingStopped(
3245e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    const ActionProcessor* processor) {
3255e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  TerminateUpdateAndNotify(ErrorCode::kUserCanceled);
3265e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3275e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3285e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::ActionCompleted(ActionProcessor* processor,
3295e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                             AbstractAction* action,
3305e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                             ErrorCode code) {
3315e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Reset download progress regardless of whether or not the download
3325e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // action succeeded.
3335e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  const string type = action->Type();
3345e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (type == DownloadAction::StaticType()) {
3350d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo    download_progress_ = 0;
3365e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3375e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (code != ErrorCode::kSuccess) {
3385e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    // If an action failed, the ActionProcessor will cancel the whole thing.
3395e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return;
3405e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (type == DownloadAction::StaticType()) {
3425e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    SetStatusAndNotify(UpdateStatus::FINALIZING);
3435e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3445e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3455e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3465e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::BytesReceived(uint64_t bytes_progressed,
3475e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                           uint64_t bytes_received,
3485e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                           uint64_t total) {
3490d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  double progress = 0;
3505e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (total)
3515e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    progress = static_cast<double>(bytes_received) / static_cast<double>(total);
3520d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  if (status_ != UpdateStatus::DOWNLOADING || bytes_received == total) {
3535e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    download_progress_ = progress;
3545e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    SetStatusAndNotify(UpdateStatus::DOWNLOADING);
3550d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  } else {
3560d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo    ProgressUpdate(progress);
3575e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3585e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3595e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3605e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::ShouldCancel(ErrorCode* cancel_reason) {
3615e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // TODO(deymo): Notify the DownloadAction that it should cancel the update
3625e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // download.
3635e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  return false;
3645e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3655e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3665e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::DownloadComplete() {
3675e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Nothing needs to be done when the download completes.
3685e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3695e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3700d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymovoid UpdateAttempterAndroid::ProgressUpdate(double progress) {
3710d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  // Self throttle based on progress. Also send notifications if progress is
3720d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  // too slow.
3730d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  if (progress == 1.0 ||
3740d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo      progress - download_progress_ >= kBroadcastThresholdProgress ||
3750d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo      TimeTicks::Now() - last_notify_time_ >=
3760d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo          TimeDelta::FromSeconds(kBroadcastThresholdSeconds)) {
3770d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo    download_progress_ = progress;
3780d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo    SetStatusAndNotify(status_);
3790d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  }
3800d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo}
3810d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo
3825e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::UpdateBootFlags() {
3835e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (updated_boot_flags_) {
3845e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    LOG(INFO) << "Already updated boot flags. Skipping.";
3855e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    CompleteUpdateBootFlags(true);
3865e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return;
3875e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3885e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // This is purely best effort.
3895e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  LOG(INFO) << "Marking booted slot as good.";
3905e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (!boot_control_->MarkBootSuccessfulAsync(
3915e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo          Bind(&UpdateAttempterAndroid::CompleteUpdateBootFlags,
3925e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo               base::Unretained(this)))) {
3935e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    LOG(ERROR) << "Failed to mark current boot as successful.";
3945e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    CompleteUpdateBootFlags(false);
3955e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
3965e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
3975e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
3985e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::CompleteUpdateBootFlags(bool successful) {
3995e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  updated_boot_flags_ = true;
4005e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  ScheduleProcessingStart();
4015e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
4025e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4035e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::ScheduleProcessingStart() {
4045e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  LOG(INFO) << "Scheduling an action processor start.";
4055e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  brillo::MessageLoop::current()->PostTask(
406f1cf34849efdc0158fb1987ba74a51f25016c5bdLuis Hector Chavez      FROM_HERE,
407f1cf34849efdc0158fb1987ba74a51f25016c5bdLuis Hector Chavez      Bind([](ActionProcessor* processor) { processor->StartProcessing(); },
408f1cf34849efdc0158fb1987ba74a51f25016c5bdLuis Hector Chavez           base::Unretained(processor_.get())));
4095e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
4105e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4115e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::TerminateUpdateAndNotify(ErrorCode error_code) {
4125e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (status_ == UpdateStatus::IDLE) {
4135e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    LOG(ERROR) << "No ongoing update, but TerminatedUpdate() called.";
4145e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    return;
4155e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
4165e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4170d29854cf5bb05a22cf161b50052539aa420a36eAlex Deymo  download_progress_ = 0;
4185e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  actions_.clear();
4195e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  UpdateStatus new_status =
4205e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      (error_code == ErrorCode::kSuccess ? UpdateStatus::UPDATED_NEED_REBOOT
4215e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                                         : UpdateStatus::IDLE);
4225e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  SetStatusAndNotify(new_status);
4235e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  ongoing_update_ = false;
4245e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4255e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  for (auto observer : daemon_state_->service_observers())
4265e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    observer->SendPayloadApplicationComplete(error_code);
4275e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
4285e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4295e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::SetStatusAndNotify(UpdateStatus status) {
4305e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  status_ = status;
4315e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  for (auto observer : daemon_state_->service_observers()) {
4325e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    observer->SendStatusUpdate(
4335e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo        0, download_progress_, status_, "", install_plan_.payload_size);
4345e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
4355e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  last_notify_time_ = TimeTicks::Now();
4365e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
4375e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4382c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymovoid UpdateAttempterAndroid::BuildUpdateActions(const string& url) {
4395e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  CHECK(!processor_->IsRunning());
4405e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  processor_->set_delegate(this);
4415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4425e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Actions:
4435e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  shared_ptr<InstallPlanAction> install_plan_action(
4445e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      new InstallPlanAction(install_plan_));
4455e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4462c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo  HttpFetcher* download_fetcher = nullptr;
4472c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo  if (FileFetcher::SupportedUrl(url)) {
4482c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo    DLOG(INFO) << "Using FileFetcher for file URL.";
4492c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo    download_fetcher = new FileFetcher();
4502c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo  } else {
45114c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#ifdef _UE_SIDELOAD
45214c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo    LOG(FATAL) << "Unsupported sideload URI: " << url;
45314c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#else
4542c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo    LibcurlHttpFetcher* libcurl_fetcher =
4552c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo        new LibcurlHttpFetcher(&proxy_resolver_, hardware_);
4562c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo    libcurl_fetcher->set_server_to_check(ServerToCheck::kDownload);
4572c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo    download_fetcher = libcurl_fetcher;
45814c0da88a93aa7b1aa71d5e7e923b537f0d419f3Alex Deymo#endif  // _UE_SIDELOAD
4592c131bbf81d8c02ade163b939c96e44aa93765e9Alex Deymo  }
4605e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  shared_ptr<DownloadAction> download_action(new DownloadAction(
4615e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      prefs_,
4625e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      boot_control_,
4635e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      hardware_,
4645e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      nullptr,                                        // system_state, not used.
4655e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      new MultiRangeHttpFetcher(download_fetcher)));  // passes ownership
466fef85fd9ece49941db274559a938fe8b2c5157bfSen Jiang  shared_ptr<FilesystemVerifierAction> filesystem_verifier_action(
467e6e4bb929acd73e57b68a30d1e3a33d76607aec3Sen Jiang      new FilesystemVerifierAction());
4685e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4695e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
470fb905d9b8d49f8fe41297c7aba2dd0942f1be311Alex Deymo      new PostinstallRunnerAction(boot_control_, hardware_));
4715e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4725e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  download_action->set_delegate(this);
4735e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  download_action_ = download_action;
474b6eef73e20736b4b6a43611fa732d8bba72ff7ffAlex Deymo  postinstall_runner_action->set_delegate(this);
4755e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4765e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  actions_.push_back(shared_ptr<AbstractAction>(install_plan_action));
4775e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  actions_.push_back(shared_ptr<AbstractAction>(download_action));
478fef85fd9ece49941db274559a938fe8b2c5157bfSen Jiang  actions_.push_back(shared_ptr<AbstractAction>(filesystem_verifier_action));
4795e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
4805e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4815e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Bond them together. We have to use the leaf-types when calling
4825e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // BondActions().
4835e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  BondActions(install_plan_action.get(), download_action.get());
484fef85fd9ece49941db274559a938fe8b2c5157bfSen Jiang  BondActions(download_action.get(), filesystem_verifier_action.get());
485fef85fd9ece49941db274559a938fe8b2c5157bfSen Jiang  BondActions(filesystem_verifier_action.get(),
4865e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo              postinstall_runner_action.get());
4875e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4885e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // Enqueue the actions.
4895e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  for (const shared_ptr<AbstractAction>& action : actions_)
4905e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    processor_->EnqueueAction(action.get());
4915e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
4925e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
4935e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymovoid UpdateAttempterAndroid::SetupDownload() {
4945e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  MultiRangeHttpFetcher* fetcher =
4955e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo      static_cast<MultiRangeHttpFetcher*>(download_action_->http_fetcher());
4965e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  fetcher->ClearRanges();
4975e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  if (install_plan_.is_resume) {
4985e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    // Resuming an update so fetch the update manifest metadata first.
4995e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    int64_t manifest_metadata_size = 0;
500f25eb491ff60f21659a7e2b230ee1c83957034c7Alex Deymo    int64_t manifest_signature_size = 0;
5015e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
502f25eb491ff60f21659a7e2b230ee1c83957034c7Alex Deymo    prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size);
503f25eb491ff60f21659a7e2b230ee1c83957034c7Alex Deymo    fetcher->AddRange(base_offset_,
504f25eb491ff60f21659a7e2b230ee1c83957034c7Alex Deymo                      manifest_metadata_size + manifest_signature_size);
5055e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    // If there're remaining unprocessed data blobs, fetch them. Be careful not
5065e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    // to request data beyond the end of the payload to avoid 416 HTTP response
5075e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    // error codes.
5085e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    int64_t next_data_offset = 0;
5095e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
510f25eb491ff60f21659a7e2b230ee1c83957034c7Alex Deymo    uint64_t resume_offset =
511f25eb491ff60f21659a7e2b230ee1c83957034c7Alex Deymo        manifest_metadata_size + manifest_signature_size + next_data_offset;
5120fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo    if (!install_plan_.payload_size) {
5130fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo      fetcher->AddRange(base_offset_ + resume_offset);
5140fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo    } else if (resume_offset < install_plan_.payload_size) {
5150fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo      fetcher->AddRange(base_offset_ + resume_offset,
5160fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo                        install_plan_.payload_size - resume_offset);
5175e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo    }
5185e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  } else {
5190fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo    if (install_plan_.payload_size) {
5200fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo      fetcher->AddRange(base_offset_, install_plan_.payload_size);
5210fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo    } else {
5220fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo      // If no payload size is passed we assume we read until the end of the
5230fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo      // stream.
5240fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo      fetcher->AddRange(base_offset_);
5250fd51ff8fd200aab7f9ab930b47736505a7b59c3Alex Deymo    }
5265e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  }
5275e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
5285e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
5295e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::WriteUpdateCompletedMarker() {
5305e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  string boot_id;
5315e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id));
5325e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  prefs_->SetString(kPrefsUpdateCompletedOnBootId, boot_id);
5335e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  return true;
5345e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
5355e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
5365e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymobool UpdateAttempterAndroid::UpdateCompletedOnThisBoot() {
5375e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // In case of an update_engine restart without a reboot, we stored the boot_id
5385e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // when the update was completed by setting a pref, so we can check whether
5395e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  // the last update was on this boot or a previous one.
5405e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  string boot_id;
5415e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id));
5425e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
5435e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  string update_completed_on_boot_id;
5445e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo  return (prefs_->Exists(kPrefsUpdateCompletedOnBootId) &&
5455e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo          prefs_->GetString(kPrefsUpdateCompletedOnBootId,
5465e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo                            &update_completed_on_boot_id) &&
5475e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo          update_completed_on_boot_id == boot_id);
5485e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}
5495e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo
5505e3ea278d9e771be3b51bd72985ad582678baddcAlex Deymo}  // namespace chromeos_update_engine
551