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