1 // Copyright (c) 2012 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.
5 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
7 #include "base/basictypes.h"
8 #include "base/compiler_specific.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/ui/app_list/app_list_service.h"
13 #include "chrome/browser/ui/native_window_tracker.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "components/constrained_window/constrained_window_views.h"
16 #include "extensions/common/extension.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/compositor/compositor.h"
19 #include "ui/compositor/layer.h"
20 #include "ui/views/controls/button/checkbox.h"
21 #include "ui/views/controls/image_view.h"
22 #include "ui/views/controls/label.h"
23 #include "ui/views/layout/layout_constants.h"
24 #include "ui/views/view.h"
25 #include "ui/views/widget/widget.h"
26 #include "ui/views/window/dialog_delegate.h"
30 const int kRightColumnWidth
= 210;
31 const int kIconSize
= 64;
33 class ExtensionUninstallDialogDelegateView
;
35 // Views implementation of the uninstall dialog.
36 class ExtensionUninstallDialogViews
37 : public extensions::ExtensionUninstallDialog
{
39 ExtensionUninstallDialogViews(
41 gfx::NativeWindow parent
,
42 extensions::ExtensionUninstallDialog::Delegate
* delegate
);
43 ~ExtensionUninstallDialogViews() override
;
45 // Called when the ExtensionUninstallDialogDelegate has been destroyed to make
46 // sure we invalidate pointers.
47 void DialogDelegateDestroyed() { view_
= NULL
; }
49 // Forwards the accept and cancels to the delegate.
50 void ExtensionUninstallAccepted(bool handle_report_abuse
);
51 void ExtensionUninstallCanceled();
56 ExtensionUninstallDialogDelegateView
* view_
;
58 // The dialog's parent window.
59 gfx::NativeWindow parent_
;
61 // Tracks whether |parent_| got destroyed.
62 scoped_ptr
<NativeWindowTracker
> parent_window_tracker_
;
64 DISALLOW_COPY_AND_ASSIGN(ExtensionUninstallDialogViews
);
67 // The dialog's view, owned by the views framework.
68 class ExtensionUninstallDialogDelegateView
: public views::DialogDelegateView
{
70 ExtensionUninstallDialogDelegateView(
71 ExtensionUninstallDialogViews
* dialog_view
,
72 const extensions::Extension
* extension
,
73 const extensions::Extension
* triggering_extension
,
74 gfx::ImageSkia
* image
);
75 ~ExtensionUninstallDialogDelegateView() override
;
77 // Called when the ExtensionUninstallDialog has been destroyed to make sure
78 // we invalidate pointers.
79 void DialogDestroyed() { dialog_
= NULL
; }
82 // views::DialogDelegate:
83 views::View
* CreateExtraView() override
;
84 base::string16
GetDialogButtonLabel(ui::DialogButton button
) const override
;
85 int GetDefaultDialogButton() const override
{
86 // Default to accept when triggered via chrome://extensions page.
87 return triggered_by_extension_
?
88 ui::DIALOG_BUTTON_CANCEL
: ui::DIALOG_BUTTON_OK
;
90 bool Accept() override
;
91 bool Cancel() override
;
93 // views::WidgetDelegate:
94 ui::ModalType
GetModalType() const override
{ return ui::MODAL_TYPE_WINDOW
; }
95 base::string16
GetWindowTitle() const override
;
98 gfx::Size
GetPreferredSize() const override
;
100 void Layout() override
;
102 ExtensionUninstallDialogViews
* dialog_
;
104 views::ImageView
* icon_
;
105 views::Label
* heading_
;
106 bool triggered_by_extension_
;
107 views::Checkbox
* report_abuse_checkbox_
;
109 DISALLOW_COPY_AND_ASSIGN(ExtensionUninstallDialogDelegateView
);
112 ExtensionUninstallDialogViews::ExtensionUninstallDialogViews(
114 gfx::NativeWindow parent
,
115 extensions::ExtensionUninstallDialog::Delegate
* delegate
)
116 : extensions::ExtensionUninstallDialog(profile
, delegate
),
120 parent_window_tracker_
= NativeWindowTracker::Create(parent_
);
123 ExtensionUninstallDialogViews::~ExtensionUninstallDialogViews() {
124 // Close the widget (the views framework will delete view_).
126 view_
->DialogDestroyed();
127 view_
->GetWidget()->CloseNow();
131 void ExtensionUninstallDialogViews::Show() {
132 if (parent_
&& parent_window_tracker_
->WasNativeWindowClosed()) {
133 delegate_
->ExtensionUninstallCanceled();
137 view_
= new ExtensionUninstallDialogDelegateView(
138 this, extension_
, triggering_extension_
, &icon_
);
139 constrained_window::CreateBrowserModalDialogViews(view_
, parent_
)->Show();
142 void ExtensionUninstallDialogViews::ExtensionUninstallAccepted(
143 bool report_abuse_checked
) {
144 // The widget gets destroyed when the dialog is accepted.
145 view_
->DialogDestroyed();
147 if (report_abuse_checked
)
149 delegate_
->ExtensionUninstallAccepted();
152 void ExtensionUninstallDialogViews::ExtensionUninstallCanceled() {
153 // The widget gets destroyed when the dialog is canceled.
154 view_
->DialogDestroyed();
156 delegate_
->ExtensionUninstallCanceled();
159 ExtensionUninstallDialogDelegateView::ExtensionUninstallDialogDelegateView(
160 ExtensionUninstallDialogViews
* dialog_view
,
161 const extensions::Extension
* extension
,
162 const extensions::Extension
* triggering_extension
,
163 gfx::ImageSkia
* image
)
164 : dialog_(dialog_view
),
165 triggered_by_extension_(triggering_extension
!= NULL
),
166 report_abuse_checkbox_(nullptr) {
167 // Scale down to icon size, but allow smaller icons (don't scale up).
168 gfx::Size
size(image
->width(), image
->height());
169 if (size
.width() > kIconSize
|| size
.height() > kIconSize
)
170 size
= gfx::Size(kIconSize
, kIconSize
);
171 icon_
= new views::ImageView();
172 icon_
->SetImageSize(size
);
173 icon_
->SetImage(*image
);
176 heading_
= new views::Label(base::UTF8ToUTF16(dialog_
->GetHeadingText()));
177 heading_
->SetMultiLine(true);
178 heading_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
179 AddChildView(heading_
);
182 ExtensionUninstallDialogDelegateView::~ExtensionUninstallDialogDelegateView() {
183 // If we're here, 2 things could have happened. Either the user closed the
184 // dialog nicely and one of ExtensionUninstallAccepted or
185 // ExtensionUninstallCanceled has been called (in which case dialog_ will be
186 // NULL), *or* neither of them have been called and we are being forced closed
187 // by our parent widget. In this case, we need to make sure to notify dialog_
188 // not to call us again, since we're about to be freed by the Widget
191 dialog_
->DialogDelegateDestroyed();
194 views::View
* ExtensionUninstallDialogDelegateView::CreateExtraView() {
195 if (dialog_
->ShouldShowReportAbuseCheckbox()) {
196 report_abuse_checkbox_
= new views::Checkbox(
197 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_UNINSTALL_REPORT_ABUSE
));
199 return report_abuse_checkbox_
;
202 base::string16
ExtensionUninstallDialogDelegateView::GetDialogButtonLabel(
203 ui::DialogButton button
) const {
204 return l10n_util::GetStringUTF16((button
== ui::DIALOG_BUTTON_OK
) ?
205 IDS_EXTENSION_PROMPT_UNINSTALL_BUTTON
: IDS_CANCEL
);
208 bool ExtensionUninstallDialogDelegateView::Accept() {
210 dialog_
->ExtensionUninstallAccepted(
211 report_abuse_checkbox_
&& report_abuse_checkbox_
->checked());
216 bool ExtensionUninstallDialogDelegateView::Cancel() {
218 dialog_
->ExtensionUninstallCanceled();
222 base::string16
ExtensionUninstallDialogDelegateView::GetWindowTitle() const {
223 return l10n_util::GetStringUTF16(IDS_EXTENSION_UNINSTALL_PROMPT_TITLE
);
226 gfx::Size
ExtensionUninstallDialogDelegateView::GetPreferredSize() const {
227 int width
= kRightColumnWidth
;
229 width
+= views::kButtonHEdgeMarginNew
* 2;
230 width
+= views::kRelatedControlHorizontalSpacing
;
232 int height
= views::kPanelVertMargin
* 2;
233 height
+= heading_
->GetHeightForWidth(kRightColumnWidth
);
235 return gfx::Size(width
,
236 std::max(height
, kIconSize
+ views::kPanelVertMargin
* 2));
239 void ExtensionUninstallDialogDelegateView::Layout() {
240 int x
= views::kButtonHEdgeMarginNew
;
241 int y
= views::kPanelVertMargin
;
243 heading_
->SizeToFit(kRightColumnWidth
);
245 if (heading_
->height() <= kIconSize
) {
246 icon_
->SetBounds(x
, y
, kIconSize
, kIconSize
);
248 x
+= views::kRelatedControlHorizontalSpacing
;
251 heading_
->SetY(y
+ (kIconSize
- heading_
->height()) / 2);
254 y
+ (heading_
->height() - kIconSize
) / 2,
258 x
+= views::kRelatedControlHorizontalSpacing
;
268 extensions::ExtensionUninstallDialog
*
269 extensions::ExtensionUninstallDialog::Create(Profile
* profile
,
270 gfx::NativeWindow parent
,
271 Delegate
* delegate
) {
272 return new ExtensionUninstallDialogViews(profile
, parent
, delegate
);