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 "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"
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
);
52 if (extension
&& !extensions::LaunchesInWindow(profile
, extension
))
55 // Bookmark app windows should match their launch url extension despite
57 if (extensions::util::IsNewBookmarkAppsEnabled()) {
58 for (extensions::ExtensionSet::const_iterator it
= extensions
.begin();
59 it
!= extensions
.end(); ++it
) {
60 if (it
->get()->from_bookmark() &&
61 extensions::AppLaunchInfo::GetLaunchWebURL(it
->get()) == url
&&
62 !extensions::LaunchesInWindow(profile
, it
->get())) {
70 const extensions::Extension
* GetExtensionByID(Profile
* profile
,
71 const std::string
& id
) {
72 return extensions::ExtensionRegistry::Get(profile
)->GetExtensionById(
73 id
, extensions::ExtensionRegistry::EVERYTHING
);
78 LauncherAppTabHelper::LauncherAppTabHelper(Profile
* profile
)
82 LauncherAppTabHelper::~LauncherAppTabHelper() {
85 std::string
LauncherAppTabHelper::GetAppID(content::WebContents
* tab
) {
86 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
87 if (profile_manager
) {
88 const std::vector
<Profile
*> profile_list
=
89 profile_manager
->GetLoadedProfiles();
90 if (profile_list
.size() > 0) {
91 for (std::vector
<Profile
*>::const_iterator it
= profile_list
.begin();
92 it
!= profile_list
.end();
94 const extensions::Extension
* extension
= GetExtensionForTab(*it
, tab
);
96 return extension
->id();
101 // If there is no profile manager we only use the known profile.
102 const extensions::Extension
* extension
= GetExtensionForTab(profile_
, tab
);
103 return extension
? extension
->id() : std::string();
106 bool LauncherAppTabHelper::IsValidIDForCurrentUser(const std::string
& id
) {
107 return GetExtensionByID(profile_
, id
) != NULL
;
110 void LauncherAppTabHelper::SetCurrentUser(Profile
* profile
) {