Commit 090e1ee7 authored by jackhou@chromium.org's avatar jackhou@chromium.org

Replace OnceOffCreateShortcuts with UpdateShortcutsForAllAppsIfNeeded.

The once-off creation logic is no longer needed as app
shortcuts have been enabled by default for a while.

This replaces kAppShortcutsHaveBeenCreated with
kAppShortcutsVersion and allows us to trigger a rebuild
of all app shortcuts. This is only implemented for Mac.

BUG=266725

Review URL: https://codereview.chromium.org/263403002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274498 0039d316-1c4b-4281-b951-d872f2087c98
parent fb4bbc19
...@@ -31,6 +31,14 @@ using extensions::Extension; ...@@ -31,6 +31,14 @@ using extensions::Extension;
namespace { namespace {
// This version number is stored in local prefs to check whether app shortcuts
// need to be recreated. This might happen when we change various aspects of app
// shortcuts like command-line flags or associated icons, binaries, etc.
const int kCurrentAppShortcutsVersion = 0;
// Delay in seconds before running UpdateShortcutsForAllApps.
const int kUpdateShortcutsForAllAppsDelay = 10;
// Creates a shortcut for an application in the applications menu, if there is // Creates a shortcut for an application in the applications menu, if there is
// not already one present. // not already one present.
void CreateShortcutsInApplicationsMenu(Profile* profile, void CreateShortcutsInApplicationsMenu(Profile* profile,
...@@ -43,10 +51,8 @@ void CreateShortcutsInApplicationsMenu(Profile* profile, ...@@ -43,10 +51,8 @@ void CreateShortcutsInApplicationsMenu(Profile* profile,
web_app::SHORTCUT_CREATION_AUTOMATED, creation_locations, profile, app); web_app::SHORTCUT_CREATION_AUTOMATED, creation_locations, profile, app);
} }
bool ShouldCreateShortcutFor(Profile* profile, const Extension* extension) { void SetCurrentAppShortcutsVersion(PrefService* prefs) {
return extension->is_platform_app() && prefs->SetInteger(prefs::kAppShortcutsVersion, kCurrentAppShortcutsVersion);
extension->location() != extensions::Manifest::COMPONENT &&
extensions::ui_util::ShouldDisplayInAppLauncher(extension, profile);
} }
} // namespace } // namespace
...@@ -55,8 +61,8 @@ bool ShouldCreateShortcutFor(Profile* profile, const Extension* extension) { ...@@ -55,8 +61,8 @@ bool ShouldCreateShortcutFor(Profile* profile, const Extension* extension) {
void AppShortcutManager::RegisterProfilePrefs( void AppShortcutManager::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) { user_prefs::PrefRegistrySyncable* registry) {
// Indicates whether app shortcuts have been created. // Indicates whether app shortcuts have been created.
registry->RegisterBooleanPref( registry->RegisterIntegerPref(
prefs::kAppShortcutsHaveBeenCreated, false, prefs::kAppShortcutsVersion, 0,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
} }
...@@ -74,10 +80,11 @@ AppShortcutManager::AppShortcutManager(Profile* profile) ...@@ -74,10 +80,11 @@ AppShortcutManager::AppShortcutManager(Profile* profile)
extension_registry_observer_.Add( extension_registry_observer_.Add(
extensions::ExtensionRegistry::Get(profile_)); extensions::ExtensionRegistry::Get(profile_));
// Wait for extensions to be ready before running OnceOffCreateShortcuts. // Wait for extensions to be ready before running
// UpdateShortcutsForAllAppsIfNeeded.
extensions::ExtensionSystem::Get(profile)->ready().Post( extensions::ExtensionSystem::Get(profile)->ready().Post(
FROM_HERE, FROM_HERE,
base::Bind(&AppShortcutManager::OnceOffCreateShortcuts, base::Bind(&AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
ProfileManager* profile_manager = g_browser_process->profile_manager(); ProfileManager* profile_manager = g_browser_process->profile_manager();
...@@ -112,7 +119,7 @@ void AppShortcutManager::OnExtensionWillBeInstalled( ...@@ -112,7 +119,7 @@ void AppShortcutManager::OnExtensionWillBeInstalled(
if (is_update && !from_ephemeral) { if (is_update && !from_ephemeral) {
web_app::UpdateAllShortcuts( web_app::UpdateAllShortcuts(
base::UTF8ToUTF16(old_name), profile_, extension); base::UTF8ToUTF16(old_name), profile_, extension);
} else if (ShouldCreateShortcutFor(profile_, extension)) { } else {
CreateShortcutsInApplicationsMenu(profile_, extension); CreateShortcutsInApplicationsMenu(profile_, extension);
} }
} }
...@@ -133,25 +140,16 @@ void AppShortcutManager::OnProfileWillBeRemoved( ...@@ -133,25 +140,16 @@ void AppShortcutManager::OnProfileWillBeRemoved(
profile_path)); profile_path));
} }
void AppShortcutManager::OnceOffCreateShortcuts() { void AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded() {
if (prefs_->GetBoolean(prefs::kAppShortcutsHaveBeenCreated)) int last_version = prefs_->GetInteger(prefs::kAppShortcutsVersion);
if (last_version >= kCurrentAppShortcutsVersion)
return; return;
prefs_->SetBoolean(prefs::kAppShortcutsHaveBeenCreated, true); content::BrowserThread::PostDelayedTask(
content::BrowserThread::UI,
// Check if extension system/service are available. They might not be in FROM_HERE,
// tests. base::Bind(&web_app::UpdateShortcutsForAllApps,
extensions::ExtensionSystem* extension_system; profile_,
ExtensionServiceInterface* extension_service; base::Bind(&SetCurrentAppShortcutsVersion, prefs_)),
if (!(extension_system = extensions::ExtensionSystem::Get(profile_)) || base::TimeDelta::FromSeconds(kUpdateShortcutsForAllAppsDelay));
!(extension_service = extension_system->extension_service()))
return;
// Create an applications menu shortcut for each app in this profile.
const extensions::ExtensionSet* apps = extension_service->extensions();
for (extensions::ExtensionSet::const_iterator it = apps->begin();
it != apps->end(); ++it) {
if (ShouldCreateShortcutFor(profile_, it->get()))
CreateShortcutsInApplicationsMenu(profile_, it->get());
}
} }
...@@ -34,9 +34,9 @@ class AppShortcutManager : public KeyedService, ...@@ -34,9 +34,9 @@ class AppShortcutManager : public KeyedService,
virtual ~AppShortcutManager(); virtual ~AppShortcutManager();
// Checks if kShortcutsEnabled is set in prefs. If not, this sets it and // Updates all shortcuts if kAppShortcutsVersion in prefs is less than
// creates shortcuts for all apps. // kCurrentAppShortcutsVersion.
void OnceOffCreateShortcuts(); void UpdateShortcutsForAllAppsIfNeeded();
// extensions::ExtensionRegistryObserver. // extensions::ExtensionRegistryObserver.
virtual void OnExtensionWillBeInstalled( virtual void OnExtensionWillBeInstalled(
......
...@@ -13,8 +13,6 @@ ...@@ -13,8 +13,6 @@
#include "base/prefs/pref_service.h" #include "base/prefs/pref_service.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/apps/shortcut_manager.h"
#include "chrome/browser/apps/shortcut_manager_factory.h"
#include "chrome/browser/browser_process.h" #include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_shutdown.h" #include "chrome/browser/browser_shutdown.h"
#include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_manager.h"
...@@ -347,11 +345,6 @@ void AppListServiceImpl::EnableAppList(Profile* initial_profile, ...@@ -347,11 +345,6 @@ void AppListServiceImpl::EnableAppList(Profile* initial_profile,
false), false),
base::TimeDelta::FromMinutes(kDiscoverabilityTimeoutMinutes)); base::TimeDelta::FromMinutes(kDiscoverabilityTimeoutMinutes));
} }
AppShortcutManager* shortcut_manager =
AppShortcutManagerFactory::GetForProfile(initial_profile);
if (shortcut_manager)
shortcut_manager->OnceOffCreateShortcuts();
} }
void AppListServiceImpl::InvalidatePendingProfileLoads() { void AppListServiceImpl::InvalidatePendingProfileLoads() {
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/threading/thread.h" #include "base/threading/thread.h"
#include "chrome/browser/extensions/extension_ui_util.h"
#include "chrome/browser/extensions/image_loader.h" #include "chrome/browser/extensions/image_loader.h"
#include "chrome/browser/extensions/tab_helper.h" #include "chrome/browser/extensions/tab_helper.h"
#include "chrome/browser/favicon/favicon_tab_helper.h" #include "chrome/browser/favicon/favicon_tab_helper.h"
...@@ -21,8 +22,10 @@ ...@@ -21,8 +22,10 @@
#include "chrome/common/extensions/manifest_handlers/app_launch_info.h" #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/constants.h" #include "extensions/common/constants.h"
#include "extensions/common/extension.h" #include "extensions/common/extension.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/manifest_handlers/icons_handler.h" #include "extensions/common/manifest_handlers/icons_handler.h"
#include "grit/theme_resources.h" #include "grit/theme_resources.h"
#include "skia/ext/image_operations.h" #include "skia/ext/image_operations.h"
...@@ -292,6 +295,13 @@ void UpdateShortcutInfoAndIconForApp(const extensions::Extension* extension, ...@@ -292,6 +295,13 @@ void UpdateShortcutInfoAndIconForApp(const extensions::Extension* extension,
base::Bind(&IgnoreFileHandlersInfo, callback)); base::Bind(&IgnoreFileHandlersInfo, callback));
} }
bool ShouldCreateShortcutFor(Profile* profile,
const extensions::Extension* extension) {
return extension->is_platform_app() &&
extension->location() != extensions::Manifest::COMPONENT &&
extensions::ui_util::ShouldDisplayInAppLauncher(extension, profile);
}
base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path, base::FilePath GetWebAppDataDirectory(const base::FilePath& profile_path,
const std::string& extension_id, const std::string& extension_id,
const GURL& url) { const GURL& url) {
...@@ -374,6 +384,9 @@ void CreateShortcuts(ShortcutCreationReason reason, ...@@ -374,6 +384,9 @@ void CreateShortcuts(ShortcutCreationReason reason,
const extensions::Extension* app) { const extensions::Extension* app) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!ShouldCreateShortcutFor(profile, app))
return;
GetInfoForApp(app, GetInfoForApp(app,
profile, profile,
base::Bind(&CreateShortcutsWithInfo, reason, locations)); base::Bind(&CreateShortcutsWithInfo, reason, locations));
......
...@@ -122,6 +122,10 @@ void UpdateShortcutInfoAndIconForApp(const extensions::Extension* extension, ...@@ -122,6 +122,10 @@ void UpdateShortcutInfoAndIconForApp(const extensions::Extension* extension,
Profile* profile, Profile* profile,
const ShortcutInfoCallback& callback); const ShortcutInfoCallback& callback);
// Whether to create a shortcut for this type of extension.
bool ShouldCreateShortcutFor(Profile* profile,
const extensions::Extension* extension);
// Gets the user data directory for given web app. The path for the directory is // Gets the user data directory for given web app. The path for the directory is
// based on |extension_id|. If |extension_id| is empty then |url| is used // based on |extension_id|. If |extension_id| is empty then |url| is used
// to construct a unique ID. // to construct a unique ID.
...@@ -172,6 +176,11 @@ void UpdateAllShortcuts(const base::string16& old_app_title, ...@@ -172,6 +176,11 @@ void UpdateAllShortcuts(const base::string16& old_app_title,
Profile* profile, Profile* profile,
const extensions::Extension* app); const extensions::Extension* app);
// Updates shortcuts for all apps in this profile. This is expected to be called
// on the UI thread.
void UpdateShortcutsForAllApps(Profile* profile,
const base::Closure& callback);
// Returns true if given url is a valid web app url. // Returns true if given url is a valid web app url.
bool IsValidUrl(const GURL& url); bool IsValidUrl(const GURL& url);
......
...@@ -5,6 +5,12 @@ ...@@ -5,6 +5,12 @@
#include "chrome/browser/web_applications/web_app.h" #include "chrome/browser/web_applications/web_app.h"
namespace web_app { namespace web_app {
void UpdateShortcutsForAllApps(Profile* profile,
const base::Closure& callback) {
callback.Run();
}
namespace internals { namespace internals {
bool CreatePlatformShortcuts( bool CreatePlatformShortcuts(
......
...@@ -5,6 +5,12 @@ ...@@ -5,6 +5,12 @@
#include "chrome/browser/web_applications/web_app.h" #include "chrome/browser/web_applications/web_app.h"
namespace web_app { namespace web_app {
void UpdateShortcutsForAllApps(Profile* profile,
const base::Closure& callback) {
callback.Run();
}
namespace internals { namespace internals {
bool CreatePlatformShortcuts( bool CreatePlatformShortcuts(
......
...@@ -11,6 +11,11 @@ ...@@ -11,6 +11,11 @@
namespace web_app { namespace web_app {
void UpdateShortcutsForAllApps(Profile* profile,
const base::Closure& callback) {
callback.Run();
}
namespace internals { namespace internals {
bool CreatePlatformShortcuts( bool CreatePlatformShortcuts(
......
...@@ -24,7 +24,9 @@ ...@@ -24,7 +24,9 @@
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#import "chrome/browser/mac/dock.h" #import "chrome/browser/mac/dock.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/shell_integration.h" #include "chrome/browser/shell_integration.h"
#include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_paths.h"
...@@ -32,6 +34,7 @@ ...@@ -32,6 +34,7 @@
#include "chrome/common/chrome_version_info.h" #include "chrome/common/chrome_version_info.h"
#import "chrome/common/mac/app_mode_common.h" #import "chrome/common/mac/app_mode_common.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h" #include "extensions/common/extension.h"
#include "grit/chrome_unscaled_resources.h" #include "grit/chrome_unscaled_resources.h"
#include "grit/chromium_strings.h" #include "grit/chromium_strings.h"
...@@ -915,6 +918,27 @@ void CreateAppShortcutInfoLoaded( ...@@ -915,6 +918,27 @@ void CreateAppShortcutInfoLoaded(
close_callback.Run(dialog_accepted); close_callback.Run(dialog_accepted);
} }
void UpdateShortcutsForAllApps(Profile* profile,
const base::Closure& callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
extensions::ExtensionRegistry* registry =
extensions::ExtensionRegistry::Get(profile);
if (!registry)
return;
// Update all apps.
scoped_ptr<extensions::ExtensionSet> everything =
registry->GenerateInstalledExtensionsSet();
for (extensions::ExtensionSet::const_iterator it = everything->begin();
it != everything->end(); ++it) {
if (web_app::ShouldCreateShortcutFor(profile, it->get()))
web_app::UpdateAllShortcuts(base::string16(), profile, it->get());
}
callback.Run();
}
namespace internals { namespace internals {
bool CreatePlatformShortcuts( bool CreatePlatformShortcuts(
......
...@@ -413,6 +413,11 @@ void UpdateRelaunchDetailsForApp(Profile* profile, ...@@ -413,6 +413,11 @@ void UpdateRelaunchDetailsForApp(Profile* profile,
base::Bind(&OnShortcutInfoLoadedForSetRelaunchDetails, hwnd)); base::Bind(&OnShortcutInfoLoadedForSetRelaunchDetails, hwnd));
} }
void UpdateShortcutsForAllApps(Profile* profile,
const base::Closure& callback) {
callback.Run();
}
namespace internals { namespace internals {
// Saves |image| to |icon_file| if the file is outdated and refresh shell's // Saves |image| to |icon_file| if the file is outdated and refresh shell's
......
...@@ -2480,9 +2480,9 @@ const char kAppLaunchForMetroRestart[] = "apps.app_launch_for_metro_restart"; ...@@ -2480,9 +2480,9 @@ const char kAppLaunchForMetroRestart[] = "apps.app_launch_for_metro_restart";
const char kAppLaunchForMetroRestartProfile[] = const char kAppLaunchForMetroRestartProfile[] =
"apps.app_launch_for_metro_restart_profile"; "apps.app_launch_for_metro_restart_profile";
// A boolean that indicates whether app shortcuts have been created. // An integer that is incremented whenever changes are made to app shortcuts.
// On a transition from false to true, shortcuts are created for all apps. // Increasing this causes all app shortcuts to be recreated.
const char kAppShortcutsHaveBeenCreated[] = "apps.shortcuts_have_been_created"; const char kAppShortcutsVersion[] = "apps.shortcuts_version";
// How often the bubble has been shown. // How often the bubble has been shown.
extern const char kModuleConflictBubbleShown[] = "module_conflict.bubble_shown"; extern const char kModuleConflictBubbleShown[] = "module_conflict.bubble_shown";
......
...@@ -860,7 +860,7 @@ extern const char kShowAppLauncherPromo[]; ...@@ -860,7 +860,7 @@ extern const char kShowAppLauncherPromo[];
extern const char kAppLaunchForMetroRestart[]; extern const char kAppLaunchForMetroRestart[];
extern const char kAppLaunchForMetroRestartProfile[]; extern const char kAppLaunchForMetroRestartProfile[];
extern const char kAppShortcutsHaveBeenCreated[]; extern const char kAppShortcutsVersion[];
extern const char kModuleConflictBubbleShown[]; extern const char kModuleConflictBubbleShown[];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment