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/common/chrome_switches.h"
16 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
17 #include "content/public/browser/navigation_entry.h"
18 #include "content/public/browser/web_contents.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/common/extension.h"
24 const extensions::Extension
* GetExtensionForTab(Profile
* profile
,
25 content::WebContents
* tab
) {
26 ExtensionService
* extension_service
= profile
->GetExtensionService();
27 if (!extension_service
|| !extension_service
->extensions_enabled())
30 Browser
* browser
= chrome::FindBrowserWithWebContents(tab
);
33 GURL url
= tab
->GetURL();
34 if (browser
->is_app()) {
35 // Only consider the original URL of an app window when determining its
36 // associated extension.
37 if (tab
->GetController().GetEntryCount())
38 url
= tab
->GetController().GetEntryAtIndex(0)->GetURL();
40 // Bookmark app windows should match their launch url extension despite
42 if (CommandLine::ForCurrentProcess()->HasSwitch(
43 switches::kEnableStreamlinedHostedApps
)) {
44 const extensions::ExtensionSet
& extensions
=
45 extensions::ExtensionRegistry::Get(profile
)->enabled_extensions();
46 for (extensions::ExtensionSet::const_iterator it
= extensions
.begin();
47 it
!= extensions
.end(); ++it
) {
48 if (it
->get()->from_bookmark() &&
49 extensions::AppLaunchInfo::GetLaunchWebURL(it
->get()) == url
) {
56 return extension_service
->GetInstalledApp(url
);
59 const extensions::Extension
* GetExtensionByID(Profile
* profile
,
60 const std::string
& id
) {
61 ExtensionService
* extension_service
= profile
->GetExtensionService();
62 if (!extension_service
|| !extension_service
->extensions_enabled())
64 return extension_service
->GetInstalledExtension(id
);
69 LauncherAppTabHelper::LauncherAppTabHelper(Profile
* profile
)
73 LauncherAppTabHelper::~LauncherAppTabHelper() {
76 std::string
LauncherAppTabHelper::GetAppID(content::WebContents
* tab
) {
77 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
78 if (profile_manager
) {
79 const std::vector
<Profile
*> profile_list
=
80 profile_manager
->GetLoadedProfiles();
81 if (profile_list
.size() > 0) {
82 for (std::vector
<Profile
*>::const_iterator it
= profile_list
.begin();
83 it
!= profile_list
.end();
85 const extensions::Extension
* extension
= GetExtensionForTab(*it
, tab
);
87 return extension
->id();
92 // If there is no profile manager we only use the known profile.
93 const extensions::Extension
* extension
= GetExtensionForTab(profile_
, tab
);
94 return extension
? extension
->id() : std::string();
97 bool LauncherAppTabHelper::IsValidIDForCurrentUser(const std::string
& id
) {
98 return GetExtensionByID(profile_
, id
) != NULL
;
101 void LauncherAppTabHelper::SetCurrentUser(Profile
* profile
) {