michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsPrintObject.h" michael@0: #include "nsIContentViewer.h" michael@0: #include "nsIDOMDocument.h" michael@0: #include "nsContentUtils.h" // for nsAutoScriptBlocker michael@0: #include "nsIInterfaceRequestorUtils.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsGkAtoms.h" michael@0: #include "nsComponentManagerUtils.h" michael@0: #include "nsIDocShellTreeItem.h" michael@0: #include "nsIBaseWindow.h" michael@0: #include "nsIDocument.h" michael@0: michael@0: //--------------------------------------------------- michael@0: //-- nsPrintObject Class Impl michael@0: //--------------------------------------------------- michael@0: nsPrintObject::nsPrintObject() : michael@0: mContent(nullptr), mFrameType(eFrame), mParent(nullptr), michael@0: mHasBeenPrinted(false), mDontPrint(true), mPrintAsIs(false), michael@0: mInvisible(false), mDidCreateDocShell(false), michael@0: mShrinkRatio(1.0), mZoomRatio(1.0) michael@0: { michael@0: MOZ_COUNT_CTOR(nsPrintObject); michael@0: } michael@0: michael@0: michael@0: nsPrintObject::~nsPrintObject() michael@0: { michael@0: MOZ_COUNT_DTOR(nsPrintObject); michael@0: for (uint32_t i=0;i baseWin(do_QueryInterface(mDocShell)); michael@0: if (baseWin) { michael@0: baseWin->Destroy(); michael@0: } michael@0: } michael@0: mDocShell = nullptr; michael@0: mTreeOwner = nullptr; // mTreeOwner must be released after mDocShell; michael@0: } michael@0: michael@0: //------------------------------------------------------------------ michael@0: nsresult michael@0: nsPrintObject::Init(nsIDocShell* aDocShell, nsIDOMDocument* aDoc, michael@0: bool aPrintPreview) michael@0: { michael@0: mPrintPreview = aPrintPreview; michael@0: michael@0: if (mPrintPreview || mParent) { michael@0: mDocShell = aDocShell; michael@0: } else { michael@0: mTreeOwner = do_GetInterface(aDocShell); michael@0: // Create a container docshell for printing. michael@0: mDocShell = do_CreateInstance("@mozilla.org/docshell;1"); michael@0: NS_ENSURE_TRUE(mDocShell, NS_ERROR_OUT_OF_MEMORY); michael@0: mDidCreateDocShell = true; michael@0: mDocShell->SetItemType(aDocShell->ItemType()); michael@0: mDocShell->SetTreeOwner(mTreeOwner); michael@0: } michael@0: NS_ENSURE_TRUE(mDocShell, NS_ERROR_FAILURE); michael@0: michael@0: nsCOMPtr dummy = do_GetInterface(mDocShell); michael@0: nsCOMPtr viewer; michael@0: mDocShell->GetContentViewer(getter_AddRefs(viewer)); michael@0: NS_ENSURE_STATE(viewer); michael@0: michael@0: nsCOMPtr doc = do_QueryInterface(aDoc); michael@0: NS_ENSURE_STATE(doc); michael@0: michael@0: if (mParent) { michael@0: nsCOMPtr window = doc->GetWindow(); michael@0: if (window) { michael@0: mContent = do_QueryInterface(window->GetFrameElementInternal()); michael@0: } michael@0: mDocument = doc; michael@0: return NS_OK; michael@0: } michael@0: michael@0: mDocument = doc->CreateStaticClone(mDocShell); michael@0: nsCOMPtr clonedDOMDoc = do_QueryInterface(mDocument); michael@0: NS_ENSURE_STATE(clonedDOMDoc); michael@0: michael@0: viewer->SetDOMDocument(clonedDOMDoc); michael@0: return NS_OK; michael@0: } michael@0: michael@0: //------------------------------------------------------------------ michael@0: // Resets PO by destroying the presentation michael@0: void michael@0: nsPrintObject::DestroyPresentation() michael@0: { michael@0: if (mPresShell) { michael@0: mPresShell->EndObservingDocument(); michael@0: nsAutoScriptBlocker scriptBlocker; michael@0: nsCOMPtr shell = mPresShell; michael@0: mPresShell = nullptr; michael@0: shell->Destroy(); michael@0: } michael@0: mPresContext = nullptr; michael@0: mViewManager = nullptr; michael@0: } michael@0: