shortcut_manager.cc revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
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/extensions/extension_system.h"
17#include "chrome/browser/profiles/profile.h"
18#include "chrome/browser/profiles/profile_info_cache.h"
19#include "chrome/browser/profiles/profile_manager.h"
20#include "chrome/browser/shell_integration.h"
21#include "chrome/browser/ui/web_applications/web_app_ui.h"
22#include "chrome/browser/web_applications/web_app.h"
23#include "chrome/common/chrome_switches.h"
24#include "chrome/common/extensions/extension_set.h"
25#include "chrome/common/pref_names.h"
26#include "components/user_prefs/pref_registry_syncable.h"
27#include "content/public/browser/browser_thread.h"
28#include "content/public/browser/notification_details.h"
29#include "content/public/browser/notification_source.h"
30
31#if defined(OS_MACOSX)
32#include "apps/app_shim/app_shim_mac.h"
33#endif
34
35using extensions::Extension;
36
37namespace {
38
39// Creates a shortcut for an application in the applications menu, if there is
40// not already one present.
41void CreateShortcutsInApplicationsMenu(
42    const ShellIntegration::ShortcutInfo& shortcut_info) {
43  ShellIntegration::ShortcutLocations creation_locations;
44  creation_locations.in_applications_menu = true;
45  // Create the shortcut in the Chrome Apps subdir.
46  creation_locations.applications_menu_subdir =
47      web_app::GetAppShortcutsSubdirName();
48  web_app::CreateShortcuts(shortcut_info, creation_locations,
49                           web_app::SHORTCUT_CREATION_AUTOMATED);
50}
51
52bool ShouldCreateShortcutFor(const extensions::Extension* extension) {
53  return extension->is_platform_app() &&
54      extension->location() != extensions::Manifest::COMPONENT &&
55      extension->ShouldDisplayInAppLauncher();
56}
57
58}  // namespace
59
60// static
61void AppShortcutManager::RegisterProfilePrefs(
62    user_prefs::PrefRegistrySyncable* registry) {
63  // Indicates whether app shortcuts have been created.
64  registry->RegisterBooleanPref(
65      prefs::kAppShortcutsHaveBeenCreated, false,
66      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
67}
68
69AppShortcutManager::AppShortcutManager(Profile* profile)
70    : profile_(profile),
71      is_profile_info_cache_observer_(false),
72      prefs_(profile->GetPrefs()),
73      weak_factory_(this) {
74  // Use of g_browser_process requires that we are either on the UI thread, or
75  // there are no threads initialized (such as in unit tests).
76  DCHECK(!content::BrowserThread::IsThreadInitialized(
77             content::BrowserThread::UI) ||
78         content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
79
80  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
81                 content::Source<Profile>(profile_));
82  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
83                 content::Source<Profile>(profile_));
84  // Wait for extensions to be ready before running OnceOffCreateShortcuts.
85  registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
86                 content::Source<Profile>(profile_));
87
88  ProfileManager* profile_manager = g_browser_process->profile_manager();
89  // profile_manager might be NULL in testing environments.
90  if (profile_manager) {
91    profile_manager->GetProfileInfoCache().AddObserver(this);
92    is_profile_info_cache_observer_ = true;
93  }
94}
95
96AppShortcutManager::~AppShortcutManager() {
97  if (g_browser_process && is_profile_info_cache_observer_) {
98    ProfileManager* profile_manager = g_browser_process->profile_manager();
99    // profile_manager might be NULL in testing environments or during shutdown.
100    if (profile_manager)
101      profile_manager->GetProfileInfoCache().RemoveObserver(this);
102  }
103}
104
105void AppShortcutManager::Observe(int type,
106                                 const content::NotificationSource& source,
107                                 const content::NotificationDetails& details) {
108  switch (type) {
109    case chrome::NOTIFICATION_EXTENSIONS_READY: {
110      OnceOffCreateShortcuts();
111      break;
112    }
113    case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
114#if defined(OS_MACOSX)
115      if (!apps::IsAppShimsEnabled())
116        break;
117#endif  // defined(OS_MACOSX)
118
119      const extensions::InstalledExtensionInfo* installed_info =
120          content::Details<const extensions::InstalledExtensionInfo>(details)
121              .ptr();
122      const Extension* extension = installed_info->extension;
123      if (ShouldCreateShortcutFor(extension)) {
124        // If the app is being updated, update any existing shortcuts but do not
125        // create new ones. If it is being installed, automatically create a
126        // shortcut in the applications menu (e.g., Start Menu).
127        base::Callback<void(const ShellIntegration::ShortcutInfo&)>
128            create_or_update;
129        if (installed_info->is_update) {
130          string16 old_title = UTF8ToUTF16(installed_info->old_name);
131          create_or_update = base::Bind(&web_app::UpdateAllShortcuts,
132                                        old_title);
133        } else {
134          create_or_update = base::Bind(&CreateShortcutsInApplicationsMenu);
135        }
136
137        web_app::UpdateShortcutInfoAndIconForApp(*extension, profile_,
138                                                 create_or_update);
139      }
140      break;
141    }
142    case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: {
143      const Extension* extension = content::Details<const Extension>(
144          details).ptr();
145      DeleteApplicationShortcuts(extension);
146      break;
147    }
148    default:
149      NOTREACHED();
150  }
151}
152
153void AppShortcutManager::OnProfileWillBeRemoved(
154    const base::FilePath& profile_path) {
155  if (profile_path != profile_->GetPath())
156    return;
157  content::BrowserThread::PostTask(
158      content::BrowserThread::FILE, FROM_HERE,
159      base::Bind(&web_app::internals::DeleteAllShortcutsForProfile,
160                 profile_path));
161}
162
163void AppShortcutManager::OnceOffCreateShortcuts() {
164  bool was_enabled = prefs_->GetBoolean(prefs::kAppShortcutsHaveBeenCreated);
165
166  // Creation of shortcuts on Mac currently sits behind --enable-app-shims.
167  // Until it is enabled permanently, we need to check the flag, and set the
168  // pref accordingly.
169#if defined(OS_MACOSX)
170  bool is_now_enabled = apps::IsAppShimsEnabled();
171#else
172  bool is_now_enabled = true;
173#endif  // defined(OS_MACOSX)
174
175  if (was_enabled != is_now_enabled)
176    prefs_->SetBoolean(prefs::kAppShortcutsHaveBeenCreated, is_now_enabled);
177
178  if (was_enabled || !is_now_enabled)
179    return;
180
181  // Check if extension system/service are available. They might not be in
182  // tests.
183  extensions::ExtensionSystem* extension_system;
184  ExtensionServiceInterface* extension_service;
185  if (!(extension_system = extensions::ExtensionSystem::Get(profile_)) ||
186      !(extension_service = extension_system->extension_service()))
187    return;
188
189  // Create an applications menu shortcut for each app in this profile.
190  const ExtensionSet* apps = extension_service->extensions();
191  for (ExtensionSet::const_iterator it = apps->begin();
192       it != apps->end(); ++it) {
193    if (ShouldCreateShortcutFor(it->get()))
194      web_app::UpdateShortcutInfoAndIconForApp(
195          *it->get(), profile_, base::Bind(&CreateShortcutsInApplicationsMenu));
196  }
197}
198
199void AppShortcutManager::DeleteApplicationShortcuts(
200    const Extension* extension) {
201  ShellIntegration::ShortcutInfo delete_info =
202      web_app::ShortcutInfoForExtensionAndProfile(extension, profile_);
203  web_app::DeleteAllShortcuts(delete_info);
204}
205