rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [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/webstore_reinstaller.h" |
| 6 | |
Devlin Cronin | 1cf1016 | 2019-01-04 01:07:51 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [diff] [blame] | 9 | #include "base/memory/ref_counted.h" |
| 10 | #include "chrome/browser/extensions/extension_install_prompt.h" |
| 11 | #include "chrome/browser/extensions/extension_service.h" |
| 12 | #include "chrome/browser/profiles/profile.h" |
| 13 | #include "content/public/browser/web_contents.h" |
| 14 | #include "extensions/browser/extension_system.h" |
| 15 | |
| 16 | namespace extensions { |
| 17 | |
| 18 | namespace { |
| 19 | const char kCouldNotUninstallExtension[] = "Failed to uninstall the extension."; |
| 20 | const char kTabClosed[] = "Tab was closed."; |
| 21 | } |
| 22 | |
| 23 | WebstoreReinstaller::WebstoreReinstaller( |
| 24 | content::WebContents* web_contents, |
| 25 | const std::string& extension_id, |
Devlin Cronin | 1cf1016 | 2019-01-04 01:07:51 | [diff] [blame] | 26 | WebstoreStandaloneInstaller::Callback callback) |
rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [diff] [blame] | 27 | : WebstoreStandaloneInstaller( |
| 28 | extension_id, |
| 29 | Profile::FromBrowserContext(web_contents->GetBrowserContext()), |
Devlin Cronin | 1cf1016 | 2019-01-04 01:07:51 | [diff] [blame] | 30 | std::move(callback)), |
rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [diff] [blame] | 31 | content::WebContentsObserver(web_contents) { |
Minh X. Nguyen | 4547901 | 2017-08-18 21:35:36 | [diff] [blame] | 32 | DCHECK( |
| 33 | ExtensionPrefs::Get(web_contents->GetBrowserContext()) |
| 34 | ->HasDisableReason(extension_id, disable_reason::DISABLE_CORRUPTED)); |
rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | WebstoreReinstaller::~WebstoreReinstaller() { |
| 38 | } |
| 39 | |
| 40 | void WebstoreReinstaller::BeginReinstall() { |
| 41 | WebstoreStandaloneInstaller::BeginInstall(); |
| 42 | } |
| 43 | |
| 44 | bool WebstoreReinstaller::CheckRequestorAlive() const { |
| 45 | return web_contents() != NULL; |
| 46 | } |
| 47 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 48 | std::unique_ptr<ExtensionInstallPrompt::Prompt> |
rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [diff] [blame] | 49 | WebstoreReinstaller::CreateInstallPrompt() const { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 50 | std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt( |
rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [diff] [blame] | 51 | new ExtensionInstallPrompt::Prompt( |
| 52 | ExtensionInstallPrompt::REPAIR_PROMPT)); |
| 53 | prompt->SetWebstoreData(localized_user_count(), |
| 54 | show_user_count(), |
| 55 | average_rating(), |
| 56 | rating_count()); |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 57 | return prompt; |
rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | bool WebstoreReinstaller::ShouldShowPostInstallUI() const { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | bool WebstoreReinstaller::ShouldShowAppInstalledBubble() const { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | content::WebContents* WebstoreReinstaller::GetWebContents() const { |
| 69 | return web_contents(); |
| 70 | } |
| 71 | |
rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [diff] [blame] | 72 | void WebstoreReinstaller::WebContentsDestroyed() { |
| 73 | // Run the callback now, because AbortInstall() doesn't do it. |
| 74 | RunCallback(false, kTabClosed, webstore_install::ABORTED); |
| 75 | AbortInstall(); |
| 76 | } |
| 77 | |
rdevlin.cronin | 4159305 | 2016-01-08 01:40:12 | [diff] [blame] | 78 | void WebstoreReinstaller::OnInstallPromptDone( |
| 79 | ExtensionInstallPrompt::Result result) { |
| 80 | if (result != ExtensionInstallPrompt::Result::ACCEPTED) { |
| 81 | WebstoreStandaloneInstaller::OnInstallPromptDone(result); |
| 82 | return; |
| 83 | } |
| 84 | |
rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [diff] [blame] | 85 | if (!ExtensionSystem::Get(profile())->extension_service()->UninstallExtension( |
| 86 | id(), |
| 87 | UNINSTALL_REASON_REINSTALL, |
rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [diff] [blame] | 88 | NULL)) { |
| 89 | // Run the callback now, because AbortInstall() doesn't do it. |
| 90 | RunCallback( |
| 91 | false, kCouldNotUninstallExtension, webstore_install::OTHER_ERROR); |
| 92 | AbortInstall(); |
Devlin Cronin | 218df7f | 2017-11-21 21:41:31 | [diff] [blame] | 93 | return; |
rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [diff] [blame] | 94 | } |
Devlin Cronin | 218df7f | 2017-11-21 21:41:31 | [diff] [blame] | 95 | WebstoreStandaloneInstaller::OnInstallPromptDone(result); |
rdevlin.cronin | 5f6b69d | 2014-09-20 01:23:35 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | } // namespace extensions |