Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_uninstall_dialog.cc
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 "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"
25
26 namespace extensions {
27
28 namespace {
29
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();
37 }
38
39 }  // namespace
40
41 ExtensionUninstallDialog::ExtensionUninstallDialog(
42     Profile* profile,
43     gfx::NativeWindow parent,
44     ExtensionUninstallDialog::Delegate* delegate)
45     : profile_(profile),
46       parent_(parent),
47       delegate_(delegate),
48       extension_(NULL),
49       triggering_extension_(NULL),
50       ui_loop_(base::MessageLoop::current()) {
51 }
52
53 ExtensionUninstallDialog::~ExtensionUninstallDialog() {
54 }
55
56 void ExtensionUninstallDialog::ConfirmProgrammaticUninstall(
57     const Extension* extension,
58     const Extension* triggering_extension) {
59   triggering_extension_ = triggering_extension;
60   ConfirmUninstall(extension);
61 }
62
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);
72
73   // Load the image asynchronously. The response will be sent to OnImageLoaded.
74   ImageLoader* loader = ImageLoader::Get(profile_);
75
76   SetIcon(gfx::Image());
77   std::vector<ImageLoader::ImageRepresentation> images_list;
78   images_list.push_back(ImageLoader::ImageRepresentation(
79       image,
80       ImageLoader::ImageRepresentation::NEVER_RESIZE,
81       gfx::Size(),
82       ui::SCALE_FACTOR_100P));
83   loader->LoadImagesAsync(extension_,
84                           images_list,
85                           base::Bind(&ExtensionUninstallDialog::OnImageLoaded,
86                                      AsWeakPtr(),
87                                      extension_->id()));
88 }
89
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
96     //     lazily.
97     icon_ = gfx::ImageSkia::CreateFrom1xBitmap(
98         GetDefaultIconBitmapForMaxScaleFactor(extension_->is_app()));
99   } else {
100     icon_ = *image.ToImageSkia();
101   }
102 }
103
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();
111     return;
112   }
113
114   SetIcon(image);
115   Show();
116 }
117
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()));
124   }
125   return l10n_util::GetStringFUTF8(IDS_EXTENSION_UNINSTALL_PROMPT_HEADING,
126                                    base::UTF8ToUTF16(extension_->name()));
127 }
128
129 }  // namespace extensions