diff options
author | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-02 22:53:18 +0000 |
---|---|---|
committer | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-02 22:53:18 +0000 |
commit | e7f2964e84ca06d711d33723e7725d03ee2aa136 (patch) | |
tree | e2cc8f033863a972998fdd668dd698d222bea3aa /net/disk_cache/entry_unittest.cc | |
parent | e9e6b1c6d3870e9a1e9a8aa7e6c454cdcd354d0f (diff) | |
download | chromium_src-e7f2964e84ca06d711d33723e7725d03ee2aa136.zip chromium_src-e7f2964e84ca06d711d33723e7725d03ee2aa136.tar.gz chromium_src-e7f2964e84ca06d711d33723e7725d03ee2aa136.tar.bz2 |
Proposed change to support resource loading for media files.
Highlights of changes:
- Added methods to disk_cache::Entry:
- Entry::PrepareTargetAsExternalFile(int index)
Prepare a stream in an entry to use external file for storage.
- Entry::GetExternalFile(int index)
Get the external file backing the stream in the entry.
- Added a property "CacheType type_" to HttpCache, along with setter and getter.
There shall be two cache types, COMMON_CACHE and MEDIA_CACHE for distinguishing between different purpose of HttpCache. We have this property to trigger special behavior for caching needs of media files.
- Added static methods to ChromeURLRequestContext
- ChromeURLRequestContext::CreateOriginalForMedia
Create a URLRequestContext for media files for the original profile.
- ChromeURLRequestContext::CreateOffTheRecordForMedia
Create a URLRequestContext for media files for off the record profile.
- Added method to Profile interface.
- GetRequestContextForMedia
To get the request context for media files from the context.
Design decissions:
- Enforce writing to external file by calling methods to Entry rather than construct Backend by a different flag.
Since we only want a valid and full response to go into an external file rather than redirection response or erroneous response, we should let HttpCache::Transaction to decide when to have an external file for response data. We eliminate a lot of useless external cache files.
- Adding the CacheType enum and property to HttpCache, we could allow possible (?) future extensions to HttpCache to handle other different caching needs. And there's no need to add change constructors of HttpCache, but maybe we should add a specific constructor to accomodate a media HttpCache?
- Adding Profile::GetRequestContextForMedia()
Since we will need to use this new request context in ResourceDispatcherHost, I think the best place to keep it is in the profile. Also we will expose to user that there's a separate cache for media, so it's better to expose it in the Profile level to allow settings to the media cache, e.g. max file size, etc.
Review URL: http://codereview.chromium.org/19747
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10745 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/disk_cache/entry_unittest.cc')
-rw-r--r-- | net/disk_cache/entry_unittest.cc | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/net/disk_cache/entry_unittest.cc b/net/disk_cache/entry_unittest.cc index 47b70d0..bb7cc2e 100644 --- a/net/disk_cache/entry_unittest.cc +++ b/net/disk_cache/entry_unittest.cc @@ -833,3 +833,101 @@ TEST_F(DiskCacheEntryTest, MemoryOnlyDoomedEntry) { DoomEntry(); } +// Check that we can hint an entry to use external file and the return value +// is a valid file handle. +TEST_F(DiskCacheEntryTest, UseExternalFile) { + InitCache(); + + disk_cache::Entry* entry; + ASSERT_TRUE(cache_->CreateEntry("key", &entry)); + base::PlatformFile cache_file = entry->UseExternalFile(0); + + // We should have a valid file handle. + EXPECT_NE(base::kInvalidPlatformFileValue, cache_file); + scoped_refptr<disk_cache::File> file(new disk_cache::File(cache_file)); + + // 4KB. + size_t kDataSize = 0x1000; + scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kDataSize); + + CacheTestFillBuffer(buffer->data(), kDataSize, false); + ASSERT_EQ(0U, file->GetLength()); + ASSERT_EQ(kDataSize, static_cast<size_t>( + entry->WriteData(0, 0, buffer, kDataSize, NULL, false))); + ASSERT_EQ(kDataSize, file->GetLength()); + entry->Close(); +} + +// Make sure we can use Entry::GetPlatformFile on an entry stored in an external +// file and get a valid file handle. +TEST_F(DiskCacheEntryTest, GetPlatformFile) { + InitCache(); + + disk_cache::Entry* entry; + ASSERT_TRUE(cache_->CreateEntry("key", &entry)); + EXPECT_NE(base::kInvalidPlatformFileValue, entry->UseExternalFile(0)); + + size_t kDataSize = 50; + scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kDataSize); + + // Fill the data buffer and write it to cache. + CacheTestFillBuffer(buffer->data(), kDataSize, false); + ASSERT_EQ(kDataSize,static_cast<size_t>( + entry->WriteData(0, 0, buffer, kDataSize, NULL, false))); + + // Close the entry. + entry->Close(); + + // Open the entry again and get it's file handle. + ASSERT_TRUE(cache_->OpenEntry("key", &entry)); + base::PlatformFile cache_file = entry->GetPlatformFile(0); + + // Make sure it's a valid file handle and verify the size of the file. + scoped_refptr<disk_cache::File> file(new disk_cache::File(cache_file)); + ASSERT_EQ(kDataSize, file->GetLength()); + + entry->Close(); +} + +// Test the behavior of EntryImpl that small entries are kept in block files +// or buffer, and only entries above certain size would be stored in an +// external file, make sure GetPlatformFile() works with both cases without +// using UseExternalFile(). +TEST_F(DiskCacheEntryTest, GetPlatformFileVariableEntrySize) { + InitCache(); + + disk_cache::Entry* entry; + + // Make the buffer just larger than disk_cache::kMaxBlockSize. + const size_t kLargeDataSize = disk_cache::kMaxBlockSize + 1; + const size_t kSmallDataSize = kLargeDataSize / 2; + scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(kLargeDataSize); + + // 1. First test with small entry. + ASSERT_TRUE(cache_->CreateEntry("small_entry", &entry)); + + CacheTestFillBuffer(buffer->data(), kSmallDataSize, false); + ASSERT_EQ(kSmallDataSize, static_cast<size_t>( + entry->WriteData(0, 0, buffer, kSmallDataSize, NULL, false))); + + // Make sure we don't get an external file. + ASSERT_EQ(base::kInvalidPlatformFileValue, entry->GetPlatformFile(0)); + + entry->Close(); + + // 2. Test with large entry. + ASSERT_TRUE(cache_->CreateEntry("large_entry", &entry)); + + CacheTestFillBuffer(buffer->data(), kLargeDataSize, false); + ASSERT_EQ(kLargeDataSize, static_cast<size_t>( + entry->WriteData(0, 0, buffer, kLargeDataSize, NULL, false))); + + base::PlatformFile cache_file = entry->GetPlatformFile(0); + EXPECT_NE(base::kInvalidPlatformFileValue, cache_file); + + // Make sure it's a valid file handle and verify the size of the file. + scoped_refptr<disk_cache::File> file(new disk_cache::File(cache_file)); + ASSERT_EQ(kLargeDataSize, file->GetLength()); + + entry->Close(); +} |