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/browser/extension_registry.h"
19 #include "extensions/common/constants.h"
20 #include "extensions/common/extension.h"
21 #include "extensions/common/extension_icon_set.h"
22 #include "extensions/common/extension_resource.h"
23 #include "extensions/common/manifest_handlers/icons_handler.h"
24 #include "grit/generated_resources.h"
25 #include "grit/theme_resources.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/gfx/image/image.h"
29 #include "ui/gfx/image/image_skia.h"
33 // Returns bitmap for the default icon with size equal to the default icon's
34 // pixel size under maximal supported scale factor.
35 SkBitmap
GetDefaultIconBitmapForMaxScaleFactor(bool is_app
) {
36 const gfx::ImageSkia
& image
= is_app
?
37 extensions::util::GetDefaultAppIcon() :
38 extensions::util::GetDefaultExtensionIcon();
39 return image
.GetRepresentation(
40 gfx::ImageSkia::GetMaxSupportedScale()).sk_bitmap();
45 ExtensionUninstallDialog::ExtensionUninstallDialog(
48 ExtensionUninstallDialog::Delegate
* delegate
)
53 triggering_extension_(NULL
),
54 state_(kImageIsLoading
),
55 ui_loop_(base::MessageLoop::current()) {
58 chrome::NOTIFICATION_BROWSER_CLOSED
,
59 content::Source
<Browser
>(browser
));
63 ExtensionUninstallDialog::~ExtensionUninstallDialog() {
66 void ExtensionUninstallDialog::ConfirmProgrammaticUninstall(
67 const extensions::Extension
* extension
,
68 const extensions::Extension
* triggering_extension
) {
69 triggering_extension_
= triggering_extension
;
70 ConfirmUninstall(extension
);
73 void ExtensionUninstallDialog::ConfirmUninstall(
74 const extensions::Extension
* extension
) {
75 DCHECK(ui_loop_
== base::MessageLoop::current());
76 extension_
= extension
;
77 // Bookmark apps may not have 128x128 icons so accept 64x64 icons.
78 const int icon_size
= extension_
->from_bookmark()
79 ? extension_misc::EXTENSION_ICON_SMALL
* 2
80 : extension_misc::EXTENSION_ICON_LARGE
;
81 extensions::ExtensionResource image
= extensions::IconsInfo::GetIconResource(
84 ExtensionIconSet::MATCH_BIGGER
);
86 // Load the image asynchronously. The response will be sent to OnImageLoaded.
87 state_
= kImageIsLoading
;
88 extensions::ImageLoader
* loader
=
89 extensions::ImageLoader::Get(profile_
);
91 std::vector
<extensions::ImageLoader::ImageRepresentation
> images_list
;
92 images_list
.push_back(extensions::ImageLoader::ImageRepresentation(
94 extensions::ImageLoader::ImageRepresentation::NEVER_RESIZE
,
96 ui::SCALE_FACTOR_100P
));
97 loader
->LoadImagesAsync(extension_
,
99 base::Bind(&ExtensionUninstallDialog::OnImageLoaded
,
104 void ExtensionUninstallDialog::SetIcon(const gfx::Image
& image
) {
105 if (image
.IsEmpty()) {
106 // Let's set default icon bitmap whose size is equal to the default icon's
107 // pixel size under maximal supported scale factor. If the bitmap is larger
108 // than the one we need, it will be scaled down by the ui code.
109 // TODO(tbarzic): We should use IconImage here and load the required bitmap
111 icon_
= gfx::ImageSkia::CreateFrom1xBitmap(
112 GetDefaultIconBitmapForMaxScaleFactor(extension_
->is_app()));
114 icon_
= *image
.ToImageSkia();
118 void ExtensionUninstallDialog::OnImageLoaded(const std::string
& extension_id
,
119 const gfx::Image
& image
) {
120 const extensions::Extension
* target_extension
=
121 extensions::ExtensionRegistry::Get(profile_
)->GetExtensionById(
122 extension_id
, extensions::ExtensionRegistry::EVERYTHING
);
123 if (!target_extension
) {
124 delegate_
->ExtensionUninstallCanceled();
130 // Show the dialog unless the browser has been closed while we were waiting
132 DCHECK(state_
== kImageIsLoading
|| state_
== kBrowserIsClosing
);
133 if (state_
== kImageIsLoading
) {
134 state_
= kDialogIsShowing
;
139 void ExtensionUninstallDialog::Observe(
141 const content::NotificationSource
& source
,
142 const content::NotificationDetails
& details
) {
143 DCHECK(type
== chrome::NOTIFICATION_BROWSER_CLOSED
);
146 // If the browser is closed while waiting for the image, we need to send a
147 // "cancel" event here, because there will not be another opportunity to
148 // notify the delegate of the cancellation as we won't open the dialog.
149 if (state_
== kImageIsLoading
) {
150 state_
= kBrowserIsClosing
;
151 delegate_
->ExtensionUninstallCanceled();
155 std::string
ExtensionUninstallDialog::GetHeadingText() {
156 if (triggering_extension_
) {
157 return l10n_util::GetStringFUTF8(
158 IDS_EXTENSION_PROGRAMMATIC_UNINSTALL_PROMPT_HEADING
,
159 base::UTF8ToUTF16(triggering_extension_
->name()),
160 base::UTF8ToUTF16(extension_
->name()));
162 return l10n_util::GetStringFUTF8(IDS_EXTENSION_UNINSTALL_PROMPT_HEADING
,
163 base::UTF8ToUTF16(extension_
->name()));