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