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; | |
Peter Kasting
2016/04/14 23:52:33
Nit: Avoid this using directive, just explicitly q
Alexander Yashkin
2016/04/15 09:14:14
Done.
Yet, IMHO, metrics::OmniboxEventProto::Page
| |
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) const { | |
25 OmniboxFieldTrial::DemotionMultipliers::const_iterator demotion_it = | |
Peter Kasting
2016/04/14 23:52:33
Nit: Wrong indentation (this line is in too far, n
Alexander Yashkin
2016/04/15 09:14:14
Done.
| |
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) : | |
Peter Kasting
2016/04/14 23:52:33
Nit: ':' goes at the beginning of the next line, n
Alexander Yashkin
2016/04/15 09:14:14
Done.
| |
52 demote_by_type_(current_page_classification) {} | |
53 bool operator()(const Match& elem1, | |
Peter Kasting
2016/04/14 23:52:33
Nit: Unnecessary linebreak
Alexander Yashkin
2016/04/15 09:14:14
Done.
| |
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 bool matches_destinations_equal = | |
59 !elem1.stripped_destination_url.is_empty() && | |
60 (elem1.stripped_destination_url == elem2.stripped_destination_url); | |
61 bool matches_destinations_empty = | |
62 elem1.stripped_destination_url.is_empty() && | |
63 elem2.stripped_destination_url.is_empty(); | |
64 if (matches_destinations_equal || matches_destinations_empty) { | |
Peter Kasting
2016/04/14 23:52:33
This is all more complicated than need be. The lo
Alexander Yashkin
2016/04/15 09:14:14
Good point. Done.
| |
65 return demote_by_type_(elem1, elem2); | |
66 } | |
67 return elem1.stripped_destination_url < elem2.stripped_destination_url; | |
68 } | |
69 | |
70 private: | |
71 CompareWithDemoteByType<Match> demote_by_type_; | |
72 }; | |
73 | |
74 #endif // COMPONENTS_OMNIBOX_BROWSER_MATCH_COMPARE_H_ | |
OLD | NEW |