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
|
// Copyright 2017 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_PUBLIC_BACKGROUND_SERVICE_BACKGROUND_DOWNLOAD_SERVICE_H_
#define COMPONENTS_DOWNLOAD_PUBLIC_BACKGROUND_SERVICE_BACKGROUND_DOWNLOAD_SERVICE_H_
#include <memory>
#include <string>
#include "base/component_export.h"
#include "base/task/sequenced_task_runner.h"
#include "components/download/public/background_service/clients.h"
#include "components/download/public/task/download_task_types.h"
#include "components/keyed_service/core/keyed_service.h"
namespace download {
class Client;
class Logger;
class ServiceConfig;
struct DownloadParams;
struct SchedulingParams;
using TaskFinishedCallback = base::OnceCallback<void(bool)>;
#if BUILDFLAG(IS_IOS)
// Identifier for background download service.
inline constexpr char kBackgroundDownloadIdentifierPrefix[] =
"background_download";
#endif // BUILDFLAG(IS_IOS)
// A service responsible for helping facilitate the scheduling and downloading
// of file content from the web. See |DownloadParams| for more details on the
// types of scheduling that can be achieved and the required input parameters
// for starting a download. Note that BackgroundDownloadService with a valid
// storage directory will persist the requests across restarts. This means that
// any feature requesting a download will have to implement a download::Client
// interface so this class knows who to contact when a download completes after
// a process restart.
// See the embedder specific factories for creation options.
class COMPONENT_EXPORT(COMPONENTS_DOWNLOAD_PUBLIC_BACKGROUND_SERVICE)
BackgroundDownloadService : public KeyedService {
public:
// The current status of the Service.
enum class ServiceStatus {
// The service is in the process of initializing and should not be used yet.
// All registered Clients will be notified via
// Client::OnServiceInitialized() once the service is ready.
STARTING_UP = 0,
// The service is ready and available for use.
READY = 1,
// The service is unavailable. This is typically due to an unrecoverable
// error on some internal component like the persistence layer.
UNAVAILABLE = 2,
};
// Returns useful configuration information about the DownloadService. Not
// supported on iOS.
virtual const ServiceConfig& GetConfig() = 0;
// Callback method to run by the service when a pre-scheduled task starts.
// This method is invoked on main thread and while it is running, the system
// holds a wakelock which is not released until either the |callback| is run
// or OnStopScheduledTask is invoked by the system. Do not call this method
// directly. Not supported on iOS.
virtual void OnStartScheduledTask(DownloadTaskType task_type,
TaskFinishedCallback callback) = 0;
// Callback method to run by the service if the system decides to stop the
// task. Returns true if the task needs to be rescheduled. Any pending
// TaskFinishedCallback should be reset after this call. Do not call this
// method directly. Not supported on iOS.
virtual bool OnStopScheduledTask(DownloadTaskType task_type) = 0;
// Whether or not the BackgroundDownloadService is currently available,
// initialized successfully, and ready to be used.
virtual ServiceStatus GetStatus() = 0;
// Sends the download to the service. A callback to
// |DownloadParams::callback| will be triggered once the download has been
// persisted and saved in the service.
virtual void StartDownload(DownloadParams download_params) = 0;
// Allows any feature to pause or resume downloads at will. Paused downloads
// will not start or stop based on scheduling criteria. They will be
// effectively frozen. Not supported on iOS.
virtual void PauseDownload(const std::string& guid) = 0;
virtual void ResumeDownload(const std::string& guid) = 0;
// Cancels a download in this service. The canceled download will be
// interrupted if it is running. Not supported on iOS.
virtual void CancelDownload(const std::string& guid) = 0;
// Changes the current scheduling criteria for a download. This is useful if
// a user action might constrain or loosen the device state during which this
// download can run. Not supported on iOS.
virtual void ChangeDownloadCriteria(const std::string& guid,
const SchedulingParams& params) = 0;
// Returns a Logger instance that is meant to be used by logging and debug UI
// components in the larger system.
virtual Logger* GetLogger() = 0;
#if BUILDFLAG(IS_IOS)
// Called by the system to handle events for background URL session. Once
// done, the passed function should be called.
virtual void HandleEventsForBackgroundURLSession(base::OnceClosure) {}
#endif // BUILDFLAG(IS_IOS)
BackgroundDownloadService(const BackgroundDownloadService&) = delete;
BackgroundDownloadService& operator=(const BackgroundDownloadService&) =
delete;
~BackgroundDownloadService() override = default;
protected:
BackgroundDownloadService() = default;
};
} // namespace download
#endif // COMPONENTS_DOWNLOAD_PUBLIC_BACKGROUND_SERVICE_BACKGROUND_DOWNLOAD_SERVICE_H_
|