Bug 1800897 - Don't fire resize events along before/afterprint. r=dholbert, a=RyanVM

This partially backs out bug 774398.

This code was added to match what Chromium was doing, but it seems (per
discussions with Chromium engineers) that:

 * Resize event listener in Chromium never changed
   innerWidth/innerHeight. That doesn't match my recollection from bug
   774398, but it's true now in Chrome stable, at least.

 * Chromium canary no longer fires a resize event
   (https://crbug.com/1076867).

 * WebKit doesn't fire resize either.

So let's get rid of this, which also causes some correctness issues with
some iframes, and keep just the media query listeners firing.

Differential Revision: https://phabricator.services.mozilla.com/D185404
This commit is contained in:
Emilio Cobos Álvarez 2023-08-04 21:37:41 +00:00
parent 92b11955cb
commit 1f7395611b
7 changed files with 20 additions and 72 deletions

View File

@ -18,9 +18,9 @@ class AutoPrintEventDispatcher {
// NOTE(emilio): For fission iframes, we dispatch this event in
// RecvCloneDocumentTreeIntoSelf.
static void CollectInProcessSubdocuments(
Document& aDoc, nsTArray<std::pair<nsCOMPtr<Document>, bool>>& aDocs) {
Document& aDoc, nsTArray<nsCOMPtr<Document>>& aDocs) {
auto recurse = [&aDocs](Document& aSubDoc) {
aDocs.AppendElement(std::make_pair(&aSubDoc, false));
aDocs.AppendElement(&aSubDoc);
CollectInProcessSubdocuments(aSubDoc, aDocs);
return CallState::Continue;
};
@ -28,69 +28,22 @@ class AutoPrintEventDispatcher {
}
MOZ_CAN_RUN_SCRIPT void DispatchEvent(bool aBefore) {
for (auto& [doc, isTop] : mDocuments) {
for (auto& doc : mDocuments) {
nsContentUtils::DispatchTrustedEvent(
doc, doc->GetWindow(), aBefore ? u"beforeprint"_ns : u"afterprint"_ns,
CanBubble::eNo, Cancelable::eNo, nullptr);
if (RefPtr<nsPresContext> presContext = doc->GetPresContext()) {
presContext->EmulateMedium(aBefore ? nsGkAtoms::print : nullptr);
bool needResizeEvent = false;
if (isTop) {
// NOTE(emilio): Having this code here means that we only fire the
// resize on the initial document clone, not on any further print
// settings changes. This is an intentional trade-off.
//
// On a continuously changing document, we don't want to keep
// re-cloning every time the user changes the print settings, as it
// changes what you're printing.
if (aBefore) {
mVisibleAreaToRestore = presContext->GetVisibleArea();
presContext->SetVisibleArea(
nsRect(mVisibleAreaToRestore.TopLeft(), mPageSize));
needResizeEvent = mVisibleAreaToRestore.Size() != mPageSize;
} else {
if (presContext->GetVisibleArea().Size() == mPageSize) {
presContext->SetVisibleArea(mVisibleAreaToRestore);
needResizeEvent = mVisibleAreaToRestore.Size() != mPageSize;
} else {
// This shouldn't happen, but can if used by privileged script,
// like in test_printpreview.xhtml. It's ok to do nothing here
// tho, we just need to make sure that the mPageSize viewport size
// doesn't persist.
NS_WARNING(
"Something changed our viewport size between firing "
"before/afterprint?");
}
}
}
// Ensure media query listeners fire.
doc->FlushPendingNotifications(FlushType::Style);
if (needResizeEvent) {
if (RefPtr ps = presContext->GetPresShell()) {
ps->FireResizeEventSync();
}
}
}
}
}
static nsSize ComputePrintPageSize(nsIPrintSettings* aPrintSettings) {
double pageWidth = 0;
double pageHeight = 0;
aPrintSettings->GetEffectivePageSize(&pageWidth, &pageHeight);
return nsSize(nsPresContext::CSSTwipsToAppUnits(NSToIntFloor(pageWidth)),
nsPresContext::CSSTwipsToAppUnits(NSToIntFloor(pageHeight)));
}
public:
MOZ_CAN_RUN_SCRIPT AutoPrintEventDispatcher(Document& aDoc,
nsIPrintSettings* aPrintSettings,
bool aIsTop)
: mPageSize(ComputePrintPageSize(aPrintSettings)) {
MOZ_CAN_RUN_SCRIPT explicit AutoPrintEventDispatcher(Document& aDoc) {
if (!aDoc.IsStaticDocument()) {
mDocuments.AppendElement(std::make_pair(&aDoc, aIsTop));
mDocuments.AppendElement(&aDoc);
CollectInProcessSubdocuments(aDoc, mDocuments);
}
@ -99,8 +52,7 @@ class AutoPrintEventDispatcher {
MOZ_CAN_RUN_SCRIPT ~AutoPrintEventDispatcher() { DispatchEvent(false); }
// The bool represents whether this is the top-level paginated document.
AutoTArray<std::pair<nsCOMPtr<Document>, bool>, 8> mDocuments;
AutoTArray<nsCOMPtr<Document>, 8> mDocuments;
const nsSize mPageSize;
nsRect mVisibleAreaToRestore;
};

View File

@ -5284,7 +5284,7 @@ Nullable<WindowProxyHolder> nsGlobalWindowOuter::Print(
}
}
AutoPrintEventDispatcher dispatcher(*docToPrint, ps, /* aIsTop = */ true);
AutoPrintEventDispatcher dispatcher(*docToPrint);
nsAutoScriptBlocker blockScripts;
RefPtr<Document> clone = docToPrint->CreateStaticClone(

View File

@ -935,8 +935,7 @@ nsresult BrowserChild::CloneDocumentTreeIntoSelf(
RefPtr<Document> clone;
{
AutoPrintEventDispatcher dispatcher(*sourceDocument, printSettings,
/* aIsTop = */ false);
AutoPrintEventDispatcher dispatcher(*sourceDocument);
nsAutoScriptBlocker scriptBlocker;
bool hasInProcessCallbacks = false;
clone = sourceDocument->CreateStaticClone(ourDocShell, cv, printSettings,

View File

@ -1,5 +0,0 @@
<!doctype html>
<div id="target">FAIL</div>
<script>
document.getElementById("target").innerHTML = `${window.innerWidth}x${window.innerHeight}`;
</script>

View File

@ -1,10 +0,0 @@
<!doctype html>
<link rel="mismatch" href="size-change-print-notref.html">
<div id="target">FAIL</div>
<script>
function update() {
document.getElementById("target").innerHTML = `${window.innerWidth}x${window.innerHeight}`;
}
update();
window.addEventListener("resize", update);
</script>

View File

@ -0,0 +1,10 @@
<!doctype html>
<title>No resize event on the original page when printing</title>
<link rel="help" href="https://drafts.csswg.org/mediaqueries-4/#valdef-media-print">
<link rel="match" href="no-resize-event-ref.html">
<div id="target">PASS</div>
<script>
window.addEventListener("resize", function() {
document.getElementById("target").innerHTML = `FAIL: ${window.innerWidth}x${window.innerHeight}`;
})
</script>

View File

@ -0,0 +1,2 @@
<!doctype html>
<div>PASS</div>