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