processed_study.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef COMPONENTS_VARIATIONS_PROCESSED_STUDY_H_
  5. #define COMPONENTS_VARIATIONS_PROCESSED_STUDY_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/component_export.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/metrics/field_trial.h"
  11. namespace variations {
  12. // These values are persisted to logs. Entries should not be renumbered and
  13. // numeric values should never be reused. Exposed for testing.
  14. enum class InvalidStudyReason {
  15. kInvalidMinVersion = 0,
  16. kInvalidMaxVersion = 1,
  17. kInvalidMinOsVersion = 2,
  18. kInvalidMaxOsVersion = 3,
  19. kMissingExperimentName = 4,
  20. kRepeatedExperimentName = 5,
  21. kTotalProbabilityOverflow = 6,
  22. kMissingDefaultExperimentInList = 7,
  23. kBlankStudyName = 8,
  24. kExperimentProbabilityOverflow = 9,
  25. kTriggerAndNonTriggerExperimentId = 10,
  26. kMaxValue = kTriggerAndNonTriggerExperimentId,
  27. };
  28. class Study;
  29. // Wrapper over Study with extra information computed during pre-processing,
  30. // such as whether the study is expired and its total probability.
  31. class COMPONENT_EXPORT(VARIATIONS) ProcessedStudy {
  32. public:
  33. // The default group used when a study doesn't specify one. This is needed
  34. // because the field trial api requires a default group name.
  35. static const char kGenericDefaultExperimentName[];
  36. ProcessedStudy();
  37. ProcessedStudy(const ProcessedStudy& other);
  38. ~ProcessedStudy();
  39. bool Init(const Study* study, bool is_expired);
  40. const Study* study() const { return study_; }
  41. base::FieldTrial::Probability total_probability() const {
  42. return total_probability_;
  43. }
  44. bool all_assignments_to_one_group() const {
  45. return all_assignments_to_one_group_;
  46. }
  47. bool is_expired() const { return is_expired_; }
  48. const std::vector<std::string>& associated_features() const {
  49. return associated_features_;
  50. }
  51. // Gets the index of the experiment with the given |name|. Returns -1 if no
  52. // experiment is found.
  53. int GetExperimentIndexByName(const std::string& name) const;
  54. // Gets the default experiment name for the study, or a generic one if none is
  55. // specified.
  56. const base::StringPiece GetDefaultExperimentName() const;
  57. private:
  58. // Corresponding Study object. Weak reference.
  59. raw_ptr<const Study> study_ = nullptr;
  60. // Computed total group probability for the study.
  61. base::FieldTrial::Probability total_probability_ = 0;
  62. // Whether all assignments are to a single group.
  63. bool all_assignments_to_one_group_ = false;
  64. // Whether the study is expired.
  65. bool is_expired_ = false;
  66. // A list of feature names associated with this study by default. Studies
  67. // might have groups that do not specify any feature associations – this is
  68. // often the case for a default group, for example. The features listed here
  69. // will be associated with all such groups.
  70. std::vector<std::string> associated_features_;
  71. };
  72. } // namespace variations
  73. #endif // COMPONENTS_VARIATIONS_PROCESSED_STUDY_H_