segregated_pref_store.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_PREFS_SEGREGATED_PREF_STORE_H_
  5. #define COMPONENTS_PREFS_SEGREGATED_PREF_STORE_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include "base/callback.h"
  11. #include "base/compiler_specific.h"
  12. #include "base/memory/raw_ptr.h"
  13. #include "base/memory/ref_counted.h"
  14. #include "base/observer_list.h"
  15. #include "components/prefs/persistent_pref_store.h"
  16. #include "components/prefs/prefs_export.h"
  17. // Provides a unified PersistentPrefStore implementation that splits its storage
  18. // and retrieval between two underlying PersistentPrefStore instances: a set of
  19. // preference names is used to partition the preferences.
  20. //
  21. // Combines properties of the two stores as follows:
  22. // * The unified read error will be:
  23. // Selected Store Error
  24. // Default Store Error | NO_ERROR | NO_FILE | other selected |
  25. // NO_ERROR | NO_ERROR | NO_ERROR | other selected |
  26. // NO_FILE | NO_FILE | NO_FILE | NO_FILE |
  27. // other default | other default | other default | other default |
  28. // * The unified initialization success, initialization completion, and
  29. // read-only state are the boolean OR of the underlying stores' properties.
  30. class COMPONENTS_PREFS_EXPORT SegregatedPrefStore : public PersistentPrefStore {
  31. public:
  32. // Creates an instance that delegates to |selected_pref_store| for the
  33. // preferences named in |selected_pref_names| and to |default_pref_store|
  34. // for all others. If an unselected preference is present in
  35. // |selected_pref_store| (i.e. because it was previously selected) it will
  36. // be migrated back to |default_pref_store| upon access via a non-const
  37. // method.
  38. SegregatedPrefStore(scoped_refptr<PersistentPrefStore> default_pref_store,
  39. scoped_refptr<PersistentPrefStore> selected_pref_store,
  40. std::set<std::string> selected_pref_names);
  41. SegregatedPrefStore(const SegregatedPrefStore&) = delete;
  42. SegregatedPrefStore& operator=(const SegregatedPrefStore&) = delete;
  43. // PrefStore implementation
  44. void AddObserver(Observer* observer) override;
  45. void RemoveObserver(Observer* observer) override;
  46. bool HasObservers() const override;
  47. bool IsInitializationComplete() const override;
  48. bool GetValue(const std::string& key,
  49. const base::Value** result) const override;
  50. base::Value::Dict GetValues() const override;
  51. // WriteablePrefStore implementation
  52. void SetValue(const std::string& key,
  53. std::unique_ptr<base::Value> value,
  54. uint32_t flags) override;
  55. void RemoveValue(const std::string& key, uint32_t flags) override;
  56. void RemoveValuesByPrefixSilently(const std::string& prefix) override;
  57. // PersistentPrefStore implementation
  58. bool GetMutableValue(const std::string& key, base::Value** result) override;
  59. void ReportValueChanged(const std::string& key, uint32_t flags) override;
  60. void SetValueSilently(const std::string& key,
  61. std::unique_ptr<base::Value> value,
  62. uint32_t flags) override;
  63. bool ReadOnly() const override;
  64. PrefReadError GetReadError() const override;
  65. PrefReadError ReadPrefs() override;
  66. void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override;
  67. void CommitPendingWrite(
  68. base::OnceClosure reply_callback = base::OnceClosure(),
  69. base::OnceClosure synchronous_done_callback =
  70. base::OnceClosure()) override;
  71. void SchedulePendingLossyWrites() override;
  72. void ClearMutableValues() override;
  73. void OnStoreDeletionFromDisk() override;
  74. protected:
  75. ~SegregatedPrefStore() override;
  76. private:
  77. // Caches event state from the underlying stores and exposes the state to the
  78. // provided "outer" SegregatedPrefStore to synthesize external events via
  79. // |read_error_delegate_| and |observers_|.
  80. class UnderlyingPrefStoreObserver : public PrefStore::Observer {
  81. public:
  82. explicit UnderlyingPrefStoreObserver(SegregatedPrefStore* outer);
  83. UnderlyingPrefStoreObserver(const UnderlyingPrefStoreObserver&) = delete;
  84. UnderlyingPrefStoreObserver& operator=(const UnderlyingPrefStoreObserver&) =
  85. delete;
  86. // PrefStore::Observer implementation
  87. void OnPrefValueChanged(const std::string& key) override;
  88. void OnInitializationCompleted(bool succeeded) override;
  89. bool initialization_succeeded() const { return initialization_succeeded_; }
  90. private:
  91. const raw_ptr<SegregatedPrefStore> outer_;
  92. bool initialization_succeeded_ = false;
  93. };
  94. // Returns true only if all underlying PrefStores have initialized
  95. // successfully, otherwise false.
  96. bool IsInitializationSuccessful() const;
  97. // Returns |selected_pref_store| if |key| is selected and
  98. // |default_pref_store| otherwise.
  99. PersistentPrefStore* StoreForKey(const std::string& key);
  100. const PersistentPrefStore* StoreForKey(const std::string& key) const;
  101. const scoped_refptr<PersistentPrefStore> default_pref_store_;
  102. const scoped_refptr<PersistentPrefStore> selected_pref_store_;
  103. const std::set<std::string> selected_preference_names_;
  104. std::unique_ptr<PersistentPrefStore::ReadErrorDelegate> read_error_delegate_;
  105. base::ObserverList<PrefStore::Observer, true>::Unchecked observers_;
  106. UnderlyingPrefStoreObserver default_observer_;
  107. UnderlyingPrefStoreObserver selected_observer_;
  108. };
  109. #endif // COMPONENTS_PREFS_SEGREGATED_PREF_STORE_H_