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"
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
;
28 typedef std::map
<std::string
, std::string
> VariationParams
;
29 typedef HUPScoringParams::ScoreBuckets ScoreBuckets
;
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
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 InitializeBucketsFromString(const std::string
& bucket_string
,
49 ScoreBuckets
* score_buckets
) {
51 score_buckets
->buckets().clear();
52 base::StringPairs kv_pairs
;
53 if (base::SplitStringIntoKeyValuePairs(bucket_string
, ':', ',', &kv_pairs
)) {
54 for (base::StringPairs::const_iterator it
= kv_pairs
.begin();
55 it
!= kv_pairs
.end(); ++it
) {
56 ScoreBuckets::CountMaxRelevance bucket
;
57 base::StringToDouble(it
->first
, &bucket
.first
);
58 base::StringToInt(it
->second
, &bucket
.second
);
59 score_buckets
->buckets().push_back(bucket
);
61 std::sort(score_buckets
->buckets().begin(),
62 score_buckets
->buckets().end(),
63 std::greater
<ScoreBuckets::CountMaxRelevance
>());
67 void InitializeScoreBuckets(const VariationParams
& params
,
68 const char* relevance_cap_param
,
69 const char* half_life_param
,
70 const char* score_buckets_param
,
71 const char* use_decay_factor_param
,
72 ScoreBuckets
* score_buckets
) {
73 VariationParams::const_iterator it
= params
.find(relevance_cap_param
);
74 if (it
!= params
.end()) {
76 if (base::StringToInt(it
->second
, &relevance_cap
))
77 score_buckets
->set_relevance_cap(relevance_cap
);
80 it
= params
.find(use_decay_factor_param
);
81 if (it
!= params
.end()) {
83 if (base::StringToInt(it
->second
, &use_decay_factor
))
84 score_buckets
->set_use_decay_factor(use_decay_factor
!= 0);
87 it
= params
.find(half_life_param
);
88 if (it
!= params
.end()) {
90 if (base::StringToInt(it
->second
, &half_life_days
))
91 score_buckets
->set_half_life_days(half_life_days
);
94 it
= params
.find(score_buckets_param
);
95 if (it
!= params
.end()) {
96 // The value of the score bucket is a comma-separated list of
97 // {DecayedCount/DecayedFactor + ":" + MaxRelevance}.
98 InitializeBucketsFromString(it
->second
, score_buckets
);
104 HUPScoringParams::ScoreBuckets::ScoreBuckets()
105 : relevance_cap_(-1),
107 use_decay_factor_(false) {
110 HUPScoringParams::ScoreBuckets::~ScoreBuckets() {
113 double HUPScoringParams::ScoreBuckets::HalfLifeTimeDecay(
114 const base::TimeDelta
& elapsed_time
) const {
116 if ((half_life_days_
<= 0) ||
117 ((time_ms
= elapsed_time
.InMillisecondsF()) <= 0))
120 const double half_life_intervals
=
121 time_ms
/ base::TimeDelta::FromDays(half_life_days_
).InMillisecondsF();
122 return pow(2.0, -half_life_intervals
);
125 void OmniboxFieldTrial::ActivateDynamicTrials() {
126 // Initialize all autocomplete dynamic field trials. This method may be
127 // called multiple times.
128 for (int i
= 0; i
< kMaxAutocompleteDynamicFieldTrials
; ++i
)
129 base::FieldTrialList::FindValue(DynamicFieldTrialName(i
));
132 int OmniboxFieldTrial::GetDisabledProviderTypes() {
133 const std::string
& types_string
= variations::GetVariationParamValue(
134 kBundledExperimentFieldTrialName
,
135 kDisableProvidersRule
);
137 if (types_string
.empty() || !base::StringToInt(types_string
, &types
)) {
143 void OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(
144 std::vector
<uint32
>* field_trial_hashes
) {
145 field_trial_hashes
->clear();
146 for (int i
= 0; i
< kMaxAutocompleteDynamicFieldTrials
; ++i
) {
147 const std::string
& trial_name
= DynamicFieldTrialName(i
);
148 if (base::FieldTrialList::TrialExists(trial_name
))
149 field_trial_hashes
->push_back(metrics::HashName(trial_name
));
151 if (base::FieldTrialList::TrialExists(kBundledExperimentFieldTrialName
)) {
152 field_trial_hashes
->push_back(
153 metrics::HashName(kBundledExperimentFieldTrialName
));
157 base::TimeDelta
OmniboxFieldTrial::StopTimerFieldTrialDuration() {
159 if (base::StringToInt(
160 base::FieldTrialList::FindFullName(kStopTimerFieldTrialName
),
162 return base::TimeDelta::FromMilliseconds(stop_timer_ms
);
163 return base::TimeDelta::FromMilliseconds(1500);
166 bool OmniboxFieldTrial::InZeroSuggestFieldTrial() {
167 if (variations::GetVariationParamValue(
168 kBundledExperimentFieldTrialName
, kZeroSuggestRule
) == "true")
170 if (variations::GetVariationParamValue(
171 kBundledExperimentFieldTrialName
, kZeroSuggestRule
) == "false")
180 bool OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() {
181 return InZeroSuggestMostVisitedWithoutSerpFieldTrial() ||
182 variations::GetVariationParamValue(
183 kBundledExperimentFieldTrialName
,
184 kZeroSuggestVariantRule
) == "MostVisited";
187 bool OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial() {
188 std::string
variant(variations::GetVariationParamValue(
189 kBundledExperimentFieldTrialName
,
190 kZeroSuggestVariantRule
));
191 if (variant
== "MostVisitedWithoutSERP")
193 #if defined(OS_ANDROID)
194 // Android defaults to MostVisitedWithoutSERP
195 return variant
.empty();
201 bool OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial() {
202 if (variations::GetVariationParamValue(
203 kBundledExperimentFieldTrialName
,
204 kSuggestVariantRule
) == "AfterTyping")
206 #if defined(OS_IOS) || defined(OS_ANDROID)
213 bool OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() {
214 return variations::GetVariationParamValue(
215 kBundledExperimentFieldTrialName
,
216 kZeroSuggestVariantRule
) == "Personalized";
219 bool OmniboxFieldTrial::ShortcutsScoringMaxRelevance(
220 OmniboxEventProto::PageClassification current_page_classification
,
221 int* max_relevance
) {
222 // The value of the rule is a string that encodes an integer containing
223 // the max relevance.
224 const std::string
& max_relevance_str
=
225 OmniboxFieldTrial::GetValueForRuleInContext(
226 kShortcutsScoringMaxRelevanceRule
, current_page_classification
);
227 if (max_relevance_str
.empty())
229 if (!base::StringToInt(max_relevance_str
, max_relevance
))
234 bool OmniboxFieldTrial::SearchHistoryPreventInlining(
235 OmniboxEventProto::PageClassification current_page_classification
) {
236 return OmniboxFieldTrial::GetValueForRuleInContext(
237 kSearchHistoryRule
, current_page_classification
) == "PreventInlining";
240 bool OmniboxFieldTrial::SearchHistoryDisable(
241 OmniboxEventProto::PageClassification current_page_classification
) {
242 return OmniboxFieldTrial::GetValueForRuleInContext(
243 kSearchHistoryRule
, current_page_classification
) == "Disable";
246 void OmniboxFieldTrial::GetDemotionsByType(
247 OmniboxEventProto::PageClassification current_page_classification
,
248 DemotionMultipliers
* demotions_by_type
) {
249 demotions_by_type
->clear();
250 std::string demotion_rule
= OmniboxFieldTrial::GetValueForRuleInContext(
251 kDemoteByTypeRule
, current_page_classification
);
252 // If there is no demotion rule for this context, then use the default
253 // value for that context. At the moment the default value is non-empty
254 // only for the fakebox-focus context.
255 if (demotion_rule
.empty() &&
256 (current_page_classification
==
257 OmniboxEventProto::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS
))
258 demotion_rule
= "1:61,2:61,3:61,4:61,16:61";
260 // The value of the DemoteByType rule is a comma-separated list of
261 // {ResultType + ":" + Number} where ResultType is an AutocompleteMatchType::
262 // Type enum represented as an integer and Number is an integer number
263 // between 0 and 100 inclusive. Relevance scores of matches of that result
264 // type are multiplied by Number / 100. 100 means no change.
265 base::StringPairs kv_pairs
;
266 if (base::SplitStringIntoKeyValuePairs(demotion_rule
, ':', ',', &kv_pairs
)) {
267 for (base::StringPairs::const_iterator it
= kv_pairs
.begin();
268 it
!= kv_pairs
.end(); ++it
) {
269 // This is a best-effort conversion; we trust the hand-crafted parameters
270 // downloaded from the server to be perfect. There's no need to handle
273 base::StringToInt(it
->first
, &k
);
274 base::StringToInt(it
->second
, &v
);
275 (*demotions_by_type
)[static_cast<AutocompleteMatchType::Type
>(k
)] =
276 static_cast<float>(v
) / 100.0f
;
281 void OmniboxFieldTrial::GetDefaultHUPScoringParams(
282 HUPScoringParams
* scoring_params
) {
283 ScoreBuckets
* type_score_buckets
= &scoring_params
->typed_count_buckets
;
284 type_score_buckets
->set_half_life_days(30);
285 type_score_buckets
->set_use_decay_factor(false);
286 // Default typed count buckets based on decayed typed count. The
287 // values here are based on the results of field trials to determine what
288 // maximized overall result quality.
289 const std::string
& typed_count_score_buckets_str
=
290 "1.0:1413,0.97:1390,0.93:1360,0.85:1340,0.72:1320,0.50:1250,0.0:1203";
291 InitializeBucketsFromString(typed_count_score_buckets_str
,
294 ScoreBuckets
* visit_score_buckets
= &scoring_params
->visited_count_buckets
;
295 visit_score_buckets
->set_half_life_days(30);
296 visit_score_buckets
->set_use_decay_factor(false);
297 // Buckets based on visit count. Like the typed count buckets above, the
298 // values here were chosen based on field trials. Note that when a URL hasn't
299 // been visited in the last 30 days, we clamp its score to 100, which
300 // basically demotes it below any other results in the dropdown.
301 const std::string
& visit_count_score_buckets_str
= "4.0:790,0.5:590,0.0:100";
302 InitializeBucketsFromString(visit_count_score_buckets_str
,
303 visit_score_buckets
);
306 void OmniboxFieldTrial::GetExperimentalHUPScoringParams(
307 HUPScoringParams
* scoring_params
) {
308 scoring_params
->experimental_scoring_enabled
= false;
310 VariationParams params
;
311 if (!variations::GetVariationParams(kBundledExperimentFieldTrialName
,
315 VariationParams::const_iterator it
= params
.find(kHUPNewScoringEnabledParam
);
316 if (it
!= params
.end()) {
318 if (base::StringToInt(it
->second
, &enabled
))
319 scoring_params
->experimental_scoring_enabled
= (enabled
!= 0);
322 InitializeScoreBuckets(params
, kHUPNewScoringTypedCountRelevanceCapParam
,
323 kHUPNewScoringTypedCountHalfLifeTimeParam
,
324 kHUPNewScoringTypedCountScoreBucketsParam
,
325 kHUPNewScoringTypedCountUseDecayFactorParam
,
326 &scoring_params
->typed_count_buckets
);
327 InitializeScoreBuckets(params
, kHUPNewScoringVisitedCountRelevanceCapParam
,
328 kHUPNewScoringVisitedCountHalfLifeTimeParam
,
329 kHUPNewScoringVisitedCountScoreBucketsParam
,
330 kHUPNewScoringVisitedCountUseDecayFactorParam
,
331 &scoring_params
->visited_count_buckets
);
334 int OmniboxFieldTrial::HQPBookmarkValue() {
335 std::string bookmark_value_str
=
336 variations::GetVariationParamValue(kBundledExperimentFieldTrialName
,
337 kHQPBookmarkValueRule
);
338 if (bookmark_value_str
.empty())
340 // This is a best-effort conversion; we trust the hand-crafted parameters
341 // downloaded from the server to be perfect. There's no need for handle
344 base::StringToInt(bookmark_value_str
, &bookmark_value
);
345 return bookmark_value
;
348 bool OmniboxFieldTrial::HQPAllowMatchInTLDValue() {
349 return variations::GetVariationParamValue(
350 kBundledExperimentFieldTrialName
,
351 kHQPAllowMatchInTLDRule
) == "true";
354 bool OmniboxFieldTrial::HQPAllowMatchInSchemeValue() {
355 return variations::GetVariationParamValue(
356 kBundledExperimentFieldTrialName
,
357 kHQPAllowMatchInSchemeRule
) == "true";
360 bool OmniboxFieldTrial::DisableResultsCaching() {
361 return variations::GetVariationParamValue(
362 kBundledExperimentFieldTrialName
,
363 kDisableResultsCachingRule
) == "true";
366 void OmniboxFieldTrial::GetSuggestPollingStrategy(bool* from_last_keystroke
,
367 int* polling_delay_ms
) {
368 *from_last_keystroke
= variations::GetVariationParamValue(
369 kBundledExperimentFieldTrialName
,
370 kMeasureSuggestPollingDelayFromLastKeystrokeRule
) == "true";
372 const std::string
& polling_delay_string
= variations::GetVariationParamValue(
373 kBundledExperimentFieldTrialName
,
374 kSuggestPollingDelayMsRule
);
375 if (polling_delay_string
.empty() ||
376 !base::StringToInt(polling_delay_string
, polling_delay_ms
) ||
377 (*polling_delay_ms
<= 0)) {
378 *polling_delay_ms
= kDefaultMinimumTimeBetweenSuggestQueriesMs
;
382 bool OmniboxFieldTrial::HQPExperimentalScoringEnabled() {
383 return variations::GetVariationParamValue(
384 kBundledExperimentFieldTrialName
,
385 kHQPExperimentalScoringEnabledParam
) == "true";
388 std::string
OmniboxFieldTrial::HQPExperimentalScoringBuckets() {
389 if (!HQPExperimentalScoringEnabled())
392 return variations::GetVariationParamValue(
393 kBundledExperimentFieldTrialName
,
394 kHQPExperimentalScoringBucketsParam
);
397 float OmniboxFieldTrial::HQPExperimentalTopicalityThreshold() {
398 if (!HQPExperimentalScoringEnabled())
401 std::string topicality_threhold_str
=
402 variations::GetVariationParamValue(
403 kBundledExperimentFieldTrialName
,
404 kHQPExperimentalScoringTopicalityThresholdParam
);
406 double topicality_threshold
;
407 if (!base::StringToDouble(topicality_threhold_str
, &topicality_threshold
))
410 return static_cast<float>(topicality_threshold
);
413 bool OmniboxFieldTrial::HQPFixFrequencyScoringBugs() {
414 return variations::GetVariationParamValue(
415 kBundledExperimentFieldTrialName
,
416 kHQPFixFrequencyScoringBugsRule
) == "true";
419 size_t OmniboxFieldTrial::HQPNumTitleWordsToAllow() {
420 // The value of the rule is a string that encodes an integer (actually
421 // size_t) containing the number of words.
422 size_t num_title_words
;
423 if (!base::StringToSizeT(
424 variations::GetVariationParamValue(kBundledExperimentFieldTrialName
,
425 kHQPNumTitleWordsRule
),
428 return num_title_words
;
431 bool OmniboxFieldTrial::HQPAlsoDoHUPLikeScoring() {
432 return variations::GetVariationParamValue(
433 kBundledExperimentFieldTrialName
,
434 kHQPAlsoDoHUPLikeScoringRule
) == "true";
437 bool OmniboxFieldTrial::PreventUWYTDefaultForNonURLInputs() {
438 return variations::GetVariationParamValue(
439 kBundledExperimentFieldTrialName
,
440 kPreventUWYTDefaultForNonURLInputsRule
) == "true";
443 const char OmniboxFieldTrial::kBundledExperimentFieldTrialName
[] =
444 "OmniboxBundledExperimentV1";
445 const char OmniboxFieldTrial::kDisableProvidersRule
[] = "DisableProviders";
446 const char OmniboxFieldTrial::kShortcutsScoringMaxRelevanceRule
[] =
447 "ShortcutsScoringMaxRelevance";
448 const char OmniboxFieldTrial::kSearchHistoryRule
[] = "SearchHistory";
449 const char OmniboxFieldTrial::kDemoteByTypeRule
[] = "DemoteByType";
450 const char OmniboxFieldTrial::kHQPBookmarkValueRule
[] =
452 const char OmniboxFieldTrial::kHQPAllowMatchInTLDRule
[] = "HQPAllowMatchInTLD";
453 const char OmniboxFieldTrial::kHQPAllowMatchInSchemeRule
[] =
454 "HQPAllowMatchInScheme";
455 const char OmniboxFieldTrial::kZeroSuggestRule
[] = "ZeroSuggest";
456 const char OmniboxFieldTrial::kZeroSuggestVariantRule
[] = "ZeroSuggestVariant";
457 const char OmniboxFieldTrial::kSuggestVariantRule
[] = "SuggestVariant";
458 const char OmniboxFieldTrial::kDisableResultsCachingRule
[] =
459 "DisableResultsCaching";
461 OmniboxFieldTrial::kMeasureSuggestPollingDelayFromLastKeystrokeRule
[] =
462 "MeasureSuggestPollingDelayFromLastKeystroke";
463 const char OmniboxFieldTrial::kSuggestPollingDelayMsRule
[] =
464 "SuggestPollingDelayMs";
465 const char OmniboxFieldTrial::kHQPFixFrequencyScoringBugsRule
[] =
466 "HQPFixFrequencyScoringBugs";
467 const char OmniboxFieldTrial::kHQPNumTitleWordsRule
[] = "HQPNumTitleWords";
468 const char OmniboxFieldTrial::kHQPAlsoDoHUPLikeScoringRule
[] =
469 "HQPAlsoDoHUPLikeScoring";
470 const char OmniboxFieldTrial::kPreventUWYTDefaultForNonURLInputsRule
[] =
471 "PreventUWYTDefaultForNonURLInputs";
473 const char OmniboxFieldTrial::kHUPNewScoringEnabledParam
[] =
474 "HUPExperimentalScoringEnabled";
475 const char OmniboxFieldTrial::kHUPNewScoringTypedCountRelevanceCapParam
[] =
476 "TypedCountRelevanceCap";
477 const char OmniboxFieldTrial::kHUPNewScoringTypedCountHalfLifeTimeParam
[] =
478 "TypedCountHalfLifeTime";
479 const char OmniboxFieldTrial::kHUPNewScoringTypedCountScoreBucketsParam
[] =
480 "TypedCountScoreBuckets";
481 const char OmniboxFieldTrial::kHUPNewScoringTypedCountUseDecayFactorParam
[] =
482 "TypedCountUseDecayFactor";
483 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountRelevanceCapParam
[] =
484 "VisitedCountRelevanceCap";
485 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountHalfLifeTimeParam
[] =
486 "VisitedCountHalfLifeTime";
487 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountScoreBucketsParam
[] =
488 "VisitedCountScoreBuckets";
489 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountUseDecayFactorParam
[] =
490 "VisitedCountUseDecayFactor";
492 const char OmniboxFieldTrial::kHQPExperimentalScoringEnabledParam
[] =
493 "HQPExperimentalScoringEnabled";
494 const char OmniboxFieldTrial::kHQPExperimentalScoringBucketsParam
[] =
495 "HQPExperimentalScoringBuckets";
497 OmniboxFieldTrial::kHQPExperimentalScoringTopicalityThresholdParam
[] =
498 "HQPExperimentalScoringTopicalityThreshold";
501 int OmniboxFieldTrial::kDefaultMinimumTimeBetweenSuggestQueriesMs
= 100;
503 // Background and implementation details:
505 // Each experiment group in any field trial can come with an optional set of
506 // parameters (key-value pairs). In the bundled omnibox experiment
507 // (kBundledExperimentFieldTrialName), each experiment group comes with a
508 // list of parameters in the form:
510 // <OmniboxEventProto::PageClassification (as an int)>:
511 // <whether Instant Extended is enabled (as a 1 or 0)>
512 // (note that there are no linebreaks in keys; this format is for
513 // presentation only>
514 // value=<arbitrary string>
515 // Both the OmniboxEventProto::PageClassification and the Instant Extended
516 // entries can be "*", which means this rule applies for all values of the
517 // matching portion of the context.
518 // One example parameter is
519 // key=SearchHistory:6:1
520 // value=PreventInlining
521 // This means in page classification context 6 (a search result page doing
522 // search term replacement) with Instant Extended enabled, the SearchHistory
523 // experiment should PreventInlining.
525 // When an exact match to the rule in the current context is missing, we
526 // give preference to a wildcard rule that matches the instant extended
527 // context over a wildcard rule that matches the page classification
528 // context. Hopefully, though, users will write their field trial configs
529 // so as not to rely on this fall back order.
531 // In short, this function tries to find the value associated with key
532 // |rule|:|page_classification|:|instant_extended|, failing that it looks up
533 // |rule|:*:|instant_extended|, failing that it looks up
534 // |rule|:|page_classification|:*, failing that it looks up |rule|:*:*,
535 // and failing that it returns the empty string.
536 std::string
OmniboxFieldTrial::GetValueForRuleInContext(
537 const std::string
& rule
,
538 OmniboxEventProto::PageClassification page_classification
) {
539 VariationParams params
;
540 if (!variations::GetVariationParams(kBundledExperimentFieldTrialName
,
542 return std::string();
544 const std::string page_classification_str
=
545 base::IntToString(static_cast<int>(page_classification
));
546 const std::string instant_extended
=
547 search::IsInstantExtendedAPIEnabled() ? "1" : "0";
548 // Look up rule in this exact context.
549 VariationParams::const_iterator it
= params
.find(
550 rule
+ ":" + page_classification_str
+ ":" + instant_extended
);
551 if (it
!= params
.end())
553 // Fall back to the global page classification context.
554 it
= params
.find(rule
+ ":*:" + instant_extended
);
555 if (it
!= params
.end())
557 // Fall back to the global instant extended context.
558 it
= params
.find(rule
+ ":" + page_classification_str
+ ":*");
559 if (it
!= params
.end())
561 // Look up rule in the global context.
562 it
= params
.find(rule
+ ":*:*");
563 return (it
!= params
.end()) ? it
->second
: std::string();