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 #include "base/prefs/json_pref_store.h" | 5 #include "base/prefs/json_pref_store.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 base::FilePath bad = path.ReplaceExtension(kBadExtension); | 76 base::FilePath bad = path.ReplaceExtension(kBadExtension); |
77 | 77 |
78 // If they've ever had a parse error before, put them in another bucket. | 78 // If they've ever had a parse error before, put them in another bucket. |
79 // TODO(erikkay) if we keep this error checking for very long, we may | 79 // TODO(erikkay) if we keep this error checking for very long, we may |
80 // want to differentiate between recent and long ago errors. | 80 // want to differentiate between recent and long ago errors. |
81 bool bad_existed = base::PathExists(bad); | 81 bool bad_existed = base::PathExists(bad); |
82 base::Move(path, bad); | 82 base::Move(path, bad); |
83 return bad_existed ? PersistentPrefStore::PREF_READ_ERROR_JSON_REPEAT | 83 return bad_existed ? PersistentPrefStore::PREF_READ_ERROR_JSON_REPEAT |
84 : PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE; | 84 : PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE; |
85 } | 85 } |
86 } else if (!value->IsType(base::Value::TYPE_DICTIONARY)) { | 86 } |
| 87 if (!value->IsType(base::Value::TYPE_DICTIONARY)) |
87 return PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE; | 88 return PersistentPrefStore::PREF_READ_ERROR_JSON_TYPE; |
88 } | |
89 return PersistentPrefStore::PREF_READ_ERROR_NONE; | 89 return PersistentPrefStore::PREF_READ_ERROR_NONE; |
90 } | 90 } |
91 | 91 |
92 // Records a sample for |size| in the Settings.JsonDataReadSizeKilobytes | 92 // Records a sample for |size| in the Settings.JsonDataReadSizeKilobytes |
93 // histogram suffixed with the base name of the JSON file under |path|. | 93 // histogram suffixed with the base name of the JSON file under |path|. |
94 void RecordJsonDataSizeHistogram(const base::FilePath& path, size_t size) { | 94 void RecordJsonDataSizeHistogram(const base::FilePath& path, size_t size) { |
95 std::string spaceless_basename; | 95 std::string spaceless_basename; |
96 base::ReplaceChars(path.BaseName().MaybeAsASCII(), " ", "_", | 96 base::ReplaceChars(path.BaseName().MaybeAsASCII(), " ", "_", |
97 &spaceless_basename); | 97 &spaceless_basename); |
98 | 98 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 const base::FilePath& filename, | 136 const base::FilePath& filename, |
137 base::SequencedWorkerPool* worker_pool) { | 137 base::SequencedWorkerPool* worker_pool) { |
138 std::string token("json_pref_store-"); | 138 std::string token("json_pref_store-"); |
139 token.append(filename.AsUTF8Unsafe()); | 139 token.append(filename.AsUTF8Unsafe()); |
140 return worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | 140 return worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( |
141 worker_pool->GetNamedSequenceToken(token), | 141 worker_pool->GetNamedSequenceToken(token), |
142 base::SequencedWorkerPool::BLOCK_SHUTDOWN); | 142 base::SequencedWorkerPool::BLOCK_SHUTDOWN); |
143 } | 143 } |
144 | 144 |
145 JsonPrefStore::JsonPrefStore( | 145 JsonPrefStore::JsonPrefStore( |
146 const base::FilePath& filename, | 146 const base::FilePath& pref_filename, |
147 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, | 147 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, |
148 scoped_ptr<PrefFilter> pref_filter) | 148 scoped_ptr<PrefFilter> pref_filter) |
149 : JsonPrefStore(filename, | 149 : JsonPrefStore(pref_filename, |
150 base::FilePath(), | 150 base::FilePath(), |
151 sequenced_task_runner, | 151 sequenced_task_runner, |
152 pref_filter.Pass()) { | 152 pref_filter.Pass()) { |
153 } | 153 } |
154 | 154 |
155 JsonPrefStore::JsonPrefStore( | 155 JsonPrefStore::JsonPrefStore( |
156 const base::FilePath& filename, | 156 const base::FilePath& pref_filename, |
157 const base::FilePath& alternate_filename, | 157 const base::FilePath& pref_alternate_filename, |
158 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, | 158 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, |
159 scoped_ptr<PrefFilter> pref_filter) | 159 scoped_ptr<PrefFilter> pref_filter) |
160 : path_(filename), | 160 : path_(pref_filename), |
161 alternate_path_(alternate_filename), | 161 alternate_path_(pref_alternate_filename), |
162 sequenced_task_runner_(sequenced_task_runner), | 162 sequenced_task_runner_(sequenced_task_runner), |
163 prefs_(new base::DictionaryValue()), | 163 prefs_(new base::DictionaryValue()), |
164 read_only_(false), | 164 read_only_(false), |
165 writer_(filename, sequenced_task_runner), | 165 writer_(pref_filename, sequenced_task_runner), |
166 pref_filter_(pref_filter.Pass()), | 166 pref_filter_(pref_filter.Pass()), |
167 initialized_(false), | 167 initialized_(false), |
168 filtering_in_progress_(false), | 168 filtering_in_progress_(false), |
169 pending_lossy_write_(false), | 169 pending_lossy_write_(false), |
170 read_error_(PREF_READ_ERROR_NONE), | 170 read_error_(PREF_READ_ERROR_NONE), |
171 write_count_histogram_(writer_.commit_interval(), path_) { | 171 write_count_histogram_(writer_.commit_interval(), path_) { |
172 DCHECK(!path_.empty()); | 172 DCHECK(!path_.empty()); |
173 } | 173 } |
174 | 174 |
175 bool JsonPrefStore::GetValue(const std::string& key, | 175 bool JsonPrefStore::GetValue(const std::string& key, |
176 const base::Value** result) const { | 176 const base::Value** result) const { |
177 DCHECK(CalledOnValidThread()); | 177 DCHECK(CalledOnValidThread()); |
178 | 178 |
179 base::Value* tmp = NULL; | 179 base::Value* tmp = nullptr; |
180 if (!prefs_->Get(key, &tmp)) | 180 if (!prefs_->Get(key, &tmp)) |
181 return false; | 181 return false; |
182 | 182 |
183 if (result) | 183 if (result) |
184 *result = tmp; | 184 *result = tmp; |
185 return true; | 185 return true; |
186 } | 186 } |
187 | 187 |
188 void JsonPrefStore::AddObserver(PrefStore::Observer* observer) { | 188 void JsonPrefStore::AddObserver(PrefStore::Observer* observer) { |
189 DCHECK(CalledOnValidThread()); | 189 DCHECK(CalledOnValidThread()); |
(...skipping 25 matching lines...) Expand all Loading... |
215 | 215 |
216 return prefs_->Get(key, result); | 216 return prefs_->Get(key, result); |
217 } | 217 } |
218 | 218 |
219 void JsonPrefStore::SetValue(const std::string& key, | 219 void JsonPrefStore::SetValue(const std::string& key, |
220 scoped_ptr<base::Value> value, | 220 scoped_ptr<base::Value> value, |
221 uint32 flags) { | 221 uint32 flags) { |
222 DCHECK(CalledOnValidThread()); | 222 DCHECK(CalledOnValidThread()); |
223 | 223 |
224 DCHECK(value); | 224 DCHECK(value); |
225 base::Value* old_value = NULL; | 225 base::Value* old_value = nullptr; |
226 prefs_->Get(key, &old_value); | 226 prefs_->Get(key, &old_value); |
227 if (!old_value || !value->Equals(old_value)) { | 227 if (!old_value || !value->Equals(old_value)) { |
228 prefs_->Set(key, value.Pass()); | 228 prefs_->Set(key, value.Pass()); |
229 ReportValueChanged(key, flags); | 229 ReportValueChanged(key, flags); |
230 } | 230 } |
231 } | 231 } |
232 | 232 |
233 void JsonPrefStore::SetValueSilently(const std::string& key, | 233 void JsonPrefStore::SetValueSilently(const std::string& key, |
234 scoped_ptr<base::Value> value, | 234 scoped_ptr<base::Value> value, |
235 uint32 flags) { | 235 uint32 flags) { |
236 DCHECK(CalledOnValidThread()); | 236 DCHECK(CalledOnValidThread()); |
237 | 237 |
238 DCHECK(value); | 238 DCHECK(value); |
239 base::Value* old_value = NULL; | 239 base::Value* old_value = nullptr; |
240 prefs_->Get(key, &old_value); | 240 prefs_->Get(key, &old_value); |
241 if (!old_value || !value->Equals(old_value)) { | 241 if (!old_value || !value->Equals(old_value)) { |
242 prefs_->Set(key, value.Pass()); | 242 prefs_->Set(key, value.Pass()); |
243 ScheduleWrite(flags); | 243 ScheduleWrite(flags); |
244 } | 244 } |
245 } | 245 } |
246 | 246 |
247 void JsonPrefStore::RemoveValue(const std::string& key, uint32 flags) { | 247 void JsonPrefStore::RemoveValue(const std::string& key, uint32 flags) { |
248 DCHECK(CalledOnValidThread()); | 248 DCHECK(CalledOnValidThread()); |
249 | 249 |
250 if (prefs_->RemovePath(key, NULL)) | 250 if (prefs_->RemovePath(key, nullptr)) |
251 ReportValueChanged(key, flags); | 251 ReportValueChanged(key, flags); |
252 } | 252 } |
253 | 253 |
254 void JsonPrefStore::RemoveValueSilently(const std::string& key, uint32 flags) { | 254 void JsonPrefStore::RemoveValueSilently(const std::string& key, uint32 flags) { |
255 DCHECK(CalledOnValidThread()); | 255 DCHECK(CalledOnValidThread()); |
256 | 256 |
257 prefs_->RemovePath(key, NULL); | 257 prefs_->RemovePath(key, nullptr); |
258 ScheduleWrite(flags); | 258 ScheduleWrite(flags); |
259 } | 259 } |
260 | 260 |
261 bool JsonPrefStore::ReadOnly() const { | 261 bool JsonPrefStore::ReadOnly() const { |
262 DCHECK(CalledOnValidThread()); | 262 DCHECK(CalledOnValidThread()); |
263 | 263 |
264 return read_only_; | 264 return read_only_; |
265 } | 265 } |
266 | 266 |
267 PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const { | 267 PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const { |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 DCHECK_EQ(31, num_buckets); | 526 DCHECK_EQ(31, num_buckets); |
527 | 527 |
528 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | 528 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
529 // macro adapted to allow for a dynamically suffixed histogram name. | 529 // macro adapted to allow for a dynamically suffixed histogram name. |
530 // Note: The factory creates and owns the histogram. | 530 // Note: The factory creates and owns the histogram. |
531 base::HistogramBase* histogram = base::Histogram::FactoryGet( | 531 base::HistogramBase* histogram = base::Histogram::FactoryGet( |
532 histogram_name, min_value, max_value, num_buckets, | 532 histogram_name, min_value, max_value, num_buckets, |
533 base::HistogramBase::kUmaTargetedHistogramFlag); | 533 base::HistogramBase::kUmaTargetedHistogramFlag); |
534 return histogram; | 534 return histogram; |
535 } | 535 } |
OLD | NEW |