Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / omnibox / omnibox_field_trial.h
blob7389677a7c1db38695f6388773dd85357eafd0f0
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 CHROME_BROWSER_OMNIBOX_OMNIBOX_FIELD_TRIAL_H_
6 #define CHROME_BROWSER_OMNIBOX_OMNIBOX_FIELD_TRIAL_H_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/basictypes.h"
14 #include "base/gtest_prod_util.h"
15 #include "chrome/browser/autocomplete/autocomplete_input.h"
16 #include "chrome/common/autocomplete_match_type.h"
18 namespace base {
19 class TimeDelta;
22 // The set of parameters customizing the HUP scoring.
23 struct HUPScoringParams {
24 // A set of parameters describing how to cap a given count score. First,
25 // we apply a half-life based decay of the given count and then find the
26 // maximum relevance score in the corresponding bucket list.
27 class ScoreBuckets {
28 public:
29 // (decayed_count, max_relevance) pair.
30 typedef std::pair<double, int> CountMaxRelevance;
32 ScoreBuckets();
33 ~ScoreBuckets();
35 // Computes a half-life time decay given the |elapsed_time|.
36 double HalfLifeTimeDecay(const base::TimeDelta& elapsed_time) const;
38 int relevance_cap() const { return relevance_cap_; }
39 void set_relevance_cap(int relevance_cap) {
40 relevance_cap_ = relevance_cap;
43 int half_life_days() const { return half_life_days_; }
44 void set_half_life_days(int half_life_days) {
45 half_life_days_ = half_life_days;
48 std::vector<CountMaxRelevance>& buckets() { return buckets_; }
49 const std::vector<CountMaxRelevance>& buckets() const { return buckets_; }
51 private:
52 // History matches with relevance score greater or equal to |relevance_cap_|
53 // are not affected by this experiment.
54 // Set to -1, if there is no relevance cap in place and all matches are
55 // subject to demotion.
56 int relevance_cap_;
58 // Half life time for a decayed count as measured since the last visit.
59 // Set to -1 if not used.
60 int half_life_days_;
62 // The relevance score caps for given decayed count values.
63 // Each pair (decayed_count, max_score) indicates what the maximum relevance
64 // score is of a decayed count equal or greater than decayed_count.
66 // Consider this example:
67 // [(1, 1000), (0.5, 500), (0, 100)]
68 // If decayed count is 2 (which is >= 1), the corresponding match's maximum
69 // relevance will be capped at 1000. In case of 0.5, the score is capped
70 // at 500. Anything below 0.5 is capped at 100.
72 // This list is sorted by the pair's first element in descending order.
73 std::vector<CountMaxRelevance> buckets_;
76 HUPScoringParams() : experimental_scoring_enabled(false) {}
78 bool experimental_scoring_enabled;
80 ScoreBuckets typed_count_buckets;
82 // Used only when the typed count is 0.
83 ScoreBuckets visited_count_buckets;
86 // This class manages the Omnibox field trials.
87 class OmniboxFieldTrial {
88 public:
89 // A mapping that contains multipliers indicating that matches of the
90 // specified type should have their relevance score multiplied by the
91 // given number. Omitted types are assumed to have multipliers of 1.0.
92 typedef std::map<AutocompleteMatchType::Type, float> DemotionMultipliers;
94 // A set of types that should not be demoted when they are the top match.
95 typedef std::set<AutocompleteMatchType::Type> UndemotableTopMatchTypes;
97 // Creates the static field trial groups.
98 // *** MUST NOT BE CALLED MORE THAN ONCE. ***
99 static void ActivateStaticTrials();
101 // Activates all dynamic field trials. The main difference between
102 // the autocomplete dynamic and static field trials is that the former
103 // don't require any code changes on the Chrome side as they are controlled
104 // on the server side. Chrome binary simply propagates all necessary
105 // information through the X-Client-Data header.
106 // This method, unlike ActivateStaticTrials(), may be called multiple times.
107 static void ActivateDynamicTrials();
109 // Returns a bitmap containing AutocompleteProvider::Type values
110 // that should be disabled in AutocompleteController.
111 // This method simply goes over all autocomplete dynamic field trial groups
112 // and looks for group names like "ProvidersDisabled_NNN" where NNN is
113 // an integer corresponding to a bitmap mask. All extracted bitmaps
114 // are OR-ed together and returned as the final result.
115 static int GetDisabledProviderTypes();
117 // Returns whether the user is in any dynamic field trial where the
118 // group has a the prefix |group_prefix|.
119 static bool HasDynamicFieldTrialGroupPrefix(const char *group_prefix);
121 // ---------------------------------------------------------
122 // For the suggest field trial.
124 // Populates |field_trial_hash| with hashes of the active suggest field trial
125 // names, if any.
126 static void GetActiveSuggestFieldTrialHashes(
127 std::vector<uint32>* field_trial_hash);
129 // ---------------------------------------------------------
130 // For the HistoryURL provider disable culling redirects field trial.
132 // Returns whether the user is in any group for this field trial.
133 // (Should always be true unless initialization went wrong.)
134 static bool InHUPCullRedirectsFieldTrial();
136 // Returns whether we should disable culling of redirects in
137 // HistoryURL provider.
138 static bool InHUPCullRedirectsFieldTrialExperimentGroup();
140 // ---------------------------------------------------------
141 // For the HistoryURL provider disable creating a shorter match
142 // field trial.
144 // Returns whether the user is in any group for this field trial.
145 // (Should always be true unless initialization went wrong.)
146 static bool InHUPCreateShorterMatchFieldTrial();
148 // Returns whether we should disable creating a shorter match in
149 // HistoryURL provider.
150 static bool InHUPCreateShorterMatchFieldTrialExperimentGroup();
152 // ---------------------------------------------------------
153 // For the AutocompleteController "stop timer" field trial.
155 // Returns the duration to be used for the AutocompleteController's stop
156 // timer. Returns the default value of 1.5 seconds if the stop timer
157 // override experiment isn't active or if parsing the experiment-provided
158 // duration fails.
159 static base::TimeDelta StopTimerFieldTrialDuration();
161 // ---------------------------------------------------------
162 // For the ZeroSuggestProvider field trial.
164 // Returns whether the user is in any field trial where the
165 // ZeroSuggestProvider should be used to get suggestions when the
166 // user clicks on the omnibox but has not typed anything yet.
167 static bool InZeroSuggestFieldTrial();
169 // Returns whether the user is in a ZeroSuggest field trial, but should
170 // show most visited URL instead. This is used to compare metrics of
171 // ZeroSuggest and most visited suggestions.
172 static bool InZeroSuggestMostVisitedFieldTrial();
174 // Returns whether the user is in a ZeroSuggest field trial and URL-based
175 // suggestions can continue to appear after the user has started typing.
176 static bool InZeroSuggestAfterTypingFieldTrial();
178 // ---------------------------------------------------------
179 // For the ShortcutsScoringMaxRelevance experiment that's part of the
180 // bundled omnibox field trial.
182 // If the user is in an experiment group that, given the provided
183 // |current_page_classification| context, changes the maximum relevance
184 // ShortcutsProvider::CalculateScore() is supposed to assign, extract
185 // that maximum relevance score and put in in |max_relevance|. Returns
186 // true on a successful extraction. CalculateScore()'s return value is
187 // a product of this maximum relevance score and some attenuating factors
188 // that are all between 0 and 1. (Note that Shortcuts results may have
189 // their scores reduced later if the assigned score is higher than allowed
190 // for non-inlineable results. Shortcuts results are not allowed to be
191 // inlined.)
192 static bool ShortcutsScoringMaxRelevance(
193 AutocompleteInput::PageClassification current_page_classification,
194 int* max_relevance);
196 // ---------------------------------------------------------
197 // For the SearchHistory experiment that's part of the bundled omnibox
198 // field trial.
200 // Returns true if the user is in the experiment group that, given the
201 // provided |current_page_classification| context, scores search history
202 // query suggestions less aggressively so that they don't inline.
203 static bool SearchHistoryPreventInlining(
204 AutocompleteInput::PageClassification current_page_classification);
206 // Returns true if the user is in the experiment group that, given the
207 // provided |current_page_classification| context, disables all query
208 // suggestions from search history.
209 static bool SearchHistoryDisable(
210 AutocompleteInput::PageClassification current_page_classification);
212 // ---------------------------------------------------------
213 // For the DemoteByType experiment that's part of the bundled omnibox field
214 // trial.
216 // If the user is in an experiment group that, in the provided
217 // |current_page_classification| context, demotes the relevance scores
218 // of certain types of matches, populates the |demotions_by_type| map
219 // appropriately. Otherwise, clears |demotions_by_type|.
220 static void GetDemotionsByType(
221 AutocompleteInput::PageClassification current_page_classification,
222 DemotionMultipliers* demotions_by_type);
224 // Get the set of types that should not be demoted if they are the top
225 // match.
226 static UndemotableTopMatchTypes GetUndemotableTopTypes(
227 AutocompleteInput::PageClassification current_page_classification);
229 // ---------------------------------------------------------
230 // For the ReorderForLegalDefaultMatch experiment that's part of the
231 // bundled omnibox field trial.
233 // Returns true if the omnibox will reorder matches, in the provided
234 // |current_page_classification| context so that a match that's allowed to
235 // be the default match will appear first. This means AutocompleteProviders
236 // can score matches however they desire without regard to making sure the
237 // top match when all the matches from all providers are merged is a legal
238 // default match.
239 static bool ReorderForLegalDefaultMatch(
240 AutocompleteInput::PageClassification current_page_classification);
242 // ---------------------------------------------------------
243 // For the HistoryURL provider new scoring experiment that is part of the
244 // bundled omnibox field trial.
246 // Initializes the HUP |scoring_params| based on the active HUP scoring
247 // experiment. If there is no such experiment, this function simply sets
248 // |scoring_params|->experimental_scoring_enabled to false.
249 static void GetExperimentalHUPScoringParams(HUPScoringParams* scoring_params);
251 // For the HQPBookmarkValue experiment that's part of the
252 // bundled omnibox field trial.
254 // Returns the value an untyped visit to a bookmark should receive.
255 // Compare this value with the default of 1 for non-bookmarked untyped
256 // visits to pages and the default of 20 for typed visits. Returns
257 // 1 if the bookmark value experiment isn't active.
258 static int HQPBookmarkValue();
260 // ---------------------------------------------------------
261 // For the HQPDiscountFrecencyWhenFewVisits experiment that's part of
262 // the bundled omnibox field trial.
264 // Returns whether to discount the frecency score estimates when a
265 // URL has fewer than ScoredHistoryMatch::kMaxVisitsToScore visits.
266 // See comments in scored_history_match.h for details. Returns false
267 // if the discount frecency experiment isn't active.
268 static bool HQPDiscountFrecencyWhenFewVisits();
270 // ---------------------------------------------------------
271 // For the HQPAllowMatchInTLD experiment that's part of the
272 // bundled omnibox field trial.
274 // Returns true if HQP should allow an input term to match in the
275 // top level domain (e.g., .com) of a URL. Returns false if the
276 // allow match in TLD experiment isn't active.
277 static bool HQPAllowMatchInTLDValue();
279 // ---------------------------------------------------------
280 // For the HQPAllowMatchInScheme experiment that's part of the
281 // bundled omnibox field trial.
283 // Returns true if HQP should allow an input term to match in the
284 // scheme (e.g., http://) of a URL. Returns false if the allow
285 // match in scheme experiment isn't active.
286 static bool HQPAllowMatchInSchemeValue();
288 // ---------------------------------------------------------
289 // Exposed publicly for the sake of unittests.
290 static const char kBundledExperimentFieldTrialName[];
291 // Rule names used by the bundled experiment.
292 static const char kShortcutsScoringMaxRelevanceRule[];
293 static const char kSearchHistoryRule[];
294 static const char kDemoteByTypeRule[];
295 static const char kUndemotableTopTypeRule[];
296 static const char kReorderForLegalDefaultMatchRule[];
297 static const char kHQPBookmarkValueRule[];
298 static const char kHQPDiscountFrecencyWhenFewVisitsRule[];
299 static const char kHQPAllowMatchInTLDRule[];
300 static const char kHQPAllowMatchInSchemeRule[];
301 // Rule values.
302 static const char kReorderForLegalDefaultMatchRuleEnabled[];
304 // Parameter names used by the HUP new scoring experiments.
305 static const char kHUPNewScoringEnabledParam[];
306 static const char kHUPNewScoringTypedCountRelevanceCapParam[];
307 static const char kHUPNewScoringTypedCountHalfLifeTimeParam[];
308 static const char kHUPNewScoringTypedCountScoreBucketsParam[];
309 static const char kHUPNewScoringVisitedCountRelevanceCapParam[];
310 static const char kHUPNewScoringVisitedCountHalfLifeTimeParam[];
311 static const char kHUPNewScoringVisitedCountScoreBucketsParam[];
313 private:
314 friend class OmniboxFieldTrialTest;
316 // The bundled omnibox experiment comes with a set of parameters
317 // (key-value pairs). Each key indicates a certain rule that applies in
318 // a certain context. The value indicates what the consequences of
319 // applying the rule are. For example, the value of a SearchHistory rule
320 // in the context of a search results page might indicate that we should
321 // prevent search history matches from inlining.
323 // This function returns the value associated with the |rule| that applies
324 // in the current context (which currently consists of |page_classification|
325 // and whether Instant Extended is enabled). If no such rule exists in the
326 // current context, fall back to the rule in various wildcard contexts and
327 // return its value if found. If the rule remains unfound in the global
328 // context, returns the empty string. For more details, including how we
329 // prioritize different wildcard contexts, see the implementation. How to
330 // interpret the value is left to the caller; this is rule-dependent.
331 static std::string GetValueForRuleInContext(
332 const std::string& rule,
333 AutocompleteInput::PageClassification page_classification);
335 DISALLOW_IMPLICIT_CONSTRUCTORS(OmniboxFieldTrial);
338 #endif // CHROME_BROWSER_OMNIBOX_OMNIBOX_FIELD_TRIAL_H_