Home | History | Annotate | Download | only in launcher
      1 // Copyright (c) 2012 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/ui/ash/launcher/launcher_app_tab_helper.h"
      6 
      7 #include <vector>
      8 
      9 #include "base/command_line.h"
     10 #include "chrome/browser/browser_process.h"
     11 #include "chrome/browser/extensions/extension_service.h"
     12 #include "chrome/browser/profiles/profile.h"
     13 #include "chrome/browser/profiles/profile_manager.h"
     14 #include "chrome/browser/ui/browser_finder.h"
     15 #include "chrome/browser/web_applications/web_app.h"
     16 #include "chrome/common/chrome_switches.h"
     17 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
     18 #include "content/public/browser/navigation_entry.h"
     19 #include "content/public/browser/web_contents.h"
     20 #include "extensions/browser/extension_registry.h"
     21 #include "extensions/browser/extension_system.h"
     22 #include "extensions/common/extension.h"
     23 #include "extensions/common/extension_set.h"
     24 
     25 namespace {
     26 
     27 const extensions::Extension* GetExtensionForTab(Profile* profile,
     28                                                 content::WebContents* tab) {
     29   ExtensionService* extension_service =
     30       extensions::ExtensionSystem::Get(profile)->extension_service();
     31   if (!extension_service || !extension_service->extensions_enabled())
     32     return NULL;
     33 
     34   // Note: It is possible to come here after a tab got removed form the browser
     35   // before it gets destroyed, in which case there is no browser.
     36   Browser* browser = chrome::FindBrowserWithWebContents(tab);
     37 
     38   extensions::ExtensionRegistry* registry =
     39       extensions::ExtensionRegistry::Get(profile);
     40 
     41   // Use the Browser's app name to determine the extension for app windows and
     42   // use the tab's url for app tabs.
     43   if (browser && browser->is_app()) {
     44     return registry->GetExtensionById(
     45         web_app::GetExtensionIdFromApplicationName(browser->app_name()),
     46         extensions::ExtensionRegistry::EVERYTHING);
     47   }
     48 
     49   const GURL url = tab->GetURL();
     50   const extensions::ExtensionSet& extensions = registry->enabled_extensions();
     51   const extensions::Extension* extension = extensions.GetAppByURL(url);
     52   if (extension)
     53     return extension;
     54 
     55   // Bookmark app windows should match their launch url extension despite
     56   // their web extents.
     57   if (CommandLine::ForCurrentProcess()->HasSwitch(
     58           switches::kEnableStreamlinedHostedApps)) {
     59     for (extensions::ExtensionSet::const_iterator it = extensions.begin();
     60          it != extensions.end(); ++it) {
     61       if (it->get()->from_bookmark() &&
     62           extensions::AppLaunchInfo::GetLaunchWebURL(it->get()) == url) {
     63         return it->get();
     64       }
     65     }
     66   }
     67   return NULL;
     68 }
     69 
     70 const extensions::Extension* GetExtensionByID(Profile* profile,
     71                                               const std::string& id) {
     72   ExtensionService* extension_service = profile->GetExtensionService();
     73   if (!extension_service || !extension_service->extensions_enabled())
     74     return NULL;
     75   return extension_service->GetInstalledExtension(id);
     76 }
     77 
     78 }  // namespace
     79 
     80 LauncherAppTabHelper::LauncherAppTabHelper(Profile* profile)
     81     : profile_(profile) {
     82 }
     83 
     84 LauncherAppTabHelper::~LauncherAppTabHelper() {
     85 }
     86 
     87 std::string LauncherAppTabHelper::GetAppID(content::WebContents* tab) {
     88   ProfileManager* profile_manager = g_browser_process->profile_manager();
     89   if (profile_manager) {
     90     const std::vector<Profile*> profile_list =
     91         profile_manager->GetLoadedProfiles();
     92     if (profile_list.size() > 0) {
     93       for (std::vector<Profile*>::const_iterator it = profile_list.begin();
     94            it != profile_list.end();
     95            ++it) {
     96         const extensions::Extension* extension = GetExtensionForTab(*it, tab);
     97         if (extension)
     98           return extension->id();
     99       }
    100       return std::string();
    101     }
    102   }
    103   // If there is no profile manager we only use the known profile.
    104   const extensions::Extension* extension = GetExtensionForTab(profile_, tab);
    105   return extension ? extension->id() : std::string();
    106 }
    107 
    108 bool LauncherAppTabHelper::IsValidIDForCurrentUser(const std::string& id) {
    109   return GetExtensionByID(profile_, id) != NULL;
    110 }
    111 
    112 void LauncherAppTabHelper::SetCurrentUser(Profile* profile) {
    113   profile_ = profile;
    114 }
    115