summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-05-20 18:20:29 +0000
committersanjeevr@chromium.org <sanjeevr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-05-20 18:20:29 +0000
commit6658ca866d0e28950179a65179c1a4d6e8e3f8df (patch)
tree5e7c4767096a10bc71aa0bd4bf2228f1476a564e
parente45b40f5bfe4f20aece5a8fa25d9a4c6c89f22e1 (diff)
downloadchromium_src-6658ca866d0e28950179a65179c1a4d6e8e3f8df.zip
chromium_src-6658ca866d0e28950179a65179c1a4d6e8e3f8df.tar.gz
chromium_src-6658ca866d0e28950179a65179c1a4d6e8e3f8df.tar.bz2
Moved ImportantFileWriter to chrome/common in preparation of moving JsonPrefStore to chrome/common.
BUG=None. TEST=Unit-tests moved and modified. Review URL: http://codereview.chromium.org/2128014 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@47818 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/bookmarks/bookmark_storage.cc3
-rw-r--r--chrome/browser/bookmarks/bookmark_storage.h2
-rw-r--r--chrome/browser/json_pref_store.cc5
-rw-r--r--chrome/browser/json_pref_store.h2
-rw-r--r--chrome/chrome_browser.gypi2
-rw-r--r--chrome/chrome_common.gypi2
-rw-r--r--chrome/chrome_tests.gypi2
-rw-r--r--chrome/common/important_file_writer.cc (renamed from chrome/browser/important_file_writer.cc)26
-rw-r--r--chrome/common/important_file_writer.h (renamed from chrome/browser/important_file_writer.h)16
-rw-r--r--chrome/common/important_file_writer_unittest.cc (renamed from chrome/browser/important_file_writer_unittest.cc)18
10 files changed, 49 insertions, 29 deletions
diff --git a/chrome/browser/bookmarks/bookmark_storage.cc b/chrome/browser/bookmarks/bookmark_storage.cc
index fd145b0..365ba37 100644
--- a/chrome/browser/bookmarks/bookmark_storage.cc
+++ b/chrome/browser/bookmarks/bookmark_storage.cc
@@ -130,7 +130,8 @@ class BookmarkStorage::LoadTask : public Task {
BookmarkStorage::BookmarkStorage(Profile* profile, BookmarkModel* model)
: profile_(profile),
model_(model),
- writer_(profile->GetPath().Append(chrome::kBookmarksFileName)),
+ writer_(profile->GetPath().Append(chrome::kBookmarksFileName),
+ ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE)),
tmp_history_path_(
profile->GetPath().Append(chrome::kHistoryBookmarksFileName)) {
writer_.set_commit_interval(base::TimeDelta::FromMilliseconds(kSaveDelayMS));
diff --git a/chrome/browser/bookmarks/bookmark_storage.h b/chrome/browser/bookmarks/bookmark_storage.h
index bc55219..4182dc4 100644
--- a/chrome/browser/bookmarks/bookmark_storage.h
+++ b/chrome/browser/bookmarks/bookmark_storage.h
@@ -9,7 +9,7 @@
#include "base/ref_counted.h"
#include "base/scoped_ptr.h"
#include "chrome/browser/bookmarks/bookmark_index.h"
-#include "chrome/browser/important_file_writer.h"
+#include "chrome/common/important_file_writer.h"
#include "chrome/common/notification_observer.h"
#include "chrome/common/notification_registrar.h"
diff --git a/chrome/browser/json_pref_store.cc b/chrome/browser/json_pref_store.cc
index 389986b..40fd309 100644
--- a/chrome/browser/json_pref_store.cc
+++ b/chrome/browser/json_pref_store.cc
@@ -8,6 +8,7 @@
#include "base/file_util.h"
#include "base/values.h"
+#include "chrome/browser/chrome_thread.h"
#include "chrome/common/json_value_serializer.h"
namespace {
@@ -21,7 +22,9 @@ JsonPrefStore::JsonPrefStore(const FilePath& filename)
: path_(filename),
prefs_(new DictionaryValue()),
read_only_(false),
- writer_(filename) { }
+ writer_(filename,
+ ChromeThread::GetMessageLoopProxyForThread(ChromeThread::FILE)) {
+}
JsonPrefStore::~JsonPrefStore() {
if (writer_.HasPendingWrite() && !read_only_)
diff --git a/chrome/browser/json_pref_store.h b/chrome/browser/json_pref_store.h
index 2bf9e22..e25d310 100644
--- a/chrome/browser/json_pref_store.h
+++ b/chrome/browser/json_pref_store.h
@@ -8,8 +8,8 @@
#include <string>
#include "base/scoped_ptr.h"
-#include "chrome/browser/important_file_writer.h"
#include "chrome/browser/pref_store.h"
+#include "chrome/common/important_file_writer.h"
class DictionaryValue;
class FilePath;
diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi
index 8241868..cbbc0b2 100644
--- a/chrome/chrome_browser.gypi
+++ b/chrome/chrome_browser.gypi
@@ -1511,8 +1511,6 @@
'browser/idle_win.cc',
'browser/ime_input.cc',
'browser/ime_input.h',
- 'browser/important_file_writer.cc',
- 'browser/important_file_writer.h',
'browser/importer/firefox2_importer.cc',
'browser/importer/firefox2_importer.h',
'browser/importer/firefox3_importer.cc',
diff --git a/chrome/chrome_common.gypi b/chrome/chrome_common.gypi
index 3fa6fe1..d0da13e 100644
--- a/chrome/chrome_common.gypi
+++ b/chrome/chrome_common.gypi
@@ -201,6 +201,8 @@
'common/gears_api.h',
'common/gpu_plugin.cc',
'common/gpu_plugin.h',
+ 'common/important_file_writer.cc',
+ 'common/important_file_writer.h',
'common/jstemplate_builder.cc',
'common/jstemplate_builder.h',
'common/libxml_utils.cc',
diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi
index 48796cd..8835fd5 100644
--- a/chrome/chrome_tests.gypi
+++ b/chrome/chrome_tests.gypi
@@ -826,7 +826,6 @@
'browser/history/visit_tracker_unittest.cc',
'browser/host_content_settings_map_unittest.cc',
'browser/host_zoom_map_unittest.cc',
- 'browser/important_file_writer_unittest.cc',
'browser/importer/firefox_importer_unittest.cc',
'browser/importer/firefox_importer_unittest_messages_internal.h',
'browser/importer/firefox_importer_unittest_utils.h',
@@ -983,6 +982,7 @@
'common/extensions/update_manifest_unittest.cc',
'common/extensions/url_pattern_unittest.cc',
'common/extensions/user_script_unittest.cc',
+ 'common/important_file_writer_unittest.cc',
'common/json_value_serializer_unittest.cc',
'common/mru_cache_unittest.cc',
# TODO(sanjeevr): Move the 2 below files to common_net_unit_tests
diff --git a/chrome/browser/important_file_writer.cc b/chrome/common/important_file_writer.cc
index 3d2029a..3466958 100644
--- a/chrome/browser/important_file_writer.cc
+++ b/chrome/common/important_file_writer.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "chrome/browser/important_file_writer.h"
+#include "chrome/common/important_file_writer.h"
#include <stdio.h>
@@ -12,12 +12,11 @@
#include "base/file_path.h"
#include "base/file_util.h"
#include "base/logging.h"
-#include "base/message_loop.h"
+#include "base/message_loop_proxy.h"
#include "base/string_util.h"
#include "base/task.h"
#include "base/thread.h"
#include "base/time.h"
-#include "chrome/browser/chrome_thread.h"
using base::TimeDelta;
@@ -53,7 +52,8 @@ class WriteToDiskTask : public Task {
}
if (bytes_written < data_.length()) {
file_util::Delete(tmp_file_path, false);
- LogFailure("error writing, bytes_written=" + UintToString(bytes_written));
+ LogFailure("error writing, bytes_written=" + UintToString(
+ static_cast<unsigned int>(bytes_written)));
return;
}
@@ -84,11 +84,15 @@ class WriteToDiskTask : public Task {
} // namespace
-ImportantFileWriter::ImportantFileWriter(const FilePath& path)
- : path_(path),
- serializer_(NULL),
- commit_interval_(TimeDelta::FromMilliseconds(kDefaultCommitIntervalMs)) {
+ImportantFileWriter::ImportantFileWriter(
+ const FilePath& path, base::MessageLoopProxy* file_message_loop_proxy)
+ : path_(path),
+ file_message_loop_proxy_(file_message_loop_proxy),
+ serializer_(NULL),
+ commit_interval_(TimeDelta::FromMilliseconds(
+ kDefaultCommitIntervalMs)) {
DCHECK(CalledOnValidThread());
+ DCHECK(file_message_loop_proxy_.get());
}
ImportantFileWriter::~ImportantFileWriter() {
@@ -109,8 +113,10 @@ void ImportantFileWriter::WriteNow(const std::string& data) {
if (HasPendingWrite())
timer_.Stop();
- ChromeThread::PostTask(
- ChromeThread::FILE, FROM_HERE, new WriteToDiskTask(path_, data));
+ // TODO(sanjeevr): Add a DCHECK for the return value of PostTask.
+ // (Some tests fail if we add the DCHECK and they need to be fixed first).
+ file_message_loop_proxy_->PostTask(FROM_HERE,
+ new WriteToDiskTask(path_, data));
}
void ImportantFileWriter::ScheduleWrite(DataSerializer* serializer) {
diff --git a/chrome/browser/important_file_writer.h b/chrome/common/important_file_writer.h
index c78d250..d4f57a0 100644
--- a/chrome/browser/important_file_writer.h
+++ b/chrome/common/important_file_writer.h
@@ -2,18 +2,20 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef CHROME_BROWSER_IMPORTANT_FILE_WRITER_H_
-#define CHROME_BROWSER_IMPORTANT_FILE_WRITER_H_
+#ifndef CHROME_COMMON_IMPORTANT_FILE_WRITER_H_
+#define CHROME_COMMON_IMPORTANT_FILE_WRITER_H_
#include <string>
#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/non_thread_safe.h"
+#include "base/ref_counted.h"
#include "base/time.h"
#include "base/timer.h"
namespace base {
+class MessageLoopProxy;
class Thread;
}
@@ -49,8 +51,11 @@ class ImportantFileWriter : public NonThreadSafe {
// Initialize the writer.
// |path| is the name of file to write.
+ // |file_message_loop_proxy| is the MessageLoopProxy for a thread on which
+ // file I/O can be done.
// All non-const methods, ctor and dtor must be called on the same thread.
- explicit ImportantFileWriter(const FilePath& path);
+ ImportantFileWriter(const FilePath& path,
+ base::MessageLoopProxy* file_message_loop_proxy);
// You have to ensure that there are no pending writes at the moment
// of destruction.
@@ -89,6 +94,9 @@ class ImportantFileWriter : public NonThreadSafe {
// Path being written to.
const FilePath path_;
+ // MessageLoopProxy for the thread on which file I/O can be done.
+ scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy_;
+
// Timer used to schedule commit after ScheduleWrite.
base::OneShotTimer<ImportantFileWriter> timer_;
@@ -101,4 +109,4 @@ class ImportantFileWriter : public NonThreadSafe {
DISALLOW_COPY_AND_ASSIGN(ImportantFileWriter);
};
-#endif // CHROME_BROWSER_IMPORTANT_FILE_WRITER_H_
+#endif // CHROME_COMMON_IMPORTANT_FILE_WRITER_H_
diff --git a/chrome/browser/important_file_writer_unittest.cc b/chrome/common/important_file_writer_unittest.cc
index 69bb00a..3d44ae0 100644
--- a/chrome/browser/important_file_writer_unittest.cc
+++ b/chrome/common/important_file_writer_unittest.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "chrome/browser/important_file_writer.h"
+#include "chrome/common/important_file_writer.h"
#include "base/compiler_specific.h"
#include "base/file_path.h"
@@ -12,7 +12,6 @@
#include "base/scoped_temp_dir.h"
#include "base/thread.h"
#include "base/time.h"
-#include "chrome/browser/chrome_thread.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
@@ -43,7 +42,7 @@ class DataSerializer : public ImportantFileWriter::DataSerializer {
class ImportantFileWriterTest : public testing::Test {
public:
- ImportantFileWriterTest() : file_thread_(ChromeThread::FILE, &loop_) { }
+ ImportantFileWriterTest() { }
virtual void SetUp() {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
file_ = temp_dir_.path().AppendASCII("test-file");
@@ -54,12 +53,12 @@ class ImportantFileWriterTest : public testing::Test {
MessageLoop loop_;
private:
- ChromeThread file_thread_;
ScopedTempDir temp_dir_;
};
TEST_F(ImportantFileWriterTest, Basic) {
- ImportantFileWriter writer(file_);
+ ImportantFileWriter writer(file_,
+ base::MessageLoopProxy::CreateForCurrentThread());
EXPECT_FALSE(file_util::PathExists(writer.path()));
writer.WriteNow("foo");
loop_.RunAllPending();
@@ -69,7 +68,8 @@ TEST_F(ImportantFileWriterTest, Basic) {
}
TEST_F(ImportantFileWriterTest, ScheduleWrite) {
- ImportantFileWriter writer(file_);
+ ImportantFileWriter writer(file_,
+ base::MessageLoopProxy::CreateForCurrentThread());
writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25));
EXPECT_FALSE(writer.HasPendingWrite());
DataSerializer serializer("foo");
@@ -84,7 +84,8 @@ TEST_F(ImportantFileWriterTest, ScheduleWrite) {
}
TEST_F(ImportantFileWriterTest, DoScheduledWrite) {
- ImportantFileWriter writer(file_);
+ ImportantFileWriter writer(file_,
+ base::MessageLoopProxy::CreateForCurrentThread());
EXPECT_FALSE(writer.HasPendingWrite());
DataSerializer serializer("foo");
writer.ScheduleWrite(&serializer);
@@ -99,7 +100,8 @@ TEST_F(ImportantFileWriterTest, DoScheduledWrite) {
}
TEST_F(ImportantFileWriterTest, BatchingWrites) {
- ImportantFileWriter writer(file_);
+ ImportantFileWriter writer(file_,
+ base::MessageLoopProxy::CreateForCurrentThread());
writer.set_commit_interval(base::TimeDelta::FromMilliseconds(25));
DataSerializer foo("foo"), bar("bar"), baz("baz");
writer.ScheduleWrite(&foo);