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

Side by Side Diff: content/browser/download/parallel_download_job.cc

Issue 2689373003: Introduce ParallelDownloadJob. (Closed)
Patch Set: Make compilers 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/download/parallel_download_job.h"
6
7 #include "base/memory/ptr_util.h"
8 #include "content/public/browser/browser_context.h"
9 #include "content/public/browser/storage_partition.h"
10
11 namespace content {
12
13 namespace {
14
15 // TODO(xingliu): Use finch parameters to configure constants.
16 // Default number of requests in a parallel download, including the original
17 // request.
18 const int kParallelRequestCount = 2;
19
20 } // namespace
21
22 ParallelDownloadJob::ParallelDownloadJob(
23 DownloadItemImpl* download_item,
24 std::unique_ptr<DownloadRequestHandleInterface> request_handle)
25 : DownloadJobImpl(download_item, std::move(request_handle)),
26 request_num_(kParallelRequestCount) {}
27
28 ParallelDownloadJob::~ParallelDownloadJob() = default;
29
30 void ParallelDownloadJob::Cancel(bool user_cancel) {
31 DownloadJobImpl::Cancel(user_cancel);
32 for (auto& worker : workers_)
33 worker->Cancel();
34 }
35
36 void ParallelDownloadJob::Pause() {
37 DownloadJobImpl::Pause();
38 for (auto& worker : workers_)
39 worker->Pause();
40 }
41
42 void ParallelDownloadJob::Resume() {
43 for (auto& worker : workers_)
44 worker->Resume();
45 DownloadJobImpl::Resume();
46 }
47
48 void ParallelDownloadJob::ForkRequestsForNewDownload(int64_t bytes_received,
49 int64_t total_bytes) {
50 if (!download_item_ || total_bytes <= 0 || bytes_received >= total_bytes ||
51 request_num_ <= 1) {
52 return;
53 }
54
55 int64_t bytes_left = total_bytes - bytes_received;
56 int64_t slice_size = bytes_left / request_num_;
57 slice_size = slice_size > 0 ? slice_size : 1;
58 int num_requests = bytes_left / slice_size;
59 int64_t current_offset = bytes_received + slice_size;
60
61 // TODO(xingliu): Add records for slices in history db.
62 for (int i = 0; i < num_requests - 1; ++i) {
63 int64_t length = (i == (num_requests - 2))
64 ? slice_size + (bytes_left % slice_size)
65 : slice_size;
66 CreateRequest(current_offset, length);
67 current_offset += slice_size;
68 }
69 }
70
71 void ParallelDownloadJob::CreateRequest(int64_t offset, int64_t length) {
72 std::unique_ptr<DownloadWorker> worker = base::MakeUnique<DownloadWorker>();
73
74 DCHECK(download_item_);
75 StoragePartition* storage_partition =
76 BrowserContext::GetStoragePartitionForSite(
77 download_item_->GetBrowserContext(), download_item_->GetSiteUrl());
78
79 std::unique_ptr<DownloadUrlParameters> download_params(
80 new DownloadUrlParameters(download_item_->GetURL(),
81 storage_partition->GetURLRequestContext()));
82 download_params->set_file_path(download_item_->GetFullPath());
83 download_params->set_last_modified(download_item_->GetLastModifiedTime());
84 download_params->set_etag(download_item_->GetETag());
85 download_params->set_offset(offset);
86
87 // Setting the length will result in range request to fetch a slice of the
88 // file.
89 download_params->set_length(length);
90
91 // Subsequent range requests have the same referrer URL as the original
92 // download request.
93 download_params->set_referrer(Referrer(download_item_->GetReferrerUrl(),
94 blink::WebReferrerPolicyAlways));
95 // Send the request.
96 worker->SendRequest(std::move(download_params));
97 workers_.push_back(std::move(worker));
98 }
99
100 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698