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 <utility>
8
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
WebstoreReinstaller(content::WebContents * web_contents,const std::string & extension_id,WebstoreStandaloneInstaller::Callback callback)23 WebstoreReinstaller::WebstoreReinstaller(
24 content::WebContents* web_contents,
25 const std::string& extension_id,
26 WebstoreStandaloneInstaller::Callback callback)
27 : WebstoreStandaloneInstaller(
28 extension_id,
29 Profile::FromBrowserContext(web_contents->GetBrowserContext()),
30 std::move(callback)),
31 content::WebContentsObserver(web_contents) {
32 DCHECK(
33 ExtensionPrefs::Get(web_contents->GetBrowserContext())
34 ->HasDisableReason(extension_id, disable_reason::DISABLE_CORRUPTED));
35 }
36
~WebstoreReinstaller()37 WebstoreReinstaller::~WebstoreReinstaller() {
38 }
39
BeginReinstall()40 void WebstoreReinstaller::BeginReinstall() {
41 WebstoreStandaloneInstaller::BeginInstall();
42 }
43
CheckRequestorAlive() const44 bool WebstoreReinstaller::CheckRequestorAlive() const {
45 return web_contents() != NULL;
46 }
47
48 std::unique_ptr<ExtensionInstallPrompt::Prompt>
CreateInstallPrompt() const49 WebstoreReinstaller::CreateInstallPrompt() const {
50 std::unique_ptr<ExtensionInstallPrompt::Prompt> prompt(
51 new ExtensionInstallPrompt::Prompt(
52 ExtensionInstallPrompt::REPAIR_PROMPT));
53 prompt->SetWebstoreData(localized_user_count(),
54 show_user_count(),
55 average_rating(),
56 rating_count());
57 return prompt;
58 }
59
ShouldShowPostInstallUI() const60 bool WebstoreReinstaller::ShouldShowPostInstallUI() const {
61 return false;
62 }
63
ShouldShowAppInstalledBubble() const64 bool WebstoreReinstaller::ShouldShowAppInstalledBubble() const {
65 return false;
66 }
67
GetWebContents() const68 content::WebContents* WebstoreReinstaller::GetWebContents() const {
69 return web_contents();
70 }
71
WebContentsDestroyed()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
OnInstallPromptDone(ExtensionInstallPrompt::Result result)78 void WebstoreReinstaller::OnInstallPromptDone(
79 ExtensionInstallPrompt::Result result) {
80 if (result != ExtensionInstallPrompt::Result::ACCEPTED) {
81 WebstoreStandaloneInstaller::OnInstallPromptDone(result);
82 return;
83 }
84
85 if (!ExtensionSystem::Get(profile())->extension_service()->UninstallExtension(
86 id(),
87 UNINSTALL_REASON_REINSTALL,
88 NULL)) {
89 // Run the callback now, because AbortInstall() doesn't do it.
90 RunCallback(
91 false, kCouldNotUninstallExtension, webstore_install::OTHER_ERROR);
92 AbortInstall();
93 return;
94 }
95 WebstoreStandaloneInstaller::OnInstallPromptDone(result);
96 }
97
98 } // namespace extensions
99