#include "content/browser/download/save_package.h"
#include <algorithm>
#include <memory>
#include <utility>
#include <vector>
#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/i18n/file_util_icu.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/not_fatal_until.h"
#include "base/rand_util.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread.h"
#include "base/threading/thread_restrictions.h"
#include "base/unguessable_token.h"
#include "build/build_config.h"
#include "components/download/public/common/download_item_impl.h"
#include "components/download/public/common/download_save_item_data.h"
#include "components/download/public/common/download_stats.h"
#include "components/download/public/common/download_task_runner.h"
#include "components/download/public/common/download_ukm_helper.h"
#include "components/download/public/common/download_utils.h"
#include "components/filename_generation/filename_generation.h"
#include "components/url_formatter/url_formatter.h"
#include "content/browser/bad_message.h"
#include "content/browser/download/download_manager_impl.h"
#include "content/browser/download/save_file.h"
#include "content/browser/download/save_file_manager.h"
#include "content/browser/download/save_item.h"
#include "content/browser/download/save_package_serialization_handler.h"
#include "content/browser/renderer_host/frame_tree.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/browser/renderer_host/page_impl.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/renderer_host/render_view_host_delegate.h"
#include "content/browser/renderer_host/render_view_host_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/download_manager_delegate.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/resource_context.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/mhtml_generation_params.h"
#include "content/public/common/referrer_type_converters.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "net/base/filename_util.h"
#include "net/base/mime_util.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "services/network/public/cpp/request_mode.h"
#include "url/url_constants.h"
#if BUILDFLAG(IS_MAC)
#include "base/mac/mac_util.h"
#endif
namespace content {
namespace {
SavePackageId GetNextSavePackageId() { … }
const char kDefaultSaveName[] = …;
const int32_t kMaxFileOrdinalNumber = …;
#if BUILDFLAG(IS_WIN)
const uint32_t kMaxFilePathLength = MAX_PATH - 1;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
const uint32_t kMaxFilePathLength = …;
#endif
const uint32_t kMaxFileOrdinalNumberPartLength = …;
base::FilePath::StringType StripOrdinalNumber(
const base::FilePath::StringType& base_name) { … }
bool CanSaveAsComplete(const std::string& contents_mime_type) { … }
void CancelSavePackage(base::WeakPtr<SavePackage> save_package,
bool user_cancel) { … }
const std::string GetMimeTypeForSaveType(SavePageType save_type) { … }
WebContents* GetWebContents(Page* page) { … }
const std::u16string& GetTitle(Page& page) { … }
bool IsSavableFrame(RenderFrameHost* rfh) { … }
}
const base::FilePath::CharType SavePackage::kDefaultHtmlExtension[] = …);
SavePackage::SavePackage(PageImpl& page)
: … { … }
SavePackage::SavePackage(PageImpl& page,
SavePageType save_type,
const base::FilePath& file_full_path,
const base::FilePath& directory_full_path)
: … { … }
SavePackage::~SavePackage() { … }
void SavePackage::ClearPage() { … }
GURL SavePackage::GetUrlToBeSaved(RenderFrameHost* main_frame) { … }
void SavePackage::Cancel(bool user_action, bool cancel_download_item) { … }
void SavePackage::InternalInit() { … }
bool SavePackage::Init(
SavePackageDownloadCreatedCallback download_created_callback) { … }
void SavePackage::InitWithDownloadItem(
SavePackageDownloadCreatedCallback download_created_callback,
download::DownloadItemImpl* item) { … }
void SavePackage::OnMHTMLGenerated(int64_t size) { … }
uint32_t SavePackage::GetMaxPathLengthForDirectory(
const base::FilePath& base_dir) { … }
bool SavePackage::TruncateBaseNameToFitPathConstraints(
const base::FilePath& dir_path,
const base::FilePath::StringType& file_name_ext,
uint32_t max_file_path_len,
base::FilePath::StringType* base_name) { … }
bool SavePackage::GenerateFileName(const std::string& disposition,
const GURL& url,
bool need_html_ext,
base::FilePath::StringType* generated_name) { … }
void SavePackage::StartSave(const SaveFileCreateInfo* info) { … }
SaveItem* SavePackage::LookupInProgressSaveItem(SaveItemId save_item_id) { … }
void SavePackage::PutInProgressItemToSavedMap(SaveItem* save_item) { … }
bool SavePackage::UpdateSaveProgress(SaveItemId save_item_id,
int64_t size,
bool write_success) { … }
void SavePackage::Stop(bool cancel_download_item) { … }
void SavePackage::CheckFinish() { … }
void SavePackage::CheckRenameAllowedForPaths(
base::flat_map<base::FilePath, base::FilePath> tmp_paths_to_final_paths) { … }
void SavePackage::RenameIfAllowed(bool allowed) { … }
void SavePackage::Finish() { … }
void SavePackage::SaveFinished(SaveItemId save_item_id,
int64_t size,
bool is_success) { … }
void SavePackage::SaveCanceled(const SaveItem* save_item) { … }
void SavePackage::SaveNextFile(bool process_all_remaining_items) { … }
int SavePackage::PercentComplete() { … }
int64_t SavePackage::CurrentSpeed() const { … }
void SavePackage::DoSavingProcess() { … }
void SavePackage::GetSerializedHtmlWithLocalLinks() { … }
void SavePackage::GetSerializedHtmlWithLocalLinksForFrame(
FrameTreeNode* target_tree_node) { … }
void SavePackage::OnDidReceiveSerializedHtmlData(
base::WeakPtr<RenderFrameHostImpl> sender,
const std::string& data) { … }
void SavePackage::OnDidFinishedSerializingHtmlData(
base::WeakPtr<RenderFrameHostImpl> sender) { … }
const SaveItem* SavePackage::LookupSaveItemForSender(
base::WeakPtr<RenderFrameHostImpl> sender) { … }
void SavePackage::GetSavableResourceLinksForRenderFrameHost(
RenderFrameHostImpl* rfh) { … }
void SavePackage::GetSavableResourceLinks() { … }
void SavePackage::SavableResourceLinksResponse(
RenderFrameHostImpl* sender,
const std::vector<GURL>& resources_list,
blink::mojom::ReferrerPtr referrer,
const std::vector<blink::mojom::SavableSubframePtr>& subframes) { … }
SaveItem* SavePackage::CreatePendingSaveItem(
int container_frame_tree_node_id,
int save_item_frame_tree_node_id,
const GURL& url,
const Referrer& referrer,
SaveFileCreateInfo::SaveFileSource save_source) { … }
void SavePackage::CreatePendingSaveItemDeduplicatingByUrl(
int container_frame_tree_node_id,
int save_item_frame_tree_node_id,
const GURL& url,
const Referrer& referrer,
SaveFileCreateInfo::SaveFileSource save_source) { … }
void SavePackage::EnqueueSavableResource(int container_frame_tree_node_id,
const GURL& url,
const Referrer& referrer) { … }
void SavePackage::EnqueueFrame(int container_frame_tree_node_id,
int frame_tree_node_id,
const GURL& frame_original_url) { … }
void SavePackage::SavableResourceLinksError(RenderFrameHostImpl* sender) { … }
void SavePackage::CompleteSavableResourceLinksResponse() { … }
void SavePackage::GetSaveInfo() { … }
base::FilePath SavePackage::CreateDirectoryOnFileThread(
const std::u16string& title,
const GURL& page_url,
bool can_save_as_complete,
const std::string& mime_type,
const base::FilePath& website_save_dir,
const base::FilePath& download_save_dir) { … }
void SavePackage::ContinueGetSaveInfo(bool can_save_as_complete,
const base::FilePath& suggested_path) { … }
void SavePackage::OnPathPicked(
SavePackagePathPickedParams params,
SavePackageDownloadCreatedCallback download_created_callback) { … }
void SavePackage::FinalizeDownloadEntry() { … }
}