#include "chrome/browser/download/download_prefs.h"
#include <stddef.h>
#include <algorithm>
#include <string>
#include <vector>
#include "base/check.h"
#include "base/feature_list.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/no_destructor.h"
#include "base/path_service.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/download/chrome_download_manager_delegate.h"
#include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/download/download_core_service_impl.h"
#include "chrome/browser/download/download_dir_util.h"
#include "chrome/browser/download/download_prompt_status.h"
#include "chrome/browser/download/download_stats.h"
#include "chrome/browser/download/download_target_determiner.h"
#include "chrome/browser/download/trusted_sources_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "components/download/public/common/download_features.h"
#include "components/download/public/common/download_item.h"
#include "components/policy/core/browser/url_list/url_blocklist_manager.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/buildflags.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/save_page_type.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "base/json/values_util.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/drive/drive_integration_service_factory.h"
#include "chrome/browser/ash/drive/file_system_util.h"
#include "chrome/browser/ash/file_manager/path_util.h"
#include "chrome/browser/ui/webui/ash/cloud_upload/cloud_upload_util.h"
#include "chromeos/ash/components/dbus/cros_disks/cros_disks_client.h"
#endif
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/flags/android/chrome_feature_list.h"
#endif
#if BUILDFLAG(SAFE_BROWSING_AVAILABLE)
#include "components/safe_browsing/content/common/file_type_policies.h"
#endif
using content::BrowserContext;
using content::BrowserThread;
using content::DownloadManager;
#if BUILDFLAG(SAFE_BROWSING_AVAILABLE)
using safe_browsing::FileTypePolicies;
#endif
namespace {
bool DownloadPathIsDangerous(const base::FilePath& download_path) {
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
base::FilePath home_dir = base::GetHomeDir();
if (download_path == home_dir) {
return true;
}
#endif
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_OHOS)
return false;
#else
base::FilePath desktop_dir;
if (!base::PathService::Get(base::DIR_USER_DESKTOP, &desktop_dir)) {
return false;
}
return (download_path == desktop_dir);
#endif
}
base::FilePath::StringType StringToFilePathString(const std::string& src) {
#if BUILDFLAG(IS_WIN)
return base::UTF8ToWide(src);
#else
return src;
#endif
}
class DefaultDownloadDirectory {
public:
DefaultDownloadDirectory(const DefaultDownloadDirectory&) = delete;
DefaultDownloadDirectory& operator=(const DefaultDownloadDirectory&) = delete;
const base::FilePath& path() const { return path_; }
void Initialize() {
#if !BUILDFLAG(ARKWEB_EX_DOWNLOAD)
if (!base::PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &path_)) {
base::GetTempDir(&path_);
}
#endif
if (DownloadPathIsDangerous(path_)) {
base::PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS_SAFE, &path_);
}
}
private:
friend class base::NoDestructor<DefaultDownloadDirectory>;
DefaultDownloadDirectory() { Initialize(); }
base::FilePath path_;
};
DefaultDownloadDirectory& GetDefaultDownloadDirectorySingleton() {
static base::NoDestructor<DefaultDownloadDirectory> instance;
return *instance;
}
}
DownloadPrefs::DownloadPrefs(Profile* profile) : profile_(profile) {
PrefService* prefs = profile->GetPrefs();
pref_change_registrar_.Init(prefs);
#if BUILDFLAG(IS_CHROMEOS)
const char* const kPathPrefs[] = {prefs::kSaveFileDefaultDirectory,
prefs::kDownloadDefaultDirectory};
for (const char* path_pref : kPathPrefs) {
const PrefService::Preference* pref = prefs->FindPreference(path_pref);
if (pref->IsUserControlled()) {
const base::FilePath current = prefs->GetFilePath(path_pref);
base::FilePath migrated;
if (!current.empty() &&
file_manager::util::MigratePathFromOldFormat(
profile_, GetDefaultDownloadDirectory(), current, &migrated)) {
prefs->SetFilePath(path_pref, migrated);
} else if (file_manager::util::MigrateToDriveFs(profile_, current,
&migrated)) {
prefs->SetFilePath(path_pref, migrated);
} else if (download_dir_util::ExpandDrivePolicyVariable(profile_, current,
&migrated)) {
prefs->SetFilePath(path_pref, migrated);
}
} else if (pref->IsDefaultValue()) {
prefs->SetDefaultPrefValue(
path_pref,
base::FilePathToValue(GetDefaultDownloadDirectoryForProfile()));
}
}
content::DownloadManager::GetTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(base::IgnoreResult(&base::CreateDirectory),
GetDefaultDownloadDirectoryForProfile()));
#endif
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
BUILDFLAG(IS_MAC) || BUILDFLAG(IS_OHOS)
should_open_pdf_in_system_reader_ =
prefs->GetBoolean(prefs::kOpenPdfDownloadInSystemReader);
#endif
if (prefs->FindPreference(prefs::kDownloadDefaultDirectory)
->IsUserControlled()) {
base::FilePath current_download_dir =
prefs->GetFilePath(prefs::kDownloadDefaultDirectory);
if (!current_download_dir.IsAbsolute()) {
prefs->SetFilePath(prefs::kDownloadDefaultDirectory,
GetDefaultDownloadDirectoryForProfile());
} else if (!prefs->GetBoolean(prefs::kDownloadDirUpgraded)) {
if (DownloadPathIsDangerous(current_download_dir)) {
prefs->SetFilePath(prefs::kDownloadDefaultDirectory,
GetDefaultDownloadDirectoryForProfile());
}
prefs->SetBoolean(prefs::kDownloadDirUpgraded, true);
}
}
prompt_for_download_.Init(prefs::kPromptForDownload, prefs);
#if BUILDFLAG(IS_ANDROID)
prompt_for_download_android_.Init(prefs::kPromptForDownloadAndroid, prefs);
RecordDownloadPromptStatus(
static_cast<DownloadPromptStatus>(*prompt_for_download_android_));
auto_open_pdf_enabled_.Init(prefs::kAutoOpenPdfEnabled, prefs);
#endif
download_path_.Init(prefs::kDownloadDefaultDirectory, prefs);
save_file_path_.Init(prefs::kSaveFileDefaultDirectory, prefs);
save_file_type_.Init(prefs::kSaveFileType, prefs);
safebrowsing_for_trusted_sources_enabled_.Init(
prefs::kSafeBrowsingForTrustedSourcesEnabled, prefs);
download_restriction_.Init(policy::policy_prefs::kDownloadRestrictions,
prefs);
pref_change_registrar_.Add(
prefs::kDownloadExtensionsToOpenByPolicy,
base::BindRepeating(&DownloadPrefs::UpdateAutoOpenByPolicy,
base::Unretained(this)));
UpdateAutoOpenByPolicy();
pref_change_registrar_.Add(
prefs::kDownloadAllowedURLsForOpenByPolicy,
base::BindRepeating(&DownloadPrefs::UpdateAllowedURLsForOpenByPolicy,
base::Unretained(this)));
UpdateAllowedURLsForOpenByPolicy();
std::string user_extensions_to_open =
prefs->GetString(prefs::kDownloadExtensionsToOpen);
for (const auto& extension_string :
base::SplitString(user_extensions_to_open, ":", base::TRIM_WHITESPACE,
base::SPLIT_WANT_ALL)) {
base::FilePath::StringType extension =
StringToFilePathString(extension_string);
if (extension.empty() ||
*extension.begin() == base::FilePath::kExtensionSeparator) {
continue;
}
base::FilePath filename_with_extension = base::FilePath(
base::FilePath::StringType(1, base::FilePath::kExtensionSeparator) +
extension);
#if BUILDFLAG(SAFE_BROWSING_AVAILABLE)
if (!FileTypePolicies::GetInstance()->IsAllowedToOpenAutomatically(
filename_with_extension)) {
continue;
}
#endif
auto_open_by_user_.insert(extension);
}
}
DownloadPrefs::~DownloadPrefs() = default;
void DownloadPrefs::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(
prefs::kPromptForDownload,
false,
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
registry->RegisterStringPref(prefs::kDownloadExtensionsToOpen, std::string());
registry->RegisterListPref(prefs::kDownloadExtensionsToOpenByPolicy, {});
registry->RegisterListPref(prefs::kDownloadAllowedURLsForOpenByPolicy, {});
registry->RegisterBooleanPref(prefs::kDownloadDirUpgraded, false);
registry->RegisterIntegerPref(prefs::kSaveFileType,
content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML);
registry->RegisterIntegerPref(policy::policy_prefs::kDownloadRestrictions, 0);
registry->RegisterBooleanPref(prefs::kDownloadBubblePartialViewEnabled, true);
registry->RegisterIntegerPref(prefs::kDownloadBubblePartialViewImpressions,
0);
registry->RegisterBooleanPref(prefs::kSafeBrowsingForTrustedSourcesEnabled,
true);
const base::FilePath& default_download_path = GetDefaultDownloadDirectory();
registry->RegisterFilePathPref(prefs::kDownloadDefaultDirectory,
default_download_path);
registry->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory,
default_download_path);
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
BUILDFLAG(IS_MAC) || BUILDFLAG(IS_OHOS)
registry->RegisterBooleanPref(prefs::kOpenPdfDownloadInSystemReader, false);
#endif
#if BUILDFLAG(IS_ANDROID)
DownloadPromptStatus download_prompt_status =
DownloadPromptStatus::SHOW_INITIAL;
registry->RegisterIntegerPref(
prefs::kPromptForDownloadAndroid,
static_cast<int>(download_prompt_status),
user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
registry->RegisterBooleanPref(prefs::kShowMissingSdCardErrorAndroid, true);
registry->RegisterBooleanPref(prefs::kAutoOpenPdfEnabled, false);
registry->RegisterListPref(prefs::kDownloadAppVerificationPromptTimestamps,
{});
#endif
}
base::FilePath DownloadPrefs::GetDefaultDownloadDirectoryForProfile() const {
#if BUILDFLAG(IS_CHROMEOS)
return file_manager::util::GetDownloadsFolderForProfile(profile_);
#else
return GetDefaultDownloadDirectory();
#endif
}
const base::FilePath& DownloadPrefs::GetDefaultDownloadDirectory() {
return GetDefaultDownloadDirectorySingleton().path();
}
DownloadPrefs* DownloadPrefs::FromDownloadManager(
DownloadManager* download_manager) {
DCHECK(download_manager->GetBrowserContext());
DownloadCoreService* service =
DownloadCoreServiceFactory::GetForBrowserContext(
download_manager->GetBrowserContext());
DCHECK(service);
ChromeDownloadManagerDelegate* delegate =
service->GetDownloadManagerDelegate();
DCHECK(delegate);
return delegate->download_prefs();
}
DownloadPrefs* DownloadPrefs::FromBrowserContext(
content::BrowserContext* context) {
return FromDownloadManager(context->GetDownloadManager());
}
bool DownloadPrefs::IsFromTrustedSource(const download::DownloadItem& item) {
if (!trusted_sources_manager_)
trusted_sources_manager_ = TrustedSourcesManager::Create();
return trusted_sources_manager_->IsFromTrustedSource(item.GetURL());
}
base::FilePath DownloadPrefs::DownloadPath() const {
return SanitizeDownloadTargetPath(*download_path_);
}
void DownloadPrefs::SetDownloadPath(const base::FilePath& path) {
download_path_.SetValue(path);
SetSaveFilePath(path);
}
base::FilePath DownloadPrefs::SaveFilePath() const {
return SanitizeDownloadTargetPath(*save_file_path_);
}
void DownloadPrefs::SetSaveFilePath(const base::FilePath& path) {
save_file_path_.SetValue(path);
}
void DownloadPrefs::SetSaveFileType(int type) {
save_file_type_.SetValue(type);
}
bool DownloadPrefs::PromptForDownload() const {
DCHECK(!download_path_.IsManaged() || !prompt_for_download_.GetValue());
#if BUILDFLAG(IS_ANDROID)
if (prompt_for_download_.IsManaged())
return prompt_for_download_.GetValue();
return *prompt_for_download_android_ !=
static_cast<int>(DownloadPromptStatus::DONT_SHOW);
#else
return *prompt_for_download_;
#endif
}
bool DownloadPrefs::IsDownloadPathManaged() const {
return download_path_.IsManaged();
}
bool DownloadPrefs::IsAutoOpenByUserUsed() const {
return CanPlatformEnableAutoOpenForPdf() || !auto_open_by_user_.empty();
}
bool DownloadPrefs::IsAutoOpenEnabled(const GURL& url,
const base::FilePath& path) const {
base::FilePath::StringType extension = path.Extension();
if (extension.empty())
return false;
DCHECK(extension[0] == base::FilePath::kExtensionSeparator);
extension.erase(0, 1);
if (base::FilePath::CompareEqualIgnoreCase(extension,
FILE_PATH_LITERAL("pdf")) &&
CanPlatformEnableAutoOpenForPdf())
return true;
return auto_open_by_user_.find(extension) != auto_open_by_user_.end() ||
IsAutoOpenByPolicy(url, path);
}
bool DownloadPrefs::IsAutoOpenByPolicy(const GURL& url,
const base::FilePath& path) const {
base::FilePath::StringType extension = path.Extension();
if (extension.empty())
return false;
DCHECK(extension[0] == base::FilePath::kExtensionSeparator);
extension.erase(0, 1);
const GURL fixed_url =
url.SchemeIsBlob() ? url::Origin::Create(url).GetURL() : url;
return auto_open_by_policy_.find(extension) != auto_open_by_policy_.end() &&
!auto_open_allowed_by_urls_->IsURLBlocked(fixed_url);
}
bool DownloadPrefs::EnableAutoOpenByUserBasedOnExtension(
const base::FilePath& file_name) {
base::FilePath::StringType extension = file_name.Extension();
#if BUILDFLAG(SAFE_BROWSING_AVAILABLE)
if (!FileTypePolicies::GetInstance()->IsAllowedToOpenAutomatically(
file_name)) {
return false;
}
DCHECK(extension[0] == base::FilePath::kExtensionSeparator);
#else
if (extension[0] != base::FilePath::kExtensionSeparator) {
return false;
}
#endif
extension.erase(0, 1);
auto_open_by_user_.insert(extension);
SaveAutoOpenState();
return true;
}
void DownloadPrefs::DisableAutoOpenByUserBasedOnExtension(
const base::FilePath& file_name) {
base::FilePath::StringType extension = file_name.Extension();
if (extension.empty())
return;
DCHECK(extension[0] == base::FilePath::kExtensionSeparator);
extension.erase(0, 1);
auto_open_by_user_.erase(extension);
SaveAutoOpenState();
}
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
BUILDFLAG(IS_MAC) || BUILDFLAG(IS_OHOS)
void DownloadPrefs::SetShouldOpenPdfInSystemReader(bool should_open) {
if (should_open_pdf_in_system_reader_ == should_open)
return;
should_open_pdf_in_system_reader_ = should_open;
profile_->GetPrefs()->SetBoolean(prefs::kOpenPdfDownloadInSystemReader,
should_open);
}
bool DownloadPrefs::ShouldOpenPdfInSystemReader() const {
#if BUILDFLAG(IS_CHROMEOS)
return true;
#else
return should_open_pdf_in_system_reader_;
#endif
}
#endif
void DownloadPrefs::ResetAutoOpenByUser() {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
BUILDFLAG(IS_MAC) || BUILDFLAG(IS_OHOS)
SetShouldOpenPdfInSystemReader(false);
#endif
auto_open_by_user_.clear();
SaveAutoOpenState();
}
void DownloadPrefs::SkipSanitizeDownloadTargetPathForTesting() {
skip_sanitize_download_target_path_for_testing_ = true;
}
#if BUILDFLAG(IS_ANDROID)
bool DownloadPrefs::IsAutoOpenPdfEnabled() {
return *auto_open_pdf_enabled_;
}
#endif
void DownloadPrefs::SaveAutoOpenState() {
std::string extensions;
for (const auto& it : auto_open_by_user_) {
#if BUILDFLAG(IS_WIN)
std::string this_extension = base::SysWideToUTF8(it);
#else
std::string this_extension = it;
#endif
extensions += this_extension + ":";
}
if (!extensions.empty())
extensions.erase(extensions.size() - 1);
profile_->GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, extensions);
}
bool DownloadPrefs::CanPlatformEnableAutoOpenForPdf() const {
#if BUILDFLAG(IS_CHROMEOS)
return false;
#elif BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || \
BUILDFLAG(IS_OHOS)
return ShouldOpenPdfInSystemReader();
#else
return false;
#endif
}
base::FilePath DownloadPrefs::SanitizeDownloadTargetPath(
const base::FilePath& path) const {
if (skip_sanitize_download_target_path_for_testing_)
return path;
#if BUILDFLAG(IS_CHROMEOS)
base::FilePath migrated_drive_path;
if (!path.empty() && file_manager::util::MigratePathFromOldFormat(
profile_, GetDefaultDownloadDirectory(), path,
&migrated_drive_path)) {
return SanitizeDownloadTargetPath(migrated_drive_path);
}
if (file_manager::util::MigrateToDriveFs(profile_, path,
&migrated_drive_path)) {
return SanitizeDownloadTargetPath(migrated_drive_path);
}
if (download_dir_util::ExpandDrivePolicyVariable(profile_, path,
&migrated_drive_path)) {
return SanitizeDownloadTargetPath(migrated_drive_path);
}
base::FilePath onedrive_path;
if (download_dir_util::ExpandOneDrivePolicyVariable(profile_, path,
&onedrive_path)) {
return SanitizeDownloadTargetPath(onedrive_path);
}
base::FilePath profile_myfiles_path =
file_manager::util::GetMyFilesFolderForProfile(profile_);
if (!path.IsAbsolute() || path.ReferencesParent())
return profile_myfiles_path;
if (profile_myfiles_path == path || profile_myfiles_path.IsParent(path))
return path;
drive::DriveIntegrationService* integration_service =
drive::DriveIntegrationServiceFactory::FindForProfile(profile_);
if (integration_service && integration_service->is_enabled() &&
integration_service->GetMountPointPath().IsParent(path)) {
return path;
}
base::FilePath temp_path;
if (base::FeatureList::IsEnabled(features::kSkyVault) &&
base::GetTempDir(&temp_path) &&
((temp_path == path) || temp_path.IsParent(path))) {
return path;
}
if (ash::CrosDisksClient::GetRemovableDiskMountPoint().IsParent(path))
return path;
if (base::FilePath(file_manager::util::GetAndroidFilesPath()).IsParent(path))
return path;
base::FilePath linux_files =
file_manager::util::GetCrostiniMountDirectory(profile_);
if (linux_files == path || linux_files.IsParent(path))
return path;
return GetDefaultDownloadDirectoryForProfile();
#else
if (path.IsAbsolute())
return path;
return GetDefaultDownloadDirectoryForProfile();
#endif
}
void DownloadPrefs::UpdateAutoOpenByPolicy() {
auto_open_by_policy_.clear();
PrefService* prefs = profile_->GetPrefs();
for (const auto& extension :
prefs->GetList(prefs::kDownloadExtensionsToOpenByPolicy)) {
base::FilePath::StringType extension_string =
StringToFilePathString(extension.GetString());
auto_open_by_policy_.insert(extension_string);
}
}
void DownloadPrefs::UpdateAllowedURLsForOpenByPolicy() {
std::unique_ptr<policy::URLBlocklist> allowed_urls =
std::make_unique<policy::URLBlocklist>();
PrefService* prefs = profile_->GetPrefs();
const auto& list = prefs->GetList(prefs::kDownloadAllowedURLsForOpenByPolicy);
if (list.size() != 0) {
allowed_urls->Allow(list);
auto blocked = base::Value::List();
blocked.Append("*");
allowed_urls->Block(blocked);
}
auto_open_allowed_by_urls_.swap(allowed_urls);
}
bool DownloadPrefs::AutoOpenCompareFunctor::operator()(
const base::FilePath::StringType& a,
const base::FilePath::StringType& b) const {
return base::FilePath::CompareLessIgnoreCase(a, b);
}