OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "apps/shortcut_manager.h" | 5 #include "apps/shortcut_manager.h" |
6 | 6 |
7 #include "apps/pref_names.h" | |
7 #include "base/bind.h" | 8 #include "base/bind.h" |
8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
9 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/prefs/pref_service.h" | |
10 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
11 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "chrome/browser/extensions/extension_service.h" | |
15 #include "chrome/browser/extensions/extension_system.h" | |
16 #include "chrome/browser/profiles/profile.h" | |
12 #include "chrome/browser/shell_integration.h" | 17 #include "chrome/browser/shell_integration.h" |
13 #include "chrome/browser/ui/web_applications/web_app_ui.h" | 18 #include "chrome/browser/ui/web_applications/web_app_ui.h" |
14 #include "chrome/browser/web_applications/web_app.h" | 19 #include "chrome/browser/web_applications/web_app.h" |
15 #include "chrome/common/chrome_notification_types.h" | 20 #include "chrome/common/chrome_notification_types.h" |
16 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
22 #include "chrome/common/extensions/extension_set.h" | |
17 #include "content/public/browser/notification_details.h" | 23 #include "content/public/browser/notification_details.h" |
18 #include "content/public/browser/notification_source.h" | 24 #include "content/public/browser/notification_source.h" |
19 | 25 |
20 using extensions::Extension; | 26 using extensions::Extension; |
21 | 27 |
22 namespace { | 28 namespace { |
23 | 29 |
24 // Creates a shortcut for an application in the applications menu. | 30 // Creates a shortcut for an application in the applications menu. |
25 void CreateShortcutsInApplicationsMenu( | 31 void CreateShortcutsInApplicationsMenu( |
26 const ShellIntegration::ShortcutInfo& shortcut_info) { | 32 const ShellIntegration::ShortcutInfo& shortcut_info) { |
27 ShellIntegration::ShortcutLocations creation_locations; | 33 ShellIntegration::ShortcutLocations creation_locations; |
28 creation_locations.in_applications_menu = true; | 34 creation_locations.in_applications_menu = true; |
29 // Create the shortcut in the Chrome Apps subdir. | 35 // Create the shortcut in the Chrome Apps subdir. |
30 creation_locations.applications_menu_subdir = | 36 creation_locations.applications_menu_subdir = |
31 web_app::GetAppShortcutsSubdirName(); | 37 web_app::GetAppShortcutsSubdirName(); |
32 web_app::CreateShortcuts(shortcut_info, creation_locations); | 38 web_app::CreateShortcuts(shortcut_info, creation_locations); |
33 } | 39 } |
34 | 40 |
41 bool ShouldCreateShortcutFor(const extensions::Extension* extension) { | |
Matt Giuca
2013/06/20 10:19:18
Thanks for doing this.
| |
42 return extension->is_platform_app() && | |
43 extension->location() != extensions::Manifest::COMPONENT && | |
44 extension->ShouldDisplayInAppLauncher(); | |
45 } | |
46 | |
35 } // namespace | 47 } // namespace |
36 | 48 |
37 namespace apps { | 49 namespace apps { |
38 | 50 |
39 ShortcutManager::ShortcutManager(Profile* profile) | 51 ShortcutManager::ShortcutManager(Profile* profile) |
40 : profile_(profile), | 52 : profile_(profile), |
53 prefs_(profile->GetPrefs()), | |
41 weak_factory_(this) { | 54 weak_factory_(this) { |
42 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, | 55 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED, |
43 content::Source<Profile>(profile_)); | 56 content::Source<Profile>(profile_)); |
44 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 57 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, |
45 content::Source<Profile>(profile_)); | 58 content::Source<Profile>(profile_)); |
59 // Wait for the profile to be added before running OnceOffCreateShortucts | |
60 // because the profile's ExtensionService is not available when BCKSs are | |
Matt Giuca
2013/06/20 10:19:18
BCKSs?
jackhou1
2013/06/20 11:03:22
BrowserContextKeyedServices. The abbreviation has
| |
61 // created. | |
62 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_ADDED, | |
benwells
2013/06/20 08:54:32
I think you should use a different notification li
jackhou1
2013/06/20 11:03:22
Done.
| |
63 content::Source<Profile>(profile_)); | |
46 } | 64 } |
47 | 65 |
48 ShortcutManager::~ShortcutManager() {} | 66 ShortcutManager::~ShortcutManager() {} |
49 | 67 |
50 void ShortcutManager::Observe(int type, | 68 void ShortcutManager::Observe(int type, |
51 const content::NotificationSource& source, | 69 const content::NotificationSource& source, |
52 const content::NotificationDetails& details) { | 70 const content::NotificationDetails& details) { |
53 switch (type) { | 71 switch (type) { |
72 case chrome::NOTIFICATION_PROFILE_ADDED: { | |
73 OnceOffCreateShortcuts(); | |
74 break; | |
75 } | |
54 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { | 76 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
55 #if defined(OS_MACOSX) | 77 #if defined(OS_MACOSX) |
56 if (!CommandLine::ForCurrentProcess()-> | 78 if (!CommandLine::ForCurrentProcess()-> |
57 HasSwitch(switches::kEnableAppShims)) | 79 HasSwitch(switches::kEnableAppShims)) |
58 break; | 80 break; |
59 #endif // defined(OS_MACOSX) | 81 #endif // defined(OS_MACOSX) |
60 | 82 |
61 const extensions::InstalledExtensionInfo* installed_info = | 83 const extensions::InstalledExtensionInfo* installed_info = |
62 content::Details<const extensions::InstalledExtensionInfo>(details) | 84 content::Details<const extensions::InstalledExtensionInfo>(details) |
63 .ptr(); | 85 .ptr(); |
64 const Extension* extension = installed_info->extension; | 86 const Extension* extension = installed_info->extension; |
65 if (extension->is_platform_app() && | 87 if (ShouldCreateShortcutFor(extension)) { |
66 extension->location() != extensions::Manifest::COMPONENT && | |
67 extension->ShouldDisplayInAppLauncher()) { | |
68 // If the app is being updated, update any existing shortcuts but do not | 88 // If the app is being updated, update any existing shortcuts but do not |
69 // create new ones. If it is being installed, automatically create a | 89 // create new ones. If it is being installed, automatically create a |
70 // shortcut in the applications menu (e.g., Start Menu). | 90 // shortcut in the applications menu (e.g., Start Menu). |
71 base::Callback<void(const ShellIntegration::ShortcutInfo&)> | 91 base::Callback<void(const ShellIntegration::ShortcutInfo&)> |
72 create_or_update; | 92 create_or_update; |
73 if (installed_info->is_update) { | 93 if (installed_info->is_update) { |
74 string16 old_title = UTF8ToUTF16(installed_info->old_name); | 94 string16 old_title = UTF8ToUTF16(installed_info->old_name); |
75 create_or_update = base::Bind(&web_app::UpdateAllShortcuts, | 95 create_or_update = base::Bind(&web_app::UpdateAllShortcuts, |
76 old_title); | 96 old_title); |
77 } else { | 97 } else { |
78 create_or_update = base::Bind(&CreateShortcutsInApplicationsMenu); | 98 create_or_update = base::Bind(&CreateShortcutsInApplicationsMenu); |
79 } | 99 } |
80 | 100 |
81 web_app::UpdateShortcutInfoAndIconForApp(*extension, profile_, | 101 web_app::UpdateShortcutInfoAndIconForApp(*extension, profile_, |
82 create_or_update); | 102 create_or_update); |
83 } | 103 } |
84 break; | 104 break; |
85 } | 105 } |
86 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { | 106 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { |
87 const Extension* extension = content::Details<const Extension>( | 107 const Extension* extension = content::Details<const Extension>( |
88 details).ptr(); | 108 details).ptr(); |
89 DeleteApplicationShortcuts(extension); | 109 DeleteApplicationShortcuts(extension); |
90 break; | 110 break; |
91 } | 111 } |
92 default: | 112 default: |
93 NOTREACHED(); | 113 NOTREACHED(); |
94 } | 114 } |
95 } | 115 } |
96 | 116 |
117 void ShortcutManager::OnceOffCreateShortcuts() { | |
118 bool was_enabled = prefs_->GetBoolean(apps::prefs::kAppShortcutsEnabled); | |
benwells
2013/06/20 08:54:32
The preference name doesn't feel right. Can you ma
jackhou1
2013/06/20 11:03:22
Went with Matt's suggestion: ShortcutsHaveBeenCrea
| |
119 | |
120 #if defined(OS_MACOSX) | |
121 bool is_now_enabled = | |
Matt Giuca
2013/06/20 10:19:18
Add a comment explaining why Mac is special and th
jackhou1
2013/06/20 11:03:22
Done.
| |
122 CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableAppShims); | |
123 #else | |
124 bool is_now_enabled = true; | |
125 #endif // defined(OS_MACOSX) | |
126 | |
127 if (was_enabled != is_now_enabled) | |
Matt Giuca
2013/06/20 10:19:18
I don't think you want to set this to false once i
jackhou1
2013/06/20 11:03:22
Yeah that's how I had it initially, but it needs t
| |
128 prefs_->SetBoolean(apps::prefs::kAppShortcutsEnabled, is_now_enabled); | |
129 | |
130 if (was_enabled || !is_now_enabled) | |
131 return; | |
132 | |
133 ExtensionServiceInterface* extension_service = | |
Matt Giuca
2013/06/20 10:19:18
// Create an applications menu shortcut for each a
jackhou1
2013/06/20 11:03:22
Done.
| |
134 extensions::ExtensionSystem::Get(profile_)->extension_service(); | |
135 | |
136 const ExtensionSet* apps = extension_service->extensions(); | |
137 for (ExtensionSet::const_iterator it = apps->begin(); | |
138 it != apps->end(); ++it) { | |
139 if (ShouldCreateShortcutFor(*it)) | |
140 web_app::UpdateShortcutInfoAndIconForApp( | |
141 *(*it), profile_, base::Bind(&CreateShortcutsInApplicationsMenu)); | |
142 } | |
143 } | |
144 | |
97 void ShortcutManager::DeleteApplicationShortcuts( | 145 void ShortcutManager::DeleteApplicationShortcuts( |
98 const Extension* extension) { | 146 const Extension* extension) { |
99 ShellIntegration::ShortcutInfo delete_info = | 147 ShellIntegration::ShortcutInfo delete_info = |
100 web_app::ShortcutInfoForExtensionAndProfile(extension, profile_); | 148 web_app::ShortcutInfoForExtensionAndProfile(extension, profile_); |
101 web_app::DeleteAllShortcuts(delete_info); | 149 web_app::DeleteAllShortcuts(delete_info); |
102 } | 150 } |
103 | 151 |
104 } // namespace apps | 152 } // namespace apps |
OLD | NEW |