Skip to content

Commit

Permalink
cbuiv extensions: migrate to new WidgetDelegate API
Browse files Browse the repository at this point in the history
This change largely migrates the classes in this directory from the older
override-based interface to WidgetDelegate to the newer setter-based
interface.

Bug: 1075649
Change-Id: Ia82d7684ebc138a49107d9e1ec944f6727850d03
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2275494
Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Reilly Grant <reillyg@chromium.org>
Reviewed-by: Devlin <rdevlin.cronin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#789044}
  • Loading branch information
Elly Fong-Jones authored and Commit Bot committed Jul 16, 2020
1 parent 1826234 commit 1ec06c6
Show file tree
Hide file tree
Showing 19 changed files with 74 additions and 186 deletions.
2 changes: 2 additions & 0 deletions chrome/browser/chooser_controller/chooser_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class ChooserController {
};

// Returns the text to be displayed in the chooser title.
// Note that this is only called once, and there is no way to update the title
// for a given instance of ChooserController.
base::string16 GetTitle() const;

// Returns whether the chooser needs to show an icon before the text.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class MediaGalleriesDialogController {

typedef std::vector<Entry> Entries;

// The title of the dialog view.
// The title of the dialog view. Note that this is only guaranteed to be
// called once, and once called there is no way to update the title text.
virtual base::string16 GetHeader() const = 0;

// Explanatory text directly below the title.
Expand Down
3 changes: 3 additions & 0 deletions chrome/browser/ui/views/device_chooser_content_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class DeviceChooserContentView : public views::View,
// views::ButtonListener:
void ButtonPressed(views::Button* sender, const ui::Event& event) override;

// Note that there is no way to update the window title - for any given
// instance of DeviceChooserContentView, this method is only called once to
// initially set the window title.
base::string16 GetWindowTitle() const;
std::unique_ptr<views::View> CreateExtraView();
bool IsDialogButtonEnabled(ui::DialogButton button) const;
Expand Down
10 changes: 2 additions & 8 deletions chrome/browser/ui/views/extensions/chooser_dialog_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ ChooserDialogView::ChooserDialogView(
views::CONTROL, views::CONTROL)));

SetExtraView(device_chooser_content_view_->CreateExtraView());
SetShowCloseButton(false);
SetTitle(device_chooser_content_view_->GetWindowTitle());

SetAcceptCallback(
base::BindOnce(&DeviceChooserContentView::Accept,
Expand All @@ -70,14 +72,6 @@ ChooserDialogView::ChooserDialogView(

ChooserDialogView::~ChooserDialogView() = default;

base::string16 ChooserDialogView::GetWindowTitle() const {
return device_chooser_content_view_->GetWindowTitle();
}

bool ChooserDialogView::ShouldShowCloseButton() const {
return false;
}

ui::ModalType ChooserDialogView::GetModalType() const {
return ui::MODAL_TYPE_CHILD;
}
Expand Down
2 changes: 0 additions & 2 deletions chrome/browser/ui/views/extensions/chooser_dialog_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ class ChooserDialogView : public views::DialogDelegateView,
~ChooserDialogView() override;

// views::WidgetDelegate:
base::string16 GetWindowTitle() const override;
bool ShouldShowCloseButton() const override;
ui::ModalType GetModalType() const override;

// views::DialogDelegate:
Expand Down
35 changes: 15 additions & 20 deletions chrome/browser/ui/views/extensions/extension_dialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,10 @@ void ExtensionDialog::SetMinimumContentsSize(int width, int height) {
extension_view_->SetPreferredSize(gfx::Size(width, height));
}

bool ExtensionDialog::CanResize() const {
#if defined(OS_CHROMEOS)
// Prevent dialog resize mouse cursor in tablet mode, crbug.com/453634.
if (ash::TabletMode::Get() && ash::TabletMode::Get()->InTabletMode())
return false;
#endif
return true;
}

ui::ModalType ExtensionDialog::GetModalType() const {
return ui::MODAL_TYPE_WINDOW;
}

bool ExtensionDialog::ShouldShowWindowTitle() const {
return !window_title_.empty();
}

base::string16 ExtensionDialog::GetWindowTitle() const {
return window_title_;
}

void ExtensionDialog::WindowClosing() {
if (observer_)
observer_->ExtensionDialogClosing(this);
Expand All @@ -113,12 +96,15 @@ void ExtensionDialog::DeleteDelegate() {
Release();
}

// TODO(ellyjones): Are either of these overrides necessary? It seems like
// extension_view_ is always this dialog's contents view, in which case
// GetWidget will already behave this way.
views::Widget* ExtensionDialog::GetWidget() {
return extension_view_->GetWidget();
return extension_view_ ? extension_view_->GetWidget() : nullptr;
}

const views::Widget* ExtensionDialog::GetWidget() const {
return extension_view_->GetWidget();
return extension_view_ ? extension_view_->GetWidget() : nullptr;
}

views::View* ExtensionDialog::GetContentsView() {
Expand Down Expand Up @@ -190,7 +176,16 @@ ExtensionDialog::ExtensionDialog(
source);
chrome::RecordDialogCreation(chrome::DialogIdentifier::EXTENSION);

set_title(init_params.title);
SetShowTitle(!init_params.title.empty());
SetTitle(init_params.title);

bool can_resize = true;
#if defined(OS_CHROMEOS)
// Prevent dialog resize mouse cursor in tablet mode, crbug.com/453634.
if (ash::TabletMode::Get() && ash::TabletMode::Get()->InTabletMode())
can_resize = false;
#endif
SetCanResize(can_resize);

views::Widget* window =
init_params.is_modal
Expand Down
6 changes: 0 additions & 6 deletions chrome/browser/ui/views/extensions/extension_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,13 @@ class ExtensionDialog : public views::DialogDelegate,
// Focus to the render view if possible.
void MaybeFocusRenderView();

// Sets the window title.
void set_title(const base::string16& title) { window_title_ = title; }

// Sets minimum contents size in pixels and makes the window resizable.
void SetMinimumContentsSize(int width, int height);

extensions::ExtensionViewHost* host() const { return host_.get(); }

// views::DialogDelegate:
bool CanResize() const override;
ui::ModalType GetModalType() const override;
bool ShouldShowWindowTitle() const override;
base::string16 GetWindowTitle() const override;
void WindowClosing() override;
void DeleteDelegate() override;
views::Widget* GetWidget() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,24 @@ ExtensionInstallBlockedDialogView::ExtensionInstallBlockedDialogView(
const base::string16& custom_error_message,
const gfx::ImageSkia& icon,
base::OnceClosure done_callback)
: title_(l10n_util::GetStringFUTF16(
IDS_EXTENSION_BLOCKED_BY_POLICY_PROMPT_TITLE,
base::UTF8ToUTF16(extension_name))),
custom_error_message_(custom_error_message),
icon_(gfx::ImageSkiaOperations::CreateResizedImage(
icon,
skia::ImageOperations::ResizeMethod::RESIZE_BEST,
gfx::Size(extension_misc::EXTENSION_ICON_SMALL,
extension_misc::EXTENSION_ICON_SMALL))),
done_callback_(std::move(done_callback)) {
: done_callback_(std::move(done_callback)) {
SetButtons(ui::DIALOG_BUTTON_CANCEL);
SetDefaultButton(ui::DIALOG_BUTTON_CANCEL);
SetButtonLabel(ui::DIALOG_BUTTON_CANCEL,
l10n_util::GetStringUTF16(IDS_CLOSE));
SetShowIcon(true);
SetIcon(gfx::ImageSkiaOperations::CreateResizedImage(
icon, skia::ImageOperations::ResizeMethod::RESIZE_BEST,
gfx::Size(extension_misc::EXTENSION_ICON_SMALL,
extension_misc::EXTENSION_ICON_SMALL)));
SetTitle(
l10n_util::GetStringFUTF16(IDS_EXTENSION_BLOCKED_BY_POLICY_PROMPT_TITLE,
base::UTF8ToUTF16(extension_name)));
set_draggable(true);
set_close_on_deactivate(false);
AddCustomMessageContents();
SetLayoutManager(std::make_unique<views::FillLayout>());
if (!custom_error_message.empty())
AddCustomMessageContents(custom_error_message);
}

ExtensionInstallBlockedDialogView::~ExtensionInstallBlockedDialogView() {
Expand All @@ -88,35 +89,15 @@ gfx::Size ExtensionInstallBlockedDialogView::CalculatePreferredSize() const {
return gfx::Size(width, GetHeightForWidth(width));
}

bool ExtensionInstallBlockedDialogView::IsDialogButtonEnabled(
ui::DialogButton button) const {
DCHECK_EQ(button, ui::DIALOG_BUTTON_CANCEL);
return true;
}

base::string16 ExtensionInstallBlockedDialogView::GetWindowTitle() const {
return title_;
}

bool ExtensionInstallBlockedDialogView::ShouldShowWindowIcon() const {
return true;
}

gfx::ImageSkia ExtensionInstallBlockedDialogView::GetWindowIcon() {
return icon_;
}

ui::ModalType ExtensionInstallBlockedDialogView::GetModalType() const {
// Make sure user know the installation is blocked before taking further
// action.
return ui::MODAL_TYPE_CHILD;
}

void ExtensionInstallBlockedDialogView::AddCustomMessageContents() {
SetLayoutManager(std::make_unique<views::FillLayout>());

if (custom_error_message_.empty())
return;
void ExtensionInstallBlockedDialogView::AddCustomMessageContents(
const base::string16& custom_error_message) {
DCHECK(!custom_error_message.empty());

const ChromeLayoutProvider* provider = ChromeLayoutProvider::Get();
auto extension_info_container = std::make_unique<views::View>();
Expand All @@ -134,7 +115,7 @@ void ExtensionInstallBlockedDialogView::AddCustomMessageContents() {

auto* header_label =
extension_info_container->AddChildView(std::make_unique<views::Label>(
custom_error_message_, CONTEXT_BODY_TEXT_LARGE));
custom_error_message, CONTEXT_BODY_TEXT_LARGE));
header_label->SetMultiLine(true);
header_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
header_label->SizeToFit(content_width);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,12 @@ class ExtensionInstallBlockedDialogView
private:
// views::BubbleDialogDelegateView
gfx::Size CalculatePreferredSize() const override;
bool IsDialogButtonEnabled(ui::DialogButton button) const override;
base::string16 GetWindowTitle() const override;
bool ShouldShowWindowIcon() const override;
gfx::ImageSkia GetWindowIcon() override;
ui::ModalType GetModalType() const override;

// Creates the contents area that contains custom error message that is set by
// administrator.
void AddCustomMessageContents();
void AddCustomMessageContents(const base::string16& custom_error_message);

const base::string16 title_;
const base::string16 custom_error_message_;
const gfx::ImageSkia icon_;
base::OnceClosure done_callback_;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,6 @@ class ExtensionInstalledBubbleView : public BubbleSyncPromoDelegate,

private:
// views::BubbleDialogDelegateView:
base::string16 GetWindowTitle() const override;
gfx::ImageSkia GetWindowIcon() override;
bool ShouldShowWindowIcon() const override;
bool ShouldShowCloseButton() const override;
void Init() override;

// BubbleSyncPromoDelegate:
Expand All @@ -146,7 +142,6 @@ class ExtensionInstalledBubbleView : public BubbleSyncPromoDelegate,

Browser* const browser_;
const std::unique_ptr<ExtensionInstalledBubbleModel> model_;
gfx::ImageSkia icon_;

DISALLOW_COPY_AND_ASSIGN(ExtensionInstalledBubbleView);
};
Expand Down Expand Up @@ -184,13 +179,20 @@ ExtensionInstalledBubbleView::ExtensionInstalledBubbleView(
? views::BubbleBorder::TOP_LEFT
: views::BubbleBorder::TOP_RIGHT),
browser_(browser),
model_(std::move(model)),
icon_(model_->MakeIconOfSize(kMaxIconSize)) {
model_(std::move(model)) {
chrome::RecordDialogCreation(chrome::DialogIdentifier::EXTENSION_INSTALLED);
SetButtons(ui::DIALOG_BUTTON_NONE);
if (model_->show_sign_in_promo()) {
SetFootnoteView(CreateSigninPromoView(browser->profile(), this));
}
SetIcon(model_->MakeIconOfSize(kMaxIconSize));
SetShowIcon(true);
SetShowCloseButton(true);

base::string16 extension_name = base::UTF8ToUTF16(model_->extension_name());
base::i18n::AdjustStringForLocaleDirection(&extension_name);
SetTitle(l10n_util::GetStringFUTF16(IDS_EXTENSION_INSTALLED_HEADING,
extension_name));
}

ExtensionInstalledBubbleView::~ExtensionInstalledBubbleView() = default;
Expand All @@ -201,26 +203,6 @@ void ExtensionInstalledBubbleView::UpdateAnchorView() {
SetAnchorView(reference_view);
}

base::string16 ExtensionInstalledBubbleView::GetWindowTitle() const {
// Add the heading (for all options).
base::string16 extension_name = base::UTF8ToUTF16(model_->extension_name());
base::i18n::AdjustStringForLocaleDirection(&extension_name);
return l10n_util::GetStringFUTF16(IDS_EXTENSION_INSTALLED_HEADING,
extension_name);
}

gfx::ImageSkia ExtensionInstalledBubbleView::GetWindowIcon() {
return icon_;
}

bool ExtensionInstalledBubbleView::ShouldShowWindowIcon() const {
return true;
}

bool ExtensionInstalledBubbleView::ShouldShowCloseButton() const {
return true;
}

void ExtensionInstalledBubbleView::Init() {
UpdateAnchorView();

Expand Down Expand Up @@ -249,7 +231,7 @@ void ExtensionInstalledBubbleView::Init() {
// Indent by the size of the icon.
layout->set_inside_border_insets(gfx::Insets(
0,
icon_.width() +
GetWindowIcon().width() +
provider->GetDistanceMetric(DISTANCE_UNRELATED_CONTROL_HORIZONTAL),
0, 0));
layout->set_cross_axis_alignment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,12 @@ class ExtensionUninstallDialogDelegateView
ui::ModalType GetModalType() const override {
return is_bubble_ ? ui::MODAL_TYPE_NONE : ui::MODAL_TYPE_WINDOW;
}
base::string16 GetWindowTitle() const override;
gfx::ImageSkia GetWindowIcon() override { return image_; }
bool ShouldShowWindowIcon() const override { return true; }
bool ShouldShowCloseButton() const override { return false; }

ExtensionUninstallDialogViews* dialog_;
const base::string16 extension_name_;
const bool is_bubble_;

views::Label* heading_;
views::Checkbox* checkbox_;
gfx::ImageSkia image_;

DISALLOW_COPY_AND_ASSIGN(ExtensionUninstallDialogDelegateView);
};
Expand Down Expand Up @@ -199,17 +193,19 @@ ExtensionUninstallDialogDelegateView::ExtensionUninstallDialogDelegateView(
anchor_view ? views::BubbleBorder::TOP_RIGHT
: views::BubbleBorder::NONE),
dialog_(dialog_view),
extension_name_(base::UTF8ToUTF16(extension->name())),
is_bubble_(anchor_view != nullptr),
checkbox_(nullptr),
image_(gfx::ImageSkiaOperations::CreateResizedImage(
*image,
skia::ImageOperations::ResizeMethod::RESIZE_GOOD,
gfx::Size(extension_misc::EXTENSION_ICON_SMALL,
extension_misc::EXTENSION_ICON_SMALL))) {
checkbox_(nullptr) {
SetButtonLabel(
ui::DIALOG_BUTTON_OK,
l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_UNINSTALL_BUTTON));
SetIcon(gfx::ImageSkiaOperations::CreateResizedImage(
*image, skia::ImageOperations::ResizeMethod::RESIZE_GOOD,
gfx::Size(extension_misc::EXTENSION_ICON_SMALL,
extension_misc::EXTENSION_ICON_SMALL)));
SetShowCloseButton(false);
SetShowIcon(true);
SetTitle(l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT_UNINSTALL_TITLE,
base::UTF8ToUTF16(extension->name())));

SetAcceptCallback(base::BindOnce(
[](ExtensionUninstallDialogDelegateView* view) {
Expand Down Expand Up @@ -294,11 +290,6 @@ gfx::Size ExtensionUninstallDialogDelegateView::CalculatePreferredSize() const {
return gfx::Size(width, GetHeightForWidth(width));
}

base::string16 ExtensionUninstallDialogDelegateView::GetWindowTitle() const {
return l10n_util::GetStringFUTF16(IDS_EXTENSION_PROMPT_UNINSTALL_TITLE,
extension_name_);
}

} // namespace

// static
Expand Down

0 comments on commit 1ec06c6

Please sign in to comment.