OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_OMNIBOX_BROWSER_MATCH_COMPARE_H_ |
| 6 #define COMPONENTS_OMNIBOX_BROWSER_MATCH_COMPARE_H_ |
| 7 |
| 8 #include "components/omnibox/browser/omnibox_field_trial.h" |
| 9 |
| 10 // This class implements a special version of AutocompleteMatch::MoreRelevant |
| 11 // that allows matches of particular types to be demoted in AutocompleteResult. |
| 12 template <class Match> |
| 13 class CompareWithDemoteByType { |
| 14 public: |
| 15 CompareWithDemoteByType( |
| 16 metrics::OmniboxEventProto::PageClassification page_classification) { |
| 17 OmniboxFieldTrial::GetDemotionsByType(page_classification, &demotions_); |
| 18 } |
| 19 |
| 20 // Returns the relevance score of |match| demoted appropriately by |
| 21 // |demotions_by_type_|. |
| 22 int GetDemotedRelevance(const Match& match) const { |
| 23 OmniboxFieldTrial::DemotionMultipliers::const_iterator demotion_it = |
| 24 demotions_.find(match.type); |
| 25 return (demotion_it == demotions_.end()) |
| 26 ? match.relevance |
| 27 : (match.relevance * demotion_it->second); |
| 28 } |
| 29 |
| 30 // Comparison function. |
| 31 bool operator()(const Match& elem1, const Match& elem2) { |
| 32 // Compute demoted relevance scores for each match. |
| 33 const int demoted_relevance1 = GetDemotedRelevance(elem1); |
| 34 const int demoted_relevance2 = GetDemotedRelevance(elem2); |
| 35 // For equal-relevance matches, we sort alphabetically, so that providers |
| 36 // who return multiple elements at the same priority get a "stable" sort |
| 37 // across multiple updates. |
| 38 return (demoted_relevance1 == demoted_relevance2) |
| 39 ? (elem1.contents < elem2.contents) |
| 40 : (demoted_relevance1 > demoted_relevance2); |
| 41 } |
| 42 |
| 43 private: |
| 44 OmniboxFieldTrial::DemotionMultipliers demotions_; |
| 45 }; |
| 46 |
| 47 template <class Match> |
| 48 class DestinationSort { |
| 49 public: |
| 50 DestinationSort( |
| 51 metrics::OmniboxEventProto::PageClassification page_classification) |
| 52 : demote_by_type_(page_classification) {} |
| 53 bool operator()(const Match& elem1, const Match& elem2) { |
| 54 // Sort identical destination_urls together. |
| 55 // Place the most relevant matches first, so that when we call |
| 56 // std::unique(), these are the ones that get preserved. |
| 57 return (elem1.stripped_destination_url == elem2.stripped_destination_url) |
| 58 ? demote_by_type_(elem1, elem2) |
| 59 : (elem1.stripped_destination_url < |
| 60 elem2.stripped_destination_url); |
| 61 } |
| 62 |
| 63 private: |
| 64 CompareWithDemoteByType<Match> demote_by_type_; |
| 65 }; |
| 66 |
| 67 #endif // COMPONENTS_OMNIBOX_BROWSER_MATCH_COMPARE_H_ |
OLD | NEW |