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 |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "base/prefs/persistent_pref_store.h" | 22 #include "base/prefs/persistent_pref_store.h" |
23 #include "base/threading/non_thread_safe.h" | 23 #include "base/threading/non_thread_safe.h" |
24 | 24 |
25 class PrefFilter; | 25 class PrefFilter; |
26 | 26 |
27 namespace base { | 27 namespace base { |
28 class Clock; | 28 class Clock; |
29 class DictionaryValue; | 29 class DictionaryValue; |
30 class FilePath; | 30 class FilePath; |
31 class HistogramBase; | 31 class HistogramBase; |
| 32 class JsonPrefStoreLossyWriteTest; |
32 class SequencedTaskRunner; | 33 class SequencedTaskRunner; |
33 class SequencedWorkerPool; | 34 class SequencedWorkerPool; |
34 class Value; | 35 class Value; |
35 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestBasic); | 36 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestBasic); |
36 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod); | 37 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod); |
37 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods); | 38 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods); |
38 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps); | 39 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps); |
39 } | 40 } |
40 | 41 |
41 // A writable PrefStore implementation that is used for user preferences. | 42 // A writable PrefStore implementation that is used for user preferences. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 // PrefStore overrides: | 76 // PrefStore overrides: |
76 bool GetValue(const std::string& key, | 77 bool GetValue(const std::string& key, |
77 const base::Value** result) const override; | 78 const base::Value** result) const override; |
78 void AddObserver(PrefStore::Observer* observer) override; | 79 void AddObserver(PrefStore::Observer* observer) override; |
79 void RemoveObserver(PrefStore::Observer* observer) override; | 80 void RemoveObserver(PrefStore::Observer* observer) override; |
80 bool HasObservers() const override; | 81 bool HasObservers() const override; |
81 bool IsInitializationComplete() const override; | 82 bool IsInitializationComplete() const override; |
82 | 83 |
83 // PersistentPrefStore overrides: | 84 // PersistentPrefStore overrides: |
84 bool GetMutableValue(const std::string& key, base::Value** result) override; | 85 bool GetMutableValue(const std::string& key, base::Value** result) override; |
85 void SetValue(const std::string& key, base::Value* value) override; | 86 void SetValue(const std::string& key, |
86 void SetValueSilently(const std::string& key, base::Value* value) override; | 87 base::Value* value, |
87 void RemoveValue(const std::string& key) override; | 88 uint32 flags) override; |
| 89 void SetValueSilently(const std::string& key, |
| 90 base::Value* value, |
| 91 uint32 flags) override; |
| 92 void RemoveValue(const std::string& key, uint32 flags) override; |
88 bool ReadOnly() const override; | 93 bool ReadOnly() const override; |
89 PrefReadError GetReadError() const override; | 94 PrefReadError GetReadError() const override; |
90 // Note this method may be asynchronous if this instance has a |pref_filter_| | 95 // Note this method may be asynchronous if this instance has a |pref_filter_| |
91 // in which case it will return PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE. | 96 // in which case it will return PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE. |
92 // See details in pref_filter.h. | 97 // See details in pref_filter.h. |
93 PrefReadError ReadPrefs() override; | 98 PrefReadError ReadPrefs() override; |
94 void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override; | 99 void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override; |
95 void CommitPendingWrite() override; | 100 void CommitPendingWrite() override; |
96 void ReportValueChanged(const std::string& key) override; | 101 void ReportValueChanged(const std::string& key, uint32 flags) override; |
97 | 102 |
98 // Just like RemoveValue(), but doesn't notify observers. Used when doing some | 103 // Just like RemoveValue(), but doesn't notify observers. Used when doing some |
99 // cleanup that shouldn't otherwise alert observers. | 104 // cleanup that shouldn't otherwise alert observers. |
100 void RemoveValueSilently(const std::string& key); | 105 void RemoveValueSilently(const std::string& key, uint32 flags); |
101 | 106 |
102 // Registers |on_next_successful_write| to be called once, on the next | 107 // Registers |on_next_successful_write| to be called once, on the next |
103 // successful write event of |writer_|. | 108 // successful write event of |writer_|. |
104 void RegisterOnNextSuccessfulWriteCallback( | 109 void RegisterOnNextSuccessfulWriteCallback( |
105 const base::Closure& on_next_successful_write); | 110 const base::Closure& on_next_successful_write); |
106 | 111 |
107 private: | 112 private: |
108 // Represents a histogram for recording the number of writes to the pref file | 113 // Represents a histogram for recording the number of writes to the pref file |
109 // that occur every kHistogramWriteReportIntervalInMins minutes. | 114 // that occur every kHistogramWriteReportIntervalInMins minutes. |
110 class BASE_PREFS_EXPORT WriteCountHistogram { | 115 class BASE_PREFS_EXPORT WriteCountHistogram { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 }; | 159 }; |
155 | 160 |
156 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, | 161 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, |
157 WriteCountHistogramTestBasic); | 162 WriteCountHistogramTestBasic); |
158 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, | 163 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, |
159 WriteCountHistogramTestSinglePeriod); | 164 WriteCountHistogramTestSinglePeriod); |
160 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, | 165 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, |
161 WriteCountHistogramTestMultiplePeriods); | 166 WriteCountHistogramTestMultiplePeriods); |
162 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, | 167 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, |
163 WriteCountHistogramTestPeriodWithGaps); | 168 WriteCountHistogramTestPeriodWithGaps); |
| 169 friend class base::JsonPrefStoreLossyWriteTest; |
164 | 170 |
165 ~JsonPrefStore() override; | 171 ~JsonPrefStore() override; |
166 | 172 |
167 // This method is called after the JSON file has been read. It then hands | 173 // This method is called after the JSON file has been read. It then hands |
168 // |value| (or an empty dictionary in some read error cases) to the | 174 // |value| (or an empty dictionary in some read error cases) to the |
169 // |pref_filter| if one is set. It also gives a callback pointing at | 175 // |pref_filter| if one is set. It also gives a callback pointing at |
170 // FinalizeFileRead() to that |pref_filter_| which is then responsible for | 176 // FinalizeFileRead() to that |pref_filter_| which is then responsible for |
171 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead() | 177 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead() |
172 // is invoked directly. | 178 // is invoked directly. |
173 void OnFileRead(scoped_ptr<ReadResult> read_result); | 179 void OnFileRead(scoped_ptr<ReadResult> read_result); |
174 | 180 |
175 // ImportantFileWriter::DataSerializer overrides: | 181 // ImportantFileWriter::DataSerializer overrides: |
176 bool SerializeData(std::string* output) override; | 182 bool SerializeData(std::string* output) override; |
177 | 183 |
178 // This method is called after the JSON file has been read and the result has | 184 // This method is called after the JSON file has been read and the result has |
179 // potentially been intercepted and modified by |pref_filter_|. | 185 // potentially been intercepted and modified by |pref_filter_|. |
180 // |initialization_successful| is pre-determined by OnFileRead() and should | 186 // |initialization_successful| is pre-determined by OnFileRead() and should |
181 // be used when reporting OnInitializationCompleted(). | 187 // be used when reporting OnInitializationCompleted(). |
182 // |schedule_write| indicates whether a write should be immediately scheduled | 188 // |schedule_write| indicates whether a write should be immediately scheduled |
183 // (typically because the |pref_filter_| has already altered the |prefs|) -- | 189 // (typically because the |pref_filter_| has already altered the |prefs|) -- |
184 // this will be ignored if this store is read-only. | 190 // this will be ignored if this store is read-only. |
185 void FinalizeFileRead(bool initialization_successful, | 191 void FinalizeFileRead(bool initialization_successful, |
186 scoped_ptr<base::DictionaryValue> prefs, | 192 scoped_ptr<base::DictionaryValue> prefs, |
187 bool schedule_write); | 193 bool schedule_write); |
188 | 194 |
| 195 // Schedule a write with the file writer as long as |flags| doesn't contain |
| 196 // WriteablePrefStore::LOSSY_PREF_WRITE_FLAG. |
| 197 void ScheduleWrite(uint32 flags); |
| 198 |
189 const base::FilePath path_; | 199 const base::FilePath path_; |
190 const base::FilePath alternate_path_; | 200 const base::FilePath alternate_path_; |
191 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; | 201 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; |
192 | 202 |
193 scoped_ptr<base::DictionaryValue> prefs_; | 203 scoped_ptr<base::DictionaryValue> prefs_; |
194 | 204 |
195 bool read_only_; | 205 bool read_only_; |
196 | 206 |
197 // Helper for safely writing pref data. | 207 // Helper for safely writing pref data. |
198 base::ImportantFileWriter writer_; | 208 base::ImportantFileWriter writer_; |
199 | 209 |
200 scoped_ptr<PrefFilter> pref_filter_; | 210 scoped_ptr<PrefFilter> pref_filter_; |
201 ObserverList<PrefStore::Observer, true> observers_; | 211 ObserverList<PrefStore::Observer, true> observers_; |
202 | 212 |
203 scoped_ptr<ReadErrorDelegate> error_delegate_; | 213 scoped_ptr<ReadErrorDelegate> error_delegate_; |
204 | 214 |
205 bool initialized_; | 215 bool initialized_; |
206 bool filtering_in_progress_; | 216 bool filtering_in_progress_; |
| 217 bool pending_lossy_write_; |
207 PrefReadError read_error_; | 218 PrefReadError read_error_; |
208 | 219 |
209 std::set<std::string> keys_need_empty_value_; | 220 std::set<std::string> keys_need_empty_value_; |
210 | 221 |
211 WriteCountHistogram write_count_histogram_; | 222 WriteCountHistogram write_count_histogram_; |
212 | 223 |
213 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); | 224 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); |
214 }; | 225 }; |
215 | 226 |
216 #endif // BASE_PREFS_JSON_PREF_STORE_H_ | 227 #endif // BASE_PREFS_JSON_PREF_STORE_H_ |
OLD | NEW |