Bug 722033 - Invalidate cache entry in nsHttpChannel::DoInvalidateCacheEntry() asynchronously

This commit is contained in:
Michal Novotny 2012-03-22 23:54:20 +01:00
Родитель 2df9700ac8
Коммит c8537395fd
10 изменённых файлов: 320 добавлений и 15 удалений

101
netwerk/cache/nsCacheService.cpp поставляемый
Просмотреть файл

@ -1034,6 +1034,91 @@ private:
nsCacheRequest *mRequest;
};
/******************************************************************************
* nsNotifyDoomListener
*****************************************************************************/
class nsNotifyDoomListener : public nsRunnable {
public:
nsNotifyDoomListener(nsICacheListener *listener,
nsresult status)
: mListener(listener) // transfers reference
, mStatus(status)
{}
NS_IMETHOD Run()
{
mListener->OnCacheEntryDoomed(mStatus);
NS_RELEASE(mListener);
return NS_OK;
}
private:
nsICacheListener *mListener;
nsresult mStatus;
};
/******************************************************************************
* nsDoomEvent
*****************************************************************************/
class nsDoomEvent : public nsRunnable {
public:
nsDoomEvent(nsCacheSession *session,
const nsACString &key,
nsICacheListener *listener)
{
mKey = *session->ClientID();
mKey.Append(':');
mKey.Append(key);
mStoragePolicy = session->StoragePolicy();
mListener = listener;
mThread = do_GetCurrentThread();
// We addref the listener here and release it in nsNotifyDoomListener
// on the callers thread. If posting of nsNotifyDoomListener event fails
// we leak the listener which is better than releasing it on a wrong
// thread.
NS_IF_ADDREF(mListener);
}
NS_IMETHOD Run()
{
nsCacheServiceAutoLock lock;
bool foundActive = true;
nsresult status = NS_ERROR_NOT_AVAILABLE;
nsCacheEntry *entry;
entry = nsCacheService::gService->mActiveEntries.GetEntry(&mKey);
if (!entry) {
bool collision = false;
foundActive = false;
entry = nsCacheService::gService->SearchCacheDevices(&mKey,
mStoragePolicy,
&collision);
}
if (entry) {
status = NS_OK;
nsCacheService::gService->DoomEntry_Internal(entry, foundActive);
}
if (mListener) {
mThread->Dispatch(new nsNotifyDoomListener(mListener, status),
NS_DISPATCH_NORMAL);
// posted event will release the reference on the correct thread
mListener = nsnull;
}
return NS_OK;
}
private:
nsCString mKey;
nsCacheStoragePolicy mStoragePolicy;
nsICacheListener *mListener;
nsCOMPtr<nsIThread> mThread;
};
/******************************************************************************
* nsCacheService
*****************************************************************************/
@ -1350,6 +1435,22 @@ nsCacheService::IsStorageEnabledForPolicy(nsCacheStoragePolicy storagePolicy,
}
nsresult
nsCacheService::DoomEntry(nsCacheSession *session,
const nsACString &key,
nsICacheListener *listener)
{
CACHE_LOG_DEBUG(("Dooming entry for session %p, key %s\n",
session, PromiseFlatCString(key).get()));
NS_ASSERTION(gService, "nsCacheService::gService is null.");
if (!gService->mInitialized)
return NS_ERROR_NOT_INITIALIZED;
return DispatchToCacheIOThread(new nsDoomEvent(session, key, listener));
}
bool
nsCacheService::IsStorageEnabledForPolicy_Locked(nsCacheStoragePolicy storagePolicy)
{

5
netwerk/cache/nsCacheService.h поставляемый
Просмотреть файл

@ -99,6 +99,10 @@ public:
static nsresult IsStorageEnabledForPolicy(nsCacheStoragePolicy storagePolicy,
bool * result);
static nsresult DoomEntry(nsCacheSession *session,
const nsACString &key,
nsICacheListener *listener);
/**
* Methods called by nsCacheEntryDescriptor
*/
@ -198,6 +202,7 @@ private:
friend class nsSetSmartSizeEvent;
friend class nsBlockOnCacheThreadEvent;
friend class nsSetDiskSmartSizeCallback;
friend class nsDoomEvent;
/**
* Internal Methods

6
netwerk/cache/nsCacheSession.cpp поставляемый
Просмотреть файл

@ -128,3 +128,9 @@ NS_IMETHODIMP nsCacheSession::IsStorageEnabled(bool *result)
return nsCacheService::IsStorageEnabledForPolicy(StoragePolicy(), result);
}
NS_IMETHODIMP nsCacheSession::DoomEntry(const nsACString &key,
nsICacheListener *listener)
{
return nsCacheService::DoomEntry(this, key, listener);
}

9
netwerk/cache/nsICacheListener.idl поставляемый
Просмотреть файл

@ -47,7 +47,7 @@
interface nsICacheEntryDescriptor;
[scriptable, uuid(638c3848-778b-4851-8ff3-9400f65b8773)]
[scriptable, uuid(8eadf2ed-8cac-4961-8025-6da6d5827e74)]
interface nsICacheListener : nsISupports
{
/**
@ -58,4 +58,11 @@ interface nsICacheListener : nsISupports
void onCacheEntryAvailable(in nsICacheEntryDescriptor descriptor,
in nsCacheAccessMode accessGranted,
in nsresult status);
/**
* Called when nsCacheSession::DoomEntry() is completed. The status
* parameter is NS_OK when the entry was doomed, or NS_ERROR_NOT_AVAILABLE
* when there is no such entry.
*/
void onCacheEntryDoomed(in nsresult status);
};

9
netwerk/cache/nsICacheSession.idl поставляемый
Просмотреть файл

@ -46,7 +46,7 @@
interface nsICacheEntryDescriptor;
interface nsICacheListener;
[scriptable, uuid(2ec1026f-e3a5-481c-908e-8d559235b721)]
[scriptable, uuid(1dd7708c-de48-4ffe-b5aa-cd218c762887)]
interface nsICacheSession : nsISupports
{
/**
@ -98,4 +98,11 @@ interface nsICacheSession : nsISupports
* are currently enabled for instantiation if they don't already exist.
*/
boolean isStorageEnabled();
/**
* Asynchronously doom an entry specified by the key. Listener will be
* notified about the status of the operation. Null may be passed if caller
* doesn't care about the result.
*/
void doomEntry(in ACString key, in nsICacheListener listener);
};

Просмотреть файл

@ -2095,6 +2095,14 @@ nsFtpState::OnCacheEntryAvailable(nsICacheEntryDescriptor *entry,
//-----------------------------------------------------------------------------
NS_IMETHODIMP
nsFtpState::OnCacheEntryDoomed(nsresult status)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
//-----------------------------------------------------------------------------
NS_IMETHODIMP
nsFtpState::OnStartRequest(nsIRequest *request, nsISupports *context)
{

Просмотреть файл

@ -4891,6 +4891,12 @@ nsHttpChannel::OnCacheEntryAvailableInternal(nsICacheEntryDescriptor *entry,
return Connect(false);
}
NS_IMETHODIMP
nsHttpChannel::OnCacheEntryDoomed(nsresult status)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
nsresult
nsHttpChannel::DoAuthRetry(nsAHttpConnection *conn)
{
@ -5209,19 +5215,7 @@ nsHttpChannel::DoInvalidateCacheEntry(nsACString &key)
if (NS_FAILED(rv))
return;
// Now, find the actual cache-entry
nsCOMPtr<nsICacheEntryDescriptor> tmpCacheEntry;
rv = session->OpenCacheEntry(key, nsICache::ACCESS_READ,
false,
getter_AddRefs(tmpCacheEntry));
// If entry was found, set its expiration-time = 0
if(NS_SUCCEEDED(rv)) {
tmpCacheEntry->SetExpirationTime(0);
LOG((" cache-entry invalidated [key=%s]\n", key.Data()));
} else {
LOG((" cache-entry not found [key=%s]\n", key.Data()));
}
session->DoomEntry(key, nsnull);
}
nsCacheStoragePolicy

Просмотреть файл

@ -612,6 +612,12 @@ nsWyciwygChannel::OnCacheEntryAvailable(nsICacheEntryDescriptor * aCacheEntry, n
return NS_OK;
}
NS_IMETHODIMP
nsWyciwygChannel::OnCacheEntryDoomed(nsresult status)
{
return NS_ERROR_NOT_IMPLEMENTED;
}
//-----------------------------------------------------------------------------
// nsWyciwygChannel::nsIStreamListener
//-----------------------------------------------------------------------------

Просмотреть файл

@ -0,0 +1,170 @@
/**
* Test for nsICacheSession.doomEntry().
* It tests dooming
* - an existent inactive entry
* - a non-existent inactive entry
* - an existent active entry
*/
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
var _CSvc;
function get_cache_service() {
if (_CSvc)
return _CSvc;
return _CSvc = Cc["@mozilla.org/network/cache-service;1"].
getService(Ci.nsICacheService);
}
function GetOutputStreamForEntry(key, asFile, append, callback)
{
this._key = key;
this._asFile = asFile;
this._append = append;
this._callback = callback;
this.run();
}
GetOutputStreamForEntry.prototype = {
_key: "",
_asFile: false,
_append: false,
_callback: null,
QueryInterface: function(iid) {
if (iid.equals(Ci.nsICacheListener) ||
iid.equals(Ci.nsISupports))
return this;
throw Cr.NS_ERROR_NO_INTERFACE;
},
onCacheEntryAvailable: function (entry, access, status) {
if (!entry)
do_throw("entry not available");
var ostream = entry.openOutputStream(this._append ? entry.dataSize : 0);
this._callback(entry, ostream);
},
run: function() {
var cache = get_cache_service();
var session = cache.createSession(
"HTTP",
this._asFile ? Ci.nsICache.STORE_ON_DISK_AS_FILE
: Ci.nsICache.STORE_ON_DISK,
Ci.nsICache.STREAM_BASED);
var cacheEntry = session.asyncOpenCacheEntry(
this._key,
this._append ? Ci.nsICache.ACCESS_READ_WRITE
: Ci.nsICache.ACCESS_WRITE,
this);
}
};
function DoomEntry(key, callback) {
this._key = key;
this._callback = callback;
this.run();
}
DoomEntry.prototype = {
_key: "",
_callback: null,
QueryInterface: function(iid) {
if (iid.equals(Ci.nsICacheListener) ||
iid.equals(Ci.nsISupports))
return this;
throw Cr.NS_ERROR_NO_INTERFACE;
},
onCacheEntryDoomed: function (status) {
this._callback(status);
},
run: function() {
get_cache_service()
.createSession("HTTP",
Ci.nsICache.STORE_ANYWHERE,
Ci.nsICache.STREAM_BASED)
.doomEntry(this._key, this);
}
};
function write_and_check(str, data, len)
{
var written = str.write(data, len);
if (written != len) {
do_throw("str.write has not written all data!\n" +
" Expected: " + len + "\n" +
" Actual: " + written + "\n");
}
}
function write_entry()
{
new GetOutputStreamForEntry("testentry", true, false, write_entry_cont);
}
function write_entry_cont(entry, ostream)
{
var data = "testdata";
write_and_check(ostream, data, data.length);
ostream.close();
entry.close();
new DoomEntry("testentry", check_doom1);
}
function check_doom1(status)
{
do_check_eq(status, Cr.NS_OK);
new DoomEntry("nonexistententry", check_doom2);
}
function check_doom2(status)
{
do_check_eq(status, Cr.NS_ERROR_NOT_AVAILABLE);
new GetOutputStreamForEntry("testentry", true, false, write_entry2);
}
var gEntry;
var gOstream;
function write_entry2(entry, ostream)
{
// write some data and doom the entry while it is active
var data = "testdata";
write_and_check(ostream, data, data.length);
gEntry = entry;
gOstream = ostream;
new DoomEntry("testentry", check_doom3);
}
function check_doom3(status)
{
do_check_eq(status, Cr.NS_OK);
// entry was doomed but writing should still succeed
var data = "testdata";
write_and_check(gOstream, data, data.length);
gEntry.close();
gOstream.close();
// dooming the same entry again should fail
new DoomEntry("testentry", check_doom4);
}
function check_doom4(status)
{
do_check_eq(status, Cr.NS_ERROR_NOT_AVAILABLE);
do_test_finished();
}
function run_test() {
do_get_profile();
// clear the cache
get_cache_service().evictEntries(Ci.nsICache.STORE_ANYWHERE);
write_entry();
do_test_pending();
}

Просмотреть файл

@ -83,6 +83,7 @@ fail-if = os == "android"
[test_bug667907.js]
[test_bug667818.js]
[test_bug669001.js]
[test_doomentry.js]
[test_cacheflags.js]
[test_channel_close.js]
[test_compareURIs.js]