Home | History | Annotate | Download | only in extensions
      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.
      4 
      5 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
      6 
      7 #include "base/bind.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"
     24 
     25 namespace extensions {
     26 
     27 namespace {
     28 
     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();
     36 }
     37 
     38 }  // namespace
     39 
     40 ExtensionUninstallDialog::ExtensionUninstallDialog(
     41     Profile* profile,
     42     ExtensionUninstallDialog::Delegate* delegate)
     43     : profile_(profile),
     44       delegate_(delegate),
     45       extension_(NULL),
     46       triggering_extension_(NULL),
     47       ui_loop_(base::MessageLoop::current()) {
     48 }
     49 
     50 ExtensionUninstallDialog::~ExtensionUninstallDialog() {
     51 }
     52 
     53 void ExtensionUninstallDialog::ConfirmProgrammaticUninstall(
     54     const Extension* extension,
     55     const Extension* triggering_extension) {
     56   triggering_extension_ = triggering_extension;
     57   ConfirmUninstall(extension);
     58 }
     59 
     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);
     69 
     70   // Load the image asynchronously. The response will be sent to OnImageLoaded.
     71   ImageLoader* loader = ImageLoader::Get(profile_);
     72 
     73   SetIcon(gfx::Image());
     74   std::vector<ImageLoader::ImageRepresentation> images_list;
     75   images_list.push_back(ImageLoader::ImageRepresentation(
     76       image,
     77       ImageLoader::ImageRepresentation::NEVER_RESIZE,
     78       gfx::Size(),
     79       ui::SCALE_FACTOR_100P));
     80   loader->LoadImagesAsync(extension_,
     81                           images_list,
     82                           base::Bind(&ExtensionUninstallDialog::OnImageLoaded,
     83                                      AsWeakPtr(),
     84                                      extension_->id()));
     85 }
     86 
     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
     93     //     lazily.
     94     icon_ = gfx::ImageSkia::CreateFrom1xBitmap(
     95         GetDefaultIconBitmapForMaxScaleFactor(extension_->is_app()));
     96   } else {
     97     icon_ = *image.ToImageSkia();
     98   }
     99 }
    100 
    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();
    108     return;
    109   }
    110 
    111   SetIcon(image);
    112   Show();
    113 }
    114 
    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()));
    121   }
    122   return l10n_util::GetStringFUTF8(IDS_EXTENSION_UNINSTALL_PROMPT_HEADING,
    123                                    base::UTF8ToUTF16(extension_->name()));
    124 }
    125 
    126 }  // namespace extensions
    127