#include "components/download/internal/background_service/controller_impl.h"
#include <inttypes.h>
#include <algorithm>
#include <optional>
#include <utility>
#include "base/functional/bind.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "base/trace_event/memory_allocator_dump.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/memory_usage_estimator.h"
#include "base/trace_event/process_memory_dump.h"
#include "components/download/internal/background_service/client_set.h"
#include "components/download/internal/background_service/config.h"
#include "components/download/internal/background_service/entry.h"
#include "components/download/internal/background_service/entry_utils.h"
#include "components/download/internal/background_service/file_monitor.h"
#include "components/download/internal/background_service/log_sink.h"
#include "components/download/internal/background_service/logger_impl.h"
#include "components/download/internal/background_service/model.h"
#include "components/download/internal/background_service/scheduler/scheduler.h"
#include "components/download/internal/background_service/stats.h"
#include "components/download/network/download_http_utils.h"
#include "components/download/public/background_service/client.h"
#include "components/download/public/background_service/download_metadata.h"
#include "components/download/public/background_service/navigation_monitor.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/resource_request_body.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
namespace download {
namespace {
void TransitTo(Entry* entry, Entry::State new_state, Model* model) { … }
void RunOnDownloadReadyToStart(
GetUploadDataCallback callback,
scoped_refptr<network::ResourceRequestBody> post_body) { … }
Client::FailureReason FailureReasonFromCompletionType(CompletionType type) { … }
bool CanActivateMoreDownloads(Configuration* config,
uint32_t active_count,
uint32_t paused_count) { … }
Model::EntryList GetRunnableEntries(const Model::EntryList& list) { … }
}
ControllerImpl::ControllerImpl(
std::unique_ptr<Configuration> config,
std::unique_ptr<Logger> logger,
LogSink* log_sink,
std::unique_ptr<ClientSet> clients,
std::unique_ptr<DownloadDriver> driver,
std::unique_ptr<Model> model,
std::unique_ptr<DeviceStatusListener> device_status_listener,
NavigationMonitor* navigation_monitor,
std::unique_ptr<Scheduler> scheduler,
std::unique_ptr<TaskScheduler> task_scheduler,
std::unique_ptr<FileMonitor> file_monitor,
const base::FilePath& download_file_dir)
: … { … }
ControllerImpl::~ControllerImpl() { … }
void ControllerImpl::Initialize(base::OnceClosure callback) { … }
const ServiceConfig& ControllerImpl::GetConfig() { … }
BackgroundDownloadService::ServiceStatus ControllerImpl::GetStatus() { … }
Controller::State ControllerImpl::GetState() { … }
void ControllerImpl::StartDownload(DownloadParams params) { … }
void ControllerImpl::PauseDownload(const std::string& guid) { … }
void ControllerImpl::ResumeDownload(const std::string& guid) { … }
void ControllerImpl::CancelDownload(const std::string& guid) { … }
void ControllerImpl::ChangeDownloadCriteria(const std::string& guid,
const SchedulingParams& params) { … }
DownloadClient ControllerImpl::GetOwnerOfDownload(const std::string& guid) { … }
void ControllerImpl::OnStartScheduledTask(DownloadTaskType task_type,
TaskFinishedCallback callback) { … }
bool ControllerImpl::OnStopScheduledTask(DownloadTaskType task_type) { … }
Logger* ControllerImpl::GetLogger() { … }
void ControllerImpl::OnCompleteCleanupTask() { … }
void ControllerImpl::RemoveCleanupEligibleDownloads() { … }
void ControllerImpl::HandleTaskFinished(DownloadTaskType task_type,
stats::ScheduledTaskStatus status) { … }
void ControllerImpl::OnDriverReady(bool success) { … }
void ControllerImpl::OnDriverHardRecoverComplete(bool success) { … }
void ControllerImpl::OnDownloadCreated(const DriverEntry& download) { … }
void ControllerImpl::OnDownloadFailed(const DriverEntry& download,
FailureType failure_type) { … }
void ControllerImpl::OnDownloadSucceeded(const DriverEntry& download) { … }
void ControllerImpl::OnDownloadUpdated(const DriverEntry& download) { … }
bool ControllerImpl::IsTrackingDownload(const std::string& guid) const { … }
void ControllerImpl::OnUploadProgress(const std::string& guid,
uint64_t bytes_uploaded) const { … }
void ControllerImpl::OnFileMonitorReady(bool success) { … }
void ControllerImpl::OnFileMonitorHardRecoverComplete(bool success) { … }
void ControllerImpl::OnModelReady(bool success) { … }
void ControllerImpl::OnModelHardRecoverComplete(bool success) { … }
void ControllerImpl::OnItemAdded(bool success,
DownloadClient client,
const std::string& guid) { … }
void ControllerImpl::OnItemUpdated(bool success,
DownloadClient client,
const std::string& guid) { … }
void ControllerImpl::OnItemRemoved(bool success,
DownloadClient client,
const std::string& guid) { … }
Controller::State ControllerImpl::GetControllerState() { … }
const StartupStatus& ControllerImpl::GetStartupStatus() { … }
LogSource::EntryDetailsList ControllerImpl::GetServiceDownloads() { … }
std::optional<LogSource::EntryDetails> ControllerImpl::GetServiceDownload(
const std::string& guid) { … }
bool ControllerImpl::OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) { … }
void ControllerImpl::OnDeviceStatusChanged(const DeviceStatus& device_status) { … }
void ControllerImpl::AttemptToFinalizeSetup() { … }
void ControllerImpl::HandleUnrecoverableSetup() { … }
void ControllerImpl::StartHardRecoveryAttempt() { … }
void ControllerImpl::PollActiveDriverDownloads() { … }
void ControllerImpl::CancelOrphanedRequests() { … }
void ControllerImpl::CleanupUnknownFiles() { … }
void ControllerImpl::ResolveInitialRequestStates() { … }
void ControllerImpl::UpdateDriverStates() { … }
void ControllerImpl::UpdateDriverStateWithGuid(const std::string& guid) { … }
void ControllerImpl::UpdateDriverState(Entry* entry) { … }
void ControllerImpl::PrepareToStartDownload(Entry* entry) { … }
void ControllerImpl::OnDownloadReadyToStart(
const std::string& guid,
scoped_refptr<network::ResourceRequestBody> post_body) { … }
DownloadBlockageStatus ControllerImpl::IsDownloadBlocked(Entry* entry) { … }
void ControllerImpl::KillTimedOutUploads() { … }
void ControllerImpl::NotifyClientsOfStartup(bool state_lost) { … }
void ControllerImpl::NotifyServiceOfStartup() { … }
void ControllerImpl::HandleStartDownloadResponse(
DownloadClient client,
const std::string& guid,
DownloadParams::StartResult result) { … }
void ControllerImpl::HandleStartDownloadResponse(
DownloadClient client,
const std::string& guid,
DownloadParams::StartResult result,
DownloadParams::StartCallback callback) { … }
void ControllerImpl::HandleCompleteDownload(CompletionType type,
const std::string& guid) { … }
void ControllerImpl::ScheduleCleanupTask() { … }
void ControllerImpl::ScheduleKillDownloadTaskIfNecessary() { … }
void ControllerImpl::KillTimedOutDownloads() { … }
void ControllerImpl::ActivateMoreDownloads() { … }
void ControllerImpl::OnNavigationEvent() { … }
bool ControllerImpl::ShouldBlockDownloadOnNavigation(Entry* entry) { … }
void ControllerImpl::HandleExternalDownload(const std::string& guid,
bool active) { … }
void ControllerImpl::SendOnServiceInitialized(
DownloadClient client_id,
bool state_lost,
const std::vector<DownloadMetaData>& downloads) { … }
void ControllerImpl::SendOnServiceUnavailable() { … }
void ControllerImpl::SendOnDownloadUpdated(DownloadClient client_id,
const std::string& guid,
uint64_t bytes_uploaded,
uint64_t bytes_downloaded) { … }
void ControllerImpl::SendOnDownloadSucceeded(
DownloadClient client_id,
const std::string& guid,
const CompletionInfo& completion_info) { … }
void ControllerImpl::SendOnDownloadFailed(
DownloadClient client_id,
const std::string& guid,
const CompletionInfo& completion_info,
download::Client::FailureReason reason) { … }
}