[email protected] | e3c0bc2 | 2012-02-24 01:34:15 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 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 | |
[email protected] | 0d9a1da8 | 2013-03-14 21:52:07 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | b19fe57 | 2013-07-18 04:54:26 | [diff] [blame] | 9 | #include "base/message_loop/message_loop.h" |
rdevlin.cronin | 4f01c5f1 | 2015-05-07 17:52:00 | [diff] [blame] | 10 | #include "base/metrics/histogram_macros.h" |
[email protected] | 226d79ad | 2014-03-14 21:34:57 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 12 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 7eb20e3 | 2014-04-30 08:50:56 | [diff] [blame] | 13 | #include "chrome/browser/extensions/extension_util.h" |
[email protected] | 472522b | 2013-10-25 00:41:28 | [diff] [blame] | 14 | #include "chrome/browser/profiles/profile.h" |
rdevlin.cronin | f3af70d5 | 2015-03-20 03:55:04 | [diff] [blame] | 15 | #include "chrome/browser/ui/browser_navigator.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 16 | #include "chrome/grit/generated_resources.h" |
rdevlin.cronin | 6620949 | 2015-06-10 20:44:05 | [diff] [blame] | 17 | #include "extensions/browser/extension_dialog_auto_confirm.h" |
[email protected] | e0ebb8d | 2014-05-21 20:26:27 | [diff] [blame] | 18 | #include "extensions/browser/extension_registry.h" |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 19 | #include "extensions/browser/extension_system.h" |
[email protected] | 326e6f0 | 2014-06-20 04:53:37 | [diff] [blame] | 20 | #include "extensions/browser/image_loader.h" |
[email protected] | cda103d | 2014-04-04 16:22:39 | [diff] [blame] | 21 | #include "extensions/common/constants.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 22 | #include "extensions/common/extension.h" |
[email protected] | 4b790884 | 2014-04-07 23:50:22 | [diff] [blame] | 23 | #include "extensions/common/extension_icon_set.h" |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 24 | #include "extensions/common/extension_resource.h" |
rdevlin.cronin | f3af70d5 | 2015-03-20 03:55:04 | [diff] [blame] | 25 | #include "extensions/common/extension_urls.h" |
[email protected] | 0db486f | 2014-04-09 19:32:22 | [diff] [blame] | 26 | #include "extensions/common/manifest_handlers/icons_handler.h" |
rdevlin.cronin | dbb8fa5 | 2015-05-06 00:16:56 | [diff] [blame] | 27 | #include "extensions/common/manifest_url_handlers.h" |
[email protected] | 226d79ad | 2014-03-14 21:34:57 | [diff] [blame] | 28 | #include "ui/base/l10n/l10n_util.h" |
rdevlin.cronin | f3af70d5 | 2015-03-20 03:55:04 | [diff] [blame] | 29 | #include "ui/base/page_transition_types.h" |
| 30 | #include "ui/base/window_open_disposition.h" |
[email protected] | bdd6eec | 2012-03-03 19:58:06 | [diff] [blame] | 31 | #include "ui/gfx/image/image.h" |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame] | 32 | #include "ui/gfx/image/image_skia.h" |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 33 | |
[email protected] | d405067 | 2014-06-04 09:18:40 | [diff] [blame] | 34 | namespace extensions { |
| 35 | |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 36 | namespace { |
| 37 | |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 38 | const char kExtensionRemovedError[] = |
| 39 | "Extension was removed before dialog closed."; |
| 40 | |
rdevlin.cronin | de23cac | 2015-07-15 23:51:39 | [diff] [blame] | 41 | const char kReferrerId[] = "chrome-remove-extension-dialog"; |
| 42 | |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 43 | // Returns bitmap for the default icon with size equal to the default icon's |
| 44 | // pixel size under maximal supported scale factor. |
| 45 | SkBitmap GetDefaultIconBitmapForMaxScaleFactor(bool is_app) { |
[email protected] | d405067 | 2014-06-04 09:18:40 | [diff] [blame] | 46 | const gfx::ImageSkia& image = |
| 47 | is_app ? util::GetDefaultAppIcon() : util::GetDefaultExtensionIcon(); |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame] | 48 | return image.GetRepresentation( |
| 49 | gfx::ImageSkia::GetMaxSupportedScale()).sk_bitmap(); |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | } // namespace |
| 53 | |
[email protected] | 6f03db06 | 2011-09-22 20:37:14 | [diff] [blame] | 54 | ExtensionUninstallDialog::ExtensionUninstallDialog( |
[email protected] | 520414b1 | 2013-01-22 19:27:36 | [diff] [blame] | 55 | Profile* profile, |
[email protected] | 6f03db06 | 2011-09-22 20:37:14 | [diff] [blame] | 56 | ExtensionUninstallDialog::Delegate* delegate) |
[email protected] | 520414b1 | 2013-01-22 19:27:36 | [diff] [blame] | 57 | : profile_(profile), |
[email protected] | 6f03db06 | 2011-09-22 20:37:14 | [diff] [blame] | 58 | delegate_(delegate), |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 59 | uninstall_reason_(UNINSTALL_REASON_FOR_TESTING) { |
[email protected] | 5f1ba41 | 2012-06-25 20:10:52 | [diff] [blame] | 60 | } |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 61 | |
[email protected] | 5f1ba41 | 2012-06-25 20:10:52 | [diff] [blame] | 62 | ExtensionUninstallDialog::~ExtensionUninstallDialog() { |
[email protected] | 5f1ba41 | 2012-06-25 20:10:52 | [diff] [blame] | 63 | } |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 64 | |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 65 | void ExtensionUninstallDialog::ConfirmUninstallByExtension( |
| 66 | const scoped_refptr<const Extension>& extension, |
| 67 | const scoped_refptr<const Extension>& triggering_extension, |
rdevlin.cronin | e18eb8cd | 2015-07-16 16:31:28 | [diff] [blame^] | 68 | UninstallReason reason, |
| 69 | UninstallSource source) { |
[email protected] | 226d79ad | 2014-03-14 21:34:57 | [diff] [blame] | 70 | triggering_extension_ = triggering_extension; |
rdevlin.cronin | e18eb8cd | 2015-07-16 16:31:28 | [diff] [blame^] | 71 | ConfirmUninstall(extension, reason, source); |
[email protected] | 226d79ad | 2014-03-14 21:34:57 | [diff] [blame] | 72 | } |
| 73 | |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 74 | void ExtensionUninstallDialog::ConfirmUninstall( |
| 75 | const scoped_refptr<const Extension>& extension, |
rdevlin.cronin | e18eb8cd | 2015-07-16 16:31:28 | [diff] [blame^] | 76 | UninstallReason reason, |
| 77 | UninstallSource source) { |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 78 | DCHECK(thread_checker_.CalledOnValidThread()); |
rdevlin.cronin | e18eb8cd | 2015-07-16 16:31:28 | [diff] [blame^] | 79 | |
| 80 | UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallSource", source, |
| 81 | NUM_UNINSTALL_SOURCES); |
| 82 | |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 83 | extension_ = extension; |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 84 | uninstall_reason_ = reason; |
[email protected] | 0d0ba18 | 2014-06-03 12:40:43 | [diff] [blame] | 85 | // Bookmark apps may not have 128x128 icons so accept 64x64 icons. |
[email protected] | 36a81316 | 2014-02-05 05:32:19 | [diff] [blame] | 86 | const int icon_size = extension_->from_bookmark() |
[email protected] | 0d0ba18 | 2014-06-03 12:40:43 | [diff] [blame] | 87 | ? extension_misc::EXTENSION_ICON_SMALL * 2 |
| 88 | : extension_misc::EXTENSION_ICON_LARGE; |
[email protected] | d405067 | 2014-06-04 09:18:40 | [diff] [blame] | 89 | ExtensionResource image = IconsInfo::GetIconResource( |
rdevlin.cronin | 90367644 | 2015-05-15 18:34:10 | [diff] [blame] | 90 | extension_.get(), icon_size, ExtensionIconSet::MATCH_BIGGER); |
[email protected] | eed8549 | 2013-01-14 20:37:46 | [diff] [blame] | 91 | |
| 92 | // Load the image asynchronously. The response will be sent to OnImageLoaded. |
[email protected] | d405067 | 2014-06-04 09:18:40 | [diff] [blame] | 93 | ImageLoader* loader = ImageLoader::Get(profile_); |
[email protected] | 0d0ba18 | 2014-06-03 12:40:43 | [diff] [blame] | 94 | |
[email protected] | 2a4939a | 2014-07-25 09:22:52 | [diff] [blame] | 95 | SetIcon(gfx::Image()); |
[email protected] | d405067 | 2014-06-04 09:18:40 | [diff] [blame] | 96 | std::vector<ImageLoader::ImageRepresentation> images_list; |
| 97 | images_list.push_back(ImageLoader::ImageRepresentation( |
[email protected] | 0d0ba18 | 2014-06-03 12:40:43 | [diff] [blame] | 98 | image, |
[email protected] | d405067 | 2014-06-04 09:18:40 | [diff] [blame] | 99 | ImageLoader::ImageRepresentation::NEVER_RESIZE, |
[email protected] | 0d0ba18 | 2014-06-03 12:40:43 | [diff] [blame] | 100 | gfx::Size(), |
| 101 | ui::SCALE_FACTOR_100P)); |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 102 | loader->LoadImagesAsync(extension_.get(), images_list, |
[email protected] | 0d0ba18 | 2014-06-03 12:40:43 | [diff] [blame] | 103 | base::Bind(&ExtensionUninstallDialog::OnImageLoaded, |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 104 | AsWeakPtr(), extension_->id())); |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | bdd6eec | 2012-03-03 19:58:06 | [diff] [blame] | 107 | void ExtensionUninstallDialog::SetIcon(const gfx::Image& image) { |
| 108 | if (image.IsEmpty()) { |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 109 | // Let's set default icon bitmap whose size is equal to the default icon's |
| 110 | // pixel size under maximal supported scale factor. If the bitmap is larger |
| 111 | // than the one we need, it will be scaled down by the ui code. |
| 112 | // TODO(tbarzic): We should use IconImage here and load the required bitmap |
| 113 | // lazily. |
[email protected] | 32e7a9b | 2013-01-23 23:00:19 | [diff] [blame] | 114 | icon_ = gfx::ImageSkia::CreateFrom1xBitmap( |
[email protected] | 1bc3ef0 | 2012-10-24 16:49:59 | [diff] [blame] | 115 | GetDefaultIconBitmapForMaxScaleFactor(extension_->is_app())); |
[email protected] | bdd6eec | 2012-03-03 19:58:06 | [diff] [blame] | 116 | } else { |
[email protected] | 6280b588 | 2012-06-05 21:56:41 | [diff] [blame] | 117 | icon_ = *image.ToImageSkia(); |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
[email protected] | e0ebb8d | 2014-05-21 20:26:27 | [diff] [blame] | 121 | void ExtensionUninstallDialog::OnImageLoaded(const std::string& extension_id, |
| 122 | const gfx::Image& image) { |
[email protected] | d405067 | 2014-06-04 09:18:40 | [diff] [blame] | 123 | const Extension* target_extension = |
| 124 | ExtensionRegistry::Get(profile_) |
| 125 | ->GetExtensionById(extension_id, ExtensionRegistry::EVERYTHING); |
[email protected] | e0ebb8d | 2014-05-21 20:26:27 | [diff] [blame] | 126 | if (!target_extension) { |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 127 | delegate_->OnExtensionUninstallDialogClosed( |
| 128 | false, base::ASCIIToUTF16(kExtensionRemovedError)); |
[email protected] | e0ebb8d | 2014-05-21 20:26:27 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 132 | SetIcon(image); |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 133 | |
rdevlin.cronin | 6620949 | 2015-06-10 20:44:05 | [diff] [blame] | 134 | switch (ScopedTestDialogAutoConfirm::GetAutoConfirmValue()) { |
| 135 | case ScopedTestDialogAutoConfirm::NONE: |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 136 | Show(); |
| 137 | break; |
rdevlin.cronin | 6620949 | 2015-06-10 20:44:05 | [diff] [blame] | 138 | case ScopedTestDialogAutoConfirm::ACCEPT: |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 139 | OnDialogClosed(CLOSE_ACTION_UNINSTALL); |
| 140 | break; |
rdevlin.cronin | 6620949 | 2015-06-10 20:44:05 | [diff] [blame] | 141 | case ScopedTestDialogAutoConfirm::CANCEL: |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 142 | OnDialogClosed(CLOSE_ACTION_CANCELED); |
| 143 | break; |
| 144 | } |
[email protected] | 5f1ba41 | 2012-06-25 20:10:52 | [diff] [blame] | 145 | } |
[email protected] | 226d79ad | 2014-03-14 21:34:57 | [diff] [blame] | 146 | |
| 147 | std::string ExtensionUninstallDialog::GetHeadingText() { |
| 148 | if (triggering_extension_) { |
| 149 | return l10n_util::GetStringFUTF8( |
| 150 | IDS_EXTENSION_PROGRAMMATIC_UNINSTALL_PROMPT_HEADING, |
| 151 | base::UTF8ToUTF16(triggering_extension_->name()), |
| 152 | base::UTF8ToUTF16(extension_->name())); |
| 153 | } |
| 154 | return l10n_util::GetStringFUTF8(IDS_EXTENSION_UNINSTALL_PROMPT_HEADING, |
| 155 | base::UTF8ToUTF16(extension_->name())); |
| 156 | } |
[email protected] | d405067 | 2014-06-04 09:18:40 | [diff] [blame] | 157 | |
rdevlin.cronin | f3af70d5 | 2015-03-20 03:55:04 | [diff] [blame] | 158 | bool ExtensionUninstallDialog::ShouldShowReportAbuseCheckbox() const { |
rdevlin.cronin | 90367644 | 2015-05-15 18:34:10 | [diff] [blame] | 159 | return ManifestURL::UpdatesFromGallery(extension_.get()); |
rdevlin.cronin | f3af70d5 | 2015-03-20 03:55:04 | [diff] [blame] | 160 | } |
| 161 | |
rdevlin.cronin | 4f01c5f1 | 2015-05-07 17:52:00 | [diff] [blame] | 162 | void ExtensionUninstallDialog::OnDialogClosed(CloseAction action) { |
| 163 | // We don't want to artificially weight any of the options, so only record if |
| 164 | // reporting abuse was available. |
| 165 | if (ShouldShowReportAbuseCheckbox()) { |
| 166 | UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallDialogAction", |
| 167 | action, |
| 168 | CLOSE_ACTION_LAST); |
| 169 | } |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 170 | |
| 171 | bool success = false; |
| 172 | base::string16 error; |
| 173 | switch (action) { |
| 174 | case CLOSE_ACTION_UNINSTALL_AND_REPORT_ABUSE: |
| 175 | HandleReportAbuse(); |
| 176 | // Fall through. |
| 177 | case CLOSE_ACTION_UNINSTALL: { |
| 178 | const Extension* current_extension = |
| 179 | ExtensionRegistry::Get(profile_)->GetExtensionById( |
| 180 | extension_->id(), ExtensionRegistry::EVERYTHING); |
| 181 | if (current_extension) { |
| 182 | success = |
| 183 | ExtensionSystem::Get(profile_) |
| 184 | ->extension_service() |
| 185 | ->UninstallExtension(extension_->id(), uninstall_reason_, |
| 186 | base::Bind(&base::DoNothing), &error); |
| 187 | } else { |
| 188 | error = base::ASCIIToUTF16(kExtensionRemovedError); |
| 189 | } |
| 190 | break; |
| 191 | } |
| 192 | case CLOSE_ACTION_CANCELED: |
| 193 | error = base::ASCIIToUTF16("User canceled uninstall dialog"); |
| 194 | break; |
| 195 | case CLOSE_ACTION_LAST: |
| 196 | NOTREACHED(); |
| 197 | } |
| 198 | |
| 199 | delegate_->OnExtensionUninstallDialogClosed(success, error); |
rdevlin.cronin | 4f01c5f1 | 2015-05-07 17:52:00 | [diff] [blame] | 200 | } |
| 201 | |
rdevlin.cronin | f3af70d5 | 2015-03-20 03:55:04 | [diff] [blame] | 202 | void ExtensionUninstallDialog::HandleReportAbuse() { |
| 203 | chrome::NavigateParams params( |
| 204 | profile_, |
rdevlin.cronin | de23cac | 2015-07-15 23:51:39 | [diff] [blame] | 205 | extension_urls::GetWebstoreReportAbuseUrl(extension_->id(), kReferrerId), |
rdevlin.cronin | f3af70d5 | 2015-03-20 03:55:04 | [diff] [blame] | 206 | ui::PAGE_TRANSITION_LINK); |
| 207 | params.disposition = NEW_FOREGROUND_TAB; |
| 208 | chrome::Navigate(¶ms); |
| 209 | } |
| 210 | |
[email protected] | d405067 | 2014-06-04 09:18:40 | [diff] [blame] | 211 | } // namespace extensions |