ExtensionInstallDialogView: fix scrolling behavior on Views (Win,Linux)
[chromium-blink-merge.git] / components / variations / variations_seed_processor.cc
blob750630ae04561f778ddff47f5644c173cd7c2d1d
1 // Copyright 2013 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/variations/variations_seed_processor.h"
7 #include <map>
8 #include <vector>
10 #include "base/command_line.h"
11 #include "base/metrics/field_trial.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "components/variations/processed_study.h"
14 #include "components/variations/study_filtering.h"
15 #include "components/variations/variations_associated_data.h"
17 namespace variations {
19 namespace {
21 // Associates the variations params of |experiment|, if present.
22 void RegisterExperimentParams(const Study& study,
23 const Study_Experiment& experiment) {
24 std::map<std::string, std::string> params;
25 for (int i = 0; i < experiment.param_size(); ++i) {
26 if (experiment.param(i).has_name() && experiment.param(i).has_value())
27 params[experiment.param(i).name()] = experiment.param(i).value();
29 if (!params.empty())
30 AssociateVariationParams(study.name(), experiment.name(), params);
33 // If there are variation ids associated with |experiment|, register the
34 // variation ids.
35 void RegisterVariationIds(const Study_Experiment& experiment,
36 const std::string& trial_name) {
37 if (experiment.has_google_web_experiment_id()) {
38 const VariationID variation_id =
39 static_cast<VariationID>(experiment.google_web_experiment_id());
40 AssociateGoogleVariationIDForce(GOOGLE_WEB_PROPERTIES,
41 trial_name,
42 experiment.name(),
43 variation_id);
45 if (experiment.has_google_web_trigger_experiment_id()) {
46 const VariationID variation_id =
47 static_cast<VariationID>(experiment.google_web_trigger_experiment_id());
48 AssociateGoogleVariationIDForce(GOOGLE_WEB_PROPERTIES_TRIGGER,
49 trial_name,
50 experiment.name(),
51 variation_id);
53 if (experiment.has_google_update_experiment_id()) {
54 const VariationID variation_id =
55 static_cast<VariationID>(experiment.google_update_experiment_id());
56 AssociateGoogleVariationIDForce(GOOGLE_UPDATE_SERVICE,
57 trial_name,
58 experiment.name(),
59 variation_id);
61 if (experiment.has_chrome_sync_experiment_id()) {
62 const VariationID variation_id =
63 static_cast<VariationID>(experiment.chrome_sync_experiment_id());
64 AssociateGoogleVariationIDForce(CHROME_SYNC_SERVICE,
65 trial_name,
66 experiment.name(),
67 variation_id);
71 // Executes |callback| on every override defined by |experiment|.
72 void ApplyUIStringOverrides(
73 const Study_Experiment& experiment,
74 const VariationsSeedProcessor::UIStringOverrideCallback& callback) {
75 for (int i = 0; i < experiment.override_ui_string_size(); ++i) {
76 const Study_Experiment_OverrideUIString& override =
77 experiment.override_ui_string(i);
78 callback.Run(override.name_hash(), base::UTF8ToUTF16(override.value()));
82 } // namespace
84 VariationsSeedProcessor::VariationsSeedProcessor() {
87 VariationsSeedProcessor::~VariationsSeedProcessor() {
90 void VariationsSeedProcessor::CreateTrialsFromSeed(
91 const VariationsSeed& seed,
92 const std::string& locale,
93 const base::Time& reference_date,
94 const base::Version& version,
95 Study_Channel channel,
96 Study_FormFactor form_factor,
97 const std::string& hardware_class,
98 const std::string& permanent_consistency_country,
99 const UIStringOverrideCallback& override_callback) {
100 std::vector<ProcessedStudy> filtered_studies;
101 FilterAndValidateStudies(seed, locale, reference_date, version, channel,
102 form_factor, hardware_class,
103 permanent_consistency_country, &filtered_studies);
105 for (size_t i = 0; i < filtered_studies.size(); ++i)
106 CreateTrialFromStudy(filtered_studies[i], override_callback);
109 void VariationsSeedProcessor::CreateTrialFromStudy(
110 const ProcessedStudy& processed_study,
111 const UIStringOverrideCallback& override_callback) {
112 const Study& study = *processed_study.study();
114 // Check if any experiments need to be forced due to a command line
115 // flag. Force the first experiment with an existing flag.
116 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
117 for (int i = 0; i < study.experiment_size(); ++i) {
118 const Study_Experiment& experiment = study.experiment(i);
119 if (experiment.has_forcing_flag() &&
120 command_line->HasSwitch(experiment.forcing_flag())) {
121 scoped_refptr<base::FieldTrial> trial(
122 base::FieldTrialList::CreateFieldTrial(study.name(),
123 experiment.name()));
124 // If |trial| is NULL, then there might already be a trial forced to a
125 // different group (e.g. via --force-fieldtrials). Break out of the loop,
126 // but don't return, so that variation ids and params for the selected
127 // group will still be picked up.
128 if (!trial.get())
129 break;
131 RegisterExperimentParams(study, experiment);
132 RegisterVariationIds(experiment, study.name());
133 if (study.activation_type() == Study_ActivationType_ACTIVATION_AUTO) {
134 trial->group();
135 // UI Strings can only be overridden from ACTIVATION_AUTO experiments.
136 ApplyUIStringOverrides(experiment, override_callback);
139 DVLOG(1) << "Trial " << study.name() << " forced by flag: "
140 << experiment.forcing_flag();
141 return;
145 uint32 randomization_seed = 0;
146 base::FieldTrial::RandomizationType randomization_type =
147 base::FieldTrial::SESSION_RANDOMIZED;
148 if (study.has_consistency() &&
149 study.consistency() == Study_Consistency_PERMANENT &&
150 // If all assignments are to a single group, no need to enable one time
151 // randomization (which is more expensive to compute), since the result
152 // will be the same.
153 !processed_study.all_assignments_to_one_group()) {
154 randomization_type = base::FieldTrial::ONE_TIME_RANDOMIZED;
155 if (study.has_randomization_seed())
156 randomization_seed = study.randomization_seed();
159 // The trial is created without specifying an expiration date because the
160 // expiration check in field_trial.cc is based on the build date. Instead,
161 // the expiration check using |reference_date| is done explicitly below.
162 scoped_refptr<base::FieldTrial> trial(
163 base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed(
164 study.name(), processed_study.total_probability(),
165 study.default_experiment_name(),
166 base::FieldTrialList::kNoExpirationYear, 1, 1, randomization_type,
167 randomization_seed, NULL));
169 bool has_overrides = false;
170 for (int i = 0; i < study.experiment_size(); ++i) {
171 const Study_Experiment& experiment = study.experiment(i);
172 RegisterExperimentParams(study, experiment);
174 // Groups with forcing flags have probability 0 and will never be selected.
175 // Therefore, there's no need to add them to the field trial.
176 if (experiment.has_forcing_flag())
177 continue;
179 if (experiment.name() != study.default_experiment_name())
180 trial->AppendGroup(experiment.name(), experiment.probability_weight());
182 RegisterVariationIds(experiment, study.name());
184 has_overrides = has_overrides || experiment.override_ui_string_size() > 0;
187 trial->SetForced();
188 if (processed_study.is_expired()) {
189 trial->Disable();
190 } else if (study.activation_type() == Study_ActivationType_ACTIVATION_AUTO) {
191 const std::string& group_name = trial->group_name();
193 // Don't try to apply overrides if none of the experiments in this study had
194 // any.
195 if (!has_overrides)
196 return;
198 // UI Strings can only be overridden from ACTIVATION_AUTO experiments.
199 int experiment_index = processed_study.GetExperimentIndexByName(group_name);
201 // The field trial was defined from |study|, so the active experiment's name
202 // must be in the |study|.
203 DCHECK_NE(-1, experiment_index);
205 ApplyUIStringOverrides(study.experiment(experiment_index),
206 override_callback);
210 } // namespace variations