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" | 15 #include "chrome/browser/extensions/extension_service.h" |
16 #include "chrome/browser/extensions/extension_ui_util.h" | 16 #include "chrome/browser/extensions/extension_ui_util.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/browser/profiles/profile_info_cache.h" | 18 #include "chrome/browser/profiles/profile_info_cache.h" |
19 #include "chrome/browser/profiles/profile_manager.h" | 19 #include "chrome/browser/profiles/profile_manager.h" |
20 #include "chrome/browser/shell_integration.h" | 20 #include "chrome/browser/shell_integration.h" |
21 #include "chrome/browser/web_applications/web_app.h" | 21 #include "chrome/browser/web_applications/web_app.h" |
22 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
23 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
24 #include "components/pref_registry/pref_registry_syncable.h" | 24 #include "components/pref_registry/pref_registry_syncable.h" |
25 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
26 #include "content/public/browser/notification_details.h" | 26 #include "content/public/browser/notification_details.h" |
27 #include "content/public/browser/notification_source.h" | 27 #include "content/public/browser/notification_source.h" |
| 28 #include "extensions/browser/extension_registry.h" |
28 #include "extensions/browser/extension_system.h" | 29 #include "extensions/browser/extension_system.h" |
29 #include "extensions/common/extension_set.h" | 30 #include "extensions/common/extension_set.h" |
30 | 31 |
31 using extensions::Extension; | 32 using extensions::Extension; |
32 | 33 |
33 namespace { | 34 namespace { |
34 | 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, |
(...skipping 19 matching lines...) Expand all Loading... |
57 user_prefs::PrefRegistrySyncable* registry) { | 58 user_prefs::PrefRegistrySyncable* registry) { |
58 // Indicates whether app shortcuts have been created. | 59 // Indicates whether app shortcuts have been created. |
59 registry->RegisterBooleanPref( | 60 registry->RegisterBooleanPref( |
60 prefs::kAppShortcutsHaveBeenCreated, false, | 61 prefs::kAppShortcutsHaveBeenCreated, false, |
61 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 62 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
62 } | 63 } |
63 | 64 |
64 AppShortcutManager::AppShortcutManager(Profile* profile) | 65 AppShortcutManager::AppShortcutManager(Profile* profile) |
65 : profile_(profile), | 66 : profile_(profile), |
66 is_profile_info_cache_observer_(false), | 67 is_profile_info_cache_observer_(false), |
67 prefs_(profile->GetPrefs()) { | 68 prefs_(profile->GetPrefs()), |
| 69 extension_registry_observer_(this) { |
68 // Use of g_browser_process requires that we are either on the UI thread, or | 70 // 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). | 71 // there are no threads initialized (such as in unit tests). |
70 DCHECK(!content::BrowserThread::IsThreadInitialized( | 72 DCHECK(!content::BrowserThread::IsThreadInitialized( |
71 content::BrowserThread::UI) || | 73 content::BrowserThread::UI) || |
72 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 74 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
73 | 75 |
74 registrar_.Add(this, | 76 extension_registry_observer_.Add( |
75 chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, | 77 extensions::ExtensionRegistry::Get(profile_)); |
76 content::Source<Profile>(profile_)); | |
77 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | |
78 content::Source<Profile>(profile_)); | |
79 // Wait for extensions to be ready before running OnceOffCreateShortcuts. | 78 // Wait for extensions to be ready before running OnceOffCreateShortcuts. |
80 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, | 79 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY, |
81 content::Source<Profile>(profile_)); | 80 content::Source<Profile>(profile_)); |
82 | 81 |
83 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 82 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
84 // profile_manager might be NULL in testing environments. | 83 // profile_manager might be NULL in testing environments. |
85 if (profile_manager) { | 84 if (profile_manager) { |
86 profile_manager->GetProfileInfoCache().AddObserver(this); | 85 profile_manager->GetProfileInfoCache().AddObserver(this); |
87 is_profile_info_cache_observer_ = true; | 86 is_profile_info_cache_observer_ = true; |
88 } | 87 } |
89 } | 88 } |
90 | 89 |
91 AppShortcutManager::~AppShortcutManager() { | 90 AppShortcutManager::~AppShortcutManager() { |
92 if (g_browser_process && is_profile_info_cache_observer_) { | 91 if (g_browser_process && is_profile_info_cache_observer_) { |
93 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 92 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
94 // profile_manager might be NULL in testing environments or during shutdown. | 93 // profile_manager might be NULL in testing environments or during shutdown. |
95 if (profile_manager) | 94 if (profile_manager) |
96 profile_manager->GetProfileInfoCache().RemoveObserver(this); | 95 profile_manager->GetProfileInfoCache().RemoveObserver(this); |
97 } | 96 } |
98 } | 97 } |
99 | 98 |
100 void AppShortcutManager::Observe(int type, | 99 void AppShortcutManager::Observe(int type, |
101 const content::NotificationSource& source, | 100 const content::NotificationSource& source, |
102 const content::NotificationDetails& details) { | 101 const content::NotificationDetails& details) { |
103 switch (type) { | 102 DCHECK_EQ(chrome::NOTIFICATION_EXTENSIONS_READY, type); |
104 case chrome::NOTIFICATION_EXTENSIONS_READY: { | 103 OnceOffCreateShortcuts(); |
105 OnceOffCreateShortcuts(); | 104 } |
106 break; | 105 |
107 } | 106 void AppShortcutManager::OnExtensionWillBeInstalled( |
108 case chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED: { | 107 content::BrowserContext* browser_context, |
109 const extensions::InstalledExtensionInfo* installed_info = | 108 const Extension* extension, |
110 content::Details<const extensions::InstalledExtensionInfo>(details) | 109 bool is_update, |
111 .ptr(); | 110 const std::string& old_name) { |
112 const Extension* extension = installed_info->extension; | 111 // If the app is being updated, update any existing shortcuts but do not |
113 // If the app is being updated, update any existing shortcuts but do not | 112 // create new ones. If it is being installed, automatically create a |
114 // create new ones. If it is being installed, automatically create a | 113 // shortcut in the applications menu (e.g., Start Menu). |
115 // shortcut in the applications menu (e.g., Start Menu). | 114 if (is_update) { |
116 if (installed_info->is_update) { | 115 web_app::UpdateAllShortcuts( |
117 web_app::UpdateAllShortcuts( | 116 base::UTF8ToUTF16(old_name), profile_, extension); |
118 base::UTF8ToUTF16(installed_info->old_name), profile_, extension); | 117 } else if (ShouldCreateShortcutFor(profile_, extension)) { |
119 } else if (ShouldCreateShortcutFor(profile_, extension)) { | 118 CreateShortcutsInApplicationsMenu(profile_, extension); |
120 CreateShortcutsInApplicationsMenu(profile_, extension); | |
121 } | |
122 break; | |
123 } | |
124 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { | |
125 const Extension* extension = content::Details<const Extension>( | |
126 details).ptr(); | |
127 web_app::DeleteAllShortcuts(profile_, extension); | |
128 break; | |
129 } | |
130 default: | |
131 NOTREACHED(); | |
132 } | 119 } |
133 } | 120 } |
134 | 121 |
| 122 void AppShortcutManager::OnExtensionUninstalled( |
| 123 content::BrowserContext* browser_context, |
| 124 const Extension* extension) { |
| 125 web_app::DeleteAllShortcuts(profile_, extension); |
| 126 } |
| 127 |
135 void AppShortcutManager::OnProfileWillBeRemoved( | 128 void AppShortcutManager::OnProfileWillBeRemoved( |
136 const base::FilePath& profile_path) { | 129 const base::FilePath& profile_path) { |
137 if (profile_path != profile_->GetPath()) | 130 if (profile_path != profile_->GetPath()) |
138 return; | 131 return; |
139 content::BrowserThread::PostTask( | 132 content::BrowserThread::PostTask( |
140 content::BrowserThread::FILE, FROM_HERE, | 133 content::BrowserThread::FILE, FROM_HERE, |
141 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile, | 134 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile, |
142 profile_path)); | 135 profile_path)); |
143 } | 136 } |
144 | 137 |
(...skipping 12 matching lines...) Expand all Loading... |
157 return; | 150 return; |
158 | 151 |
159 // Create an applications menu shortcut for each app in this profile. | 152 // Create an applications menu shortcut for each app in this profile. |
160 const extensions::ExtensionSet* apps = extension_service->extensions(); | 153 const extensions::ExtensionSet* apps = extension_service->extensions(); |
161 for (extensions::ExtensionSet::const_iterator it = apps->begin(); | 154 for (extensions::ExtensionSet::const_iterator it = apps->begin(); |
162 it != apps->end(); ++it) { | 155 it != apps->end(); ++it) { |
163 if (ShouldCreateShortcutFor(profile_, it->get())) | 156 if (ShouldCreateShortcutFor(profile_, it->get())) |
164 CreateShortcutsInApplicationsMenu(profile_, it->get()); | 157 CreateShortcutsInApplicationsMenu(profile_, it->get()); |
165 } | 158 } |
166 } | 159 } |
OLD | NEW |