[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" |
khmel | 0df82202 | 2017-06-23 02:02:56 | [diff] [blame^] | 12 | #include "chrome/browser/extensions/chrome_app_icon_service.h" |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 13 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 7eb20e3 | 2014-04-30 08:50:56 | [diff] [blame] | 14 | #include "chrome/browser/extensions/extension_util.h" |
[email protected] | 472522b | 2013-10-25 00:41:28 | [diff] [blame] | 15 | #include "chrome/browser/profiles/profile.h" |
rdevlin.cronin | f3af70d5 | 2015-03-20 03:55:04 | [diff] [blame] | 16 | #include "chrome/browser/ui/browser_navigator.h" |
thestig | e8082124 | 2015-09-30 23:46:08 | [diff] [blame] | 17 | #include "chrome/browser/ui/browser_navigator_params.h" |
khmel | 0df82202 | 2017-06-23 02:02:56 | [diff] [blame^] | 18 | #include "chrome/browser/ui/native_window_tracker.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 19 | #include "chrome/grit/generated_resources.h" |
rdevlin.cronin | 6620949 | 2015-06-10 20:44:05 | [diff] [blame] | 20 | #include "extensions/browser/extension_dialog_auto_confirm.h" |
[email protected] | e0ebb8d | 2014-05-21 20:26:27 | [diff] [blame] | 21 | #include "extensions/browser/extension_registry.h" |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 22 | #include "extensions/browser/extension_system.h" |
[email protected] | 326e6f0 | 2014-06-20 04:53:37 | [diff] [blame] | 23 | #include "extensions/browser/image_loader.h" |
[email protected] | cda103d | 2014-04-04 16:22:39 | [diff] [blame] | 24 | #include "extensions/common/constants.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 25 | #include "extensions/common/extension.h" |
rdevlin.cronin | f3af70d5 | 2015-03-20 03:55:04 | [diff] [blame] | 26 | #include "extensions/common/extension_urls.h" |
[email protected] | 0db486f | 2014-04-09 19:32:22 | [diff] [blame] | 27 | #include "extensions/common/manifest_handlers/icons_handler.h" |
rdevlin.cronin | dbb8fa5 | 2015-05-06 00:16:56 | [diff] [blame] | 28 | #include "extensions/common/manifest_url_handlers.h" |
[email protected] | 226d79ad | 2014-03-14 21:34:57 | [diff] [blame] | 29 | #include "ui/base/l10n/l10n_util.h" |
khmel | 0df82202 | 2017-06-23 02:02:56 | [diff] [blame^] | 30 | #include "ui/base/layout.h" |
rdevlin.cronin | f3af70d5 | 2015-03-20 03:55:04 | [diff] [blame] | 31 | #include "ui/base/page_transition_types.h" |
| 32 | #include "ui/base/window_open_disposition.h" |
khmel | 0df82202 | 2017-06-23 02:02:56 | [diff] [blame^] | 33 | #include "ui/display/display.h" |
| 34 | #include "ui/display/screen.h" |
[email protected] | bdd6eec | 2012-03-03 19:58:06 | [diff] [blame] | 35 | #include "ui/gfx/image/image.h" |
[email protected] | 50b6626 | 2013-09-24 03:25:48 | [diff] [blame] | 36 | #include "ui/gfx/image/image_skia.h" |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 37 | |
[email protected] | d405067 | 2014-06-04 09:18:40 | [diff] [blame] | 38 | namespace extensions { |
| 39 | |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 40 | namespace { |
| 41 | |
khmel | 0df82202 | 2017-06-23 02:02:56 | [diff] [blame^] | 42 | constexpr int kIconSize = 64; |
| 43 | |
| 44 | constexpr char kExtensionRemovedError[] = |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 45 | "Extension was removed before dialog closed."; |
| 46 | |
khmel | 0df82202 | 2017-06-23 02:02:56 | [diff] [blame^] | 47 | constexpr char kReferrerId[] = "chrome-remove-extension-dialog"; |
rdevlin.cronin | de23cac | 2015-07-15 23:51:39 | [diff] [blame] | 48 | |
khmel | 0df82202 | 2017-06-23 02:02:56 | [diff] [blame^] | 49 | float GetScaleFactor(gfx::NativeWindow window) { |
| 50 | const display::Screen* screen = display::Screen::GetScreen(); |
| 51 | if (!screen) |
| 52 | return 1.0; // Happens in unit_tests. |
| 53 | if (window) |
| 54 | return screen->GetDisplayNearestWindow(window).device_scale_factor(); |
| 55 | return screen->GetPrimaryDisplay().device_scale_factor(); |
[email protected] | dd46a4ce | 2012-09-15 10:50:50 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | } // namespace |
| 59 | |
[email protected] | 6f03db06 | 2011-09-22 20:37:14 | [diff] [blame] | 60 | ExtensionUninstallDialog::ExtensionUninstallDialog( |
[email protected] | 520414b1 | 2013-01-22 19:27:36 | [diff] [blame] | 61 | Profile* profile, |
khmel | 0df82202 | 2017-06-23 02:02:56 | [diff] [blame^] | 62 | gfx::NativeWindow parent, |
[email protected] | 6f03db06 | 2011-09-22 20:37:14 | [diff] [blame] | 63 | ExtensionUninstallDialog::Delegate* delegate) |
khmel | 0df82202 | 2017-06-23 02:02:56 | [diff] [blame^] | 64 | : profile_(profile), parent_(parent), delegate_(delegate), observer_(this) { |
| 65 | if (parent) |
| 66 | parent_window_tracker_ = NativeWindowTracker::Create(parent); |
[email protected] | 5f1ba41 | 2012-06-25 20:10:52 | [diff] [blame] | 67 | } |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 68 | |
khmel | 0df82202 | 2017-06-23 02:02:56 | [diff] [blame^] | 69 | ExtensionUninstallDialog::~ExtensionUninstallDialog() = default; |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 70 | |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 71 | void ExtensionUninstallDialog::ConfirmUninstallByExtension( |
| 72 | const scoped_refptr<const Extension>& extension, |
| 73 | const scoped_refptr<const Extension>& triggering_extension, |
rdevlin.cronin | e18eb8cd | 2015-07-16 16:31:28 | [diff] [blame] | 74 | UninstallReason reason, |
| 75 | UninstallSource source) { |
[email protected] | 226d79ad | 2014-03-14 21:34:57 | [diff] [blame] | 76 | triggering_extension_ = triggering_extension; |
rdevlin.cronin | e18eb8cd | 2015-07-16 16:31:28 | [diff] [blame] | 77 | ConfirmUninstall(extension, reason, source); |
[email protected] | 226d79ad | 2014-03-14 21:34:57 | [diff] [blame] | 78 | } |
| 79 | |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 80 | void ExtensionUninstallDialog::ConfirmUninstall( |
| 81 | const scoped_refptr<const Extension>& extension, |
rdevlin.cronin | e18eb8cd | 2015-07-16 16:31:28 | [diff] [blame] | 82 | UninstallReason reason, |
| 83 | UninstallSource source) { |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 84 | DCHECK(thread_checker_.CalledOnValidThread()); |
rdevlin.cronin | e18eb8cd | 2015-07-16 16:31:28 | [diff] [blame] | 85 | |
| 86 | UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallSource", source, |
| 87 | NUM_UNINSTALL_SOURCES); |
| 88 | |
[email protected] | 502e3961 | 2011-03-26 01:36:28 | [diff] [blame] | 89 | extension_ = extension; |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 90 | uninstall_reason_ = reason; |
[email protected] | eed8549 | 2013-01-14 20:37:46 | [diff] [blame] | 91 | |
khmel | 0df82202 | 2017-06-23 02:02:56 | [diff] [blame^] | 92 | if (parent() && parent_window_tracker_->WasNativeWindowClosed()) { |
| 93 | OnDialogClosed(CLOSE_ACTION_CANCELED); |
[email protected] | e0ebb8d | 2014-05-21 20:26:27 | [diff] [blame] | 94 | return; |
| 95 | } |
| 96 | |
khmel | 0df82202 | 2017-06-23 02:02:56 | [diff] [blame^] | 97 | // Track that extension uninstalled externally. |
| 98 | DCHECK(!observer_.IsObserving(ExtensionRegistry::Get(profile_))); |
| 99 | observer_.Add(ExtensionRegistry::Get(profile_)); |
| 100 | |
| 101 | // Dialog will be shown once icon is loaded. |
| 102 | DCHECK(!dialog_shown_); |
| 103 | icon_ = ChromeAppIconService::Get(profile_)->CreateIcon(this, extension->id(), |
| 104 | kIconSize); |
| 105 | icon_->image_skia().GetRepresentation(GetScaleFactor(parent_)); |
| 106 | } |
| 107 | |
| 108 | void ExtensionUninstallDialog::OnIconUpdated(ChromeAppIcon* icon) { |
| 109 | // Ignore initial update. |
| 110 | if (!icon_ || dialog_shown_) |
| 111 | return; |
| 112 | |
| 113 | DCHECK_EQ(icon, icon_.get()); |
| 114 | |
| 115 | dialog_shown_ = true; |
| 116 | |
| 117 | if (parent() && parent_window_tracker_->WasNativeWindowClosed()) { |
| 118 | OnDialogClosed(CLOSE_ACTION_CANCELED); |
| 119 | return; |
| 120 | } |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 121 | |
rdevlin.cronin | 6620949 | 2015-06-10 20:44:05 | [diff] [blame] | 122 | switch (ScopedTestDialogAutoConfirm::GetAutoConfirmValue()) { |
| 123 | case ScopedTestDialogAutoConfirm::NONE: |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 124 | Show(); |
| 125 | break; |
catmullings | ff559d9 | 2017-05-20 01:43:36 | [diff] [blame] | 126 | case ScopedTestDialogAutoConfirm::ACCEPT_AND_OPTION: |
| 127 | OnDialogClosed(CLOSE_ACTION_UNINSTALL_AND_REPORT_ABUSE); |
| 128 | break; |
rdevlin.cronin | 6620949 | 2015-06-10 20:44:05 | [diff] [blame] | 129 | case ScopedTestDialogAutoConfirm::ACCEPT: |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 130 | OnDialogClosed(CLOSE_ACTION_UNINSTALL); |
| 131 | break; |
rdevlin.cronin | 6620949 | 2015-06-10 20:44:05 | [diff] [blame] | 132 | case ScopedTestDialogAutoConfirm::CANCEL: |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 133 | OnDialogClosed(CLOSE_ACTION_CANCELED); |
| 134 | break; |
| 135 | } |
[email protected] | 5f1ba41 | 2012-06-25 20:10:52 | [diff] [blame] | 136 | } |
[email protected] | 226d79ad | 2014-03-14 21:34:57 | [diff] [blame] | 137 | |
khmel | 0df82202 | 2017-06-23 02:02:56 | [diff] [blame^] | 138 | void ExtensionUninstallDialog::OnExtensionUninstalled( |
| 139 | content::BrowserContext* browser_context, |
| 140 | const Extension* extension, |
| 141 | UninstallReason reason) { |
| 142 | // Handle the case when extension was uninstalled externally and we have to |
| 143 | // close current dialog. |
| 144 | if (extension != extension_) |
| 145 | return; |
| 146 | |
| 147 | delegate_->OnExtensionUninstallDialogClosed( |
| 148 | false, base::ASCIIToUTF16(kExtensionRemovedError)); |
| 149 | } |
| 150 | |
[email protected] | 226d79ad | 2014-03-14 21:34:57 | [diff] [blame] | 151 | std::string ExtensionUninstallDialog::GetHeadingText() { |
| 152 | if (triggering_extension_) { |
| 153 | return l10n_util::GetStringFUTF8( |
| 154 | IDS_EXTENSION_PROGRAMMATIC_UNINSTALL_PROMPT_HEADING, |
| 155 | base::UTF8ToUTF16(triggering_extension_->name()), |
| 156 | base::UTF8ToUTF16(extension_->name())); |
| 157 | } |
| 158 | return l10n_util::GetStringFUTF8(IDS_EXTENSION_UNINSTALL_PROMPT_HEADING, |
| 159 | base::UTF8ToUTF16(extension_->name())); |
| 160 | } |
[email protected] | d405067 | 2014-06-04 09:18:40 | [diff] [blame] | 161 | |
rdevlin.cronin | f3af70d5 | 2015-03-20 03:55:04 | [diff] [blame] | 162 | bool ExtensionUninstallDialog::ShouldShowReportAbuseCheckbox() const { |
rdevlin.cronin | 90367644 | 2015-05-15 18:34:10 | [diff] [blame] | 163 | return ManifestURL::UpdatesFromGallery(extension_.get()); |
rdevlin.cronin | f3af70d5 | 2015-03-20 03:55:04 | [diff] [blame] | 164 | } |
| 165 | |
rdevlin.cronin | 4f01c5f1 | 2015-05-07 17:52:00 | [diff] [blame] | 166 | void ExtensionUninstallDialog::OnDialogClosed(CloseAction action) { |
| 167 | // We don't want to artificially weight any of the options, so only record if |
| 168 | // reporting abuse was available. |
| 169 | if (ShouldShowReportAbuseCheckbox()) { |
| 170 | UMA_HISTOGRAM_ENUMERATION("Extensions.UninstallDialogAction", |
| 171 | action, |
| 172 | CLOSE_ACTION_LAST); |
| 173 | } |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 174 | |
| 175 | bool success = false; |
| 176 | base::string16 error; |
| 177 | switch (action) { |
| 178 | case CLOSE_ACTION_UNINSTALL_AND_REPORT_ABUSE: |
catmullings | ff559d9 | 2017-05-20 01:43:36 | [diff] [blame] | 179 | // If the extension specifies a custom uninstall page via |
| 180 | // chrome.runtime.setUninstallURL, then at uninstallation its uninstall |
| 181 | // page opens. To ensure that the CWS Report Abuse page is the active tab |
| 182 | // at uninstallation, HandleReportAbuse() is called after Uninstall(). |
| 183 | success = Uninstall(&error); |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 184 | HandleReportAbuse(); |
catmullings | ff559d9 | 2017-05-20 01:43:36 | [diff] [blame] | 185 | break; |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 186 | case CLOSE_ACTION_UNINSTALL: { |
catmullings | ff559d9 | 2017-05-20 01:43:36 | [diff] [blame] | 187 | success = Uninstall(&error); |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 188 | break; |
| 189 | } |
| 190 | case CLOSE_ACTION_CANCELED: |
| 191 | error = base::ASCIIToUTF16("User canceled uninstall dialog"); |
| 192 | break; |
| 193 | case CLOSE_ACTION_LAST: |
| 194 | NOTREACHED(); |
| 195 | } |
rdevlin.cronin | ac8ce8c | 2015-05-27 23:00:02 | [diff] [blame] | 196 | delegate_->OnExtensionUninstallDialogClosed(success, error); |
rdevlin.cronin | 4f01c5f1 | 2015-05-07 17:52:00 | [diff] [blame] | 197 | } |
| 198 | |
catmullings | ff559d9 | 2017-05-20 01:43:36 | [diff] [blame] | 199 | bool ExtensionUninstallDialog::Uninstall(base::string16* error) { |
| 200 | const Extension* current_extension = |
| 201 | ExtensionRegistry::Get(profile_)->GetExtensionById( |
| 202 | extension_->id(), ExtensionRegistry::EVERYTHING); |
| 203 | if (current_extension) { |
khmel | 0df82202 | 2017-06-23 02:02:56 | [diff] [blame^] | 204 | // Prevent notifications triggered by our request. |
| 205 | observer_.RemoveAll(); |
catmullings | ff559d9 | 2017-05-20 01:43:36 | [diff] [blame] | 206 | return ExtensionSystem::Get(profile_) |
| 207 | ->extension_service() |
| 208 | ->UninstallExtension(extension_->id(), uninstall_reason_, |
| 209 | base::Bind(&base::DoNothing), error); |
| 210 | } |
| 211 | *error = base::ASCIIToUTF16(kExtensionRemovedError); |
| 212 | return false; |
| 213 | } |
| 214 | |
rdevlin.cronin | f3af70d5 | 2015-03-20 03:55:04 | [diff] [blame] | 215 | void ExtensionUninstallDialog::HandleReportAbuse() { |
| 216 | chrome::NavigateParams params( |
| 217 | profile_, |
rdevlin.cronin | de23cac | 2015-07-15 23:51:39 | [diff] [blame] | 218 | extension_urls::GetWebstoreReportAbuseUrl(extension_->id(), kReferrerId), |
rdevlin.cronin | f3af70d5 | 2015-03-20 03:55:04 | [diff] [blame] | 219 | ui::PAGE_TRANSITION_LINK); |
nick | 3b04f32 | 2016-08-31 19:29:19 | [diff] [blame] | 220 | params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; |
rdevlin.cronin | f3af70d5 | 2015-03-20 03:55:04 | [diff] [blame] | 221 | chrome::Navigate(¶ms); |
| 222 | } |
| 223 | |
[email protected] | d405067 | 2014-06-04 09:18:40 | [diff] [blame] | 224 | } // namespace extensions |