| 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 } // 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(bool resume_request) { |
| 43 DownloadJobImpl::Resume(resume_request); |
| 44 if (!resume_request) |
| 45 return; |
| 46 |
| 47 for (auto& worker : workers_) |
| 48 worker->Resume(); |
| 49 } |
| 50 |
| 51 void ParallelDownloadJob::ForkRequestsForNewDownload(int64_t bytes_received, |
| 52 int64_t total_bytes) { |
| 53 if (!download_item_ || total_bytes <= 0 || bytes_received >= total_bytes || |
| 54 request_num_ <= 1) { |
| 55 return; |
| 56 } |
| 57 |
| 58 int64_t bytes_left = total_bytes - bytes_received; |
| 59 int64_t slice_size = bytes_left / request_num_; |
| 60 slice_size = slice_size > 0 ? slice_size : 1; |
| 61 int num_requests = bytes_left / slice_size; |
| 62 int64_t current_offset = bytes_received + slice_size; |
| 63 |
| 64 // TODO(xingliu): Add records for slices in history db. |
| 65 for (int i = 0; i < num_requests - 1; ++i) { |
| 66 int64_t length = (i == (num_requests - 2)) |
| 67 ? slice_size + (bytes_left % slice_size) |
| 68 : slice_size; |
| 69 CreateRequest(current_offset, length); |
| 70 current_offset += slice_size; |
| 71 } |
| 72 } |
| 73 |
| 74 void ParallelDownloadJob::CreateRequest(int64_t offset, int64_t length) { |
| 75 std::unique_ptr<DownloadWorker> worker = base::MakeUnique<DownloadWorker>(); |
| 76 |
| 77 DCHECK(download_item_); |
| 78 StoragePartition* storage_partition = |
| 79 BrowserContext::GetStoragePartitionForSite( |
| 80 download_item_->GetBrowserContext(), download_item_->GetSiteUrl()); |
| 81 |
| 82 std::unique_ptr<DownloadUrlParameters> download_params( |
| 83 new DownloadUrlParameters(download_item_->GetURL(), |
| 84 storage_partition->GetURLRequestContext())); |
| 85 download_params->set_file_path(download_item_->GetFullPath()); |
| 86 download_params->set_last_modified(download_item_->GetLastModifiedTime()); |
| 87 download_params->set_etag(download_item_->GetETag()); |
| 88 download_params->set_offset(offset); |
| 89 |
| 90 // Setting the length will result in range request to fetch a slice of the |
| 91 // file. |
| 92 download_params->set_length(length); |
| 93 |
| 94 // Subsequent range requests have the same referrer URL as the original |
| 95 // download request. |
| 96 download_params->set_referrer(Referrer(download_item_->GetReferrerUrl(), |
| 97 blink::WebReferrerPolicyAlways)); |
| 98 // Send the request. |
| 99 worker->SendRequest(std::move(download_params)); |
| 100 workers_.push_back(std::move(worker)); |
| 101 } |
| 102 |
| 103 } // namespace content |
| OLD | NEW |