Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / app_list / app_list_controller_delegate.cc
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_prefs.h"
20 #include "extensions/browser/extension_registry.h"
21 #include "extensions/browser/extension_system.h"
22 #include "extensions/browser/management_policy.h"
23 #include "extensions/common/extension.h"
24 #include "extensions/common/extension_set.h"
25 #include "net/base/url_util.h"
26 #include "ui/app_list/app_list_folder_item.h"
27 #include "ui/app_list/app_list_item.h"
28 #include "ui/app_list/app_list_model.h"
29 #include "ui/app_list/app_list_switches.h"
30
31 #if defined(ENABLE_RLZ)
32 #include "chrome/browser/rlz/rlz.h"
33 #endif
34
35 using extensions::ExtensionRegistry;
36
37 namespace {
38
39 const extensions::Extension* GetExtension(Profile* profile,
40                                           const std::string& extension_id) {
41   const ExtensionService* service =
42       extensions::ExtensionSystem::Get(profile)->extension_service();
43   const extensions::Extension* extension =
44       service->GetInstalledExtension(extension_id);
45   return extension;
46 }
47
48 }  // namespace
49
50 AppListControllerDelegate::~AppListControllerDelegate() {}
51
52 bool AppListControllerDelegate::ForceNativeDesktop() const {
53   return false;
54 }
55
56 void AppListControllerDelegate::ViewClosing() {}
57
58 void AppListControllerDelegate::OnShowExtensionPrompt() {}
59 void AppListControllerDelegate::OnCloseExtensionPrompt() {}
60
61 std::string AppListControllerDelegate::AppListSourceToString(
62     AppListSource source) {
63   switch (source) {
64     case LAUNCH_FROM_APP_LIST:
65       return extension_urls::kLaunchSourceAppList;
66     case LAUNCH_FROM_APP_LIST_SEARCH:
67       return extension_urls::kLaunchSourceAppListSearch;
68     default:
69       return std::string();
70   }
71 }
72
73 bool AppListControllerDelegate::UserMayModifySettings(
74     Profile* profile,
75     const std::string& app_id) {
76   const extensions::Extension* extension = GetExtension(profile, app_id);
77   const extensions::ManagementPolicy* policy =
78       extensions::ExtensionSystem::Get(profile)->management_policy();
79   return extension &&
80          policy->UserMayModifySettings(extension, NULL);
81 }
82
83 bool AppListControllerDelegate::CanDoShowAppInfoFlow() {
84   return app_list::switches::IsAppInfoEnabled();
85 }
86
87 void AppListControllerDelegate::DoShowAppInfoFlow(
88     Profile* profile,
89     const std::string& extension_id) {
90   DCHECK(CanDoShowAppInfoFlow());
91   ExtensionService* service =
92       extensions::ExtensionSystem::Get(profile)->extension_service();
93   DCHECK(service);
94   const extensions::Extension* extension = service->GetInstalledExtension(
95       extension_id);
96   DCHECK(extension);
97
98   gfx::NativeWindow parent_window = GetAppListWindow();
99   if (!parent_window)
100     return;
101
102   OnShowExtensionPrompt();
103   ShowAppInfoDialog(
104       parent_window,
105       profile,
106       extension,
107       base::Bind(&AppListControllerDelegate::OnCloseExtensionPrompt,
108                  base::Unretained(this)));
109 }
110
111 void AppListControllerDelegate::UninstallApp(Profile* profile,
112                                              const std::string& app_id) {
113   // ExtensionUninstall deletes itself when done or aborted.
114   ExtensionUninstaller* uninstaller =
115       new ExtensionUninstaller(profile, app_id, this);
116   uninstaller->Run();
117 }
118
119 bool AppListControllerDelegate::IsAppFromWebStore(
120     Profile* profile,
121     const std::string& app_id) {
122   const extensions::Extension* extension = GetExtension(profile, app_id);
123   return extension && extension->from_webstore();
124 }
125
126 void AppListControllerDelegate::ShowAppInWebStore(
127     Profile* profile,
128     const std::string& app_id,
129     bool is_search_result) {
130   const extensions::Extension* extension = GetExtension(profile, app_id);
131   if (!extension)
132     return;
133
134   const GURL url = extensions::ManifestURL::GetDetailsURL(extension);
135   DCHECK_NE(url, GURL::EmptyGURL());
136
137   const std::string source = AppListSourceToString(
138       is_search_result ?
139           AppListControllerDelegate::LAUNCH_FROM_APP_LIST_SEARCH :
140           AppListControllerDelegate::LAUNCH_FROM_APP_LIST);
141   chrome::NavigateParams params(
142       profile,
143       net::AppendQueryParameter(url,
144                                 extension_urls::kWebstoreSourceField,
145                                 source),
146       content::PAGE_TRANSITION_LINK);
147   chrome::Navigate(&params);
148 }
149
150 bool AppListControllerDelegate::HasOptionsPage(
151     Profile* profile,
152     const std::string& app_id) {
153   const extensions::Extension* extension = GetExtension(profile, app_id);
154   return extensions::util::IsAppLaunchableWithoutEnabling(app_id, profile) &&
155          extension &&
156          !extensions::ManifestURL::GetOptionsPage(extension).is_empty();
157 }
158
159 void AppListControllerDelegate::ShowOptionsPage(
160     Profile* profile,
161     const std::string& app_id) {
162   const extensions::Extension* extension = GetExtension(profile, app_id);
163   if (!extension)
164     return;
165
166   chrome::NavigateParams params(
167       profile,
168       extensions::ManifestURL::GetOptionsPage(extension),
169       content::PAGE_TRANSITION_LINK);
170   chrome::Navigate(&params);
171 }
172
173 extensions::LaunchType AppListControllerDelegate::GetExtensionLaunchType(
174     Profile* profile,
175     const std::string& app_id) {
176   return extensions::GetLaunchType(extensions::ExtensionPrefs::Get(profile),
177                                    GetExtension(profile, app_id));
178 }
179
180 void AppListControllerDelegate::SetExtensionLaunchType(
181     Profile* profile,
182     const std::string& extension_id,
183     extensions::LaunchType launch_type) {
184   ExtensionService* service =
185       extensions::ExtensionSystem::Get(profile)->extension_service();
186   extensions::SetLaunchType(
187       service, extension_id, launch_type);
188 }
189
190 bool AppListControllerDelegate::IsExtensionInstalled(
191     Profile* profile, const std::string& app_id) {
192   return !!GetExtension(profile, app_id);
193 }
194
195 extensions::InstallTracker* AppListControllerDelegate::GetInstallTrackerFor(
196     Profile* profile) {
197   if (extensions::ExtensionSystem::Get(profile)->extension_service())
198     return extensions::InstallTrackerFactory::GetForProfile(profile);
199   return NULL;
200 }
201
202 void AppListControllerDelegate::GetApps(Profile* profile,
203                                         extensions::ExtensionSet* out_apps) {
204   ExtensionRegistry* registry = ExtensionRegistry::Get(profile);
205   DCHECK(registry);
206   out_apps->InsertAll(registry->enabled_extensions());
207   out_apps->InsertAll(registry->disabled_extensions());
208   out_apps->InsertAll(registry->terminated_extensions());
209 }
210
211 void AppListControllerDelegate::OnSearchStarted() {
212 #if defined(ENABLE_RLZ)
213   RLZTracker::RecordAppListSearch();
214 #endif
215 }