Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: content/browser/download/parallel_download_job.cc

Issue 2767593003: Handle early pause, cancel for parallel requests. (Closed)
Patch Set: Work on feedback. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 DCHECK(!requests_sent_); 83 DCHECK(!requests_sent_);
79 DCHECK(!timer_.IsRunning()); 84 DCHECK(!timer_.IsRunning());
80 85
81 timer_.Start(FROM_HERE, GetParallelRequestDelayConfig(), this, 86 timer_.Start(FROM_HERE, GetParallelRequestDelayConfig(), this,
82 &ParallelDownloadJob::BuildParallelRequests); 87 &ParallelDownloadJob::BuildParallelRequests);
83 } 88 }
84 89
85 void ParallelDownloadJob::OnByteStreamReady( 90 void ParallelDownloadJob::OnByteStreamReady(
86 DownloadWorker* worker, 91 DownloadWorker* worker,
87 std::unique_ptr<ByteStreamReader> stream_reader) { 92 std::unique_ptr<ByteStreamReader> stream_reader) {
88 DownloadJob::AddByteStream(std::move(stream_reader), worker->offset(), 93 bool success = DownloadJob::AddByteStream(std::move(stream_reader),
89 worker->length()); 94 worker->offset(), worker->length());
95
96 // Destroy the request if the sink is gone.
97 if (!success) {
98 VLOG(kVerboseLevel)
99 << "Byte stream arrived after download file is released.";
100 worker->Cancel();
101 }
90 } 102 }
91 103
92 void ParallelDownloadJob::OnServerResponseError( 104 void ParallelDownloadJob::OnServerResponseError(
93 DownloadWorker* worker, 105 DownloadWorker* worker,
94 DownloadInterruptReason reason) { 106 DownloadInterruptReason reason) {
95 // TODO(xingliu): Consider to let the original request to cover the full 107 // TODO(xingliu): Consider to let the original request to cover the full
96 // content if the sub-requests get invalid response. Consider retry on certain 108 // content if the sub-requests get invalid response. Consider retry on certain
97 // error. 109 // error.
98 DownloadJob::Interrupt(reason); 110 DownloadJob::Interrupt(reason);
99 } 111 }
100 112
113 bool ParallelDownloadJob::IsPaused() const {
114 return DownloadJob::is_paused();
115 }
116
117 bool ParallelDownloadJob::IsCanceled() const {
118 return DownloadJob::is_canceled();
119 }
120
101 void ParallelDownloadJob::BuildParallelRequests() { 121 void ParallelDownloadJob::BuildParallelRequests() {
102 DCHECK(!requests_sent_); 122 DCHECK(!requests_sent_);
103 // 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
104 // |kParallelRequestCount| unless |kParallelRequestCount| is changed after 124 // |kParallelRequestCount| unless |kParallelRequestCount| is changed after
105 // 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
106 // the number of parallel requests. 126 // the number of parallel requests.
107 // Get the next |kParallelRequestCount - 1| slices and fork 127 // Get the next |kParallelRequestCount - 1| slices and fork
108 // 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
109 // of the workers finish their job. 129 // of the workers finish their job.
110 DownloadItem::ReceivedSlices slices_to_download; 130 DownloadItem::ReceivedSlices slices_to_download;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 // download request. 190 // download request.
171 download_params->set_referrer(Referrer(download_item_->GetReferrerUrl(), 191 download_params->set_referrer(Referrer(download_item_->GetReferrerUrl(),
172 blink::WebReferrerPolicyAlways)); 192 blink::WebReferrerPolicyAlways));
173 // Send the request. 193 // Send the request.
174 worker->SendRequest(std::move(download_params)); 194 worker->SendRequest(std::move(download_params));
175 DCHECK(workers_.find(offset) == workers_.end()); 195 DCHECK(workers_.find(offset) == workers_.end());
176 workers_[offset] = std::move(worker); 196 workers_[offset] = std::move(worker);
177 } 197 }
178 198
179 } // namespace content 199 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698