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 
7 #include <set>
8 #include <string>
9 
10 #include "base/logging.h"
11 #include "base/version.h"
12 #include "components/variations/proto/study.pb.h"
13 
14 namespace variations {
15 
16 namespace {
17 
18 // Validates the sanity of |study| and computes the total probability and
19 // whether all assignments are to a single group.
ValidateStudyAndComputeTotalProbability(const Study & study,base::FieldTrial::Probability * total_probability,bool * all_assignments_to_one_group,std::vector<std::string> * associated_features)20 bool ValidateStudyAndComputeTotalProbability(
21     const Study& study,
22     base::FieldTrial::Probability* total_probability,
23     bool* all_assignments_to_one_group,
24     std::vector<std::string>* associated_features) {
25   if (study.filter().has_min_version() &&
26       !base::Version::IsValidWildcardString(study.filter().min_version())) {
27     DVLOG(1) << study.name() << " has invalid min version: "
28              << study.filter().min_version();
29     return false;
30   }
31   if (study.filter().has_max_version() &&
32       !base::Version::IsValidWildcardString(study.filter().max_version())) {
33     DVLOG(1) << study.name() << " has invalid max version: "
34              << study.filter().max_version();
35     return false;
36   }
37 
38   const std::string& default_group_name = study.default_experiment_name();
39   base::FieldTrial::Probability divisor = 0;
40 
41   bool multiple_assigned_groups = false;
42   bool found_default_group = false;
43 
44   std::set<std::string> experiment_names;
45   std::set<std::string> features_to_associate;
46 
47   for (int i = 0; i < study.experiment_size(); ++i) {
48     const Study_Experiment& experiment = study.experiment(i);
49     if (experiment.name().empty()) {
50       DVLOG(1) << study.name() << " is missing experiment " << i << " name";
51       return false;
52     }
53     if (!experiment_names.insert(experiment.name()).second) {
54       DVLOG(1) << study.name() << " has a repeated experiment name "
55                << study.experiment(i).name();
56       return false;
57     }
58 
59     // Note: This checks for ACTIVATE_ON_QUERY, since there is no reason to
60     // have this association with ACTIVATE_ON_STARTUP (where the trial starts
61     // active), as well as allowing flexibility to disable this behavior in the
62     // future from the server by introducing a new activation type.
63     if (study.activation_type() == Study_ActivationType_ACTIVATE_ON_QUERY) {
64       const auto& features = experiment.feature_association();
65       for (int i = 0; i < features.enable_feature_size(); ++i) {
66         features_to_associate.insert(features.enable_feature(i));
67       }
68       for (int i = 0; i < features.disable_feature_size(); ++i) {
69         features_to_associate.insert(features.disable_feature(i));
70       }
71     }
72 
73     if (!experiment.has_forcing_flag() && experiment.probability_weight() > 0) {
74       // If |divisor| is not 0, there was at least one prior non-zero group.
75       if (divisor != 0)
76         multiple_assigned_groups = true;
77       divisor += experiment.probability_weight();
78     }
79     if (study.experiment(i).name() == default_group_name)
80       found_default_group = true;
81   }
82 
83   // Specifying a default experiment is optional, so finding it in the
84   // experiment list is only required when it is specified.
85   if (!study.default_experiment_name().empty() && !found_default_group) {
86     DVLOG(1) << study.name() << " is missing default experiment ("
87              << study.default_experiment_name() << ") in its experiment list";
88     // The default group was not found in the list of groups. This study is not
89     // valid.
90     return false;
91   }
92 
93   // Ensure that groups that don't explicitly enable/disable any features get
94   // associated with all features in the study (i.e. so "Default" group gets
95   // reported).
96   if (!features_to_associate.empty()) {
97     associated_features->insert(associated_features->end(),
98                                 features_to_associate.begin(),
99                                 features_to_associate.end());
100   }
101 
102   *total_probability = divisor;
103   *all_assignments_to_one_group = !multiple_assigned_groups;
104   return true;
105 }
106 
107 
108 }  // namespace
109 
110 // static
111 const char ProcessedStudy::kGenericDefaultExperimentName[] =
112     "VariationsDefaultExperiment";
113 
ProcessedStudy()114 ProcessedStudy::ProcessedStudy() {}
115 
116 ProcessedStudy::ProcessedStudy(const ProcessedStudy& other) = default;
117 
~ProcessedStudy()118 ProcessedStudy::~ProcessedStudy() {
119 }
120 
Init(const Study * study,bool is_expired)121 bool ProcessedStudy::Init(const Study* study, bool is_expired) {
122   base::FieldTrial::Probability total_probability = 0;
123   bool all_assignments_to_one_group = false;
124   std::vector<std::string> associated_features;
125   if (!ValidateStudyAndComputeTotalProbability(*study, &total_probability,
126                                                &all_assignments_to_one_group,
127                                                &associated_features)) {
128     return false;
129   }
130 
131   study_ = study;
132   is_expired_ = is_expired;
133   total_probability_ = total_probability;
134   all_assignments_to_one_group_ = all_assignments_to_one_group;
135   associated_features_.swap(associated_features);
136   return true;
137 }
138 
GetExperimentIndexByName(const std::string & name) const139 int ProcessedStudy::GetExperimentIndexByName(const std::string& name) const {
140   for (int i = 0; i < study_->experiment_size(); ++i) {
141     if (study_->experiment(i).name() == name)
142       return i;
143   }
144 
145   return -1;
146 }
147 
GetDefaultExperimentName() const148 const char* ProcessedStudy::GetDefaultExperimentName() const {
149   if (study_->default_experiment_name().empty())
150     return kGenericDefaultExperimentName;
151 
152   return study_->default_experiment_name().c_str();
153 }
154 
155 // static
ValidateAndAppendStudy(const Study * study,bool is_expired,std::vector<ProcessedStudy> * processed_studies)156 bool ProcessedStudy::ValidateAndAppendStudy(
157     const Study* study,
158     bool is_expired,
159     std::vector<ProcessedStudy>* processed_studies) {
160   ProcessedStudy processed_study;
161   if (processed_study.Init(study, is_expired)) {
162     processed_studies->push_back(processed_study);
163     return true;
164   }
165   return false;
166 }
167 
168 }  // namespace variations
169