| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/download/parallel_download_job.h" | 5 #include "content/browser/download/parallel_download_job.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "content/browser/download/download_create_info.h" | 8 #include "content/browser/download/download_create_info.h" |
| 9 #include "content/browser/download/parallel_download_utils.h" | 9 #include "content/browser/download/parallel_download_utils.h" |
| 10 #include "content/public/browser/browser_context.h" | 10 #include "content/public/browser/browser_context.h" |
| 11 #include "content/public/browser/storage_partition.h" | 11 #include "content/public/browser/storage_partition.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 | 14 |
| 15 namespace { | |
| 16 | |
| 17 // TODO(xingliu): Use finch parameters to configure constants. | |
| 18 // Default number of requests in a parallel download, including the original | |
| 19 // request. | |
| 20 const int kParallelRequestCount = 2; | |
| 21 | |
| 22 } // namespace | |
| 23 | |
| 24 ParallelDownloadJob::ParallelDownloadJob( | 15 ParallelDownloadJob::ParallelDownloadJob( |
| 25 DownloadItemImpl* download_item, | 16 DownloadItemImpl* download_item, |
| 26 std::unique_ptr<DownloadRequestHandleInterface> request_handle, | 17 std::unique_ptr<DownloadRequestHandleInterface> request_handle, |
| 27 const DownloadCreateInfo& create_info) | 18 const DownloadCreateInfo& create_info) |
| 28 : DownloadJobImpl(download_item, std::move(request_handle)), | 19 : DownloadJobImpl(download_item, std::move(request_handle)), |
| 29 request_num_(kParallelRequestCount), | |
| 30 initial_request_offset_(create_info.save_info->offset), | 20 initial_request_offset_(create_info.save_info->offset), |
| 31 initial_request_length_(create_info.save_info->length) {} | 21 initial_request_length_(create_info.save_info->length) {} |
| 32 | 22 |
| 33 ParallelDownloadJob::~ParallelDownloadJob() = default; | 23 ParallelDownloadJob::~ParallelDownloadJob() = default; |
| 34 | 24 |
| 35 void ParallelDownloadJob::Start() { | 25 void ParallelDownloadJob::Start() { |
| 36 DownloadJobImpl::Start(); | 26 DownloadJobImpl::Start(); |
| 37 | 27 |
| 38 BuildParallelRequests(); | 28 BuildParallelRequests(); |
| 39 } | 29 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 53 void ParallelDownloadJob::Resume(bool resume_request) { | 43 void ParallelDownloadJob::Resume(bool resume_request) { |
| 54 DownloadJobImpl::Resume(resume_request); | 44 DownloadJobImpl::Resume(resume_request); |
| 55 if (!resume_request) | 45 if (!resume_request) |
| 56 return; | 46 return; |
| 57 | 47 |
| 58 for (auto& worker : workers_) | 48 for (auto& worker : workers_) |
| 59 worker->Resume(); | 49 worker->Resume(); |
| 60 } | 50 } |
| 61 | 51 |
| 62 void ParallelDownloadJob::ForkRequestsForNewDownload(int64_t bytes_received, | 52 void ParallelDownloadJob::ForkRequestsForNewDownload(int64_t bytes_received, |
| 63 int64_t total_bytes) { | 53 int64_t total_bytes, |
| 54 int request_count) { |
| 64 if (!download_item_ || total_bytes <= 0 || bytes_received >= total_bytes || | 55 if (!download_item_ || total_bytes <= 0 || bytes_received >= total_bytes || |
| 65 request_num_ <= 1) { | 56 request_count <= 1) { |
| 66 return; | 57 return; |
| 67 } | 58 } |
| 68 | 59 |
| 69 int64_t bytes_left = total_bytes - bytes_received; | 60 int64_t bytes_left = total_bytes - bytes_received; |
| 70 int64_t slice_size = bytes_left / request_num_; | 61 int64_t slice_size = bytes_left / request_count; |
| 71 slice_size = slice_size > 0 ? slice_size : 1; | 62 slice_size = slice_size > 0 ? slice_size : 1; |
| 72 int num_requests = bytes_left / slice_size; | 63 int num_requests = bytes_left / slice_size; |
| 73 int64_t current_offset = bytes_received + slice_size; | 64 int64_t current_offset = bytes_received + slice_size; |
| 74 | 65 |
| 75 // TODO(xingliu): Add records for slices in history db. | 66 // TODO(xingliu): Add records for slices in history db. |
| 76 for (int i = 0; i < num_requests - 1; ++i) { | 67 for (int i = 0; i < num_requests - 1; ++i) { |
| 77 int64_t length = (i == (num_requests - 2)) | 68 int64_t length = (i == (num_requests - 2)) |
| 78 ? slice_size + (bytes_left % slice_size) | 69 ? slice_size + (bytes_left % slice_size) |
| 79 : slice_size; | 70 : slice_size; |
| 80 CreateRequest(current_offset, length); | 71 CreateRequest(current_offset, length); |
| 81 current_offset += slice_size; | 72 current_offset += slice_size; |
| 82 } | 73 } |
| 83 } | 74 } |
| 84 | 75 |
| 85 void ParallelDownloadJob::BuildParallelRequests() { | 76 void ParallelDownloadJob::BuildParallelRequests() { |
| 86 // Calculate the slices to download and fork parallel requests. | 77 // Calculate the slices to download and fork parallel requests. |
| 87 std::vector<DownloadItem::ReceivedSlice> slices_to_download = | 78 std::vector<DownloadItem::ReceivedSlice> slices_to_download = |
| 88 FindSlicesToDownload(download_item_->GetReceivedSlices()); | 79 FindSlicesToDownload(download_item_->GetReceivedSlices()); |
| 89 // The initial request has already been sent, it should cover the first slice. | 80 // The initial request has already been sent, it should cover the first slice. |
| 90 DCHECK_GE(slices_to_download[0].offset, initial_request_offset_); | 81 DCHECK_GE(slices_to_download[0].offset, initial_request_offset_); |
| 91 DCHECK(initial_request_length_ == DownloadSaveInfo::kLengthFullContent || | 82 DCHECK(initial_request_length_ == DownloadSaveInfo::kLengthFullContent || |
| 92 initial_request_offset_ + initial_request_length_ >= | 83 initial_request_offset_ + initial_request_length_ >= |
| 93 slices_to_download[0].offset + | 84 slices_to_download[0].offset + |
| 94 slices_to_download[0].received_bytes); | 85 slices_to_download[0].received_bytes); |
| 95 if (slices_to_download.size() >= kParallelRequestCount) { | 86 if (slices_to_download.size() >= |
| 87 static_cast<size_t>(GetParallelRequestCountConfig())) { |
| 96 // The size of |slices_to_download| should be no larger than | 88 // The size of |slices_to_download| should be no larger than |
| 97 // |kParallelRequestCount| unless |kParallelRequestCount| is changed after | 89 // |kParallelRequestCount| unless |kParallelRequestCount| is changed after |
| 98 // a download is interrupted. This could happen if we use finch to config | 90 // a download is interrupted. This could happen if we use finch to config |
| 99 // the number of parallel requests. | 91 // the number of parallel requests. |
| 100 // TODO(qinmin): Get the next |kParallelRequestCount - 1| slices and fork | 92 // TODO(qinmin): Get the next |kParallelRequestCount - 1| slices and fork |
| 101 // new requests. For the remaining slices, they will be handled once some | 93 // new requests. For the remaining slices, they will be handled once some |
| 102 // of the workers finish their job. | 94 // of the workers finish their job. |
| 103 } else { | 95 } else { |
| 104 // TODO(qinmin): Check the size of the last slice. If it is huge, we can | 96 // TODO(qinmin): Check the size of the last slice. If it is huge, we can |
| 105 // split it into N pieces and pass the last N-1 pirces to different workers. | 97 // split it into N pieces and pass the last N-1 pirces to different workers. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 130 // Subsequent range requests have the same referrer URL as the original | 122 // Subsequent range requests have the same referrer URL as the original |
| 131 // download request. | 123 // download request. |
| 132 download_params->set_referrer(Referrer(download_item_->GetReferrerUrl(), | 124 download_params->set_referrer(Referrer(download_item_->GetReferrerUrl(), |
| 133 blink::WebReferrerPolicyAlways)); | 125 blink::WebReferrerPolicyAlways)); |
| 134 // Send the request. | 126 // Send the request. |
| 135 worker->SendRequest(std::move(download_params)); | 127 worker->SendRequest(std::move(download_params)); |
| 136 workers_.push_back(std::move(worker)); | 128 workers_.push_back(std::move(worker)); |
| 137 } | 129 } |
| 138 | 130 |
| 139 } // namespace content | 131 } // namespace content |
| OLD | NEW |