relnote: refactoring spdy/quic utils, adding SpdyHeadersToBalsaHeaders
[chromium-blink-merge.git] / base / prefs / json_pref_store.h
blob0be7702481ed21239e24e95b9aab5940b29cc8ce
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.
5 #ifndef BASE_PREFS_JSON_PREF_STORE_H_
6 #define BASE_PREFS_JSON_PREF_STORE_H_
8 #include <set>
9 #include <string>
11 #include "base/basictypes.h"
12 #include "base/callback_forward.h"
13 #include "base/compiler_specific.h"
14 #include "base/files/file_path.h"
15 #include "base/files/important_file_writer.h"
16 #include "base/gtest_prod_util.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/scoped_vector.h"
19 #include "base/memory/weak_ptr.h"
20 #include "base/observer_list.h"
21 #include "base/prefs/base_prefs_export.h"
22 #include "base/prefs/persistent_pref_store.h"
23 #include "base/threading/non_thread_safe.h"
25 class PrefFilter;
27 namespace base {
28 class Clock;
29 class DictionaryValue;
30 class FilePath;
31 class HistogramBase;
32 class JsonPrefStoreLossyWriteTest;
33 class SequencedTaskRunner;
34 class SequencedWorkerPool;
35 class Value;
36 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestBasic);
37 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod);
38 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods);
39 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps);
42 // A writable PrefStore implementation that is used for user preferences.
43 class BASE_PREFS_EXPORT JsonPrefStore
44 : public PersistentPrefStore,
45 public base::ImportantFileWriter::DataSerializer,
46 public base::SupportsWeakPtr<JsonPrefStore>,
47 public base::NonThreadSafe {
48 public:
49 struct ReadResult;
51 // Returns instance of SequencedTaskRunner which guarantees that file
52 // operations on the same file will be executed in sequenced order.
53 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForFile(
54 const base::FilePath& pref_filename,
55 base::SequencedWorkerPool* worker_pool);
57 // Same as the constructor below with no alternate filename.
58 JsonPrefStore(
59 const base::FilePath& pref_filename,
60 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner,
61 scoped_ptr<PrefFilter> pref_filter);
63 // |sequenced_task_runner| must be a shutdown-blocking task runner, ideally
64 // created by the GetTaskRunnerForFile() method above.
65 // |pref_filename| is the path to the file to read prefs from.
66 // |pref_alternate_filename| is the path to an alternate file which the
67 // desired prefs may have previously been written to. If |pref_filename|
68 // doesn't exist and |pref_alternate_filename| does, |pref_alternate_filename|
69 // will be moved to |pref_filename| before the read occurs.
70 JsonPrefStore(
71 const base::FilePath& pref_filename,
72 const base::FilePath& pref_alternate_filename,
73 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner,
74 scoped_ptr<PrefFilter> pref_filter);
76 // PrefStore overrides:
77 bool GetValue(const std::string& key,
78 const base::Value** result) const override;
79 void AddObserver(PrefStore::Observer* observer) override;
80 void RemoveObserver(PrefStore::Observer* observer) override;
81 bool HasObservers() const override;
82 bool IsInitializationComplete() const override;
84 // PersistentPrefStore overrides:
85 bool GetMutableValue(const std::string& key, base::Value** result) override;
86 void SetValue(const std::string& key,
87 scoped_ptr<base::Value> value,
88 uint32 flags) override;
89 void SetValueSilently(const std::string& key,
90 scoped_ptr<base::Value> value,
91 uint32 flags) override;
92 void RemoveValue(const std::string& key, uint32 flags) override;
93 bool ReadOnly() const override;
94 PrefReadError GetReadError() const override;
95 // Note this method may be asynchronous if this instance has a |pref_filter_|
96 // in which case it will return PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE.
97 // See details in pref_filter.h.
98 PrefReadError ReadPrefs() override;
99 void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override;
100 void CommitPendingWrite() override;
101 void SchedulePendingLossyWrites() override;
102 void ReportValueChanged(const std::string& key, uint32 flags) override;
104 // Just like RemoveValue(), but doesn't notify observers. Used when doing some
105 // cleanup that shouldn't otherwise alert observers.
106 void RemoveValueSilently(const std::string& key, uint32 flags);
108 // Registers |on_next_successful_write| to be called once, on the next
109 // successful write event of |writer_|.
110 void RegisterOnNextSuccessfulWriteCallback(
111 const base::Closure& on_next_successful_write);
113 private:
114 // Represents a histogram for recording the number of writes to the pref file
115 // that occur every kHistogramWriteReportIntervalInMins minutes.
116 class BASE_PREFS_EXPORT WriteCountHistogram {
117 public:
118 static const int32_t kHistogramWriteReportIntervalMins;
120 WriteCountHistogram(const base::TimeDelta& commit_interval,
121 const base::FilePath& path);
122 // Constructor for testing. |clock| is a test Clock that is used to retrieve
123 // the time.
124 WriteCountHistogram(const base::TimeDelta& commit_interval,
125 const base::FilePath& path,
126 scoped_ptr<base::Clock> clock);
127 ~WriteCountHistogram();
129 // Record that a write has occured.
130 void RecordWriteOccured();
132 // Reports writes (that have not yet been reported) in all of the recorded
133 // intervals that have elapsed up until current time.
134 void ReportOutstandingWrites();
136 base::HistogramBase* GetHistogram();
138 private:
139 // The minimum interval at which writes can occur.
140 const base::TimeDelta commit_interval_;
142 // The path to the file.
143 const base::FilePath path_;
145 // Clock which is used to retrieve the current time.
146 scoped_ptr<base::Clock> clock_;
148 // The interval at which to report write counts.
149 const base::TimeDelta report_interval_;
151 // The time at which the last histogram value was reported for the number
152 // of write counts.
153 base::Time last_report_time_;
155 // The number of writes that have occured since the last write count was
156 // reported.
157 uint32_t writes_since_last_report_;
159 DISALLOW_COPY_AND_ASSIGN(WriteCountHistogram);
162 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
163 WriteCountHistogramTestBasic);
164 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
165 WriteCountHistogramTestSinglePeriod);
166 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
167 WriteCountHistogramTestMultiplePeriods);
168 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
169 WriteCountHistogramTestPeriodWithGaps);
170 friend class base::JsonPrefStoreLossyWriteTest;
172 ~JsonPrefStore() override;
174 // This method is called after the JSON file has been read. It then hands
175 // |value| (or an empty dictionary in some read error cases) to the
176 // |pref_filter| if one is set. It also gives a callback pointing at
177 // FinalizeFileRead() to that |pref_filter_| which is then responsible for
178 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead()
179 // is invoked directly.
180 void OnFileRead(scoped_ptr<ReadResult> read_result);
182 // ImportantFileWriter::DataSerializer overrides:
183 bool SerializeData(std::string* output) override;
185 // This method is called after the JSON file has been read and the result has
186 // potentially been intercepted and modified by |pref_filter_|.
187 // |initialization_successful| is pre-determined by OnFileRead() and should
188 // be used when reporting OnInitializationCompleted().
189 // |schedule_write| indicates whether a write should be immediately scheduled
190 // (typically because the |pref_filter_| has already altered the |prefs|) --
191 // this will be ignored if this store is read-only.
192 void FinalizeFileRead(bool initialization_successful,
193 scoped_ptr<base::DictionaryValue> prefs,
194 bool schedule_write);
196 // Schedule a write with the file writer as long as |flags| doesn't contain
197 // WriteablePrefStore::LOSSY_PREF_WRITE_FLAG.
198 void ScheduleWrite(uint32 flags);
200 const base::FilePath path_;
201 const base::FilePath alternate_path_;
202 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
204 scoped_ptr<base::DictionaryValue> prefs_;
206 bool read_only_;
208 // Helper for safely writing pref data.
209 base::ImportantFileWriter writer_;
211 scoped_ptr<PrefFilter> pref_filter_;
212 base::ObserverList<PrefStore::Observer, true> observers_;
214 scoped_ptr<ReadErrorDelegate> error_delegate_;
216 bool initialized_;
217 bool filtering_in_progress_;
218 bool pending_lossy_write_;
219 PrefReadError read_error_;
221 std::set<std::string> keys_need_empty_value_;
223 WriteCountHistogram write_count_histogram_;
225 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore);
228 #endif // BASE_PREFS_JSON_PREF_STORE_H_