| 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 namespace { |
| 15 |
| 16 const int kVerboseLevel = 1; |
| 17 |
| 18 } // namespace |
| 14 | 19 |
| 15 ParallelDownloadJob::ParallelDownloadJob( | 20 ParallelDownloadJob::ParallelDownloadJob( |
| 16 DownloadItemImpl* download_item, | 21 DownloadItemImpl* download_item, |
| 17 std::unique_ptr<DownloadRequestHandleInterface> request_handle, | 22 std::unique_ptr<DownloadRequestHandleInterface> request_handle, |
| 18 const DownloadCreateInfo& create_info) | 23 const DownloadCreateInfo& create_info) |
| 19 : DownloadJobImpl(download_item, std::move(request_handle)), | 24 : DownloadJobImpl(download_item, std::move(request_handle)), |
| 20 initial_request_offset_(create_info.offset), | 25 initial_request_offset_(create_info.offset), |
| 21 content_length_(create_info.total_bytes), | 26 content_length_(create_info.total_bytes), |
| 22 requests_sent_(false) {} | 27 requests_sent_(false), |
| 28 is_canceled_(false) {} |
| 23 | 29 |
| 24 ParallelDownloadJob::~ParallelDownloadJob() = default; | 30 ParallelDownloadJob::~ParallelDownloadJob() = default; |
| 25 | 31 |
| 26 void ParallelDownloadJob::Start() { | 32 void ParallelDownloadJob::Start() { |
| 27 DownloadJobImpl::Start(); | 33 DownloadJobImpl::Start(); |
| 28 | 34 |
| 29 BuildParallelRequestAfterDelay(); | 35 BuildParallelRequestAfterDelay(); |
| 30 } | 36 } |
| 31 | 37 |
| 32 void ParallelDownloadJob::Cancel(bool user_cancel) { | 38 void ParallelDownloadJob::Cancel(bool user_cancel) { |
| 39 is_canceled_ = true; |
| 33 DownloadJobImpl::Cancel(user_cancel); | 40 DownloadJobImpl::Cancel(user_cancel); |
| 34 | 41 |
| 35 if (!requests_sent_) { | 42 if (!requests_sent_) { |
| 36 timer_.Stop(); | 43 timer_.Stop(); |
| 37 return; | 44 return; |
| 38 } | 45 } |
| 39 | 46 |
| 40 for (auto& worker : workers_) | 47 for (auto& worker : workers_) |
| 41 worker.second->Cancel(); | 48 worker.second->Cancel(); |
| 42 } | 49 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 62 if (!requests_sent_) { | 69 if (!requests_sent_) { |
| 63 if (!timer_.IsRunning()) | 70 if (!timer_.IsRunning()) |
| 64 BuildParallelRequestAfterDelay(); | 71 BuildParallelRequestAfterDelay(); |
| 65 return; | 72 return; |
| 66 } | 73 } |
| 67 | 74 |
| 68 for (auto& worker : workers_) | 75 for (auto& worker : workers_) |
| 69 worker.second->Resume(); | 76 worker.second->Resume(); |
| 70 } | 77 } |
| 71 | 78 |
| 72 | |
| 73 int ParallelDownloadJob::GetParallelRequestCount() const { | 79 int ParallelDownloadJob::GetParallelRequestCount() const { |
| 74 return GetParallelRequestCountConfig(); | 80 return GetParallelRequestCountConfig(); |
| 75 } | 81 } |
| 76 | 82 |
| 77 bool ParallelDownloadJob::UsesParallelRequests() const { | 83 bool ParallelDownloadJob::UsesParallelRequests() const { |
| 78 return true; | 84 return true; |
| 79 } | 85 } |
| 80 | 86 |
| 81 void ParallelDownloadJob::BuildParallelRequestAfterDelay() { | 87 void ParallelDownloadJob::BuildParallelRequestAfterDelay() { |
| 82 DCHECK(workers_.empty()); | 88 DCHECK(workers_.empty()); |
| 83 DCHECK(!requests_sent_); | 89 DCHECK(!requests_sent_); |
| 84 DCHECK(!timer_.IsRunning()); | 90 DCHECK(!timer_.IsRunning()); |
| 85 | 91 |
| 86 timer_.Start(FROM_HERE, GetParallelRequestDelayConfig(), this, | 92 timer_.Start(FROM_HERE, GetParallelRequestDelayConfig(), this, |
| 87 &ParallelDownloadJob::BuildParallelRequests); | 93 &ParallelDownloadJob::BuildParallelRequests); |
| 88 } | 94 } |
| 89 | 95 |
| 90 void ParallelDownloadJob::OnByteStreamReady( | 96 void ParallelDownloadJob::OnByteStreamReady( |
| 91 DownloadWorker* worker, | 97 DownloadWorker* worker, |
| 92 std::unique_ptr<ByteStreamReader> stream_reader) { | 98 std::unique_ptr<ByteStreamReader> stream_reader) { |
| 93 DownloadJob::AddByteStream(std::move(stream_reader), worker->offset(), | 99 bool success = DownloadJob::AddByteStream(std::move(stream_reader), |
| 94 worker->length()); | 100 worker->offset(), worker->length()); |
| 101 |
| 102 // Destroy the request if the sink is gone. |
| 103 if (!success) { |
| 104 VLOG(kVerboseLevel) |
| 105 << "Byte stream arrived after download file is released."; |
| 106 worker->Cancel(); |
| 107 } |
| 95 } | 108 } |
| 96 | 109 |
| 97 void ParallelDownloadJob::OnServerResponseError( | 110 void ParallelDownloadJob::OnServerResponseError( |
| 98 DownloadWorker* worker, | 111 DownloadWorker* worker, |
| 99 DownloadInterruptReason reason) { | 112 DownloadInterruptReason reason) { |
| 100 // TODO(xingliu): Consider to let the original request to cover the full | 113 // TODO(xingliu): Consider to let the original request to cover the full |
| 101 // content if the sub-requests get invalid response. Consider retry on certain | 114 // content if the sub-requests get invalid response. Consider retry on certain |
| 102 // error. | 115 // error. |
| 103 DownloadJob::Interrupt(reason); | 116 DownloadJob::Interrupt(reason); |
| 104 } | 117 } |
| 105 | 118 |
| 106 void ParallelDownloadJob::BuildParallelRequests() { | 119 void ParallelDownloadJob::BuildParallelRequests() { |
| 107 DCHECK(!requests_sent_); | 120 DCHECK(!requests_sent_); |
| 121 DCHECK(!is_paused()); |
| 122 if (is_canceled_) |
| 123 return; |
| 124 |
| 108 // TODO(qinmin): The size of |slices_to_download| should be no larger than | 125 // TODO(qinmin): The size of |slices_to_download| should be no larger than |
| 109 // |kParallelRequestCount| unless |kParallelRequestCount| is changed after | 126 // |kParallelRequestCount| unless |kParallelRequestCount| is changed after |
| 110 // a download is interrupted. This could happen if we use finch to config | 127 // a download is interrupted. This could happen if we use finch to config |
| 111 // the number of parallel requests. | 128 // the number of parallel requests. |
| 112 // Get the next |kParallelRequestCount - 1| slices and fork | 129 // Get the next |kParallelRequestCount - 1| slices and fork |
| 113 // new requests. For the remaining slices, they will be handled once some | 130 // new requests. For the remaining slices, they will be handled once some |
| 114 // of the workers finish their job. | 131 // of the workers finish their job. |
| 115 DownloadItem::ReceivedSlices slices_to_download; | 132 DownloadItem::ReceivedSlices slices_to_download; |
| 116 if (download_item_->GetReceivedSlices().empty()) { | 133 if (download_item_->GetReceivedSlices().empty()) { |
| 117 slices_to_download = FindSlicesForRemainingContent( | 134 slices_to_download = FindSlicesForRemainingContent( |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 // download request. | 192 // download request. |
| 176 download_params->set_referrer(Referrer(download_item_->GetReferrerUrl(), | 193 download_params->set_referrer(Referrer(download_item_->GetReferrerUrl(), |
| 177 blink::WebReferrerPolicyAlways)); | 194 blink::WebReferrerPolicyAlways)); |
| 178 // Send the request. | 195 // Send the request. |
| 179 worker->SendRequest(std::move(download_params)); | 196 worker->SendRequest(std::move(download_params)); |
| 180 DCHECK(workers_.find(offset) == workers_.end()); | 197 DCHECK(workers_.find(offset) == workers_.end()); |
| 181 workers_[offset] = std::move(worker); | 198 workers_[offset] = std::move(worker); |
| 182 } | 199 } |
| 183 | 200 |
| 184 } // namespace content | 201 } // namespace content |
| OLD | NEW |