Backed out changeset b462e7b742d8 (bug 1908261) for causing multiple reftest failures...
[gecko.git] / dom / base / AutoPrintEventDispatcher.h
blob8c91285f38e5f208e0a04f61cc283aa74a1e0ac8
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_AutoPrintEventDispatcher_h
8 #define mozilla_dom_AutoPrintEventDispatcher_h
10 #include "mozilla/dom/Document.h"
11 #include "mozilla/dom/DocumentInlines.h"
12 #include "nsContentUtils.h"
13 #include "nsGlobalWindowOuter.h"
14 #include "nsIPrintSettings.h"
16 namespace mozilla::dom {
18 class AutoPrintEventDispatcher {
19 // NOTE(emilio): For fission iframes, we dispatch this event in
20 // RecvCloneDocumentTreeIntoSelf.
21 static void CollectInProcessSubdocuments(
22 Document& aDoc, nsTArray<nsCOMPtr<Document>>& aDocs) {
23 aDoc.EnumerateSubDocuments([&aDocs](Document& aSubDoc) {
24 aDocs.AppendElement(&aSubDoc);
25 CollectInProcessSubdocuments(aSubDoc, aDocs);
26 return CallState::Continue;
27 });
30 MOZ_CAN_RUN_SCRIPT void DispatchEvent(bool aBefore) {
31 for (auto& doc : mDocuments) {
32 nsContentUtils::DispatchTrustedEvent(
33 doc, nsGlobalWindowOuter::Cast(doc->GetWindow()),
34 aBefore ? u"beforeprint"_ns : u"afterprint"_ns, CanBubble::eNo,
35 Cancelable::eNo, nullptr);
36 if (RefPtr<nsPresContext> presContext = doc->GetPresContext()) {
37 presContext->EmulateMedium(aBefore ? nsGkAtoms::print : nullptr);
38 // Ensure media query listeners fire.
39 // FIXME(emilio): This is hacky, at best, but is required for compat
40 // with some pages, see bug 774398.
41 doc->EvaluateMediaQueriesAndReportChanges(/* aRecurse = */ false);
46 public:
47 MOZ_CAN_RUN_SCRIPT explicit AutoPrintEventDispatcher(Document& aDoc) {
48 if (!aDoc.IsStaticDocument()) {
49 mDocuments.AppendElement(&aDoc);
50 CollectInProcessSubdocuments(aDoc, mDocuments);
53 DispatchEvent(true);
56 MOZ_CAN_RUN_SCRIPT ~AutoPrintEventDispatcher() { DispatchEvent(false); }
58 AutoTArray<nsCOMPtr<Document>, 8> mDocuments;
59 const nsSize mPageSize;
60 nsRect mVisibleAreaToRestore;
63 } // namespace mozilla::dom
65 #endif