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/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/profiles/profile_info_cache.h" | 16 #include "chrome/browser/profiles/profile_info_cache.h" |
18 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
19 #include "chrome/browser/shell_integration.h" | 18 #include "chrome/browser/shell_integration.h" |
20 #include "chrome/browser/web_applications/web_app.h" | 19 #include "chrome/browser/web_applications/web_app.h" |
21 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
22 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
23 #include "components/pref_registry/pref_registry_syncable.h" | 22 #include "components/pref_registry/pref_registry_syncable.h" |
24 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
25 #include "content/public/browser/notification_details.h" | 24 #include "content/public/browser/notification_details.h" |
26 #include "content/public/browser/notification_source.h" | 25 #include "content/public/browser/notification_source.h" |
27 #include "extensions/browser/extension_system.h" | |
28 #include "extensions/common/extension_set.h" | |
29 | 26 |
30 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) |
31 #include "apps/app_shim/app_shim_mac.h" | 28 #include "apps/app_shim/app_shim_mac.h" |
32 #endif | 29 #endif |
33 | 30 |
34 using extensions::Extension; | 31 using extensions::Extension; |
35 | 32 |
36 namespace { | 33 namespace { |
37 | 34 |
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. | |
Matt Giuca
2014/05/23 07:01:57
Nit: command-line
jackhou1
2014/05/26 07:57:37
Done.
| |
38 const int kCurrentAppShortcutsVersion = 1; | |
39 | |
38 // Creates a shortcut for an application in the applications menu, if there is | 40 // Creates a shortcut for an application in the applications menu, if there is |
39 // not already one present. | 41 // not already one present. |
40 void CreateShortcutsInApplicationsMenu(Profile* profile, | 42 void CreateShortcutsInApplicationsMenu(Profile* profile, |
41 const Extension* app) { | 43 const Extension* app) { |
42 web_app::ShortcutLocations creation_locations; | 44 web_app::ShortcutLocations creation_locations; |
43 // Create the shortcut in the Chrome Apps subdir. | 45 // Create the shortcut in the Chrome Apps subdir. |
44 creation_locations.applications_menu_location = | 46 creation_locations.applications_menu_location = |
45 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS; | 47 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS; |
46 web_app::CreateShortcuts( | 48 web_app::CreateShortcuts( |
47 web_app::SHORTCUT_CREATION_AUTOMATED, creation_locations, profile, app); | 49 web_app::SHORTCUT_CREATION_AUTOMATED, creation_locations, profile, app); |
48 } | 50 } |
49 | 51 |
50 bool ShouldCreateShortcutFor(const Extension* extension) { | |
51 return extension->is_platform_app() && | |
52 extension->location() != extensions::Manifest::COMPONENT && | |
53 extension->ShouldDisplayInAppLauncher(); | |
54 } | |
55 | |
56 } // namespace | 52 } // namespace |
57 | 53 |
58 // static | 54 // static |
59 void AppShortcutManager::RegisterProfilePrefs( | 55 void AppShortcutManager::RegisterProfilePrefs( |
60 user_prefs::PrefRegistrySyncable* registry) { | 56 user_prefs::PrefRegistrySyncable* registry) { |
61 // Indicates whether app shortcuts have been created. | 57 // Indicates whether app shortcuts have been created. |
62 registry->RegisterBooleanPref( | 58 registry->RegisterIntegerPref( |
63 prefs::kAppShortcutsHaveBeenCreated, false, | 59 prefs::kAppShortcutsVersion, 0, |
64 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 60 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
65 } | 61 } |
66 | 62 |
67 AppShortcutManager::AppShortcutManager(Profile* profile) | 63 AppShortcutManager::AppShortcutManager(Profile* profile) |
68 : profile_(profile), | 64 : profile_(profile), |
69 is_profile_info_cache_observer_(false), | 65 is_profile_info_cache_observer_(false), |
70 prefs_(profile->GetPrefs()) { | 66 prefs_(profile->GetPrefs()) { |
71 // Use of g_browser_process requires that we are either on the UI thread, or | 67 // Use of g_browser_process requires that we are either on the UI thread, or |
72 // there are no threads initialized (such as in unit tests). | 68 // there are no threads initialized (such as in unit tests). |
73 DCHECK(!content::BrowserThread::IsThreadInitialized( | 69 DCHECK(!content::BrowserThread::IsThreadInitialized( |
74 content::BrowserThread::UI) || | 70 content::BrowserThread::UI) || |
75 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 71 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
76 | 72 |
77 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, | 73 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, |
78 content::Source<Profile>(profile_)); | 74 content::Source<Profile>(profile_)); |
79 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 75 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
80 content::Source<Profile>(profile_)); | 76 content::Source<Profile>(profile_)); |
81 // Wait for extensions to be ready before running OnceOffCreateShortcuts. | 77 // Wait for extensions to be ready before running |
78 // UpdateShortcutsForAllAppsIfNeeded. | |
82 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, | 79 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, |
83 content::Source<Profile>(profile_)); | 80 content::Source<Profile>(profile_)); |
84 | 81 |
85 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 82 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
86 // profile_manager might be NULL in testing environments. | 83 // profile_manager might be NULL in testing environments. |
87 if (profile_manager) { | 84 if (profile_manager) { |
88 profile_manager->GetProfileInfoCache().AddObserver(this); | 85 profile_manager->GetProfileInfoCache().AddObserver(this); |
89 is_profile_info_cache_observer_ = true; | 86 is_profile_info_cache_observer_ = true; |
90 } | 87 } |
91 } | 88 } |
92 | 89 |
93 AppShortcutManager::~AppShortcutManager() { | 90 AppShortcutManager::~AppShortcutManager() { |
94 if (g_browser_process && is_profile_info_cache_observer_) { | 91 if (g_browser_process && is_profile_info_cache_observer_) { |
95 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 92 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
96 // profile_manager might be NULL in testing environments or during shutdown. | 93 // profile_manager might be NULL in testing environments or during shutdown. |
97 if (profile_manager) | 94 if (profile_manager) |
98 profile_manager->GetProfileInfoCache().RemoveObserver(this); | 95 profile_manager->GetProfileInfoCache().RemoveObserver(this); |
99 } | 96 } |
100 } | 97 } |
101 | 98 |
102 void AppShortcutManager::Observe(int type, | 99 void AppShortcutManager::Observe(int type, |
103 const content::NotificationSource& source, | 100 const content::NotificationSource& source, |
104 const content::NotificationDetails& details) { | 101 const content::NotificationDetails& details) { |
105 switch (type) { | 102 switch (type) { |
106 case chrome::NOTIFICATION_EXTENSIONS_READY: { | 103 case chrome::NOTIFICATION_EXTENSIONS_READY: { |
107 OnceOffCreateShortcuts(); | 104 UpdateShortcutsForAllAppsIfNeeded(); |
108 break; | 105 break; |
109 } | 106 } |
110 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { | 107 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
111 #if defined(OS_MACOSX) | 108 #if defined(OS_MACOSX) |
112 if (!apps::IsAppShimsEnabled()) | 109 if (!apps::IsAppShimsEnabled()) |
113 break; | 110 break; |
114 #endif // defined(OS_MACOSX) | 111 #endif // defined(OS_MACOSX) |
115 | 112 |
116 const extensions::InstalledExtensionInfo* installed_info = | 113 const extensions::InstalledExtensionInfo* installed_info = |
117 content::Details<const extensions::InstalledExtensionInfo>(details) | 114 content::Details<const extensions::InstalledExtensionInfo>(details) |
118 .ptr(); | 115 .ptr(); |
119 const Extension* extension = installed_info->extension; | 116 const Extension* extension = installed_info->extension; |
120 // If the app is being updated, update any existing shortcuts but do not | 117 // If the app is being updated, update any existing shortcuts but do not |
121 // create new ones. If it is being installed, automatically create a | 118 // create new ones. If it is being installed, automatically create a |
122 // shortcut in the applications menu (e.g., Start Menu). | 119 // shortcut in the applications menu (e.g., Start Menu). |
123 if (installed_info->is_update) { | 120 if (installed_info->is_update) { |
124 web_app::UpdateAllShortcuts( | 121 web_app::UpdateAllShortcuts( |
125 base::UTF8ToUTF16(installed_info->old_name), profile_, extension); | 122 base::UTF8ToUTF16(installed_info->old_name), profile_, extension); |
126 } else if (ShouldCreateShortcutFor(extension)) { | 123 } else { |
127 CreateShortcutsInApplicationsMenu(profile_, extension); | 124 CreateShortcutsInApplicationsMenu(profile_, extension); |
128 } | 125 } |
129 break; | 126 break; |
130 } | 127 } |
131 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { | 128 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { |
132 const Extension* extension = content::Details<const Extension>( | 129 const Extension* extension = content::Details<const Extension>( |
133 details).ptr(); | 130 details).ptr(); |
134 web_app::DeleteAllShortcuts(profile_, extension); | 131 web_app::DeleteAllShortcuts(profile_, extension); |
135 break; | 132 break; |
136 } | 133 } |
137 default: | 134 default: |
138 NOTREACHED(); | 135 NOTREACHED(); |
139 } | 136 } |
140 } | 137 } |
141 | 138 |
142 void AppShortcutManager::OnProfileWillBeRemoved( | 139 void AppShortcutManager::OnProfileWillBeRemoved( |
143 const base::FilePath& profile_path) { | 140 const base::FilePath& profile_path) { |
144 if (profile_path != profile_->GetPath()) | 141 if (profile_path != profile_->GetPath()) |
145 return; | 142 return; |
146 content::BrowserThread::PostTask( | 143 content::BrowserThread::PostTask( |
147 content::BrowserThread::FILE, FROM_HERE, | 144 content::BrowserThread::FILE, FROM_HERE, |
148 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile, | 145 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile, |
149 profile_path)); | 146 profile_path)); |
150 } | 147 } |
151 | 148 |
152 void AppShortcutManager::OnceOffCreateShortcuts() { | 149 void AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded() { |
153 bool was_enabled = prefs_->GetBoolean(prefs::kAppShortcutsHaveBeenCreated); | 150 int last_version = prefs_->GetInteger(prefs::kAppShortcutsVersion); |
154 | 151 if (last_version >= kCurrentAppShortcutsVersion) |
155 // Creation of shortcuts on Mac currently can be disabled with | |
156 // --disable-app-shims, so check the flag, and set the pref accordingly. | |
157 #if defined(OS_MACOSX) | |
158 bool is_now_enabled = apps::IsAppShimsEnabled(); | |
159 #else | |
160 bool is_now_enabled = true; | |
161 #endif // defined(OS_MACOSX) | |
162 | |
163 if (was_enabled != is_now_enabled) | |
164 prefs_->SetBoolean(prefs::kAppShortcutsHaveBeenCreated, is_now_enabled); | |
165 | |
166 if (was_enabled || !is_now_enabled) | |
167 return; | 152 return; |
168 | 153 |
169 // Check if extension system/service are available. They might not be in | 154 web_app::UpdateShortcutsForAllApps(profile_); |
170 // tests. | 155 prefs_->SetInteger(prefs::kAppShortcutsVersion, kCurrentAppShortcutsVersion); |
171 extensions::ExtensionSystem* extension_system; | |
172 ExtensionServiceInterface* extension_service; | |
173 if (!(extension_system = extensions::ExtensionSystem::Get(profile_)) || | |
174 !(extension_service = extension_system->extension_service())) | |
175 return; | |
176 | |
177 // Create an applications menu shortcut for each app in this profile. | |
178 const extensions::ExtensionSet* apps = extension_service->extensions(); | |
179 for (extensions::ExtensionSet::const_iterator it = apps->begin(); | |
180 it != apps->end(); ++it) { | |
181 if (ShouldCreateShortcutFor(it->get())) | |
182 CreateShortcutsInApplicationsMenu(profile_, it->get()); | |
183 } | |
184 } | 156 } |
OLD | NEW |