Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/apps/shortcut_manager.h" | 5 #include "chrome/browser/apps/shortcut_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 25 #include "extensions/browser/extension_registry.h" | 25 #include "extensions/browser/extension_registry.h" |
| 26 #include "extensions/browser/extension_system.h" | 26 #include "extensions/browser/extension_system.h" |
| 27 #include "extensions/common/extension_set.h" | 27 #include "extensions/common/extension_set.h" |
| 28 #include "extensions/common/one_shot_event.h" | 28 #include "extensions/common/one_shot_event.h" |
| 29 | 29 |
| 30 using extensions::Extension; | 30 using extensions::Extension; |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 // This version number is stored in local prefs to check whether app shortcuts | |
| 35 // need to be recreated. This might happen when we change various aspects of app | |
| 36 // shortcuts like command-line flags or associated icons, binaries, etc. | |
| 37 const int kCurrentAppShortcutsVersion = 1; | |
|
tapted
2014/05/30 06:33:27
Note we might want this to be per-platform. And th
jackhou1
2014/06/02 04:25:19
Yeah, I think ifdefs here is fine. Similar to kDes
| |
| 38 | |
| 34 // Creates a shortcut for an application in the applications menu, if there is | 39 // Creates a shortcut for an application in the applications menu, if there is |
| 35 // not already one present. | 40 // not already one present. |
| 36 void CreateShortcutsInApplicationsMenu(Profile* profile, | 41 void CreateShortcutsInApplicationsMenu(Profile* profile, |
| 37 const Extension* app) { | 42 const Extension* app) { |
| 38 web_app::ShortcutLocations creation_locations; | 43 web_app::ShortcutLocations creation_locations; |
| 39 // Create the shortcut in the Chrome Apps subdir. | 44 // Create the shortcut in the Chrome Apps subdir. |
| 40 creation_locations.applications_menu_location = | 45 creation_locations.applications_menu_location = |
| 41 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS; | 46 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS; |
| 42 web_app::CreateShortcuts( | 47 web_app::CreateShortcuts( |
| 43 web_app::SHORTCUT_CREATION_AUTOMATED, creation_locations, profile, app); | 48 web_app::SHORTCUT_CREATION_AUTOMATED, creation_locations, profile, app); |
| 44 } | 49 } |
| 45 | 50 |
| 46 bool ShouldCreateShortcutFor(Profile* profile, const Extension* extension) { | |
| 47 return extension->is_platform_app() && | |
| 48 extension->location() != extensions::Manifest::COMPONENT && | |
| 49 extensions::ui_util::ShouldDisplayInAppLauncher(extension, profile); | |
| 50 } | |
| 51 | |
| 52 } // namespace | 51 } // namespace |
| 53 | 52 |
| 54 // static | 53 // static |
| 55 void AppShortcutManager::RegisterProfilePrefs( | 54 void AppShortcutManager::RegisterProfilePrefs( |
| 56 user_prefs::PrefRegistrySyncable* registry) { | 55 user_prefs::PrefRegistrySyncable* registry) { |
| 57 // Indicates whether app shortcuts have been created. | 56 // Indicates whether app shortcuts have been created. |
| 58 registry->RegisterBooleanPref( | 57 registry->RegisterIntegerPref( |
| 59 prefs::kAppShortcutsHaveBeenCreated, false, | 58 prefs::kAppShortcutsVersion, 0, |
| 60 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 59 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 61 } | 60 } |
| 62 | 61 |
| 63 AppShortcutManager::AppShortcutManager(Profile* profile) | 62 AppShortcutManager::AppShortcutManager(Profile* profile) |
| 64 : profile_(profile), | 63 : profile_(profile), |
| 65 is_profile_info_cache_observer_(false), | 64 is_profile_info_cache_observer_(false), |
| 66 prefs_(profile->GetPrefs()), | 65 prefs_(profile->GetPrefs()), |
| 67 extension_registry_observer_(this), | 66 extension_registry_observer_(this), |
| 68 weak_ptr_factory_(this) { | 67 weak_ptr_factory_(this) { |
| 69 // Use of g_browser_process requires that we are either on the UI thread, or | 68 // Use of g_browser_process requires that we are either on the UI thread, or |
| 70 // there are no threads initialized (such as in unit tests). | 69 // there are no threads initialized (such as in unit tests). |
| 71 DCHECK(!content::BrowserThread::IsThreadInitialized( | 70 DCHECK(!content::BrowserThread::IsThreadInitialized( |
| 72 content::BrowserThread::UI) || | 71 content::BrowserThread::UI) || |
| 73 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 72 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 74 | 73 |
| 75 extension_registry_observer_.Add( | 74 extension_registry_observer_.Add( |
| 76 extensions::ExtensionRegistry::Get(profile_)); | 75 extensions::ExtensionRegistry::Get(profile_)); |
| 77 // Wait for extensions to be ready before running OnceOffCreateShortcuts. | 76 // Wait for extensions to be ready before running |
| 77 // UpdateShortcutsForAllAppsIfNeeded. | |
| 78 extensions::ExtensionSystem::Get(profile)->ready().Post( | 78 extensions::ExtensionSystem::Get(profile)->ready().Post( |
| 79 FROM_HERE, | 79 FROM_HERE, |
| 80 base::Bind(&AppShortcutManager::OnceOffCreateShortcuts, | 80 base::Bind(&AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded, |
| 81 weak_ptr_factory_.GetWeakPtr())); | 81 weak_ptr_factory_.GetWeakPtr())); |
| 82 | 82 |
| 83 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 83 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 84 // profile_manager might be NULL in testing environments. | 84 // profile_manager might be NULL in testing environments. |
| 85 if (profile_manager) { | 85 if (profile_manager) { |
| 86 profile_manager->GetProfileInfoCache().AddObserver(this); | 86 profile_manager->GetProfileInfoCache().AddObserver(this); |
| 87 is_profile_info_cache_observer_ = true; | 87 is_profile_info_cache_observer_ = true; |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 | 90 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 105 const std::string& old_name) { | 105 const std::string& old_name) { |
| 106 if (!extension->is_app()) | 106 if (!extension->is_app()) |
| 107 return; | 107 return; |
| 108 | 108 |
| 109 // If the app is being updated, update any existing shortcuts but do not | 109 // If the app is being updated, update any existing shortcuts but do not |
| 110 // create new ones. If it is being installed, automatically create a | 110 // create new ones. If it is being installed, automatically create a |
| 111 // shortcut in the applications menu (e.g., Start Menu). | 111 // shortcut in the applications menu (e.g., Start Menu). |
| 112 if (is_update && !from_ephemeral) { | 112 if (is_update && !from_ephemeral) { |
| 113 web_app::UpdateAllShortcuts( | 113 web_app::UpdateAllShortcuts( |
| 114 base::UTF8ToUTF16(old_name), profile_, extension); | 114 base::UTF8ToUTF16(old_name), profile_, extension); |
| 115 } else if (ShouldCreateShortcutFor(profile_, extension)) { | 115 } else { |
| 116 CreateShortcutsInApplicationsMenu(profile_, extension); | 116 CreateShortcutsInApplicationsMenu(profile_, extension); |
| 117 } | 117 } |
| 118 } | 118 } |
| 119 | 119 |
| 120 void AppShortcutManager::OnExtensionUninstalled( | 120 void AppShortcutManager::OnExtensionUninstalled( |
| 121 content::BrowserContext* browser_context, | 121 content::BrowserContext* browser_context, |
| 122 const Extension* extension) { | 122 const Extension* extension) { |
| 123 web_app::DeleteAllShortcuts(profile_, extension); | 123 web_app::DeleteAllShortcuts(profile_, extension); |
| 124 } | 124 } |
| 125 | 125 |
| 126 void AppShortcutManager::OnProfileWillBeRemoved( | 126 void AppShortcutManager::OnProfileWillBeRemoved( |
| 127 const base::FilePath& profile_path) { | 127 const base::FilePath& profile_path) { |
| 128 if (profile_path != profile_->GetPath()) | 128 if (profile_path != profile_->GetPath()) |
| 129 return; | 129 return; |
| 130 content::BrowserThread::PostTask( | 130 content::BrowserThread::PostTask( |
| 131 content::BrowserThread::FILE, FROM_HERE, | 131 content::BrowserThread::FILE, FROM_HERE, |
| 132 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile, | 132 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile, |
| 133 profile_path)); | 133 profile_path)); |
| 134 } | 134 } |
| 135 | 135 |
| 136 void AppShortcutManager::OnceOffCreateShortcuts() { | 136 void AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded() { |
| 137 if (prefs_->GetBoolean(prefs::kAppShortcutsHaveBeenCreated)) | 137 int last_version = prefs_->GetInteger(prefs::kAppShortcutsVersion); |
| 138 if (last_version >= kCurrentAppShortcutsVersion) | |
| 138 return; | 139 return; |
| 139 | 140 |
| 140 prefs_->SetBoolean(prefs::kAppShortcutsHaveBeenCreated, true); | 141 web_app::UpdateShortcutsForAllApps(profile_); |
| 141 | 142 prefs_->SetInteger(prefs::kAppShortcutsVersion, kCurrentAppShortcutsVersion); |
| 142 // Check if extension system/service are available. They might not be in | |
| 143 // tests. | |
| 144 extensions::ExtensionSystem* extension_system; | |
| 145 ExtensionServiceInterface* extension_service; | |
| 146 if (!(extension_system = extensions::ExtensionSystem::Get(profile_)) || | |
| 147 !(extension_service = extension_system->extension_service())) | |
| 148 return; | |
| 149 | |
| 150 // Create an applications menu shortcut for each app in this profile. | |
| 151 const extensions::ExtensionSet* apps = extension_service->extensions(); | |
| 152 for (extensions::ExtensionSet::const_iterator it = apps->begin(); | |
| 153 it != apps->end(); ++it) { | |
| 154 if (ShouldCreateShortcutFor(profile_, it->get())) | |
| 155 CreateShortcutsInApplicationsMenu(profile_, it->get()); | |
| 156 } | |
| 157 } | 143 } |
| OLD | NEW |