#include "chrome/browser/ui/webui/new_tab_page/new_tab_page_handler.h"
#include <algorithm>
#include <iterator>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "base/base64.h"
#include "base/containers/contains.h"
#include "base/containers/fixed_flat_map.h"
#include "base/containers/fixed_flat_set.h"
#include "base/containers/flat_map.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/i18n/rtl.h"
#include "base/json/json_reader.h"
#include "base/json/json_string_value_serializer.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/ranges/algorithm.h"
#include "base/strings/strcat.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/new_tab_page/feature_promo_helper/new_tab_page_feature_promo_helper.h"
#include "chrome/browser/new_tab_page/modules/new_tab_page_modules.h"
#include "chrome/browser/new_tab_page/promos/promo_service_factory.h"
#include "chrome/browser/optimization_guide/optimization_guide_keyed_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/background/ntp_background_service.h"
#include "chrome/browser/search/background/ntp_background_service_factory.h"
#include "chrome/browser/search/background/ntp_custom_background_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/themes/custom_theme_supplier.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/chrome_select_file_policy.h"
#include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/hats/hats_service.h"
#include "chrome/browser/ui/hats/hats_service_factory.h"
#include "chrome/browser/ui/views/side_panel/customize_chrome/customize_chrome_utils.h"
#include "chrome/browser/ui/views/side_panel/customize_chrome/side_panel_controller_views.h"
#include "chrome/browser/ui/webui/new_tab_page/ntp_pref_names.h"
#include "chrome/browser/ui/webui/side_panel/customize_chrome/customize_chrome_section.h"
#include "chrome/browser/ui/webui/webui_util_desktop.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/theme_resources.h"
#include "components/feature_engagement/public/event_constants.h"
#include "components/feature_engagement/public/feature_constants.h"
#include "components/keyed_service/core/service_access_type.h"
#include "components/omnibox/browser/omnibox.mojom.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/search/ntp_features.h"
#include "components/search_engines/template_url_service.h"
#include "components/search_provider_logos/logo_service.h"
#include "components/search_provider_logos/switches.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/theme_provider.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/color_utils.h"
#include "ui/native_theme/native_theme.h"
#include "ui/shell_dialogs/selected_file_info.h"
namespace {
const int64_t kMaxDownloadBytes = …;
constexpr char kDisableInteraction[] = …;
constexpr char kDismissInteraction[] = …;
constexpr char kIgnoreInteraction[] = …;
constexpr char kUseInteraction[] = …;
constexpr auto kModuleInteractionNames = …;
std::vector<std::string> GetSurveyEligibleModuleIds() { … }
bool ShouldForceDarkForegroundColorsForLogo(const ThemeService* theme_service) { … }
new_tab_page::mojom::ThemePtr MakeTheme(
const ui::ColorProvider& color_provider,
const ui::ThemeProvider* theme_provider,
ThemeService* theme_service,
NtpCustomBackgroundService* ntp_custom_background_service,
content::WebContents* web_contents) { … }
SkColor ParseHexColor(const std::string& color) { … }
new_tab_page::mojom::ImageDoodlePtr MakeImageDoodle(
search_provider_logos::LogoType type,
const std::string& data,
const std::string& mime_type,
const GURL& animated_url,
int width_px,
int height_px,
const std::string& background_color,
GURL log_url,
GURL cta_log_url) { … }
new_tab_page::mojom::PromoPtr MakePromo(const PromoData& data) { … }
base::Value::Dict MakeModuleInteractionTriggerIdDictionary() { … }
}
const char NewTabPageHandler::kModuleDismissedHistogram[] = …;
const char NewTabPageHandler::kModuleRestoredHistogram[] = …;
NewTabPageHandler::NewTabPageHandler(
mojo::PendingReceiver<new_tab_page::mojom::PageHandler>
pending_page_handler,
mojo::PendingRemote<new_tab_page::mojom::Page> pending_page,
Profile* profile,
NtpCustomBackgroundService* ntp_custom_background_service,
ThemeService* theme_service,
search_provider_logos::LogoService* logo_service,
content::WebContents* web_contents,
std::unique_ptr<NewTabPageFeaturePromoHelper>
customize_chrome_feature_promo_helper,
const base::Time& ntp_navigation_start_time,
const std::vector<std::pair<const std::string, int>>* module_id_names,
customize_chrome::SidePanelController*
customize_chrome_side_panel_controller)
: … { … }
NewTabPageHandler::~NewTabPageHandler() { … }
void NewTabPageHandler::TabWillDelete() { … }
void NewTabPageHandler::RegisterProfilePrefs(PrefRegistrySimple* registry) { … }
void NewTabPageHandler::SetMostVisitedSettings(bool custom_links_enabled,
bool visible) { … }
void NewTabPageHandler::GetMostVisitedSettings(
GetMostVisitedSettingsCallback callback) { … }
void NewTabPageHandler::SetBackgroundImage(const std::string& attribution_1,
const std::string& attribution_2,
const GURL& attribution_url,
const GURL& image_url,
const GURL& thumbnail_url,
const std::string& collection_id) { … }
void NewTabPageHandler::SetDailyRefreshCollectionId(
const std::string& collection_id) { … }
void NewTabPageHandler::SetNoBackgroundImage() { … }
void NewTabPageHandler::RevertBackgroundChanges() { … }
void NewTabPageHandler::ConfirmBackgroundChanges() { … }
void NewTabPageHandler::GetBackgroundCollections(
GetBackgroundCollectionsCallback callback) { … }
void NewTabPageHandler::GetBackgroundImages(
const std::string& collection_id,
GetBackgroundImagesCallback callback) { … }
void NewTabPageHandler::GetDoodle(GetDoodleCallback callback) { … }
void NewTabPageHandler::ChooseLocalCustomBackground(
ChooseLocalCustomBackgroundCallback callback) { … }
void NewTabPageHandler::UpdatePromoData() { … }
void NewTabPageHandler::BlocklistPromo(const std::string& promo_id) { … }
void NewTabPageHandler::UndoBlocklistPromo(const std::string& promo_id) { … }
void NewTabPageHandler::OnDismissModule(const std::string& module_id) { … }
void NewTabPageHandler::OnRestoreModule(const std::string& module_id) { … }
void NewTabPageHandler::SetModulesVisible(bool visible) { … }
void NewTabPageHandler::SetModuleDisabled(const std::string& module_id,
bool disabled) { … }
void NewTabPageHandler::UpdateDisabledModules() { … }
void NewTabPageHandler::OnModulesLoadedWithData(
const std::vector<std::string>& module_ids) { … }
void NewTabPageHandler::OnModuleUsed(const std::string& module_id) { … }
void NewTabPageHandler::GetModulesIdNames(GetModulesIdNamesCallback callback) { … }
void NewTabPageHandler::SetModulesOrder(
const std::vector<std::string>& module_ids) { … }
void NewTabPageHandler::GetModulesOrder(GetModulesOrderCallback callback) { … }
void NewTabPageHandler::SetCustomizeChromeSidePanelVisible(
bool visible,
new_tab_page::mojom::CustomizeChromeSection section_mojo) { … }
void NewTabPageHandler::IncrementCustomizeChromeButtonOpenCount() { … }
void NewTabPageHandler::MaybeShowFeaturePromo(
new_tab_page::mojom::IphFeature iph_feature) { … }
void NewTabPageHandler::IncrementWallpaperSearchButtonShownCount() { … }
void NewTabPageHandler::OnAppRendered(double time) { … }
void NewTabPageHandler::OnOneGoogleBarRendered(double time) { … }
void NewTabPageHandler::OnPromoRendered(double time,
const std::optional<GURL>& log_url) { … }
void NewTabPageHandler::OnCustomizeDialogAction(
new_tab_page::mojom::CustomizeDialogAction action) { … }
void NewTabPageHandler::OnDoodleImageClicked(
new_tab_page::mojom::DoodleImageType type,
const std::optional<::GURL>& log_url) { … }
void NewTabPageHandler::OnDoodleImageRendered(
new_tab_page::mojom::DoodleImageType type,
double time,
const GURL& log_url,
OnDoodleImageRenderedCallback callback) { … }
void NewTabPageHandler::OnDoodleShared(
new_tab_page::mojom::DoodleShareChannel channel,
const std::string& doodle_id,
const std::optional<std::string>& share_id) { … }
void NewTabPageHandler::OnPromoLinkClicked() { … }
void NewTabPageHandler::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) { … }
void NewTabPageHandler::OnThemeChanged() { … }
void NewTabPageHandler::OnCustomBackgroundImageUpdated() { … }
void NewTabPageHandler::OnCollectionInfoAvailable() { … }
void NewTabPageHandler::OnCollectionImagesAvailable() { … }
void NewTabPageHandler::OnNextCollectionImageAvailable() { … }
void NewTabPageHandler::OnNtpBackgroundServiceShuttingDown() { … }
void NewTabPageHandler::OnPromoDataUpdated() { … }
void NewTabPageHandler::OnPromoServiceShuttingDown() { … }
void NewTabPageHandler::OnChangeInFeatureCurrentlyEnabledState(
bool is_now_enabled) { … }
void NewTabPageHandler::FileSelected(const ui::SelectedFileInfo& file,
int index) { … }
void NewTabPageHandler::FileSelectionCanceled() { … }
void NewTabPageHandler::OnLogoAvailable(
GetDoodleCallback callback,
search_provider_logos::LogoCallbackReason type,
const std::optional<search_provider_logos::EncodedLogo>& logo) { … }
void NewTabPageHandler::LogEvent(NTPLoggingEventType event) { … }
void NewTabPageHandler::Fetch(const GURL& url,
OnFetchResultCallback on_result) { … }
void NewTabPageHandler::OnFetchResult(const network::SimpleURLLoader* loader,
OnFetchResultCallback on_result,
std::unique_ptr<std::string> body) { … }
void NewTabPageHandler::OnLogFetchResult(OnDoodleImageRenderedCallback callback,
bool success,
std::unique_ptr<std::string> body) { … }
bool NewTabPageHandler::IsCustomLinksEnabled() const { … }
bool NewTabPageHandler::IsShortcutsVisible() const { … }
void NewTabPageHandler::NotifyCustomizeChromeSidePanelVisibilityChanged(
bool is_open) { … }
void NewTabPageHandler::MaybeLaunchInteractionSurvey(
std::string_view interaction,
const std::string& module_id,
int delay_time_ms) { … }
void NewTabPageHandler::MaybeShowWebstoreToast() { … }
void NewTabPageHandler::IncrementDictPrefKeyCount(const std::string& pref_name,
const std::string& key) { … }
const std::string& NewTabPageHandler::GetSurveyTriggerIdForModuleAndInteraction(
std::string_view interaction,
const std::string& module_id) { … }