[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, |
Alexei Svitkine | 5bbc915 | 2018-02-27 16:02:30 | [diff] [blame] | 23 | std::vector<std::string>* associated_features) { |
[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 | |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 43 | std::set<std::string> experiment_names; |
Alexei Svitkine | c808b53 | 2018-03-15 23:53:38 | [diff] [blame] | 44 | std::set<std::string> features_to_associate; |
| 45 | |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 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 | |
Steven Holte | 93e681c | 2019-01-30 00:33:01 | [diff] [blame] | 58 | // 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 Svitkine | c808b53 | 2018-03-15 23:53:38 | [diff] [blame] | 60 | // active), as well as allowing flexibility to disable this behavior in the |
| 61 | // future from the server by introducing a new activation type. |
Steven Holte | 93e681c | 2019-01-30 00:33:01 | [diff] [blame] | 62 | if (study.activation_type() == Study_ActivationType_ACTIVATE_ON_QUERY) { |
asvitkine | 64e9e11 | 2016-03-17 17:32:00 | [diff] [blame] | 63 | const auto& features = experiment.feature_association(); |
| 64 | for (int i = 0; i < features.enable_feature_size(); ++i) { |
Alexei Svitkine | c808b53 | 2018-03-15 23:53:38 | [diff] [blame] | 65 | features_to_associate.insert(features.enable_feature(i)); |
asvitkine | 64e9e11 | 2016-03-17 17:32:00 | [diff] [blame] | 66 | } |
| 67 | for (int i = 0; i < features.disable_feature_size(); ++i) { |
Alexei Svitkine | c808b53 | 2018-03-15 23:53:38 | [diff] [blame] | 68 | features_to_associate.insert(features.disable_feature(i)); |
asvitkine | 64e9e11 | 2016-03-17 17:32:00 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 72 | 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] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 78 | if (study.experiment(i).name() == default_group_name) |
| 79 | found_default_group = true; |
| 80 | } |
| 81 | |
jwd | 5ff5410 | 2017-01-06 18:09:34 | [diff] [blame] | 82 | // 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] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 87 | // The default group was not found in the list of groups. This study is not |
| 88 | // valid. |
| 89 | return false; |
| 90 | } |
| 91 | |
Ilya Sherman | 8e076b0a | 2018-04-10 23:24:08 | [diff] [blame] | 92 | // Ensure that groups that don't explicitly enable/disable any features get |
Alexei Svitkine | c808b53 | 2018-03-15 23:53:38 | [diff] [blame] | 93 | // 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 Svitkine | 5bbc915 | 2018-02-27 16:02:30 | [diff] [blame] | 99 | } |
asvitkine | 64e9e11 | 2016-03-17 17:32:00 | [diff] [blame] | 100 | |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 101 | *total_probability = divisor; |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 102 | *all_assignments_to_one_group = !multiple_assigned_groups; |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | } // namespace |
| 108 | |
jwd | 5ff5410 | 2017-01-06 18:09:34 | [diff] [blame] | 109 | // static |
| 110 | const char ProcessedStudy::kGenericDefaultExperimentName[] = |
| 111 | "VariationsDefaultExperiment"; |
| 112 | |
Alexei Svitkine | 5bbc915 | 2018-02-27 16:02:30 | [diff] [blame] | 113 | ProcessedStudy::ProcessedStudy() {} |
| 114 | |
| 115 | ProcessedStudy::ProcessedStudy(const ProcessedStudy& other) = default; |
[email protected] | d4f8485 | 2013-11-08 01:05:35 | [diff] [blame] | 116 | |
| 117 | ProcessedStudy::~ProcessedStudy() { |
| 118 | } |
| 119 | |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 120 | bool ProcessedStudy::Init(const Study* study, bool is_expired) { |
| 121 | base::FieldTrial::Probability total_probability = 0; |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 122 | bool all_assignments_to_one_group = false; |
Alexei Svitkine | 5bbc915 | 2018-02-27 16:02:30 | [diff] [blame] | 123 | std::vector<std::string> associated_features; |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 124 | if (!ValidateStudyAndComputeTotalProbability(*study, &total_probability, |
asvitkine | 64e9e11 | 2016-03-17 17:32:00 | [diff] [blame] | 125 | &all_assignments_to_one_group, |
Alexei Svitkine | 5bbc915 | 2018-02-27 16:02:30 | [diff] [blame] | 126 | &associated_features)) { |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 127 | return false; |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 128 | } |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 129 | |
| 130 | study_ = study; |
| 131 | is_expired_ = is_expired; |
| 132 | total_probability_ = total_probability; |
asvitkine | 30ac0913 | 2015-02-20 19:50:10 | [diff] [blame] | 133 | all_assignments_to_one_group_ = all_assignments_to_one_group; |
Alexei Svitkine | 5bbc915 | 2018-02-27 16:02:30 | [diff] [blame] | 134 | associated_features_.swap(associated_features); |
[email protected] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | |
[email protected] | e24fff36 | 2014-07-22 01:19:02 | [diff] [blame] | 138 | int 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 | |
jwd | 5ff5410 | 2017-01-06 18:09:34 | [diff] [blame] | 147 | const 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] | 70fbd005 | 2013-11-20 02:22:06 | [diff] [blame] | 154 | // static |
| 155 | bool 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] | 59b6f67 | 2014-07-26 18:35:47 | [diff] [blame] | 167 | } // namespace variations |