Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(274)

Side by Side Diff: components/omnibox/browser/match_compare.h

Issue 1877833002: Optimize shortcuts provider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reverted rename of ShortcutMatchToACMatch to ShortcutToACMatch Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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> class CompareWithDemoteByType {
13 public:
14 CompareWithDemoteByType(
15 metrics::OmniboxEventProto::PageClassification page_classification) {
16 OmniboxFieldTrial::GetDemotionsByType(page_classification, &demotions_);
17 }
18
19 // Returns the relevance score of |match| demoted appropriately by
20 // |demotions_by_type_|.
21 int GetDemotedRelevance(const Match& match) const {
22 OmniboxFieldTrial::DemotionMultipliers::const_iterator demotion_it =
23 demotions_.find(match.type);
24 return (demotion_it == demotions_.end()) ?
25 match.relevance : (match.relevance * demotion_it->second);
26 }
27
28 // Comparison function.
29 bool operator()(const Match& elem1, const Match& elem2) {
30 // Compute demoted relevance scores for each match.
31 const int demoted_relevance1 = GetDemotedRelevance(elem1);
32 const int demoted_relevance2 = GetDemotedRelevance(elem2);
33 // For equal-relevance matches, we sort alphabetically, so that providers
34 // who return multiple elements at the same priority get a "stable" sort
35 // across multiple updates.
36 return (demoted_relevance1 == demoted_relevance2) ?
37 (elem1.contents < elem2.contents) :
38 (demoted_relevance1 > demoted_relevance2);
Peter Kasting 2016/04/15 20:23:44 Not sure this indentation is correct for the ?: op
Alexander Yashkin 2016/04/16 05:18:17 Sorry, I completely forgot about git cl format com
39 }
40
41 private:
42 OmniboxFieldTrial::DemotionMultipliers demotions_;
43 };
44
45 template<class Match> class DestinationSort {
46 public:
47 DestinationSort(
48 metrics::OmniboxEventProto::PageClassification page_classification)
49 : demote_by_type_(page_classification) {}
50 bool operator()(const Match& elem1, const Match& elem2) {
51 // Sort identical destination_urls together.
52 // Place the most relevant matches first, so that when we call
53 // std::unique(), these are the ones that get preserved.
54 return (elem1.stripped_destination_url == elem2.stripped_destination_url) ?
55 demote_by_type_(elem1, elem2) :
56 (elem1.stripped_destination_url < elem2.stripped_destination_url);
57 }
58
59 private:
60 CompareWithDemoteByType<Match> demote_by_type_;
61 };
62
63 #endif // COMPONENTS_OMNIBOX_BROWSER_MATCH_COMPARE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698