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