safe_seed_manager.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright 2017 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_SERVICE_SAFE_SEED_MANAGER_H_
  5. #define COMPONENTS_VARIATIONS_SERVICE_SAFE_SEED_MANAGER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/time/time.h"
  10. class PrefRegistrySimple;
  11. class PrefService;
  12. namespace variations {
  13. struct ClientFilterableState;
  14. class VariationsSeedStore;
  15. // As of January 2018, users at the 99.5th percentile, across all platforms,
  16. // tend to experience fewer than 3 consecutive crashes: [1], [2], [3], [4].
  17. // Note, however, that this is less true for the less-stable channels on some
  18. // platforms.
  19. // [1] All platforms, stable channel (consistently stable):
  20. // https://uma.googleplex.com/timeline_v2?sid=90ac80f4573249fb341a8e49501bfcfd
  21. // [2] Most platforms, all channels (consistently stable other than occasional
  22. // spikes on Canary):
  23. // https://uma.googleplex.com/timeline_v2?sid=7af5ba1969db76689a401f982a1db539
  24. // [3] A less stable platform, all channels:
  25. // https://uma.googleplex.com/timeline_v2?sid=07dbc8e4fa9f08e332fb609309a21882
  26. // [4] Another less stable platform, all channels:
  27. // https://uma.googleplex.com/timeline_v2?sid=a7b529ef5d52863fae2d216e963c4cbc
  28. // Overall, the only {platform, channel} combinations that spike above 3
  29. // consecutive crashes are ones with very few users, plus Canary. It's probably
  30. // not realistic to avoid false positives for these less-stable configurations.
  31. constexpr int kCrashStreakThreshold = 3;
  32. // The primary class that encapsulates state for managing the safe seed.
  33. class SafeSeedManager {
  34. public:
  35. // Creates a SafeSeedManager instance and updates a safe mode pref,
  36. // kVariationsFailedToFetchSeedStreak, for bookkeeping.
  37. explicit SafeSeedManager(PrefService* local_state);
  38. SafeSeedManager(const SafeSeedManager&) = delete;
  39. SafeSeedManager& operator=(const SafeSeedManager&) = delete;
  40. virtual ~SafeSeedManager();
  41. // Registers safe mode prefs in Local State.
  42. static void RegisterPrefs(PrefRegistrySimple* registry);
  43. // Returns true iff the client should use the safe seed for variations state.
  44. // Virtual for testing.
  45. virtual bool ShouldRunInSafeMode() const;
  46. // Stores the combined server and client state that control the active
  47. // variations state. May be called at most once per Chrome app launch. As an
  48. // optimization, should not be called when running in safe mode.
  49. // Virtual for testing.
  50. virtual void SetActiveSeedState(
  51. const std::string& seed_data,
  52. const std::string& base64_seed_signature,
  53. int seed_milestone,
  54. std::unique_ptr<ClientFilterableState> client_filterable_state,
  55. base::Time seed_fetch_time);
  56. // Records that a fetch has started: pessimistically increments the
  57. // corresponding failure streak for safe mode.
  58. void RecordFetchStarted();
  59. // Records a successful fetch: resets the failure streaks for safe mode.
  60. // Writes the currently active seed to the |seed_store| as a safe seed, if
  61. // appropriate.
  62. void RecordSuccessfulFetch(VariationsSeedStore* seed_store);
  63. private:
  64. // The combined server and client state needed to save an active seed as a
  65. // safe seed. Not set when running in safe mode.
  66. struct ActiveSeedState {
  67. ActiveSeedState(
  68. const std::string& seed_data,
  69. const std::string& base64_seed_signature,
  70. int seed_milestone,
  71. std::unique_ptr<ClientFilterableState> client_filterable_state,
  72. base::Time seed_fetch_time);
  73. ~ActiveSeedState();
  74. // The serialized variations seed data.
  75. const std::string seed_data;
  76. // The base64-encoded signature for the seed data.
  77. const std::string base64_seed_signature;
  78. // The milestone with which the active seed was fetched.
  79. const int seed_milestone;
  80. // The client state which is used for filtering studies.
  81. const std::unique_ptr<ClientFilterableState> client_filterable_state;
  82. // The latest timestamp at which this seed was fetched. This is always a
  83. // client-side timestamp, never a server-provided timestamp.
  84. const base::Time seed_fetch_time;
  85. };
  86. std::unique_ptr<ActiveSeedState> active_seed_state_;
  87. // The active seed state must never be set more than once.
  88. bool has_set_active_seed_state_ = false;
  89. // The pref service used to persist the variations seed. Weak reference; must
  90. // outlive |this| instance.
  91. raw_ptr<PrefService> local_state_;
  92. };
  93. } // namespace variations
  94. #endif // COMPONENTS_VARIATIONS_SERVICE_SAFE_SEED_MANAGER_H_