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. | |
| 38 // 4. (Undetermined) Not under http proxy, e.g. data saver. | |
| 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> FindSlicesForNewDownload( | |
| 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; | |
|
qinmin
2017/03/11 06:08:22
shouldn't this just use request_count? why using a
xingliu
2017/03/13 17:53:17
This logic considers an edge case, like 3 bytes le
| |
| 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, 0); | |
|
qinmin
2017/03/11 06:08:22
s/0/DownloadSaveInfo::kLengthFullContent/
xingliu
2017/03/13 17:53:17
Done.
| |
| 73 return slices_to_download; | |
| 74 } | |
| 75 | |
| 33 std::vector<DownloadItem::ReceivedSlice> FindSlicesToDownload( | 76 std::vector<DownloadItem::ReceivedSlice> FindSlicesToDownload( |
| 34 const std::vector<DownloadItem::ReceivedSlice>& received_slices) { | 77 const std::vector<DownloadItem::ReceivedSlice>& received_slices) { |
| 35 std::vector<DownloadItem::ReceivedSlice> result; | 78 std::vector<DownloadItem::ReceivedSlice> result; |
| 36 if (received_slices.empty()) { | 79 if (received_slices.empty()) { |
| 37 result.emplace_back(0, DownloadSaveInfo::kLengthFullContent); | 80 result.emplace_back(0, DownloadSaveInfo::kLengthFullContent); |
| 38 return result; | 81 return result; |
| 39 } | 82 } |
| 40 | 83 |
| 41 std::vector<DownloadItem::ReceivedSlice>::const_iterator iter = | 84 std::vector<DownloadItem::ReceivedSlice>::const_iterator iter = |
| 42 received_slices.begin(); | 85 received_slices.begin(); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 73 int GetParallelRequestCountConfig() { | 116 int GetParallelRequestCountConfig() { |
| 74 std::string finch_value = base::GetFieldTrialParamValueByFeature( | 117 std::string finch_value = base::GetFieldTrialParamValueByFeature( |
| 75 features::kParallelDownloading, kParallelRequestCountFinchKey); | 118 features::kParallelDownloading, kParallelRequestCountFinchKey); |
| 76 int result; | 119 int result; |
| 77 return !finch_value.empty() && base::StringToInt(finch_value, &result) | 120 return !finch_value.empty() && base::StringToInt(finch_value, &result) |
| 78 ? result | 121 ? result |
| 79 : kParallelRequestCount; | 122 : kParallelRequestCount; |
| 80 } | 123 } |
| 81 | 124 |
| 82 } // namespace content | 125 } // namespace content |
| OLD | NEW |