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