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

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

Issue 2752603002: Propagate server response error and interrupt the download. (Closed)
Patch Set: Work on feedbacks. 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"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 42
43 void ParallelDownloadJob::Resume(bool resume_request) { 43 void ParallelDownloadJob::Resume(bool resume_request) {
44 DownloadJobImpl::Resume(resume_request); 44 DownloadJobImpl::Resume(resume_request);
45 if (!resume_request) 45 if (!resume_request)
46 return; 46 return;
47 47
48 for (auto& worker : workers_) 48 for (auto& worker : workers_)
49 worker->Resume(); 49 worker->Resume();
50 } 50 }
51 51
52 void ParallelDownloadJob::OnServerResponseError(
53 DownloadWorker* worker,
54 DownloadInterruptReason reason) {
55 // TODO(xingliu): Consider to let the original request to cover the full
56 // content if the sub-requests get invalid response. Consider retry on certain
57 // error.
58 DownloadJob::Interrupt(reason);
59 }
60
52 void ParallelDownloadJob::ForkRequestsForNewDownload(int64_t bytes_received, 61 void ParallelDownloadJob::ForkRequestsForNewDownload(int64_t bytes_received,
53 int64_t total_bytes, 62 int64_t total_bytes,
54 int request_count) { 63 int request_count) {
55 if (!download_item_ || total_bytes <= 0 || bytes_received >= total_bytes || 64 if (!download_item_ || total_bytes <= 0 || bytes_received >= total_bytes ||
56 request_count <= 1) { 65 request_count <= 1) {
57 return; 66 return;
58 } 67 }
59 68
60 int64_t bytes_left = total_bytes - bytes_received; 69 int64_t bytes_left = total_bytes - bytes_received;
61 int64_t slice_size = bytes_left / request_count; 70 int64_t slice_size = bytes_left / request_count;
62 slice_size = slice_size > 0 ? slice_size : 1; 71 slice_size = slice_size > 0 ? slice_size : 1;
63 int num_requests = bytes_left / slice_size; 72 int num_requests = bytes_left / slice_size;
64 int64_t current_offset = bytes_received + slice_size; 73 int64_t current_offset = bytes_received + slice_size;
65 74
66 // TODO(xingliu): Add records for slices in history db.
67 for (int i = 0; i < num_requests - 1; ++i) { 75 for (int i = 0; i < num_requests - 1; ++i) {
68 int64_t length = (i == (num_requests - 2)) 76 int64_t length = (i == (num_requests - 2))
69 ? slice_size + (bytes_left % slice_size) 77 ? slice_size + (bytes_left % slice_size)
70 : slice_size; 78 : slice_size;
71 CreateRequest(current_offset, length); 79 CreateRequest(current_offset, length);
72 current_offset += slice_size; 80 current_offset += slice_size;
73 } 81 }
74 } 82 }
75 83
76 void ParallelDownloadJob::BuildParallelRequests() { 84 void ParallelDownloadJob::BuildParallelRequests() {
(...skipping 16 matching lines...) Expand all
93 // new requests. For the remaining slices, they will be handled once some 101 // new requests. For the remaining slices, they will be handled once some
94 // of the workers finish their job. 102 // of the workers finish their job.
95 } else { 103 } else {
96 // TODO(qinmin): Check the size of the last slice. If it is huge, we can 104 // TODO(qinmin): Check the size of the last slice. If it is huge, we can
97 // split it into N pieces and pass the last N-1 pirces to different workers. 105 // split it into N pieces and pass the last N-1 pirces to different workers.
98 // Otherwise, just fork |slices_to_download.size()| number of workers. 106 // Otherwise, just fork |slices_to_download.size()| number of workers.
99 } 107 }
100 } 108 }
101 109
102 void ParallelDownloadJob::CreateRequest(int64_t offset, int64_t length) { 110 void ParallelDownloadJob::CreateRequest(int64_t offset, int64_t length) {
103 std::unique_ptr<DownloadWorker> worker = base::MakeUnique<DownloadWorker>(); 111 std::unique_ptr<DownloadWorker> worker =
112 base::MakeUnique<DownloadWorker>(this, offset, length);
104 113
105 DCHECK(download_item_); 114 DCHECK(download_item_);
106 StoragePartition* storage_partition = 115 StoragePartition* storage_partition =
107 BrowserContext::GetStoragePartitionForSite( 116 BrowserContext::GetStoragePartitionForSite(
108 download_item_->GetBrowserContext(), download_item_->GetSiteUrl()); 117 download_item_->GetBrowserContext(), download_item_->GetSiteUrl());
109 118
110 std::unique_ptr<DownloadUrlParameters> download_params( 119 std::unique_ptr<DownloadUrlParameters> download_params(
111 new DownloadUrlParameters(download_item_->GetURL(), 120 new DownloadUrlParameters(download_item_->GetURL(),
112 storage_partition->GetURLRequestContext())); 121 storage_partition->GetURLRequestContext()));
113 download_params->set_file_path(download_item_->GetFullPath()); 122 download_params->set_file_path(download_item_->GetFullPath());
114 download_params->set_last_modified(download_item_->GetLastModifiedTime()); 123 download_params->set_last_modified(download_item_->GetLastModifiedTime());
115 download_params->set_etag(download_item_->GetETag()); 124 download_params->set_etag(download_item_->GetETag());
116 download_params->set_offset(offset); 125 download_params->set_offset(offset);
117 126
118 // Setting the length will result in range request to fetch a slice of the 127 // Setting the length will result in range request to fetch a slice of the
119 // file. 128 // file.
120 download_params->set_length(length); 129 download_params->set_length(length);
121 130
122 // Subsequent range requests have the same referrer URL as the original 131 // Subsequent range requests have the same referrer URL as the original
123 // download request. 132 // download request.
124 download_params->set_referrer(Referrer(download_item_->GetReferrerUrl(), 133 download_params->set_referrer(Referrer(download_item_->GetReferrerUrl(),
125 blink::WebReferrerPolicyAlways)); 134 blink::WebReferrerPolicyAlways));
126 // Send the request. 135 // Send the request.
127 worker->SendRequest(std::move(download_params)); 136 worker->SendRequest(std::move(download_params));
128 workers_.push_back(std::move(worker)); 137 workers_.push_back(std::move(worker));
129 } 138 }
130 139
131 } // namespace content 140 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698