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