blob: 245a583599217181908538f678cc3a0cc0f19031 [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,
23 std::string* single_feature_name) {
[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 std::string single_feature_name_seen;
43 bool has_multiple_features = false;
44
[email protected]70fbd0052013-11-20 02:22:0645 std::set<std::string> experiment_names;
46 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
asvitkine64e9e112016-03-17 17:32:0058 if (!has_multiple_features) {
59 const auto& features = experiment.feature_association();
60 for (int i = 0; i < features.enable_feature_size(); ++i) {
61 const std::string& feature_name = features.enable_feature(i);
62 if (single_feature_name_seen.empty()) {
63 single_feature_name_seen = feature_name;
64 } else if (feature_name != single_feature_name_seen) {
65 has_multiple_features = true;
66 break;
67 }
68 }
69 for (int i = 0; i < features.disable_feature_size(); ++i) {
70 const std::string& feature_name = features.disable_feature(i);
71 if (single_feature_name_seen.empty()) {
72 single_feature_name_seen = feature_name;
73 } else if (feature_name != single_feature_name_seen) {
74 has_multiple_features = true;
75 break;
76 }
77 }
78 }
79
asvitkine30ac09132015-02-20 19:50:1080 if (!experiment.has_forcing_flag() && experiment.probability_weight() > 0) {
81 // If |divisor| is not 0, there was at least one prior non-zero group.
82 if (divisor != 0)
83 multiple_assigned_groups = true;
84 divisor += experiment.probability_weight();
85 }
[email protected]70fbd0052013-11-20 02:22:0686 if (study.experiment(i).name() == default_group_name)
87 found_default_group = true;
88 }
89
jwd5ff54102017-01-06 18:09:3490 // Specifying a default experiment is optional, so finding it in the
91 // experiment list is only required when it is specified.
92 if (!study.default_experiment_name().empty() && !found_default_group) {
93 DVLOG(1) << study.name() << " is missing default experiment ("
94 << study.default_experiment_name() << ") in its experiment list";
[email protected]70fbd0052013-11-20 02:22:0695 // The default group was not found in the list of groups. This study is not
96 // valid.
97 return false;
98 }
99
asvitkine64e9e112016-03-17 17:32:00100 if (!has_multiple_features && !single_feature_name_seen.empty())
101 single_feature_name->swap(single_feature_name_seen);
102 else
103 single_feature_name->clear();
104
[email protected]70fbd0052013-11-20 02:22:06105 *total_probability = divisor;
asvitkine30ac09132015-02-20 19:50:10106 *all_assignments_to_one_group = !multiple_assigned_groups;
[email protected]70fbd0052013-11-20 02:22:06107 return true;
108}
109
110
111} // namespace
112
jwd5ff54102017-01-06 18:09:34113// static
114const char ProcessedStudy::kGenericDefaultExperimentName[] =
115 "VariationsDefaultExperiment";
116
[email protected]70fbd0052013-11-20 02:22:06117ProcessedStudy::ProcessedStudy()
asvitkine30ac09132015-02-20 19:50:10118 : study_(NULL),
119 total_probability_(0),
120 all_assignments_to_one_group_(false),
121 is_expired_(false) {
[email protected]d4f84852013-11-08 01:05:35122}
123
124ProcessedStudy::~ProcessedStudy() {
125}
126
[email protected]70fbd0052013-11-20 02:22:06127bool ProcessedStudy::Init(const Study* study, bool is_expired) {
128 base::FieldTrial::Probability total_probability = 0;
asvitkine30ac09132015-02-20 19:50:10129 bool all_assignments_to_one_group = false;
asvitkine64e9e112016-03-17 17:32:00130 std::string single_feature_name;
asvitkine30ac09132015-02-20 19:50:10131 if (!ValidateStudyAndComputeTotalProbability(*study, &total_probability,
asvitkine64e9e112016-03-17 17:32:00132 &all_assignments_to_one_group,
133 &single_feature_name)) {
[email protected]70fbd0052013-11-20 02:22:06134 return false;
asvitkine30ac09132015-02-20 19:50:10135 }
[email protected]70fbd0052013-11-20 02:22:06136
137 study_ = study;
138 is_expired_ = is_expired;
139 total_probability_ = total_probability;
asvitkine30ac09132015-02-20 19:50:10140 all_assignments_to_one_group_ = all_assignments_to_one_group;
asvitkine64e9e112016-03-17 17:32:00141 single_feature_name_.swap(single_feature_name);
[email protected]70fbd0052013-11-20 02:22:06142 return true;
143}
144
[email protected]e24fff362014-07-22 01:19:02145int ProcessedStudy::GetExperimentIndexByName(const std::string& name) const {
146 for (int i = 0; i < study_->experiment_size(); ++i) {
147 if (study_->experiment(i).name() == name)
148 return i;
149 }
150
151 return -1;
152}
153
jwd5ff54102017-01-06 18:09:34154const char* ProcessedStudy::GetDefaultExperimentName() const {
155 if (study_->default_experiment_name().empty())
156 return kGenericDefaultExperimentName;
157
158 return study_->default_experiment_name().c_str();
159}
160
[email protected]70fbd0052013-11-20 02:22:06161// static
162bool ProcessedStudy::ValidateAndAppendStudy(
163 const Study* study,
164 bool is_expired,
165 std::vector<ProcessedStudy>* processed_studies) {
166 ProcessedStudy processed_study;
167 if (processed_study.Init(study, is_expired)) {
168 processed_studies->push_back(processed_study);
169 return true;
170 }
171 return false;
172}
173
[email protected]59b6f672014-07-26 18:35:47174} // namespace variations