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/extensions/extension_util.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "extensions/browser/extension_registry.h"
15 #include "extensions/browser/image_loader.h"
16 #include "extensions/common/constants.h"
17 #include "extensions/common/extension.h"
18 #include "extensions/common/extension_icon_set.h"
19 #include "extensions/common/extension_resource.h"
20 #include "extensions/common/manifest_handlers/icons_handler.h"
21 #include "ui/base/l10n/l10n_util.h"
22 #include "ui/gfx/image/image.h"
23 #include "ui/gfx/image/image_skia.h"
25 namespace extensions
{
29 // Returns bitmap for the default icon with size equal to the default icon's
30 // pixel size under maximal supported scale factor.
31 SkBitmap
GetDefaultIconBitmapForMaxScaleFactor(bool is_app
) {
32 const gfx::ImageSkia
& image
=
33 is_app
? util::GetDefaultAppIcon() : util::GetDefaultExtensionIcon();
34 return image
.GetRepresentation(
35 gfx::ImageSkia::GetMaxSupportedScale()).sk_bitmap();
40 ExtensionUninstallDialog::ExtensionUninstallDialog(
42 ExtensionUninstallDialog::Delegate
* delegate
)
46 triggering_extension_(NULL
),
47 ui_loop_(base::MessageLoop::current()) {
50 ExtensionUninstallDialog::~ExtensionUninstallDialog() {
53 void ExtensionUninstallDialog::ConfirmProgrammaticUninstall(
54 const Extension
* extension
,
55 const Extension
* triggering_extension
) {
56 triggering_extension_
= triggering_extension
;
57 ConfirmUninstall(extension
);
60 void ExtensionUninstallDialog::ConfirmUninstall(const Extension
* extension
) {
61 DCHECK(ui_loop_
== base::MessageLoop::current());
62 extension_
= extension
;
63 // Bookmark apps may not have 128x128 icons so accept 64x64 icons.
64 const int icon_size
= extension_
->from_bookmark()
65 ? extension_misc::EXTENSION_ICON_SMALL
* 2
66 : extension_misc::EXTENSION_ICON_LARGE
;
67 ExtensionResource image
= IconsInfo::GetIconResource(
68 extension_
, icon_size
, ExtensionIconSet::MATCH_BIGGER
);
70 // Load the image asynchronously. The response will be sent to OnImageLoaded.
71 ImageLoader
* loader
= ImageLoader::Get(profile_
);
73 SetIcon(gfx::Image());
74 std::vector
<ImageLoader::ImageRepresentation
> images_list
;
75 images_list
.push_back(ImageLoader::ImageRepresentation(
77 ImageLoader::ImageRepresentation::NEVER_RESIZE
,
79 ui::SCALE_FACTOR_100P
));
80 loader
->LoadImagesAsync(extension_
,
82 base::Bind(&ExtensionUninstallDialog::OnImageLoaded
,
87 void ExtensionUninstallDialog::SetIcon(const gfx::Image
& image
) {
88 if (image
.IsEmpty()) {
89 // Let's set default icon bitmap whose size is equal to the default icon's
90 // pixel size under maximal supported scale factor. If the bitmap is larger
91 // than the one we need, it will be scaled down by the ui code.
92 // TODO(tbarzic): We should use IconImage here and load the required bitmap
94 icon_
= gfx::ImageSkia::CreateFrom1xBitmap(
95 GetDefaultIconBitmapForMaxScaleFactor(extension_
->is_app()));
97 icon_
= *image
.ToImageSkia();
101 void ExtensionUninstallDialog::OnImageLoaded(const std::string
& extension_id
,
102 const gfx::Image
& image
) {
103 const Extension
* target_extension
=
104 ExtensionRegistry::Get(profile_
)
105 ->GetExtensionById(extension_id
, ExtensionRegistry::EVERYTHING
);
106 if (!target_extension
) {
107 delegate_
->ExtensionUninstallCanceled();
115 std::string
ExtensionUninstallDialog::GetHeadingText() {
116 if (triggering_extension_
) {
117 return l10n_util::GetStringFUTF8(
118 IDS_EXTENSION_PROGRAMMATIC_UNINSTALL_PROMPT_HEADING
,
119 base::UTF8ToUTF16(triggering_extension_
->name()),
120 base::UTF8ToUTF16(extension_
->name()));
122 return l10n_util::GetStringFUTF8(IDS_EXTENSION_UNINSTALL_PROMPT_HEADING
,
123 base::UTF8ToUTF16(extension_
->name()));
126 } // namespace extensions