Chromium Code Reviews| 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 immediately | |
| 41 // scheduled (typically because the OnFileReadInterceptor has already altered | |
| 42 // the |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 // Note, this method is used with asynchronous file reading, so class exposes | |
|
Bernhard Bauer
2014/04/29 16:38:54
Nit: "this class"
gab
2014/04/29 20:12:14
Done.
| |
| 107 // it only for the internal needs (read: do not call it manually). | |
|
Bernhard Bauer
2014/04/29 16:38:54
Wait, now I'm confused. Who calls this method, and
gab
2014/04/29 20:12:14
This is just putting back a comment that was on th
Bernhard Bauer
2014/04/29 21:20:28
Oh, OK. If you want to update it, that would be gr
gab
2014/04/29 22:36:05
SG, added TODO.
| |
| 108 void OnFileRead(scoped_ptr<base::Value> value, | |
| 109 PrefReadError error, | |
| 110 bool no_dir); | |
| 76 | 111 |
| 77 private: | 112 private: |
| 78 virtual ~JsonPrefStore(); | 113 virtual ~JsonPrefStore(); |
| 79 | 114 |
| 80 // ImportantFileWriter::DataSerializer overrides: | 115 // ImportantFileWriter::DataSerializer overrides: |
| 81 virtual bool SerializeData(std::string* output) OVERRIDE; | 116 virtual bool SerializeData(std::string* output) OVERRIDE; |
| 82 | 117 |
| 118 // This method is called after the JSON file has been read and the result has | |
| 119 // potentially been intercepted and modified by |on_file_read_interceptor_|. | |
| 120 // |initialization_successful| is pre-determined by OnFileRead() and should | |
| 121 // be used when reporting OnInitializationCompleted(). | |
| 122 // |schedule_write| indicates whether a write should be immediately scheduled | |
| 123 // (typically because the OnFileReadInterceptor has already altered the | |
| 124 // |prefs|) -- this will be ignored if this store is read-only. | |
| 125 void FinalizeFileRead(bool initialization_successful, | |
| 126 scoped_ptr<base::DictionaryValue> prefs, | |
| 127 bool schedule_write); | |
| 128 | |
| 129 // Callback to be invoked only once (and subsequently deleted) on the next | |
| 130 // OnFileRead event. It will be allowed to modify the JSON value read from | |
| 131 // disk before this JsonPrefStore gets to declare itself initialized and | |
| 132 // begins using it. It is responsible for handing it back to this | |
| 133 // JsonPrefStore via the FinalizePrefReadCallback handed to it. | |
| 134 // Note: Setting an |on_file_read_interceptor_| may make ReadPrefs() | |
| 135 // asynchronous if the |on_file_read_interceptor_| is asynchronous. | |
| 136 scoped_ptr<OnFileReadInterceptor> on_file_read_interceptor_; | |
| 137 | |
| 83 base::FilePath path_; | 138 base::FilePath path_; |
| 84 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; | 139 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; |
| 85 | 140 |
| 86 scoped_ptr<base::DictionaryValue> prefs_; | 141 scoped_ptr<base::DictionaryValue> prefs_; |
| 87 | 142 |
| 88 bool read_only_; | 143 bool read_only_; |
| 89 | 144 |
| 90 // Helper for safely writing pref data. | 145 // Helper for safely writing pref data. |
| 91 base::ImportantFileWriter writer_; | 146 base::ImportantFileWriter writer_; |
| 92 | 147 |
| 93 scoped_ptr<PrefFilter> pref_filter_; | 148 scoped_ptr<PrefFilter> pref_filter_; |
| 94 ObserverList<PrefStore::Observer, true> observers_; | 149 ObserverList<PrefStore::Observer, true> observers_; |
| 95 | 150 |
| 96 scoped_ptr<ReadErrorDelegate> error_delegate_; | 151 scoped_ptr<ReadErrorDelegate> error_delegate_; |
| 97 | 152 |
| 98 bool initialized_; | 153 bool initialized_; |
| 99 PrefReadError read_error_; | 154 PrefReadError read_error_; |
| 100 | 155 |
| 101 std::set<std::string> keys_need_empty_value_; | 156 std::set<std::string> keys_need_empty_value_; |
| 102 | 157 |
| 103 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); | 158 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); |
| 104 }; | 159 }; |
| 105 | 160 |
| 106 #endif // BASE_PREFS_JSON_PREF_STORE_H_ | 161 #endif // BASE_PREFS_JSON_PREF_STORE_H_ |
| OLD | NEW |