| 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 <set> | 11 #include <set> |
| 11 #include <string> | 12 #include <string> |
| 12 | 13 |
| 13 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
| 14 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 15 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 16 #include "base/files/important_file_writer.h" | 17 #include "base/files/important_file_writer.h" |
| 17 #include "base/gtest_prod_util.h" | 18 #include "base/gtest_prod_util.h" |
| 18 #include "base/macros.h" | 19 #include "base/macros.h" |
| 19 #include "base/memory/scoped_ptr.h" | |
| 20 #include "base/memory/weak_ptr.h" | 20 #include "base/memory/weak_ptr.h" |
| 21 #include "base/observer_list.h" | 21 #include "base/observer_list.h" |
| 22 #include "base/threading/non_thread_safe.h" | 22 #include "base/threading/non_thread_safe.h" |
| 23 #include "components/prefs/base_prefs_export.h" | 23 #include "components/prefs/base_prefs_export.h" |
| 24 #include "components/prefs/persistent_pref_store.h" | 24 #include "components/prefs/persistent_pref_store.h" |
| 25 | 25 |
| 26 class PrefFilter; | 26 class PrefFilter; |
| 27 | 27 |
| 28 namespace base { | 28 namespace base { |
| 29 class Clock; | 29 class Clock; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 52 // Returns instance of SequencedTaskRunner which guarantees that file | 52 // Returns instance of SequencedTaskRunner which guarantees that file |
| 53 // operations on the same file will be executed in sequenced order. | 53 // operations on the same file will be executed in sequenced order. |
| 54 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForFile( | 54 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForFile( |
| 55 const base::FilePath& pref_filename, | 55 const base::FilePath& pref_filename, |
| 56 base::SequencedWorkerPool* worker_pool); | 56 base::SequencedWorkerPool* worker_pool); |
| 57 | 57 |
| 58 // Same as the constructor below with no alternate filename. | 58 // Same as the constructor below with no alternate filename. |
| 59 JsonPrefStore( | 59 JsonPrefStore( |
| 60 const base::FilePath& pref_filename, | 60 const base::FilePath& pref_filename, |
| 61 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, | 61 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, |
| 62 scoped_ptr<PrefFilter> pref_filter); | 62 std::unique_ptr<PrefFilter> pref_filter); |
| 63 | 63 |
| 64 // |sequenced_task_runner| must be a shutdown-blocking task runner, ideally | 64 // |sequenced_task_runner| must be a shutdown-blocking task runner, ideally |
| 65 // created by the GetTaskRunnerForFile() method above. | 65 // created by the GetTaskRunnerForFile() method above. |
| 66 // |pref_filename| is the path to the file to read prefs from. | 66 // |pref_filename| is the path to the file to read prefs from. |
| 67 // |pref_alternate_filename| is the path to an alternate file which the | 67 // |pref_alternate_filename| is the path to an alternate file which the |
| 68 // desired prefs may have previously been written to. If |pref_filename| | 68 // desired prefs may have previously been written to. If |pref_filename| |
| 69 // doesn't exist and |pref_alternate_filename| does, |pref_alternate_filename| | 69 // doesn't exist and |pref_alternate_filename| does, |pref_alternate_filename| |
| 70 // will be moved to |pref_filename| before the read occurs. | 70 // will be moved to |pref_filename| before the read occurs. |
| 71 JsonPrefStore( | 71 JsonPrefStore( |
| 72 const base::FilePath& pref_filename, | 72 const base::FilePath& pref_filename, |
| 73 const base::FilePath& pref_alternate_filename, | 73 const base::FilePath& pref_alternate_filename, |
| 74 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, | 74 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, |
| 75 scoped_ptr<PrefFilter> pref_filter); | 75 std::unique_ptr<PrefFilter> pref_filter); |
| 76 | 76 |
| 77 // PrefStore overrides: | 77 // PrefStore overrides: |
| 78 bool GetValue(const std::string& key, | 78 bool GetValue(const std::string& key, |
| 79 const base::Value** result) const override; | 79 const base::Value** result) const override; |
| 80 void AddObserver(PrefStore::Observer* observer) override; | 80 void AddObserver(PrefStore::Observer* observer) override; |
| 81 void RemoveObserver(PrefStore::Observer* observer) override; | 81 void RemoveObserver(PrefStore::Observer* observer) override; |
| 82 bool HasObservers() const override; | 82 bool HasObservers() const override; |
| 83 bool IsInitializationComplete() const override; | 83 bool IsInitializationComplete() const override; |
| 84 | 84 |
| 85 // PersistentPrefStore overrides: | 85 // PersistentPrefStore overrides: |
| 86 bool GetMutableValue(const std::string& key, base::Value** result) override; | 86 bool GetMutableValue(const std::string& key, base::Value** result) override; |
| 87 void SetValue(const std::string& key, | 87 void SetValue(const std::string& key, |
| 88 scoped_ptr<base::Value> value, | 88 std::unique_ptr<base::Value> value, |
| 89 uint32_t flags) override; | 89 uint32_t flags) override; |
| 90 void SetValueSilently(const std::string& key, | 90 void SetValueSilently(const std::string& key, |
| 91 scoped_ptr<base::Value> value, | 91 std::unique_ptr<base::Value> value, |
| 92 uint32_t flags) override; | 92 uint32_t flags) override; |
| 93 void RemoveValue(const std::string& key, uint32_t flags) override; | 93 void RemoveValue(const std::string& key, uint32_t flags) override; |
| 94 bool ReadOnly() const override; | 94 bool ReadOnly() const override; |
| 95 PrefReadError GetReadError() const override; | 95 PrefReadError GetReadError() const override; |
| 96 // Note this method may be asynchronous if this instance has a |pref_filter_| | 96 // Note this method may be asynchronous if this instance has a |pref_filter_| |
| 97 // in which case it will return PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE. | 97 // in which case it will return PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE. |
| 98 // See details in pref_filter.h. | 98 // See details in pref_filter.h. |
| 99 PrefReadError ReadPrefs() override; | 99 PrefReadError ReadPrefs() override; |
| 100 void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override; | 100 void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override; |
| 101 void CommitPendingWrite() override; | 101 void CommitPendingWrite() override; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 119 class COMPONENTS_PREFS_EXPORT WriteCountHistogram { | 119 class COMPONENTS_PREFS_EXPORT WriteCountHistogram { |
| 120 public: | 120 public: |
| 121 static const int32_t kHistogramWriteReportIntervalMins; | 121 static const int32_t kHistogramWriteReportIntervalMins; |
| 122 | 122 |
| 123 WriteCountHistogram(const base::TimeDelta& commit_interval, | 123 WriteCountHistogram(const base::TimeDelta& commit_interval, |
| 124 const base::FilePath& path); | 124 const base::FilePath& path); |
| 125 // Constructor for testing. |clock| is a test Clock that is used to retrieve | 125 // Constructor for testing. |clock| is a test Clock that is used to retrieve |
| 126 // the time. | 126 // the time. |
| 127 WriteCountHistogram(const base::TimeDelta& commit_interval, | 127 WriteCountHistogram(const base::TimeDelta& commit_interval, |
| 128 const base::FilePath& path, | 128 const base::FilePath& path, |
| 129 scoped_ptr<base::Clock> clock); | 129 std::unique_ptr<base::Clock> clock); |
| 130 ~WriteCountHistogram(); | 130 ~WriteCountHistogram(); |
| 131 | 131 |
| 132 // Record that a write has occured. | 132 // Record that a write has occured. |
| 133 void RecordWriteOccured(); | 133 void RecordWriteOccured(); |
| 134 | 134 |
| 135 // Reports writes (that have not yet been reported) in all of the recorded | 135 // Reports writes (that have not yet been reported) in all of the recorded |
| 136 // intervals that have elapsed up until current time. | 136 // intervals that have elapsed up until current time. |
| 137 void ReportOutstandingWrites(); | 137 void ReportOutstandingWrites(); |
| 138 | 138 |
| 139 base::HistogramBase* GetHistogram(); | 139 base::HistogramBase* GetHistogram(); |
| 140 | 140 |
| 141 private: | 141 private: |
| 142 // The minimum interval at which writes can occur. | 142 // The minimum interval at which writes can occur. |
| 143 const base::TimeDelta commit_interval_; | 143 const base::TimeDelta commit_interval_; |
| 144 | 144 |
| 145 // The path to the file. | 145 // The path to the file. |
| 146 const base::FilePath path_; | 146 const base::FilePath path_; |
| 147 | 147 |
| 148 // Clock which is used to retrieve the current time. | 148 // Clock which is used to retrieve the current time. |
| 149 scoped_ptr<base::Clock> clock_; | 149 std::unique_ptr<base::Clock> clock_; |
| 150 | 150 |
| 151 // The interval at which to report write counts. | 151 // The interval at which to report write counts. |
| 152 const base::TimeDelta report_interval_; | 152 const base::TimeDelta report_interval_; |
| 153 | 153 |
| 154 // The time at which the last histogram value was reported for the number | 154 // The time at which the last histogram value was reported for the number |
| 155 // of write counts. | 155 // of write counts. |
| 156 base::Time last_report_time_; | 156 base::Time last_report_time_; |
| 157 | 157 |
| 158 // The number of writes that have occured since the last write count was | 158 // The number of writes that have occured since the last write count was |
| 159 // reported. | 159 // reported. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 173 friend class base::JsonPrefStoreLossyWriteTest; | 173 friend class base::JsonPrefStoreLossyWriteTest; |
| 174 | 174 |
| 175 ~JsonPrefStore() override; | 175 ~JsonPrefStore() override; |
| 176 | 176 |
| 177 // This method is called after the JSON file has been read. It then hands | 177 // This method is called after the JSON file has been read. It then hands |
| 178 // |value| (or an empty dictionary in some read error cases) to the | 178 // |value| (or an empty dictionary in some read error cases) to the |
| 179 // |pref_filter| if one is set. It also gives a callback pointing at | 179 // |pref_filter| if one is set. It also gives a callback pointing at |
| 180 // FinalizeFileRead() to that |pref_filter_| which is then responsible for | 180 // FinalizeFileRead() to that |pref_filter_| which is then responsible for |
| 181 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead() | 181 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead() |
| 182 // is invoked directly. | 182 // is invoked directly. |
| 183 void OnFileRead(scoped_ptr<ReadResult> read_result); | 183 void OnFileRead(std::unique_ptr<ReadResult> read_result); |
| 184 | 184 |
| 185 // ImportantFileWriter::DataSerializer overrides: | 185 // ImportantFileWriter::DataSerializer overrides: |
| 186 bool SerializeData(std::string* output) override; | 186 bool SerializeData(std::string* output) override; |
| 187 | 187 |
| 188 // This method is called after the JSON file has been read and the result has | 188 // This method is called after the JSON file has been read and the result has |
| 189 // potentially been intercepted and modified by |pref_filter_|. | 189 // potentially been intercepted and modified by |pref_filter_|. |
| 190 // |initialization_successful| is pre-determined by OnFileRead() and should | 190 // |initialization_successful| is pre-determined by OnFileRead() and should |
| 191 // be used when reporting OnInitializationCompleted(). | 191 // be used when reporting OnInitializationCompleted(). |
| 192 // |schedule_write| indicates whether a write should be immediately scheduled | 192 // |schedule_write| indicates whether a write should be immediately scheduled |
| 193 // (typically because the |pref_filter_| has already altered the |prefs|) -- | 193 // (typically because the |pref_filter_| has already altered the |prefs|) -- |
| 194 // this will be ignored if this store is read-only. | 194 // this will be ignored if this store is read-only. |
| 195 void FinalizeFileRead(bool initialization_successful, | 195 void FinalizeFileRead(bool initialization_successful, |
| 196 scoped_ptr<base::DictionaryValue> prefs, | 196 std::unique_ptr<base::DictionaryValue> prefs, |
| 197 bool schedule_write); | 197 bool schedule_write); |
| 198 | 198 |
| 199 // Schedule a write with the file writer as long as |flags| doesn't contain | 199 // Schedule a write with the file writer as long as |flags| doesn't contain |
| 200 // WriteablePrefStore::LOSSY_PREF_WRITE_FLAG. | 200 // WriteablePrefStore::LOSSY_PREF_WRITE_FLAG. |
| 201 void ScheduleWrite(uint32_t flags); | 201 void ScheduleWrite(uint32_t flags); |
| 202 | 202 |
| 203 const base::FilePath path_; | 203 const base::FilePath path_; |
| 204 const base::FilePath alternate_path_; | 204 const base::FilePath alternate_path_; |
| 205 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; | 205 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; |
| 206 | 206 |
| 207 scoped_ptr<base::DictionaryValue> prefs_; | 207 std::unique_ptr<base::DictionaryValue> prefs_; |
| 208 | 208 |
| 209 bool read_only_; | 209 bool read_only_; |
| 210 | 210 |
| 211 // Helper for safely writing pref data. | 211 // Helper for safely writing pref data. |
| 212 base::ImportantFileWriter writer_; | 212 base::ImportantFileWriter writer_; |
| 213 | 213 |
| 214 scoped_ptr<PrefFilter> pref_filter_; | 214 std::unique_ptr<PrefFilter> pref_filter_; |
| 215 base::ObserverList<PrefStore::Observer, true> observers_; | 215 base::ObserverList<PrefStore::Observer, true> observers_; |
| 216 | 216 |
| 217 scoped_ptr<ReadErrorDelegate> error_delegate_; | 217 std::unique_ptr<ReadErrorDelegate> error_delegate_; |
| 218 | 218 |
| 219 bool initialized_; | 219 bool initialized_; |
| 220 bool filtering_in_progress_; | 220 bool filtering_in_progress_; |
| 221 bool pending_lossy_write_; | 221 bool pending_lossy_write_; |
| 222 PrefReadError read_error_; | 222 PrefReadError read_error_; |
| 223 | 223 |
| 224 std::set<std::string> keys_need_empty_value_; | 224 std::set<std::string> keys_need_empty_value_; |
| 225 | 225 |
| 226 WriteCountHistogram write_count_histogram_; | 226 WriteCountHistogram write_count_histogram_; |
| 227 | 227 |
| 228 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); | 228 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); |
| 229 }; | 229 }; |
| 230 | 230 |
| 231 #endif // COMPONENTS_PREFS_JSON_PREF_STORE_H_ | 231 #endif // COMPONENTS_PREFS_JSON_PREF_STORE_H_ |
| OLD | NEW |