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/http/http_cache_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/http/http_cache_unittest.cc')
-rw-r--r-- | net/http/http_cache_unittest.cc | 99 |
1 files changed, 97 insertions, 2 deletions
diff --git a/net/http/http_cache_unittest.cc b/net/http/http_cache_unittest.cc index b52f2a4..5db229b 100644 --- a/net/http/http_cache_unittest.cc +++ b/net/http/http_cache_unittest.cc @@ -6,6 +6,7 @@ #include "base/hash_tables.h" #include "base/message_loop.h" +#include "base/platform_file.h" #include "base/string_util.h" #include "net/base/net_errors.h" #include "net/base/load_flags.h" @@ -26,10 +27,12 @@ namespace { class MockDiskEntry : public disk_cache::Entry, public base::RefCounted<MockDiskEntry> { public: - MockDiskEntry() : test_mode_(0), doomed_(false) { + MockDiskEntry() + : test_mode_(0), doomed_(false), platform_file_(global_platform_file_) { } - MockDiskEntry(const std::string& key) : key_(key), doomed_(false) { + MockDiskEntry(const std::string& key) + : key_(key), doomed_(false), platform_file_(global_platform_file_) { const MockTransaction* t = FindMockTransaction(GURL(key)); DCHECK(t); test_mode_ = t->test_mode; @@ -98,6 +101,18 @@ class MockDiskEntry : public disk_cache::Entry, return buf_len; } + base::PlatformFile UseExternalFile(int index) { + return platform_file_; + } + + base::PlatformFile GetPlatformFile(int index) { + return platform_file_; + } + + static void set_global_platform_file(base::PlatformFile platform_file) { + global_platform_file_ = platform_file; + } + private: // Unlike the callbacks for MockHttpTransaction, we want this one to run even // if the consumer called Close on the MockDiskEntry. We achieve that by @@ -114,8 +129,13 @@ class MockDiskEntry : public disk_cache::Entry, std::vector<char> data_[2]; int test_mode_; bool doomed_; + base::PlatformFile platform_file_; + static base::PlatformFile global_platform_file_; }; +base::PlatformFile MockDiskEntry::global_platform_file_ = + base::kInvalidPlatformFileValue; + class MockDiskCache : public disk_cache::Backend { public: MockDiskCache() : open_count_(0), create_count_(0), fail_requests_(0) { @@ -1184,3 +1204,78 @@ TEST(HttpCache, OutlivedTransactions) { delete cache; delete trans; } + +// Make sure Entry::UseExternalFile is called when a new entry is created in +// a HttpCache with MEDIA type. Also make sure Entry::GetPlatformFile is called +// when an entry is loaded from a HttpCache with MEDIA type. Also confirm we +// will receive a file handle in ResponseInfo from a media cache. +TEST(HttpCache, SimpleGET_MediaCache) { + // Initialize the HttpCache with MEDIA type. + MockHttpCache cache; + cache.http_cache()->set_type(net::HttpCache::MEDIA); + + // Define some fake file handles for testing. + base::PlatformFile kFakePlatformFile1, kFakePlatformFile2; +#if defined(OS_WIN) + kFakePlatformFile1 = reinterpret_cast<base::PlatformFile>(1); + kFakePlatformFile2 = reinterpret_cast<base::PlatformFile>(2); +#else + kFakePlatformFile1 = 1; + kFakePlatformFile2 = 2; +#endif + + ScopedMockTransaction trans_info(kSimpleGET_Transaction); + TestCompletionCallback callback; + + { + // Set the fake file handle to MockDiskEntry so cache is written with an + // entry created with our fake file handle. + MockDiskEntry::set_global_platform_file(kFakePlatformFile1); + + scoped_ptr<net::HttpTransaction> trans( + cache.http_cache()->CreateTransaction()); + ASSERT_TRUE(trans.get()); + + MockHttpRequest request(trans_info); + + int rv = trans->Start(&request, &callback); + if (rv == net::ERR_IO_PENDING) + rv = callback.WaitForResult(); + ASSERT_EQ(net::OK, rv); + + const net::HttpResponseInfo* response = trans->GetResponseInfo(); + ASSERT_TRUE(response); + + ASSERT_EQ(kFakePlatformFile1, response->response_data_file); + + ReadAndVerifyTransaction(trans.get(), trans_info); + } + + // Load only from cache so we would get the same file handle. + trans_info.load_flags |= net::LOAD_ONLY_FROM_CACHE; + + { + // Set a different file handle value to MockDiskEntry so any new entry + // created in the cache won't have the same file handle value. + MockDiskEntry::set_global_platform_file(kFakePlatformFile2); + + scoped_ptr<net::HttpTransaction> trans( + cache.http_cache()->CreateTransaction()); + ASSERT_TRUE(trans.get()); + + MockHttpRequest request(trans_info); + + int rv = trans->Start(&request, &callback); + if (rv == net::ERR_IO_PENDING) + rv = callback.WaitForResult(); + ASSERT_EQ(net::OK, rv); + + const net::HttpResponseInfo* response = trans->GetResponseInfo(); + ASSERT_TRUE(response); + + // Make sure we get the same file handle as in the first request. + ASSERT_EQ(kFakePlatformFile1, response->response_data_file); + + ReadAndVerifyTransaction(trans.get(), trans_info); + } +} |