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.
5 #include "chrome/browser/ui/ash/launcher/launcher_app_tab_helper.h"
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"
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())
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);
38 extensions::ExtensionRegistry* registry =
39 extensions::ExtensionRegistry::Get(profile);
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);
49 const GURL url = tab->GetURL();
50 const extensions::ExtensionSet& extensions = registry->enabled_extensions();
51 const extensions::Extension* extension = extensions.GetAppByURL(url);
55 // Bookmark app windows should match their launch url extension despite
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) {
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())
75 return extension_service->GetInstalledExtension(id);
80 LauncherAppTabHelper::LauncherAppTabHelper(Profile* profile)
84 LauncherAppTabHelper::~LauncherAppTabHelper() {
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();
96 const extensions::Extension* extension = GetExtensionForTab(*it, tab);
98 return extension->id();
100 return std::string();
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();
108 bool LauncherAppTabHelper::IsValidIDForCurrentUser(const std::string& id) {
109 return GetExtensionByID(profile_, id) != NULL;
112 void LauncherAppTabHelper::SetCurrentUser(Profile* profile) {