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) { |
| 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 extensions to be ready before running OnceOffCreateShortcuts. |
| 60 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, |
| 61 content::Source<Profile>(profile_)); |
46 } | 62 } |
47 | 63 |
48 ShortcutManager::~ShortcutManager() {} | 64 ShortcutManager::~ShortcutManager() {} |
49 | 65 |
50 void ShortcutManager::Observe(int type, | 66 void ShortcutManager::Observe(int type, |
51 const content::NotificationSource& source, | 67 const content::NotificationSource& source, |
52 const content::NotificationDetails& details) { | 68 const content::NotificationDetails& details) { |
53 switch (type) { | 69 switch (type) { |
| 70 case chrome::NOTIFICATION_EXTENSIONS_READY: { |
| 71 OnceOffCreateShortcuts(); |
| 72 break; |
| 73 } |
54 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { | 74 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
55 #if defined(OS_MACOSX) | 75 #if defined(OS_MACOSX) |
56 if (!CommandLine::ForCurrentProcess()-> | 76 if (!CommandLine::ForCurrentProcess()-> |
57 HasSwitch(switches::kEnableAppShims)) | 77 HasSwitch(switches::kEnableAppShims)) |
58 break; | 78 break; |
59 #endif // defined(OS_MACOSX) | 79 #endif // defined(OS_MACOSX) |
60 | 80 |
61 const extensions::InstalledExtensionInfo* installed_info = | 81 const extensions::InstalledExtensionInfo* installed_info = |
62 content::Details<const extensions::InstalledExtensionInfo>(details) | 82 content::Details<const extensions::InstalledExtensionInfo>(details) |
63 .ptr(); | 83 .ptr(); |
64 const Extension* extension = installed_info->extension; | 84 const Extension* extension = installed_info->extension; |
65 if (extension->is_platform_app() && | 85 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 | 86 // 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 | 87 // create new ones. If it is being installed, automatically create a |
70 // shortcut in the applications menu (e.g., Start Menu). | 88 // shortcut in the applications menu (e.g., Start Menu). |
71 base::Callback<void(const ShellIntegration::ShortcutInfo&)> | 89 base::Callback<void(const ShellIntegration::ShortcutInfo&)> |
72 create_or_update; | 90 create_or_update; |
73 if (installed_info->is_update) { | 91 if (installed_info->is_update) { |
74 string16 old_title = UTF8ToUTF16(installed_info->old_name); | 92 string16 old_title = UTF8ToUTF16(installed_info->old_name); |
75 create_or_update = base::Bind(&web_app::UpdateAllShortcuts, | 93 create_or_update = base::Bind(&web_app::UpdateAllShortcuts, |
76 old_title); | 94 old_title); |
77 } else { | 95 } else { |
78 create_or_update = base::Bind(&CreateShortcutsInApplicationsMenu); | 96 create_or_update = base::Bind(&CreateShortcutsInApplicationsMenu); |
79 } | 97 } |
80 | 98 |
81 web_app::UpdateShortcutInfoAndIconForApp(*extension, profile_, | 99 web_app::UpdateShortcutInfoAndIconForApp(*extension, profile_, |
82 create_or_update); | 100 create_or_update); |
83 } | 101 } |
84 break; | 102 break; |
85 } | 103 } |
86 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { | 104 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { |
87 const Extension* extension = content::Details<const Extension>( | 105 const Extension* extension = content::Details<const Extension>( |
88 details).ptr(); | 106 details).ptr(); |
89 DeleteApplicationShortcuts(extension); | 107 DeleteApplicationShortcuts(extension); |
90 break; | 108 break; |
91 } | 109 } |
92 default: | 110 default: |
93 NOTREACHED(); | 111 NOTREACHED(); |
94 } | 112 } |
95 } | 113 } |
96 | 114 |
| 115 void ShortcutManager::OnceOffCreateShortcuts() { |
| 116 bool was_enabled = prefs_->GetBoolean(apps::prefs::kShortcutsHaveBeenCreated); |
| 117 |
| 118 // Creation of shortcuts on Mac currently sits behind --enable-app-shims. |
| 119 // Until it is enabled permanently, we need to check the flag, and set the |
| 120 // pref accordingly. |
| 121 #if defined(OS_MACOSX) |
| 122 bool is_now_enabled = |
| 123 CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableAppShims); |
| 124 #else |
| 125 bool is_now_enabled = true; |
| 126 #endif // defined(OS_MACOSX) |
| 127 |
| 128 if (was_enabled != is_now_enabled) |
| 129 prefs_->SetBoolean(apps::prefs::kShortcutsHaveBeenCreated, is_now_enabled); |
| 130 |
| 131 if (was_enabled || !is_now_enabled) |
| 132 return; |
| 133 |
| 134 // Check if extension system/service are available. They might not be in |
| 135 // tests. |
| 136 extensions::ExtensionSystem* extension_system; |
| 137 ExtensionServiceInterface* extension_service; |
| 138 if (!(extension_system = extensions::ExtensionSystem::Get(profile_)) || |
| 139 !(extension_service = extension_system->extension_service())) |
| 140 return; |
| 141 |
| 142 // Create an applications menu shortcut for each app in this profile. |
| 143 const ExtensionSet* apps = extension_service->extensions(); |
| 144 for (ExtensionSet::const_iterator it = apps->begin(); |
| 145 it != apps->end(); ++it) { |
| 146 if (ShouldCreateShortcutFor(*it)) |
| 147 web_app::UpdateShortcutInfoAndIconForApp( |
| 148 *(*it), profile_, base::Bind(&CreateShortcutsInApplicationsMenu)); |
| 149 } |
| 150 } |
| 151 |
97 void ShortcutManager::DeleteApplicationShortcuts( | 152 void ShortcutManager::DeleteApplicationShortcuts( |
98 const Extension* extension) { | 153 const Extension* extension) { |
99 ShellIntegration::ShortcutInfo delete_info = | 154 ShellIntegration::ShortcutInfo delete_info = |
100 web_app::ShortcutInfoForExtensionAndProfile(extension, profile_); | 155 web_app::ShortcutInfoForExtensionAndProfile(extension, profile_); |
101 web_app::DeleteAllShortcuts(delete_info); | 156 web_app::DeleteAllShortcuts(delete_info); |
102 } | 157 } |
103 | 158 |
104 } // namespace apps | 159 } // namespace apps |
OLD | NEW |