Bug 1029782 - Have OPEN_SECRETLY flag for opening cache entries, r=michal

This commit is contained in:
Honza Bambas 2014-09-16 15:51:50 +02:00
parent d7feb5510d
commit 9c6f58fc20
5 changed files with 56 additions and 4 deletions

View File

@ -76,7 +76,8 @@ CacheEntryHandle::~CacheEntryHandle()
CacheEntry::Callback::Callback(CacheEntry* aEntry,
nsICacheEntryOpenCallback *aCallback,
bool aReadOnly, bool aCheckOnAnyThread)
bool aReadOnly, bool aCheckOnAnyThread,
bool aSecret)
: mEntry(aEntry)
, mCallback(aCallback)
, mTargetThread(do_GetCurrentThread())
@ -84,6 +85,7 @@ CacheEntry::Callback::Callback(CacheEntry* aEntry,
, mCheckOnAnyThread(aCheckOnAnyThread)
, mRecheckAfterWrite(false)
, mNotWanted(false)
, mSecret(aSecret)
{
MOZ_COUNT_CTOR(CacheEntry::Callback);
@ -101,6 +103,7 @@ CacheEntry::Callback::Callback(CacheEntry::Callback const &aThat)
, mCheckOnAnyThread(aThat.mCheckOnAnyThread)
, mRecheckAfterWrite(aThat.mRecheckAfterWrite)
, mNotWanted(aThat.mNotWanted)
, mSecret(aThat.mSecret)
{
MOZ_COUNT_CTOR(CacheEntry::Callback);
@ -269,11 +272,12 @@ void CacheEntry::AsyncOpen(nsICacheEntryOpenCallback* aCallback, uint32_t aFlags
bool truncate = aFlags & nsICacheStorage::OPEN_TRUNCATE;
bool priority = aFlags & nsICacheStorage::OPEN_PRIORITY;
bool multithread = aFlags & nsICacheStorage::CHECK_MULTITHREADED;
bool secret = aFlags & nsICacheStorage::OPEN_SECRETLY;
MOZ_ASSERT(!readonly || !truncate, "Bad flags combination");
MOZ_ASSERT(!(truncate && mState > LOADING), "Must not call truncate on already loaded entry");
Callback callback(this, aCallback, readonly, multithread);
Callback callback(this, aCallback, readonly, multithread, secret);
if (!Open(callback, truncate, priority, bypassIfBusy)) {
// We get here when the callback wants to bypass cache when it's busy.
@ -759,7 +763,7 @@ void CacheEntry::InvokeAvailableCallback(Callback const & aCallback)
return;
}
if (NS_SUCCEEDED(mFileStatus)) {
if (NS_SUCCEEDED(mFileStatus) && !aCallback.mSecret) {
// Let the last-fetched and fetch-count properties be updated.
mFile->OnFetched();
}
@ -773,6 +777,8 @@ void CacheEntry::InvokeAvailableCallback(Callback const & aCallback)
if (state == READY) {
LOG((" ready/has-meta, notifying OCEA with entry and NS_OK"));
if (!aCallback.mSecret)
{
mozilla::MutexAutoLock lock(mLock);
BackgroundOp(Ops::FRECENCYUPDATE);
@ -822,7 +828,10 @@ CacheEntryHandle* CacheEntry::NewWriteHandle()
{
mozilla::MutexAutoLock lock(mLock);
// Ignore the OPEN_SECRETLY flag on purpose here, which should actually be
// used only along with OPEN_READONLY, but there is no need to enforce that.
BackgroundOp(Ops::FRECENCYUPDATE);
return (mWriter = NewHandle());
}

View File

@ -137,7 +137,7 @@ private:
public:
Callback(CacheEntry* aEntry,
nsICacheEntryOpenCallback *aCallback,
bool aReadOnly, bool aCheckOnAnyThread);
bool aReadOnly, bool aCheckOnAnyThread, bool aSecret);
Callback(Callback const &aThat);
~Callback();
@ -155,6 +155,7 @@ private:
bool mCheckOnAnyThread : 1;
bool mRecheckAfterWrite : 1;
bool mNotWanted : 1;
bool mSecret : 1;
nsresult OnCheckThread(bool *aOnCheckThread) const;
nsresult OnAvailThread(bool *aOnAvailThread) const;

View File

@ -51,6 +51,11 @@ interface nsICacheStorage : nsISupports
*/
const uint32_t CHECK_MULTITHREADED = 1 << 4;
/**
* Don't automatically update any 'last used' metadata of the entry.
*/
const uint32_t OPEN_SECRETLY = 1 << 5;
/**
* Asynchronously opens a cache entry for the specified URI.
* Result is fetched asynchronously via the callback.

View File

@ -0,0 +1,34 @@
function run_test()
{
do_get_profile();
function NowSeconds() {
return parseInt((new Date()).getTime() / 1000);
}
function do_check_time(a, b) {
do_check_true(Math.abs(a - b) < 0.5);
}
asyncOpenCacheEntry("http://t/", "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
new OpenCallback(NEW, "m", "d", function(entry) {
var now1 = NowSeconds();
do_check_eq(entry.fetchCount, 1);
do_check_time(entry.lastFetched, now1);
do_check_time(entry.lastModified, now1);
do_timeout(2000, () => {
asyncOpenCacheEntry("http://t/", "disk", Ci.nsICacheStorage.OPEN_SECRETLY, null,
new OpenCallback(NORMAL, "m", "d", function(entry) {
do_check_eq(entry.fetchCount, 1);
do_check_time(entry.lastFetched, now1);
do_check_time(entry.lastModified, now1);
finish_cache2_test();
})
);
})
})
);
do_test_pending();
}

View File

@ -62,6 +62,9 @@ skip-if = os == "android"
[test_cache2-28-last-access-attrs.js]
# This test will be fixed in bug 1067931
skip-if = true
[test_cache2-28a-OPEN_SECRETLY.js]
# This test will be fixed in bug 1067931
skip-if = true
[test_304_responses.js]
[test_cacheForOfflineUse_no-store.js]
[test_307_redirect.js]