entropy_state.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2020 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_METRICS_ENTROPY_STATE_H_
  5. #define COMPONENTS_METRICS_ENTROPY_STATE_H_
  6. #include <string>
  7. #include "base/gtest_prod_util.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "components/prefs/pref_registry_simple.h"
  10. class PrefService;
  11. namespace metrics {
  12. // A class to get entropy source values from the PrefService.
  13. class EntropyState final {
  14. public:
  15. // Creates the EntropyState with the given |local_state| to get
  16. // the entropy source value from this helper class.
  17. explicit EntropyState(PrefService* local_state);
  18. EntropyState(const EntropyState&) = delete;
  19. EntropyState& operator=(const EntropyState&) = delete;
  20. // Clears low_entropy_source and old_low_entropy_source in the prefs.
  21. static void ClearPrefs(PrefService* local_state);
  22. // Registers low_entropy_source and old_low_entropy_source in the prefs.
  23. static void RegisterPrefs(PrefRegistrySimple* registry);
  24. // Returns the high entropy source for this client, which is composed of a
  25. // client ID and the low entropy source. This is intended to be unique for
  26. // each install. |initial_client_id| is the client_id that was used to
  27. // randomize field trials and must not be empty.
  28. std::string GetHighEntropySource(const std::string& initial_client_id);
  29. // Returns the low entropy source that is used to randomize field trials on
  30. // startup for this client. Generates a new value if there is none. See the
  31. // |low_entropy_source_| comment for more info.
  32. int GetLowEntropySource();
  33. // Returns the pseudo low entropy source for this client. Generates a new
  34. // value if there is none. See the |pseudo_low_entropy_source_| comment
  35. // for more info.
  36. int GetPseudoLowEntropySource();
  37. // Returns the old low entropy source for this client. Does not generate a new
  38. // value, but instead returns |kLowEntropySourceNotSet|, if there is none. See
  39. // the |old_low_entropy_source_| comment for more info.
  40. int GetOldLowEntropySource();
  41. private:
  42. FRIEND_TEST_ALL_PREFIXES(EntropyStateTest, LowEntropySourceNotReset);
  43. FRIEND_TEST_ALL_PREFIXES(EntropyStateTest, PseudoLowEntropySourceNotReset);
  44. FRIEND_TEST_ALL_PREFIXES(EntropyStateTest, HaveNoLowEntropySource);
  45. FRIEND_TEST_ALL_PREFIXES(EntropyStateTest, HaveOnlyNewLowEntropySource);
  46. FRIEND_TEST_ALL_PREFIXES(EntropyStateTest, HaveOnlyOldLowEntropySource);
  47. FRIEND_TEST_ALL_PREFIXES(EntropyStateTest, CorruptNewLowEntropySources);
  48. FRIEND_TEST_ALL_PREFIXES(EntropyStateTest, CorruptOldLowEntropySources);
  49. // Default value for prefs::kMetricsLowEntropySource.
  50. static constexpr int kLowEntropySourceNotSet = -1;
  51. // Loads the low entropy source values from prefs. Creates the new source
  52. // value if it doesn't exist, but doesn't create the old source value. After
  53. // this function finishes, |low_entropy_source_| will be set, but
  54. // |old_low_entropy_source_| may still be |kLowEntropySourceNotSet|.
  55. void UpdateLowEntropySources();
  56. // Checks whether a value is on the range of allowed low entropy source
  57. // values.
  58. static bool IsValidLowEntropySource(int value);
  59. // The local state prefs store.
  60. const raw_ptr<PrefService> local_state_;
  61. // The non-identifying low entropy source values. These values seed the
  62. // pseudorandom generators which pick experimental groups. The "old" value is
  63. // thought to be biased in the wild, and is no longer used for experiments
  64. // requiring low entropy. Clients which already have an "old" value continue
  65. // incorporating it into the high entropy source, to avoid changing those
  66. // group assignments. New clients only have the new source.
  67. //
  68. // The pseudo-low entropy source is not used for experiment diversion, but
  69. // only for statistical validation. (Since it's not used for experiment
  70. // diversion, it won't be subject to drift over time as experiment effects
  71. // accumulate in actual low entropy source buckets.)
  72. //
  73. // During startup these are set to the values used for randomizing field
  74. // trials and won't be changed within the session even after calling
  75. // |ClearPrefs|
  76. int low_entropy_source_ = kLowEntropySourceNotSet;
  77. int old_low_entropy_source_ = kLowEntropySourceNotSet;
  78. int pseudo_low_entropy_source_ = kLowEntropySourceNotSet;
  79. };
  80. } // namespace metrics
  81. #endif // COMPONENTS_METRICS_ENTROPY_STATE_H_