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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
// 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.
#include "components/download/internal/background_service/in_memory_download_driver.h"
#include "base/task/single_thread_task_runner.h"
#include "components/download/internal/background_service/in_memory_download.h"
#include "components/download/public/background_service/url_loader_factory_getter.h"
#include "services/network/public/cpp/resource_request_body.h"
namespace download {
namespace {
DriverEntry::State ToDriverEntryState(InMemoryDownload::State state) {
switch (state) {
case InMemoryDownload::State::INITIAL:
case InMemoryDownload::State::RETRIEVE_URL_LOADER_FACTIORY:
case InMemoryDownload::State::RETRIEVE_BLOB_CONTEXT:
case InMemoryDownload::State::IN_PROGRESS:
return DriverEntry::State::IN_PROGRESS;
case InMemoryDownload::State::FAILED:
return DriverEntry::State::INTERRUPTED;
case InMemoryDownload::State::COMPLETE:
return DriverEntry::State::COMPLETE;
}
NOTREACHED();
}
// Helper function to create download driver entry based on in memory download.
DriverEntry CreateDriverEntry(const InMemoryDownload& download) {
DriverEntry entry;
entry.guid = download.guid();
entry.state = ToDriverEntryState(download.state());
entry.paused = download.paused();
entry.done = entry.state == DriverEntry::State::COMPLETE ||
entry.state == DriverEntry::State::CANCELLED;
entry.bytes_downloaded = download.bytes_downloaded();
entry.url_chain = download.url_chain();
entry.response_headers = download.response_headers();
if (entry.response_headers) {
entry.expected_total_size = entry.response_headers->GetContentLength();
}
// Currently incognito mode network backend can't resume in the middle.
entry.can_resume = false;
if (download.state() == InMemoryDownload::State::COMPLETE) {
auto blob_handle = download.ResultAsBlob();
if (blob_handle) {
entry.blob_handle = std::optional<storage::BlobDataHandle>(*blob_handle);
}
}
return entry;
}
} // namespace
InMemoryDownloadFactory::InMemoryDownloadFactory(
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
: io_task_runner_(io_task_runner) {}
InMemoryDownloadFactory::~InMemoryDownloadFactory() = default;
std::unique_ptr<InMemoryDownload> InMemoryDownloadFactory::Create(
const std::string& guid,
const RequestParams& request_params,
scoped_refptr<network::ResourceRequestBody> request_body,
const net::NetworkTrafficAnnotationTag& traffic_annotation,
InMemoryDownload::Delegate* delegate) {
return std::make_unique<InMemoryDownloadImpl>(
guid, request_params, std::move(request_body), traffic_annotation,
delegate, io_task_runner_);
}
InMemoryDownloadDriver::InMemoryDownloadDriver(
std::unique_ptr<InMemoryDownload::Factory> download_factory,
BlobContextGetterFactoryPtr blob_context_getter_factory,
URLLoaderFactoryGetterPtr url_loader_factory_getter)
: client_(nullptr),
download_factory_(std::move(download_factory)),
blob_context_getter_factory_(std::move(blob_context_getter_factory)),
url_loader_factory_getter_(std::move(url_loader_factory_getter)) {}
InMemoryDownloadDriver::~InMemoryDownloadDriver() = default;
void InMemoryDownloadDriver::Initialize(DownloadDriver::Client* client) {
DCHECK(!client_) << "Initialize can be called only once.";
client_ = client;
DCHECK(client_);
client_->OnDriverReady(true);
}
void InMemoryDownloadDriver::HardRecover() {
client_->OnDriverHardRecoverComplete(true);
}
bool InMemoryDownloadDriver::IsReady() const {
return true;
}
void InMemoryDownloadDriver::Start(
const RequestParams& request_params,
const std::string& guid,
const base::FilePath& file_path,
scoped_refptr<network::ResourceRequestBody> request_body,
const net::NetworkTrafficAnnotationTag& traffic_annotation) {
std::unique_ptr<InMemoryDownload> download = download_factory_->Create(
guid, request_params, std::move(request_body), traffic_annotation, this);
InMemoryDownload* download_ptr = download.get();
DCHECK(downloads_.find(guid) == downloads_.end()) << "Existing GUID found.";
downloads_.emplace(guid, std::move(download));
download_ptr->Start();
}
void InMemoryDownloadDriver::Remove(const std::string& guid, bool remove_file) {
downloads_.erase(guid);
}
void InMemoryDownloadDriver::Pause(const std::string& guid) {
auto it = downloads_.find(guid);
if (it != downloads_.end()) {
it->second->Pause();
}
}
void InMemoryDownloadDriver::Resume(const std::string& guid) {
auto it = downloads_.find(guid);
if (it != downloads_.end()) {
it->second->Resume();
}
}
std::optional<DriverEntry> InMemoryDownloadDriver::Find(
const std::string& guid) {
std::optional<DriverEntry> entry;
auto it = downloads_.find(guid);
if (it != downloads_.end()) {
entry = CreateDriverEntry(*it->second);
}
return entry;
}
std::set<std::string> InMemoryDownloadDriver::GetActiveDownloads() {
std::set<std::string> downloads;
for (const auto& it : downloads_) {
if (it.second->state() == InMemoryDownload::State::INITIAL ||
it.second->state() == InMemoryDownload::State::IN_PROGRESS) {
downloads.emplace(it.first);
}
}
return downloads;
}
size_t InMemoryDownloadDriver::EstimateMemoryUsage() const {
size_t memory_usage = 0u;
for (const auto& it : downloads_) {
memory_usage += it.second->EstimateMemoryUsage();
}
return memory_usage;
}
void InMemoryDownloadDriver::OnDownloadStarted(InMemoryDownload* download) {
DCHECK(client_);
client_->OnDownloadCreated(CreateDriverEntry(*download));
}
void InMemoryDownloadDriver::OnDownloadProgress(InMemoryDownload* download) {
DCHECK(client_);
client_->OnDownloadUpdated(CreateDriverEntry(*download));
}
void InMemoryDownloadDriver::OnDownloadComplete(InMemoryDownload* download) {
DCHECK(download);
DCHECK(client_);
DriverEntry entry = CreateDriverEntry(*download);
switch (download->state()) {
case InMemoryDownload::State::FAILED:
// URLFetcher retries for network failures.
client_->OnDownloadFailed(entry, FailureType::NOT_RECOVERABLE);
// Should immediately return in case |client_| removes |download| in
// OnDownloadFailed.
return;
case InMemoryDownload::State::COMPLETE:
client_->OnDownloadSucceeded(entry);
// Should immediately return in case |client_| removes |download| in
// OnDownloadSucceeded.
return;
case InMemoryDownload::State::INITIAL:
case InMemoryDownload::State::RETRIEVE_URL_LOADER_FACTIORY:
case InMemoryDownload::State::RETRIEVE_BLOB_CONTEXT:
case InMemoryDownload::State::IN_PROGRESS:
NOTREACHED();
}
}
void InMemoryDownloadDriver::OnUploadProgress(InMemoryDownload* download) {
DCHECK(download);
DCHECK(client_);
client_->OnUploadProgress(download->guid(), download->bytes_uploaded());
}
void InMemoryDownloadDriver::RetrieveBlobContextGetter(
BlobContextGetterCallback callback) {
blob_context_getter_factory_->RetrieveBlobContextGetter(std::move(callback));
}
void InMemoryDownloadDriver::RetrievedURLLoaderFactory(
URLLoaderFactoryGetterCallback callback) {
url_loader_factory_getter_->RetrieveURLLoaderFactory(std::move(callback));
}
} // namespace download
|