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..b9b2fa5640dea3d05b9ecb55747a36cd43b974f9 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; |
-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; |
@@ -214,7 +137,8 @@ void AutocompleteResult::SortAndCull( |
// 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 |
@@ -372,7 +296,7 @@ void AutocompleteResult::DedupMatchesByDestination( |
OmniboxEventProto::PageClassification page_classification, |
bool set_duplicate_matches, |
ACMatches* matches) { |
- DestinationSort destination_sort(page_classification); |
+ DestinationSort<AutocompleteMatch> destination_sort(page_classification); |
// Sort matches such that duplicate matches are consecutive. |
std::sort(matches->begin(), matches->end(), destination_sort); |