Bug 1753471 - Only notify http-on-image-cache-response once per cache entry r=tnikkel

I've also tested the behavior in Chrome and I think this is
the correct way of doing it. Chrome also only notifies the
cached entry once, it won't notify it even if I manually
trigger it after pageload.

Differential Revision: https://phabricator.services.mozilla.com/D138346
This commit is contained in:
Sean Feng 2022-02-10 18:18:28 +00:00
Родитель 3a5e7ade87
Коммит 8e783e6bee
4 изменённых файлов: 62 добавлений и 1 удалений

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

@ -992,7 +992,8 @@ imgCacheEntry::imgCacheEntry(imgLoader* loader, imgRequest* request,
// PutIntoCache will set this to false.
mEvicted(true),
mHasNoProxies(true),
mForcePrincipalCheck(forcePrincipalCheck) {}
mForcePrincipalCheck(forcePrincipalCheck),
mHasNotified(false) {}
imgCacheEntry::~imgCacheEntry() {
LOG_FUNC(gImgLog, "imgCacheEntry::~imgCacheEntry()");
@ -1901,6 +1902,11 @@ void imgLoader::NotifyObserversForCachedImage(
imgCacheEntry* aEntry, imgRequest* request, nsIURI* aURI,
nsIReferrerInfo* aReferrerInfo, Document* aLoadingDocument,
nsIPrincipal* aTriggeringPrincipal, CORSMode aCORSMode) {
if (aEntry->HasNotified()) {
return;
}
aEntry->SetHasNotified();
nsCOMPtr<nsIChannel> newChannel;
bool forcePrincipalCheck;
nsresult rv =

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

@ -108,6 +108,12 @@ class imgCacheEntry {
bool ForcePrincipalCheck() const { return mForcePrincipalCheck; }
bool HasNotified() const { return mHasNotified; }
void SetHasNotified() {
MOZ_ASSERT(!mHasNotified);
mHasNotified = true;
}
imgLoader* Loader() const { return mLoader; }
private: // methods
@ -136,6 +142,7 @@ class imgCacheEntry {
bool mEvicted : 1;
bool mHasNoProxies : 1;
bool mForcePrincipalCheck : 1;
bool mHasNotified : 1;
};
#include <vector>

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

@ -165,3 +165,4 @@ disabled = bug 1295501
[test_undisplayed_iframe.html]
[test_webcam.html]
[test_xultree_animation.xhtml]
[test_image_cache_notification.html]

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

@ -0,0 +1,47 @@
<!DOCTYPE HTML>
<html>
<head>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<button>Add Image</button>
<script>
/* Test to ensure http-on-image-cache-response should only be notified
* once per image
*/
SimpleTest.waitForExplicitFinish();
async function addImage() {
const newImage = document.createElement("img");
const imageLoaded = new Promise((r) => {
newImage.onload = r;
});
newImage.src = "./over.png";
document.body.appendChild(newImage);
return imageLoaded;
}
let imageCacheCallbackRunCount = 0;
const cb = SpecialPowers.wrapCallback(() => {
imageCacheCallbackRunCount += 1;
});
SpecialPowers.addObserver(cb, "http-on-image-cache-response");
async function runTest() {
await addImage();
SimpleTest.ok(imageCacheCallbackRunCount == 0, "first load of over.png shouldn't be cached");
await addImage();
SimpleTest.ok(imageCacheCallbackRunCount == 1, "second load of over.png should be cached");
await addImage();
await addImage();
await addImage();
SimpleTest.ok(imageCacheCallbackRunCount == 1, "further loads of over.png shouldn't be notified");
SimpleTest.finish();
}
runTest();
</script>
</body>
</html>