Skip to content

Commit

Permalink
When app shortcuts are enabled, do a once-off creation of all shortcuts.
Browse files Browse the repository at this point in the history
This adds a boolean (kAppShortcutsEnabled) to user prefs. At startup,
ShortcutManager checks the pref and sets the current value. On a
transition from false to true, ShortcutManager iterates over all
apps and creates shortcuts as if the app is being installed.

BUG=249189
TEST=Delete app shortcuts from the OS applications folder.
Start Chrome, the shortcuts should be created.
Delete the shortcuts again.
Restart Chrome, the shortcuts are not created a second time.

Review URL: https://chromiumcodereview.appspot.com/17261016

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@207832 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
jackhou@chromium.org committed Jun 21, 2013
1 parent 36d7746 commit 819cd78
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 3 deletions.
1 change: 1 addition & 0 deletions apps/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ include_rules = [
"+base",
"+content",
"+components/browser_context_keyed_service",
"+components/user_prefs/pref_registry_syncable.h",
"+ui",
"+win8",
# Temporary allowed includes.
Expand Down
4 changes: 4 additions & 0 deletions apps/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ const char kAppLauncherHasBeenEnabled[] =
const char kAppLauncherIsEnabled[] =
"apps.app_launcher.should_show_apps_page";

// 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 kShortcutsHaveBeenCreated[] = "apps.shortcuts_have_been_created";

// A boolean identifying if we should show the app launcher promo or not.
const char kShowAppLauncherPromo[] = "app_launcher.show_promo";

Expand Down
1 change: 1 addition & 0 deletions apps/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extern const char kAppLaunchForMetroRestartProfile[];
extern const char kAppLaunchForMetroRestart[];
extern const char kAppLauncherHasBeenEnabled[];
extern const char kAppLauncherIsEnabled[];
extern const char kShortcutsHaveBeenCreated[];
extern const char kShowAppLauncherPromo[];

} // namespace prefs
Expand Down
8 changes: 8 additions & 0 deletions apps/prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "apps/app_launcher.h"
#include "apps/pref_names.h"
#include "base/prefs/pref_registry_simple.h"
#include "components/user_prefs/pref_registry_syncable.h"

namespace apps {

Expand All @@ -32,4 +33,11 @@ void RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(prefs::kShowAppLauncherPromo, true);
}

void RegisterUserPrefs(user_prefs::PrefRegistrySyncable* registry) {
// Indicates whether app shortcuts have been created.
registry->RegisterBooleanPref(
prefs::kShortcutsHaveBeenCreated, false,
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
}

} // namespace apps
7 changes: 7 additions & 0 deletions apps/prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@

class PrefRegistrySimple;

namespace user_prefs {
class PrefRegistrySyncable;
}

namespace apps {

// Register preferences for the apps system.
void RegisterPrefs(PrefRegistrySimple* registry);

// Register per-profile preferences for the apps system.
void RegisterUserPrefs(user_prefs::PrefRegistrySyncable* registry);

} // namespace apps

#endif // APPS_PREFS_H_
61 changes: 58 additions & 3 deletions apps/shortcut_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@

#include "apps/shortcut_manager.h"

#include "apps/pref_names.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/shell_integration.h"
#include "chrome/browser/ui/web_applications/web_app_ui.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_set.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"

Expand All @@ -32,17 +38,27 @@ void CreateShortcutsInApplicationsMenu(
web_app::CreateShortcuts(shortcut_info, creation_locations);
}

bool ShouldCreateShortcutFor(const extensions::Extension* extension) {
return extension->is_platform_app() &&
extension->location() != extensions::Manifest::COMPONENT &&
extension->ShouldDisplayInAppLauncher();
}

} // namespace

namespace apps {

ShortcutManager::ShortcutManager(Profile* profile)
: profile_(profile),
prefs_(profile->GetPrefs()),
weak_factory_(this) {
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
content::Source<Profile>(profile_));
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
content::Source<Profile>(profile_));
// Wait for extensions to be ready before running OnceOffCreateShortcuts.
registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
content::Source<Profile>(profile_));
}

ShortcutManager::~ShortcutManager() {}
Expand All @@ -51,6 +67,10 @@ void ShortcutManager::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_EXTENSIONS_READY: {
OnceOffCreateShortcuts();
break;
}
case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
#if defined(OS_MACOSX)
if (!CommandLine::ForCurrentProcess()->
Expand All @@ -62,9 +82,7 @@ void ShortcutManager::Observe(int type,
content::Details<const extensions::InstalledExtensionInfo>(details)
.ptr();
const Extension* extension = installed_info->extension;
if (extension->is_platform_app() &&
extension->location() != extensions::Manifest::COMPONENT &&
extension->ShouldDisplayInAppLauncher()) {
if (ShouldCreateShortcutFor(extension)) {
// If the app is being updated, update any existing shortcuts but do not
// create new ones. If it is being installed, automatically create a
// shortcut in the applications menu (e.g., Start Menu).
Expand Down Expand Up @@ -94,6 +112,43 @@ void ShortcutManager::Observe(int type,
}
}

void ShortcutManager::OnceOffCreateShortcuts() {
bool was_enabled = prefs_->GetBoolean(apps::prefs::kShortcutsHaveBeenCreated);

// Creation of shortcuts on Mac currently sits behind --enable-app-shims.
// Until it is enabled permanently, we need to check the flag, and set the
// pref accordingly.
#if defined(OS_MACOSX)
bool is_now_enabled =
CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableAppShims);
#else
bool is_now_enabled = true;
#endif // defined(OS_MACOSX)

if (was_enabled != is_now_enabled)
prefs_->SetBoolean(apps::prefs::kShortcutsHaveBeenCreated, is_now_enabled);

if (was_enabled || !is_now_enabled)
return;

// 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 ExtensionSet* apps = extension_service->extensions();
for (ExtensionSet::const_iterator it = apps->begin();
it != apps->end(); ++it) {
if (ShouldCreateShortcutFor(*it))
web_app::UpdateShortcutInfoAndIconForApp(
*(*it), profile_, base::Bind(&CreateShortcutsInApplicationsMenu));
}
}

void ShortcutManager::DeleteApplicationShortcuts(
const Extension* extension) {
ShellIntegration::ShortcutInfo delete_info =
Expand Down
6 changes: 6 additions & 0 deletions apps/shortcut_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"

class PrefService;
class Profile;

namespace apps {
Expand All @@ -29,10 +30,15 @@ class ShortcutManager : public BrowserContextKeyedService,
const content::NotificationDetails& details) OVERRIDE;

private:
// Checks if kShortcutsEnabled is set in prefs. If not, this sets it and
// creates shortcuts for all apps.
void OnceOffCreateShortcuts();

void DeleteApplicationShortcuts(const extensions::Extension* extension);

content::NotificationRegistrar registrar_;
Profile* profile_;
PrefService* prefs_;

// Fields used when installing application shortcuts.
base::WeakPtrFactory<ShortcutManager> weak_factory_;
Expand Down
1 change: 1 addition & 0 deletions chrome/browser/prefs/browser_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ void RegisterUserPrefs(user_prefs::PrefRegistrySyncable* registry) {
TRACE_EVENT0("browser", "chrome::RegisterUserPrefs");
// User prefs. Please keep this list alphabetized.
AlternateErrorPageTabObserver::RegisterUserPrefs(registry);
apps::RegisterUserPrefs(registry);
autofill::AutofillDialogControllerImpl::RegisterUserPrefs(registry);
autofill::AutofillManager::RegisterUserPrefs(registry);
BookmarkPromptPrefs::RegisterUserPrefs(registry);
Expand Down

0 comments on commit 819cd78

Please sign in to comment.