OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/apps/shortcut_manager.h" | 5 #include "chrome/browser/apps/shortcut_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
15 #include "chrome/browser/extensions/extension_service.h" | |
16 #include "chrome/browser/extensions/extension_ui_util.h" | |
17 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/profiles/profile_info_cache.h" | 16 #include "chrome/browser/profiles/profile_info_cache.h" |
19 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
20 #include "chrome/browser/shell_integration.h" | 18 #include "chrome/browser/shell_integration.h" |
21 #include "chrome/browser/web_applications/web_app.h" | 19 #include "chrome/browser/web_applications/web_app.h" |
22 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
23 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
24 #include "components/pref_registry/pref_registry_syncable.h" | 22 #include "components/pref_registry/pref_registry_syncable.h" |
25 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
26 #include "content/public/browser/notification_details.h" | 24 #include "content/public/browser/notification_details.h" |
27 #include "content/public/browser/notification_source.h" | 25 #include "content/public/browser/notification_source.h" |
28 #include "extensions/browser/extension_system.h" | |
29 #include "extensions/common/extension_set.h" | |
30 | 26 |
31 using extensions::Extension; | 27 using extensions::Extension; |
32 | 28 |
33 namespace { | 29 namespace { |
34 | 30 |
31 // This version number is stored in local prefs to check whether app shortcuts | |
32 // need to be recreated. This might happen when we change various aspects of app | |
33 // shortcuts like command-line flags or associated icons, binaries, etc. | |
34 const int kCurrentAppShortcutsVersion = 1; | |
35 | |
35 // Creates a shortcut for an application in the applications menu, if there is | 36 // Creates a shortcut for an application in the applications menu, if there is |
36 // not already one present. | 37 // not already one present. |
37 void CreateShortcutsInApplicationsMenu(Profile* profile, | 38 void CreateShortcutsInApplicationsMenu(Profile* profile, |
38 const Extension* app) { | 39 const Extension* app) { |
39 web_app::ShortcutLocations creation_locations; | 40 web_app::ShortcutLocations creation_locations; |
40 // Create the shortcut in the Chrome Apps subdir. | 41 // Create the shortcut in the Chrome Apps subdir. |
41 creation_locations.applications_menu_location = | 42 creation_locations.applications_menu_location = |
42 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS; | 43 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS; |
43 web_app::CreateShortcuts( | 44 web_app::CreateShortcuts( |
44 web_app::SHORTCUT_CREATION_AUTOMATED, creation_locations, profile, app); | 45 web_app::SHORTCUT_CREATION_AUTOMATED, creation_locations, profile, app); |
45 } | 46 } |
46 | 47 |
47 bool ShouldCreateShortcutFor(Profile* profile, const Extension* extension) { | |
48 return extension->is_platform_app() && | |
49 extension->location() != extensions::Manifest::COMPONENT && | |
50 extensions::ui_util::ShouldDisplayInAppLauncher(extension, profile); | |
51 } | |
52 | |
53 } // namespace | 48 } // namespace |
54 | 49 |
55 // static | 50 // static |
56 void AppShortcutManager::RegisterProfilePrefs( | 51 void AppShortcutManager::RegisterProfilePrefs( |
57 user_prefs::PrefRegistrySyncable* registry) { | 52 user_prefs::PrefRegistrySyncable* registry) { |
58 // Indicates whether app shortcuts have been created. | 53 // Indicates whether app shortcuts have been created. |
59 registry->RegisterBooleanPref( | 54 registry->RegisterIntegerPref( |
60 prefs::kAppShortcutsHaveBeenCreated, false, | 55 prefs::kAppShortcutsVersion, 0, |
61 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 56 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
62 } | 57 } |
63 | 58 |
64 AppShortcutManager::AppShortcutManager(Profile* profile) | 59 AppShortcutManager::AppShortcutManager(Profile* profile) |
65 : profile_(profile), | 60 : profile_(profile), |
66 is_profile_info_cache_observer_(false), | 61 is_profile_info_cache_observer_(false), |
67 prefs_(profile->GetPrefs()) { | 62 prefs_(profile->GetPrefs()) { |
68 // Use of g_browser_process requires that we are either on the UI thread, or | 63 // Use of g_browser_process requires that we are either on the UI thread, or |
69 // there are no threads initialized (such as in unit tests). | 64 // there are no threads initialized (such as in unit tests). |
70 DCHECK(!content::BrowserThread::IsThreadInitialized( | 65 DCHECK(!content::BrowserThread::IsThreadInitialized( |
71 content::BrowserThread::UI) || | 66 content::BrowserThread::UI) || |
72 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 67 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
73 | 68 |
74 registrar_.Add(this, | 69 registrar_.Add(this, |
75 chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, | 70 chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, |
76 content::Source<Profile>(profile_)); | 71 content::Source<Profile>(profile_)); |
77 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 72 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
78 content::Source<Profile>(profile_)); | 73 content::Source<Profile>(profile_)); |
79 // Wait for extensions to be ready before running OnceOffCreateShortcuts. | 74 // Wait for extensions to be ready before running |
75 // UpdateShortcutsForAllAppsIfNeeded. | |
80 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, | 76 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, |
81 content::Source<Profile>(profile_)); | 77 content::Source<Profile>(profile_)); |
82 | 78 |
83 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 79 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
84 // profile_manager might be NULL in testing environments. | 80 // profile_manager might be NULL in testing environments. |
85 if (profile_manager) { | 81 if (profile_manager) { |
86 profile_manager->GetProfileInfoCache().AddObserver(this); | 82 profile_manager->GetProfileInfoCache().AddObserver(this); |
87 is_profile_info_cache_observer_ = true; | 83 is_profile_info_cache_observer_ = true; |
88 } | 84 } |
89 } | 85 } |
90 | 86 |
91 AppShortcutManager::~AppShortcutManager() { | 87 AppShortcutManager::~AppShortcutManager() { |
92 if (g_browser_process && is_profile_info_cache_observer_) { | 88 if (g_browser_process && is_profile_info_cache_observer_) { |
93 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 89 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
94 // profile_manager might be NULL in testing environments or during shutdown. | 90 // profile_manager might be NULL in testing environments or during shutdown. |
95 if (profile_manager) | 91 if (profile_manager) |
96 profile_manager->GetProfileInfoCache().RemoveObserver(this); | 92 profile_manager->GetProfileInfoCache().RemoveObserver(this); |
97 } | 93 } |
98 } | 94 } |
99 | 95 |
100 void AppShortcutManager::Observe(int type, | 96 void AppShortcutManager::Observe(int type, |
101 const content::NotificationSource& source, | 97 const content::NotificationSource& source, |
102 const content::NotificationDetails& details) { | 98 const content::NotificationDetails& details) { |
103 switch (type) { | 99 switch (type) { |
104 case chrome::NOTIFICATION_EXTENSIONS_READY: { | 100 case chrome::NOTIFICATION_EXTENSIONS_READY: { |
105 OnceOffCreateShortcuts(); | 101 UpdateShortcutsForAllAppsIfNeeded(); |
106 break; | 102 break; |
107 } | 103 } |
108 case chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED: { | 104 case chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED: { |
109 const extensions::InstalledExtensionInfo* installed_info = | 105 const extensions::InstalledExtensionInfo* installed_info = |
110 content::Details<const extensions::InstalledExtensionInfo>(details) | 106 content::Details<const extensions::InstalledExtensionInfo>(details) |
111 .ptr(); | 107 .ptr(); |
112 const Extension* extension = installed_info->extension; | 108 const Extension* extension = installed_info->extension; |
113 // If the app is being updated, update any existing shortcuts but do not | 109 // If the app is being updated, update any existing shortcuts but do not |
114 // create new ones. If it is being installed, automatically create a | 110 // create new ones. If it is being installed, automatically create a |
115 // shortcut in the applications menu (e.g., Start Menu). | 111 // shortcut in the applications menu (e.g., Start Menu). |
116 if (installed_info->is_update) { | 112 if (installed_info->is_update) { |
117 web_app::UpdateAllShortcuts( | 113 web_app::UpdateAllShortcuts( |
118 base::UTF8ToUTF16(installed_info->old_name), profile_, extension); | 114 base::UTF8ToUTF16(installed_info->old_name), profile_, extension); |
119 } else if (ShouldCreateShortcutFor(profile_, extension)) { | 115 } else { |
120 CreateShortcutsInApplicationsMenu(profile_, extension); | 116 CreateShortcutsInApplicationsMenu(profile_, extension); |
121 } | 117 } |
122 break; | 118 break; |
123 } | 119 } |
124 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { | 120 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { |
125 const Extension* extension = content::Details<const Extension>( | 121 const Extension* extension = content::Details<const Extension>( |
126 details).ptr(); | 122 details).ptr(); |
127 web_app::DeleteAllShortcuts(profile_, extension); | 123 web_app::DeleteAllShortcuts(profile_, extension); |
128 break; | 124 break; |
129 } | 125 } |
130 default: | 126 default: |
131 NOTREACHED(); | 127 NOTREACHED(); |
132 } | 128 } |
133 } | 129 } |
134 | 130 |
135 void AppShortcutManager::OnProfileWillBeRemoved( | 131 void AppShortcutManager::OnProfileWillBeRemoved( |
136 const base::FilePath& profile_path) { | 132 const base::FilePath& profile_path) { |
137 if (profile_path != profile_->GetPath()) | 133 if (profile_path != profile_->GetPath()) |
138 return; | 134 return; |
139 content::BrowserThread::PostTask( | 135 content::BrowserThread::PostTask( |
140 content::BrowserThread::FILE, FROM_HERE, | 136 content::BrowserThread::FILE, FROM_HERE, |
141 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile, | 137 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile, |
142 profile_path)); | 138 profile_path)); |
143 } | 139 } |
144 | 140 |
145 void AppShortcutManager::OnceOffCreateShortcuts() { | 141 void AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded() { |
146 if (prefs_->GetBoolean(prefs::kAppShortcutsHaveBeenCreated)) | 142 int last_version = prefs_->GetInteger(prefs::kAppShortcutsVersion); |
tapted
2014/05/29 08:56:40
Is this pref still used elsewhere? (delete it?)
tapted
2014/05/29 13:07:44
oops - I was looking for red deletey text :o. It's
| |
143 if (last_version >= kCurrentAppShortcutsVersion) | |
147 return; | 144 return; |
148 | 145 |
149 prefs_->SetBoolean(prefs::kAppShortcutsHaveBeenCreated, true); | 146 web_app::UpdateShortcutsForAllApps(profile_); |
150 | 147 prefs_->SetInteger(prefs::kAppShortcutsVersion, kCurrentAppShortcutsVersion); |
151 // Check if extension system/service are available. They might not be in | |
152 // tests. | |
153 extensions::ExtensionSystem* extension_system; | |
154 ExtensionServiceInterface* extension_service; | |
155 if (!(extension_system = extensions::ExtensionSystem::Get(profile_)) || | |
156 !(extension_service = extension_system->extension_service())) | |
157 return; | |
158 | |
159 // Create an applications menu shortcut for each app in this profile. | |
160 const extensions::ExtensionSet* apps = extension_service->extensions(); | |
161 for (extensions::ExtensionSet::const_iterator it = apps->begin(); | |
162 it != apps->end(); ++it) { | |
163 if (ShouldCreateShortcutFor(profile_, it->get())) | |
164 CreateShortcutsInApplicationsMenu(profile_, it->get()); | |
165 } | |
166 } | 148 } |
OLD | NEW |