update_engine: Create cros vs. aosp boundary clear

Its time to make the boundary between Chrome OS and Android code more
clear. This CL moves all CrOS only code to "chromeos" directory and the
same for Android (in "android" directory). This way we would easily know
which code is uses in which project and can keep the code cleaner and
more maintainable.

One big remaining problem is download_action* files. It seems like
DownloadAction class does a lot of things that chrome OS needs and it
depends on a lot of Chrome OS stuff, but Android is also using thie
Action in a way that circumvent the Chrome OS stuff. For example Android
checks for SystemState to be nullptr to not do things. This is really
fragile and needs to change. Probably Android Team has to implement
their own DownloadAction of some sort and not re use the Chrome OS one
in a very fragile way.

Removed a few android files that have not been used anywhere.

Changed some clang-format and lint issues in order to pass preupload.

BUG=b:171829801
TEST=cros_workon_make --board reef --test update_engine

Change-Id: I3fff1d4a100a065a5c1484a845241b5521614d9f
Reviewed-on: https://chromium-review.googlesource.com/c/aosp/platform/system/update_engine/+/2508965
Tested-by: Amin Hassani <ahassani@chromium.org>
Auto-Submit: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Jae Hoon Kim <kimjae@chromium.org>
Reviewed-by: Tianjie Xu <xunchang@google.com>
Reviewed-by: Kelvin Zhang <zhangkelvin@google.com>
Commit-Queue: Amin Hassani <ahassani@chromium.org>
diff --git a/payload_consumer/delta_performer.cc b/payload_consumer/delta_performer.cc
index d2ed24a..e6ec67a 100644
--- a/payload_consumer/delta_performer.cc
+++ b/payload_consumer/delta_performer.cc
@@ -41,6 +41,7 @@
 #include <puffin/puffpatch.h>
 
 #include "update_engine/common/constants.h"
+#include "update_engine/common/download_action.h"
 #include "update_engine/common/error_code.h"
 #include "update_engine/common/error_code_utils.h"
 #include "update_engine/common/hardware_interface.h"
@@ -50,7 +51,6 @@
 #include "update_engine/payload_consumer/bzip_extent_writer.h"
 #include "update_engine/payload_consumer/cached_file_descriptor.h"
 #include "update_engine/payload_consumer/certificate_parser_interface.h"
-#include "update_engine/payload_consumer/download_action.h"
 #include "update_engine/payload_consumer/extent_reader.h"
 #include "update_engine/payload_consumer/extent_writer.h"
 #include "update_engine/payload_consumer/partition_update_generator_interface.h"
@@ -497,6 +497,7 @@
                  << "Trusting metadata size in payload = " << metadata_size_;
   }
 
+  // NOLINTNEXTLINE(whitespace/braces)
   auto [payload_verifier, perform_verification] = CreatePayloadVerifier();
   if (!payload_verifier) {
     LOG(ERROR) << "Failed to create payload verifier.";
@@ -1809,6 +1810,7 @@
       ErrorCode::kPayloadHashMismatchError,
       payload_hash_calculator_.raw_hash() == update_check_response_hash);
 
+  // NOLINTNEXTLINE(whitespace/braces)
   auto [payload_verifier, perform_verification] = CreatePayloadVerifier();
   if (!perform_verification) {
     LOG(WARNING) << "Not verifying signed delta payload -- missing public key.";
diff --git a/payload_consumer/delta_performer_fuzzer.cc b/payload_consumer/delta_performer_fuzzer.cc
index 73082c4..0ce5081 100644
--- a/payload_consumer/delta_performer_fuzzer.cc
+++ b/payload_consumer/delta_performer_fuzzer.cc
@@ -18,11 +18,11 @@
 #include <base/logging.h>
 #include <fuzzer/FuzzedDataProvider.h>
 
+#include "update_engine/common/download_action.h"
 #include "update_engine/common/fake_boot_control.h"
 #include "update_engine/common/fake_hardware.h"
 #include "update_engine/common/prefs.h"
 #include "update_engine/payload_consumer/delta_performer.h"
-#include "update_engine/payload_consumer/download_action.h"
 #include "update_engine/payload_consumer/install_plan.h"
 
 namespace chromeos_update_engine {
diff --git a/payload_consumer/delta_performer_integration_test.cc b/payload_consumer/delta_performer_integration_test.cc
index 74ddd27..374131e 100644
--- a/payload_consumer/delta_performer_integration_test.cc
+++ b/payload_consumer/delta_performer_integration_test.cc
@@ -37,12 +37,12 @@
 #include "update_engine/common/fake_boot_control.h"
 #include "update_engine/common/fake_hardware.h"
 #include "update_engine/common/fake_prefs.h"
+#include "update_engine/common/hardware_interface.h"
+#include "update_engine/common/mock_download_action.h"
 #include "update_engine/common/mock_prefs.h"
 #include "update_engine/common/test_utils.h"
 #include "update_engine/common/utils.h"
-#include "update_engine/hardware_android.h"
 #include "update_engine/payload_consumer/install_plan.h"
-#include "update_engine/payload_consumer/mock_download_action.h"
 #include "update_engine/payload_consumer/payload_constants.h"
 #include "update_engine/payload_consumer/payload_metadata.h"
 #include "update_engine/payload_consumer/payload_verifier.h"
diff --git a/payload_consumer/delta_performer_unittest.cc b/payload_consumer/delta_performer_unittest.cc
index 65b9dac..9269882 100644
--- a/payload_consumer/delta_performer_unittest.cc
+++ b/payload_consumer/delta_performer_unittest.cc
@@ -41,10 +41,10 @@
 #include "update_engine/common/fake_hardware.h"
 #include "update_engine/common/fake_prefs.h"
 #include "update_engine/common/hardware_interface.h"
+#include "update_engine/common/mock_download_action.h"
 #include "update_engine/common/test_utils.h"
 #include "update_engine/common/utils.h"
 #include "update_engine/payload_consumer/fake_file_descriptor.h"
-#include "update_engine/payload_consumer/mock_download_action.h"
 #include "update_engine/payload_consumer/payload_constants.h"
 #include "update_engine/payload_consumer/payload_metadata.h"
 #include "update_engine/payload_generator/bzip.h"
diff --git a/payload_consumer/download_action.cc b/payload_consumer/download_action.cc
deleted file mode 100644
index ea99892..0000000
--- a/payload_consumer/download_action.cc
+++ /dev/null
@@ -1,468 +0,0 @@
-//
-// Copyright (C) 2011 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/payload_consumer/download_action.h"
-
-#include <errno.h>
-
-#include <algorithm>
-#include <string>
-
-#include <base/files/file_path.h>
-#include <base/metrics/statistics_recorder.h>
-#include <base/strings/stringprintf.h>
-
-#include "update_engine/common/action_pipe.h"
-#include "update_engine/common/boot_control_interface.h"
-#include "update_engine/common/error_code_utils.h"
-#include "update_engine/common/multi_range_http_fetcher.h"
-#include "update_engine/common/utils.h"
-#include "update_engine/omaha_request_params.h"
-#include "update_engine/p2p_manager.h"
-#include "update_engine/payload_state_interface.h"
-
-using base::FilePath;
-using std::string;
-
-namespace chromeos_update_engine {
-
-DownloadAction::DownloadAction(PrefsInterface* prefs,
-                               BootControlInterface* boot_control,
-                               HardwareInterface* hardware,
-                               SystemState* system_state,
-                               HttpFetcher* http_fetcher,
-                               bool interactive)
-    : prefs_(prefs),
-      boot_control_(boot_control),
-      hardware_(hardware),
-      system_state_(system_state),
-      http_fetcher_(new MultiRangeHttpFetcher(http_fetcher)),
-      interactive_(interactive),
-      writer_(nullptr),
-      code_(ErrorCode::kSuccess),
-      delegate_(nullptr),
-      p2p_sharing_fd_(-1),
-      p2p_visible_(true) {}
-
-DownloadAction::~DownloadAction() {}
-
-void DownloadAction::CloseP2PSharingFd(bool delete_p2p_file) {
-  if (p2p_sharing_fd_ != -1) {
-    if (close(p2p_sharing_fd_) != 0) {
-      PLOG(ERROR) << "Error closing p2p sharing fd";
-    }
-    p2p_sharing_fd_ = -1;
-  }
-
-  if (delete_p2p_file) {
-    FilePath path = system_state_->p2p_manager()->FileGetPath(p2p_file_id_);
-    if (unlink(path.value().c_str()) != 0) {
-      PLOG(ERROR) << "Error deleting p2p file " << path.value();
-    } else {
-      LOG(INFO) << "Deleted p2p file " << path.value();
-    }
-  }
-
-  // Don't use p2p from this point onwards.
-  p2p_file_id_.clear();
-}
-
-bool DownloadAction::SetupP2PSharingFd() {
-  P2PManager* p2p_manager = system_state_->p2p_manager();
-
-  if (!p2p_manager->FileShare(p2p_file_id_, payload_->size)) {
-    LOG(ERROR) << "Unable to share file via p2p";
-    CloseP2PSharingFd(true);  // delete p2p file
-    return false;
-  }
-
-  // File has already been created (and allocated, xattrs been
-  // populated etc.) by FileShare() so just open it for writing.
-  FilePath path = p2p_manager->FileGetPath(p2p_file_id_);
-  p2p_sharing_fd_ = open(path.value().c_str(), O_WRONLY);
-  if (p2p_sharing_fd_ == -1) {
-    PLOG(ERROR) << "Error opening file " << path.value();
-    CloseP2PSharingFd(true);  // Delete p2p file.
-    return false;
-  }
-
-  // Ensure file to share is world-readable, otherwise
-  // p2p-server and p2p-http-server can't access it.
-  //
-  // (Q: Why doesn't the file have mode 0644 already? A: Because
-  // the process-wide umask is set to 0700 in main.cc.)
-  if (fchmod(p2p_sharing_fd_, 0644) != 0) {
-    PLOG(ERROR) << "Error setting mode 0644 on " << path.value();
-    CloseP2PSharingFd(true);  // Delete p2p file.
-    return false;
-  }
-
-  // All good.
-  LOG(INFO) << "Writing payload contents to " << path.value();
-  p2p_manager->FileGetVisible(p2p_file_id_, &p2p_visible_);
-  return true;
-}
-
-void DownloadAction::WriteToP2PFile(const void* data,
-                                    size_t length,
-                                    off_t file_offset) {
-  if (p2p_sharing_fd_ == -1) {
-    if (!SetupP2PSharingFd())
-      return;
-  }
-
-  // Check that the file is at least |file_offset| bytes long - if
-  // it's not something is wrong and we must immediately delete the
-  // file to avoid propagating this problem to other peers.
-  //
-  // How can this happen? It could be that we're resuming an update
-  // after a system crash... in this case, it could be that
-  //
-  //  1. the p2p file didn't get properly synced to stable storage; or
-  //  2. the file was deleted at bootup (it's in /var/cache after all); or
-  //  3. other reasons
-  off_t p2p_size = utils::FileSize(p2p_sharing_fd_);
-  if (p2p_size < 0) {
-    PLOG(ERROR) << "Error getting file status for p2p file";
-    CloseP2PSharingFd(true);  // Delete p2p file.
-    return;
-  }
-  if (p2p_size < file_offset) {
-    LOG(ERROR) << "Wanting to write to file offset " << file_offset
-               << " but existing p2p file is only " << p2p_size << " bytes.";
-    CloseP2PSharingFd(true);  // Delete p2p file.
-    return;
-  }
-
-  off_t cur_file_offset = lseek(p2p_sharing_fd_, file_offset, SEEK_SET);
-  if (cur_file_offset != static_cast<off_t>(file_offset)) {
-    PLOG(ERROR) << "Error seeking to position " << file_offset
-                << " in p2p file";
-    CloseP2PSharingFd(true);  // Delete p2p file.
-  } else {
-    // OK, seeking worked, now write the data
-    ssize_t bytes_written = write(p2p_sharing_fd_, data, length);
-    if (bytes_written != static_cast<ssize_t>(length)) {
-      PLOG(ERROR) << "Error writing " << length << " bytes at file offset "
-                  << file_offset << " in p2p file";
-      CloseP2PSharingFd(true);  // Delete p2p file.
-    }
-  }
-}
-
-void DownloadAction::PerformAction() {
-  http_fetcher_->set_delegate(this);
-
-  // Get the InstallPlan and read it
-  CHECK(HasInputObject());
-  install_plan_ = GetInputObject();
-  install_plan_.Dump();
-
-  bytes_received_ = 0;
-  bytes_received_previous_payloads_ = 0;
-  bytes_total_ = 0;
-  for (const auto& payload : install_plan_.payloads)
-    bytes_total_ += payload.size;
-
-  if (install_plan_.is_resume) {
-    int64_t payload_index = 0;
-    if (prefs_->GetInt64(kPrefsUpdateStatePayloadIndex, &payload_index) &&
-        static_cast<size_t>(payload_index) < install_plan_.payloads.size()) {
-      // Save the index for the resume payload before downloading any previous
-      // payload, otherwise it will be overwritten.
-      resume_payload_index_ = payload_index;
-      for (int i = 0; i < payload_index; i++)
-        install_plan_.payloads[i].already_applied = true;
-    }
-  }
-  // TODO(senj): check that install plan has at least one payload.
-  if (!payload_)
-    payload_ = &install_plan_.payloads[0];
-
-  LOG(INFO) << "Marking new slot as unbootable";
-  if (!boot_control_->MarkSlotUnbootable(install_plan_.target_slot)) {
-    LOG(WARNING) << "Unable to mark new slot "
-                 << BootControlInterface::SlotName(install_plan_.target_slot)
-                 << ". Proceeding with the update anyway.";
-  }
-
-  StartDownloading();
-}
-
-bool DownloadAction::LoadCachedManifest(int64_t manifest_size) {
-  std::string cached_manifest_bytes;
-  if (!prefs_->GetString(kPrefsManifestBytes, &cached_manifest_bytes) ||
-      cached_manifest_bytes.size() <= 0) {
-    LOG(INFO) << "Cached Manifest data not found";
-    return false;
-  }
-  if (static_cast<int64_t>(cached_manifest_bytes.size()) != manifest_size) {
-    LOG(WARNING) << "Cached metadata has unexpected size: "
-                 << cached_manifest_bytes.size() << " vs. " << manifest_size;
-    return false;
-  }
-
-  ErrorCode error;
-  const bool success =
-      delta_performer_->Write(
-          cached_manifest_bytes.data(), cached_manifest_bytes.size(), &error) &&
-      delta_performer_->IsManifestValid();
-  if (success) {
-    LOG(INFO) << "Successfully parsed cached manifest";
-  } else {
-    // If parsing of cached data failed, fall back to fetch them using HTTP
-    LOG(WARNING) << "Cached manifest data fails to load, error code:"
-                 << static_cast<int>(error) << "," << error;
-  }
-  return success;
-}
-
-void DownloadAction::StartDownloading() {
-  download_active_ = true;
-  http_fetcher_->ClearRanges();
-
-  if (writer_ && writer_ != delta_performer_.get()) {
-    LOG(INFO) << "Using writer for test.";
-  } else {
-    delta_performer_.reset(new DeltaPerformer(prefs_,
-                                              boot_control_,
-                                              hardware_,
-                                              delegate_,
-                                              &install_plan_,
-                                              payload_,
-                                              interactive_));
-    writer_ = delta_performer_.get();
-  }
-
-  if (install_plan_.is_resume &&
-      payload_ == &install_plan_.payloads[resume_payload_index_]) {
-    // Resuming an update so parse the cached manifest first
-    int64_t manifest_metadata_size = 0;
-    int64_t manifest_signature_size = 0;
-    prefs_->GetInt64(kPrefsManifestMetadataSize, &manifest_metadata_size);
-    prefs_->GetInt64(kPrefsManifestSignatureSize, &manifest_signature_size);
-
-    // TODO(zhangkelvin) Add unittest for success and fallback route
-    if (!LoadCachedManifest(manifest_metadata_size + manifest_signature_size)) {
-      if (delta_performer_) {
-        // Create a new DeltaPerformer to reset all its state
-        delta_performer_ = std::make_unique<DeltaPerformer>(prefs_,
-                                                            boot_control_,
-                                                            hardware_,
-                                                            delegate_,
-                                                            &install_plan_,
-                                                            payload_,
-                                                            interactive_);
-        writer_ = delta_performer_.get();
-      }
-      http_fetcher_->AddRange(base_offset_,
-                              manifest_metadata_size + manifest_signature_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 + manifest_signature_size + next_data_offset;
-    if (!payload_->size) {
-      http_fetcher_->AddRange(base_offset_ + resume_offset);
-    } else if (resume_offset < payload_->size) {
-      http_fetcher_->AddRange(base_offset_ + resume_offset,
-                              payload_->size - resume_offset);
-    }
-  } else {
-    if (payload_->size) {
-      http_fetcher_->AddRange(base_offset_, payload_->size);
-    } else {
-      // If no payload size is passed we assume we read until the end of the
-      // stream.
-      http_fetcher_->AddRange(base_offset_);
-    }
-  }
-
-  if (system_state_ != nullptr) {
-    const PayloadStateInterface* payload_state = system_state_->payload_state();
-    string file_id = utils::CalculateP2PFileId(payload_->hash, payload_->size);
-    if (payload_state->GetUsingP2PForSharing()) {
-      // If we're sharing the update, store the file_id to convey
-      // that we should write to the file.
-      p2p_file_id_ = file_id;
-      LOG(INFO) << "p2p file id: " << p2p_file_id_;
-    } else {
-      // Even if we're not sharing the update, it could be that
-      // there's a partial file from a previous attempt with the same
-      // hash. If this is the case, we NEED to clean it up otherwise
-      // we're essentially timing out other peers downloading from us
-      // (since we're never going to complete the file).
-      FilePath path = system_state_->p2p_manager()->FileGetPath(file_id);
-      if (!path.empty()) {
-        if (unlink(path.value().c_str()) != 0) {
-          PLOG(ERROR) << "Error deleting p2p file " << path.value();
-        } else {
-          LOG(INFO) << "Deleting partial p2p file " << path.value()
-                    << " since we're not using p2p to share.";
-        }
-      }
-    }
-
-    // Tweak timeouts on the HTTP fetcher if we're downloading from a
-    // local peer.
-    if (payload_state->GetUsingP2PForDownloading() &&
-        payload_state->GetP2PUrl() == install_plan_.download_url) {
-      LOG(INFO) << "Tweaking HTTP fetcher since we're downloading via p2p";
-      http_fetcher_->set_low_speed_limit(kDownloadP2PLowSpeedLimitBps,
-                                         kDownloadP2PLowSpeedTimeSeconds);
-      http_fetcher_->set_max_retry_count(kDownloadP2PMaxRetryCount);
-      http_fetcher_->set_connect_timeout(kDownloadP2PConnectTimeoutSeconds);
-    }
-  }
-
-  http_fetcher_->BeginTransfer(install_plan_.download_url);
-}
-
-void DownloadAction::SuspendAction() {
-  http_fetcher_->Pause();
-}
-
-void DownloadAction::ResumeAction() {
-  http_fetcher_->Unpause();
-}
-
-void DownloadAction::TerminateProcessing() {
-  if (writer_) {
-    writer_->Close();
-    writer_ = nullptr;
-  }
-  download_active_ = false;
-  CloseP2PSharingFd(false);  // Keep p2p file.
-  // Terminates the transfer. The action is terminated, if necessary, when the
-  // TransferTerminated callback is received.
-  http_fetcher_->TerminateTransfer();
-}
-
-void DownloadAction::SeekToOffset(off_t offset) {
-  bytes_received_ = offset;
-}
-
-bool DownloadAction::ReceivedBytes(HttpFetcher* fetcher,
-                                   const void* bytes,
-                                   size_t length) {
-  // Note that bytes_received_ is the current offset.
-  if (!p2p_file_id_.empty()) {
-    WriteToP2PFile(bytes, length, bytes_received_);
-  }
-
-  bytes_received_ += length;
-  uint64_t bytes_downloaded_total =
-      bytes_received_previous_payloads_ + bytes_received_;
-  if (delegate_ && download_active_) {
-    delegate_->BytesReceived(length, bytes_downloaded_total, bytes_total_);
-  }
-  if (writer_ && !writer_->Write(bytes, length, &code_)) {
-    if (code_ != ErrorCode::kSuccess) {
-      LOG(ERROR) << "Error " << utils::ErrorCodeToString(code_) << " (" << code_
-                 << ") in DeltaPerformer's Write method when "
-                 << "processing the received payload -- Terminating processing";
-    }
-    // Delete p2p file, if applicable.
-    if (!p2p_file_id_.empty())
-      CloseP2PSharingFd(true);
-    // Don't tell the action processor that the action is complete until we get
-    // the TransferTerminated callback. Otherwise, this and the HTTP fetcher
-    // objects may get destroyed before all callbacks are complete.
-    TerminateProcessing();
-    return false;
-  }
-
-  // Call p2p_manager_->FileMakeVisible() when we've successfully
-  // verified the manifest!
-  if (!p2p_visible_ && system_state_ && delta_performer_.get() &&
-      delta_performer_->IsManifestValid()) {
-    LOG(INFO) << "Manifest has been validated. Making p2p file visible.";
-    system_state_->p2p_manager()->FileMakeVisible(p2p_file_id_);
-    p2p_visible_ = true;
-  }
-  return true;
-}
-
-void DownloadAction::TransferComplete(HttpFetcher* fetcher, bool successful) {
-  if (writer_) {
-    LOG_IF(WARNING, writer_->Close() != 0) << "Error closing the writer.";
-    if (delta_performer_.get() == writer_) {
-      // no delta_performer_ in tests, so leave the test writer in place
-      writer_ = nullptr;
-    }
-  }
-  download_active_ = false;
-  ErrorCode code =
-      successful ? ErrorCode::kSuccess : ErrorCode::kDownloadTransferError;
-  if (code == ErrorCode::kSuccess) {
-    if (delta_performer_ && !payload_->already_applied)
-      code = delta_performer_->VerifyPayload(payload_->hash, payload_->size);
-    if (code == ErrorCode::kSuccess) {
-      if (payload_ < &install_plan_.payloads.back() &&
-          system_state_->payload_state()->NextPayload()) {
-        LOG(INFO) << "Incrementing to next payload";
-        // No need to reset if this payload was already applied.
-        if (delta_performer_ && !payload_->already_applied)
-          DeltaPerformer::ResetUpdateProgress(prefs_, false);
-        // Start downloading next payload.
-        bytes_received_previous_payloads_ += payload_->size;
-        payload_++;
-        install_plan_.download_url =
-            system_state_->payload_state()->GetCurrentUrl();
-        StartDownloading();
-        return;
-      }
-
-      // All payloads have been applied and verified.
-      if (delegate_)
-        delegate_->DownloadComplete();
-
-      // Log UpdateEngine.DownloadAction.* histograms to help diagnose
-      // long-blocking operations.
-      std::string histogram_output;
-      base::StatisticsRecorder::WriteGraph("UpdateEngine.DownloadAction.",
-                                           &histogram_output);
-      LOG(INFO) << histogram_output;
-    } else {
-      LOG(ERROR) << "Download of " << install_plan_.download_url
-                 << " failed due to payload verification error.";
-      // Delete p2p file, if applicable.
-      if (!p2p_file_id_.empty())
-        CloseP2PSharingFd(true);
-    }
-  }
-
-  // Write the path to the output pipe if we're successful.
-  if (code == ErrorCode::kSuccess && HasOutputPipe())
-    SetOutputObject(install_plan_);
-  processor_->ActionComplete(this, code);
-}
-
-void DownloadAction::TransferTerminated(HttpFetcher* fetcher) {
-  if (code_ != ErrorCode::kSuccess) {
-    processor_->ActionComplete(this, code_);
-  } else if (payload_->already_applied) {
-    LOG(INFO) << "TransferTerminated with ErrorCode::kSuccess when the current "
-                 "payload has already applied, treating as TransferComplete.";
-    TransferComplete(fetcher, true);
-  }
-}
-
-}  // namespace chromeos_update_engine
diff --git a/payload_consumer/download_action.h b/payload_consumer/download_action.h
deleted file mode 100644
index 6928443..0000000
--- a/payload_consumer/download_action.h
+++ /dev/null
@@ -1,203 +0,0 @@
-//
-// Copyright (C) 2011 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_PAYLOAD_CONSUMER_DOWNLOAD_ACTION_H_
-#define UPDATE_ENGINE_PAYLOAD_CONSUMER_DOWNLOAD_ACTION_H_
-
-#include <fcntl.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#include <memory>
-#include <string>
-
-#include "update_engine/common/action.h"
-#include "update_engine/common/boot_control_interface.h"
-#include "update_engine/common/http_fetcher.h"
-#include "update_engine/common/multi_range_http_fetcher.h"
-#include "update_engine/payload_consumer/delta_performer.h"
-#include "update_engine/payload_consumer/install_plan.h"
-#include "update_engine/system_state.h"
-
-// The Download Action downloads a specified url to disk. The url should point
-// to an update in a delta payload format. The payload will be piped into a
-// DeltaPerformer that will apply the delta to the disk.
-
-namespace chromeos_update_engine {
-
-class DownloadActionDelegate {
- public:
-  virtual ~DownloadActionDelegate() = default;
-
-  // Called periodically after bytes are received. This method will be invoked
-  // only if the DownloadAction is running. |bytes_progressed| is the number of
-  // bytes downloaded since the last call of this method, |bytes_received|
-  // the number of bytes downloaded thus far and |total| is the number of bytes
-  // expected.
-  virtual void BytesReceived(uint64_t bytes_progressed,
-                             uint64_t bytes_received,
-                             uint64_t total) = 0;
-
-  // Returns whether the download should be canceled, in which case the
-  // |cancel_reason| error should be set to the reason why the download was
-  // canceled.
-  virtual bool ShouldCancel(ErrorCode* cancel_reason) = 0;
-
-  // Called once the complete payload has been downloaded. Note that any errors
-  // while applying or downloading the partial payload will result in this
-  // method not being called.
-  virtual void DownloadComplete() = 0;
-};
-
-class PrefsInterface;
-
-class DownloadAction : public InstallPlanAction, public HttpFetcherDelegate {
- public:
-  // Debugging/logging
-  static std::string StaticType() { return "DownloadAction"; }
-
-  // Takes ownership of the passed in HttpFetcher. Useful for testing.
-  // A good calling pattern is:
-  // DownloadAction(prefs, boot_contol, hardware, system_state,
-  //                new WhateverHttpFetcher, false);
-  DownloadAction(PrefsInterface* prefs,
-                 BootControlInterface* boot_control,
-                 HardwareInterface* hardware,
-                 SystemState* system_state,
-                 HttpFetcher* http_fetcher,
-                 bool interactive);
-  ~DownloadAction() override;
-
-  // InstallPlanAction overrides.
-  void PerformAction() override;
-  void SuspendAction() override;
-  void ResumeAction() override;
-  void TerminateProcessing() override;
-  std::string Type() const override { return StaticType(); }
-
-  // Testing
-  void SetTestFileWriter(FileWriter* writer) { writer_ = writer; }
-
-  int GetHTTPResponseCode() { return http_fetcher_->http_response_code(); }
-
-  // HttpFetcherDelegate methods (see http_fetcher.h)
-  bool ReceivedBytes(HttpFetcher* fetcher,
-                     const void* bytes,
-                     size_t length) override;
-  void SeekToOffset(off_t offset) override;
-  void TransferComplete(HttpFetcher* fetcher, bool successful) override;
-  void TransferTerminated(HttpFetcher* fetcher) override;
-
-  DownloadActionDelegate* delegate() const { return delegate_; }
-  void set_delegate(DownloadActionDelegate* delegate) { delegate_ = delegate; }
-
-  void set_base_offset(int64_t base_offset) { base_offset_ = base_offset; }
-
-  HttpFetcher* http_fetcher() { return http_fetcher_.get(); }
-
-  // Returns the p2p file id for the file being written or the empty
-  // string if we're not writing to a p2p file.
-  std::string p2p_file_id() { return p2p_file_id_; }
-
- private:
-  // Closes the file descriptor for the p2p file being written and
-  // clears |p2p_file_id_| to indicate that we're no longer sharing
-  // the file. If |delete_p2p_file| is True, also deletes the file.
-  // If there is no p2p file descriptor, this method does nothing.
-  void CloseP2PSharingFd(bool delete_p2p_file);
-
-  // Starts sharing the p2p file. Must be called before
-  // WriteToP2PFile(). Returns True if this worked.
-  bool SetupP2PSharingFd();
-
-  // Writes |length| bytes of payload from |data| into |file_offset|
-  // of the p2p file. Also does validation checks; for example ensures we
-  // don't end up with a file with holes in it.
-  //
-  // This method does nothing if SetupP2PSharingFd() hasn't been
-  // called or if CloseP2PSharingFd() has been called.
-  void WriteToP2PFile(const void* data, size_t length, off_t file_offset);
-
-  // Attempt to load cached manifest data from prefs
-  // return true on success, false otherwise.
-  bool LoadCachedManifest(int64_t manifest_size);
-
-  // Start downloading the current payload using delta_performer.
-  void StartDownloading();
-
-  // Pointer to the current payload in install_plan_.payloads.
-  InstallPlan::Payload* payload_{nullptr};
-
-  // SystemState required pointers.
-  PrefsInterface* prefs_;
-  BootControlInterface* boot_control_;
-  HardwareInterface* hardware_;
-
-  // Global context for the system.
-  SystemState* system_state_;
-
-  // Pointer to the MultiRangeHttpFetcher that does the http work.
-  std::unique_ptr<MultiRangeHttpFetcher> http_fetcher_;
-
-  // If |true|, the update is user initiated (vs. periodic update checks). Hence
-  // the |delta_performer_| can decide not to use O_DSYNC flag for faster
-  // update.
-  bool interactive_;
-
-  // The FileWriter that downloaded data should be written to. It will
-  // either point to *decompressing_file_writer_ or *delta_performer_.
-  FileWriter* writer_;
-
-  std::unique_ptr<DeltaPerformer> delta_performer_;
-
-  // Used by TransferTerminated to figure if this action terminated itself or
-  // was terminated by the action processor.
-  ErrorCode code_;
-
-  // For reporting status to outsiders
-  DownloadActionDelegate* delegate_;
-  uint64_t bytes_received_{0};  // per file/range
-  uint64_t bytes_received_previous_payloads_{0};
-  uint64_t bytes_total_{0};
-  bool download_active_{false};
-
-  // The file-id for the file we're sharing or the empty string
-  // if we're not using p2p to share.
-  std::string p2p_file_id_;
-
-  // The file descriptor for the p2p file used for caching the payload or -1
-  // if we're not using p2p to share.
-  int p2p_sharing_fd_;
-
-  // Set to |false| if p2p file is not visible.
-  bool p2p_visible_;
-
-  // Loaded from prefs before downloading any payload.
-  size_t resume_payload_index_{0};
-
-  // Offset of the payload in the download URL, used by UpdateAttempterAndroid.
-  int64_t base_offset_{0};
-
-  DISALLOW_COPY_AND_ASSIGN(DownloadAction);
-};
-
-// We want to be sure that we're compiled with large file support on linux,
-// just in case we find ourselves downloading large images.
-static_assert(8 == sizeof(off_t), "off_t not 64 bit");
-
-}  // namespace chromeos_update_engine
-
-#endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_DOWNLOAD_ACTION_H_
diff --git a/payload_consumer/download_action_android_unittest.cc b/payload_consumer/download_action_android_unittest.cc
deleted file mode 100644
index f78845f..0000000
--- a/payload_consumer/download_action_android_unittest.cc
+++ /dev/null
@@ -1,90 +0,0 @@
-//
-// Copyright (C) 2020 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 "common/mock_action_processor.h"
-#include <gmock/gmock-actions.h>
-#include <gmock/gmock-function-mocker.h>
-#include <gmock/gmock-spec-builders.h>
-
-#include "payload_consumer/install_plan.h"
-#include "update_engine/common/action_pipe.h"
-#include "update_engine/common/boot_control_stub.h"
-#include "update_engine/common/constants.h"
-#include "update_engine/common/mock_http_fetcher.h"
-#include "update_engine/common/mock_prefs.h"
-#include "update_engine/common/test_utils.h"
-#include "update_engine/payload_consumer/download_action.h"
-
-#include <gmock/gmock.h>
-#include <gtest/gtest.h>
-#include <memory>
-
-namespace chromeos_update_engine {
-using testing::_;
-using testing::DoAll;
-using testing::Return;
-using testing::SetArgPointee;
-
-class DownloadActionTest : public ::testing::Test {
- public:
-  static constexpr int64_t METADATA_SIZE = 1024;
-  static constexpr int64_t SIGNATURE_SIZE = 256;
-  std::shared_ptr<ActionPipe<InstallPlan>> action_pipe{
-      new ActionPipe<InstallPlan>()};
-};
-
-TEST_F(DownloadActionTest, CacheManifestInvalid) {
-  std::string data(METADATA_SIZE + SIGNATURE_SIZE, '-');
-  MockPrefs prefs;
-  EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStatePayloadIndex, _))
-      .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true)));
-  EXPECT_CALL(prefs, GetInt64(kPrefsManifestMetadataSize, _))
-      .WillRepeatedly(DoAll(SetArgPointee<1>(METADATA_SIZE), Return(true)));
-  EXPECT_CALL(prefs, GetInt64(kPrefsManifestSignatureSize, _))
-      .WillRepeatedly(DoAll(SetArgPointee<1>(SIGNATURE_SIZE), Return(true)));
-  EXPECT_CALL(prefs, GetInt64(kPrefsUpdateStateNextDataOffset, _))
-      .WillRepeatedly(DoAll(SetArgPointee<1>(0L), Return(true)));
-  EXPECT_CALL(prefs, GetString(kPrefsManifestBytes, _))
-      .WillRepeatedly(DoAll(SetArgPointee<1>(data), Return(true)));
-
-  BootControlStub boot_control;
-  MockHttpFetcher* http_fetcher =
-      new MockHttpFetcher(data.data(), data.size(), nullptr);
-  http_fetcher->set_delay(false);
-  InstallPlan install_plan;
-  auto& payload = install_plan.payloads.emplace_back();
-  install_plan.download_url = "http://fake_url.invalid";
-  payload.size = data.size();
-  payload.payload_urls.emplace_back("http://fake_url.invalid");
-  install_plan.is_resume = true;
-  action_pipe->set_contents(install_plan);
-
-  // takes ownership of passed in HttpFetcher
-  auto download_action =
-      std::make_unique<DownloadAction>(&prefs,
-                                       &boot_control,
-                                       nullptr,
-                                       nullptr,
-                                       http_fetcher,
-                                       false /* interactive */);
-  download_action->set_in_pipe(action_pipe);
-  MockActionProcessor mock_processor;
-  download_action->SetProcessor(&mock_processor);
-  download_action->PerformAction();
-  ASSERT_EQ(download_action->http_fetcher()->GetBytesDownloaded(), data.size());
-}
-
-}  // namespace chromeos_update_engine
diff --git a/payload_consumer/download_action_unittest.cc b/payload_consumer/download_action_unittest.cc
deleted file mode 100644
index 9daa791..0000000
--- a/payload_consumer/download_action_unittest.cc
+++ /dev/null
@@ -1,707 +0,0 @@
-//
-// Copyright (C) 2011 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/payload_consumer/download_action.h"
-
-#include <gmock/gmock.h>
-#include <gtest/gtest.h>
-
-#include <memory>
-#include <string>
-#include <utility>
-#include <vector>
-
-#include <base/bind.h>
-#include <base/files/file_path.h>
-#include <base/files/file_util.h>
-#include <base/location.h>
-#include <base/strings/stringprintf.h>
-#include <brillo/message_loops/fake_message_loop.h>
-#include <brillo/message_loops/message_loop.h>
-
-#include "update_engine/common/action_pipe.h"
-#include "update_engine/common/hash_calculator.h"
-#include "update_engine/common/mock_http_fetcher.h"
-#include "update_engine/common/mock_prefs.h"
-#include "update_engine/common/test_utils.h"
-#include "update_engine/common/utils.h"
-#include "update_engine/fake_p2p_manager_configuration.h"
-#include "update_engine/fake_system_state.h"
-#include "update_engine/mock_file_writer.h"
-#include "update_engine/payload_consumer/mock_download_action.h"
-#include "update_engine/update_manager/fake_update_manager.h"
-
-namespace chromeos_update_engine {
-
-using base::FilePath;
-using base::ReadFileToString;
-using base::WriteFile;
-using std::string;
-using std::unique_ptr;
-using testing::_;
-using testing::AtLeast;
-using testing::InSequence;
-using testing::Return;
-using testing::SetArgPointee;
-
-class DownloadActionTest : public ::testing::Test {};
-
-namespace {
-
-class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
- public:
-  DownloadActionTestProcessorDelegate()
-      : processing_done_called_(false), expected_code_(ErrorCode::kSuccess) {}
-  ~DownloadActionTestProcessorDelegate() override {
-    EXPECT_TRUE(processing_done_called_);
-  }
-  void ProcessingDone(const ActionProcessor* processor,
-                      ErrorCode code) override {
-    brillo::MessageLoop::current()->BreakLoop();
-    brillo::Blob found_data;
-    ASSERT_TRUE(utils::ReadFile(path_, &found_data));
-    if (expected_code_ != ErrorCode::kDownloadWriteError) {
-      ASSERT_EQ(expected_data_.size(), found_data.size());
-      for (unsigned i = 0; i < expected_data_.size(); i++) {
-        EXPECT_EQ(expected_data_[i], found_data[i]);
-      }
-    }
-    processing_done_called_ = true;
-  }
-
-  void ActionCompleted(ActionProcessor* processor,
-                       AbstractAction* action,
-                       ErrorCode code) override {
-    const string type = action->Type();
-    if (type == DownloadAction::StaticType()) {
-      EXPECT_EQ(expected_code_, code);
-      p2p_file_id_ = static_cast<DownloadAction*>(action)->p2p_file_id();
-    } else {
-      EXPECT_EQ(ErrorCode::kSuccess, code);
-    }
-  }
-
-  string path_;
-  brillo::Blob expected_data_;
-  bool processing_done_called_;
-  ErrorCode expected_code_;
-  string p2p_file_id_;
-};
-
-class TestDirectFileWriter : public DirectFileWriter {
- public:
-  TestDirectFileWriter() : fail_write_(0), current_write_(0) {}
-  void set_fail_write(int fail_write) { fail_write_ = fail_write; }
-
-  virtual bool Write(const void* bytes, size_t count) {
-    if (++current_write_ == fail_write_) {
-      return false;
-    }
-    return DirectFileWriter::Write(bytes, count);
-  }
-
- private:
-  // If positive, fail on the |fail_write_| call to Write.
-  int fail_write_;
-  int current_write_;
-};
-
-void StartProcessorInRunLoop(ActionProcessor* processor,
-                             MockHttpFetcher* http_fetcher) {
-  processor->StartProcessing();
-  http_fetcher->SetOffset(1);
-}
-
-void TestWithData(const brillo::Blob& data,
-                  int fail_write,
-                  bool use_download_delegate) {
-  brillo::FakeMessageLoop loop(nullptr);
-  loop.SetAsCurrent();
-  FakeSystemState fake_system_state;
-
-  ScopedTempFile output_temp_file;
-  TestDirectFileWriter writer;
-  EXPECT_EQ(
-      0, writer.Open(output_temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
-  writer.set_fail_write(fail_write);
-
-  uint64_t size = data.size() - 1;
-  InstallPlan install_plan;
-  install_plan.payloads.push_back(
-      {.size = size, .type = InstallPayloadType::kDelta});
-  // We pull off the first byte from data and seek past it.
-  EXPECT_TRUE(HashCalculator::RawHashOfBytes(
-      &data[1], data.size() - 1, &install_plan.payloads[0].hash));
-  install_plan.source_slot = 0;
-  install_plan.target_slot = 1;
-  // We mark both slots as bootable. Only the target slot should be unbootable
-  // after the download starts.
-  fake_system_state.fake_boot_control()->SetSlotBootable(
-      install_plan.source_slot, true);
-  fake_system_state.fake_boot_control()->SetSlotBootable(
-      install_plan.target_slot, true);
-  auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
-  feeder_action->set_obj(install_plan);
-  MockPrefs prefs;
-  MockHttpFetcher* http_fetcher =
-      new MockHttpFetcher(data.data(), data.size(), nullptr);
-  // takes ownership of passed in HttpFetcher
-  auto download_action =
-      std::make_unique<DownloadAction>(&prefs,
-                                       fake_system_state.boot_control(),
-                                       fake_system_state.hardware(),
-                                       &fake_system_state,
-                                       http_fetcher,
-                                       false /* interactive */);
-  download_action->SetTestFileWriter(&writer);
-  BondActions(feeder_action.get(), download_action.get());
-  MockDownloadActionDelegate download_delegate;
-  if (use_download_delegate) {
-    InSequence s;
-    download_action->set_delegate(&download_delegate);
-    if (data.size() > kMockHttpFetcherChunkSize)
-      EXPECT_CALL(download_delegate,
-                  BytesReceived(_, kMockHttpFetcherChunkSize, _));
-    EXPECT_CALL(download_delegate, BytesReceived(_, _, _)).Times(AtLeast(1));
-    EXPECT_CALL(download_delegate, DownloadComplete())
-        .Times(fail_write == 0 ? 1 : 0);
-  }
-  DownloadActionTestProcessorDelegate delegate;
-  delegate.expected_code_ =
-      (fail_write > 0) ? ErrorCode::kDownloadWriteError : ErrorCode::kSuccess;
-  delegate.expected_data_ = brillo::Blob(data.begin() + 1, data.end());
-  delegate.path_ = output_temp_file.path();
-  ActionProcessor processor;
-  processor.set_delegate(&delegate);
-  processor.EnqueueAction(std::move(feeder_action));
-  processor.EnqueueAction(std::move(download_action));
-
-  loop.PostTask(FROM_HERE,
-                base::Bind(&StartProcessorInRunLoop, &processor, http_fetcher));
-  loop.Run();
-  EXPECT_FALSE(loop.PendingTasks());
-
-  EXPECT_TRUE(fake_system_state.fake_boot_control()->IsSlotBootable(
-      install_plan.source_slot));
-  EXPECT_FALSE(fake_system_state.fake_boot_control()->IsSlotBootable(
-      install_plan.target_slot));
-}
-}  // namespace
-
-TEST(DownloadActionTest, SimpleTest) {
-  brillo::Blob small;
-  const char* foo = "foo";
-  small.insert(small.end(), foo, foo + strlen(foo));
-  TestWithData(small,
-               0,      // fail_write
-               true);  // use_download_delegate
-}
-
-TEST(DownloadActionTest, LargeTest) {
-  brillo::Blob big(5 * kMockHttpFetcherChunkSize);
-  char c = '0';
-  for (unsigned int i = 0; i < big.size(); i++) {
-    big[i] = c;
-    c = ('9' == c) ? '0' : c + 1;
-  }
-  TestWithData(big,
-               0,      // fail_write
-               true);  // use_download_delegate
-}
-
-TEST(DownloadActionTest, FailWriteTest) {
-  brillo::Blob big(5 * kMockHttpFetcherChunkSize);
-  char c = '0';
-  for (unsigned int i = 0; i < big.size(); i++) {
-    big[i] = c;
-    c = ('9' == c) ? '0' : c + 1;
-  }
-  TestWithData(big,
-               2,      // fail_write
-               true);  // use_download_delegate
-}
-
-TEST(DownloadActionTest, NoDownloadDelegateTest) {
-  brillo::Blob small;
-  const char* foo = "foofoo";
-  small.insert(small.end(), foo, foo + strlen(foo));
-  TestWithData(small,
-               0,       // fail_write
-               false);  // use_download_delegate
-}
-
-TEST(DownloadActionTest, MultiPayloadProgressTest) {
-  std::vector<brillo::Blob> payload_datas;
-  // the first payload must be the largest, as it's the actual payload used by
-  // the MockHttpFetcher for all downloaded data.
-  payload_datas.emplace_back(4 * kMockHttpFetcherChunkSize + 256);
-  payload_datas.emplace_back(2 * kMockHttpFetcherChunkSize);
-  brillo::FakeMessageLoop loop(nullptr);
-  loop.SetAsCurrent();
-  FakeSystemState fake_system_state;
-  EXPECT_CALL(*fake_system_state.mock_payload_state(), NextPayload())
-      .WillOnce(Return(true));
-
-  MockFileWriter mock_file_writer;
-  EXPECT_CALL(mock_file_writer, Close()).WillRepeatedly(Return(0));
-  EXPECT_CALL(mock_file_writer, Write(_, _, _))
-      .WillRepeatedly(
-          DoAll(SetArgPointee<2>(ErrorCode::kSuccess), Return(true)));
-
-  InstallPlan install_plan;
-  uint64_t total_expected_download_size{0};
-  for (const auto& data : payload_datas) {
-    uint64_t size = data.size();
-    install_plan.payloads.push_back(
-        {.size = size, .type = InstallPayloadType::kFull});
-    total_expected_download_size += size;
-  }
-  auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
-  feeder_action->set_obj(install_plan);
-  MockPrefs prefs;
-  MockHttpFetcher* http_fetcher = new MockHttpFetcher(
-      payload_datas[0].data(), payload_datas[0].size(), nullptr);
-  // takes ownership of passed in HttpFetcher
-  auto download_action =
-      std::make_unique<DownloadAction>(&prefs,
-                                       fake_system_state.boot_control(),
-                                       fake_system_state.hardware(),
-                                       &fake_system_state,
-                                       http_fetcher,
-                                       false /* interactive */);
-  download_action->SetTestFileWriter(&mock_file_writer);
-  BondActions(feeder_action.get(), download_action.get());
-  MockDownloadActionDelegate download_delegate;
-  {
-    InSequence s;
-    download_action->set_delegate(&download_delegate);
-    // these are hand-computed based on the payloads specified above
-    EXPECT_CALL(download_delegate,
-                BytesReceived(kMockHttpFetcherChunkSize,
-                              kMockHttpFetcherChunkSize,
-                              total_expected_download_size));
-    EXPECT_CALL(download_delegate,
-                BytesReceived(kMockHttpFetcherChunkSize,
-                              kMockHttpFetcherChunkSize * 2,
-                              total_expected_download_size));
-    EXPECT_CALL(download_delegate,
-                BytesReceived(kMockHttpFetcherChunkSize,
-                              kMockHttpFetcherChunkSize * 3,
-                              total_expected_download_size));
-    EXPECT_CALL(download_delegate,
-                BytesReceived(kMockHttpFetcherChunkSize,
-                              kMockHttpFetcherChunkSize * 4,
-                              total_expected_download_size));
-    EXPECT_CALL(download_delegate,
-                BytesReceived(256,
-                              kMockHttpFetcherChunkSize * 4 + 256,
-                              total_expected_download_size));
-    EXPECT_CALL(download_delegate,
-                BytesReceived(kMockHttpFetcherChunkSize,
-                              kMockHttpFetcherChunkSize * 5 + 256,
-                              total_expected_download_size));
-    EXPECT_CALL(download_delegate,
-                BytesReceived(kMockHttpFetcherChunkSize,
-                              total_expected_download_size,
-                              total_expected_download_size));
-  }
-  ActionProcessor processor;
-  processor.EnqueueAction(std::move(feeder_action));
-  processor.EnqueueAction(std::move(download_action));
-
-  loop.PostTask(
-      FROM_HERE,
-      base::Bind(
-          [](ActionProcessor* processor) { processor->StartProcessing(); },
-          base::Unretained(&processor)));
-  loop.Run();
-  EXPECT_FALSE(loop.PendingTasks());
-}
-
-namespace {
-class TerminateEarlyTestProcessorDelegate : public ActionProcessorDelegate {
- public:
-  void ProcessingStopped(const ActionProcessor* processor) {
-    brillo::MessageLoop::current()->BreakLoop();
-  }
-};
-
-void TerminateEarlyTestStarter(ActionProcessor* processor) {
-  processor->StartProcessing();
-  CHECK(processor->IsRunning());
-  processor->StopProcessing();
-}
-
-void TestTerminateEarly(bool use_download_delegate) {
-  brillo::FakeMessageLoop loop(nullptr);
-  loop.SetAsCurrent();
-
-  brillo::Blob data(kMockHttpFetcherChunkSize + kMockHttpFetcherChunkSize / 2);
-  memset(data.data(), 0, data.size());
-
-  ScopedTempFile temp_file;
-  {
-    DirectFileWriter writer;
-    EXPECT_EQ(0, writer.Open(temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
-
-    // takes ownership of passed in HttpFetcher
-    auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
-    InstallPlan install_plan;
-    install_plan.payloads.resize(1);
-    feeder_action->set_obj(install_plan);
-    FakeSystemState fake_system_state_;
-    MockPrefs prefs;
-    auto download_action = std::make_unique<DownloadAction>(
-        &prefs,
-        fake_system_state_.boot_control(),
-        fake_system_state_.hardware(),
-        &fake_system_state_,
-        new MockHttpFetcher(data.data(), data.size(), nullptr),
-        false /* interactive */);
-    download_action->SetTestFileWriter(&writer);
-    MockDownloadActionDelegate download_delegate;
-    if (use_download_delegate) {
-      download_action->set_delegate(&download_delegate);
-      EXPECT_CALL(download_delegate, BytesReceived(_, _, _)).Times(0);
-    }
-    TerminateEarlyTestProcessorDelegate delegate;
-    ActionProcessor processor;
-    processor.set_delegate(&delegate);
-    BondActions(feeder_action.get(), download_action.get());
-    processor.EnqueueAction(std::move(feeder_action));
-    processor.EnqueueAction(std::move(download_action));
-
-    loop.PostTask(FROM_HERE,
-                  base::Bind(&TerminateEarlyTestStarter, &processor));
-    loop.Run();
-    EXPECT_FALSE(loop.PendingTasks());
-  }
-
-  // 1 or 0 chunks should have come through
-  const off_t resulting_file_size(utils::FileSize(temp_file.path()));
-  EXPECT_GE(resulting_file_size, 0);
-  if (resulting_file_size != 0)
-    EXPECT_EQ(kMockHttpFetcherChunkSize,
-              static_cast<size_t>(resulting_file_size));
-}
-
-}  // namespace
-
-TEST(DownloadActionTest, TerminateEarlyTest) {
-  TestTerminateEarly(true);
-}
-
-TEST(DownloadActionTest, TerminateEarlyNoDownloadDelegateTest) {
-  TestTerminateEarly(false);
-}
-
-class DownloadActionTestAction;
-
-template <>
-class ActionTraits<DownloadActionTestAction> {
- public:
-  typedef InstallPlan OutputObjectType;
-  typedef InstallPlan InputObjectType;
-};
-
-// This is a simple Action class for testing.
-class DownloadActionTestAction : public Action<DownloadActionTestAction> {
- public:
-  DownloadActionTestAction() = default;
-  typedef InstallPlan InputObjectType;
-  typedef InstallPlan OutputObjectType;
-  ActionPipe<InstallPlan>* in_pipe() { return in_pipe_.get(); }
-  ActionPipe<InstallPlan>* out_pipe() { return out_pipe_.get(); }
-  ActionProcessor* processor() { return processor_; }
-  void PerformAction() {
-    ASSERT_TRUE(HasInputObject());
-    EXPECT_TRUE(expected_input_object_ == GetInputObject());
-    ASSERT_TRUE(processor());
-    processor()->ActionComplete(this, ErrorCode::kSuccess);
-  }
-  static std::string StaticType() { return "DownloadActionTestAction"; }
-  string Type() const { return StaticType(); }
-  InstallPlan expected_input_object_;
-};
-
-namespace {
-// This class is an ActionProcessorDelegate that simply terminates the
-// run loop when the ActionProcessor has completed processing. It's used
-// only by the test PassObjectOutTest.
-class PassObjectOutTestProcessorDelegate : public ActionProcessorDelegate {
- public:
-  void ProcessingDone(const ActionProcessor* processor,
-                      ErrorCode code) override {
-    brillo::MessageLoop::current()->BreakLoop();
-  }
-  void ActionCompleted(ActionProcessor* processor,
-                       AbstractAction* action,
-                       ErrorCode code) override {
-    if (action->Type() == DownloadActionTestAction::StaticType()) {
-      did_test_action_run_ = true;
-    }
-  }
-
-  bool did_test_action_run_ = false;
-};
-
-}  // namespace
-
-TEST(DownloadActionTest, PassObjectOutTest) {
-  brillo::FakeMessageLoop loop(nullptr);
-  loop.SetAsCurrent();
-
-  DirectFileWriter writer;
-  EXPECT_EQ(0, writer.Open("/dev/null", O_WRONLY | O_CREAT, 0));
-
-  // takes ownership of passed in HttpFetcher
-  InstallPlan install_plan;
-  install_plan.payloads.push_back({.size = 1});
-  EXPECT_TRUE(
-      HashCalculator::RawHashOfData({'x'}, &install_plan.payloads[0].hash));
-  auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
-  feeder_action->set_obj(install_plan);
-  MockPrefs prefs;
-  FakeSystemState fake_system_state_;
-  auto download_action =
-      std::make_unique<DownloadAction>(&prefs,
-                                       fake_system_state_.boot_control(),
-                                       fake_system_state_.hardware(),
-                                       &fake_system_state_,
-                                       new MockHttpFetcher("x", 1, nullptr),
-                                       false /* interactive */);
-  download_action->SetTestFileWriter(&writer);
-
-  auto test_action = std::make_unique<DownloadActionTestAction>();
-  test_action->expected_input_object_ = install_plan;
-  BondActions(feeder_action.get(), download_action.get());
-  BondActions(download_action.get(), test_action.get());
-
-  ActionProcessor processor;
-  PassObjectOutTestProcessorDelegate delegate;
-  processor.set_delegate(&delegate);
-  processor.EnqueueAction(std::move(feeder_action));
-  processor.EnqueueAction(std::move(download_action));
-  processor.EnqueueAction(std::move(test_action));
-
-  loop.PostTask(
-      FROM_HERE,
-      base::Bind(
-          [](ActionProcessor* processor) { processor->StartProcessing(); },
-          base::Unretained(&processor)));
-  loop.Run();
-  EXPECT_FALSE(loop.PendingTasks());
-
-  EXPECT_EQ(true, delegate.did_test_action_run_);
-}
-
-// Test fixture for P2P tests.
-class P2PDownloadActionTest : public testing::Test {
- protected:
-  P2PDownloadActionTest()
-      : start_at_offset_(0), fake_um_(fake_system_state_.fake_clock()) {}
-
-  ~P2PDownloadActionTest() override {}
-
-  // Derived from testing::Test.
-  void SetUp() override { loop_.SetAsCurrent(); }
-
-  // Derived from testing::Test.
-  void TearDown() override { EXPECT_FALSE(loop_.PendingTasks()); }
-
-  // To be called by tests to setup the download. The
-  // |starting_offset| parameter is for where to resume.
-  void SetupDownload(off_t starting_offset) {
-    start_at_offset_ = starting_offset;
-    // Prepare data 10 kB of data.
-    data_.clear();
-    for (unsigned int i = 0; i < 10 * 1000; i++)
-      data_ += 'a' + (i % 25);
-
-    // Setup p2p.
-    FakeP2PManagerConfiguration* test_conf = new FakeP2PManagerConfiguration();
-    p2p_manager_.reset(P2PManager::Construct(test_conf,
-                                             nullptr,
-                                             &fake_um_,
-                                             "cros_au",
-                                             3,
-                                             base::TimeDelta::FromDays(5)));
-    fake_system_state_.set_p2p_manager(p2p_manager_.get());
-  }
-
-  // To be called by tests to perform the download. The
-  // |use_p2p_to_share| parameter is used to indicate whether the
-  // payload should be shared via p2p.
-  void StartDownload(bool use_p2p_to_share) {
-    EXPECT_CALL(*fake_system_state_.mock_payload_state(),
-                GetUsingP2PForSharing())
-        .WillRepeatedly(Return(use_p2p_to_share));
-
-    ScopedTempFile output_temp_file;
-    TestDirectFileWriter writer;
-    EXPECT_EQ(
-        0, writer.Open(output_temp_file.path().c_str(), O_WRONLY | O_CREAT, 0));
-    InstallPlan install_plan;
-    install_plan.payloads.push_back(
-        {.size = data_.length(),
-         .hash = {'1', '2', '3', '4', 'h', 'a', 's', 'h'}});
-    auto feeder_action = std::make_unique<ObjectFeederAction<InstallPlan>>();
-    feeder_action->set_obj(install_plan);
-    MockPrefs prefs;
-    // Note that DownloadAction takes ownership of the passed in HttpFetcher.
-    auto download_action = std::make_unique<DownloadAction>(
-        &prefs,
-        fake_system_state_.boot_control(),
-        fake_system_state_.hardware(),
-        &fake_system_state_,
-        new MockHttpFetcher(data_.c_str(), data_.length(), nullptr),
-        false /* interactive */);
-    auto http_fetcher = download_action->http_fetcher();
-    download_action->SetTestFileWriter(&writer);
-    BondActions(feeder_action.get(), download_action.get());
-    delegate_.expected_data_ =
-        brillo::Blob(data_.begin() + start_at_offset_, data_.end());
-    delegate_.path_ = output_temp_file.path();
-    processor_.set_delegate(&delegate_);
-    processor_.EnqueueAction(std::move(feeder_action));
-    processor_.EnqueueAction(std::move(download_action));
-
-    loop_.PostTask(
-        FROM_HERE,
-        base::Bind(
-            [](P2PDownloadActionTest* action_test, HttpFetcher* http_fetcher) {
-              action_test->processor_.StartProcessing();
-              http_fetcher->SetOffset(action_test->start_at_offset_);
-            },
-            base::Unretained(this),
-            base::Unretained(http_fetcher)));
-    loop_.Run();
-  }
-
-  // Mainloop used to make StartDownload() synchronous.
-  brillo::FakeMessageLoop loop_{nullptr};
-
-  // Delegate that is passed to the ActionProcessor.
-  DownloadActionTestProcessorDelegate delegate_;
-
-  // The P2PManager used in the test.
-  unique_ptr<P2PManager> p2p_manager_;
-
-  // The ActionProcessor used for running the actions.
-  ActionProcessor processor_;
-
-  // A fake system state.
-  FakeSystemState fake_system_state_;
-
-  // The data being downloaded.
-  string data_;
-
- private:
-  // The requested starting offset passed to SetupDownload().
-  off_t start_at_offset_;
-
-  chromeos_update_manager::FakeUpdateManager fake_um_;
-};
-
-TEST_F(P2PDownloadActionTest, IsWrittenTo) {
-  SetupDownload(0);     // starting_offset
-  StartDownload(true);  // use_p2p_to_share
-
-  // Check the p2p file and its content matches what was sent.
-  string file_id = delegate_.p2p_file_id_;
-  EXPECT_NE("", file_id);
-  EXPECT_EQ(static_cast<int>(data_.length()),
-            p2p_manager_->FileGetSize(file_id));
-  EXPECT_EQ(static_cast<int>(data_.length()),
-            p2p_manager_->FileGetExpectedSize(file_id));
-  string p2p_file_contents;
-  EXPECT_TRUE(
-      ReadFileToString(p2p_manager_->FileGetPath(file_id), &p2p_file_contents));
-  EXPECT_EQ(data_, p2p_file_contents);
-}
-
-TEST_F(P2PDownloadActionTest, DeleteIfHoleExists) {
-  SetupDownload(1000);  // starting_offset
-  StartDownload(true);  // use_p2p_to_share
-
-  // DownloadAction should convey that the file is not being shared.
-  // and that we don't have any p2p files.
-  EXPECT_EQ(delegate_.p2p_file_id_, "");
-  EXPECT_EQ(p2p_manager_->CountSharedFiles(), 0);
-}
-
-TEST_F(P2PDownloadActionTest, CanAppend) {
-  SetupDownload(1000);  // starting_offset
-
-  // Prepare the file with existing data before starting to write to
-  // it via DownloadAction.
-  string file_id = utils::CalculateP2PFileId(
-      {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length());
-  ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
-  string existing_data;
-  for (unsigned int i = 0; i < 1000; i++)
-    existing_data += '0' + (i % 10);
-  ASSERT_EQ(
-      WriteFile(
-          p2p_manager_->FileGetPath(file_id), existing_data.c_str(), 1000),
-      1000);
-
-  StartDownload(true);  // use_p2p_to_share
-
-  // DownloadAction should convey the same file_id and the file should
-  // have the expected size.
-  EXPECT_EQ(delegate_.p2p_file_id_, file_id);
-  EXPECT_EQ(static_cast<ssize_t>(data_.length()),
-            p2p_manager_->FileGetSize(file_id));
-  EXPECT_EQ(static_cast<ssize_t>(data_.length()),
-            p2p_manager_->FileGetExpectedSize(file_id));
-  string p2p_file_contents;
-  // Check that the first 1000 bytes wasn't touched and that we
-  // appended the remaining as appropriate.
-  EXPECT_TRUE(
-      ReadFileToString(p2p_manager_->FileGetPath(file_id), &p2p_file_contents));
-  EXPECT_EQ(existing_data, p2p_file_contents.substr(0, 1000));
-  EXPECT_EQ(data_.substr(1000), p2p_file_contents.substr(1000));
-}
-
-TEST_F(P2PDownloadActionTest, DeletePartialP2PFileIfResumingWithoutP2P) {
-  SetupDownload(1000);  // starting_offset
-
-  // Prepare the file with all existing data before starting to write
-  // to it via DownloadAction.
-  string file_id = utils::CalculateP2PFileId(
-      {'1', '2', '3', '4', 'h', 'a', 's', 'h'}, data_.length());
-  ASSERT_TRUE(p2p_manager_->FileShare(file_id, data_.length()));
-  string existing_data;
-  for (unsigned int i = 0; i < 1000; i++)
-    existing_data += '0' + (i % 10);
-  ASSERT_EQ(
-      WriteFile(
-          p2p_manager_->FileGetPath(file_id), existing_data.c_str(), 1000),
-      1000);
-
-  // Check that the file is there.
-  EXPECT_EQ(1000, p2p_manager_->FileGetSize(file_id));
-  EXPECT_EQ(1, p2p_manager_->CountSharedFiles());
-
-  StartDownload(false);  // use_p2p_to_share
-
-  // DownloadAction should have deleted the p2p file. Check that it's gone.
-  EXPECT_EQ(-1, p2p_manager_->FileGetSize(file_id));
-  EXPECT_EQ(0, p2p_manager_->CountSharedFiles());
-}
-
-}  // namespace chromeos_update_engine
diff --git a/payload_consumer/mock_download_action.h b/payload_consumer/mock_download_action.h
deleted file mode 100644
index 3abb809..0000000
--- a/payload_consumer/mock_download_action.h
+++ /dev/null
@@ -1,41 +0,0 @@
-//
-// Copyright (C) 2015 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_PAYLOAD_CONSUMER_MOCK_DOWNLOAD_ACTION_H_
-#define UPDATE_ENGINE_PAYLOAD_CONSUMER_MOCK_DOWNLOAD_ACTION_H_
-
-#include <stdint.h>
-
-#include <gmock/gmock.h>
-
-#include "update_engine/common/error_code.h"
-#include "update_engine/payload_consumer/download_action.h"
-
-namespace chromeos_update_engine {
-
-class MockDownloadActionDelegate : public DownloadActionDelegate {
- public:
-  MOCK_METHOD3(BytesReceived,
-               void(uint64_t bytes_progressed,
-                    uint64_t bytes_received,
-                    uint64_t total));
-  MOCK_METHOD1(ShouldCancel, bool(ErrorCode* cancel_reason));
-  MOCK_METHOD0(DownloadComplete, void());
-};
-
-}  // namespace chromeos_update_engine
-
-#endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_MOCK_DOWNLOAD_ACTION_H_
diff --git a/payload_consumer/mock_file_writer.h b/payload_consumer/mock_file_writer.h
new file mode 100644
index 0000000..26cd45d
--- /dev/null
+++ b/payload_consumer/mock_file_writer.h
@@ -0,0 +1,34 @@
+//
+// Copyright (C) 2010 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_MOCK_FILE_WRITER_H_
+#define UPDATE_ENGINE_MOCK_FILE_WRITER_H_
+
+#include <gmock/gmock.h>
+#include "update_engine/payload_consumer/file_writer.h"
+
+namespace chromeos_update_engine {
+
+class MockFileWriter : public FileWriter {
+ public:
+  MOCK_METHOD2(Write, bool(const void* bytes, size_t count));
+  MOCK_METHOD3(Write, bool(const void* bytes, size_t count, ErrorCode* error));
+  MOCK_METHOD0(Close, int());
+};
+
+}  // namespace chromeos_update_engine
+
+#endif  // UPDATE_ENGINE_MOCK_FILE_WRITER_H_
diff --git a/payload_consumer/postinstall_runner_action_unittest.cc b/payload_consumer/postinstall_runner_action_unittest.cc
index 5910c23..cce86e9 100644
--- a/payload_consumer/postinstall_runner_action_unittest.cc
+++ b/payload_consumer/postinstall_runner_action_unittest.cc
@@ -45,7 +45,7 @@
 #include "update_engine/common/subprocess.h"
 #include "update_engine/common/test_utils.h"
 #include "update_engine/common/utils.h"
-#include "update_engine/mock_payload_state.h"
+#include "update_engine/cros/mock_payload_state.h"
 
 using brillo::MessageLoop;
 using chromeos_update_engine::test_utils::ScopedLoopbackDeviceBinder;