Chromium Code Reviews| 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 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 relevances != NULL, false, current_query_string16)); | 256 relevances != NULL, false, current_query_string16)); |
| 257 } | 257 } |
| 258 } | 258 } |
| 259 } | 259 } |
| 260 | 260 |
| 261 void ZeroSuggestProvider::AddSuggestResultsToMap( | 261 void ZeroSuggestProvider::AddSuggestResultsToMap( |
| 262 const SuggestResults& results, | 262 const SuggestResults& results, |
| 263 const TemplateURL* template_url, | 263 const TemplateURL* template_url, |
| 264 MatchMap* map) { | 264 MatchMap* map) { |
| 265 for (size_t i = 0; i < results.size(); ++i) { | 265 for (size_t i = 0; i < results.size(); ++i) { |
| 266 AddMatchToMap(results[i].relevance(), AutocompleteMatchType::SEARCH_SUGGEST, | 266 const base::string16& query_string(results[i].suggestion()); |
| 267 template_url, results[i].suggestion(), i, map); | 267 // Pass in query_string as the input_text to avoid bolding |
| 268 const SuggestResult suggestion(query_string, | |
| 269 AutocompleteMatchType::SEARCH_SUGGEST, query_string, base::string16(), | |
| 270 std::string(), std::string(), false, results[i].relevance(), true, | |
| 271 false, query_string); | |
| 272 AddMatchToMap(suggestion, AutocompleteInput(), query_string, template_url, | |
|
Mark P
2014/02/10 23:25:55
Hopefully this ugly (pre-existing) AutocompleteInp
Maria
2014/02/11 21:42:32
Sadly, no :(
| |
| 273 std::string(), i, true, map); | |
| 268 } | 274 } |
| 269 } | 275 } |
| 270 | 276 |
| 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( | 277 AutocompleteMatch ZeroSuggestProvider::NavigationToMatch( |
| 308 const NavigationResult& navigation) { | 278 const NavigationResult& navigation) { |
| 309 AutocompleteMatch match(this, navigation.relevance(), false, | 279 AutocompleteMatch match(this, navigation.relevance(), false, |
| 310 AutocompleteMatchType::NAVSUGGEST); | 280 AutocompleteMatchType::NAVSUGGEST); |
| 311 match.destination_url = navigation.url(); | 281 match.destination_url = navigation.url(); |
| 312 | 282 |
| 313 // Zero suggest results should always omit protocols and never appear bold. | 283 // Zero suggest results should always omit protocols and never appear bold. |
| 314 const std::string languages( | 284 const std::string languages( |
| 315 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); | 285 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
| 316 match.contents = net::FormatUrl(navigation.url(), languages, | 286 match.contents = net::FormatUrl(navigation.url(), languages, |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 445 match.is_history_what_you_typed_match = false; | 415 match.is_history_what_you_typed_match = false; |
| 446 match.allowed_to_be_default_match = true; | 416 match.allowed_to_be_default_match = true; |
| 447 | 417 |
| 448 // The placeholder suggestion for the current URL has high relevance so | 418 // The placeholder suggestion for the current URL has high relevance so |
| 449 // that it is in the first suggestion slot and inline autocompleted. It | 419 // that it is in the first suggestion slot and inline autocompleted. It |
| 450 // gets dropped as soon as the user types something. | 420 // gets dropped as soon as the user types something. |
| 451 match.relevance = verbatim_relevance_; | 421 match.relevance = verbatim_relevance_; |
| 452 | 422 |
| 453 return match; | 423 return match; |
| 454 } | 424 } |
| OLD | NEW |