1 // Copyright 2013 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/apps/shortcut_manager.h"
8 #include "base/command_line.h"
9 #include "base/compiler_specific.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/extensions/extension_system.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_info_cache.h"
19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/browser/shell_integration.h"
21 #include "chrome/browser/ui/web_applications/web_app_ui.h"
22 #include "chrome/browser/web_applications/web_app.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/common/extensions/extension_set.h"
25 #include "chrome/common/pref_names.h"
26 #include "components/user_prefs/pref_registry_syncable.h"
27 #include "content/public/browser/browser_thread.h"
28 #include "content/public/browser/notification_details.h"
29 #include "content/public/browser/notification_source.h"
31 #if defined(OS_MACOSX)
32 #include "apps/app_shim/app_shim_mac.h"
35 using extensions::Extension;
39 // Creates a shortcut for an application in the applications menu, if there is
40 // not already one present.
41 void CreateShortcutsInApplicationsMenu(
42 const ShellIntegration::ShortcutInfo& shortcut_info) {
43 ShellIntegration::ShortcutLocations creation_locations;
44 creation_locations.in_applications_menu = true;
45 // Create the shortcut in the Chrome Apps subdir.
46 creation_locations.applications_menu_subdir =
47 web_app::GetAppShortcutsSubdirName();
48 web_app::CreateShortcuts(shortcut_info, creation_locations,
49 web_app::SHORTCUT_CREATION_AUTOMATED);
52 bool ShouldCreateShortcutFor(const extensions::Extension* extension) {
53 return extension->is_platform_app() &&
54 extension->location() != extensions::Manifest::COMPONENT &&
55 extension->ShouldDisplayInAppLauncher();
61 void AppShortcutManager::RegisterProfilePrefs(
62 user_prefs::PrefRegistrySyncable* registry) {
63 // Indicates whether app shortcuts have been created.
64 registry->RegisterBooleanPref(
65 prefs::kAppShortcutsHaveBeenCreated, false,
66 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
69 AppShortcutManager::AppShortcutManager(Profile* profile)
71 is_profile_info_cache_observer_(false),
72 prefs_(profile->GetPrefs()),
74 // Use of g_browser_process requires that we are either on the UI thread, or
75 // there are no threads initialized (such as in unit tests).
76 DCHECK(!content::BrowserThread::IsThreadInitialized(
77 content::BrowserThread::UI) ||
78 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
80 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
81 content::Source<Profile>(profile_));
82 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
83 content::Source<Profile>(profile_));
84 // Wait for extensions to be ready before running OnceOffCreateShortcuts.
85 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
86 content::Source<Profile>(profile_));
88 ProfileManager* profile_manager = g_browser_process->profile_manager();
89 // profile_manager might be NULL in testing environments.
90 if (profile_manager) {
91 profile_manager->GetProfileInfoCache().AddObserver(this);
92 is_profile_info_cache_observer_ = true;
96 AppShortcutManager::~AppShortcutManager() {
97 if (g_browser_process && is_profile_info_cache_observer_) {
98 ProfileManager* profile_manager = g_browser_process->profile_manager();
99 // profile_manager might be NULL in testing environments or during shutdown.
101 profile_manager->GetProfileInfoCache().RemoveObserver(this);
105 void AppShortcutManager::Observe(int type,
106 const content::NotificationSource& source,
107 const content::NotificationDetails& details) {
109 case chrome::NOTIFICATION_EXTENSIONS_READY: {
110 OnceOffCreateShortcuts();
113 case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
114 #if defined(OS_MACOSX)
115 if (!apps::IsAppShimsEnabled())
117 #endif // defined(OS_MACOSX)
119 const extensions::InstalledExtensionInfo* installed_info =
120 content::Details<const extensions::InstalledExtensionInfo>(details)
122 const Extension* extension = installed_info->extension;
123 if (ShouldCreateShortcutFor(extension)) {
124 // If the app is being updated, update any existing shortcuts but do not
125 // create new ones. If it is being installed, automatically create a
126 // shortcut in the applications menu (e.g., Start Menu).
127 base::Callback<void(const ShellIntegration::ShortcutInfo&)>
129 if (installed_info->is_update) {
130 string16 old_title = UTF8ToUTF16(installed_info->old_name);
131 create_or_update = base::Bind(&web_app::UpdateAllShortcuts,
134 create_or_update = base::Bind(&CreateShortcutsInApplicationsMenu);
137 web_app::UpdateShortcutInfoAndIconForApp(*extension, profile_,
142 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: {
143 const Extension* extension = content::Details<const Extension>(
145 DeleteApplicationShortcuts(extension);
153 void AppShortcutManager::OnProfileWillBeRemoved(
154 const base::FilePath& profile_path) {
155 if (profile_path != profile_->GetPath())
157 content::BrowserThread::PostTask(
158 content::BrowserThread::FILE, FROM_HERE,
159 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile,
163 void AppShortcutManager::OnceOffCreateShortcuts() {
164 bool was_enabled = prefs_->GetBoolean(prefs::kAppShortcutsHaveBeenCreated);
166 // Creation of shortcuts on Mac currently sits behind --enable-app-shims.
167 // Until it is enabled permanently, we need to check the flag, and set the
169 #if defined(OS_MACOSX)
170 bool is_now_enabled = apps::IsAppShimsEnabled();
172 bool is_now_enabled = true;
173 #endif // defined(OS_MACOSX)
175 if (was_enabled != is_now_enabled)
176 prefs_->SetBoolean(prefs::kAppShortcutsHaveBeenCreated, is_now_enabled);
178 if (was_enabled || !is_now_enabled)
181 // Check if extension system/service are available. They might not be in
183 extensions::ExtensionSystem* extension_system;
184 ExtensionServiceInterface* extension_service;
185 if (!(extension_system = extensions::ExtensionSystem::Get(profile_)) ||
186 !(extension_service = extension_system->extension_service()))
189 // Create an applications menu shortcut for each app in this profile.
190 const ExtensionSet* apps = extension_service->extensions();
191 for (ExtensionSet::const_iterator it = apps->begin();
192 it != apps->end(); ++it) {
193 if (ShouldCreateShortcutFor(it->get()))
194 web_app::UpdateShortcutInfoAndIconForApp(
195 *it->get(), profile_, base::Bind(&CreateShortcutsInApplicationsMenu));
199 void AppShortcutManager::DeleteApplicationShortcuts(
200 const Extension* extension) {
201 ShellIntegration::ShortcutInfo delete_info =
202 web_app::ShortcutInfoForExtensionAndProfile(extension, profile_);
203 web_app::DeleteAllShortcuts(delete_info);