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