| 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 #ifndef CONTENT_BROWSER_DOWNLOAD_PARALLEL_DOWNLOAD_JOB_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_PARALLEL_DOWNLOAD_JOB_H_ |
| 6 #define CONTENT_BROWSER_DOWNLOAD_PARALLEL_DOWNLOAD_JOB_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_PARALLEL_DOWNLOAD_JOB_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <unordered_map> |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
| 13 #include "content/browser/download/download_job_impl.h" | 14 #include "content/browser/download/download_job_impl.h" |
| 14 #include "content/browser/download/download_worker.h" | 15 #include "content/browser/download/download_worker.h" |
| 15 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
| 16 | 17 |
| 17 namespace content { | 18 namespace content { |
| 18 | 19 |
| 19 // DownloadJob that can create concurrent range requests to fetch different | 20 // DownloadJob that can create concurrent range requests to fetch different |
| 20 // parts of the file. | 21 // parts of the file. |
| 21 // The original request is hold in base class DownloadUrlJob. | 22 // The original request is hold in base class. |
| 22 class CONTENT_EXPORT ParallelDownloadJob : public DownloadJobImpl { | 23 class CONTENT_EXPORT ParallelDownloadJob : public DownloadJobImpl, |
| 24 public DownloadWorker::Delegate { |
| 23 public: | 25 public: |
| 24 ParallelDownloadJob( | 26 ParallelDownloadJob( |
| 25 DownloadItemImpl* download_item, | 27 DownloadItemImpl* download_item, |
| 26 std::unique_ptr<DownloadRequestHandleInterface> request_handle, | 28 std::unique_ptr<DownloadRequestHandleInterface> request_handle, |
| 27 const DownloadCreateInfo& create_info); | 29 const DownloadCreateInfo& create_info); |
| 28 ~ParallelDownloadJob() override; | 30 ~ParallelDownloadJob() override; |
| 29 | 31 |
| 30 // DownloadUrlJob implementation. | 32 // DownloadJobImpl implementation. |
| 31 void Start() override; | 33 void Start() override; |
| 32 void Cancel(bool user_cancel) override; | 34 void Cancel(bool user_cancel) override; |
| 33 void Pause() override; | 35 void Pause() override; |
| 34 void Resume(bool resume_request) override; | 36 void Resume(bool resume_request) override; |
| 35 | 37 |
| 38 protected: |
| 39 // Virtual for testing. |
| 40 virtual int GetParallelRequestCount() const; |
| 41 |
| 36 private: | 42 private: |
| 37 friend class ParallelDownloadJobTest; | 43 friend class ParallelDownloadJobTest; |
| 38 | 44 |
| 39 typedef std::vector<std::unique_ptr<DownloadWorker>> WorkerList; | 45 using WorkerMap = |
| 46 std::unordered_map<int64_t, std::unique_ptr<DownloadWorker>>; |
| 40 | 47 |
| 41 // Build multiple http requests for a new download, | 48 // DownloadWorker::Delegate implementation. |
| 42 // the rest of the bytes starting from |bytes_received| will be equally | 49 void OnByteStreamReady( |
| 43 // distributed to each connection, including the original connection. | 50 DownloadWorker* worker, |
| 44 // the last connection may take additional bytes. | 51 std::unique_ptr<ByteStreamReader> stream_reader) override; |
| 45 void ForkRequestsForNewDownload(int64_t bytes_received, | |
| 46 int64_t total_bytes, | |
| 47 int request_count); | |
| 48 | 52 |
| 49 // Build parallel requests after a delay, to effectively measure the single | 53 // Build parallel requests after a delay, to effectively measure the single |
| 50 // stream bandwidth. | 54 // stream bandwidth. |
| 51 void BuildParallelRequestAfterDelay(); | 55 void BuildParallelRequestAfterDelay(); |
| 52 | 56 |
| 53 // Build parallel requests to download the remaining slices. | 57 // Build parallel requests to download. This function is the entry point for |
| 54 // TODO(qinmin): remove ForkRequestsForNewDownload() and move the logic into | 58 // all parallel downloads. |
| 55 // this function. | |
| 56 void BuildParallelRequests(); | 59 void BuildParallelRequests(); |
| 57 | 60 |
| 61 // Build one http request for each slice from the second slice. |
| 62 // The first slice represents the original request. |
| 63 void ForkSubRequests(const DownloadItem::ReceivedSlices& slices_to_download); |
| 64 |
| 58 // Create one range request, virtual for testing. | 65 // Create one range request, virtual for testing. |
| 59 virtual void CreateRequest(int64_t offset, int64_t length); | 66 virtual void CreateRequest(int64_t offset, int64_t length); |
| 60 | 67 |
| 61 // Information about the initial request when download is started. | 68 // Information about the initial request when download is started. |
| 62 int64_t initial_request_offset_; | 69 int64_t initial_request_offset_; |
| 63 int64_t initial_request_length_; | |
| 64 | 70 |
| 65 // Subsequent tasks to send range requests. | 71 // The length of the response body of the original request. May be less than |
| 66 WorkerList workers_; | 72 // the size of the target file if the request starts from non-zero offset. |
| 73 int64_t content_length_; |
| 74 |
| 75 // Map from the offset position of the slice to the worker that downloads the |
| 76 // slice. |
| 77 WorkerMap workers_; |
| 67 | 78 |
| 68 // Used to send parallel requests after a delay based on Finch config. | 79 // Used to send parallel requests after a delay based on Finch config. |
| 69 base::OneShotTimer timer_; | 80 base::OneShotTimer timer_; |
| 70 | 81 |
| 71 // If we have sent parallel requests. | 82 // If we have sent parallel requests. |
| 72 bool requests_sent_; | 83 bool requests_sent_; |
| 73 | 84 |
| 74 DISALLOW_COPY_AND_ASSIGN(ParallelDownloadJob); | 85 DISALLOW_COPY_AND_ASSIGN(ParallelDownloadJob); |
| 75 }; | 86 }; |
| 76 | 87 |
| 77 } // namespace content | 88 } // namespace content |
| 78 | 89 |
| 79 #endif // CONTENT_BROWSER_DOWNLOAD_PARALLEL_DOWNLOAD_JOB_H_ | 90 #endif // CONTENT_BROWSER_DOWNLOAD_PARALLEL_DOWNLOAD_JOB_H_ |
| OLD | NEW |