1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_DOWNLOAD_INTERNAL_BACKGROUND_SERVICE_IOS_BACKGROUND_DOWNLOAD_SERVICE_IMPL_H_
#define COMPONENTS_DOWNLOAD_INTERNAL_BACKGROUND_SERVICE_IOS_BACKGROUND_DOWNLOAD_SERVICE_IMPL_H_
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "components/download/internal/background_service/initializable_background_download_service.h"
#include "components/download/internal/background_service/log_source.h"
#include "components/download/internal/background_service/model_impl.h"
#include "components/download/internal/background_service/service_config_impl.h"
#include "components/download/internal/background_service/startup_status.h"
#include "components/download/public/task/download_task_types.h"
namespace download {
class BackgroundDownloadTaskHelper;
class ClientSet;
class FileMonitor;
class Logger;
class LogSink;
class Model;
struct Configuration;
struct DownloadParams;
struct SchedulingParams;
class BackgroundDownloadServiceImpl
: public InitializableBackgroundDownloadService,
public LogSource,
public Model::Client {
public:
BackgroundDownloadServiceImpl(
std::unique_ptr<ClientSet> clients,
std::unique_ptr<Model> model,
std::unique_ptr<BackgroundDownloadTaskHelper> download_helper,
std::unique_ptr<FileMonitor> file_monitor,
const base::FilePath& download_dir,
std::unique_ptr<Logger> logger,
LogSink* log_sink,
base::Clock* clock);
~BackgroundDownloadServiceImpl() override;
private:
// InitializableBackgroundDownloadService implementation.
void Initialize(base::OnceClosure callback) override;
const ServiceConfig& GetConfig() override;
void OnStartScheduledTask(DownloadTaskType task_type,
TaskFinishedCallback callback) override;
bool OnStopScheduledTask(DownloadTaskType task_type) override;
ServiceStatus GetStatus() override;
void StartDownload(DownloadParams download_params) override;
void PauseDownload(const std::string& guid) override;
void ResumeDownload(const std::string& guid) override;
void CancelDownload(const std::string& guid) override;
void ChangeDownloadCriteria(const std::string& guid,
const SchedulingParams& params) override;
Logger* GetLogger() override;
void HandleEventsForBackgroundURLSession(
base::OnceClosure completion_handler) override;
// Model::Client implementation.
void OnModelReady(bool success) override;
void OnModelHardRecoverComplete(bool success) override;
void OnItemAdded(bool success,
DownloadClient client,
const std::string& guid) override;
void OnItemUpdated(bool success,
DownloadClient client,
const std::string& guid) override;
void OnItemRemoved(bool success,
DownloadClient client,
const std::string& guid) override;
// LogSource implementation.
Controller::State GetControllerState() override;
const StartupStatus& GetStartupStatus() override;
LogSource::EntryDetailsList GetServiceDownloads() override;
std::optional<LogSource::EntryDetails> GetServiceDownload(
const std::string& guid) override;
void PruneDbRecords();
void OnFileMonitorInitialized(bool success);
void OnFilesPruned();
void NotifyServiceUnavailable();
void InvokeStartCallback(DownloadClient client,
const std::string& guid,
DownloadParams::StartResult result,
DownloadParams::StartCallback callback);
void OnDownloadFinished(DownloadClient download_client,
const std::string& guid,
bool success,
const base::FilePath& file_path,
int64_t file_size);
void OnDownloadUpdated(DownloadClient download_client,
const std::string& guid,
int64_t bytes_downloaded);
void MaybeUpdateProgress(const std::string& guid, uint64_t bytes_downloaded);
std::unique_ptr<Configuration> config_;
ServiceConfigImpl service_config_;
std::unique_ptr<ClientSet> clients_;
std::unique_ptr<Model> model_;
std::unique_ptr<BackgroundDownloadTaskHelper> download_helper_;
std::unique_ptr<FileMonitor> file_monitor_;
std::unique_ptr<Logger> logger_;
LogSink* log_sink_;
StartupStatus startup_status_;
std::map<std::string, DownloadParams::StartCallback> start_callbacks_;
base::Time update_time_;
base::Clock* clock_;
base::OnceClosure init_callback_;
// A directory to hold download service files. The files in here will be
// pruned frequently.
const base::FilePath download_dir_;
std::set<std::string> cancelled_downloads_;
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<BackgroundDownloadServiceImpl> weak_ptr_factory_{this};
};
} // namespace download
#endif // COMPONENTS_DOWNLOAD_INTERNAL_BACKGROUND_SERVICE_IOS_BACKGROUND_DOWNLOAD_SERVICE_IMPL_H_
|