Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(18)

Side by Side Diff: chrome/browser/ui/ash/app_list/search_builder.cc

Issue 10871011: Move common app_list delegate code to chrome/browser/ui/app_list. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Unit test file only in chrome Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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/ash/app_list/search_builder.h"
6
7 #include <string>
8
9 #include "base/command_line.h"
10 #include "chrome/browser/autocomplete/autocomplete_controller.h"
11 #include "chrome/browser/autocomplete/autocomplete_input.h"
12 #include "chrome/browser/autocomplete/autocomplete_match.h"
13 #include "chrome/browser/autocomplete/autocomplete_result.h"
14 #include "chrome/browser/autocomplete/extension_app_provider.h"
15 #include "chrome/browser/event_disposition.h"
16 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/extensions/image_loading_tracker.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/ash/app_list/app_list_controller.h"
20 #include "chrome/browser/ui/browser.h"
21 #include "chrome/browser/ui/browser_navigator.h"
22 #include "chrome/browser/ui/browser_tabstrip.h"
23 #include "chrome/common/extensions/extension.h"
24 #include "chrome/common/extensions/extension_constants.h"
25 #include "chrome/common/extensions/extension_icon_set.h"
26 #include "chrome/common/url_constants.h"
27 #include "content/public/browser/user_metrics.h"
28 #include "content/public/browser/web_contents.h"
29 #include "grit/generated_resources.h"
30 #include "grit/theme_resources.h"
31 #include "ui/app_list/app_list_switches.h"
32 #include "ui/app_list/search_box_model.h"
33 #include "ui/app_list/search_result.h"
34 #include "ui/base/l10n/l10n_util.h"
35 #include "ui/base/resource/resource_bundle.h"
36
37 namespace {
38
39 int ACMatchStyleToTagStyle(int styles) {
40 int tag_styles = 0;
41 if (styles & ACMatchClassification::URL)
42 tag_styles |= app_list::SearchResult::Tag::URL;
43 if (styles & ACMatchClassification::MATCH)
44 tag_styles |= app_list::SearchResult::Tag::MATCH;
45 if (styles & ACMatchClassification::DIM)
46 tag_styles |= app_list::SearchResult::Tag::DIM;
47
48 return tag_styles;
49 }
50
51 // Translates ACMatchClassifications into SearchResult tags.
52 void ACMatchClassificationsToTags(
53 const string16& text,
54 const ACMatchClassifications& text_classes,
55 app_list::SearchResult::Tags* tags) {
56 int tag_styles = app_list::SearchResult::Tag::NONE;
57 size_t tag_start = 0;
58
59 for (size_t i = 0; i < text_classes.size(); ++i) {
60 const ACMatchClassification& text_class = text_classes[i];
61
62 // Closes current tag.
63 if (tag_styles != app_list::SearchResult::Tag::NONE) {
64 tags->push_back(app_list::SearchResult::Tag(
65 tag_styles, tag_start, text_class.offset));
66 tag_styles = app_list::SearchResult::Tag::NONE;
67 }
68
69 if (text_class.style == ACMatchClassification::NONE)
70 continue;
71
72 tag_start = text_class.offset;
73 tag_styles = ACMatchStyleToTagStyle(text_class.style);
74 }
75
76 if (tag_styles != app_list::SearchResult::Tag::NONE) {
77 tags->push_back(app_list::SearchResult::Tag(
78 tag_styles, tag_start, text.length()));
79 }
80 }
81
82 const extensions::Extension* GetExtensionByURL(Profile* profile,
83 const GURL& url) {
84 ExtensionService* service = profile->GetExtensionService();
85 // Need to explicitly get chrome app because it does not override new tab and
86 // not having a web extent to include new tab url, thus GetInstalledApp does
87 // not find it.
88 return url.spec() == chrome::kChromeUINewTabURL ?
89 service->extensions()->GetByID(extension_misc::kChromeAppId) :
90 service->GetInstalledApp(url);
91 }
92
93 // SearchBuildResult is an app list SearchResult built from an
94 // AutocompleteMatch.
95 class SearchBuilderResult : public app_list::SearchResult,
96 public ImageLoadingTracker::Observer {
97 public:
98 SearchBuilderResult(Profile* profile,
99 const AutocompleteMatch& match)
100 : profile_(profile),
101 match_(match) {
102 UpdateIcon();
103 UpdateTitleAndDetails();
104 }
105
106 const AutocompleteMatch& match() const {
107 return match_;
108 }
109
110 private:
111 void UpdateIcon() {
112 if (match_.type == AutocompleteMatch::EXTENSION_APP) {
113 const extensions::Extension* extension =
114 GetExtensionByURL(profile_, match_.destination_url);
115 if (extension) {
116 LoadExtensionIcon(extension);
117 return;
118 }
119 }
120
121 int resource_id = match_.starred ?
122 IDR_OMNIBOX_STAR : AutocompleteMatch::TypeToIcon(match_.type);
123 SetIcon(*ui::ResourceBundle::GetSharedInstance().GetBitmapNamed(
124 resource_id));
125 }
126
127 void LoadExtensionIcon(const extensions::Extension* extension) {
128 tracker_.reset(new ImageLoadingTracker(this));
129 // TODO(xiyuan): Fix this for HD.
130 tracker_->LoadImage(extension,
131 extension->GetIconResource(
132 extension_misc::EXTENSION_ICON_SMALL,
133 ExtensionIconSet::MATCH_BIGGER),
134 gfx::Size(extension_misc::EXTENSION_ICON_SMALL,
135 extension_misc::EXTENSION_ICON_SMALL),
136 ImageLoadingTracker::DONT_CACHE);
137 }
138
139 void UpdateTitleAndDetails() {
140 set_title(match_.contents);
141 app_list::SearchResult::Tags title_tags;
142 ACMatchClassificationsToTags(match_.contents,
143 match_.contents_class,
144 &title_tags);
145 set_title_tags(title_tags);
146
147 set_details(match_.description);
148 app_list::SearchResult::Tags details_tags;
149 ACMatchClassificationsToTags(match_.description,
150 match_.description_class,
151 &details_tags);
152 set_details_tags(details_tags);
153 }
154
155 // Overridden from ImageLoadingTracker::Observer:
156 virtual void OnImageLoaded(const gfx::Image& image,
157 const std::string& extension_id,
158 int tracker_index) OVERRIDE {
159 if (!image.IsEmpty()) {
160 SetIcon(*image.ToSkBitmap());
161 return;
162 }
163
164 SetIcon(profile_->GetExtensionService()->GetOmniboxPopupIcon(extension_id).
165 AsImageSkia());
166 }
167
168 Profile* profile_;
169 AutocompleteMatch match_;
170 scoped_ptr<ImageLoadingTracker> tracker_;
171
172 DISALLOW_COPY_AND_ASSIGN(SearchBuilderResult);
173 };
174
175 } // namespace
176
177 SearchBuilder::SearchBuilder(
178 Profile* profile,
179 app_list::SearchBoxModel* search_box,
180 app_list::AppListModel::SearchResults* results,
181 AppListController* list_controller)
182 : profile_(profile),
183 search_box_(search_box),
184 results_(results),
185 list_controller_(list_controller) {
186 search_box_->SetHintText(
187 l10n_util::GetStringUTF16(IDS_SEARCH_BOX_HINT));
188 search_box_->SetIcon(*ui::ResourceBundle::GetSharedInstance().
189 GetImageSkiaNamed(IDR_OMNIBOX_SEARCH));
190
191 if (CommandLine::ForCurrentProcess()->HasSwitch(
192 app_list::switches::kAppListShowAppsOnly)) {
193 // ExtensionAppProvider is a synchronous provider and does not really need a
194 // listener.
195 apps_provider_ = new ExtensionAppProvider(NULL, profile);
196 } else {
197 controller_.reset(new AutocompleteController(profile, this));
198 }
199 }
200
201 SearchBuilder::~SearchBuilder() {
202 }
203
204 void SearchBuilder::StartSearch() {
205 const string16& user_text = search_box_->text();
206
207 if (controller_.get()) {
208 // Omnibox features such as keyword selection/accepting and instant query
209 // are not implemented.
210 // TODO(xiyuan): Figure out the features that need to support here.
211 controller_->Start(user_text, string16(), false, false, true,
212 AutocompleteInput::ALL_MATCHES);
213 } else {
214 AutocompleteInput input(user_text, string16(), false, false, true,
215 AutocompleteInput::ALL_MATCHES);
216 apps_provider_->Start(input, false);
217
218 // ExtensionAppProvider is a synchronous provider and results are ready
219 // after returning from Start.
220 AutocompleteResult ac_result;
221 ac_result.AppendMatches(apps_provider_->matches());
222 ac_result.SortAndCull(input);
223 PopulateFromACResult(ac_result);
224 }
225 }
226
227 void SearchBuilder::StopSearch() {
228 if (controller_.get())
229 controller_->Stop(true);
230 else
231 apps_provider_->Stop(true);
232 }
233
234 void SearchBuilder::OpenResult(const app_list::SearchResult& result,
235 int event_flags) {
236 const SearchBuilderResult* builder_result =
237 static_cast<const SearchBuilderResult*>(&result);
238 const AutocompleteMatch& match = builder_result->match();
239
240 // Count AppList.Search here because it is composed of search + action.
241 content::RecordAction(content::UserMetricsAction("AppList_Search"));
242
243 if (match.type == AutocompleteMatch::EXTENSION_APP) {
244 const extensions::Extension* extension =
245 GetExtensionByURL(profile_, match.destination_url);
246 if (extension) {
247 content::RecordAction(
248 content::UserMetricsAction("AppList_ClickOnAppFromSearch"));
249 list_controller_->ActivateApp(profile_, extension->id(), event_flags);
250 }
251 } else {
252 // TODO(xiyuan): What should we do for alternate url case?
253 chrome::NavigateParams params(profile_,
254 match.destination_url,
255 match.transition);
256 params.disposition = chrome::DispositionFromEventFlags(event_flags);
257 chrome::Navigate(&params);
258 }
259 }
260
261 void SearchBuilder::PopulateFromACResult(const AutocompleteResult& ac_result) {
262 results_->DeleteAll();
263 for (ACMatches::const_iterator it = ac_result.begin();
264 it != ac_result.end();
265 ++it) {
266 results_->Add(new SearchBuilderResult(profile_, *it));
267 }
268 }
269
270 void SearchBuilder::OnResultChanged(bool default_match_changed) {
271 // TODO(xiyuan): Handle default match properly.
272 const AutocompleteResult& ac_result = controller_->result();
273 PopulateFromACResult(ac_result);
274 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/ash/app_list/search_builder.h ('k') | chrome/browser/ui/ash/chrome_shell_delegate.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698