Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsPrintObject.h" |
michael@0 | 7 | #include "nsIContentViewer.h" |
michael@0 | 8 | #include "nsIDOMDocument.h" |
michael@0 | 9 | #include "nsContentUtils.h" // for nsAutoScriptBlocker |
michael@0 | 10 | #include "nsIInterfaceRequestorUtils.h" |
michael@0 | 11 | #include "nsPIDOMWindow.h" |
michael@0 | 12 | #include "nsGkAtoms.h" |
michael@0 | 13 | #include "nsComponentManagerUtils.h" |
michael@0 | 14 | #include "nsIDocShellTreeItem.h" |
michael@0 | 15 | #include "nsIBaseWindow.h" |
michael@0 | 16 | #include "nsIDocument.h" |
michael@0 | 17 | |
michael@0 | 18 | //--------------------------------------------------- |
michael@0 | 19 | //-- nsPrintObject Class Impl |
michael@0 | 20 | //--------------------------------------------------- |
michael@0 | 21 | nsPrintObject::nsPrintObject() : |
michael@0 | 22 | mContent(nullptr), mFrameType(eFrame), mParent(nullptr), |
michael@0 | 23 | mHasBeenPrinted(false), mDontPrint(true), mPrintAsIs(false), |
michael@0 | 24 | mInvisible(false), mDidCreateDocShell(false), |
michael@0 | 25 | mShrinkRatio(1.0), mZoomRatio(1.0) |
michael@0 | 26 | { |
michael@0 | 27 | MOZ_COUNT_CTOR(nsPrintObject); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | |
michael@0 | 31 | nsPrintObject::~nsPrintObject() |
michael@0 | 32 | { |
michael@0 | 33 | MOZ_COUNT_DTOR(nsPrintObject); |
michael@0 | 34 | for (uint32_t i=0;i<mKids.Length();i++) { |
michael@0 | 35 | nsPrintObject* po = mKids[i]; |
michael@0 | 36 | delete po; |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | DestroyPresentation(); |
michael@0 | 40 | if (mDidCreateDocShell && mDocShell) { |
michael@0 | 41 | nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(mDocShell)); |
michael@0 | 42 | if (baseWin) { |
michael@0 | 43 | baseWin->Destroy(); |
michael@0 | 44 | } |
michael@0 | 45 | } |
michael@0 | 46 | mDocShell = nullptr; |
michael@0 | 47 | mTreeOwner = nullptr; // mTreeOwner must be released after mDocShell; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | //------------------------------------------------------------------ |
michael@0 | 51 | nsresult |
michael@0 | 52 | nsPrintObject::Init(nsIDocShell* aDocShell, nsIDOMDocument* aDoc, |
michael@0 | 53 | bool aPrintPreview) |
michael@0 | 54 | { |
michael@0 | 55 | mPrintPreview = aPrintPreview; |
michael@0 | 56 | |
michael@0 | 57 | if (mPrintPreview || mParent) { |
michael@0 | 58 | mDocShell = aDocShell; |
michael@0 | 59 | } else { |
michael@0 | 60 | mTreeOwner = do_GetInterface(aDocShell); |
michael@0 | 61 | // Create a container docshell for printing. |
michael@0 | 62 | mDocShell = do_CreateInstance("@mozilla.org/docshell;1"); |
michael@0 | 63 | NS_ENSURE_TRUE(mDocShell, NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 64 | mDidCreateDocShell = true; |
michael@0 | 65 | mDocShell->SetItemType(aDocShell->ItemType()); |
michael@0 | 66 | mDocShell->SetTreeOwner(mTreeOwner); |
michael@0 | 67 | } |
michael@0 | 68 | NS_ENSURE_TRUE(mDocShell, NS_ERROR_FAILURE); |
michael@0 | 69 | |
michael@0 | 70 | nsCOMPtr<nsIDOMDocument> dummy = do_GetInterface(mDocShell); |
michael@0 | 71 | nsCOMPtr<nsIContentViewer> viewer; |
michael@0 | 72 | mDocShell->GetContentViewer(getter_AddRefs(viewer)); |
michael@0 | 73 | NS_ENSURE_STATE(viewer); |
michael@0 | 74 | |
michael@0 | 75 | nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDoc); |
michael@0 | 76 | NS_ENSURE_STATE(doc); |
michael@0 | 77 | |
michael@0 | 78 | if (mParent) { |
michael@0 | 79 | nsCOMPtr<nsPIDOMWindow> window = doc->GetWindow(); |
michael@0 | 80 | if (window) { |
michael@0 | 81 | mContent = do_QueryInterface(window->GetFrameElementInternal()); |
michael@0 | 82 | } |
michael@0 | 83 | mDocument = doc; |
michael@0 | 84 | return NS_OK; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | mDocument = doc->CreateStaticClone(mDocShell); |
michael@0 | 88 | nsCOMPtr<nsIDOMDocument> clonedDOMDoc = do_QueryInterface(mDocument); |
michael@0 | 89 | NS_ENSURE_STATE(clonedDOMDoc); |
michael@0 | 90 | |
michael@0 | 91 | viewer->SetDOMDocument(clonedDOMDoc); |
michael@0 | 92 | return NS_OK; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | //------------------------------------------------------------------ |
michael@0 | 96 | // Resets PO by destroying the presentation |
michael@0 | 97 | void |
michael@0 | 98 | nsPrintObject::DestroyPresentation() |
michael@0 | 99 | { |
michael@0 | 100 | if (mPresShell) { |
michael@0 | 101 | mPresShell->EndObservingDocument(); |
michael@0 | 102 | nsAutoScriptBlocker scriptBlocker; |
michael@0 | 103 | nsCOMPtr<nsIPresShell> shell = mPresShell; |
michael@0 | 104 | mPresShell = nullptr; |
michael@0 | 105 | shell->Destroy(); |
michael@0 | 106 | } |
michael@0 | 107 | mPresContext = nullptr; |
michael@0 | 108 | mViewManager = nullptr; |
michael@0 | 109 | } |
michael@0 | 110 |