Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / app_list / search / webstore / webstore_result.cc
index cc5e8de..9437036 100644 (file)
@@ -7,7 +7,6 @@
 #include <vector>
 
 #include "base/bind.h"
-#include "base/command_line.h"
 #include "base/memory/ref_counted.h"
 #include "base/strings/utf_string_conversions.h"
 #include "chrome/browser/apps/ephemeral_app_launcher.h"
@@ -20,7 +19,6 @@
 #include "chrome/browser/ui/app_list/search/webstore/webstore_installer.h"
 #include "chrome/browser/ui/browser_navigator.h"
 #include "chrome/browser/ui/extensions/application_launch.h"
-#include "chrome/common/chrome_switches.h"
 #include "extensions/browser/extension_registry.h"
 #include "extensions/browser/extension_system.h"
 #include "extensions/browser/extension_util.h"
 
 namespace {
 
-const int kIconSize = 32;
 const int kLaunchEphemeralAppAction = 1;
 
 // BadgedImageSource adds a webstore badge to a webstore app icon.
 class BadgedIconSource : public gfx::CanvasImageSource {
  public:
-  explicit BadgedIconSource(const gfx::ImageSkia& icon)
-      : CanvasImageSource(gfx::Size(kIconSize, kIconSize), false),
-        icon_(icon) {
-  }
+  BadgedIconSource(const gfx::ImageSkia& icon, const gfx::Size& icon_size)
+      : CanvasImageSource(icon_size, false), icon_(icon) {}
 
   virtual void Draw(gfx::Canvas* canvas) OVERRIDE {
     canvas->DrawImageInt(icon_, 0, 0);
@@ -69,34 +64,38 @@ WebstoreResult::WebstoreResult(Profile* profile,
                                const std::string& app_id,
                                const std::string& localized_name,
                                const GURL& icon_url,
+                               bool is_paid,
+                               extensions::Manifest::Type item_type,
                                AppListControllerDelegate* controller)
     : profile_(profile),
       app_id_(app_id),
       localized_name_(localized_name),
       icon_url_(icon_url),
-      weak_factory_(this),
+      is_paid_(is_paid),
+      item_type_(item_type),
       controller_(controller),
       install_tracker_(NULL),
-      extension_registry_(NULL) {
+      extension_registry_(NULL),
+      weak_factory_(this) {
   set_id(extensions::Extension::GetBaseURLFromExtensionId(app_id_).spec());
   set_relevance(0.0);  // What is the right value to use?
 
   set_title(base::UTF8ToUTF16(localized_name_));
   SetDefaultDetails();
 
+  InitAndStartObserving();
   UpdateActions();
 
+  int icon_dimension = GetPreferredIconDimension();
   icon_ = gfx::ImageSkia(
-      new UrlIconSource(base::Bind(&WebstoreResult::OnIconLoaded,
-                                   weak_factory_.GetWeakPtr()),
-                        profile_->GetRequestContext(),
-                        icon_url_,
-                        kIconSize,
-                        IDR_WEBSTORE_ICON_32),
-      gfx::Size(kIconSize, kIconSize));
+      new UrlIconSource(
+          base::Bind(&WebstoreResult::OnIconLoaded, weak_factory_.GetWeakPtr()),
+          profile_->GetRequestContext(),
+          icon_url_,
+          icon_dimension,
+          IDR_WEBSTORE_ICON_32),
+      gfx::Size(icon_dimension, icon_dimension));
   SetIcon(icon_);
-
-  StartObserving();
 }
 
 WebstoreResult::~WebstoreResult() {
@@ -118,12 +117,42 @@ void WebstoreResult::Open(int event_flags) {
 }
 
 void WebstoreResult::InvokeAction(int action_index, int event_flags) {
+  if (is_paid_) {
+    // Paid apps cannot be installed directly from the launcher. Instead, open
+    // the webstore page for the app.
+    Open(event_flags);
+    return;
+  }
+
   StartInstall(action_index == kLaunchEphemeralAppAction);
 }
 
 scoped_ptr<ChromeSearchResult> WebstoreResult::Duplicate() {
-  return scoped_ptr<ChromeSearchResult>(new WebstoreResult(
-      profile_, app_id_, localized_name_, icon_url_, controller_)).Pass();
+  return scoped_ptr<ChromeSearchResult>(new WebstoreResult(profile_,
+                                                           app_id_,
+                                                           localized_name_,
+                                                           icon_url_,
+                                                           is_paid_,
+                                                           item_type_,
+                                                           controller_)).Pass();
+}
+
+void WebstoreResult::InitAndStartObserving() {
+  DCHECK(!install_tracker_ && !extension_registry_);
+
+  install_tracker_ =
+      extensions::InstallTrackerFactory::GetForBrowserContext(profile_);
+  extension_registry_ = extensions::ExtensionRegistry::Get(profile_);
+
+  const extensions::ActiveInstallData* install_data =
+      install_tracker_->GetActiveInstall(app_id_);
+  if (install_data) {
+    SetPercentDownloaded(install_data->percent_downloaded);
+    SetIsInstalling(true);
+  }
+
+  install_tracker_->AddObserver(this);
+  extension_registry_->AddObserver(this);
 }
 
 void WebstoreResult::UpdateActions() {
@@ -134,15 +163,18 @@ void WebstoreResult::UpdateActions() {
       extensions::util::IsExtensionInstalledPermanently(app_id_, profile_);
 
   if (!is_otr && !is_installed && !is_installing()) {
-    if (CommandLine::ForCurrentProcess()->HasSwitch(
-            switches::kEnableEphemeralApps)) {
+    if (EphemeralAppLauncher::IsFeatureEnabled()) {
       actions.push_back(Action(
           l10n_util::GetStringUTF16(IDS_WEBSTORE_RESULT_INSTALL),
           l10n_util::GetStringUTF16(
               IDS_EXTENSION_INLINE_INSTALL_PROMPT_TITLE)));
-      actions.push_back(Action(
-          l10n_util::GetStringUTF16(IDS_WEBSTORE_RESULT_LAUNCH),
-          l10n_util::GetStringUTF16(IDS_WEBSTORE_RESULT_LAUNCH_APP_TOOLTIP)));
+      if ((item_type_ == extensions::Manifest::TYPE_PLATFORM_APP ||
+           item_type_ == extensions::Manifest::TYPE_HOSTED_APP) &&
+          !is_paid_) {
+        actions.push_back(Action(
+            l10n_util::GetStringUTF16(IDS_WEBSTORE_RESULT_LAUNCH),
+            l10n_util::GetStringUTF16(IDS_WEBSTORE_RESULT_LAUNCH_APP_TOOLTIP)));
+      }
     } else {
       actions.push_back(Action(
           l10n_util::GetStringUTF16(IDS_EXTENSION_INLINE_INSTALL_PROMPT_TITLE),
@@ -169,9 +201,9 @@ void WebstoreResult::OnIconLoaded() {
   const std::vector<gfx::ImageSkiaRep>& image_reps = icon_.image_reps();
   for (size_t i = 0; i < image_reps.size(); ++i)
     icon_.RemoveRepresentation(image_reps[i].scale());
-
-  icon_ = gfx::ImageSkia(new BadgedIconSource(icon_),
-                         gfx::Size(kIconSize, kIconSize));
+  int icon_dimension = GetPreferredIconDimension();
+  gfx::Size icon_size(icon_dimension, icon_dimension);
+  icon_ = gfx::ImageSkia(new BadgedIconSource(icon_, icon_size), icon_size);
 
   SetIcon(icon_);
 }
@@ -186,7 +218,7 @@ void WebstoreResult::StartInstall(bool launch_ephemeral_app) {
             app_id_,
             profile_,
             controller_->GetAppListWindow(),
-            base::Bind(&WebstoreResult::InstallCallback,
+            base::Bind(&WebstoreResult::LaunchCallback,
                        weak_factory_.GetWeakPtr()));
     installer->Start();
     return;
@@ -202,7 +234,10 @@ void WebstoreResult::StartInstall(bool launch_ephemeral_app) {
   installer->BeginInstall();
 }
 
-void WebstoreResult::InstallCallback(bool success, const std::string& error) {
+void WebstoreResult::InstallCallback(
+    bool success,
+    const std::string& error,
+    extensions::webstore_install::Result result) {
   if (!success) {
     LOG(ERROR) << "Failed to install app, error=" << error;
     SetIsInstalling(false);
@@ -213,14 +248,12 @@ void WebstoreResult::InstallCallback(bool success, const std::string& error) {
   SetPercentDownloaded(100);
 }
 
-void WebstoreResult::StartObserving() {
-  DCHECK(!install_tracker_ && !extension_registry_);
-
-  install_tracker_ = extensions::InstallTrackerFactory::GetForProfile(profile_);
-  install_tracker_->AddObserver(this);
+void WebstoreResult::LaunchCallback(extensions::webstore_install::Result result,
+                                    const std::string& error) {
+  if (result != extensions::webstore_install::SUCCESS)
+    LOG(ERROR) << "Failed to launch app, error=" << error;
 
-  extension_registry_ = extensions::ExtensionRegistry::Get(profile_);
-  extension_registry_->AddObserver(this);
+  SetIsInstalling(false);
 }
 
 void WebstoreResult::StopObservingInstall() {
@@ -243,18 +276,20 @@ void WebstoreResult::OnDownloadProgress(const std::string& extension_id,
   SetPercentDownloaded(percent_downloaded);
 }
 
-void WebstoreResult::OnExtensionWillBeInstalled(
+void WebstoreResult::OnExtensionInstalled(
     content::BrowserContext* browser_context,
     const extensions::Extension* extension,
-    bool is_update,
-    bool from_ephemeral,
-    const std::string& old_name) {
+    bool is_update) {
   if (extension->id() != app_id_)
     return;
 
   SetIsInstalling(false);
   UpdateActions();
-  NotifyItemInstalled();
+
+  if (extensions::util::IsExtensionInstalledPermanently(extension->id(),
+                                                        profile_)) {
+    NotifyItemInstalled();
+  }
 }
 
 void WebstoreResult::OnShutdown() {