1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_DOWNLOAD_INTERNAL_COMMON_PARALLEL_DOWNLOAD_JOB_H_
#define COMPONENTS_DOWNLOAD_INTERNAL_COMMON_PARALLEL_DOWNLOAD_JOB_H_
#include <memory>
#include <unordered_map>
#include <vector>
#include "base/macros.h"
#include "base/timer/timer.h"
#include "components/download/internal/common/download_job_impl.h"
#include "components/download/internal/common/download_worker.h"
#include "components/download/public/common/download_export.h"
#include "components/download/public/common/parallel_download_configs.h"
namespace net {
class URLRequestContextGetter;
}
namespace download {
// DownloadJob that can create concurrent range requests to fetch different
// parts of the file.
// The original request is hold in base class.
class COMPONENTS_DOWNLOAD_EXPORT ParallelDownloadJob
: public DownloadJobImpl,
public DownloadWorker::Delegate {
public:
// TODO(qinmin): Remove |url_request_context_getter| once network service is
// enabled.
ParallelDownloadJob(
DownloadItem* download_item,
std::unique_ptr<DownloadRequestHandleInterface> request_handle,
const DownloadCreateInfo& create_info,
scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory,
net::URLRequestContextGetter* url_request_context_getter);
~ParallelDownloadJob() override;
// DownloadJobImpl implementation.
void Cancel(bool user_cancel) override;
void Pause() override;
void Resume(bool resume_request) override;
void CancelRequestWithOffset(int64_t offset) override;
protected:
// DownloadJobImpl implementation.
void OnDownloadFileInitialized(DownloadFile::InitializeCallback callback,
DownloadInterruptReason result,
int64_t bytes_wasted) override;
// Virtual for testing.
virtual int GetParallelRequestCount() const;
virtual int64_t GetMinSliceSize() const;
virtual int GetMinRemainingTimeInSeconds() const;
using WorkerMap =
std::unordered_map<int64_t, std::unique_ptr<DownloadWorker>>;
// Map from the offset position of the slice to the worker that downloads the
// slice.
WorkerMap workers_;
private:
friend class ParallelDownloadJobTest;
// DownloadWorker::Delegate implementation.
void OnInputStreamReady(DownloadWorker* worker,
std::unique_ptr<InputStream> input_stream) override;
// Build parallel requests after a delay, to effectively measure the single
// stream bandwidth.
void BuildParallelRequestAfterDelay();
// Build parallel requests to download. This function is the entry point for
// all parallel downloads.
void BuildParallelRequests();
// Build one http request for each slice from the second slice.
// The first slice represents the original request.
void ForkSubRequests(const DownloadItem::ReceivedSlices& slices_to_download);
// Create one range request, virtual for testing. Range request will start
// from |offset| to |length|. Range request will be half open, e.g.
// "Range:50-" if |length| is 0.
virtual void CreateRequest(int64_t offset, int64_t length);
// Information about the initial request when download is started.
int64_t initial_request_offset_;
// A snapshot of received slices when creating the parallel download job.
// Download item's received slices may be different from this snapshot when
// |BuildParallelRequests| is called.
DownloadItem::ReceivedSlices initial_received_slices_;
// The length of the response body of the original request.
// Used to estimate the remaining size of the content when the initial
// request is half open, i.e, |initial_request_length_| is
// DownloadSaveInfo::kLengthFullContent.
int64_t content_length_;
// Used to send parallel requests after a delay based on Finch config.
base::OneShotTimer timer_;
// If we have sent parallel requests.
bool requests_sent_;
// If the download progress is canceled.
bool is_canceled_;
// SharedURLLoaderFactory to issue network requests with network service
scoped_refptr<network::SharedURLLoaderFactory> shared_url_loader_factory_;
// URLRequestContextGetter for issueing network requests when network service
// is disabled.
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
DISALLOW_COPY_AND_ASSIGN(ParallelDownloadJob);
};
} // namespace download
#endif // COMPONENTS_DOWNLOAD_INTERNAL_COMMON_PARALLEL_DOWNLOAD_JOB_H_
|