Chromium Code Reviews| 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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, | 151 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, |
| 152 scoped_ptr<PrefFilter> pref_filter) | 152 scoped_ptr<PrefFilter> pref_filter) |
| 153 : path_(filename), | 153 : path_(filename), |
| 154 sequenced_task_runner_(sequenced_task_runner), | 154 sequenced_task_runner_(sequenced_task_runner), |
| 155 prefs_(new base::DictionaryValue()), | 155 prefs_(new base::DictionaryValue()), |
| 156 read_only_(false), | 156 read_only_(false), |
| 157 writer_(filename, sequenced_task_runner), | 157 writer_(filename, sequenced_task_runner), |
| 158 pref_filter_(pref_filter.Pass()), | 158 pref_filter_(pref_filter.Pass()), |
| 159 initialized_(false), | 159 initialized_(false), |
| 160 filtering_in_progress_(false), | 160 filtering_in_progress_(false), |
| 161 pending_lossy_write_(false), | |
| 161 read_error_(PREF_READ_ERROR_NONE), | 162 read_error_(PREF_READ_ERROR_NONE), |
| 162 write_count_histogram_(writer_.commit_interval(), path_) { | 163 write_count_histogram_(writer_.commit_interval(), path_) { |
| 163 DCHECK(!path_.empty()); | 164 DCHECK(!path_.empty()); |
| 164 } | 165 } |
| 165 | 166 |
| 166 JsonPrefStore::JsonPrefStore( | 167 JsonPrefStore::JsonPrefStore( |
| 167 const base::FilePath& filename, | 168 const base::FilePath& filename, |
| 168 const base::FilePath& alternate_filename, | 169 const base::FilePath& alternate_filename, |
| 169 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, | 170 const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner, |
| 170 scoped_ptr<PrefFilter> pref_filter) | 171 scoped_ptr<PrefFilter> pref_filter) |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 base::Value* value, | 246 base::Value* value, |
| 246 uint32 flags) { | 247 uint32 flags) { |
| 247 DCHECK(CalledOnValidThread()); | 248 DCHECK(CalledOnValidThread()); |
| 248 | 249 |
| 249 DCHECK(value); | 250 DCHECK(value); |
| 250 scoped_ptr<base::Value> new_value(value); | 251 scoped_ptr<base::Value> new_value(value); |
| 251 base::Value* old_value = NULL; | 252 base::Value* old_value = NULL; |
| 252 prefs_->Get(key, &old_value); | 253 prefs_->Get(key, &old_value); |
| 253 if (!old_value || !value->Equals(old_value)) { | 254 if (!old_value || !value->Equals(old_value)) { |
| 254 prefs_->Set(key, new_value.release()); | 255 prefs_->Set(key, new_value.release()); |
| 255 if (!read_only_) | 256 ScheduleWrite(flags); |
| 256 writer_.ScheduleWrite(this); | |
| 257 } | 257 } |
| 258 } | 258 } |
| 259 | 259 |
| 260 void JsonPrefStore::RemoveValue(const std::string& key, uint32 flags) { | 260 void JsonPrefStore::RemoveValue(const std::string& key, uint32 flags) { |
| 261 DCHECK(CalledOnValidThread()); | 261 DCHECK(CalledOnValidThread()); |
| 262 | 262 |
| 263 if (prefs_->RemovePath(key, NULL)) | 263 if (prefs_->RemovePath(key, NULL)) |
| 264 ReportValueChanged(key, flags); | 264 ReportValueChanged(key, flags); |
| 265 } | 265 } |
| 266 | 266 |
| 267 void JsonPrefStore::RemoveValueSilently(const std::string& key, uint32 flags) { | 267 void JsonPrefStore::RemoveValueSilently(const std::string& key, uint32 flags) { |
| 268 DCHECK(CalledOnValidThread()); | 268 DCHECK(CalledOnValidThread()); |
| 269 | 269 |
| 270 prefs_->RemovePath(key, NULL); | 270 prefs_->RemovePath(key, NULL); |
| 271 if (!read_only_) | 271 ScheduleWrite(flags); |
| 272 writer_.ScheduleWrite(this); | |
| 273 } | 272 } |
| 274 | 273 |
| 275 bool JsonPrefStore::ReadOnly() const { | 274 bool JsonPrefStore::ReadOnly() const { |
| 276 DCHECK(CalledOnValidThread()); | 275 DCHECK(CalledOnValidThread()); |
| 277 | 276 |
| 278 return read_only_; | 277 return read_only_; |
| 279 } | 278 } |
| 280 | 279 |
| 281 PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const { | 280 PersistentPrefStore::PrefReadError JsonPrefStore::GetReadError() const { |
| 282 DCHECK(CalledOnValidThread()); | 281 DCHECK(CalledOnValidThread()); |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 302 base::PostTaskAndReplyWithResult( | 301 base::PostTaskAndReplyWithResult( |
| 303 sequenced_task_runner_.get(), | 302 sequenced_task_runner_.get(), |
| 304 FROM_HERE, | 303 FROM_HERE, |
| 305 base::Bind(&ReadPrefsFromDisk, path_, alternate_path_), | 304 base::Bind(&ReadPrefsFromDisk, path_, alternate_path_), |
| 306 base::Bind(&JsonPrefStore::OnFileRead, AsWeakPtr())); | 305 base::Bind(&JsonPrefStore::OnFileRead, AsWeakPtr())); |
| 307 } | 306 } |
| 308 | 307 |
| 309 void JsonPrefStore::CommitPendingWrite() { | 308 void JsonPrefStore::CommitPendingWrite() { |
| 310 DCHECK(CalledOnValidThread()); | 309 DCHECK(CalledOnValidThread()); |
| 311 | 310 |
| 311 // Schedule a write for any lossy writes that are outstanding to ensure that | |
| 312 // they get flushed when this function is called. | |
| 313 if (pending_lossy_write_) | |
| 314 writer_.ScheduleWrite(this); | |
| 315 | |
| 312 if (writer_.HasPendingWrite() && !read_only_) | 316 if (writer_.HasPendingWrite() && !read_only_) |
| 313 writer_.DoScheduledWrite(); | 317 writer_.DoScheduledWrite(); |
| 314 } | 318 } |
| 315 | 319 |
| 316 void JsonPrefStore::ReportValueChanged(const std::string& key, uint32 flags) { | 320 void JsonPrefStore::ReportValueChanged(const std::string& key, uint32 flags) { |
| 317 DCHECK(CalledOnValidThread()); | 321 DCHECK(CalledOnValidThread()); |
| 318 | 322 |
| 319 if (pref_filter_) | 323 if (pref_filter_) |
| 320 pref_filter_->FilterUpdate(key); | 324 pref_filter_->FilterUpdate(key); |
| 321 | 325 |
| 322 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); | 326 FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key)); |
| 323 | 327 |
| 324 if (!read_only_) | 328 ScheduleWrite(flags); |
| 325 writer_.ScheduleWrite(this); | |
| 326 } | 329 } |
| 327 | 330 |
| 328 void JsonPrefStore::RegisterOnNextSuccessfulWriteCallback( | 331 void JsonPrefStore::RegisterOnNextSuccessfulWriteCallback( |
| 329 const base::Closure& on_next_successful_write) { | 332 const base::Closure& on_next_successful_write) { |
| 330 DCHECK(CalledOnValidThread()); | 333 DCHECK(CalledOnValidThread()); |
| 331 | 334 |
| 332 writer_.RegisterOnNextSuccessfulWriteCallback(on_next_successful_write); | 335 writer_.RegisterOnNextSuccessfulWriteCallback(on_next_successful_write); |
| 333 } | 336 } |
| 334 | 337 |
| 335 void JsonPrefStore::OnFileRead(scoped_ptr<ReadResult> read_result) { | 338 void JsonPrefStore::OnFileRead(scoped_ptr<ReadResult> read_result) { |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 394 } | 397 } |
| 395 } | 398 } |
| 396 | 399 |
| 397 JsonPrefStore::~JsonPrefStore() { | 400 JsonPrefStore::~JsonPrefStore() { |
| 398 CommitPendingWrite(); | 401 CommitPendingWrite(); |
| 399 } | 402 } |
| 400 | 403 |
| 401 bool JsonPrefStore::SerializeData(std::string* output) { | 404 bool JsonPrefStore::SerializeData(std::string* output) { |
| 402 DCHECK(CalledOnValidThread()); | 405 DCHECK(CalledOnValidThread()); |
| 403 | 406 |
| 407 pending_lossy_write_ = false; | |
| 408 | |
| 404 write_count_histogram_.RecordWriteOccured(); | 409 write_count_histogram_.RecordWriteOccured(); |
| 405 | 410 |
| 406 if (pref_filter_) | 411 if (pref_filter_) |
| 407 pref_filter_->FilterSerializeData(prefs_.get()); | 412 pref_filter_->FilterSerializeData(prefs_.get()); |
| 408 | 413 |
| 409 JSONStringValueSerializer serializer(output); | 414 JSONStringValueSerializer serializer(output); |
| 410 // Not pretty-printing prefs shrinks pref file size by ~30%. To obtain | 415 // Not pretty-printing prefs shrinks pref file size by ~30%. To obtain |
| 411 // readable prefs for debugging purposes, you can dump your prefs into any | 416 // readable prefs for debugging purposes, you can dump your prefs into any |
| 412 // command-line or online JSON pretty printing tool. | 417 // command-line or online JSON pretty printing tool. |
| 413 serializer.set_pretty_print(false); | 418 serializer.set_pretty_print(false); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 425 FOR_EACH_OBSERVER(PrefStore::Observer, | 430 FOR_EACH_OBSERVER(PrefStore::Observer, |
| 426 observers_, | 431 observers_, |
| 427 OnInitializationCompleted(false)); | 432 OnInitializationCompleted(false)); |
| 428 return; | 433 return; |
| 429 } | 434 } |
| 430 | 435 |
| 431 prefs_ = prefs.Pass(); | 436 prefs_ = prefs.Pass(); |
| 432 | 437 |
| 433 initialized_ = true; | 438 initialized_ = true; |
| 434 | 439 |
| 435 if (schedule_write && !read_only_) | 440 if (schedule_write) |
| 436 writer_.ScheduleWrite(this); | 441 ScheduleWrite(DEFAULT_PREF_WRITE_FLAGS); |
| 437 | 442 |
| 438 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) | 443 if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE) |
| 439 error_delegate_->OnError(read_error_); | 444 error_delegate_->OnError(read_error_); |
| 440 | 445 |
| 441 FOR_EACH_OBSERVER(PrefStore::Observer, | 446 FOR_EACH_OBSERVER(PrefStore::Observer, |
| 442 observers_, | 447 observers_, |
| 443 OnInitializationCompleted(true)); | 448 OnInitializationCompleted(true)); |
| 444 | 449 |
| 445 return; | 450 return; |
| 446 } | 451 } |
| 447 | 452 |
| 453 void JsonPrefStore::ScheduleWrite(uint32 flags) { | |
| 454 if (read_only_) | |
| 455 return; | |
| 456 | |
| 457 if (flags & LOSSY_PREF_WRITE_FLAG) { | |
|
Mattias Nissler (ping if slow)
2015/05/06 07:26:04
This file doesn't use braces for single-line condi
raymes
2015/05/07 05:45:41
Done.
| |
| 458 pending_lossy_write_ = true; | |
| 459 } else { | |
| 460 writer_.ScheduleWrite(this); | |
| 461 } | |
| 462 } | |
| 463 | |
| 448 // NOTE: This value should NOT be changed without renaming the histogram | 464 // NOTE: This value should NOT be changed without renaming the histogram |
| 449 // otherwise it will create incompatible buckets. | 465 // otherwise it will create incompatible buckets. |
| 450 const int32_t | 466 const int32_t |
| 451 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins = 5; | 467 JsonPrefStore::WriteCountHistogram::kHistogramWriteReportIntervalMins = 5; |
| 452 | 468 |
| 453 JsonPrefStore::WriteCountHistogram::WriteCountHistogram( | 469 JsonPrefStore::WriteCountHistogram::WriteCountHistogram( |
| 454 const base::TimeDelta& commit_interval, | 470 const base::TimeDelta& commit_interval, |
| 455 const base::FilePath& path) | 471 const base::FilePath& path) |
| 456 : WriteCountHistogram(commit_interval, | 472 : WriteCountHistogram(commit_interval, |
| 457 path, | 473 path, |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 525 DCHECK_EQ(31, num_buckets); | 541 DCHECK_EQ(31, num_buckets); |
| 526 | 542 |
| 527 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS | 543 // The histogram below is an expansion of the UMA_HISTOGRAM_CUSTOM_COUNTS |
| 528 // macro adapted to allow for a dynamically suffixed histogram name. | 544 // macro adapted to allow for a dynamically suffixed histogram name. |
| 529 // Note: The factory creates and owns the histogram. | 545 // Note: The factory creates and owns the histogram. |
| 530 base::HistogramBase* histogram = base::Histogram::FactoryGet( | 546 base::HistogramBase* histogram = base::Histogram::FactoryGet( |
| 531 histogram_name, min_value, max_value, num_buckets, | 547 histogram_name, min_value, max_value, num_buckets, |
| 532 base::HistogramBase::kUmaTargetedHistogramFlag); | 548 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 533 return histogram; | 549 return histogram; |
| 534 } | 550 } |
| OLD | NEW |