1 // Copyright 2013 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/search/app_result.h"
7 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/extensions/extension_system_factory.h"
9 #include "chrome/browser/extensions/extension_util.h"
10 #include "chrome/browser/extensions/install_tracker.h"
11 #include "chrome/browser/extensions/install_tracker_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/app_list/app_context_menu.h"
14 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
15 #include "chrome/browser/ui/app_list/search/tokenized_string.h"
16 #include "chrome/browser/ui/app_list/search/tokenized_string_match.h"
17 #include "chrome/browser/ui/extensions/extension_enable_flow.h"
18 #include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h"
19 #include "chrome/common/extensions/extension_icon_set.h"
20 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
21 #include "content/public/browser/user_metrics.h"
22 #include "extensions/common/extension.h"
23 #include "ui/gfx/color_utils.h"
24 #include "ui/gfx/image/image_skia_operations.h"
28 AppResult::AppResult(Profile
* profile
,
29 const std::string
& app_id
,
30 AppListControllerDelegate
* controller
)
33 controller_(controller
),
34 install_tracker_(NULL
) {
35 set_id(extensions::Extension::GetBaseURLFromExtensionId(app_id_
).spec());
37 const extensions::Extension
* extension
=
38 extensions::ExtensionSystem::Get(profile_
)->extension_service()
39 ->GetInstalledExtension(app_id_
);
42 is_platform_app_
= extension
->is_platform_app();
44 icon_
.reset(new extensions::IconImage(
47 extensions::IconsInfo::GetIcons(extension
),
48 extension_misc::EXTENSION_ICON_SMALL
,
49 extensions::IconsInfo::GetDefaultAppIcon(),
52 StartObservingInstall();
55 AppResult::~AppResult() {
56 StopObservingInstall();
59 void AppResult::UpdateFromMatch(const TokenizedString
& title
,
60 const TokenizedStringMatch
& match
) {
61 const TokenizedStringMatch::Hits
& hits
= match
.hits();
64 tags
.reserve(hits
.size());
65 for (size_t i
= 0; i
< hits
.size(); ++i
)
66 tags
.push_back(Tag(Tag::MATCH
, hits
[i
].start(), hits
[i
].end()));
68 set_title(title
.text());
70 set_relevance(match
.relevance());
73 void AppResult::Open(int event_flags
) {
74 const extensions::Extension
* extension
=
75 extensions::ExtensionSystem::Get(profile_
)->extension_service()
76 ->GetInstalledExtension(app_id_
);
80 // Check if enable flow is already running or should be started
81 if (RunExtensionEnableFlow())
84 CoreAppLauncherHandler::RecordAppListSearchLaunch(extension
);
85 content::RecordAction(
86 base::UserMetricsAction("AppList_ClickOnAppFromSearch"));
88 controller_
->ActivateApp(
91 AppListControllerDelegate::LAUNCH_FROM_APP_LIST_SEARCH
,
95 void AppResult::InvokeAction(int action_index
, int event_flags
) {}
97 scoped_ptr
<ChromeSearchResult
> AppResult::Duplicate() {
98 scoped_ptr
<ChromeSearchResult
> copy(
99 new AppResult(profile_
, app_id_
, controller_
));
100 copy
->set_title(title());
101 copy
->set_title_tags(title_tags());
106 ChromeSearchResultType
AppResult::GetType() {
107 return APP_SEARCH_RESULT
;
110 ui::MenuModel
* AppResult::GetContextMenuModel() {
111 if (!context_menu_
) {
112 context_menu_
.reset(new AppContextMenu(
113 this, profile_
, app_id_
, controller_
, is_platform_app_
, true));
116 return context_menu_
->GetMenuModel();
119 void AppResult::StartObservingInstall() {
120 DCHECK(!install_tracker_
);
122 install_tracker_
= extensions::InstallTrackerFactory::GetForProfile(profile_
);
123 install_tracker_
->AddObserver(this);
126 void AppResult::StopObservingInstall() {
127 if (install_tracker_
)
128 install_tracker_
->RemoveObserver(this);
130 install_tracker_
= NULL
;
133 bool AppResult::RunExtensionEnableFlow() {
134 const ExtensionService
* service
=
135 extensions::ExtensionSystem::Get(profile_
)->extension_service();
136 if (extension_util::IsAppLaunchableWithoutEnabling(app_id_
, service
))
139 if (!extension_enable_flow_
) {
140 controller_
->OnShowExtensionPrompt();
142 extension_enable_flow_
.reset(new ExtensionEnableFlow(
143 profile_
, app_id_
, this));
144 extension_enable_flow_
->StartForNativeWindow(
145 controller_
->GetAppListWindow());
150 void AppResult::UpdateIcon() {
151 gfx::ImageSkia icon
= icon_
->image_skia();
153 const ExtensionService
* service
=
154 extensions::ExtensionSystem::Get(profile_
)->extension_service();
155 const bool can_launch
= extension_util::IsAppLaunchable(app_id_
, service
);
157 const color_utils::HSL shift
= {-1, 0, 0.6};
158 icon
= gfx::ImageSkiaOperations::CreateHSLShiftedImage(icon
, shift
);
164 void AppResult::OnExtensionIconImageChanged(extensions::IconImage
* image
) {
165 DCHECK_EQ(icon_
.get(), image
);
169 void AppResult::ExecuteLaunchCommand(int event_flags
) {
173 void AppResult::ExtensionEnableFlowFinished() {
174 extension_enable_flow_
.reset();
175 controller_
->OnCloseExtensionPrompt();
177 // Automatically open app after enabling.
181 void AppResult::ExtensionEnableFlowAborted(bool user_initiated
) {
182 extension_enable_flow_
.reset();
183 controller_
->OnCloseExtensionPrompt();
186 void AppResult::OnBeginExtensionInstall(
187 const ExtensionInstallParams
& params
) {}
189 void AppResult::OnDownloadProgress(const std::string
& extension_id
,
190 int percent_downloaded
) {}
192 void AppResult::OnInstallFailure(const std::string
& extension_id
) {}
194 void AppResult::OnExtensionInstalled(const extensions::Extension
* extension
) {}
196 void AppResult::OnExtensionLoaded(const extensions::Extension
* extension
) {
200 void AppResult::OnExtensionUnloaded(const extensions::Extension
* extension
) {}
202 void AppResult::OnExtensionUninstalled(const extensions::Extension
* extension
) {
203 if (extension
->id() != app_id_
)
206 NotifyItemUninstalled();
209 void AppResult::OnAppsReordered() {}
211 void AppResult::OnAppInstalledToAppList(const std::string
& extension_id
) {}
213 void AppResult::OnShutdown() { StopObservingInstall(); }
215 } // namespace app_list