Bug 1202085 - Part 6: Clear the entries in the image cache belonging to a controlled document when it gets destroyed; r=seth

This commit is contained in:
Ehsan Akhgari 2015-10-28 16:01:07 -04:00
parent 3a539c9718
commit e5b08c2286
4 changed files with 40 additions and 3 deletions

View File

@ -70,7 +70,8 @@
#include "mozilla/dom/TreeWalker.h" #include "mozilla/dom/TreeWalker.h"
#include "nsIServiceManager.h" #include "nsIServiceManager.h"
#include "nsIServiceWorkerManager.h" #include "mozilla/dom/workers/ServiceWorkerManager.h"
#include "imgLoader.h"
#include "nsCanvasFrame.h" #include "nsCanvasFrame.h"
#include "nsContentCID.h" #include "nsContentCID.h"
@ -8893,8 +8894,13 @@ nsDocument::Destroy()
mRegistry = nullptr; mRegistry = nullptr;
nsCOMPtr<nsIServiceWorkerManager> swm = mozilla::services::GetServiceWorkerManager(); using mozilla::dom::workers::ServiceWorkerManager;
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
if (swm) { if (swm) {
ErrorResult error;
if (swm->IsControlled(this, error)) {
nsContentUtils::GetImgLoaderForDocument(this)->ClearCacheForControlledDocument(this);
}
swm->MaybeStopControlling(this); swm->MaybeStopControlling(this);
} }

View File

@ -46,6 +46,10 @@ public:
/// Is this cache entry for a chrome image? /// Is this cache entry for a chrome image?
bool IsChrome() const { return mIsChrome; } bool IsChrome() const { return mIsChrome; }
/// A token indicating which service worker controlled document this entry
/// belongs to, if any.
void* ControlledDocument() const { return mControlledDocument; }
private: private:
static uint32_t ComputeHash(ImageURL* aURI, static uint32_t ComputeHash(ImageURL* aURI,
const Maybe<uint64_t>& aBlobSerial, const Maybe<uint64_t>& aBlobSerial,

View File

@ -7,6 +7,7 @@
#include "nsISupports.idl" #include "nsISupports.idl"
interface imgIRequest; interface imgIRequest;
interface nsIDocument;
interface nsIDOMDocument; interface nsIDOMDocument;
interface nsIProperties; interface nsIProperties;
interface nsIURI; interface nsIURI;
@ -18,7 +19,7 @@ interface nsIURI;
* @version 0.1 * @version 0.1
* @see imagelib2 * @see imagelib2
*/ */
[scriptable, builtinclass, uuid(6c1e3f96-4701-4c75-9883-217a790a53e2)] [scriptable, builtinclass, uuid(bfdf23ff-378e-402e-8a6c-840f0c82b6c3)]
interface imgICache : nsISupports interface imgICache : nsISupports
{ {
/** /**
@ -52,4 +53,11 @@ interface imgICache : nsISupports
* last-pb-context-exited notification is observed. * last-pb-context-exited notification is observed.
*/ */
void respectPrivacyNotifications(); void respectPrivacyNotifications();
/**
* Clear the image cache for a document. Controlled documents are responsible
* for doing this manually when they get destroyed.
*/
[noscript, notxpcom]
void clearCacheForControlledDocument(in nsIDocument doc);
}; };

View File

@ -1361,6 +1361,25 @@ imgLoader::FindEntryProperties(nsIURI* uri,
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP_(void)
imgLoader::ClearCacheForControlledDocument(nsIDocument* aDoc)
{
MOZ_ASSERT(aDoc);
nsAutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
imgCacheTable& cache = GetCache(false);
for (auto iter = cache.Iter(); !iter.Done(); iter.Next()) {
auto& key = iter.Key();
if (key.ControlledDocument() == aDoc) {
entriesToBeRemoved.AppendElement(iter.Data());
}
}
for (auto& entry : entriesToBeRemoved) {
if (!RemoveFromCache(entry)) {
NS_WARNING("Couldn't remove an entry from the cache in ClearCacheForControlledDocument()\n");
}
}
}
void void
imgLoader::Shutdown() imgLoader::Shutdown()
{ {