Android: Implement the update attempter for Android.

This patch implements the update workflow for non-Brillo targets. Only
the applyPayload() method is implemented here with a minimal subset of
functionally implemented in it. This new class acts as the service
delegate, handling all the binder service method calls for non-Brillo
targets.

Bug: 25631949
TEST=FEATURES=test emerge-link update_engine
TEST=`mmma system/update_engine` on aosp_arm-eng and edison-eng
TEST=Deployed on a non-Brillo device and tested with update_engine_client

Change-Id: I678cd141633bc2c0920a09ef27a02d53682d5330
diff --git a/Android.mk b/Android.mk
index 686414d..bebcc52 100644
--- a/Android.mk
+++ b/Android.mk
@@ -359,12 +359,15 @@
 # loop to apply payloads provided by the upper layer via a Binder interface.
 ue_libupdate_engine_android_exported_static_libraries := \
     libpayload_consumer \
+    libfs_mgr \
     $(ue_libpayload_consumer_exported_static_libraries)
 ue_libupdate_engine_android_exported_shared_libraries := \
     $(ue_libpayload_consumer_exported_shared_libraries) \
     libbinder \
     libbinderwrapper \
     libbrillo-binder \
+    libcutils \
+    libhardware \
     libutils
 
 include $(CLEAR_VARS)
@@ -386,8 +389,12 @@
     binder_bindings/android/os/IUpdateEngine.aidl \
     binder_bindings/android/os/IUpdateEngineCallback.aidl \
     binder_service_android.cc \
+    boot_control_android.cc \
     daemon.cc \
-    daemon_state_android.cc
+    daemon_state_android.cc \
+    hardware_android.cc \
+    proxy_resolver.cc \
+    update_attempter_android.cc
 include $(BUILD_STATIC_LIBRARY)
 
 endif  # !defined(BRILLO)
diff --git a/daemon_state_android.cc b/daemon_state_android.cc
index c1d9fcc..6a5aa9f 100644
--- a/daemon_state_android.cc
+++ b/daemon_state_android.cc
@@ -16,14 +16,59 @@
 
 #include "update_engine/daemon_state_android.h"
 
+#include <base/logging.h>
+
+#include "update_engine/common/boot_control.h"
+#include "update_engine/common/boot_control_stub.h"
+#include "update_engine/common/hardware.h"
+#include "update_engine/common/prefs.h"
+#include "update_engine/update_attempter_android.h"
+
 namespace chromeos_update_engine {
 
 bool DaemonStateAndroid::Initialize() {
-  // TODO(deymo): Implement the Android updater state.
+  boot_control_ = boot_control::CreateBootControl();
+  if (!boot_control_) {
+    LOG(WARNING) << "Unable to create BootControl instance, using stub "
+                 << "instead. All update attempts will fail.";
+    boot_control_.reset(new BootControlStub());
+  }
+
+  hardware_ = hardware::CreateHardware();
+  if (!hardware_) {
+    LOG(ERROR) << "Error intializing the HardwareInterface.";
+    return false;
+  }
+
+  LOG_IF(INFO, !hardware_->IsNormalBootMode()) << "Booted in dev mode.";
+  LOG_IF(INFO, !hardware_->IsOfficialBuild()) << "Booted non-official build.";
+
+  // Initialize prefs.
+  base::FilePath non_volatile_path;
+  // TODO(deymo): Fall back to in-memory prefs if there's no physical directory
+  // available.
+  if (!hardware_->GetNonVolatileDirectory(&non_volatile_path)) {
+    LOG(ERROR) << "Failed to get a non-volatile directory.";
+    return false;
+  }
+  Prefs* prefs = new Prefs();
+  prefs_.reset(prefs);
+  if (!prefs->Init(non_volatile_path.Append(kPrefsSubDirectory))) {
+    LOG(ERROR) << "Failed to initialize preferences.";
+    return false;
+  }
+
+  // Initialize the UpdateAttempter before the UpdateManager.
+  update_attempter_.reset(new UpdateAttempterAndroid(
+      this, prefs_.get(), boot_control_.get(), hardware_.get()));
+  update_attempter_->Init();
+
   return true;
 }
 
 bool DaemonStateAndroid::StartUpdater() {
+  // The DaemonState in Android is a passive daemon. It will only start applying
+  // an update when instructed to do so from the exposed binder API.
   return true;
 }
 
@@ -36,8 +81,7 @@
 }
 
 ServiceDelegateAndroidInterface* DaemonStateAndroid::service_delegate() {
-  // TODO(deymo): Implement a service delegate and return it here.
-  return nullptr;
+  return update_attempter_.get();
 }
 
 }  // namespace chromeos_update_engine
diff --git a/daemon_state_android.h b/daemon_state_android.h
index e0fac00..5db3ef0 100644
--- a/daemon_state_android.h
+++ b/daemon_state_android.h
@@ -17,11 +17,17 @@
 #ifndef UPDATE_ENGINE_DAEMON_STATE_ANDROID_H_
 #define UPDATE_ENGINE_DAEMON_STATE_ANDROID_H_
 
+#include <memory>
 #include <set>
 
+#include "update_engine/common/boot_control_interface.h"
+#include "update_engine/common/certificate_checker.h"
+#include "update_engine/common/hardware_interface.h"
+#include "update_engine/common/prefs_interface.h"
 #include "update_engine/daemon_state_interface.h"
 #include "update_engine/service_delegate_android_interface.h"
 #include "update_engine/service_observer_interface.h"
+#include "update_engine/update_attempter_android.h"
 
 namespace chromeos_update_engine {
 
@@ -46,6 +52,18 @@
 
  protected:
   std::set<ServiceObserverInterface*> service_observers_;
+
+  // Interface for the boot control functions.
+  std::unique_ptr<BootControlInterface> boot_control_;
+
+  // Interface for the hardware functions.
+  std::unique_ptr<HardwareInterface> hardware_;
+
+  // Interface for persisted store.
+  std::unique_ptr<PrefsInterface> prefs_;
+
+  // The main class handling the updates.
+  std::unique_ptr<UpdateAttempterAndroid> update_attempter_;
 };
 
 }  // namespace chromeos_update_engine
diff --git a/update_attempter_android.cc b/update_attempter_android.cc
new file mode 100644
index 0000000..131efbb
--- /dev/null
+++ b/update_attempter_android.cc
@@ -0,0 +1,389 @@
+//
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "update_engine/update_attempter_android.h"
+
+#include <algorithm>
+#include <utility>
+
+#include <base/bind.h>
+#include <base/logging.h>
+#include <brillo/bind_lambda.h>
+#include <brillo/message_loops/message_loop.h>
+
+#include "update_engine/common/constants.h"
+#include "update_engine/common/libcurl_http_fetcher.h"
+#include "update_engine/common/multi_range_http_fetcher.h"
+#include "update_engine/common/utils.h"
+#include "update_engine/daemon_state_android.h"
+#include "update_engine/payload_consumer/download_action.h"
+#include "update_engine/payload_consumer/filesystem_verifier_action.h"
+#include "update_engine/payload_consumer/postinstall_runner_action.h"
+
+using base::Bind;
+using base::TimeDelta;
+using base::TimeTicks;
+using std::shared_ptr;
+using std::string;
+using std::vector;
+
+namespace chromeos_update_engine {
+
+namespace {
+
+const char* const kErrorDomain = "update_engine";
+// TODO(deymo): Convert the different errors to a numeric value to report them
+// back on the service error.
+const char* const kGenericError = "generic_error";
+
+// Log and set the error on the passed ErrorPtr.
+bool LogAndSetError(brillo::ErrorPtr* error,
+                    const tracked_objects::Location& location,
+                    const string& reason) {
+  brillo::Error::AddTo(error, location, kErrorDomain, kGenericError, reason);
+  LOG(ERROR) << "Replying with failure: " << location.ToString() << ": "
+             << reason;
+  return false;
+}
+
+}  // namespace
+
+UpdateAttempterAndroid::UpdateAttempterAndroid(
+    DaemonStateAndroid* daemon_state,
+    PrefsInterface* prefs,
+    BootControlInterface* boot_control,
+    HardwareInterface* hardware)
+    : daemon_state_(daemon_state),
+      prefs_(prefs),
+      boot_control_(boot_control),
+      hardware_(hardware),
+      processor_(new ActionProcessor()) {
+}
+
+UpdateAttempterAndroid::~UpdateAttempterAndroid() {
+  // Release ourselves as the ActionProcessor's delegate to prevent
+  // re-scheduling the updates due to the processing stopped.
+  processor_->set_delegate(nullptr);
+}
+
+void UpdateAttempterAndroid::Init() {
+  // In case of update_engine restart without a reboot we need to restore the
+  // reboot needed state.
+  if (UpdateCompletedOnThisBoot())
+    status_ = UpdateStatus::UPDATED_NEED_REBOOT;
+  else
+    status_ = UpdateStatus::IDLE;
+}
+
+bool UpdateAttempterAndroid::ApplyPayload(
+    const string& payload_url,
+    int64_t payload_offset,
+    int64_t payload_size,
+    const vector<string>& key_value_pair_headers,
+    brillo::ErrorPtr* error) {
+  if (status_ == UpdateStatus::UPDATED_NEED_REBOOT) {
+    return LogAndSetError(
+        error, FROM_HERE, "An update already applied, waiting for reboot");
+  }
+  if (ongoing_update_) {
+    return LogAndSetError(
+        error, FROM_HERE, "Already processing an update, cancel it first.");
+  }
+  DCHECK(status_ == UpdateStatus::IDLE);
+
+  // Unique identifier for the payload.
+  // TODO(deymo): Set this the payload unique id using a value passed in the
+  // |key_value_pair_headers| list to allow resuming updates.
+  string payload_id = "";
+
+  // Setup the InstallPlan based on the request.
+  install_plan_ = InstallPlan();
+
+  install_plan_.download_url = payload_url;
+  install_plan_.version = "";
+  install_plan_.payload_size = payload_size;
+  // TODO(deymo): Retrieve the payload_hash from the properties.
+  install_plan_.payload_hash = "";
+  install_plan_.metadata_size = 0;
+  install_plan_.metadata_signature = "";
+  // The |public_key_rsa| key would override the public key stored on disk.
+  install_plan_.public_key_rsa = "";
+
+  install_plan_.hash_checks_mandatory = hardware_->IsOfficialBuild();
+  install_plan_.is_resume = !payload_id.empty() &&
+                            DeltaPerformer::CanResumeUpdate(prefs_, payload_id);
+  if (!install_plan_.is_resume) {
+    if (!DeltaPerformer::ResetUpdateProgress(prefs_, false)) {
+      LOG(WARNING) << "Unable to reset the update progress.";
+    }
+    if (!prefs_->SetString(kPrefsUpdateCheckResponseHash, payload_id)) {
+      LOG(WARNING) << "Unable to save the update check response hash.";
+    }
+  }
+  // The |is_full_update| is not used anymore since minor_version 3.
+  install_plan_.is_full_update = true;
+
+  install_plan_.source_slot = boot_control_->GetCurrentSlot();
+  install_plan_.target_slot = install_plan_.source_slot == 0 ? 1 : 0;
+  install_plan_.powerwash_required = false;
+
+  LOG(INFO) << "Using this install plan:";
+  install_plan_.Dump();
+
+  BuildUpdateActions();
+  SetupDownload();
+  cpu_limiter_.StartLimiter();
+  SetStatusAndNotify(UpdateStatus::UPDATE_AVAILABLE);
+
+  // Just in case we didn't update boot flags yet, make sure they're updated
+  // before any update processing starts. This will start the update process.
+  UpdateBootFlags();
+  return true;
+}
+
+bool UpdateAttempterAndroid::SuspendUpdate(brillo::ErrorPtr* error) {
+  // TODO(deymo): Implement suspend/resume.
+  return LogAndSetError(error, FROM_HERE, "Suspend/resume not implemented");
+}
+
+bool UpdateAttempterAndroid::ResumeUpdate(brillo::ErrorPtr* error) {
+  // TODO(deymo): Implement suspend/resume.
+  return LogAndSetError(error, FROM_HERE, "Suspend/resume not implemented");
+}
+
+bool UpdateAttempterAndroid::CancelUpdate(brillo::ErrorPtr* error) {
+  if (status_ == UpdateStatus::IDLE ||
+      status_ == UpdateStatus::UPDATED_NEED_REBOOT) {
+    return LogAndSetError(error, FROM_HERE, "No ongoing update to cancel.");
+  }
+
+  // TODO(deymo): Implement cancel.
+  return LogAndSetError(error, FROM_HERE, "Cancel not implemented");
+}
+
+void UpdateAttempterAndroid::ProcessingDone(const ActionProcessor* processor,
+                                            ErrorCode code) {
+  LOG(INFO) << "Processing Done.";
+
+  if (code == ErrorCode::kSuccess) {
+    // Update succeeded.
+    WriteUpdateCompletedMarker();
+    prefs_->SetInt64(kPrefsDeltaUpdateFailures, 0);
+    DeltaPerformer::ResetUpdateProgress(prefs_, false);
+
+    LOG(INFO) << "Update successfully applied, waiting to reboot.";
+  }
+
+  TerminateUpdateAndNotify(code);
+}
+
+void UpdateAttempterAndroid::ProcessingStopped(
+    const ActionProcessor* processor) {
+  TerminateUpdateAndNotify(ErrorCode::kUserCanceled);
+}
+
+void UpdateAttempterAndroid::ActionCompleted(ActionProcessor* processor,
+                                             AbstractAction* action,
+                                             ErrorCode code) {
+  // Reset download progress regardless of whether or not the download
+  // action succeeded.
+  const string type = action->Type();
+  if (type == DownloadAction::StaticType()) {
+    download_progress_ = 0.0;
+  }
+  if (code != ErrorCode::kSuccess) {
+    // If an action failed, the ActionProcessor will cancel the whole thing.
+    return;
+  }
+  if (type == DownloadAction::StaticType()) {
+    SetStatusAndNotify(UpdateStatus::FINALIZING);
+  }
+}
+
+void UpdateAttempterAndroid::BytesReceived(uint64_t bytes_progressed,
+                                           uint64_t bytes_received,
+                                           uint64_t total) {
+  double progress = 0.;
+  if (total)
+    progress = static_cast<double>(bytes_received) / static_cast<double>(total);
+  // Self throttle based on progress. Also send notifications if
+  // progress is too slow.
+  const double kDeltaPercent = 0.01;  // 1%
+  if (status_ != UpdateStatus::DOWNLOADING || bytes_received == total ||
+      progress - download_progress_ >= kDeltaPercent ||
+      TimeTicks::Now() - last_notify_time_ >= TimeDelta::FromSeconds(10)) {
+    download_progress_ = progress;
+    SetStatusAndNotify(UpdateStatus::DOWNLOADING);
+  }
+}
+
+bool UpdateAttempterAndroid::ShouldCancel(ErrorCode* cancel_reason) {
+  // TODO(deymo): Notify the DownloadAction that it should cancel the update
+  // download.
+  return false;
+}
+
+void UpdateAttempterAndroid::DownloadComplete() {
+  // Nothing needs to be done when the download completes.
+}
+
+void UpdateAttempterAndroid::UpdateBootFlags() {
+  if (updated_boot_flags_) {
+    LOG(INFO) << "Already updated boot flags. Skipping.";
+    CompleteUpdateBootFlags(true);
+    return;
+  }
+  // This is purely best effort.
+  LOG(INFO) << "Marking booted slot as good.";
+  if (!boot_control_->MarkBootSuccessfulAsync(
+          Bind(&UpdateAttempterAndroid::CompleteUpdateBootFlags,
+               base::Unretained(this)))) {
+    LOG(ERROR) << "Failed to mark current boot as successful.";
+    CompleteUpdateBootFlags(false);
+  }
+}
+
+void UpdateAttempterAndroid::CompleteUpdateBootFlags(bool successful) {
+  updated_boot_flags_ = true;
+  ScheduleProcessingStart();
+}
+
+void UpdateAttempterAndroid::ScheduleProcessingStart() {
+  LOG(INFO) << "Scheduling an action processor start.";
+  brillo::MessageLoop::current()->PostTask(
+      FROM_HERE, Bind([this] { this->processor_->StartProcessing(); }));
+}
+
+void UpdateAttempterAndroid::TerminateUpdateAndNotify(ErrorCode error_code) {
+  if (status_ == UpdateStatus::IDLE) {
+    LOG(ERROR) << "No ongoing update, but TerminatedUpdate() called.";
+    return;
+  }
+
+  // Reset cpu shares back to normal.
+  cpu_limiter_.StopLimiter();
+  download_progress_ = 0.0;
+  actions_.clear();
+  UpdateStatus new_status =
+      (error_code == ErrorCode::kSuccess ? UpdateStatus::UPDATED_NEED_REBOOT
+                                         : UpdateStatus::IDLE);
+  SetStatusAndNotify(new_status);
+  ongoing_update_ = false;
+
+  for (auto observer : daemon_state_->service_observers())
+    observer->SendPayloadApplicationComplete(error_code);
+}
+
+void UpdateAttempterAndroid::SetStatusAndNotify(UpdateStatus status) {
+  status_ = status;
+  for (auto observer : daemon_state_->service_observers()) {
+    observer->SendStatusUpdate(
+        0, download_progress_, status_, "", install_plan_.payload_size);
+  }
+  last_notify_time_ = TimeTicks::Now();
+}
+
+void UpdateAttempterAndroid::BuildUpdateActions() {
+  CHECK(!processor_->IsRunning());
+  processor_->set_delegate(this);
+
+  // Actions:
+  shared_ptr<InstallPlanAction> install_plan_action(
+      new InstallPlanAction(install_plan_));
+
+  LibcurlHttpFetcher* download_fetcher =
+      new LibcurlHttpFetcher(&proxy_resolver_, hardware_);
+  download_fetcher->set_server_to_check(ServerToCheck::kDownload);
+  shared_ptr<DownloadAction> download_action(new DownloadAction(
+      prefs_,
+      boot_control_,
+      hardware_,
+      nullptr,                                        // system_state, not used.
+      new MultiRangeHttpFetcher(download_fetcher)));  // passes ownership
+  shared_ptr<FilesystemVerifierAction> dst_filesystem_verifier_action(
+      new FilesystemVerifierAction(boot_control_,
+                                   VerifierMode::kVerifyTargetHash));
+
+  shared_ptr<PostinstallRunnerAction> postinstall_runner_action(
+      new PostinstallRunnerAction(boot_control_));
+
+  download_action->set_delegate(this);
+  download_action_ = download_action;
+
+  actions_.push_back(shared_ptr<AbstractAction>(install_plan_action));
+  actions_.push_back(shared_ptr<AbstractAction>(download_action));
+  actions_.push_back(
+      shared_ptr<AbstractAction>(dst_filesystem_verifier_action));
+  actions_.push_back(shared_ptr<AbstractAction>(postinstall_runner_action));
+
+  // Bond them together. We have to use the leaf-types when calling
+  // BondActions().
+  BondActions(install_plan_action.get(), download_action.get());
+  BondActions(download_action.get(), dst_filesystem_verifier_action.get());
+  BondActions(dst_filesystem_verifier_action.get(),
+              postinstall_runner_action.get());
+
+  // Enqueue the actions.
+  for (const shared_ptr<AbstractAction>& action : actions_)
+    processor_->EnqueueAction(action.get());
+}
+
+void UpdateAttempterAndroid::SetupDownload() {
+  MultiRangeHttpFetcher* fetcher =
+      static_cast<MultiRangeHttpFetcher*>(download_action_->http_fetcher());
+  fetcher->ClearRanges();
+  if (install_plan_.is_resume) {
+    // Resuming an update so fetch the update manifest metadata first.
+    int64_t manifest_metadata_size = 0;
+    prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
+    fetcher->AddRange(0, manifest_metadata_size);
+    // If there're remaining unprocessed data blobs, fetch them. Be careful not
+    // to request data beyond the end of the payload to avoid 416 HTTP response
+    // error codes.
+    int64_t next_data_offset = 0;
+    prefs_->GetInt64(kPrefsUpdateStateNextDataOffset, &next_data_offset);
+    uint64_t resume_offset = manifest_metadata_size + next_data_offset;
+    if (!install_plan_.payload_size ||
+        resume_offset < install_plan_.payload_size) {
+      fetcher->AddRange(resume_offset);
+    }
+  } else {
+    fetcher->AddRange(0);
+  }
+}
+
+bool UpdateAttempterAndroid::WriteUpdateCompletedMarker() {
+  string boot_id;
+  TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id));
+  prefs_->SetString(kPrefsUpdateCompletedOnBootId, boot_id);
+  return true;
+}
+
+bool UpdateAttempterAndroid::UpdateCompletedOnThisBoot() {
+  // In case of an update_engine restart without a reboot, we stored the boot_id
+  // when the update was completed by setting a pref, so we can check whether
+  // the last update was on this boot or a previous one.
+  string boot_id;
+  TEST_AND_RETURN_FALSE(utils::GetBootId(&boot_id));
+
+  string update_completed_on_boot_id;
+  return (prefs_->Exists(kPrefsUpdateCompletedOnBootId) &&
+          prefs_->GetString(kPrefsUpdateCompletedOnBootId,
+                            &update_completed_on_boot_id) &&
+          update_completed_on_boot_id == boot_id);
+}
+
+}  // namespace chromeos_update_engine
diff --git a/update_attempter_android.h b/update_attempter_android.h
new file mode 100644
index 0000000..a2e3169
--- /dev/null
+++ b/update_attempter_android.h
@@ -0,0 +1,165 @@
+//
+// Copyright (C) 2016 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_
+#define UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_
+
+#include <stdint.h>
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <base/time/time.h>
+
+#include "update_engine/client_library/include/update_engine/update_status.h"
+#include "update_engine/common/action_processor.h"
+#include "update_engine/common/boot_control_interface.h"
+#include "update_engine/common/cpu_limiter.h"
+#include "update_engine/common/hardware_interface.h"
+#include "update_engine/common/prefs_interface.h"
+#include "update_engine/payload_consumer/download_action.h"
+#include "update_engine/service_delegate_android_interface.h"
+#include "update_engine/service_observer_interface.h"
+
+namespace chromeos_update_engine {
+
+class DaemonStateAndroid;
+
+class UpdateAttempterAndroid : public ServiceDelegateAndroidInterface,
+                               public ActionProcessorDelegate,
+                               public DownloadActionDelegate {
+ public:
+  using UpdateStatus = update_engine::UpdateStatus;
+
+  UpdateAttempterAndroid(DaemonStateAndroid* daemon_state,
+                         PrefsInterface* prefs,
+                         BootControlInterface* boot_control_,
+                         HardwareInterface* hardware_);
+  ~UpdateAttempterAndroid() override;
+
+  // Further initialization to be done post construction.
+  void Init();
+
+  // ServiceDelegateAndroidInterface overrides.
+  bool ApplyPayload(const std::string& payload_url,
+                    int64_t payload_offset,
+                    int64_t payload_size,
+                    const std::vector<std::string>& key_value_pair_headers,
+                    brillo::ErrorPtr* error) override;
+  bool SuspendUpdate(brillo::ErrorPtr* error) override;
+  bool ResumeUpdate(brillo::ErrorPtr* error) override;
+  bool CancelUpdate(brillo::ErrorPtr* error) override;
+
+  // ActionProcessorDelegate methods:
+  void ProcessingDone(const ActionProcessor* processor,
+                      ErrorCode code) override;
+  void ProcessingStopped(const ActionProcessor* processor) override;
+  void ActionCompleted(ActionProcessor* processor,
+                       AbstractAction* action,
+                       ErrorCode code) override;
+
+  // DownloadActionDelegate overrides.
+  void BytesReceived(uint64_t bytes_progressed,
+                     uint64_t bytes_received,
+                     uint64_t total) override;
+  bool ShouldCancel(ErrorCode* cancel_reason) override;
+  void DownloadComplete() override;
+
+ private:
+  // Asynchronously marks the current slot as successful if needed. If already
+  // marked as good, CompleteUpdateBootFlags() is called starting the action
+  // processor.
+  void UpdateBootFlags();
+
+  // Called when the boot flags have been updated.
+  void CompleteUpdateBootFlags(bool success);
+
+  // Schedules an event loop callback to start the action processor. This is
+  // scheduled asynchronously to unblock the event loop.
+  void ScheduleProcessingStart();
+
+  // Notifies an update request completed with the given error |code| to all
+  // observers.
+  void TerminateUpdateAndNotify(ErrorCode error_code);
+
+  // Sets the status to the given |status| and notifies a status update to
+  // all observers.
+  void SetStatusAndNotify(UpdateStatus status);
+
+  // Helper method to construct the sequence of actions to be performed for
+  // applying an update.
+  void BuildUpdateActions();
+
+  // Sets up the download parameters based on the update requested on the
+  // |install_plan_|.
+  void SetupDownload();
+
+  // Writes to the processing completed marker. Does nothing if
+  // |update_completed_marker_| is empty.
+  bool WriteUpdateCompletedMarker();
+
+  // Returns whether an update was completed in the current boot.
+  bool UpdateCompletedOnThisBoot();
+
+  DaemonStateAndroid* daemon_state_;
+
+  // DaemonStateAndroid pointers.
+  PrefsInterface* prefs_;
+  BootControlInterface* boot_control_;
+  HardwareInterface* hardware_;
+
+  // Last status notification timestamp used for throttling. Use monotonic
+  // TimeTicks to ensure that notifications are sent even if the system clock is
+  // set back in the middle of an update.
+  base::TimeTicks last_notify_time_;
+
+  // The list of actions and action processor that runs them asynchronously.
+  // Only used when |ongoing_update_| is true.
+  std::vector<std::shared_ptr<AbstractAction>> actions_;
+  std::unique_ptr<ActionProcessor> processor_;
+
+  // Pointer to the DownloadAction in the actions_ vector.
+  std::shared_ptr<DownloadAction> download_action_;
+
+  // Whether there is an ongoing update. This implies that an update was started
+  // but not finished yet. This value will be true even if the update was
+  // suspended.
+  bool ongoing_update_{false};
+
+  // The InstallPlan used during the ongoing update.
+  InstallPlan install_plan_;
+
+  // For status:
+  UpdateStatus status_{UpdateStatus::IDLE};
+  double download_progress_{0.0};
+
+  // Only direct proxy supported.
+  DirectProxyResolver proxy_resolver_;
+
+  // CPU limiter during the update.
+  CPULimiter cpu_limiter_;
+
+  // Whether we have marked the current slot as good. This step is required
+  // before applying an update to the other slot.
+  bool updated_boot_flags_ = false;
+
+  DISALLOW_COPY_AND_ASSIGN(UpdateAttempterAndroid);
+};
+
+}  // namespace chromeos_update_engine
+
+#endif  // UPDATE_ENGINE_UPDATE_ATTEMPTER_ANDROID_H_