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