OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/autocomplete/zero_suggest_provider.h" | 5 #include "chrome/browser/autocomplete/zero_suggest_provider.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/i18n/case_conversion.h" | 8 #include "base/i18n/case_conversion.h" |
9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 AutocompleteProvider::TYPE_ZERO_SUGGEST), | 168 AutocompleteProvider::TYPE_ZERO_SUGGEST), |
169 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), | 169 template_url_service_(TemplateURLServiceFactory::GetForProfile(profile)), |
170 have_pending_request_(false), | 170 have_pending_request_(false), |
171 verbatim_relevance_(kDefaultVerbatimZeroSuggestRelevance), | 171 verbatim_relevance_(kDefaultVerbatimZeroSuggestRelevance), |
172 weak_ptr_factory_(this) { | 172 weak_ptr_factory_(this) { |
173 } | 173 } |
174 | 174 |
175 ZeroSuggestProvider::~ZeroSuggestProvider() { | 175 ZeroSuggestProvider::~ZeroSuggestProvider() { |
176 } | 176 } |
177 | 177 |
| 178 const TemplateURL* ZeroSuggestProvider::GetTemplateURL( |
| 179 const SuggestResult& result) const { |
| 180 // Zero suggest provider should not receive keyword results. |
| 181 DCHECK(!result.from_keyword_provider()); |
| 182 return template_url_service_->GetDefaultSearchProvider(); |
| 183 } |
| 184 |
| 185 const AutocompleteInput ZeroSuggestProvider::GetInput( |
| 186 const SuggestResult& result) const { |
| 187 AutocompleteInput input; |
| 188 // Set |input|'s text to be |query_string| to avoid bolding. |
| 189 input.UpdateText(result.suggestion(), base::string16::npos, input.parts()); |
| 190 return input; |
| 191 } |
| 192 |
| 193 bool ZeroSuggestProvider::ShouldAppendExtraParams( |
| 194 const SuggestResult& result) const { |
| 195 // We always use the default provider for search, so append the params. |
| 196 return true; |
| 197 } |
| 198 |
178 void ZeroSuggestProvider::FillResults(const base::Value& root_val, | 199 void ZeroSuggestProvider::FillResults(const base::Value& root_val, |
179 int* verbatim_relevance, | 200 int* verbatim_relevance, |
180 SuggestResults* suggest_results, | 201 SuggestResults* suggest_results, |
181 NavigationResults* navigation_results) { | 202 NavigationResults* navigation_results) { |
182 base::string16 query; | 203 base::string16 query; |
183 const base::ListValue* root_list = NULL; | 204 const base::ListValue* root_list = NULL; |
184 const base::ListValue* results = NULL; | 205 const base::ListValue* results = NULL; |
185 const base::ListValue* relevances = NULL; | 206 const base::ListValue* relevances = NULL; |
186 // The response includes the query, which should be empty for ZeroSuggest | 207 // The response includes the query, which should be empty for ZeroSuggest |
187 // responses. | 208 // responses. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 suggest_results->push_back(SuggestResult( | 274 suggest_results->push_back(SuggestResult( |
254 result, AutocompleteMatchType::SEARCH_SUGGEST, result, | 275 result, AutocompleteMatchType::SEARCH_SUGGEST, result, |
255 base::string16(), std::string(), std::string(), false, relevance, | 276 base::string16(), std::string(), std::string(), false, relevance, |
256 relevances != NULL, false, current_query_string16)); | 277 relevances != NULL, false, current_query_string16)); |
257 } | 278 } |
258 } | 279 } |
259 } | 280 } |
260 | 281 |
261 void ZeroSuggestProvider::AddSuggestResultsToMap( | 282 void ZeroSuggestProvider::AddSuggestResultsToMap( |
262 const SuggestResults& results, | 283 const SuggestResults& results, |
263 const TemplateURL* template_url, | |
264 MatchMap* map) { | 284 MatchMap* map) { |
265 for (size_t i = 0; i < results.size(); ++i) { | 285 for (size_t i = 0; i < results.size(); ++i) { |
266 AddMatchToMap(results[i].relevance(), AutocompleteMatchType::SEARCH_SUGGEST, | 286 const base::string16& query_string(results[i].suggestion()); |
267 template_url, results[i].suggestion(), i, map); | 287 // TODO(mariakhomenko): Do not reconstruct SuggestResult objects with |
| 288 // a different query -- create correct objects to begin with. |
| 289 const SuggestResult suggestion( |
| 290 query_string, AutocompleteMatchType::SEARCH_SUGGEST, query_string, |
| 291 base::string16(), std::string(), std::string(), false, |
| 292 results[i].relevance(), true, false, query_string); |
| 293 AddMatchToMap(suggestion, std::string(), i, map); |
268 } | 294 } |
269 } | 295 } |
270 | 296 |
271 void ZeroSuggestProvider::AddMatchToMap(int relevance, | |
272 AutocompleteMatch::Type type, | |
273 const TemplateURL* template_url, | |
274 const base::string16& query_string, | |
275 int accepted_suggestion, | |
276 MatchMap* map) { | |
277 // Pass in query_string as the input_text to avoid bolding. | |
278 SuggestResult suggestion( | |
279 query_string, type, query_string, base::string16(), std::string(), | |
280 std::string(), false, relevance, true, false, query_string); | |
281 // TODO(samarth|melevin): use the actual omnibox margin here as well instead | |
282 // of passing in -1. | |
283 AutocompleteMatch match = CreateSearchSuggestion(this, AutocompleteInput(), | |
284 query_string, suggestion, template_url, accepted_suggestion, -1, true); | |
285 if (!match.destination_url.is_valid()) | |
286 return; | |
287 | |
288 // Try to add |match| to |map|. If a match for |query_string| is already in | |
289 // |map|, replace it if |match| is more relevant. | |
290 // NOTE: Keep this ToLower() call in sync with url_database.cc. | |
291 MatchKey match_key( | |
292 std::make_pair(base::i18n::ToLower(query_string), std::string())); | |
293 const std::pair<MatchMap::iterator, bool> i(map->insert( | |
294 std::make_pair(match_key, match))); | |
295 // NOTE: We purposefully do a direct relevance comparison here instead of | |
296 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added | |
297 // first" rather than "items alphabetically first" when the scores are equal. | |
298 // The only case this matters is when a user has results with the same score | |
299 // that differ only by capitalization; because the history system returns | |
300 // results sorted by recency, this means we'll pick the most recent such | |
301 // result even if the precision of our relevance score is too low to | |
302 // distinguish the two. | |
303 if (!i.second && (match.relevance > i.first->second.relevance)) | |
304 i.first->second = match; | |
305 } | |
306 | |
307 AutocompleteMatch ZeroSuggestProvider::NavigationToMatch( | 297 AutocompleteMatch ZeroSuggestProvider::NavigationToMatch( |
308 const NavigationResult& navigation) { | 298 const NavigationResult& navigation) { |
309 AutocompleteMatch match(this, navigation.relevance(), false, | 299 AutocompleteMatch match(this, navigation.relevance(), false, |
310 AutocompleteMatchType::NAVSUGGEST); | 300 AutocompleteMatchType::NAVSUGGEST); |
311 match.destination_url = navigation.url(); | 301 match.destination_url = navigation.url(); |
312 | 302 |
313 // Zero suggest results should always omit protocols and never appear bold. | 303 // Zero suggest results should always omit protocols and never appear bold. |
314 const std::string languages( | 304 const std::string languages( |
315 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); | 305 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
316 match.contents = net::FormatUrl(navigation.url(), languages, | 306 match.contents = net::FormatUrl(navigation.url(), languages, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 have_pending_request_ = true; | 350 have_pending_request_ = true; |
361 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); | 351 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); |
362 } | 352 } |
363 | 353 |
364 void ZeroSuggestProvider::ParseSuggestResults(const base::Value& root_val) { | 354 void ZeroSuggestProvider::ParseSuggestResults(const base::Value& root_val) { |
365 SuggestResults suggest_results; | 355 SuggestResults suggest_results; |
366 FillResults(root_val, &verbatim_relevance_, | 356 FillResults(root_val, &verbatim_relevance_, |
367 &suggest_results, &navigation_results_); | 357 &suggest_results, &navigation_results_); |
368 | 358 |
369 query_matches_map_.clear(); | 359 query_matches_map_.clear(); |
370 AddSuggestResultsToMap(suggest_results, | 360 AddSuggestResultsToMap(suggest_results, &query_matches_map_); |
371 template_url_service_->GetDefaultSearchProvider(), | |
372 &query_matches_map_); | |
373 } | 361 } |
374 | 362 |
375 void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( | 363 void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( |
376 const history::MostVisitedURLList& urls) { | 364 const history::MostVisitedURLList& urls) { |
377 most_visited_urls_ = urls; | 365 most_visited_urls_ = urls; |
378 } | 366 } |
379 | 367 |
380 void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { | 368 void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { |
381 matches_.clear(); | 369 matches_.clear(); |
382 | 370 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 match.is_history_what_you_typed_match = false; | 433 match.is_history_what_you_typed_match = false; |
446 match.allowed_to_be_default_match = true; | 434 match.allowed_to_be_default_match = true; |
447 | 435 |
448 // The placeholder suggestion for the current URL has high relevance so | 436 // The placeholder suggestion for the current URL has high relevance so |
449 // that it is in the first suggestion slot and inline autocompleted. It | 437 // that it is in the first suggestion slot and inline autocompleted. It |
450 // gets dropped as soon as the user types something. | 438 // gets dropped as soon as the user types something. |
451 match.relevance = verbatim_relevance_; | 439 match.relevance = verbatim_relevance_; |
452 | 440 |
453 return match; | 441 return match; |
454 } | 442 } |
OLD | NEW |