active_field_trials.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2014 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_ACTIVE_FIELD_TRIALS_H_
  5. #define COMPONENTS_VARIATIONS_ACTIVE_FIELD_TRIALS_H_
  6. #include <stdint.h>
  7. #include <string>
  8. #include "base/component_export.h"
  9. #include "base/metrics/field_trial.h"
  10. #include "base/strings/string_piece.h"
  11. namespace variations {
  12. // The Unique ID of a trial and its active group, where the name and group
  13. // identifiers are hashes of the trial and group name strings.
  14. struct COMPONENT_EXPORT(VARIATIONS) ActiveGroupId {
  15. uint32_t name;
  16. uint32_t group;
  17. };
  18. // Returns an ActiveGroupId struct for the given trial and group names.
  19. COMPONENT_EXPORT(VARIATIONS)
  20. ActiveGroupId MakeActiveGroupId(base::StringPiece trial_name,
  21. base::StringPiece group_name);
  22. // We need to supply a Compare class for templates since ActiveGroupId is a
  23. // user-defined type.
  24. struct COMPONENT_EXPORT(VARIATIONS) ActiveGroupIdCompare {
  25. bool operator() (const ActiveGroupId& lhs, const ActiveGroupId& rhs) const {
  26. // The group and name fields are just SHA-1 Hashes, so we just need to treat
  27. // them as IDs and do a less-than comparison. We test group first, since
  28. // name is more likely to collide.
  29. if (lhs.group != rhs.group)
  30. return lhs.group < rhs.group;
  31. return lhs.name < rhs.name;
  32. }
  33. };
  34. // Populates |name_group_ids| based on |active_groups|. Field trial names are
  35. // suffixed with |suffix| before hashing is executed.
  36. COMPONENT_EXPORT(VARIATIONS)
  37. void GetFieldTrialActiveGroupIdsForActiveGroups(
  38. base::StringPiece suffix,
  39. const base::FieldTrial::ActiveGroups& active_groups,
  40. std::vector<ActiveGroupId>* name_group_ids);
  41. // Fills the supplied vector |name_group_ids| (which must be empty when called)
  42. // with unique ActiveGroupIds for each Field Trial that has a chosen group.
  43. // Field Trials for which a group has not been chosen yet are NOT returned in
  44. // this list. Field trial names are suffixed with |suffix| before hashing is
  45. // executed.
  46. COMPONENT_EXPORT(VARIATIONS)
  47. void GetFieldTrialActiveGroupIds(base::StringPiece suffix,
  48. std::vector<ActiveGroupId>* name_group_ids);
  49. // Fills the supplied vector |output| (which must be empty when called) with
  50. // unique string representations of ActiveGroupIds for each Field Trial that
  51. // has a chosen group. The strings are formatted as "<TrialName>-<GroupName>",
  52. // with the names as hex strings. Field Trials for which a group has not been
  53. // chosen yet are NOT returned in this list. Field trial names are suffixed with
  54. // |suffix| before hashing is executed.
  55. COMPONENT_EXPORT(VARIATIONS)
  56. void GetFieldTrialActiveGroupIdsAsStrings(base::StringPiece suffix,
  57. std::vector<std::string>* output);
  58. // TODO(rkaplow): Support suffixing for synthetic trials.
  59. // Fills the supplied vector |output| (which must be empty when called) with
  60. // unique string representations of ActiveGroupIds for each Syntehtic Trial
  61. // group. The strings are formatted as "<TrialName>-<GroupName>",
  62. // with the names as hex strings. Synthetic Field Trials for which a group
  63. // which hasn't been chosen yet are NOT returned in this list.
  64. COMPONENT_EXPORT(VARIATIONS)
  65. void GetSyntheticTrialGroupIdsAsString(std::vector<std::string>* output);
  66. // Returns true if a synthetic trial with the name `trial_name` is currently
  67. // active, i.e. the named trial has chosen a group. Returns false otherwise.
  68. COMPONENT_EXPORT(VARIATIONS)
  69. bool HasSyntheticTrial(const std::string& trial_name);
  70. // Returns true if a synthetic trial with the name `trial_name` is active
  71. // with its chosen group matching `trial_group`. Returns false otherwise.
  72. COMPONENT_EXPORT(VARIATIONS)
  73. bool IsInSyntheticTrialGroup(const std::string& trial_name,
  74. const std::string& trial_group);
  75. // Sets the version of the seed that the current set of FieldTrials was
  76. // generated from.
  77. // TODO(crbug/507665): Move this to field_trials_provider once it moves
  78. // into components/variations
  79. COMPONENT_EXPORT(VARIATIONS)
  80. void SetSeedVersion(const std::string& seed_version);
  81. // Gets the version of the seed that the current set of FieldTrials was
  82. // generated from.
  83. // Only works on the browser process; returns the empty string from other
  84. // processes.
  85. // TODO(crbug/507665): Move this to field_trials_provider once it moves
  86. // into components/variations
  87. COMPONENT_EXPORT(VARIATIONS)
  88. const std::string& GetSeedVersion();
  89. // Expose some functions for testing. These functions just wrap functionality
  90. // that is implemented above.
  91. namespace testing {
  92. COMPONENT_EXPORT(VARIATIONS)
  93. void TestGetFieldTrialActiveGroupIds(
  94. base::StringPiece suffix,
  95. const base::FieldTrial::ActiveGroups& active_groups,
  96. std::vector<ActiveGroupId>* name_group_ids);
  97. } // namespace testing
  98. } // namespace variations
  99. #endif // COMPONENTS_VARIATIONS_ACTIVE_FIELD_TRIALS_H_