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