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