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