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/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/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 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()) {
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()) {
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
>());
89 HUPScoringParams::ScoreBuckets::ScoreBuckets()
94 HUPScoringParams::ScoreBuckets::~ScoreBuckets() {
97 double HUPScoringParams::ScoreBuckets::HalfLifeTimeDecay(
98 const base::TimeDelta
& elapsed_time
) const {
100 if ((half_life_days_
<= 0) ||
101 ((time_ms
= elapsed_time
.InMillisecondsF()) <= 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
);
121 if (types_string
.empty() || !base::StringToInt(types_string
, &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() {
143 if (base::StringToInt(
144 base::FieldTrialList::FindFullName(kStopTimerFieldTrialName
),
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")
154 if (variations::GetVariationParamValue(
155 kBundledExperimentFieldTrialName
, kZeroSuggestRule
) == "false")
157 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_LINUX) || \
158 (defined(OS_MACOSX) && !defined(OS_IOS))
165 bool OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() {
166 return InZeroSuggestMostVisitedWithoutSerpFieldTrial() ||
167 variations::GetVariationParamValue(
168 kBundledExperimentFieldTrialName
,
169 kZeroSuggestVariantRule
) == "MostVisited";
172 bool OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial() {
173 return variations::GetVariationParamValue(
174 kBundledExperimentFieldTrialName
,
175 kZeroSuggestVariantRule
) == "MostVisitedWithoutSERP";
178 bool OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial() {
179 return variations::GetVariationParamValue(
180 kBundledExperimentFieldTrialName
,
181 kZeroSuggestVariantRule
) == "AfterTyping";
184 bool OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() {
185 return variations::GetVariationParamValue(
186 kBundledExperimentFieldTrialName
,
187 kZeroSuggestVariantRule
) == "Personalized";
190 bool OmniboxFieldTrial::ShortcutsScoringMaxRelevance(
191 OmniboxEventProto::PageClassification current_page_classification
,
192 int* max_relevance
) {
193 // The value of the rule is a string that encodes an integer containing
194 // the max relevance.
195 const std::string
& max_relevance_str
=
196 OmniboxFieldTrial::GetValueForRuleInContext(
197 kShortcutsScoringMaxRelevanceRule
, current_page_classification
);
198 if (max_relevance_str
.empty())
200 if (!base::StringToInt(max_relevance_str
, max_relevance
))
205 bool OmniboxFieldTrial::SearchHistoryPreventInlining(
206 OmniboxEventProto::PageClassification current_page_classification
) {
207 return OmniboxFieldTrial::GetValueForRuleInContext(
208 kSearchHistoryRule
, current_page_classification
) == "PreventInlining";
211 bool OmniboxFieldTrial::SearchHistoryDisable(
212 OmniboxEventProto::PageClassification current_page_classification
) {
213 return OmniboxFieldTrial::GetValueForRuleInContext(
214 kSearchHistoryRule
, current_page_classification
) == "Disable";
217 void OmniboxFieldTrial::GetDemotionsByType(
218 OmniboxEventProto::PageClassification current_page_classification
,
219 DemotionMultipliers
* demotions_by_type
) {
220 demotions_by_type
->clear();
221 std::string demotion_rule
= OmniboxFieldTrial::GetValueForRuleInContext(
222 kDemoteByTypeRule
, current_page_classification
);
223 // If there is no demotion rule for this context, then use the default
224 // value for that context. At the moment the default value is non-empty
225 // only for the fakebox-focus context.
226 if (demotion_rule
.empty() &&
227 (current_page_classification
==
228 OmniboxEventProto::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS
))
229 demotion_rule
= "1:61,2:61,3:61,4:61,16:61";
231 // The value of the DemoteByType rule is a comma-separated list of
232 // {ResultType + ":" + Number} where ResultType is an AutocompleteMatchType::
233 // Type enum represented as an integer and Number is an integer number
234 // between 0 and 100 inclusive. Relevance scores of matches of that result
235 // type are multiplied by Number / 100. 100 means no change.
236 base::StringPairs kv_pairs
;
237 if (base::SplitStringIntoKeyValuePairs(demotion_rule
, ':', ',', &kv_pairs
)) {
238 for (base::StringPairs::const_iterator it
= kv_pairs
.begin();
239 it
!= kv_pairs
.end(); ++it
) {
240 // This is a best-effort conversion; we trust the hand-crafted parameters
241 // downloaded from the server to be perfect. There's no need to handle
244 base::StringToInt(it
->first
, &k
);
245 base::StringToInt(it
->second
, &v
);
246 (*demotions_by_type
)[static_cast<AutocompleteMatchType::Type
>(k
)] =
247 static_cast<float>(v
) / 100.0f
;
252 void OmniboxFieldTrial::GetExperimentalHUPScoringParams(
253 HUPScoringParams
* scoring_params
) {
254 scoring_params
->experimental_scoring_enabled
= false;
256 VariationParams params
;
257 if (!variations::GetVariationParams(kBundledExperimentFieldTrialName
,
261 VariationParams::const_iterator it
= params
.find(kHUPNewScoringEnabledParam
);
262 if (it
!= params
.end()) {
264 if (base::StringToInt(it
->second
, &enabled
))
265 scoring_params
->experimental_scoring_enabled
= (enabled
!= 0);
268 InitializeScoreBuckets(params
, kHUPNewScoringTypedCountRelevanceCapParam
,
269 kHUPNewScoringTypedCountHalfLifeTimeParam
,
270 kHUPNewScoringTypedCountScoreBucketsParam
,
271 &scoring_params
->typed_count_buckets
);
272 InitializeScoreBuckets(params
, kHUPNewScoringVisitedCountRelevanceCapParam
,
273 kHUPNewScoringVisitedCountHalfLifeTimeParam
,
274 kHUPNewScoringVisitedCountScoreBucketsParam
,
275 &scoring_params
->visited_count_buckets
);
278 int OmniboxFieldTrial::HQPBookmarkValue() {
279 std::string bookmark_value_str
=
280 variations::GetVariationParamValue(kBundledExperimentFieldTrialName
,
281 kHQPBookmarkValueRule
);
282 if (bookmark_value_str
.empty())
284 // This is a best-effort conversion; we trust the hand-crafted parameters
285 // downloaded from the server to be perfect. There's no need for handle
288 base::StringToInt(bookmark_value_str
, &bookmark_value
);
289 return bookmark_value
;
292 bool OmniboxFieldTrial::HQPAllowMatchInTLDValue() {
293 return variations::GetVariationParamValue(
294 kBundledExperimentFieldTrialName
,
295 kHQPAllowMatchInTLDRule
) == "true";
298 bool OmniboxFieldTrial::HQPAllowMatchInSchemeValue() {
299 return variations::GetVariationParamValue(
300 kBundledExperimentFieldTrialName
,
301 kHQPAllowMatchInSchemeRule
) == "true";
304 bool OmniboxFieldTrial::EnableAnswersInSuggest() {
305 const base::CommandLine
* cl
= base::CommandLine::ForCurrentProcess();
306 if (cl
->HasSwitch(switches::kDisableAnswersInSuggest
))
308 if (cl
->HasSwitch(switches::kEnableAnswersInSuggest
))
311 return variations::GetVariationParamValue(
312 kBundledExperimentFieldTrialName
,
313 kAnswersInSuggestRule
) == "true";
316 bool OmniboxFieldTrial::DisableResultsCaching() {
317 return variations::GetVariationParamValue(
318 kBundledExperimentFieldTrialName
,
319 kDisableResultsCachingRule
) == "true";
322 void OmniboxFieldTrial::GetSuggestPollingStrategy(bool* from_last_keystroke
,
323 int* polling_delay_ms
) {
324 *from_last_keystroke
= variations::GetVariationParamValue(
325 kBundledExperimentFieldTrialName
,
326 kMeasureSuggestPollingDelayFromLastKeystrokeRule
) == "true";
328 const std::string
& polling_delay_string
= variations::GetVariationParamValue(
329 kBundledExperimentFieldTrialName
,
330 kSuggestPollingDelayMsRule
);
331 if (polling_delay_string
.empty() ||
332 !base::StringToInt(polling_delay_string
, polling_delay_ms
) ||
333 (*polling_delay_ms
<= 0)) {
334 *polling_delay_ms
= kDefaultMinimumTimeBetweenSuggestQueriesMs
;
338 bool OmniboxFieldTrial::HQPExperimentalScoringEnabled() {
339 return variations::GetVariationParamValue(
340 kBundledExperimentFieldTrialName
,
341 kHQPExperimentalScoringEnabledParam
) == "true";
344 std::string
OmniboxFieldTrial::HQPExperimentalScoringBuckets() {
345 if (!HQPExperimentalScoringEnabled())
348 return variations::GetVariationParamValue(
349 kBundledExperimentFieldTrialName
,
350 kHQPExperimentalScoringBucketsParam
);
353 float OmniboxFieldTrial::HQPExperimentalTopicalityThreshold() {
354 if (!HQPExperimentalScoringEnabled())
357 std::string topicality_threhold_str
=
358 variations::GetVariationParamValue(
359 kBundledExperimentFieldTrialName
,
360 kHQPExperimentalScoringTopicalityThresholdParam
);
362 double topicality_threshold
;
363 if (!base::StringToDouble(topicality_threhold_str
, &topicality_threshold
))
366 return static_cast<float>(topicality_threshold
);
369 bool OmniboxFieldTrial::HQPFixFrequencyScoringBugs() {
370 return variations::GetVariationParamValue(
371 kBundledExperimentFieldTrialName
,
372 kHQPFixFrequencyScoringBugsRule
) == "true";
375 size_t OmniboxFieldTrial::HQPNumTitleWordsToAllow() {
376 // The value of the rule is a string that encodes an integer (actually
377 // size_t) containing the number of words.
378 size_t num_title_words
;
379 if (!base::StringToSizeT(
380 variations::GetVariationParamValue(kBundledExperimentFieldTrialName
,
381 kHQPNumTitleWordsRule
),
384 return num_title_words
;
387 bool OmniboxFieldTrial::HQPAlsoDoHUPLikeScoring() {
388 return variations::GetVariationParamValue(
389 kBundledExperimentFieldTrialName
,
390 kHQPAlsoDoHUPLikeScoringRule
) == "true";
393 const char OmniboxFieldTrial::kBundledExperimentFieldTrialName
[] =
394 "OmniboxBundledExperimentV1";
395 const char OmniboxFieldTrial::kDisableProvidersRule
[] = "DisableProviders";
396 const char OmniboxFieldTrial::kShortcutsScoringMaxRelevanceRule
[] =
397 "ShortcutsScoringMaxRelevance";
398 const char OmniboxFieldTrial::kSearchHistoryRule
[] = "SearchHistory";
399 const char OmniboxFieldTrial::kDemoteByTypeRule
[] = "DemoteByType";
400 const char OmniboxFieldTrial::kHQPBookmarkValueRule
[] =
402 const char OmniboxFieldTrial::kHQPAllowMatchInTLDRule
[] = "HQPAllowMatchInTLD";
403 const char OmniboxFieldTrial::kHQPAllowMatchInSchemeRule
[] =
404 "HQPAllowMatchInScheme";
405 const char OmniboxFieldTrial::kZeroSuggestRule
[] = "ZeroSuggest";
406 const char OmniboxFieldTrial::kZeroSuggestVariantRule
[] = "ZeroSuggestVariant";
407 const char OmniboxFieldTrial::kAnswersInSuggestRule
[] = "AnswersInSuggest";
408 const char OmniboxFieldTrial::kDisableResultsCachingRule
[] =
409 "DisableResultsCaching";
411 OmniboxFieldTrial::kMeasureSuggestPollingDelayFromLastKeystrokeRule
[] =
412 "MeasureSuggestPollingDelayFromLastKeystroke";
413 const char OmniboxFieldTrial::kSuggestPollingDelayMsRule
[] =
414 "SuggestPollingDelayMs";
415 const char OmniboxFieldTrial::kHQPFixFrequencyScoringBugsRule
[] =
416 "HQPFixFrequencyScoringBugs";
417 const char OmniboxFieldTrial::kHQPNumTitleWordsRule
[] = "HQPNumTitleWords";
418 const char OmniboxFieldTrial::kHQPAlsoDoHUPLikeScoringRule
[] =
419 "HQPAlsoDoHUPLikeScoring";
421 const char OmniboxFieldTrial::kHUPNewScoringEnabledParam
[] =
422 "HUPExperimentalScoringEnabled";
423 const char OmniboxFieldTrial::kHUPNewScoringTypedCountRelevanceCapParam
[] =
424 "TypedCountRelevanceCap";
425 const char OmniboxFieldTrial::kHUPNewScoringTypedCountHalfLifeTimeParam
[] =
426 "TypedCountHalfLifeTime";
427 const char OmniboxFieldTrial::kHUPNewScoringTypedCountScoreBucketsParam
[] =
428 "TypedCountScoreBuckets";
429 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountRelevanceCapParam
[] =
430 "VisitedCountRelevanceCap";
431 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountHalfLifeTimeParam
[] =
432 "VisitedCountHalfLifeTime";
433 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountScoreBucketsParam
[] =
434 "VisitedCountScoreBuckets";
436 const char OmniboxFieldTrial::kHQPExperimentalScoringEnabledParam
[] =
437 "HQPExperimentalScoringEnabled";
438 const char OmniboxFieldTrial::kHQPExperimentalScoringBucketsParam
[] =
439 "HQPExperimentalScoringBuckets";
441 OmniboxFieldTrial::kHQPExperimentalScoringTopicalityThresholdParam
[] =
442 "HQPExperimentalScoringTopicalityThreshold";
445 int OmniboxFieldTrial::kDefaultMinimumTimeBetweenSuggestQueriesMs
= 100;
447 // Background and implementation details:
449 // Each experiment group in any field trial can come with an optional set of
450 // parameters (key-value pairs). In the bundled omnibox experiment
451 // (kBundledExperimentFieldTrialName), each experiment group comes with a
452 // list of parameters in the form:
454 // <OmniboxEventProto::PageClassification (as an int)>:
455 // <whether Instant Extended is enabled (as a 1 or 0)>
456 // (note that there are no linebreaks in keys; this format is for
457 // presentation only>
458 // value=<arbitrary string>
459 // Both the OmniboxEventProto::PageClassification and the Instant Extended
460 // entries can be "*", which means this rule applies for all values of the
461 // matching portion of the context.
462 // One example parameter is
463 // key=SearchHistory:6:1
464 // value=PreventInlining
465 // This means in page classification context 6 (a search result page doing
466 // search term replacement) with Instant Extended enabled, the SearchHistory
467 // experiment should PreventInlining.
469 // When an exact match to the rule in the current context is missing, we
470 // give preference to a wildcard rule that matches the instant extended
471 // context over a wildcard rule that matches the page classification
472 // context. Hopefully, though, users will write their field trial configs
473 // so as not to rely on this fall back order.
475 // In short, this function tries to find the value associated with key
476 // |rule|:|page_classification|:|instant_extended|, failing that it looks up
477 // |rule|:*:|instant_extended|, failing that it looks up
478 // |rule|:|page_classification|:*, failing that it looks up |rule|:*:*,
479 // and failing that it returns the empty string.
480 std::string
OmniboxFieldTrial::GetValueForRuleInContext(
481 const std::string
& rule
,
482 OmniboxEventProto::PageClassification page_classification
) {
483 VariationParams params
;
484 if (!variations::GetVariationParams(kBundledExperimentFieldTrialName
,
486 return std::string();
488 const std::string page_classification_str
=
489 base::IntToString(static_cast<int>(page_classification
));
490 const std::string instant_extended
=
491 chrome::IsInstantExtendedAPIEnabled() ? "1" : "0";
492 // Look up rule in this exact context.
493 VariationParams::const_iterator it
= params
.find(
494 rule
+ ":" + page_classification_str
+ ":" + instant_extended
);
495 if (it
!= params
.end())
497 // Fall back to the global page classification context.
498 it
= params
.find(rule
+ ":*:" + instant_extended
);
499 if (it
!= params
.end())
501 // Fall back to the global instant extended context.
502 it
= params
.find(rule
+ ":" + page_classification_str
+ ":*");
503 if (it
!= params
.end())
505 // Look up rule in the global context.
506 it
= params
.find(rule
+ ":*:*");
507 return (it
!= params
.end()) ? it
->second
: std::string();