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 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 } | 289 } |
290 } else { | 290 } else { |
291 suggest_results->push_back(SearchProvider::SuggestResult( | 291 suggest_results->push_back(SearchProvider::SuggestResult( |
292 result, false, relevance, relevances != NULL)); | 292 result, false, relevance, relevances != NULL)); |
293 } | 293 } |
294 } | 294 } |
295 } | 295 } |
296 | 296 |
297 void ZeroSuggestProvider::AddSuggestResultsToMap( | 297 void ZeroSuggestProvider::AddSuggestResultsToMap( |
298 const SearchProvider::SuggestResults& results, | 298 const SearchProvider::SuggestResults& results, |
299 const string16& provider_keyword, | 299 const TemplateURL* template_url, |
300 SearchProvider::MatchMap* map) { | 300 SearchProvider::MatchMap* map) { |
301 for (size_t i = 0; i < results.size(); ++i) { | 301 for (size_t i = 0; i < results.size(); ++i) { |
302 AddMatchToMap(results[i].suggestion(), | 302 AddMatchToMap(results[i].relevance(), AutocompleteMatchType::SEARCH_SUGGEST, |
303 provider_keyword, | 303 template_url, results[i].suggestion(), i, map); |
304 results[i].relevance(), | |
305 AutocompleteMatchType::SEARCH_SUGGEST, i, map); | |
306 } | 304 } |
307 } | 305 } |
308 | 306 |
309 void ZeroSuggestProvider::AddMatchToMap(const string16& query_string, | 307 void ZeroSuggestProvider::AddMatchToMap(int relevance, |
310 const string16& provider_keyword, | |
311 int relevance, | |
312 AutocompleteMatch::Type type, | 308 AutocompleteMatch::Type type, |
| 309 const TemplateURL* template_url, |
| 310 const string16& query_string, |
313 int accepted_suggestion, | 311 int accepted_suggestion, |
314 SearchProvider::MatchMap* map) { | 312 SearchProvider::MatchMap* map) { |
315 // Pass in query_string as the input_text since we don't want any bolding. | 313 // Pass in query_string as the input_text since we don't want any bolding. |
316 // TODO(samarth|melevin): use the actual omnibox margin here as well instead | 314 // TODO(samarth|melevin): use the actual omnibox margin here as well instead |
317 // of passing in -1. | 315 // of passing in -1. |
318 AutocompleteMatch match = SearchProvider::CreateSearchSuggestion( | 316 AutocompleteMatch match = SearchProvider::CreateSearchSuggestion( |
319 profile_, this, AutocompleteInput(), | 317 this, relevance, type, template_url, query_string, query_string, |
320 query_string, query_string, relevance, type, accepted_suggestion, | 318 AutocompleteInput(), false, accepted_suggestion, -1, true); |
321 false, provider_keyword, -1); | |
322 if (!match.destination_url.is_valid()) | 319 if (!match.destination_url.is_valid()) |
323 return; | 320 return; |
324 | 321 |
325 // Try to add |match| to |map|. If a match for |query_string| is already in | 322 // Try to add |match| to |map|. If a match for |query_string| is already in |
326 // |map|, replace it if |match| is more relevant. | 323 // |map|, replace it if |match| is more relevant. |
327 // NOTE: Keep this ToLower() call in sync with url_database.cc. | 324 // NOTE: Keep this ToLower() call in sync with url_database.cc. |
328 const std::pair<SearchProvider::MatchMap::iterator, bool> i(map->insert( | 325 const std::pair<SearchProvider::MatchMap::iterator, bool> i(map->insert( |
329 std::make_pair(base::i18n::ToLower(query_string), match))); | 326 std::make_pair(base::i18n::ToLower(query_string), match))); |
330 // NOTE: We purposefully do a direct relevance comparison here instead of | 327 // NOTE: We purposefully do a direct relevance comparison here instead of |
331 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added | 328 // using AutocompleteMatch::MoreRelevant(), so that we'll prefer "items added |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 have_pending_request_ = true; | 405 have_pending_request_ = true; |
409 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); | 406 LogOmniboxZeroSuggestRequest(ZERO_SUGGEST_REQUEST_SENT); |
410 } | 407 } |
411 | 408 |
412 void ZeroSuggestProvider::ParseSuggestResults(const Value& root_val) { | 409 void ZeroSuggestProvider::ParseSuggestResults(const Value& root_val) { |
413 SearchProvider::SuggestResults suggest_results; | 410 SearchProvider::SuggestResults suggest_results; |
414 FillResults(root_val, &verbatim_relevance_, | 411 FillResults(root_val, &verbatim_relevance_, |
415 &suggest_results, &navigation_results_); | 412 &suggest_results, &navigation_results_); |
416 | 413 |
417 query_matches_map_.clear(); | 414 query_matches_map_.clear(); |
418 const TemplateURL* default_provider = | 415 AddSuggestResultsToMap(suggest_results, |
419 template_url_service_->GetDefaultSearchProvider(); | 416 template_url_service_->GetDefaultSearchProvider(), |
420 AddSuggestResultsToMap(suggest_results, default_provider->keyword(), | |
421 &query_matches_map_); | 417 &query_matches_map_); |
422 } | 418 } |
423 | 419 |
424 void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { | 420 void ZeroSuggestProvider::ConvertResultsToAutocompleteMatches() { |
425 matches_.clear(); | 421 matches_.clear(); |
426 | 422 |
427 const TemplateURL* default_provider = | 423 const TemplateURL* default_provider = |
428 template_url_service_->GetDefaultSearchProvider(); | 424 template_url_service_->GetDefaultSearchProvider(); |
429 // Fail if we can't set the clickthrough URL for query suggestions. | 425 // Fail if we can't set the clickthrough URL for query suggestions. |
430 if (default_provider == NULL || !default_provider->SupportsReplacement()) | 426 if (default_provider == NULL || !default_provider->SupportsReplacement()) |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 match.is_history_what_you_typed_match = false; | 460 match.is_history_what_you_typed_match = false; |
465 match.inline_autocomplete_offset = string16::npos; | 461 match.inline_autocomplete_offset = string16::npos; |
466 | 462 |
467 // The placeholder suggestion for the current URL has high relevance so | 463 // The placeholder suggestion for the current URL has high relevance so |
468 // that it is in the first suggestion slot and inline autocompleted. It | 464 // that it is in the first suggestion slot and inline autocompleted. It |
469 // gets dropped as soon as the user types something. | 465 // gets dropped as soon as the user types something. |
470 match.relevance = verbatim_relevance_; | 466 match.relevance = verbatim_relevance_; |
471 | 467 |
472 return match; | 468 return match; |
473 } | 469 } |
OLD | NEW |