| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/views/srt_prompt_dialog.h" | |
| 6 | |
| 7 #include "base/strings/string16.h" | |
| 8 #include "chrome/browser/safe_browsing/srt_prompt_controller.h" | |
| 9 #include "chrome/browser/ui/browser.h" | |
| 10 #include "chrome/browser/ui/browser_dialogs.h" | |
| 11 #include "chrome/browser/ui/browser_window.h" | |
| 12 #include "components/constrained_window/constrained_window_views.h" | |
| 13 #include "ui/base/ui_base_types.h" | |
| 14 #include "ui/events/event.h" | |
| 15 #include "ui/gfx/native_widget_types.h" | |
| 16 #include "ui/gfx/text_constants.h" | |
| 17 #include "ui/views/controls/label.h" | |
| 18 #include "ui/views/layout/box_layout.h" | |
| 19 #include "ui/views/layout/layout_constants.h" | |
| 20 #include "ui/views/widget/widget.h" | |
| 21 | |
| 22 namespace chrome { | |
| 23 | |
| 24 void ShowSRTPrompt(Browser* browser, | |
| 25 safe_browsing::SRTPromptController* controller) { | |
| 26 SRTPromptDialog* dialog = new SRTPromptDialog(controller); | |
| 27 dialog->Show(browser); | |
| 28 } | |
| 29 | |
| 30 } // namespace chrome | |
| 31 | |
| 32 namespace { | |
| 33 constexpr int kDialogWidth = 448; | |
| 34 } // namespace | |
| 35 | |
| 36 //////////////////////////////////////////////////////////////////////////////// | |
| 37 // SRTPromptDialog | |
| 38 | |
| 39 SRTPromptDialog::SRTPromptDialog(safe_browsing::SRTPromptController* controller) | |
| 40 : browser_(nullptr), | |
| 41 controller_(controller), | |
| 42 advanced_button_( | |
| 43 new views::LabelButton(this, controller_->GetAdvancedButtonLabel())) { | |
| 44 DCHECK(controller_); | |
| 45 | |
| 46 SetLayoutManager(new views::BoxLayout( | |
| 47 /*orientation=*/views::BoxLayout::kVertical, | |
| 48 /*inside_border_horizontal_spacing=*/views::kButtonHEdgeMarginNew, | |
| 49 /*inside_border_vertical_spacing=*/views::kPanelVertMargin, | |
| 50 /*between_child_spacing=*/0)); | |
| 51 views::Label* label = new views::Label(controller_->GetMainText()); | |
| 52 label->SetMultiLine(true); | |
| 53 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 54 AddChildView(label); | |
| 55 | |
| 56 advanced_button_->SetStyle(views::Button::STYLE_BUTTON); | |
| 57 } | |
| 58 | |
| 59 SRTPromptDialog::~SRTPromptDialog() { | |
| 60 // Make sure the controller is correctly notified in case the dialog widget is | |
| 61 // closed by some other means than the dialog buttons. | |
| 62 if (controller_) | |
| 63 controller_->Cancel(); | |
| 64 } | |
| 65 | |
| 66 void SRTPromptDialog::Show(Browser* browser) { | |
| 67 DCHECK(browser); | |
| 68 DCHECK(!browser_); | |
| 69 DCHECK(controller_); | |
| 70 | |
| 71 browser_ = browser; | |
| 72 constrained_window::CreateBrowserModalDialogViews( | |
| 73 this, browser_->window()->GetNativeWindow()) | |
| 74 ->Show(); | |
| 75 controller_->DialogShown(); | |
| 76 } | |
| 77 | |
| 78 // DialogModel overrides. | |
| 79 | |
| 80 bool SRTPromptDialog::ShouldDefaultButtonBeBlue() const { | |
| 81 return true; | |
| 82 } | |
| 83 | |
| 84 // WidgetDelegate overrides. | |
| 85 | |
| 86 ui::ModalType SRTPromptDialog::GetModalType() const { | |
| 87 return ui::MODAL_TYPE_WINDOW; | |
| 88 } | |
| 89 | |
| 90 base::string16 SRTPromptDialog::GetWindowTitle() const { | |
| 91 DCHECK(controller_); | |
| 92 return controller_->GetWindowTitle(); | |
| 93 } | |
| 94 | |
| 95 // DialogDelegate overrides. | |
| 96 | |
| 97 base::string16 SRTPromptDialog::GetDialogButtonLabel( | |
| 98 ui::DialogButton button) const { | |
| 99 DCHECK(button == ui::DIALOG_BUTTON_OK || button == ui::DIALOG_BUTTON_CANCEL); | |
| 100 DCHECK(controller_); | |
| 101 | |
| 102 return button == ui::DIALOG_BUTTON_OK | |
| 103 ? controller_->GetAcceptButtonLabel() | |
| 104 : DialogDelegate::GetDialogButtonLabel(button); | |
| 105 } | |
| 106 | |
| 107 views::View* SRTPromptDialog::CreateExtraView() { | |
| 108 return advanced_button_; | |
| 109 } | |
| 110 | |
| 111 bool SRTPromptDialog::Accept() { | |
| 112 if (controller_) { | |
| 113 controller_->Accept(); | |
| 114 controller_ = nullptr; | |
| 115 } | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 119 bool SRTPromptDialog::Cancel() { | |
| 120 if (controller_) { | |
| 121 controller_->Cancel(); | |
| 122 controller_ = nullptr; | |
| 123 } | |
| 124 return true; | |
| 125 } | |
| 126 | |
| 127 bool SRTPromptDialog::Close() { | |
| 128 if (controller_) { | |
| 129 controller_->Close(); | |
| 130 controller_ = nullptr; | |
| 131 } | |
| 132 return true; | |
| 133 } | |
| 134 | |
| 135 // View overrides. | |
| 136 | |
| 137 gfx::Size SRTPromptDialog::GetPreferredSize() const { | |
| 138 return gfx::Size(kDialogWidth, GetHeightForWidth(kDialogWidth)); | |
| 139 } | |
| 140 | |
| 141 // views::ButtonListener overrides. | |
| 142 | |
| 143 void SRTPromptDialog::ButtonPressed(views::Button* sender, | |
| 144 const ui::Event& event) { | |
| 145 DCHECK_EQ(sender, advanced_button_); | |
| 146 DCHECK(browser_); | |
| 147 | |
| 148 // TODO(alito): Navigate to the webui version of the Chrome Cleaner UI when | |
| 149 // that is implemented. | |
| 150 if (controller_) { | |
| 151 controller_->AdvancedButtonClicked(); | |
| 152 controller_ = nullptr; | |
| 153 } | |
| 154 GetWidget()->Close(); | |
| 155 } | |
| OLD | NEW |