pref_filter.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2013 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_PREF_FILTER_H_
  5. #define COMPONENTS_PREFS_PREF_FILTER_H_
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include "base/callback_forward.h"
  10. #include "components/prefs/prefs_export.h"
  11. namespace base {
  12. class DictionaryValue;
  13. } // namespace base
  14. // Filters preferences as they are loaded from disk or updated at runtime.
  15. // Currently supported only by JsonPrefStore.
  16. class COMPONENTS_PREFS_EXPORT PrefFilter {
  17. public:
  18. // A pair of pre-write and post-write callbacks.
  19. using OnWriteCallbackPair =
  20. std::pair<base::OnceClosure, base::OnceCallback<void(bool success)>>;
  21. // A callback to be invoked when |prefs| have been read (and possibly
  22. // pre-modified) and are now ready to be handed back to this callback's
  23. // builder. |schedule_write| indicates whether a write should be immediately
  24. // scheduled (typically because the |prefs| were pre-modified).
  25. using PostFilterOnLoadCallback =
  26. base::OnceCallback<void(std::unique_ptr<base::DictionaryValue> prefs,
  27. bool schedule_write)>;
  28. virtual ~PrefFilter() {}
  29. // This method is given ownership of the |pref_store_contents| read from disk
  30. // before the underlying PersistentPrefStore gets to use them. It must hand
  31. // them back via |post_filter_on_load_callback|, but may modify them first.
  32. // Note: This method is asynchronous, which may make calls like
  33. // PersistentPrefStore::ReadPrefs() asynchronous. The owner of filtered
  34. // PersistentPrefStores should handle this to make the reads look synchronous
  35. // to external users (see SegregatedPrefStore::ReadPrefs() for an example).
  36. virtual void FilterOnLoad(
  37. PostFilterOnLoadCallback post_filter_on_load_callback,
  38. std::unique_ptr<base::DictionaryValue> pref_store_contents) = 0;
  39. // Receives notification when a pref store value is changed, before Observers
  40. // are notified.
  41. virtual void FilterUpdate(const std::string& path) = 0;
  42. // Receives notification when the pref store is about to serialize data
  43. // contained in |pref_store_contents| to a string. Modifications to
  44. // |pref_store_contents| will be persisted to disk and also affect the
  45. // in-memory state.
  46. // If the returned callbacks are non-null, they will be registered to be
  47. // invoked synchronously after the next write (from the I/O TaskRunner so they
  48. // must not be bound to thread-unsafe member state).
  49. virtual OnWriteCallbackPair FilterSerializeData(
  50. base::DictionaryValue* pref_store_contents) = 0;
  51. // Cleans preference data that may have been saved outside of the store.
  52. virtual void OnStoreDeletionFromDisk() = 0;
  53. };
  54. #endif // COMPONENTS_PREFS_PREF_FILTER_H_