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 126
|
// Copyright 2017 The Chromium Authors
// 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 "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_create_info.h"
#include "components/download/public/common/download_export.h"
#include "components/download/public/common/download_job_factory.h"
#include "components/download/public/common/parallel_download_configs.h"
#include "components/download/public/common/url_loader_factory_provider.h"
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:
ParallelDownloadJob(
DownloadItem* download_item,
CancelRequestCallback cancel_request_callback,
const DownloadCreateInfo& create_info,
URLLoaderFactoryProvider::URLLoaderFactoryProviderPtr
url_loader_factory_provider,
DownloadJobFactory::WakeLockProviderBinder wake_lock_provider_binder);
ParallelDownloadJob(const ParallelDownloadJob&) = delete;
ParallelDownloadJob& operator=(const ParallelDownloadJob&) = delete;
~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,
std::unique_ptr<DownloadCreateInfo> download_create_info) 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| and will be half open.
virtual void CreateRequest(int64_t offset);
// 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_;
// URLLoaderFactoryProvider to retrieve the URLLoaderFactory and issue
// parallel requests.
URLLoaderFactoryProvider::URLLoaderFactoryProviderPtr
url_loader_factory_provider_;
// Callbac used for binding WakeLockProvider receivers as needed by each
// subrequest.
const DownloadJobFactory::WakeLockProviderBinder wake_lock_provider_binder_;
};
} // namespace download
#endif // COMPONENTS_DOWNLOAD_INTERNAL_COMMON_PARALLEL_DOWNLOAD_JOB_H_
|