Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / apps / shortcut_manager.cc
blob819df6168040c392b256a92fe4a36baab448d3f8
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.
5 #include "chrome/browser/apps/shortcut_manager.h"
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/browser/extension_util.h"
28 #include "extensions/common/extension_set.h"
29 #include "extensions/common/one_shot_event.h"
31 using extensions::Extension;
33 namespace {
35 // This version number is stored in local prefs to check whether app shortcuts
36 // need to be recreated. This might happen when we change various aspects of app
37 // shortcuts like command-line flags or associated icons, binaries, etc.
38 #if defined(OS_MACOSX)
39 // This was changed to 3 at r316520, but reverted again. Next time we need to
40 // trigger a recreate, increment this to 4.
41 const int kCurrentAppShortcutsVersion = 2;
42 #else
43 const int kCurrentAppShortcutsVersion = 0;
44 #endif
46 // Delay in seconds before running UpdateShortcutsForAllApps.
47 const int kUpdateShortcutsForAllAppsDelay = 10;
49 void CreateShortcutsForApp(Profile* profile, const Extension* app) {
50 web_app::ShortcutLocations creation_locations;
52 if (extensions::util::IsEphemeralApp(app->id(), profile)) {
53 // Ephemeral apps should not have visible shortcuts, but may still require
54 // platform-specific handling.
55 creation_locations.applications_menu_location =
56 web_app::APP_MENU_LOCATION_HIDDEN;
57 } else {
58 // Creates a shortcut for an app in the Chrome Apps subdir of the
59 // applications menu, if there is not already one present.
60 creation_locations.applications_menu_location =
61 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS;
64 web_app::CreateShortcuts(
65 web_app::SHORTCUT_CREATION_AUTOMATED, creation_locations, profile, app);
68 void SetCurrentAppShortcutsVersion(PrefService* prefs) {
69 prefs->SetInteger(prefs::kAppShortcutsVersion, kCurrentAppShortcutsVersion);
72 } // namespace
74 // static
75 void AppShortcutManager::RegisterProfilePrefs(
76 user_prefs::PrefRegistrySyncable* registry) {
77 // Indicates whether app shortcuts have been created.
78 registry->RegisterIntegerPref(prefs::kAppShortcutsVersion, 0);
81 AppShortcutManager::AppShortcutManager(Profile* profile)
82 : profile_(profile),
83 is_profile_info_cache_observer_(false),
84 prefs_(profile->GetPrefs()),
85 extension_registry_observer_(this),
86 weak_ptr_factory_(this) {
87 // Use of g_browser_process requires that we are either on the UI thread, or
88 // there are no threads initialized (such as in unit tests).
89 DCHECK(!content::BrowserThread::IsThreadInitialized(
90 content::BrowserThread::UI) ||
91 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
93 extension_registry_observer_.Add(
94 extensions::ExtensionRegistry::Get(profile_));
95 // Wait for extensions to be ready before running
96 // UpdateShortcutsForAllAppsIfNeeded.
97 extensions::ExtensionSystem::Get(profile)->ready().Post(
98 FROM_HERE,
99 base::Bind(&AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded,
100 weak_ptr_factory_.GetWeakPtr()));
102 ProfileManager* profile_manager = g_browser_process->profile_manager();
103 // profile_manager might be NULL in testing environments.
104 if (profile_manager) {
105 profile_manager->GetProfileInfoCache().AddObserver(this);
106 is_profile_info_cache_observer_ = true;
110 AppShortcutManager::~AppShortcutManager() {
111 if (g_browser_process && is_profile_info_cache_observer_) {
112 ProfileManager* profile_manager = g_browser_process->profile_manager();
113 // profile_manager might be NULL in testing environments or during shutdown.
114 if (profile_manager)
115 profile_manager->GetProfileInfoCache().RemoveObserver(this);
119 void AppShortcutManager::OnExtensionWillBeInstalled(
120 content::BrowserContext* browser_context,
121 const Extension* extension,
122 bool is_update,
123 bool from_ephemeral,
124 const std::string& old_name) {
125 if (!extension->is_app())
126 return;
128 // If the app is being updated, update any existing shortcuts but do not
129 // create new ones. If it is being installed, automatically create a
130 // shortcut in the applications menu (e.g., Start Menu).
131 if (is_update && !from_ephemeral) {
132 web_app::UpdateAllShortcuts(
133 base::UTF8ToUTF16(old_name), profile_, extension);
134 } else {
135 CreateShortcutsForApp(profile_, extension);
139 void AppShortcutManager::OnExtensionUninstalled(
140 content::BrowserContext* browser_context,
141 const Extension* extension,
142 extensions::UninstallReason reason) {
143 web_app::DeleteAllShortcuts(profile_, extension);
146 void AppShortcutManager::OnProfileWillBeRemoved(
147 const base::FilePath& profile_path) {
148 if (profile_path != profile_->GetPath())
149 return;
150 content::BrowserThread::PostTask(
151 content::BrowserThread::FILE, FROM_HERE,
152 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile,
153 profile_path));
156 void AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded() {
157 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType))
158 return;
160 int last_version = prefs_->GetInteger(prefs::kAppShortcutsVersion);
161 if (last_version >= kCurrentAppShortcutsVersion)
162 return;
164 content::BrowserThread::PostDelayedTask(
165 content::BrowserThread::UI,
166 FROM_HERE,
167 base::Bind(&web_app::UpdateShortcutsForAllApps,
168 profile_,
169 base::Bind(&SetCurrentAppShortcutsVersion, prefs_)),
170 base::TimeDelta::FromSeconds(kUpdateShortcutsForAllAppsDelay));