diff options
author | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-08 14:41:08 +0000 |
---|---|---|
committer | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-05-08 14:41:08 +0000 |
commit | 6c116404c0e4ebcbfea94ff91e0979bee67222e4 (patch) | |
tree | c749a0f0f2f538ed7651da2f0cda4a5667cd8fcc /chrome/common/important_file_writer.cc | |
parent | 6f45d5f57decad87b06a63239d77657ed458e361 (diff) | |
download | chromium_src-6c116404c0e4ebcbfea94ff91e0979bee67222e4.zip chromium_src-6c116404c0e4ebcbfea94ff91e0979bee67222e4.tar.gz chromium_src-6c116404c0e4ebcbfea94ff91e0979bee67222e4.tar.bz2 |
Converted BookmarkStorage to use ImportantFileWriter
Also made BookmarkStorage completely responsible for
migration of bookmark data from history database.
Previously the logic crossed file and class boundaries a few
times. This made it a bit hard to follow. Now it should be
a bit more clear.
Made ImportantFileWriter also batch data serializations.
TEST=Make sure that bookmarks still work. Also test migrating bookmarks from history database.
http://crbug.com/10618
Review URL: http://codereview.chromium.org/99192
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15635 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/important_file_writer.cc')
-rw-r--r-- | chrome/common/important_file_writer.cc | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/chrome/common/important_file_writer.cc b/chrome/common/important_file_writer.cc index 5707744..03e138c 100644 --- a/chrome/common/important_file_writer.cc +++ b/chrome/common/important_file_writer.cc @@ -86,22 +86,27 @@ ImportantFileWriter::ImportantFileWriter(const FilePath& path, const base::Thread* backend_thread) : path_(path), backend_thread_(backend_thread), + serializer_(NULL), commit_interval_(TimeDelta::FromMilliseconds(kDefaultCommitIntervalMs)) { DCHECK(CalledOnValidThread()); } ImportantFileWriter::~ImportantFileWriter() { + // We're usually a member variable of some other object, which also tends + // to be our serializer. It may not be safe to call back to the parent object + // being destructed. + DCHECK(!HasPendingWrite()); +} + +bool ImportantFileWriter::HasPendingWrite() const { DCHECK(CalledOnValidThread()); - if (timer_.IsRunning()) { - timer_.Stop(); - CommitPendingData(); - } + return timer_.IsRunning(); } void ImportantFileWriter::WriteNow(const std::string& data) { DCHECK(CalledOnValidThread()); - if (timer_.IsRunning()) + if (HasPendingWrite()) timer_.Stop(); Task* task = new WriteToDiskTask(path_, data); @@ -113,16 +118,32 @@ void ImportantFileWriter::WriteNow(const std::string& data) { } } -void ImportantFileWriter::ScheduleWrite(const std::string& data) { +void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) { DCHECK(CalledOnValidThread()); - data_ = data; + DCHECK(serializer); + serializer_ = serializer; + + if (!MessageLoop::current()) { + // Happens in unit tests. + DoScheduledWrite(); + return; + } + if (!timer_.IsRunning()) { timer_.Start(commit_interval_, this, - &ImportantFileWriter::CommitPendingData); + &ImportantFileWriter::DoScheduledWrite); } } -void ImportantFileWriter::CommitPendingData() { - WriteNow(data_); +void ImportantFileWriter::DoScheduledWrite() { + DCHECK(serializer_); + std::string data; + if (serializer_->SerializeData(&data)) { + WriteNow(data); + } else { + LOG(WARNING) << "failed to serialize data to be saved in " + << path_.value(); + } + serializer_ = NULL; } |