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"
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
;
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;
43 const int kCurrentAppShortcutsVersion
= 0;
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
;
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
);
75 void AppShortcutManager::RegisterProfilePrefs(
76 user_prefs::PrefRegistrySyncable
* registry
) {
77 // Indicates whether app shortcuts have been created.
78 registry
->RegisterIntegerPref(
79 prefs::kAppShortcutsVersion
, 0,
80 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
83 AppShortcutManager::AppShortcutManager(Profile
* profile
)
85 is_profile_info_cache_observer_(false),
86 prefs_(profile
->GetPrefs()),
87 extension_registry_observer_(this),
88 weak_ptr_factory_(this) {
89 // Use of g_browser_process requires that we are either on the UI thread, or
90 // there are no threads initialized (such as in unit tests).
91 DCHECK(!content::BrowserThread::IsThreadInitialized(
92 content::BrowserThread::UI
) ||
93 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
95 extension_registry_observer_
.Add(
96 extensions::ExtensionRegistry::Get(profile_
));
97 // Wait for extensions to be ready before running
98 // UpdateShortcutsForAllAppsIfNeeded.
99 extensions::ExtensionSystem::Get(profile
)->ready().Post(
101 base::Bind(&AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded
,
102 weak_ptr_factory_
.GetWeakPtr()));
104 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
105 // profile_manager might be NULL in testing environments.
106 if (profile_manager
) {
107 profile_manager
->GetProfileInfoCache().AddObserver(this);
108 is_profile_info_cache_observer_
= true;
112 AppShortcutManager::~AppShortcutManager() {
113 if (g_browser_process
&& is_profile_info_cache_observer_
) {
114 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
115 // profile_manager might be NULL in testing environments or during shutdown.
117 profile_manager
->GetProfileInfoCache().RemoveObserver(this);
121 void AppShortcutManager::OnExtensionWillBeInstalled(
122 content::BrowserContext
* browser_context
,
123 const Extension
* extension
,
126 const std::string
& old_name
) {
127 if (!extension
->is_app())
130 // If the app is being updated, update any existing shortcuts but do not
131 // create new ones. If it is being installed, automatically create a
132 // shortcut in the applications menu (e.g., Start Menu).
133 if (is_update
&& !from_ephemeral
) {
134 web_app::UpdateAllShortcuts(
135 base::UTF8ToUTF16(old_name
), profile_
, extension
);
137 CreateShortcutsForApp(profile_
, extension
);
141 void AppShortcutManager::OnExtensionUninstalled(
142 content::BrowserContext
* browser_context
,
143 const Extension
* extension
,
144 extensions::UninstallReason reason
) {
145 web_app::DeleteAllShortcuts(profile_
, extension
);
148 void AppShortcutManager::OnProfileWillBeRemoved(
149 const base::FilePath
& profile_path
) {
150 if (profile_path
!= profile_
->GetPath())
152 content::BrowserThread::PostTask(
153 content::BrowserThread::FILE, FROM_HERE
,
154 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile
,
158 void AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded() {
159 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType
))
162 int last_version
= prefs_
->GetInteger(prefs::kAppShortcutsVersion
);
163 if (last_version
>= kCurrentAppShortcutsVersion
)
166 content::BrowserThread::PostDelayedTask(
167 content::BrowserThread::UI
,
169 base::Bind(&web_app::UpdateShortcutsForAllApps
,
171 base::Bind(&SetCurrentAppShortcutsVersion
, prefs_
)),
172 base::TimeDelta::FromSeconds(kUpdateShortcutsForAllAppsDelay
));