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 "nsIDocumentViewer.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(nsIDocShell
& aDocShell
, Document
& aDoc
,
34 nsPrintObject
* aParent
)
35 : mDocShell(&aDocShell
), mDocument(&aDoc
), mParent(aParent
) {
36 MOZ_COUNT_CTOR(nsPrintObject
);
37 MOZ_ASSERT(aDoc
.IsStaticDocument());
40 // We are a root nsPrintObject.
41 // Ensure the document has no presentation.
42 DestroyPresentation();
44 // We are a nested nsPrintObject.
45 nsCOMPtr
<nsPIDOMWindowOuter
> window
= aDoc
.GetWindow();
46 mContent
= window
->GetFrameElementInternal();
50 nsPrintObject::~nsPrintObject() {
51 MOZ_COUNT_DTOR(nsPrintObject
);
53 DestroyPresentation();
55 mTreeOwner
= nullptr; // mTreeOwner must be released after mDocShell;
58 //------------------------------------------------------------------
59 // Resets PO by destroying the presentation
60 void nsPrintObject::DestroyPresentation() {
62 if (RefPtr
<PresShell
> ps
= mDocument
->GetPresShell()) {
63 MOZ_DIAGNOSTIC_ASSERT(!mPresShell
|| ps
== mPresShell
);
65 nsAutoScriptBlocker scriptBlocker
;
66 ps
->EndObservingDocument();
71 mPresContext
= nullptr;
72 mViewManager
= nullptr;
75 void nsPrintObject::EnablePrinting(bool aEnable
) {
76 mPrintingIsEnabled
= aEnable
;
78 for (const UniquePtr
<nsPrintObject
>& kid
: mKids
) {
79 kid
->EnablePrinting(aEnable
);
83 bool nsPrintObject::HasSelection() const {
84 return mDocument
&& mDocument
->GetProperty(nsGkAtoms::printselectionranges
);
87 void nsPrintObject::EnablePrintingSelectionOnly() {
88 mPrintingIsEnabled
= HasSelection();
90 for (const UniquePtr
<nsPrintObject
>& kid
: mKids
) {
91 kid
->EnablePrintingSelectionOnly();