blob: b8abc4386820dfcbc3cc53cf901e2bc7b965b9d0 [file] [log] [blame]
[email protected]d4f84852013-11-08 01:05:351// 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
5#include "components/variations/processed_study.h"
6
[email protected]70fbd0052013-11-20 02:22:067#include <set>
[email protected]e24fff362014-07-22 01:19:028#include <string>
[email protected]70fbd0052013-11-20 02:22:069
10#include "base/version.h"
[email protected]d4f84852013-11-08 01:05:3511#include "components/variations/proto/study.pb.h"
12
[email protected]59b6f672014-07-26 18:35:4713namespace variations {
[email protected]d4f84852013-11-08 01:05:3514
[email protected]70fbd0052013-11-20 02:22:0615namespace {
16
asvitkine30ac09132015-02-20 19:50:1017// Validates the sanity of |study| and computes the total probability and
18// whether all assignments are to a single group.
[email protected]70fbd0052013-11-20 02:22:0619bool ValidateStudyAndComputeTotalProbability(
20 const Study& study,
asvitkine30ac09132015-02-20 19:50:1021 base::FieldTrial::Probability* total_probability,
asvitkine64e9e112016-03-17 17:32:0022 bool* all_assignments_to_one_group,
Alexei Svitkine5bbc9152018-02-27 16:02:3023 std::vector<std::string>* associated_features) {
[email protected]70fbd0052013-11-20 02:22:0624 if (study.filter().has_min_version() &&
pwnalla9cfc0f2016-08-30 23:17:2325 !base::Version::IsValidWildcardString(study.filter().min_version())) {
[email protected]70fbd0052013-11-20 02:22:0626 DVLOG(1) << study.name() << " has invalid min version: "
27 << study.filter().min_version();
28 return false;
29 }
30 if (study.filter().has_max_version() &&
pwnalla9cfc0f2016-08-30 23:17:2331 !base::Version::IsValidWildcardString(study.filter().max_version())) {
[email protected]70fbd0052013-11-20 02:22:0632 DVLOG(1) << study.name() << " has invalid max version: "
33 << study.filter().max_version();
34 return false;
35 }
36
37 const std::string& default_group_name = study.default_experiment_name();
38 base::FieldTrial::Probability divisor = 0;
39
asvitkine30ac09132015-02-20 19:50:1040 bool multiple_assigned_groups = false;
[email protected]70fbd0052013-11-20 02:22:0641 bool found_default_group = false;
asvitkine64e9e112016-03-17 17:32:0042
[email protected]70fbd0052013-11-20 02:22:0643 std::set<std::string> experiment_names;
Alexei Svitkinec808b532018-03-15 23:53:3844 std::set<std::string> features_to_associate;
45
[email protected]70fbd0052013-11-20 02:22:0646 for (int i = 0; i < study.experiment_size(); ++i) {
asvitkine30ac09132015-02-20 19:50:1047 const Study_Experiment& experiment = study.experiment(i);
48 if (experiment.name().empty()) {
[email protected]70fbd0052013-11-20 02:22:0649 DVLOG(1) << study.name() << " is missing experiment " << i << " name";
50 return false;
51 }
asvitkine30ac09132015-02-20 19:50:1052 if (!experiment_names.insert(experiment.name()).second) {
[email protected]70fbd0052013-11-20 02:22:0653 DVLOG(1) << study.name() << " has a repeated experiment name "
54 << study.experiment(i).name();
55 return false;
56 }
57
Steven Holte93e681c2019-01-30 00:33:0158 // Note: This checks for ACTIVATE_ON_QUERY, since there is no reason to
59 // have this association with ACTIVATE_ON_STARTUP (where the trial starts
Alexei Svitkinec808b532018-03-15 23:53:3860 // active), as well as allowing flexibility to disable this behavior in the
61 // future from the server by introducing a new activation type.
Steven Holte93e681c2019-01-30 00:33:0162 if (study.activation_type() == Study_ActivationType_ACTIVATE_ON_QUERY) {
asvitkine64e9e112016-03-17 17:32:0063 const auto& features = experiment.feature_association();
64 for (int i = 0; i < features.enable_feature_size(); ++i) {
Alexei Svitkinec808b532018-03-15 23:53:3865 features_to_associate.insert(features.enable_feature(i));
asvitkine64e9e112016-03-17 17:32:0066 }
67 for (int i = 0; i < features.disable_feature_size(); ++i) {
Alexei Svitkinec808b532018-03-15 23:53:3868 features_to_associate.insert(features.disable_feature(i));
asvitkine64e9e112016-03-17 17:32:0069 }
70 }
71
asvitkine30ac09132015-02-20 19:50:1072 if (!experiment.has_forcing_flag() && experiment.probability_weight() > 0) {
73 // If |divisor| is not 0, there was at least one prior non-zero group.
74 if (divisor != 0)
75 multiple_assigned_groups = true;
76 divisor += experiment.probability_weight();
77 }
[email protected]70fbd0052013-11-20 02:22:0678 if (study.experiment(i).name() == default_group_name)
79 found_default_group = true;
80 }
81
jwd5ff54102017-01-06 18:09:3482 // Specifying a default experiment is optional, so finding it in the
83 // experiment list is only required when it is specified.
84 if (!study.default_experiment_name().empty() && !found_default_group) {
85 DVLOG(1) << study.name() << " is missing default experiment ("
86 << study.default_experiment_name() << ") in its experiment list";
[email protected]70fbd0052013-11-20 02:22:0687 // The default group was not found in the list of groups. This study is not
88 // valid.
89 return false;
90 }
91
Ilya Sherman8e076b0a2018-04-10 23:24:0892 // Ensure that groups that don't explicitly enable/disable any features get
Alexei Svitkinec808b532018-03-15 23:53:3893 // associated with all features in the study (i.e. so "Default" group gets
94 // reported).
95 if (!features_to_associate.empty()) {
96 associated_features->insert(associated_features->end(),
97 features_to_associate.begin(),
98 features_to_associate.end());
Alexei Svitkine5bbc9152018-02-27 16:02:3099 }
asvitkine64e9e112016-03-17 17:32:00100
[email protected]70fbd0052013-11-20 02:22:06101 *total_probability = divisor;
asvitkine30ac09132015-02-20 19:50:10102 *all_assignments_to_one_group = !multiple_assigned_groups;
[email protected]70fbd0052013-11-20 02:22:06103 return true;
104}
105
106
107} // namespace
108
jwd5ff54102017-01-06 18:09:34109// static
110const char ProcessedStudy::kGenericDefaultExperimentName[] =
111 "VariationsDefaultExperiment";
112
Alexei Svitkine5bbc9152018-02-27 16:02:30113ProcessedStudy::ProcessedStudy() {}
114
115ProcessedStudy::ProcessedStudy(const ProcessedStudy& other) = default;
[email protected]d4f84852013-11-08 01:05:35116
117ProcessedStudy::~ProcessedStudy() {
118}
119
[email protected]70fbd0052013-11-20 02:22:06120bool ProcessedStudy::Init(const Study* study, bool is_expired) {
121 base::FieldTrial::Probability total_probability = 0;
asvitkine30ac09132015-02-20 19:50:10122 bool all_assignments_to_one_group = false;
Alexei Svitkine5bbc9152018-02-27 16:02:30123 std::vector<std::string> associated_features;
asvitkine30ac09132015-02-20 19:50:10124 if (!ValidateStudyAndComputeTotalProbability(*study, &total_probability,
asvitkine64e9e112016-03-17 17:32:00125 &all_assignments_to_one_group,
Alexei Svitkine5bbc9152018-02-27 16:02:30126 &associated_features)) {
[email protected]70fbd0052013-11-20 02:22:06127 return false;
asvitkine30ac09132015-02-20 19:50:10128 }
[email protected]70fbd0052013-11-20 02:22:06129
130 study_ = study;
131 is_expired_ = is_expired;
132 total_probability_ = total_probability;
asvitkine30ac09132015-02-20 19:50:10133 all_assignments_to_one_group_ = all_assignments_to_one_group;
Alexei Svitkine5bbc9152018-02-27 16:02:30134 associated_features_.swap(associated_features);
[email protected]70fbd0052013-11-20 02:22:06135 return true;
136}
137
[email protected]e24fff362014-07-22 01:19:02138int ProcessedStudy::GetExperimentIndexByName(const std::string& name) const {
139 for (int i = 0; i < study_->experiment_size(); ++i) {
140 if (study_->experiment(i).name() == name)
141 return i;
142 }
143
144 return -1;
145}
146
jwd5ff54102017-01-06 18:09:34147const char* ProcessedStudy::GetDefaultExperimentName() const {
148 if (study_->default_experiment_name().empty())
149 return kGenericDefaultExperimentName;
150
151 return study_->default_experiment_name().c_str();
152}
153
[email protected]70fbd0052013-11-20 02:22:06154// static
155bool ProcessedStudy::ValidateAndAppendStudy(
156 const Study* study,
157 bool is_expired,
158 std::vector<ProcessedStudy>* processed_studies) {
159 ProcessedStudy processed_study;
160 if (processed_study.Init(study, is_expired)) {
161 processed_studies->push_back(processed_study);
162 return true;
163 }
164 return false;
165}
166
[email protected]59b6f672014-07-26 18:35:47167} // namespace variations