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 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 const base::string16& query_string(result.suggestion()); | |
|
msw
2014/02/13 22:47:18
nit: inline this below.
Maria
2014/02/13 23:19:23
Done.
| |
| 189 // Set |input|'s text to be |query_string| to avoid bolding. | |
| 190 input.UpdateText(query_string, base::string16::npos, input.parts()); | |
| 191 return input; | |
| 192 } | |
| 193 | |
| 194 bool ZeroSuggestProvider::ShouldAppendExtraParams( | |
| 195 const SuggestResult& result) const { | |
| 196 // We always use the default provider for search, so append the params. | |
| 197 return true; | |
| 198 } | |
| 199 | |
| 178 void ZeroSuggestProvider::FillResults(const base::Value& root_val, | 200 void ZeroSuggestProvider::FillResults(const base::Value& root_val, |
| 179 int* verbatim_relevance, | 201 int* verbatim_relevance, |
| 180 SuggestResults* suggest_results, | 202 SuggestResults* suggest_results, |
| 181 NavigationResults* navigation_results) { | 203 NavigationResults* navigation_results) { |
| 182 base::string16 query; | 204 base::string16 query; |
| 183 const base::ListValue* root_list = NULL; | 205 const base::ListValue* root_list = NULL; |
| 184 const base::ListValue* results = NULL; | 206 const base::ListValue* results = NULL; |
| 185 const base::ListValue* relevances = NULL; | 207 const base::ListValue* relevances = NULL; |
| 186 // The response includes the query, which should be empty for ZeroSuggest | 208 // The response includes the query, which should be empty for ZeroSuggest |
| 187 // responses. | 209 // responses. |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 253 suggest_results->push_back(SuggestResult( | 275 suggest_results->push_back(SuggestResult( |
| 254 result, AutocompleteMatchType::SEARCH_SUGGEST, result, | 276 result, AutocompleteMatchType::SEARCH_SUGGEST, result, |
| 255 base::string16(), std::string(), std::string(), false, relevance, | 277 base::string16(), std::string(), std::string(), false, relevance, |
| 256 relevances != NULL, false, current_query_string16)); | 278 relevances != NULL, false, current_query_string16)); |
| 257 } | 279 } |
| 258 } | 280 } |
| 259 } | 281 } |
| 260 | 282 |
| 261 void ZeroSuggestProvider::AddSuggestResultsToMap( | 283 void ZeroSuggestProvider::AddSuggestResultsToMap( |
| 262 const SuggestResults& results, | 284 const SuggestResults& results, |
| 263 const TemplateURL* template_url, | |
| 264 MatchMap* map) { | 285 MatchMap* map) { |
| 265 for (size_t i = 0; i < results.size(); ++i) { | 286 for (size_t i = 0; i < results.size(); ++i) { |
| 266 AddMatchToMap(results[i].relevance(), AutocompleteMatchType::SEARCH_SUGGEST, | 287 const base::string16& query_string(results[i].suggestion()); |
| 267 template_url, results[i].suggestion(), i, map); | 288 // TODO(mariakhomenko): Do not reconstruct SuggestResult object with |
|
msw
2014/02/13 22:47:18
nit: "objects" or "each object" or similar.
Maria
2014/02/13 23:19:23
Done.
| |
| 289 // a different query -- create correct objects to begin with. | |
| 290 const SuggestResult suggestion( | |
| 291 query_string, AutocompleteMatchType::SEARCH_SUGGEST, query_string, | |
| 292 base::string16(), std::string(), std::string(), false, | |
| 293 results[i].relevance(), true, false, query_string); | |
| 294 AddMatchToMap(suggestion, std::string(), i, map); | |
| 268 } | 295 } |
| 269 } | 296 } |
| 270 | 297 |
| 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( | 298 AutocompleteMatch ZeroSuggestProvider::NavigationToMatch( |
| 308 const NavigationResult& navigation) { | 299 const NavigationResult& navigation) { |
| 309 AutocompleteMatch match(this, navigation.relevance(), false, | 300 AutocompleteMatch match(this, navigation.relevance(), false, |
| 310 AutocompleteMatchType::NAVSUGGEST); | 301 AutocompleteMatchType::NAVSUGGEST); |
| 311 match.destination_url = navigation.url(); | 302 match.destination_url = navigation.url(); |
| 312 | 303 |
| 313 // Zero suggest results should always omit protocols and never appear bold. | 304 // Zero suggest results should always omit protocols and never appear bold. |
| 314 const std::string languages( | 305 const std::string languages( |
| 315 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); | 306 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages)); |
| 316 match.contents = net::FormatUrl(navigation.url(), languages, | 307 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; | 351 have_pending_request_ = true; |
| 361 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); | 352 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); |
| 362 } | 353 } |
| 363 | 354 |
| 364 void ZeroSuggestProvider::ParseSuggestResults(const base::Value& root_val) { | 355 void ZeroSuggestProvider::ParseSuggestResults(const base::Value& root_val) { |
| 365 SuggestResults suggest_results; | 356 SuggestResults suggest_results; |
| 366 FillResults(root_val, &verbatim_relevance_, | 357 FillResults(root_val, &verbatim_relevance_, |
| 367 &suggest_results, &navigation_results_); | 358 &suggest_results, &navigation_results_); |
| 368 | 359 |
| 369 query_matches_map_.clear(); | 360 query_matches_map_.clear(); |
| 370 AddSuggestResultsToMap(suggest_results, | 361 AddSuggestResultsToMap(suggest_results, &query_matches_map_); |
| 371 template_url_service_->GetDefaultSearchProvider(), | |
| 372 &query_matches_map_); | |
| 373 } | 362 } |
| 374 | 363 |
| 375 void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( | 364 void ZeroSuggestProvider::OnMostVisitedUrlsAvailable( |
| 376 const history::MostVisitedURLList& urls) { | 365 const history::MostVisitedURLList& urls) { |
| 377 most_visited_urls_ = urls; | 366 most_visited_urls_ = urls; |
| 378 } | 367 } |
| 379 | 368 |
| 380 void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { | 369 void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { |
| 381 matches_.clear(); | 370 matches_.clear(); |
| 382 | 371 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 445 match.is_history_what_you_typed_match = false; | 434 match.is_history_what_you_typed_match = false; |
| 446 match.allowed_to_be_default_match = true; | 435 match.allowed_to_be_default_match = true; |
| 447 | 436 |
| 448 // The placeholder suggestion for the current URL has high relevance so | 437 // The placeholder suggestion for the current URL has high relevance so |
| 449 // that it is in the first suggestion slot and inline autocompleted. It | 438 // that it is in the first suggestion slot and inline autocompleted. It |
| 450 // gets dropped as soon as the user types something. | 439 // gets dropped as soon as the user types something. |
| 451 match.relevance = verbatim_relevance_; | 440 match.relevance = verbatim_relevance_; |
| 452 | 441 |
| 453 return match; | 442 return match; |
| 454 } | 443 } |
| OLD | NEW |