Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Unified Diff: base/prefs/json_pref_store.cc

Issue 1127963002: Implement lossy pref behavior for JsonPrefStore. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@prefs-fix-flags
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/prefs/json_pref_store.cc
diff --git a/base/prefs/json_pref_store.cc b/base/prefs/json_pref_store.cc
index 8ce59745536d02fe4af6279a3d85312966a34937..a9a3ec7dc72a686028052f060cc5cf8526b0d665 100644
--- a/base/prefs/json_pref_store.cc
+++ b/base/prefs/json_pref_store.cc
@@ -150,35 +150,44 @@ JsonPrefStore::JsonPrefStore(
const base::FilePath& filename,
const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner,
scoped_ptr<PrefFilter> pref_filter)
- : path_(filename),
- sequenced_task_runner_(sequenced_task_runner),
- prefs_(new base::DictionaryValue()),
- read_only_(false),
- writer_(filename, sequenced_task_runner),
- pref_filter_(pref_filter.Pass()),
- initialized_(false),
- filtering_in_progress_(false),
- read_error_(PREF_READ_ERROR_NONE),
- write_count_histogram_(writer_.commit_interval(), path_) {
- DCHECK(!path_.empty());
+ : JsonPrefStore(filename,
+ base::FilePath(),
+ sequenced_task_runner,
+ pref_filter.Pass()) {
+}
+
+JsonPrefStore::JsonPrefStore(
+ const base::FilePath& filename,
+ const base::FilePath& alternate_filename,
+ const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner,
+ scoped_ptr<PrefFilter> pref_filter)
+ : JsonPrefStore(
+ filename,
+ alternate_filename,
+ sequenced_task_runner,
+ scoped_ptr<base::ImportantFileWriter>(
+ new base::ImportantFileWriterImpl(filename,
+ sequenced_task_runner)),
+ pref_filter.Pass()) {
}
JsonPrefStore::JsonPrefStore(
const base::FilePath& filename,
const base::FilePath& alternate_filename,
const scoped_refptr<base::SequencedTaskRunner>& sequenced_task_runner,
+ scoped_ptr<base::ImportantFileWriter> file_writer,
scoped_ptr<PrefFilter> pref_filter)
: path_(filename),
alternate_path_(alternate_filename),
sequenced_task_runner_(sequenced_task_runner),
prefs_(new base::DictionaryValue()),
read_only_(false),
- writer_(filename, sequenced_task_runner),
+ writer_(file_writer.Pass()),
pref_filter_(pref_filter.Pass()),
initialized_(false),
filtering_in_progress_(false),
read_error_(PREF_READ_ERROR_NONE),
- write_count_histogram_(writer_.commit_interval(), path_) {
+ write_count_histogram_(writer_->CommitInterval(), path_) {
DCHECK(!path_.empty());
}
@@ -252,8 +261,7 @@ void JsonPrefStore::SetValueSilently(const std::string& key,
prefs_->Get(key, &old_value);
if (!old_value || !value->Equals(old_value)) {
prefs_->Set(key, new_value.release());
- if (!read_only_)
- writer_.ScheduleWrite(this);
+ ScheduleWrite(flags);
}
}
@@ -268,8 +276,7 @@ void JsonPrefStore::RemoveValueSilently(const std::string& key, uint32 flags) {
DCHECK(CalledOnValidThread());
prefs_->RemovePath(key, NULL);
- if (!read_only_)
- writer_.ScheduleWrite(this);
+ ScheduleWrite(flags);
}
bool JsonPrefStore::ReadOnly() const {
@@ -309,8 +316,13 @@ void JsonPrefStore::ReadPrefsAsync(ReadErrorDelegate* error_delegate) {
void JsonPrefStore::CommitPendingWrite() {
DCHECK(CalledOnValidThread());
- if (writer_.HasPendingWrite() && !read_only_)
- writer_.DoScheduledWrite();
+ // Schedule a write for any lossy writes that are outstanding to ensure that
+ // they get flushed when this function is called.
+ if (pending_lossy_write_)
+ writer_->ScheduleWrite(this);
+
+ if (writer_->HasPendingWrite() && !read_only_)
+ writer_->DoScheduledWrite();
}
void JsonPrefStore::ReportValueChanged(const std::string& key, uint32 flags) {
@@ -321,15 +333,14 @@ void JsonPrefStore::ReportValueChanged(const std::string& key, uint32 flags) {
FOR_EACH_OBSERVER(PrefStore::Observer, observers_, OnPrefValueChanged(key));
- if (!read_only_)
- writer_.ScheduleWrite(this);
+ ScheduleWrite(flags);
}
void JsonPrefStore::RegisterOnNextSuccessfulWriteCallback(
const base::Closure& on_next_successful_write) {
DCHECK(CalledOnValidThread());
- writer_.RegisterOnNextSuccessfulWriteCallback(on_next_successful_write);
+ writer_->RegisterOnNextSuccessfulWriteCallback(on_next_successful_write);
}
void JsonPrefStore::OnFileRead(scoped_ptr<ReadResult> read_result) {
@@ -401,6 +412,8 @@ JsonPrefStore::~JsonPrefStore() {
bool JsonPrefStore::SerializeData(std::string* output) {
DCHECK(CalledOnValidThread());
+ pending_lossy_write_ = false;
+
write_count_histogram_.RecordWriteOccured();
if (pref_filter_)
@@ -432,8 +445,8 @@ void JsonPrefStore::FinalizeFileRead(bool initialization_successful,
initialized_ = true;
- if (schedule_write && !read_only_)
- writer_.ScheduleWrite(this);
+ if (schedule_write)
+ ScheduleWrite(DEFAULT_PREF_WRITE_FLAGS);
if (error_delegate_ && read_error_ != PREF_READ_ERROR_NONE)
error_delegate_->OnError(read_error_);
@@ -445,6 +458,16 @@ void JsonPrefStore::FinalizeFileRead(bool initialization_successful,
return;
}
+void JsonPrefStore::ScheduleWrite(uint32 flags) {
+ if (read_only_)
+ return;
+
+ if (flags & LOSSY_PREF_WRITE_FLAG)
+ pending_lossy_write_ = true;
+ else
+ writer_->ScheduleWrite(this);
+}
+
// NOTE: This value should NOT be changed without renaming the histogram
// otherwise it will create incompatible buckets.
const int32_t

Powered by Google App Engine
This is Rietveld 408576698