Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / components / omnibox / browser / omnibox_field_trial.cc
blob75527959f1ab1eecb51c8ce7293ebc8eeed92dac
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 #include "components/omnibox/browser/omnibox_field_trial.h"
7 #include <cmath>
8 #include <string>
10 #include "base/command_line.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/time/time.h"
17 #include "components/metrics/proto/omnibox_event.pb.h"
18 #include "components/omnibox/browser/omnibox_switches.h"
19 #include "components/search/search.h"
20 #include "components/variations/active_field_trials.h"
21 #include "components/variations/metrics_util.h"
22 #include "components/variations/variations_associated_data.h"
24 using metrics::OmniboxEventProto;
26 namespace {
28 typedef std::map<std::string, std::string> VariationParams;
29 typedef HUPScoringParams::ScoreBuckets ScoreBuckets;
31 // Field trial names.
32 const char kStopTimerFieldTrialName[] = "OmniboxStopTimer";
34 // The autocomplete dynamic field trial name prefix. Each field trial is
35 // configured dynamically and is retrieved automatically by Chrome during
36 // the startup.
37 const char kAutocompleteDynamicFieldTrialPrefix[] = "AutocompleteDynamicTrial_";
38 // The maximum number of the autocomplete dynamic field trials (aka layers).
39 const int kMaxAutocompleteDynamicFieldTrials = 5;
42 // Concatenates the autocomplete dynamic field trial prefix with a field trial
43 // ID to form a complete autocomplete field trial name.
44 std::string DynamicFieldTrialName(int id) {
45 return base::StringPrintf("%s%d", kAutocompleteDynamicFieldTrialPrefix, id);
48 void InitializeScoreBuckets(const VariationParams& params,
49 const char* relevance_cap_param,
50 const char* half_life_param,
51 const char* score_buckets_param,
52 ScoreBuckets* score_buckets) {
53 VariationParams::const_iterator it = params.find(relevance_cap_param);
54 if (it != params.end()) {
55 int relevance_cap;
56 if (base::StringToInt(it->second, &relevance_cap))
57 score_buckets->set_relevance_cap(relevance_cap);
60 it = params.find(half_life_param);
61 if (it != params.end()) {
62 int half_life_days;
63 if (base::StringToInt(it->second, &half_life_days))
64 score_buckets->set_half_life_days(half_life_days);
67 it = params.find(score_buckets_param);
68 if (it != params.end()) {
69 // The value of the score bucket is a comma-separated list of
70 // {DecayedCount + ":" + MaxRelevance}.
71 base::StringPairs kv_pairs;
72 if (base::SplitStringIntoKeyValuePairs(it->second, ':', ',', &kv_pairs)) {
73 for (base::StringPairs::const_iterator it = kv_pairs.begin();
74 it != kv_pairs.end(); ++it) {
75 ScoreBuckets::CountMaxRelevance bucket;
76 base::StringToDouble(it->first, &bucket.first);
77 base::StringToInt(it->second, &bucket.second);
78 score_buckets->buckets().push_back(bucket);
80 std::sort(score_buckets->buckets().begin(),
81 score_buckets->buckets().end(),
82 std::greater<ScoreBuckets::CountMaxRelevance>());
87 } // namespace
89 HUPScoringParams::ScoreBuckets::ScoreBuckets()
90 : relevance_cap_(-1),
91 half_life_days_(-1) {
94 HUPScoringParams::ScoreBuckets::~ScoreBuckets() {
97 double HUPScoringParams::ScoreBuckets::HalfLifeTimeDecay(
98 const base::TimeDelta& elapsed_time) const {
99 double time_ms;
100 if ((half_life_days_ <= 0) ||
101 ((time_ms = elapsed_time.InMillisecondsF()) <= 0))
102 return 1.0;
104 const double half_life_intervals =
105 time_ms / base::TimeDelta::FromDays(half_life_days_).InMillisecondsF();
106 return pow(2.0, -half_life_intervals);
109 void OmniboxFieldTrial::ActivateDynamicTrials() {
110 // Initialize all autocomplete dynamic field trials. This method may be
111 // called multiple times.
112 for (int i = 0; i < kMaxAutocompleteDynamicFieldTrials; ++i)
113 base::FieldTrialList::FindValue(DynamicFieldTrialName(i));
116 int OmniboxFieldTrial::GetDisabledProviderTypes() {
117 const std::string& types_string = variations::GetVariationParamValue(
118 kBundledExperimentFieldTrialName,
119 kDisableProvidersRule);
120 int types = 0;
121 if (types_string.empty() || !base::StringToInt(types_string, &types)) {
122 return 0;
124 return types;
127 void OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(
128 std::vector<uint32>* field_trial_hashes) {
129 field_trial_hashes->clear();
130 for (int i = 0; i < kMaxAutocompleteDynamicFieldTrials; ++i) {
131 const std::string& trial_name = DynamicFieldTrialName(i);
132 if (base::FieldTrialList::TrialExists(trial_name))
133 field_trial_hashes->push_back(metrics::HashName(trial_name));
135 if (base::FieldTrialList::TrialExists(kBundledExperimentFieldTrialName)) {
136 field_trial_hashes->push_back(
137 metrics::HashName(kBundledExperimentFieldTrialName));
141 base::TimeDelta OmniboxFieldTrial::StopTimerFieldTrialDuration() {
142 int stop_timer_ms;
143 if (base::StringToInt(
144 base::FieldTrialList::FindFullName(kStopTimerFieldTrialName),
145 &stop_timer_ms))
146 return base::TimeDelta::FromMilliseconds(stop_timer_ms);
147 return base::TimeDelta::FromMilliseconds(1500);
150 bool OmniboxFieldTrial::InZeroSuggestFieldTrial() {
151 if (variations::GetVariationParamValue(
152 kBundledExperimentFieldTrialName, kZeroSuggestRule) == "true")
153 return true;
154 if (variations::GetVariationParamValue(
155 kBundledExperimentFieldTrialName, kZeroSuggestRule) == "false")
156 return false;
157 #if defined(OS_IOS)
158 return false;
159 #else
160 return true;
161 #endif
164 bool OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() {
165 return InZeroSuggestMostVisitedWithoutSerpFieldTrial() ||
166 variations::GetVariationParamValue(
167 kBundledExperimentFieldTrialName,
168 kZeroSuggestVariantRule) == "MostVisited";
171 bool OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial() {
172 std::string variant(variations::GetVariationParamValue(
173 kBundledExperimentFieldTrialName,
174 kZeroSuggestVariantRule));
175 if (variant == "MostVisitedWithoutSERP")
176 return true;
177 #if defined(OS_ANDROID)
178 // Android defaults to MostVisitedWithoutSERP
179 return variant.empty();
180 #else
181 return false;
182 #endif
185 bool OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial() {
186 if (variations::GetVariationParamValue(
187 kBundledExperimentFieldTrialName,
188 kSuggestVariantRule) == "AfterTyping")
189 return true;
190 #if defined(OS_IOS) || defined(OS_ANDROID)
191 return false;
192 #else
193 return true;
194 #endif
197 bool OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() {
198 return variations::GetVariationParamValue(
199 kBundledExperimentFieldTrialName,
200 kZeroSuggestVariantRule) == "Personalized";
203 bool OmniboxFieldTrial::ShortcutsScoringMaxRelevance(
204 OmniboxEventProto::PageClassification current_page_classification,
205 int* max_relevance) {
206 // The value of the rule is a string that encodes an integer containing
207 // the max relevance.
208 const std::string& max_relevance_str =
209 OmniboxFieldTrial::GetValueForRuleInContext(
210 kShortcutsScoringMaxRelevanceRule, current_page_classification);
211 if (max_relevance_str.empty())
212 return false;
213 if (!base::StringToInt(max_relevance_str, max_relevance))
214 return false;
215 return true;
218 bool OmniboxFieldTrial::SearchHistoryPreventInlining(
219 OmniboxEventProto::PageClassification current_page_classification) {
220 return OmniboxFieldTrial::GetValueForRuleInContext(
221 kSearchHistoryRule, current_page_classification) == "PreventInlining";
224 bool OmniboxFieldTrial::SearchHistoryDisable(
225 OmniboxEventProto::PageClassification current_page_classification) {
226 return OmniboxFieldTrial::GetValueForRuleInContext(
227 kSearchHistoryRule, current_page_classification) == "Disable";
230 void OmniboxFieldTrial::GetDemotionsByType(
231 OmniboxEventProto::PageClassification current_page_classification,
232 DemotionMultipliers* demotions_by_type) {
233 demotions_by_type->clear();
234 std::string demotion_rule = OmniboxFieldTrial::GetValueForRuleInContext(
235 kDemoteByTypeRule, current_page_classification);
236 // If there is no demotion rule for this context, then use the default
237 // value for that context. At the moment the default value is non-empty
238 // only for the fakebox-focus context.
239 if (demotion_rule.empty() &&
240 (current_page_classification ==
241 OmniboxEventProto::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS))
242 demotion_rule = "1:61,2:61,3:61,4:61,16:61";
244 // The value of the DemoteByType rule is a comma-separated list of
245 // {ResultType + ":" + Number} where ResultType is an AutocompleteMatchType::
246 // Type enum represented as an integer and Number is an integer number
247 // between 0 and 100 inclusive. Relevance scores of matches of that result
248 // type are multiplied by Number / 100. 100 means no change.
249 base::StringPairs kv_pairs;
250 if (base::SplitStringIntoKeyValuePairs(demotion_rule, ':', ',', &kv_pairs)) {
251 for (base::StringPairs::const_iterator it = kv_pairs.begin();
252 it != kv_pairs.end(); ++it) {
253 // This is a best-effort conversion; we trust the hand-crafted parameters
254 // downloaded from the server to be perfect. There's no need to handle
255 // errors smartly.
256 int k, v;
257 base::StringToInt(it->first, &k);
258 base::StringToInt(it->second, &v);
259 (*demotions_by_type)[static_cast<AutocompleteMatchType::Type>(k)] =
260 static_cast<float>(v) / 100.0f;
265 void OmniboxFieldTrial::GetExperimentalHUPScoringParams(
266 HUPScoringParams* scoring_params) {
267 scoring_params->experimental_scoring_enabled = false;
269 VariationParams params;
270 if (!variations::GetVariationParams(kBundledExperimentFieldTrialName,
271 &params))
272 return;
274 VariationParams::const_iterator it = params.find(kHUPNewScoringEnabledParam);
275 if (it != params.end()) {
276 int enabled = 0;
277 if (base::StringToInt(it->second, &enabled))
278 scoring_params->experimental_scoring_enabled = (enabled != 0);
281 InitializeScoreBuckets(params, kHUPNewScoringTypedCountRelevanceCapParam,
282 kHUPNewScoringTypedCountHalfLifeTimeParam,
283 kHUPNewScoringTypedCountScoreBucketsParam,
284 &scoring_params->typed_count_buckets);
285 InitializeScoreBuckets(params, kHUPNewScoringVisitedCountRelevanceCapParam,
286 kHUPNewScoringVisitedCountHalfLifeTimeParam,
287 kHUPNewScoringVisitedCountScoreBucketsParam,
288 &scoring_params->visited_count_buckets);
291 int OmniboxFieldTrial::HQPBookmarkValue() {
292 std::string bookmark_value_str =
293 variations::GetVariationParamValue(kBundledExperimentFieldTrialName,
294 kHQPBookmarkValueRule);
295 if (bookmark_value_str.empty())
296 return 10;
297 // This is a best-effort conversion; we trust the hand-crafted parameters
298 // downloaded from the server to be perfect. There's no need for handle
299 // errors smartly.
300 int bookmark_value;
301 base::StringToInt(bookmark_value_str, &bookmark_value);
302 return bookmark_value;
305 bool OmniboxFieldTrial::HQPAllowMatchInTLDValue() {
306 return variations::GetVariationParamValue(
307 kBundledExperimentFieldTrialName,
308 kHQPAllowMatchInTLDRule) == "true";
311 bool OmniboxFieldTrial::HQPAllowMatchInSchemeValue() {
312 return variations::GetVariationParamValue(
313 kBundledExperimentFieldTrialName,
314 kHQPAllowMatchInSchemeRule) == "true";
317 bool OmniboxFieldTrial::DisableResultsCaching() {
318 return variations::GetVariationParamValue(
319 kBundledExperimentFieldTrialName,
320 kDisableResultsCachingRule) == "true";
323 void OmniboxFieldTrial::GetSuggestPollingStrategy(bool* from_last_keystroke,
324 int* polling_delay_ms) {
325 *from_last_keystroke = variations::GetVariationParamValue(
326 kBundledExperimentFieldTrialName,
327 kMeasureSuggestPollingDelayFromLastKeystrokeRule) == "true";
329 const std::string& polling_delay_string = variations::GetVariationParamValue(
330 kBundledExperimentFieldTrialName,
331 kSuggestPollingDelayMsRule);
332 if (polling_delay_string.empty() ||
333 !base::StringToInt(polling_delay_string, polling_delay_ms) ||
334 (*polling_delay_ms <= 0)) {
335 *polling_delay_ms = kDefaultMinimumTimeBetweenSuggestQueriesMs;
339 bool OmniboxFieldTrial::HQPExperimentalScoringEnabled() {
340 return variations::GetVariationParamValue(
341 kBundledExperimentFieldTrialName,
342 kHQPExperimentalScoringEnabledParam) == "true";
345 std::string OmniboxFieldTrial::HQPExperimentalScoringBuckets() {
346 if (!HQPExperimentalScoringEnabled())
347 return "";
349 return variations::GetVariationParamValue(
350 kBundledExperimentFieldTrialName,
351 kHQPExperimentalScoringBucketsParam);
354 float OmniboxFieldTrial::HQPExperimentalTopicalityThreshold() {
355 if (!HQPExperimentalScoringEnabled())
356 return -1;
358 std::string topicality_threhold_str =
359 variations::GetVariationParamValue(
360 kBundledExperimentFieldTrialName,
361 kHQPExperimentalScoringTopicalityThresholdParam);
363 double topicality_threshold;
364 if (!base::StringToDouble(topicality_threhold_str, &topicality_threshold))
365 return -1;
367 return static_cast<float>(topicality_threshold);
370 bool OmniboxFieldTrial::HQPFixFrequencyScoringBugs() {
371 return variations::GetVariationParamValue(
372 kBundledExperimentFieldTrialName,
373 kHQPFixFrequencyScoringBugsRule) == "true";
376 size_t OmniboxFieldTrial::HQPNumTitleWordsToAllow() {
377 // The value of the rule is a string that encodes an integer (actually
378 // size_t) containing the number of words.
379 size_t num_title_words;
380 if (!base::StringToSizeT(
381 variations::GetVariationParamValue(kBundledExperimentFieldTrialName,
382 kHQPNumTitleWordsRule),
383 &num_title_words))
384 return 10;
385 return num_title_words;
388 bool OmniboxFieldTrial::HQPAlsoDoHUPLikeScoring() {
389 return variations::GetVariationParamValue(
390 kBundledExperimentFieldTrialName,
391 kHQPAlsoDoHUPLikeScoringRule) == "true";
394 bool OmniboxFieldTrial::PreventUWYTDefaultForNonURLInputs() {
395 return variations::GetVariationParamValue(
396 kBundledExperimentFieldTrialName,
397 kPreventUWYTDefaultForNonURLInputsRule) == "true";
400 const char OmniboxFieldTrial::kBundledExperimentFieldTrialName[] =
401 "OmniboxBundledExperimentV1";
402 const char OmniboxFieldTrial::kDisableProvidersRule[] = "DisableProviders";
403 const char OmniboxFieldTrial::kShortcutsScoringMaxRelevanceRule[] =
404 "ShortcutsScoringMaxRelevance";
405 const char OmniboxFieldTrial::kSearchHistoryRule[] = "SearchHistory";
406 const char OmniboxFieldTrial::kDemoteByTypeRule[] = "DemoteByType";
407 const char OmniboxFieldTrial::kHQPBookmarkValueRule[] =
408 "HQPBookmarkValue";
409 const char OmniboxFieldTrial::kHQPAllowMatchInTLDRule[] = "HQPAllowMatchInTLD";
410 const char OmniboxFieldTrial::kHQPAllowMatchInSchemeRule[] =
411 "HQPAllowMatchInScheme";
412 const char OmniboxFieldTrial::kZeroSuggestRule[] = "ZeroSuggest";
413 const char OmniboxFieldTrial::kZeroSuggestVariantRule[] = "ZeroSuggestVariant";
414 const char OmniboxFieldTrial::kSuggestVariantRule[] = "SuggestVariant";
415 const char OmniboxFieldTrial::kDisableResultsCachingRule[] =
416 "DisableResultsCaching";
417 const char
418 OmniboxFieldTrial::kMeasureSuggestPollingDelayFromLastKeystrokeRule[] =
419 "MeasureSuggestPollingDelayFromLastKeystroke";
420 const char OmniboxFieldTrial::kSuggestPollingDelayMsRule[] =
421 "SuggestPollingDelayMs";
422 const char OmniboxFieldTrial::kHQPFixFrequencyScoringBugsRule[] =
423 "HQPFixFrequencyScoringBugs";
424 const char OmniboxFieldTrial::kHQPNumTitleWordsRule[] = "HQPNumTitleWords";
425 const char OmniboxFieldTrial::kHQPAlsoDoHUPLikeScoringRule[] =
426 "HQPAlsoDoHUPLikeScoring";
427 const char OmniboxFieldTrial::kPreventUWYTDefaultForNonURLInputsRule[] =
428 "PreventUWYTDefaultForNonURLInputs";
430 const char OmniboxFieldTrial::kHUPNewScoringEnabledParam[] =
431 "HUPExperimentalScoringEnabled";
432 const char OmniboxFieldTrial::kHUPNewScoringTypedCountRelevanceCapParam[] =
433 "TypedCountRelevanceCap";
434 const char OmniboxFieldTrial::kHUPNewScoringTypedCountHalfLifeTimeParam[] =
435 "TypedCountHalfLifeTime";
436 const char OmniboxFieldTrial::kHUPNewScoringTypedCountScoreBucketsParam[] =
437 "TypedCountScoreBuckets";
438 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountRelevanceCapParam[] =
439 "VisitedCountRelevanceCap";
440 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountHalfLifeTimeParam[] =
441 "VisitedCountHalfLifeTime";
442 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountScoreBucketsParam[] =
443 "VisitedCountScoreBuckets";
445 const char OmniboxFieldTrial::kHQPExperimentalScoringEnabledParam[] =
446 "HQPExperimentalScoringEnabled";
447 const char OmniboxFieldTrial::kHQPExperimentalScoringBucketsParam[] =
448 "HQPExperimentalScoringBuckets";
449 const char
450 OmniboxFieldTrial::kHQPExperimentalScoringTopicalityThresholdParam[] =
451 "HQPExperimentalScoringTopicalityThreshold";
453 // static
454 int OmniboxFieldTrial::kDefaultMinimumTimeBetweenSuggestQueriesMs = 100;
456 // Background and implementation details:
458 // Each experiment group in any field trial can come with an optional set of
459 // parameters (key-value pairs). In the bundled omnibox experiment
460 // (kBundledExperimentFieldTrialName), each experiment group comes with a
461 // list of parameters in the form:
462 // key=<Rule>:
463 // <OmniboxEventProto::PageClassification (as an int)>:
464 // <whether Instant Extended is enabled (as a 1 or 0)>
465 // (note that there are no linebreaks in keys; this format is for
466 // presentation only>
467 // value=<arbitrary string>
468 // Both the OmniboxEventProto::PageClassification and the Instant Extended
469 // entries can be "*", which means this rule applies for all values of the
470 // matching portion of the context.
471 // One example parameter is
472 // key=SearchHistory:6:1
473 // value=PreventInlining
474 // This means in page classification context 6 (a search result page doing
475 // search term replacement) with Instant Extended enabled, the SearchHistory
476 // experiment should PreventInlining.
478 // When an exact match to the rule in the current context is missing, we
479 // give preference to a wildcard rule that matches the instant extended
480 // context over a wildcard rule that matches the page classification
481 // context. Hopefully, though, users will write their field trial configs
482 // so as not to rely on this fall back order.
484 // In short, this function tries to find the value associated with key
485 // |rule|:|page_classification|:|instant_extended|, failing that it looks up
486 // |rule|:*:|instant_extended|, failing that it looks up
487 // |rule|:|page_classification|:*, failing that it looks up |rule|:*:*,
488 // and failing that it returns the empty string.
489 std::string OmniboxFieldTrial::GetValueForRuleInContext(
490 const std::string& rule,
491 OmniboxEventProto::PageClassification page_classification) {
492 VariationParams params;
493 if (!variations::GetVariationParams(kBundledExperimentFieldTrialName,
494 &params)) {
495 return std::string();
497 const std::string page_classification_str =
498 base::IntToString(static_cast<int>(page_classification));
499 const std::string instant_extended =
500 search::IsInstantExtendedAPIEnabled() ? "1" : "0";
501 // Look up rule in this exact context.
502 VariationParams::const_iterator it = params.find(
503 rule + ":" + page_classification_str + ":" + instant_extended);
504 if (it != params.end())
505 return it->second;
506 // Fall back to the global page classification context.
507 it = params.find(rule + ":*:" + instant_extended);
508 if (it != params.end())
509 return it->second;
510 // Fall back to the global instant extended context.
511 it = params.find(rule + ":" + page_classification_str + ":*");
512 if (it != params.end())
513 return it->second;
514 // Look up rule in the global context.
515 it = params.find(rule + ":*:*");
516 return (it != params.end()) ? it->second : std::string();