230 lines
8.9 KiB
C++
230 lines
8.9 KiB
C++
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#include "brave/browser/brave_tab_helpers.h"
|
|
|
|
#include <memory>
|
|
#include <utility>
|
|
|
|
#include "base/command_line.h"
|
|
#include "base/feature_list.h"
|
|
#include "base/functional/bind.h"
|
|
#include "base/functional/callback_helpers.h"
|
|
#include "brave/browser/ai_chat/ai_chat_service_factory.h"
|
|
#include "brave/browser/ai_chat/ai_chat_utils.h"
|
|
#include "brave/browser/brave_ads/creatives/search_result_ad/creative_search_result_ad_tab_helper.h"
|
|
#include "brave/browser/brave_ads/tabs/ads_tab_helper.h"
|
|
#include "brave/browser/brave_browser_process.h"
|
|
#include "brave/browser/brave_news/brave_news_tab_helper.h"
|
|
#include "brave/browser/brave_rewards/rewards_tab_helper.h"
|
|
#include "brave/browser/brave_shields/brave_shields_web_contents_observer.h"
|
|
#include "brave/browser/brave_wallet/brave_wallet_tab_helper.h"
|
|
#include "brave/browser/ephemeral_storage/ephemeral_storage_tab_helper.h"
|
|
#include "brave/browser/misc_metrics/page_metrics_tab_helper.h"
|
|
#include "brave/browser/misc_metrics/process_misc_metrics.h"
|
|
#include "brave/browser/ntp_background/ntp_tab_helper.h"
|
|
#include "brave/browser/playlist/playlist_service_factory.h"
|
|
#include "brave/browser/ui/brave_ui_features.h"
|
|
#include "brave/components/ai_chat/content/browser/ai_chat_tab_helper.h"
|
|
#include "brave/components/brave_perf_predictor/browser/perf_predictor_tab_helper.h"
|
|
#include "brave/components/brave_wayback_machine/buildflags/buildflags.h"
|
|
#include "brave/components/playlist/content/browser/playlist_tab_helper.h"
|
|
#include "brave/components/playlist/core/common/features.h"
|
|
#include "brave/components/request_otr/common/buildflags/buildflags.h"
|
|
#include "brave/components/speedreader/common/buildflags/buildflags.h"
|
|
#include "brave/components/tor/buildflags/buildflags.h"
|
|
#include "brave/components/web_discovery/buildflags/buildflags.h"
|
|
#include "build/build_config.h"
|
|
#include "chrome/browser/browser_process.h"
|
|
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
|
|
#include "chrome/browser/profiles/profile.h"
|
|
#include "chrome/common/chrome_isolated_world_ids.h"
|
|
#include "components/user_prefs/user_prefs.h"
|
|
#include "content/public/browser/browser_context.h"
|
|
#include "content/public/browser/web_contents.h"
|
|
#include "extensions/buildflags/buildflags.h"
|
|
#include "net/base/features.h"
|
|
#include "printing/buildflags/buildflags.h"
|
|
#include "third_party/widevine/cdm/buildflags.h"
|
|
|
|
#if BUILDFLAG(IS_ANDROID)
|
|
#include "brave/browser/android/youtube_script_injector/youtube_script_injector_tab_helper.h"
|
|
#endif
|
|
|
|
#if !BUILDFLAG(IS_ANDROID)
|
|
#include "brave/browser/brave_shields/brave_shields_tab_helper.h"
|
|
#include "brave/browser/ui/geolocation/brave_geolocation_permission_tab_helper.h"
|
|
#include "chrome/browser/ui/thumbnails/thumbnail_tab_helper.h"
|
|
#endif
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
#include "brave/browser/new_tab/background_color_tab_helper.h"
|
|
#endif
|
|
|
|
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
|
|
#include "brave/browser/ai_chat/print_preview_extractor.h"
|
|
#include "brave/browser/ai_chat/print_preview_extractor_internal.h"
|
|
#include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
|
|
#endif // BUILDFLAG(ENABLE_PRINT_PREVIEW)
|
|
|
|
#if BUILDFLAG(ENABLE_WIDEVINE)
|
|
#include "brave/browser/brave_drm_tab_helper.h"
|
|
#endif
|
|
|
|
#if BUILDFLAG(ENABLE_BRAVE_WAYBACK_MACHINE)
|
|
#include "brave/components/brave_wayback_machine/brave_wayback_machine_tab_helper.h"
|
|
#endif
|
|
|
|
#if BUILDFLAG(ENABLE_SPEEDREADER)
|
|
#include "brave/browser/ui/speedreader/speedreader_tab_helper.h"
|
|
#endif
|
|
|
|
#if BUILDFLAG(ENABLE_TOR)
|
|
#include "brave/components/tor/onion_location_tab_helper.h"
|
|
#include "brave/components/tor/tor_tab_helper.h"
|
|
#endif
|
|
|
|
#if BUILDFLAG(ENABLE_EXTENSIONS) || BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE)
|
|
#include "brave/browser/web_discovery/web_discovery_tab_helper.h"
|
|
#endif
|
|
|
|
#if BUILDFLAG(ENABLE_REQUEST_OTR)
|
|
#include "brave/browser/request_otr/request_otr_tab_helper.h"
|
|
#include "brave/components/request_otr/common/features.h"
|
|
#endif
|
|
|
|
#if defined(TOOLKIT_VIEWS)
|
|
#include "brave/browser/onboarding/onboarding_tab_helper.h"
|
|
#include "brave/browser/ui/sidebar/sidebar_tab_helper.h"
|
|
#endif
|
|
|
|
namespace brave {
|
|
|
|
void AttachTabHelpers(content::WebContents* web_contents) {
|
|
// If your TabHelper is related to privacy consider whether it belongs in this
|
|
// method, so it can also be attached to background WebContents.
|
|
AttachPrivacySensitiveTabHelpers(web_contents);
|
|
|
|
#if BUILDFLAG(IS_ANDROID)
|
|
YouTubeScriptInjectorTabHelper::CreateForWebContents(web_contents);
|
|
#else
|
|
// Add tab helpers here unless they are intended for android too
|
|
brave_shields::BraveShieldsTabHelper::CreateForWebContents(web_contents);
|
|
ThumbnailTabHelper::CreateForWebContents(web_contents);
|
|
BraveGeolocationPermissionTabHelper::CreateForWebContents(web_contents);
|
|
#endif
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
if (base::FeatureList::IsEnabled(features::kBraveWorkaroundNewWindowFlash)) {
|
|
BackgroundColorTabHelper::CreateForWebContents(web_contents);
|
|
}
|
|
#endif
|
|
|
|
brave_rewards::RewardsTabHelper::CreateForWebContents(web_contents);
|
|
|
|
content::BrowserContext* context = web_contents->GetBrowserContext();
|
|
if (ai_chat::IsAllowedForContext(context)) {
|
|
ai_chat::AIChatTabHelper::CreateForWebContents(
|
|
web_contents,
|
|
#if BUILDFLAG(ENABLE_PRINT_PREVIEW)
|
|
std::make_unique<ai_chat::PrintPreviewExtractor>(
|
|
web_contents,
|
|
base::BindRepeating(
|
|
[](content::WebContents* web_contents, bool is_pdf,
|
|
ai_chat::PrintPreviewExtractor::Extractor::ImageCallback&&
|
|
callback)
|
|
-> std::unique_ptr<
|
|
ai_chat::PrintPreviewExtractor::Extractor> {
|
|
return std::make_unique<
|
|
ai_chat::PrintPreviewExtractorInternal>(
|
|
web_contents,
|
|
Profile::FromBrowserContext(
|
|
web_contents->GetBrowserContext()),
|
|
is_pdf, std::move(callback),
|
|
base::BindRepeating(
|
|
[]() -> base::IDMap<
|
|
printing::mojom::PrintPreviewUI*>& {
|
|
return printing::PrintPreviewUI::
|
|
GetPrintPreviewUIIdMap();
|
|
}),
|
|
base::BindRepeating([]() -> base::flat_map<int, int>& {
|
|
return printing::PrintPreviewUI::
|
|
GetPrintPreviewUIRequestIdMap();
|
|
}));
|
|
}))
|
|
#else
|
|
nullptr
|
|
#endif
|
|
);
|
|
}
|
|
|
|
#if BUILDFLAG(ENABLE_WIDEVINE)
|
|
BraveDrmTabHelper::CreateForWebContents(web_contents);
|
|
#endif
|
|
|
|
#if BUILDFLAG(ENABLE_BRAVE_WAYBACK_MACHINE)
|
|
BraveWaybackMachineTabHelper::CreateIfNeeded(web_contents);
|
|
#endif
|
|
|
|
brave_perf_predictor::PerfPredictorTabHelper::CreateForWebContents(
|
|
web_contents);
|
|
|
|
brave_ads::AdsTabHelper::CreateForWebContents(web_contents);
|
|
brave_ads::CreativeSearchResultAdTabHelper::MaybeCreateForWebContents(
|
|
web_contents);
|
|
#if BUILDFLAG(ENABLE_EXTENSIONS) || BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE)
|
|
web_discovery::WebDiscoveryTabHelper::MaybeCreateForWebContents(web_contents);
|
|
#endif
|
|
|
|
#if BUILDFLAG(ENABLE_SPEEDREADER)
|
|
speedreader::SpeedreaderTabHelper::MaybeCreateForWebContents(web_contents);
|
|
#endif
|
|
|
|
#if BUILDFLAG(ENABLE_TOR)
|
|
tor::TorTabHelper::MaybeCreateForWebContents(web_contents);
|
|
tor::OnionLocationTabHelper::CreateForWebContents(web_contents);
|
|
#endif
|
|
|
|
BraveNewsTabHelper::MaybeCreateForWebContents(web_contents);
|
|
|
|
#if defined(TOOLKIT_VIEWS)
|
|
OnboardingTabHelper::MaybeCreateForWebContents(web_contents);
|
|
sidebar::SidebarTabHelper::MaybeCreateForWebContents(web_contents);
|
|
#endif
|
|
|
|
brave_wallet::BraveWalletTabHelper::CreateForWebContents(web_contents);
|
|
|
|
if (!web_contents->GetBrowserContext()->IsOffTheRecord()) {
|
|
ntp_background_images::NTPTabHelper::CreateForWebContents(web_contents);
|
|
misc_metrics::PageMetricsTabHelper::CreateForWebContents(web_contents);
|
|
#if BUILDFLAG(ENABLE_REQUEST_OTR)
|
|
if (base::FeatureList::IsEnabled(
|
|
request_otr::features::kBraveRequestOTRTab)) {
|
|
RequestOTRTabHelper::CreateForWebContents(web_contents);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
if (base::FeatureList::IsEnabled(playlist::features::kPlaylist)) {
|
|
if (auto* playlist_service =
|
|
playlist::PlaylistServiceFactory::GetForBrowserContext(
|
|
web_contents->GetBrowserContext())) {
|
|
playlist::PlaylistTabHelper::CreateForWebContents(web_contents,
|
|
playlist_service);
|
|
}
|
|
}
|
|
}
|
|
|
|
void AttachPrivacySensitiveTabHelpers(content::WebContents* web_contents) {
|
|
brave_shields::BraveShieldsWebContentsObserver::CreateForWebContents(
|
|
web_contents);
|
|
if (base::FeatureList::IsEnabled(net::features::kBraveEphemeralStorage)) {
|
|
ephemeral_storage::EphemeralStorageTabHelper::CreateForWebContents(
|
|
web_contents);
|
|
}
|
|
}
|
|
|
|
} // namespace brave
|