1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_DOWNLOAD_INTERNAL_COMMON_PARALLEL_DOWNLOAD_UTILS_H_
6 #define COMPONENTS_DOWNLOAD_INTERNAL_COMMON_PARALLEL_DOWNLOAD_UTILS_H_
7 
8 #include <vector>
9 
10 #include "components/download/public/common/download_export.h"
11 #include "components/download/public/common/download_file_impl.h"
12 #include "components/download/public/common/download_item.h"
13 
14 namespace download {
15 
16 // Given an array of slices that are received, returns an array of slices to
17 // download. |received_slices| must be ordered by offsets.
18 COMPONENTS_DOWNLOAD_EXPORT std::vector<DownloadItem::ReceivedSlice>
19 FindSlicesToDownload(
20     const std::vector<DownloadItem::ReceivedSlice>& received_slices);
21 
22 // Adds or merges a new received slice into a vector of sorted slices. If the
23 // slice can be merged with the slice preceding it, merge the 2 slices.
24 // Otherwise, insert the slice and keep the vector sorted. Returns the index
25 // of the newly updated slice.
26 COMPONENTS_DOWNLOAD_EXPORT size_t AddOrMergeReceivedSliceIntoSortedArray(
27     const DownloadItem::ReceivedSlice& new_slice,
28     std::vector<DownloadItem::ReceivedSlice>& received_slices);
29 
30 // Returns if a preceding stream can still download the part of content that
31 // was arranged to |error_stream|.
32 COMPONENTS_DOWNLOAD_EXPORT bool CanRecoverFromError(
33     const DownloadFileImpl::SourceStream* error_stream,
34     const DownloadFileImpl::SourceStream* preceding_neighbor);
35 
36 // Chunks the content that starts from |current_offset|, into at most
37 // std::max(|request_count|, 1) smaller slices.
38 // Each slice contains at least |min_slice_size| bytes unless |total_length|
39 // is less than |min_slice_size|.
40 // The last slice is half opened.
41 COMPONENTS_DOWNLOAD_EXPORT std::vector<download::DownloadItem::ReceivedSlice>
42 FindSlicesForRemainingContent(int64_t current_offset,
43                               int64_t total_length,
44                               int request_count,
45                               int64_t min_slice_size);
46 
47 // Finch configuration utilities.
48 //
49 // Get the minimum slice size to use parallel download from finch configuration.
50 // A slice won't be further chunked into smaller slices if the size is less
51 // than the minimum size.
52 COMPONENTS_DOWNLOAD_EXPORT int64_t GetMinSliceSizeConfig();
53 
54 // Get the request count for parallel download from finch configuration.
55 COMPONENTS_DOWNLOAD_EXPORT int GetParallelRequestCountConfig();
56 
57 // Get the time delay to send parallel requests after the response of original
58 // request is handled.
59 COMPONENTS_DOWNLOAD_EXPORT base::TimeDelta GetParallelRequestDelayConfig();
60 
61 // Get the required remaining time before creating parallel requests.
62 COMPONENTS_DOWNLOAD_EXPORT base::TimeDelta
63 GetParallelRequestRemainingTimeConfig();
64 
65 // Given an ordered array of slices, get the maximum size of a contiguous data
66 // block that starts from offset 0. If the first slice doesn't start from offset
67 // 0, return 0.
68 COMPONENTS_DOWNLOAD_EXPORT int64_t GetMaxContiguousDataBlockSizeFromBeginning(
69     const download::DownloadItem::ReceivedSlices& slices);
70 
71 // Returns whether parallel download is enabled.
72 COMPONENTS_DOWNLOAD_EXPORT bool IsParallelDownloadEnabled();
73 
74 // Print the states of received slices for debugging.
75 void DebugSlicesInfo(const DownloadItem::ReceivedSlices& slices);
76 
77 }  //  namespace download
78 
79 #endif  // COMPONENTS_DOWNLOAD_INTERNAL_COMMON_PARALLEL_DOWNLOAD_UTILS_H_
80