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/callback_forward.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/files/important_file_writer.h" | 15 #include "base/files/important_file_writer.h" |
16 #include "base/gtest_prod_util.h" | |
16 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
18 #include "base/memory/scoped_vector.h" | |
17 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
18 #include "base/message_loop/message_loop_proxy.h" | 20 #include "base/message_loop/message_loop_proxy.h" |
19 #include "base/observer_list.h" | 21 #include "base/observer_list.h" |
20 #include "base/prefs/base_prefs_export.h" | 22 #include "base/prefs/base_prefs_export.h" |
21 #include "base/prefs/persistent_pref_store.h" | 23 #include "base/prefs/persistent_pref_store.h" |
22 #include "base/threading/non_thread_safe.h" | 24 #include "base/threading/non_thread_safe.h" |
23 | 25 |
24 class PrefFilter; | 26 class PrefFilter; |
25 | 27 |
26 namespace base { | 28 namespace base { |
27 class DictionaryValue; | 29 class DictionaryValue; |
28 class FilePath; | 30 class FilePath; |
31 class HistogramBase; | |
29 class SequencedTaskRunner; | 32 class SequencedTaskRunner; |
30 class SequencedWorkerPool; | 33 class SequencedWorkerPool; |
31 class Value; | 34 class Value; |
35 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestBasic); | |
36 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestSinglePeriod); | |
37 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestMultiplePeriods); | |
38 FORWARD_DECLARE_TEST(JsonPrefStoreTest, WriteCountHistogramTestPeriodWithGaps); | |
32 } | 39 } |
33 | 40 |
34 // A writable PrefStore implementation that is used for user preferences. | 41 // A writable PrefStore implementation that is used for user preferences. |
35 class BASE_PREFS_EXPORT JsonPrefStore | 42 class BASE_PREFS_EXPORT JsonPrefStore |
36 : public PersistentPrefStore, | 43 : public PersistentPrefStore, |
37 public base::ImportantFileWriter::DataSerializer, | 44 public base::ImportantFileWriter::DataSerializer, |
38 public base::SupportsWeakPtr<JsonPrefStore>, | 45 public base::SupportsWeakPtr<JsonPrefStore>, |
39 public base::NonThreadSafe { | 46 public base::NonThreadSafe { |
40 public: | 47 public: |
41 struct ReadResult; | 48 struct ReadResult; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 // Just like RemoveValue(), but doesn't notify observers. Used when doing some | 98 // Just like RemoveValue(), but doesn't notify observers. Used when doing some |
92 // cleanup that shouldn't otherwise alert observers. | 99 // cleanup that shouldn't otherwise alert observers. |
93 void RemoveValueSilently(const std::string& key); | 100 void RemoveValueSilently(const std::string& key); |
94 | 101 |
95 // Registers |on_next_successful_write| to be called once, on the next | 102 // Registers |on_next_successful_write| to be called once, on the next |
96 // successful write event of |writer_|. | 103 // successful write event of |writer_|. |
97 void RegisterOnNextSuccessfulWriteCallback( | 104 void RegisterOnNextSuccessfulWriteCallback( |
98 const base::Closure& on_next_successful_write); | 105 const base::Closure& on_next_successful_write); |
99 | 106 |
100 private: | 107 private: |
108 // Represents a histogram for recording the number of writes to the pref file | |
109 // that occur every kHistogramWriteReportIntervalInMins minutes. | |
110 class WriteCountHistogram { | |
111 public: | |
112 static const int32_t kHistogramWriteReportIntervalMins; | |
113 | |
114 WriteCountHistogram(const base::TimeDelta& commit_interval, | |
115 const base::FilePath& path); | |
116 // Constructor for testing. |get_time_func| is a function which returns the | |
117 // current time. | |
118 WriteCountHistogram(const base::TimeDelta& commit_interval, | |
119 const base::FilePath& path, | |
120 const base::Callback<base::Time(void)>& get_time_func); | |
121 ~WriteCountHistogram(); | |
122 | |
123 // Record that a write has occured. | |
124 void RecordWriteOccured(); | |
125 | |
126 // Reports writes (that have not yet been reported) in all of the recorded | |
127 // intervals that have elapsed up until current time. | |
128 void ReportOutstandingWrites(); | |
129 | |
130 base::HistogramBase* GetHistogram(); | |
131 | |
132 private: | |
133 // The minimum interval at which writes can occur. | |
134 const base::TimeDelta commit_interval_; | |
135 | |
136 // The path to the file. | |
137 const base::FilePath path_; | |
138 | |
139 // Function which returns the current time. Passed in for testing. | |
140 base::Callback<base::Time(void)> get_time_func_; | |
Alexei Svitkine (slow)
2015/04/24 16:34:47
Can you use a base::Clock instead?
raymes
2015/04/27 03:27:54
Ahh! This is what I was looking for!! Done.
| |
141 | |
142 // The interval at which to report write counts. | |
143 const base::TimeDelta report_interval_; | |
144 | |
145 // The time at which the last histogram value was reported for the number | |
146 // of write counts. | |
147 base::Time last_report_time_; | |
148 | |
149 // The number of writes that have occured since the last write count was | |
150 // reported. | |
151 uint32_t writes_since_last_report_; | |
152 | |
153 DISALLOW_COPY_AND_ASSIGN(WriteCountHistogram); | |
154 }; | |
155 | |
156 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, | |
157 WriteCountHistogramTestBasic); | |
158 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, | |
159 WriteCountHistogramTestSinglePeriod); | |
160 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, | |
161 WriteCountHistogramTestMultiplePeriods); | |
162 FRIEND_TEST_ALL_PREFIXES(base::JsonPrefStoreTest, | |
163 WriteCountHistogramTestPeriodWithGaps); | |
164 | |
101 ~JsonPrefStore() override; | 165 ~JsonPrefStore() override; |
102 | 166 |
103 // This method is called after the JSON file has been read. It then hands | 167 // This method is called after the JSON file has been read. It then hands |
104 // |value| (or an empty dictionary in some read error cases) to the | 168 // |value| (or an empty dictionary in some read error cases) to the |
105 // |pref_filter| if one is set. It also gives a callback pointing at | 169 // |pref_filter| if one is set. It also gives a callback pointing at |
106 // FinalizeFileRead() to that |pref_filter_| which is then responsible for | 170 // FinalizeFileRead() to that |pref_filter_| which is then responsible for |
107 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead() | 171 // invoking it when done. If there is no |pref_filter_|, FinalizeFileRead() |
108 // is invoked directly. | 172 // is invoked directly. |
109 void OnFileRead(scoped_ptr<ReadResult> read_result); | 173 void OnFileRead(scoped_ptr<ReadResult> read_result); |
110 | 174 |
(...skipping 26 matching lines...) Expand all Loading... | |
137 ObserverList<PrefStore::Observer, true> observers_; | 201 ObserverList<PrefStore::Observer, true> observers_; |
138 | 202 |
139 scoped_ptr<ReadErrorDelegate> error_delegate_; | 203 scoped_ptr<ReadErrorDelegate> error_delegate_; |
140 | 204 |
141 bool initialized_; | 205 bool initialized_; |
142 bool filtering_in_progress_; | 206 bool filtering_in_progress_; |
143 PrefReadError read_error_; | 207 PrefReadError read_error_; |
144 | 208 |
145 std::set<std::string> keys_need_empty_value_; | 209 std::set<std::string> keys_need_empty_value_; |
146 | 210 |
211 WriteCountHistogram write_count_histogram_; | |
212 | |
147 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); | 213 DISALLOW_COPY_AND_ASSIGN(JsonPrefStore); |
148 }; | 214 }; |
149 | 215 |
150 #endif // BASE_PREFS_JSON_PREF_STORE_H_ | 216 #endif // BASE_PREFS_JSON_PREF_STORE_H_ |
OLD | NEW |