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 10 matching lines...) Expand all Loading... | |
| 21 const int64_t kMinSliceSizeParallelDownload = 2097152; | 21 const int64_t kMinSliceSizeParallelDownload = 2097152; |
| 22 | 22 |
| 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 bool compareReceivedSlices(const DownloadItem::ReceivedSlice& lhs, | |
|
David Trainor- moved to gerrit
2017/03/09 17:44:02
Comparator operators on the DownloadItem::Received
qinmin
2017/03/09 21:44:58
Added a TODO here.
| |
| 32 const DownloadItem::ReceivedSlice& rhs) { | |
| 33 return lhs.offset < rhs.offset; | |
| 34 } | |
| 35 | |
| 31 } // namespace | 36 } // namespace |
| 32 | 37 |
| 33 std::vector<DownloadItem::ReceivedSlice> FindSlicesToDownload( | 38 std::vector<DownloadItem::ReceivedSlice> FindSlicesToDownload( |
| 34 const std::vector<DownloadItem::ReceivedSlice>& received_slices) { | 39 const std::vector<DownloadItem::ReceivedSlice>& received_slices) { |
| 35 std::vector<DownloadItem::ReceivedSlice> result; | 40 std::vector<DownloadItem::ReceivedSlice> result; |
| 36 if (received_slices.empty()) { | 41 if (received_slices.empty()) { |
| 37 result.emplace_back(0, DownloadSaveInfo::kLengthFullContent); | 42 result.emplace_back(0, DownloadSaveInfo::kLengthFullContent); |
| 38 return result; | 43 return result; |
| 39 } | 44 } |
| 40 | 45 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 54 } | 59 } |
| 55 | 60 |
| 56 DCHECK_GE(next->offset, offset); | 61 DCHECK_GE(next->offset, offset); |
| 57 if (next->offset > offset) | 62 if (next->offset > offset) |
| 58 result.emplace_back(offset, next->offset - offset); | 63 result.emplace_back(offset, next->offset - offset); |
| 59 iter = next; | 64 iter = next; |
| 60 } | 65 } |
| 61 return result; | 66 return result; |
| 62 } | 67 } |
| 63 | 68 |
| 69 size_t AddOrMergeReceivedSliceIntoSortedArray( | |
| 70 const DownloadItem::ReceivedSlice& new_slice, | |
| 71 std::vector<DownloadItem::ReceivedSlice>& received_slices) { | |
| 72 std::vector<DownloadItem::ReceivedSlice>::iterator it = | |
| 73 std::upper_bound(received_slices.begin(), received_slices.end(), | |
| 74 new_slice, compareReceivedSlices); | |
| 75 if (it != received_slices.begin()) { | |
| 76 std::vector<DownloadItem::ReceivedSlice>::iterator prev = std::prev(it); | |
| 77 if (prev->offset + prev->received_bytes == new_slice.offset) { | |
| 78 prev->received_bytes += new_slice.received_bytes; | |
| 79 return static_cast<size_t>(std::distance(received_slices.begin(), prev)); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 it = received_slices.emplace(it, new_slice); | |
| 84 return static_cast<size_t>(std::distance(received_slices.begin(), it)); | |
| 85 } | |
| 86 | |
| 64 int64_t GetMinSliceSizeConfig() { | 87 int64_t GetMinSliceSizeConfig() { |
| 65 std::string finch_value = base::GetFieldTrialParamValueByFeature( | 88 std::string finch_value = base::GetFieldTrialParamValueByFeature( |
| 66 features::kParallelDownloading, kMinSliceSizeFinchKey); | 89 features::kParallelDownloading, kMinSliceSizeFinchKey); |
| 67 int64_t result; | 90 int64_t result; |
| 68 return !finch_value.empty() && base::StringToInt64(finch_value, &result) | 91 return !finch_value.empty() && base::StringToInt64(finch_value, &result) |
| 69 ? result | 92 ? result |
| 70 : kMinSliceSizeParallelDownload; | 93 : kMinSliceSizeParallelDownload; |
| 71 } | 94 } |
| 72 | 95 |
| 73 int GetParallelRequestCountConfig() { | 96 int GetParallelRequestCountConfig() { |
| 74 std::string finch_value = base::GetFieldTrialParamValueByFeature( | 97 std::string finch_value = base::GetFieldTrialParamValueByFeature( |
| 75 features::kParallelDownloading, kParallelRequestCountFinchKey); | 98 features::kParallelDownloading, kParallelRequestCountFinchKey); |
| 76 int result; | 99 int result; |
| 77 return !finch_value.empty() && base::StringToInt(finch_value, &result) | 100 return !finch_value.empty() && base::StringToInt(finch_value, &result) |
| 78 ? result | 101 ? result |
| 79 : kParallelRequestCount; | 102 : kParallelRequestCount; |
| 80 } | 103 } |
| 81 | 104 |
| 82 } // namespace content | 105 } // namespace content |
| OLD | NEW |