#include "content/browser/download/download_manager_impl.h"
#include <iterator>
#include <utility>
#include "base/containers/contains.h"
#include "base/debug/alias.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/i18n/case_conversion.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_macros.h"
#include "base/observer_list.h"
#include "base/strings/sys_string_conversions.h"
#include "base/supports_user_data.h"
#include "base/synchronization/lock.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "build/build_config.h"
#include "components/download/database/in_progress/download_entry.h"
#include "components/download/public/common/download_create_info.h"
#include "components/download/public/common/download_danger_type.h"
#include "components/download/public/common/download_features.h"
#include "components/download/public/common/download_file.h"
#include "components/download/public/common/download_interrupt_reasons.h"
#include "components/download/public/common/download_item_factory.h"
#include "components/download/public/common/download_item_impl.h"
#include "components/download/public/common/download_item_rename_handler.h"
#include "components/download/public/common/download_stats.h"
#include "components/download/public/common/download_target_info.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/download/public/common/url_download_handler_factory.h"
#include "content/browser/bad_message.h"
#include "content/browser/blob_storage/chrome_blob_storage_context.h"
#include "content/browser/child_process_security_policy_impl.h"
#include "content/browser/data_url_loader_factory.h"
#include "content/browser/devtools/devtools_instrumentation.h"
#include "content/browser/download/embedder_download_data.pb.h"
#include "content/browser/file_system/file_system_url_loader_factory.h"
#include "content/browser/loader/file_url_loader_factory.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/site_info.h"
#include "content/browser/storage_partition_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/device_service.h"
#include "content/public/browser/disallow_activation_reason.h"
#include "content/public/browser/download_item_utils.h"
#include "content/public/browser/download_manager_delegate.h"
#include "content/public/browser/download_request_utils.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/resource_context.h"
#include "content/public/browser/shared_cors_origin_access_list.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_ui_url_loader_factory.h"
#include "content/public/common/content_client.h"
#include "content/public/common/url_constants.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "net/base/elements_upload_data_stream.h"
#include "net/base/isolation_info.h"
#include "net/base/load_flags.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/base/request_priority.h"
#include "net/base/upload_bytes_element_reader.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/is_potentially_trustworthy.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/url_loader_factory_builder.h"
#include "services/network/public/cpp/url_util.h"
#include "services/network/public/cpp/wrapper_shared_url_loader_factory.h"
#include "third_party/blink/public/common/loader/referrer_utils.h"
#include "third_party/blink/public/common/loader/throttling_url_loader.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "url/origin.h"
#if BUILDFLAG(IS_ANDROID)
#include "net/http/http_content_disposition.h"
#endif
namespace content {
namespace {
#if BUILDFLAG(IS_ANDROID)
constexpr char kPdfMimeType[] = "application/pdf";
#endif
void DeleteDownloadedFileOnUIThread(const base::FilePath& file_path) { … }
StoragePartitionImpl* GetStoragePartitionForConfig(
BrowserContext* context,
const StoragePartitionConfig& storage_partition_config) { … }
void OnDownloadStarted(
download::DownloadItemImpl* download,
download::DownloadUrlParameters::OnStartedCallback on_started) { … }
void CreateInterruptedDownload(
std::unique_ptr<download::DownloadUrlParameters> params,
download::DownloadInterruptReason reason,
base::WeakPtr<DownloadManagerImpl> download_manager) { … }
class DownloadItemFactoryImpl : public download::DownloadItemFactory { … };
std::unique_ptr<network::PendingSharedURLLoaderFactory>
CreatePendingSharedURLLoaderFactory(StoragePartitionImpl* storage_partition,
RenderFrameHost* rfh) { … }
void RecordDownloadOpenerType(RenderFrameHost* current,
RenderFrameHost* opener) { … }
}
DownloadManagerImpl::DownloadManagerImpl(BrowserContext* browser_context)
: … { … }
DownloadManagerImpl::~DownloadManagerImpl() { … }
download::DownloadItemImpl* DownloadManagerImpl::CreateActiveItem(
uint32_t id,
const download::DownloadCreateInfo& info) { … }
void DownloadManagerImpl::GetNextId(GetNextIdCallback callback) { … }
void DownloadManagerImpl::SetNextId(uint32_t next_id) { … }
std::string
DownloadManagerImpl::StoragePartitionConfigToSerializedEmbedderDownloadData(
const StoragePartitionConfig& storage_partition_config) { … }
StoragePartitionConfig
DownloadManagerImpl::SerializedEmbedderDownloadDataToStoragePartitionConfig(
const std::string& serialized_embedder_download_data) { … }
void DownloadManagerImpl::OnHistoryNextIdRetrieved(uint32_t next_id) { … }
StoragePartitionConfig DownloadManagerImpl::GetStoragePartitionConfigForSiteUrl(
const GURL& site_url) { … }
void DownloadManagerImpl::DetermineDownloadTarget(
download::DownloadItemImpl* item,
download::DownloadTargetCallback callback) { … }
bool DownloadManagerImpl::ShouldCompleteDownload(
download::DownloadItemImpl* item,
base::OnceClosure complete_callback) { … }
bool DownloadManagerImpl::ShouldAutomaticallyOpenFile(
const GURL& url,
const base::FilePath& path) { … }
bool DownloadManagerImpl::ShouldAutomaticallyOpenFileByPolicy(
const GURL& url,
const base::FilePath& path) { … }
bool DownloadManagerImpl::ShouldOpenDownload(
download::DownloadItemImpl* item,
ShouldOpenDownloadCallback callback) { … }
void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) { … }
DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() { … }
void DownloadManagerImpl::Shutdown() { … }
bool DownloadManagerImpl::InterceptDownload(
const download::DownloadCreateInfo& info) { … }
base::FilePath DownloadManagerImpl::GetDefaultDownloadDirectory() { … }
void DownloadManagerImpl::OnDownloadsInitialized() { … }
void DownloadManagerImpl::StartDownloadItem(
std::unique_ptr<download::DownloadCreateInfo> info,
download::DownloadUrlParameters::OnStartedCallback on_started,
download::InProgressDownloadManager::StartDownloadItemCallback callback) { … }
void DownloadManagerImpl::OnNewDownloadIdRetrieved(
std::unique_ptr<download::DownloadCreateInfo> info,
download::DownloadUrlParameters::OnStartedCallback on_started,
download::InProgressDownloadManager::StartDownloadItemCallback callback,
uint32_t id) { … }
void DownloadManagerImpl::CreateNewDownloadItemToStart(
std::unique_ptr<download::DownloadCreateInfo> info,
download::DownloadUrlParameters::OnStartedCallback on_started,
download::InProgressDownloadManager::StartDownloadItemCallback callback,
uint32_t id,
const base::FilePath& duplicate_download_file_path,
bool duplicate_file_exists) { … }
void DownloadManagerImpl::BindWakeLockProvider(
mojo::PendingReceiver<device::mojom::WakeLockProvider> receiver) { … }
download::QuarantineConnectionCallback
DownloadManagerImpl::GetQuarantineConnectionCallback() { … }
std::unique_ptr<download::DownloadItemRenameHandler>
DownloadManagerImpl::GetRenameHandlerForDownload(
download::DownloadItemImpl* download_item) { … }
void DownloadManagerImpl::StartDownload(
std::unique_ptr<download::DownloadCreateInfo> info,
std::unique_ptr<download::InputStream> stream,
download::DownloadUrlParameters::OnStartedCallback on_started) { … }
void DownloadManagerImpl::CheckForHistoryFilesRemoval() { … }
void DownloadManagerImpl::OnHistoryQueryComplete(
base::OnceClosure load_history_downloads_cb) { … }
void DownloadManagerImpl::CheckForFileRemoval(
download::DownloadItemImpl* download_item) { … }
void DownloadManagerImpl::OnFileExistenceChecked(const std::string& guid,
bool result) { … }
std::string DownloadManagerImpl::GetApplicationClientIdForFileScanning() const { … }
BrowserContext* DownloadManagerImpl::GetBrowserContext() { … }
void DownloadManagerImpl::CreateSavePackageDownloadItem(
const base::FilePath& main_file_path,
const GURL& page_url,
const std::string& mime_type,
int render_process_id,
int render_frame_id,
download::DownloadJob::CancelRequestCallback cancel_request_callback,
DownloadItemImplCreated item_created) { … }
void DownloadManagerImpl::CreateSavePackageDownloadItemWithId(
const base::FilePath& main_file_path,
const GURL& page_url,
const std::string& mime_type,
int render_process_id,
int render_frame_id,
download::DownloadJob::CancelRequestCallback cancel_request_callback,
DownloadItemImplCreated item_created,
uint32_t id) { … }
void DownloadManagerImpl::ResumeInterruptedDownload(
std::unique_ptr<download::DownloadUrlParameters> params,
const std::string& serialized_embedder_download_data) { … }
void DownloadManagerImpl::SetDownloadItemFactoryForTesting(
std::unique_ptr<download::DownloadItemFactory> item_factory) { … }
void DownloadManagerImpl::SetDownloadFileFactoryForTesting(
std::unique_ptr<download::DownloadFileFactory> file_factory) { … }
download::DownloadFileFactory*
DownloadManagerImpl::GetDownloadFileFactoryForTesting() { … }
void DownloadManagerImpl::DownloadRemoved(
download::DownloadItemImpl* download) { … }
void DownloadManagerImpl::DownloadInterrupted(
download::DownloadItemImpl* download) { … }
bool DownloadManagerImpl::IsOffTheRecord() const { … }
void DownloadManagerImpl::ReportBytesWasted(
download::DownloadItemImpl* download) { … }
void DownloadManagerImpl::InterceptNavigation(
std::unique_ptr<network::ResourceRequest> resource_request,
std::vector<GURL> url_chain,
network::mojom::URLResponseHeadPtr response_head,
mojo::ScopedDataPipeConsumerHandle response_body,
network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints,
net::CertStatus cert_status,
int frame_tree_node_id,
bool from_download_cross_origin_redirect) { … }
int DownloadManagerImpl::RemoveDownloadsByURLAndTime(
const base::RepeatingCallback<bool(const GURL&)>& url_filter,
base::Time remove_begin,
base::Time remove_end) { … }
bool DownloadManagerImpl::CanDownload(
download::DownloadUrlParameters* parameters) { … }
void DownloadManagerImpl::DownloadUrl(
std::unique_ptr<download::DownloadUrlParameters> params) { … }
void DownloadManagerImpl::DownloadUrl(
std::unique_ptr<download::DownloadUrlParameters> params,
scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory) { … }
void DownloadManagerImpl::AddObserver(Observer* observer) { … }
void DownloadManagerImpl::RemoveObserver(Observer* observer) { … }
download::DownloadItem* DownloadManagerImpl::CreateDownloadItem(
const std::string& guid,
uint32_t id,
const base::FilePath& current_path,
const base::FilePath& target_path,
const std::vector<GURL>& url_chain,
const GURL& referrer_url,
const StoragePartitionConfig& storage_partition_config,
const GURL& tab_url,
const GURL& tab_refererr_url,
const std::optional<url::Origin>& request_initiator,
const std::string& mime_type,
const std::string& original_mime_type,
base::Time start_time,
base::Time end_time,
const std::string& etag,
const std::string& last_modified,
int64_t received_bytes,
int64_t total_bytes,
const std::string& hash,
download::DownloadItem::DownloadState state,
download::DownloadDangerType danger_type,
download::DownloadInterruptReason interrupt_reason,
bool opened,
base::Time last_access_time,
bool transient,
const std::vector<download::DownloadItem::ReceivedSlice>& received_slices) { … }
void DownloadManagerImpl::OnDownloadCreated(
std::unique_ptr<download::DownloadItemImpl> download) { … }
void DownloadManagerImpl::PostInitialization(
DownloadInitializationDependency dependency) { … }
void DownloadManagerImpl::ImportInProgressDownloads(uint32_t id) { … }
void DownloadManagerImpl::OnDownloadManagerInitialized() { … }
bool DownloadManagerImpl::IsManagerInitialized() { … }
int DownloadManagerImpl::InProgressCount() { … }
int DownloadManagerImpl::BlockingShutdownCount() { … }
download::DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) { … }
download::DownloadItem* DownloadManagerImpl::GetDownloadByGuid(
const std::string& guid) { … }
void DownloadManagerImpl::GetAllDownloads(
download::SimpleDownloadManager::DownloadVector* downloads) { … }
void DownloadManagerImpl::GetUninitializedActiveDownloadsIfAny(
download::SimpleDownloadManager::DownloadVector* downloads) { … }
void DownloadManagerImpl::OpenDownload(download::DownloadItemImpl* download) { … }
void DownloadManagerImpl::ShowDownloadInShell(
download::DownloadItemImpl* download) { … }
void DownloadManagerImpl::DropDownload() { … }
void DownloadManagerImpl::InterceptNavigationOnChecksComplete(
int frame_tree_node_id,
std::unique_ptr<network::ResourceRequest> resource_request,
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,
bool is_download_allowed) { … }
void DownloadManagerImpl::BeginResourceDownloadOnChecksComplete(
std::unique_ptr<download::DownloadUrlParameters> params,
scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
bool is_new_download,
const StoragePartitionConfig& storage_partition_config,
bool is_download_allowed) { … }
void DownloadManagerImpl::BeginDownloadInternal(
std::unique_ptr<download::DownloadUrlParameters> params,
scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
bool is_new_download,
const std::string& serialized_embedder_download_data) { … }
bool DownloadManagerImpl::IsNextIdInitialized() const { … }
bool DownloadManagerImpl::ShouldClearDownloadFromDB(
const GURL& url,
download::DownloadItem::DownloadState state,
download::DownloadInterruptReason reason,
const base::Time& start_time) { … }
std::unique_ptr<download::DownloadItemImpl>
DownloadManagerImpl::RetrieveInProgressDownload(uint32_t id) { … }
}