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/image_view.h"
21 #include "ui/views/controls/label.h"
22 #include "ui/views/layout/layout_constants.h"
23 #include "ui/views/view.h"
24 #include "ui/views/widget/widget.h"
25 #include "ui/views/window/dialog_delegate.h"
29 const int kRightColumnWidth
= 210;
30 const int kIconSize
= 64;
32 class ExtensionUninstallDialogDelegateView
;
34 // Views implementation of the uninstall dialog.
35 class ExtensionUninstallDialogViews
36 : public extensions::ExtensionUninstallDialog
{
38 ExtensionUninstallDialogViews(
40 gfx::NativeWindow parent
,
41 extensions::ExtensionUninstallDialog::Delegate
* delegate
);
42 ~ExtensionUninstallDialogViews() override
;
44 // Called when the ExtensionUninstallDialogDelegate has been destroyed to make
45 // sure we invalidate pointers.
46 void DialogDelegateDestroyed() { view_
= NULL
; }
48 // Forwards the accept and cancels to the delegate.
49 void ExtensionUninstallAccepted();
50 void ExtensionUninstallCanceled();
55 ExtensionUninstallDialogDelegateView
* view_
;
57 // The dialog's parent window.
58 gfx::NativeWindow parent_
;
60 // Tracks whether |parent_| got destroyed.
61 scoped_ptr
<NativeWindowTracker
> parent_window_tracker_
;
63 DISALLOW_COPY_AND_ASSIGN(ExtensionUninstallDialogViews
);
66 // The dialog's view, owned by the views framework.
67 class ExtensionUninstallDialogDelegateView
: public views::DialogDelegateView
{
69 ExtensionUninstallDialogDelegateView(
70 ExtensionUninstallDialogViews
* dialog_view
,
71 const extensions::Extension
* extension
,
72 const extensions::Extension
* triggering_extension
,
73 gfx::ImageSkia
* image
);
74 ~ExtensionUninstallDialogDelegateView() override
;
76 // Called when the ExtensionUninstallDialog has been destroyed to make sure
77 // we invalidate pointers.
78 void DialogDestroyed() { dialog_
= NULL
; }
81 // views::DialogDelegate:
82 base::string16
GetDialogButtonLabel(ui::DialogButton button
) const override
;
83 int GetDefaultDialogButton() const override
{
84 // Default to accept when triggered via chrome://extensions page.
85 return triggered_by_extension_
?
86 ui::DIALOG_BUTTON_CANCEL
: ui::DIALOG_BUTTON_OK
;
88 bool Accept() override
;
89 bool Cancel() override
;
91 // views::WidgetDelegate:
92 ui::ModalType
GetModalType() const override
{ return ui::MODAL_TYPE_WINDOW
; }
93 base::string16
GetWindowTitle() const override
;
96 gfx::Size
GetPreferredSize() const override
;
98 void Layout() override
;
100 ExtensionUninstallDialogViews
* dialog_
;
102 views::ImageView
* icon_
;
103 views::Label
* heading_
;
104 bool triggered_by_extension_
;
106 DISALLOW_COPY_AND_ASSIGN(ExtensionUninstallDialogDelegateView
);
109 ExtensionUninstallDialogViews::ExtensionUninstallDialogViews(
111 gfx::NativeWindow parent
,
112 extensions::ExtensionUninstallDialog::Delegate
* delegate
)
113 : extensions::ExtensionUninstallDialog(profile
, delegate
),
117 parent_window_tracker_
= NativeWindowTracker::Create(parent_
);
120 ExtensionUninstallDialogViews::~ExtensionUninstallDialogViews() {
121 // Close the widget (the views framework will delete view_).
123 view_
->DialogDestroyed();
124 view_
->GetWidget()->CloseNow();
128 void ExtensionUninstallDialogViews::Show() {
129 if (parent_
&& parent_window_tracker_
->WasNativeWindowClosed()) {
130 delegate_
->ExtensionUninstallCanceled();
134 view_
= new ExtensionUninstallDialogDelegateView(
135 this, extension_
, triggering_extension_
, &icon_
);
136 constrained_window::CreateBrowserModalDialogViews(view_
, parent_
)->Show();
139 void ExtensionUninstallDialogViews::ExtensionUninstallAccepted() {
140 // The widget gets destroyed when the dialog is accepted.
141 view_
->DialogDestroyed();
143 delegate_
->ExtensionUninstallAccepted();
146 void ExtensionUninstallDialogViews::ExtensionUninstallCanceled() {
147 // The widget gets destroyed when the dialog is canceled.
148 view_
->DialogDestroyed();
150 delegate_
->ExtensionUninstallCanceled();
153 ExtensionUninstallDialogDelegateView::ExtensionUninstallDialogDelegateView(
154 ExtensionUninstallDialogViews
* dialog_view
,
155 const extensions::Extension
* extension
,
156 const extensions::Extension
* triggering_extension
,
157 gfx::ImageSkia
* image
)
158 : dialog_(dialog_view
),
159 triggered_by_extension_(triggering_extension
!= NULL
) {
160 // Scale down to icon size, but allow smaller icons (don't scale up).
161 gfx::Size
size(image
->width(), image
->height());
162 if (size
.width() > kIconSize
|| size
.height() > kIconSize
)
163 size
= gfx::Size(kIconSize
, kIconSize
);
164 icon_
= new views::ImageView();
165 icon_
->SetImageSize(size
);
166 icon_
->SetImage(*image
);
169 heading_
= new views::Label(base::UTF8ToUTF16(dialog_
->GetHeadingText()));
170 heading_
->SetMultiLine(true);
171 heading_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
172 AddChildView(heading_
);
175 ExtensionUninstallDialogDelegateView::~ExtensionUninstallDialogDelegateView() {
176 // If we're here, 2 things could have happened. Either the user closed the
177 // dialog nicely and one of ExtensionUninstallAccepted or
178 // ExtensionUninstallCanceled has been called (in which case dialog_ will be
179 // NULL), *or* neither of them have been called and we are being forced closed
180 // by our parent widget. In this case, we need to make sure to notify dialog_
181 // not to call us again, since we're about to be freed by the Widget
184 dialog_
->DialogDelegateDestroyed();
187 base::string16
ExtensionUninstallDialogDelegateView::GetDialogButtonLabel(
188 ui::DialogButton button
) const {
189 return l10n_util::GetStringUTF16((button
== ui::DIALOG_BUTTON_OK
) ?
190 IDS_EXTENSION_PROMPT_UNINSTALL_BUTTON
: IDS_CANCEL
);
193 bool ExtensionUninstallDialogDelegateView::Accept() {
195 dialog_
->ExtensionUninstallAccepted();
199 bool ExtensionUninstallDialogDelegateView::Cancel() {
201 dialog_
->ExtensionUninstallCanceled();
205 base::string16
ExtensionUninstallDialogDelegateView::GetWindowTitle() const {
206 return l10n_util::GetStringUTF16(IDS_EXTENSION_UNINSTALL_PROMPT_TITLE
);
209 gfx::Size
ExtensionUninstallDialogDelegateView::GetPreferredSize() const {
210 int width
= kRightColumnWidth
;
212 width
+= views::kButtonHEdgeMarginNew
* 2;
213 width
+= views::kRelatedControlHorizontalSpacing
;
215 int height
= views::kPanelVertMargin
* 2;
216 height
+= heading_
->GetHeightForWidth(kRightColumnWidth
);
218 return gfx::Size(width
,
219 std::max(height
, kIconSize
+ views::kPanelVertMargin
* 2));
222 void ExtensionUninstallDialogDelegateView::Layout() {
223 int x
= views::kButtonHEdgeMarginNew
;
224 int y
= views::kPanelVertMargin
;
226 heading_
->SizeToFit(kRightColumnWidth
);
228 if (heading_
->height() <= kIconSize
) {
229 icon_
->SetBounds(x
, y
, kIconSize
, kIconSize
);
231 x
+= views::kRelatedControlHorizontalSpacing
;
234 heading_
->SetY(y
+ (kIconSize
- heading_
->height()) / 2);
237 y
+ (heading_
->height() - kIconSize
) / 2,
241 x
+= views::kRelatedControlHorizontalSpacing
;
251 extensions::ExtensionUninstallDialog
*
252 extensions::ExtensionUninstallDialog::Create(Profile
* profile
,
253 gfx::NativeWindow parent
,
254 Delegate
* delegate
) {
255 return new ExtensionUninstallDialogViews(profile
, parent
, delegate
);