AutoPrintEventDispatcher.h (2213B)
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/. */ 6 7 #ifndef mozilla_dom_AutoPrintEventDispatcher_h 8 #define mozilla_dom_AutoPrintEventDispatcher_h 9 10 #include "mozilla/dom/Document.h" 11 #include "mozilla/dom/DocumentInlines.h" 12 #include "nsContentUtils.h" 13 #include "nsGlobalWindowOuter.h" 14 #include "nsIPrintSettings.h" 15 16 namespace mozilla::dom { 17 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 }); 28 } 29 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(); 42 } 43 } 44 } 45 46 public: 47 MOZ_CAN_RUN_SCRIPT explicit AutoPrintEventDispatcher(Document& aDoc) { 48 if (!aDoc.IsStaticDocument()) { 49 mDocuments.AppendElement(&aDoc); 50 CollectInProcessSubdocuments(aDoc, mDocuments); 51 } 52 53 DispatchEvent(true); 54 } 55 56 MOZ_CAN_RUN_SCRIPT ~AutoPrintEventDispatcher() { DispatchEvent(false); } 57 58 AutoTArray<nsCOMPtr<Document>, 8> mDocuments; 59 const nsSize mPageSize; 60 nsRect mVisibleAreaToRestore; 61 }; 62 63 } // namespace mozilla::dom 64 65 #endif