Merge pull request #22584 from brave/revert-22395-wayback_machine_dialog

Revert "Use dialog for wayback machine instead of infobar"
This commit is contained in:
Brian Clifton
2024-03-14 01:13:09 -07:00
committed by GitHub
27 changed files with 223 additions and 449 deletions
@@ -12,16 +12,19 @@ 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.SYNC_CANNOT_RUN_INFOBAR,
BraveInfoBarIdentifier.WEB_DISCOVERY_INFOBAR_DELEGATE,
BraveInfoBarIdentifier.BRAVE_SYNC_ACCOUNT_DELETED_INFOBAR})
@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
})
@Retention(RetentionPolicy.SOURCE)
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;
-6
View File
@@ -928,12 +928,6 @@ Or change later at <ph name="SETTINGS_EXTENIONS_LINK">$2<ex>brave://settings/ext
<message name="IDS_BRAVE_WAYBACK_MACHINE_DONT_ASK_AGAIN_TEXT" desc="Text to show for the don't ask again button.">
Don't ask me again
</message>
<message name="IDS_BRAVE_WAYBACK_MACHINE_NO_THANKS_BUTTON_TEXT" desc="Text to show for the no thanks button.">
No thanks
</message>
<message name="IDS_BRAVE_WAYBACK_MACHINE_CLOSE_BUTTON_TEXT" desc="Text to show for the close button.">
Close
</message>
<!-- Brave's copyright statement -->
<message name="IDS_BRAVE_VERSION_UI_LICENSE" desc="The label below the copyright message, containing the URLs.">
+2
View File
@@ -26,6 +26,8 @@
<structure type="chrome_scaled_image" name="IDR_BRAVE_BOOKMARK_FOLDER_OPEN_LIN_LIGHT" file="common/brave_bookmark_folder_open_lin_light.png" />
<structure type="chrome_scaled_image" name="IDR_BRAVE_BOOKMARK_FOLDER_OPEN_WIN_DARK" file="common/brave_bookmark_folder_open_win_dark.png" />
<structure type="chrome_scaled_image" name="IDR_BRAVE_BOOKMARK_FOLDER_OPEN_WIN_LIGHT" file="common/brave_bookmark_folder_open_win_light.png" />
<structure type="chrome_scaled_image" name="IDR_BRAVE_WAYBACK_INFOBAR" file="brave/brave_wayback_infobar.png" />
<structure type="chrome_scaled_image" name="IDR_BRAVE_WAYBACK_INFOBAR_DARK" file="brave/brave_wayback_infobar_dark.png" />
</if>
<if expr="enable_ai_chat">
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

@@ -5,12 +5,12 @@
#include "brave/browser/infobars/brave_wayback_machine_delegate_impl.h"
#include <memory>
#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,11 @@ 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<BraveWaybackMachineInfoBarDelegate>(),
web_contents),
true);
}
@@ -6,13 +6,20 @@
#ifndef BRAVE_BROWSER_INFOBARS_BRAVE_WAYBACK_MACHINE_DELEGATE_IMPL_H_
#define BRAVE_BROWSER_INFOBARS_BRAVE_WAYBACK_MACHINE_DELEGATE_IMPL_H_
#include <memory>
#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 BraveWaybackMachineInfoBarDelegate;
class BraveWaybackMachineDelegateImpl : public BraveWaybackMachineDelegate {
public:
static void AttachTabHelperIfNeeded(content::WebContents* web_contents);
@@ -27,7 +34,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<infobars::InfoBar> CreateInfoBarView(
std::unique_ptr<BraveWaybackMachineInfoBarDelegate> delegate,
content::WebContents* contents);
};
#endif // BRAVE_BROWSER_INFOBARS_BRAVE_WAYBACK_MACHINE_DELEGATE_IMPL_H_
+1 -8
View File
@@ -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" ]
}
}
-3
View File
@@ -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_
+46
View File
@@ -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
@@ -3,12 +3,12 @@
* 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_fetch_button.h"
#include "brave/browser/ui/views/infobars/brave_wayback_machine_infobar_button_container.h"
#include <memory>
#include <utility>
#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<views::MdTextButton>(
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
@@ -3,29 +3,31 @@
* 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_
#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 WaybackMachineThrobber;
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 WaybackMachineFetchButton : public views::View {
METADATA_HEADER(WaybackMachineFetchButton, views::View)
class BraveWaybackMachineInfoBarButtonContainer : public views::View {
METADATA_HEADER(BraveWaybackMachineInfoBarButtonContainer, views::View)
public:
explicit WaybackMachineFetchButton(views::Button::PressedCallback callback);
~WaybackMachineFetchButton() override;
explicit BraveWaybackMachineInfoBarButtonContainer(
views::Button::PressedCallback callback);
~BraveWaybackMachineInfoBarButtonContainer() override;
WaybackMachineFetchButton(const WaybackMachineFetchButton&) = delete;
WaybackMachineFetchButton& operator=(const WaybackMachineFetchButton&) =
delete;
BraveWaybackMachineInfoBarButtonContainer(
const BraveWaybackMachineInfoBarButtonContainer&) = delete;
BraveWaybackMachineInfoBarButtonContainer& operator=(
const BraveWaybackMachineInfoBarButtonContainer&) = delete;
void StartThrobber();
void StopThrobber();
@@ -37,8 +39,8 @@ class WaybackMachineFetchButton : public views::View {
private:
void AdjustButtonInsets(bool add_insets);
raw_ptr<WaybackMachineThrobber> throbber_ = nullptr;
raw_ptr<BraveWaybackMachineInfoBarThrobber> throbber_ = nullptr;
raw_ptr<views::View> button_ = nullptr;
};
#endif // BRAVE_BROWSER_UI_VIEWS_WAYBACK_MACHINE_FETCH_BUTTON_H_
#endif // BRAVE_BROWSER_UI_VIEWS_INFOBARS_BRAVE_WAYBACK_MACHINE_INFOBAR_BUTTON_CONTAINER_H_
@@ -3,7 +3,7 @@
* 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_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
@@ -3,8 +3,8 @@
* 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_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_
@@ -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 <memory>
#include <string>
#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<views::FlexLayout>())
->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<DontAskAgainCheckbox>(
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<views::View>());
button_row
->SetLayoutManager(std::make_unique<views::BoxLayout>(
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<CustomMdTextButton>(
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<WaybackMachineFetchButton>(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<CustomMdTextButton>(
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
@@ -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<views::Checkbox> dont_ask_again_ = nullptr;
raw_ptr<views::View> no_thanks_ = nullptr;
raw_ptr<WaybackMachineFetchButton> fetch_url_button_ = nullptr;
raw_ptr<content::WebContents> web_contents_ = nullptr;
WaybackMachineURLFetcher wayback_machine_url_fetcher_;
const raw_ptr<PrefService> pref_service_ = nullptr;
bool wayback_url_fetch_requested_ = false;
};
#endif // BRAVE_BROWSER_UI_VIEWS_WAYBACK_MACHINE_DIALOG_VIEW_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
@@ -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",
-1
View File
@@ -2,5 +2,4 @@ include_rules = [
"+content/public/browser",
"+content/public/common",
"+services/network/public",
"+ui/gfx/native_widget_types.h",
]
@@ -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);
}
@@ -1,7 +1,7 @@
/* 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/. */
* You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef BRAVE_COMPONENTS_BRAVE_WAYBACK_MACHINE_BRAVE_WAYBACK_MACHINE_DELEGATE_H_
#define BRAVE_COMPONENTS_BRAVE_WAYBACK_MACHINE_BRAVE_WAYBACK_MACHINE_DELEGATE_H_
@@ -15,7 +15,7 @@ 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_
@@ -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 https://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();
}
@@ -0,0 +1,27 @@
/* 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_COMPONENTS_BRAVE_WAYBACK_MACHINE_BRAVE_WAYBACK_MACHINE_INFOBAR_DELEGATE_H_
#define BRAVE_COMPONENTS_BRAVE_WAYBACK_MACHINE_BRAVE_WAYBACK_MACHINE_INFOBAR_DELEGATE_H_
#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_
@@ -60,28 +60,27 @@ 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<int> responses = {
net::HTTP_NOT_FOUND, // 404
@@ -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,26 @@ class BraveWaybackMachineTabHelper
const BraveWaybackMachineTabHelper&) = delete;
void set_delegate(std::unique_ptr<BraveWaybackMachineDelegate> 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<PrefService> pref_service_;
std::unique_ptr<BraveWaybackMachineDelegate> delegate_;
base::WeakPtrFactory<BraveWaybackMachineTabHelper> weak_factory_;
friend WebContentsUserData;
WEB_CONTENTS_USER_DATA_KEY_DECL();
};
#endif // BRAVE_COMPONENTS_BRAVE_WAYBACK_MACHINE_BRAVE_WAYBACK_MACHINE_TAB_HELPER_H_