Bug 1854550 - pt 10. Allow LOG() with zero extra arguments r=glandium
[gecko.git] / dom / base / AutoPrintEventDispatcher.h
blob38382fdfea6966f75636690a5ea40623f6ce5f34
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 auto recurse = [&aDocs](Document& aSubDoc) {
24 aDocs.AppendElement(&aSubDoc);
25 CollectInProcessSubdocuments(aSubDoc, aDocs);
26 return CallState::Continue;
28 aDoc.EnumerateSubDocuments(recurse);
31 MOZ_CAN_RUN_SCRIPT void DispatchEvent(bool aBefore) {
32 for (auto& doc : mDocuments) {
33 nsContentUtils::DispatchTrustedEvent(
34 doc, nsGlobalWindowOuter::Cast(doc->GetWindow()),
35 aBefore ? u"beforeprint"_ns : u"afterprint"_ns, CanBubble::eNo,
36 Cancelable::eNo, nullptr);
37 if (RefPtr<nsPresContext> presContext = doc->GetPresContext()) {
38 presContext->EmulateMedium(aBefore ? nsGkAtoms::print : nullptr);
39 // Ensure media query listeners fire.
40 // FIXME(emilio): This is hacky, at best, but is required for compat
41 // with some pages, see bug 774398.
42 doc->EvaluateMediaQueriesAndReportChanges(/* aRecurse = */ false);
47 public:
48 MOZ_CAN_RUN_SCRIPT explicit AutoPrintEventDispatcher(Document& aDoc) {
49 if (!aDoc.IsStaticDocument()) {
50 mDocuments.AppendElement(&aDoc);
51 CollectInProcessSubdocuments(aDoc, mDocuments);
54 DispatchEvent(true);
57 MOZ_CAN_RUN_SCRIPT ~AutoPrintEventDispatcher() { DispatchEvent(false); }
59 AutoTArray<nsCOMPtr<Document>, 8> mDocuments;
60 const nsSize mPageSize;
61 nsRect mVisibleAreaToRestore;
64 } // namespace mozilla::dom
66 #endif