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;
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
// not already one present.
void CreateShortcutsInApplicationsMenu(Profile* profile,
......@@ -43,10 +51,8 @@ void CreateShortcutsInApplicationsMenu(Profile* profile,
web_app::SHORTCUT_CREATION_AUTOMATED, creation_locations, profile, app);
}
bool ShouldCreateShortcutFor(Profile* profile, const Extension* extension) {
return extension->is_platform_app() &&
extension->location() != extensions::Manifest::COMPONENT &&
extensions::ui_util::ShouldDisplayInAppLauncher(extension, profile);
void SetCurrentAppShortcutsVersion(PrefService* prefs) {
prefs->SetInteger(prefs::kAppShortcutsVersion, kCurrentAppShortcutsVersion);
}
} // namespace
......@@ -55,8 +61,8 @@ bool ShouldCreateShortcutFor(Profile* profile, const Extension* extension) {
void AppShortcutManager::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
// Indicates whether app shortcuts have been created.
registry->RegisterBooleanPref(
prefs::kAppShortcutsHaveBeenCreated, false,
registry->RegisterIntegerPref(
prefs::kAppShortcutsVersion, 0,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}
......@@ -74,10 +80,11 @@ AppShortcutManager::AppShortcutManager(Profile* profile)
extension_registry_observer_.Add(
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(
FROM_HERE,
base::Bind(&AppShortcutManager::OnceOffCreateShortcuts,
base::Bind(&AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded,
weak_ptr_factory_.GetWeakPtr()));
ProfileManager* profile_manager = g_browser_process->profile_manager();
......@@ -112,7 +119,7 @@ void AppShortcutManager::OnExtensionWillBeInstalled(
if (is_update && !from_ephemeral) {
web_app::UpdateAllShortcuts(
base::UTF8ToUTF16(old_name), profile_, extension);
} else if (ShouldCreateShortcutFor(profile_, extension)) {
} else {
CreateShortcutsInApplicationsMenu(profile_, extension);
}
}
......@@ -133,25 +140,16 @@ void AppShortcutManager::OnProfileWillBeRemoved(
profile_path));
}
void AppShortcutManager::OnceOffCreateShortcuts() {
if (prefs_->GetBoolean(prefs::kAppShortcutsHaveBeenCreated))
void AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded() {
int last_version = prefs_->GetInteger(prefs::kAppShortcutsVersion);
if (last_version >= kCurrentAppShortcutsVersion)
return;
prefs_->SetBoolean(prefs::kAppShortcutsHaveBeenCreated, true);
// Check if extension system/service are available. They might not be in
// tests.
extensions::ExtensionSystem* extension_system;
ExtensionServiceInterface* extension_service;
if (!(extension_system = extensions::ExtensionSystem::Get(profile_)) ||
!(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());
}
content::BrowserThread::PostDelayedTask(
content::BrowserThread::UI,
FROM_HERE,
base::Bind(&web_app::UpdateShortcutsForAllApps,
profile_,
base::Bind(&SetCurrentAppShortcutsVersion, prefs_)),
base::TimeDelta::FromSeconds(kUpdateShortcutsForAllAppsDelay));
}
......@@ -34,9 +34,9 @@ class AppShortcutManager : public KeyedService,
virtual ~AppShortcutManager();
// Checks if kShortcutsEnabled is set in prefs. If not, this sets it and
// creates shortcuts for all apps.
void OnceOffCreateShortcuts();
// Updates all shortcuts if kAppShortcutsVersion in prefs is less than
// kCurrentAppShortcutsVersion.
void UpdateShortcutsForAllAppsIfNeeded();
// extensions::ExtensionRegistryObserver.
virtual void OnExtensionWillBeInstalled(
......
......@@ -13,8 +13,6 @@
#include "base/prefs/pref_service.h"
#include "base/strings/string16.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_shutdown.h"
#include "chrome/browser/profiles/profile_manager.h"
......@@ -347,11 +345,6 @@ void AppListServiceImpl::EnableAppList(Profile* initial_profile,
false),
base::TimeDelta::FromMinutes(kDiscoverabilityTimeoutMinutes));
}
AppShortcutManager* shortcut_manager =
AppShortcutManagerFactory::GetForProfile(initial_profile);
if (shortcut_manager)
shortcut_manager->OnceOffCreateShortcuts();
}
void AppListServiceImpl::InvalidatePendingProfileLoads() {
......
......@@ -12,6 +12,7 @@
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.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/tab_helper.h"
#include "chrome/browser/favicon/favicon_tab_helper.h"
......@@ -21,8 +22,10 @@
#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/manifest_handlers/icons_handler.h"
#include "grit/theme_resources.h"
#include "skia/ext/image_operations.h"
......@@ -292,6 +295,13 @@ void UpdateShortcutInfoAndIconForApp(const extensions::Extension* extension,
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,
const std::string& extension_id,
const GURL& url) {
......@@ -374,6 +384,9 @@ void CreateShortcuts(ShortcutCreationReason reason,
const extensions::Extension* app) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (!ShouldCreateShortcutFor(profile, app))
return;
GetInfoForApp(app,
profile,
base::Bind(&CreateShortcutsWithInfo, reason, locations));
......
......@@ -122,6 +122,10 @@ void UpdateShortcutInfoAndIconForApp(const extensions::Extension* extension,
Profile* profile,
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
// based on |extension_id|. If |extension_id| is empty then |url| is used
// to construct a unique ID.
......@@ -172,6 +176,11 @@ void UpdateAllShortcuts(const base::string16& old_app_title,
Profile* profile,
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.
bool IsValidUrl(const GURL& url);
......
......@@ -5,6 +5,12 @@
#include "chrome/browser/web_applications/web_app.h"
namespace web_app {
void UpdateShortcutsForAllApps(Profile* profile,
const base::Closure& callback) {
callback.Run();
}
namespace internals {
bool CreatePlatformShortcuts(
......
......@@ -5,6 +5,12 @@
#include "chrome/browser/web_applications/web_app.h"
namespace web_app {
void UpdateShortcutsForAllApps(Profile* profile,
const base::Closure& callback) {
callback.Run();
}
namespace internals {
bool CreatePlatformShortcuts(
......
......@@ -11,6 +11,11 @@
namespace web_app {
void UpdateShortcutsForAllApps(Profile* profile,
const base::Closure& callback) {
callback.Run();
}
namespace internals {
bool CreatePlatformShortcuts(
......
......@@ -24,7 +24,9 @@
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#import "chrome/browser/mac/dock.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/shell_integration.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
......@@ -32,6 +34,7 @@
#include "chrome/common/chrome_version_info.h"
#import "chrome/common/mac/app_mode_common.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/extension.h"
#include "grit/chrome_unscaled_resources.h"
#include "grit/chromium_strings.h"
......@@ -915,6 +918,27 @@ void CreateAppShortcutInfoLoaded(
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 {
bool CreatePlatformShortcuts(
......
......@@ -413,6 +413,11 @@ void UpdateRelaunchDetailsForApp(Profile* profile,
base::Bind(&OnShortcutInfoLoadedForSetRelaunchDetails, hwnd));
}
void UpdateShortcutsForAllApps(Profile* profile,
const base::Closure& callback) {
callback.Run();
}
namespace internals {
// 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";
const char kAppLaunchForMetroRestartProfile[] =
"apps.app_launch_for_metro_restart_profile";
// A boolean that indicates whether app shortcuts have been created.
// On a transition from false to true, shortcuts are created for all apps.
const char kAppShortcutsHaveBeenCreated[] = "apps.shortcuts_have_been_created";
// An integer that is incremented whenever changes are made to app shortcuts.
// Increasing this causes all app shortcuts to be recreated.
const char kAppShortcutsVersion[] = "apps.shortcuts_version";
// How often the bubble has been shown.
extern const char kModuleConflictBubbleShown[] = "module_conflict.bubble_shown";
......
......@@ -860,7 +860,7 @@ extern const char kShowAppLauncherPromo[];
extern const char kAppLaunchForMetroRestart[];
extern const char kAppLaunchForMetroRestartProfile[];
extern const char kAppShortcutsHaveBeenCreated[];
extern const char kAppShortcutsVersion[];
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