Bug 1731994: part 4) Declare more methods around `ContentPermissionRequestBase` ...
[gecko.git] / layout / printing / nsPrintObject.cpp
blobd5a6ae388e2338dda42b8b29e9b8398b3b089e80
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 #include "nsPrintObject.h"
9 #include "nsIContentViewer.h"
10 #include "nsContentUtils.h" // for nsAutoScriptBlocker
11 #include "nsIInterfaceRequestorUtils.h"
12 #include "nsPIDOMWindow.h"
13 #include "nsPresContext.h"
14 #include "nsGkAtoms.h"
15 #include "nsComponentManagerUtils.h"
16 #include "nsIBaseWindow.h"
17 #include "nsDocShell.h"
19 #include "mozilla/PresShell.h"
20 #include "mozilla/dom/BrowsingContext.h"
21 #include "mozilla/dom/Document.h"
22 #include "mozilla/dom/Element.h"
24 using namespace mozilla;
25 using mozilla::dom::BrowsingContext;
26 using mozilla::dom::Document;
27 using mozilla::dom::Element;
28 using mozilla::dom::Selection;
30 //---------------------------------------------------
31 //-- nsPrintObject Class Impl
32 //---------------------------------------------------
33 nsPrintObject::nsPrintObject()
34 : mContent(nullptr),
35 mFrameType(eFrame),
36 mParent(nullptr),
37 mHasBeenPrinted(false),
38 mInvisible(false),
39 mShrinkRatio(1.0),
40 mZoomRatio(1.0) {
41 MOZ_COUNT_CTOR(nsPrintObject);
44 nsPrintObject::~nsPrintObject() {
45 MOZ_COUNT_DTOR(nsPrintObject);
47 DestroyPresentation();
48 mDocShell = nullptr;
49 mTreeOwner = nullptr; // mTreeOwner must be released after mDocShell;
52 //------------------------------------------------------------------
54 nsresult nsPrintObject::InitAsRootObject(nsIDocShell* aDocShell, Document* aDoc,
55 bool aForPrintPreview) {
56 NS_ENSURE_STATE(aDocShell);
57 NS_ENSURE_STATE(aDoc);
59 MOZ_ASSERT(aDoc->IsStaticDocument());
61 mDocShell = aDocShell;
62 mDocument = aDoc;
64 // Ensure the document has no presentation.
65 DestroyPresentation();
67 return NS_OK;
70 nsresult nsPrintObject::InitAsNestedObject(nsIDocShell* aDocShell,
71 Document* aDoc,
72 nsPrintObject* aParent) {
73 NS_ENSURE_STATE(aDocShell);
74 NS_ENSURE_STATE(aDoc);
76 mParent = aParent;
77 mDocShell = aDocShell;
78 mDocument = aDoc;
80 nsCOMPtr<nsPIDOMWindowOuter> window = aDoc->GetWindow();
81 mContent = window->GetFrameElementInternal();
83 // "frame" elements not in a frameset context should be treated
84 // as iframes
85 if (mContent->IsHTMLElement(nsGkAtoms::frame) &&
86 mParent->mFrameType == eFrameSet) {
87 mFrameType = eFrame;
88 } else {
89 // Assume something iframe-like, i.e. iframe, object, or embed
90 mFrameType = eIFrame;
92 return NS_OK;
95 //------------------------------------------------------------------
96 // Resets PO by destroying the presentation
97 void nsPrintObject::DestroyPresentation() {
98 if (mDocument) {
99 if (RefPtr<PresShell> ps = mDocument->GetPresShell()) {
100 MOZ_DIAGNOSTIC_ASSERT(!mPresShell || ps == mPresShell);
101 mPresShell = nullptr;
102 nsAutoScriptBlocker scriptBlocker;
103 ps->EndObservingDocument();
104 ps->Destroy();
107 mPresShell = nullptr;
108 mPresContext = nullptr;
109 mViewManager = nullptr;
112 void nsPrintObject::EnablePrinting(bool aEnable) {
113 mPrintingIsEnabled = aEnable;
115 for (const UniquePtr<nsPrintObject>& kid : mKids) {
116 kid->EnablePrinting(aEnable);
120 bool nsPrintObject::HasSelection() const {
121 return mDocument && mDocument->GetProperty(nsGkAtoms::printselectionranges);
124 void nsPrintObject::EnablePrintingSelectionOnly() {
125 mPrintingIsEnabled = HasSelection();
127 for (const UniquePtr<nsPrintObject>& kid : mKids) {
128 kid->EnablePrintingSelectionOnly();