Make app list recommendations into their own DisplayType.
[chromium-blink-merge.git] / chrome / browser / ui / app_list / search / app_result.cc
blob21f31c0b6ff661c18d32d9c9eb52a8469f53be8c
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 "base/time/time.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/extensions/extension_util.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/app_list/app_context_menu.h"
12 #include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
13 #include "chrome/browser/ui/app_list/search/search_util.h"
14 #include "chrome/browser/ui/extensions/extension_enable_flow.h"
15 #include "chrome/common/extensions/extension_metrics.h"
16 #include "content/public/browser/user_metrics.h"
17 #include "extensions/browser/extension_registry.h"
18 #include "extensions/browser/extension_system.h"
19 #include "extensions/browser/extension_system_provider.h"
20 #include "extensions/browser/extensions_browser_client.h"
21 #include "extensions/common/constants.h"
22 #include "extensions/common/extension.h"
23 #include "extensions/common/extension_icon_set.h"
24 #include "extensions/common/manifest_handlers/icons_handler.h"
25 #include "ui/app_list/app_list_switches.h"
26 #include "ui/app_list/search/tokenized_string.h"
27 #include "ui/app_list/search/tokenized_string_match.h"
28 #include "ui/gfx/color_utils.h"
29 #include "ui/gfx/image/image_skia_operations.h"
31 namespace app_list {
33 AppResult::AppResult(Profile* profile,
34 const std::string& app_id,
35 AppListControllerDelegate* controller,
36 bool is_recommendation)
37 : profile_(profile),
38 app_id_(app_id),
39 controller_(controller),
40 extension_registry_(NULL) {
41 set_id(extensions::Extension::GetBaseURLFromExtensionId(app_id_).spec());
42 if (app_list::switches::IsExperimentalAppListEnabled())
43 set_display_type(is_recommendation ? DISPLAY_RECOMMENDATION : DISPLAY_TILE);
45 const extensions::Extension* extension =
46 extensions::ExtensionSystem::Get(profile_)->extension_service()
47 ->GetInstalledExtension(app_id_);
48 DCHECK(extension);
50 is_platform_app_ = extension->is_platform_app();
52 icon_.reset(
53 new extensions::IconImage(profile_,
54 extension,
55 extensions::IconsInfo::GetIcons(extension),
56 GetPreferredIconDimension(),
57 extensions::util::GetDefaultAppIcon(),
58 this));
59 UpdateIcon();
61 StartObservingExtensionRegistry();
64 AppResult::~AppResult() {
65 StopObservingExtensionRegistry();
68 void AppResult::UpdateFromMatch(const TokenizedString& title,
69 const TokenizedStringMatch& match) {
70 const TokenizedStringMatch::Hits& hits = match.hits();
72 Tags tags;
73 tags.reserve(hits.size());
74 for (size_t i = 0; i < hits.size(); ++i)
75 tags.push_back(Tag(Tag::MATCH, hits[i].start(), hits[i].end()));
77 set_title(title.text());
78 set_title_tags(tags);
79 set_relevance(match.relevance());
82 void AppResult::UpdateFromLastLaunched(const base::Time& current_time,
83 const base::Time& last_launched) {
84 base::TimeDelta delta = current_time - last_launched;
85 // |current_time| can be before |last_launched| in weird cases such as users
86 // playing with their clocks. Handle this gracefully.
87 if (current_time < last_launched) {
88 set_relevance(1.0);
89 return;
92 const int kSecondsInWeek = 60 * 60 * 24 * 7;
94 // Set the relevance to a value between 0 and 1. This function decays as the
95 // time delta increases and reaches a value of 0.5 at 1 week.
96 set_relevance(1 / (1 + delta.InSecondsF() / kSecondsInWeek));
99 void AppResult::Open(int event_flags) {
100 RecordHistogram(APP_SEARCH_RESULT);
101 const extensions::Extension* extension =
102 extensions::ExtensionSystem::Get(profile_)->extension_service()
103 ->GetInstalledExtension(app_id_);
104 if (!extension)
105 return;
107 // Don't auto-enable apps that cannot be launched.
108 if (!extensions::util::IsAppLaunchable(app_id_, profile_))
109 return;
111 // Check if enable flow is already running or should be started
112 if (RunExtensionEnableFlow())
113 return;
115 extensions::RecordAppListSearchLaunch(extension);
116 content::RecordAction(
117 base::UserMetricsAction("AppList_ClickOnAppFromSearch"));
119 controller_->ActivateApp(
120 profile_,
121 extension,
122 AppListControllerDelegate::LAUNCH_FROM_APP_LIST_SEARCH,
123 event_flags);
126 scoped_ptr<SearchResult> AppResult::Duplicate() {
127 scoped_ptr<SearchResult> copy(
128 new AppResult(profile_, app_id_, controller_,
129 display_type() == DISPLAY_RECOMMENDATION));
130 copy->set_title(title());
131 copy->set_title_tags(title_tags());
133 return copy.Pass();
136 ui::MenuModel* AppResult::GetContextMenuModel() {
137 if (!context_menu_) {
138 context_menu_.reset(new AppContextMenu(
139 this, profile_, app_id_, controller_));
140 context_menu_->set_is_platform_app(is_platform_app_);
141 context_menu_->set_is_search_result(true);
144 return context_menu_->GetMenuModel();
147 void AppResult::StartObservingExtensionRegistry() {
148 DCHECK(!extension_registry_);
150 extension_registry_ = extensions::ExtensionRegistry::Get(profile_);
151 extension_registry_->AddObserver(this);
154 void AppResult::StopObservingExtensionRegistry() {
155 if (extension_registry_)
156 extension_registry_->RemoveObserver(this);
157 extension_registry_ = NULL;
160 bool AppResult::RunExtensionEnableFlow() {
161 if (extensions::util::IsAppLaunchableWithoutEnabling(app_id_, profile_))
162 return false;
164 if (!extension_enable_flow_) {
165 controller_->OnShowChildDialog();
167 extension_enable_flow_.reset(new ExtensionEnableFlow(
168 profile_, app_id_, this));
169 extension_enable_flow_->StartForNativeWindow(
170 controller_->GetAppListWindow());
172 return true;
175 void AppResult::UpdateIcon() {
176 gfx::ImageSkia icon = icon_->image_skia();
178 if (!extensions::util::IsAppLaunchable(app_id_, profile_)) {
179 const color_utils::HSL shift = {-1, 0, 0.6};
180 icon = gfx::ImageSkiaOperations::CreateHSLShiftedImage(icon, shift);
183 SetIcon(icon);
186 void AppResult::OnExtensionIconImageChanged(extensions::IconImage* image) {
187 DCHECK_EQ(icon_.get(), image);
188 UpdateIcon();
191 void AppResult::ExecuteLaunchCommand(int event_flags) {
192 Open(event_flags);
195 void AppResult::ExtensionEnableFlowFinished() {
196 extension_enable_flow_.reset();
197 controller_->OnCloseChildDialog();
199 // Automatically open app after enabling.
200 Open(ui::EF_NONE);
203 void AppResult::ExtensionEnableFlowAborted(bool user_initiated) {
204 extension_enable_flow_.reset();
205 controller_->OnCloseChildDialog();
208 void AppResult::OnExtensionLoaded(content::BrowserContext* browser_context,
209 const extensions::Extension* extension) {
210 UpdateIcon();
213 void AppResult::OnShutdown(extensions::ExtensionRegistry* registry) {
214 DCHECK_EQ(extension_registry_, registry);
215 StopObservingExtensionRegistry();
218 } // namespace app_list