Chromium Code Reviews| 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_utils.h" | 5 #include "content/browser/download/parallel_download_utils.h" |
| 6 | 6 |
| 7 #include "base/metrics/field_trial_params.h" | 7 #include "base/metrics/field_trial_params.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "content/public/browser/download_save_info.h" | 9 #include "content/public/browser/download_save_info.h" |
| 10 #include "content/public/common/content_features.h" | 10 #include "content/public/common/content_features.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 // Finch parameter key value for number of parallel requests in a parallel | 23 // Finch parameter key value for number of parallel requests in a parallel |
| 24 // download, including the original request. | 24 // download, including the original request. |
| 25 const char kParallelRequestCountFinchKey[] = "request_count"; | 25 const char kParallelRequestCountFinchKey[] = "request_count"; |
| 26 | 26 |
| 27 // Default value for |kParallelRequestCountFinchKey|, when no parameter is | 27 // Default value for |kParallelRequestCountFinchKey|, when no parameter is |
| 28 // specified. | 28 // specified. |
| 29 const int kParallelRequestCount = 2; | 29 const int kParallelRequestCount = 2; |
| 30 | 30 |
| 31 } // namespace | 31 } // namespace |
| 32 | 32 |
| 33 bool ShouldUseParallelDownload(const DownloadCreateInfo& create_info) { | |
| 34 // 1. Accept-Ranges, Content-Length and strong validators response headers. | |
| 35 // 2. Feature |kParallelDownloading| enabled. | |
| 36 // 3. Content-Length is no less than the minimum slice size configuration. | |
| 37 // 3. (Undetermined) Http/1.1 protocol. | |
|
asanka
2017/03/17 17:10:09
Probably needs to be plumbed up from DownloadReque
xingliu
2017/03/17 19:08:26
Thanks, will do it in a separate CL.
| |
| 38 // 4. (Undetermined) Not under http proxy, e.g. data saver. | |
|
asanka
2017/03/17 17:10:09
I'd suggest being a bit conservative about avoidin
xingliu
2017/03/17 19:08:26
Make sense, removed the http proxy line.
| |
| 39 | |
| 40 // Etag and last modified are stored into DownloadCreateInfo in | |
| 41 // DownloadRequestCore only if the response header complies to the strong | |
| 42 // validator rule. | |
| 43 bool has_strong_validator = | |
| 44 !create_info.etag.empty() || !create_info.last_modified.empty(); | |
| 45 | |
| 46 return has_strong_validator && create_info.accept_range && | |
| 47 create_info.total_bytes >= GetMinSliceSizeConfig() && | |
| 48 base::FeatureList::IsEnabled(features::kParallelDownloading); | |
| 49 } | |
| 50 | |
| 51 std::vector<DownloadItem::ReceivedSlice> FindSlicesForRemainingContent( | |
| 52 int64_t bytes_received, | |
| 53 int64_t total_bytes, | |
| 54 int request_count) { | |
| 55 std::vector<DownloadItem::ReceivedSlice> slices_to_download; | |
| 56 if (bytes_received >= total_bytes || request_count <= 0) | |
| 57 return slices_to_download; | |
| 58 | |
| 59 // TODO(xingliu): Consider to use minimum size of a slice. | |
| 60 int64_t bytes_left = total_bytes - bytes_received; | |
| 61 int64_t slice_size = bytes_left / request_count; | |
| 62 slice_size = slice_size > 0 ? slice_size : 1; | |
| 63 int64_t current_offset = bytes_received; | |
| 64 for (int i = 0, num_requests = bytes_left / slice_size; i < num_requests - 1; | |
| 65 ++i) { | |
| 66 slices_to_download.emplace_back(current_offset, slice_size); | |
| 67 current_offset += slice_size; | |
| 68 } | |
| 69 | |
| 70 // Last slice is a half open slice, which results in sending range request | |
| 71 // like "Range:50-" to fetch from 50 bytes to the end of the file. | |
| 72 slices_to_download.emplace_back(current_offset, | |
| 73 DownloadSaveInfo::kLengthFullContent); | |
| 74 return slices_to_download; | |
| 75 } | |
| 76 | |
| 33 std::vector<DownloadItem::ReceivedSlice> FindSlicesToDownload( | 77 std::vector<DownloadItem::ReceivedSlice> FindSlicesToDownload( |
| 34 const std::vector<DownloadItem::ReceivedSlice>& received_slices) { | 78 const std::vector<DownloadItem::ReceivedSlice>& received_slices) { |
| 35 std::vector<DownloadItem::ReceivedSlice> result; | 79 std::vector<DownloadItem::ReceivedSlice> result; |
| 36 if (received_slices.empty()) { | 80 if (received_slices.empty()) { |
| 37 result.emplace_back(0, DownloadSaveInfo::kLengthFullContent); | 81 result.emplace_back(0, DownloadSaveInfo::kLengthFullContent); |
| 38 return result; | 82 return result; |
| 39 } | 83 } |
| 40 | 84 |
| 41 std::vector<DownloadItem::ReceivedSlice>::const_iterator iter = | 85 std::vector<DownloadItem::ReceivedSlice>::const_iterator iter = |
| 42 received_slices.begin(); | 86 received_slices.begin(); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 73 int GetParallelRequestCountConfig() { | 117 int GetParallelRequestCountConfig() { |
| 74 std::string finch_value = base::GetFieldTrialParamValueByFeature( | 118 std::string finch_value = base::GetFieldTrialParamValueByFeature( |
| 75 features::kParallelDownloading, kParallelRequestCountFinchKey); | 119 features::kParallelDownloading, kParallelRequestCountFinchKey); |
| 76 int result; | 120 int result; |
| 77 return !finch_value.empty() && base::StringToInt(finch_value, &result) | 121 return !finch_value.empty() && base::StringToInt(finch_value, &result) |
| 78 ? result | 122 ? result |
| 79 : kParallelRequestCount; | 123 : kParallelRequestCount; |
| 80 } | 124 } |
| 81 | 125 |
| 82 } // namespace content | 126 } // namespace content |
| OLD | NEW |