diff --git a/android/java/org/chromium/chrome/browser/infobar/BraveInfoBarIdentifier.java b/android/java/org/chromium/chrome/browser/infobar/BraveInfoBarIdentifier.java index d063fbe2ff9..d1d7e7f0084 100644 --- a/android/java/org/chromium/chrome/browser/infobar/BraveInfoBarIdentifier.java +++ b/android/java/org/chromium/chrome/browser/infobar/BraveInfoBarIdentifier.java @@ -13,6 +13,7 @@ import java.lang.annotation.RetentionPolicy; // Reflects enum items from chromium_src/components/infobars/core/infobar_delegate.h @IntDef({BraveInfoBarIdentifier.INVALID, BraveInfoBarIdentifier.BRAVE_CONFIRM_P3A_INFOBAR_DELEGATE, + BraveInfoBarIdentifier.WAYBACK_MACHINE_INFOBAR_DELEGATE, BraveInfoBarIdentifier.SYNC_CANNOT_RUN_INFOBAR, BraveInfoBarIdentifier.WEB_DISCOVERY_INFOBAR_DELEGATE, BraveInfoBarIdentifier.BRAVE_SYNC_ACCOUNT_DELETED_INFOBAR}) @@ -21,7 +22,7 @@ import java.lang.annotation.RetentionPolicy; public @interface BraveInfoBarIdentifier { int INVALID = -1; int BRAVE_CONFIRM_P3A_INFOBAR_DELEGATE = 500; - // int WAYBACK_MACHINE_INFOBAR_DELEGATE = 502; - deprecated + int WAYBACK_MACHINE_INFOBAR_DELEGATE = 502; // int SYNC_V2_MIGRATE_INFOBAR_DELEGATE = 503; - deprecated // int ANDROID_SYSTEM_SYNC_DISABLED_INFOBAR = 504; - deprecated int SYNC_CANNOT_RUN_INFOBAR = 505; diff --git a/app/brave_generated_resources.grd b/app/brave_generated_resources.grd index 294601a5174..27d2c822f2c 100644 --- a/app/brave_generated_resources.grd +++ b/app/brave_generated_resources.grd @@ -928,12 +928,6 @@ Or change later at $2brave://settings/ext Don't ask me again - - No thanks - - - Close - diff --git a/app/theme/brave_theme_resources.grd b/app/theme/brave_theme_resources.grd index 593f30440a3..dbc8fb19dfc 100644 --- a/app/theme/brave_theme_resources.grd +++ b/app/theme/brave_theme_resources.grd @@ -26,6 +26,8 @@ + + diff --git a/app/theme/default_100_percent/brave/brave_wayback_infobar.png b/app/theme/default_100_percent/brave/brave_wayback_infobar.png new file mode 100644 index 00000000000..eb3ed2a81e1 Binary files /dev/null and b/app/theme/default_100_percent/brave/brave_wayback_infobar.png differ diff --git a/app/theme/default_100_percent/brave/brave_wayback_infobar_dark.png b/app/theme/default_100_percent/brave/brave_wayback_infobar_dark.png new file mode 100644 index 00000000000..d2630ae3734 Binary files /dev/null and b/app/theme/default_100_percent/brave/brave_wayback_infobar_dark.png differ diff --git a/app/theme/default_200_percent/brave/brave_wayback_infobar.png b/app/theme/default_200_percent/brave/brave_wayback_infobar.png new file mode 100644 index 00000000000..0eca7bc78f4 Binary files /dev/null and b/app/theme/default_200_percent/brave/brave_wayback_infobar.png differ diff --git a/app/theme/default_200_percent/brave/brave_wayback_infobar_dark.png b/app/theme/default_200_percent/brave/brave_wayback_infobar_dark.png new file mode 100644 index 00000000000..121365ddbde Binary files /dev/null and b/app/theme/default_200_percent/brave/brave_wayback_infobar_dark.png differ diff --git a/browser/infobars/brave_wayback_machine_delegate_impl.cc b/browser/infobars/brave_wayback_machine_delegate_impl.cc index aaa275ea447..298974f9a69 100644 --- a/browser/infobars/brave_wayback_machine_delegate_impl.cc +++ b/browser/infobars/brave_wayback_machine_delegate_impl.cc @@ -5,12 +5,12 @@ #include "brave/browser/infobars/brave_wayback_machine_delegate_impl.h" -#include - #include "base/command_line.h" -#include "brave/browser/ui/browser_dialogs.h" +#include "brave/components/brave_wayback_machine/brave_wayback_machine_infobar_delegate.h" #include "brave/components/brave_wayback_machine/brave_wayback_machine_tab_helper.h" #include "brave/components/constants/brave_switches.h" +#include "components/infobars/content/content_infobar_manager.h" +#include "components/infobars/core/infobar.h" // static void BraveWaybackMachineDelegateImpl::AttachTabHelperIfNeeded( @@ -28,7 +28,12 @@ void BraveWaybackMachineDelegateImpl::AttachTabHelperIfNeeded( BraveWaybackMachineDelegateImpl::BraveWaybackMachineDelegateImpl() = default; BraveWaybackMachineDelegateImpl::~BraveWaybackMachineDelegateImpl() = default; -void BraveWaybackMachineDelegateImpl::ShowWaybackMachineDialog( + +void BraveWaybackMachineDelegateImpl::CreateInfoBar( content::WebContents* web_contents) { - brave::ShowWaybackMachineWebModalDialog(web_contents); + infobars::ContentInfoBarManager::FromWebContents(web_contents) + ->AddInfoBar(CreateInfoBarView( + std::make_unique(), + web_contents), + true); } diff --git a/browser/infobars/brave_wayback_machine_delegate_impl.h b/browser/infobars/brave_wayback_machine_delegate_impl.h index 92a4c66a84b..f05295a43a0 100644 --- a/browser/infobars/brave_wayback_machine_delegate_impl.h +++ b/browser/infobars/brave_wayback_machine_delegate_impl.h @@ -6,13 +6,18 @@ #ifndef BRAVE_BROWSER_INFOBARS_BRAVE_WAYBACK_MACHINE_DELEGATE_IMPL_H_ #define BRAVE_BROWSER_INFOBARS_BRAVE_WAYBACK_MACHINE_DELEGATE_IMPL_H_ +#include + #include "brave/components/brave_wayback_machine/brave_wayback_machine_delegate.h" namespace content { class WebContents; } // namespace content -// TODO(simonhong): Move this to //brave/browser/wayback_machine. +namespace infobars { +class InfoBar; +} // namespace infobars + class BraveWaybackMachineDelegateImpl : public BraveWaybackMachineDelegate { public: static void AttachTabHelperIfNeeded(content::WebContents* web_contents); @@ -27,7 +32,11 @@ class BraveWaybackMachineDelegateImpl : public BraveWaybackMachineDelegate { private: // BraveWaybackMachineDelegate overrides: - void ShowWaybackMachineDialog(content::WebContents* web_contents) override; + void CreateInfoBar(content::WebContents* web_contents) override; + + std::unique_ptr CreateInfoBarView( + std::unique_ptr delegate, + content::WebContents* contents); }; #endif // BRAVE_BROWSER_INFOBARS_BRAVE_WAYBACK_MACHINE_DELEGATE_IMPL_H_ diff --git a/browser/ui/BUILD.gn b/browser/ui/BUILD.gn index dfb56712586..574d3b99ed9 100644 --- a/browser/ui/BUILD.gn +++ b/browser/ui/BUILD.gn @@ -739,14 +739,7 @@ source_set("ui") { deps += [ "//brave/components/brave_wayback_machine" ] if (toolkit_views) { - sources += [ - "views/wayback_machine_dialog_view.cc", - "views/wayback_machine_dialog_view.h", - "views/wayback_machine_fetch_button.cc", - "views/wayback_machine_fetch_button.h", - "views/wayback_machine_throbber.cc", - "views/wayback_machine_throbber.h", - ] + deps += [ "//brave/browser/ui/views/infobars:brave_wayback_machine" ] } } diff --git a/browser/ui/browser_dialogs.h b/browser/ui/browser_dialogs.h index d689b2d4685..fe8e313a176 100644 --- a/browser/ui/browser_dialogs.h +++ b/browser/ui/browser_dialogs.h @@ -34,9 +34,6 @@ void ShowTextRecognitionDialog(content::WebContents* web_contents, #if BUILDFLAG(ENABLE_BRAVE_VPN) void ShowBraveVpnIKEv2FallbackDialog(); #endif - -void ShowWaybackMachineWebModalDialog(content::WebContents* web_contents); - } // namespace brave #endif // BRAVE_BROWSER_UI_BROWSER_DIALOGS_H_ diff --git a/browser/ui/views/infobars/BUILD.gn b/browser/ui/views/infobars/BUILD.gn index 2b7309d72ff..0bf673a323e 100644 --- a/browser/ui/views/infobars/BUILD.gn +++ b/browser/ui/views/infobars/BUILD.gn @@ -42,6 +42,52 @@ source_set("infobars") { ] } +if (enable_brave_wayback_machine) { + source_set("brave_wayback_machine") { + # Remove when https://github.com/brave/brave-browser/issues/10652 is resolved + check_includes = false + + sources = [ + "brave_wayback_machine_infobar_button_container.cc", + "brave_wayback_machine_infobar_button_container.h", + "brave_wayback_machine_infobar_contents_view.cc", + "brave_wayback_machine_infobar_contents_view.h", + "brave_wayback_machine_infobar_throbber.cc", + "brave_wayback_machine_infobar_throbber.h", + "brave_wayback_machine_infobar_view.cc", + "brave_wayback_machine_infobar_view.h", + ] + + deps = [ + "//brave/app:brave_generated_resources_grit", + "//brave/app/theme:brave_theme_resources", + "//brave/app/vector_icons", + "//brave/browser/themes", + "//brave/components/brave_wayback_machine", + "//chrome/browser:theme_properties", + "//components/infobars/content", + "//components/infobars/core", + "//components/prefs:prefs", + "//components/resources", + "//components/user_prefs", + "//content/public/browser", + "//ui/base", + "//ui/gfx", + "//ui/native_theme", + "//ui/views", + "//url", + ] + + # Below dep list are indirectly used by brave_wayback_machine target. + # Only added to fix intermittent build failure. + # chrome/common/buildflags.h is included indirectly from + # brave_wayback_machine_infobar_contents_view.cc. + # However, this buildflags.h sometimes not generated when + # brave_wayback_machine_infobar_contents_view.cc is compiled. + deps += [ "//chrome/common:buildflags" ] + } +} + source_set("brave_sync_account_deleted_infobar") { # Remove when https://github.com/brave/brave-browser/issues/10652 is resolved check_includes = false diff --git a/browser/ui/views/wayback_machine_fetch_button.cc b/browser/ui/views/infobars/brave_wayback_machine_infobar_button_container.cc similarity index 70% rename from browser/ui/views/wayback_machine_fetch_button.cc rename to browser/ui/views/infobars/brave_wayback_machine_infobar_button_container.cc index 75fbe5717fc..f16e98c2ce3 100644 --- a/browser/ui/views/wayback_machine_fetch_button.cc +++ b/browser/ui/views/infobars/brave_wayback_machine_infobar_button_container.cc @@ -1,14 +1,14 @@ /* Copyright (c) 2019 The Brave Authors. All rights reserved. * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at https://mozilla.org/MPL/2.0/. */ + * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include "brave/browser/ui/views/wayback_machine_fetch_button.h" +#include "brave/browser/ui/views/infobars/brave_wayback_machine_infobar_button_container.h" #include #include -#include "brave/browser/ui/views/wayback_machine_throbber.h" +#include "brave/browser/ui/views/infobars/brave_wayback_machine_infobar_throbber.h" #include "brave/components/l10n/common/localization_util.h" #include "brave/grit/brave_generated_resources.h" #include "chrome/browser/ui/views/chrome_layout_provider.h" @@ -21,27 +21,27 @@ constexpr int kThrobberDiameter = 16; constexpr int kInsetOffsetsForThrobber = kThrobberDiameter; } // namespace -WaybackMachineFetchButton:: - WaybackMachineFetchButton( +BraveWaybackMachineInfoBarButtonContainer:: + BraveWaybackMachineInfoBarButtonContainer( views::Button::PressedCallback callback) { auto button = std::make_unique( std::move(callback), brave_l10n::GetLocalizedResourceUTF16String( IDS_BRAVE_WAYBACK_MACHINE_CHECK_BUTTON_TEXT)); button_ = button.get(); - button->SetKind(views::MdTextButton::Kind::kPrimary); + button->SetStyle(ui::ButtonStyle::kProminent); button->SizeToPreferredSize(); AddChildView(button.release()); - throbber_ = new WaybackMachineThrobber; + throbber_ = new BraveWaybackMachineInfoBarThrobber; throbber_->SetSize(gfx::Size(kThrobberDiameter, kThrobberDiameter)); throbber_->SetVisible(false); button_->AddChildView(throbber_.get()); } -WaybackMachineFetchButton:: - ~WaybackMachineFetchButton() = default; +BraveWaybackMachineInfoBarButtonContainer:: + ~BraveWaybackMachineInfoBarButtonContainer() = default; -void WaybackMachineFetchButton::Layout(PassKey) { +void BraveWaybackMachineInfoBarButtonContainer::Layout(PassKey) { if (throbber_->GetVisible()) { int x = button_->width() - throbber_->width() - kThrobberDiameter / 2; int y = (button_->height() - throbber_->height()) / 2; @@ -50,27 +50,27 @@ void WaybackMachineFetchButton::Layout(PassKey) { } gfx::Size -WaybackMachineFetchButton::CalculatePreferredSize() const { +BraveWaybackMachineInfoBarButtonContainer::CalculatePreferredSize() const { // This container doesn't need more space than button because throbber is // drawn over the button. return button_->GetPreferredSize(); } -void WaybackMachineFetchButton::StartThrobber() { +void BraveWaybackMachineInfoBarButtonContainer::StartThrobber() { AdjustButtonInsets(true); throbber_->SetVisible(true); throbber_->Start(); DeprecatedLayoutImmediately(); } -void WaybackMachineFetchButton::StopThrobber() { +void BraveWaybackMachineInfoBarButtonContainer::StopThrobber() { AdjustButtonInsets(false); throbber_->SetVisible(false); throbber_->Stop(); DeprecatedLayoutImmediately(); } -void WaybackMachineFetchButton::AdjustButtonInsets( +void BraveWaybackMachineInfoBarButtonContainer::AdjustButtonInsets( bool add_insets) { const gfx::Insets insets_offset = gfx::Insets::TLBR(0, 0, 0, kInsetOffsetsForThrobber); @@ -81,5 +81,5 @@ void WaybackMachineFetchButton::AdjustButtonInsets( button_->SizeToPreferredSize(); } -BEGIN_METADATA(WaybackMachineFetchButton) +BEGIN_METADATA(BraveWaybackMachineInfoBarButtonContainer) END_METADATA diff --git a/browser/ui/views/infobars/brave_wayback_machine_infobar_button_container.h b/browser/ui/views/infobars/brave_wayback_machine_infobar_button_container.h new file mode 100644 index 00000000000..2184a06fa2a --- /dev/null +++ b/browser/ui/views/infobars/brave_wayback_machine_infobar_button_container.h @@ -0,0 +1,46 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_BROWSER_UI_VIEWS_INFOBARS_BRAVE_WAYBACK_MACHINE_INFOBAR_BUTTON_CONTAINER_H_ +#define BRAVE_BROWSER_UI_VIEWS_INFOBARS_BRAVE_WAYBACK_MACHINE_INFOBAR_BUTTON_CONTAINER_H_ + +#include "base/memory/raw_ptr.h" +#include "ui/base/metadata/metadata_header_macros.h" +#include "ui/views/controls/button/button.h" +#include "ui/views/view.h" + +class BraveWaybackMachineInfoBarThrobber; + +// This manages button and throbber controls. +// buttons occupies all this containers area and throbber runs over the button. +// When throbbing is requested, button extends its right inset and throbber runs +// on that area. +class BraveWaybackMachineInfoBarButtonContainer : public views::View { + METADATA_HEADER(BraveWaybackMachineInfoBarButtonContainer, views::View) + public: + explicit BraveWaybackMachineInfoBarButtonContainer( + views::Button::PressedCallback callback); + ~BraveWaybackMachineInfoBarButtonContainer() override; + + BraveWaybackMachineInfoBarButtonContainer( + const BraveWaybackMachineInfoBarButtonContainer&) = delete; + BraveWaybackMachineInfoBarButtonContainer& operator=( + const BraveWaybackMachineInfoBarButtonContainer&) = delete; + + void StartThrobber(); + void StopThrobber(); + + // views::View overrides: + void Layout(PassKey) override; + gfx::Size CalculatePreferredSize() const override; + + private: + void AdjustButtonInsets(bool add_insets); + + raw_ptr throbber_ = nullptr; + raw_ptr button_ = nullptr; +}; + +#endif // BRAVE_BROWSER_UI_VIEWS_INFOBARS_BRAVE_WAYBACK_MACHINE_INFOBAR_BUTTON_CONTAINER_H_ diff --git a/browser/ui/views/wayback_machine_throbber.cc b/browser/ui/views/infobars/brave_wayback_machine_infobar_throbber.cc similarity index 59% rename from browser/ui/views/wayback_machine_throbber.cc rename to browser/ui/views/infobars/brave_wayback_machine_infobar_throbber.cc index 687b97791cf..c28f0b78a97 100644 --- a/browser/ui/views/wayback_machine_throbber.cc +++ b/browser/ui/views/infobars/brave_wayback_machine_infobar_throbber.cc @@ -1,9 +1,9 @@ /* Copyright (c) 2019 The Brave Authors. All rights reserved. * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at https://mozilla.org/MPL/2.0/. */ + * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include "brave/browser/ui/views/wayback_machine_throbber.h" +#include "brave/browser/ui/views/infobars/brave_wayback_machine_infobar_throbber.h" #include "base/functional/bind.h" #include "base/location.h" @@ -11,26 +11,26 @@ #include "ui/gfx/canvas.h" #include "ui/gfx/paint_throbber.h" -WaybackMachineThrobber::WaybackMachineThrobber() = +BraveWaybackMachineInfoBarThrobber::BraveWaybackMachineInfoBarThrobber() = default; -WaybackMachineThrobber::~WaybackMachineThrobber() { +BraveWaybackMachineInfoBarThrobber::~BraveWaybackMachineInfoBarThrobber() { Stop(); } -void WaybackMachineThrobber::Start() { +void BraveWaybackMachineInfoBarThrobber::Start() { if (IsRunning()) return; start_time_ = base::TimeTicks::Now(); timer_.Start( FROM_HERE, base::Milliseconds(30), - base::BindRepeating(&WaybackMachineThrobber::SchedulePaint, + base::BindRepeating(&BraveWaybackMachineInfoBarThrobber::SchedulePaint, base::Unretained(this))); SchedulePaint(); // paint right away } -void WaybackMachineThrobber::Stop() { +void BraveWaybackMachineInfoBarThrobber::Stop() { if (!IsRunning()) return; @@ -38,7 +38,7 @@ void WaybackMachineThrobber::Stop() { SchedulePaint(); } -void WaybackMachineThrobber::OnPaint(gfx::Canvas* canvas) { +void BraveWaybackMachineInfoBarThrobber::OnPaint(gfx::Canvas* canvas) { if (!IsRunning()) return; @@ -47,9 +47,9 @@ void WaybackMachineThrobber::OnPaint(gfx::Canvas* canvas) { canvas, GetContentsBounds(), SK_ColorWHITE, elapsed_time); } -bool WaybackMachineThrobber::IsRunning() const { +bool BraveWaybackMachineInfoBarThrobber::IsRunning() const { return timer_.IsRunning(); } -BEGIN_METADATA(WaybackMachineThrobber) +BEGIN_METADATA(BraveWaybackMachineInfoBarThrobber) END_METADATA diff --git a/browser/ui/views/wayback_machine_throbber.h b/browser/ui/views/infobars/brave_wayback_machine_infobar_throbber.h similarity index 52% rename from browser/ui/views/wayback_machine_throbber.h rename to browser/ui/views/infobars/brave_wayback_machine_infobar_throbber.h index b42b798de50..01ffa24934b 100644 --- a/browser/ui/views/wayback_machine_throbber.h +++ b/browser/ui/views/infobars/brave_wayback_machine_infobar_throbber.h @@ -1,10 +1,10 @@ /* Copyright (c) 2019 The Brave Authors. All rights reserved. * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at https://mozilla.org/MPL/2.0/. */ + * You can obtain one at http://mozilla.org/MPL/2.0/. */ -#ifndef BRAVE_BROWSER_UI_VIEWS_WAYBACK_MACHINE_THROBBER_H_ -#define BRAVE_BROWSER_UI_VIEWS_WAYBACK_MACHINE_THROBBER_H_ +#ifndef BRAVE_BROWSER_UI_VIEWS_INFOBARS_BRAVE_WAYBACK_MACHINE_INFOBAR_THROBBER_H_ +#define BRAVE_BROWSER_UI_VIEWS_INFOBARS_BRAVE_WAYBACK_MACHINE_INFOBAR_THROBBER_H_ #include "base/time/time.h" #include "base/timer/timer.h" @@ -12,16 +12,16 @@ #include "ui/views/view.h" // White throbber. Most of codes are copied from views::Throbber. -class WaybackMachineThrobber : public views::View { - METADATA_HEADER(WaybackMachineThrobber, views::View) +class BraveWaybackMachineInfoBarThrobber : public views::View { + METADATA_HEADER(BraveWaybackMachineInfoBarThrobber, views::View) public: - WaybackMachineThrobber(); - ~WaybackMachineThrobber() override; + BraveWaybackMachineInfoBarThrobber(); + ~BraveWaybackMachineInfoBarThrobber() override; - WaybackMachineThrobber( - const WaybackMachineThrobber&) = delete; - WaybackMachineThrobber& operator=( - const WaybackMachineThrobber&) = delete; + BraveWaybackMachineInfoBarThrobber( + const BraveWaybackMachineInfoBarThrobber&) = delete; + BraveWaybackMachineInfoBarThrobber& operator=( + const BraveWaybackMachineInfoBarThrobber&) = delete; // Start and stop the throbber animation. void Start(); @@ -38,4 +38,4 @@ class WaybackMachineThrobber : public views::View { base::RepeatingTimer timer_; // Used to schedule Run calls. }; -#endif // BRAVE_BROWSER_UI_VIEWS_WAYBACK_MACHINE_THROBBER_H_ +#endif // BRAVE_BROWSER_UI_VIEWS_INFOBARS_BRAVE_WAYBACK_MACHINE_INFOBAR_THROBBER_H_ diff --git a/browser/ui/views/wayback_machine_dialog_view.cc b/browser/ui/views/wayback_machine_dialog_view.cc deleted file mode 100644 index 46ccd57a131..00000000000 --- a/browser/ui/views/wayback_machine_dialog_view.cc +++ /dev/null @@ -1,284 +0,0 @@ -/* Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/. */ - -#include "brave/browser/ui/views/wayback_machine_dialog_view.h" - -#include -#include - -#include "base/functional/bind.h" -#include "brave/browser/ui/views/wayback_machine_fetch_button.h" -#include "brave/components/brave_wayback_machine/brave_wayback_machine_tab_helper.h" -#include "brave/components/brave_wayback_machine/pref_names.h" -#include "brave/components/l10n/common/localization_util.h" -#include "brave/grit/brave_generated_resources.h" -#include "chrome/browser/ui/views/chrome_layout_provider.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/navigation_controller.h" -#include "content/public/browser/storage_partition.h" -#include "content/public/browser/web_contents.h" -#include "ui/base/metadata/metadata_impl_macros.h" -#include "ui/views/controls/button/checkbox.h" -#include "ui/views/controls/button/md_text_button.h" -#include "ui/views/controls/label.h" -#include "ui/views/layout/box_layout.h" -#include "ui/views/layout/flex_layout.h" -#include "ui/views/window/dialog_client_view.h" - -namespace { - -BraveWaybackMachineTabHelper* GetTabHelper(content::WebContents* web_contents) { - return BraveWaybackMachineTabHelper::FromWebContents(web_contents); -} - -gfx::FontList GetFont(int font_size, gfx::Font::Weight weight) { - gfx::FontList font_list; - return font_list.DeriveWithSizeDelta(font_size - font_list.GetFontSize()) - .DeriveWithWeight(weight); -} - -// Subclass for custom font. -class CustomMdTextButton : public views::MdTextButton { - METADATA_HEADER(CustomMdTextButton, views::MdTextButton) - public: - using MdTextButton::MdTextButton; - CustomMdTextButton(const CustomMdTextButton&) = delete; - CustomMdTextButton& operator=(const CustomMdTextButton&) = delete; - - void SetFontSize(int size) { - label()->SetFontList(GetFont(size, gfx::Font::Weight::SEMIBOLD)); - } -}; - -BEGIN_METADATA(CustomMdTextButton) -END_METADATA - -// Subclass for custom font. -class DontAskAgainCheckbox : public views::Checkbox { - METADATA_HEADER(DontAskAgainCheckbox, views::Checkbox) - public: - using views::Checkbox::Checkbox; - ~DontAskAgainCheckbox() override = default; - - void SetFontList(const gfx::FontList& font_list) { - label()->SetFontList(font_list); - } -}; - -BEGIN_METADATA(DontAskAgainCheckbox) -END_METADATA - -} // namespace - -namespace brave { - -void ShowWaybackMachineWebModalDialog(content::WebContents* web_contents) { - auto* tab_helper = GetTabHelper(web_contents); - if (!tab_helper) { - return; - } - - // Close previous one if exists. - if (gfx::NativeWindow previous_dialog = tab_helper->active_dialog()) { - views::Widget::GetWidgetForNativeWindow(previous_dialog) - ->CloseWithReason(views::Widget::ClosedReason::kUnspecified); - } - - auto* widget = constrained_window::ShowWebModalDialogViews( - new WaybackMachineDialogView(web_contents), web_contents); - tab_helper->set_active_dialog(widget->GetNativeWindow()); -} - -} // namespace brave - -WaybackMachineDialogView::WaybackMachineDialogView( - content::WebContents* web_contents) - : web_contents_(web_contents), - wayback_machine_url_fetcher_( - this, - web_contents_->GetBrowserContext() - ->GetDefaultStoragePartition() - ->GetURLLoaderFactoryForBrowserProcess()), - pref_service_( - user_prefs::UserPrefs::Get(web_contents_->GetBrowserContext())) { - SetModalType(ui::MODAL_TYPE_CHILD); - SetButtons(ui::DIALOG_BUTTON_NONE); - - // Unretained here is safe because this class is owned by widget. - RegisterWindowWillCloseCallback(base::BindOnce( - &WaybackMachineDialogView::OnWillCloseDialog, base::Unretained(this))); - - SetLayoutManager(std::make_unique()) - ->SetOrientation(views::LayoutOrientation::kVertical) - .SetMainAxisAlignment(views::LayoutAlignment::kStart) - .SetInteriorMargin(gfx::Insets::TLBR(0, 26, 26, 26)); - - auto* label = CreateLabel(brave_l10n::GetLocalizedResourceUTF16String( - IDS_BRAVE_WAYBACK_MACHINE_INFOBAR_PAGE_MISSING_TEXT)); - views_visible_before_checking_.push_back(label); - label->SetFontList( - label->font_list().DeriveWithWeight(gfx::Font::Weight::BOLD)); - label->SetProperty(views::kMarginsKey, gfx::Insets::TLBR(0, 0, 10, 0)); - AddChildView(label); - - label = CreateLabel(brave_l10n::GetLocalizedResourceUTF16String( - IDS_BRAVE_WAYBACK_MACHINE_INFOBAR_ASK_ABOUT_CHECK_TEXT)); - views_visible_before_checking_.push_back(label); - label->SetMultiLine(true); - label->SetMaximumWidth(400); - label->SetProperty(views::kMarginsKey, gfx::Insets::TLBR(0, 0, 10, 0)); - AddChildView(label); - - // Unretained is safe beaause this button is owned by this class. - auto* dont_ask_again = AddChildView(std::make_unique( - brave_l10n::GetLocalizedResourceUTF16String( - IDS_BRAVE_WAYBACK_MACHINE_DONT_ASK_AGAIN_TEXT), - base::BindRepeating(&WaybackMachineDialogView::OnCheckboxUpdated, - base::Unretained(this)))); - views_visible_before_checking_.push_back(dont_ask_again); - - // Use same font with label. Checkbox's default font size is a little bit - // smaller than label. - dont_ask_again->SetFontList(label->font_list()); - dont_ask_again->SetProperty(views::kMarginsKey, - gfx::Insets::TLBR(0, 0, 10, 0)); - dont_ask_again_ = dont_ask_again; - - label = CreateLabel(brave_l10n::GetLocalizedResourceUTF16String( - IDS_BRAVE_WAYBACK_MACHINE_INFOBAR_NOT_AVAILABLE_TEXT)); - views_visible_after_checking_.push_back(label); - label->SetFontList( - label->font_list().DeriveWithWeight(gfx::Font::Weight::BOLD)); - label->SetProperty(views::kMarginsKey, gfx::Insets::TLBR(0, 0, 40, 60)); - AddChildView(label); - - auto* button_row = AddChildView(std::make_unique()); - button_row - ->SetLayoutManager(std::make_unique( - views::BoxLayout::Orientation::kHorizontal, - /*inside_border_insets*/ gfx::Insets(), - /*between_child_spacing*/ 12, - /*collapse_margins_spacing*/ true)) - ->set_main_axis_alignment(views::BoxLayout::MainAxisAlignment::kEnd); - - // Unretained is safe beaause this button is owned by this class. - auto* no_thanks = - button_row->AddChildView(std::make_unique( - views::Button::PressedCallback(base::BindRepeating( - &WaybackMachineDialogView::OnCancel, base::Unretained(this))))); - no_thanks->SetKind(views::MdTextButton::Kind::kQuaternary); - no_thanks->SetText(brave_l10n::GetLocalizedResourceUTF16String( - IDS_BRAVE_WAYBACK_MACHINE_NO_THANKS_BUTTON_TEXT)); - no_thanks->SetFontSize(13); - no_thanks->SetTooltipText(brave_l10n::GetLocalizedResourceUTF16String( - IDS_BRAVE_WAYBACK_MACHINE_NO_THANKS_BUTTON_TEXT)); - no_thanks_ = no_thanks; - - fetch_url_button_ = button_row->AddChildView( - std::make_unique(base::BindRepeating( - &WaybackMachineDialogView::OnFetchURLButtonPressed, - base::Unretained(this)))); - views_visible_before_checking_.push_back(fetch_url_button_.get()); - - // Unretained is safe beaause this button is owned by this class. - auto* close = button_row->AddChildView(std::make_unique( - views::Button::PressedCallback(base::BindRepeating( - &WaybackMachineDialogView::OnCancel, base::Unretained(this))))); - views_visible_after_checking_.push_back(close); - close->SetKind(views::MdTextButton::Kind::kPrimary); - close->SetText(brave_l10n::GetLocalizedResourceUTF16String( - IDS_BRAVE_WAYBACK_MACHINE_CLOSE_BUTTON_TEXT)); - close->SetTooltipText(brave_l10n::GetLocalizedResourceUTF16String( - IDS_BRAVE_WAYBACK_MACHINE_CLOSE_BUTTON_TEXT)); - - UpdateChildrenVisibility(true); -} - -WaybackMachineDialogView::~WaybackMachineDialogView() = default; - -views::Label* WaybackMachineDialogView::CreateLabel( - const std::u16string& text) { - views::Label* label = - new views::Label(text, views::style::CONTEXT_DIALOG_BODY_TEXT); - label->SetHorizontalAlignment(gfx::ALIGN_LEFT); - return label; -} - -void WaybackMachineDialogView::UpdateChildrenVisibility( - bool show_before_checking_views) { - for (views::View* view : views_visible_before_checking_) { - view->SetVisible(show_before_checking_views); - } - for (views::View* view : views_visible_after_checking_) { - view->SetVisible(!show_before_checking_views); - } -} - -void WaybackMachineDialogView::OnCheckboxUpdated() { - pref_service_->SetBoolean(kBraveWaybackMachineEnabled, - !dont_ask_again_->GetChecked()); -} - -void WaybackMachineDialogView::OnFetchURLButtonPressed() { - if (wayback_url_fetch_requested_) { - return; - } - - // We don't need to show no thanks button anymore after fetching starts. - no_thanks_->SetVisible(false); - wayback_url_fetch_requested_ = true; - FetchWaybackURL(); -} - -void WaybackMachineDialogView::FetchWaybackURL() { - fetch_url_button_->StartThrobber(); - wayback_machine_url_fetcher_.Fetch(web_contents_->GetVisibleURL()); -} - -void WaybackMachineDialogView::LoadURL(const GURL& url) { - web_contents_->GetController().LoadURL( - url, content::Referrer(), ui::PAGE_TRANSITION_LINK, std::string()); -} - -void WaybackMachineDialogView::OnWaybackURLFetched( - const GURL& latest_wayback_url) { - DCHECK(wayback_url_fetch_requested_); - wayback_url_fetch_requested_ = false; - - fetch_url_button_->StopThrobber(); - - if (latest_wayback_url.is_empty()) { - UpdateDialogForWaybackNotAvailable(); - return; - } - - LoadURL(latest_wayback_url); - // After loading to archived url, don't need to show dialog anymore. - GetWidget()->CloseWithReason( - views::Widget::ClosedReason::kAcceptButtonClicked); -} - -void WaybackMachineDialogView::UpdateDialogForWaybackNotAvailable() { - UpdateChildrenVisibility(false); - // Update widget's size as children's visibility is changed. - GetWidget()->SetSize(GetDialogClientView()->GetPreferredSize()); -} - -void WaybackMachineDialogView::OnWillCloseDialog() { - if (auto* tab_helper = GetTabHelper(web_contents_)) { - tab_helper->set_active_dialog(nullptr); - } -} - -void WaybackMachineDialogView::OnCancel() { - GetWidget()->CloseWithReason( - views::Widget::ClosedReason::kCancelButtonClicked); -} - -BEGIN_METADATA(WaybackMachineDialogView) -END_METADATA diff --git a/browser/ui/views/wayback_machine_dialog_view.h b/browser/ui/views/wayback_machine_dialog_view.h deleted file mode 100644 index abb55c86616..00000000000 --- a/browser/ui/views/wayback_machine_dialog_view.h +++ /dev/null @@ -1,57 +0,0 @@ -/* Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/. */ - -#ifndef BRAVE_BROWSER_UI_VIEWS_WAYBACK_MACHINE_DIALOG_VIEW_H_ -#define BRAVE_BROWSER_UI_VIEWS_WAYBACK_MACHINE_DIALOG_VIEW_H_ - -#include "base/memory/raw_ptr.h" -#include "brave/components/brave_wayback_machine/wayback_machine_url_fetcher.h" -#include "ui/base/metadata/metadata_header_macros.h" -#include "ui/views/window/dialog_delegate.h" - -namespace content { -class WebContents; -} // namespace content - -namespace views { -class Checkbox; -} // namespace views - -class WaybackMachineFetchButton; -class PrefService; - -class WaybackMachineDialogView : public views::DialogDelegateView, - public WaybackMachineURLFetcher::Client { - METADATA_HEADER(WaybackMachineDialogView, views::DialogDelegateView) - public: - explicit WaybackMachineDialogView(content::WebContents* web_contents); - ~WaybackMachineDialogView() override; - - private: - // WaybackMachineURLFetcher::Client overrides: - void OnWaybackURLFetched(const GURL& latest_wayback_url) override; - - views::Label* CreateLabel(const std::u16string& text); - void UpdateChildrenVisibility(bool show_before_checking_views); - void OnCheckboxUpdated(); - void OnFetchURLButtonPressed(); - void FetchWaybackURL(); - void LoadURL(const GURL& url); - void UpdateDialogForWaybackNotAvailable(); - void OnWillCloseDialog(); - void OnCancel(); - - views::View::Views views_visible_before_checking_; - views::View::Views views_visible_after_checking_; - raw_ptr dont_ask_again_ = nullptr; - raw_ptr no_thanks_ = nullptr; - raw_ptr fetch_url_button_ = nullptr; - raw_ptr web_contents_ = nullptr; - WaybackMachineURLFetcher wayback_machine_url_fetcher_; - const raw_ptr pref_service_ = nullptr; - bool wayback_url_fetch_requested_ = false; -}; - -#endif // BRAVE_BROWSER_UI_VIEWS_WAYBACK_MACHINE_DIALOG_VIEW_H_ diff --git a/browser/ui/views/wayback_machine_fetch_button.h b/browser/ui/views/wayback_machine_fetch_button.h deleted file mode 100644 index 0aa844f6907..00000000000 --- a/browser/ui/views/wayback_machine_fetch_button.h +++ /dev/null @@ -1,44 +0,0 @@ -/* Copyright (c) 2019 The Brave Authors. All rights reserved. - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this file, - * You can obtain one at https://mozilla.org/MPL/2.0/. */ - -#ifndef BRAVE_BROWSER_UI_VIEWS_WAYBACK_MACHINE_FETCH_BUTTON_H_ -#define BRAVE_BROWSER_UI_VIEWS_WAYBACK_MACHINE_FETCH_BUTTON_H_ - -#include "base/memory/raw_ptr.h" -#include "ui/base/metadata/metadata_header_macros.h" -#include "ui/views/controls/button/button.h" -#include "ui/views/view.h" - -class WaybackMachineThrobber; - -// This manages button and throbber controls. -// buttons occupies all this containers area and throbber runs over the button. -// When throbbing is requested, button extends its right inset and throbber runs -// on that area. -class WaybackMachineFetchButton : public views::View { - METADATA_HEADER(WaybackMachineFetchButton, views::View) - public: - explicit WaybackMachineFetchButton(views::Button::PressedCallback callback); - ~WaybackMachineFetchButton() override; - - WaybackMachineFetchButton(const WaybackMachineFetchButton&) = delete; - WaybackMachineFetchButton& operator=(const WaybackMachineFetchButton&) = - delete; - - void StartThrobber(); - void StopThrobber(); - - // views::View overrides: - void Layout(PassKey) override; - gfx::Size CalculatePreferredSize() const override; - - private: - void AdjustButtonInsets(bool add_insets); - - raw_ptr throbber_ = nullptr; - raw_ptr button_ = nullptr; -}; - -#endif // BRAVE_BROWSER_UI_VIEWS_WAYBACK_MACHINE_FETCH_BUTTON_H_ diff --git a/chromium_src/components/infobars/core/infobar_delegate.h b/chromium_src/components/infobars/core/infobar_delegate.h index 670712f7095..4ee10b25c38 100644 --- a/chromium_src/components/infobars/core/infobar_delegate.h +++ b/chromium_src/components/infobars/core/infobar_delegate.h @@ -13,7 +13,8 @@ // automatically #define BRAVE_INFOBAR_DELEGATE_IDENTIFIERS \ - BRAVE_CONFIRM_P3A_INFOBAR_DELEGATE = 500, SYNC_CANNOT_RUN_INFOBAR = 505, \ + BRAVE_CONFIRM_P3A_INFOBAR_DELEGATE = 500, \ + WAYBACK_MACHINE_INFOBAR_DELEGATE = 502, SYNC_CANNOT_RUN_INFOBAR = 505, \ WEB_DISCOVERY_INFOBAR_DELEGATE = 506, \ BRAVE_SYNC_ACCOUNT_DELETED_INFOBAR = 507, \ BRAVE_REQUEST_OTR_INFOBAR_DELEGATE = 508, BRAVE_IPFS_INFOBAR_DELEGATE = 509, \ @@ -22,7 +23,6 @@ BRAVE_IPFS_ALWAYS_START_INFOBAR_DELEGATE = 512, // Deprecated: -// WAYBACK_MACHINE_INFOBAR_DELEGATE = 502 // SYNC_V2_MIGRATE_INFOBAR_DELEGATE = 503 // ANDROID_SYSTEM_SYNC_DISABLED_INFOBAR = 504 diff --git a/components/brave_wayback_machine/BUILD.gn b/components/brave_wayback_machine/BUILD.gn index ee49ccb6ae5..e2cd8a1fc62 100644 --- a/components/brave_wayback_machine/BUILD.gn +++ b/components/brave_wayback_machine/BUILD.gn @@ -12,6 +12,8 @@ static_library("brave_wayback_machine") { sources = [ "brave_wayback_machine_delegate.h", + "brave_wayback_machine_infobar_delegate.cc", + "brave_wayback_machine_infobar_delegate.h", "brave_wayback_machine_tab_helper.cc", "brave_wayback_machine_tab_helper.h", "brave_wayback_machine_utils.cc", @@ -25,6 +27,8 @@ static_library("brave_wayback_machine") { deps = [ "//base", "//brave/components/api_request_helper", + "//components/infobars/content", + "//components/infobars/core", "//components/prefs", "//components/user_prefs", "//content/public/browser", diff --git a/components/brave_wayback_machine/DEPS b/components/brave_wayback_machine/DEPS index d8b1e0bfc3b..b51df550579 100644 --- a/components/brave_wayback_machine/DEPS +++ b/components/brave_wayback_machine/DEPS @@ -2,5 +2,4 @@ include_rules = [ "+content/public/browser", "+content/public/common", "+services/network/public", - "+ui/gfx/native_widget_types.h", ] diff --git a/components/brave_wayback_machine/brave_wayback_machine_browsertest.cc b/components/brave_wayback_machine/brave_wayback_machine_browsertest.cc index 5a6f0e73189..11218430f34 100644 --- a/components/brave_wayback_machine/brave_wayback_machine_browsertest.cc +++ b/components/brave_wayback_machine/brave_wayback_machine_browsertest.cc @@ -4,24 +4,40 @@ * You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "brave/components/brave_wayback_machine/brave_wayback_machine_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 "net/http/http_status_code.h" +#include "testing/gmock/include/gmock/gmock.h" using BraveWaybackMachineTest = InProcessBrowserTest; +using ::testing::_; -IN_PROC_BROWSER_TEST_F(BraveWaybackMachineTest, DialogLaunchTest) { +namespace { + +class TestObserver : public infobars::InfoBarManager::Observer { + public: + TestObserver() = default; + ~TestObserver() override = default; + MOCK_METHOD1(OnInfoBarAdded, void(infobars::InfoBar* infobar)); +}; + +} // namespace + +IN_PROC_BROWSER_TEST_F(BraveWaybackMachineTest, InfobarAddTest) { auto* model = browser()->tab_strip_model(); auto* contents = model->GetActiveWebContents(); - BraveWaybackMachineTabHelper* tab_helper = - BraveWaybackMachineTabHelper::FromWebContents(contents); - EXPECT_FALSE(tab_helper->ShouldShowWaybackMachineDialog(net::HTTP_OK)); - EXPECT_TRUE(tab_helper->ShouldShowWaybackMachineDialog(net::HTTP_NOT_FOUND)); - tab_helper->ShowWaybackMachineDialog(); + auto* tab_helper = BraveWaybackMachineTabHelper::FromWebContents(contents); + auto* infobar_manager = + infobars::ContentInfoBarManager::FromWebContents(contents); - // Check dialog is launched. - EXPECT_TRUE(!!tab_helper->active_dialog()); + TestObserver observer; + EXPECT_CALL(observer, OnInfoBarAdded(_)).Times(1); + infobar_manager->AddObserver(&observer); + tab_helper->CreateInfoBar(); + infobar_manager->RemoveObserver(&observer); } diff --git a/components/brave_wayback_machine/brave_wayback_machine_delegate.h b/components/brave_wayback_machine/brave_wayback_machine_delegate.h index 55bc811e965..553a5ad8a20 100644 --- a/components/brave_wayback_machine/brave_wayback_machine_delegate.h +++ b/components/brave_wayback_machine/brave_wayback_machine_delegate.h @@ -11,11 +11,13 @@ namespace content { class WebContents; } // namespace content +class BraveWaybackMachineInfoBarDelegate; + class BraveWaybackMachineDelegate { public: virtual ~BraveWaybackMachineDelegate() = default; - virtual void ShowWaybackMachineDialog(content::WebContents* web_contents) = 0; + virtual void CreateInfoBar(content::WebContents* web_contents) = 0; }; #endif // BRAVE_COMPONENTS_BRAVE_WAYBACK_MACHINE_BRAVE_WAYBACK_MACHINE_DELEGATE_H_ diff --git a/components/brave_wayback_machine/brave_wayback_machine_infobar_delegate.cc b/components/brave_wayback_machine/brave_wayback_machine_infobar_delegate.cc new file mode 100644 index 00000000000..0294643e2cf --- /dev/null +++ b/components/brave_wayback_machine/brave_wayback_machine_infobar_delegate.cc @@ -0,0 +1,23 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/components/brave_wayback_machine/brave_wayback_machine_infobar_delegate.h" + +using InfoBarIdentifier = infobars::InfoBarDelegate::InfoBarIdentifier; + +BraveWaybackMachineInfoBarDelegate:: +BraveWaybackMachineInfoBarDelegate() = default; + +BraveWaybackMachineInfoBarDelegate:: +~BraveWaybackMachineInfoBarDelegate() = default; + +InfoBarIdentifier BraveWaybackMachineInfoBarDelegate::GetIdentifier() const { + return WAYBACK_MACHINE_INFOBAR_DELEGATE; +} + +bool BraveWaybackMachineInfoBarDelegate::EqualsDelegate( + infobars::InfoBarDelegate* delegate) const { + return delegate->GetIdentifier() == GetIdentifier(); +} diff --git a/components/brave_wayback_machine/brave_wayback_machine_infobar_delegate.h b/components/brave_wayback_machine/brave_wayback_machine_infobar_delegate.h new file mode 100644 index 00000000000..2e2d1846769 --- /dev/null +++ b/components/brave_wayback_machine/brave_wayback_machine_infobar_delegate.h @@ -0,0 +1,30 @@ +/* Copyright (c) 2019 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_COMPONENTS_BRAVE_WAYBACK_MACHINE_BRAVE_WAYBACK_MACHINE_INFOBAR_DELEGATE_H_ +#define BRAVE_COMPONENTS_BRAVE_WAYBACK_MACHINE_BRAVE_WAYBACK_MACHINE_INFOBAR_DELEGATE_H_ + +#include + +#include "components/infobars/core/infobar_delegate.h" + +class BraveWaybackMachineInfoBarDelegate : public infobars::InfoBarDelegate { + public: + BraveWaybackMachineInfoBarDelegate(); + ~BraveWaybackMachineInfoBarDelegate() override; + + BraveWaybackMachineInfoBarDelegate( + const BraveWaybackMachineInfoBarDelegate&) = delete; + BraveWaybackMachineInfoBarDelegate& operator=( + const BraveWaybackMachineInfoBarDelegate&) = delete; + + private: + // infobars::InfoBarDelegate overrides: + InfoBarIdentifier GetIdentifier() const override; + bool EqualsDelegate( + infobars::InfoBarDelegate* delegate) const override; +}; + +#endif // BRAVE_COMPONENTS_BRAVE_WAYBACK_MACHINE_BRAVE_WAYBACK_MACHINE_INFOBAR_DELEGATE_H_ diff --git a/components/brave_wayback_machine/brave_wayback_machine_tab_helper.cc b/components/brave_wayback_machine/brave_wayback_machine_tab_helper.cc index 2071de42b11..6c9479bdfc7 100644 --- a/components/brave_wayback_machine/brave_wayback_machine_tab_helper.cc +++ b/components/brave_wayback_machine/brave_wayback_machine_tab_helper.cc @@ -60,28 +60,26 @@ void BraveWaybackMachineTabHelper::DidFinishNavigation( if (const net::HttpResponseHeaders* header = navigation_handle->GetResponseHeaders()) { - if (!ShouldShowWaybackMachineDialog(header->response_code())) { + if (!ShouldAttachWaybackMachineInfoBar(header->response_code())) return; - } // Create infobar in the next loop for not blocking navigation. base::SequencedTaskRunner::GetCurrentDefault()->PostTask( - FROM_HERE, - base::BindOnce(&BraveWaybackMachineTabHelper::ShowWaybackMachineDialog, - weak_factory_.GetWeakPtr())); + FROM_HERE, base::BindOnce(&BraveWaybackMachineTabHelper::CreateInfoBar, + weak_factory_.GetWeakPtr())); } } -void BraveWaybackMachineTabHelper::ShowWaybackMachineDialog() { +void BraveWaybackMachineTabHelper::CreateInfoBar() { DCHECK(delegate_); - delegate_->ShowWaybackMachineDialog(web_contents()); + delegate_->CreateInfoBar(web_contents()); } bool BraveWaybackMachineTabHelper::IsWaybackMachineEnabled() const { return pref_service_->GetBoolean(kBraveWaybackMachineEnabled); } -bool BraveWaybackMachineTabHelper::ShouldShowWaybackMachineDialog( +bool BraveWaybackMachineTabHelper::ShouldAttachWaybackMachineInfoBar( int response_code) const { static base::flat_set responses = { net::HTTP_NOT_FOUND, // 404 diff --git a/components/brave_wayback_machine/brave_wayback_machine_tab_helper.h b/components/brave_wayback_machine/brave_wayback_machine_tab_helper.h index 2d1574cd02d..69d0fccca29 100644 --- a/components/brave_wayback_machine/brave_wayback_machine_tab_helper.h +++ b/components/brave_wayback_machine/brave_wayback_machine_tab_helper.h @@ -13,7 +13,6 @@ #include "base/memory/weak_ptr.h" #include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_user_data.h" -#include "ui/gfx/native_widget_types.h" class BraveWaybackMachineDelegate; class PrefService; @@ -30,30 +29,25 @@ class BraveWaybackMachineTabHelper const BraveWaybackMachineTabHelper&) = delete; void set_delegate(std::unique_ptr delegate); - void set_active_dialog(gfx::NativeWindow dialog) { active_dialog_ = dialog; } - gfx::NativeWindow active_dialog() const { return active_dialog_; } + WEB_CONTENTS_USER_DATA_KEY_DECL(); private: - FRIEND_TEST_ALL_PREFIXES(BraveWaybackMachineTest, DialogLaunchTest); + FRIEND_TEST_ALL_PREFIXES(BraveWaybackMachineTest, InfobarAddTest); // content::WebContentsObserver overrides: void DidFinishNavigation( content::NavigationHandle* navigation_handle) override; - void ShowWaybackMachineDialog(); + void CreateInfoBar(); bool IsWaybackMachineEnabled() const; - bool ShouldShowWaybackMachineDialog(int response_code) const; + // virtual for test. + virtual bool ShouldAttachWaybackMachineInfoBar(int response_code) const; - // If |active_dialog_| exists, close it before launching another one. - gfx::NativeWindow active_dialog_ = nullptr; raw_ref pref_service_; std::unique_ptr delegate_; base::WeakPtrFactory weak_factory_; - - friend WebContentsUserData; - WEB_CONTENTS_USER_DATA_KEY_DECL(); }; #endif // BRAVE_COMPONENTS_BRAVE_WAYBACK_MACHINE_BRAVE_WAYBACK_MACHINE_TAB_HELPER_H_