Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(727)

Unified Diff: content/browser/download/parallel_download_job.cc

Issue 2689373003: Introduce ParallelDownloadJob. (Closed)
Patch Set: Make windows compiler happy. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/download/parallel_download_job.cc
diff --git a/content/browser/download/parallel_download_job.cc b/content/browser/download/parallel_download_job.cc
new file mode 100644
index 0000000000000000000000000000000000000000..cdddd8df281a70bb0f802dc091b7148f09d12c1d
--- /dev/null
+++ b/content/browser/download/parallel_download_job.cc
@@ -0,0 +1,104 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/browser/download/parallel_download_job.h"
+
+#include "base/memory/ptr_util.h"
+#include "content/browser/download/download_url_task.h"
+#include "content/public/browser/browser_context.h"
+#include "content/public/browser/storage_partition.h"
+
+namespace content {
+
+namespace {
+
+// TODO(xingliu): Use finch parameters to configure constants.
+// Default number of requests in a parallel download, including the original
+// request.
asanka 2017/02/16 16:27:39 It'll be quite unlikely that we'd find a number th
+const int kParallelRequestNumber = 2;
asanka 2017/02/16 16:27:39 kParallelRequestCount or somesuch. Parallel reques
xingliu 2017/02/20 18:59:11 Done.
+
+// Default minimum size of bytes for each range request.
+// If bytes left are less than this, no additional requests will be created.
+const int64_t kMinRangeLength = 1024;
+
+} // namespace
+
+ParallelDownloadJob::ParallelDownloadJob(
+ std::unique_ptr<DownloadRequestHandleInterface> request_handle)
+ : DownloadUrlJob(std::move(request_handle)),
+ request_num_(kParallelRequestNumber),
+ min_length_(kMinRangeLength) {}
+
+ParallelDownloadJob::~ParallelDownloadJob() = default;
+
+void ParallelDownloadJob::Cancel(bool user_cancel) {
+ DownloadUrlJob::Cancel(user_cancel);
+ for (auto& task : tasks_)
+ task->Cancel();
+}
+
+void ParallelDownloadJob::Pause() {
+ DownloadUrlJob::Pause();
+ for (auto& task : tasks_)
+ task->Pause();
+}
+
+void ParallelDownloadJob::Resume() {
+ DownloadUrlJob::Resume();
+ for (auto& task : tasks_)
+ task->Resume();
+}
+
+void ParallelDownloadJob::ForkParallelRequests(int64_t bytes_received,
+ int64_t total_bytes) {
asanka 2017/02/16 16:27:39 This differs from the "list of slices" model that'
xingliu 2017/02/20 18:59:11 Renamed the function name. Added some comment in t
+ if (!download_item_ || total_bytes <= 0 || bytes_received >= total_bytes ||
+ request_num_ <= 1) {
+ return;
+ }
+
+ int64_t bytes_left = total_bytes - bytes_received;
+ int64_t piece_size = std::max(bytes_left / request_num_, min_length_);
asanka 2017/02/16 16:27:39 Let's use consistent terminology. Elsewhere we've
xingliu 2017/02/20 18:59:11 Done.
+ DCHECK(piece_size > 0);
+
+ // We may produce less requests if (bytes_left / request_num_) is less than
asanka 2017/02/16 16:27:39 s/less requests/fewer requests/
xingliu 2017/02/20 18:59:11 Done.
+ // |min_length_|.
+ int num_requests = bytes_left / piece_size;
+ int64_t current_offset = bytes_received + piece_size;
+
+ for (int i = 0; i < num_requests - 1; ++i) {
+ int64_t length = (i == (num_requests - 2))
+ ? piece_size + (bytes_left % piece_size)
+ : piece_size;
+ CreateRequest(current_offset, length);
+ current_offset += piece_size;
+ }
+}
+
+void ParallelDownloadJob::CreateRequest(int64_t offset, int64_t length) {
+ std::unique_ptr<DownloadUrlTask> task = base::MakeUnique<DownloadUrlTask>();
+
+ DCHECK(download_item_);
+ StoragePartition* storage_partition =
+ BrowserContext::GetStoragePartitionForSite(
+ download_item_->GetBrowserContext(), download_item_->GetSiteUrl());
+
+ std::unique_ptr<DownloadUrlParameters> download_params(
+ new DownloadUrlParameters(download_item_->GetURL(),
+ storage_partition->GetURLRequestContext()));
+ 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);
+ download_params->set_length(length);
asanka 2017/02/16 16:27:39 Worth adding a comment here that the length parame
xingliu 2017/02/20 18:59:11 Done.
+
+ // Subsequent range requests have the same referrer URL as the original
+ // download request.
+ download_params->set_referrer(Referrer(download_item_->GetReferrerUrl(),
+ blink::WebReferrerPolicyAlways));
+ // Send the request.
+ task->SendRequest(std::move(download_params));
+ tasks_.push_back(std::move(task));
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698