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/user_prefs/pref_registry_syncable.h" | 22 #include "components/user_prefs/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 const int kCurrentAppShortcutsVersion = 1; | |
Matt Giuca
2014/05/07 05:21:51
This needs a comment. Please say something about e
jackhou1
2014/05/22 05:26:47
Done.
The 0 case should be gone soon. I'll put up
| |
36 | |
38 // Creates a shortcut for an application in the applications menu, if there is | 37 // Creates a shortcut for an application in the applications menu, if there is |
39 // not already one present. | 38 // not already one present. |
40 void CreateShortcutsInApplicationsMenu(Profile* profile, | 39 void CreateShortcutsInApplicationsMenu(Profile* profile, |
41 const Extension* app) { | 40 const Extension* app) { |
42 web_app::ShortcutLocations creation_locations; | 41 web_app::ShortcutLocations creation_locations; |
43 // Create the shortcut in the Chrome Apps subdir. | 42 // Create the shortcut in the Chrome Apps subdir. |
44 creation_locations.applications_menu_location = | 43 creation_locations.applications_menu_location = |
45 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS; | 44 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS; |
46 web_app::CreateShortcuts( | 45 web_app::CreateShortcuts( |
47 web_app::SHORTCUT_CREATION_AUTOMATED, creation_locations, profile, app); | 46 web_app::SHORTCUT_CREATION_AUTOMATED, creation_locations, profile, app); |
48 } | 47 } |
49 | 48 |
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 | 49 } // namespace |
57 | 50 |
58 // static | 51 // static |
59 void AppShortcutManager::RegisterProfilePrefs( | 52 void AppShortcutManager::RegisterProfilePrefs( |
60 user_prefs::PrefRegistrySyncable* registry) { | 53 user_prefs::PrefRegistrySyncable* registry) { |
61 // Indicates whether app shortcuts have been created. | 54 // Indicates whether app shortcuts have been created. |
62 registry->RegisterBooleanPref( | 55 registry->RegisterIntegerPref( |
63 prefs::kAppShortcutsHaveBeenCreated, false, | 56 prefs::kAppShortcutsVersion, 0, |
64 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 57 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
65 } | 58 } |
66 | 59 |
67 AppShortcutManager::AppShortcutManager(Profile* profile) | 60 AppShortcutManager::AppShortcutManager(Profile* profile) |
68 : profile_(profile), | 61 : profile_(profile), |
69 is_profile_info_cache_observer_(false), | 62 is_profile_info_cache_observer_(false), |
70 prefs_(profile->GetPrefs()) { | 63 prefs_(profile->GetPrefs()) { |
71 // Use of g_browser_process requires that we are either on the UI thread, or | 64 // 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). | 65 // there are no threads initialized (such as in unit tests). |
73 DCHECK(!content::BrowserThread::IsThreadInitialized( | 66 DCHECK(!content::BrowserThread::IsThreadInitialized( |
74 content::BrowserThread::UI) || | 67 content::BrowserThread::UI) || |
75 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 68 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
76 | 69 |
77 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, | 70 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, |
78 content::Source<Profile>(profile_)); | 71 content::Source<Profile>(profile_)); |
79 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 72 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
80 content::Source<Profile>(profile_)); | 73 content::Source<Profile>(profile_)); |
81 // Wait for extensions to be ready before running OnceOffCreateShortcuts. | 74 // Wait for extensions to be ready before running |
75 // UpdateShortcutsForAllAppsIfNeeded. | |
82 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, | 76 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, |
83 content::Source<Profile>(profile_)); | 77 content::Source<Profile>(profile_)); |
84 | 78 |
85 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 79 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
86 // profile_manager might be NULL in testing environments. | 80 // profile_manager might be NULL in testing environments. |
87 if (profile_manager) { | 81 if (profile_manager) { |
88 profile_manager->GetProfileInfoCache().AddObserver(this); | 82 profile_manager->GetProfileInfoCache().AddObserver(this); |
89 is_profile_info_cache_observer_ = true; | 83 is_profile_info_cache_observer_ = true; |
90 } | 84 } |
91 } | 85 } |
92 | 86 |
93 AppShortcutManager::~AppShortcutManager() { | 87 AppShortcutManager::~AppShortcutManager() { |
94 if (g_browser_process && is_profile_info_cache_observer_) { | 88 if (g_browser_process && is_profile_info_cache_observer_) { |
95 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 89 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
96 // profile_manager might be NULL in testing environments or during shutdown. | 90 // profile_manager might be NULL in testing environments or during shutdown. |
97 if (profile_manager) | 91 if (profile_manager) |
98 profile_manager->GetProfileInfoCache().RemoveObserver(this); | 92 profile_manager->GetProfileInfoCache().RemoveObserver(this); |
99 } | 93 } |
100 } | 94 } |
101 | 95 |
102 void AppShortcutManager::Observe(int type, | 96 void AppShortcutManager::Observe(int type, |
103 const content::NotificationSource& source, | 97 const content::NotificationSource& source, |
104 const content::NotificationDetails& details) { | 98 const content::NotificationDetails& details) { |
105 switch (type) { | 99 switch (type) { |
106 case chrome::NOTIFICATION_EXTENSIONS_READY: { | 100 case chrome::NOTIFICATION_EXTENSIONS_READY: { |
107 OnceOffCreateShortcuts(); | 101 UpdateShortcutsForAllAppsIfNeeded(); |
108 break; | 102 break; |
109 } | 103 } |
110 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { | 104 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
111 #if defined(OS_MACOSX) | 105 #if defined(OS_MACOSX) |
112 if (!apps::IsAppShimsEnabled()) | 106 if (!apps::IsAppShimsEnabled()) |
113 break; | 107 break; |
114 #endif // defined(OS_MACOSX) | 108 #endif // defined(OS_MACOSX) |
115 | 109 |
116 const extensions::InstalledExtensionInfo* installed_info = | 110 const extensions::InstalledExtensionInfo* installed_info = |
117 content::Details<const extensions::InstalledExtensionInfo>(details) | 111 content::Details<const extensions::InstalledExtensionInfo>(details) |
118 .ptr(); | 112 .ptr(); |
119 const Extension* extension = installed_info->extension; | 113 const Extension* extension = installed_info->extension; |
120 // If the app is being updated, update any existing shortcuts but do not | 114 // 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 | 115 // create new ones. If it is being installed, automatically create a |
122 // shortcut in the applications menu (e.g., Start Menu). | 116 // shortcut in the applications menu (e.g., Start Menu). |
123 if (installed_info->is_update) { | 117 if (installed_info->is_update) { |
124 web_app::UpdateAllShortcuts( | 118 web_app::UpdateAllShortcuts( |
125 base::UTF8ToUTF16(installed_info->old_name), profile_, extension); | 119 base::UTF8ToUTF16(installed_info->old_name), profile_, extension); |
126 } else if (ShouldCreateShortcutFor(extension)) { | 120 } else { |
127 CreateShortcutsInApplicationsMenu(profile_, extension); | 121 CreateShortcutsInApplicationsMenu(profile_, extension); |
128 } | 122 } |
129 break; | 123 break; |
130 } | 124 } |
131 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { | 125 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { |
132 const Extension* extension = content::Details<const Extension>( | 126 const Extension* extension = content::Details<const Extension>( |
133 details).ptr(); | 127 details).ptr(); |
134 web_app::DeleteAllShortcuts(profile_, extension); | 128 web_app::DeleteAllShortcuts(profile_, extension); |
135 break; | 129 break; |
136 } | 130 } |
137 default: | 131 default: |
138 NOTREACHED(); | 132 NOTREACHED(); |
139 } | 133 } |
140 } | 134 } |
141 | 135 |
142 void AppShortcutManager::OnProfileWillBeRemoved( | 136 void AppShortcutManager::OnProfileWillBeRemoved( |
143 const base::FilePath& profile_path) { | 137 const base::FilePath& profile_path) { |
144 if (profile_path != profile_->GetPath()) | 138 if (profile_path != profile_->GetPath()) |
145 return; | 139 return; |
146 content::BrowserThread::PostTask( | 140 content::BrowserThread::PostTask( |
147 content::BrowserThread::FILE, FROM_HERE, | 141 content::BrowserThread::FILE, FROM_HERE, |
148 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile, | 142 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile, |
149 profile_path)); | 143 profile_path)); |
150 } | 144 } |
151 | 145 |
152 void AppShortcutManager::OnceOffCreateShortcuts() { | 146 void AppShortcutManager::UpdateShortcutsForAllAppsIfNeeded() { |
153 bool was_enabled = prefs_->GetBoolean(prefs::kAppShortcutsHaveBeenCreated); | 147 int last_version = prefs_->GetInteger(prefs::kAppShortcutsVersion); |
148 int current_version = kCurrentAppShortcutsVersion; | |
154 | 149 |
155 // Creation of shortcuts on Mac currently can be disabled with | 150 // Creation of shortcuts on Mac currently can be disabled with |
156 // --disable-app-shims, so check the flag, and set the pref accordingly. | 151 // --disable-app-shims, so check the flag, and set the pref accordingly. |
157 #if defined(OS_MACOSX) | 152 #if defined(OS_MACOSX) |
158 bool is_now_enabled = apps::IsAppShimsEnabled(); | 153 if (!apps::IsAppShimsEnabled()) |
159 #else | 154 current_version = 0; |
160 bool is_now_enabled = true; | |
161 #endif // defined(OS_MACOSX) | 155 #endif // defined(OS_MACOSX) |
162 | 156 |
163 if (was_enabled != is_now_enabled) | 157 if (last_version == current_version) |
Matt Giuca
2014/05/07 05:21:51
Should probably be (last_version >= current_versio
jackhou1
2014/05/22 05:26:47
Done.
| |
164 prefs_->SetBoolean(prefs::kAppShortcutsHaveBeenCreated, is_now_enabled); | |
165 | |
166 if (was_enabled || !is_now_enabled) | |
167 return; | 158 return; |
168 | 159 |
169 // Check if extension system/service are available. They might not be in | 160 web_app::UpdateShortcutsForAllApps(profile_); |
calamity
2014/05/07 06:00:19
Maybe this should support a callback so that the p
| |
170 // tests. | 161 prefs_->SetInteger(prefs::kAppShortcutsVersion, current_version); |
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 } | 162 } |
OLD | NEW |