[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 1 | // Copyright 2014 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/external_install_error.h" |
| 6 | |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 7 | #include <stddef.h> |
Peter Boström | f47f505d | 2021-04-15 05:03:20 | [diff] [blame] | 8 | |
| 9 | #include <memory> |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 10 | #include <utility> |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 11 | |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 12 | #include "base/bind.h" |
fdoray | 283082bd | 2016-06-02 20:18:46 | [diff] [blame] | 13 | #include "base/location.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 14 | #include "base/macros.h" |
proberge | bc529d6 | 2018-04-24 14:48:26 | [diff] [blame] | 15 | #include "base/metrics/field_trial_params.h" |
rdevlin.cronin | b2daf2e4 | 2016-01-14 20:00:54 | [diff] [blame] | 16 | #include "base/metrics/histogram_macros.h" |
fdoray | 283082bd | 2016-06-02 20:18:46 | [diff] [blame] | 17 | #include "base/single_thread_task_runner.h" |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 18 | #include "base/strings/utf_string_conversions.h" |
fdoray | 283082bd | 2016-06-02 20:18:46 | [diff] [blame] | 19 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 20 | #include "chrome/app/chrome_command_ids.h" |
lazyboy | 0b9b30f | 2016-01-05 03:15:37 | [diff] [blame] | 21 | #include "chrome/browser/extensions/extension_install_error_menu_item_id_provider.h" |
pkotwicz | 2f18178 | 2014-10-29 17:33:45 | [diff] [blame] | 22 | #include "chrome/browser/extensions/extension_install_prompt_show_params.h" |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 23 | #include "chrome/browser/extensions/extension_service.h" |
proberge | bc529d6 | 2018-04-24 14:48:26 | [diff] [blame] | 24 | #include "chrome/browser/extensions/external_install_error_constants.h" |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 25 | #include "chrome/browser/extensions/external_install_manager.h" |
| 26 | #include "chrome/browser/extensions/webstore_data_fetcher.h" |
| 27 | #include "chrome/browser/profiles/profile.h" |
| 28 | #include "chrome/browser/ui/browser.h" |
| 29 | #include "chrome/browser/ui/browser_finder.h" |
| 30 | #include "chrome/browser/ui/global_error/global_error.h" |
| 31 | #include "chrome/browser/ui/global_error/global_error_service.h" |
| 32 | #include "chrome/browser/ui/global_error/global_error_service_factory.h" |
| 33 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
proberge | bc529d6 | 2018-04-24 14:48:26 | [diff] [blame] | 34 | #include "chrome/common/chrome_features.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 35 | #include "chrome/grit/generated_resources.h" |
rdevlin.cronin | a1c3f1a | 2017-05-18 17:45:46 | [diff] [blame] | 36 | #include "components/keyed_service/content/browser_context_dependency_manager.h" |
jam | b84299e | 2016-04-12 16:58:59 | [diff] [blame] | 37 | #include "content/public/browser/storage_partition.h" |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 38 | #include "extensions/browser/extension_registry.h" |
| 39 | #include "extensions/browser/extension_system.h" |
[email protected] | e43c61f | 2014-07-20 21:46:34 | [diff] [blame] | 40 | #include "extensions/browser/uninstall_reason.h" |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 41 | #include "extensions/common/constants.h" |
| 42 | #include "extensions/common/extension.h" |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 43 | #include "ui/base/l10n/l10n_util.h" |
| 44 | #include "ui/gfx/image/image.h" |
rdevlin.cronin | 3fe4bd3 | 2016-01-12 18:45:40 | [diff] [blame] | 45 | #include "ui/gfx/image/image_skia.h" |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 46 | #include "ui/gfx/image/image_skia_operations.h" |
| 47 | |
| 48 | namespace extensions { |
| 49 | |
| 50 | namespace { |
| 51 | |
| 52 | // Return the menu label for a global error. |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 53 | std::u16string GetMenuItemLabel(const Extension* extension) { |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 54 | if (!extension) |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 55 | return std::u16string(); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 56 | |
| 57 | int id = -1; |
| 58 | if (extension->is_app()) |
| 59 | id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_APP; |
| 60 | else if (extension->is_theme()) |
| 61 | id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_THEME; |
| 62 | else |
| 63 | id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_EXTENSION; |
| 64 | |
| 65 | return l10n_util::GetStringFUTF16(id, base::UTF8ToUTF16(extension->name())); |
| 66 | } |
| 67 | |
proberge | bc529d6 | 2018-04-24 14:48:26 | [diff] [blame] | 68 | ExternalInstallError::DefaultDialogButtonSetting |
| 69 | MapDefaultButtonStringToSetting(const std::string& button_setting_string) { |
| 70 | if (button_setting_string == kDefaultDialogButtonSettingOk) |
| 71 | return ExternalInstallError::DIALOG_BUTTON_OK; |
| 72 | if (button_setting_string == kDefaultDialogButtonSettingCancel) |
| 73 | return ExternalInstallError::DIALOG_BUTTON_CANCEL; |
| 74 | if (button_setting_string == kDefaultDialogButtonSettingNoDefault) |
| 75 | return ExternalInstallError::NO_DEFAULT_DIALOG_BUTTON; |
| 76 | |
| 77 | NOTREACHED() << "Unexpected default button string: " << button_setting_string; |
| 78 | return ExternalInstallError::NOT_SPECIFIED; |
| 79 | } |
| 80 | |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 81 | // A global error that spawns a dialog when the menu item is clicked. |
| 82 | class ExternalInstallMenuAlert : public GlobalError { |
| 83 | public: |
| 84 | explicit ExternalInstallMenuAlert(ExternalInstallError* error); |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 85 | ~ExternalInstallMenuAlert() override; |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 86 | |
| 87 | private: |
| 88 | // GlobalError implementation. |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 89 | Severity GetSeverity() override; |
| 90 | bool HasMenuItem() override; |
| 91 | int MenuItemCommandID() override; |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 92 | std::u16string MenuItemLabel() override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 93 | void ExecuteMenuItem(Browser* browser) override; |
| 94 | bool HasBubbleView() override; |
| 95 | bool HasShownBubbleView() override; |
| 96 | void ShowBubbleView(Browser* browser) override; |
| 97 | GlobalErrorBubbleViewBase* GetBubbleView() override; |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 98 | |
| 99 | // The owning ExternalInstallError. |
| 100 | ExternalInstallError* error_; |
| 101 | |
lazyboy | 0b9b30f | 2016-01-05 03:15:37 | [diff] [blame] | 102 | // Provides menu item id for GlobalError. |
| 103 | ExtensionInstallErrorMenuItemIdProvider id_provider_; |
| 104 | |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 105 | DISALLOW_COPY_AND_ASSIGN(ExternalInstallMenuAlert); |
| 106 | }; |
| 107 | |
| 108 | // A global error that spawns a bubble when the menu item is clicked. |
| 109 | class ExternalInstallBubbleAlert : public GlobalErrorWithStandardBubble { |
| 110 | public: |
rdevlin.cronin | 2e25269 | 2015-12-15 21:47:02 | [diff] [blame] | 111 | ExternalInstallBubbleAlert(ExternalInstallError* error, |
| 112 | ExtensionInstallPrompt::Prompt* prompt); |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 113 | ~ExternalInstallBubbleAlert() override; |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 114 | |
| 115 | private: |
| 116 | // GlobalError implementation. |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 117 | Severity GetSeverity() override; |
| 118 | bool HasMenuItem() override; |
| 119 | int MenuItemCommandID() override; |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 120 | std::u16string MenuItemLabel() override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 121 | void ExecuteMenuItem(Browser* browser) override; |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 122 | |
| 123 | // GlobalErrorWithStandardBubble implementation. |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 124 | std::u16string GetBubbleViewTitle() override; |
| 125 | std::vector<std::u16string> GetBubbleViewMessages() override; |
| 126 | std::u16string GetBubbleViewAcceptButtonLabel() override; |
| 127 | std::u16string GetBubbleViewCancelButtonLabel() override; |
proberge | bc529d6 | 2018-04-24 14:48:26 | [diff] [blame] | 128 | int GetDefaultDialogButton() const override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 129 | void OnBubbleViewDidClose(Browser* browser) override; |
| 130 | void BubbleViewAcceptButtonPressed(Browser* browser) override; |
| 131 | void BubbleViewCancelButtonPressed(Browser* browser) override; |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 132 | |
| 133 | // The owning ExternalInstallError. |
| 134 | ExternalInstallError* error_; |
lazyboy | 0b9b30f | 2016-01-05 03:15:37 | [diff] [blame] | 135 | ExtensionInstallErrorMenuItemIdProvider id_provider_; |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 136 | |
| 137 | // The Prompt with all information, which we then use to populate the bubble. |
rdevlin.cronin | 2e25269 | 2015-12-15 21:47:02 | [diff] [blame] | 138 | // Owned by |error|. |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 139 | ExtensionInstallPrompt::Prompt* prompt_; |
| 140 | |
| 141 | DISALLOW_COPY_AND_ASSIGN(ExternalInstallBubbleAlert); |
| 142 | }; |
| 143 | |
| 144 | //////////////////////////////////////////////////////////////////////////////// |
| 145 | // ExternalInstallMenuAlert |
| 146 | |
| 147 | ExternalInstallMenuAlert::ExternalInstallMenuAlert(ExternalInstallError* error) |
| 148 | : error_(error) { |
| 149 | } |
| 150 | |
| 151 | ExternalInstallMenuAlert::~ExternalInstallMenuAlert() { |
| 152 | } |
| 153 | |
| 154 | GlobalError::Severity ExternalInstallMenuAlert::GetSeverity() { |
| 155 | return SEVERITY_LOW; |
| 156 | } |
| 157 | |
| 158 | bool ExternalInstallMenuAlert::HasMenuItem() { |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | int ExternalInstallMenuAlert::MenuItemCommandID() { |
lazyboy | 0b9b30f | 2016-01-05 03:15:37 | [diff] [blame] | 163 | return id_provider_.menu_command_id(); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 164 | } |
| 165 | |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 166 | std::u16string ExternalInstallMenuAlert::MenuItemLabel() { |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 167 | return GetMenuItemLabel(error_->GetExtension()); |
| 168 | } |
| 169 | |
| 170 | void ExternalInstallMenuAlert::ExecuteMenuItem(Browser* browser) { |
| 171 | error_->ShowDialog(browser); |
| 172 | } |
| 173 | |
| 174 | bool ExternalInstallMenuAlert::HasBubbleView() { |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | bool ExternalInstallMenuAlert::HasShownBubbleView() { |
| 179 | NOTREACHED(); |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | void ExternalInstallMenuAlert::ShowBubbleView(Browser* browser) { |
| 184 | NOTREACHED(); |
| 185 | } |
| 186 | |
| 187 | GlobalErrorBubbleViewBase* ExternalInstallMenuAlert::GetBubbleView() { |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | //////////////////////////////////////////////////////////////////////////////// |
| 192 | // ExternalInstallBubbleAlert |
| 193 | |
| 194 | ExternalInstallBubbleAlert::ExternalInstallBubbleAlert( |
| 195 | ExternalInstallError* error, |
| 196 | ExtensionInstallPrompt::Prompt* prompt) |
| 197 | : error_(error), prompt_(prompt) { |
| 198 | DCHECK(error_); |
| 199 | DCHECK(prompt_); |
| 200 | } |
| 201 | |
| 202 | ExternalInstallBubbleAlert::~ExternalInstallBubbleAlert() { |
| 203 | } |
| 204 | |
| 205 | GlobalError::Severity ExternalInstallBubbleAlert::GetSeverity() { |
| 206 | return SEVERITY_LOW; |
| 207 | } |
| 208 | |
| 209 | bool ExternalInstallBubbleAlert::HasMenuItem() { |
| 210 | return true; |
| 211 | } |
| 212 | |
| 213 | int ExternalInstallBubbleAlert::MenuItemCommandID() { |
lazyboy | 0b9b30f | 2016-01-05 03:15:37 | [diff] [blame] | 214 | return id_provider_.menu_command_id(); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 215 | } |
| 216 | |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 217 | std::u16string ExternalInstallBubbleAlert::MenuItemLabel() { |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 218 | return GetMenuItemLabel(error_->GetExtension()); |
| 219 | } |
| 220 | |
| 221 | void ExternalInstallBubbleAlert::ExecuteMenuItem(Browser* browser) { |
lazyboy | 1899eec4 | 2016-03-08 19:00:50 | [diff] [blame] | 222 | // |browser| is nullptr in unit test. |
| 223 | if (browser) |
| 224 | ShowBubbleView(browser); |
| 225 | error_->DidOpenBubbleView(); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 226 | } |
| 227 | |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 228 | std::u16string ExternalInstallBubbleAlert::GetBubbleViewTitle() { |
treib | 5e16e45 | 2015-06-19 09:55:39 | [diff] [blame] | 229 | return l10n_util::GetStringFUTF16( |
| 230 | IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_BUBBLE_TITLE, |
| 231 | base::UTF8ToUTF16(prompt_->extension()->name())); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 232 | } |
| 233 | |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 234 | std::vector<std::u16string> |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 235 | ExternalInstallBubbleAlert::GetBubbleViewMessages() { |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 236 | std::vector<std::u16string> messages; |
treib | 5e16e45 | 2015-06-19 09:55:39 | [diff] [blame] | 237 | int heading_id = |
| 238 | IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_BUBBLE_HEADING_EXTENSION; |
| 239 | if (prompt_->extension()->is_app()) |
| 240 | heading_id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_BUBBLE_HEADING_APP; |
| 241 | else if (prompt_->extension()->is_theme()) |
| 242 | heading_id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_BUBBLE_HEADING_THEME; |
| 243 | messages.push_back(l10n_util::GetStringUTF16(heading_id)); |
| 244 | |
Devlin Cronin | d6e136a | 2018-05-15 23:39:32 | [diff] [blame] | 245 | if (prompt_->GetPermissionCount()) { |
| 246 | messages.push_back(prompt_->GetPermissionsHeading()); |
| 247 | for (size_t i = 0; i < prompt_->GetPermissionCount(); ++i) { |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 248 | messages.push_back(l10n_util::GetStringFUTF16( |
Devlin Cronin | d6e136a | 2018-05-15 23:39:32 | [diff] [blame] | 249 | IDS_EXTENSION_PERMISSION_LINE, prompt_->GetPermission(i))); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | // TODO(yoz): OAuth issue advice? |
| 253 | return messages; |
| 254 | } |
| 255 | |
proberge | bc529d6 | 2018-04-24 14:48:26 | [diff] [blame] | 256 | int ExternalInstallBubbleAlert::GetDefaultDialogButton() const { |
| 257 | switch (error_->default_dialog_button_setting()) { |
| 258 | case ExternalInstallError::DIALOG_BUTTON_OK: |
| 259 | return ui::DIALOG_BUTTON_OK; |
| 260 | case ExternalInstallError::DIALOG_BUTTON_CANCEL: |
| 261 | return ui::DIALOG_BUTTON_CANCEL; |
| 262 | case ExternalInstallError::NO_DEFAULT_DIALOG_BUTTON: |
| 263 | return ui::DIALOG_BUTTON_NONE; |
| 264 | case ExternalInstallError::NOT_SPECIFIED: |
| 265 | break; |
| 266 | } |
| 267 | return GlobalErrorWithStandardBubble::GetDefaultDialogButton(); |
| 268 | } |
| 269 | |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 270 | std::u16string ExternalInstallBubbleAlert::GetBubbleViewAcceptButtonLabel() { |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 271 | return prompt_->GetAcceptButtonLabel(); |
| 272 | } |
| 273 | |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 274 | std::u16string ExternalInstallBubbleAlert::GetBubbleViewCancelButtonLabel() { |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 275 | return prompt_->GetAbortButtonLabel(); |
| 276 | } |
| 277 | |
| 278 | void ExternalInstallBubbleAlert::OnBubbleViewDidClose(Browser* browser) { |
lazyboy | 1899eec4 | 2016-03-08 19:00:50 | [diff] [blame] | 279 | error_->DidCloseBubbleView(); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void ExternalInstallBubbleAlert::BubbleViewAcceptButtonPressed( |
| 283 | Browser* browser) { |
Igor Ruvinov | 6cbc892 | 2021-07-14 17:51:41 | [diff] [blame] | 284 | error_->OnInstallPromptDone(ExtensionInstallPrompt::DoneCallbackPayload( |
| 285 | ExtensionInstallPrompt::Result::ACCEPTED)); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | void ExternalInstallBubbleAlert::BubbleViewCancelButtonPressed( |
| 289 | Browser* browser) { |
Igor Ruvinov | 6cbc892 | 2021-07-14 17:51:41 | [diff] [blame] | 290 | error_->OnInstallPromptDone(ExtensionInstallPrompt::DoneCallbackPayload( |
| 291 | ExtensionInstallPrompt::Result::USER_CANCELED)); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | } // namespace |
| 295 | |
| 296 | //////////////////////////////////////////////////////////////////////////////// |
| 297 | // ExternalInstallError |
| 298 | |
proberge | bc529d6 | 2018-04-24 14:48:26 | [diff] [blame] | 299 | // static |
| 300 | ExternalInstallError::DefaultDialogButtonSetting |
| 301 | ExternalInstallError::GetDefaultDialogButton( |
| 302 | const base::Value& webstore_response) { |
| 303 | const base::Value* value = webstore_response.FindKeyOfType( |
| 304 | kExternalInstallDefaultButtonKey, base::Value::Type::STRING); |
| 305 | if (value) { |
| 306 | return MapDefaultButtonStringToSetting(value->GetString()); |
| 307 | } |
| 308 | |
| 309 | if (base::FeatureList::IsEnabled( |
Oscar Johansson | 7f4c1b93 | 2018-06-12 06:11:58 | [diff] [blame] | 310 | ::features::kExternalExtensionDefaultButtonControl)) { |
proberge | bc529d6 | 2018-04-24 14:48:26 | [diff] [blame] | 311 | std::string default_button = base::GetFieldTrialParamValueByFeature( |
Oscar Johansson | 7f4c1b93 | 2018-06-12 06:11:58 | [diff] [blame] | 312 | ::features::kExternalExtensionDefaultButtonControl, |
proberge | bc529d6 | 2018-04-24 14:48:26 | [diff] [blame] | 313 | kExternalInstallDefaultButtonKey); |
| 314 | if (!default_button.empty()) { |
| 315 | return MapDefaultButtonStringToSetting(default_button); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | return NOT_SPECIFIED; |
| 320 | } |
| 321 | |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 322 | ExternalInstallError::ExternalInstallError( |
| 323 | content::BrowserContext* browser_context, |
| 324 | const std::string& extension_id, |
| 325 | AlertType alert_type, |
| 326 | ExternalInstallManager* manager) |
| 327 | : browser_context_(browser_context), |
| 328 | extension_id_(extension_id), |
| 329 | alert_type_(alert_type), |
| 330 | manager_(manager), |
| 331 | error_service_(GlobalErrorServiceFactory::GetForProfile( |
Jeremy Roman | 495db68 | 2019-07-12 16:03:24 | [diff] [blame] | 332 | Profile::FromBrowserContext(browser_context_))) { |
Peter Boström | f47f505d | 2021-04-15 05:03:20 | [diff] [blame] | 333 | prompt_ = std::make_unique<ExtensionInstallPrompt::Prompt>( |
| 334 | ExtensionInstallPrompt::EXTERNAL_INSTALL_PROMPT); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 335 | |
Peter Boström | f47f505d | 2021-04-15 05:03:20 | [diff] [blame] | 336 | webstore_data_fetcher_ = |
| 337 | std::make_unique<WebstoreDataFetcher>(this, GURL(), extension_id_); |
Lukasz Anforowicz | b9a969a | 2021-04-29 15:26:25 | [diff] [blame] | 338 | webstore_data_fetcher_->Start(browser_context_->GetDefaultStoragePartition() |
| 339 | ->GetURLLoaderFactoryForBrowserProcess() |
| 340 | .get()); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | ExternalInstallError::~ExternalInstallError() { |
rdevlin.cronin | a1c3f1a | 2017-05-18 17:45:46 | [diff] [blame] | 344 | #if DCHECK_IS_ON() |
| 345 | // Errors should only be removed while the profile is valid, since removing |
| 346 | // the error can trigger other subsystems listening for changes. |
| 347 | BrowserContextDependencyManager::GetInstance() |
| 348 | ->AssertBrowserContextWasntDestroyed(browser_context_); |
| 349 | #endif |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 350 | if (global_error_.get()) |
avi | 2451b25 | 2016-12-13 16:55:17 | [diff] [blame] | 351 | error_service_->RemoveUnownedGlobalError(global_error_.get()); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 352 | } |
| 353 | |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 354 | void ExternalInstallError::OnInstallPromptDone( |
Igor Ruvinov | 6cbc892 | 2021-07-14 17:51:41 | [diff] [blame] | 355 | ExtensionInstallPrompt::DoneCallbackPayload payload) { |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 356 | const Extension* extension = GetExtension(); |
rdevlin.cronin | b2daf2e4 | 2016-01-14 20:00:54 | [diff] [blame] | 357 | |
| 358 | // If the error isn't removed and deleted as part of handling the user's |
| 359 | // response (which can happen, e.g., if an uninstall fails), be sure to remove |
| 360 | // the error directly in order to ensure it's not called twice. |
fdoray | 283082bd | 2016-06-02 20:18:46 | [diff] [blame] | 361 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
tzik | 8d880ee | 2017-04-20 19:46:24 | [diff] [blame] | 362 | FROM_HERE, base::BindOnce(&ExternalInstallError::RemoveError, |
| 363 | weak_factory_.GetWeakPtr())); |
rdevlin.cronin | b2daf2e4 | 2016-01-14 20:00:54 | [diff] [blame] | 364 | |
Igor Ruvinov | 6cbc892 | 2021-07-14 17:51:41 | [diff] [blame] | 365 | switch (payload.result) { |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 366 | case ExtensionInstallPrompt::Result::ACCEPTED: |
Tim Judkins | e922187 | 2020-01-25 01:37:53 | [diff] [blame] | 367 | case ExtensionInstallPrompt::Result::ACCEPTED_AND_OPTION_CHECKED: |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 368 | if (extension) { |
| 369 | ExtensionSystem::Get(browser_context_) |
| 370 | ->extension_service() |
| 371 | ->GrantPermissionsAndEnableExtension(extension); |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 372 | } |
| 373 | break; |
| 374 | case ExtensionInstallPrompt::Result::USER_CANCELED: |
| 375 | if (extension) { |
Devlin Cronin | 24cd22c3 | 2019-01-29 18:14:36 | [diff] [blame] | 376 | ExtensionSystem::Get(browser_context_) |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 377 | ->extension_service() |
| 378 | ->UninstallExtension(extension_id_, |
| 379 | extensions::UNINSTALL_REASON_INSTALL_CANCELED, |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 380 | nullptr); // Ignore error. |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 381 | } |
| 382 | break; |
| 383 | case ExtensionInstallPrompt::Result::ABORTED: |
lazyboy | 1899eec4 | 2016-03-08 19:00:50 | [diff] [blame] | 384 | manager_->DidChangeInstallAlertVisibility(this, false); |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 385 | break; |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 386 | } |
rdevlin.cronin | b2daf2e4 | 2016-01-14 20:00:54 | [diff] [blame] | 387 | // NOTE: We may be deleted here! |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 388 | } |
| 389 | |
lazyboy | 1899eec4 | 2016-03-08 19:00:50 | [diff] [blame] | 390 | void ExternalInstallError::DidOpenBubbleView() { |
| 391 | manager_->DidChangeInstallAlertVisibility(this, true); |
| 392 | } |
| 393 | |
| 394 | void ExternalInstallError::DidCloseBubbleView() { |
| 395 | manager_->DidChangeInstallAlertVisibility(this, false); |
| 396 | } |
| 397 | |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 398 | void ExternalInstallError::ShowDialog(Browser* browser) { |
| 399 | DCHECK(install_ui_.get()); |
| 400 | DCHECK(prompt_.get()); |
| 401 | DCHECK(browser); |
| 402 | content::WebContents* web_contents = NULL; |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 403 | web_contents = browser->tab_strip_model()->GetActiveWebContents(); |
lazyboy | 1899eec4 | 2016-03-08 19:00:50 | [diff] [blame] | 404 | manager_->DidChangeInstallAlertVisibility(this, true); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 405 | ExtensionInstallPrompt::GetDefaultShowDialogCallback().Run( |
Istiaque Ahmed | 52f8fec | 2021-05-19 20:15:39 | [diff] [blame] | 406 | std::make_unique<ExtensionInstallPromptShowParams>(web_contents), |
Yi Gu | 7fabef12 | 2020-12-16 13:03:31 | [diff] [blame] | 407 | base::BindOnce(&ExternalInstallError::OnInstallPromptDone, |
| 408 | weak_factory_.GetWeakPtr()), |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 409 | std::move(prompt_)); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | const Extension* ExternalInstallError::GetExtension() const { |
| 413 | return ExtensionRegistry::Get(browser_context_) |
| 414 | ->GetExtensionById(extension_id_, ExtensionRegistry::EVERYTHING); |
| 415 | } |
| 416 | |
Toby Huang | 7b4816f | 2020-02-07 23:54:07 | [diff] [blame] | 417 | void ExternalInstallError::OnWebstoreRequestFailure( |
| 418 | const std::string& extension_id) { |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 419 | OnFetchComplete(); |
| 420 | } |
| 421 | |
| 422 | void ExternalInstallError::OnWebstoreResponseParseSuccess( |
Toby Huang | 7b4816f | 2020-02-07 23:54:07 | [diff] [blame] | 423 | const std::string& extension_id, |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 424 | std::unique_ptr<base::DictionaryValue> webstore_data) { |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 425 | std::string localized_user_count; |
[email protected] | 96aebe2 | 2014-07-16 04:07:51 | [diff] [blame] | 426 | double average_rating = 0; |
| 427 | int rating_count = 0; |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 428 | if (!webstore_data->GetString(kUsersKey, &localized_user_count) || |
| 429 | !webstore_data->GetDouble(kAverageRatingKey, &average_rating) || |
| 430 | !webstore_data->GetInteger(kRatingCountKey, &rating_count)) { |
| 431 | // If we don't get a valid webstore response, short circuit, and continue |
| 432 | // to show a prompt without webstore data. |
| 433 | OnFetchComplete(); |
| 434 | return; |
| 435 | } |
| 436 | |
proberge | bc529d6 | 2018-04-24 14:48:26 | [diff] [blame] | 437 | default_dialog_button_setting_ = GetDefaultDialogButton(*webstore_data.get()); |
| 438 | |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 439 | bool show_user_count = true; |
| 440 | webstore_data->GetBoolean(kShowUserCountKey, &show_user_count); |
| 441 | |
| 442 | prompt_->SetWebstoreData( |
| 443 | localized_user_count, show_user_count, average_rating, rating_count); |
| 444 | OnFetchComplete(); |
| 445 | } |
| 446 | |
| 447 | void ExternalInstallError::OnWebstoreResponseParseFailure( |
Toby Huang | 7b4816f | 2020-02-07 23:54:07 | [diff] [blame] | 448 | const std::string& extension_id, |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 449 | const std::string& error) { |
| 450 | OnFetchComplete(); |
| 451 | } |
| 452 | |
| 453 | void ExternalInstallError::OnFetchComplete() { |
| 454 | // Create a new ExtensionInstallPrompt. We pass in NULL for the UI |
| 455 | // components because we display at a later point, and don't want |
| 456 | // to pass ones which may be invalidated. |
Peter Boström | f47f505d | 2021-04-15 05:03:20 | [diff] [blame] | 457 | install_ui_ = base::WrapUnique( |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 458 | new ExtensionInstallPrompt(Profile::FromBrowserContext(browser_context_), |
Peter Boström | f47f505d | 2021-04-15 05:03:20 | [diff] [blame] | 459 | /*native_window=*/nullptr)); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 460 | |
Yi Gu | 7fabef12 | 2020-12-16 13:03:31 | [diff] [blame] | 461 | install_ui_->ShowDialog( |
| 462 | base::BindOnce(&ExternalInstallError::OnInstallPromptDone, |
| 463 | weak_factory_.GetWeakPtr()), |
| 464 | GetExtension(), |
| 465 | nullptr, // Force a fetch of the icon. |
| 466 | std::move(prompt_), |
| 467 | base::BindRepeating(&ExternalInstallError::OnDialogReady, |
| 468 | weak_factory_.GetWeakPtr())); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void ExternalInstallError::OnDialogReady( |
Istiaque Ahmed | 52f8fec | 2021-05-19 20:15:39 | [diff] [blame] | 472 | std::unique_ptr<ExtensionInstallPromptShowParams> show_params, |
Yi Gu | 7fabef12 | 2020-12-16 13:03:31 | [diff] [blame] | 473 | ExtensionInstallPrompt::DoneCallback callback, |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 474 | std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt) { |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 475 | prompt_ = std::move(prompt); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 476 | |
| 477 | if (alert_type_ == BUBBLE_ALERT) { |
Peter Boström | f47f505d | 2021-04-15 05:03:20 | [diff] [blame] | 478 | global_error_ = |
| 479 | std::make_unique<ExternalInstallBubbleAlert>(this, prompt_.get()); |
avi | 2451b25 | 2016-12-13 16:55:17 | [diff] [blame] | 480 | error_service_->AddUnownedGlobalError(global_error_.get()); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 481 | |
lazyboy | 1899eec4 | 2016-03-08 19:00:50 | [diff] [blame] | 482 | if (!manager_->has_currently_visible_install_alert()) { |
| 483 | // |browser| is nullptr during unit tests, so call |
| 484 | // DidChangeInstallAlertVisibility() regardless because we depend on this |
| 485 | // in unit tests. |
| 486 | manager_->DidChangeInstallAlertVisibility(this, true); |
| 487 | Browser* browser = chrome::FindTabbedBrowser( |
| 488 | Profile::FromBrowserContext(browser_context_), true); |
| 489 | if (browser) |
| 490 | global_error_->ShowBubbleView(browser); |
| 491 | } |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 492 | } else { |
| 493 | DCHECK(alert_type_ == MENU_ALERT); |
Peter Boström | f47f505d | 2021-04-15 05:03:20 | [diff] [blame] | 494 | global_error_ = std::make_unique<ExternalInstallMenuAlert>(this); |
avi | 2451b25 | 2016-12-13 16:55:17 | [diff] [blame] | 495 | error_service_->AddUnownedGlobalError(global_error_.get()); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
rdevlin.cronin | b2daf2e4 | 2016-01-14 20:00:54 | [diff] [blame] | 499 | void ExternalInstallError::RemoveError() { |
| 500 | manager_->RemoveExternalInstallError(extension_id_); |
| 501 | } |
| 502 | |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 503 | } // namespace extensions |