#include "chrome/browser/safe_browsing/download_protection/download_protection_service.h"
#include <memory>
#include <optional>
#include <utility>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/not_fatal_until.h"
#include "base/strings/string_split.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/download/chrome_download_manager_delegate.h"
#include "chrome/browser/download/download_core_service.h"
#include "chrome/browser/download/download_core_service_factory.h"
#include "chrome/browser/download/download_item_warning_data.h"
#include "chrome/browser/enterprise/connectors/connectors_manager.h"
#include "chrome/browser/extensions/api/safe_browsing_private/safe_browsing_private_event_router.h"
#include "chrome/browser/extensions/api/safe_browsing_private/safe_browsing_private_event_router_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/safe_browsing/advanced_protection_status_manager.h"
#include "chrome/browser/safe_browsing/advanced_protection_status_manager_factory.h"
#include "chrome/browser/safe_browsing/download_protection/check_client_download_request.h"
#include "chrome/browser/safe_browsing/download_protection/check_file_system_access_write_request.h"
#include "chrome/browser/safe_browsing/download_protection/deep_scanning_request.h"
#include "chrome/browser/safe_browsing/download_protection/download_feedback_service.h"
#include "chrome/browser/safe_browsing/download_protection/download_protection_util.h"
#include "chrome/browser/safe_browsing/download_protection/download_request_maker.h"
#include "chrome/browser/safe_browsing/download_protection/download_url_sb_client.h"
#include "chrome/browser/safe_browsing/download_protection/ppapi_download_request.h"
#include "chrome/browser/safe_browsing/safe_browsing_metrics_collector_factory.h"
#include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager_factory.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "chrome/browser/safe_browsing/services_delegate.h"
#include "chrome/common/safe_browsing/binary_feature_extractor.h"
#include "chrome/common/safe_browsing/download_type_util.h"
#include "chrome/common/url_constants.h"
#include "components/download/public/common/download_danger_type.h"
#include "components/download/public/common/download_item.h"
#include "components/google/core/common/google_util.h"
#include "components/prefs/pref_service.h"
#include "components/safe_browsing/content/browser/safe_browsing_navigation_observer_manager.h"
#include "components/safe_browsing/content/browser/web_ui/safe_browsing_ui.h"
#include "components/safe_browsing/core/common/features.h"
#include "components/safe_browsing/core/common/safe_browsing_prefs.h"
#include "components/safe_browsing/core/common/safebrowsing_switches.h"
#include "components/sessions/content/session_tab_helper.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_item_utils.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/web_contents.h"
#include "net/base/url_util.h"
#include "net/cert/x509_util.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
BrowserThread;
ReportThreatDetailsResult;
namespace safe_browsing {
namespace {
inline constexpr int kDownloadAttributionUserGestureLimit = …;
inline constexpr int kDownloadAttributionUserGestureLimitForExtendedReporting = …;
const int64_t kDownloadRequestTimeoutMs = …;
const double kAllowlistDownloadSampleRate = …;
bool IsDownloadSecuritySensitive(safe_browsing::DownloadCheckResult result) { … }
void MaybeLogSecuritySensitiveDownloadEvent(
content::BrowserContext* browser_context,
DownloadCheckResult result) { … }
}
const void* const DownloadProtectionService::kDownloadProtectionDataKey = …;
DownloadProtectionService::DownloadProtectionService(
SafeBrowsingService* sb_service)
: … { … }
DownloadProtectionService::~DownloadProtectionService() { … }
void DownloadProtectionService::SetEnabled(bool enabled) { … }
void DownloadProtectionService::ParseManualBlocklistFlag() { … }
bool DownloadProtectionService::IsHashManuallyBlocklisted(
const std::string& sha256_hash) const { … }
void DownloadProtectionService::CheckClientDownload(
download::DownloadItem* item,
CheckDownloadRepeatingCallback callback,
base::optional_ref<const std::string> password) { … }
bool DownloadProtectionService::MaybeCheckClientDownload(
download::DownloadItem* item,
CheckDownloadRepeatingCallback callback) { … }
void DownloadProtectionService::CancelChecksForDownload(
download::DownloadItem* item) { … }
bool DownloadProtectionService::ShouldCheckDownloadUrl(
download::DownloadItem* item) { … }
void DownloadProtectionService::CheckDownloadUrl(
download::DownloadItem* item,
CheckDownloadCallback callback) { … }
bool DownloadProtectionService::IsSupportedDownload(
const download::DownloadItem& item,
const base::FilePath& target_path) const { … }
void DownloadProtectionService::CheckPPAPIDownloadRequest(
const GURL& requestor_url,
content::RenderFrameHost* initiating_frame,
const base::FilePath& default_file_path,
const std::vector<base::FilePath::StringType>& alternate_extensions,
Profile* profile,
CheckDownloadCallback callback) { … }
void DownloadProtectionService::CheckFileSystemAccessWrite(
std::unique_ptr<content::FileSystemAccessWriteItem> item,
CheckDownloadCallback callback) { … }
base::CallbackListSubscription
DownloadProtectionService::RegisterClientDownloadRequestCallback(
const ClientDownloadRequestCallback& callback) { … }
base::CallbackListSubscription
DownloadProtectionService::RegisterFileSystemAccessWriteRequestCallback(
const FileSystemAccessWriteRequestCallback& callback) { … }
base::CallbackListSubscription
DownloadProtectionService::RegisterPPAPIDownloadRequestCallback(
const PPAPIDownloadRequestCallback& callback) { … }
void DownloadProtectionService::CancelPendingRequests() { … }
void DownloadProtectionService::RequestFinished(
CheckClientDownloadRequestBase* request,
content::BrowserContext* browser_context,
DownloadCheckResult result) { … }
void DownloadProtectionService::PPAPIDownloadCheckRequestFinished(
PPAPIDownloadRequest* request) { … }
void DownloadProtectionService::ShowDetailsForDownload(
const download::DownloadItem* item,
content::PageNavigator* navigator) { … }
void DownloadProtectionService::SetDownloadProtectionData(
download::DownloadItem* item,
const std::string& token,
const ClientDownloadResponse::Verdict& verdict,
const ClientDownloadResponse::TailoredVerdict& tailored_verdict) { … }
std::string DownloadProtectionService::GetDownloadPingToken(
const download::DownloadItem* item) { … }
bool DownloadProtectionService::HasDownloadProtectionVerdict(
const download::DownloadItem* item) { … }
ClientDownloadResponse::Verdict
DownloadProtectionService::GetDownloadProtectionVerdict(
const download::DownloadItem* item) { … }
ClientDownloadResponse::TailoredVerdict
DownloadProtectionService::GetDownloadProtectionTailoredVerdict(
const download::DownloadItem* item) { … }
bool DownloadProtectionService::ShouldSendDangerousDownloadReport(
download::DownloadItem* item) { … }
void DownloadProtectionService::MaybeSendDangerousDownloadOpenedReport(
download::DownloadItem* item,
bool show_download_in_folder) { … }
void DownloadProtectionService::ReportDelayedBypassEvent(
download::DownloadItem* download,
download::DownloadDangerType danger_type) { … }
void DownloadProtectionService::AddReferrerChainToPPAPIClientDownloadRequest(
content::WebContents* web_contents,
const GURL& initiating_frame_url,
const content::GlobalRenderFrameHostId& initiating_outermost_main_frame_id,
const GURL& initiating_main_frame_url,
SessionID tab_id,
bool has_user_gesture,
ClientDownloadRequest* out_request) { … }
void DownloadProtectionService::OnDangerousDownloadOpened(
const download::DownloadItem* item,
Profile* profile) { … }
base::TimeDelta DownloadProtectionService::GetDownloadRequestTimeout() const { … }
bool DownloadProtectionService::MaybeBeginFeedbackForDownload(
Profile* profile,
download::DownloadItem* download,
DownloadCommands::Command download_command) { … }
void DownloadProtectionService::UploadForDeepScanning(
download::DownloadItem* item,
CheckDownloadRepeatingCallback callback,
DownloadItemWarningData::DeepScanTrigger trigger,
DownloadCheckResult download_check_result,
enterprise_connectors::AnalysisSettings analysis_settings,
base::optional_ref<const std::string> password) { … }
void DownloadProtectionService::UploadForConsumerDeepScanning(
download::DownloadItem* item,
DownloadItemWarningData::DeepScanTrigger trigger,
base::optional_ref<const std::string> password) { … }
void DownloadProtectionService::CheckDownloadWithLocalDecryption(
download::DownloadItem* item,
base::optional_ref<const std::string> password) { … }
void DownloadProtectionService::UploadSavePackageForDeepScanning(
download::DownloadItem* item,
base::flat_map<base::FilePath, base::FilePath> save_package_files,
CheckDownloadRepeatingCallback callback,
enterprise_connectors::AnalysisSettings analysis_settings) { … }
std::vector<DeepScanningRequest*>
DownloadProtectionService::GetDeepScanningRequests() { … }
scoped_refptr<network::SharedURLLoaderFactory>
DownloadProtectionService::GetURLLoaderFactory(
content::BrowserContext* browser_context) { … }
void DownloadProtectionService::RemovePendingDownloadRequests(
content::BrowserContext* browser_context) { … }
int DownloadProtectionService::GetDownloadAttributionUserGestureLimit(
download::DownloadItem* item) { … }
void DownloadProtectionService::RequestFinished(DeepScanningRequest* request) { … }
BinaryUploadService* DownloadProtectionService::GetBinaryUploadService(
Profile* profile,
const enterprise_connectors::AnalysisSettings& settings) { … }
SafeBrowsingNavigationObserverManager*
DownloadProtectionService::GetNavigationObserverManager(
content::WebContents* web_contents) { … }
void DownloadProtectionService::MaybeCheckMetadataAfterDeepScanning(
download::DownloadItem* item,
CheckDownloadRepeatingCallback callback,
DownloadCheckResult result) { … }
}