| 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/download_job_factory.h" | 5 #include "content/browser/download/download_job_factory.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "content/browser/download/download_item_impl.h" | 10 #include "content/browser/download/download_item_impl.h" |
| 11 #include "content/browser/download/download_job.h" | 11 #include "content/browser/download/download_job.h" |
| 12 #include "content/browser/download/download_job_impl.h" | 12 #include "content/browser/download/download_job_impl.h" |
| 13 #include "content/browser/download/download_stats.h" |
| 13 #include "content/browser/download/parallel_download_job.h" | 14 #include "content/browser/download/parallel_download_job.h" |
| 14 #include "content/browser/download/parallel_download_utils.h" | 15 #include "content/browser/download/parallel_download_utils.h" |
| 15 #include "content/public/common/content_features.h" | 16 #include "content/public/common/content_features.h" |
| 16 | 17 |
| 17 namespace content { | 18 namespace content { |
| 18 | 19 |
| 20 namespace { |
| 21 |
| 22 // Returns if the download should be a parallel download. |
| 23 bool ShouldUseParallelDownload(const DownloadCreateInfo& create_info) { |
| 24 // To enable parallel download, following conditions need to be satisfied. |
| 25 // 1. Feature |kParallelDownloading| enabled. |
| 26 // 2. Strong validators response headers. i.e. ETag and Last-Modified. |
| 27 // 3. Accept-Ranges header. |
| 28 // 4. Content-Length header. |
| 29 // 5. Content-Length is no less than the minimum slice size configuration. |
| 30 // 6. HTTP/1.1 protocol, not QUIC nor HTTP/1.0. |
| 31 if (!base::FeatureList::IsEnabled(features::kParallelDownloading)) |
| 32 return false; |
| 33 |
| 34 // Etag and last modified are stored into DownloadCreateInfo in |
| 35 // DownloadRequestCore only if the response header complies to the strong |
| 36 // validator rule. |
| 37 bool has_strong_validator = |
| 38 !create_info.etag.empty() || !create_info.last_modified.empty(); |
| 39 bool has_content_length = create_info.total_bytes > 0; |
| 40 bool satisfy_min_file_size = |
| 41 create_info.total_bytes >= GetMinSliceSizeConfig(); |
| 42 bool satisfy_connection_type = create_info.connection_info == |
| 43 net::HttpResponseInfo::CONNECTION_INFO_HTTP1_1; |
| 44 |
| 45 using Reason = ParallelDownloadFailReason; |
| 46 if (!has_strong_validator) |
| 47 RecordParallelDownloadFailReason(Reason::STRONG_VALIDATORS); |
| 48 if (!create_info.accept_range) |
| 49 RecordParallelDownloadFailReason(Reason::ACCEPT_RANGE_HEADER); |
| 50 if (!has_content_length) |
| 51 RecordParallelDownloadFailReason(Reason::CONTENT_LENGTH_HEADER); |
| 52 if (!satisfy_min_file_size) |
| 53 RecordParallelDownloadFailReason(Reason::FILE_SIZE); |
| 54 if (!satisfy_connection_type) |
| 55 RecordParallelDownloadFailReason(Reason::CONNECTION_TYPE); |
| 56 |
| 57 bool should_use_parallel_download = |
| 58 has_strong_validator && create_info.accept_range && has_content_length && |
| 59 satisfy_min_file_size && satisfy_connection_type; |
| 60 if (!should_use_parallel_download) |
| 61 RecordParallelDownloadFailReason(Reason::FAIL_COUNT); |
| 62 |
| 63 return should_use_parallel_download; |
| 64 } |
| 65 |
| 66 } // namespace |
| 67 |
| 19 std::unique_ptr<DownloadJob> DownloadJobFactory::CreateJob( | 68 std::unique_ptr<DownloadJob> DownloadJobFactory::CreateJob( |
| 20 DownloadItemImpl* download_item, | 69 DownloadItemImpl* download_item, |
| 21 std::unique_ptr<DownloadRequestHandleInterface> req_handle, | 70 std::unique_ptr<DownloadRequestHandleInterface> req_handle, |
| 22 const DownloadCreateInfo& create_info) { | 71 const DownloadCreateInfo& create_info) { |
| 23 // Build parallel download job. | 72 // Build parallel download job. |
| 24 if (ShouldUseParallelDownload(create_info)) { | 73 if (ShouldUseParallelDownload(create_info)) { |
| 25 return base::MakeUnique<ParallelDownloadJob>(download_item, | 74 return base::MakeUnique<ParallelDownloadJob>(download_item, |
| 26 std::move(req_handle), | 75 std::move(req_handle), |
| 27 create_info); | 76 create_info); |
| 28 } | 77 } |
| 29 | 78 |
| 30 // An ordinary download job. | 79 // An ordinary download job. |
| 31 return base::MakeUnique<DownloadJobImpl>(download_item, | 80 return base::MakeUnique<DownloadJobImpl>(download_item, |
| 32 std::move(req_handle)); | 81 std::move(req_handle)); |
| 33 } | 82 } |
| 34 | 83 |
| 35 } // namespace | 84 } // namespace |
| OLD | NEW |