overlay_user_pref_store.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2012 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_OVERLAY_USER_PREF_STORE_H_
  5. #define COMPONENTS_PREFS_OVERLAY_USER_PREF_STORE_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <string>
  9. #include "base/memory/ref_counted.h"
  10. #include "base/observer_list.h"
  11. #include "base/values.h"
  12. #include "components/prefs/persistent_pref_store.h"
  13. #include "components/prefs/pref_value_map.h"
  14. #include "components/prefs/prefs_export.h"
  15. // PersistentPrefStore that directs all write operations into an in-memory
  16. // PrefValueMap. Read operations are first answered by the PrefValueMap.
  17. // If the PrefValueMap does not contain a value for the requested key,
  18. // the look-up is passed on to an underlying PersistentPrefStore
  19. // |persistent_user_pref_store_|.
  20. class COMPONENTS_PREFS_EXPORT OverlayUserPrefStore
  21. : public PersistentPrefStore {
  22. public:
  23. explicit OverlayUserPrefStore(PersistentPrefStore* persistent);
  24. // The |ephemeral| store must already be initialized.
  25. OverlayUserPrefStore(PersistentPrefStore* ephemeral,
  26. PersistentPrefStore* persistent);
  27. OverlayUserPrefStore(const OverlayUserPrefStore&) = delete;
  28. OverlayUserPrefStore& operator=(const OverlayUserPrefStore&) = delete;
  29. // Returns true if a value has been set for the |key| in this
  30. // OverlayUserPrefStore, i.e. if it potentially overrides a value
  31. // from the |persistent_user_pref_store_|.
  32. virtual bool IsSetInOverlay(const std::string& key) const;
  33. // Methods of PrefStore.
  34. void AddObserver(PrefStore::Observer* observer) override;
  35. void RemoveObserver(PrefStore::Observer* observer) override;
  36. bool HasObservers() const override;
  37. bool IsInitializationComplete() const override;
  38. bool GetValue(const std::string& key,
  39. const base::Value** result) const override;
  40. base::Value::Dict GetValues() const override;
  41. // Methods of PersistentPrefStore.
  42. bool GetMutableValue(const std::string& key, base::Value** result) override;
  43. void SetValue(const std::string& key,
  44. std::unique_ptr<base::Value> value,
  45. uint32_t flags) override;
  46. void SetValueSilently(const std::string& key,
  47. std::unique_ptr<base::Value> value,
  48. uint32_t flags) override;
  49. void RemoveValue(const std::string& key, uint32_t flags) override;
  50. void RemoveValuesByPrefixSilently(const std::string& prefix) override;
  51. bool ReadOnly() const override;
  52. PrefReadError GetReadError() const override;
  53. PrefReadError ReadPrefs() override;
  54. void ReadPrefsAsync(ReadErrorDelegate* delegate) override;
  55. void CommitPendingWrite(base::OnceClosure reply_callback,
  56. base::OnceClosure synchronous_done_callback) override;
  57. void SchedulePendingLossyWrites() override;
  58. void ReportValueChanged(const std::string& key, uint32_t flags) override;
  59. // Registers preferences that should be stored in the persistent preferences
  60. // (|persistent_user_pref_store_|).
  61. void RegisterPersistentPref(const std::string& key);
  62. void ClearMutableValues() override;
  63. void OnStoreDeletionFromDisk() override;
  64. protected:
  65. ~OverlayUserPrefStore() override;
  66. private:
  67. typedef std::set<std::string> NamesSet;
  68. class ObserverAdapter;
  69. void OnPrefValueChanged(bool ephemeral, const std::string& key);
  70. void OnInitializationCompleted(bool ephemeral, bool succeeded);
  71. // Returns true if |key| corresponds to a preference that shall be stored in
  72. // persistent PrefStore.
  73. bool ShallBeStoredInPersistent(const std::string& key) const;
  74. base::ObserverList<PrefStore::Observer, true>::Unchecked observers_;
  75. std::unique_ptr<ObserverAdapter> ephemeral_pref_store_observer_;
  76. std::unique_ptr<ObserverAdapter> persistent_pref_store_observer_;
  77. scoped_refptr<PersistentPrefStore> ephemeral_user_pref_store_;
  78. scoped_refptr<PersistentPrefStore> persistent_user_pref_store_;
  79. NamesSet persistent_names_set_;
  80. NamesSet written_ephemeral_names_;
  81. };
  82. #endif // COMPONENTS_PREFS_OVERLAY_USER_PREF_STORE_H_