From ba0ee1543e26f4fb059a1dfb474158d3c4ea1f38 Mon Sep 17 00:00:00 2001 From: Honza Bambas Date: Tue, 16 Sep 2014 15:51:50 +0200 Subject: [PATCH] Bug 1029782 - Have OPEN_SECRETLY flag for opening cache entries, r=michal --- netwerk/cache2/CacheEntry.cpp | 15 ++++++-- netwerk/cache2/CacheEntry.h | 3 +- netwerk/cache2/nsICacheStorage.idl | 5 +++ .../unit/test_cache2-28a-OPEN_SECRETLY.js | 34 +++++++++++++++++++ netwerk/test/unit/xpcshell.ini | 3 ++ 5 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 netwerk/test/unit/test_cache2-28a-OPEN_SECRETLY.js diff --git a/netwerk/cache2/CacheEntry.cpp b/netwerk/cache2/CacheEntry.cpp index 9aacd15b1fa5..a8aec185c035 100644 --- a/netwerk/cache2/CacheEntry.cpp +++ b/netwerk/cache2/CacheEntry.cpp @@ -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()); } diff --git a/netwerk/cache2/CacheEntry.h b/netwerk/cache2/CacheEntry.h index f24d0eec259a..2c9e6ca8f5c9 100644 --- a/netwerk/cache2/CacheEntry.h +++ b/netwerk/cache2/CacheEntry.h @@ -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; diff --git a/netwerk/cache2/nsICacheStorage.idl b/netwerk/cache2/nsICacheStorage.idl index 991d050a61bd..49812a590daa 100644 --- a/netwerk/cache2/nsICacheStorage.idl +++ b/netwerk/cache2/nsICacheStorage.idl @@ -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. diff --git a/netwerk/test/unit/test_cache2-28a-OPEN_SECRETLY.js b/netwerk/test/unit/test_cache2-28a-OPEN_SECRETLY.js new file mode 100644 index 000000000000..fdc66e10fb99 --- /dev/null +++ b/netwerk/test/unit/test_cache2-28a-OPEN_SECRETLY.js @@ -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(); +} diff --git a/netwerk/test/unit/xpcshell.ini b/netwerk/test/unit/xpcshell.ini index ddbfd6bf907f..cf10ad75ab63 100644 --- a/netwerk/test/unit/xpcshell.ini +++ b/netwerk/test/unit/xpcshell.ini @@ -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]