#include "components/download/public/common/in_progress_download_manager.h"
#include <optional>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "components/download/database/download_db_entry.h"
#include "components/download/database/download_db_impl.h"
#include "components/download/database/download_namespace.h"
#include "components/download/internal/common/download_db_cache.h"
#include "components/download/internal/common/resource_downloader.h"
#include "components/download/public/common/download_features.h"
#include "components/download/public/common/download_file.h"
#include "components/download/public/common/download_item_impl.h"
#include "components/download/public/common/download_start_observer.h"
#include "components/download/public/common/download_stats.h"
#include "components/download/public/common/download_task_runner.h"
#include "components/download/public/common/download_url_parameters.h"
#include "components/download/public/common/download_utils.h"
#include "components/download/public/common/input_stream.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "services/device/public/mojom/wake_lock_provider.mojom.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/build_info.h"
#include "components/download/internal/common/android/download_collection_bridge.h"
#include "components/download/public/common/download_path_reservation_tracker.h"
#endif
namespace download {
namespace {
#if BUILDFLAG(IS_ANDROID)
constexpr char kPdfMimeType[] = "application/pdf";
#endif
std::unique_ptr<DownloadItemImpl> CreateDownloadItemImpl(
DownloadItemImplDelegate* delegate,
const DownloadDBEntry entry,
std::unique_ptr<DownloadEntry> download_entry) { … }
void OnUrlDownloadHandlerCreated(
UrlDownloadHandler::UniqueUrlDownloadHandlerPtr downloader,
base::WeakPtr<InProgressDownloadManager> download_manager,
const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) { … }
void BeginResourceDownload(
std::unique_ptr<DownloadUrlParameters> params,
std::unique_ptr<network::ResourceRequest> request,
std::unique_ptr<network::PendingSharedURLLoaderFactory>
pending_url_loader_factory,
const URLSecurityPolicy& url_security_policy,
bool is_new_download,
base::WeakPtr<InProgressDownloadManager> download_manager,
const std::string& serialized_embedder_download_data,
const GURL& tab_url,
const GURL& tab_referrer_url,
mojo::PendingRemote<device::mojom::WakeLockProvider> wake_lock_provider,
bool is_background_mode,
const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner) { … }
void CreateDownloadHandlerForNavigation(
base::WeakPtr<InProgressDownloadManager> download_manager,
std::unique_ptr<network::ResourceRequest> resource_request,
int render_process_id,
int render_frame_id,
const std::string& serialized_embedder_download_data,
const GURL& tab_url,
const GURL& tab_referrer_url,
std::vector<GURL> url_chain,
net::CertStatus cert_status,
network::mojom::URLResponseHeadPtr response_head,
mojo::ScopedDataPipeConsumerHandle response_body,
network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints,
std::unique_ptr<network::PendingSharedURLLoaderFactory>
pending_url_loader_factory,
const URLSecurityPolicy& url_security_policy,
mojo::PendingRemote<device::mojom::WakeLockProvider> wake_lock_provider,
const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner,
bool is_transient) { … }
#if BUILDFLAG(IS_ANDROID)
void OnDownloadDisplayNamesReturned(
DownloadCollectionBridge::GetDisplayNamesCallback callback,
const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner,
InProgressDownloadManager::DisplayNames download_names) {
main_task_runner->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), std::move(download_names)));
}
void OnPathReserved(
DownloadTargetCallback callback,
DownloadDangerType danger_type,
DownloadItem::InsecureDownloadStatus insecure_download_status,
const InProgressDownloadManager::IntermediatePathCallback&
intermediate_path_cb,
const base::FilePath& forced_file_path,
PathValidationResult result,
const base::FilePath& target_path) {
base::FilePath intermediate_path;
if (!target_path.empty() &&
(download::IsPathValidationSuccessful(result) ||
result == download::PathValidationResult::SAME_AS_SOURCE)) {
if (!forced_file_path.empty()) {
DCHECK_EQ(target_path, forced_file_path);
intermediate_path = target_path;
} else if (intermediate_path_cb) {
intermediate_path = intermediate_path_cb.Run(target_path);
}
}
DownloadTargetInfo target_info;
target_info.target_path = target_path;
target_info.intermediate_path = intermediate_path;
target_info.danger_type = danger_type;
target_info.interrupt_reason = intermediate_path.empty()
? DOWNLOAD_INTERRUPT_REASON_FILE_FAILED
: DOWNLOAD_INTERRUPT_REASON_NONE;
target_info.insecure_download_status = insecure_download_status;
std::move(callback).Run(std::move(target_info));
}
#endif
}
bool InProgressDownloadManager::Delegate::InterceptDownload(
const DownloadCreateInfo& download_create_info) { … }
base::FilePath
InProgressDownloadManager::Delegate::GetDefaultDownloadDirectory() { … }
InProgressDownloadManager::InProgressDownloadManager(
Delegate* delegate,
const base::FilePath& in_progress_db_dir,
leveldb_proto::ProtoDatabaseProvider* db_provider,
const IsOriginSecureCallback& is_origin_secure_cb,
const URLSecurityPolicy& url_security_policy,
WakeLockProviderBinder wake_lock_provider_binder)
: … { … }
InProgressDownloadManager::~InProgressDownloadManager() = default;
void InProgressDownloadManager::OnUrlDownloadStarted(
std::unique_ptr<DownloadCreateInfo> download_create_info,
std::unique_ptr<InputStream> input_stream,
URLLoaderFactoryProvider::URLLoaderFactoryProviderPtr
url_loader_factory_provider,
UrlDownloadHandlerID downloader,
DownloadUrlParameters::OnStartedCallback callback) { … }
void InProgressDownloadManager::OnUrlDownloadStopped(
UrlDownloadHandlerID downloader) { … }
void InProgressDownloadManager::OnUrlDownloadHandlerCreated(
UrlDownloadHandler::UniqueUrlDownloadHandlerPtr downloader) { … }
void InProgressDownloadManager::DownloadUrl(
std::unique_ptr<DownloadUrlParameters> params) { … }
bool InProgressDownloadManager::CanDownload(DownloadUrlParameters* params) { … }
void InProgressDownloadManager::GetAllDownloads(
SimpleDownloadManager::DownloadVector* downloads) { … }
DownloadItem* InProgressDownloadManager::GetDownloadByGuid(
const std::string& guid) { … }
void InProgressDownloadManager::BeginDownload(
std::unique_ptr<DownloadUrlParameters> params,
std::unique_ptr<network::PendingSharedURLLoaderFactory>
pending_url_loader_factory,
bool is_new_download,
const std::string& serialized_embedder_download_data,
const GURL& tab_url,
const GURL& tab_referrer_url) { … }
void InProgressDownloadManager::InterceptDownloadFromNavigation(
std::unique_ptr<network::ResourceRequest> resource_request,
int render_process_id,
int render_frame_id,
const std::string& serialized_embedder_download_data,
const GURL& tab_url,
const GURL& tab_referrer_url,
std::vector<GURL> url_chain,
net::CertStatus cert_status,
network::mojom::URLResponseHeadPtr response_head,
mojo::ScopedDataPipeConsumerHandle response_body,
network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints,
std::unique_ptr<network::PendingSharedURLLoaderFactory>
pending_url_loader_factory,
bool is_transient) { … }
void InProgressDownloadManager::Initialize(
const base::FilePath& in_progress_db_dir,
leveldb_proto::ProtoDatabaseProvider* db_provider) { … }
void InProgressDownloadManager::ShutDown() { … }
void InProgressDownloadManager::DetermineDownloadTarget(
DownloadItemImpl* download,
DownloadTargetCallback callback) { … }
void InProgressDownloadManager::ResumeInterruptedDownload(
std::unique_ptr<DownloadUrlParameters> params,
const std::string& serialized_embedder_download_data) { … }
bool InProgressDownloadManager::ShouldOpenDownload(
DownloadItemImpl* item,
ShouldOpenDownloadCallback callback) { … }
void InProgressDownloadManager::ReportBytesWasted(DownloadItemImpl* download) { … }
void InProgressDownloadManager::RemoveInProgressDownload(
const std::string& guid) { … }
void InProgressDownloadManager::StartDownload(
std::unique_ptr<DownloadCreateInfo> info,
std::unique_ptr<InputStream> stream,
URLLoaderFactoryProvider::URLLoaderFactoryProviderPtr
url_loader_factory_provider,
DownloadJob::CancelRequestCallback cancel_request_callback,
DownloadUrlParameters::OnStartedCallback on_started) { … }
void InProgressDownloadManager::StartDownloadWithItem(
std::unique_ptr<InputStream> stream,
URLLoaderFactoryProvider::URLLoaderFactoryProviderPtr
url_loader_factory_provider,
DownloadJob::CancelRequestCallback cancel_request_callback,
std::unique_ptr<DownloadCreateInfo> info,
DownloadItemImpl* download,
const base::FilePath& duplicate_download_file_path,
bool should_persist_new_download) { … }
void InProgressDownloadManager::OnDBInitialized(
bool success,
std::unique_ptr<std::vector<DownloadDBEntry>> entries) { … }
void InProgressDownloadManager::OnDownloadNamesRetrieved(
std::unique_ptr<std::vector<DownloadDBEntry>> entries,
DisplayNames display_names) { … }
std::vector<std::unique_ptr<download::DownloadItemImpl>>
InProgressDownloadManager::TakeInProgressDownloads() { … }
base::FilePath InProgressDownloadManager::GetDownloadDisplayName(
const base::FilePath& path) { … }
void InProgressDownloadManager::OnAllInprogressDownloadsLoaded() { … }
void InProgressDownloadManager::SetDelegate(Delegate* delegate) { … }
void InProgressDownloadManager::OnDownloadsInitialized() { … }
void InProgressDownloadManager::NotifyDownloadsInitialized() { … }
void InProgressDownloadManager::AddInProgressDownloadForTest(
std::unique_ptr<download::DownloadItemImpl> download) { … }
void InProgressDownloadManager::CancelUrlDownload(
UrlDownloadHandlerID downloader,
bool user_cancel) { … }
}