Commit fa8ab985 authored by Elly Fong-Jones's avatar Elly Fong-Jones Committed by Commit Bot

cbuiv: use closure callbacks in Chrome Cleaner dialogs

One of these retains a callback method on the object; the other uses the
lambda-captures-&controller pattern.

Bug: 1011446
Change-Id: Ic5ea4f6231a94e3e6d14d646bb78f00220e90ad0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2032231Reviewed-by: default avatarPeter Boström <pbos@chromium.org>
Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#737457}
parent 6ed220a3
......@@ -66,6 +66,16 @@ ChromeCleanerDialog::ChromeCleanerDialog(
this, l10n_util::GetStringUTF16(
IDS_CHROME_CLEANUP_PROMPT_DETAILS_BUTTON_LABEL)));
DialogDelegate::set_accept_callback(
base::BindOnce(&ChromeCleanerDialog::HandleDialogInteraction,
base::Unretained(this), DialogInteractionResult::kAccept));
DialogDelegate::set_cancel_callback(
base::BindOnce(&ChromeCleanerDialog::HandleDialogInteraction,
base::Unretained(this), DialogInteractionResult::kCancel));
DialogDelegate::set_close_callback(
base::BindOnce(&ChromeCleanerDialog::HandleDialogInteraction,
base::Unretained(this), DialogInteractionResult::kClose));
ChromeLayoutProvider* layout_provider = ChromeLayoutProvider::Get();
set_margins(
layout_provider->GetDialogInsetsForContentType(views::TEXT, views::TEXT));
......@@ -142,23 +152,6 @@ bool ChromeCleanerDialog::ShouldShowCloseButton() const {
return false;
}
// DialogDelegate overrides.
bool ChromeCleanerDialog::Accept() {
HandleDialogInteraction(DialogInteractionResult::kAccept);
return true;
}
bool ChromeCleanerDialog::Cancel() {
HandleDialogInteraction(DialogInteractionResult::kCancel);
return true;
}
bool ChromeCleanerDialog::Close() {
HandleDialogInteraction(DialogInteractionResult::kClose);
return true;
}
// View overrides.
gfx::Size ChromeCleanerDialog::CalculatePreferredSize() const {
......
......@@ -54,11 +54,6 @@ class ChromeCleanerDialog
views::View* GetInitiallyFocusedView() override;
bool ShouldShowCloseButton() const override;
// views::DialogDelegate overrides.
bool Accept() override;
bool Cancel() override;
bool Close() override;
// views::View overrides.
gfx::Size CalculatePreferredSize() const override;
......
......@@ -54,16 +54,37 @@ ChromeCleanerRebootDialog::ChromeCleanerRebootDialog(
l10n_util::GetStringUTF16(
IDS_CHROME_CLEANUP_REBOOT_PROMPT_RESTART_BUTTON_LABEL));
using Controller = safe_browsing::ChromeCleanerRebootDialogController;
using ControllerClosureFn = void (Controller::*)(void);
auto close_callback = [](Controller** controller, ControllerClosureFn fn) {
// This lambda gets bound later to form callbacks for the dialog's close
// methods (Accept, Cancel, Close). At most one of these three callbacks may
// be invoked, so it swaps this instance's controller pointer with nullptr,
// which inhibits a second callback to the controller in
// ~ChromeCleanerRebootDialog.
(std::exchange(*controller, nullptr)->*(fn))();
};
DialogDelegate::set_accept_callback(
base::BindOnce(close_callback, base::Unretained(&dialog_controller_),
&Controller::Accept));
DialogDelegate::set_cancel_callback(
base::BindOnce(close_callback, base::Unretained(&dialog_controller_),
&Controller::Cancel));
DialogDelegate::set_close_callback(
base::BindOnce(close_callback, base::Unretained(&dialog_controller_),
&Controller::Close));
set_margins(ChromeLayoutProvider::Get()->GetDialogInsetsForContentType(
views::TEXT, views::TEXT));
}
ChromeCleanerRebootDialog::~ChromeCleanerRebootDialog() {
// Make sure the controller is correctly notified in case the dialog widget is
// closed by some other means than the dialog buttons.
if (dialog_controller_) {
HandleDialogInteraction(DialogInteractionResult::kClosedOnDestruction);
}
// If the controller is still non-null, none of the dialog's closure methods
// have run - see this class's constructor. In that case, notify the
// controller that this dialog is going away.
if (dialog_controller_)
std::exchange(dialog_controller_, nullptr)->Close();
}
void ChromeCleanerRebootDialog::Show(Browser* browser) {
......@@ -93,43 +114,6 @@ views::View* ChromeCleanerRebootDialog::GetInitiallyFocusedView() {
return GetCancelButton();
}
// DialogDelegate overrides.
bool ChromeCleanerRebootDialog::Accept() {
HandleDialogInteraction(DialogInteractionResult::kAccept);
return true;
}
bool ChromeCleanerRebootDialog::Cancel() {
HandleDialogInteraction(DialogInteractionResult::kCancel);
return true;
}
bool ChromeCleanerRebootDialog::Close() {
HandleDialogInteraction(DialogInteractionResult::kClose);
return true;
}
void ChromeCleanerRebootDialog::HandleDialogInteraction(
DialogInteractionResult result) {
if (!dialog_controller_)
return;
switch (result) {
case DialogInteractionResult::kAccept:
dialog_controller_->Accept();
break;
case DialogInteractionResult::kCancel:
dialog_controller_->Cancel();
break;
case DialogInteractionResult::kClose:
case DialogInteractionResult::kClosedOnDestruction:
// Fallthrough.
dialog_controller_->Close();
break;
}
dialog_controller_ = nullptr;
}
gfx::Rect ChromeCleanerRebootDialog::GetDialogBounds(Browser* browser) const {
gfx::Rect browser_bounds = browser->window()->GetBounds();
return gfx::Rect(
......
......@@ -39,21 +39,7 @@ class ChromeCleanerRebootDialog : public views::DialogDelegateView {
base::string16 GetWindowTitle() const override;
views::View* GetInitiallyFocusedView() override;
// views::DialogDelegate overrides.
bool Accept() override;
bool Cancel() override;
bool Close() override;
private:
enum class DialogInteractionResult {
kAccept,
kCancel,
kClose,
kClosedOnDestruction,
};
void HandleDialogInteraction(DialogInteractionResult result);
gfx::Rect GetDialogBounds(Browser* browser) const;
// The pointer will be set to nullptr once the controller has been notified of
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment