Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / ui / app_list / search / app_result.cc
blobe282aa8e1a263c8579c0e94ef5a88b91ec588dc5
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/browser/ui/webui/ntp/core_app_launcher_handler.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 : profile_(profile),
37 app_id_(app_id),
38 controller_(controller),
39 extension_registry_(NULL) {
40 set_id(extensions::Extension::GetBaseURLFromExtensionId(app_id_).spec());
41 #if !defined(USE_ATHENA)
42 // TODO(mukai): Athena also needs to use tile-styled search results for apps.
43 // Implement it and then remove this ifdef.
44 if (app_list::switches::IsExperimentalAppListEnabled())
45 set_display_type(DISPLAY_TILE);
46 #endif
48 const extensions::Extension* extension =
49 extensions::ExtensionSystem::Get(profile_)->extension_service()
50 ->GetInstalledExtension(app_id_);
51 DCHECK(extension);
53 is_platform_app_ = extension->is_platform_app();
55 icon_.reset(
56 new extensions::IconImage(profile_,
57 extension,
58 extensions::IconsInfo::GetIcons(extension),
59 GetPreferredIconDimension(),
60 extensions::util::GetDefaultAppIcon(),
61 this));
62 UpdateIcon();
64 StartObservingExtensionRegistry();
67 AppResult::~AppResult() {
68 StopObservingExtensionRegistry();
71 void AppResult::UpdateFromMatch(const TokenizedString& title,
72 const TokenizedStringMatch& match) {
73 const TokenizedStringMatch::Hits& hits = match.hits();
75 Tags tags;
76 tags.reserve(hits.size());
77 for (size_t i = 0; i < hits.size(); ++i)
78 tags.push_back(Tag(Tag::MATCH, hits[i].start(), hits[i].end()));
80 set_title(title.text());
81 set_title_tags(tags);
82 set_relevance(match.relevance());
85 void AppResult::UpdateFromLastLaunched(const base::Time& current_time,
86 const base::Time& last_launched) {
87 base::TimeDelta delta = current_time - last_launched;
88 DCHECK_LE(0, delta.InSeconds());
89 const int kSecondsInWeek = 60 * 60 * 24 * 7;
91 // Set the relevance to a value between 0 and 1. This function decays as the
92 // time delta increases and reaches a value of 0.5 at 1 week.
93 set_relevance(1 / (1 + delta.InSecondsF() / kSecondsInWeek));
96 void AppResult::Open(int event_flags) {
97 RecordHistogram(APP_SEARCH_RESULT);
98 const extensions::Extension* extension =
99 extensions::ExtensionSystem::Get(profile_)->extension_service()
100 ->GetInstalledExtension(app_id_);
101 if (!extension)
102 return;
104 // Don't auto-enable apps that cannot be launched.
105 if (!extensions::util::IsAppLaunchable(app_id_, profile_))
106 return;
108 // Check if enable flow is already running or should be started
109 if (RunExtensionEnableFlow())
110 return;
112 CoreAppLauncherHandler::RecordAppListSearchLaunch(extension);
113 content::RecordAction(
114 base::UserMetricsAction("AppList_ClickOnAppFromSearch"));
116 controller_->ActivateApp(
117 profile_,
118 extension,
119 AppListControllerDelegate::LAUNCH_FROM_APP_LIST_SEARCH,
120 event_flags);
123 scoped_ptr<SearchResult> AppResult::Duplicate() {
124 scoped_ptr<SearchResult> copy(new AppResult(profile_, app_id_, controller_));
125 copy->set_title(title());
126 copy->set_title_tags(title_tags());
128 return copy.Pass();
131 ui::MenuModel* AppResult::GetContextMenuModel() {
132 if (!context_menu_) {
133 context_menu_.reset(new AppContextMenu(
134 this, profile_, app_id_, controller_));
135 context_menu_->set_is_platform_app(is_platform_app_);
136 context_menu_->set_is_search_result(true);
139 return context_menu_->GetMenuModel();
142 void AppResult::StartObservingExtensionRegistry() {
143 DCHECK(!extension_registry_);
145 extension_registry_ = extensions::ExtensionRegistry::Get(profile_);
146 extension_registry_->AddObserver(this);
149 void AppResult::StopObservingExtensionRegistry() {
150 if (extension_registry_)
151 extension_registry_->RemoveObserver(this);
152 extension_registry_ = NULL;
155 bool AppResult::RunExtensionEnableFlow() {
156 if (extensions::util::IsAppLaunchableWithoutEnabling(app_id_, profile_))
157 return false;
159 if (!extension_enable_flow_) {
160 controller_->OnShowChildDialog();
162 extension_enable_flow_.reset(new ExtensionEnableFlow(
163 profile_, app_id_, this));
164 extension_enable_flow_->StartForNativeWindow(
165 controller_->GetAppListWindow());
167 return true;
170 void AppResult::UpdateIcon() {
171 gfx::ImageSkia icon = icon_->image_skia();
173 if (!extensions::util::IsAppLaunchable(app_id_, profile_)) {
174 const color_utils::HSL shift = {-1, 0, 0.6};
175 icon = gfx::ImageSkiaOperations::CreateHSLShiftedImage(icon, shift);
178 SetIcon(icon);
181 void AppResult::OnExtensionIconImageChanged(extensions::IconImage* image) {
182 DCHECK_EQ(icon_.get(), image);
183 UpdateIcon();
186 void AppResult::ExecuteLaunchCommand(int event_flags) {
187 Open(event_flags);
190 void AppResult::ExtensionEnableFlowFinished() {
191 extension_enable_flow_.reset();
192 controller_->OnCloseChildDialog();
194 // Automatically open app after enabling.
195 Open(ui::EF_NONE);
198 void AppResult::ExtensionEnableFlowAborted(bool user_initiated) {
199 extension_enable_flow_.reset();
200 controller_->OnCloseChildDialog();
203 void AppResult::OnExtensionLoaded(content::BrowserContext* browser_context,
204 const extensions::Extension* extension) {
205 UpdateIcon();
208 void AppResult::OnExtensionUninstalled(content::BrowserContext* browser_context,
209 const extensions::Extension* extension,
210 extensions::UninstallReason reason) {
211 if (extension->id() != app_id_)
212 return;
214 NotifyItemUninstalled();
217 void AppResult::OnShutdown(extensions::ExtensionRegistry* registry) {
218 DCHECK_EQ(extension_registry_, registry);
219 StopObservingExtensionRegistry();
222 } // namespace app_list