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" |
| 17 #include "base/memory/weak_ptr.h" |
16 #include "base/message_loop/message_loop_proxy.h" | 18 #include "base/message_loop/message_loop_proxy.h" |
17 #include "base/observer_list.h" | 19 #include "base/observer_list.h" |
18 #include "base/prefs/base_prefs_export.h" | 20 #include "base/prefs/base_prefs_export.h" |
19 #include "base/prefs/persistent_pref_store.h" | 21 #include "base/prefs/persistent_pref_store.h" |
20 | 22 |
21 class PrefFilter; | 23 class PrefFilter; |
22 | 24 |
23 namespace base { | 25 namespace base { |
24 class DictionaryValue; | 26 class DictionaryValue; |
25 class FilePath; | 27 class FilePath; |
26 class SequencedTaskRunner; | 28 class SequencedTaskRunner; |
27 class SequencedWorkerPool; | 29 class SequencedWorkerPool; |
28 class Value; | 30 class Value; |
29 } | 31 } |
30 | 32 |
31 | 33 |
32 // A writable PrefStore implementation that is used for user preferences. | 34 // A writable PrefStore implementation that is used for user preferences. |
33 class BASE_PREFS_EXPORT JsonPrefStore | 35 class BASE_PREFS_EXPORT JsonPrefStore |
34 : public PersistentPrefStore, | 36 : public PersistentPrefStore, |
35 public base::ImportantFileWriter::DataSerializer { | 37 public base::ImportantFileWriter::DataSerializer { |
36 public: | 38 public: |
| 39 // A callback to be invoked when |prefs| have been read (and possibly |
| 40 // pre-modified) and are now ready to be handed back to this callback's |
| 41 // builder. |schedule_write| indicates whether a write should be immediately |
| 42 // scheduled (typically because the OnFileReadInterceptor has already altered |
| 43 // the |prefs|). |
| 44 // Returns a weak pointer to this JsonPrefStore which can be used by the |
| 45 // caller for future cleanup operations while this JsonPrefStore is still up. |
| 46 typedef base::Callback<base::WeakPtr<JsonPrefStore>( |
| 47 scoped_ptr<base::DictionaryValue> prefs, |
| 48 bool schedule_write)> FinalizePrefsReadCallback; |
| 49 |
| 50 // A callback to be invoked from OnFileRead, after the read errors have been |
| 51 // handled, but before |prefs| is handed to this JsonPrefStore. The callee of |
| 52 // 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 const FinalizePrefsReadCallback& |
| 58 finalize_prefs_read)> OnFileReadInterceptor; |
| 59 |
37 // Returns instance of SequencedTaskRunner which guarantees that file | 60 // Returns instance of SequencedTaskRunner which guarantees that file |
38 // operations on the same file will be executed in sequenced order. | 61 // operations on the same file will be executed in sequenced order. |
39 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForFile( | 62 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForFile( |
40 const base::FilePath& pref_filename, | 63 const base::FilePath& pref_filename, |
41 base::SequencedWorkerPool* worker_pool); | 64 base::SequencedWorkerPool* worker_pool); |
42 | 65 |
43 // |sequenced_task_runner| is must be a shutdown-blocking task runner, ideally | 66 // |sequenced_task_runner| is must be a shutdown-blocking task runner, ideally |
44 // created by GetTaskRunnerForFile() method above. | 67 // created by GetTaskRunnerForFile() method above. |
45 JsonPrefStore(const base::FilePath& pref_filename, | 68 JsonPrefStore(const base::FilePath& pref_filename, |
46 base::SequencedTaskRunner* sequenced_task_runner, | 69 base::SequencedTaskRunner* sequenced_task_runner, |
(...skipping 14 matching lines...) Expand all Loading... |
61 virtual void SetValueSilently(const std::string& key, | 84 virtual void SetValueSilently(const std::string& key, |
62 base::Value* value) OVERRIDE; | 85 base::Value* value) OVERRIDE; |
63 virtual void RemoveValue(const std::string& key) OVERRIDE; | 86 virtual void RemoveValue(const std::string& key) OVERRIDE; |
64 virtual bool ReadOnly() const OVERRIDE; | 87 virtual bool ReadOnly() const OVERRIDE; |
65 virtual PrefReadError GetReadError() const OVERRIDE; | 88 virtual PrefReadError GetReadError() const OVERRIDE; |
66 virtual PrefReadError ReadPrefs() OVERRIDE; | 89 virtual PrefReadError ReadPrefs() OVERRIDE; |
67 virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) OVERRIDE; | 90 virtual void ReadPrefsAsync(ReadErrorDelegate* error_delegate) OVERRIDE; |
68 virtual void CommitPendingWrite() OVERRIDE; | 91 virtual void CommitPendingWrite() OVERRIDE; |
69 virtual void ReportValueChanged(const std::string& key) OVERRIDE; | 92 virtual void ReportValueChanged(const std::string& key) OVERRIDE; |
70 | 93 |
71 // This method is called after JSON file has been read. Method takes | 94 // Just like RemoveValue(), but doesn't notify observers. Used when doing some |
72 // ownership of the |value| pointer. Note, this method is used with | 95 // cleanup that shouldn't otherwise alert observers. |
73 // asynchronous file reading, so class exposes it only for the internal needs. | 96 void RemoveValueSilently(const std::string& key); |
74 // (read: do not call it manually). | 97 |
75 void OnFileRead(base::Value* value_owned, PrefReadError error, bool no_dir); | 98 // Registers |on_next_successful_write| to be called once, on the next |
| 99 // successful write event of |writer_|. |
| 100 void RegisterOnNextSuccessfulWriteCallback( |
| 101 const base::Closure& on_next_successful_write); |
| 102 |
| 103 // Registers |on_file_read_interceptor| to intercept the next OnFileRead |
| 104 // event. At most one OnFileReadInterceptor should be registered per file |
| 105 // read. |
| 106 void InterceptNextFileRead( |
| 107 const OnFileReadInterceptor& on_file_read_interceptor); |
| 108 |
| 109 // This method is called after the JSON file has been read. It then hands |
| 110 // |value| (or an empty dictionary in some read error cases) to the |
| 111 // |on_file_read_interceptor_| if one is set. It also gives a callback |
| 112 // pointing at FinalizeFileRead() to the |on_file_read_interceptor_| which is |
| 113 // then responsible for invoking it when done. If there is no |
| 114 // |on_file_read_interceptor_|, FinalizeFileRead() is invoked directly. |
| 115 // Note, this method is used with asynchronous file reading, so this class |
| 116 // exposes it only for the internal needs (read: do not call it manually). |
| 117 // TODO(gab): Move this method to the private section and hand a callback to |
| 118 // it to FileThreadDeserializer rather than exposing this public method and |
| 119 // giving a JsonPrefStore* to FileThreadDeserializer. |
| 120 void OnFileRead(scoped_ptr<base::Value> value, |
| 121 PrefReadError error, |
| 122 bool no_dir); |
76 | 123 |
77 private: | 124 private: |
78 virtual ~JsonPrefStore(); | 125 virtual ~JsonPrefStore(); |
79 | 126 |
80 // ImportantFileWriter::DataSerializer overrides: | 127 // ImportantFileWriter::DataSerializer overrides: |
81 virtual bool SerializeData(std::string* output) OVERRIDE; | 128 virtual bool SerializeData(std::string* output) OVERRIDE; |
82 | 129 |
| 130 // This method is called after the JSON file has been read and the result has |
| 131 // potentially been intercepted and modified by |on_file_read_interceptor_|. |
| 132 // |initialization_successful| is pre-determined by OnFileRead() and should |
| 133 // be used when reporting OnInitializationCompleted(). |
| 134 // |schedule_write| indicates whether a write should be immediately scheduled |
| 135 // (typically because the OnFileReadInterceptor has already altered the |
| 136 // |prefs|) -- this will be ignored if this store is read-only. |
| 137 // Returns a weak pointer to this JsonPrefStore which can be used for future |
| 138 // cleanup operations while this JsonPrefStore is still up. |
| 139 base::WeakPtr<JsonPrefStore> FinalizeFileRead( |
| 140 bool initialization_successful, |
| 141 scoped_ptr<base::DictionaryValue> prefs, |
| 142 bool schedule_write); |
| 143 |
| 144 // Callback to be invoked only once (and subsequently reset) on the next |
| 145 // OnFileRead event. It will be allowed to modify the JSON value read from |
| 146 // disk before this JsonPrefStore gets to declare itself initialized and |
| 147 // begins using it. It is responsible for handing it back to this |
| 148 // JsonPrefStore via the FinalizePrefReadCallback handed to it. |
| 149 // Note: Setting an |on_file_read_interceptor_| may make ReadPrefs() |
| 150 // asynchronous if the |on_file_read_interceptor_| is asynchronous. |
| 151 OnFileReadInterceptor on_file_read_interceptor_; |
| 152 |
83 base::FilePath path_; | 153 base::FilePath path_; |
84 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; | 154 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_; |
85 | 155 |
86 scoped_ptr<base::DictionaryValue> prefs_; | 156 scoped_ptr<base::DictionaryValue> prefs_; |
87 | 157 |
88 bool read_only_; | 158 bool read_only_; |
89 | 159 |
90 // Helper for safely writing pref data. | 160 // Helper for safely writing pref data. |
91 base::ImportantFileWriter writer_; | 161 base::ImportantFileWriter writer_; |
92 | 162 |
93 scoped_ptr<PrefFilter> pref_filter_; | 163 scoped_ptr<PrefFilter> pref_filter_; |
94 ObserverList<PrefStore::Observer, true> observers_; | 164 ObserverList<PrefStore::Observer, true> observers_; |
95 | 165 |
96 scoped_ptr<ReadErrorDelegate> error_delegate_; | 166 scoped_ptr<ReadErrorDelegate> error_delegate_; |
97 | 167 |
98 bool initialized_; | 168 bool initialized_; |
99 PrefReadError read_error_; | 169 PrefReadError read_error_; |
100 | 170 |
101 std::set<std::string> keys_need_empty_value_; | 171 std::set<std::string> keys_need_empty_value_; |
102 | 172 |
| 173 base::WeakPtrFactory<JsonPrefStore> weak_factory_; |
| 174 |
103 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); | 175 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); |
104 }; | 176 }; |
105 | 177 |
106 #endif // BASE_PREFS_JSON_PREF_STORE_H_ | 178 #endif // BASE_PREFS_JSON_PREF_STORE_H_ |
OLD | NEW |