Home | History | Annotate | Download | only in extensions
      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) {
     30 }
     31 
     32 WebstoreReinstaller::~WebstoreReinstaller() {
     33 }
     34 
     35 void WebstoreReinstaller::BeginReinstall() {
     36   WebstoreStandaloneInstaller::BeginInstall();
     37 }
     38 
     39 bool WebstoreReinstaller::CheckRequestorAlive() const {
     40   return web_contents() != NULL;
     41 }
     42 
     43 const GURL& WebstoreReinstaller::GetRequestorURL() const {
     44   return GURL::EmptyGURL();
     45 }
     46 
     47 scoped_refptr<ExtensionInstallPrompt::Prompt>
     48 WebstoreReinstaller::CreateInstallPrompt() const {
     49   scoped_refptr<ExtensionInstallPrompt::Prompt> prompt(
     50       new ExtensionInstallPrompt::Prompt(
     51           ExtensionInstallPrompt::REPAIR_PROMPT));
     52   prompt->SetWebstoreData(localized_user_count(),
     53                           show_user_count(),
     54                           average_rating(),
     55                           rating_count());
     56   return prompt;
     57 }
     58 
     59 bool WebstoreReinstaller::ShouldShowPostInstallUI() const {
     60   return false;
     61 }
     62 
     63 bool WebstoreReinstaller::ShouldShowAppInstalledBubble() const {
     64   return false;
     65 }
     66 
     67 content::WebContents* WebstoreReinstaller::GetWebContents() const {
     68   return web_contents();
     69 }
     70 
     71 bool WebstoreReinstaller::CheckInlineInstallPermitted(
     72     const base::DictionaryValue& webstore_data,
     73     std::string* error) const {
     74   return true;
     75 }
     76 
     77 bool WebstoreReinstaller::CheckRequestorPermitted(
     78     const base::DictionaryValue& webstore_data,
     79     std::string* error) const {
     80   return true;
     81 }
     82 
     83 void WebstoreReinstaller::WebContentsDestroyed() {
     84   // Run the callback now, because AbortInstall() doesn't do it.
     85   RunCallback(false, kTabClosed, webstore_install::ABORTED);
     86   AbortInstall();
     87 }
     88 
     89 void WebstoreReinstaller::InstallUIProceed() {
     90   if (!ExtensionSystem::Get(profile())->extension_service()->UninstallExtension(
     91           id(),
     92           UNINSTALL_REASON_REINSTALL,
     93           base::Bind(&WebstoreReinstaller::OnDeletionDone, this),
     94           NULL)) {
     95     // Run the callback now, because AbortInstall() doesn't do it.
     96     RunCallback(
     97         false, kCouldNotUninstallExtension, webstore_install::OTHER_ERROR);
     98     AbortInstall();
     99   }
    100 }
    101 
    102 void WebstoreReinstaller::OnDeletionDone() {
    103   WebstoreStandaloneInstaller::InstallUIProceed();
    104 }
    105 
    106 }  // namespace extensions
    107