variations_seed_processor.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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.
  4. #include "components/variations/variations_seed_processor.h"
  5. #include <stddef.h>
  6. #include <map>
  7. #include <vector>
  8. #include "base/command_line.h"
  9. #include "base/feature_list.h"
  10. #include "base/metrics/field_trial.h"
  11. #include "base/metrics/histogram_functions.h"
  12. #include "base/metrics/histogram_macros.h"
  13. #include "base/strings/utf_string_conversions.h"
  14. #include "components/variations/client_filterable_state.h"
  15. #include "components/variations/processed_study.h"
  16. #include "components/variations/study_filtering.h"
  17. #include "components/variations/variations_associated_data.h"
  18. #include "components/variations/variations_layers.h"
  19. #include "third_party/abseil-cpp/absl/types/optional.h"
  20. namespace variations {
  21. namespace {
  22. // Associates the variations params of |experiment|, if present.
  23. void RegisterExperimentParams(const Study& study,
  24. const Study::Experiment& experiment) {
  25. std::map<std::string, std::string> params;
  26. for (int i = 0; i < experiment.param_size(); ++i) {
  27. if (experiment.param(i).has_name() && experiment.param(i).has_value())
  28. params[experiment.param(i).name()] = experiment.param(i).value();
  29. }
  30. if (!params.empty())
  31. AssociateVariationParams(study.name(), experiment.name(), params);
  32. }
  33. // Returns the IDCollectionKey with which |experiment| should be associated.
  34. // Returns nullopt when |experiment| doesn't have a Google web or Google web
  35. // trigger experiment ID.
  36. absl::optional<IDCollectionKey> GetKeyForWebExperiment(
  37. const Study::Experiment& experiment) {
  38. bool has_web_experiment_id = experiment.has_google_web_experiment_id();
  39. bool has_web_trigger_experiment_id =
  40. experiment.has_google_web_trigger_experiment_id();
  41. if (!has_web_experiment_id && !has_web_trigger_experiment_id)
  42. return absl::nullopt;
  43. // An experiment cannot have both |google_web_experiment_id| and
  44. // |google_trigger_web_experiment_id|. This is enforced by the variations
  45. // server before generating a variations seed.
  46. DCHECK(!(has_web_experiment_id && has_web_trigger_experiment_id));
  47. Study::GoogleWebVisibility visibility = experiment.google_web_visibility();
  48. if (visibility == Study::FIRST_PARTY) {
  49. return has_web_trigger_experiment_id
  50. ? GOOGLE_WEB_PROPERTIES_TRIGGER_FIRST_PARTY
  51. : GOOGLE_WEB_PROPERTIES_FIRST_PARTY;
  52. }
  53. return has_web_trigger_experiment_id
  54. ? GOOGLE_WEB_PROPERTIES_TRIGGER_ANY_CONTEXT
  55. : GOOGLE_WEB_PROPERTIES_ANY_CONTEXT;
  56. }
  57. // If there are VariationIDs associated with |experiment|, register the
  58. // VariationIDs.
  59. void RegisterVariationIds(const Study::Experiment& experiment,
  60. const std::string& trial_name) {
  61. if (experiment.has_google_app_experiment_id()) {
  62. const VariationID variation_id =
  63. static_cast<VariationID>(experiment.google_app_experiment_id());
  64. AssociateGoogleVariationIDForce(GOOGLE_APP, trial_name, experiment.name(),
  65. variation_id);
  66. }
  67. absl::optional<IDCollectionKey> key = GetKeyForWebExperiment(experiment);
  68. if (!key.has_value())
  69. return;
  70. // An experiment cannot have both |google_web_experiment_id| and
  71. // |google_trigger_web_experiment_id|. See GetKeyForWebExperiment() for more
  72. // details.
  73. const VariationID variation_id =
  74. experiment.has_google_web_trigger_experiment_id()
  75. ? static_cast<VariationID>(
  76. experiment.google_web_trigger_experiment_id())
  77. : static_cast<VariationID>(experiment.google_web_experiment_id());
  78. AssociateGoogleVariationIDForce(key.value(), trial_name, experiment.name(),
  79. variation_id);
  80. }
  81. // Executes |callback| on every override defined by |experiment|.
  82. void ApplyUIStringOverrides(
  83. const Study::Experiment& experiment,
  84. const VariationsSeedProcessor::UIStringOverrideCallback& callback) {
  85. UMA_HISTOGRAM_COUNTS_100("Variations.StringsOverridden",
  86. experiment.override_ui_string_size());
  87. for (int i = 0; i < experiment.override_ui_string_size(); ++i) {
  88. const Study::Experiment::OverrideUIString& override =
  89. experiment.override_ui_string(i);
  90. callback.Run(override.name_hash(), base::UTF8ToUTF16(override.value()));
  91. }
  92. }
  93. // Forces the specified |experiment| to be enabled in |study|.
  94. void ForceExperimentState(
  95. const Study& study,
  96. const Study::Experiment& experiment,
  97. const VariationsSeedProcessor::UIStringOverrideCallback& override_callback,
  98. base::FieldTrial* trial) {
  99. RegisterExperimentParams(study, experiment);
  100. RegisterVariationIds(experiment, study.name());
  101. if (study.activation_type() == Study::ACTIVATE_ON_STARTUP) {
  102. // This call must happen after all params have been registered for the
  103. // trial. Otherwise, since we look up params by trial and group name, the
  104. // params won't be registered under the correct key.
  105. trial->group();
  106. // UI Strings can only be overridden from ACTIVATE_ON_STARTUP experiments.
  107. ApplyUIStringOverrides(experiment, override_callback);
  108. }
  109. }
  110. // Registers feature overrides for the chosen experiment in the specified study.
  111. void RegisterFeatureOverrides(const ProcessedStudy& processed_study,
  112. base::FieldTrial* trial,
  113. base::FeatureList* feature_list) {
  114. const std::string& group_name = trial->GetGroupNameWithoutActivation();
  115. int experiment_index = processed_study.GetExperimentIndexByName(group_name);
  116. // If the chosen experiment was not found in the study, simply return.
  117. // Although not normally expected, but could happen in exception cases, see
  118. // tests: ExpiredStudy_NoDefaultGroup, ExistingFieldTrial_ExpiredByConfig
  119. if (experiment_index == -1)
  120. return;
  121. const Study& study = *processed_study.study();
  122. const Study::Experiment& experiment = study.experiment(experiment_index);
  123. // Process all the features to enable.
  124. int feature_count = experiment.feature_association().enable_feature_size();
  125. for (int i = 0; i < feature_count; ++i) {
  126. feature_list->RegisterFieldTrialOverride(
  127. experiment.feature_association().enable_feature(i),
  128. base::FeatureList::OVERRIDE_ENABLE_FEATURE, trial);
  129. }
  130. // Process all the features to disable.
  131. feature_count = experiment.feature_association().disable_feature_size();
  132. for (int i = 0; i < feature_count; ++i) {
  133. feature_list->RegisterFieldTrialOverride(
  134. experiment.feature_association().disable_feature(i),
  135. base::FeatureList::OVERRIDE_DISABLE_FEATURE, trial);
  136. }
  137. // Associate features for groups that do not specify them manually (e.g.
  138. // "Default" group), so that such groups are reported.
  139. if (!experiment.has_feature_association()) {
  140. for (const auto& feature_name : processed_study.associated_features()) {
  141. feature_list->RegisterFieldTrialOverride(
  142. feature_name, base::FeatureList::OVERRIDE_USE_DEFAULT, trial);
  143. }
  144. }
  145. }
  146. // Checks if |experiment| is associated with a forcing flag or feature and if it
  147. // is, returns whether it should be forced enabled based on the |command_line|
  148. // or |feature_list| state.
  149. bool ShouldForceExperiment(const Study::Experiment& experiment,
  150. const base::CommandLine& command_line,
  151. const base::FeatureList& feature_list) {
  152. if (experiment.feature_association().has_forcing_feature_on()) {
  153. return feature_list.IsFeatureOverriddenFromCommandLine(
  154. experiment.feature_association().forcing_feature_on(),
  155. base::FeatureList::OVERRIDE_ENABLE_FEATURE);
  156. }
  157. if (experiment.feature_association().has_forcing_feature_off()) {
  158. return feature_list.IsFeatureOverriddenFromCommandLine(
  159. experiment.feature_association().forcing_feature_off(),
  160. base::FeatureList::OVERRIDE_DISABLE_FEATURE);
  161. }
  162. if (experiment.has_forcing_flag())
  163. return command_line.HasSwitch(experiment.forcing_flag());
  164. return false;
  165. }
  166. } // namespace
  167. VariationsSeedProcessor::VariationsSeedProcessor() = default;
  168. VariationsSeedProcessor::~VariationsSeedProcessor() = default;
  169. void VariationsSeedProcessor::CreateTrialsFromSeed(
  170. const VariationsSeed& seed,
  171. const ClientFilterableState& client_state,
  172. const UIStringOverrideCallback& override_callback,
  173. const base::FieldTrial::EntropyProvider* low_entropy_provider,
  174. base::FeatureList* feature_list) {
  175. base::UmaHistogramCounts1000("Variations.AppliedSeed.StudyCount",
  176. seed.study().size());
  177. std::vector<ProcessedStudy> filtered_studies;
  178. VariationsLayers layers(seed, low_entropy_provider);
  179. FilterAndValidateStudies(seed, client_state, layers, &filtered_studies);
  180. SetSeedVersion(seed.version());
  181. for (const ProcessedStudy& study : filtered_studies) {
  182. CreateTrialFromStudy(study, override_callback, low_entropy_provider,
  183. feature_list);
  184. }
  185. }
  186. // static
  187. bool VariationsSeedProcessor::ShouldStudyUseLowEntropy(const Study& study) {
  188. // This should be kept in sync with the server-side layer validation
  189. // code: https://go/chrome-variations-layer-validation
  190. for (int i = 0; i < study.experiment_size(); ++i) {
  191. const Study::Experiment& experiment = study.experiment(i);
  192. if (experiment.has_google_web_experiment_id() ||
  193. experiment.has_google_web_trigger_experiment_id() ||
  194. experiment.has_chrome_sync_experiment_id()) {
  195. return true;
  196. }
  197. }
  198. return false;
  199. }
  200. void VariationsSeedProcessor::CreateTrialFromStudy(
  201. const ProcessedStudy& processed_study,
  202. const UIStringOverrideCallback& override_callback,
  203. const base::FieldTrial::EntropyProvider* low_entropy_provider,
  204. base::FeatureList* feature_list) {
  205. const Study& study = *processed_study.study();
  206. // If the trial already exists, check if the selected group exists in the
  207. // |processed_study|. If not, there is nothing to do here.
  208. base::FieldTrial* existing_trial = base::FieldTrialList::Find(study.name());
  209. if (existing_trial) {
  210. int experiment_index = processed_study.GetExperimentIndexByName(
  211. existing_trial->GetGroupNameWithoutActivation());
  212. if (experiment_index == -1)
  213. return;
  214. }
  215. // Check if any experiments need to be forced due to a command line
  216. // flag. Force the first experiment with an existing flag.
  217. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  218. for (int i = 0; i < study.experiment_size(); ++i) {
  219. const Study::Experiment& experiment = study.experiment(i);
  220. if (ShouldForceExperiment(experiment, *command_line, *feature_list)) {
  221. base::FieldTrial* trial = base::FieldTrialList::CreateFieldTrial(
  222. study.name(), experiment.name());
  223. // If |trial| is null, then there might already be a trial forced to a
  224. // different group (e.g. via --force-fieldtrials). Break out of the loop,
  225. // but don't return, so that variation ids and params for the selected
  226. // group will still be picked up.
  227. if (!trial)
  228. break;
  229. if (experiment.feature_association().has_forcing_feature_on()) {
  230. feature_list->AssociateReportingFieldTrial(
  231. experiment.feature_association().forcing_feature_on(),
  232. base::FeatureList::OVERRIDE_ENABLE_FEATURE, trial);
  233. } else if (experiment.feature_association().has_forcing_feature_off()) {
  234. feature_list->AssociateReportingFieldTrial(
  235. experiment.feature_association().forcing_feature_off(),
  236. base::FeatureList::OVERRIDE_DISABLE_FEATURE, trial);
  237. }
  238. ForceExperimentState(study, experiment, override_callback, trial);
  239. return;
  240. }
  241. }
  242. // This study has no randomized experiments and none of its experiments were
  243. // forced by flags so don't create a field trial.
  244. if (processed_study.total_probability() <= 0)
  245. return;
  246. uint32_t randomization_seed = 0;
  247. base::FieldTrial::RandomizationType randomization_type =
  248. base::FieldTrial::SESSION_RANDOMIZED;
  249. if (study.has_consistency() &&
  250. study.consistency() == Study_Consistency_PERMANENT &&
  251. // If all assignments are to a single group, no need to enable one time
  252. // randomization (which is more expensive to compute), since the result
  253. // will be the same.
  254. !processed_study.all_assignments_to_one_group()) {
  255. randomization_type = base::FieldTrial::ONE_TIME_RANDOMIZED;
  256. if (study.has_randomization_seed())
  257. randomization_seed = study.randomization_seed();
  258. }
  259. // The trial is created without specifying an expiration date because the
  260. // expiration check in field_trial.cc is based on the build date. Instead,
  261. // the expiration check using |reference_date| is done explicitly below.
  262. scoped_refptr<base::FieldTrial> trial(
  263. base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed(
  264. study.name(), processed_study.total_probability(),
  265. processed_study.GetDefaultExperimentName(), randomization_type,
  266. randomization_seed, nullptr,
  267. ShouldStudyUseLowEntropy(study) ? low_entropy_provider : nullptr));
  268. bool has_overrides = false;
  269. bool enables_or_disables_features = false;
  270. for (int i = 0; i < study.experiment_size(); ++i) {
  271. const Study::Experiment& experiment = study.experiment(i);
  272. RegisterExperimentParams(study, experiment);
  273. // Groups with forcing flags have probability 0 and will never be selected.
  274. // Therefore, there's no need to add them to the field trial.
  275. if (experiment.has_forcing_flag() ||
  276. experiment.feature_association().has_forcing_feature_on() ||
  277. experiment.feature_association().has_forcing_feature_off()) {
  278. continue;
  279. }
  280. if (experiment.name() != study.default_experiment_name())
  281. trial->AppendGroup(experiment.name(), experiment.probability_weight());
  282. RegisterVariationIds(experiment, study.name());
  283. has_overrides = has_overrides || experiment.override_ui_string_size() > 0;
  284. if (experiment.feature_association().enable_feature_size() != 0 ||
  285. experiment.feature_association().disable_feature_size() != 0) {
  286. enables_or_disables_features = true;
  287. }
  288. }
  289. trial->SetForced();
  290. if (processed_study.is_expired())
  291. trial->Disable();
  292. if (enables_or_disables_features)
  293. RegisterFeatureOverrides(processed_study, trial.get(), feature_list);
  294. if (study.activation_type() == Study::ACTIVATE_ON_STARTUP) {
  295. // This call must happen after all params have been registered for the
  296. // trial. Otherwise, since we look up params by trial and group name, the
  297. // params won't be registered under the correct key.
  298. const std::string& group_name = trial->group_name();
  299. // Don't try to apply overrides if none of the experiments in this study had
  300. // any.
  301. if (!has_overrides)
  302. return;
  303. // UI Strings can only be overridden from ACTIVATE_ON_STARTUP experiments.
  304. int experiment_index = processed_study.GetExperimentIndexByName(group_name);
  305. // If the chosen experiment was not found in the study, simply return.
  306. // Although not normally expected, but could happen in exception cases, see
  307. // tests: ExpiredStudy_NoDefaultGroup, ExistingFieldTrial_ExpiredByConfig
  308. if (experiment_index != -1) {
  309. ApplyUIStringOverrides(study.experiment(experiment_index),
  310. override_callback);
  311. }
  312. }
  313. }
  314. } // namespace variations