| OLD | NEW |
| (Empty) |
| 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 "chrome/browser/browser_process.h" | |
| 10 #include "chrome/browser/extensions/extension_service.h" | |
| 11 #include "chrome/browser/extensions/extension_util.h" | |
| 12 #include "chrome/browser/extensions/launch_util.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "chrome/browser/profiles/profile_manager.h" | |
| 15 #include "chrome/browser/ui/browser_finder.h" | |
| 16 #include "chrome/browser/web_applications/web_app.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 #if defined(OS_CHROMEOS) | |
| 26 #include "chrome/browser/ui/app_list/arc/arc_app_list_prefs.h" | |
| 27 #endif | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 const extensions::Extension* GetExtensionForTab(Profile* profile, | |
| 32 content::WebContents* tab) { | |
| 33 ExtensionService* extension_service = | |
| 34 extensions::ExtensionSystem::Get(profile)->extension_service(); | |
| 35 if (!extension_service || !extension_service->extensions_enabled()) | |
| 36 return NULL; | |
| 37 | |
| 38 // Note: It is possible to come here after a tab got removed form the browser | |
| 39 // before it gets destroyed, in which case there is no browser. | |
| 40 Browser* browser = chrome::FindBrowserWithWebContents(tab); | |
| 41 | |
| 42 extensions::ExtensionRegistry* registry = | |
| 43 extensions::ExtensionRegistry::Get(profile); | |
| 44 | |
| 45 // Use the Browser's app name to determine the extension for app windows and | |
| 46 // use the tab's url for app tabs. | |
| 47 if (browser && browser->is_app()) { | |
| 48 return registry->GetExtensionById( | |
| 49 web_app::GetExtensionIdFromApplicationName(browser->app_name()), | |
| 50 extensions::ExtensionRegistry::EVERYTHING); | |
| 51 } | |
| 52 | |
| 53 const GURL url = tab->GetURL(); | |
| 54 const extensions::ExtensionSet& extensions = registry->enabled_extensions(); | |
| 55 const extensions::Extension* extension = extensions.GetAppByURL(url); | |
| 56 if (extension && !extensions::LaunchesInWindow(profile, extension)) | |
| 57 return extension; | |
| 58 | |
| 59 // Bookmark app windows should match their launch url extension despite | |
| 60 // their web extents. | |
| 61 if (extensions::util::IsNewBookmarkAppsEnabled()) { | |
| 62 for (extensions::ExtensionSet::const_iterator it = extensions.begin(); | |
| 63 it != extensions.end(); ++it) { | |
| 64 if (it->get()->from_bookmark() && | |
| 65 extensions::AppLaunchInfo::GetLaunchWebURL(it->get()) == url && | |
| 66 !extensions::LaunchesInWindow(profile, it->get())) { | |
| 67 return it->get(); | |
| 68 } | |
| 69 } | |
| 70 } | |
| 71 return NULL; | |
| 72 } | |
| 73 | |
| 74 const extensions::Extension* GetExtensionByID(Profile* profile, | |
| 75 const std::string& id) { | |
| 76 return extensions::ExtensionRegistry::Get(profile)->GetExtensionById( | |
| 77 id, extensions::ExtensionRegistry::EVERYTHING); | |
| 78 } | |
| 79 | |
| 80 } // namespace | |
| 81 | |
| 82 LauncherAppTabHelper::LauncherAppTabHelper(Profile* profile) | |
| 83 : profile_(profile) { | |
| 84 } | |
| 85 | |
| 86 LauncherAppTabHelper::~LauncherAppTabHelper() { | |
| 87 } | |
| 88 | |
| 89 std::string LauncherAppTabHelper::GetAppID(content::WebContents* tab) { | |
| 90 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 91 if (profile_manager) { | |
| 92 const std::vector<Profile*> profile_list = | |
| 93 profile_manager->GetLoadedProfiles(); | |
| 94 if (profile_list.size() > 0) { | |
| 95 for (std::vector<Profile*>::const_iterator it = profile_list.begin(); | |
| 96 it != profile_list.end(); | |
| 97 ++it) { | |
| 98 const extensions::Extension* extension = GetExtensionForTab(*it, tab); | |
| 99 if (extension) | |
| 100 return extension->id(); | |
| 101 } | |
| 102 return std::string(); | |
| 103 } | |
| 104 } | |
| 105 // If there is no profile manager we only use the known profile. | |
| 106 const extensions::Extension* extension = GetExtensionForTab(profile_, tab); | |
| 107 return extension ? extension->id() : std::string(); | |
| 108 } | |
| 109 | |
| 110 bool LauncherAppTabHelper::IsValidIDForCurrentUser(const std::string& id) { | |
| 111 #if defined(OS_CHROMEOS) | |
| 112 if (ArcAppListPrefs::Get(profile_)->IsRegistered(id)) | |
| 113 return true; | |
| 114 #endif | |
| 115 return GetExtensionByID(profile_, id) != NULL; | |
| 116 } | |
| 117 | |
| 118 void LauncherAppTabHelper::SetCurrentUser(Profile* profile) { | |
| 119 profile_ = profile; | |
| 120 } | |
| OLD | NEW |