brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 5 | #ifndef COMPONENTS_PREFS_JSON_PREF_STORE_H_ |
| 6 | #define COMPONENTS_PREFS_JSON_PREF_STORE_H_ |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 7 | |
| 8 | #include <stdint.h> |
| 9 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 10 | #include <memory> |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 11 | #include <set> |
| 12 | #include <string> |
| 13 | |
| 14 | #include "base/callback_forward.h" |
| 15 | #include "base/compiler_specific.h" |
| 16 | #include "base/files/file_path.h" |
| 17 | #include "base/files/important_file_writer.h" |
| 18 | #include "base/gtest_prod_util.h" |
| 19 | #include "base/macros.h" |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 20 | #include "base/memory/weak_ptr.h" |
| 21 | #include "base/observer_list.h" |
gab | 6e1fb535 | 2017-05-31 18:27:12 | [diff] [blame] | 22 | #include "base/sequence_checker.h" |
Gabriel Charette | 44db142 | 2018-08-06 11:19:33 | [diff] [blame] | 23 | #include "base/task/post_task.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 24 | #include "components/prefs/persistent_pref_store.h" |
fdoray | 5f4a7a0b | 2017-05-15 16:15:58 | [diff] [blame] | 25 | #include "components/prefs/pref_filter.h" |
Brett Wilson | 5c6cf26 | 2017-09-09 02:05:54 | [diff] [blame] | 26 | #include "components/prefs/prefs_export.h" |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 27 | |
| 28 | class PrefFilter; |
| 29 | |
| 30 | namespace base { |
| 31 | class Clock; |
| 32 | class DictionaryValue; |
| 33 | class FilePath; |
| 34 | class HistogramBase; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 35 | class JsonPrefStoreCallbackTest; |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 36 | class JsonPrefStoreLossyWriteTest; |
| 37 | class SequencedTaskRunner; |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 38 | class WriteCallbacksObserver; |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 39 | class Value; |
| 40 | FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestBasic); |
| 41 | FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod); |
| 42 | FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods); |
| 43 | FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps); |
| 44 | } |
| 45 | |
| 46 | // A writable PrefStore implementation that is used for user preferences. |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 47 | class COMPONENTS_PREFS_EXPORT JsonPrefStore |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 48 | : public PersistentPrefStore, |
| 49 | public base::ImportantFileWriter::DataSerializer, |
gab | 6e1fb535 | 2017-05-31 18:27:12 | [diff] [blame] | 50 | public base::SupportsWeakPtr<JsonPrefStore> { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 51 | public: |
| 52 | struct ReadResult; |
| 53 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 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 | |
fdoray | 5f4a7a0b | 2017-05-15 16:15:58 | [diff] [blame] | 59 | // |pref_filename| is the path to the file to read prefs from. It is incorrect |
| 60 | // to create multiple JsonPrefStore with the same |pref_filename|. |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 61 | // |file_task_runner| is used for asynchronous reads and writes. It must |
fdoray | 5f4a7a0b | 2017-05-15 16:15:58 | [diff] [blame] | 62 | // have the base::TaskShutdownBehavior::BLOCK_SHUTDOWN and base::MayBlock() |
| 63 | // traits. Unless external tasks need to run on the same sequence as |
| 64 | // JsonPrefStore tasks, keep the default value. |
Gabriel Charette | 1f53ca3 | 2018-08-07 21:31:17 | [diff] [blame] | 65 | // The initial read is done synchronously, the TaskPriority is thus only used |
| 66 | // for flushes to disks and BACKGROUND is therefore appropriate. Priority of |
| 67 | // remaining BACKGROUND+BLOCK_SHUTDOWN tasks is bumped by the TaskScheduler on |
| 68 | // shutdown. However, some shutdown use cases happen without |
| 69 | // TaskScheduler::Shutdown() (e.g. ChromeRestartRequest::Start() and |
| 70 | // BrowserProcessImpl::EndSession()) and we must thus unfortunately make this |
| 71 | // USER_VISIBLE until we solve https://crbug.com/747495 to allow bumping |
| 72 | // priority of a sequence on demand. |
fdoray | 5f4a7a0b | 2017-05-15 16:15:58 | [diff] [blame] | 73 | JsonPrefStore(const base::FilePath& pref_filename, |
Gabriel Charette | 1f53ca3 | 2018-08-07 21:31:17 | [diff] [blame] | 74 | std::unique_ptr<PrefFilter> pref_filter = nullptr, |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 75 | scoped_refptr<base::SequencedTaskRunner> file_task_runner = |
fdoray | 5f4a7a0b | 2017-05-15 16:15:58 | [diff] [blame] | 76 | base::CreateSequencedTaskRunnerWithTraits( |
Gabriel Charette | 1f53ca3 | 2018-08-07 21:31:17 | [diff] [blame] | 77 | {base::MayBlock(), base::TaskPriority::USER_VISIBLE, |
| 78 | base::TaskShutdownBehavior::BLOCK_SHUTDOWN})); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 79 | |
| 80 | // PrefStore overrides: |
| 81 | bool GetValue(const std::string& key, |
| 82 | const base::Value** result) const override; |
tibell | e23659b4 | 2017-02-23 01:44:13 | [diff] [blame] | 83 | std::unique_ptr<base::DictionaryValue> GetValues() const override; |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 84 | void AddObserver(PrefStore::Observer* observer) override; |
| 85 | void RemoveObserver(PrefStore::Observer* observer) override; |
| 86 | bool HasObservers() const override; |
| 87 | bool IsInitializationComplete() const override; |
| 88 | |
| 89 | // PersistentPrefStore overrides: |
| 90 | bool GetMutableValue(const std::string& key, base::Value** result) override; |
| 91 | void SetValue(const std::string& key, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 92 | std::unique_ptr<base::Value> value, |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 93 | uint32_t flags) override; |
| 94 | void SetValueSilently(const std::string& key, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 95 | std::unique_ptr<base::Value> value, |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 96 | uint32_t flags) override; |
| 97 | void RemoveValue(const std::string& key, uint32_t flags) override; |
| 98 | bool ReadOnly() const override; |
| 99 | PrefReadError GetReadError() const override; |
| 100 | // Note this method may be asynchronous if this instance has a |pref_filter_| |
| 101 | // in which case it will return PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE. |
| 102 | // See details in pref_filter.h. |
| 103 | PrefReadError ReadPrefs() override; |
| 104 | void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override; |
Gabriel Charette | 788eaf6 | 2018-08-07 20:11:46 | [diff] [blame] | 105 | void CommitPendingWrite( |
| 106 | base::OnceClosure reply_callback = base::OnceClosure(), |
| 107 | base::OnceClosure synchronous_done_callback = |
| 108 | base::OnceClosure()) override; |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 109 | void SchedulePendingLossyWrites() override; |
| 110 | void ReportValueChanged(const std::string& key, uint32_t flags) override; |
| 111 | |
| 112 | // Just like RemoveValue(), but doesn't notify observers. Used when doing some |
| 113 | // cleanup that shouldn't otherwise alert observers. |
| 114 | void RemoveValueSilently(const std::string& key, uint32_t flags); |
| 115 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 116 | // Registers |on_next_successful_write_reply| to be called once, on the next |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 117 | // successful write event of |writer_|. |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 118 | // |on_next_successful_write_reply| will be called on the thread from which |
| 119 | // this method is called and does not need to be thread safe. |
| 120 | void RegisterOnNextSuccessfulWriteReply( |
| 121 | const base::Closure& on_next_successful_write_reply); |
| 122 | |
dvadym | 53fc0d4 | 2016-02-05 13:34:57 | [diff] [blame] | 123 | void ClearMutableValues() override; |
| 124 | |
proberge | 45e34728 | 2017-08-16 21:24:05 | [diff] [blame] | 125 | void OnStoreDeletionFromDisk() override; |
| 126 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 127 | private: |
| 128 | // Represents a histogram for recording the number of writes to the pref file |
| 129 | // that occur every kHistogramWriteReportIntervalInMins minutes. |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 130 | class COMPONENTS_PREFS_EXPORT WriteCountHistogram { |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 131 | public: |
| 132 | static const int32_t kHistogramWriteReportIntervalMins; |
| 133 | |
| 134 | WriteCountHistogram(const base::TimeDelta& commit_interval, |
| 135 | const base::FilePath& path); |
| 136 | // Constructor for testing. |clock| is a test Clock that is used to retrieve |
| 137 | // the time. |
| 138 | WriteCountHistogram(const base::TimeDelta& commit_interval, |
| 139 | const base::FilePath& path, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 140 | std::unique_ptr<base::Clock> clock); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 141 | ~WriteCountHistogram(); |
| 142 | |
| 143 | // Record that a write has occured. |
| 144 | void RecordWriteOccured(); |
| 145 | |
| 146 | // Reports writes (that have not yet been reported) in all of the recorded |
| 147 | // intervals that have elapsed up until current time. |
| 148 | void ReportOutstandingWrites(); |
| 149 | |
| 150 | base::HistogramBase* GetHistogram(); |
| 151 | |
| 152 | private: |
| 153 | // The minimum interval at which writes can occur. |
| 154 | const base::TimeDelta commit_interval_; |
| 155 | |
| 156 | // The path to the file. |
| 157 | const base::FilePath path_; |
| 158 | |
| 159 | // Clock which is used to retrieve the current time. |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 160 | std::unique_ptr<base::Clock> clock_; |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 161 | |
| 162 | // The interval at which to report write counts. |
| 163 | const base::TimeDelta report_interval_; |
| 164 | |
| 165 | // The time at which the last histogram value was reported for the number |
| 166 | // of write counts. |
| 167 | base::Time last_report_time_; |
| 168 | |
| 169 | // The number of writes that have occured since the last write count was |
| 170 | // reported. |
| 171 | uint32_t writes_since_last_report_; |
| 172 | |
| 173 | DISALLOW_COPY_AND_ASSIGN(WriteCountHistogram); |
| 174 | }; |
| 175 | |
| 176 | FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, |
| 177 | WriteCountHistogramTestBasic); |
| 178 | FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, |
| 179 | WriteCountHistogramTestSinglePeriod); |
| 180 | FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, |
| 181 | WriteCountHistogramTestMultiplePeriods); |
| 182 | FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, |
| 183 | WriteCountHistogramTestPeriodWithGaps); |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 184 | friend class base::JsonPrefStoreCallbackTest; |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 185 | friend class base::JsonPrefStoreLossyWriteTest; |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 186 | friend class base::WriteCallbacksObserver; |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 187 | |
| 188 | ~JsonPrefStore() override; |
| 189 | |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 190 | // If |write_success| is true, runs |on_next_successful_write_|. |
| 191 | // Otherwise, re-registers |on_next_successful_write_|. |
| 192 | void RunOrScheduleNextSuccessfulWriteCallback(bool write_success); |
| 193 | |
| 194 | // Handles the result of a write with result |write_success|. Runs |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 195 | // |on_next_write_callback| on the current thread and posts |
| 196 | // |on_next_write_reply| on |reply_task_runner|. |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 197 | static void PostWriteCallback( |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 198 | const base::Callback<void(bool success)>& on_next_write_callback, |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 199 | const base::Callback<void(bool success)>& on_next_write_reply, |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 200 | scoped_refptr<base::SequencedTaskRunner> reply_task_runner, |
| 201 | bool write_success); |
| 202 | |
proberge | c503d69 | 2016-09-28 19:51:05 | [diff] [blame] | 203 | // Registers the |callbacks| pair to be called once synchronously before and |
| 204 | // after, respectively, the next write event of |writer_|. |
| 205 | // Both callbacks must be thread-safe. |
| 206 | void RegisterOnNextWriteSynchronousCallbacks(OnWriteCallbackPair callbacks); |
| 207 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 208 | // This method is called after the JSON file has been read. It then hands |
| 209 | // |value| (or an empty dictionary in some read error cases) to the |
| 210 | // |pref_filter| if one is set. It also gives a callback pointing at |
| 211 | // FinalizeFileRead() to that |pref_filter_| which is then responsible for |
| 212 | // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead() |
| 213 | // is invoked directly. |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 214 | void OnFileRead(std::unique_ptr<ReadResult> read_result); |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 215 | |
| 216 | // ImportantFileWriter::DataSerializer overrides: |
| 217 | bool SerializeData(std::string* output) override; |
| 218 | |
| 219 | // This method is called after the JSON file has been read and the result has |
| 220 | // potentially been intercepted and modified by |pref_filter_|. |
| 221 | // |initialization_successful| is pre-determined by OnFileRead() and should |
| 222 | // be used when reporting OnInitializationCompleted(). |
| 223 | // |schedule_write| indicates whether a write should be immediately scheduled |
| 224 | // (typically because the |pref_filter_| has already altered the |prefs|) -- |
| 225 | // this will be ignored if this store is read-only. |
| 226 | void FinalizeFileRead(bool initialization_successful, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 227 | std::unique_ptr<base::DictionaryValue> prefs, |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 228 | bool schedule_write); |
| 229 | |
| 230 | // Schedule a write with the file writer as long as |flags| doesn't contain |
| 231 | // WriteablePrefStore::LOSSY_PREF_WRITE_FLAG. |
| 232 | void ScheduleWrite(uint32_t flags); |
| 233 | |
| 234 | const base::FilePath path_; |
Francois Doray | 405dd2d | 2017-06-09 15:23:33 | [diff] [blame] | 235 | const scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 236 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 237 | std::unique_ptr<base::DictionaryValue> prefs_; |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 238 | |
| 239 | bool read_only_; |
| 240 | |
| 241 | // Helper for safely writing pref data. |
| 242 | base::ImportantFileWriter writer_; |
| 243 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 244 | std::unique_ptr<PrefFilter> pref_filter_; |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 245 | base::ObserverList<PrefStore::Observer, true>::Unchecked observers_; |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 246 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 247 | std::unique_ptr<ReadErrorDelegate> error_delegate_; |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 248 | |
| 249 | bool initialized_; |
| 250 | bool filtering_in_progress_; |
| 251 | bool pending_lossy_write_; |
| 252 | PrefReadError read_error_; |
| 253 | |
| 254 | std::set<std::string> keys_need_empty_value_; |
| 255 | |
proberge | 269fd09 | 2016-10-04 22:13:41 | [diff] [blame] | 256 | bool has_pending_write_reply_ = true; |
proberge | fc46ac1 | 2016-09-21 18:03:00 | [diff] [blame] | 257 | base::Closure on_next_successful_write_reply_; |
| 258 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 259 | WriteCountHistogram write_count_histogram_; |
| 260 | |
gab | 6e1fb535 | 2017-05-31 18:27:12 | [diff] [blame] | 261 | SEQUENCE_CHECKER(sequence_checker_); |
| 262 | |
brettw | 58cd1f1 | 2016-01-30 05:56:05 | [diff] [blame] | 263 | DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); |
| 264 | }; |
| 265 | |
brettw | 06650868 | 2016-02-03 08:22:02 | [diff] [blame] | 266 | #endif // COMPONENTS_PREFS_JSON_PREF_STORE_H_ |