[safe-browsing] Database full hash matches like prefix match.
[chromium-blink-merge.git] / chrome / browser / apps / shortcut_manager.cc
blob326722cf0c64c6ade62503ad19d8e9b3c388b257
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
3 // found in the LICENSE file.
5 #include "chrome/browser/apps/shortcut_manager.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/compiler_specific.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_info_cache.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/shell_integration.h"
20 #include "chrome/browser/web_applications/web_app.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/pref_names.h"
23 #include "components/user_prefs/pref_registry_syncable.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/notification_details.h"
26 #include "content/public/browser/notification_source.h"
27 #include "extensions/browser/extension_system.h"
28 #include "extensions/common/extension_set.h"
30 #if defined(OS_MACOSX)
31 #include "apps/app_shim/app_shim_mac.h"
32 #endif
34 using extensions::Extension;
36 namespace {
38 // Creates a shortcut for an application in the applications menu, if there is
39 // not already one present.
40 void CreateShortcutsInApplicationsMenu(Profile* profile,
41 const Extension* app) {
42 web_app::ShortcutLocations creation_locations;
43 // Create the shortcut in the Chrome Apps subdir.
44 creation_locations.applications_menu_location =
45 web_app::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS;
46 web_app::CreateShortcuts(
47 web_app::SHORTCUT_CREATION_AUTOMATED, creation_locations, profile, app);
50 bool ShouldCreateShortcutFor(const Extension* extension) {
51 return extension->is_platform_app() &&
52 extension->location() != extensions::Manifest::COMPONENT &&
53 extension->ShouldDisplayInAppLauncher();
56 } // namespace
58 // static
59 void AppShortcutManager::RegisterProfilePrefs(
60 user_prefs::PrefRegistrySyncable* registry) {
61 // Indicates whether app shortcuts have been created.
62 registry->RegisterBooleanPref(
63 prefs::kAppShortcutsHaveBeenCreated, false,
64 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
67 AppShortcutManager::AppShortcutManager(Profile* profile)
68 : profile_(profile),
69 is_profile_info_cache_observer_(false),
70 prefs_(profile->GetPrefs()) {
71 // 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).
73 DCHECK(!content::BrowserThread::IsThreadInitialized(
74 content::BrowserThread::UI) ||
75 content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
77 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
78 content::Source<Profile>(profile_));
79 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
80 content::Source<Profile>(profile_));
81 // Wait for extensions to be ready before running OnceOffCreateShortcuts.
82 registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
83 content::Source<Profile>(profile_));
85 ProfileManager* profile_manager = g_browser_process->profile_manager();
86 // profile_manager might be NULL in testing environments.
87 if (profile_manager) {
88 profile_manager->GetProfileInfoCache().AddObserver(this);
89 is_profile_info_cache_observer_ = true;
93 AppShortcutManager::~AppShortcutManager() {
94 if (g_browser_process && is_profile_info_cache_observer_) {
95 ProfileManager* profile_manager = g_browser_process->profile_manager();
96 // profile_manager might be NULL in testing environments or during shutdown.
97 if (profile_manager)
98 profile_manager->GetProfileInfoCache().RemoveObserver(this);
102 void AppShortcutManager::Observe(int type,
103 const content::NotificationSource& source,
104 const content::NotificationDetails& details) {
105 switch (type) {
106 case chrome::NOTIFICATION_EXTENSIONS_READY: {
107 OnceOffCreateShortcuts();
108 break;
110 case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
111 #if defined(OS_MACOSX)
112 if (!apps::IsAppShimsEnabled())
113 break;
114 #endif // defined(OS_MACOSX)
116 const extensions::InstalledExtensionInfo* installed_info =
117 content::Details<const extensions::InstalledExtensionInfo>(details)
118 .ptr();
119 const Extension* extension = installed_info->extension;
120 // 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
122 // shortcut in the applications menu (e.g., Start Menu).
123 if (installed_info->is_update) {
124 web_app::UpdateAllShortcuts(
125 base::UTF8ToUTF16(installed_info->old_name), profile_, extension);
126 } else if (ShouldCreateShortcutFor(extension)) {
127 CreateShortcutsInApplicationsMenu(profile_, extension);
129 break;
131 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: {
132 const Extension* extension = content::Details<const Extension>(
133 details).ptr();
134 web_app::DeleteAllShortcuts(profile_, extension);
135 break;
137 default:
138 NOTREACHED();
142 void AppShortcutManager::OnProfileWillBeRemoved(
143 const base::FilePath& profile_path) {
144 if (profile_path != profile_->GetPath())
145 return;
146 content::BrowserThread::PostTask(
147 content::BrowserThread::FILE, FROM_HERE,
148 base::Bind(&web_app::internals::DeleteAllShortcutsForProfile,
149 profile_path));
152 void AppShortcutManager::OnceOffCreateShortcuts() {
153 bool was_enabled = prefs_->GetBoolean(prefs::kAppShortcutsHaveBeenCreated);
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)
163 if (was_enabled != is_now_enabled)
164 prefs_->SetBoolean(prefs::kAppShortcutsHaveBeenCreated, is_now_enabled);
166 if (was_enabled || !is_now_enabled)
167 return;
169 // Check if extension system/service are available. They might not be in
170 // tests.
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;
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());