Chromium Code Reviews| 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 using metrics::OmniboxEventProto; | |
| 11 | |
| 12 // This class implements a special version of AutocompleteMatch::MoreRelevant | |
| 13 // that allows matches of particular types to be demoted in AutocompleteResult. | |
| 14 template <class Match> class CompareWithDemoteByType { | |
| 15 public: | |
| 16 CompareWithDemoteByType( | |
| 17 OmniboxEventProto::PageClassification current_page_classification) { | |
| 18 OmniboxFieldTrial::GetDemotionsByType(current_page_classification, | |
| 19 &demotions_); | |
| 20 } | |
| 21 | |
| 22 // Returns the relevance score of |match| demoted appropriately by | |
| 23 // |demotions_by_type_|. | |
| 24 int GetDemotedRelevance(const Match& match) { | |
|
Peter Kasting
2016/04/12 00:55:08
Nit: Can be const
Alexander Yashkin
2016/04/12 09:09:20
Done.
| |
| 25 OmniboxFieldTrial::DemotionMultipliers::const_iterator demotion_it = | |
| 26 demotions_.find(match.type); | |
| 27 return (demotion_it == demotions_.end()) ? | |
| 28 match.relevance : (match.relevance * demotion_it->second); | |
| 29 } | |
| 30 | |
| 31 // Comparison function. | |
| 32 bool operator()(const Match& elem1, const Match& elem2) { | |
| 33 // Compute demoted relevance scores for each match. | |
| 34 const int demoted_relevance1 = GetDemotedRelevance(elem1); | |
| 35 const int demoted_relevance2 = GetDemotedRelevance(elem2); | |
| 36 // For equal-relevance matches, we sort alphabetically, so that providers | |
| 37 // who return multiple elements at the same priority get a "stable" sort | |
| 38 // across multiple updates. | |
| 39 return (demoted_relevance1 == demoted_relevance2) ? | |
| 40 (elem1.contents < elem2.contents) : | |
| 41 (demoted_relevance1 > demoted_relevance2); | |
| 42 } | |
| 43 | |
| 44 private: | |
| 45 OmniboxFieldTrial::DemotionMultipliers demotions_; | |
| 46 }; | |
| 47 | |
| 48 template<class Match> class DestinationSort { | |
| 49 public: | |
| 50 DestinationSort( | |
| 51 OmniboxEventProto::PageClassification current_page_classification) : | |
| 52 demote_by_type_(current_page_classification) {} | |
| 53 bool operator()(const Match& elem1, | |
| 54 const Match& elem2) { | |
| 55 // Sort identical destination_urls together. | |
| 56 // Place the most relevant matches first, so that when we call | |
| 57 // std::unique(), these are the ones that get preserved. | |
| 58 if (Match::DestinationsEqual(elem1, elem2) || | |
| 59 (elem1.stripped_destination_url.is_empty() && | |
| 60 elem2.stripped_destination_url.is_empty())) { | |
| 61 return demote_by_type_(elem1, elem2); | |
| 62 } | |
| 63 return elem1.stripped_destination_url < elem2.stripped_destination_url; | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 CompareWithDemoteByType<Match> demote_by_type_; | |
| 68 }; | |
| 69 | |
| 70 #endif // COMPONENTS_OMNIBOX_BROWSER_MATCH_COMPARE_H_ | |
| OLD | NEW |