Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / ash / launcher / launcher_app_tab_helper.cc
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/profiles/profile.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "content/public/browser/navigation_entry.h"
15 #include "content/public/browser/web_contents.h"
16 #include "extensions/common/extension.h"
17
18 namespace {
19
20 const extensions::Extension* GetExtensionForTab(Profile* profile,
21                                                 content::WebContents* tab) {
22   ExtensionService* extension_service = profile->GetExtensionService();
23   if (!extension_service || !extension_service->extensions_enabled())
24     return NULL;
25   Browser* browser = chrome::FindBrowserWithWebContents(tab);
26   DCHECK(browser);
27   GURL url = tab->GetURL();
28   if (browser->is_app() && tab->GetController().GetEntryCount())
29     url = tab->GetController().GetEntryAtIndex(0)->GetURL();
30   return extension_service->GetInstalledApp(url);
31 }
32
33 const extensions::Extension* GetExtensionByID(Profile* profile,
34                                               const std::string& id) {
35   ExtensionService* extension_service = profile->GetExtensionService();
36   if (!extension_service || !extension_service->extensions_enabled())
37     return NULL;
38   return extension_service->GetInstalledExtension(id);
39 }
40
41 }  // namespace
42
43 LauncherAppTabHelper::LauncherAppTabHelper(Profile* profile)
44     : profile_(profile) {
45 }
46
47 LauncherAppTabHelper::~LauncherAppTabHelper() {
48 }
49
50 std::string LauncherAppTabHelper::GetAppID(content::WebContents* tab) {
51   ProfileManager* profile_manager = g_browser_process->profile_manager();
52   if (profile_manager) {
53     const std::vector<Profile*> profile_list =
54         profile_manager->GetLoadedProfiles();
55     if (profile_list.size() > 0) {
56       for (std::vector<Profile*>::const_iterator it = profile_list.begin();
57            it != profile_list.end();
58            ++it) {
59         const extensions::Extension* extension = GetExtensionForTab(*it, tab);
60         if (extension)
61           return extension->id();
62       }
63       return std::string();
64     }
65   }
66   // If there is no profile manager we only use the known profile.
67   const extensions::Extension* extension = GetExtensionForTab(profile_, tab);
68   return extension ? extension->id() : std::string();
69 }
70
71 bool LauncherAppTabHelper::IsValidIDForCurrentUser(const std::string& id) {
72   return GetExtensionByID(profile_, id) != NULL;
73 }
74
75 void LauncherAppTabHelper::SetCurrentUser(Profile* profile) {
76   profile_ = profile;
77 }