| OLD | NEW |
| (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 // Default minimum size of bytes for each range request. |
| 21 // If bytes left are less than this, no additional requests will be created. |
| 22 const int64_t kMinRangeLength = 1024; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 ParallelDownloadJob::ParallelDownloadJob( |
| 27 DownloadItemImpl* download_item, |
| 28 std::unique_ptr<DownloadRequestHandleInterface> request_handle) |
| 29 : DownloadJobImpl(download_item, std::move(request_handle)), |
| 30 request_num_(kParallelRequestCount), |
| 31 min_length_(kMinRangeLength) {} |
| 32 |
| 33 ParallelDownloadJob::~ParallelDownloadJob() = default; |
| 34 |
| 35 void ParallelDownloadJob::Cancel(bool user_cancel) { |
| 36 DownloadJobImpl::Cancel(user_cancel); |
| 37 for (auto& worker : workers_) |
| 38 worker->Cancel(); |
| 39 } |
| 40 |
| 41 void ParallelDownloadJob::Pause() { |
| 42 DownloadJobImpl::Pause(); |
| 43 for (auto& worker : workers_) |
| 44 worker->Pause(); |
| 45 } |
| 46 |
| 47 void ParallelDownloadJob::Resume() { |
| 48 for (auto& worker : workers_) |
| 49 worker->Resume(); |
| 50 DownloadJobImpl::Resume(); |
| 51 } |
| 52 |
| 53 void ParallelDownloadJob::ForkRequestsForNewDownload(int64_t bytes_received, |
| 54 int64_t total_bytes) { |
| 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 slice_size = std::max(bytes_left / request_num_, min_length_); |
| 62 DCHECK(slice_size > 0); |
| 63 |
| 64 // We may produce fewer requests if (bytes_left / request_num_) is less than |
| 65 // |min_length_|. |
| 66 int num_requests = bytes_left / slice_size; |
| 67 int64_t current_offset = bytes_received + slice_size; |
| 68 |
| 69 // TODO(xingliu): Add records for slices in history db. |
| 70 for (int i = 0; i < num_requests - 1; ++i) { |
| 71 int64_t length = (i == (num_requests - 2)) |
| 72 ? slice_size + (bytes_left % slice_size) |
| 73 : slice_size; |
| 74 CreateRequest(current_offset, length); |
| 75 current_offset += slice_size; |
| 76 } |
| 77 } |
| 78 |
| 79 void ParallelDownloadJob::CreateRequest(int64_t offset, int64_t length) { |
| 80 std::unique_ptr<DownloadWorker> worker = base::MakeUnique<DownloadWorker>(); |
| 81 |
| 82 DCHECK(download_item_); |
| 83 StoragePartition* storage_partition = |
| 84 BrowserContext::GetStoragePartitionForSite( |
| 85 download_item_->GetBrowserContext(), download_item_->GetSiteUrl()); |
| 86 |
| 87 std::unique_ptr<DownloadUrlParameters> download_params( |
| 88 new DownloadUrlParameters(download_item_->GetURL(), |
| 89 storage_partition->GetURLRequestContext())); |
| 90 download_params->set_file_path(download_item_->GetFullPath()); |
| 91 download_params->set_last_modified(download_item_->GetLastModifiedTime()); |
| 92 download_params->set_etag(download_item_->GetETag()); |
| 93 download_params->set_offset(offset); |
| 94 |
| 95 // Setting the length will result in range request to fetch a slice of the |
| 96 // file. |
| 97 download_params->set_length(length); |
| 98 |
| 99 // Subsequent range requests have the same referrer URL as the original |
| 100 // download request. |
| 101 download_params->set_referrer(Referrer(download_item_->GetReferrerUrl(), |
| 102 blink::WebReferrerPolicyAlways)); |
| 103 // Send the request. |
| 104 worker->SendRequest(std::move(download_params)); |
| 105 workers_.push_back(std::move(worker)); |
| 106 } |
| 107 |
| 108 } // namespace content |
| OLD | NEW |