| 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 COMPONENTS_PREFS_JSON_PREF_STORE_H_ | 5 #ifndef COMPONENTS_PREFS_JSON_PREF_STORE_H_ |
| 6 #define COMPONENTS_PREFS_JSON_PREF_STORE_H_ | 6 #define COMPONENTS_PREFS_JSON_PREF_STORE_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 namespace base { | 28 namespace base { |
| 29 class Clock; | 29 class Clock; |
| 30 class DictionaryValue; | 30 class DictionaryValue; |
| 31 class FilePath; | 31 class FilePath; |
| 32 class HistogramBase; | 32 class HistogramBase; |
| 33 class JsonPrefStoreCallbackTest; | 33 class JsonPrefStoreCallbackTest; |
| 34 class JsonPrefStoreLossyWriteTest; | 34 class JsonPrefStoreLossyWriteTest; |
| 35 class SequencedTaskRunner; | 35 class SequencedTaskRunner; |
| 36 class SequencedWorkerPool; | 36 class SequencedWorkerPool; |
| 37 class WriteCallbacksObserver; |
| 37 class Value; | 38 class Value; |
| 38 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestBasic); | 39 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestBasic); |
| 39 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod); | 40 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod); |
| 40 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods); | 41 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods); |
| 41 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps); | 42 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps); |
| 42 } | 43 } |
| 43 | 44 |
| 44 // A writable PrefStore implementation that is used for user preferences. | 45 // A writable PrefStore implementation that is used for user preferences. |
| 45 class COMPONENTS_PREFS_EXPORT JsonPrefStore | 46 class COMPONENTS_PREFS_EXPORT JsonPrefStore |
| 46 : public PersistentPrefStore, | 47 : public PersistentPrefStore, |
| 47 public base::ImportantFileWriter::DataSerializer, | 48 public base::ImportantFileWriter::DataSerializer, |
| 48 public base::SupportsWeakPtr<JsonPrefStore>, | 49 public base::SupportsWeakPtr<JsonPrefStore>, |
| 49 public base::NonThreadSafe { | 50 public base::NonThreadSafe { |
| 50 public: | 51 public: |
| 51 struct ReadResult; | 52 struct ReadResult; |
| 52 | 53 |
| 54 // A pair of callbacks to call before and after the preference file is written |
| 55 // to disk. |
| 56 using OnWriteCallbackPair = |
| 57 std::pair<base::Closure, base::Callback<void(bool success)>>; |
| 58 |
| 53 // Returns instance of SequencedTaskRunner which guarantees that file | 59 // Returns instance of SequencedTaskRunner which guarantees that file |
| 54 // operations on the same file will be executed in sequenced order. | 60 // operations on the same file will be executed in sequenced order. |
| 55 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForFile( | 61 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForFile( |
| 56 const base::FilePath& pref_filename, | 62 const base::FilePath& pref_filename, |
| 57 base::SequencedWorkerPool* worker_pool); | 63 base::SequencedWorkerPool* worker_pool); |
| 58 | 64 |
| 59 // Same as the constructor below with no alternate filename. | 65 // Same as the constructor below with no alternate filename. |
| 60 JsonPrefStore( | 66 JsonPrefStore( |
| 61 const base::FilePath& pref_filename, | 67 const base::FilePath& pref_filename, |
| 62 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, | 68 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 // cleanup that shouldn't otherwise alert observers. | 113 // cleanup that shouldn't otherwise alert observers. |
| 108 void RemoveValueSilently(const std::string& key, uint32_t flags); | 114 void RemoveValueSilently(const std::string& key, uint32_t flags); |
| 109 | 115 |
| 110 // Registers |on_next_successful_write_reply| to be called once, on the next | 116 // Registers |on_next_successful_write_reply| to be called once, on the next |
| 111 // successful write event of |writer_|. | 117 // successful write event of |writer_|. |
| 112 // |on_next_successful_write_reply| will be called on the thread from which | 118 // |on_next_successful_write_reply| will be called on the thread from which |
| 113 // this method is called and does not need to be thread safe. | 119 // this method is called and does not need to be thread safe. |
| 114 void RegisterOnNextSuccessfulWriteReply( | 120 void RegisterOnNextSuccessfulWriteReply( |
| 115 const base::Closure& on_next_successful_write_reply); | 121 const base::Closure& on_next_successful_write_reply); |
| 116 | 122 |
| 117 // Registers |on_next_write_callback| to be called once synchronously, on the | |
| 118 // next write event of |writer_|. | |
| 119 // |on_next_write_callback| must be thread-safe. | |
| 120 void RegisterOnNextWriteSynchronousCallback( | |
| 121 const base::Callback<void(bool success)>& on_next_write_callback); | |
| 122 | |
| 123 void ClearMutableValues() override; | 123 void ClearMutableValues() override; |
| 124 | 124 |
| 125 private: | 125 private: |
| 126 // Represents a histogram for recording the number of writes to the pref file | 126 // Represents a histogram for recording the number of writes to the pref file |
| 127 // that occur every kHistogramWriteReportIntervalInMins minutes. | 127 // that occur every kHistogramWriteReportIntervalInMins minutes. |
| 128 class COMPONENTS_PREFS_EXPORT WriteCountHistogram { | 128 class COMPONENTS_PREFS_EXPORT WriteCountHistogram { |
| 129 public: | 129 public: |
| 130 static const int32_t kHistogramWriteReportIntervalMins; | 130 static const int32_t kHistogramWriteReportIntervalMins; |
| 131 | 131 |
| 132 WriteCountHistogram(const base::TimeDelta& commit_interval, | 132 WriteCountHistogram(const base::TimeDelta& commit_interval, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, | 174 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, |
| 175 WriteCountHistogramTestBasic); | 175 WriteCountHistogramTestBasic); |
| 176 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, | 176 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, |
| 177 WriteCountHistogramTestSinglePeriod); | 177 WriteCountHistogramTestSinglePeriod); |
| 178 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, | 178 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, |
| 179 WriteCountHistogramTestMultiplePeriods); | 179 WriteCountHistogramTestMultiplePeriods); |
| 180 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, | 180 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, |
| 181 WriteCountHistogramTestPeriodWithGaps); | 181 WriteCountHistogramTestPeriodWithGaps); |
| 182 friend class base::JsonPrefStoreCallbackTest; | 182 friend class base::JsonPrefStoreCallbackTest; |
| 183 friend class base::JsonPrefStoreLossyWriteTest; | 183 friend class base::JsonPrefStoreLossyWriteTest; |
| 184 friend class base::WriteCallbacksObserver; |
| 184 | 185 |
| 185 ~JsonPrefStore() override; | 186 ~JsonPrefStore() override; |
| 186 | 187 |
| 187 // If |write_success| is true, runs |on_next_successful_write_|. | 188 // If |write_success| is true, runs |on_next_successful_write_|. |
| 188 // Otherwise, re-registers |on_next_successful_write_|. | 189 // Otherwise, re-registers |on_next_successful_write_|. |
| 189 void RunOrScheduleNextSuccessfulWriteCallback(bool write_success); | 190 void RunOrScheduleNextSuccessfulWriteCallback(bool write_success); |
| 190 | 191 |
| 191 // Handles the result of a write with result |write_success|. Runs | 192 // Handles the result of a write with result |write_success|. Runs |
| 192 // |on_next_write| callback on the current thread and posts | 193 // |on_next_write| callback on the current thread and posts |
| 193 // |RunOrScheduleNextSuccessfulWriteCallback| on |reply_task_runner|. | 194 // |RunOrScheduleNextSuccessfulWriteCallback| on |reply_task_runner|. |
| 194 static void PostWriteCallback( | 195 static void PostWriteCallback( |
| 195 const base::Callback<void(bool success)>& on_next_write_reply, | 196 const base::Callback<void(bool success)>& on_next_write_reply, |
| 196 const base::Callback<void(bool success)>& on_next_write_callback, | 197 const base::Callback<void(bool success)>& on_next_write_callback, |
| 197 scoped_refptr<base::SequencedTaskRunner> reply_task_runner, | 198 scoped_refptr<base::SequencedTaskRunner> reply_task_runner, |
| 198 bool write_success); | 199 bool write_success); |
| 199 | 200 |
| 201 // Registers the |callbacks| pair to be called once synchronously before and |
| 202 // after, respectively, the next write event of |writer_|. |
| 203 // Both callbacks must be thread-safe. |
| 204 void RegisterOnNextWriteSynchronousCallbacks(OnWriteCallbackPair callbacks); |
| 205 |
| 200 // This method is called after the JSON file has been read. It then hands | 206 // This method is called after the JSON file has been read. It then hands |
| 201 // |value| (or an empty dictionary in some read error cases) to the | 207 // |value| (or an empty dictionary in some read error cases) to the |
| 202 // |pref_filter| if one is set. It also gives a callback pointing at | 208 // |pref_filter| if one is set. It also gives a callback pointing at |
| 203 // FinalizeFileRead() to that |pref_filter_| which is then responsible for | 209 // FinalizeFileRead() to that |pref_filter_| which is then responsible for |
| 204 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead() | 210 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead() |
| 205 // is invoked directly. | 211 // is invoked directly. |
| 206 void OnFileRead(std::unique_ptr<ReadResult> read_result); | 212 void OnFileRead(std::unique_ptr<ReadResult> read_result); |
| 207 | 213 |
| 208 // ImportantFileWriter::DataSerializer overrides: | 214 // ImportantFileWriter::DataSerializer overrides: |
| 209 bool SerializeData(std::string* output) override; | 215 bool SerializeData(std::string* output) override; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 240 std::unique_ptr<ReadErrorDelegate> error_delegate_; | 246 std::unique_ptr<ReadErrorDelegate> error_delegate_; |
| 241 | 247 |
| 242 bool initialized_; | 248 bool initialized_; |
| 243 bool filtering_in_progress_; | 249 bool filtering_in_progress_; |
| 244 bool pending_lossy_write_; | 250 bool pending_lossy_write_; |
| 245 PrefReadError read_error_; | 251 PrefReadError read_error_; |
| 246 | 252 |
| 247 std::set<std::string> keys_need_empty_value_; | 253 std::set<std::string> keys_need_empty_value_; |
| 248 | 254 |
| 249 bool has_pending_successful_write_reply_; | 255 bool has_pending_successful_write_reply_; |
| 250 bool has_pending_write_callback_; | 256 bool has_pending_write_callbacks_; |
| 251 base::Closure on_next_successful_write_reply_; | 257 base::Closure on_next_successful_write_reply_; |
| 252 | 258 |
| 253 WriteCountHistogram write_count_histogram_; | 259 WriteCountHistogram write_count_histogram_; |
| 254 | 260 |
| 255 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); | 261 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); |
| 256 }; | 262 }; |
| 257 | 263 |
| 258 #endif // COMPONENTS_PREFS_JSON_PREF_STORE_H_ | 264 #endif // COMPONENTS_PREFS_JSON_PREF_STORE_H_ |
| OLD | NEW |