File: processed_study.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (255 lines) | stat: -rw-r--r-- 9,591 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/variations/processed_study.h"

#include <cstdint>
#include <set>
#include <string>
#include <string_view>

#include "base/containers/contains.h"
#include "base/logging.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_util.h"
#include "base/version.h"
#include "components/variations/proto/study.pb.h"
#include "entropy_provider.h"
#include "variations_layers.h"

namespace variations {
namespace {

void LogInvalidReason(InvalidStudyReason reason) {
  base::UmaHistogramEnumeration("Variations.InvalidStudyReason", reason);
}

// TODO(crbug.com/41449497): Use
// base::FeatureList::IsValidFeatureOrFieldTrialName once WebRTC trials with ","
// in group names are removed.
bool IsValidExperimentName(const std::string& name) {
  return base::IsStringASCII(name) &&
         name.find_first_of("<*") == std::string::npos;
}

// Validates the sanity of |study| and computes the total probability and
// whether all assignments are to a single group.
bool ValidateStudyAndComputeTotalProbability(
    const Study& study,
    base::FieldTrial::Probability* total_probability,
    bool* all_assignments_to_one_group) {
  if (study.name().empty()) {
    LogInvalidReason(InvalidStudyReason::kBlankStudyName);
    DVLOG(1) << "study with missing study name";
    return false;
  }

  if (!base::FeatureList::IsValidFeatureOrFieldTrialName(study.name())) {
    LogInvalidReason(InvalidStudyReason::kInvalidStudyName);
    DVLOG(1) << study.name() << " is an invalid experiment name";
    return false;
  }

  if (study.filter().has_min_version() &&
      !base::Version::IsValidWildcardString(study.filter().min_version())) {
    LogInvalidReason(InvalidStudyReason::kInvalidMinVersion);
    DVLOG(1) << study.name()
             << " has invalid min version: " << study.filter().min_version();
    return false;
  }
  if (study.filter().has_max_version() &&
      !base::Version::IsValidWildcardString(study.filter().max_version())) {
    LogInvalidReason(InvalidStudyReason::kInvalidMaxVersion);
    DVLOG(1) << study.name()
             << " has invalid max version: " << study.filter().max_version();
    return false;
  }
  if (study.filter().has_min_os_version() &&
      !base::Version::IsValidWildcardString(study.filter().min_os_version())) {
    LogInvalidReason(InvalidStudyReason::kInvalidMinOsVersion);
    DVLOG(1) << study.name() << " has invalid min os version: "
             << study.filter().min_os_version();
    return false;
  }
  if (study.filter().has_max_os_version() &&
      !base::Version::IsValidWildcardString(study.filter().max_os_version())) {
    LogInvalidReason(InvalidStudyReason::kInvalidMaxOsVersion);
    DVLOG(1) << study.name() << " has invalid max os version: "
             << study.filter().max_os_version();
    return false;
  }

  // Validate experiment names
  {
    std::set<std::string> experiment_names;
    for (const auto& experiment : study.experiment()) {
      if (experiment.name().empty()) {
        LogInvalidReason(InvalidStudyReason::kMissingExperimentName);
        DVLOG(1) << study.name() << " is missing an experiment name";
        return false;
      }
      if (!IsValidExperimentName(experiment.name())) {
        LogInvalidReason(InvalidStudyReason::kInvalidExperimentName);
        DVLOG(1) << study.name() << " has a invalid experiment name "
                 << experiment.name();
        return false;
      }
      if (!experiment_names.insert(experiment.name()).second) {
        LogInvalidReason(InvalidStudyReason::kRepeatedExperimentName);
        DVLOG(1) << study.name() << " has a repeated experiment name "
                 << experiment.name();
        return false;
      }
    }

    // Specifying a default experiment is optional, so finding it in the
    // experiment list is only required when it is specified.
    if (!study.default_experiment_name().empty() &&
        !base::Contains(experiment_names, study.default_experiment_name())) {
      LogInvalidReason(InvalidStudyReason::kMissingDefaultExperimentInList);
      DVLOG(1) << study.name() << " is missing default experiment ("
               << study.default_experiment_name() << ") in its experiment list";
      // The default group was not found in the list of groups. This study is
      // not valid.
      return false;
    }
  }

  for (const auto& experiment : study.experiment()) {
    if (!experiment.has_feature_association())
      continue;
    for (const auto& feature :
         experiment.feature_association().enable_feature()) {
      if (!base::FeatureList::IsValidFeatureOrFieldTrialName(feature)) {
        LogInvalidReason(InvalidStudyReason::kInvalidFeatureName);
        DVLOG(1) << study.name() << " has a feature experiment name "
                 << feature;
        return false;
      }
    }
    for (const auto& feature :
         experiment.feature_association().disable_feature()) {
      if (!base::FeatureList::IsValidFeatureOrFieldTrialName(feature)) {
        LogInvalidReason(InvalidStudyReason::kInvalidFeatureName);
        DVLOG(1) << study.name() << " has a feature experiment name "
                 << feature;
        return false;
      }
    }
    if (!base::FeatureList::IsValidFeatureOrFieldTrialName(
            experiment.feature_association().forcing_feature_on())) {
      LogInvalidReason(InvalidStudyReason::kInvalidFeatureName);
      DVLOG(1) << study.name() << " has a feature experiment name "
               << experiment.feature_association().forcing_feature_on();
      return false;
    }
    if (!base::FeatureList::IsValidFeatureOrFieldTrialName(
            experiment.feature_association().forcing_feature_off())) {
      LogInvalidReason(InvalidStudyReason::kInvalidFeatureName);
      DVLOG(1) << study.name() << " has a feature experiment name "
               << experiment.feature_association().forcing_feature_off();
      return false;
    }
  }

  for (const auto& experiment : study.experiment()) {
    const auto& flag = experiment.forcing_flag();
    // Forcing flags are passed to CommandLine::HasSwitch. It should be safe to
    // pass invalid flags to that, but we do validation to prevent triggering
    // the DCHECK there.
    if (base::ToLowerASCII(flag) != flag) {
      LogInvalidReason(InvalidStudyReason::kInvalidForcingFlag);
      DVLOG(1) << study.name() << " has an invalid forcing flag " << flag;
      return false;
    }
  }

  base::FieldTrial::Probability divisor = 0;
  bool multiple_assigned_groups = false;

  for (const auto& experiment : study.experiment()) {
    if (experiment.has_google_web_experiment_id() &&
        experiment.has_google_web_trigger_experiment_id()) {
      LogInvalidReason(InvalidStudyReason::kTriggerAndNonTriggerExperimentId);
      DVLOG(1) << study.name() << " has experiment (" << experiment.name()
               << ") with a google_web_experiment_id and a "
               << "web_trigger_experiment_id.";
      return false;
    }

    if (!experiment.has_forcing_flag() && experiment.probability_weight() > 0) {
      // If |divisor| is not 0, there was at least one prior non-zero group.
      if (divisor != 0)
        multiple_assigned_groups = true;

      if (experiment.probability_weight() >
          std::numeric_limits<base::FieldTrial::Probability>::max()) {
        LogInvalidReason(InvalidStudyReason::kExperimentProbabilityOverflow);
        DVLOG(1) << study.name() << " has an experiment (" << experiment.name()
                 << ") with a probability weight of "
                 << experiment.probability_weight()
                 << " that exceeds the maximum supported value";
        return false;
      }

      if (divisor + experiment.probability_weight() >
          std::numeric_limits<base::FieldTrial::Probability>::max()) {
        LogInvalidReason(InvalidStudyReason::kTotalProbabilityOverflow);
        DVLOG(1) << study.name() << "'s total probability weight exceeds the "
                 << "maximum supported value";
        return false;
      }
      divisor += experiment.probability_weight();
    }
  }

  *total_probability = divisor;
  *all_assignments_to_one_group = !multiple_assigned_groups;
  return true;
}

}  // namespace

// static
const char ProcessedStudy::kGenericDefaultExperimentName[] =
    "VariationsDefaultExperiment";

ProcessedStudy::ProcessedStudy() = default;

ProcessedStudy::ProcessedStudy(const ProcessedStudy& other) = default;

ProcessedStudy::~ProcessedStudy() = default;

bool ProcessedStudy::Init(const Study* study) {
  base::FieldTrial::Probability total_probability = 0;
  bool all_assignments_to_one_group = false;
  std::vector<std::string> associated_features;
  if (!ValidateStudyAndComputeTotalProbability(*study, &total_probability,
                                               &all_assignments_to_one_group)) {
    return false;
  }

  study_ = study;
  total_probability_ = total_probability;
  all_assignments_to_one_group_ = all_assignments_to_one_group;
  return true;
}

int ProcessedStudy::GetExperimentIndexByName(const std::string& name) const {
  for (int i = 0; i < study_->experiment_size(); ++i) {
    if (study_->experiment(i).name() == name)
      return i;
  }

  return -1;
}

const std::string_view ProcessedStudy::GetDefaultExperimentName() const {
  if (study_->default_experiment_name().empty())
    return kGenericDefaultExperimentName;

  return study_->default_experiment_name();
}

}  // namespace variations