diff --git a/app/brave_generated_resources.grd b/app/brave_generated_resources.grd index 2946b909f97a..d4f954d22cbc 100644 --- a/app/brave_generated_resources.grd +++ b/app/brave_generated_resources.grd @@ -506,28 +506,6 @@ Or change later at $2brave://settings/ext - - - - Want to make Brave Search even better? - - - Opt in to the Web Discovery Project to anonymously contribute some search & browsing data, and improve Brave Search results for all. Privacy-preserving. And zero effort. $1Learn more - - - Learn more - - - Don't ask again - - - Turn on - - - No thanks - - - @@ -601,6 +579,22 @@ Or change later at $2brave://settings/ext Dismiss Brave search conversion, press Enter to remove this suggestion + + + Help improve $1Brave Search by sending anonymous usage data. $2More info + + + Brave Search + + + More info + + + OK + + + No thanks + diff --git a/app/theme/brave_theme_resources.grd b/app/theme/brave_theme_resources.grd index 610b7ca63461..72d1fc645725 100644 --- a/app/theme/brave_theme_resources.grd +++ b/app/theme/brave_theme_resources.grd @@ -97,9 +97,11 @@ + + + - diff --git a/app/theme/default_100_percent/brave/brave_search_logo_in_web_discovery_dialog.png b/app/theme/default_100_percent/brave/brave_search_logo_in_web_discovery_dialog.png deleted file mode 100644 index 8f78b6f4cd0a..000000000000 Binary files a/app/theme/default_100_percent/brave/brave_search_logo_in_web_discovery_dialog.png and /dev/null differ diff --git a/app/theme/default_100_percent/brave/brave_web_discovery_infobar_bg_graphic.png b/app/theme/default_100_percent/brave/brave_web_discovery_infobar_bg_graphic.png new file mode 100644 index 000000000000..aac403cf3fb9 Binary files /dev/null and b/app/theme/default_100_percent/brave/brave_web_discovery_infobar_bg_graphic.png differ diff --git a/app/theme/default_100_percent/brave/brave_web_discovery_infobar_bg_graphic_dark.png b/app/theme/default_100_percent/brave/brave_web_discovery_infobar_bg_graphic_dark.png new file mode 100644 index 000000000000..0e1862c48990 Binary files /dev/null and b/app/theme/default_100_percent/brave/brave_web_discovery_infobar_bg_graphic_dark.png differ diff --git a/app/theme/default_100_percent/brave/brave_web_discovery_infobar_icon.png b/app/theme/default_100_percent/brave/brave_web_discovery_infobar_icon.png new file mode 100644 index 000000000000..9f87d2ba98d0 Binary files /dev/null and b/app/theme/default_100_percent/brave/brave_web_discovery_infobar_icon.png differ diff --git a/app/theme/default_200_percent/brave/brave_search_logo_in_web_discovery_dialog.png b/app/theme/default_200_percent/brave/brave_search_logo_in_web_discovery_dialog.png deleted file mode 100644 index 019cd3bda789..000000000000 Binary files a/app/theme/default_200_percent/brave/brave_search_logo_in_web_discovery_dialog.png and /dev/null differ diff --git a/app/theme/default_200_percent/brave/brave_web_discovery_infobar_bg_graphic.png b/app/theme/default_200_percent/brave/brave_web_discovery_infobar_bg_graphic.png new file mode 100644 index 000000000000..bc09685e1480 Binary files /dev/null and b/app/theme/default_200_percent/brave/brave_web_discovery_infobar_bg_graphic.png differ diff --git a/app/theme/default_200_percent/brave/brave_web_discovery_infobar_bg_graphic_dark.png b/app/theme/default_200_percent/brave/brave_web_discovery_infobar_bg_graphic_dark.png new file mode 100644 index 000000000000..12488227853b Binary files /dev/null and b/app/theme/default_200_percent/brave/brave_web_discovery_infobar_bg_graphic_dark.png differ diff --git a/app/theme/default_200_percent/brave/brave_web_discovery_infobar_icon.png b/app/theme/default_200_percent/brave/brave_web_discovery_infobar_icon.png new file mode 100644 index 000000000000..0320e3df7e0a Binary files /dev/null and b/app/theme/default_200_percent/brave/brave_web_discovery_infobar_icon.png differ diff --git a/app/vector_icons/BUILD.gn b/app/vector_icons/BUILD.gn index 53883c24a92d..17a969e908df 100644 --- a/app/vector_icons/BUILD.gn +++ b/app/vector_icons/BUILD.gn @@ -29,6 +29,7 @@ aggregate_vector_icons("brave_vector_icons") { "sidebar_toolbar_button_right.icon", "vpn_indicator.icon", "wallet_toolbar_button.icon", + "web_discovery_infobar_close_button.icon", ] if (enable_sidebar) { diff --git a/app/vector_icons/web_discovery_infobar_close_button.icon b/app/vector_icons/web_discovery_infobar_close_button.icon new file mode 100644 index 000000000000..b2a9e1659e7b --- /dev/null +++ b/app/vector_icons/web_discovery_infobar_close_button.icon @@ -0,0 +1,21 @@ +// Copyright (c) 2022 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/. + +CANVAS_DIMENSIONS, 20, +MOVE_TO, 4.99f, 4.99f, +R_ARC_TO, 0.71f, 0.71f, 0, 0, 0, 0, 1, +LINE_TO, 9, 10, +LINE_TO, 4.99f, 14.01f, +R_ARC_TO, 0.71f, 0.71f, 0, 1, 0, 1, 1, +LINE_TO, 10, 11, +R_LINE_TO, 4.01f, 4.01f, +R_ARC_TO, 0.71f, 0.71f, 0, 0, 0, 1, -1, +LINE_TO, 11, 10, +R_LINE_TO, 4.01f, -4.01f, +R_ARC_TO, 0.71f, 0.71f, 0, 1, 0, -1, -1, +LINE_TO, 10, 9, +LINE_TO, 6, 4.99f, +R_ARC_TO, 0.71f, 0.71f, 0, 0, 0, -1, 0, +CLOSE diff --git a/browser/brave_profile_prefs.cc b/browser/brave_profile_prefs.cc index 5f18fbc00660..4eef7fb45c63 100644 --- a/browser/brave_profile_prefs.cc +++ b/browser/brave_profile_prefs.cc @@ -176,6 +176,12 @@ void RegisterProfilePrefsForMigration( // Added 01/2022 registry->RegisterBooleanPref(brave_rewards::prefs::kHideButton, false); + +#if BUILDFLAG(ENABLE_EXTENSIONS) + // Added 11/2022 + registry->RegisterBooleanPref(kDontAskEnableWebDiscovery, false); + registry->RegisterIntegerPref(kBraveSearchVisitCount, 0); +#endif } void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { @@ -393,8 +399,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { #if BUILDFLAG(ENABLE_EXTENSIONS) // Web discovery extension, default false registry->RegisterBooleanPref(kWebDiscoveryEnabled, false); - registry->RegisterBooleanPref(kDontAskEnableWebDiscovery, false); - registry->RegisterIntegerPref(kBraveSearchVisitCount, 0); + registry->RegisterDictionaryPref(kWebDiscoveryCTAState); #endif #if BUILDFLAG(ENABLE_SPEEDREADER) diff --git a/browser/ui/BUILD.gn b/browser/ui/BUILD.gn index 04b07ea2cc73..b68292042f44 100644 --- a/browser/ui/BUILD.gn +++ b/browser/ui/BUILD.gn @@ -280,8 +280,6 @@ source_set("ui") { "views/toolbar/bookmark_button.h", "views/toolbar/brave_app_menu.cc", "views/toolbar/brave_app_menu.h", - "views/web_discovery_dialog_view.cc", - "views/web_discovery_dialog_view.h", "views/window_closing_confirm_dialog_view.cc", "views/window_closing_confirm_dialog_view.h", ] diff --git a/browser/ui/browser_dialogs.h b/browser/ui/browser_dialogs.h index 779bdb20a82c..566d3a9cd92e 100644 --- a/browser/ui/browser_dialogs.h +++ b/browser/ui/browser_dialogs.h @@ -8,15 +8,10 @@ class Browser; -namespace content { -class WebContents; -} // namespace content - namespace brave { // Tab restore dialog will be launched after ask dialog is closed. void ShowCrashReportPermissionAskDialog(Browser* browser); -void ShowWebDiscoveryDialog(Browser* browser, content::WebContents* contents); } // namespace brave diff --git a/browser/ui/color/brave_color_id.h b/browser/ui/color/brave_color_id.h index 5ce09b476bc5..e4ad845e73dd 100644 --- a/browser/ui/color/brave_color_id.h +++ b/browser/ui/color/brave_color_id.h @@ -21,6 +21,11 @@ E_CPONLY(kColorLocationBarFocusRing) \ E_CPONLY(kColorDialogDontAskAgainButton) \ E_CPONLY(kColorDialogDontAskAgainButtonHovered) \ + E_CPONLY(kColorWebDiscoveryInfoBarBackground) \ + E_CPONLY(kColorWebDiscoveryInfoBarMessage) \ + E_CPONLY(kColorWebDiscoveryInfoBarLink) \ + E_CPONLY(kColorWebDiscoveryInfoBarNoThanks) \ + E_CPONLY(kColorWebDiscoveryInfoBarClose) #define BRAVE_SEARCH_CONVERSION_COLOR_IDS \ E_CPONLY(kColorSearchConversionBannerTypeBackgroundBorder) \ diff --git a/browser/ui/color/brave_color_mixer.cc b/browser/ui/color/brave_color_mixer.cc index 6dbed9147bb8..646224d31f7f 100644 --- a/browser/ui/color/brave_color_mixer.cc +++ b/browser/ui/color/brave_color_mixer.cc @@ -268,6 +268,13 @@ void AddBraveLightThemeColorMixer(ui::ColorProvider* provider, SkColorSetRGB(0xF0, 0xF2, 0xFF))}; #endif + mixer[kColorWebDiscoveryInfoBarBackground] = { + SkColorSetRGB(0xFF, 0xFF, 0xFF)}; + mixer[kColorWebDiscoveryInfoBarMessage] = {SkColorSetRGB(0x1D, 0x1F, 0x25)}; + mixer[kColorWebDiscoveryInfoBarLink] = {SkColorSetRGB(0x4C, 0x54, 0xD2)}; + mixer[kColorWebDiscoveryInfoBarNoThanks] = {SkColorSetRGB(0x6B, 0x70, 0x84)}; + mixer[kColorWebDiscoveryInfoBarClose] = {SkColorSetRGB(0x6B, 0x70, 0x84)}; + // Colors for HelpBubble. IDs are defined in // chrome/browser/ui/color/chrome_color_id.h mixer[kColorFeaturePromoBubbleBackground] = {SK_ColorWHITE}; @@ -353,6 +360,15 @@ void AddBraveDarkThemeColorMixer(ui::ColorProvider* provider, SkColorSetRGB(0xF0, 0xF2, 0xFF))}; #endif + mixer[kColorWebDiscoveryInfoBarBackground] = { + SkColorSetRGB(0x1A, 0x1C, 0x22)}; + mixer[kColorWebDiscoveryInfoBarMessage] = {SkColorSetRGB(0xFF, 0xFF, 0xFF)}; + mixer[kColorWebDiscoveryInfoBarLink] = {SkColorSetRGB(0xA6, 0xAB, 0xEC)}; + mixer[kColorWebDiscoveryInfoBarNoThanks] = { + SkColorSetARGB(0xBF, 0xEC, 0xEF, 0xF2)}; + mixer[kColorWebDiscoveryInfoBarClose] = { + SkColorSetARGB(0xBF, 0x8C, 0x90, 0xA1)}; + // Colors for HelpBubble. IDs are defined in // chrome/browser/ui/color/chrome_color_id.h mixer[kColorFeaturePromoBubbleBackground] = {SkColorSetRGB(0x12, 0x13, 0x16)}; diff --git a/browser/ui/views/infobars/BUILD.gn b/browser/ui/views/infobars/BUILD.gn index 462780243d05..82c6cbedf8d8 100644 --- a/browser/ui/views/infobars/BUILD.gn +++ b/browser/ui/views/infobars/BUILD.gn @@ -2,6 +2,8 @@ source_set("brave_wayback_machine") { # Remove when https://github.com/brave/brave-browser/issues/10652 is resolved check_includes = false sources = [ + "brave_infobar_container_view.cc", + "brave_infobar_container_view.h", "brave_wayback_machine_infobar_button_container.cc", "brave_wayback_machine_infobar_button_container.h", "brave_wayback_machine_infobar_contents_view.cc", @@ -10,6 +12,12 @@ source_set("brave_wayback_machine") { "brave_wayback_machine_infobar_throbber.h", "brave_wayback_machine_infobar_view.cc", "brave_wayback_machine_infobar_view.h", + "custom_styled_label.cc", + "custom_styled_label.h", + "web_discovery_infobar_content_view.cc", + "web_discovery_infobar_content_view.h", + "web_discovery_infobar_view.cc", + "web_discovery_infobar_view.h", ] deps = [ diff --git a/browser/ui/views/infobars/brave_infobar_container_view.cc b/browser/ui/views/infobars/brave_infobar_container_view.cc new file mode 100644 index 000000000000..10c7606373dd --- /dev/null +++ b/browser/ui/views/infobars/brave_infobar_container_view.cc @@ -0,0 +1,19 @@ +/* Copyright (c) 2022 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/ui/views/infobars/brave_infobar_container_view.h" + +#include + +BraveInfoBarContainerView::BraveInfoBarContainerView( + infobars::InfoBarContainer::Delegate* delegate) + : InfoBarContainerView(delegate) { + // To hide shadow, replace it with empty view. + DCHECK(content_shadow_); + RemoveChildViewT(content_shadow_); + content_shadow_ = AddChildView(std::make_unique()); +} + +BraveInfoBarContainerView::~BraveInfoBarContainerView() = default; diff --git a/browser/ui/views/infobars/brave_infobar_container_view.h b/browser/ui/views/infobars/brave_infobar_container_view.h new file mode 100644 index 000000000000..45a53299c631 --- /dev/null +++ b/browser/ui/views/infobars/brave_infobar_container_view.h @@ -0,0 +1,21 @@ +/* Copyright (c) 2022 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 "chrome/browser/ui/views/infobars/infobar_container_view.h" + +#ifndef BRAVE_BROWSER_UI_VIEWS_INFOBARS_BRAVE_INFOBAR_CONTAINER_VIEW_H_ +#define BRAVE_BROWSER_UI_VIEWS_INFOBARS_BRAVE_INFOBAR_CONTAINER_VIEW_H_ + +class BraveInfoBarContainerView : public InfoBarContainerView { + public: + explicit BraveInfoBarContainerView( + infobars::InfoBarContainer::Delegate* delegate); + BraveInfoBarContainerView(const BraveInfoBarContainerView&) = delete; + BraveInfoBarContainerView& operator=(const BraveInfoBarContainerView&) = + delete; + ~BraveInfoBarContainerView() override; +}; + +#endif // BRAVE_BROWSER_UI_VIEWS_INFOBARS_BRAVE_INFOBAR_CONTAINER_VIEW_H_ diff --git a/browser/ui/views/infobars/custom_styled_label.cc b/browser/ui/views/infobars/custom_styled_label.cc new file mode 100644 index 000000000000..11e1af8b00c3 --- /dev/null +++ b/browser/ui/views/infobars/custom_styled_label.cc @@ -0,0 +1,49 @@ +/* Copyright (c) 2022 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/ui/views/infobars/custom_styled_label.h" + +#include + +#include "ui/base/metadata/metadata_impl_macros.h" +#include "ui/views/controls/link_fragment.h" + +CustomStyledLabel::~CustomStyledLabel() = default; + +std::unique_ptr CustomStyledLabel::CreateLabel( + const std::u16string& text, + const RangeStyleInfo& style_info, + const gfx::Range& range, + views::LinkFragment** previous_link_fragment) const { + if (style_info.text_style != views::style::STYLE_LINK) + return StyledLabel::CreateLabel(text, style_info, range, + previous_link_fragment); + + std::unique_ptr result; + auto link = std::make_unique( + text, text_context_, views::style::STYLE_LINK, *previous_link_fragment); + *previous_link_fragment = link.get(); + link->SetCallback(style_info.callback); + link->SetFontList(style_info.custom_font.value()); + if (!style_info.accessible_name.empty()) + link->SetAccessibleName(style_info.accessible_name); + + result = std::move(link); + + if (style_info.override_color) + result->SetEnabledColor(style_info.override_color.value()); + if (!style_info.tooltip.empty()) + result->SetTooltipText(style_info.tooltip); + if (!style_info.accessible_name.empty()) + result->SetAccessibleName(style_info.accessible_name); + if (displayed_on_background_color_) + result->SetBackgroundColor(displayed_on_background_color_.value()); + result->SetAutoColorReadabilityEnabled(auto_color_readability_enabled_); + result->SetSubpixelRenderingEnabled(subpixel_rendering_enabled_); + return result; +} + +BEGIN_METADATA(CustomStyledLabel, views::StyledLabel) +END_METADATA diff --git a/browser/ui/views/infobars/custom_styled_label.h b/browser/ui/views/infobars/custom_styled_label.h new file mode 100644 index 000000000000..63533cbe28fa --- /dev/null +++ b/browser/ui/views/infobars/custom_styled_label.h @@ -0,0 +1,35 @@ +/* Copyright (c) 2022 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/. */ + +#ifndef BRAVE_BROWSER_UI_VIEWS_INFOBARS_CUSTOM_STYLED_LABEL_H_ +#define BRAVE_BROWSER_UI_VIEWS_INFOBARS_CUSTOM_STYLED_LABEL_H_ + +#include +#include + +#include "ui/base/metadata/metadata_header_macros.h" +#include "ui/views/controls/styled_label.h" + +// TODO(simonhong): Move common place if needed. +// StyledLabel doesn't allow using custom font for link style. +// Use for setting custom font for link. +class CustomStyledLabel : public views::StyledLabel { + public: + METADATA_HEADER(CustomStyledLabel); + using StyledLabel::StyledLabel; + CustomStyledLabel(const CustomStyledLabel&) = delete; + CustomStyledLabel& operator=(const CustomStyledLabel&) = delete; + ~CustomStyledLabel() override; + + private: + // views::StyledLabel overrides: + std::unique_ptr CreateLabel( + const std::u16string& text, + const RangeStyleInfo& style_info, + const gfx::Range& range, + views::LinkFragment** previous_link_component) const override; +}; + +#endif // BRAVE_BROWSER_UI_VIEWS_INFOBARS_CUSTOM_STYLED_LABEL_H_ diff --git a/browser/ui/views/infobars/web_discovery_infobar_content_view.cc b/browser/ui/views/infobars/web_discovery_infobar_content_view.cc new file mode 100644 index 000000000000..47b2c644bae6 --- /dev/null +++ b/browser/ui/views/infobars/web_discovery_infobar_content_view.cc @@ -0,0 +1,502 @@ +/* Copyright (c) 2022 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/ui/views/infobars/web_discovery_infobar_content_view.h" + +#include +#include +#include + +#include "brave/app/vector_icons/vector_icons.h" +#include "brave/browser/ui/color/brave_color_id.h" +#include "brave/browser/ui/views/infobars/custom_styled_label.h" +#include "brave/browser/web_discovery/web_discovery_infobar_delegate.h" +#include "brave/components/constants/url_constants.h" +#include "brave/grit/brave_generated_resources.h" +#include "brave/grit/brave_theme_resources.h" +#include "chrome/browser/ui/browser_list.h" +#include "chrome/browser/ui/singleton_tabs.h" +#include "third_party/abseil-cpp/absl/types/optional.h" +#include "ui/base/l10n/l10n_util.h" +#include "ui/base/metadata/metadata_impl_macros.h" +#include "ui/base/resource/resource_bundle.h" +#include "ui/color/color_provider.h" +#include "ui/gfx/canvas.h" +#include "ui/gfx/font_list.h" +#include "ui/gfx/geometry/rect.h" +#include "ui/gfx/geometry/skia_conversions.h" +#include "ui/gfx/image/image_skia_operations.h" +#include "ui/gfx/paint_vector_icon.h" +#include "ui/native_theme/native_theme.h" +#include "ui/views/background.h" +#include "ui/views/border.h" +#include "ui/views/controls/button/image_button.h" +#include "ui/views/controls/button/label_button.h" +#include "ui/views/controls/button/md_text_button.h" +#include "ui/views/controls/image_view.h" +#include "ui/views/controls/styled_label.h" +#include "ui/views/layout/box_layout.h" +#include "ui/views/layout/fill_layout.h" +#include "ui/views/layout/flex_layout.h" + +namespace { + +using ButtonState = views::Button::ButtonState; + +constexpr int kWideLayoutHorizontalPadding = 67; +constexpr int kNarrowLayoutHorizontalPadding = 10; + +void OpenMoreInfoPage() { + if (auto* browser = BrowserList::GetInstance()->GetLastActive()) + ShowSingletonTab(browser, GURL(kWebDiscoveryLearnMoreUrl)); +} + +// Customize min size as half size of preferred. +// Re-calculated preferred size as it doesn't give proper +// size when enlarged. +class InfoBarStyledLabel : public CustomStyledLabel { + public: + METADATA_HEADER(InfoBarStyledLabel); + using CustomStyledLabel::CustomStyledLabel; + InfoBarStyledLabel(const InfoBarStyledLabel&) = delete; + InfoBarStyledLabel& operator=(const InfoBarStyledLabel&) = delete; + ~InfoBarStyledLabel() override = default; + + private: + // CustomStyledLabel overrides: + gfx::Size GetMinimumSize() const override { + const auto pref_size = GetPreferredSize(); + return gfx::Size(pref_size.width() * 0.55, pref_size.height()); + } + + gfx::Size CalculatePreferredSize() const override { + // Reset message label's width so that it can calculate preferred size + // ignoring the current size. This will allow the label to grow bigger than + // it is. + // https://github.com/chromium/chromium/blob/366e028e485ffd5de42a2f5a898a5eda1edbbc08/ui/views/controls/styled_label.cc#L228 + return GetLayoutSizeInfoForWidth(std::numeric_limits::max()) + .total_size; + } + + void OnBoundsChanged(const gfx::Rect& previous_bounds) override { + auto height = GetHeightForWidth(width()); + SetSize({width(), height}); + SetPosition({x(), (parent()->height() - height) / 2}); + } +}; + +BEGIN_METADATA(InfoBarStyledLabel, CustomStyledLabel) +END_METADATA + +// TODO(simonhong): Use leo MdTextButton when it's stabilized. +class OkButton : public views::LabelButton { + public: + METADATA_HEADER(OkButton); + explicit OkButton(PressedCallback callback, const std::u16string& text) + : LabelButton(std::move(callback), text) { + SetHorizontalAlignment(gfx::ALIGN_CENTER); + SetEnabledTextColors(SK_ColorWHITE); + SetTextColor(ButtonState::STATE_DISABLED, SK_ColorWHITE); + } + + OkButton(const OkButton&) = delete; + OkButton& operator=(const OkButton&) = delete; + + void UpdateBackgroundColor() override { + constexpr SkColor kBgColor[][ButtonState::STATE_COUNT] = { + { + // Light theme. + SkColorSetRGB(0x4E, 0x32, 0xEE), // normal + SkColorSetRGB(0x32, 0x2F, 0xB4), // hover + SkColorSetRGB(0x4E, 0x32, 0xEE), // focused + SkColorSetRGB(0xAC, 0xAF, 0xBB) // disabled + }, + { + // Dark theme. + SkColorSetRGB(0x4E, 0x32, 0xEE), // normal + SkColorSetRGB(0x87, 0x84, 0xF4), // hover + SkColorSetRGB(0x4E, 0x32, 0xEE), // focused + SkColorSetRGB(0x58, 0x5C, 0x6D), // disabled + }}; + + const int theme = + ui::NativeTheme::GetInstanceForNativeUi()->ShouldUseDarkColors(); + SetBackground(CreateBackgroundFromPainter( + views::Painter::CreateRoundRectWith1PxBorderPainter( + kBgColor[theme][GetVisualState()], SK_ColorTRANSPARENT, 100))); + } + + void OnThemeChanged() override { + LabelButton::OnThemeChanged(); + UpdateBackgroundColor(); + } +}; + +BEGIN_METADATA(OkButton, views::LabelButton) +END_METADATA + +// Subclassed for font setting. +class NoThanksButton : public views::LabelButton { + public: + METADATA_HEADER(NoThanksButton); + using views::LabelButton::LabelButton; + NoThanksButton(const NoThanksButton&) = delete; + NoThanksButton& operator=(const NoThanksButton&) = delete; + ~NoThanksButton() override = default; + + void SetFontList(const gfx::FontList& font_list) { + label()->SetFontList(font_list); + } +}; + +BEGIN_METADATA(NoThanksButton, views::LabelButton) +END_METADATA + +// Use image as background. +class WebDiscoveryInfoBarContentViewBackground : public views::Background { + public: + using Background::Background; + ~WebDiscoveryInfoBarContentViewBackground() override = default; + WebDiscoveryInfoBarContentViewBackground( + const WebDiscoveryInfoBarContentViewBackground&) = delete; + WebDiscoveryInfoBarContentViewBackground& operator=( + const WebDiscoveryInfoBarContentViewBackground&) = delete; + + void Paint(gfx::Canvas* canvas, views::View* view) const override { + ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); + canvas->DrawImageInt( + gfx::ImageSkiaOperations::CreateResizedImage( + *rb.GetImageSkiaNamed( + ui::NativeTheme::GetInstanceForNativeUi()->ShouldUseDarkColors() + ? IDR_BRAVE_WEB_DISCOVERY_INFOBAR_BG_GRAPHIC_DARK + : IDR_BRAVE_WEB_DISCOVERY_INFOBAR_BG_GRAPHIC), + skia::ImageOperations::RESIZE_BEST, view->size()), + 0, 0); + } +}; + +} // namespace + +WebDiscoveryInfoBarContentView::WebDiscoveryInfoBarContentView( + WebDiscoveryInfoBarDelegate* delegate) + : delegate_(delegate) { + SetBackground(std::make_unique()); +} + +WebDiscoveryInfoBarContentView::~WebDiscoveryInfoBarContentView() = default; + +void WebDiscoveryInfoBarContentView::OnPaint(gfx::Canvas* canvas) { + SkPath mask; + mask.addRoundRect(gfx::RectToSkRect(GetContentsBounds()), 16, 16); + canvas->ClipPath(mask, true); + View::OnPaint(canvas); +} + +void WebDiscoveryInfoBarContentView::OnThemeChanged() { + View::OnThemeChanged(); + + // Re-initialze whenever theme changed because it's complicate to change the + // StyledLabel's text color. + InitChildren(); + SwitchChildLayout(); +} + +void WebDiscoveryInfoBarContentView::AddedToWidget() { + // When active tab is changed to others, infobar is also hidden. + // And it's added to widget again when brave search tab is activated. + // When activated, infobar should be switched to proper layout again. + SwitchChildLayout(); +} + +void WebDiscoveryInfoBarContentView::OnBoundsChanged( + const gfx::Rect& previous_bounds) { + SwitchChildLayout(); +} + +void WebDiscoveryInfoBarContentView::SwitchChildLayout() { + // Not initialized yet. + if (wide_layout_min_width_ == 0 || narrow_layout_preferred_width_ == 0) + return; + + // There are three layout. + // - Wide layout with wide border + // - Wide layout with narrow border + // - Narrow layout with narrow border + const bool needs_wide_layout = (width() > narrow_layout_preferred_width_); + const bool needs_small_padding_with_wide_layout = + (narrow_layout_preferred_width_ < width() && + width() <= wide_layout_min_width_); + if (needs_wide_layout) { + SetBorder(views::CreateEmptyBorder( + gfx::Insets::VH(10, needs_small_padding_with_wide_layout + ? 10 + : kWideLayoutHorizontalPadding))); + } else { + SetBorder(views::CreateEmptyBorder( + gfx::Insets::VH(8, kNarrowLayoutHorizontalPadding))); + } + + wide_layout_container_->SetVisible(needs_wide_layout); + wide_layout_container_->SetBoundsRect(GetContentsBounds()); + narrow_layout_container_->SetVisible(!needs_wide_layout); + narrow_layout_container_->SetBoundsRect(GetContentsBounds()); + + constexpr int kWideLayoutHeight = 84; + constexpr int kNarrowLayoutHeight = 151; + SetPreferredSize(gfx::Size( + width(), needs_wide_layout ? kWideLayoutHeight : kNarrowLayoutHeight)); +} + +void WebDiscoveryInfoBarContentView::InitChildren() { + RemoveAllChildViews(); + + InitChildrenForWideLayout(); + InitChildrenForNarrowLayout(); + wide_layout_container_->SetVisible(false); + narrow_layout_container_->SetVisible(false); +} + +// Locate all children in one horizontal layout. +void WebDiscoveryInfoBarContentView::InitChildrenForWideLayout() { + const int infobar_content_height = GetContentsBounds().height(); + + wide_layout_container_ = AddChildView(std::make_unique()); + wide_layout_container_ + ->SetLayoutManager(std::make_unique()) + ->SetOrientation(views::LayoutOrientation::kHorizontal) + .SetCrossAxisAlignment(views::LayoutAlignment::kCenter); + + wide_layout_container_->AddChildView( + GetSpacer({40, infobar_content_height}, 2)); + wide_layout_container_->AddChildView( + GetSpacer({197, infobar_content_height}, 4, + views::MinimumFlexSizeRule::kScaleToZero)); + wide_layout_container_->AddChildView(GetIcon(1)); + wide_layout_container_->AddChildView( + GetSpacer({24, infobar_content_height}, 2)); + wide_layout_container_->AddChildView(GetMessage(3, 24)); + wide_layout_container_->AddChildView( + GetSpacer({40, infobar_content_height}, 4, + views::MinimumFlexSizeRule::kScaleToZero)); + wide_layout_container_->AddChildView(GetOkButton({101, 38}, 3)); + wide_layout_container_->AddChildView( + GetSpacer({}, 4, views::MinimumFlexSizeRule::kScaleToZero, + views::MaximumFlexSizeRule::kUnbounded)); + wide_layout_container_->AddChildView(GetNoThanksButton(3)); + wide_layout_container_->AddChildView(GetCloseButton()); + wide_layout_container_->AddChildView( + GetSpacer({40, infobar_content_height}, 2)); + + wide_layout_min_width_ = wide_layout_container_->GetMinimumSize().width() + + kWideLayoutHorizontalPadding * 2; +} + +void WebDiscoveryInfoBarContentView::InitChildrenForNarrowLayout() { + // |narrow_layout_container_| has two parts - contents and + // close button horizontally. + narrow_layout_container_ = AddChildView(std::make_unique()); + narrow_layout_container_ + ->SetLayoutManager(std::make_unique()) + ->SetOrientation(views::LayoutOrientation::kHorizontal) + .SetCrossAxisAlignment(views::LayoutAlignment::kStart); + + // |center_contents| has two parts vertically - message and button rows. + auto* contents = + narrow_layout_container_->AddChildView(std::make_unique()); + contents->SetProperty( + views::kFlexBehaviorKey, + views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToMinimum, + views::MaximumFlexSizeRule::kUnbounded) + .WithOrder(2)); + contents->SetLayoutManager(std::make_unique()) + ->SetOrientation(views::LayoutOrientation::kVertical) + .SetCrossAxisAlignment(views::LayoutAlignment::kStart); + + contents->AddChildView(GetSpacer({10, 18}, 1)); + + auto* message_row = contents->AddChildView(std::make_unique()); + message_row->SetLayoutManager(std::make_unique()) + ->SetOrientation(views::LayoutOrientation::kHorizontal); + message_row->SetProperty( + views::kFlexBehaviorKey, + views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToMinimum, + views::MaximumFlexSizeRule::kUnbounded) + .WithOrder(2)); + + message_row->AddChildView(GetSpacer({22, 20}, 1)); + message_row->AddChildView(GetIcon(1)); + message_row->AddChildView(GetSpacer({18, 20}, 1)); + message_row->AddChildView(GetMessage(3, 22)); + + // Space between message and buttons rows. + contents->AddChildView(GetSpacer({10, 14}, 1)); + + auto* buttons_row = contents->AddChildView(std::make_unique()); + buttons_row->SetProperty( + views::kFlexBehaviorKey, + views::FlexSpecification(views::MinimumFlexSizeRule::kPreferred, + views::MaximumFlexSizeRule::kUnbounded) + .WithOrder(3)); + + buttons_row->SetLayoutManager(std::make_unique()) + ->SetOrientation(views::LayoutOrientation::kHorizontal) + .SetCrossAxisAlignment(views::LayoutAlignment::kCenter); + + buttons_row->AddChildView(GetSpacer({60, 20}, 1)); + buttons_row->AddChildView(GetNoThanksButton(1)); + buttons_row->AddChildView(GetSpacer({40, 38}, 2, + views::MinimumFlexSizeRule::kScaleToZero, + views::MaximumFlexSizeRule::kUnbounded)); + buttons_row->AddChildView(GetOkButton({196, 38}, 1)); + contents->AddChildView(GetSpacer({10, 18}, 1)); + + auto* close = narrow_layout_container_->AddChildView(GetCloseButton()); + close->SetProperty(views::kMarginsKey, gfx::Insets::TLBR(8, 20, 105, 10)); + + narrow_layout_preferred_width_ = + narrow_layout_container_->GetPreferredSize().width() + + kNarrowLayoutHorizontalPadding * 2; +} + +std::unique_ptr WebDiscoveryInfoBarContentView::GetMessage( + int order, + int line_height) { + const std::u16string brave_search_text = + l10n_util::GetStringUTF16(IDS_WEB_DISCOVERY_INFOBAR_MESSAGE_BRAVE_SEARCH); + const std::u16string more_info_text = + l10n_util::GetStringUTF16(IDS_WEB_DISCOVERY_INFOBAR_MESSAGE_MORE_INFO); + std::vector offsets; + const std::u16string message_text = + l10n_util::GetStringFUTF16(IDS_WEB_DISCOVERY_INFOBAR_MESSAGE, + brave_search_text, more_info_text, &offsets); + auto message_label = std::make_unique(); + message_label->SetLineHeight(line_height); + message_label->SetText(message_text); + + views::StyledLabel::RangeStyleInfo brave_search_style; + brave_search_style.custom_font = gfx::FontList("Poppins, Semi-Bold 14px"); + brave_search_style.override_color = + GetColorProvider()->GetColor(kColorWebDiscoveryInfoBarMessage); + message_label->AddStyleRange( + gfx::Range(offsets[0], offsets[0] + brave_search_text.length()), + brave_search_style); + + views::StyledLabel::RangeStyleInfo more_info_style = + views::StyledLabel::RangeStyleInfo::CreateForLink( + base::BindRepeating(OpenMoreInfoPage)); + more_info_style.custom_font = gfx::FontList("Poppins, Normal 14px"); + more_info_style.override_color = + GetColorProvider()->GetColor(kColorWebDiscoveryInfoBarLink); + message_label->AddStyleRange( + gfx::Range(offsets[1], offsets[1] + more_info_text.length()), + more_info_style); + + views::StyledLabel::RangeStyleInfo default_style; + default_style.custom_font = gfx::FontList("Poppins, Normal 14px"); + default_style.override_color = + GetColorProvider()->GetColor(kColorWebDiscoveryInfoBarMessage); + message_label->AddStyleRange(gfx::Range(0, offsets[0]), default_style); + message_label->AddStyleRange( + gfx::Range(offsets[0] + brave_search_text.length(), + message_text.length() - more_info_text.length()), + default_style); + message_label->SetProperty( + views::kFlexBehaviorKey, + views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToMinimum, + views::MaximumFlexSizeRule::kPreferred) + .WithOrder(order)); + return message_label; +} + +std::unique_ptr WebDiscoveryInfoBarContentView::GetIcon( + int order) { + ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); + auto icon = std::make_unique(ui::ImageModel::FromImageSkia( + *rb.GetImageSkiaNamed(IDR_BRAVE_WEB_DISCOVERY_INFOBAR_ICON))); + icon->SetProperty( + views::kFlexBehaviorKey, + views::FlexSpecification(views::MinimumFlexSizeRule::kPreferred, + views::MaximumFlexSizeRule::kPreferred) + .WithOrder(order)); + return icon; +} + +std::unique_ptr WebDiscoveryInfoBarContentView::GetNoThanksButton( + int order) { + auto no_thanks = std::make_unique( + base::BindRepeating(&WebDiscoveryInfoBarContentView::Dismiss, + base::Unretained(this)), + l10n_util::GetStringUTF16(IDS_WEB_DISCOVERY_INFOBAR_NO_THANKS_LABEL)); + no_thanks->SetFontList(gfx::FontList("Poppins, Semi-Bold 12px")); + no_thanks->SetEnabledTextColors( + GetColorProvider()->GetColor(kColorWebDiscoveryInfoBarNoThanks)); + no_thanks->SetProperty(views::kMarginsKey, gfx::Insets::TLBR(0, 0, 0, 16)); + no_thanks->SetProperty( + views::kFlexBehaviorKey, + views::FlexSpecification(views::MinimumFlexSizeRule::kPreferred, + views::MaximumFlexSizeRule::kPreferred) + .WithOrder(order)); + return no_thanks; +} + +std::unique_ptr WebDiscoveryInfoBarContentView::GetOkButton( + const gfx::Size& size, + int order) { + auto ok_button = std::make_unique( + base::BindRepeating(&WebDiscoveryInfoBarContentView::EnableWebDiscovery, + base::Unretained(this)), + l10n_util::GetStringUTF16(IDS_WEB_DISCOVERY_INFOBAR_OK_BUTTON_LABEL)); + ok_button->SetPreferredSize(size); + ok_button->SetProperty(views::kMarginsKey, gfx::Insets::TLBR(0, 16, 0, 16)); + ok_button->SetProperty( + views::kFlexBehaviorKey, + views::FlexSpecification(views::MinimumFlexSizeRule::kPreferred, + views::MaximumFlexSizeRule::kPreferred) + .WithOrder(order)); + return ok_button; +} + +std::unique_ptr WebDiscoveryInfoBarContentView::GetCloseButton() { + auto close_button = std::make_unique(base::BindRepeating( + &WebDiscoveryInfoBarContentView::CloseInfoBar, base::Unretained(this))); + close_button->SetImage( + views::Button::STATE_NORMAL, + gfx::CreateVectorIcon( + kWebDiscoveryInfobarCloseButtonIcon, + GetColorProvider()->GetColor(kColorWebDiscoveryInfoBarClose))); + close_button->SetProperty( + views::kFlexBehaviorKey, + views::FlexSpecification(views::MinimumFlexSizeRule::kPreferred, + views::MaximumFlexSizeRule::kPreferred)); + return close_button; +} + +std::unique_ptr WebDiscoveryInfoBarContentView::GetSpacer( + const gfx::Size& size, + int order, + views::MinimumFlexSizeRule min_rule, + views::MaximumFlexSizeRule max_rule) { + auto spacer = std::make_unique(); + spacer->SetPreferredSize(size); + spacer->SetProperty( + views::kFlexBehaviorKey, + views::FlexSpecification(min_rule, max_rule).WithOrder(order)); + return spacer; +} + +void WebDiscoveryInfoBarContentView::EnableWebDiscovery() { + delegate_->EnableWebDiscovery(); +} + +void WebDiscoveryInfoBarContentView::Dismiss() { + delegate_->Close(true); +} + +void WebDiscoveryInfoBarContentView::CloseInfoBar() { + delegate_->Close(false); +} + +BEGIN_METADATA(WebDiscoveryInfoBarContentView, views::View) +END_METADATA diff --git a/browser/ui/views/infobars/web_discovery_infobar_content_view.h b/browser/ui/views/infobars/web_discovery_infobar_content_view.h new file mode 100644 index 000000000000..23430c37bcac --- /dev/null +++ b/browser/ui/views/infobars/web_discovery_infobar_content_view.h @@ -0,0 +1,66 @@ +/* Copyright (c) 2022 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/. */ + +#ifndef BRAVE_BROWSER_UI_VIEWS_INFOBARS_WEB_DISCOVERY_INFOBAR_CONTENT_VIEW_H_ +#define BRAVE_BROWSER_UI_VIEWS_INFOBARS_WEB_DISCOVERY_INFOBAR_CONTENT_VIEW_H_ + +#include + +#include "base/memory/raw_ptr.h" +#include "ui/base/metadata/metadata_header_macros.h" +#include "ui/views/layout/flex_layout_types.h" +#include "ui/views/view.h" + +class WebDiscoveryInfoBarDelegate; + +// Occupy whole infobar area. +class WebDiscoveryInfoBarContentView : public views::View { + public: + METADATA_HEADER(WebDiscoveryInfoBarContentView); + + explicit WebDiscoveryInfoBarContentView( + WebDiscoveryInfoBarDelegate* delegate); + WebDiscoveryInfoBarContentView(const WebDiscoveryInfoBarContentView&) = + delete; + WebDiscoveryInfoBarContentView& operator=( + const WebDiscoveryInfoBarContentView&) = delete; + ~WebDiscoveryInfoBarContentView() override; + + private: + // views::View overrides: + void OnPaint(gfx::Canvas* canvas) override; + void OnThemeChanged() override; + void AddedToWidget() override; + void OnBoundsChanged(const gfx::Rect& previous_bounds) override; + + void SwitchChildLayout(); + void InitChildren(); + void InitChildrenForWideLayout(); + void InitChildrenForNarrowLayout(); + std::unique_ptr GetMessage(int order, int line_height); + std::unique_ptr GetIcon(int order); + std::unique_ptr GetNoThanksButton(int order); + std::unique_ptr GetOkButton(const gfx::Size& size, int order); + std::unique_ptr GetCloseButton(); + std::unique_ptr GetSpacer( + const gfx::Size& size, + int order = 1, + views::MinimumFlexSizeRule min_rule = + views::MinimumFlexSizeRule::kPreferred, + views::MaximumFlexSizeRule max_rule = + views::MaximumFlexSizeRule::kPreferred); + + void EnableWebDiscovery(); + void Dismiss(); + void CloseInfoBar(); + + raw_ptr wide_layout_container_ = nullptr; + raw_ptr narrow_layout_container_ = nullptr; + int wide_layout_min_width_ = 0; + int narrow_layout_preferred_width_ = 0; + raw_ptr delegate_ = nullptr; +}; + +#endif // BRAVE_BROWSER_UI_VIEWS_INFOBARS_WEB_DISCOVERY_INFOBAR_CONTENT_VIEW_H_ diff --git a/browser/ui/views/infobars/web_discovery_infobar_view.cc b/browser/ui/views/infobars/web_discovery_infobar_view.cc new file mode 100644 index 000000000000..7bd6082e5401 --- /dev/null +++ b/browser/ui/views/infobars/web_discovery_infobar_view.cc @@ -0,0 +1,50 @@ +/* Copyright (c) 2022 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/ui/views/infobars/web_discovery_infobar_view.h" + +#include + +#include "brave/browser/ui/views/infobars/web_discovery_infobar_content_view.h" +#include "brave/browser/web_discovery/web_discovery_infobar_delegate.h" +#include "ui/base/metadata/metadata_impl_macros.h" +#include "ui/views/widget/widget.h" + +// web_discovery_tab_helper.cc has decls. +std::unique_ptr CreateWebDiscoveryInfoBar( + std::unique_ptr delegate) { + return std::make_unique(std::move(delegate)); +} + +WebDiscoveryInfoBarView::WebDiscoveryInfoBarView( + std::unique_ptr delegate) + : InfoBarView(std::move(delegate)) { + content_view_ = AddChildView( + std::make_unique(GetDelegate())); +} + +WebDiscoveryInfoBarView::~WebDiscoveryInfoBarView() = default; + +WebDiscoveryInfoBarDelegate* WebDiscoveryInfoBarView::GetDelegate() { + return static_cast(delegate()); +} + +void WebDiscoveryInfoBarView::Layout() { + // Don't need to layout base class's elements as our |content_view_| covers + // all area except bottom separator. + content_view_->SetBounds(0, OffsetY(content_view_), width(), + content_view_->height()); +} + +void WebDiscoveryInfoBarView::ChildPreferredSizeChanged(views::View* child) { + if (child == content_view_) { + child->SizeToPreferredSize(); + RecalculateHeight(); + InvalidateLayout(); + } +} + +BEGIN_METADATA(WebDiscoveryInfoBarView, views::View) +END_METADATA diff --git a/browser/ui/views/infobars/web_discovery_infobar_view.h b/browser/ui/views/infobars/web_discovery_infobar_view.h new file mode 100644 index 000000000000..512dcee37300 --- /dev/null +++ b/browser/ui/views/infobars/web_discovery_infobar_view.h @@ -0,0 +1,38 @@ +/* Copyright (c) 2022 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/. */ + +#ifndef BRAVE_BROWSER_UI_VIEWS_INFOBARS_WEB_DISCOVERY_INFOBAR_VIEW_H_ +#define BRAVE_BROWSER_UI_VIEWS_INFOBARS_WEB_DISCOVERY_INFOBAR_VIEW_H_ + +#include + +#include "base/memory/raw_ptr.h" +#include "chrome/browser/ui/views/infobars/infobar_view.h" +#include "ui/base/metadata/metadata_header_macros.h" + +class WebDiscoveryInfoBarDelegate; + +class WebDiscoveryInfoBarView : public InfoBarView { + public: + METADATA_HEADER(WebDiscoveryInfoBarView); + + explicit WebDiscoveryInfoBarView( + std::unique_ptr delegate); + ~WebDiscoveryInfoBarView() override; + + WebDiscoveryInfoBarView(const WebDiscoveryInfoBarView&) = delete; + WebDiscoveryInfoBarView& operator=(const WebDiscoveryInfoBarView&) = delete; + + private: + // InfoBarView overrides: + void Layout() override; + void ChildPreferredSizeChanged(views::View* child) override; + + WebDiscoveryInfoBarDelegate* GetDelegate(); + + raw_ptr content_view_ = nullptr; +}; + +#endif // BRAVE_BROWSER_UI_VIEWS_INFOBARS_WEB_DISCOVERY_INFOBAR_VIEW_H_ diff --git a/browser/ui/views/web_discovery_dialog_view.cc b/browser/ui/views/web_discovery_dialog_view.cc deleted file mode 100644 index 238a09691162..000000000000 --- a/browser/ui/views/web_discovery_dialog_view.cc +++ /dev/null @@ -1,186 +0,0 @@ -/* Copyright (c) 2021 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/ui/views/web_discovery_dialog_view.h" - -#include - -#include "base/bind.h" -#include "brave/browser/ui/browser_dialogs.h" -#include "brave/components/constants/pref_names.h" -#include "brave/components/constants/url_constants.h" -#include "brave/components/l10n/common/localization_util.h" -#include "brave/grit/brave_generated_resources.h" -#include "brave/grit/brave_theme_resources.h" -#include "chrome/browser/browser_process.h" -#include "chrome/browser/shell_integration.h" -#include "chrome/browser/ui/browser.h" -#include "chrome/browser/ui/browser_tabstrip.h" -#include "chrome/browser/ui/browser_window.h" -#include "chrome/browser/ui/tabs/tab_strip_model.h" -#include "components/constrained_window/constrained_window_views.h" -#include "components/prefs/pref_service.h" -#include "components/user_prefs/user_prefs.h" -#include "content/public/browser/browser_context.h" -#include "content/public/browser/web_contents.h" -#include "ui/base/l10n/l10n_util.h" -#include "ui/base/metadata/metadata_impl_macros.h" -#include "ui/base/resource/resource_bundle.h" -#include "ui/views/controls/button/checkbox.h" -#include "ui/views/controls/label.h" -#include "ui/views/controls/styled_label.h" -#include "ui/views/layout/box_layout.h" -#include "ui/views/layout/flex_layout.h" -#include "ui/views/layout/layout_provider.h" -#include "ui/views/window/dialog_client_view.h" -#include "url/gurl.h" - -namespace brave { - -void ShowWebDiscoveryDialog(Browser* browser, content::WebContents* contents) { - auto* prefs = user_prefs::UserPrefs::Get(contents->GetBrowserContext()); - constrained_window::ShowWebModalDialogViews( - new WebDiscoveryDialogView(browser, prefs), contents) - ->Show(); -} - -} // namespace brave - -// For vertically aligned dialog buttons. -class WebDiscoveryDialogClientView : public views::DialogClientView { - public: - METADATA_HEADER(WebDiscoveryDialogClientView); - using DialogClientView::DialogClientView; - - WebDiscoveryDialogClientView(const WebDiscoveryDialogClientView&) = delete; - WebDiscoveryDialogClientView& operator=(const WebDiscoveryDialogClientView&) = - delete; - - // views::DialogClientView overrides: - void SetupLayout() override { - views::DialogClientView::SetupLayout(); - - SetupButtonsLayoutVertically(); - } -}; - -BEGIN_METADATA(WebDiscoveryDialogClientView, views::DialogClientView) -END_METADATA - -WebDiscoveryDialogView::WebDiscoveryDialogView(Browser* browser, - PrefService* prefs) - : browser_(browser), prefs_(prefs) { - set_should_ignore_snapping(true); - SetModalType(ui::MODAL_TYPE_CHILD); - SetButtonLabel(ui::DIALOG_BUTTON_OK, - brave_l10n::GetLocalizedResourceUTF16String( - IDS_WEB_DISCOVERY_DIALOG_OK_BUTTON_TEXT)); - SetButtonLabel(ui::DIALOG_BUTTON_CANCEL, - brave_l10n::GetLocalizedResourceUTF16String( - IDS_WEB_DISCOVERY_DIALOG_CANCEL_BUTTON_TEXT)); - - SetAcceptCallback(base::BindOnce( - &WebDiscoveryDialogView::OnAcceptButtonClicked, base::Unretained(this))); - RegisterWindowClosingCallback(base::BindOnce( - &WebDiscoveryDialogView::OnWindowClosing, base::Unretained(this))); - - CreateChildViews(); -} - -WebDiscoveryDialogView::~WebDiscoveryDialogView() = default; - -void WebDiscoveryDialogView::CreateChildViews() { - SetLayoutManager(std::make_unique()) - ->SetOrientation(views::LayoutOrientation::kVertical) - .SetMainAxisAlignment(views::LayoutAlignment::kStart) - .SetInteriorMargin(gfx::Insets::TLBR(0, 36, 24, 36)); - - auto& bundle = ui::ResourceBundle::GetSharedInstance(); - AddChildView(std::make_unique( - ui::ImageModel::FromImageSkia(*bundle.GetImageSkiaNamed( - IDR_BRAVE_SEARCH_LOGO_IN_WEB_DISCOVERY_DIALOG)))); - - // Use 15px font size for header text. - int size_diff = 15 - views::Label::GetDefaultFontList().GetFontSize(); - views::Label::CustomFont header_font = { - views::Label::GetDefaultFontList() - .DeriveWithSizeDelta(size_diff) - .DeriveWithWeight(gfx::Font::Weight::SEMIBOLD)}; - auto* header_label = AddChildView(std::make_unique( - brave_l10n::GetLocalizedResourceUTF16String( - IDS_WEB_DISCOVERY_DIALOG_HEADER_TEXT), - header_font)); - header_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); - header_label->SetProperty(views::kMarginsKey, gfx::Insets::TLBR(22, 0, 0, 0)); - - size_t learn_more_offset; - const std::u16string learn_more_text = - brave_l10n::GetLocalizedResourceUTF16String( - IDS_WEB_DISCOVERY_DIALOG_CONTENTS_TEXT_LEARN_MORE_PART); - const std::u16string contents_text = - l10n_util::GetStringFUTF16(IDS_WEB_DISCOVERY_DIALOG_CONTENTS_TEXT, - learn_more_text, &learn_more_offset); - - auto* contents_label = AddChildView(std::make_unique()); - contents_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); - contents_label->SetText(contents_text); - contents_label->SetProperty(views::kMarginsKey, - gfx::Insets::TLBR(8, 0, 25, 0)); - - // Apply link style to learn more link text. - views::StyledLabel::RangeStyleInfo learn_more_style = - views::StyledLabel::RangeStyleInfo::CreateForLink(base::BindRepeating( - &WebDiscoveryDialogView::OnLearnMoreClicked, base::Unretained(this))); - contents_label->AddStyleRange( - gfx::Range(learn_more_offset, - learn_more_offset + learn_more_text.length()), - learn_more_style); - - // Use 14px font size for contents text. - views::StyledLabel::RangeStyleInfo contents_default_style; - size_diff = 14 - views::Label::GetDefaultFontList().GetFontSize(); - contents_default_style.custom_font = - views::Label::GetDefaultFontList() - .DeriveWithSizeDelta(size_diff) - .DeriveWithWeight(gfx::Font::Weight::NORMAL); - - contents_label->AddStyleRange(gfx::Range(0, learn_more_offset), - contents_default_style); - contents_label->SizeToFit(360); - - dont_ask_again_checkbox_ = AddChildView(std::make_unique( - brave_l10n::GetLocalizedResourceUTF16String( - IDS_WEB_DISCOVERY_DIALOG_DONT_ASK_AGAIN_TEXT))); -} - -views::ClientView* WebDiscoveryDialogView::CreateClientView( - views::Widget* widget) { - return new WebDiscoveryDialogClientView(widget, - TransferOwnershipOfContentsView()); -} - -bool WebDiscoveryDialogView::ShouldShowCloseButton() const { - return true; -} - -void WebDiscoveryDialogView::OnAcceptButtonClicked() { - prefs_->SetBoolean(kWebDiscoveryEnabled, true); -} - -void WebDiscoveryDialogView::OnWindowClosing() { - prefs_->SetBoolean(kDontAskEnableWebDiscovery, - dont_ask_again_checkbox_->GetChecked()); -} - -void WebDiscoveryDialogView::OnLearnMoreClicked() { - auto* tab_model = browser_->tab_strip_model(); - const int active_index = tab_model->active_index(); - // Open link right next to the current tab. - chrome::AddTabAt(browser_, GURL(kWebDiscoveryLearnMoreUrl), active_index + 1, - true); -} - -BEGIN_METADATA(WebDiscoveryDialogView, views::DialogDelegateView) -END_METADATA diff --git a/browser/ui/views/web_discovery_dialog_view.h b/browser/ui/views/web_discovery_dialog_view.h deleted file mode 100644 index afd774718ef9..000000000000 --- a/browser/ui/views/web_discovery_dialog_view.h +++ /dev/null @@ -1,46 +0,0 @@ -/* Copyright (c) 2021 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/. */ - -#ifndef BRAVE_BROWSER_UI_VIEWS_WEB_DISCOVERY_DIALOG_VIEW_H_ -#define BRAVE_BROWSER_UI_VIEWS_WEB_DISCOVERY_DIALOG_VIEW_H_ - -#include - -#include "ui/base/metadata/metadata_header_macros.h" -#include "ui/views/window/dialog_delegate.h" - -namespace views { -class Checkbox; -} // namespace views - -class Browser; -class PrefService; - -class WebDiscoveryDialogView : public views::DialogDelegateView { - public: - METADATA_HEADER(WebDiscoveryDialogView); - - WebDiscoveryDialogView(Browser* browser, PrefService* prefs); - ~WebDiscoveryDialogView() override; - - WebDiscoveryDialogView(const WebDiscoveryDialogView&) = delete; - WebDiscoveryDialogView& operator=(const WebDiscoveryDialogView&) = delete; - - // views::DialogDelegateView overrides: - views::ClientView* CreateClientView(views::Widget* widget) override; - bool ShouldShowCloseButton() const override; - - private: - void CreateChildViews(); - void OnAcceptButtonClicked(); - void OnWindowClosing(); - void OnLearnMoreClicked(); - - views::Checkbox* dont_ask_again_checkbox_ = nullptr; - Browser* browser_ = nullptr; - PrefService* prefs_ = nullptr; -}; - -#endif // BRAVE_BROWSER_UI_VIEWS_WEB_DISCOVERY_DIALOG_VIEW_H_ diff --git a/browser/web_discovery/BUILD.gn b/browser/web_discovery/BUILD.gn index 9fc16a88e4c9..ba369e8cb8e3 100644 --- a/browser/web_discovery/BUILD.gn +++ b/browser/web_discovery/BUILD.gn @@ -25,3 +25,25 @@ source_set("unit_tests") { ] } } + +source_set("browser_tests") { + if (enable_extensions) { + testonly = true + defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ] + + sources = [ "web_discovery_browsertest.cc" ] + deps = [ + "//brave/components/search_engines", + "//chrome/browser", + "//chrome/browser/profiles:profile", + "//chrome/browser/ui", + "//chrome/test:test_support", + "//components/infobars/content", + "//components/prefs", + "//content/public/browser", + "//content/test:test_support", + "//testing/gmock", + "//testing/gtest", + ] + } +} diff --git a/browser/web_discovery/sources.gni b/browser/web_discovery/sources.gni index 3c3456a16c3d..b3d871302e98 100644 --- a/browser/web_discovery/sources.gni +++ b/browser/web_discovery/sources.gni @@ -10,6 +10,10 @@ brave_browser_web_discovery_deps = [] if (enable_extensions) { brave_browser_web_discovery_sources += [ + "//brave/browser/web_discovery/web_discovery_cta_util.cc", + "//brave/browser/web_discovery/web_discovery_cta_util.h", + "//brave/browser/web_discovery/web_discovery_infobar_delegate.cc", + "//brave/browser/web_discovery/web_discovery_infobar_delegate.h", "//brave/browser/web_discovery/web_discovery_tab_helper.cc", "//brave/browser/web_discovery/web_discovery_tab_helper.h", ] @@ -21,9 +25,10 @@ if (enable_extensions) { "//brave/components/search_engines", "//chrome/browser/profiles", "//chrome/browser/ui", + "//components/infobars/content", + "//components/infobars/core", "//components/prefs", "//components/search_engines", - "//components/user_prefs", "//content/public/browser", ] } diff --git a/browser/web_discovery/web_discovery_browsertest.cc b/browser/web_discovery/web_discovery_browsertest.cc new file mode 100644 index 000000000000..e3ae1aa112ed --- /dev/null +++ b/browser/web_discovery/web_discovery_browsertest.cc @@ -0,0 +1,43 @@ +/* Copyright (c) 2022 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/web_discovery/web_discovery_tab_helper.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/tabs/tab_strip_model.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "components/infobars/content/content_infobar_manager.h" +#include "components/infobars/core/infobar_manager.h" +#include "content/public/browser/web_contents.h" +#include "content/public/test/browser_test.h" +#include "testing/gmock/include/gmock/gmock.h" + +using WebDiscoveryTest = InProcessBrowserTest; +using ::testing::_; + +namespace { + +class TestObserver : public infobars::InfoBarManager::Observer { + public: + TestObserver() = default; + ~TestObserver() override = default; + MOCK_METHOD(void, OnInfoBarAdded, (infobars::InfoBar * infobar), (override)); +}; + +} // namespace + +IN_PROC_BROWSER_TEST_F(WebDiscoveryTest, InfobarAddedTest) { + auto* model = browser()->tab_strip_model(); + auto* contents = model->GetActiveWebContents(); + auto* tab_helper = WebDiscoveryTabHelper::FromWebContents(contents); + auto* infobar_manager = + infobars::ContentInfoBarManager::FromWebContents(contents); + + TestObserver observer; + EXPECT_CALL(observer, OnInfoBarAdded(_)).Times(1); + infobar_manager->AddObserver(&observer); + tab_helper->ShowInfoBar(browser()->profile()->GetPrefs()); + infobar_manager->RemoveObserver(&observer); +} diff --git a/browser/web_discovery/web_discovery_cta_util.cc b/browser/web_discovery/web_discovery_cta_util.cc new file mode 100644 index 000000000000..7e84e08ab383 --- /dev/null +++ b/browser/web_discovery/web_discovery_cta_util.cc @@ -0,0 +1,136 @@ +/* Copyright (c) 2022 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/web_discovery/web_discovery_cta_util.h" + +#include "base/check_is_test.h" +#include "base/json/values_util.h" +#include "base/logging.h" +#include "base/no_destructor.h" +#include "base/time/clock.h" +#include "base/values.h" +#include "brave/components/constants/pref_names.h" +#include "brave/components/constants/url_constants.h" +#include "brave/components/search_engines/brave_prepopulated_engines.h" +#include "components/prefs/pref_service.h" +#include "components/prefs/scoped_user_pref_update.h" +#include "components/search_engines/template_url.h" +#include "components/search_engines/template_url_service.h" +#include "net/base/url_util.h" +#include "url/gurl.h" + +namespace { + +constexpr int kMaxDisplayCount = 5; +constexpr char kWebDiscoveryCTAStateIdKey[] = "id"; +constexpr char kWebDiscoveryCTAStateCountKey[] = "count"; +constexpr char kWebDiscoveryCTAStateDismissedKey[] = "dismissed"; +constexpr char kWebDiscoveryCTAStateLastDisplayedKey[] = "last_displayed"; + +bool IsBraveSearchDefault(TemplateURLService* template_service) { + DCHECK(template_service); + + auto* template_url = template_service->GetDefaultSearchProvider(); + if (!template_url) + return false; + return template_url->prepopulate_id() == + TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_BRAVE; +} + +} // namespace + +WebDiscoveryCTAState GetWebDiscoveryCTAState(PrefService* prefs, + const std::string& cta_id) { + WebDiscoveryCTAState state; + const auto& state_value = prefs->GetDict(kWebDiscoveryCTAState); + const auto* id = state_value.FindString(kWebDiscoveryCTAStateIdKey); + if (!id || *id != cta_id) { + // Give fresh state with new id. + state.id = cta_id; + return state; + } + + state.id = *id; + + const auto count = state_value.FindInt(kWebDiscoveryCTAStateCountKey); + const auto dismissed = + state_value.FindBool(kWebDiscoveryCTAStateDismissedKey); + const auto* last_displayed = + state_value.Find(kWebDiscoveryCTAStateLastDisplayedKey); + // If state is invalid, give fresh one. + if (!count || !dismissed || !last_displayed) + return state; + + state.count = *count; + state.dismissed = *dismissed; + state.last_displayed = *base::ValueToTime(last_displayed); + + return state; +} + +std::string GetWebDiscoveryCurrentCTAId() { + if (!GetWebDiscoveryCTAIDForTesting().empty()) { + CHECK_IS_TEST(); + return GetWebDiscoveryCTAIDForTesting(); + } + + // Update this when we want to start cta again. + // TODO(simonhong): Need to improve for updating current cta id. + // Maybe fetching new cta id remotely? + constexpr char kCurrentCTAId[] = "v1"; + return kCurrentCTAId; +} + +bool ShouldShowWebDiscoveryInfoBar(TemplateURLService* service, + PrefService* prefs, + const WebDiscoveryCTAState& state, + base::Clock* test_clock) { + if (prefs->GetBoolean(kWebDiscoveryEnabled)) + return false; + + if (!service || !IsBraveSearchDefault(service)) + return false; + + // Show when |state| is newly created one. + if (state.last_displayed.is_null() && state.count == 0) + return true; + + // Don't show if user dismissed explicitely. + if (state.dismissed) + return false; + + // Don't show same cta more than 5 times. + if (state.count >= kMaxDisplayCount) + return false; + + const auto now = test_clock ? test_clock->Now() : base::Time::Now(); + + // Don't show if |last_displayed| is not valid. + // ex) last_displayed is newer than now. + if (now < state.last_displayed) + return false; + + // Don't show same cta twice in one day. + if (now - state.last_displayed < base::Days(1)) + return false; + + return true; +} + +void SetWebDiscoveryCTAStateToPrefs(PrefService* prefs, + const WebDiscoveryCTAState& state) { + DictionaryPrefUpdate update(prefs, kWebDiscoveryCTAState); + base::Value::Dict& dict = update->GetDict(); + dict.Set(kWebDiscoveryCTAStateIdKey, state.id); + dict.Set(kWebDiscoveryCTAStateCountKey, state.count); + dict.Set(kWebDiscoveryCTAStateDismissedKey, state.dismissed); + dict.Set(kWebDiscoveryCTAStateLastDisplayedKey, + base::TimeToValue(state.last_displayed)); +} + +std::string& GetWebDiscoveryCTAIDForTesting() { + static base::NoDestructor cta_id; + return *cta_id; +} diff --git a/browser/web_discovery/web_discovery_cta_util.h b/browser/web_discovery/web_discovery_cta_util.h new file mode 100644 index 000000000000..cf58beca907e --- /dev/null +++ b/browser/web_discovery/web_discovery_cta_util.h @@ -0,0 +1,42 @@ +/* Copyright (c) 2022 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/. */ + +#ifndef BRAVE_BROWSER_WEB_DISCOVERY_WEB_DISCOVERY_CTA_UTIL_H_ +#define BRAVE_BROWSER_WEB_DISCOVERY_WEB_DISCOVERY_CTA_UTIL_H_ + +#include + +#include "base/time/time.h" +#include "third_party/abseil-cpp/absl/types/optional.h" + +class GURL; +class PrefService; +class TemplateURLService; + +namespace base { +class Clock; +} // namespace base + +// Stores the current cta's state. +struct WebDiscoveryCTAState { + std::string id; + // The number of times Infobar shown so far. + int count = 0; + bool dismissed = false; + base::Time last_displayed; +}; + +WebDiscoveryCTAState GetWebDiscoveryCTAState(PrefService* prefs, + const std::string& cta_id); +void SetWebDiscoveryCTAStateToPrefs(PrefService* prefs, + const WebDiscoveryCTAState& state); +std::string GetWebDiscoveryCurrentCTAId(); +bool ShouldShowWebDiscoveryInfoBar(TemplateURLService* service, + PrefService* prefs, + const WebDiscoveryCTAState& state, + base::Clock* test_clock = nullptr); +std::string& GetWebDiscoveryCTAIDForTesting(); + +#endif // BRAVE_BROWSER_WEB_DISCOVERY_WEB_DISCOVERY_CTA_UTIL_H_ diff --git a/browser/web_discovery/web_discovery_infobar_delegate.cc b/browser/web_discovery/web_discovery_infobar_delegate.cc new file mode 100644 index 000000000000..c9db24eaa35b --- /dev/null +++ b/browser/web_discovery/web_discovery_infobar_delegate.cc @@ -0,0 +1,47 @@ +/* Copyright (c) 2022 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/web_discovery/web_discovery_infobar_delegate.h" + +#include "brave/browser/web_discovery/web_discovery_cta_util.h" +#include "brave/components/constants/pref_names.h" +#include "components/infobars/core/infobar.h" +#include "components/prefs/pref_service.h" + +WebDiscoveryInfoBarDelegate::WebDiscoveryInfoBarDelegate(PrefService* prefs) + : prefs_(prefs) {} + +WebDiscoveryInfoBarDelegate::~WebDiscoveryInfoBarDelegate() = default; + +infobars::InfoBarDelegate::InfoBarIdentifier +WebDiscoveryInfoBarDelegate::GetIdentifier() const { + return WEB_DISCOVERY_INFOBAR_DELEGATE; +} + +bool WebDiscoveryInfoBarDelegate::EqualsDelegate( + infobars::InfoBarDelegate* delegate) const { + return delegate->GetIdentifier() == GetIdentifier(); +} + +bool WebDiscoveryInfoBarDelegate::IsCloseable() const { + // To hide default close button. This infobar has custom close button. + return false; +} + +void WebDiscoveryInfoBarDelegate::Close(bool dismiss) { + if (dismiss) { + WebDiscoveryCTAState state = + GetWebDiscoveryCTAState(prefs_, GetWebDiscoveryCurrentCTAId()); + state.dismissed = true; + SetWebDiscoveryCTAStateToPrefs(prefs_, state); + } + + infobar()->RemoveSelf(); +} + +void WebDiscoveryInfoBarDelegate::EnableWebDiscovery() { + prefs_->SetBoolean(kWebDiscoveryEnabled, true); + infobar()->RemoveSelf(); +} diff --git a/browser/web_discovery/web_discovery_infobar_delegate.h b/browser/web_discovery/web_discovery_infobar_delegate.h new file mode 100644 index 000000000000..7c774a99def8 --- /dev/null +++ b/browser/web_discovery/web_discovery_infobar_delegate.h @@ -0,0 +1,33 @@ +/* Copyright (c) 2022 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/. */ + +#ifndef BRAVE_BROWSER_WEB_DISCOVERY_WEB_DISCOVERY_INFOBAR_DELEGATE_H_ +#define BRAVE_BROWSER_WEB_DISCOVERY_WEB_DISCOVERY_INFOBAR_DELEGATE_H_ + +#include "components/infobars/core/infobar_delegate.h" + +class PrefService; + +class WebDiscoveryInfoBarDelegate : public infobars::InfoBarDelegate { + public: + explicit WebDiscoveryInfoBarDelegate(PrefService* prefs); + WebDiscoveryInfoBarDelegate(const WebDiscoveryInfoBarDelegate&) = delete; + WebDiscoveryInfoBarDelegate& operator=(const WebDiscoveryInfoBarDelegate&) = + delete; + ~WebDiscoveryInfoBarDelegate() override; + + void Close(bool dismiss); + void EnableWebDiscovery(); + + private: + // InfoBarDelegate overrides + infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override; + bool EqualsDelegate(infobars::InfoBarDelegate* delegate) const override; + bool IsCloseable() const override; + + raw_ptr prefs_ = nullptr; +}; + +#endif // BRAVE_BROWSER_WEB_DISCOVERY_WEB_DISCOVERY_INFOBAR_DELEGATE_H_ diff --git a/browser/web_discovery/web_discovery_tab_helper.cc b/browser/web_discovery/web_discovery_tab_helper.cc index 33a29b48bd02..08083dadd712 100644 --- a/browser/web_discovery/web_discovery_tab_helper.cc +++ b/browser/web_discovery/web_discovery_tab_helper.cc @@ -5,44 +5,30 @@ #include "brave/browser/web_discovery/web_discovery_tab_helper.h" +#include + +#include "base/time/time.h" #include "brave/browser/profiles/profile_util.h" -#include "brave/browser/ui/browser_dialogs.h" -#include "brave/components/constants/pref_names.h" +#include "brave/browser/web_discovery/web_discovery_cta_util.h" +#include "brave/browser/web_discovery/web_discovery_infobar_delegate.h" #include "brave/components/constants/url_constants.h" -#include "brave/components/search_engines/brave_prepopulated_engines.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search_engines/template_url_service_factory.h" -#include "chrome/browser/ui/browser.h" -#include "chrome/browser/ui/browser_finder.h" -#include "components/prefs/pref_service.h" -#include "components/search_engines/template_url.h" -#include "components/search_engines/template_url_service.h" -#include "components/user_prefs/user_prefs.h" +#include "components/infobars/content/content_infobar_manager.h" +#include "components/infobars/core/infobar.h" #include "content/public/browser/browser_context.h" -#include "content/public/browser/page.h" #include "content/public/browser/render_frame_host.h" #include "content/public/browser/web_contents.h" -namespace { - -bool ShouldCreateWebDiscoveryTabHelper(content::WebContents* contents) { - DCHECK(contents); - auto* context = contents->GetBrowserContext(); - if (!brave::IsRegularProfile(context)) - return false; - auto* prefs = user_prefs::UserPrefs::Get(context); - if (!prefs) - return false; - return !prefs->GetBoolean(kDontAskEnableWebDiscovery) && - prefs->GetInteger(kBraveSearchVisitCount) < 20; -} - -} // namespace +#if defined(TOOLKIT_VIEWS) +std::unique_ptr CreateWebDiscoveryInfoBar( + std::unique_ptr delegate); +#endif // static void WebDiscoveryTabHelper::MaybeCreateForWebContents( content::WebContents* contents) { - if (contents && ShouldCreateWebDiscoveryTabHelper(contents)) + if (contents && brave::IsRegularProfile(contents->GetBrowserContext())) WebDiscoveryTabHelper::CreateForWebContents(contents); } @@ -55,85 +41,41 @@ WebDiscoveryTabHelper::~WebDiscoveryTabHelper() = default; void WebDiscoveryTabHelper::DidFinishLoad( content::RenderFrameHost* render_frame_host, const GURL& validated_url) { - // Only care about main frame. - if (render_frame_host->GetParent()) - return; - - if (validated_url != base::StringPiece(kBraveSearchUrl)) + if (validated_url.host() != kBraveSearchHost) return; - auto* browser = chrome::FindBrowserWithWebContents(web_contents()); - if (!browser) + // Only care about main frame. + if (render_frame_host->GetParent()) return; - auto* profile = browser->profile(); + auto* profile = + Profile::FromBrowserContext(web_contents()->GetBrowserContext()); if (!profile) return; - auto* service = TemplateURLServiceFactory::GetForProfile(profile); - if (!service) - return; - auto* prefs = profile->GetPrefs(); - if (!prefs) - return; + WebDiscoveryCTAState state = + GetWebDiscoveryCTAState(prefs, GetWebDiscoveryCurrentCTAId()); - if (!NeedVisitCountHandling(prefs, service)) + auto* service = TemplateURLServiceFactory::GetForProfile(profile); + if (!ShouldShowWebDiscoveryInfoBar(service, prefs, state)) return; - IncreaseBraveSearchVisitCount(prefs); - - if (ShouldShowWebDiscoveryDialog(prefs)) - brave::ShowWebDiscoveryDialog(browser, web_contents()); + state.count++; + state.last_displayed = base::Time::Now(); + SetWebDiscoveryCTAStateToPrefs(prefs, state); + ShowInfoBar(prefs); } -bool WebDiscoveryTabHelper::NeedVisitCountHandling( - PrefService* prefs, - TemplateURLService* template_service) { - DCHECK(prefs && template_service); - - if (prefs->GetBoolean(kDontAskEnableWebDiscovery)) - return false; - - if (!IsBraveSearchDefault(template_service)) - return false; - - if (prefs->GetBoolean(kWebDiscoveryEnabled)) - return false; - - if (prefs->GetInteger(kBraveSearchVisitCount) >= 20) - return false; - - return true; -} - -bool WebDiscoveryTabHelper::IsBraveSearchDefault( - TemplateURLService* template_service) { - DCHECK(template_service); - - auto* template_url = template_service->GetDefaultSearchProvider(); - if (!template_url) - return false; - return template_url->prepopulate_id() == - TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_BRAVE; -} - -bool WebDiscoveryTabHelper::ShouldShowWebDiscoveryDialog(PrefService* prefs) { - DCHECK(prefs); - - const int visit_count = prefs->GetInteger(kBraveSearchVisitCount); - return (visit_count == 3 || visit_count == 10 || visit_count == 20); -} - -void WebDiscoveryTabHelper::IncreaseBraveSearchVisitCount(PrefService* prefs) { - DCHECK(prefs); - - const int visit_count = prefs->GetInteger(kBraveSearchVisitCount) + 1; - // Don't need to increase anymore. We don't show again after 20th visit. - if (visit_count > 20) - return; - - prefs->SetInteger(kBraveSearchVisitCount, visit_count); +void WebDiscoveryTabHelper::ShowInfoBar(PrefService* prefs) { + // Only support view toolkit based WebDiscovery InfoBar. +#if defined(TOOLKIT_VIEWS) + infobars::ContentInfoBarManager::FromWebContents(web_contents()) + ->AddInfoBar(CreateWebDiscoveryInfoBar( + std::make_unique(prefs))); +#else + NOTREACHED() << "We don't support WDP infobar"; +#endif } WEB_CONTENTS_USER_DATA_KEY_IMPL(WebDiscoveryTabHelper); diff --git a/browser/web_discovery/web_discovery_tab_helper.h b/browser/web_discovery/web_discovery_tab_helper.h index 10622b4e3ef2..c43ed5f9e5fe 100644 --- a/browser/web_discovery/web_discovery_tab_helper.h +++ b/browser/web_discovery/web_discovery_tab_helper.h @@ -10,7 +10,6 @@ #include "content/public/browser/web_contents_user_data.h" class PrefService; -class TemplateURLService; class WebDiscoveryTabHelper : public content::WebContentsObserver, @@ -24,8 +23,9 @@ class WebDiscoveryTabHelper WebDiscoveryTabHelper& operator=(const WebDiscoveryTabHelper&) = delete; private: + FRIEND_TEST_ALL_PREFIXES(WebDiscoveryTest, InfobarAddedTest); + friend class content::WebContentsUserData; - friend class WebDiscoveryDialogTest; explicit WebDiscoveryTabHelper(content::WebContents* contents); @@ -33,11 +33,7 @@ class WebDiscoveryTabHelper void DidFinishLoad(content::RenderFrameHost* render_frame_host, const GURL& validated_url) override; - bool NeedVisitCountHandling(PrefService* prefs, - TemplateURLService* template_service); - bool IsBraveSearchDefault(TemplateURLService* template_service); - bool ShouldShowWebDiscoveryDialog(PrefService* prefs); - void IncreaseBraveSearchVisitCount(PrefService* prefs); + void ShowInfoBar(PrefService* prefs); WEB_CONTENTS_USER_DATA_KEY_DECL(); }; diff --git a/browser/web_discovery/web_discovery_unittest.cc b/browser/web_discovery/web_discovery_unittest.cc index a314dd13851d..1c5dee3c5a97 100644 --- a/browser/web_discovery/web_discovery_unittest.cc +++ b/browser/web_discovery/web_discovery_unittest.cc @@ -5,11 +5,16 @@ #include +#include "base/test/simple_test_clock.h" +#include "brave/browser/brave_local_state_prefs.h" +#include "brave/browser/web_discovery/web_discovery_cta_util.h" #include "brave/browser/web_discovery/web_discovery_tab_helper.h" #include "brave/components/constants/pref_names.h" #include "brave/components/search_engines/brave_prepopulated_engines.h" +#include "chrome/browser/prefs/browser_prefs.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search_engines/template_url_service_test_util.h" +#include "chrome/test/base/testing_browser_process.h" #include "chrome/test/base/testing_profile.h" #include "components/prefs/pref_service.h" #include "components/search_engines/search_engines_pref_names.h" @@ -21,11 +26,16 @@ #include "content/public/test/web_contents_tester.h" #include "testing/gtest/include/gtest/gtest.h" -class WebDiscoveryDialogTest : public testing::Test { +class WebDiscoveryCTATest : public testing::Test { public: - WebDiscoveryDialogTest() = default; + WebDiscoveryCTATest() = default; void SetUp() override { + // Setup g_browser_process because local_state() is refered during the + // TemplateURLServiceTestUtil initialization. + RegisterLocalState(test_local_state_.registry()); + TestingBrowserProcess::GetGlobal()->SetLocalState(&test_local_state_); + test_clock_.SetNow(base::Time::Now()); test_util_ = std::make_unique(); test_util_->profile() ->GetTestingPrefService() @@ -36,6 +46,10 @@ class WebDiscoveryDialogTest : public testing::Test { ASSERT_TRUE(web_contents_.get()); } + void TearDown() override { + TestingBrowserProcess::GetGlobal()->SetLocalState(nullptr); + } + WebDiscoveryTabHelper* tab_helper() { return WebDiscoveryTabHelper::FromWebContents(web_contents()); } @@ -57,22 +71,24 @@ class WebDiscoveryDialogTest : public testing::Test { base::Time::FromTimeT(100), false, false, TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_GOOGLE); service()->SetUserSelectedDefaultSearchProvider(google.get()); - } - - bool ShouldShowWebDiscoveryDialog() { - return tab_helper()->ShouldShowWebDiscoveryDialog(prefs()); + ASSERT_FALSE(IsBraveSearchDefault()); } bool IsBraveSearchDefault() { - return tab_helper()->IsBraveSearchDefault(service()); + auto* template_url = service()->GetDefaultSearchProvider(); + if (!template_url) + return false; + return template_url->prepopulate_id() == + TemplateURLPrepopulateData::PREPOPULATED_ENGINE_ID_BRAVE; } - bool NeedVisitCountHandling() { - return tab_helper()->NeedVisitCountHandling(prefs(), service()); + WebDiscoveryCTAState GetCurrentCTAState() { + return GetWebDiscoveryCTAState(prefs(), GetWebDiscoveryCurrentCTAId()); } - void IncreaseBraveSearchVisitCount() { - return tab_helper()->IncreaseBraveSearchVisitCount(prefs()); + bool ShouldShowWebDiscoveryInfoBar() { + return ::ShouldShowWebDiscoveryInfoBar(service(), prefs(), + GetCurrentCTAState(), &test_clock_); } TemplateURLServiceTestUtil* test_util() { return test_util_.get(); } @@ -81,111 +97,80 @@ class WebDiscoveryDialogTest : public testing::Test { Profile* profile() { return test_util()->profile(); } content::WebContents* web_contents() { return web_contents_.get(); } + base::SimpleTestClock test_clock_; content::BrowserTaskEnvironment task_environment_; content::RenderViewHostTestEnabler render_view_host_test_enabler_; std::unique_ptr test_util_; std::unique_ptr web_contents_; + TestingPrefServiceSimple test_local_state_; }; -TEST_F(WebDiscoveryDialogTest, InitialDataTest) { - EXPECT_EQ(0, prefs()->GetInteger(kBraveSearchVisitCount)); - EXPECT_FALSE(prefs()->GetBoolean(kDontAskEnableWebDiscovery)); +TEST_F(WebDiscoveryCTATest, InitialDataTest) { EXPECT_FALSE(prefs()->GetBoolean(kWebDiscoveryEnabled)); + const auto& info_value = prefs()->GetDict(kWebDiscoveryCTAState); + EXPECT_TRUE(info_value.empty()); // Tab helper can be created by default. WebDiscoveryTabHelper::MaybeCreateForWebContents(web_contents()); EXPECT_TRUE(tab_helper()); } -TEST_F(WebDiscoveryDialogTest, IncreaseBraveSearchVisitCountTest) { - // Count will not grow more than 20. - EXPECT_EQ(0, prefs()->GetInteger(kBraveSearchVisitCount)); - IncreaseBraveSearchVisitCount(); - IncreaseBraveSearchVisitCount(); - EXPECT_EQ(2, prefs()->GetInteger(kBraveSearchVisitCount)); - - prefs()->SetInteger(kBraveSearchVisitCount, 19); - IncreaseBraveSearchVisitCount(); - EXPECT_EQ(20, prefs()->GetInteger(kBraveSearchVisitCount)); +TEST_F(WebDiscoveryCTATest, ShouldCreateTabHelperWithPrivateProfileTest) { + // We don't need tab helper for private profile. + auto* private_profile = profile()->GetOffTheRecordProfile( + Profile::OTRProfileID::CreateUniqueForTesting(), true); + auto web_contents = content::WebContentsTester::CreateTestWebContents( + private_profile, nullptr); + ASSERT_TRUE(web_contents.get()); - IncreaseBraveSearchVisitCount(); - IncreaseBraveSearchVisitCount(); - EXPECT_EQ(20, prefs()->GetInteger(kBraveSearchVisitCount)); + WebDiscoveryTabHelper::MaybeCreateForWebContents(web_contents.get()); + // Check helper is not attached. + EXPECT_FALSE(WebDiscoveryTabHelper::FromWebContents(web_contents.get())); } -TEST_F(WebDiscoveryDialogTest, ShouldShowWebDiscoveryDialogTest) { - // Create tab helper. - WebDiscoveryTabHelper::MaybeCreateForWebContents(web_contents()); - ASSERT_TRUE(tab_helper()); - - // Set non-brave as a default search provider. +TEST_F(WebDiscoveryCTATest, ShouldShowInfoBarTest) { + // Test with search provider change. + // Set Non as a default provider. SetNonBraveSearchAsDefaultProvider(); - EXPECT_FALSE(IsBraveSearchDefault()); - EXPECT_FALSE(NeedVisitCountHandling()); + EXPECT_FALSE(ShouldShowWebDiscoveryInfoBar()); - // Set brave search as a default provider. + // Set Brave as a default provider. SetBraveSearchAsDefaultProvider(); - EXPECT_TRUE(NeedVisitCountHandling()); + EXPECT_TRUE(ShouldShowWebDiscoveryInfoBar()); + // Don't show if already enabled. prefs()->SetBoolean(kWebDiscoveryEnabled, true); - EXPECT_FALSE(NeedVisitCountHandling()); - - prefs()->SetBoolean(kWebDiscoveryEnabled, false); - prefs()->SetBoolean(kDontAskEnableWebDiscovery, true); - EXPECT_FALSE(NeedVisitCountHandling()); + EXPECT_FALSE(ShouldShowWebDiscoveryInfoBar()); prefs()->SetBoolean(kWebDiscoveryEnabled, false); - prefs()->SetBoolean(kDontAskEnableWebDiscovery, false); - EXPECT_TRUE(NeedVisitCountHandling()); + EXPECT_TRUE(ShouldShowWebDiscoveryInfoBar()); - // After reaching 20 visit count, we don't need to handling count any more. - prefs()->SetInteger(kBraveSearchVisitCount, 20); - EXPECT_FALSE(NeedVisitCountHandling()); + WebDiscoveryCTAState state = GetCurrentCTAState(); + state.count = 3; + state.last_displayed = test_clock_.Now(); + SetWebDiscoveryCTAStateToPrefs(prefs(), state); - // Check dialog is shown for 3rd, 10th and 20th visit. - prefs()->SetInteger(kBraveSearchVisitCount, 1); - EXPECT_FALSE(ShouldShowWebDiscoveryDialog()); + // Should not show because 1day is not passed from previous display + EXPECT_FALSE(ShouldShowWebDiscoveryInfoBar()); - prefs()->SetInteger(kBraveSearchVisitCount, 3); - EXPECT_TRUE(ShouldShowWebDiscoveryDialog()); + // Should show after one day passed. + test_clock_.Advance(base::Days(1)); + EXPECT_TRUE(ShouldShowWebDiscoveryInfoBar()); - prefs()->SetInteger(kBraveSearchVisitCount, 5); - EXPECT_FALSE(ShouldShowWebDiscoveryDialog()); + state.count = 5; + SetWebDiscoveryCTAStateToPrefs(prefs(), state); + EXPECT_FALSE(ShouldShowWebDiscoveryInfoBar()); - prefs()->SetInteger(kBraveSearchVisitCount, 10); - EXPECT_TRUE(ShouldShowWebDiscoveryDialog()); + state.count = 4; + SetWebDiscoveryCTAStateToPrefs(prefs(), state); + EXPECT_TRUE(ShouldShowWebDiscoveryInfoBar()); - prefs()->SetInteger(kBraveSearchVisitCount, 20); - EXPECT_TRUE(ShouldShowWebDiscoveryDialog()); -} + // Don't show again if dismissed already. + state.dismissed = true; + SetWebDiscoveryCTAStateToPrefs(prefs(), state); + EXPECT_FALSE(ShouldShowWebDiscoveryInfoBar()); -TEST_F(WebDiscoveryDialogTest, ShouldCreateTabHelperTest) { - // We don't need tab helper after user set "don't ask again" checkbox. - prefs()->SetBoolean(kDontAskEnableWebDiscovery, true); - WebDiscoveryTabHelper::MaybeCreateForWebContents(web_contents()); - EXPECT_FALSE(tab_helper()); - prefs()->SetBoolean(kDontAskEnableWebDiscovery, false); - - // Don't need tab helper if user visited 20 times. - prefs()->SetInteger(kBraveSearchVisitCount, 20); - WebDiscoveryTabHelper::MaybeCreateForWebContents(web_contents()); - EXPECT_FALSE(tab_helper()); - - // Created visit cound is less than 20. - prefs()->SetInteger(kBraveSearchVisitCount, 10); - WebDiscoveryTabHelper::MaybeCreateForWebContents(web_contents()); - EXPECT_TRUE(tab_helper()); -} - -TEST_F(WebDiscoveryDialogTest, ShouldCreateTabHelperWithPrivateProfileTest) { - // We don't need tab helper for private profile. - auto* private_profile = profile()->GetOffTheRecordProfile( - Profile::OTRProfileID::CreateUniqueForTesting(), true); - auto web_contents = content::WebContentsTester::CreateTestWebContents( - private_profile, nullptr); - ASSERT_TRUE(web_contents.get()); - - WebDiscoveryTabHelper::MaybeCreateForWebContents(web_contents.get()); - // Check helper is not attached. - EXPECT_FALSE(WebDiscoveryTabHelper::FromWebContents(web_contents.get())); + // Start new CTA when new id is set. + GetWebDiscoveryCTAIDForTesting() = "v2"; + EXPECT_TRUE(ShouldShowWebDiscoveryInfoBar()); } diff --git a/chromium_src/chrome/browser/prefs/browser_prefs.cc b/chromium_src/chrome/browser/prefs/browser_prefs.cc index 8efd2b62ea91..fd916f2f4525 100644 --- a/chromium_src/chrome/browser/prefs/browser_prefs.cc +++ b/chromium_src/chrome/browser/prefs/browser_prefs.cc @@ -91,6 +91,12 @@ void MigrateObsoleteProfilePrefs(Profile* profile) { profile->GetPrefs()->ClearPref(kDefaultBrowserLaunchingCount); #endif +#if BUILDFLAG(ENABLE_EXTENSIONS) + // Added 11/2022 + profile->GetPrefs()->ClearPref(kDontAskEnableWebDiscovery); + profile->GetPrefs()->ClearPref(kBraveSearchVisitCount); +#endif + brave_wallet::KeyringService::MigrateObsoleteProfilePrefs( profile->GetPrefs()); brave_wallet::MigrateObsoleteProfilePrefs(profile->GetPrefs()); diff --git a/chromium_src/chrome/browser/ui/views/frame/browser_view.cc b/chromium_src/chrome/browser/ui/views/frame/browser_view.cc index c1ad19f05822..0ef592bb7e6b 100644 --- a/chromium_src/chrome/browser/ui/views/frame/browser_view.cc +++ b/chromium_src/chrome/browser/ui/views/frame/browser_view.cc @@ -8,6 +8,7 @@ #include "chrome/browser/ui/views/frame/browser_view.h" #include "brave/browser/ui/views/frame/brave_browser_view_layout.h" +#include "brave/browser/ui/views/infobars/brave_infobar_container_view.h" #include "brave/browser/ui/views/side_panel/brave_side_panel.h" #include "brave/browser/ui/views/tabs/brave_browser_tab_strip_controller.h" #include "brave/browser/ui/views/tabs/brave_tab_strip.h" @@ -17,6 +18,7 @@ #include "chrome/browser/ui/views/frame/tab_strip_region_view.h" #include "chrome/browser/ui/views/side_panel/side_panel.h" +#define InfoBarContainerView BraveInfoBarContainerView #define BrowserViewLayout BraveBrowserViewLayout #define ToolbarView BraveToolbarView #define BrowserTabStripController BraveBrowserTabStripController @@ -34,3 +36,4 @@ #undef SidePanel #undef kAlignLeft #undef kAlignRight +#undef InfoBarContainerView diff --git a/chromium_src/chrome/browser/ui/views/infobars/infobar_container_view.h b/chromium_src/chrome/browser/ui/views/infobars/infobar_container_view.h new file mode 100644 index 000000000000..8b8d7f591640 --- /dev/null +++ b/chromium_src/chrome/browser/ui/views/infobars/infobar_container_view.h @@ -0,0 +1,20 @@ +/* Copyright (c) 2022 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/. */ + +#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_INFOBARS_INFOBAR_CONTAINER_VIEW_H_ +#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_INFOBARS_INFOBAR_CONTAINER_VIEW_H_ + +#include "components/infobars/core/infobar_container.h" + +#define PlatformSpecificInfoBarStateChanged \ + PlatformSpecificInfoBarStateChanged_UnUsed() {} \ + friend class BraveInfoBarContainerView; \ + void PlatformSpecificInfoBarStateChanged + +#include "src/chrome/browser/ui/views/infobars/infobar_container_view.h" + +#undef PlatformSpecificInfoBarStateChanged + +#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_VIEWS_INFOBARS_INFOBAR_CONTAINER_VIEW_H_ diff --git a/chromium_src/components/infobars/core/infobar_delegate.h b/chromium_src/components/infobars/core/infobar_delegate.h index 66c44d3625d2..8b288adaafb7 100644 --- a/chromium_src/components/infobars/core/infobar_delegate.h +++ b/chromium_src/components/infobars/core/infobar_delegate.h @@ -11,11 +11,12 @@ // When there will be too many items, redo java_cpp_enum.py to generate it // automatically -#define BRAVE_INFOBAR_DELEGATE_IDENTIFIERS \ - BRAVE_CONFIRM_P3A_INFOBAR_DELEGATE = 500, \ - WAYBACK_MACHINE_INFOBAR_DELEGATE = 502, \ - SYNC_V2_MIGRATE_INFOBAR_DELEGATE = 503, \ - ANDROID_SYSTEM_SYNC_DISABLED_INFOBAR = 504, SYNC_CANNOT_RUN_INFOBAR = 505, +#define BRAVE_INFOBAR_DELEGATE_IDENTIFIERS \ + BRAVE_CONFIRM_P3A_INFOBAR_DELEGATE = 500, \ + WAYBACK_MACHINE_INFOBAR_DELEGATE = 502, \ + SYNC_V2_MIGRATE_INFOBAR_DELEGATE = 503, \ + ANDROID_SYSTEM_SYNC_DISABLED_INFOBAR = 504, SYNC_CANNOT_RUN_INFOBAR = 505, \ + WEB_DISCOVERY_INFOBAR_DELEGATE = 506, #include "src/components/infobars/core/infobar_delegate.h" diff --git a/chromium_src/ui/views/controls/styled_label.h b/chromium_src/ui/views/controls/styled_label.h new file mode 100644 index 000000000000..2df3a9d4a5c1 --- /dev/null +++ b/chromium_src/ui/views/controls/styled_label.h @@ -0,0 +1,23 @@ +// Copyright (c) 2022 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/. + +#ifndef BRAVE_CHROMIUM_SRC_UI_VIEWS_CONTROLS_STYLED_LABEL_H_ +#define BRAVE_CHROMIUM_SRC_UI_VIEWS_CONTROLS_STYLED_LABEL_H_ + +class CustomStyledLabel; + +#define CreateLabel \ + CreateLabel_UnUsed(const std::u16string& text, \ + const RangeStyleInfo& style_info, \ + const gfx::Range& range, \ + views::LinkFragment** previous_link_component) const; \ + friend class ::CustomStyledLabel; \ + virtual std::unique_ptr CreateLabel + +#include "src/ui/views/controls/styled_label.h" + +#undef CreateLabel + +#endif // BRAVE_CHROMIUM_SRC_UI_VIEWS_CONTROLS_STYLED_LABEL_H_ diff --git a/components/constants/pref_names.cc b/components/constants/pref_names.cc index c4fb77f1d9d0..ca96dbeb0604 100644 --- a/components/constants/pref_names.cc +++ b/components/constants/pref_names.cc @@ -74,6 +74,7 @@ const char kDefaultBrowserPromptEnabled[] = "brave.default_browser_prompt_enabled"; const char kWebDiscoveryEnabled[] = "brave.web_discovery_enabled"; +const char kWebDiscoveryCTAState[] = "brave.web_discovery.cta_state"; const char kDontAskEnableWebDiscovery[] = "brave.dont_ask_enable_web_discovery"; const char kBraveSearchVisitCount[] = "brave.brave_search_visit_count"; diff --git a/components/constants/pref_names.h b/components/constants/pref_names.h index 8c76e9629080..0ddcb1cdb834 100644 --- a/components/constants/pref_names.h +++ b/components/constants/pref_names.h @@ -65,8 +65,9 @@ extern const char kDefaultBrowserPromptEnabled[]; // Web discovery project extern const char kWebDiscoveryEnabled[]; -extern const char kDontAskEnableWebDiscovery[]; -extern const char kBraveSearchVisitCount[]; +extern const char kWebDiscoveryCTAState[]; +extern const char kDontAskEnableWebDiscovery[]; // Deprecated. +extern const char kBraveSearchVisitCount[]; // Deprecated. extern const char kBraveGCMChannelStatus[]; extern const char kImportDialogExtensions[]; diff --git a/components/constants/url_constants.cc b/components/constants/url_constants.cc index 05fc9ed571bf..76f1a4b0e4c4 100644 --- a/components/constants/url_constants.cc +++ b/components/constants/url_constants.cc @@ -26,7 +26,7 @@ const char kSpeedreaderLearnMoreUrl[] = "360045031392-What-is-SpeedReader"; const char kWebDiscoveryLearnMoreUrl[] = "https://brave.com/privacy/browser/#web-discovery-project"; -const char kBraveSearchUrl[] = "https://search.brave.com/"; +const char kBraveSearchHost[] = "search.brave.com"; const char kWidevineLearnMoreUrl[] = "https://support.brave.com/hc/en-us/articles/" "360023851591-How-do-I-view-DRM-protected-content-"; diff --git a/components/constants/url_constants.h b/components/constants/url_constants.h index d135455508b6..4b7b91b4027e 100644 --- a/components/constants/url_constants.h +++ b/components/constants/url_constants.h @@ -21,7 +21,7 @@ extern const char kCryptoWalletsLearnMoreURL[]; extern const char kPermissionPromptLearnMoreUrl[]; extern const char kSpeedreaderLearnMoreUrl[]; extern const char kWebDiscoveryLearnMoreUrl[]; -extern const char kBraveSearchUrl[]; +extern const char kBraveSearchHost[]; extern const char kWidevineLearnMoreUrl[]; // This is introduced to replace |kDownloadChromeUrl| in diff --git a/test/BUILD.gn b/test/BUILD.gn index 99e02b4e6ac6..158003126c31 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -847,6 +847,7 @@ test("brave_browser_tests") { "//brave/browser/ui/tabs", "//brave/browser/ui/tabs/test:browser_tests", "//brave/browser/ui/views/tabs:browser_tests", + "//brave/browser/web_discovery:browser_tests", "//brave/browser/widevine:browser_tests", "//brave/common", "//brave/components/adblock_rust_ffi",