1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsPrintObject.h"
7 #include "nsIContentViewer.h"
8 #include "nsIDOMDocument.h"
9 #include "nsContentUtils.h" // for nsAutoScriptBlocker
10 #include "nsIInterfaceRequestorUtils.h"
11 #include "nsPIDOMWindow.h"
12 #include "nsGkAtoms.h"
13 #include "nsComponentManagerUtils.h"
14 #include "nsIDocShellTreeItem.h"
15 #include "nsIBaseWindow.h"
16 #include "nsIDocument.h"
18 //---------------------------------------------------
19 //-- nsPrintObject Class Impl
20 //---------------------------------------------------
21 nsPrintObject::nsPrintObject() :
22 mContent(nullptr), mFrameType(eFrame
), mParent(nullptr),
23 mHasBeenPrinted(false), mDontPrint(true), mPrintAsIs(false),
24 mInvisible(false), mDidCreateDocShell(false),
25 mShrinkRatio(1.0), mZoomRatio(1.0)
27 MOZ_COUNT_CTOR(nsPrintObject
);
31 nsPrintObject::~nsPrintObject()
33 MOZ_COUNT_DTOR(nsPrintObject
);
34 for (uint32_t i
=0;i
<mKids
.Length();i
++) {
35 nsPrintObject
* po
= mKids
[i
];
39 DestroyPresentation();
40 if (mDidCreateDocShell
&& mDocShell
) {
41 nsCOMPtr
<nsIBaseWindow
> baseWin(do_QueryInterface(mDocShell
));
47 mTreeOwner
= nullptr; // mTreeOwner must be released after mDocShell;
50 //------------------------------------------------------------------
52 nsPrintObject::Init(nsIDocShell
* aDocShell
, nsIDOMDocument
* aDoc
,
55 mPrintPreview
= aPrintPreview
;
57 if (mPrintPreview
|| mParent
) {
58 mDocShell
= aDocShell
;
60 mTreeOwner
= do_GetInterface(aDocShell
);
61 // Create a container docshell for printing.
62 mDocShell
= do_CreateInstance("@mozilla.org/docshell;1");
63 NS_ENSURE_TRUE(mDocShell
, NS_ERROR_OUT_OF_MEMORY
);
64 mDidCreateDocShell
= true;
65 mDocShell
->SetItemType(aDocShell
->ItemType());
66 mDocShell
->SetTreeOwner(mTreeOwner
);
68 NS_ENSURE_TRUE(mDocShell
, NS_ERROR_FAILURE
);
70 nsCOMPtr
<nsIDOMDocument
> dummy
= do_GetInterface(mDocShell
);
71 nsCOMPtr
<nsIContentViewer
> viewer
;
72 mDocShell
->GetContentViewer(getter_AddRefs(viewer
));
73 NS_ENSURE_STATE(viewer
);
75 nsCOMPtr
<nsIDocument
> doc
= do_QueryInterface(aDoc
);
79 nsCOMPtr
<nsPIDOMWindow
> window
= doc
->GetWindow();
81 mContent
= window
->GetFrameElementInternal();
87 mDocument
= doc
->CreateStaticClone(mDocShell
);
88 nsCOMPtr
<nsIDOMDocument
> clonedDOMDoc
= do_QueryInterface(mDocument
);
89 NS_ENSURE_STATE(clonedDOMDoc
);
91 viewer
->SetDOMDocument(clonedDOMDoc
);
95 //------------------------------------------------------------------
96 // Resets PO by destroying the presentation
98 nsPrintObject::DestroyPresentation()
101 mPresShell
->EndObservingDocument();
102 nsAutoScriptBlocker scriptBlocker
;
103 nsCOMPtr
<nsIPresShell
> shell
= mPresShell
;
104 mPresShell
= nullptr;
107 mPresContext
= nullptr;
108 mViewManager
= nullptr;