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()
37 mHasBeenPrinted(false),
41 MOZ_COUNT_CTOR(nsPrintObject
);
44 nsPrintObject::~nsPrintObject() {
45 MOZ_COUNT_DTOR(nsPrintObject
);
47 DestroyPresentation();
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
;
64 // Ensure the document has no presentation.
65 DestroyPresentation();
70 nsresult
nsPrintObject::InitAsNestedObject(nsIDocShell
* aDocShell
,
72 nsPrintObject
* aParent
) {
73 NS_ENSURE_STATE(aDocShell
);
74 NS_ENSURE_STATE(aDoc
);
77 mDocShell
= aDocShell
;
80 nsCOMPtr
<nsPIDOMWindowOuter
> window
= aDoc
->GetWindow();
81 mContent
= window
->GetFrameElementInternal();
83 // "frame" elements not in a frameset context should be treated
85 if (mContent
->IsHTMLElement(nsGkAtoms::frame
) &&
86 mParent
->mFrameType
== eFrameSet
) {
89 // Assume something iframe-like, i.e. iframe, object, or embed
95 //------------------------------------------------------------------
96 // Resets PO by destroying the presentation
97 void nsPrintObject::DestroyPresentation() {
99 if (RefPtr
<PresShell
> ps
= mDocument
->GetPresShell()) {
100 MOZ_DIAGNOSTIC_ASSERT(!mPresShell
|| ps
== mPresShell
);
101 mPresShell
= nullptr;
102 nsAutoScriptBlocker scriptBlocker
;
103 ps
->EndObservingDocument();
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();