Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / omnibox / omnibox_field_trial.cc
blob98c69dd8970dd8aecf999421389ae93e074c8fd0
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 #include "chrome/browser/omnibox/omnibox_field_trial.h"
7 #include <cmath>
8 #include <string>
10 #include "base/metrics/field_trial.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/time/time.h"
16 #include "chrome/browser/autocomplete/autocomplete_input.h"
17 #include "chrome/browser/search/search.h"
18 #include "chrome/common/metrics/variations/variation_ids.h"
19 #include "chrome/common/metrics/variations/variations_util.h"
20 #include "components/variations/metrics_util.h"
22 namespace {
24 typedef std::map<std::string, std::string> VariationParams;
25 typedef HUPScoringParams::ScoreBuckets ScoreBuckets;
27 // Field trial names.
28 const char kHUPCullRedirectsFieldTrialName[] = "OmniboxHUPCullRedirects";
29 const char kHUPCreateShorterMatchFieldTrialName[] =
30 "OmniboxHUPCreateShorterMatch";
31 const char kStopTimerFieldTrialName[] = "OmniboxStopTimer";
32 const char kEnableZeroSuggestGroupPrefix[] = "EnableZeroSuggest";
33 const char kEnableZeroSuggestMostVisitedGroupPrefix[] =
34 "EnableZeroSuggestMostVisited";
35 const char kEnableZeroSuggestAfterTypingGroupPrefix[] =
36 "EnableZeroSuggestAfterTyping";
38 // The autocomplete dynamic field trial name prefix. Each field trial is
39 // configured dynamically and is retrieved automatically by Chrome during
40 // the startup.
41 const char kAutocompleteDynamicFieldTrialPrefix[] = "AutocompleteDynamicTrial_";
42 // The maximum number of the autocomplete dynamic field trials (aka layers).
43 const int kMaxAutocompleteDynamicFieldTrials = 5;
45 // Field trial experiment probabilities.
47 // For HistoryURL provider cull redirects field trial, put 0% ( = 0/100 )
48 // of the users in the don't-cull-redirects experiment group.
49 // TODO(mpearson): Remove this field trial and the code it uses once I'm
50 // sure it's no longer needed.
51 const base::FieldTrial::Probability kHUPCullRedirectsFieldTrialDivisor = 100;
52 const base::FieldTrial::Probability
53 kHUPCullRedirectsFieldTrialExperimentFraction = 0;
55 // For HistoryURL provider create shorter match field trial, put 0%
56 // ( = 25/100 ) of the users in the don't-create-a-shorter-match
57 // experiment group.
58 // TODO(mpearson): Remove this field trial and the code it uses once I'm
59 // sure it's no longer needed.
60 const base::FieldTrial::Probability
61 kHUPCreateShorterMatchFieldTrialDivisor = 100;
62 const base::FieldTrial::Probability
63 kHUPCreateShorterMatchFieldTrialExperimentFraction = 0;
65 // Field trial IDs.
66 // Though they are not literally "const", they are set only once, in
67 // ActivateStaticTrials() below.
69 // Whether the static field trials have been initialized by
70 // ActivateStaticTrials() method.
71 bool static_field_trials_initialized = false;
73 // Field trial ID for the HistoryURL provider cull redirects experiment group.
74 int hup_dont_cull_redirects_experiment_group = 0;
76 // Field trial ID for the HistoryURL provider create shorter match
77 // experiment group.
78 int hup_dont_create_shorter_match_experiment_group = 0;
81 // Concatenates the autocomplete dynamic field trial prefix with a field trial
82 // ID to form a complete autocomplete field trial name.
83 std::string DynamicFieldTrialName(int id) {
84 return base::StringPrintf("%s%d", kAutocompleteDynamicFieldTrialPrefix, id);
87 void InitializeScoreBuckets(const VariationParams& params,
88 const char* relevance_cap_param,
89 const char* half_life_param,
90 const char* score_buckets_param,
91 ScoreBuckets* score_buckets) {
92 VariationParams::const_iterator it = params.find(relevance_cap_param);
93 if (it != params.end()) {
94 int relevance_cap;
95 if (base::StringToInt(it->second, &relevance_cap))
96 score_buckets->set_relevance_cap(relevance_cap);
99 it = params.find(half_life_param);
100 if (it != params.end()) {
101 int half_life_days;
102 if (base::StringToInt(it->second, &half_life_days))
103 score_buckets->set_half_life_days(half_life_days);
106 it = params.find(score_buckets_param);
107 if (it != params.end()) {
108 // The value of the score bucket is a comma-separated list of
109 // {DecayedCount + ":" + MaxRelevance}.
110 base::StringPairs kv_pairs;
111 if (base::SplitStringIntoKeyValuePairs(it->second, ':', ',', &kv_pairs)) {
112 for (base::StringPairs::const_iterator it = kv_pairs.begin();
113 it != kv_pairs.end(); ++it) {
114 ScoreBuckets::CountMaxRelevance bucket;
115 base::StringToDouble(it->first, &bucket.first);
116 base::StringToInt(it->second, &bucket.second);
117 score_buckets->buckets().push_back(bucket);
119 std::sort(score_buckets->buckets().begin(),
120 score_buckets->buckets().end(),
121 std::greater<ScoreBuckets::CountMaxRelevance>());
126 } // namespace
128 HUPScoringParams::ScoreBuckets::ScoreBuckets()
129 : relevance_cap_(-1),
130 half_life_days_(-1) {
133 HUPScoringParams::ScoreBuckets::~ScoreBuckets() {
136 double HUPScoringParams::ScoreBuckets::HalfLifeTimeDecay(
137 const base::TimeDelta& elapsed_time) const {
138 double time_ms;
139 if ((half_life_days_ <= 0) ||
140 ((time_ms = elapsed_time.InMillisecondsF()) <= 0))
141 return 1.0;
143 const double half_life_intervals =
144 time_ms / base::TimeDelta::FromDays(half_life_days_).InMillisecondsF();
145 return pow(2.0, -half_life_intervals);
148 void OmniboxFieldTrial::ActivateStaticTrials() {
149 DCHECK(!static_field_trials_initialized);
151 // Create the HistoryURL provider cull redirects field trial.
152 // Make it expire on March 1, 2013.
153 scoped_refptr<base::FieldTrial> trial(
154 base::FieldTrialList::FactoryGetFieldTrial(
155 kHUPCullRedirectsFieldTrialName, kHUPCullRedirectsFieldTrialDivisor,
156 "Standard", 2013, 3, 1, base::FieldTrial::ONE_TIME_RANDOMIZED, NULL));
157 hup_dont_cull_redirects_experiment_group =
158 trial->AppendGroup("DontCullRedirects",
159 kHUPCullRedirectsFieldTrialExperimentFraction);
161 // Create the HistoryURL provider create shorter match field trial.
162 // Make it expire on March 1, 2013.
163 trial = base::FieldTrialList::FactoryGetFieldTrial(
164 kHUPCreateShorterMatchFieldTrialName,
165 kHUPCreateShorterMatchFieldTrialDivisor, "Standard", 2013, 3, 1,
166 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL);
167 hup_dont_create_shorter_match_experiment_group =
168 trial->AppendGroup("DontCreateShorterMatch",
169 kHUPCreateShorterMatchFieldTrialExperimentFraction);
171 static_field_trials_initialized = true;
174 void OmniboxFieldTrial::ActivateDynamicTrials() {
175 // Initialize all autocomplete dynamic field trials. This method may be
176 // called multiple times.
177 for (int i = 0; i < kMaxAutocompleteDynamicFieldTrials; ++i)
178 base::FieldTrialList::FindValue(DynamicFieldTrialName(i));
181 int OmniboxFieldTrial::GetDisabledProviderTypes() {
182 // Make sure that Autocomplete dynamic field trials are activated. It's OK to
183 // call this method multiple times.
184 ActivateDynamicTrials();
186 // Look for group names in form of "DisabledProviders_<mask>" where "mask"
187 // is a bitmap of disabled provider types (AutocompleteProvider::Type).
188 int provider_types = 0;
189 for (int i = 0; i < kMaxAutocompleteDynamicFieldTrials; ++i) {
190 std::string group_name = base::FieldTrialList::FindFullName(
191 DynamicFieldTrialName(i));
192 const char kDisabledProviders[] = "DisabledProviders_";
193 if (!StartsWithASCII(group_name, kDisabledProviders, true))
194 continue;
195 int types = 0;
196 if (!base::StringToInt(base::StringPiece(
197 group_name.substr(strlen(kDisabledProviders))), &types))
198 continue;
199 provider_types |= types;
201 return provider_types;
204 void OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(
205 std::vector<uint32>* field_trial_hashes) {
206 field_trial_hashes->clear();
207 for (int i = 0; i < kMaxAutocompleteDynamicFieldTrials; ++i) {
208 const std::string& trial_name = DynamicFieldTrialName(i);
209 if (base::FieldTrialList::TrialExists(trial_name))
210 field_trial_hashes->push_back(metrics::HashName(trial_name));
214 bool OmniboxFieldTrial::InHUPCullRedirectsFieldTrial() {
215 return base::FieldTrialList::TrialExists(kHUPCullRedirectsFieldTrialName);
218 bool OmniboxFieldTrial::InHUPCullRedirectsFieldTrialExperimentGroup() {
219 if (!base::FieldTrialList::TrialExists(kHUPCullRedirectsFieldTrialName))
220 return false;
222 // Return true if we're in the experiment group.
223 const int group = base::FieldTrialList::FindValue(
224 kHUPCullRedirectsFieldTrialName);
225 return group == hup_dont_cull_redirects_experiment_group;
228 bool OmniboxFieldTrial::InHUPCreateShorterMatchFieldTrial() {
229 return
230 base::FieldTrialList::TrialExists(kHUPCreateShorterMatchFieldTrialName);
233 bool OmniboxFieldTrial::InHUPCreateShorterMatchFieldTrialExperimentGroup() {
234 if (!base::FieldTrialList::TrialExists(kHUPCreateShorterMatchFieldTrialName))
235 return false;
237 // Return true if we're in the experiment group.
238 const int group = base::FieldTrialList::FindValue(
239 kHUPCreateShorterMatchFieldTrialName);
240 return group == hup_dont_create_shorter_match_experiment_group;
243 base::TimeDelta OmniboxFieldTrial::StopTimerFieldTrialDuration() {
244 int stop_timer_ms;
245 if (base::StringToInt(
246 base::FieldTrialList::FindFullName(kStopTimerFieldTrialName),
247 &stop_timer_ms))
248 return base::TimeDelta::FromMilliseconds(stop_timer_ms);
249 return base::TimeDelta::FromMilliseconds(1500);
252 bool OmniboxFieldTrial::HasDynamicFieldTrialGroupPrefix(
253 const char* group_prefix) {
254 // Make sure that Autocomplete dynamic field trials are activated. It's OK to
255 // call this method multiple times.
256 ActivateDynamicTrials();
258 // Look for group names starting with |group_prefix|.
259 for (int i = 0; i < kMaxAutocompleteDynamicFieldTrials; ++i) {
260 const std::string& group_name = base::FieldTrialList::FindFullName(
261 DynamicFieldTrialName(i));
262 if (StartsWithASCII(group_name, group_prefix, true))
263 return true;
265 return false;
268 bool OmniboxFieldTrial::InZeroSuggestFieldTrial() {
269 return HasDynamicFieldTrialGroupPrefix(kEnableZeroSuggestGroupPrefix);
272 bool OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() {
273 return HasDynamicFieldTrialGroupPrefix(
274 kEnableZeroSuggestMostVisitedGroupPrefix);
277 bool OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial() {
278 return HasDynamicFieldTrialGroupPrefix(
279 kEnableZeroSuggestAfterTypingGroupPrefix);
282 bool OmniboxFieldTrial::ShortcutsScoringMaxRelevance(
283 AutocompleteInput::PageClassification current_page_classification,
284 int* max_relevance) {
285 // The value of the rule is a string that encodes an integer containing
286 // the max relevance.
287 const std::string& max_relevance_str =
288 OmniboxFieldTrial::GetValueForRuleInContext(
289 kShortcutsScoringMaxRelevanceRule, current_page_classification);
290 if (max_relevance_str.empty())
291 return false;
292 if (!base::StringToInt(max_relevance_str, max_relevance))
293 return false;
294 return true;
297 bool OmniboxFieldTrial::SearchHistoryPreventInlining(
298 AutocompleteInput::PageClassification current_page_classification) {
299 return OmniboxFieldTrial::GetValueForRuleInContext(
300 kSearchHistoryRule, current_page_classification) == "PreventInlining";
303 bool OmniboxFieldTrial::SearchHistoryDisable(
304 AutocompleteInput::PageClassification current_page_classification) {
305 return OmniboxFieldTrial::GetValueForRuleInContext(
306 kSearchHistoryRule, current_page_classification) == "Disable";
309 void OmniboxFieldTrial::GetDemotionsByType(
310 AutocompleteInput::PageClassification current_page_classification,
311 DemotionMultipliers* demotions_by_type) {
312 demotions_by_type->clear();
313 const std::string demotion_rule =
314 OmniboxFieldTrial::GetValueForRuleInContext(
315 kDemoteByTypeRule,
316 current_page_classification);
317 // The value of the DemoteByType rule is a comma-separated list of
318 // {ResultType + ":" + Number} where ResultType is an AutocompleteMatchType::
319 // Type enum represented as an integer and Number is an integer number
320 // between 0 and 100 inclusive. Relevance scores of matches of that result
321 // type are multiplied by Number / 100. 100 means no change.
322 base::StringPairs kv_pairs;
323 if (base::SplitStringIntoKeyValuePairs(demotion_rule, ':', ',', &kv_pairs)) {
324 for (base::StringPairs::const_iterator it = kv_pairs.begin();
325 it != kv_pairs.end(); ++it) {
326 // This is a best-effort conversion; we trust the hand-crafted parameters
327 // downloaded from the server to be perfect. There's no need to handle
328 // errors smartly.
329 int k, v;
330 base::StringToInt(it->first, &k);
331 base::StringToInt(it->second, &v);
332 (*demotions_by_type)[static_cast<AutocompleteMatchType::Type>(k)] =
333 static_cast<float>(v) / 100.0f;
338 OmniboxFieldTrial::UndemotableTopMatchTypes
339 OmniboxFieldTrial::GetUndemotableTopTypes(
340 AutocompleteInput::PageClassification current_page_classification) {
341 UndemotableTopMatchTypes undemotable_types;
342 const std::string types_rule =
343 OmniboxFieldTrial::GetValueForRuleInContext(
344 kUndemotableTopTypeRule,
345 current_page_classification);
346 // The value of the UndemotableTopTypes rule is a comma-separated list of
347 // AutocompleteMatchType::Type enums represented as an integer. The
348 // DemoteByType rule does not apply to the top match if the type of the top
349 // match is in this list.
350 std::vector<std::string> types;
351 base::SplitString(types_rule, ',', &types);
352 for (std::vector<std::string>::const_iterator it = types.begin();
353 it != types.end(); ++it) {
354 // This is a best-effort conversion; we trust the hand-crafted parameters
355 // downloaded from the server to be perfect. There's no need to handle
356 // errors smartly.
357 int t;
358 base::StringToInt(*it, &t);
359 undemotable_types.insert(static_cast<AutocompleteMatchType::Type>(t));
361 return undemotable_types;
364 bool OmniboxFieldTrial::ReorderForLegalDefaultMatch(
365 AutocompleteInput::PageClassification current_page_classification) {
366 return OmniboxFieldTrial::GetValueForRuleInContext(
367 kReorderForLegalDefaultMatchRule, current_page_classification) ==
368 kReorderForLegalDefaultMatchRuleEnabled;
371 void OmniboxFieldTrial::GetExperimentalHUPScoringParams(
372 HUPScoringParams* scoring_params) {
373 scoring_params->experimental_scoring_enabled = false;
375 VariationParams params;
376 if (!chrome_variations::GetVariationParams(kBundledExperimentFieldTrialName,
377 &params))
378 return;
380 VariationParams::const_iterator it = params.find(kHUPNewScoringEnabledParam);
381 if (it != params.end()) {
382 int enabled = 0;
383 if (base::StringToInt(it->second, &enabled))
384 scoring_params->experimental_scoring_enabled = (enabled != 0);
387 InitializeScoreBuckets(params, kHUPNewScoringTypedCountRelevanceCapParam,
388 kHUPNewScoringTypedCountHalfLifeTimeParam,
389 kHUPNewScoringTypedCountScoreBucketsParam,
390 &scoring_params->typed_count_buckets);
391 InitializeScoreBuckets(params, kHUPNewScoringVisitedCountRelevanceCapParam,
392 kHUPNewScoringVisitedCountHalfLifeTimeParam,
393 kHUPNewScoringVisitedCountScoreBucketsParam,
394 &scoring_params->visited_count_buckets);
397 int OmniboxFieldTrial::HQPBookmarkValue() {
398 std::string bookmark_value_str = chrome_variations::
399 GetVariationParamValue(kBundledExperimentFieldTrialName,
400 kHQPBookmarkValueRule);
401 if (bookmark_value_str.empty())
402 return 1;
403 // This is a best-effort conversion; we trust the hand-crafted parameters
404 // downloaded from the server to be perfect. There's no need for handle
405 // errors smartly.
406 int bookmark_value;
407 base::StringToInt(bookmark_value_str, &bookmark_value);
408 return bookmark_value;
411 bool OmniboxFieldTrial::HQPDiscountFrecencyWhenFewVisits() {
412 return chrome_variations::GetVariationParamValue(
413 kBundledExperimentFieldTrialName,
414 kHQPDiscountFrecencyWhenFewVisitsRule) == "true";
417 bool OmniboxFieldTrial::HQPAllowMatchInTLDValue() {
418 return chrome_variations::GetVariationParamValue(
419 kBundledExperimentFieldTrialName,
420 kHQPAllowMatchInTLDRule) == "true";
423 bool OmniboxFieldTrial::HQPAllowMatchInSchemeValue() {
424 return chrome_variations::GetVariationParamValue(
425 kBundledExperimentFieldTrialName,
426 kHQPAllowMatchInSchemeRule) == "true";
429 const char OmniboxFieldTrial::kBundledExperimentFieldTrialName[] =
430 "OmniboxBundledExperimentV1";
431 const char OmniboxFieldTrial::kShortcutsScoringMaxRelevanceRule[] =
432 "ShortcutsScoringMaxRelevance";
433 const char OmniboxFieldTrial::kSearchHistoryRule[] = "SearchHistory";
434 const char OmniboxFieldTrial::kDemoteByTypeRule[] = "DemoteByType";
435 const char OmniboxFieldTrial::kUndemotableTopTypeRule[] = "UndemotableTopTypes";
436 const char OmniboxFieldTrial::kReorderForLegalDefaultMatchRule[] =
437 "ReorderForLegalDefaultMatch";
438 const char OmniboxFieldTrial::kHQPBookmarkValueRule[] =
439 "HQPBookmarkValue";
440 const char OmniboxFieldTrial::kHQPDiscountFrecencyWhenFewVisitsRule[] =
441 "HQPDiscountFrecencyWhenFewVisits";
442 const char OmniboxFieldTrial::kHQPAllowMatchInTLDRule[] = "HQPAllowMatchInTLD";
443 const char OmniboxFieldTrial::kHQPAllowMatchInSchemeRule[] =
444 "HQPAllowMatchInScheme";
445 const char OmniboxFieldTrial::kReorderForLegalDefaultMatchRuleEnabled[] =
446 "ReorderForLegalDefaultMatch";
448 const char OmniboxFieldTrial::kHUPNewScoringEnabledParam[] =
449 "HUPExperimentalScoringEnabled";
450 const char OmniboxFieldTrial::kHUPNewScoringTypedCountRelevanceCapParam[] =
451 "TypedCountRelevanceCap";
452 const char OmniboxFieldTrial::kHUPNewScoringTypedCountHalfLifeTimeParam[] =
453 "TypedCountHalfLifeTime";
454 const char OmniboxFieldTrial::kHUPNewScoringTypedCountScoreBucketsParam[] =
455 "TypedCountScoreBuckets";
456 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountRelevanceCapParam[] =
457 "VisitedCountRelevanceCap";
458 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountHalfLifeTimeParam[] =
459 "VisitedCountHalfLifeTime";
460 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountScoreBucketsParam[] =
461 "VisitedCountScoreBuckets";
463 // Background and implementation details:
465 // Each experiment group in any field trial can come with an optional set of
466 // parameters (key-value pairs). In the bundled omnibox experiment
467 // (kBundledExperimentFieldTrialName), each experiment group comes with a
468 // list of parameters in the form:
469 // key=<Rule>:
470 // <AutocompleteInput::PageClassification (as an int)>:
471 // <whether Instant Extended is enabled (as a 1 or 0)>
472 // (note that there are no linebreaks in keys; this format is for
473 // presentation only>
474 // value=<arbitrary string>
475 // Both the AutocompleteInput::PageClassification and the Instant Extended
476 // entries can be "*", which means this rule applies for all values of the
477 // matching portion of the context.
478 // One example parameter is
479 // key=SearchHistory:6:1
480 // value=PreventInlining
481 // This means in page classification context 6 (a search result page doing
482 // search term replacement) with Instant Extended enabled, the SearchHistory
483 // experiment should PreventInlining.
485 // When an exact match to the rule in the current context is missing, we
486 // give preference to a wildcard rule that matches the instant extended
487 // context over a wildcard rule that matches the page classification
488 // context. Hopefully, though, users will write their field trial configs
489 // so as not to rely on this fall back order.
491 // In short, this function tries to find the value associated with key
492 // |rule|:|page_classification|:|instant_extended|, failing that it looks up
493 // |rule|:*:|instant_extended|, failing that it looks up
494 // |rule|:|page_classification|:*, failing that it looks up |rule|:*:*,
495 // and failing that it returns the empty string.
496 std::string OmniboxFieldTrial::GetValueForRuleInContext(
497 const std::string& rule,
498 AutocompleteInput::PageClassification page_classification) {
499 VariationParams params;
500 if (!chrome_variations::GetVariationParams(kBundledExperimentFieldTrialName,
501 &params)) {
502 return std::string();
504 const std::string page_classification_str =
505 base::IntToString(static_cast<int>(page_classification));
506 const std::string instant_extended =
507 chrome::IsInstantExtendedAPIEnabled() ? "1" : "0";
508 // Look up rule in this exact context.
509 VariationParams::const_iterator it = params.find(
510 rule + ":" + page_classification_str + ":" + instant_extended);
511 if (it != params.end())
512 return it->second;
513 // Fall back to the global page classification context.
514 it = params.find(rule + ":*:" + instant_extended);
515 if (it != params.end())
516 return it->second;
517 // Fall back to the global instant extended context.
518 it = params.find(rule + ":" + page_classification_str + ":*");
519 if (it != params.end())
520 return it->second;
521 // Look up rule in the global context.
522 it = params.find(rule + ":*:*");
523 return (it != params.end()) ? it->second : std::string();