NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / omnibox / omnibox_field_trial.cc
blob26a32c07aa927140c1abee07d87473db89f8aef3
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));
212 if (base::FieldTrialList::TrialExists(kBundledExperimentFieldTrialName)) {
213 field_trial_hashes->push_back(
214 metrics::HashName(kBundledExperimentFieldTrialName));
218 bool OmniboxFieldTrial::InHUPCullRedirectsFieldTrial() {
219 return base::FieldTrialList::TrialExists(kHUPCullRedirectsFieldTrialName);
222 bool OmniboxFieldTrial::InHUPCullRedirectsFieldTrialExperimentGroup() {
223 if (!base::FieldTrialList::TrialExists(kHUPCullRedirectsFieldTrialName))
224 return false;
226 // Return true if we're in the experiment group.
227 const int group = base::FieldTrialList::FindValue(
228 kHUPCullRedirectsFieldTrialName);
229 return group == hup_dont_cull_redirects_experiment_group;
232 bool OmniboxFieldTrial::InHUPCreateShorterMatchFieldTrial() {
233 return
234 base::FieldTrialList::TrialExists(kHUPCreateShorterMatchFieldTrialName);
237 bool OmniboxFieldTrial::InHUPCreateShorterMatchFieldTrialExperimentGroup() {
238 if (!base::FieldTrialList::TrialExists(kHUPCreateShorterMatchFieldTrialName))
239 return false;
241 // Return true if we're in the experiment group.
242 const int group = base::FieldTrialList::FindValue(
243 kHUPCreateShorterMatchFieldTrialName);
244 return group == hup_dont_create_shorter_match_experiment_group;
247 base::TimeDelta OmniboxFieldTrial::StopTimerFieldTrialDuration() {
248 int stop_timer_ms;
249 if (base::StringToInt(
250 base::FieldTrialList::FindFullName(kStopTimerFieldTrialName),
251 &stop_timer_ms))
252 return base::TimeDelta::FromMilliseconds(stop_timer_ms);
253 return base::TimeDelta::FromMilliseconds(1500);
256 bool OmniboxFieldTrial::HasDynamicFieldTrialGroupPrefix(
257 const char* group_prefix) {
258 // Make sure that Autocomplete dynamic field trials are activated. It's OK to
259 // call this method multiple times.
260 ActivateDynamicTrials();
262 // Look for group names starting with |group_prefix|.
263 for (int i = 0; i < kMaxAutocompleteDynamicFieldTrials; ++i) {
264 const std::string& group_name = base::FieldTrialList::FindFullName(
265 DynamicFieldTrialName(i));
266 if (StartsWithASCII(group_name, group_prefix, true))
267 return true;
269 return false;
272 bool OmniboxFieldTrial::InZeroSuggestFieldTrial() {
273 return HasDynamicFieldTrialGroupPrefix(kEnableZeroSuggestGroupPrefix) ||
274 chrome_variations::GetVariationParamValue(
275 kBundledExperimentFieldTrialName, kZeroSuggestRule) == "true";
278 bool OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() {
279 return HasDynamicFieldTrialGroupPrefix(
280 kEnableZeroSuggestMostVisitedGroupPrefix) ||
281 chrome_variations::GetVariationParamValue(
282 kBundledExperimentFieldTrialName,
283 kZeroSuggestVariantRule) == "MostVisited";
286 bool OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial() {
287 return HasDynamicFieldTrialGroupPrefix(
288 kEnableZeroSuggestAfterTypingGroupPrefix) ||
289 chrome_variations::GetVariationParamValue(
290 kBundledExperimentFieldTrialName,
291 kZeroSuggestVariantRule) == "AfterTyping";
294 bool OmniboxFieldTrial::ShortcutsScoringMaxRelevance(
295 AutocompleteInput::PageClassification current_page_classification,
296 int* max_relevance) {
297 // The value of the rule is a string that encodes an integer containing
298 // the max relevance.
299 const std::string& max_relevance_str =
300 OmniboxFieldTrial::GetValueForRuleInContext(
301 kShortcutsScoringMaxRelevanceRule, current_page_classification);
302 if (max_relevance_str.empty())
303 return false;
304 if (!base::StringToInt(max_relevance_str, max_relevance))
305 return false;
306 return true;
309 bool OmniboxFieldTrial::SearchHistoryPreventInlining(
310 AutocompleteInput::PageClassification current_page_classification) {
311 return OmniboxFieldTrial::GetValueForRuleInContext(
312 kSearchHistoryRule, current_page_classification) == "PreventInlining";
315 bool OmniboxFieldTrial::SearchHistoryDisable(
316 AutocompleteInput::PageClassification current_page_classification) {
317 return OmniboxFieldTrial::GetValueForRuleInContext(
318 kSearchHistoryRule, current_page_classification) == "Disable";
321 void OmniboxFieldTrial::GetDemotionsByType(
322 AutocompleteInput::PageClassification current_page_classification,
323 DemotionMultipliers* demotions_by_type) {
324 demotions_by_type->clear();
325 const std::string demotion_rule =
326 OmniboxFieldTrial::GetValueForRuleInContext(
327 kDemoteByTypeRule,
328 current_page_classification);
329 // The value of the DemoteByType rule is a comma-separated list of
330 // {ResultType + ":" + Number} where ResultType is an AutocompleteMatchType::
331 // Type enum represented as an integer and Number is an integer number
332 // between 0 and 100 inclusive. Relevance scores of matches of that result
333 // type are multiplied by Number / 100. 100 means no change.
334 base::StringPairs kv_pairs;
335 if (base::SplitStringIntoKeyValuePairs(demotion_rule, ':', ',', &kv_pairs)) {
336 for (base::StringPairs::const_iterator it = kv_pairs.begin();
337 it != kv_pairs.end(); ++it) {
338 // This is a best-effort conversion; we trust the hand-crafted parameters
339 // downloaded from the server to be perfect. There's no need to handle
340 // errors smartly.
341 int k, v;
342 base::StringToInt(it->first, &k);
343 base::StringToInt(it->second, &v);
344 (*demotions_by_type)[static_cast<AutocompleteMatchType::Type>(k)] =
345 static_cast<float>(v) / 100.0f;
350 OmniboxFieldTrial::UndemotableTopMatchTypes
351 OmniboxFieldTrial::GetUndemotableTopTypes(
352 AutocompleteInput::PageClassification current_page_classification) {
353 UndemotableTopMatchTypes undemotable_types;
354 const std::string types_rule =
355 OmniboxFieldTrial::GetValueForRuleInContext(
356 kUndemotableTopTypeRule,
357 current_page_classification);
358 // The value of the UndemotableTopTypes rule is a comma-separated list of
359 // AutocompleteMatchType::Type enums represented as an integer. The
360 // DemoteByType rule does not apply to the top match if the type of the top
361 // match is in this list.
362 std::vector<std::string> types;
363 base::SplitString(types_rule, ',', &types);
364 for (std::vector<std::string>::const_iterator it = types.begin();
365 it != types.end(); ++it) {
366 // This is a best-effort conversion; we trust the hand-crafted parameters
367 // downloaded from the server to be perfect. There's no need to handle
368 // errors smartly.
369 int t;
370 base::StringToInt(*it, &t);
371 undemotable_types.insert(static_cast<AutocompleteMatchType::Type>(t));
373 return undemotable_types;
376 bool OmniboxFieldTrial::ReorderForLegalDefaultMatch(
377 AutocompleteInput::PageClassification current_page_classification) {
378 return OmniboxFieldTrial::GetValueForRuleInContext(
379 kReorderForLegalDefaultMatchRule, current_page_classification) !=
380 kReorderForLegalDefaultMatchRuleDisabled;
383 void OmniboxFieldTrial::GetExperimentalHUPScoringParams(
384 HUPScoringParams* scoring_params) {
385 scoring_params->experimental_scoring_enabled = false;
387 VariationParams params;
388 if (!chrome_variations::GetVariationParams(kBundledExperimentFieldTrialName,
389 &params))
390 return;
392 VariationParams::const_iterator it = params.find(kHUPNewScoringEnabledParam);
393 if (it != params.end()) {
394 int enabled = 0;
395 if (base::StringToInt(it->second, &enabled))
396 scoring_params->experimental_scoring_enabled = (enabled != 0);
399 InitializeScoreBuckets(params, kHUPNewScoringTypedCountRelevanceCapParam,
400 kHUPNewScoringTypedCountHalfLifeTimeParam,
401 kHUPNewScoringTypedCountScoreBucketsParam,
402 &scoring_params->typed_count_buckets);
403 InitializeScoreBuckets(params, kHUPNewScoringVisitedCountRelevanceCapParam,
404 kHUPNewScoringVisitedCountHalfLifeTimeParam,
405 kHUPNewScoringVisitedCountScoreBucketsParam,
406 &scoring_params->visited_count_buckets);
409 int OmniboxFieldTrial::HQPBookmarkValue() {
410 std::string bookmark_value_str = chrome_variations::
411 GetVariationParamValue(kBundledExperimentFieldTrialName,
412 kHQPBookmarkValueRule);
413 if (bookmark_value_str.empty())
414 return 1;
415 // This is a best-effort conversion; we trust the hand-crafted parameters
416 // downloaded from the server to be perfect. There's no need for handle
417 // errors smartly.
418 int bookmark_value;
419 base::StringToInt(bookmark_value_str, &bookmark_value);
420 return bookmark_value;
423 bool OmniboxFieldTrial::HQPDiscountFrecencyWhenFewVisits() {
424 return chrome_variations::GetVariationParamValue(
425 kBundledExperimentFieldTrialName,
426 kHQPDiscountFrecencyWhenFewVisitsRule) == "true";
429 bool OmniboxFieldTrial::HQPAllowMatchInTLDValue() {
430 return chrome_variations::GetVariationParamValue(
431 kBundledExperimentFieldTrialName,
432 kHQPAllowMatchInTLDRule) == "true";
435 bool OmniboxFieldTrial::HQPAllowMatchInSchemeValue() {
436 return chrome_variations::GetVariationParamValue(
437 kBundledExperimentFieldTrialName,
438 kHQPAllowMatchInSchemeRule) == "true";
441 const char OmniboxFieldTrial::kBundledExperimentFieldTrialName[] =
442 "OmniboxBundledExperimentV1";
443 const char OmniboxFieldTrial::kShortcutsScoringMaxRelevanceRule[] =
444 "ShortcutsScoringMaxRelevance";
445 const char OmniboxFieldTrial::kSearchHistoryRule[] = "SearchHistory";
446 const char OmniboxFieldTrial::kDemoteByTypeRule[] = "DemoteByType";
447 const char OmniboxFieldTrial::kUndemotableTopTypeRule[] = "UndemotableTopTypes";
448 const char OmniboxFieldTrial::kReorderForLegalDefaultMatchRule[] =
449 "ReorderForLegalDefaultMatch";
450 const char OmniboxFieldTrial::kHQPBookmarkValueRule[] =
451 "HQPBookmarkValue";
452 const char OmniboxFieldTrial::kHQPDiscountFrecencyWhenFewVisitsRule[] =
453 "HQPDiscountFrecencyWhenFewVisits";
454 const char OmniboxFieldTrial::kHQPAllowMatchInTLDRule[] = "HQPAllowMatchInTLD";
455 const char OmniboxFieldTrial::kHQPAllowMatchInSchemeRule[] =
456 "HQPAllowMatchInScheme";
457 const char OmniboxFieldTrial::kZeroSuggestRule[] = "ZeroSuggest";
458 const char OmniboxFieldTrial::kZeroSuggestVariantRule[] = "ZeroSuggestVariant";
459 const char OmniboxFieldTrial::kReorderForLegalDefaultMatchRuleDisabled[] =
460 "DontReorderForLegalDefaultMatch";
462 const char OmniboxFieldTrial::kHUPNewScoringEnabledParam[] =
463 "HUPExperimentalScoringEnabled";
464 const char OmniboxFieldTrial::kHUPNewScoringTypedCountRelevanceCapParam[] =
465 "TypedCountRelevanceCap";
466 const char OmniboxFieldTrial::kHUPNewScoringTypedCountHalfLifeTimeParam[] =
467 "TypedCountHalfLifeTime";
468 const char OmniboxFieldTrial::kHUPNewScoringTypedCountScoreBucketsParam[] =
469 "TypedCountScoreBuckets";
470 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountRelevanceCapParam[] =
471 "VisitedCountRelevanceCap";
472 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountHalfLifeTimeParam[] =
473 "VisitedCountHalfLifeTime";
474 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountScoreBucketsParam[] =
475 "VisitedCountScoreBuckets";
477 // Background and implementation details:
479 // Each experiment group in any field trial can come with an optional set of
480 // parameters (key-value pairs). In the bundled omnibox experiment
481 // (kBundledExperimentFieldTrialName), each experiment group comes with a
482 // list of parameters in the form:
483 // key=<Rule>:
484 // <AutocompleteInput::PageClassification (as an int)>:
485 // <whether Instant Extended is enabled (as a 1 or 0)>
486 // (note that there are no linebreaks in keys; this format is for
487 // presentation only>
488 // value=<arbitrary string>
489 // Both the AutocompleteInput::PageClassification and the Instant Extended
490 // entries can be "*", which means this rule applies for all values of the
491 // matching portion of the context.
492 // One example parameter is
493 // key=SearchHistory:6:1
494 // value=PreventInlining
495 // This means in page classification context 6 (a search result page doing
496 // search term replacement) with Instant Extended enabled, the SearchHistory
497 // experiment should PreventInlining.
499 // When an exact match to the rule in the current context is missing, we
500 // give preference to a wildcard rule that matches the instant extended
501 // context over a wildcard rule that matches the page classification
502 // context. Hopefully, though, users will write their field trial configs
503 // so as not to rely on this fall back order.
505 // In short, this function tries to find the value associated with key
506 // |rule|:|page_classification|:|instant_extended|, failing that it looks up
507 // |rule|:*:|instant_extended|, failing that it looks up
508 // |rule|:|page_classification|:*, failing that it looks up |rule|:*:*,
509 // and failing that it returns the empty string.
510 std::string OmniboxFieldTrial::GetValueForRuleInContext(
511 const std::string& rule,
512 AutocompleteInput::PageClassification page_classification) {
513 VariationParams params;
514 if (!chrome_variations::GetVariationParams(kBundledExperimentFieldTrialName,
515 &params)) {
516 return std::string();
518 const std::string page_classification_str =
519 base::IntToString(static_cast<int>(page_classification));
520 const std::string instant_extended =
521 chrome::IsInstantExtendedAPIEnabled() ? "1" : "0";
522 // Look up rule in this exact context.
523 VariationParams::const_iterator it = params.find(
524 rule + ":" + page_classification_str + ":" + instant_extended);
525 if (it != params.end())
526 return it->second;
527 // Fall back to the global page classification context.
528 it = params.find(rule + ":*:" + instant_extended);
529 if (it != params.end())
530 return it->second;
531 // Fall back to the global instant extended context.
532 it = params.find(rule + ":" + page_classification_str + ":*");
533 if (it != params.end())
534 return it->second;
535 // Look up rule in the global context.
536 it = params.find(rule + ":*:*");
537 return (it != params.end()) ? it->second : std::string();