app_result.cc revision ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16
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.
4
5#include "chrome/browser/ui/app_list/search/app_result.h"
6
7#include "chrome/browser/extensions/extension_service.h"
8#include "chrome/browser/extensions/extension_system_factory.h"
9#include "chrome/browser/extensions/install_tracker.h"
10#include "chrome/browser/extensions/install_tracker_factory.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/ui/app_list/app_context_menu.h"
13#include "chrome/browser/ui/app_list/app_list_controller_delegate.h"
14#include "chrome/browser/ui/app_list/search/tokenized_string.h"
15#include "chrome/browser/ui/app_list/search/tokenized_string_match.h"
16#include "chrome/browser/ui/webui/ntp/core_app_launcher_handler.h"
17#include "chrome/common/extensions/extension.h"
18#include "chrome/common/extensions/extension_icon_set.h"
19#include "chrome/common/extensions/manifest_handlers/icons_handler.h"
20#include "content/public/browser/user_metrics.h"
21
22namespace app_list {
23
24AppResult::AppResult(Profile* profile,
25                     const std::string& app_id,
26                     AppListControllerDelegate* controller)
27    : profile_(profile),
28      app_id_(app_id),
29      controller_(controller),
30      install_tracker_(NULL) {
31  set_id(extensions::Extension::GetBaseURLFromExtensionId(app_id_).spec());
32
33  const extensions::Extension* extension =
34      extensions::ExtensionSystem::Get(profile_)->extension_service()
35          ->GetInstalledExtension(app_id_);
36  DCHECK(extension);
37
38  is_platform_app_ = extension->is_platform_app();
39
40  icon_.reset(new extensions::IconImage(
41      profile_,
42      extension,
43      extensions::IconsInfo::GetIcons(extension),
44      extension_misc::EXTENSION_ICON_SMALL,
45      extensions::IconsInfo::GetDefaultAppIcon(),
46      this));
47  SetIcon(icon_->image_skia());
48  StartObservingInstall();
49}
50
51AppResult::~AppResult() {
52  StopObservingInstall();
53}
54
55void AppResult::UpdateFromMatch(const TokenizedString& title,
56                                const TokenizedStringMatch& match) {
57  const TokenizedStringMatch::Hits& hits = match.hits();
58
59  Tags tags;
60  tags.reserve(hits.size());
61  for (size_t i = 0; i < hits.size(); ++i)
62    tags.push_back(Tag(Tag::MATCH, hits[i].start(), hits[i].end()));
63
64  set_title(title.text());
65  set_title_tags(tags);
66  set_relevance(match.relevance());
67}
68
69void AppResult::Open(int event_flags) {
70  const extensions::Extension* extension =
71      extensions::ExtensionSystem::Get(profile_)->extension_service()
72          ->GetInstalledExtension(app_id_);
73  if (!extension)
74    return;
75
76  CoreAppLauncherHandler::RecordAppListSearchLaunch(extension);
77  content::RecordAction(
78      content::UserMetricsAction("AppList_ClickOnAppFromSearch"));
79
80  controller_->ActivateApp(profile_, extension, event_flags);
81}
82
83void AppResult::InvokeAction(int action_index, int event_flags) {}
84
85scoped_ptr<ChromeSearchResult> AppResult::Duplicate() {
86  scoped_ptr<ChromeSearchResult> copy(
87      new AppResult(profile_, app_id_, controller_));
88  copy->set_title(title());
89  copy->set_title_tags(title_tags());
90
91  return copy.Pass();
92}
93
94ChromeSearchResultType AppResult::GetType() {
95  return APP_SEARCH_RESULT;
96}
97
98ui::MenuModel* AppResult::GetContextMenuModel() {
99  if (!context_menu_) {
100    context_menu_.reset(new AppContextMenu(
101        this, profile_, app_id_, controller_, is_platform_app_));
102  }
103
104  return context_menu_->GetMenuModel();
105}
106
107void AppResult::StartObservingInstall() {
108  DCHECK(!install_tracker_);
109
110  install_tracker_ = extensions::InstallTrackerFactory::GetForProfile(profile_);
111  install_tracker_->AddObserver(this);
112}
113
114void AppResult::StopObservingInstall() {
115  if (install_tracker_)
116    install_tracker_->RemoveObserver(this);
117
118  install_tracker_ = NULL;
119}
120
121void AppResult::OnExtensionIconImageChanged(extensions::IconImage* image) {
122  DCHECK_EQ(icon_.get(), image);
123  SetIcon(icon_->image_skia());
124}
125
126void AppResult::ExecuteLaunchCommand(int event_flags) {
127  Open(event_flags);
128}
129
130void AppResult::OnBeginExtensionInstall(const std::string& extension_id,
131                                        const std::string& extension_name,
132                                        const gfx::ImageSkia& installing_icon,
133                                        bool is_app,
134                                        bool is_platform_app) {}
135
136void AppResult::OnDownloadProgress(const std::string& extension_id,
137                                   int percent_downloaded) {}
138
139void AppResult::OnInstallFailure(const std::string& extension_id) {}
140
141void AppResult::OnExtensionInstalled(const extensions::Extension* extension) {}
142
143void AppResult::OnExtensionLoaded(const extensions::Extension* extension) {}
144
145void AppResult::OnExtensionUnloaded(const extensions::Extension* extension) {}
146
147void AppResult::OnExtensionUninstalled(const extensions::Extension* extension) {
148  if (extension->id() != app_id_)
149    return;
150
151  NotifyItemUninstalled();
152}
153
154void AppResult::OnAppsReordered() {}
155
156void AppResult::OnAppInstalledToAppList(const std::string& extension_id) {}
157
158void AppResult::OnShutdown() { StopObservingInstall(); }
159
160}  // namespace app_list
161