[email protected] | d4f8485 | 2013-11-08 01:05:35 | [diff] [blame] | 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 | |
| 5 | #include "components/variations/processed_study.h" |
| 6 | |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 7 | #include <set> |
[email protected] | e24fff36 | 2014-07-22 01:19:02 | [diff] [blame] | 8 | #include <string> |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 9 | |
| 10 | #include "base/version.h" |
[email protected] | d4f8485 | 2013-11-08 01:05:35 | [diff] [blame] | 11 | #include "components/variations/proto/study.pb.h" |
| 12 | |
[email protected] | 59b6f67 | 2014-07-26 18:35:47 | [diff] [blame] | 13 | namespace variations { |
[email protected] | d4f8485 | 2013-11-08 01:05:35 | [diff] [blame] | 14 | |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 15 | namespace { |
| 16 | |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 17 | // Validates the sanity of |study| and computes the total probability and |
| 18 | // whether all assignments are to a single group. |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 19 | bool ValidateStudyAndComputeTotalProbability( |
| 20 | const Study& study, |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 21 | base::FieldTrial::Probability* total_probability, |
asvitkine | 64e9e11 | 2016-03-17 17:32:00 | [diff] [blame] | 22 | bool* all_assignments_to_one_group, |
| 23 | std::string* single_feature_name) { |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 24 | if (study.filter().has_min_version() && |
pwnall | a9cfc0f | 2016-08-30 23:17:23 | [diff] [blame] | 25 | !base::Version::IsValidWildcardString(study.filter().min_version())) { |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 26 | DVLOG(1) << study.name() << " has invalid min version: " |
| 27 | << study.filter().min_version(); |
| 28 | return false; |
| 29 | } |
| 30 | if (study.filter().has_max_version() && |
pwnall | a9cfc0f | 2016-08-30 23:17:23 | [diff] [blame] | 31 | !base::Version::IsValidWildcardString(study.filter().max_version())) { |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 32 | 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 | |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 40 | bool multiple_assigned_groups = false; |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 41 | bool found_default_group = false; |
asvitkine | 64e9e11 | 2016-03-17 17:32:00 | [diff] [blame] | 42 | std::string single_feature_name_seen; |
| 43 | bool has_multiple_features = false; |
| 44 | |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 45 | std::set<std::string> experiment_names; |
| 46 | for (int i = 0; i < study.experiment_size(); ++i) { |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 47 | const Study_Experiment& experiment = study.experiment(i); |
| 48 | if (experiment.name().empty()) { |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 49 | DVLOG(1) << study.name() << " is missing experiment " << i << " name"; |
| 50 | return false; |
| 51 | } |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 52 | if (!experiment_names.insert(experiment.name()).second) { |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 53 | DVLOG(1) << study.name() << " has a repeated experiment name " |
| 54 | << study.experiment(i).name(); |
| 55 | return false; |
| 56 | } |
| 57 | |
asvitkine | 64e9e11 | 2016-03-17 17:32:00 | [diff] [blame] | 58 | 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 | |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 80 | 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] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 86 | if (study.experiment(i).name() == default_group_name) |
| 87 | found_default_group = true; |
| 88 | } |
| 89 | |
jwd | 5ff5410 | 2017-01-06 18:09:34 | [diff] [blame] | 90 | // 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] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 95 | // The default group was not found in the list of groups. This study is not |
| 96 | // valid. |
| 97 | return false; |
| 98 | } |
| 99 | |
asvitkine | 64e9e11 | 2016-03-17 17:32:00 | [diff] [blame] | 100 | 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] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 105 | *total_probability = divisor; |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 106 | *all_assignments_to_one_group = !multiple_assigned_groups; |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | } // namespace |
| 112 | |
jwd | 5ff5410 | 2017-01-06 18:09:34 | [diff] [blame] | 113 | // static |
| 114 | const char ProcessedStudy::kGenericDefaultExperimentName[] = |
| 115 | "VariationsDefaultExperiment"; |
| 116 | |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 117 | ProcessedStudy::ProcessedStudy() |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 118 | : study_(NULL), |
| 119 | total_probability_(0), |
| 120 | all_assignments_to_one_group_(false), |
| 121 | is_expired_(false) { |
[email protected] | d4f8485 | 2013-11-08 01:05:35 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | ProcessedStudy::~ProcessedStudy() { |
| 125 | } |
| 126 | |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 127 | bool ProcessedStudy::Init(const Study* study, bool is_expired) { |
| 128 | base::FieldTrial::Probability total_probability = 0; |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 129 | bool all_assignments_to_one_group = false; |
asvitkine | 64e9e11 | 2016-03-17 17:32:00 | [diff] [blame] | 130 | std::string single_feature_name; |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 131 | if (!ValidateStudyAndComputeTotalProbability(*study, &total_probability, |
asvitkine | 64e9e11 | 2016-03-17 17:32:00 | [diff] [blame] | 132 | &all_assignments_to_one_group, |
| 133 | &single_feature_name)) { |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 134 | return false; |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 135 | } |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 136 | |
| 137 | study_ = study; |
| 138 | is_expired_ = is_expired; |
| 139 | total_probability_ = total_probability; |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 140 | all_assignments_to_one_group_ = all_assignments_to_one_group; |
asvitkine | 64e9e11 | 2016-03-17 17:32:00 | [diff] [blame] | 141 | single_feature_name_.swap(single_feature_name); |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 142 | return true; |
| 143 | } |
| 144 | |
[email protected] | e24fff36 | 2014-07-22 01:19:02 | [diff] [blame] | 145 | int 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 | |
jwd | 5ff5410 | 2017-01-06 18:09:34 | [diff] [blame] | 154 | const 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] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 161 | // static |
| 162 | bool 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] | 59b6f67 | 2014-07-26 18:35:47 | [diff] [blame] | 174 | } // namespace variations |