app_list_controller_delegate.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
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_system.h"
9#include "chrome/browser/extensions/extension_util.h"
10#include "chrome/browser/extensions/install_tracker_factory.h"
11#include "chrome/browser/extensions/launch_util.h"
12#include "chrome/browser/ui/app_list/extension_uninstaller.h"
13#include "chrome/browser/ui/browser_navigator.h"
14#include "chrome/common/extensions/extension_constants.h"
15#include "chrome/common/extensions/extension_set.h"
16#include "chrome/common/extensions/manifest_url_handler.h"
17#include "extensions/browser/management_policy.h"
18#include "extensions/common/extension.h"
19#include "net/base/url_util.h"
20
21namespace {
22
23const extensions::Extension* GetExtension(Profile* profile,
24                              const std::string& extension_id) {
25  const ExtensionService* service =
26      extensions::ExtensionSystem::Get(profile)->extension_service();
27  const extensions::Extension* extension =
28      service->GetInstalledExtension(extension_id);
29  return extension;
30}
31
32}  // namespace
33
34AppListControllerDelegate::~AppListControllerDelegate() {}
35
36bool AppListControllerDelegate::ForceNativeDesktop() const {
37  return false;
38}
39
40void AppListControllerDelegate::ViewClosing() {}
41
42void AppListControllerDelegate::OnShowExtensionPrompt() {}
43void AppListControllerDelegate::OnCloseExtensionPrompt() {}
44
45std::string AppListControllerDelegate::AppListSourceToString(
46    AppListSource source) {
47  switch (source) {
48    case LAUNCH_FROM_APP_LIST:
49      return extension_urls::kLaunchSourceAppList;
50    case LAUNCH_FROM_APP_LIST_SEARCH:
51      return extension_urls::kLaunchSourceAppListSearch;
52    default: return std::string();
53  }
54}
55
56bool AppListControllerDelegate::UserMayModifySettings(
57    Profile* profile,
58    const std::string& app_id) {
59  const extensions::Extension* extension = GetExtension(profile, app_id);
60  const extensions::ManagementPolicy* policy =
61      extensions::ExtensionSystem::Get(profile)->management_policy();
62  return extension &&
63         policy->UserMayModifySettings(extension, NULL);
64}
65
66void AppListControllerDelegate::UninstallApp(Profile* profile,
67                                             const std::string& app_id) {
68  // ExtensionUninstall deletes itself when done or aborted.
69  ExtensionUninstaller* uninstaller =
70      new ExtensionUninstaller(profile, app_id, this);
71  uninstaller->Run();
72}
73
74bool AppListControllerDelegate::IsAppFromWebStore(
75    Profile* profile,
76    const std::string& app_id) {
77  const extensions::Extension* extension = GetExtension(profile, app_id);
78  return extension && extension->from_webstore();
79}
80
81void AppListControllerDelegate::ShowAppInWebStore(
82    Profile* profile,
83    const std::string& app_id,
84    bool is_search_result) {
85  const extensions::Extension* extension = GetExtension(profile, app_id);
86  if (!extension)
87    return;
88
89  const GURL url = extensions::ManifestURL::GetDetailsURL(extension);
90  DCHECK_NE(url, GURL::EmptyGURL());
91
92  const std::string source = AppListSourceToString(
93      is_search_result ?
94          AppListControllerDelegate::LAUNCH_FROM_APP_LIST_SEARCH :
95          AppListControllerDelegate::LAUNCH_FROM_APP_LIST);
96  chrome::NavigateParams params(
97      profile,
98      net::AppendQueryParameter(url,
99                                extension_urls::kWebstoreSourceField,
100                                source),
101      content::PAGE_TRANSITION_LINK);
102  chrome::Navigate(&params);
103}
104
105bool AppListControllerDelegate::HasOptionsPage(
106    Profile* profile,
107    const std::string& app_id) {
108  const ExtensionService* service =
109      extensions::ExtensionSystem::Get(profile)->extension_service();
110  const extensions::Extension* extension = GetExtension(profile, app_id);
111  return extension_util::IsAppLaunchableWithoutEnabling(app_id, service) &&
112         extension &&
113         !extensions::ManifestURL::GetOptionsPage(extension).is_empty();
114}
115
116void AppListControllerDelegate::ShowOptionsPage(
117    Profile* profile,
118    const std::string& app_id) {
119  const extensions::Extension* extension = GetExtension(profile, app_id);
120  if (!extension)
121    return;
122
123  chrome::NavigateParams params(
124      profile,
125      extensions::ManifestURL::GetOptionsPage(extension),
126      content::PAGE_TRANSITION_LINK);
127  chrome::Navigate(&params);
128}
129
130extensions::LaunchType AppListControllerDelegate::GetExtensionLaunchType(
131    Profile* profile,
132    const std::string& app_id) {
133  ExtensionService* service =
134      extensions::ExtensionSystem::Get(profile)->extension_service();
135  return extensions::GetLaunchType(service->extension_prefs(),
136                                   GetExtension(profile, app_id));
137}
138
139void AppListControllerDelegate::SetExtensionLaunchType(
140    Profile* profile,
141    const std::string& extension_id,
142    extensions::LaunchType launch_type) {
143  ExtensionService* service =
144      extensions::ExtensionSystem::Get(profile)->extension_service();
145  extensions::SetLaunchType(
146      service->extension_prefs(), extension_id, launch_type);
147}
148
149bool AppListControllerDelegate::IsExtensionInstalled(
150    Profile* profile, const std::string& app_id) {
151  return !!GetExtension(profile, app_id);
152}
153
154extensions::InstallTracker* AppListControllerDelegate::GetInstallTrackerFor(
155    Profile* profile) {
156  if (extensions::ExtensionSystem::Get(profile)->extension_service())
157    return extensions::InstallTrackerFactory::GetForProfile(profile);
158  return NULL;
159}
160
161void AppListControllerDelegate::GetApps(Profile* profile,
162                                        ExtensionSet* out_apps) {
163  ExtensionService* service =
164      extensions::ExtensionSystem::Get(profile)->extension_service();
165  DCHECK(service);
166  out_apps->InsertAll(*service->extensions());
167  out_apps->InsertAll(*service->disabled_extensions());
168  out_apps->InsertAll(*service->terminated_extensions());
169}
170