#include "chrome/browser/ui/webui/downloads/downloads_dom_handler.h"
#include <algorithm>
#include <functional>
#include <memory>
#include <string>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/i18n/rtl.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/supports_user_data.h"
#include "base/task/current_thread.h"
#include "base/threading/thread.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/download/bubble/download_bubble_ui_controller.h"
#include "chrome/browser/download/download_danger_prompt.h"
#include "chrome/browser/download/download_history.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/download/download_item_warning_data.h"
#include "chrome/browser/download/download_prefs.h"
#include "chrome/browser/download/download_query.h"
#include "chrome/browser/download/download_ui_safe_browsing_util.h"
#include "chrome/browser/download/download_warning_desktop_hats_utils.h"
#include "chrome/browser/download/drag_download_item.h"
#include "chrome/browser/download/offline_item_utils.h"
#include "chrome/browser/feature_engagement/tracker_factory.h"
#include "chrome/browser/lifetime/browser_shutdown.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/safe_browsing/download_protection/download_protection_util.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/hats/trust_safety_sentiment_service.h"
#include "chrome/browser/ui/hats/trust_safety_sentiment_service_factory.h"
#include "chrome/browser/ui/webui/downloads/downloads.mojom.h"
#include "chrome/browser/ui/webui/fileicon_source.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/url_constants.h"
#include "components/download/public/common/download_item.h"
#include "components/history/core/common/pref_names.h"
#include "components/prefs/pref_service.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_referral_methods.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_item_utils.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "net/base/filename_util.h"
#include "ui/base/l10n/time_format.h"
#include "ui/display/screen.h"
#include "ui/gfx/image/image.h"
BrowserThread;
namespace {
WarningAction;
WarningSurface;
enum DownloadsDOMEvent { … };
void CountDownloadsDOMEvents(DownloadsDOMEvent event) { … }
bool CanLogWarningMetrics(download::DownloadItem* file) { … }
std::string InteractionTypeToString(
DangerousDownloadInterstitialInteraction interaction_type) { … }
void RecordDangerousDownloadInterstitialActionHistogram(
DangerousDownloadInterstitialAction action) { … }
void RecordDangerousDownloadInterstitialInteractionHistogram(
DangerousDownloadInterstitialInteraction interaction_type,
const base::TimeDelta elapsed_time) { … }
void PromptForScanningInBubble(content::WebContents* web_contents,
download::DownloadItem* download) { … }
void MaybeReportBypassAction(download::DownloadItem* file,
WarningSurface surface,
WarningAction action) { … }
void MaybeTriggerTrustSafetySurvey(download::DownloadItem* file,
WarningSurface surface,
WarningAction action) { … }
void RecordDownloadsPageValidatedHistogram(download::DownloadItem* item) { … }
}
DownloadsDOMHandler::DownloadsDOMHandler(
mojo::PendingReceiver<downloads::mojom::PageHandler> receiver,
mojo::PendingRemote<downloads::mojom::Page> page,
content::DownloadManager* download_manager,
content::WebUI* web_ui)
: … { … }
DownloadsDOMHandler::~DownloadsDOMHandler() { … }
void DownloadsDOMHandler::PrimaryMainFrameRenderProcessGone(
base::TerminationStatus status) { … }
void DownloadsDOMHandler::GetDownloads(
const std::vector<std::string>& search_terms) { … }
void DownloadsDOMHandler::OpenFileRequiringGesture(const std::string& id) { … }
void DownloadsDOMHandler::Drag(const std::string& id) { … }
void DownloadsDOMHandler::SaveSuspiciousRequiringGesture(
const std::string& id) { … }
void DownloadsDOMHandler::RecordOpenBypassWarningDialog(const std::string& id) { … }
void DownloadsDOMHandler::RecordOpenBypassWarningInterstitial(
const std::string& id) { … }
void DownloadsDOMHandler::RecordOpenSurveyOnDangerousInterstitial(
const std::string& id) { … }
void DownloadsDOMHandler::SaveDangerousFromDialogRequiringGesture(
const std::string& id) { … }
void DownloadsDOMHandler::SaveDangerousFromInterstitialNeedGesture(
const std::string& id,
downloads::mojom::DangerousDownloadInterstitialSurveyOptions response) { … }
void DownloadsDOMHandler::RecordCancelBypassWarningDialog(
const std::string& id) { … }
void DownloadsDOMHandler::RecordCancelBypassWarningInterstitial(
const std::string& id) { … }
void DownloadsDOMHandler::DiscardDangerous(const std::string& id) { … }
void DownloadsDOMHandler::RetryDownload(const std::string& id) { … }
void DownloadsDOMHandler::Show(const std::string& id) { … }
void DownloadsDOMHandler::Pause(const std::string& id) { … }
void DownloadsDOMHandler::Resume(const std::string& id) { … }
void DownloadsDOMHandler::Remove(const std::string& id) { … }
void DownloadsDOMHandler::Undo() { … }
void DownloadsDOMHandler::Cancel(const std::string& id) { … }
void DownloadsDOMHandler::ClearAll() { … }
void DownloadsDOMHandler::RemoveDownloads(const DownloadVector& to_remove) { … }
void DownloadsDOMHandler::OpenDownloadsFolderRequiringGesture() { … }
void DownloadsDOMHandler::OpenDuringScanningRequiringGesture(
const std::string& id) { … }
void DownloadsDOMHandler::DeepScan(const std::string& id) { … }
void DownloadsDOMHandler::BypassDeepScanRequiringGesture(
const std::string& id) { … }
void DownloadsDOMHandler::ReviewDangerousRequiringGesture(
const std::string& id) { … }
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
void DownloadsDOMHandler::OpenEsbSettings() {
Browser* browser = chrome::FindBrowserWithTab(GetWebUIWebContents());
if (!browser) {
return;
}
chrome::ShowSafeBrowsingEnhancedProtectionWithIph(
browser,
safe_browsing::SafeBrowsingSettingReferralMethod::kDownloadPageRowPromo);
feature_engagement::Tracker* tracker =
feature_engagement::TrackerFactory::GetForBrowserContext(
browser->profile());
tracker->NotifyEvent("esb_download_promo_row_clicked");
base::RecordAction(
base::UserMetricsAction("SafeBrowsing.EsbDownloadRowPromo.Click"));
base::UmaHistogramEnumeration(
"SafeBrowsing.EsbDownloadRowPromo.Outcome",
SafeBrowsingEsbDownloadRowPromoOutcome::kClicked);
}
void DownloadsDOMHandler::IsEligibleForEsbPromo(
IsEligibleForEsbPromoCallback callback) {
content::DownloadManager* manager = GetMainNotifierManager();
if (!manager) {
std::move(callback).Run(false);
return;
}
content::BrowserContext* browser_context = manager->GetBrowserContext();
if (!safe_browsing::SafeBrowsingService::IsUserEligibleForESBPromo(
Profile::FromBrowserContext(browser_context))) {
std::move(callback).Run(false);
return;
}
bool should_show_esb_promo = false;
if (feature_engagement::Tracker* tracker =
feature_engagement::TrackerFactory::GetForBrowserContext(
browser_context);
tracker && tracker->ShouldTriggerHelpUI(
feature_engagement::kEsbDownloadRowPromoFeature)) {
should_show_esb_promo = true;
tracker->Dismissed(feature_engagement::kEsbDownloadRowPromoFeature);
}
std::move(callback).Run(should_show_esb_promo);
}
void DownloadsDOMHandler::LogEsbPromotionRowViewed() {
content::DownloadManager* manager = GetMainNotifierManager();
if (!manager) {
return;
}
feature_engagement::Tracker* tracker =
feature_engagement::TrackerFactory::GetForBrowserContext(
manager->GetBrowserContext());
tracker->NotifyEvent("esb_download_promo_row_viewed");
base::UmaHistogramEnumeration("SafeBrowsing.EsbDownloadRowPromo.Outcome",
SafeBrowsingEsbDownloadRowPromoOutcome::kShown);
}
#else
void DownloadsDOMHandler::OpenEsbSettings() { … }
void DownloadsDOMHandler::IsEligibleForEsbPromo(
IsEligibleForEsbPromoCallback callback) { … }
void DownloadsDOMHandler::LogEsbPromotionRowViewed() { … }
#endif
content::DownloadManager* DownloadsDOMHandler::GetMainNotifierManager() const { … }
content::DownloadManager* DownloadsDOMHandler::GetOriginalNotifierManager()
const { … }
void DownloadsDOMHandler::FinalizeRemovals() { … }
void DownloadsDOMHandler::MaybeTriggerDownloadWarningHatsSurvey(
download::DownloadItem* item,
DownloadWarningHatsType survey_type) { … }
void DownloadsDOMHandler::OnDownloadsPageDismissed() { … }
bool DownloadsDOMHandler::IsDeletingHistoryAllowed() { … }
download::DownloadItem* DownloadsDOMHandler::GetDownloadByStringId(
const std::string& id) { … }
download::DownloadItem* DownloadsDOMHandler::GetDownloadById(uint32_t id) { … }
content::WebContents* DownloadsDOMHandler::GetWebUIWebContents() { … }
void DownloadsDOMHandler::CheckForRemovedFiles() { … }
void DownloadsDOMHandler::RemoveDownloadInArgs(const std::string& id) { … }