| 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 "content/public/browser/download_save_info.h" | 7 #include "content/public/browser/download_save_info.h" |
| 8 | 8 |
| 9 namespace content { | 9 namespace content { |
| 10 | 10 |
| 11 DownloadItem::ReceivedSlice FindNextSliceToDownload( | 11 std::vector<DownloadItem::ReceivedSlice> FindSlicesToDownload( |
| 12 const std::vector<DownloadItem::ReceivedSlice>& received_slices) { | 12 const std::vector<DownloadItem::ReceivedSlice>& received_slices) { |
| 13 if (received_slices.empty()) | 13 std::vector<DownloadItem::ReceivedSlice> result; |
| 14 return DownloadItem::ReceivedSlice(0, DownloadSaveInfo::kLengthFullContent); | 14 if (received_slices.empty()) { |
| 15 result.emplace_back(0, DownloadSaveInfo::kLengthFullContent); |
| 16 return result; |
| 17 } |
| 15 | 18 |
| 16 std::vector<DownloadItem::ReceivedSlice>::const_iterator iter = | 19 std::vector<DownloadItem::ReceivedSlice>::const_iterator iter = |
| 17 received_slices.begin(); | 20 received_slices.begin(); |
| 18 DCHECK_GE(iter->offset, 0); | 21 DCHECK_GE(iter->offset, 0); |
| 19 if (iter->offset != 0) | 22 if (iter->offset != 0) |
| 20 return DownloadItem::ReceivedSlice(0, iter->offset); | 23 result.emplace_back(0, iter->offset); |
| 21 | 24 |
| 22 int64_t offset = 0; | 25 while (true) { |
| 23 int64_t remaining_bytes = DownloadSaveInfo::kLengthFullContent; | 26 int64_t offset = iter->offset + iter->received_bytes; |
| 24 while (iter != received_slices.end()) { | |
| 25 offset = iter->offset + iter->received_bytes; | |
| 26 std::vector<DownloadItem::ReceivedSlice>::const_iterator next = | 27 std::vector<DownloadItem::ReceivedSlice>::const_iterator next = |
| 27 std::next(iter); | 28 std::next(iter); |
| 28 if (next == received_slices.end()) | 29 if (next == received_slices.end()) { |
| 30 result.emplace_back(offset, DownloadSaveInfo::kLengthFullContent); |
| 29 break; | 31 break; |
| 32 } |
| 30 | 33 |
| 31 DCHECK_GE(next->offset, offset); | 34 DCHECK_GE(next->offset, offset); |
| 32 if (next->offset > offset) { | 35 if (next->offset > offset) |
| 33 remaining_bytes = next->offset - offset; | 36 result.emplace_back(offset, next->offset - offset); |
| 34 break; | |
| 35 } | |
| 36 iter = next; | 37 iter = next; |
| 37 } | 38 } |
| 38 return DownloadItem::ReceivedSlice(offset, remaining_bytes); | 39 return result; |
| 39 } | 40 } |
| 40 | 41 |
| 41 } // namespace content | 42 } // namespace content |
| OLD | NEW |