diff options
author | ttuttle@chromium.org <ttuttle@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-26 08:28:41 +0000 |
---|---|---|
committer | ttuttle@chromium.org <ttuttle@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-07-26 08:28:41 +0000 |
commit | dc02c6f484678bdbb7c1df7b454f665c61a6d5f5 (patch) | |
tree | 53bfb88bcfbf3477a21a79cfed6f3ca4a8f688be /net/disk_cache | |
parent | 45a07944f41929553254bdb13a823d259c4093d3 (diff) | |
download | chromium_src-dc02c6f484678bdbb7c1df7b454f665c61a6d5f5.zip chromium_src-dc02c6f484678bdbb7c1df7b454f665c61a6d5f5.tar.gz chromium_src-dc02c6f484678bdbb7c1df7b454f665c61a6d5f5.tar.bz2 |
Use struct SimpleIndexLoadResult* for SimpleIndexFile callbacks.
Accept an out_load_result parameter for LoadIndexFile (and other
methods internal to SimpleIndexFile) and fill it in, instead of
passing the result to the callback directly.
Review URL: https://chromiumcodereview.appspot.com/19653002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@213799 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/disk_cache')
-rw-r--r-- | net/disk_cache/simple/simple_index.cc | 25 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_index.h | 4 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_index_file.cc | 132 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_index_file.h | 45 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_index_file_unittest.cc | 86 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_index_unittest.cc | 52 |
6 files changed, 171 insertions, 173 deletions
diff --git a/net/disk_cache/simple/simple_index.cc b/net/disk_cache/simple/simple_index.cc index 8dbb88a..caec19f60 100644 --- a/net/disk_cache/simple/simple_index.cc +++ b/net/disk_cache/simple/simple_index.cc @@ -152,10 +152,13 @@ void SimpleIndex::Initialize(base::Time cache_mtime) { base::Bind(&SimpleIndex::OnActivityStateChange, AsWeakPtr()))); #endif - index_file_->LoadIndexEntries(cache_mtime, - io_thread_, - base::Bind(&SimpleIndex::MergeInitializingSet, - AsWeakPtr())); + SimpleIndexLoadResult* load_result = new SimpleIndexLoadResult(); + scoped_ptr<SimpleIndexLoadResult> load_result_scoped(load_result); + base::Closure reply = base::Bind( + &SimpleIndex::MergeInitializingSet, + AsWeakPtr(), + base::Passed(&load_result_scoped)); + index_file_->LoadIndexEntries(cache_mtime, reply, load_result); } bool SimpleIndex::SetMaxSize(int max_bytes) { @@ -342,10 +345,12 @@ void SimpleIndex::UpdateEntryIteratorSize(EntrySet::iterator* it, (*it)->second.SetEntrySize(entry_size); } -void SimpleIndex::MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries, - bool force_index_flush) { +void SimpleIndex::MergeInitializingSet( + scoped_ptr<SimpleIndexLoadResult> load_result) { DCHECK(io_thread_checker_.CalledOnValidThread()); - DCHECK(index_file_entries); + DCHECK(load_result->did_load); + + SimpleIndex::EntrySet* index_file_entries = &load_result->entries; // First, remove the entries that are in the |removed_entries_| from both // sets. for (base::hash_set<uint64>::const_iterator it = @@ -369,9 +374,9 @@ void SimpleIndex::MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries, initialized_ = true; removed_entries_.clear(); - // The actual IO is asynchronous, so calling WriteToDisk() shouldn't slow down - // much the merge. - if (force_index_flush) + // The actual IO is asynchronous, so calling WriteToDisk() shouldn't slow the + // merge down much. + if (load_result->flush_required) WriteToDisk(); UMA_HISTOGRAM_CUSTOM_COUNTS("SimpleCache.IndexInitializationWaiters", diff --git a/net/disk_cache/simple/simple_index.h b/net/disk_cache/simple/simple_index.h index 08a83bb..929b02d 100644 --- a/net/disk_cache/simple/simple_index.h +++ b/net/disk_cache/simple/simple_index.h @@ -34,6 +34,7 @@ class PickleIterator; namespace disk_cache { class SimpleIndexFile; +struct SimpleIndexLoadResult; class NET_EXPORT_PRIVATE EntryMetadata { public: @@ -136,8 +137,7 @@ class NET_EXPORT_PRIVATE SimpleIndex void UpdateEntryIteratorSize(EntrySet::iterator* it, uint64 entry_size); // Must run on IO Thread. - void MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries, - bool force_index_flush); + void MergeInitializingSet(scoped_ptr<SimpleIndexLoadResult> load_result); #if defined(OS_ANDROID) void OnActivityStateChange(base::android::ActivityState state); diff --git a/net/disk_cache/simple/simple_index_file.cc b/net/disk_cache/simple/simple_index_file.cc index f25d376..a797ca6 100644 --- a/net/disk_cache/simple/simple_index_file.cc +++ b/net/disk_cache/simple/simple_index_file.cc @@ -69,6 +69,19 @@ void WriteToDiskInternal(const base::FilePath& index_filename, namespace disk_cache { +SimpleIndexLoadResult::SimpleIndexLoadResult() : did_load(false), + flush_required(false) { +} + +SimpleIndexLoadResult::~SimpleIndexLoadResult() { +} + +void SimpleIndexLoadResult::Reset() { + did_load = false; + flush_required = false; + entries.clear(); +} + // static const char SimpleIndexFile::kIndexFileName[] = "the-real-index"; // static @@ -122,18 +135,13 @@ SimpleIndexFile::SimpleIndexFile( SimpleIndexFile::~SimpleIndexFile() {} -void SimpleIndexFile::LoadIndexEntries( - base::Time cache_last_modified, - scoped_refptr<base::SingleThreadTaskRunner> response_thread, - const IndexCompletionCallback& completion_callback) { - worker_pool_->PostTask( - FROM_HERE, - base::Bind(&SimpleIndexFile::SyncLoadIndexEntries, - cache_last_modified, - cache_directory_, - index_file_, - response_thread, - completion_callback)); +void SimpleIndexFile::LoadIndexEntries(base::Time cache_last_modified, + const base::Closure& callback, + SimpleIndexLoadResult* out_result) { + base::Closure task = base::Bind(&SimpleIndexFile::SyncLoadIndexEntries, + cache_last_modified, cache_directory_, + index_file_, out_result); + worker_pool_->PostTaskAndReply(FROM_HERE, task, callback); } void SimpleIndexFile::WriteToDisk(const SimpleIndex::EntrySet& entry_set, @@ -171,9 +179,10 @@ void SimpleIndexFile::SyncLoadIndexEntries( base::Time cache_last_modified, const base::FilePath& cache_directory, const base::FilePath& index_file_path, - scoped_refptr<base::SingleThreadTaskRunner> response_thread, - const IndexCompletionCallback& completion_callback) { - scoped_ptr<SimpleIndex::EntrySet> index_file_entries; + SimpleIndexLoadResult* out_result) { + // TODO(felipeg): probably could load a stale index and use it for something. + const SimpleIndex::EntrySet& entries = out_result->entries; + const bool index_file_exists = base::PathExists(index_file_path); // Used in histograms. Please only add new values at the end. @@ -199,30 +208,25 @@ void SimpleIndexFile::SyncLoadIndexEntries( } const base::TimeTicks start = base::TimeTicks::Now(); - index_file_entries = SyncLoadFromDisk(index_file_path); + SyncLoadFromDisk(index_file_path, out_result); UMA_HISTOGRAM_TIMES("SimpleCache.IndexLoadTime", base::TimeTicks::Now() - start); UMA_HISTOGRAM_COUNTS("SimpleCache.IndexEntriesLoaded", - index_file_entries ? index_file_entries->size() : 0); - if (!index_file_entries) + out_result->did_load ? entries.size() : 0); + if (!out_result->did_load) index_file_state = INDEX_STATE_CORRUPT; } UMA_HISTOGRAM_ENUMERATION("SimpleCache.IndexFileStateOnLoad", index_file_state, INDEX_STATE_MAX); - bool force_index_flush = false; - if (!index_file_entries) { + if (!out_result->did_load) { const base::TimeTicks start = base::TimeTicks::Now(); - index_file_entries = SyncRestoreFromDisk(cache_directory, index_file_path); + SyncRestoreFromDisk(cache_directory, index_file_path, out_result); UMA_HISTOGRAM_MEDIUM_TIMES("SimpleCache.IndexRestoreTime", base::TimeTicks::Now() - start); UMA_HISTOGRAM_COUNTS("SimpleCache.IndexEntriesRestored", - index_file_entries->size()); - - // When we restore from disk we write the merged index file to disk right - // away, this might save us from having to restore again next time. - force_index_flush = true; + entries.size()); } // Used in histograms. Please only add new values at the end. @@ -234,42 +238,36 @@ void SimpleIndexFile::SyncLoadIndexEntries( }; int initialize_method; if (index_file_exists) { - if (force_index_flush) + if (out_result->flush_required) initialize_method = INITIALIZE_METHOD_RECOVERED; else initialize_method = INITIALIZE_METHOD_LOADED; } else { UMA_HISTOGRAM_COUNTS("SimpleCache.IndexCreatedEntryCount", - index_file_entries->size()); + entries.size()); initialize_method = INITIALIZE_METHOD_NEWCACHE; } UMA_HISTOGRAM_ENUMERATION("SimpleCache.IndexInitializeMethod", initialize_method, INITIALIZE_METHOD_MAX); - response_thread->PostTask(FROM_HERE, - base::Bind(completion_callback, - base::Passed(&index_file_entries), - force_index_flush)); } // static -scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::SyncLoadFromDisk( - const base::FilePath& index_filename) { +void SimpleIndexFile::SyncLoadFromDisk(const base::FilePath& index_filename, + SimpleIndexLoadResult* out_result) { + out_result->Reset(); + std::string contents; if (!file_util::ReadFileToString(index_filename, &contents)) { LOG(WARNING) << "Could not read Simple Index file."; base::DeleteFile(index_filename, false); - return scoped_ptr<SimpleIndex::EntrySet>(); + return; } - scoped_ptr<SimpleIndex::EntrySet> entries = - SimpleIndexFile::Deserialize(contents.data(), contents.size()); - if (!entries) { - base::DeleteFile(index_filename, false); - return scoped_ptr<SimpleIndex::EntrySet>(); - } + SimpleIndexFile::Deserialize(contents.data(), contents.size(), out_result); - return entries.Pass(); + if (!out_result->did_load) + base::DeleteFile(index_filename, false); } // static @@ -291,13 +289,17 @@ scoped_ptr<Pickle> SimpleIndexFile::Serialize( } // static -scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::Deserialize(const char* data, - int data_len) { +void SimpleIndexFile::Deserialize(const char* data, int data_len, + SimpleIndexLoadResult* out_result) { DCHECK(data); + + out_result->Reset(); + SimpleIndex::EntrySet* entries = &out_result->entries; + Pickle pickle(data, data_len); if (!pickle.data()) { LOG(WARNING) << "Corrupt Simple Index File."; - return scoped_ptr<SimpleIndex::EntrySet>(); + return; } PickleIterator pickle_it(pickle); @@ -309,46 +311,45 @@ scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::Deserialize(const char* data, if (crc_read != crc_calculated) { LOG(WARNING) << "Invalid CRC in Simple Index file."; - return scoped_ptr<SimpleIndex::EntrySet>(); + return; } SimpleIndexFile::IndexMetadata index_metadata; if (!index_metadata.Deserialize(&pickle_it)) { LOG(ERROR) << "Invalid index_metadata on Simple Cache Index."; - return scoped_ptr<SimpleIndex::EntrySet>(); + return; } if (!index_metadata.CheckIndexMetadata()) { LOG(ERROR) << "Invalid index_metadata on Simple Cache Index."; - return scoped_ptr<SimpleIndex::EntrySet>(); + return; } - scoped_ptr<SimpleIndex::EntrySet> index_file_entries( - new SimpleIndex::EntrySet()); - while (index_file_entries->size() < index_metadata.GetNumberOfEntries()) { + while (entries->size() < index_metadata.GetNumberOfEntries()) { uint64 hash_key; EntryMetadata entry_metadata; if (!pickle_it.ReadUInt64(&hash_key) || !entry_metadata.Deserialize(&pickle_it)) { LOG(WARNING) << "Invalid EntryMetadata in Simple Index file."; - return scoped_ptr<SimpleIndex::EntrySet>(); + entries->clear(); + return; } - SimpleIndex::InsertInEntrySet( - hash_key, entry_metadata, index_file_entries.get()); + SimpleIndex::InsertInEntrySet(hash_key, entry_metadata, entries); } - return index_file_entries.Pass(); + out_result->did_load = true; } // static -scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::SyncRestoreFromDisk( +void SimpleIndexFile::SyncRestoreFromDisk( const base::FilePath& cache_directory, - const base::FilePath& index_file_path) { + const base::FilePath& index_file_path, + SimpleIndexLoadResult* out_result) { LOG(INFO) << "Simple Cache Index is being restored from disk."; base::DeleteFile(index_file_path, /* recursive = */ false); - scoped_ptr<SimpleIndex::EntrySet> index_file_entries( - new SimpleIndex::EntrySet()); + out_result->Reset(); + SimpleIndex::EntrySet* entries = &out_result->entries; // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format. COMPILE_ASSERT(kSimpleEntryFileCount == 3, @@ -387,18 +388,23 @@ scoped_ptr<SimpleIndex::EntrySet> SimpleIndexFile::SyncRestoreFromDisk( last_used_time = info.GetLastModifiedTime(); int64 file_size = info.GetSize(); - SimpleIndex::EntrySet::iterator it = index_file_entries->find(hash_key); - if (it == index_file_entries->end()) { + SimpleIndex::EntrySet::iterator it = entries->find(hash_key); + if (it == entries->end()) { SimpleIndex::InsertInEntrySet( hash_key, EntryMetadata(last_used_time, file_size), - index_file_entries.get()); + entries); } else { // Summing up the total size of the entry through all the *_[0-2] files it->second.SetEntrySize(it->second.GetEntrySize() + file_size); } } - return index_file_entries.Pass(); + + out_result->did_load = true; + + // When we restore from disk we write the merged index file to disk right + // away, this might save us from having to restore again next time. + out_result->flush_required = true; } // static diff --git a/net/disk_cache/simple/simple_index_file.h b/net/disk_cache/simple/simple_index_file.h index e328bc1..8a96a72 100644 --- a/net/disk_cache/simple/simple_index_file.h +++ b/net/disk_cache/simple/simple_index_file.h @@ -28,6 +28,16 @@ namespace disk_cache { const uint64 kSimpleIndexMagicNumber = GG_UINT64_C(0x656e74657220796f); +struct NET_EXPORT_PRIVATE SimpleIndexLoadResult { + SimpleIndexLoadResult(); + ~SimpleIndexLoadResult(); + void Reset(); + + bool did_load; + SimpleIndex::EntrySet entries; + bool flush_required; +}; + // Simple Index File format is a pickle serialized data of IndexMetadata and // EntryMetadata objects. The file format is as follows: one instance of // serialized |IndexMetadata| followed serialized |EntryMetadata| entries @@ -63,20 +73,15 @@ class NET_EXPORT_PRIVATE SimpleIndexFile { uint64 cache_size_; // Total cache storage size in bytes. }; - typedef base::Callback<void( - scoped_ptr<SimpleIndex::EntrySet>, bool force_index_flush)> - IndexCompletionCallback; - SimpleIndexFile(base::SingleThreadTaskRunner* cache_thread, base::TaskRunner* worker_pool, const base::FilePath& cache_directory); virtual ~SimpleIndexFile(); // Get index entries based on current disk context. - virtual void LoadIndexEntries( - base::Time cache_last_modified, - scoped_refptr<base::SingleThreadTaskRunner> response_thread, - const SimpleIndexFile::IndexCompletionCallback& completion_callback); + virtual void LoadIndexEntries(base::Time cache_last_modified, + const base::Closure& callback, + SimpleIndexLoadResult* out_result); // Write the specified set of entries to disk. virtual void WriteToDisk(const SimpleIndex::EntrySet& entry_set, @@ -93,17 +98,15 @@ class NET_EXPORT_PRIVATE SimpleIndexFile { friend class WrappedSimpleIndexFile; // Synchronous (IO performing) implementation of LoadIndexEntries. - static void SyncLoadIndexEntries( - base::Time cache_last_modified, - const base::FilePath& cache_directory, - const base::FilePath& index_file_path, - scoped_refptr<base::SingleThreadTaskRunner> response_thread, - const SimpleIndexFile::IndexCompletionCallback& completion_callback); + static void SyncLoadIndexEntries(base::Time cache_last_modified, + const base::FilePath& cache_directory, + const base::FilePath& index_file_path, + SimpleIndexLoadResult* out_result); // Load the index file from disk returning an EntrySet. Upon failure, returns // NULL. - static scoped_ptr<SimpleIndex::EntrySet> SyncLoadFromDisk( - const base::FilePath& index_filename); + static void SyncLoadFromDisk(const base::FilePath& index_filename, + SimpleIndexLoadResult* out_result); // Returns a scoped_ptr for a newly allocated Pickle containing the serialized // data to be written to a file. @@ -113,14 +116,14 @@ class NET_EXPORT_PRIVATE SimpleIndexFile { // Given the contents of an index file |data| of length |data_len|, returns // the corresponding EntrySet. Returns NULL on error. - static scoped_ptr<SimpleIndex::EntrySet> Deserialize(const char* data, - int data_len); + static void Deserialize(const char* data, int data_len, + SimpleIndexLoadResult* out_result); // Scan the index directory for entries, returning an EntrySet of all entries // found. - static scoped_ptr<SimpleIndex::EntrySet> SyncRestoreFromDisk( - const base::FilePath& cache_directory, - const base::FilePath& index_file_path); + static void SyncRestoreFromDisk(const base::FilePath& cache_directory, + const base::FilePath& index_file_path, + SimpleIndexLoadResult* out_result); // Determines if an index file is stale relative to the time of last // modification of the cache directory. diff --git a/net/disk_cache/simple/simple_index_file_unittest.cc b/net/disk_cache/simple/simple_index_file_unittest.cc index e1267fa..bf7ee83 100644 --- a/net/disk_cache/simple/simple_index_file_unittest.cc +++ b/net/disk_cache/simple/simple_index_file_unittest.cc @@ -80,38 +80,22 @@ class SimpleIndexFileTest : public testing::Test { } protected: - struct IndexCompletionCallbackResult { - IndexCompletionCallbackResult( - scoped_ptr<SimpleIndex::EntrySet> index_file_entries, - bool force_index_flush) - : index_file_entries(index_file_entries.Pass()), - force_index_flush(force_index_flush) { - } - - const scoped_ptr<SimpleIndex::EntrySet> index_file_entries; - const bool force_index_flush; - }; - - SimpleIndexFile::IndexCompletionCallback GetCallback() { - return base::Bind(&SimpleIndexFileTest::IndexCompletionCallback, + SimpleIndexFileTest() : callback_called_(false) {} + + base::Closure GetCallback() { + return base::Bind(&SimpleIndexFileTest::LoadIndexEntriesCallback, base::Unretained(this)); } - IndexCompletionCallbackResult* callback_result() { - return callback_result_.get(); - } + bool callback_called() { return callback_called_; } private: - void IndexCompletionCallback( - scoped_ptr<SimpleIndex::EntrySet> index_file_entries, - bool force_index_flush) { - EXPECT_FALSE(callback_result_); - callback_result_.reset( - new IndexCompletionCallbackResult(index_file_entries.Pass(), - force_index_flush)); + void LoadIndexEntriesCallback() { + EXPECT_FALSE(callback_called_); + callback_called_ = true; } - scoped_ptr<IndexCompletionCallbackResult> callback_result_; + bool callback_called_; }; TEST_F(SimpleIndexFileTest, Serialize) { @@ -133,15 +117,17 @@ TEST_F(SimpleIndexFileTest, Serialize) { index_metadata, entries); EXPECT_TRUE(pickle.get() != NULL); - scoped_ptr<SimpleIndex::EntrySet> new_entries = - WrappedSimpleIndexFile::Deserialize( - static_cast<const char*>(pickle->data()), pickle->size()); - EXPECT_TRUE(new_entries.get() != NULL); - EXPECT_EQ(entries.size(), new_entries->size()); + SimpleIndexLoadResult deserialize_result; + WrappedSimpleIndexFile::Deserialize(static_cast<const char*>(pickle->data()), + pickle->size(), + &deserialize_result); + EXPECT_TRUE(deserialize_result.did_load); + const SimpleIndex::EntrySet& new_entries = deserialize_result.entries; + EXPECT_EQ(entries.size(), new_entries.size()); for (size_t i = 0; i < kNumHashes; ++i) { - SimpleIndex::EntrySet::iterator it = new_entries->find(kHashes[i]); - EXPECT_TRUE(new_entries->end() != it); + SimpleIndex::EntrySet::const_iterator it = new_entries.find(kHashes[i]); + EXPECT_TRUE(new_entries.end() != it); EXPECT_TRUE(CompareTwoEntryMetadata(it->second, metadata_entries[i])); } } @@ -205,25 +191,24 @@ TEST_F(SimpleIndexFileTest, WriteThenLoadIndex) { EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); } - WrappedSimpleIndexFile simple_index_file2(cache_dir.path()); + WrappedSimpleIndexFile simple_index_file(cache_dir.path()); base::Time fake_cache_mtime; - ASSERT_TRUE(simple_util::GetMTime(simple_index_file2.GetIndexFilePath(), + ASSERT_TRUE(simple_util::GetMTime(simple_index_file.GetIndexFilePath(), &fake_cache_mtime)); - simple_index_file2.LoadIndexEntries(fake_cache_mtime, - base::MessageLoopProxy::current(), - GetCallback()); + SimpleIndexLoadResult load_index_result; + simple_index_file.LoadIndexEntries(fake_cache_mtime, + GetCallback(), + &load_index_result); base::RunLoop().RunUntilIdle(); - EXPECT_TRUE(base::PathExists(simple_index_file2.GetIndexFilePath())); - ASSERT_TRUE(callback_result()); - EXPECT_FALSE(callback_result()->force_index_flush); - const SimpleIndex::EntrySet* read_entries = - callback_result()->index_file_entries.get(); - ASSERT_TRUE(read_entries); + EXPECT_TRUE(base::PathExists(simple_index_file.GetIndexFilePath())); + ASSERT_TRUE(callback_called()); + EXPECT_TRUE(load_index_result.did_load); + EXPECT_FALSE(load_index_result.flush_required); - EXPECT_EQ(kNumHashes, read_entries->size()); + EXPECT_EQ(kNumHashes, load_index_result.entries.size()); for (size_t i = 0; i < kNumHashes; ++i) - EXPECT_EQ(1U, read_entries->count(kHashes[i])); + EXPECT_EQ(1U, load_index_result.entries.count(kHashes[i])); } TEST_F(SimpleIndexFileTest, LoadCorruptIndex) { @@ -243,15 +228,16 @@ TEST_F(SimpleIndexFileTest, LoadCorruptIndex) { EXPECT_FALSE(WrappedSimpleIndexFile::IsIndexFileStale(fake_cache_mtime, index_path)); + SimpleIndexLoadResult load_index_result; simple_index_file.LoadIndexEntries(fake_cache_mtime, - base::MessageLoopProxy::current().get(), - GetCallback()); + GetCallback(), + &load_index_result); base::RunLoop().RunUntilIdle(); EXPECT_FALSE(base::PathExists(index_path)); - ASSERT_TRUE(callback_result()); - EXPECT_TRUE(callback_result()->force_index_flush); - EXPECT_TRUE(callback_result()->index_file_entries); + ASSERT_TRUE(callback_called()); + EXPECT_TRUE(load_index_result.did_load); + EXPECT_TRUE(load_index_result.flush_required); } } // namespace disk_cache diff --git a/net/disk_cache/simple/simple_index_unittest.cc b/net/disk_cache/simple/simple_index_unittest.cc index 3c76d5e..cfc83b3 100644 --- a/net/disk_cache/simple/simple_index_unittest.cc +++ b/net/disk_cache/simple/simple_index_unittest.cc @@ -48,19 +48,18 @@ class MockSimpleIndexFile : public SimpleIndexFile, public: MockSimpleIndexFile() : SimpleIndexFile(NULL, NULL, base::FilePath()), - get_index_entries_calls_(0), + load_result_(NULL), + load_index_entries_calls_(0), doom_entry_set_calls_(0), - last_response_thread_(NULL), disk_writes_(0) {} virtual void LoadIndexEntries( base::Time cache_last_modified, - scoped_refptr<base::SingleThreadTaskRunner> response_thread, - const SimpleIndexFile::IndexCompletionCallback& - completion_callback) OVERRIDE { - last_response_thread_ = response_thread.get(); - completion_callback_ = completion_callback; - ++get_index_entries_calls_; + const base::Closure& callback, + SimpleIndexLoadResult* out_load_result) OVERRIDE { + load_callback_ = callback; + load_result_ = out_load_result; + ++load_index_entries_calls_; } virtual void WriteToDisk(const SimpleIndex::EntrySet& entry_set, @@ -83,10 +82,9 @@ class MockSimpleIndexFile : public SimpleIndexFile, entry_set->swap(disk_write_entry_set_); } - const SimpleIndexFile::IndexCompletionCallback& completion_callback() const { - return completion_callback_; - } - int get_index_entries_calls() const { return get_index_entries_calls_; } + const base::Closure& load_callback() const { return load_callback_; } + SimpleIndexLoadResult* load_result() const { return load_result_; } + int load_index_entries_calls() const { return load_index_entries_calls_; } int disk_writes() const { return disk_writes_; } const std::vector<uint64>& last_doom_entry_hashes() const { return last_doom_entry_hashes_; @@ -94,11 +92,11 @@ class MockSimpleIndexFile : public SimpleIndexFile, int doom_entry_set_calls() const { return doom_entry_set_calls_; } private: - SimpleIndexFile::IndexCompletionCallback completion_callback_; - int get_index_entries_calls_; + base::Closure load_callback_; + SimpleIndexLoadResult* load_result_; + int load_index_entries_calls_; std::vector<uint64> last_doom_entry_hashes_; int doom_entry_set_calls_; - base::SingleThreadTaskRunner* last_response_thread_; base::Callback<void(int)> last_doom_reply_callback_; int disk_writes_; SimpleIndex::EntrySet disk_write_entry_set_; @@ -137,14 +135,13 @@ class SimpleIndexTest : public testing::Test { base::Time last_used_time, uint64 entry_size) { uint64 hash_key(simple_util::GetEntryHashKey(key)); - index_file_return_map_.insert(std::make_pair( + index_file_->load_result()->entries.insert(std::make_pair( hash_key, EntryMetadata(last_used_time, entry_size))); } void ReturnIndexFile() { - scoped_ptr<SimpleIndex::EntrySet> map(new SimpleIndex::EntrySet); - map->swap(index_file_return_map_); - index_file_->completion_callback().Run(map.Pass(), false); + index_file_->load_result()->did_load = true; + index_file_->load_callback().Run(); } // Non-const for timer manipulation. @@ -152,7 +149,6 @@ class SimpleIndexTest : public testing::Test { const MockSimpleIndexFile* index_file() const { return index_file_.get(); } protected: - SimpleIndex::EntrySet index_file_return_map_; scoped_ptr<SimpleIndex> index_; base::WeakPtr<MockSimpleIndexFile> index_file_; }; @@ -193,16 +189,18 @@ TEST_F(SimpleIndexTest, IndexSizeCorrectOnMerge) { index()->UpdateEntrySize("seven", 7); EXPECT_EQ(14U, index()->cache_size_); { - scoped_ptr<EntrySet> entries(new EntrySet()); - index()->MergeInitializingSet(entries.Pass(), false); + scoped_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); + result->did_load = true; + index()->MergeInitializingSet(result.Pass()); } EXPECT_EQ(14U, index()->cache_size_); { - scoped_ptr<EntrySet> entries(new EntrySet()); + scoped_ptr<SimpleIndexLoadResult> result(new SimpleIndexLoadResult()); + result->did_load = true; const uint64 hash_key = simple_util::GetEntryHashKey("eleven"); - entries->insert( + result->entries.insert( std::make_pair(hash_key, EntryMetadata(base::Time::Now(), 11))); - index()->MergeInitializingSet(entries.Pass(), false); + index()->MergeInitializingSet(result.Pass()); } EXPECT_EQ(25U, index()->cache_size_); } @@ -233,7 +231,7 @@ TEST_F(SimpleIndexTest, BasicInsertRemove) { TEST_F(SimpleIndexTest, Has) { // Confirm the base index has dispatched the request for index entries. EXPECT_TRUE(index_file_.get()); - EXPECT_EQ(1, index_file_->get_index_entries_calls()); + EXPECT_EQ(1, index_file_->load_index_entries_calls()); // Confirm "Has()" always returns true before the callback is called. EXPECT_TRUE(index()->Has(kKey1Hash)); @@ -255,7 +253,7 @@ TEST_F(SimpleIndexTest, Has) { TEST_F(SimpleIndexTest, UseIfExists) { // Confirm the base index has dispatched the request for index entries. EXPECT_TRUE(index_file_.get()); - EXPECT_EQ(1, index_file_->get_index_entries_calls()); + EXPECT_EQ(1, index_file_->load_index_entries_calls()); // Confirm "UseIfExists()" always returns true before the callback is called // and updates mod time if the entry was really there. |