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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
// 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.
#include "components/download/internal/common/parallel_download_job.h"
#include <algorithm>
#include "base/functional/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/time/time.h"
#include "components/download/internal/common/parallel_download_utils.h"
#include "components/download/public/common/download_create_info.h"
#include "components/download/public/common/download_stats.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/referrer_policy.h"
namespace download {
namespace {
const int kDownloadJobVerboseLevel = 1;
} // namespace
ParallelDownloadJob::ParallelDownloadJob(
DownloadItem* download_item,
CancelRequestCallback cancel_request_callback,
const DownloadCreateInfo& create_info,
URLLoaderFactoryProvider::URLLoaderFactoryProviderPtr
url_loader_factory_provider,
DownloadJobFactory::WakeLockProviderBinder wake_lock_provider_binder)
: DownloadJobImpl(download_item, std::move(cancel_request_callback), true),
initial_request_offset_(create_info.offset),
initial_received_slices_(download_item->GetReceivedSlices()),
content_length_(create_info.total_bytes),
requests_sent_(false),
is_canceled_(false),
url_loader_factory_provider_(std::move(url_loader_factory_provider)),
wake_lock_provider_binder_(std::move(wake_lock_provider_binder)) {}
ParallelDownloadJob::~ParallelDownloadJob() = default;
void ParallelDownloadJob::OnDownloadFileInitialized(
DownloadFile::InitializeCallback callback,
DownloadInterruptReason result,
int64_t bytes_wasted) {
DownloadJobImpl::OnDownloadFileInitialized(std::move(callback), result,
bytes_wasted);
if (result == DOWNLOAD_INTERRUPT_REASON_NONE)
BuildParallelRequestAfterDelay();
}
void ParallelDownloadJob::Cancel(bool user_cancel) {
is_canceled_ = true;
DownloadJobImpl::Cancel(user_cancel);
if (!requests_sent_) {
timer_.Stop();
return;
}
for (auto& worker : workers_)
worker.second->Cancel(user_cancel);
}
void ParallelDownloadJob::Pause() {
DownloadJobImpl::Pause();
if (!requests_sent_) {
timer_.Stop();
return;
}
for (auto& worker : workers_)
worker.second->Pause();
}
void ParallelDownloadJob::Resume(bool resume_request) {
DownloadJobImpl::Resume(resume_request);
if (!resume_request)
return;
// Send parallel requests if the download is paused previously.
if (!requests_sent_) {
if (!timer_.IsRunning())
BuildParallelRequestAfterDelay();
return;
}
for (auto& worker : workers_)
worker.second->Resume();
}
int ParallelDownloadJob::GetParallelRequestCount() const {
return GetParallelRequestCountConfig();
}
int64_t ParallelDownloadJob::GetMinSliceSize() const {
return GetMinSliceSizeConfig();
}
int ParallelDownloadJob::GetMinRemainingTimeInSeconds() const {
return GetParallelRequestRemainingTimeConfig().InSeconds();
}
void ParallelDownloadJob::CancelRequestWithOffset(int64_t offset) {
if (initial_request_offset_ == offset) {
DownloadJobImpl::Cancel(false);
return;
}
auto it = workers_.find(offset);
CHECK(it != workers_.end());
it->second->Cancel(false);
}
void ParallelDownloadJob::BuildParallelRequestAfterDelay() {
DCHECK(workers_.empty());
DCHECK(!requests_sent_);
DCHECK(!timer_.IsRunning());
timer_.Start(FROM_HERE, GetParallelRequestDelayConfig(), this,
&ParallelDownloadJob::BuildParallelRequests);
}
void ParallelDownloadJob::OnInputStreamReady(
DownloadWorker* worker,
std::unique_ptr<InputStream> input_stream,
std::unique_ptr<DownloadCreateInfo> download_create_info) {
bool success =
DownloadJob::AddInputStream(std::move(input_stream), worker->offset());
// Destroy the request if the sink is gone.
if (!success) {
VLOG(kDownloadJobVerboseLevel)
<< "Byte stream arrived after download file is released.";
worker->Cancel(false);
}
}
void ParallelDownloadJob::BuildParallelRequests() {
DCHECK(!requests_sent_);
DCHECK(!is_paused());
if (is_canceled_ ||
download_item_->GetState() != DownloadItem::DownloadState::IN_PROGRESS) {
return;
}
// TODO(qinmin): The size of |slices_to_download| should be no larger than
// |kParallelRequestCount| unless |kParallelRequestCount| is changed after
// a download is interrupted. This could happen if we use finch to config
// the number of parallel requests.
// Get the next |kParallelRequestCount - 1| slices and fork
// new requests. For the remaining slices, they will be handled once some
// of the workers finish their job.
const DownloadItem::ReceivedSlices& received_slices =
download_item_->GetReceivedSlices();
DownloadItem::ReceivedSlices slices_to_download =
FindSlicesToDownload(received_slices);
DCHECK(!slices_to_download.empty());
int64_t first_slice_offset = slices_to_download[0].offset;
// We may build parallel job without slices. The slices can be cleared or
// previous session only has one stream writing to disk. In these cases, fall
// back to non parallel download.
if (initial_request_offset_ > first_slice_offset) {
VLOG(kDownloadJobVerboseLevel)
<< "Received slices data mismatch initial request offset.";
return;
}
// Create more slices for a new download. The initial request may generate
// a received slice.
if (slices_to_download.size() <= 1 && download_item_->GetTotalBytes() > 0) {
int64_t current_bytes_per_second =
std::max(static_cast<int64_t>(1), download_item_->CurrentSpeed());
int64_t remaining_bytes =
download_item_->GetTotalBytes() - download_item_->GetReceivedBytes();
if (remaining_bytes / current_bytes_per_second >
GetMinRemainingTimeInSeconds()) {
// Fork more requests to accelerate, only if one slice is left to download
// and remaining time seems to be long enough.
slices_to_download = FindSlicesForRemainingContent(
first_slice_offset,
content_length_ - first_slice_offset + initial_request_offset_,
GetParallelRequestCount(), GetMinSliceSize());
}
}
DCHECK(!slices_to_download.empty());
// If the last received slice is finished, remove the last request which can
// be out of the range of the file. E.g, the file is 100 bytes, and the last
// request's range header will be "Range:100-".
if (!received_slices.empty() && received_slices.back().finished)
slices_to_download.pop_back();
if (slices_to_download.empty())
return;
ForkSubRequests(slices_to_download);
requests_sent_ = true;
}
void ParallelDownloadJob::ForkSubRequests(
const DownloadItem::ReceivedSlices& slices_to_download) {
// If the initial request is working on the first hole, don't create parallel
// request for this hole.
bool skip_first_slice = true;
DownloadItem::ReceivedSlices initial_slices_to_download =
FindSlicesToDownload(initial_received_slices_);
if (initial_slices_to_download.size() > 1) {
DCHECK_EQ(initial_request_offset_, initial_slices_to_download[0].offset);
int64_t first_hole_max = initial_slices_to_download[0].offset +
initial_slices_to_download[0].received_bytes;
skip_first_slice = slices_to_download[0].offset <= first_hole_max;
}
for (auto it = slices_to_download.begin(); it != slices_to_download.end();
++it) {
if (skip_first_slice) {
skip_first_slice = false;
continue;
}
DCHECK_GE(it->offset, initial_request_offset_);
// All parallel requests are half open, which sends request headers like
// "Range:50-".
// If server rejects a certain request, others should take over.
CreateRequest(it->offset);
}
}
void ParallelDownloadJob::CreateRequest(int64_t offset) {
DCHECK(download_item_);
auto worker = std::make_unique<DownloadWorker>(this, offset);
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("parallel_download_job", R"(
semantics {
sender: "Parallel Download"
description:
"Chrome makes parallel request to speed up download of a file."
trigger:
"When user starts a download request, if it would be technically "
"possible, Chrome starts parallel downloading."
data: "None."
destination: WEBSITE
}
policy {
cookies_allowed: YES
cookies_store: "user"
setting: "This feature cannot be disabled in settings."
chrome_policy {
DownloadRestrictions {
DownloadRestrictions: 3
}
}
})");
// The parallel requests only use GET method.
std::unique_ptr<DownloadUrlParameters> download_params(
new DownloadUrlParameters(download_item_->GetURL(), traffic_annotation));
download_params->set_file_path(download_item_->GetFullPath());
download_params->set_last_modified(download_item_->GetLastModifiedTime());
download_params->set_etag(download_item_->GetETag());
download_params->set_offset(offset);
// Subsequent range requests don't need the "If-Range" header.
download_params->set_use_if_range(false);
// Subsequent range requests have the same referrer URL as the original
// download request.
download_params->set_referrer(download_item_->GetReferrerUrl());
download_params->set_referrer_policy(net::ReferrerPolicy::NEVER_CLEAR);
// TODO(xingliu): We should not support redirect at all for parallel requests.
// Currently the network service code path still can redirect as long as it's
// the same origin.
download_params->set_cross_origin_redirects(
network::mojom::RedirectMode::kError);
// Send the request.
mojo::PendingRemote<device::mojom::WakeLockProvider> wake_lock_provider;
wake_lock_provider_binder_.Run(
wake_lock_provider.InitWithNewPipeAndPassReceiver());
worker->SendRequest(std::move(download_params),
url_loader_factory_provider_.get(),
std::move(wake_lock_provider));
DCHECK(workers_.find(offset) == workers_.end());
workers_[offset] = std::move(worker);
}
} // namespace download
|