content: Hook up Compositor to RendererScheduler.
[chromium-blink-merge.git] / components / omnibox / omnibox_field_trial.cc
blob7ad29563fa1631a8a80d784486116f873a055291
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"
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/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 // Make sure that Autocomplete dynamic field trials are activated. It's OK to
118 // call this method multiple times.
119 ActivateDynamicTrials();
121 // Look for group names in form of "DisabledProviders_<mask>" where "mask"
122 // is a bitmap of disabled provider types (AutocompleteProvider::Type).
123 int provider_types = 0;
124 for (int i = 0; i < kMaxAutocompleteDynamicFieldTrials; ++i) {
125 std::string group_name = base::FieldTrialList::FindFullName(
126 DynamicFieldTrialName(i));
127 const char kDisabledProviders[] = "DisabledProviders_";
128 if (!StartsWithASCII(group_name, kDisabledProviders, true))
129 continue;
130 int types = 0;
131 if (!base::StringToInt(base::StringPiece(
132 group_name.substr(strlen(kDisabledProviders))), &types))
133 continue;
134 provider_types |= types;
136 return provider_types;
139 void OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(
140 std::vector<uint32>* field_trial_hashes) {
141 field_trial_hashes->clear();
142 for (int i = 0; i < kMaxAutocompleteDynamicFieldTrials; ++i) {
143 const std::string& trial_name = DynamicFieldTrialName(i);
144 if (base::FieldTrialList::TrialExists(trial_name))
145 field_trial_hashes->push_back(metrics::HashName(trial_name));
147 if (base::FieldTrialList::TrialExists(kBundledExperimentFieldTrialName)) {
148 field_trial_hashes->push_back(
149 metrics::HashName(kBundledExperimentFieldTrialName));
153 base::TimeDelta OmniboxFieldTrial::StopTimerFieldTrialDuration() {
154 int stop_timer_ms;
155 if (base::StringToInt(
156 base::FieldTrialList::FindFullName(kStopTimerFieldTrialName),
157 &stop_timer_ms))
158 return base::TimeDelta::FromMilliseconds(stop_timer_ms);
159 return base::TimeDelta::FromMilliseconds(1500);
162 bool OmniboxFieldTrial::InZeroSuggestFieldTrial() {
163 if (variations::GetVariationParamValue(
164 kBundledExperimentFieldTrialName, kZeroSuggestRule) == "true")
165 return true;
166 if (variations::GetVariationParamValue(
167 kBundledExperimentFieldTrialName, kZeroSuggestRule) == "false")
168 return false;
169 #if defined(OS_WIN) || defined(OS_CHROMEOS) || defined(OS_LINUX) || \
170 (defined(OS_MACOSX) && !defined(OS_IOS))
171 return true;
172 #else
173 return false;
174 #endif
177 bool OmniboxFieldTrial::InZeroSuggestMostVisitedFieldTrial() {
178 return InZeroSuggestMostVisitedWithoutSerpFieldTrial() ||
179 variations::GetVariationParamValue(
180 kBundledExperimentFieldTrialName,
181 kZeroSuggestVariantRule) == "MostVisited";
184 bool OmniboxFieldTrial::InZeroSuggestMostVisitedWithoutSerpFieldTrial() {
185 return variations::GetVariationParamValue(
186 kBundledExperimentFieldTrialName,
187 kZeroSuggestVariantRule) == "MostVisitedWithoutSERP";
190 bool OmniboxFieldTrial::InZeroSuggestAfterTypingFieldTrial() {
191 return variations::GetVariationParamValue(
192 kBundledExperimentFieldTrialName,
193 kZeroSuggestVariantRule) == "AfterTyping";
196 bool OmniboxFieldTrial::InZeroSuggestPersonalizedFieldTrial() {
197 return variations::GetVariationParamValue(
198 kBundledExperimentFieldTrialName,
199 kZeroSuggestVariantRule) == "Personalized";
202 bool OmniboxFieldTrial::ShortcutsScoringMaxRelevance(
203 OmniboxEventProto::PageClassification current_page_classification,
204 int* max_relevance) {
205 // The value of the rule is a string that encodes an integer containing
206 // the max relevance.
207 const std::string& max_relevance_str =
208 OmniboxFieldTrial::GetValueForRuleInContext(
209 kShortcutsScoringMaxRelevanceRule, current_page_classification);
210 if (max_relevance_str.empty())
211 return false;
212 if (!base::StringToInt(max_relevance_str, max_relevance))
213 return false;
214 return true;
217 bool OmniboxFieldTrial::SearchHistoryPreventInlining(
218 OmniboxEventProto::PageClassification current_page_classification) {
219 return OmniboxFieldTrial::GetValueForRuleInContext(
220 kSearchHistoryRule, current_page_classification) == "PreventInlining";
223 bool OmniboxFieldTrial::SearchHistoryDisable(
224 OmniboxEventProto::PageClassification current_page_classification) {
225 return OmniboxFieldTrial::GetValueForRuleInContext(
226 kSearchHistoryRule, current_page_classification) == "Disable";
229 void OmniboxFieldTrial::GetDemotionsByType(
230 OmniboxEventProto::PageClassification current_page_classification,
231 DemotionMultipliers* demotions_by_type) {
232 demotions_by_type->clear();
233 std::string demotion_rule = OmniboxFieldTrial::GetValueForRuleInContext(
234 kDemoteByTypeRule, current_page_classification);
235 // If there is no demotion rule for this context, then use the default
236 // value for that context. At the moment the default value is non-empty
237 // only for the fakebox-focus context.
238 if (demotion_rule.empty() &&
239 (current_page_classification ==
240 OmniboxEventProto::INSTANT_NTP_WITH_FAKEBOX_AS_STARTING_FOCUS))
241 demotion_rule = "1:61,2:61,3:61,4:61,16:61";
243 // The value of the DemoteByType rule is a comma-separated list of
244 // {ResultType + ":" + Number} where ResultType is an AutocompleteMatchType::
245 // Type enum represented as an integer and Number is an integer number
246 // between 0 and 100 inclusive. Relevance scores of matches of that result
247 // type are multiplied by Number / 100. 100 means no change.
248 base::StringPairs kv_pairs;
249 if (base::SplitStringIntoKeyValuePairs(demotion_rule, ':', ',', &kv_pairs)) {
250 for (base::StringPairs::const_iterator it = kv_pairs.begin();
251 it != kv_pairs.end(); ++it) {
252 // This is a best-effort conversion; we trust the hand-crafted parameters
253 // downloaded from the server to be perfect. There's no need to handle
254 // errors smartly.
255 int k, v;
256 base::StringToInt(it->first, &k);
257 base::StringToInt(it->second, &v);
258 (*demotions_by_type)[static_cast<AutocompleteMatchType::Type>(k)] =
259 static_cast<float>(v) / 100.0f;
264 void OmniboxFieldTrial::GetExperimentalHUPScoringParams(
265 HUPScoringParams* scoring_params) {
266 scoring_params->experimental_scoring_enabled = false;
268 VariationParams params;
269 if (!variations::GetVariationParams(kBundledExperimentFieldTrialName,
270 &params))
271 return;
273 VariationParams::const_iterator it = params.find(kHUPNewScoringEnabledParam);
274 if (it != params.end()) {
275 int enabled = 0;
276 if (base::StringToInt(it->second, &enabled))
277 scoring_params->experimental_scoring_enabled = (enabled != 0);
280 InitializeScoreBuckets(params, kHUPNewScoringTypedCountRelevanceCapParam,
281 kHUPNewScoringTypedCountHalfLifeTimeParam,
282 kHUPNewScoringTypedCountScoreBucketsParam,
283 &scoring_params->typed_count_buckets);
284 InitializeScoreBuckets(params, kHUPNewScoringVisitedCountRelevanceCapParam,
285 kHUPNewScoringVisitedCountHalfLifeTimeParam,
286 kHUPNewScoringVisitedCountScoreBucketsParam,
287 &scoring_params->visited_count_buckets);
290 int OmniboxFieldTrial::HQPBookmarkValue() {
291 std::string bookmark_value_str =
292 variations::GetVariationParamValue(kBundledExperimentFieldTrialName,
293 kHQPBookmarkValueRule);
294 if (bookmark_value_str.empty())
295 return 10;
296 // This is a best-effort conversion; we trust the hand-crafted parameters
297 // downloaded from the server to be perfect. There's no need for handle
298 // errors smartly.
299 int bookmark_value;
300 base::StringToInt(bookmark_value_str, &bookmark_value);
301 return bookmark_value;
304 bool OmniboxFieldTrial::HQPAllowMatchInTLDValue() {
305 return variations::GetVariationParamValue(
306 kBundledExperimentFieldTrialName,
307 kHQPAllowMatchInTLDRule) == "true";
310 bool OmniboxFieldTrial::HQPAllowMatchInSchemeValue() {
311 return variations::GetVariationParamValue(
312 kBundledExperimentFieldTrialName,
313 kHQPAllowMatchInSchemeRule) == "true";
316 bool OmniboxFieldTrial::DisableInlining() {
317 return variations::GetVariationParamValue(
318 kBundledExperimentFieldTrialName,
319 kDisableInliningRule) == "true";
322 bool OmniboxFieldTrial::EnableAnswersInSuggest() {
323 const CommandLine* cl = CommandLine::ForCurrentProcess();
324 if (cl->HasSwitch(switches::kDisableAnswersInSuggest))
325 return false;
326 if (cl->HasSwitch(switches::kEnableAnswersInSuggest))
327 return true;
329 return variations::GetVariationParamValue(
330 kBundledExperimentFieldTrialName,
331 kAnswersInSuggestRule) == "true";
334 bool OmniboxFieldTrial::AddUWYTMatchEvenIfPromotedURLs() {
335 return variations::GetVariationParamValue(
336 kBundledExperimentFieldTrialName,
337 kAddUWYTMatchEvenIfPromotedURLsRule) == "true";
340 bool OmniboxFieldTrial::DisplayHintTextWhenPossible() {
341 return variations::GetVariationParamValue(
342 kBundledExperimentFieldTrialName,
343 kDisplayHintTextWhenPossibleRule) == "true";
346 bool OmniboxFieldTrial::DisableResultsCaching() {
347 return variations::GetVariationParamValue(
348 kBundledExperimentFieldTrialName,
349 kDisableResultsCachingRule) == "true";
352 void OmniboxFieldTrial::GetSuggestPollingStrategy(bool* from_last_keystroke,
353 int* polling_delay_ms) {
354 *from_last_keystroke = variations::GetVariationParamValue(
355 kBundledExperimentFieldTrialName,
356 kMeasureSuggestPollingDelayFromLastKeystrokeRule) == "true";
358 const std::string& polling_delay_string = variations::GetVariationParamValue(
359 kBundledExperimentFieldTrialName,
360 kSuggestPollingDelayMsRule);
361 if (polling_delay_string.empty() ||
362 !base::StringToInt(polling_delay_string, polling_delay_ms) ||
363 (*polling_delay_ms <= 0)) {
364 *polling_delay_ms = kDefaultMinimumTimeBetweenSuggestQueriesMs;
368 const char OmniboxFieldTrial::kBundledExperimentFieldTrialName[] =
369 "OmniboxBundledExperimentV1";
370 const char OmniboxFieldTrial::kShortcutsScoringMaxRelevanceRule[] =
371 "ShortcutsScoringMaxRelevance";
372 const char OmniboxFieldTrial::kSearchHistoryRule[] = "SearchHistory";
373 const char OmniboxFieldTrial::kDemoteByTypeRule[] = "DemoteByType";
374 const char OmniboxFieldTrial::kHQPBookmarkValueRule[] =
375 "HQPBookmarkValue";
376 const char OmniboxFieldTrial::kHQPAllowMatchInTLDRule[] = "HQPAllowMatchInTLD";
377 const char OmniboxFieldTrial::kHQPAllowMatchInSchemeRule[] =
378 "HQPAllowMatchInScheme";
379 const char OmniboxFieldTrial::kZeroSuggestRule[] = "ZeroSuggest";
380 const char OmniboxFieldTrial::kZeroSuggestVariantRule[] = "ZeroSuggestVariant";
381 const char OmniboxFieldTrial::kDisableInliningRule[] = "DisableInlining";
382 const char OmniboxFieldTrial::kAnswersInSuggestRule[] = "AnswersInSuggest";
383 const char OmniboxFieldTrial::kAddUWYTMatchEvenIfPromotedURLsRule[] =
384 "AddUWYTMatchEvenIfPromotedURLs";
385 const char OmniboxFieldTrial::kDisplayHintTextWhenPossibleRule[] =
386 "DisplayHintTextWhenPossible";
387 const char OmniboxFieldTrial::kDisableResultsCachingRule[] =
388 "DisableResultsCaching";
389 const char
390 OmniboxFieldTrial::kMeasureSuggestPollingDelayFromLastKeystrokeRule[] =
391 "MeasureSuggestPollingDelayFromLastKeystroke";
392 const char OmniboxFieldTrial::kSuggestPollingDelayMsRule[] =
393 "SuggestPollingDelayMs";
395 const char OmniboxFieldTrial::kHUPNewScoringEnabledParam[] =
396 "HUPExperimentalScoringEnabled";
397 const char OmniboxFieldTrial::kHUPNewScoringTypedCountRelevanceCapParam[] =
398 "TypedCountRelevanceCap";
399 const char OmniboxFieldTrial::kHUPNewScoringTypedCountHalfLifeTimeParam[] =
400 "TypedCountHalfLifeTime";
401 const char OmniboxFieldTrial::kHUPNewScoringTypedCountScoreBucketsParam[] =
402 "TypedCountScoreBuckets";
403 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountRelevanceCapParam[] =
404 "VisitedCountRelevanceCap";
405 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountHalfLifeTimeParam[] =
406 "VisitedCountHalfLifeTime";
407 const char OmniboxFieldTrial::kHUPNewScoringVisitedCountScoreBucketsParam[] =
408 "VisitedCountScoreBuckets";
410 // static
411 int OmniboxFieldTrial::kDefaultMinimumTimeBetweenSuggestQueriesMs = 100;
413 // Background and implementation details:
415 // Each experiment group in any field trial can come with an optional set of
416 // parameters (key-value pairs). In the bundled omnibox experiment
417 // (kBundledExperimentFieldTrialName), each experiment group comes with a
418 // list of parameters in the form:
419 // key=<Rule>:
420 // <OmniboxEventProto::PageClassification (as an int)>:
421 // <whether Instant Extended is enabled (as a 1 or 0)>
422 // (note that there are no linebreaks in keys; this format is for
423 // presentation only>
424 // value=<arbitrary string>
425 // Both the OmniboxEventProto::PageClassification and the Instant Extended
426 // entries can be "*", which means this rule applies for all values of the
427 // matching portion of the context.
428 // One example parameter is
429 // key=SearchHistory:6:1
430 // value=PreventInlining
431 // This means in page classification context 6 (a search result page doing
432 // search term replacement) with Instant Extended enabled, the SearchHistory
433 // experiment should PreventInlining.
435 // When an exact match to the rule in the current context is missing, we
436 // give preference to a wildcard rule that matches the instant extended
437 // context over a wildcard rule that matches the page classification
438 // context. Hopefully, though, users will write their field trial configs
439 // so as not to rely on this fall back order.
441 // In short, this function tries to find the value associated with key
442 // |rule|:|page_classification|:|instant_extended|, failing that it looks up
443 // |rule|:*:|instant_extended|, failing that it looks up
444 // |rule|:|page_classification|:*, failing that it looks up |rule|:*:*,
445 // and failing that it returns the empty string.
446 std::string OmniboxFieldTrial::GetValueForRuleInContext(
447 const std::string& rule,
448 OmniboxEventProto::PageClassification page_classification) {
449 VariationParams params;
450 if (!variations::GetVariationParams(kBundledExperimentFieldTrialName,
451 &params)) {
452 return std::string();
454 const std::string page_classification_str =
455 base::IntToString(static_cast<int>(page_classification));
456 const std::string instant_extended =
457 chrome::IsInstantExtendedAPIEnabled() ? "1" : "0";
458 // Look up rule in this exact context.
459 VariationParams::const_iterator it = params.find(
460 rule + ":" + page_classification_str + ":" + instant_extended);
461 if (it != params.end())
462 return it->second;
463 // Fall back to the global page classification context.
464 it = params.find(rule + ":*:" + instant_extended);
465 if (it != params.end())
466 return it->second;
467 // Fall back to the global instant extended context.
468 it = params.find(rule + ":" + page_classification_str + ":*");
469 if (it != params.end())
470 return it->second;
471 // Look up rule in the global context.
472 it = params.find(rule + ":*:*");
473 return (it != params.end()) ? it->second : std::string();