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/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" 30 31 #if defined(OS_MACOSX) 32 #include "apps/app_shim/app_shim_mac.h" 33 #endif 34 35 using extensions::Extension; 36 37 namespace { 38 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 // 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 51 bool 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 60 void 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 68 AppShortcutManager::AppShortcutManager(Profile* profile) 69 : profile_(profile), 70 is_profile_info_cache_observer_(false), 71 prefs_(profile->GetPrefs()), 72 weak_factory_(this) { 73 // Use of g_browser_process requires that we are either on the UI thread, or 74 // there are no threads initialized (such as in unit tests). 75 DCHECK(!content::BrowserThread::IsThreadInitialized( 76 content::BrowserThread::UI) || 77 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 78 79 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, 80 content::Source<Profile>(profile_)); 81 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, 82 content::Source<Profile>(profile_)); 83 // Wait for extensions to be ready before running OnceOffCreateShortcuts. 84 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, 85 content::Source<Profile>(profile_)); 86 87 ProfileManager* profile_manager = g_browser_process->profile_manager(); 88 // profile_manager might be NULL in testing environments. 89 if (profile_manager) { 90 profile_manager->GetProfileInfoCache().AddObserver(this); 91 is_profile_info_cache_observer_ = true; 92 } 93 } 94 95 AppShortcutManager::~AppShortcutManager() { 96 if (g_browser_process && is_profile_info_cache_observer_) { 97 ProfileManager* profile_manager = g_browser_process->profile_manager(); 98 // profile_manager might be NULL in testing environments or during shutdown. 99 if (profile_manager) 100 profile_manager->GetProfileInfoCache().RemoveObserver(this); 101 } 102 } 103 104 void AppShortcutManager::Observe(int type, 105 const content::NotificationSource& source, 106 const content::NotificationDetails& details) { 107 switch (type) { 108 case chrome::NOTIFICATION_EXTENSIONS_READY: { 109 OnceOffCreateShortcuts(); 110 break; 111 } 112 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { 113 #if defined(OS_MACOSX) 114 if (!apps::IsAppShimsEnabled()) 115 break; 116 #endif // defined(OS_MACOSX) 117 118 const extensions::InstalledExtensionInfo* installed_info = 119 content::Details<const extensions::InstalledExtensionInfo>(details) 120 .ptr(); 121 const Extension* extension = installed_info->extension; 122 if (ShouldCreateShortcutFor(extension)) { 123 // If the app is being updated, update any existing shortcuts but do not 124 // create new ones. If it is being installed, automatically create a 125 // shortcut in the applications menu (e.g., Start Menu). 126 base::Callback<void(const ShellIntegration::ShortcutInfo&)> 127 create_or_update; 128 if (installed_info->is_update) { 129 base::string16 old_title = UTF8ToUTF16(installed_info->old_name); 130 create_or_update = base::Bind(&web_app::UpdateAllShortcuts, 131 old_title); 132 } else { 133 create_or_update = base::Bind(&CreateShortcutsInApplicationsMenu); 134 } 135 136 web_app::UpdateShortcutInfoAndIconForApp(*extension, profile_, 137 create_or_update); 138 } 139 break; 140 } 141 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { 142 const Extension* extension = content::Details<const Extension>( 143 details).ptr(); 144 DeleteApplicationShortcuts(extension); 145 break; 146 } 147 default: 148 NOTREACHED(); 149 } 150 } 151 152 void AppShortcutManager::OnProfileWillBeRemoved( 153 const base::FilePath& profile_path) { 154 if (profile_path != profile_->GetPath()) 155 return; 156 content::BrowserThread::PostTask( 157 content::BrowserThread::FILE, FROM_HERE, 158 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile, 159 profile_path)); 160 } 161 162 void AppShortcutManager::OnceOffCreateShortcuts() { 163 bool was_enabled = prefs_->GetBoolean(prefs::kAppShortcutsHaveBeenCreated); 164 165 // Creation of shortcuts on Mac currently sits behind --enable-app-shims. 166 // Until it is enabled permanently, we need to check the flag, and set the 167 // pref accordingly. 168 #if defined(OS_MACOSX) 169 bool is_now_enabled = apps::IsAppShimsEnabled(); 170 #else 171 bool is_now_enabled = true; 172 #endif // defined(OS_MACOSX) 173 174 if (was_enabled != is_now_enabled) 175 prefs_->SetBoolean(prefs::kAppShortcutsHaveBeenCreated, is_now_enabled); 176 177 if (was_enabled || !is_now_enabled) 178 return; 179 180 // Check if extension system/service are available. They might not be in 181 // tests. 182 extensions::ExtensionSystem* extension_system; 183 ExtensionServiceInterface* extension_service; 184 if (!(extension_system = extensions::ExtensionSystem::Get(profile_)) || 185 !(extension_service = extension_system->extension_service())) 186 return; 187 188 // Create an applications menu shortcut for each app in this profile. 189 const ExtensionSet* apps = extension_service->extensions(); 190 for (ExtensionSet::const_iterator it = apps->begin(); 191 it != apps->end(); ++it) { 192 if (ShouldCreateShortcutFor(it->get())) 193 web_app::UpdateShortcutInfoAndIconForApp( 194 *it->get(), profile_, base::Bind(&CreateShortcutsInApplicationsMenu)); 195 } 196 } 197 198 void AppShortcutManager::DeleteApplicationShortcuts( 199 const Extension* extension) { 200 ShellIntegration::ShortcutInfo delete_info = 201 web_app::ShortcutInfoForExtensionAndProfile(extension, profile_); 202 web_app::DeleteAllShortcuts(delete_info); 203 } 204