app_list_controller_delegate.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright (c) 2012 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/ui/app_list/app_list_controller_delegate.h"
6
7#include "chrome/browser/extensions/extension_service.h"
8#include "chrome/browser/extensions/extension_util.h"
9#include "chrome/browser/extensions/install_tracker_factory.h"
10#include "chrome/browser/extensions/launch_util.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/ui/app_list/app_list_syncable_service.h"
13#include "chrome/browser/ui/app_list/app_list_syncable_service_factory.h"
14#include "chrome/browser/ui/app_list/extension_uninstaller.h"
15#include "chrome/browser/ui/apps/app_info_dialog.h"
16#include "chrome/browser/ui/browser_navigator.h"
17#include "chrome/common/extensions/extension_constants.h"
18#include "chrome/common/extensions/manifest_url_handler.h"
19#include "extensions/browser/extension_registry.h"
20#include "extensions/browser/extension_system.h"
21#include "extensions/browser/management_policy.h"
22#include "extensions/common/extension.h"
23#include "extensions/common/extension_set.h"
24#include "net/base/url_util.h"
25#include "ui/app_list/app_list_folder_item.h"
26#include "ui/app_list/app_list_item.h"
27#include "ui/app_list/app_list_model.h"
28#include "ui/app_list/app_list_switches.h"
29
30using extensions::ExtensionRegistry;
31
32namespace {
33
34const extensions::Extension* GetExtension(Profile* profile,
35                                          const std::string& extension_id) {
36  const ExtensionService* service =
37      extensions::ExtensionSystem::Get(profile)->extension_service();
38  const extensions::Extension* extension =
39      service->GetInstalledExtension(extension_id);
40  return extension;
41}
42
43}  // namespace
44
45AppListControllerDelegate::~AppListControllerDelegate() {}
46
47bool AppListControllerDelegate::ForceNativeDesktop() const {
48  return false;
49}
50
51void AppListControllerDelegate::ViewClosing() {}
52
53void AppListControllerDelegate::OnShowExtensionPrompt() {}
54void AppListControllerDelegate::OnCloseExtensionPrompt() {}
55
56std::string AppListControllerDelegate::AppListSourceToString(
57    AppListSource source) {
58  switch (source) {
59    case LAUNCH_FROM_APP_LIST:
60      return extension_urls::kLaunchSourceAppList;
61    case LAUNCH_FROM_APP_LIST_SEARCH:
62      return extension_urls::kLaunchSourceAppListSearch;
63    default:
64      return std::string();
65  }
66}
67
68bool AppListControllerDelegate::UserMayModifySettings(
69    Profile* profile,
70    const std::string& app_id) {
71  const extensions::Extension* extension = GetExtension(profile, app_id);
72  const extensions::ManagementPolicy* policy =
73      extensions::ExtensionSystem::Get(profile)->management_policy();
74  return extension &&
75         policy->UserMayModifySettings(extension, NULL);
76}
77
78bool AppListControllerDelegate::CanDoShowAppInfoFlow() {
79  return app_list::switches::IsAppInfoEnabled();
80}
81
82void AppListControllerDelegate::DoShowAppInfoFlow(
83    Profile* profile,
84    const std::string& extension_id) {
85  DCHECK(CanDoShowAppInfoFlow());
86  ExtensionService* service =
87      extensions::ExtensionSystem::Get(profile)->extension_service();
88  DCHECK(service);
89  const extensions::Extension* extension = service->GetInstalledExtension(
90      extension_id);
91  DCHECK(extension);
92
93  gfx::NativeWindow parent_window = GetAppListWindow();
94  if (!parent_window)
95    return;
96
97  OnShowExtensionPrompt();
98  ShowChromeAppInfoDialog(
99      parent_window, profile, extension,
100      base::Bind(&AppListControllerDelegate::OnCloseExtensionPrompt,
101                 base::Unretained(this)));
102}
103
104void AppListControllerDelegate::UninstallApp(Profile* profile,
105                                             const std::string& app_id) {
106  // ExtensionUninstall deletes itself when done or aborted.
107  ExtensionUninstaller* uninstaller =
108      new ExtensionUninstaller(profile, app_id, this);
109  uninstaller->Run();
110}
111
112void AppListControllerDelegate::RemoveAppFromFolder(Profile* profile,
113                                                    const std::string& app_id) {
114  app_list::AppListModel* model =
115      app_list::AppListSyncableServiceFactory::GetForProfile(
116          profile)->model();
117  app_list::AppListItem* item = model->FindItem(app_id);
118  DCHECK(item) << "App not found in model: " << app_id;
119  syncer::StringOrdinal position;
120  app_list::AppListFolderItem* folder_item =
121      model->FindFolderItem(item->folder_id());
122  DCHECK(folder_item) << "No folder for item: " << item->ToDebugString();
123  // Position the item just after the folder.
124  position = folder_item->position().CreateAfter();
125  model->MoveItemToFolderAt(item, "", position);
126}
127
128bool AppListControllerDelegate::IsAppFromWebStore(
129    Profile* profile,
130    const std::string& app_id) {
131  const extensions::Extension* extension = GetExtension(profile, app_id);
132  return extension && extension->from_webstore();
133}
134
135void AppListControllerDelegate::ShowAppInWebStore(
136    Profile* profile,
137    const std::string& app_id,
138    bool is_search_result) {
139  const extensions::Extension* extension = GetExtension(profile, app_id);
140  if (!extension)
141    return;
142
143  const GURL url = extensions::ManifestURL::GetDetailsURL(extension);
144  DCHECK_NE(url, GURL::EmptyGURL());
145
146  const std::string source = AppListSourceToString(
147      is_search_result ?
148          AppListControllerDelegate::LAUNCH_FROM_APP_LIST_SEARCH :
149          AppListControllerDelegate::LAUNCH_FROM_APP_LIST);
150  chrome::NavigateParams params(
151      profile,
152      net::AppendQueryParameter(url,
153                                extension_urls::kWebstoreSourceField,
154                                source),
155      content::PAGE_TRANSITION_LINK);
156  chrome::Navigate(&params);
157}
158
159bool AppListControllerDelegate::HasOptionsPage(
160    Profile* profile,
161    const std::string& app_id) {
162  const extensions::Extension* extension = GetExtension(profile, app_id);
163  return extensions::util::IsAppLaunchableWithoutEnabling(app_id, profile) &&
164         extension &&
165         !extensions::ManifestURL::GetOptionsPage(extension).is_empty();
166}
167
168void AppListControllerDelegate::ShowOptionsPage(
169    Profile* profile,
170    const std::string& app_id) {
171  const extensions::Extension* extension = GetExtension(profile, app_id);
172  if (!extension)
173    return;
174
175  chrome::NavigateParams params(
176      profile,
177      extensions::ManifestURL::GetOptionsPage(extension),
178      content::PAGE_TRANSITION_LINK);
179  chrome::Navigate(&params);
180}
181
182extensions::LaunchType AppListControllerDelegate::GetExtensionLaunchType(
183    Profile* profile,
184    const std::string& app_id) {
185  ExtensionService* service =
186      extensions::ExtensionSystem::Get(profile)->extension_service();
187  return extensions::GetLaunchType(service->extension_prefs(),
188                                   GetExtension(profile, app_id));
189}
190
191void AppListControllerDelegate::SetExtensionLaunchType(
192    Profile* profile,
193    const std::string& extension_id,
194    extensions::LaunchType launch_type) {
195  ExtensionService* service =
196      extensions::ExtensionSystem::Get(profile)->extension_service();
197  extensions::SetLaunchType(
198      service, extension_id, launch_type);
199}
200
201bool AppListControllerDelegate::IsExtensionInstalled(
202    Profile* profile, const std::string& app_id) {
203  return !!GetExtension(profile, app_id);
204}
205
206extensions::InstallTracker* AppListControllerDelegate::GetInstallTrackerFor(
207    Profile* profile) {
208  if (extensions::ExtensionSystem::Get(profile)->extension_service())
209    return extensions::InstallTrackerFactory::GetForProfile(profile);
210  return NULL;
211}
212
213void AppListControllerDelegate::GetApps(Profile* profile,
214                                        extensions::ExtensionSet* out_apps) {
215  ExtensionRegistry* registry = ExtensionRegistry::Get(profile);
216  DCHECK(registry);
217  out_apps->InsertAll(registry->enabled_extensions());
218  out_apps->InsertAll(registry->disabled_extensions());
219  out_apps->InsertAll(registry->terminated_extensions());
220}
221