Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / omnibox / browser / scored_history_match.h
bloba4da02eb1dfbc2e88212cabaf38e03800909828e
1 // Copyright (c) 2012 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.
5 #ifndef COMPONENTS_OMNIBOX_BROWSER_SCORED_HISTORY_MATCH_H_
6 #define COMPONENTS_OMNIBOX_BROWSER_SCORED_HISTORY_MATCH_H_
8 #include <string>
9 #include <vector>
11 #include "base/strings/string16.h"
12 #include "base/time/time.h"
13 #include "components/history/core/browser/history_match.h"
14 #include "components/history/core/browser/history_types.h"
15 #include "components/omnibox/browser/in_memory_url_index_types.h"
16 #include "testing/gtest/include/gtest/gtest_prod.h"
18 class ScoredHistoryMatchTest;
20 // An HistoryMatch that has a score as well as metrics defining where in the
21 // history item's URL and/or page title matches have occurred.
22 struct ScoredHistoryMatch : public history::HistoryMatch {
23 // ScoreMaxRelevance maps from an intermediate-score to the maximum
24 // final-relevance score given to a URL for this intermediate score.
25 // This is used to store the score ranges of HQP relevance buckets.
26 // Please see GetFinalRelevancyScore() for details.
27 typedef std::pair<double, int> ScoreMaxRelevance;
29 // Required for STL, we don't use this directly.
30 ScoredHistoryMatch();
32 // Initializes the ScoredHistoryMatch with a raw score calculated for the
33 // history item given in |row| with recent visits as indicated in |visits|. It
34 // first determines if the row qualifies by seeing if all of the terms in
35 // |terms_vector| occur in |row|. If so, calculates a raw score. This raw
36 // score is in part determined by whether the matches occur at word
37 // boundaries, the locations of which are stored in |word_starts|. For some
38 // terms, it's appropriate to look for the word boundary within the term. For
39 // instance, the term ".net" should look for a word boundary at the "n". These
40 // offsets (".net" should have an offset of 1) come from
41 // |terms_to_word_starts_offsets|. |is_url_bookmarked| indicates whether the
42 // match's URL is referenced by any bookmarks, which can also affect the raw
43 // score. The raw score allows the matches to be ordered and can be used to
44 // influence the final score calculated by the client of this index. If the
45 // row does not qualify the raw score will be 0. |languages| is used to help
46 // parse/format the URL before looking for the terms.
47 ScoredHistoryMatch(const history::URLRow& row,
48 const VisitInfoVector& visits,
49 const std::string& languages,
50 const base::string16& lower_string,
51 const String16Vector& terms_vector,
52 const WordStarts& terms_to_word_starts_offsets,
53 const RowWordStarts& word_starts,
54 bool is_url_bookmarked,
55 base::Time now);
57 ~ScoredHistoryMatch();
59 // Compares two matches by score. Functor supporting URLIndexPrivateData's
60 // HistoryItemsForTerms function. Looks at particular fields within
61 // with url_info to make tie-breaking a bit smarter.
62 static bool MatchScoreGreater(const ScoredHistoryMatch& m1,
63 const ScoredHistoryMatch& m2);
65 // Returns |term_matches| after removing all matches that are not at a
66 // word break that are in the range [|start_pos|, |end_pos|).
67 // start_pos == string::npos is treated as start_pos = length of string.
68 // (In other words, no matches will be filtered.)
69 // end_pos == string::npos is treated as end_pos = length of string.
70 static TermMatches FilterTermMatchesByWordStarts(
71 const TermMatches& term_matches,
72 const WordStarts& terms_to_word_starts_offsets,
73 const WordStarts& word_starts,
74 size_t start_pos,
75 size_t end_pos);
77 // The maximum number of recent visits to examine in GetFrequency().
78 // Public so url_index_private_data.cc knows how many visits it is
79 // expected to deliver (at minimum) to this class.
80 static const size_t kMaxVisitsToScore;
82 // An interim score taking into consideration location and completeness
83 // of the match.
84 int raw_score;
86 // Both these TermMatches contain the set of matches that are considered
87 // important. At this time, that means they exclude mid-word matches
88 // except in the hostname of the URL. (Technically, during early
89 // construction of ScoredHistoryMatch, they may contain all matches, but
90 // unimportant matches are eliminated by GetTopicalityScore(), called
91 // during construction.)
93 // Term matches within the URL.
94 TermMatches url_matches;
95 // Term matches within the page title.
96 TermMatches title_matches;
98 // True if this is a candidate for in-line autocompletion.
99 bool can_inline;
101 private:
102 friend class ScoredHistoryMatchTest;
103 FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, GetFinalRelevancyScore);
104 FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, GetHQPBucketsFromString);
105 FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, ScoringBookmarks);
106 FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, ScoringScheme);
107 FRIEND_TEST_ALL_PREFIXES(ScoredHistoryMatchTest, ScoringTLD);
109 // Initialize ScoredHistoryMatch statics. Must be called before any other
110 // method of ScoredHistoryMatch and before creating any instances.
111 static void Init();
113 // Return a topicality score based on how many matches appear in the url and
114 // the page's title and where they are (e.g., at word boundaries). Revises
115 // url_matches and title_matches in the process so they only reflect matches
116 // used for scoring. (For instance, some mid-word matches are not given
117 // credit in scoring.)
118 float GetTopicalityScore(const int num_terms,
119 const base::string16& cleaned_up_url,
120 const WordStarts& terms_to_word_starts_offsets,
121 const RowWordStarts& word_starts);
123 // Returns a recency score based on |last_visit_days_ago|, which is
124 // how many days ago the page was last visited.
125 float GetRecencyScore(int last_visit_days_ago) const;
127 // Examines the first kMaxVisitsToScore and return a score (higher is
128 // better) based the rate of visits, whether the page is bookmarked, and
129 // how often those visits are typed navigations (i.e., explicitly
130 // invoked by the user). |now| is passed in to avoid unnecessarily
131 // recomputing it frequently.
132 float GetFrequency(const base::Time& now,
133 const bool bookmarked,
134 const VisitInfoVector& visits) const;
136 // Combines the two component scores into a final score that's
137 // an appropriate value to use as a relevancy score. Scoring buckets are
138 // specified through |hqp_relevance_buckets|. Please see the function
139 // implementation for more details.
140 static float GetFinalRelevancyScore(
141 float topicality_score,
142 float frequency_score,
143 const std::vector<ScoreMaxRelevance>& hqp_relevance_buckets);
145 // Initializes the HQP experimental params: |hqp_relevance_buckets_|
146 // to default buckets. If hqp experimental scoring is enabled, it
147 // fetches the |hqp_experimental_scoring_enabled_|, |topicality_threshold_|
148 // and |hqp_relevance_buckets_| from omnibox field trials.
149 static void InitHQPExperimentalParams();
151 // Helper function to parse the string containing the scoring buckets.
152 // For example,
153 // String: "0.0:400,1.5:600,12.0:1300,20.0:1399"
154 // Buckets: vector[(0.0, 400),(1.5,600),(12.0,1300),(20.0,1399)]
155 // Returns false, in case if it fail to parse the string.
156 static bool GetHQPBucketsFromString(
157 const std::string& buckets_str,
158 std::vector<ScoreMaxRelevance>* hqp_buckets);
160 // If true, assign raw scores to be max(whatever it normally would be, a
161 // score that's similar to the score HistoryURL provider would assign).
162 static bool also_do_hup_like_scoring_;
164 // Untyped visits to bookmarked pages score this, compared to 1 for
165 // untyped visits to non-bookmarked pages and 20 for typed visits.
166 static int bookmark_value_;
168 // True if we should fix certain bugs in frequency scoring.
169 static bool fix_frequency_bugs_;
171 // If true, we allow input terms to match in the TLD (e.g., ".com").
172 static bool allow_tld_matches_;
174 // If true, we allow input terms to match in the scheme (e.g., "http://").
175 static bool allow_scheme_matches_;
177 // The number of title words examined when computing topicality scores.
178 // Words beyond this number are ignored.
179 static size_t num_title_words_to_allow_;
181 // True, if hqp experimental scoring is enabled.
182 static bool hqp_experimental_scoring_enabled_;
184 // |topicality_threshold_| is used to control the topicality scoring.
185 // If |topicality_threshold_| > 0, then URLs with topicality-score < threshold
186 // are given topicality score of 0. By default it is initalized to -1.
187 static float topicality_threshold_;
189 // |hqp_relevance_buckets_| gives mapping from (topicality*frequency)
190 // to the final relevance scoring. Please see GetFinalRelevancyScore()
191 // for more details and scoring method.
192 static std::vector<ScoreMaxRelevance>* hqp_relevance_buckets_;
194 typedef std::vector<ScoredHistoryMatch> ScoredHistoryMatches;
196 #endif // COMPONENTS_OMNIBOX_BROWSER_SCORED_HISTORY_MATCH_H_