shortcut_manager.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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 "base/bind.h"
8#include "base/command_line.h"
9#include "base/compiler_specific.h"
10#include "base/prefs/pref_service.h"
11#include "base/strings/string16.h"
12#include "base/strings/utf_string_conversions.h"
13#include "chrome/browser/browser_process.h"
14#include "chrome/browser/extensions/extension_service.h"
15#include "chrome/browser/extensions/extension_ui_util.h"
16#include "chrome/browser/profiles/profile.h"
17#include "chrome/browser/profiles/profile_info_cache.h"
18#include "chrome/browser/profiles/profile_manager.h"
19#include "chrome/browser/shell_integration.h"
20#include "chrome/browser/web_applications/web_app.h"
21#include "chrome/common/chrome_switches.h"
22#include "chrome/common/pref_names.h"
23#include "components/pref_registry/pref_registry_syncable.h"
24#include "content/public/browser/browser_thread.h"
25#include "extensions/browser/extension_registry.h"
26#include "extensions/browser/extension_system.h"
27#include "extensions/common/extension_set.h"
28#include "extensions/common/one_shot_event.h"
29
30using extensions::Extension;
31
32namespace {
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.
37const int kCurrentAppShortcutsVersion = 0;
38
39// Delay in seconds before running UpdateShortcutsForAllApps.
40const int kUpdateShortcutsForAllAppsDelay = 10;
41
42// Creates a shortcut for an application in the applications menu, if there is
43// not already one present.
44void CreateShortcutsInApplicationsMenu(Profile* profile,
45                                       const Extension* app) {
46  web_app::ShortcutLocations creation_locations;
47  // Create the shortcut in the Chrome Apps subdir.
48  creation_locations.applications_menu_location =
49      web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS;
50  web_app::CreateShortcuts(
51      web_app::SHORTCUT_CREATION_AUTOMATED, creation_locations, profile, app);
52}
53
54void SetCurrentAppShortcutsVersion(PrefService* prefs) {
55  prefs->SetInteger(prefs::kAppShortcutsVersion, kCurrentAppShortcutsVersion);
56}
57
58}  // namespace
59
60// static
61void AppShortcutManager::RegisterProfilePrefs(
62    user_prefs::PrefRegistrySyncable* registry) {
63  // Indicates whether app shortcuts have been created.
64  registry->RegisterIntegerPref(
65      prefs::kAppShortcutsVersion, 0,
66      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
67}
68
69AppShortcutManager::AppShortcutManager(Profile* profile)
70    : profile_(profile),
71      is_profile_info_cache_observer_(false),
72      prefs_(profile->GetPrefs()),
73      extension_registry_observer_(this),
74      weak_ptr_factory_(this) {
75  // Use of g_browser_process requires that we are either on the UI thread, or
76  // there are no threads initialized (such as in unit tests).
77  DCHECK(!content::BrowserThread::IsThreadInitialized(
78             content::BrowserThread::UI) ||
79         content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
80
81  extension_registry_observer_.Add(
82      extensions::ExtensionRegistry::Get(profile_));
83  // Wait for extensions to be ready before running
84  // UpdateShortcutsForAllAppsIfNeeded.
85  extensions::ExtensionSystem::Get(profile)->ready().Post(
86      FROM_HERE,
87      base::Bind(&AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded,
88                 weak_ptr_factory_.GetWeakPtr()));
89
90  ProfileManager* profile_manager = g_browser_process->profile_manager();
91  // profile_manager might be NULL in testing environments.
92  if (profile_manager) {
93    profile_manager->GetProfileInfoCache().AddObserver(this);
94    is_profile_info_cache_observer_ = true;
95  }
96}
97
98AppShortcutManager::~AppShortcutManager() {
99  if (g_browser_process && is_profile_info_cache_observer_) {
100    ProfileManager* profile_manager = g_browser_process->profile_manager();
101    // profile_manager might be NULL in testing environments or during shutdown.
102    if (profile_manager)
103      profile_manager->GetProfileInfoCache().RemoveObserver(this);
104  }
105}
106
107void AppShortcutManager::OnExtensionWillBeInstalled(
108    content::BrowserContext* browser_context,
109    const Extension* extension,
110    bool is_update,
111    bool from_ephemeral,
112    const std::string& old_name) {
113  if (!extension->is_app())
114    return;
115
116  // If the app is being updated, update any existing shortcuts but do not
117  // create new ones. If it is being installed, automatically create a
118  // shortcut in the applications menu (e.g., Start Menu).
119  if (is_update && !from_ephemeral) {
120    web_app::UpdateAllShortcuts(
121        base::UTF8ToUTF16(old_name), profile_, extension);
122  } else {
123    CreateShortcutsInApplicationsMenu(profile_, extension);
124  }
125}
126
127void AppShortcutManager::OnExtensionUninstalled(
128    content::BrowserContext* browser_context,
129    const Extension* extension) {
130  web_app::DeleteAllShortcuts(profile_, extension);
131}
132
133void AppShortcutManager::OnProfileWillBeRemoved(
134    const base::FilePath& profile_path) {
135  if (profile_path != profile_->GetPath())
136    return;
137  content::BrowserThread::PostTask(
138      content::BrowserThread::FILE, FROM_HERE,
139      base::Bind(&web_app::internals::DeleteAllShortcutsForProfile,
140                 profile_path));
141}
142
143void AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded() {
144  int last_version = prefs_->GetInteger(prefs::kAppShortcutsVersion);
145  if (last_version >= kCurrentAppShortcutsVersion)
146    return;
147
148  content::BrowserThread::PostDelayedTask(
149      content::BrowserThread::UI,
150      FROM_HERE,
151      base::Bind(&web_app::UpdateShortcutsForAllApps,
152                 profile_,
153                 base::Bind(&SetCurrentAppShortcutsVersion, prefs_)),
154      base::TimeDelta::FromSeconds(kUpdateShortcutsForAllAppsDelay));
155}
156