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