Chromium Code Reviews| Index: components/omnibox/browser/autocomplete_result.cc |
| diff --git a/components/omnibox/browser/autocomplete_result.cc b/components/omnibox/browser/autocomplete_result.cc |
| index 9e4bf1d769a98938709b48fcf57250ab96408133..c09f5c3a5472f51ec758a86978243d6ea9b0d166 100644 |
| --- a/components/omnibox/browser/autocomplete_result.cc |
| +++ b/components/omnibox/browser/autocomplete_result.cc |
| @@ -15,6 +15,7 @@ |
| #include "components/omnibox/browser/autocomplete_input.h" |
| #include "components/omnibox/browser/autocomplete_match.h" |
| #include "components/omnibox/browser/autocomplete_provider.h" |
| +#include "components/omnibox/browser/match_compare.h" |
| #include "components/omnibox/browser/omnibox_field_trial.h" |
| #include "components/omnibox/browser/omnibox_switches.h" |
| #include "components/search/search.h" |
| @@ -22,84 +23,6 @@ |
| using metrics::OmniboxEventProto; |
|
Peter Kasting
2016/04/14 23:52:33
Nit: You can remove this now and explicitly qualif
Alexander Yashkin
2016/04/15 09:14:14
Done.
|
| -namespace { |
| - |
| -// This class implements a special version of AutocompleteMatch::MoreRelevant |
| -// that allows matches of particular types to be demoted in AutocompleteResult. |
| -class CompareWithDemoteByType { |
| - public: |
| - CompareWithDemoteByType( |
| - OmniboxEventProto::PageClassification current_page_classification); |
| - |
| - // Returns the relevance score of |match| demoted appropriately by |
| - // |demotions_by_type_|. |
| - int GetDemotedRelevance(const AutocompleteMatch& match); |
| - |
| - // Comparison function. |
| - bool operator()(const AutocompleteMatch& elem1, |
| - const AutocompleteMatch& elem2); |
| - |
| - private: |
| - OmniboxFieldTrial::DemotionMultipliers demotions_; |
| -}; |
| - |
| -CompareWithDemoteByType::CompareWithDemoteByType( |
| - OmniboxEventProto::PageClassification current_page_classification) { |
| - OmniboxFieldTrial::GetDemotionsByType(current_page_classification, |
| - &demotions_); |
| -} |
| - |
| -int CompareWithDemoteByType::GetDemotedRelevance( |
| - const AutocompleteMatch& match) { |
| - OmniboxFieldTrial::DemotionMultipliers::const_iterator demotion_it = |
| - demotions_.find(match.type); |
| - return (demotion_it == demotions_.end()) ? |
| - match.relevance : (match.relevance * demotion_it->second); |
| -} |
| - |
| -bool CompareWithDemoteByType::operator()(const AutocompleteMatch& elem1, |
| - const AutocompleteMatch& elem2) { |
| - // Compute demoted relevance scores for each match. |
| - const int demoted_relevance1 = GetDemotedRelevance(elem1); |
| - const int demoted_relevance2 = GetDemotedRelevance(elem2); |
| - // For equal-relevance matches, we sort alphabetically, so that providers |
| - // who return multiple elements at the same priority get a "stable" sort |
| - // across multiple updates. |
| - return (demoted_relevance1 == demoted_relevance2) ? |
| - (elem1.contents < elem2.contents) : |
| - (demoted_relevance1 > demoted_relevance2); |
| -} |
| - |
| -class DestinationSort { |
| - public: |
| - DestinationSort( |
| - OmniboxEventProto::PageClassification current_page_classification); |
| - bool operator()(const AutocompleteMatch& elem1, |
| - const AutocompleteMatch& elem2); |
| - |
| - private: |
| - CompareWithDemoteByType demote_by_type_; |
| -}; |
| - |
| -DestinationSort::DestinationSort( |
| - OmniboxEventProto::PageClassification current_page_classification) : |
| - demote_by_type_(current_page_classification) {} |
| - |
| -bool DestinationSort::operator()(const AutocompleteMatch& elem1, |
| - const AutocompleteMatch& elem2) { |
| - // Sort identical destination_urls together. Place the most relevant matches |
| - // first, so that when we call std::unique(), these are the ones that get |
| - // preserved. |
| - if (AutocompleteMatch::DestinationsEqual(elem1, elem2) || |
| - (elem1.stripped_destination_url.is_empty() && |
| - elem2.stripped_destination_url.is_empty())) { |
| - return demote_by_type_(elem1, elem2); |
| - } |
| - return elem1.stripped_destination_url < elem2.stripped_destination_url; |
| -} |
| - |
| -}; // namespace |
| - |
| // static |
| const size_t AutocompleteResult::kMaxMatches = 6; |
| @@ -209,12 +132,12 @@ void AutocompleteResult::SortAndCull( |
| for (ACMatches::iterator i(matches_.begin()); i != matches_.end(); ++i) |
| i->ComputeStrippedDestinationURL(input, languages, template_url_service); |
| - DedupMatchesByDestination(input.current_page_classification(), true, |
| - &matches_); |
| + SortAndDedupMatches(input.current_page_classification(), &matches_); |
| // Sort and trim to the most relevant kMaxMatches matches. |
| size_t max_num_matches = std::min(kMaxMatches, matches_.size()); |
| - CompareWithDemoteByType comparing_object(input.current_page_classification()); |
| + CompareWithDemoteByType<AutocompleteMatch> |
| + comparing_object(input.current_page_classification()); |
| std::sort(matches_.begin(), matches_.end(), comparing_object); |
| if (!matches_.empty() && !matches_.begin()->allowed_to_be_default_match) { |
| // Top match is not allowed to be the default match. Find the most |
| @@ -368,27 +291,24 @@ GURL AutocompleteResult::ComputeAlternateNavUrl( |
| input.canonicalized_url() : GURL(); |
| } |
| -void AutocompleteResult::DedupMatchesByDestination( |
| +void AutocompleteResult::SortAndDedupMatches( |
| OmniboxEventProto::PageClassification page_classification, |
| - bool set_duplicate_matches, |
| ACMatches* matches) { |
| - DestinationSort destination_sort(page_classification); |
| // Sort matches such that duplicate matches are consecutive. |
| - std::sort(matches->begin(), matches->end(), destination_sort); |
| - |
| - if (set_duplicate_matches) { |
| - // Set duplicate_matches for the first match before erasing duplicate |
| - // matches. |
| - for (ACMatches::iterator i(matches->begin()); i != matches->end(); ++i) { |
| - for (int j = 1; (i + j != matches->end()) && |
| - AutocompleteMatch::DestinationsEqual(*i, *(i + j)); ++j) { |
| - AutocompleteMatch& dup_match(*(i + j)); |
| - i->duplicate_matches.insert(i->duplicate_matches.end(), |
| - dup_match.duplicate_matches.begin(), |
| - dup_match.duplicate_matches.end()); |
| - dup_match.duplicate_matches.clear(); |
| - i->duplicate_matches.push_back(dup_match); |
| - } |
| + std::sort(matches->begin(), matches->end(), |
| + DestinationSort<AutocompleteMatch>(page_classification)); |
| + |
| + // Set duplicate_matches for the first match before erasing duplicate |
| + // matches. |
| + for (ACMatches::iterator i(matches->begin()); i != matches->end(); ++i) { |
| + for (int j = 1; (i + j != matches->end()) && |
| + AutocompleteMatch::DestinationsEqual(*i, *(i + j)); ++j) { |
| + AutocompleteMatch& dup_match(*(i + j)); |
| + i->duplicate_matches.insert(i->duplicate_matches.end(), |
| + dup_match.duplicate_matches.begin(), |
| + dup_match.duplicate_matches.end()); |
| + dup_match.duplicate_matches.clear(); |
| + i->duplicate_matches.push_back(dup_match); |
| } |
| } |