[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() { |
gpdavis.chromium | 0fbac4d | 2014-09-19 20:57:54 | [diff] [blame] | 246 | ExtensionInstallPrompt::PermissionsType regular_permissions = |
| 247 | ExtensionInstallPrompt::PermissionsType::REGULAR_PERMISSIONS; |
| 248 | ExtensionInstallPrompt::PermissionsType withheld_permissions = |
| 249 | ExtensionInstallPrompt::PermissionsType::WITHHELD_PERMISSIONS; |
| 250 | |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 251 | std::vector<base::string16> messages; |
treib | 5e16e45 | 2015-06-19 09:55:39 | [diff] [blame] | 252 | int heading_id = |
| 253 | IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_BUBBLE_HEADING_EXTENSION; |
| 254 | if (prompt_->extension()->is_app()) |
| 255 | heading_id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_BUBBLE_HEADING_APP; |
| 256 | else if (prompt_->extension()->is_theme()) |
| 257 | heading_id = IDS_EXTENSION_EXTERNAL_INSTALL_ALERT_BUBBLE_HEADING_THEME; |
| 258 | messages.push_back(l10n_util::GetStringUTF16(heading_id)); |
| 259 | |
gpdavis.chromium | 0fbac4d | 2014-09-19 20:57:54 | [diff] [blame] | 260 | if (prompt_->GetPermissionCount(regular_permissions)) { |
| 261 | messages.push_back(prompt_->GetPermissionsHeading(regular_permissions)); |
| 262 | for (size_t i = 0; i < prompt_->GetPermissionCount(regular_permissions); |
| 263 | ++i) { |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 264 | messages.push_back(l10n_util::GetStringFUTF16( |
gpdavis.chromium | 0fbac4d | 2014-09-19 20:57:54 | [diff] [blame] | 265 | IDS_EXTENSION_PERMISSION_LINE, |
| 266 | prompt_->GetPermission(i, regular_permissions))); |
| 267 | } |
| 268 | } |
| 269 | if (prompt_->GetPermissionCount(withheld_permissions)) { |
| 270 | messages.push_back(prompt_->GetPermissionsHeading(withheld_permissions)); |
| 271 | for (size_t i = 0; i < prompt_->GetPermissionCount(withheld_permissions); |
| 272 | ++i) { |
| 273 | messages.push_back(l10n_util::GetStringFUTF16( |
| 274 | IDS_EXTENSION_PERMISSION_LINE, |
| 275 | prompt_->GetPermission(i, withheld_permissions))); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | // TODO(yoz): OAuth issue advice? |
| 279 | return messages; |
| 280 | } |
| 281 | |
proberge | bc529d6 | 2018-04-24 14:48:26 | [diff] [blame^] | 282 | int ExternalInstallBubbleAlert::GetDefaultDialogButton() const { |
| 283 | switch (error_->default_dialog_button_setting()) { |
| 284 | case ExternalInstallError::DIALOG_BUTTON_OK: |
| 285 | return ui::DIALOG_BUTTON_OK; |
| 286 | case ExternalInstallError::DIALOG_BUTTON_CANCEL: |
| 287 | return ui::DIALOG_BUTTON_CANCEL; |
| 288 | case ExternalInstallError::NO_DEFAULT_DIALOG_BUTTON: |
| 289 | return ui::DIALOG_BUTTON_NONE; |
| 290 | case ExternalInstallError::NOT_SPECIFIED: |
| 291 | break; |
| 292 | } |
| 293 | return GlobalErrorWithStandardBubble::GetDefaultDialogButton(); |
| 294 | } |
| 295 | |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 296 | base::string16 ExternalInstallBubbleAlert::GetBubbleViewAcceptButtonLabel() { |
| 297 | return prompt_->GetAcceptButtonLabel(); |
| 298 | } |
| 299 | |
| 300 | base::string16 ExternalInstallBubbleAlert::GetBubbleViewCancelButtonLabel() { |
| 301 | return prompt_->GetAbortButtonLabel(); |
| 302 | } |
| 303 | |
| 304 | void ExternalInstallBubbleAlert::OnBubbleViewDidClose(Browser* browser) { |
lazyboy | 1899eec4 | 2016-03-08 19:00:50 | [diff] [blame] | 305 | error_->DidCloseBubbleView(); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void ExternalInstallBubbleAlert::BubbleViewAcceptButtonPressed( |
| 309 | Browser* browser) { |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 310 | error_->OnInstallPromptDone(ExtensionInstallPrompt::Result::ACCEPTED); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | void ExternalInstallBubbleAlert::BubbleViewCancelButtonPressed( |
| 314 | Browser* browser) { |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 315 | error_->OnInstallPromptDone(ExtensionInstallPrompt::Result::USER_CANCELED); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | } // namespace |
| 319 | |
| 320 | //////////////////////////////////////////////////////////////////////////////// |
| 321 | // ExternalInstallError |
| 322 | |
proberge | bc529d6 | 2018-04-24 14:48:26 | [diff] [blame^] | 323 | // static |
| 324 | ExternalInstallError::DefaultDialogButtonSetting |
| 325 | ExternalInstallError::GetDefaultDialogButton( |
| 326 | const base::Value& webstore_response) { |
| 327 | const base::Value* value = webstore_response.FindKeyOfType( |
| 328 | kExternalInstallDefaultButtonKey, base::Value::Type::STRING); |
| 329 | if (value) { |
| 330 | return MapDefaultButtonStringToSetting(value->GetString()); |
| 331 | } |
| 332 | |
| 333 | if (base::FeatureList::IsEnabled( |
| 334 | features::kExternalExtensionDefaultButtonControl)) { |
| 335 | std::string default_button = base::GetFieldTrialParamValueByFeature( |
| 336 | features::kExternalExtensionDefaultButtonControl, |
| 337 | kExternalInstallDefaultButtonKey); |
| 338 | if (!default_button.empty()) { |
| 339 | return MapDefaultButtonStringToSetting(default_button); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return NOT_SPECIFIED; |
| 344 | } |
| 345 | |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 346 | ExternalInstallError::ExternalInstallError( |
| 347 | content::BrowserContext* browser_context, |
| 348 | const std::string& extension_id, |
| 349 | AlertType alert_type, |
| 350 | ExternalInstallManager* manager) |
| 351 | : browser_context_(browser_context), |
| 352 | extension_id_(extension_id), |
| 353 | alert_type_(alert_type), |
| 354 | manager_(manager), |
| 355 | error_service_(GlobalErrorServiceFactory::GetForProfile( |
| 356 | Profile::FromBrowserContext(browser_context_))), |
| 357 | weak_factory_(this) { |
rdevlin.cronin | 2e25269 | 2015-12-15 21:47:02 | [diff] [blame] | 358 | prompt_.reset(new ExtensionInstallPrompt::Prompt( |
| 359 | ExtensionInstallPrompt::EXTERNAL_INSTALL_PROMPT)); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 360 | |
| 361 | webstore_data_fetcher_.reset(new WebstoreDataFetcher( |
jam | b84299e | 2016-04-12 16:58:59 | [diff] [blame] | 362 | this, |
| 363 | content::BrowserContext::GetDefaultStoragePartition(browser_context_)-> |
| 364 | GetURLRequestContext(), |
| 365 | GURL(), extension_id_)); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 366 | webstore_data_fetcher_->Start(); |
| 367 | } |
| 368 | |
| 369 | ExternalInstallError::~ExternalInstallError() { |
rdevlin.cronin | a1c3f1a | 2017-05-18 17:45:46 | [diff] [blame] | 370 | #if DCHECK_IS_ON() |
| 371 | // Errors should only be removed while the profile is valid, since removing |
| 372 | // the error can trigger other subsystems listening for changes. |
| 373 | BrowserContextDependencyManager::GetInstance() |
| 374 | ->AssertBrowserContextWasntDestroyed(browser_context_); |
| 375 | #endif |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 376 | if (global_error_.get()) |
avi | 2451b25 | 2016-12-13 16:55:17 | [diff] [blame] | 377 | error_service_->RemoveUnownedGlobalError(global_error_.get()); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 378 | } |
| 379 | |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 380 | void ExternalInstallError::OnInstallPromptDone( |
| 381 | ExtensionInstallPrompt::Result result) { |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 382 | const Extension* extension = GetExtension(); |
rdevlin.cronin | b2daf2e4 | 2016-01-14 20:00:54 | [diff] [blame] | 383 | |
| 384 | // If the error isn't removed and deleted as part of handling the user's |
| 385 | // response (which can happen, e.g., if an uninstall fails), be sure to remove |
| 386 | // the error directly in order to ensure it's not called twice. |
fdoray | 283082bd | 2016-06-02 20:18:46 | [diff] [blame] | 387 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
tzik | 8d880ee | 2017-04-20 19:46:24 | [diff] [blame] | 388 | FROM_HERE, base::BindOnce(&ExternalInstallError::RemoveError, |
| 389 | weak_factory_.GetWeakPtr())); |
rdevlin.cronin | b2daf2e4 | 2016-01-14 20:00:54 | [diff] [blame] | 390 | |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 391 | switch (result) { |
| 392 | case ExtensionInstallPrompt::Result::ACCEPTED: |
| 393 | if (extension) { |
| 394 | ExtensionSystem::Get(browser_context_) |
| 395 | ->extension_service() |
| 396 | ->GrantPermissionsAndEnableExtension(extension); |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 397 | } |
| 398 | break; |
| 399 | case ExtensionInstallPrompt::Result::USER_CANCELED: |
| 400 | if (extension) { |
rdevlin.cronin | b2daf2e4 | 2016-01-14 20:00:54 | [diff] [blame] | 401 | bool uninstallation_result = ExtensionSystem::Get(browser_context_) |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 402 | ->extension_service() |
| 403 | ->UninstallExtension(extension_id_, |
| 404 | extensions::UNINSTALL_REASON_INSTALL_CANCELED, |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 405 | nullptr); // Ignore error. |
rdevlin.cronin | b2daf2e4 | 2016-01-14 20:00:54 | [diff] [blame] | 406 | UMA_HISTOGRAM_BOOLEAN("Extensions.ExternalWarningUninstallationResult", |
| 407 | uninstallation_result); |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 408 | } |
| 409 | break; |
| 410 | case ExtensionInstallPrompt::Result::ABORTED: |
lazyboy | 1899eec4 | 2016-03-08 19:00:50 | [diff] [blame] | 411 | manager_->DidChangeInstallAlertVisibility(this, false); |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 412 | break; |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 413 | } |
rdevlin.cronin | b2daf2e4 | 2016-01-14 20:00:54 | [diff] [blame] | 414 | // NOTE: We may be deleted here! |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 415 | } |
| 416 | |
lazyboy | 1899eec4 | 2016-03-08 19:00:50 | [diff] [blame] | 417 | void ExternalInstallError::DidOpenBubbleView() { |
| 418 | manager_->DidChangeInstallAlertVisibility(this, true); |
| 419 | } |
| 420 | |
| 421 | void ExternalInstallError::DidCloseBubbleView() { |
| 422 | manager_->DidChangeInstallAlertVisibility(this, false); |
| 423 | } |
| 424 | |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 425 | void ExternalInstallError::ShowDialog(Browser* browser) { |
| 426 | DCHECK(install_ui_.get()); |
| 427 | DCHECK(prompt_.get()); |
| 428 | DCHECK(browser); |
| 429 | content::WebContents* web_contents = NULL; |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 430 | web_contents = browser->tab_strip_model()->GetActiveWebContents(); |
pkotwicz | 2f18178 | 2014-10-29 17:33:45 | [diff] [blame] | 431 | install_ui_show_params_.reset( |
| 432 | new ExtensionInstallPromptShowParams(web_contents)); |
lazyboy | 1899eec4 | 2016-03-08 19:00:50 | [diff] [blame] | 433 | manager_->DidChangeInstallAlertVisibility(this, true); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 434 | ExtensionInstallPrompt::GetDefaultShowDialogCallback().Run( |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 435 | install_ui_show_params_.get(), |
| 436 | base::Bind(&ExternalInstallError::OnInstallPromptDone, |
| 437 | weak_factory_.GetWeakPtr()), |
| 438 | std::move(prompt_)); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | const Extension* ExternalInstallError::GetExtension() const { |
| 442 | return ExtensionRegistry::Get(browser_context_) |
| 443 | ->GetExtensionById(extension_id_, ExtensionRegistry::EVERYTHING); |
| 444 | } |
| 445 | |
| 446 | void ExternalInstallError::OnWebstoreRequestFailure() { |
| 447 | OnFetchComplete(); |
| 448 | } |
| 449 | |
| 450 | void ExternalInstallError::OnWebstoreResponseParseSuccess( |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 451 | std::unique_ptr<base::DictionaryValue> webstore_data) { |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 452 | std::string localized_user_count; |
[email protected] | 96aebe2 | 2014-07-16 04:07:51 | [diff] [blame] | 453 | double average_rating = 0; |
| 454 | int rating_count = 0; |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 455 | if (!webstore_data->GetString(kUsersKey, &localized_user_count) || |
| 456 | !webstore_data->GetDouble(kAverageRatingKey, &average_rating) || |
| 457 | !webstore_data->GetInteger(kRatingCountKey, &rating_count)) { |
| 458 | // If we don't get a valid webstore response, short circuit, and continue |
| 459 | // to show a prompt without webstore data. |
| 460 | OnFetchComplete(); |
| 461 | return; |
| 462 | } |
| 463 | |
proberge | bc529d6 | 2018-04-24 14:48:26 | [diff] [blame^] | 464 | default_dialog_button_setting_ = GetDefaultDialogButton(*webstore_data.get()); |
| 465 | |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 466 | bool show_user_count = true; |
| 467 | webstore_data->GetBoolean(kShowUserCountKey, &show_user_count); |
| 468 | |
| 469 | prompt_->SetWebstoreData( |
| 470 | localized_user_count, show_user_count, average_rating, rating_count); |
| 471 | OnFetchComplete(); |
| 472 | } |
| 473 | |
| 474 | void ExternalInstallError::OnWebstoreResponseParseFailure( |
| 475 | const std::string& error) { |
| 476 | OnFetchComplete(); |
| 477 | } |
| 478 | |
| 479 | void ExternalInstallError::OnFetchComplete() { |
| 480 | // Create a new ExtensionInstallPrompt. We pass in NULL for the UI |
| 481 | // components because we display at a later point, and don't want |
| 482 | // to pass ones which may be invalidated. |
| 483 | install_ui_.reset( |
| 484 | new ExtensionInstallPrompt(Profile::FromBrowserContext(browser_context_), |
pkotwicz | 2175c62 | 2014-10-22 19:56:28 | [diff] [blame] | 485 | NULL)); // NULL native window. |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 486 | |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 487 | install_ui_->ShowDialog(base::Bind(&ExternalInstallError::OnInstallPromptDone, |
| 488 | weak_factory_.GetWeakPtr()), |
| 489 | GetExtension(), |
rdevlin.cronin | f84cab7 | 2015-12-12 03:45:23 | [diff] [blame] | 490 | nullptr, // Force a fetch of the icon. |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 491 | std::move(prompt_), |
rdevlin.cronin | f84cab7 | 2015-12-12 03:45:23 | [diff] [blame] | 492 | base::Bind(&ExternalInstallError::OnDialogReady, |
| 493 | weak_factory_.GetWeakPtr())); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void ExternalInstallError::OnDialogReady( |
pkotwicz | 2f18178 | 2014-10-29 17:33:45 | [diff] [blame] | 497 | ExtensionInstallPromptShowParams* show_params, |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 498 | const ExtensionInstallPrompt::DoneCallback& callback, |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 499 | std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt) { |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 500 | prompt_ = std::move(prompt); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 501 | |
| 502 | if (alert_type_ == BUBBLE_ALERT) { |
dcheng | c704794 | 2014-08-26 05:05:31 | [diff] [blame] | 503 | global_error_.reset(new ExternalInstallBubbleAlert(this, prompt_.get())); |
avi | 2451b25 | 2016-12-13 16:55:17 | [diff] [blame] | 504 | error_service_->AddUnownedGlobalError(global_error_.get()); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 505 | |
lazyboy | 1899eec4 | 2016-03-08 19:00:50 | [diff] [blame] | 506 | if (!manager_->has_currently_visible_install_alert()) { |
| 507 | // |browser| is nullptr during unit tests, so call |
| 508 | // DidChangeInstallAlertVisibility() regardless because we depend on this |
| 509 | // in unit tests. |
| 510 | manager_->DidChangeInstallAlertVisibility(this, true); |
| 511 | Browser* browser = chrome::FindTabbedBrowser( |
| 512 | Profile::FromBrowserContext(browser_context_), true); |
| 513 | if (browser) |
| 514 | global_error_->ShowBubbleView(browser); |
| 515 | } |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 516 | } else { |
| 517 | DCHECK(alert_type_ == MENU_ALERT); |
| 518 | global_error_.reset(new ExternalInstallMenuAlert(this)); |
avi | 2451b25 | 2016-12-13 16:55:17 | [diff] [blame] | 519 | error_service_->AddUnownedGlobalError(global_error_.get()); |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
rdevlin.cronin | b2daf2e4 | 2016-01-14 20:00:54 | [diff] [blame] | 523 | void ExternalInstallError::RemoveError() { |
| 524 | manager_->RemoveExternalInstallError(extension_id_); |
| 525 | } |
| 526 | |
[email protected] | 2894a51 | 2014-06-26 19:03:56 | [diff] [blame] | 527 | } // namespace extensions |