writeable_pref_store.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_WRITEABLE_PREF_STORE_H_
  5. #define COMPONENTS_PREFS_WRITEABLE_PREF_STORE_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <vector>
  11. #include "components/prefs/pref_store.h"
  12. namespace base {
  13. class Value;
  14. }
  15. // A pref store that can be written to as well as read from.
  16. class COMPONENTS_PREFS_EXPORT WriteablePrefStore : public PrefStore {
  17. public:
  18. // PrefWriteFlags can be used to change the way a pref will be written to
  19. // storage.
  20. enum PrefWriteFlags : uint32_t {
  21. // No flags are specified.
  22. DEFAULT_PREF_WRITE_FLAGS = 0,
  23. // This marks the pref as "lossy". There is no strict time guarantee on when
  24. // a lossy pref will be persisted to permanent storage when it is modified.
  25. LOSSY_PREF_WRITE_FLAG = 1 << 1
  26. };
  27. WriteablePrefStore() {}
  28. WriteablePrefStore(const WriteablePrefStore&) = delete;
  29. WriteablePrefStore& operator=(const WriteablePrefStore&) = delete;
  30. // Sets a |value| for |key| in the store. |value| must be non-NULL. |flags| is
  31. // a bitmask of PrefWriteFlags.
  32. virtual void SetValue(const std::string& key,
  33. std::unique_ptr<base::Value> value,
  34. uint32_t flags) = 0;
  35. // Removes the value for |key|.
  36. virtual void RemoveValue(const std::string& key, uint32_t flags) = 0;
  37. // Equivalent to PrefStore::GetValue but returns a mutable value.
  38. virtual bool GetMutableValue(const std::string& key,
  39. base::Value** result) = 0;
  40. // Triggers a value changed notification. This function or
  41. // ReportSubValuesChanged needs to be called if one retrieves a list or
  42. // dictionary with GetMutableValue and change its value. SetValue takes care
  43. // of notifications itself. Note that ReportValueChanged will trigger
  44. // notifications even if nothing has changed. |flags| is a bitmask of
  45. // PrefWriteFlags.
  46. virtual void ReportValueChanged(const std::string& key, uint32_t flags) = 0;
  47. // Triggers a value changed notification for |path_components| in the |key|
  48. // pref. This function or ReportValueChanged needs to be called if one
  49. // retrieves a list or dictionary with GetMutableValue and change its value.
  50. // SetValue takes care of notifications itself. Note that
  51. // ReportSubValuesChanged will trigger notifications even if nothing has
  52. // changed. |flags| is a bitmask of PrefWriteFlags.
  53. virtual void ReportSubValuesChanged(
  54. const std::string& key,
  55. std::set<std::vector<std::string>> path_components,
  56. uint32_t flags);
  57. // Same as SetValue, but doesn't generate notifications. This is used by
  58. // PrefService::GetMutableUserPref() in order to put empty entries
  59. // into the user pref store. Using SetValue is not an option since existing
  60. // tests rely on the number of notifications generated. |flags| is a bitmask
  61. // of PrefWriteFlags.
  62. virtual void SetValueSilently(const std::string& key,
  63. std::unique_ptr<base::Value> value,
  64. uint32_t flags) = 0;
  65. // Clears all the preferences which names start with |prefix| and doesn't
  66. // generate update notifications.
  67. virtual void RemoveValuesByPrefixSilently(const std::string& prefix) = 0;
  68. protected:
  69. ~WriteablePrefStore() override {}
  70. };
  71. #endif // COMPONENTS_PREFS_WRITEABLE_PREF_STORE_H_