Bug 1268889 - Implement Clear-Site-Data header - part 4 - cleanup image cache, r=aosmond

This commit is contained in:
Andrea Marchesini 2018-06-20 11:57:50 -04:00
Родитель 1f52a1bed1
Коммит 1e49c45c00
4 изменённых файлов: 69 добавлений и 1 удалений

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

@ -41,9 +41,11 @@ public:
bool operator==(const ImageCacheKey& aOther) const;
PLDHashNumber Hash() const { return mHash; }
/// A weak pointer to the URI. For logging only.
/// A weak pointer to the URI.
nsIURI* URI() const { return mURI; }
const OriginAttributes& OriginAttributesRef() const { return mOriginAttributes; }
/// Is this cache entry for a chrome image?
bool IsChrome() const { return mIsChrome; }

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

@ -8,11 +8,20 @@
interface imgIRequest;
interface nsIDocument;
interface nsIPrincipal;
interface nsIProperties;
interface nsIURI;
webidl Document;
%{ C++
namespace mozilla {
class OriginAttributes;
} // mozilla namespace
%}
[ptr] native OriginAttributesPtr(mozilla::OriginAttributes);
/**
* imgICache interface
*
@ -41,6 +50,14 @@ interface imgICache : nsISupports
*/
[noscript] void removeEntry(in nsIURI uri, [optional] in Document doc);
/**
* Evict images from the cache with the same origin and the same
* originAttributes of the passed principal.
*
* @param aPrincipal The principal
*/
void removeEntriesFromPrincipal(in nsIPrincipal aPrincipal);
/**
* Find Properties
* Used to get properties such as 'type' and 'content-disposition'

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

@ -1469,6 +1469,45 @@ imgLoader::ClearCache(bool chrome)
}
NS_IMETHODIMP
imgLoader::RemoveEntriesFromPrincipal(nsIPrincipal* aPrincipal)
{
nsAutoString origin;
nsresult rv = nsContentUtils::GetUTFOrigin(aPrincipal, origin);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
AutoTArray<RefPtr<imgCacheEntry>, 128> entriesToBeRemoved;
imgCacheTable& cache = GetCache(nsContentUtils::IsSystemPrincipal(aPrincipal));
for (auto iter = cache.Iter(); !iter.Done(); iter.Next()) {
auto& key = iter.Key();
if (key.OriginAttributesRef() != BasePrincipal::Cast(aPrincipal)->OriginAttributesRef()) {
continue;
}
nsAutoString imageOrigin;
nsresult rv = nsContentUtils::GetUTFOrigin(key.URI(), imageOrigin);
if (NS_WARN_IF(NS_FAILED(rv))) {
continue;
}
if (imageOrigin == origin) {
entriesToBeRemoved.AppendElement(iter.Data());
}
}
for (auto& entry : entriesToBeRemoved) {
if (!RemoveFromCache(entry)) {
NS_WARNING("Couldn't remove an entry from the cache in RemoveEntriesFromPrincipal()\n");
}
}
return NS_OK;
}
NS_IMETHODIMP
imgLoader::RemoveEntry(nsIURI* aURI,
nsIDocument* aDoc)

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

@ -95,6 +95,16 @@ const NetworkCacheCleaner = {
};
const ImageCacheCleaner = {
deleteByPrincipal(aPrincipal) {
return new Promise(aResolve => {
let imageCache = Cc["@mozilla.org/image/tools;1"]
.getService(Ci.imgITools)
.getImgCacheForDocument(null);
imageCache.removeEntriesFromPrincipal(aPrincipal);
aResolve();
});
},
deleteAll() {
return new Promise(aResolve => {
let imageCache = Cc["@mozilla.org/image/tools;1"]