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 gfx::NativeWindow parent,
43 ExtensionUninstallDialog::Delegate* delegate)
48 triggering_extension_(NULL),
49 ui_loop_(base::MessageLoop::current()) {
52 ExtensionUninstallDialog::~ExtensionUninstallDialog() {
55 void ExtensionUninstallDialog::ConfirmProgrammaticUninstall(
56 const Extension* extension,
57 const Extension* triggering_extension) {
58 triggering_extension_ = triggering_extension;
59 ConfirmUninstall(extension);
62 void ExtensionUninstallDialog::ConfirmUninstall(const Extension* extension) {
63 DCHECK(ui_loop_ == base::MessageLoop::current());
64 extension_ = extension;
65 // Bookmark apps may not have 128x128 icons so accept 64x64 icons.
66 const int icon_size = extension_->from_bookmark()
67 ? extension_misc::EXTENSION_ICON_SMALL * 2
68 : extension_misc::EXTENSION_ICON_LARGE;
69 ExtensionResource image = IconsInfo::GetIconResource(
70 extension_, icon_size, ExtensionIconSet::MATCH_BIGGER);
72 // Load the image asynchronously. The response will be sent to OnImageLoaded.
73 ImageLoader* loader = ImageLoader::Get(profile_);
75 SetIcon(gfx::Image());
76 std::vector<ImageLoader::ImageRepresentation> images_list;
77 images_list.push_back(ImageLoader::ImageRepresentation(
79 ImageLoader::ImageRepresentation::NEVER_RESIZE,
81 ui::SCALE_FACTOR_100P));
82 loader->LoadImagesAsync(extension_,
84 base::Bind(&ExtensionUninstallDialog::OnImageLoaded,
89 void ExtensionUninstallDialog::SetIcon(const gfx::Image& image) {
90 if (image.IsEmpty()) {
91 // Let's set default icon bitmap whose size is equal to the default icon's
92 // pixel size under maximal supported scale factor. If the bitmap is larger
93 // than the one we need, it will be scaled down by the ui code.
94 // TODO(tbarzic): We should use IconImage here and load the required bitmap
96 icon_ = gfx::ImageSkia::CreateFrom1xBitmap(
97 GetDefaultIconBitmapForMaxScaleFactor(extension_->is_app()));
99 icon_ = *image.ToImageSkia();
103 void ExtensionUninstallDialog::OnImageLoaded(const std::string& extension_id,
104 const gfx::Image& image) {
105 const Extension* target_extension =
106 ExtensionRegistry::Get(profile_)
107 ->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING);
108 if (!target_extension) {
109 delegate_->ExtensionUninstallCanceled();
117 std::string ExtensionUninstallDialog::GetHeadingText() {
118 if (triggering_extension_) {
119 return l10n_util::GetStringFUTF8(
120 IDS_EXTENSION_PROGRAMMATIC_UNINSTALL_PROMPT_HEADING,
121 base::UTF8ToUTF16(triggering_extension_->name()),
122 base::UTF8ToUTF16(extension_->name()));
124 return l10n_util::GetStringFUTF8(IDS_EXTENSION_UNINSTALL_PROMPT_HEADING,
125 base::UTF8ToUTF16(extension_->name()));
128 } // namespace extensions