gecko-dev/dom/base/test/browser_blocking_image.js
Emilio Cobos Álvarez 64ca3f2d61 Bug 1076583 - Make src attribute not load sync. r=smaug,extension-reviewers,devtools-reviewers,robwu,nchevobbe
This fixes src loads to be consistent with srcset/picture loads, modulo
the special synchronous case in the spec
(https://html.spec.whatwg.org/#update-the-image-data step 7), which
requires src loads to be sync if the image is available.

We now avoid triggering the load from the parser consistently for src /
srcset / picture, and unify the codepath with BindToTree. That should
avoid some useless task allocations.

Only the sync load code-path needs a script runner (mostly to deal with
anonymous content like the video poster <img> and such, but it also
helps not trigger sync loads at unexpected times like on adoption).

About the HTMLImageElement::Complete() getter change, we need to also
return false if there's an existing load task. That is the proposal in
https://github.com/whatwg/html/issues/4884, and prevents some failures
in the-img-element/{update-src-complete,img.complete}.html WPTs. It
technically changes our behavior on .srcset changes, but it makes it
consistent with .src changes and other browsers, so seems fine.

There are a couple regressions in CSP tests and the networkEvent stubs,
but these are really a pre-existing issue. What happens is that, since
the loads are now async, CSP can't figure out the script that triggered
the load anymore. I need to look if there's an easy way to propagate
that information in the image load tasks, but this is trivially
reproducible by changing these tests to use srcset rather than src.

The rest of the test changes are as expected: either new passes, or
expected test changes from this.

Differential Revision: https://phabricator.services.mozilla.com/D215519
2024-08-05 12:23:44 +00:00

182 lines
5.2 KiB
JavaScript

const TEST_URI =
getRootDirectory(gTestPath).replace(
"chrome://mochitests/content",
"https://example.com"
) + "file_blocking_image.html";
/**
* Loading an image from https:// should work.
*/
add_task(async function load_image_from_https_test() {
let tab = BrowserTestUtils.addTab(gBrowser, TEST_URI);
await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
gBrowser.selectedTab = tab;
await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
function imgListener(img) {
return new Promise((resolve, reject) => {
img.addEventListener("load", () => resolve());
img.addEventListener("error", () => reject());
});
}
let img = content.document.createElement("img");
img.src = "https://example.com/tests/image/test/mochitest/shaver.png";
content.document.body.appendChild(img);
try {
await imgListener(img);
Assert.ok(true);
} catch (e) {
Assert.ok(false);
}
});
gBrowser.removeTab(tab);
});
/**
* Loading an image from http:// should be rejected.
*/
add_task(async function load_image_from_http_test() {
let tab = BrowserTestUtils.addTab(gBrowser, TEST_URI);
await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
gBrowser.selectedTab = tab;
await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
function imgListener(img) {
return new Promise((resolve, reject) => {
img.addEventListener("load", () => reject());
img.addEventListener("error", () => resolve());
});
}
let img = content.document.createElement("img");
img.src = "http://example.com/tests/image/test/mochitest/shaver.png";
content.document.body.appendChild(img);
try {
await imgListener(img);
Assert.ok(true);
} catch (e) {
Assert.ok(false);
}
});
gBrowser.removeTab(tab);
});
/**
* Loading an image from http:// immediately after loading from https://
* The load from https:// should be replaced.
*/
add_task(async function load_https_and_http_test() {
let tab = BrowserTestUtils.addTab(gBrowser, TEST_URI);
await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
// Clear image cache, otherwise in non-e10s mode the image might be cached by
// previous test, and make the image from https is loaded immediately.
let imgTools = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools);
let imageCache = imgTools.getImgCacheForDocument(window.document);
imageCache.clearCache(false); // false=content
gBrowser.selectedTab = tab;
await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
function imgListener(img) {
return new Promise((resolve, reject) => {
img.addEventListener("load", () => reject());
img.addEventListener("error", () => resolve());
});
}
let img = content.document.createElement("img");
img.src = "https://example.com/tests/image/test/mochitest/shaver.png";
img.src = "http://example.com/tests/image/test/mochitest/shaver.png";
content.document.body.appendChild(img);
try {
await imgListener(img);
Assert.ok(true);
} catch (e) {
Assert.ok(false);
}
});
gBrowser.removeTab(tab);
});
/**
* Loading an image from https.
* Then after we have size information of the image, we immediately change the
* location to a http:// site (hence should be blocked by CSP).
*/
add_task(async function block_pending_request_test() {
let tab = BrowserTestUtils.addTab(gBrowser, TEST_URI);
await BrowserTestUtils.browserLoaded(tab.linkedBrowser);
gBrowser.selectedTab = tab;
await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
let wrapper = {
_resolve: null,
_sizeAvail: false,
sizeAvailable(request) {
// In non-e10s mode the image may have already been cached, so sometimes
// sizeAvailable() is called earlier then waitUntilSizeAvailable().
if (this._resolve) {
this._resolve();
} else {
this._sizeAvail = true;
}
},
waitUntilSizeAvailable() {
return new Promise(resolve => {
this._resolve = resolve;
if (this._sizeAvail) {
resolve();
}
});
},
QueryInterface(aIID) {
if (aIID.equals(Ci.imgIScriptedNotificationObserver)) {
return this;
}
throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
},
};
let observer = Cc["@mozilla.org/image/tools;1"]
.getService(Ci.imgITools)
.createScriptedObserver(wrapper);
let img = content.document.createElement("img");
img.src = "https://example.com/tests/image/test/mochitest/shaver.png";
img.addObserver(observer);
content.document.body.appendChild(img);
info("Wait until Size Available");
await wrapper.waitUntilSizeAvailable();
info("Size Available now!");
img.removeObserver(observer);
let req = img.getRequest(Ci.nsIImageLoadingContent.CURRENT_REQUEST);
// Now we change to load from http:// site, which will be blocked.
img.src = "http://example.com/tests/image/test/mochitest/shaver.png";
Assert.equal(
img.getRequest(Ci.nsIImageLoadingContent.CURRENT_REQUEST),
req,
"CURRENT_REQUEST shouldn't be replaced."
);
});
gBrowser.removeTab(tab);
});