Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(149)

Side by Side Diff: base/prefs/json_pref_store.h

Issue 1648403002: Move base/prefs to components/prefs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/prefs/default_pref_store_unittest.cc ('k') | base/prefs/json_pref_store.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // TODO(brettw) remove this forwarding header when prefs is completely moved to
6 #define BASE_PREFS_JSON_PREF_STORE_H_ 6 // components.
7 7 #include "components/prefs/json_pref_store.h"
8 #include <stdint.h>
9
10 #include <set>
11 #include <string>
12
13 #include "base/callback_forward.h"
14 #include "base/compiler_specific.h"
15 #include "base/files/file_path.h"
16 #include "base/files/important_file_writer.h"
17 #include "base/gtest_prod_util.h"
18 #include "base/macros.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/memory/weak_ptr.h"
21 #include "base/observer_list.h"
22 #include "base/prefs/base_prefs_export.h"
23 #include "base/prefs/persistent_pref_store.h"
24 #include "base/threading/non_thread_safe.h"
25
26 class PrefFilter;
27
28 namespace base {
29 class Clock;
30 class DictionaryValue;
31 class FilePath;
32 class HistogramBase;
33 class JsonPrefStoreLossyWriteTest;
34 class SequencedTaskRunner;
35 class SequencedWorkerPool;
36 class Value;
37 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestBasic);
38 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod);
39 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods);
40 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps);
41 }
42
43 // A writable PrefStore implementation that is used for user preferences.
44 class BASE_PREFS_EXPORT JsonPrefStore
45 : public PersistentPrefStore,
46 public base::ImportantFileWriter::DataSerializer,
47 public base::SupportsWeakPtr<JsonPrefStore>,
48 public base::NonThreadSafe {
49 public:
50 struct ReadResult;
51
52 // Returns instance of SequencedTaskRunner which guarantees that file
53 // operations on the same file will be executed in sequenced order.
54 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForFile(
55 const base::FilePath& pref_filename,
56 base::SequencedWorkerPool* worker_pool);
57
58 // Same as the constructor below with no alternate filename.
59 JsonPrefStore(
60 const base::FilePath& pref_filename,
61 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner,
62 scoped_ptr<PrefFilter> pref_filter);
63
64 // |sequenced_task_runner| must be a shutdown-blocking task runner, ideally
65 // created by the GetTaskRunnerForFile() method above.
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
68 // desired prefs may have previously been written to. If |pref_filename|
69 // doesn't exist and |pref_alternate_filename| does, |pref_alternate_filename|
70 // will be moved to |pref_filename| before the read occurs.
71 JsonPrefStore(
72 const base::FilePath& pref_filename,
73 const base::FilePath& pref_alternate_filename,
74 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner,
75 scoped_ptr<PrefFilter> pref_filter);
76
77 // PrefStore overrides:
78 bool GetValue(const std::string& key,
79 const base::Value** result) const override;
80 void AddObserver(PrefStore::Observer* observer) override;
81 void RemoveObserver(PrefStore::Observer* observer) override;
82 bool HasObservers() const override;
83 bool IsInitializationComplete() const override;
84
85 // PersistentPrefStore overrides:
86 bool GetMutableValue(const std::string& key, base::Value** result) override;
87 void SetValue(const std::string& key,
88 scoped_ptr<base::Value> value,
89 uint32_t flags) override;
90 void SetValueSilently(const std::string& key,
91 scoped_ptr<base::Value> value,
92 uint32_t flags) override;
93 void RemoveValue(const std::string& key, uint32_t flags) override;
94 bool ReadOnly() const override;
95 PrefReadError GetReadError() const override;
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.
98 // See details in pref_filter.h.
99 PrefReadError ReadPrefs() override;
100 void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override;
101 void CommitPendingWrite() override;
102 void SchedulePendingLossyWrites() override;
103 void ReportValueChanged(const std::string& key, uint32_t flags) override;
104
105 // Just like RemoveValue(), but doesn't notify observers. Used when doing some
106 // cleanup that shouldn't otherwise alert observers.
107 void RemoveValueSilently(const std::string& key, uint32_t flags);
108
109 // Registers |on_next_successful_write| to be called once, on the next
110 // successful write event of |writer_|.
111 void RegisterOnNextSuccessfulWriteCallback(
112 const base::Closure& on_next_successful_write);
113
114 private:
115 // Represents a histogram for recording the number of writes to the pref file
116 // that occur every kHistogramWriteReportIntervalInMins minutes.
117 class BASE_PREFS_EXPORT WriteCountHistogram {
118 public:
119 static const int32_t kHistogramWriteReportIntervalMins;
120
121 WriteCountHistogram(const base::TimeDelta& commit_interval,
122 const base::FilePath& path);
123 // Constructor for testing. |clock| is a test Clock that is used to retrieve
124 // the time.
125 WriteCountHistogram(const base::TimeDelta& commit_interval,
126 const base::FilePath& path,
127 scoped_ptr<base::Clock> clock);
128 ~WriteCountHistogram();
129
130 // Record that a write has occured.
131 void RecordWriteOccured();
132
133 // Reports writes (that have not yet been reported) in all of the recorded
134 // intervals that have elapsed up until current time.
135 void ReportOutstandingWrites();
136
137 base::HistogramBase* GetHistogram();
138
139 private:
140 // The minimum interval at which writes can occur.
141 const base::TimeDelta commit_interval_;
142
143 // The path to the file.
144 const base::FilePath path_;
145
146 // Clock which is used to retrieve the current time.
147 scoped_ptr<base::Clock> clock_;
148
149 // The interval at which to report write counts.
150 const base::TimeDelta report_interval_;
151
152 // The time at which the last histogram value was reported for the number
153 // of write counts.
154 base::Time last_report_time_;
155
156 // The number of writes that have occured since the last write count was
157 // reported.
158 uint32_t writes_since_last_report_;
159
160 DISALLOW_COPY_AND_ASSIGN(WriteCountHistogram);
161 };
162
163 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
164 WriteCountHistogramTestBasic);
165 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
166 WriteCountHistogramTestSinglePeriod);
167 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
168 WriteCountHistogramTestMultiplePeriods);
169 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
170 WriteCountHistogramTestPeriodWithGaps);
171 friend class base::JsonPrefStoreLossyWriteTest;
172
173 ~JsonPrefStore() override;
174
175 // This method is called after the JSON file has been read. It then hands
176 // |value| (or an empty dictionary in some read error cases) to the
177 // |pref_filter| if one is set. It also gives a callback pointing at
178 // FinalizeFileRead() to that |pref_filter_| which is then responsible for
179 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead()
180 // is invoked directly.
181 void OnFileRead(scoped_ptr<ReadResult> read_result);
182
183 // ImportantFileWriter::DataSerializer overrides:
184 bool SerializeData(std::string* output) override;
185
186 // This method is called after the JSON file has been read and the result has
187 // potentially been intercepted and modified by |pref_filter_|.
188 // |initialization_successful| is pre-determined by OnFileRead() and should
189 // be used when reporting OnInitializationCompleted().
190 // |schedule_write| indicates whether a write should be immediately scheduled
191 // (typically because the |pref_filter_| has already altered the |prefs|) --
192 // this will be ignored if this store is read-only.
193 void FinalizeFileRead(bool initialization_successful,
194 scoped_ptr<base::DictionaryValue> prefs,
195 bool schedule_write);
196
197 // Schedule a write with the file writer as long as |flags| doesn't contain
198 // WriteablePrefStore::LOSSY_PREF_WRITE_FLAG.
199 void ScheduleWrite(uint32_t flags);
200
201 const base::FilePath path_;
202 const base::FilePath alternate_path_;
203 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
204
205 scoped_ptr<base::DictionaryValue> prefs_;
206
207 bool read_only_;
208
209 // Helper for safely writing pref data.
210 base::ImportantFileWriter writer_;
211
212 scoped_ptr<PrefFilter> pref_filter_;
213 base::ObserverList<PrefStore::Observer, true> observers_;
214
215 scoped_ptr<ReadErrorDelegate> error_delegate_;
216
217 bool initialized_;
218 bool filtering_in_progress_;
219 bool pending_lossy_write_;
220 PrefReadError read_error_;
221
222 std::set<std::string> keys_need_empty_value_;
223
224 WriteCountHistogram write_count_histogram_;
225
226 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore);
227 };
228
229 #endif // BASE_PREFS_JSON_PREF_STORE_H_
OLDNEW
« no previous file with comments | « base/prefs/default_pref_store_unittest.cc ('k') | base/prefs/json_pref_store.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698