Update Polymer components and re-run reproduce.sh
[chromium-blink-merge.git] / components / omnibox / omnibox_field_trial.h
blobe01de8e9b8d4eaee0d33d32215ec081073eb9ae9
1 // Copyright 2014 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_OMNIBOX_FIELD_TRIAL_H_
6 #define COMPONENTS_OMNIBOX_OMNIBOX_FIELD_TRIAL_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/gtest_prod_util.h"
14 #include "components/metrics/proto/omnibox_event.pb.h"
15 #include "components/omnibox/autocomplete_match_type.h"
17 namespace base {
18 class TimeDelta;
21 // The set of parameters customizing the HUP scoring.
22 struct HUPScoringParams {
23 // A set of parameters describing how to cap a given count score. First,
24 // we apply a half-life based decay of the given count and then find the
25 // maximum relevance score in the corresponding bucket list.
26 class ScoreBuckets {
27 public:
28 // (decayed_count, max_relevance) pair.
29 typedef std::pair<double, int> CountMaxRelevance;
31 ScoreBuckets();
32 ~ScoreBuckets();
34 // Computes a half-life time decay given the |elapsed_time|.
35 double HalfLifeTimeDecay(const base::TimeDelta& elapsed_time) const;
37 int relevance_cap() const { return relevance_cap_; }
38 void set_relevance_cap(int relevance_cap) {
39 relevance_cap_ = relevance_cap;
42 int half_life_days() const { return half_life_days_; }
43 void set_half_life_days(int half_life_days) {
44 half_life_days_ = half_life_days;
47 std::vector<CountMaxRelevance>& buckets() { return buckets_; }
48 const std::vector<CountMaxRelevance>& buckets() const { return buckets_; }
50 private:
51 // History matches with relevance score greater or equal to |relevance_cap_|
52 // are not affected by this experiment.
53 // Set to -1, if there is no relevance cap in place and all matches are
54 // subject to demotion.
55 int relevance_cap_;
57 // Half life time for a decayed count as measured since the last visit.
58 // Set to -1 if not used.
59 int half_life_days_;
61 // The relevance score caps for given decayed count values.
62 // Each pair (decayed_count, max_score) indicates what the maximum relevance
63 // score is of a decayed count equal or greater than decayed_count.
65 // Consider this example:
66 // [(1, 1000), (0.5, 500), (0, 100)]
67 // If decayed count is 2 (which is >= 1), the corresponding match's maximum
68 // relevance will be capped at 1000. In case of 0.5, the score is capped
69 // at 500. Anything below 0.5 is capped at 100.
71 // This list is sorted by the pair's first element in descending order.
72 std::vector<CountMaxRelevance> buckets_;
75 HUPScoringParams() : experimental_scoring_enabled(false) {}
77 bool experimental_scoring_enabled;
79 ScoreBuckets typed_count_buckets;
81 // Used only when the typed count is 0.
82 ScoreBuckets visited_count_buckets;
85 // This class manages the Omnibox field trials.
86 class OmniboxFieldTrial {
87 public:
88 // A mapping that contains multipliers indicating that matches of the
89 // specified type should have their relevance score multiplied by the
90 // given number. Omitted types are assumed to have multipliers of 1.0.
91 typedef std::map<AutocompleteMatchType::Type, float> DemotionMultipliers;
93 // Activates all dynamic field trials. The main difference between
94 // the autocomplete dynamic and static field trials is that the former
95 // don't require any code changes on the Chrome side as they are controlled
96 // on the server side. Chrome binary simply propagates all necessary
97 // information through the X-Client-Data header.
98 // This method may be called multiple times.
99 static void ActivateDynamicTrials();
101 // ---------------------------------------------------------
102 // For any experiment that's part of the bundled omnibox field trial.
104 // Returns a bitmap containing AutocompleteProvider::Type values
105 // that should be disabled in AutocompleteController.
106 static int GetDisabledProviderTypes();
108 // Returns whether the user is in any dynamic field trial where the
109 // group has a the prefix |group_prefix|.
110 static bool HasDynamicFieldTrialGroupPrefix(const char *group_prefix);
112 // ---------------------------------------------------------
113 // For the suggest field trial.
115 // Populates |field_trial_hash| with hashes of the active suggest field trial
116 // names, if any.
117 static void GetActiveSuggestFieldTrialHashes(
118 std::vector<uint32>* field_trial_hash);
120 // ---------------------------------------------------------
121 // For the AutocompleteController "stop timer" field trial.
123 // Returns the duration to be used for the AutocompleteController's stop
124 // timer. Returns the default value of 1.5 seconds if the stop timer
125 // override experiment isn't active or if parsing the experiment-provided
126 // duration fails.
127 static base::TimeDelta StopTimerFieldTrialDuration();
129 // ---------------------------------------------------------
130 // For the ZeroSuggestProvider field trial.
132 // Returns whether the user is in any field trial where the
133 // ZeroSuggestProvider should be used to get suggestions when the
134 // user clicks on the omnibox but has not typed anything yet.
135 static bool InZeroSuggestFieldTrial();
137 // Returns whether the user is in a ZeroSuggest field trial, which shows
138 // most visited URLs. This is true for both "MostVisited" and
139 // "MostVisitedWithoutSERP" trials.
140 static bool InZeroSuggestMostVisitedFieldTrial();
142 // Returns whether the user is in ZeroSuggest field trial showing most
143 // visited URLs except it doesn't show suggestions on Google search result
144 // pages.
145 static bool InZeroSuggestMostVisitedWithoutSerpFieldTrial();
147 // Returns whether the user is in a ZeroSuggest field trial and URL-based
148 // suggestions can continue to appear after the user has started typing.
149 static bool InZeroSuggestAfterTypingFieldTrial();
151 // Returns whether the user is in a ZeroSuggest field trial, but should
152 // show recently searched-for queries instead.
153 static bool InZeroSuggestPersonalizedFieldTrial();
155 // ---------------------------------------------------------
156 // For the ShortcutsScoringMaxRelevance experiment that's part of the
157 // bundled omnibox field trial.
159 // If the user is in an experiment group that, given the provided
160 // |current_page_classification| context, changes the maximum relevance
161 // ShortcutsProvider::CalculateScore() is supposed to assign, extract
162 // that maximum relevance score and put in in |max_relevance|. Returns
163 // true on a successful extraction. CalculateScore()'s return value is
164 // a product of this maximum relevance score and some attenuating factors
165 // that are all between 0 and 1. (Note that Shortcuts results may have
166 // their scores reduced later if the assigned score is higher than allowed
167 // for non-inlineable results. Shortcuts results are not allowed to be
168 // inlined.)
169 static bool ShortcutsScoringMaxRelevance(
170 metrics::OmniboxEventProto::PageClassification
171 current_page_classification,
172 int* max_relevance);
174 // ---------------------------------------------------------
175 // For the SearchHistory experiment that's part of the bundled omnibox
176 // field trial.
178 // Returns true if the user is in the experiment group that, given the
179 // provided |current_page_classification| context, scores search history
180 // query suggestions less aggressively so that they don't inline.
181 static bool SearchHistoryPreventInlining(
182 metrics::OmniboxEventProto::PageClassification
183 current_page_classification);
185 // Returns true if the user is in the experiment group that, given the
186 // provided |current_page_classification| context, disables all query
187 // suggestions from search history.
188 static bool SearchHistoryDisable(
189 metrics::OmniboxEventProto::PageClassification
190 current_page_classification);
192 // ---------------------------------------------------------
193 // For the DemoteByType experiment that's part of the bundled omnibox field
194 // trial.
196 // If the user is in an experiment group that, in the provided
197 // |current_page_classification| context, demotes the relevance scores
198 // of certain types of matches, populates the |demotions_by_type| map
199 // appropriately. Otherwise, sets |demotions_by_type| to its default
200 // value based on the context.
201 static void GetDemotionsByType(
202 metrics::OmniboxEventProto::PageClassification
203 current_page_classification,
204 DemotionMultipliers* demotions_by_type);
206 // ---------------------------------------------------------
207 // For the HistoryURL provider new scoring experiment that is part of the
208 // bundled omnibox field trial.
210 // Initializes the HUP |scoring_params| based on the active HUP scoring
211 // experiment. If there is no such experiment, this function simply sets
212 // |scoring_params|->experimental_scoring_enabled to false.
213 static void GetExperimentalHUPScoringParams(HUPScoringParams* scoring_params);
215 // For the HQPBookmarkValue experiment that's part of the
216 // bundled omnibox field trial.
218 // Returns the value an untyped visit to a bookmark should receive.
219 // Compare this value with the default of 1 for non-bookmarked untyped
220 // visits to pages and the default of 20 for typed visits. Returns
221 // 10 if the bookmark value experiment isn't active.
222 static int HQPBookmarkValue();
224 // ---------------------------------------------------------
225 // For the HQPAllowMatchInTLD experiment that's part of the
226 // bundled omnibox field trial.
228 // Returns true if HQP should allow an input term to match in the
229 // top level domain (e.g., .com) of a URL. Returns false if the
230 // allow match in TLD experiment isn't active.
231 static bool HQPAllowMatchInTLDValue();
233 // ---------------------------------------------------------
234 // For the HQPAllowMatchInScheme experiment that's part of the
235 // bundled omnibox field trial.
237 // Returns true if HQP should allow an input term to match in the
238 // scheme (e.g., http://) of a URL. Returns false if the allow
239 // match in scheme experiment isn't active.
240 static bool HQPAllowMatchInSchemeValue();
242 // ---------------------------------------------------------
243 // For the AnswersInSuggest experiment that's part of the bundled omnibox
244 // field trial.
246 // Returns true if the AnswersInSuggest feature should be enabled causing
247 // query responses such as current weather conditions or stock quotes
248 // to be provided in the Omnibox suggestion list. Considers both the
249 // field trial state as well as the overriding command-line flags.
250 static bool EnableAnswersInSuggest();
252 // ---------------------------------------------------------
253 // For SearchProvider related experiments.
255 // Returns true if the search provider should not be caching results.
256 static bool DisableResultsCaching();
258 // Returns how the search provider should poll Suggest. Currently, we support
259 // measuring polling delay from the last keystroke or last suggest request.
260 static void GetSuggestPollingStrategy(bool* from_last_keystroke,
261 int* polling_delay_ms);
263 // ---------------------------------------------------------
264 // For HQP scoring related experiments to control the topicality and scoring
265 // ranges of relevancy scores.
267 // Returns true if HQP experimental scoring is enabled. Returns false if
268 // |kHQPExperimentalScoringEnabledParam| is not specified in the field trial.
269 static bool HQPExperimentalScoringEnabled();
271 // Returns the scoring buckets for HQP experiments. Returns empty string
272 // in case |kHQPExperimentalScoringBucketsParam| or
273 // |kHQPExperimentalScoringEnabledParam| is not specified in the
274 // field trial. Scoring buckets are stored in string form giving mapping from
275 // (topicality_score, frequency_score) to final relevance score.
276 // Please see GetRelevancyScore() under
277 // chrome/browser/history::ScoredHistoryMatch for details.
278 static std::string HQPExperimentalScoringBuckets();
280 // Returns the topicality threshold for HQP experiments. Returns -1 if
281 // |kHQPExperimentalScoringTopicalityThresholdParam| or
282 // |kHQPExperimentalScoringEnabledParam| is not specified in the field trial.
283 static float HQPExperimentalTopicalityThreshold();
285 // ---------------------------------------------------------
286 // For the HQPFixFrequencyScoring experiment that's part of the
287 // bundled omnibox field trial.
289 // Returns true if HQP should apply the bug fixes to the GetFrequency()
290 // function.
291 static bool HQPFixFrequencyScoringBugs();
293 // ---------------------------------------------------------
294 // For the HQPNumTitleWords experiment that's part of the
295 // bundled omnibox field trial.
297 // Returns the number of title words that are allowed to contribute
298 // to the topicality score. Words later in the title are ignored.
299 // Returns 10 as a default if the experiment isn't active.
300 static size_t HQPNumTitleWordsToAllow();
302 // ---------------------------------------------------------
303 // For the HQPAlsoDoHUPLikeScoring experiment that's part of the
304 // bundled omnibox field trial.
306 // Returns whether HistoryQuick provider (HQP) should attempt to score
307 // suggestions also with a HistoryURL-provider-like (HUP-like) mode, and
308 // assign suggestions the max of this score and the normal score.
309 // Returns false if the experiment isn't active.
310 static bool HQPAlsoDoHUPLikeScoring();
312 // ---------------------------------------------------------
313 // Exposed publicly for the sake of unittests.
314 static const char kBundledExperimentFieldTrialName[];
315 // Rule names used by the bundled experiment.
316 static const char kDisableProvidersRule[];
317 static const char kShortcutsScoringMaxRelevanceRule[];
318 static const char kSearchHistoryRule[];
319 static const char kDemoteByTypeRule[];
320 static const char kHQPBookmarkValueRule[];
321 static const char kHQPDiscountFrecencyWhenFewVisitsRule[];
322 static const char kHQPAllowMatchInTLDRule[];
323 static const char kHQPAllowMatchInSchemeRule[];
324 static const char kZeroSuggestRule[];
325 static const char kZeroSuggestVariantRule[];
326 static const char kAnswersInSuggestRule[];
327 static const char kDisableResultsCachingRule[];
328 static const char kMeasureSuggestPollingDelayFromLastKeystrokeRule[];
329 static const char kSuggestPollingDelayMsRule[];
330 static const char kHQPFixFrequencyScoringBugsRule[];
331 static const char kHQPNumTitleWordsRule[];
332 static const char kHQPAlsoDoHUPLikeScoringRule[];
334 // Parameter names used by the HUP new scoring experiments.
335 static const char kHUPNewScoringEnabledParam[];
336 static const char kHUPNewScoringTypedCountRelevanceCapParam[];
337 static const char kHUPNewScoringTypedCountHalfLifeTimeParam[];
338 static const char kHUPNewScoringTypedCountScoreBucketsParam[];
339 static const char kHUPNewScoringVisitedCountRelevanceCapParam[];
340 static const char kHUPNewScoringVisitedCountHalfLifeTimeParam[];
341 static const char kHUPNewScoringVisitedCountScoreBucketsParam[];
343 // Parameter names used by the HQP experimental scoring experiments.
344 static const char kHQPExperimentalScoringEnabledParam[];
345 static const char kHQPExperimentalScoringBucketsParam[];
346 static const char kHQPExperimentalScoringTopicalityThresholdParam[];
348 // The amount of time to wait before sending a new suggest request after the
349 // previous one unless overridden by a field trial parameter.
350 // Non-const because some unittests modify this value.
351 static int kDefaultMinimumTimeBetweenSuggestQueriesMs;
353 private:
354 friend class OmniboxFieldTrialTest;
356 // The bundled omnibox experiment comes with a set of parameters
357 // (key-value pairs). Each key indicates a certain rule that applies in
358 // a certain context. The value indicates what the consequences of
359 // applying the rule are. For example, the value of a SearchHistory rule
360 // in the context of a search results page might indicate that we should
361 // prevent search history matches from inlining.
363 // This function returns the value associated with the |rule| that applies
364 // in the current context (which currently consists of |page_classification|
365 // and whether Instant Extended is enabled). If no such rule exists in the
366 // current context, fall back to the rule in various wildcard contexts and
367 // return its value if found. If the rule remains unfound in the global
368 // context, returns the empty string. For more details, including how we
369 // prioritize different wildcard contexts, see the implementation. How to
370 // interpret the value is left to the caller; this is rule-dependent.
371 static std::string GetValueForRuleInContext(
372 const std::string& rule,
373 metrics::OmniboxEventProto::PageClassification page_classification);
375 DISALLOW_IMPLICIT_CONSTRUCTORS(OmniboxFieldTrial);
378 #endif // COMPONENTS_OMNIBOX_OMNIBOX_FIELD_TRIAL_H_