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

Side by Side 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 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/browser/download/download_url_task.h"
9 #include "content/public/browser/browser_context.h"
10 #include "content/public/browser/storage_partition.h"
11
12 namespace content {
13
14 namespace {
15
16 // TODO(xingliu): Use finch parameters to configure constants.
17 // Default number of requests in a parallel download, including the original
18 // request.
asanka 2017/02/16 16:27:39 It'll be quite unlikely that we'd find a number th
19 const int kParallelRequestNumber = 2;
asanka 2017/02/16 16:27:39 kParallelRequestCount or somesuch. Parallel reques
xingliu 2017/02/20 18:59:11 Done.
20
21 // Default minimum size of bytes for each range request.
22 // If bytes left are less than this, no additional requests will be created.
23 const int64_t kMinRangeLength = 1024;
24
25 } // namespace
26
27 ParallelDownloadJob::ParallelDownloadJob(
28 std::unique_ptr<DownloadRequestHandleInterface> request_handle)
29 : DownloadUrlJob(std::move(request_handle)),
30 request_num_(kParallelRequestNumber),
31 min_length_(kMinRangeLength) {}
32
33 ParallelDownloadJob::~ParallelDownloadJob() = default;
34
35 void ParallelDownloadJob::Cancel(bool user_cancel) {
36 DownloadUrlJob::Cancel(user_cancel);
37 for (auto& task : tasks_)
38 task->Cancel();
39 }
40
41 void ParallelDownloadJob::Pause() {
42 DownloadUrlJob::Pause();
43 for (auto& task : tasks_)
44 task->Pause();
45 }
46
47 void ParallelDownloadJob::Resume() {
48 DownloadUrlJob::Resume();
49 for (auto& task : tasks_)
50 task->Resume();
51 }
52
53 void ParallelDownloadJob::ForkParallelRequests(int64_t bytes_received,
54 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
55 if (!download_item_ || total_bytes <= 0 || bytes_received >= total_bytes ||
56 request_num_ <= 1) {
57 return;
58 }
59
60 int64_t bytes_left = total_bytes - bytes_received;
61 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.
62 DCHECK(piece_size > 0);
63
64 // 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.
65 // |min_length_|.
66 int num_requests = bytes_left / piece_size;
67 int64_t current_offset = bytes_received + piece_size;
68
69 for (int i = 0; i < num_requests - 1; ++i) {
70 int64_t length = (i == (num_requests - 2))
71 ? piece_size + (bytes_left % piece_size)
72 : piece_size;
73 CreateRequest(current_offset, length);
74 current_offset += piece_size;
75 }
76 }
77
78 void ParallelDownloadJob::CreateRequest(int64_t offset, int64_t length) {
79 std::unique_ptr<DownloadUrlTask> task = base::MakeUnique<DownloadUrlTask>();
80
81 DCHECK(download_item_);
82 StoragePartition* storage_partition =
83 BrowserContext::GetStoragePartitionForSite(
84 download_item_->GetBrowserContext(), download_item_->GetSiteUrl());
85
86 std::unique_ptr<DownloadUrlParameters> download_params(
87 new DownloadUrlParameters(download_item_->GetURL(),
88 storage_partition->GetURLRequestContext()));
89 download_params->set_file_path(download_item_->GetFullPath());
90 download_params->set_last_modified(download_item_->GetLastModifiedTime());
91 download_params->set_etag(download_item_->GetETag());
92 download_params->set_offset(offset);
93 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.
94
95 // Subsequent range requests have the same referrer URL as the original
96 // download request.
97 download_params->set_referrer(Referrer(download_item_->GetReferrerUrl(),
98 blink::WebReferrerPolicyAlways));
99 // Send the request.
100 task->SendRequest(std::move(download_params));
101 tasks_.push_back(std::move(task));
102 }
103
104 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698