shortcut_manager.cc revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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
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  creation_locations.in_applications_menu = true;
44  // Create the shortcut in the Chrome Apps subdir.
45  creation_locations.applications_menu_subdir =
46      web_app::GetAppShortcutsSubdirName();
47  web_app::CreateShortcuts(shortcut_info, creation_locations,
48                           web_app::SHORTCUT_CREATION_AUTOMATED);
49}
50
51bool ShouldCreateShortcutFor(const extensions::Extension* extension) {
52  return extension->is_platform_app() &&
53      extension->location() != extensions::Manifest::COMPONENT &&
54      extension->ShouldDisplayInAppLauncher();
55}
56
57}  // namespace
58
59AppShortcutManager::AppShortcutManager(Profile* profile)
60    : profile_(profile),
61      is_profile_info_cache_observer_(false),
62      prefs_(profile->GetPrefs()),
63      weak_factory_(this) {
64  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALLED,
65                 content::Source<Profile>(profile_));
66  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED,
67                 content::Source<Profile>(profile_));
68  // Wait for extensions to be ready before running OnceOffCreateShortcuts.
69  registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
70                 content::Source<Profile>(profile_));
71
72  // TODO(mgiuca): This use of g_browser_process should be DCHECKed for
73  // content::BrowserThread::CurrentlyOn(content::BrowserThread::UI). This check
74  // currently fails browser_tests, due to http://crbug.com/251191.
75  ProfileManager* profile_manager = g_browser_process->profile_manager();
76  // profile_manager might be NULL in testing environments.
77  if (profile_manager) {
78    profile_manager->GetProfileInfoCache().AddObserver(this);
79    is_profile_info_cache_observer_ = true;
80  }
81}
82
83AppShortcutManager::~AppShortcutManager() {
84  if (g_browser_process && is_profile_info_cache_observer_) {
85    ProfileManager* profile_manager = g_browser_process->profile_manager();
86    // profile_manager might be NULL in testing environments or during shutdown.
87    if (profile_manager)
88      profile_manager->GetProfileInfoCache().RemoveObserver(this);
89  }
90}
91
92void AppShortcutManager::Observe(int type,
93                                 const content::NotificationSource& source,
94                                 const content::NotificationDetails& details) {
95  switch (type) {
96    case chrome::NOTIFICATION_EXTENSIONS_READY: {
97      OnceOffCreateShortcuts();
98      break;
99    }
100    case chrome::NOTIFICATION_EXTENSION_INSTALLED: {
101#if defined(OS_MACOSX)
102      if (!apps::IsAppShimsEnabled())
103        break;
104#endif  // defined(OS_MACOSX)
105
106      const extensions::InstalledExtensionInfo* installed_info =
107          content::Details<const extensions::InstalledExtensionInfo>(details)
108              .ptr();
109      const Extension* extension = installed_info->extension;
110      if (ShouldCreateShortcutFor(extension)) {
111        // 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
113        // shortcut in the applications menu (e.g., Start Menu).
114        base::Callback<void(const ShellIntegration::ShortcutInfo&)>
115            create_or_update;
116        if (installed_info->is_update) {
117          string16 old_title = UTF8ToUTF16(installed_info->old_name);
118          create_or_update = base::Bind(&web_app::UpdateAllShortcuts,
119                                        old_title);
120        } else {
121          create_or_update = base::Bind(&CreateShortcutsInApplicationsMenu);
122        }
123
124        web_app::UpdateShortcutInfoAndIconForApp(*extension, profile_,
125                                                 create_or_update);
126      }
127      break;
128    }
129    case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: {
130      const Extension* extension = content::Details<const Extension>(
131          details).ptr();
132      DeleteApplicationShortcuts(extension);
133      break;
134    }
135    default:
136      NOTREACHED();
137  }
138}
139
140void AppShortcutManager::OnProfileWillBeRemoved(
141    const base::FilePath& profile_path) {
142  if (profile_path != profile_->GetPath())
143    return;
144  content::BrowserThread::PostTask(
145      content::BrowserThread::FILE, FROM_HERE,
146      base::Bind(&web_app::internals::DeleteAllShortcutsForProfile,
147                 profile_path));
148}
149
150void AppShortcutManager::OnceOffCreateShortcuts() {
151  bool was_enabled = prefs_->GetBoolean(apps::prefs::kShortcutsHaveBeenCreated);
152
153  // Creation of shortcuts on Mac currently sits behind --enable-app-shims.
154  // Until it is enabled permanently, we need to check the flag, and set the
155  // pref accordingly.
156#if defined(OS_MACOSX)
157  bool is_now_enabled = apps::IsAppShimsEnabled();
158#else
159  bool is_now_enabled = true;
160#endif  // defined(OS_MACOSX)
161
162  if (was_enabled != is_now_enabled)
163    prefs_->SetBoolean(apps::prefs::kShortcutsHaveBeenCreated, is_now_enabled);
164
165  if (was_enabled || !is_now_enabled)
166    return;
167
168  // Check if extension system/service are available. They might not be in
169  // tests.
170  extensions::ExtensionSystem* extension_system;
171  ExtensionServiceInterface* extension_service;
172  if (!(extension_system = extensions::ExtensionSystem::Get(profile_)) ||
173      !(extension_service = extension_system->extension_service()))
174    return;
175
176  // Create an applications menu shortcut for each app in this profile.
177  const ExtensionSet* apps = extension_service->extensions();
178  for (ExtensionSet::const_iterator it = apps->begin();
179       it != apps->end(); ++it) {
180    if (ShouldCreateShortcutFor(it->get()))
181      web_app::UpdateShortcutInfoAndIconForApp(
182          *it->get(), profile_, base::Bind(&CreateShortcutsInApplicationsMenu));
183  }
184}
185
186void AppShortcutManager::DeleteApplicationShortcuts(
187    const Extension* extension) {
188  ShellIntegration::ShortcutInfo delete_info =
189      web_app::ShortcutInfoForExtensionAndProfile(extension, profile_);
190  web_app::DeleteAllShortcuts(delete_info);
191}
192