shortcut_manager.cc revision e5d81f57cb97b3b6b7fccc9c5610d21eb81db09d
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.
4
5#include "chrome/browser/apps/shortcut_manager.h"
6
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"
29
30#if defined(OS_MACOSX)
31#include "apps/app_shim/app_shim_mac.h"
32#endif
33
34using extensions::Extension;
35
36namespace {
37
38// Creates a shortcut for an application in the applications menu, if there is
39// not already one present.
40void CreateShortcutsInApplicationsMenu(
41    const ShellIntegration::ShortcutInfo& shortcut_info) {
42  ShellIntegration::ShortcutLocations creation_locations;
43  // Create the shortcut in the Chrome Apps subdir.
44  creation_locations.applications_menu_location =
45      ShellIntegration::APP_MENU_LOCATION_SUBDIR_CHROMEAPPS;
46  web_app::CreateShortcuts(shortcut_info, creation_locations,
47                           web_app::SHORTCUT_CREATION_AUTOMATED);
48}
49
50bool ShouldCreateShortcutFor(const extensions::Extension* extension) {
51  return extension->is_platform_app() &&
52      extension->location() != extensions::Manifest::COMPONENT &&
53      extension->ShouldDisplayInAppLauncher();
54}
55
56}  // namespace
57
58// static
59void 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);
65}
66
67AppShortcutManager::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));
76
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_));
84
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;
90  }
91}
92
93AppShortcutManager::~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);
99  }
100}
101
102void 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;
109    }
110    case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
111#if defined(OS_MACOSX)
112      if (!apps::IsAppShimsEnabled())
113        break;
114#endif  // defined(OS_MACOSX)
115
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      base::Callback<void(const ShellIntegration::ShortcutInfo&)>
124          create_or_update;
125      if (installed_info->is_update) {
126        base::string16 old_title =
127            base::UTF8ToUTF16(installed_info->old_name);
128        create_or_update = base::Bind(&web_app::UpdateAllShortcuts,
129                                      old_title);
130      } else if (ShouldCreateShortcutFor(extension)) {
131        create_or_update = base::Bind(&CreateShortcutsInApplicationsMenu);
132      }
133      if (!create_or_update.is_null()) {
134        web_app::UpdateShortcutInfoAndIconForApp(extension, profile_,
135                                                 create_or_update);
136      }
137      break;
138    }
139    case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: {
140      const Extension* extension = content::Details<const Extension>(
141          details).ptr();
142      DeleteApplicationShortcuts(extension);
143      break;
144    }
145    default:
146      NOTREACHED();
147  }
148}
149
150void AppShortcutManager::OnProfileWillBeRemoved(
151    const base::FilePath& profile_path) {
152  if (profile_path != profile_->GetPath())
153    return;
154  content::BrowserThread::PostTask(
155      content::BrowserThread::FILE, FROM_HERE,
156      base::Bind(&web_app::internals::DeleteAllShortcutsForProfile,
157                 profile_path));
158}
159
160void AppShortcutManager::OnceOffCreateShortcuts() {
161  bool was_enabled = prefs_->GetBoolean(prefs::kAppShortcutsHaveBeenCreated);
162
163  // Creation of shortcuts on Mac currently can be disabled with
164  // --disable-app-shims, so check the flag, and set the pref accordingly.
165#if defined(OS_MACOSX)
166  bool is_now_enabled = apps::IsAppShimsEnabled();
167#else
168  bool is_now_enabled = true;
169#endif  // defined(OS_MACOSX)
170
171  if (was_enabled != is_now_enabled)
172    prefs_->SetBoolean(prefs::kAppShortcutsHaveBeenCreated, is_now_enabled);
173
174  if (was_enabled || !is_now_enabled)
175    return;
176
177  // Check if extension system/service are available. They might not be in
178  // tests.
179  extensions::ExtensionSystem* extension_system;
180  ExtensionServiceInterface* extension_service;
181  if (!(extension_system = extensions::ExtensionSystem::Get(profile_)) ||
182      !(extension_service = extension_system->extension_service()))
183    return;
184
185  // Create an applications menu shortcut for each app in this profile.
186  const extensions::ExtensionSet* apps = extension_service->extensions();
187  for (extensions::ExtensionSet::const_iterator it = apps->begin();
188       it != apps->end(); ++it) {
189    if (ShouldCreateShortcutFor(it->get()))
190      web_app::UpdateShortcutInfoAndIconForApp(
191          it->get(), profile_, base::Bind(&CreateShortcutsInApplicationsMenu));
192  }
193}
194
195void AppShortcutManager::DeleteApplicationShortcuts(
196    const Extension* extension) {
197  ShellIntegration::ShortcutInfo delete_info =
198      web_app::ShortcutInfoForExtensionAndProfile(extension, profile_);
199  web_app::DeleteAllShortcuts(delete_info);
200}
201