Bumping manifests a=b2g-bump
[gecko.git] / layout / printing / nsPrintObject.cpp
blob6506891db9c8ec058ded5a58ac56f3166d63939c
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];
36 delete po;
39 DestroyPresentation();
40 if (mDidCreateDocShell && mDocShell) {
41 nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(mDocShell));
42 if (baseWin) {
43 baseWin->Destroy();
46 mDocShell = nullptr;
47 mTreeOwner = nullptr; // mTreeOwner must be released after mDocShell;
50 //------------------------------------------------------------------
51 nsresult
52 nsPrintObject::Init(nsIDocShell* aDocShell, nsIDOMDocument* aDoc,
53 bool aPrintPreview)
55 mPrintPreview = aPrintPreview;
57 if (mPrintPreview || mParent) {
58 mDocShell = aDocShell;
59 } else {
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);
76 NS_ENSURE_STATE(doc);
78 if (mParent) {
79 nsCOMPtr<nsPIDOMWindow> window = doc->GetWindow();
80 if (window) {
81 mContent = window->GetFrameElementInternal();
83 mDocument = doc;
84 return NS_OK;
87 mDocument = doc->CreateStaticClone(mDocShell);
88 nsCOMPtr<nsIDOMDocument> clonedDOMDoc = do_QueryInterface(mDocument);
89 NS_ENSURE_STATE(clonedDOMDoc);
91 viewer->SetDOMDocument(clonedDOMDoc);
92 return NS_OK;
95 //------------------------------------------------------------------
96 // Resets PO by destroying the presentation
97 void
98 nsPrintObject::DestroyPresentation()
100 if (mPresShell) {
101 mPresShell->EndObservingDocument();
102 nsAutoScriptBlocker scriptBlocker;
103 nsCOMPtr<nsIPresShell> shell = mPresShell;
104 mPresShell = nullptr;
105 shell->Destroy();
107 mPresContext = nullptr;
108 mViewManager = nullptr;