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_
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"
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.
29 // (decayed_count, max_relevance) pair.
30 typedef std::pair
<double, int> CountMaxRelevance
;
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_
; }
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.
58 // Half life time for a decayed count as measured since the last visit.
59 // Set to -1 if not used.
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
{
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
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
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
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 // Returns whether the user is in a ZeroSuggest field trial, but should
179 // show recently searched-for queries instead.
180 static bool InZeroSuggestPersonalizedFieldTrial();
182 // ---------------------------------------------------------
183 // For the ShortcutsScoringMaxRelevance experiment that's part of the
184 // bundled omnibox field trial.
186 // If the user is in an experiment group that, given the provided
187 // |current_page_classification| context, changes the maximum relevance
188 // ShortcutsProvider::CalculateScore() is supposed to assign, extract
189 // that maximum relevance score and put in in |max_relevance|. Returns
190 // true on a successful extraction. CalculateScore()'s return value is
191 // a product of this maximum relevance score and some attenuating factors
192 // that are all between 0 and 1. (Note that Shortcuts results may have
193 // their scores reduced later if the assigned score is higher than allowed
194 // for non-inlineable results. Shortcuts results are not allowed to be
196 static bool ShortcutsScoringMaxRelevance(
197 AutocompleteInput::PageClassification current_page_classification
,
200 // ---------------------------------------------------------
201 // For the SearchHistory experiment that's part of the bundled omnibox
204 // Returns true if the user is in the experiment group that, given the
205 // provided |current_page_classification| context, scores search history
206 // query suggestions less aggressively so that they don't inline.
207 static bool SearchHistoryPreventInlining(
208 AutocompleteInput::PageClassification current_page_classification
);
210 // Returns true if the user is in the experiment group that, given the
211 // provided |current_page_classification| context, disables all query
212 // suggestions from search history.
213 static bool SearchHistoryDisable(
214 AutocompleteInput::PageClassification current_page_classification
);
216 // ---------------------------------------------------------
217 // For the DemoteByType experiment that's part of the bundled omnibox field
220 // If the user is in an experiment group that, in the provided
221 // |current_page_classification| context, demotes the relevance scores
222 // of certain types of matches, populates the |demotions_by_type| map
223 // appropriately. Otherwise, sets |demotions_by_type| to its default
224 // value based on the context.
225 static void GetDemotionsByType(
226 AutocompleteInput::PageClassification current_page_classification
,
227 DemotionMultipliers
* demotions_by_type
);
229 // Get the set of types that should not be demoted if they are the top
231 static UndemotableTopMatchTypes
GetUndemotableTopTypes(
232 AutocompleteInput::PageClassification current_page_classification
);
234 // ---------------------------------------------------------
235 // For the ReorderForLegalDefaultMatch experiment that's part of the
236 // bundled omnibox field trial.
238 // Returns true if the omnibox will reorder matches, in the provided
239 // |current_page_classification| context so that a match that's allowed to
240 // be the default match will appear first. This means AutocompleteProviders
241 // can score matches however they desire without regard to making sure the
242 // top match when all the matches from all providers are merged is a legal
244 static bool ReorderForLegalDefaultMatch(
245 AutocompleteInput::PageClassification current_page_classification
);
247 // ---------------------------------------------------------
248 // For the HistoryURL provider new scoring experiment that is part of the
249 // bundled omnibox field trial.
251 // Initializes the HUP |scoring_params| based on the active HUP scoring
252 // experiment. If there is no such experiment, this function simply sets
253 // |scoring_params|->experimental_scoring_enabled to false.
254 static void GetExperimentalHUPScoringParams(HUPScoringParams
* scoring_params
);
256 // For the HQPBookmarkValue experiment that's part of the
257 // bundled omnibox field trial.
259 // Returns the value an untyped visit to a bookmark should receive.
260 // Compare this value with the default of 1 for non-bookmarked untyped
261 // visits to pages and the default of 20 for typed visits. Returns
262 // 1 if the bookmark value experiment isn't active.
263 static int HQPBookmarkValue();
265 // ---------------------------------------------------------
266 // For the HQPDiscountFrecencyWhenFewVisits experiment that's part of
267 // the bundled omnibox field trial.
269 // Returns whether to discount the frecency score estimates when a
270 // URL has fewer than ScoredHistoryMatch::kMaxVisitsToScore visits.
271 // See comments in scored_history_match.h for details. Returns false
272 // if the discount frecency experiment isn't active.
273 static bool HQPDiscountFrecencyWhenFewVisits();
275 // ---------------------------------------------------------
276 // For the HQPAllowMatchInTLD experiment that's part of the
277 // bundled omnibox field trial.
279 // Returns true if HQP should allow an input term to match in the
280 // top level domain (e.g., .com) of a URL. Returns false if the
281 // allow match in TLD experiment isn't active.
282 static bool HQPAllowMatchInTLDValue();
284 // ---------------------------------------------------------
285 // For the HQPAllowMatchInScheme experiment that's part of the
286 // bundled omnibox field trial.
288 // Returns true if HQP should allow an input term to match in the
289 // scheme (e.g., http://) of a URL. Returns false if the allow
290 // match in scheme experiment isn't active.
291 static bool HQPAllowMatchInSchemeValue();
293 // ---------------------------------------------------------
294 // Exposed publicly for the sake of unittests.
295 static const char kBundledExperimentFieldTrialName
[];
296 // Rule names used by the bundled experiment.
297 static const char kShortcutsScoringMaxRelevanceRule
[];
298 static const char kSearchHistoryRule
[];
299 static const char kDemoteByTypeRule
[];
300 static const char kUndemotableTopTypeRule
[];
301 static const char kReorderForLegalDefaultMatchRule
[];
302 static const char kHQPBookmarkValueRule
[];
303 static const char kHQPDiscountFrecencyWhenFewVisitsRule
[];
304 static const char kHQPAllowMatchInTLDRule
[];
305 static const char kHQPAllowMatchInSchemeRule
[];
306 static const char kZeroSuggestRule
[];
307 static const char kZeroSuggestVariantRule
[];
309 static const char kReorderForLegalDefaultMatchRuleDisabled
[];
311 // Parameter names used by the HUP new scoring experiments.
312 static const char kHUPNewScoringEnabledParam
[];
313 static const char kHUPNewScoringTypedCountRelevanceCapParam
[];
314 static const char kHUPNewScoringTypedCountHalfLifeTimeParam
[];
315 static const char kHUPNewScoringTypedCountScoreBucketsParam
[];
316 static const char kHUPNewScoringVisitedCountRelevanceCapParam
[];
317 static const char kHUPNewScoringVisitedCountHalfLifeTimeParam
[];
318 static const char kHUPNewScoringVisitedCountScoreBucketsParam
[];
321 friend class OmniboxFieldTrialTest
;
323 // The bundled omnibox experiment comes with a set of parameters
324 // (key-value pairs). Each key indicates a certain rule that applies in
325 // a certain context. The value indicates what the consequences of
326 // applying the rule are. For example, the value of a SearchHistory rule
327 // in the context of a search results page might indicate that we should
328 // prevent search history matches from inlining.
330 // This function returns the value associated with the |rule| that applies
331 // in the current context (which currently consists of |page_classification|
332 // and whether Instant Extended is enabled). If no such rule exists in the
333 // current context, fall back to the rule in various wildcard contexts and
334 // return its value if found. If the rule remains unfound in the global
335 // context, returns the empty string. For more details, including how we
336 // prioritize different wildcard contexts, see the implementation. How to
337 // interpret the value is left to the caller; this is rule-dependent.
338 static std::string
GetValueForRuleInContext(
339 const std::string
& rule
,
340 AutocompleteInput::PageClassification page_classification
);
342 DISALLOW_IMPLICIT_CONSTRUCTORS(OmniboxFieldTrial
);
345 #endif // CHROME_BROWSER_OMNIBOX_OMNIBOX_FIELD_TRIAL_H_