NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / app_list / app_list_controller_delegate.cc
blobd45be3b60a663dc0c029ac0c40492418ed82ec66
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.
5 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
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"
30 using extensions::ExtensionRegistry;
32 namespace {
34 const 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;
43 } // namespace
45 AppListControllerDelegate::~AppListControllerDelegate() {}
47 bool AppListControllerDelegate::ForceNativeDesktop() const {
48 return false;
51 void AppListControllerDelegate::ViewClosing() {}
53 void AppListControllerDelegate::OnShowExtensionPrompt() {}
54 void AppListControllerDelegate::OnCloseExtensionPrompt() {}
56 std::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();
68 bool 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);
78 bool AppListControllerDelegate::CanDoShowAppInfoFlow() {
79 return app_list::switches::IsAppInfoEnabled();
82 void 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);
93 gfx::NativeWindow parent_window = GetAppListWindow();
94 if (!parent_window)
95 return;
97 OnShowExtensionPrompt();
98 ShowChromeAppInfoDialog(
99 parent_window, profile, extension,
100 base::Bind(&AppListControllerDelegate::OnCloseExtensionPrompt,
101 base::Unretained(this)));
104 void 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();
112 void 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);
128 bool 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();
135 void 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;
143 const GURL url = extensions::ManifestURL::GetDetailsURL(extension);
144 DCHECK_NE(url, GURL::EmptyGURL());
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);
159 bool 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();
168 void 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;
175 chrome::NavigateParams params(
176 profile,
177 extensions::ManifestURL::GetOptionsPage(extension),
178 content::PAGE_TRANSITION_LINK);
179 chrome::Navigate(&params);
182 extensions::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));
191 void 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);
201 bool AppListControllerDelegate::IsExtensionInstalled(
202 Profile* profile, const std::string& app_id) {
203 return !!GetExtension(profile, app_id);
206 extensions::InstallTracker* AppListControllerDelegate::GetInstallTrackerFor(
207 Profile* profile) {
208 if (extensions::ExtensionSystem::Get(profile)->extension_service())
209 return extensions::InstallTrackerFactory::GetForProfile(profile);
210 return NULL;
213 void 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());