OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 BASE_PREFS_JSON_PREF_STORE_H_ | 5 #ifndef BASE_PREFS_JSON_PREF_STORE_H_ |
6 #define BASE_PREFS_JSON_PREF_STORE_H_ | 6 #define BASE_PREFS_JSON_PREF_STORE_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/callback_forward.h" | |
12 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/files/important_file_writer.h" | 15 #include "base/files/important_file_writer.h" |
15 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
16 #include "base/message_loop/message_loop_proxy.h" | 17 #include "base/message_loop/message_loop_proxy.h" |
17 #include "base/observer_list.h" | 18 #include "base/observer_list.h" |
18 #include "base/prefs/base_prefs_export.h" | 19 #include "base/prefs/base_prefs_export.h" |
19 #include "base/prefs/persistent_pref_store.h" | 20 #include "base/prefs/persistent_pref_store.h" |
20 | 21 |
21 class PrefFilter; | 22 class PrefFilter; |
22 | 23 |
23 namespace base { | 24 namespace base { |
24 class DictionaryValue; | 25 class DictionaryValue; |
25 class FilePath; | 26 class FilePath; |
26 class SequencedTaskRunner; | 27 class SequencedTaskRunner; |
27 class SequencedWorkerPool; | 28 class SequencedWorkerPool; |
28 class Value; | 29 class Value; |
29 } | 30 } |
30 | 31 |
31 | 32 |
32 // A writable PrefStore implementation that is used for user preferences. | 33 // A writable PrefStore implementation that is used for user preferences. |
33 class BASE_PREFS_EXPORT JsonPrefStore | 34 class BASE_PREFS_EXPORT JsonPrefStore |
34 : public PersistentPrefStore, | 35 : public PersistentPrefStore, |
35 public base::ImportantFileWriter::DataSerializer { | 36 public base::ImportantFileWriter::DataSerializer { |
36 public: | 37 public: |
38 // A callback to be invoked when |prefs| have been read (and possibly | |
39 // pre-modified) and are now ready to be handed back to this callback's | |
40 // builder. |schedule_write| indicates whether a write should be scheduled | |
41 // (typically because the OnFileReadInterceptor has already altered the | |
42 // |prefs|). | |
43 typedef base::Callback<void(scoped_ptr<base::DictionaryValue> prefs, | |
44 bool schedule_write)> | |
45 FinalizePrefsReadCallback; | |
46 | |
47 // A callback to be invoked from OnFileRead, after the read errors have been | |
48 // handled, but before |prefs| is handed to this JsonPrefStore. | |
49 // |read_only| indicates whether this store was turned read-only because of | |
50 // an error (in which case it is still possible to alter the in-memory state | |
51 // of this store, but the result won't be flushed back to disk). The callee | |
52 // of this callback is responsible for invoking |finalize_prefs_read| to hand | |
53 // |prefs| back to this JsonPrefStore when it's done -- this JsonPrefStore | |
54 // will not consider its read complete nor invoke observers until it has been | |
55 // invoked. | |
56 typedef base::Callback<void(scoped_ptr<base::DictionaryValue> prefs, | |
57 bool read_only, | |
58 const FinalizePrefsReadCallback& | |
59 finalize_prefs_read)> OnFileReadInterceptor; | |
60 | |
37 // Returns instance of SequencedTaskRunner which guarantees that file | 61 // Returns instance of SequencedTaskRunner which guarantees that file |
38 // operations on the same file will be executed in sequenced order. | 62 // operations on the same file will be executed in sequenced order. |
39 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForFile( | 63 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForFile( |
40 const base::FilePath& pref_filename, | 64 const base::FilePath& pref_filename, |
41 base::SequencedWorkerPool* worker_pool); | 65 base::SequencedWorkerPool* worker_pool); |
42 | 66 |
43 // |sequenced_task_runner| is must be a shutdown-blocking task runner, ideally | 67 // |sequenced_task_runner| is must be a shutdown-blocking task runner, ideally |
44 // created by GetTaskRunnerForFile() method above. | 68 // created by GetTaskRunnerForFile() method above. |
45 JsonPrefStore(const base::FilePath& pref_filename, | 69 JsonPrefStore(const base::FilePath& pref_filename, |
46 base::SequencedTaskRunner* sequenced_task_runner, | 70 base::SequencedTaskRunner* sequenced_task_runner, |
(...skipping 14 matching lines...) Expand all Loading... | |
61 virtual void SetValueSilently(const std::string& key, | 85 virtual void SetValueSilently(const std::string& key, |
62 base::Value* value) OVERRIDE; | 86 base::Value* value) OVERRIDE; |
63 virtual void RemoveValue(const std::string& key) OVERRIDE; | 87 virtual void RemoveValue(const std::string& key) OVERRIDE; |
64 virtual bool ReadOnly() const OVERRIDE; | 88 virtual bool ReadOnly() const OVERRIDE; |
65 virtual PrefReadError GetReadError() const OVERRIDE; | 89 virtual PrefReadError GetReadError() const OVERRIDE; |
66 virtual PrefReadError ReadPrefs() OVERRIDE; | 90 virtual PrefReadError ReadPrefs() OVERRIDE; |
67 virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) OVERRIDE; | 91 virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) OVERRIDE; |
68 virtual void CommitPendingWrite() OVERRIDE; | 92 virtual void CommitPendingWrite() OVERRIDE; |
69 virtual void ReportValueChanged(const std::string& key) OVERRIDE; | 93 virtual void ReportValueChanged(const std::string& key) OVERRIDE; |
70 | 94 |
71 // This method is called after JSON file has been read. Method takes | 95 // Registers |on_file_read_interceptor| to intercept the next OnFileRead |
72 // ownership of the |value| pointer. Note, this method is used with | 96 // event. |
73 // asynchronous file reading, so class exposes it only for the internal needs. | 97 void InterceptNextFileRead( |
74 // (read: do not call it manually). | 98 const OnFileReadInterceptor& on_file_read_interceptor); |
75 void OnFileRead(base::Value* value_owned, PrefReadError error, bool no_dir); | 99 |
100 // This method is called after the JSON file has been read. It then hands | |
101 // |value| (or an empty dictionary in some read error cases) to the | |
102 // |on_file_read_interceptor_| if one is set. It also gives a callback | |
103 // pointing at FinalizeFileRead() to the |on_file_read_interceptor_| which is | |
104 // then responsible for invoking it when done. If there is no | |
105 // |on_file_read_interceptor_|, FinalizeFileRead() is invoked directly. | |
106 void OnFileRead(scoped_ptr<base::Value> value, | |
107 PrefReadError error, | |
108 bool no_dir); | |
76 | 109 |
77 private: | 110 private: |
78 virtual ~JsonPrefStore(); | 111 virtual ~JsonPrefStore(); |
79 | 112 |
80 // ImportantFileWriter::DataSerializer overrides: | 113 // ImportantFileWriter::DataSerializer overrides: |
81 virtual bool SerializeData(std::string* output) OVERRIDE; | 114 virtual bool SerializeData(std::string* output) OVERRIDE; |
82 | 115 |
116 // This method is called after the JSON file has been read and the result has | |
117 // potentially been intercepted and modified by on_file_read_interceptor_. | |
Bernhard Bauer
2014/04/29 15:13:04
Nit: Put |on_file_read_interceptor_| between pipes
gab
2014/04/29 15:46:44
Done.
| |
118 // |initialization_successful| is pre-determined by OnFileRead() and should | |
119 // be used when reporting OnInitializationCompleted(). | |
120 // |schedule_write| indicates whether a write should be scheduled (typically | |
121 // because the OnFileReadInterceptor has already altered the |prefs|) -- this | |
122 // will be ignored if this store is read-only. | |
123 void FinalizeFileRead(bool initialization_successful, | |
124 scoped_ptr<base::DictionaryValue> prefs, | |
125 bool schedule_write); | |
126 | |
127 // Callback to be invoked only once (and subsequently deleted) on the next | |
128 // OnFileRead event. It will be allowed to modify the JSON value read from | |
129 // disk before this JsonPrefStore gets to declare itself initialized and | |
130 // begins using it. It is responsible for handing it back to this | |
131 // JsonPrefStore via the FinalizePrefReadCallback handed to it. | |
132 // Note: Setting an |on_file_read_interceptor_| may make ReadPrefs() | |
133 // asynchronous if the |on_file_read_interceptor_| is asynchronous. | |
134 scoped_ptr<OnFileReadInterceptor> on_file_read_interceptor_; | |
Bernhard Bauer
2014/04/29 15:13:04
Why do you need a scoped_ptr to a Callback? Callba
gab
2014/04/29 15:46:44
I wasn't sure how to best express that this Callba
Bernhard Bauer
2014/04/29 16:38:53
Callback also has a Reset() (capital R) method.
gab
2014/04/29 20:12:14
Oh awesome :)! (I'd looked for that in callback.h,
| |
135 | |
83 base::FilePath path_; | 136 base::FilePath path_; |
84 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; | 137 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; |
85 | 138 |
86 scoped_ptr<base::DictionaryValue> prefs_; | 139 scoped_ptr<base::DictionaryValue> prefs_; |
87 | 140 |
88 bool read_only_; | 141 bool read_only_; |
89 | 142 |
90 // Helper for safely writing pref data. | 143 // Helper for safely writing pref data. |
91 base::ImportantFileWriter writer_; | 144 base::ImportantFileWriter writer_; |
92 | 145 |
93 scoped_ptr<PrefFilter> pref_filter_; | 146 scoped_ptr<PrefFilter> pref_filter_; |
94 ObserverList<PrefStore::Observer, true> observers_; | 147 ObserverList<PrefStore::Observer, true> observers_; |
95 | 148 |
96 scoped_ptr<ReadErrorDelegate> error_delegate_; | 149 scoped_ptr<ReadErrorDelegate> error_delegate_; |
97 | 150 |
98 bool initialized_; | 151 bool initialized_; |
99 PrefReadError read_error_; | 152 PrefReadError read_error_; |
100 | 153 |
101 std::set<std::string> keys_need_empty_value_; | 154 std::set<std::string> keys_need_empty_value_; |
102 | 155 |
103 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); | 156 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); |
104 }; | 157 }; |
105 | 158 |
106 #endif // BASE_PREFS_JSON_PREF_STORE_H_ | 159 #endif // BASE_PREFS_JSON_PREF_STORE_H_ |
OLD | NEW |