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"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/extension_util.h"
13 #include "chrome/browser/extensions/image_loader.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "content/public/browser/notification_service.h"
17 #include "content/public/browser/notification_source.h"
18 #include "extensions/common/constants.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_icon_set.h"
21 #include "extensions/common/extension_resource.h"
22 #include "extensions/common/manifest_handlers/icons_handler.h"
23 #include "grit/generated_resources.h"
24 #include "grit/theme_resources.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/gfx/image/image.h"
28 #include "ui/gfx/image/image_skia.h"
32 // Returns pixel size under maximal scale factor for the icon whose device
33 // independent size is |size_in_dip|
34 int GetSizeForMaxScaleFactor(int size_in_dip
) {
35 float max_scale_factor_scale
= gfx::ImageSkia::GetMaxSupportedScale();
37 return static_cast<int>(size_in_dip
* max_scale_factor_scale
);
40 // Returns bitmap for the default icon with size equal to the default icon's
41 // pixel size under maximal supported scale factor.
42 SkBitmap
GetDefaultIconBitmapForMaxScaleFactor(bool is_app
) {
43 const gfx::ImageSkia
& image
= is_app
?
44 extensions::util::GetDefaultAppIcon() :
45 extensions::util::GetDefaultExtensionIcon();
46 return image
.GetRepresentation(
47 gfx::ImageSkia::GetMaxSupportedScale()).sk_bitmap();
52 // Size of extension icon in top left of dialog.
53 static const int kIconSize
= 69;
55 ExtensionUninstallDialog::ExtensionUninstallDialog(
58 ExtensionUninstallDialog::Delegate
* delegate
)
63 triggering_extension_(NULL
),
64 state_(kImageIsLoading
),
65 ui_loop_(base::MessageLoop::current()) {
68 chrome::NOTIFICATION_BROWSER_CLOSED
,
69 content::Source
<Browser
>(browser
));
73 ExtensionUninstallDialog::~ExtensionUninstallDialog() {
76 void ExtensionUninstallDialog::ConfirmProgrammaticUninstall(
77 const extensions::Extension
* extension
,
78 const extensions::Extension
* triggering_extension
) {
79 triggering_extension_
= triggering_extension
;
80 ConfirmUninstall(extension
);
83 void ExtensionUninstallDialog::ConfirmUninstall(
84 const extensions::Extension
* extension
) {
85 DCHECK(ui_loop_
== base::MessageLoop::current());
86 extension_
= extension
;
87 // Bookmark apps may not have 128x128 icons so accept 48x48 icons.
88 const int icon_size
= extension_
->from_bookmark()
89 ? extension_misc::EXTENSION_ICON_MEDIUM
90 : extension_misc::EXTENSION_ICON_LARGE
;
91 extensions::ExtensionResource image
= extensions::IconsInfo::GetIconResource(
94 ExtensionIconSet::MATCH_BIGGER
);
95 // Load the icon whose pixel size is large enough to be displayed under
96 // maximal supported scale factor. UI code will scale the icon down if needed.
97 int pixel_size
= GetSizeForMaxScaleFactor(kIconSize
);
99 // Load the image asynchronously. The response will be sent to OnImageLoaded.
100 state_
= kImageIsLoading
;
101 extensions::ImageLoader
* loader
=
102 extensions::ImageLoader::Get(profile_
);
103 loader
->LoadImageAsync(extension_
, image
,
104 gfx::Size(pixel_size
, pixel_size
),
105 base::Bind(&ExtensionUninstallDialog::OnImageLoaded
,
109 void ExtensionUninstallDialog::SetIcon(const gfx::Image
& image
) {
110 if (image
.IsEmpty()) {
111 // Let's set default icon bitmap whose size is equal to the default icon's
112 // pixel size under maximal supported scale factor. If the bitmap is larger
113 // than the one we need, it will be scaled down by the ui code.
114 // TODO(tbarzic): We should use IconImage here and load the required bitmap
116 icon_
= gfx::ImageSkia::CreateFrom1xBitmap(
117 GetDefaultIconBitmapForMaxScaleFactor(extension_
->is_app()));
119 icon_
= *image
.ToImageSkia();
123 void ExtensionUninstallDialog::OnImageLoaded(const gfx::Image
& image
) {
126 // Show the dialog unless the browser has been closed while we were waiting
128 DCHECK(state_
== kImageIsLoading
|| state_
== kBrowserIsClosing
);
129 if (state_
== kImageIsLoading
) {
130 state_
= kDialogIsShowing
;
135 void ExtensionUninstallDialog::Observe(
137 const content::NotificationSource
& source
,
138 const content::NotificationDetails
& details
) {
139 DCHECK(type
== chrome::NOTIFICATION_BROWSER_CLOSED
);
142 // If the browser is closed while waiting for the image, we need to send a
143 // "cancel" event here, because there will not be another opportunity to
144 // notify the delegate of the cancellation as we won't open the dialog.
145 if (state_
== kImageIsLoading
) {
146 state_
= kBrowserIsClosing
;
147 delegate_
->ExtensionUninstallCanceled();
151 std::string
ExtensionUninstallDialog::GetHeadingText() {
152 if (triggering_extension_
) {
153 return l10n_util::GetStringFUTF8(
154 IDS_EXTENSION_PROGRAMMATIC_UNINSTALL_PROMPT_HEADING
,
155 base::UTF8ToUTF16(triggering_extension_
->name()),
156 base::UTF8ToUTF16(extension_
->name()));
158 return l10n_util::GetStringFUTF8(IDS_EXTENSION_UNINSTALL_PROMPT_HEADING
,
159 base::UTF8ToUTF16(extension_
->name()));