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