Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / base / prefs / json_pref_store.h
blobd6ded59b575f15616b7bd3c0743268fec7f062d7
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/message_loop/message_loop_proxy.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"
26 class PrefFilter;
28 namespace base {
29 class Clock;
30 class DictionaryValue;
31 class FilePath;
32 class HistogramBase;
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, base::Value* value) override;
87 void SetValueSilently(const std::string& key, base::Value* value) override;
88 void RemoveValue(const std::string& key) override;
89 bool ReadOnly() const override;
90 PrefReadError GetReadError() const override;
91 // Note this method may be asynchronous if this instance has a |pref_filter_|
92 // in which case it will return PREF_READ_ERROR_ASYNCHRONOUS_TASK_INCOMPLETE.
93 // See details in pref_filter.h.
94 PrefReadError ReadPrefs() override;
95 void ReadPrefsAsync(ReadErrorDelegate* error_delegate) override;
96 void CommitPendingWrite() override;
97 void ReportValueChanged(const std::string& key) override;
99 // Just like RemoveValue(), but doesn't notify observers. Used when doing some
100 // cleanup that shouldn't otherwise alert observers.
101 void RemoveValueSilently(const std::string& key);
103 // Registers |on_next_successful_write| to be called once, on the next
104 // successful write event of |writer_|.
105 void RegisterOnNextSuccessfulWriteCallback(
106 const base::Closure& on_next_successful_write);
108 private:
109 // Represents a histogram for recording the number of writes to the pref file
110 // that occur every kHistogramWriteReportIntervalInMins minutes.
111 class BASE_PREFS_EXPORT WriteCountHistogram {
112 public:
113 static const int32_t kHistogramWriteReportIntervalMins;
115 WriteCountHistogram(const base::TimeDelta& commit_interval,
116 const base::FilePath& path);
117 // Constructor for testing. |clock| is a test Clock that is used to retrieve
118 // the time.
119 WriteCountHistogram(const base::TimeDelta& commit_interval,
120 const base::FilePath& path,
121 scoped_ptr<base::Clock> clock);
122 ~WriteCountHistogram();
124 // Record that a write has occured.
125 void RecordWriteOccured();
127 // Reports writes (that have not yet been reported) in all of the recorded
128 // intervals that have elapsed up until current time.
129 void ReportOutstandingWrites();
131 base::HistogramBase* GetHistogram();
133 private:
134 // The minimum interval at which writes can occur.
135 const base::TimeDelta commit_interval_;
137 // The path to the file.
138 const base::FilePath path_;
140 // Clock which is used to retrieve the current time.
141 scoped_ptr<base::Clock> clock_;
143 // The interval at which to report write counts.
144 const base::TimeDelta report_interval_;
146 // The time at which the last histogram value was reported for the number
147 // of write counts.
148 base::Time last_report_time_;
150 // The number of writes that have occured since the last write count was
151 // reported.
152 uint32_t writes_since_last_report_;
154 DISALLOW_COPY_AND_ASSIGN(WriteCountHistogram);
157 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
158 WriteCountHistogramTestBasic);
159 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
160 WriteCountHistogramTestSinglePeriod);
161 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
162 WriteCountHistogramTestMultiplePeriods);
163 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest,
164 WriteCountHistogramTestPeriodWithGaps);
166 ~JsonPrefStore() override;
168 // This method is called after the JSON file has been read. It then hands
169 // |value| (or an empty dictionary in some read error cases) to the
170 // |pref_filter| if one is set. It also gives a callback pointing at
171 // FinalizeFileRead() to that |pref_filter_| which is then responsible for
172 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead()
173 // is invoked directly.
174 void OnFileRead(scoped_ptr<ReadResult> read_result);
176 // ImportantFileWriter::DataSerializer overrides:
177 bool SerializeData(std::string* output) override;
179 // This method is called after the JSON file has been read and the result has
180 // potentially been intercepted and modified by |pref_filter_|.
181 // |initialization_successful| is pre-determined by OnFileRead() and should
182 // be used when reporting OnInitializationCompleted().
183 // |schedule_write| indicates whether a write should be immediately scheduled
184 // (typically because the |pref_filter_| has already altered the |prefs|) --
185 // this will be ignored if this store is read-only.
186 void FinalizeFileRead(bool initialization_successful,
187 scoped_ptr<base::DictionaryValue> prefs,
188 bool schedule_write);
190 const base::FilePath path_;
191 const base::FilePath alternate_path_;
192 const scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner_;
194 scoped_ptr<base::DictionaryValue> prefs_;
196 bool read_only_;
198 // Helper for safely writing pref data.
199 base::ImportantFileWriter writer_;
201 scoped_ptr<PrefFilter> pref_filter_;
202 ObserverList<PrefStore::Observer, true> observers_;
204 scoped_ptr<ReadErrorDelegate> error_delegate_;
206 bool initialized_;
207 bool filtering_in_progress_;
208 PrefReadError read_error_;
210 std::set<std::string> keys_need_empty_value_;
212 WriteCountHistogram write_count_histogram_;
214 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore);
217 #endif // BASE_PREFS_JSON_PREF_STORE_H_