| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/variations/variations_seed_processor.h" | 5 #include "components/variations/variations_seed_processor.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/feature_list.h" |
| 11 #include "base/metrics/field_trial.h" | 12 #include "base/metrics/field_trial.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "components/variations/processed_study.h" | 14 #include "components/variations/processed_study.h" |
| 14 #include "components/variations/study_filtering.h" | 15 #include "components/variations/study_filtering.h" |
| 15 #include "components/variations/variations_associated_data.h" | 16 #include "components/variations/variations_associated_data.h" |
| 16 | 17 |
| 17 namespace variations { | 18 namespace variations { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 void ApplyUIStringOverrides( | 73 void ApplyUIStringOverrides( |
| 73 const Study_Experiment& experiment, | 74 const Study_Experiment& experiment, |
| 74 const VariationsSeedProcessor::UIStringOverrideCallback& callback) { | 75 const VariationsSeedProcessor::UIStringOverrideCallback& callback) { |
| 75 for (int i = 0; i < experiment.override_ui_string_size(); ++i) { | 76 for (int i = 0; i < experiment.override_ui_string_size(); ++i) { |
| 76 const Study_Experiment_OverrideUIString& override = | 77 const Study_Experiment_OverrideUIString& override = |
| 77 experiment.override_ui_string(i); | 78 experiment.override_ui_string(i); |
| 78 callback.Run(override.name_hash(), base::UTF8ToUTF16(override.value())); | 79 callback.Run(override.name_hash(), base::UTF8ToUTF16(override.value())); |
| 79 } | 80 } |
| 80 } | 81 } |
| 81 | 82 |
| 83 // Forces the specified |experiment| to be enabled in |study|. |
| 84 void ForceExperimentState( |
| 85 const Study& study, |
| 86 const Study_Experiment& experiment, |
| 87 const VariationsSeedProcessor::UIStringOverrideCallback& override_callback, |
| 88 base::FieldTrial* trial) { |
| 89 RegisterExperimentParams(study, experiment); |
| 90 RegisterVariationIds(experiment, study.name()); |
| 91 if (study.activation_type() == Study_ActivationType_ACTIVATION_AUTO) { |
| 92 trial->group(); |
| 93 // UI Strings can only be overridden from ACTIVATION_AUTO experiments. |
| 94 ApplyUIStringOverrides(experiment, override_callback); |
| 95 } |
| 96 } |
| 97 |
| 98 // Registers feature overrides for the chosen experiment in the specified study. |
| 99 void RegisterFeatureOverrides(const ProcessedStudy& processed_study, |
| 100 base::FieldTrial* trial, |
| 101 base::FeatureList* feature_list) { |
| 102 const std::string& group_name = trial->GetGroupNameWithoutActivation(); |
| 103 int experiment_index = processed_study.GetExperimentIndexByName(group_name); |
| 104 // The field trial was defined from |study|, so the active experiment's name |
| 105 // must be in the |study|. |
| 106 DCHECK_NE(-1, experiment_index); |
| 107 |
| 108 const Study_Experiment& experiment = |
| 109 processed_study.study()->experiment(experiment_index); |
| 110 |
| 111 // Process all the features to enable. |
| 112 int feature_count = experiment.feature_association().enable_feature_size(); |
| 113 for (int i = 0; i < feature_count; ++i) { |
| 114 feature_list->RegisterFieldTrialOverride( |
| 115 experiment.feature_association().enable_feature(i), |
| 116 base::FeatureList::OVERRIDE_ENABLE_FEATURE, trial); |
| 117 } |
| 118 |
| 119 // Process all the features to disable. |
| 120 feature_count = experiment.feature_association().disable_feature_size(); |
| 121 for (int i = 0; i < feature_count; ++i) { |
| 122 feature_list->RegisterFieldTrialOverride( |
| 123 experiment.feature_association().disable_feature(i), |
| 124 base::FeatureList::OVERRIDE_DISABLE_FEATURE, trial); |
| 125 } |
| 126 } |
| 127 |
| 82 } // namespace | 128 } // namespace |
| 83 | 129 |
| 84 VariationsSeedProcessor::VariationsSeedProcessor() { | 130 VariationsSeedProcessor::VariationsSeedProcessor() { |
| 85 } | 131 } |
| 86 | 132 |
| 87 VariationsSeedProcessor::~VariationsSeedProcessor() { | 133 VariationsSeedProcessor::~VariationsSeedProcessor() { |
| 88 } | 134 } |
| 89 | 135 |
| 90 void VariationsSeedProcessor::CreateTrialsFromSeed( | 136 void VariationsSeedProcessor::CreateTrialsFromSeed( |
| 91 const VariationsSeed& seed, | 137 const VariationsSeed& seed, |
| 92 const std::string& locale, | 138 const std::string& locale, |
| 93 const base::Time& reference_date, | 139 const base::Time& reference_date, |
| 94 const base::Version& version, | 140 const base::Version& version, |
| 95 Study_Channel channel, | 141 Study_Channel channel, |
| 96 Study_FormFactor form_factor, | 142 Study_FormFactor form_factor, |
| 97 const std::string& hardware_class, | 143 const std::string& hardware_class, |
| 98 const std::string& session_consistency_country, | 144 const std::string& session_consistency_country, |
| 99 const std::string& permanent_consistency_country, | 145 const std::string& permanent_consistency_country, |
| 100 const UIStringOverrideCallback& override_callback) { | 146 const UIStringOverrideCallback& override_callback, |
| 147 base::FeatureList* feature_list) { |
| 101 std::vector<ProcessedStudy> filtered_studies; | 148 std::vector<ProcessedStudy> filtered_studies; |
| 102 FilterAndValidateStudies(seed, locale, reference_date, version, channel, | 149 FilterAndValidateStudies(seed, locale, reference_date, version, channel, |
| 103 form_factor, hardware_class, | 150 form_factor, hardware_class, |
| 104 session_consistency_country, | 151 session_consistency_country, |
| 105 permanent_consistency_country, &filtered_studies); | 152 permanent_consistency_country, &filtered_studies); |
| 106 | 153 |
| 107 for (size_t i = 0; i < filtered_studies.size(); ++i) | 154 for (size_t i = 0; i < filtered_studies.size(); ++i) |
| 108 CreateTrialFromStudy(filtered_studies[i], override_callback); | 155 CreateTrialFromStudy(filtered_studies[i], override_callback, feature_list); |
| 109 } | 156 } |
| 110 | 157 |
| 111 void VariationsSeedProcessor::CreateTrialFromStudy( | 158 void VariationsSeedProcessor::CreateTrialFromStudy( |
| 112 const ProcessedStudy& processed_study, | 159 const ProcessedStudy& processed_study, |
| 113 const UIStringOverrideCallback& override_callback) { | 160 const UIStringOverrideCallback& override_callback, |
| 161 base::FeatureList* feature_list) { |
| 114 const Study& study = *processed_study.study(); | 162 const Study& study = *processed_study.study(); |
| 115 | 163 |
| 116 // Check if any experiments need to be forced due to a command line | 164 // Check if any experiments need to be forced due to a command line |
| 117 // flag. Force the first experiment with an existing flag. | 165 // flag. Force the first experiment with an existing flag. |
| 118 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 166 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 119 for (int i = 0; i < study.experiment_size(); ++i) { | 167 for (int i = 0; i < study.experiment_size(); ++i) { |
| 120 const Study_Experiment& experiment = study.experiment(i); | 168 const Study_Experiment& experiment = study.experiment(i); |
| 121 if (experiment.has_forcing_flag() && | 169 if (experiment.has_forcing_flag() && |
| 122 command_line->HasSwitch(experiment.forcing_flag())) { | 170 command_line->HasSwitch(experiment.forcing_flag())) { |
| 123 scoped_refptr<base::FieldTrial> trial( | 171 base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial( |
| 124 base::FieldTrialList::CreateFieldTrial(study.name(), | 172 study.name(), experiment.name()); |
| 125 experiment.name())); | 173 // If |trial| is null, then there might already be a trial forced to a |
| 126 // If |trial| is NULL, then there might already be a trial forced to a | |
| 127 // different group (e.g. via --force-fieldtrials). Break out of the loop, | 174 // different group (e.g. via --force-fieldtrials). Break out of the loop, |
| 128 // but don't return, so that variation ids and params for the selected | 175 // but don't return, so that variation ids and params for the selected |
| 129 // group will still be picked up. | 176 // group will still be picked up. |
| 130 if (!trial.get()) | 177 if (!trial) |
| 131 break; | 178 break; |
| 132 | 179 |
| 133 RegisterExperimentParams(study, experiment); | 180 ForceExperimentState(study, experiment, override_callback, trial); |
| 134 RegisterVariationIds(experiment, study.name()); | 181 return; |
| 135 if (study.activation_type() == Study_ActivationType_ACTIVATION_AUTO) { | 182 } |
| 136 trial->group(); | |
| 137 // UI Strings can only be overridden from ACTIVATION_AUTO experiments. | |
| 138 ApplyUIStringOverrides(experiment, override_callback); | |
| 139 } | |
| 140 | 183 |
| 141 DVLOG(1) << "Trial " << study.name() << " forced by flag: " | 184 base::FieldTrial* trial; |
| 142 << experiment.forcing_flag(); | 185 if (experiment.feature_association().has_forcing_feature_on()) { |
| 186 trial = feature_list->AssociateReportingFieldTrial( |
| 187 experiment.feature_association().forcing_feature_on(), |
| 188 base::FeatureList::OVERRIDE_ENABLE_FEATURE, study.name(), |
| 189 experiment.name()); |
| 190 } else if (experiment.feature_association().has_forcing_feature_off()) { |
| 191 trial = feature_list->AssociateReportingFieldTrial( |
| 192 experiment.feature_association().forcing_feature_off(), |
| 193 base::FeatureList::OVERRIDE_DISABLE_FEATURE, study.name(), |
| 194 experiment.name()); |
| 195 } else { |
| 196 continue; |
| 197 } |
| 198 |
| 199 // If |trial| is null, then the specified feature has not been forced on or |
| 200 // off on the FeatureList via the command-line, so safe to skip this entry. |
| 201 if (trial) { |
| 202 // Otherwise, force this experiment state and we're done with this study. |
| 203 ForceExperimentState(study, experiment, override_callback, trial); |
| 143 return; | 204 return; |
| 144 } | 205 } |
| 145 } | 206 } |
| 146 | 207 |
| 147 uint32 randomization_seed = 0; | 208 uint32 randomization_seed = 0; |
| 148 base::FieldTrial::RandomizationType randomization_type = | 209 base::FieldTrial::RandomizationType randomization_type = |
| 149 base::FieldTrial::SESSION_RANDOMIZED; | 210 base::FieldTrial::SESSION_RANDOMIZED; |
| 150 if (study.has_consistency() && | 211 if (study.has_consistency() && |
| 151 study.consistency() == Study_Consistency_PERMANENT && | 212 study.consistency() == Study_Consistency_PERMANENT && |
| 152 // If all assignments are to a single group, no need to enable one time | 213 // If all assignments are to a single group, no need to enable one time |
| 153 // randomization (which is more expensive to compute), since the result | 214 // randomization (which is more expensive to compute), since the result |
| 154 // will be the same. | 215 // will be the same. |
| 155 !processed_study.all_assignments_to_one_group()) { | 216 !processed_study.all_assignments_to_one_group()) { |
| 156 randomization_type = base::FieldTrial::ONE_TIME_RANDOMIZED; | 217 randomization_type = base::FieldTrial::ONE_TIME_RANDOMIZED; |
| 157 if (study.has_randomization_seed()) | 218 if (study.has_randomization_seed()) |
| 158 randomization_seed = study.randomization_seed(); | 219 randomization_seed = study.randomization_seed(); |
| 159 } | 220 } |
| 160 | 221 |
| 161 // The trial is created without specifying an expiration date because the | 222 // The trial is created without specifying an expiration date because the |
| 162 // expiration check in field_trial.cc is based on the build date. Instead, | 223 // expiration check in field_trial.cc is based on the build date. Instead, |
| 163 // the expiration check using |reference_date| is done explicitly below. | 224 // the expiration check using |reference_date| is done explicitly below. |
| 164 scoped_refptr<base::FieldTrial> trial( | 225 scoped_refptr<base::FieldTrial> trial( |
| 165 base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( | 226 base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( |
| 166 study.name(), processed_study.total_probability(), | 227 study.name(), processed_study.total_probability(), |
| 167 study.default_experiment_name(), | 228 study.default_experiment_name(), |
| 168 base::FieldTrialList::kNoExpirationYear, 1, 1, randomization_type, | 229 base::FieldTrialList::kNoExpirationYear, 1, 1, randomization_type, |
| 169 randomization_seed, NULL)); | 230 randomization_seed, NULL)); |
| 170 | 231 |
| 171 bool has_overrides = false; | 232 bool has_overrides = false; |
| 233 bool controls_feature_state = false; |
| 172 for (int i = 0; i < study.experiment_size(); ++i) { | 234 for (int i = 0; i < study.experiment_size(); ++i) { |
| 173 const Study_Experiment& experiment = study.experiment(i); | 235 const Study_Experiment& experiment = study.experiment(i); |
| 174 RegisterExperimentParams(study, experiment); | 236 RegisterExperimentParams(study, experiment); |
| 175 | 237 |
| 176 // Groups with forcing flags have probability 0 and will never be selected. | 238 // Groups with forcing flags have probability 0 and will never be selected. |
| 177 // Therefore, there's no need to add them to the field trial. | 239 // Therefore, there's no need to add them to the field trial. |
| 178 if (experiment.has_forcing_flag()) | 240 if (experiment.has_forcing_flag() || |
| 241 experiment.feature_association().has_forcing_feature_on() || |
| 242 experiment.feature_association().has_forcing_feature_off()) { |
| 179 continue; | 243 continue; |
| 244 } |
| 180 | 245 |
| 181 if (experiment.name() != study.default_experiment_name()) | 246 if (experiment.name() != study.default_experiment_name()) |
| 182 trial->AppendGroup(experiment.name(), experiment.probability_weight()); | 247 trial->AppendGroup(experiment.name(), experiment.probability_weight()); |
| 183 | 248 |
| 184 RegisterVariationIds(experiment, study.name()); | 249 RegisterVariationIds(experiment, study.name()); |
| 185 | 250 |
| 186 has_overrides = has_overrides || experiment.override_ui_string_size() > 0; | 251 has_overrides = has_overrides || experiment.override_ui_string_size() > 0; |
| 252 if (experiment.feature_association().enable_feature_size() != 0 || |
| 253 experiment.feature_association().disable_feature_size() != 0) { |
| 254 controls_feature_state = true; |
| 255 } |
| 187 } | 256 } |
| 188 | 257 |
| 189 trial->SetForced(); | 258 trial->SetForced(); |
| 259 |
| 260 if (controls_feature_state) |
| 261 RegisterFeatureOverrides(processed_study, trial.get(), feature_list); |
| 262 |
| 190 if (processed_study.is_expired()) { | 263 if (processed_study.is_expired()) { |
| 191 trial->Disable(); | 264 trial->Disable(); |
| 192 } else if (study.activation_type() == Study_ActivationType_ACTIVATION_AUTO) { | 265 } else if (study.activation_type() == Study_ActivationType_ACTIVATION_AUTO) { |
| 193 const std::string& group_name = trial->group_name(); | 266 const std::string& group_name = trial->group_name(); |
| 194 | 267 |
| 195 // Don't try to apply overrides if none of the experiments in this study had | 268 // Don't try to apply overrides if none of the experiments in this study had |
| 196 // any. | 269 // any. |
| 197 if (!has_overrides) | 270 if (!has_overrides) |
| 198 return; | 271 return; |
| 199 | 272 |
| 200 // UI Strings can only be overridden from ACTIVATION_AUTO experiments. | 273 // UI Strings can only be overridden from ACTIVATION_AUTO experiments. |
| 201 int experiment_index = processed_study.GetExperimentIndexByName(group_name); | 274 int experiment_index = processed_study.GetExperimentIndexByName(group_name); |
| 202 | 275 |
| 203 // The field trial was defined from |study|, so the active experiment's name | 276 // The field trial was defined from |study|, so the active experiment's name |
| 204 // must be in the |study|. | 277 // must be in the |study|. |
| 205 DCHECK_NE(-1, experiment_index); | 278 DCHECK_NE(-1, experiment_index); |
| 206 | 279 |
| 207 ApplyUIStringOverrides(study.experiment(experiment_index), | 280 ApplyUIStringOverrides(study.experiment(experiment_index), |
| 208 override_callback); | 281 override_callback); |
| 209 } | 282 } |
| 210 } | 283 } |
| 211 | 284 |
| 212 } // namespace variations | 285 } // namespace variations |
| OLD | NEW |