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
|
// Copyright 2018 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_COMMON_ANDROID_AUTO_RESUMPTION_HANDLER_H_
#define COMPONENTS_DOWNLOAD_PUBLIC_COMMON_ANDROID_AUTO_RESUMPTION_HANDLER_H_
#include <stddef.h>
#include <memory>
#include <set>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/download/network/network_status_listener.h"
#include "components/download/public/common/download_export.h"
#include "components/download/public/common/download_item.h"
#include "components/download/public/task/task_manager.h"
namespace base {
class Clock;
} // namespace base
namespace download {
// Handles auto-resumptions for downloads. Listens to network changes and
// schecules task to resume downloads accordingly.
class COMPONENTS_DOWNLOAD_EXPORT AutoResumptionHandler
: public download::NetworkStatusListener::Observer,
public download::DownloadItem::Observer {
public:
struct COMPONENTS_DOWNLOAD_EXPORT Config {
Config();
~Config() = default;
int auto_resumption_size_limit;
bool is_auto_resumption_enabled_in_native;
};
// Creates the singleton instance of AutoResumptionHandler.
static void Create(
std::unique_ptr<download::NetworkStatusListener> network_listener,
std::unique_ptr<download::TaskManager> task_manager,
std::unique_ptr<Config> config,
base::Clock* clock);
// Returns the singleton instance of the AutoResumptionHandler, or nullptr if
// initialization is not yet complete.
static AutoResumptionHandler* Get();
AutoResumptionHandler(
std::unique_ptr<download::NetworkStatusListener> network_listener,
std::unique_ptr<download::TaskManager> task_manager,
std::unique_ptr<Config> config,
base::Clock* clock);
AutoResumptionHandler(const AutoResumptionHandler&) = delete;
AutoResumptionHandler& operator=(const AutoResumptionHandler&) = delete;
~AutoResumptionHandler() override;
void SetResumableDownloads(
const std::vector<raw_ptr<download::DownloadItem, VectorExperimental>>&
downloads);
bool IsActiveNetworkMetered() const;
void OnStartScheduledTask(DownloadTaskType type,
TaskFinishedCallback callback);
bool OnStopScheduledTask(DownloadTaskType type);
void OnDownloadStarted(download::DownloadItem* item);
// DownloadItem::Observer overrides.
void OnDownloadUpdated(download::DownloadItem* item) override;
void OnDownloadRemoved(download::DownloadItem* item) override;
void OnDownloadDestroyed(download::DownloadItem* item) override;
private:
using DownloadMap =
std::map<std::string, raw_ptr<DownloadItem, CtnExperimental>>;
// NetworkStatusListener::Observer implementation.
void OnNetworkStatusReady(network::mojom::ConnectionType type) override;
void OnNetworkChanged(network::mojom::ConnectionType type) override;
void ResumePendingDownloads();
// Maybe resume some of the |downloads|. Returns the number of downloads
// resumed. Pass by value is intentional to avoid concurrent modification.
int MaybeResumeDownloads(DownloadMap downloads);
void RecomputeTaskParams();
void RescheduleTaskIfNecessary();
void RescheduleTaskIfNecessaryForTaskType(DownloadTaskType task_type);
void ResumeDownloadImmediately();
bool ShouldResumeNow(download::DownloadItem* download) const;
bool IsAutoResumableDownload(download::DownloadItem* item) const;
// Listens to network events to stop/resume downloads accordingly.
std::unique_ptr<download::NetworkStatusListener> network_listener_;
std::unique_ptr<download::TaskManager> task_manager_;
std::unique_ptr<Config> config_;
raw_ptr<base::Clock> clock_;
// List of downloads that are auto-resumable. These will be resumed as soon as
// network conditions becomes favorable.
DownloadMap resumable_downloads_;
// A temporary list of downloads which are being retried immediately.
std::set<raw_ptr<download::DownloadItem, SetExperimental>>
downloads_to_retry_;
bool recompute_task_params_scheduled_ = false;
base::WeakPtrFactory<AutoResumptionHandler> weak_factory_{this};
};
} // namespace download
#endif // COMPONENTS_DOWNLOAD_PUBLIC_COMMON_ANDROID_AUTO_RESUMPTION_HANDLER_H_
|