shortcut_manager.cc revision effb81e5f8246d0db0270817048dc992db66e9fb
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. 4 5#include "chrome/browser/apps/shortcut_manager.h" 6 7#include "base/bind.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/profiles/profile.h" 17#include "chrome/browser/profiles/profile_info_cache.h" 18#include "chrome/browser/profiles/profile_manager.h" 19#include "chrome/browser/shell_integration.h" 20#include "chrome/browser/ui/web_applications/web_app_ui.h" 21#include "chrome/browser/web_applications/web_app.h" 22#include "chrome/common/chrome_switches.h" 23#include "chrome/common/pref_names.h" 24#include "components/user_prefs/pref_registry_syncable.h" 25#include "content/public/browser/browser_thread.h" 26#include "content/public/browser/notification_details.h" 27#include "content/public/browser/notification_source.h" 28#include "extensions/browser/extension_system.h" 29#include "extensions/common/extension_set.h" 30 31#if defined(OS_MACOSX) 32#include "apps/app_shim/app_shim_mac.h" 33#endif 34 35using extensions::Extension; 36 37namespace { 38 39// Creates a shortcut for an application in the applications menu, if there is 40// not already one present. 41void CreateShortcutsInApplicationsMenu( 42 const ShellIntegration::ShortcutInfo& shortcut_info) { 43 ShellIntegration::ShortcutLocations creation_locations; 44 // Create the shortcut in the Chrome Apps subdir. 45 creation_locations.applications_menu_location = 46 ShellIntegration::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS; 47 web_app::CreateShortcuts(shortcut_info, creation_locations, 48 web_app::SHORTCUT_CREATION_AUTOMATED); 49} 50 51bool ShouldCreateShortcutFor(const extensions::Extension* extension) { 52 return extension->is_platform_app() && 53 extension->location() != extensions::Manifest::COMPONENT && 54 extension->ShouldDisplayInAppLauncher(); 55} 56 57} // namespace 58 59// static 60void AppShortcutManager::RegisterProfilePrefs( 61 user_prefs::PrefRegistrySyncable* registry) { 62 // Indicates whether app shortcuts have been created. 63 registry->RegisterBooleanPref( 64 prefs::kAppShortcutsHaveBeenCreated, false, 65 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 66} 67 68AppShortcutManager::AppShortcutManager(Profile* profile) 69 : profile_(profile), 70 is_profile_info_cache_observer_(false), 71 prefs_(profile->GetPrefs()) { 72 // Use of g_browser_process requires that we are either on the UI thread, or 73 // there are no threads initialized (such as in unit tests). 74 DCHECK(!content::BrowserThread::IsThreadInitialized( 75 content::BrowserThread::UI) || 76 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 77 78 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, 79 content::Source<Profile>(profile_)); 80 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, 81 content::Source<Profile>(profile_)); 82 // Wait for extensions to be ready before running OnceOffCreateShortcuts. 83 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, 84 content::Source<Profile>(profile_)); 85 86 ProfileManager* profile_manager = g_browser_process->profile_manager(); 87 // profile_manager might be NULL in testing environments. 88 if (profile_manager) { 89 profile_manager->GetProfileInfoCache().AddObserver(this); 90 is_profile_info_cache_observer_ = true; 91 } 92} 93 94AppShortcutManager::~AppShortcutManager() { 95 if (g_browser_process && is_profile_info_cache_observer_) { 96 ProfileManager* profile_manager = g_browser_process->profile_manager(); 97 // profile_manager might be NULL in testing environments or during shutdown. 98 if (profile_manager) 99 profile_manager->GetProfileInfoCache().RemoveObserver(this); 100 } 101} 102 103void AppShortcutManager::Observe(int type, 104 const content::NotificationSource& source, 105 const content::NotificationDetails& details) { 106 switch (type) { 107 case chrome::NOTIFICATION_EXTENSIONS_READY: { 108 OnceOffCreateShortcuts(); 109 break; 110 } 111 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { 112#if defined(OS_MACOSX) 113 if (!apps::IsAppShimsEnabled()) 114 break; 115#endif // defined(OS_MACOSX) 116 117 const extensions::InstalledExtensionInfo* installed_info = 118 content::Details<const extensions::InstalledExtensionInfo>(details) 119 .ptr(); 120 const Extension* extension = installed_info->extension; 121 // If the app is being updated, update any existing shortcuts but do not 122 // create new ones. If it is being installed, automatically create a 123 // shortcut in the applications menu (e.g., Start Menu). 124 base::Callback<void(const ShellIntegration::ShortcutInfo&)> 125 create_or_update; 126 if (installed_info->is_update) { 127 base::string16 old_title = 128 base::UTF8ToUTF16(installed_info->old_name); 129 create_or_update = base::Bind(&web_app::UpdateAllShortcuts, 130 old_title); 131 } else if (ShouldCreateShortcutFor(extension)) { 132 create_or_update = base::Bind(&CreateShortcutsInApplicationsMenu); 133 } 134 if (!create_or_update.is_null()) { 135 web_app::UpdateShortcutInfoAndIconForApp(extension, profile_, 136 create_or_update); 137 } 138 break; 139 } 140 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { 141 const Extension* extension = content::Details<const Extension>( 142 details).ptr(); 143 DeleteApplicationShortcuts(extension); 144 break; 145 } 146 default: 147 NOTREACHED(); 148 } 149} 150 151void AppShortcutManager::OnProfileWillBeRemoved( 152 const base::FilePath& profile_path) { 153 if (profile_path != profile_->GetPath()) 154 return; 155 content::BrowserThread::PostTask( 156 content::BrowserThread::FILE, FROM_HERE, 157 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile, 158 profile_path)); 159} 160 161void AppShortcutManager::OnceOffCreateShortcuts() { 162 bool was_enabled = prefs_->GetBoolean(prefs::kAppShortcutsHaveBeenCreated); 163 164 // Creation of shortcuts on Mac currently can be disabled with 165 // --disable-app-shims, so check the flag, and set the pref accordingly. 166#if defined(OS_MACOSX) 167 bool is_now_enabled = apps::IsAppShimsEnabled(); 168#else 169 bool is_now_enabled = true; 170#endif // defined(OS_MACOSX) 171 172 if (was_enabled != is_now_enabled) 173 prefs_->SetBoolean(prefs::kAppShortcutsHaveBeenCreated, is_now_enabled); 174 175 if (was_enabled || !is_now_enabled) 176 return; 177 178 // Check if extension system/service are available. They might not be in 179 // tests. 180 extensions::ExtensionSystem* extension_system; 181 ExtensionServiceInterface* extension_service; 182 if (!(extension_system = extensions::ExtensionSystem::Get(profile_)) || 183 !(extension_service = extension_system->extension_service())) 184 return; 185 186 // Create an applications menu shortcut for each app in this profile. 187 const extensions::ExtensionSet* apps = extension_service->extensions(); 188 for (extensions::ExtensionSet::const_iterator it = apps->begin(); 189 it != apps->end(); ++it) { 190 if (ShouldCreateShortcutFor(it->get())) 191 web_app::UpdateShortcutInfoAndIconForApp( 192 it->get(), profile_, base::Bind(&CreateShortcutsInApplicationsMenu)); 193 } 194} 195 196void AppShortcutManager::DeleteApplicationShortcuts( 197 const Extension* extension) { 198 ShellIntegration::ShortcutInfo delete_info = 199 web_app::ShortcutInfoForExtensionAndProfile(extension, profile_); 200 web_app::DeleteAllShortcuts(delete_info); 201} 202