Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / tab_helper.cc
index a1ce5ea..26da0e1 100644 (file)
@@ -4,6 +4,7 @@
 
 #include "chrome/browser/extensions/tab_helper.h"
 
+#include "base/command_line.h"
 #include "base/logging.h"
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
@@ -16,7 +17,6 @@
 #include "chrome/browser/extensions/extension_action.h"
 #include "chrome/browser/extensions/extension_action_manager.h"
 #include "chrome/browser/extensions/extension_service.h"
-#include "chrome/browser/extensions/extension_system.h"
 #include "chrome/browser/extensions/extension_tab_util.h"
 #include "chrome/browser/extensions/favicon_downloader.h"
 #include "chrome/browser/extensions/image_loader.h"
 #include "chrome/browser/sessions/session_id.h"
 #include "chrome/browser/sessions/session_tab_helper.h"
 #include "chrome/browser/shell_integration.h"
-#include "chrome/browser/ui/app_list/app_list_service.h"
-#include "chrome/browser/ui/app_list/app_list_util.h"
 #include "chrome/browser/ui/browser_commands.h"
 #include "chrome/browser/ui/browser_dialogs.h"
 #include "chrome/browser/ui/browser_finder.h"
+#include "chrome/browser/ui/browser_window.h"
 #include "chrome/browser/ui/host_desktop.h"
 #include "chrome/browser/ui/web_applications/web_app_ui.h"
 #include "chrome/browser/web_applications/web_app.h"
+#include "chrome/common/chrome_switches.h"
 #include "chrome/common/extensions/extension_constants.h"
 #include "chrome/common/extensions/extension_icon_set.h"
 #include "chrome/common/extensions/extension_messages.h"
@@ -57,6 +57,8 @@
 #include "content/public/browser/web_contents_view.h"
 #include "content/public/common/frame_navigate_params.h"
 #include "extensions/browser/extension_error.h"
+#include "extensions/browser/extension_registry.h"
+#include "extensions/browser/extension_system.h"
 #include "extensions/common/extension.h"
 #include "extensions/common/extension_resource.h"
 #include "extensions/common/extension_urls.h"
 #include "ui/gfx/color_analysis.h"
 #include "ui/gfx/image/image.h"
 
+#if defined(OS_CHROMEOS)
+#include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
+#endif
+
 using content::NavigationController;
 using content::NavigationEntry;
 using content::RenderViewHost;
@@ -136,14 +142,16 @@ void TabHelper::GenerateContainerIcon(std::map<int, SkBitmap>* bitmaps,
   --it;
   // This is the biggest icon smaller than |output_size|.
   const SkBitmap& base_icon = it->second;
-  scoped_ptr<SkCanvas> canvas(
-      skia::CreateBitmapCanvas(output_size, output_size, false));
-  DCHECK(canvas);
 
   const size_t kBorderRadius = 5;
   const size_t kColorStripHeight = 3;
-  const size_t kColorStripRadius = 2;
   const SkColor kBorderColor = 0xFFD5D5D5;
+  const SkColor kBackgroundColor = 0xFFFFFFFF;
+
+  // Create a separate canvas for the color strip.
+  scoped_ptr<SkCanvas> color_strip_canvas(
+      skia::CreateBitmapCanvas(output_size, output_size, false));
+  DCHECK(color_strip_canvas);
 
   // Draw a rounded rect of the |base_icon|'s dominant color.
   SkPaint color_strip_paint;
@@ -153,18 +161,35 @@ void TabHelper::GenerateContainerIcon(std::map<int, SkBitmap>* bitmaps,
       color_utils::CalculateKMeanColorOfPNG(
           gfx::Image::CreateFrom1xBitmap(base_icon).As1xPNGBytes(),
           100, 665, &sampler));
-  canvas->drawRoundRect(
-      SkRect::MakeXYWH(1, 0, output_size - 2, output_size - 1),
-      kColorStripRadius, kColorStripRadius, color_strip_paint);
+  color_strip_canvas->drawRoundRect(
+      SkRect::MakeWH(output_size, output_size),
+      kBorderRadius, kBorderRadius, color_strip_paint);
 
   // Erase the top of the rounded rect to leave a color strip.
   SkPaint clear_paint;
   clear_paint.setColor(SK_ColorTRANSPARENT);
   clear_paint.setXfermodeMode(SkXfermode::kSrc_Mode);
-  canvas->drawRect(
-      SkRect::MakeWH(output_size, output_size - kColorStripHeight - 1),
+  color_strip_canvas->drawRect(
+      SkRect::MakeWH(output_size, output_size - kColorStripHeight),
       clear_paint);
 
+  // Draw each element to an output canvas.
+  scoped_ptr<SkCanvas> canvas(
+      skia::CreateBitmapCanvas(output_size, output_size, false));
+  DCHECK(canvas);
+
+  // Draw the background.
+  SkPaint background_paint;
+  background_paint.setColor(kBackgroundColor);
+  background_paint.setFlags(SkPaint::kAntiAlias_Flag);
+  canvas->drawRoundRect(
+      SkRect::MakeWH(output_size, output_size),
+      kBorderRadius, kBorderRadius, background_paint);
+
+  // Draw the color strip.
+  canvas->drawBitmap(color_strip_canvas->getDevice()->accessBitmap(false),
+                     0, 0);
+
   // Draw the border.
   SkPaint border_paint;
   border_paint.setColor(kBorderColor);
@@ -174,7 +199,7 @@ void TabHelper::GenerateContainerIcon(std::map<int, SkBitmap>* bitmaps,
       SkRect::MakeWH(output_size, output_size),
       kBorderRadius, kBorderRadius, border_paint);
 
-  // Draw the centered base icon.
+  // Draw the centered base icon to the output canvas.
   canvas->drawBitmap(base_icon,
                      (output_size - base_icon.width()) / 2,
                      (output_size - base_icon.height()) / 2);
@@ -273,6 +298,9 @@ bool TabHelper::CanCreateApplicationShortcuts() const {
 
 void TabHelper::SetExtensionApp(const Extension* extension) {
   DCHECK(!extension || AppLaunchInfo::GetFullLaunchURL(extension).is_valid());
+  if (extension_app_ == extension)
+    return;
+
   extension_app_ = extension;
 
   UpdateExtensionAppIcon(extension_app_);
@@ -323,7 +351,20 @@ void TabHelper::DidNavigateMainFrame(
   if (!service)
     return;
 
-  UpdateExtensionAppIcon(service->GetInstalledExtensionByUrl(params.url));
+  if (CommandLine::ForCurrentProcess()->HasSwitch(
+          switches::kEnableStreamlinedHostedApps)) {
+#if !defined(OS_ANDROID)
+    Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
+    if (browser && browser->is_app()) {
+      SetExtensionApp(service->GetInstalledExtension(
+          web_app::GetExtensionIdFromApplicationName(browser->app_name())));
+    } else {
+      UpdateExtensionAppIcon(service->GetInstalledExtensionByUrl(params.url));
+    }
+#endif
+  } else {
+    UpdateExtensionAppIcon(service->GetInstalledExtensionByUrl(params.url));
+  }
 
   if (details.is_in_page)
     return;
@@ -394,16 +435,13 @@ void TabHelper::FinishCreateHostedApp(
     return;
   }
 
-  WebApplicationInfo install_info(web_app_info_);
-  if (install_info.app_url.is_empty())
-    install_info.app_url = web_contents()->GetURL();
+  if (web_app_info_.app_url.is_empty())
+    web_app_info_.app_url = web_contents()->GetURL();
 
-  if (install_info.title.empty())
-    install_info.title = web_contents()->GetTitle();
-  if (install_info.title.empty())
-    install_info.title = base::UTF8ToUTF16(install_info.app_url.spec());
-
-  install_info.is_bookmark_app = true;
+  if (web_app_info_.title.empty())
+    web_app_info_.title = web_contents()->GetTitle();
+  if (web_app_info_.title.empty())
+    web_app_info_.title = base::UTF8ToUTF16(web_app_info_.app_url.spec());
 
   // Add the downloaded icons. Extensions only allow certain icon sizes. First
   // populate icons that match the allowed sizes exactly and then downscale
@@ -464,7 +502,7 @@ void TabHelper::FinishCreateHostedApp(
     icon_info.data = resized_bitmaps_it->second;
     icon_info.width = icon_info.data.width();
     icon_info.height = icon_info.data.height();
-    install_info.icons.push_back(icon_info);
+    web_app_info_.icons.push_back(icon_info);
   }
 
   // Install the app.
@@ -473,7 +511,7 @@ void TabHelper::FinishCreateHostedApp(
   scoped_refptr<extensions::CrxInstaller> installer(
       extensions::CrxInstaller::CreateSilent(profile->GetExtensionService()));
   installer->set_error_on_unsupported_requirements(true);
-  installer->InstallWebApp(install_info);
+  installer->InstallWebApp(web_app_info_);
   favicon_downloader_.reset();
 }
 
@@ -546,16 +584,15 @@ void TabHelper::OnInlineWebstoreInstall(
 void TabHelper::OnGetAppInstallState(const GURL& requestor_url,
                                      int return_route_id,
                                      int callback_id) {
-  Profile* profile =
-      Profile::FromBrowserContext(web_contents()->GetBrowserContext());
-  ExtensionService* extension_service = profile->GetExtensionService();
-  const ExtensionSet* extensions = extension_service->extensions();
-  const ExtensionSet* disabled = extension_service->disabled_extensions();
+  ExtensionRegistry* registry =
+      ExtensionRegistry::Get(web_contents()->GetBrowserContext());
+  const ExtensionSet& extensions = registry->enabled_extensions();
+  const ExtensionSet& disabled_extensions = registry->disabled_extensions();
 
   std::string state;
-  if (extensions->GetHostedAppByURL(requestor_url))
+  if (extensions.GetHostedAppByURL(requestor_url))
     state = extension_misc::kAppStateInstalled;
-  else if (disabled->GetHostedAppByURL(requestor_url))
+  else if (disabled_extensions.GetHostedAppByURL(requestor_url))
     state = extension_misc::kAppStateDisabled;
   else
     state = extension_misc::kAppStateNotInstalled;
@@ -717,34 +754,21 @@ void TabHelper::Observe(int type,
 
       const Extension* extension =
           content::Details<const Extension>(details).ptr();
-      if (!extension || !extension->from_bookmark())
+      if (!extension ||
+          AppLaunchInfo::GetLaunchWebURL(extension) != web_app_info_.app_url)
         return;
 
-      // If enabled, launch the app launcher and highlight the new app.
-      // Otherwise, open the chrome://apps page in a new foreground tab.
-      if (IsAppLauncherEnabled()) {
-        AppListService::Get(chrome::GetHostDesktopTypeForNativeView(
-            web_contents()->GetView()->GetNativeView()))->
-            ShowForProfile(profile_);
-
-        content::NotificationService::current()->Notify(
-            chrome::NOTIFICATION_APP_INSTALLED_TO_APPLIST,
-            content::Source<Profile>(profile_),
-            content::Details<const std::string>(&extension->id()));
-        return;
-      }
+#if defined(OS_CHROMEOS)
+      ChromeLauncherController::instance()->PinAppWithID(extension->id());
+#endif
 
       // Android does not implement browser_finder.cc.
 #if !defined(OS_ANDROID)
       Browser* browser =
           chrome::FindBrowserWithWebContents(web_contents());
       if (browser) {
-        browser->OpenURL(
-            content::OpenURLParams(GURL(chrome::kChromeUIAppsURL),
-                                   content::Referrer(),
-                                   NEW_FOREGROUND_TAB,
-                                   content::PAGE_TRANSITION_LINK,
-                                   false));
+        browser->window()->ShowBookmarkAppBubble(web_app_info_,
+                                                 extension->id());
       }
 #endif
     }