2017-10-27 17:33:53 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2002-08-21 12:01:05 +00:00
|
|
|
|
|
|
|
#include "nsPrintObject.h"
|
2018-11-12 22:20:52 +00:00
|
|
|
|
2006-12-10 08:05:05 +00:00
|
|
|
#include "nsIContentViewer.h"
|
2013-08-14 06:56:21 +00:00
|
|
|
#include "nsContentUtils.h" // for nsAutoScriptBlocker
|
2009-12-11 04:02:13 +00:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2018-11-12 22:20:52 +00:00
|
|
|
#include "nsPresContext.h"
|
2009-12-11 04:02:13 +00:00
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsIDocShellTreeItem.h"
|
2011-01-14 12:27:31 +00:00
|
|
|
#include "nsIBaseWindow.h"
|
2018-11-05 12:43:10 +00:00
|
|
|
#include "nsDocShell.h"
|
|
|
|
|
2019-04-16 07:25:10 +00:00
|
|
|
#include "mozilla/PresShell.h"
|
2018-11-05 12:43:10 +00:00
|
|
|
#include "mozilla/dom/BrowsingContext.h"
|
2019-04-16 07:25:10 +00:00
|
|
|
#include "mozilla/dom/Document.h"
|
2018-11-12 22:20:52 +00:00
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
|
2019-05-01 16:03:35 +00:00
|
|
|
using mozilla::PresShell;
|
2018-11-12 22:20:52 +00:00
|
|
|
using mozilla::dom::BrowsingContext;
|
2019-05-01 16:03:35 +00:00
|
|
|
using mozilla::dom::Document;
|
2018-11-12 22:20:52 +00:00
|
|
|
using mozilla::dom::Element;
|
2013-10-02 20:09:18 +00:00
|
|
|
|
2002-08-21 12:01:05 +00:00
|
|
|
//---------------------------------------------------
|
|
|
|
//-- nsPrintObject Class Impl
|
|
|
|
//---------------------------------------------------
|
2018-04-13 13:01:28 +00:00
|
|
|
nsPrintObject::nsPrintObject()
|
|
|
|
: mContent(nullptr),
|
|
|
|
mFrameType(eFrame),
|
|
|
|
mParent(nullptr),
|
|
|
|
mHasBeenPrinted(false),
|
|
|
|
mDontPrint(true),
|
|
|
|
mPrintAsIs(false),
|
2018-07-12 06:42:14 +00:00
|
|
|
mInvisible(false),
|
|
|
|
mDidCreateDocShell(false),
|
2018-04-13 13:01:28 +00:00
|
|
|
mShrinkRatio(1.0),
|
|
|
|
mZoomRatio(1.0) {
|
2011-06-16 18:20:13 +00:00
|
|
|
MOZ_COUNT_CTOR(nsPrintObject);
|
2002-08-21 12:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPrintObject::~nsPrintObject() {
|
2011-06-16 18:20:13 +00:00
|
|
|
MOZ_COUNT_DTOR(nsPrintObject);
|
2002-08-21 12:01:05 +00:00
|
|
|
|
2006-12-10 08:05:05 +00:00
|
|
|
DestroyPresentation();
|
2011-01-14 12:27:31 +00:00
|
|
|
if (mDidCreateDocShell && mDocShell) {
|
|
|
|
nsCOMPtr<nsIBaseWindow> baseWin(do_QueryInterface(mDocShell));
|
|
|
|
if (baseWin) {
|
|
|
|
baseWin->Destroy();
|
|
|
|
}
|
2017-03-06 07:35:39 +00:00
|
|
|
}
|
2012-07-30 14:20:58 +00:00
|
|
|
mDocShell = nullptr;
|
2017-03-06 07:35:39 +00:00
|
|
|
mTreeOwner = nullptr; // mTreeOwner must be released after mDocShell;
|
2002-08-21 12:01:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
2019-05-28 11:48:21 +00:00
|
|
|
nsresult nsPrintObject::InitAsRootObject(nsIDocShell* aDocShell, Document* aDoc,
|
|
|
|
bool aForPrintPreview) {
|
|
|
|
NS_ENSURE_STATE(aDocShell);
|
2018-05-11 17:46:15 +00:00
|
|
|
NS_ENSURE_STATE(aDoc);
|
|
|
|
|
2019-05-28 11:48:21 +00:00
|
|
|
if (aForPrintPreview) {
|
2019-06-04 15:01:33 +00:00
|
|
|
nsCOMPtr<nsIContentViewer> viewer;
|
|
|
|
aDocShell->GetContentViewer(getter_AddRefs(viewer));
|
|
|
|
if (viewer && viewer->GetDocument() && viewer->GetDocument()->IsShowing()) {
|
|
|
|
// We're about to discard this document, and it needs mIsShowing to be
|
|
|
|
// false to avoid triggering the assertion in its dtor.
|
|
|
|
viewer->GetDocument()->OnPageHide(false, nullptr);
|
|
|
|
}
|
2009-12-11 04:02:13 +00:00
|
|
|
mDocShell = aDocShell;
|
|
|
|
} else {
|
2019-05-28 11:48:21 +00:00
|
|
|
// When doing an actual print, we create a BrowsingContext/nsDocShell that
|
|
|
|
// is detached from any browser window or tab.
|
2018-11-05 12:43:10 +00:00
|
|
|
|
|
|
|
// Create a new BrowsingContext to create our DocShell in.
|
|
|
|
RefPtr<BrowsingContext> bc = BrowsingContext::Create(
|
2018-11-27 09:59:44 +00:00
|
|
|
/* aParent */ nullptr,
|
|
|
|
/* aOpener */ nullptr, EmptyString(),
|
2018-11-05 12:43:10 +00:00
|
|
|
aDocShell->ItemType() == nsIDocShellTreeItem::typeContent
|
|
|
|
? BrowsingContext::Type::Content
|
|
|
|
: BrowsingContext::Type::Chrome);
|
|
|
|
|
2009-12-11 04:02:13 +00:00
|
|
|
// Create a container docshell for printing.
|
2018-11-05 12:43:10 +00:00
|
|
|
mDocShell = nsDocShell::Create(bc);
|
2009-12-11 04:02:13 +00:00
|
|
|
NS_ENSURE_TRUE(mDocShell, NS_ERROR_OUT_OF_MEMORY);
|
2018-11-05 12:43:10 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mDidCreateDocShell = true;
|
2018-11-05 12:43:10 +00:00
|
|
|
MOZ_ASSERT(mDocShell->ItemType() == aDocShell->ItemType());
|
2019-05-28 11:48:21 +00:00
|
|
|
|
|
|
|
mTreeOwner = do_GetInterface(aDocShell);
|
2013-02-12 22:02:51 +00:00
|
|
|
mDocShell->SetTreeOwner(mTreeOwner);
|
2019-05-28 11:48:21 +00:00
|
|
|
|
|
|
|
// Make sure nsDocShell::EnsureContentViewer() is called:
|
|
|
|
mozilla::Unused << nsDocShell::Cast(mDocShell)->GetDocument();
|
2009-12-11 04:02:13 +00:00
|
|
|
}
|
|
|
|
|
2019-05-28 11:48:21 +00:00
|
|
|
mDocument = aDoc->CreateStaticClone(mDocShell);
|
|
|
|
NS_ENSURE_STATE(mDocument);
|
2016-07-18 16:37:11 +00:00
|
|
|
|
2006-12-10 08:05:05 +00:00
|
|
|
nsCOMPtr<nsIContentViewer> viewer;
|
2009-12-11 04:02:13 +00:00
|
|
|
mDocShell->GetContentViewer(getter_AddRefs(viewer));
|
2019-09-05 14:55:11 +00:00
|
|
|
NS_ENSURE_STATE(viewer);
|
|
|
|
viewer->SetDocument(mDocument);
|
2009-12-11 04:02:13 +00:00
|
|
|
|
2019-05-28 11:48:21 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsPrintObject::InitAsNestedObject(nsIDocShell* aDocShell,
|
|
|
|
Document* aDoc,
|
|
|
|
nsPrintObject* aParent) {
|
|
|
|
NS_ENSURE_STATE(aDocShell);
|
|
|
|
NS_ENSURE_STATE(aDoc);
|
|
|
|
|
|
|
|
mParent = aParent;
|
|
|
|
mDocShell = aDocShell;
|
|
|
|
mDocument = aDoc;
|
|
|
|
|
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> window = aDoc->GetWindow();
|
|
|
|
mContent = window->GetFrameElementInternal();
|
2002-08-21 12:01:05 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Resets PO by destroying the presentation
|
|
|
|
void nsPrintObject::DestroyPresentation() {
|
2006-12-10 08:05:05 +00:00
|
|
|
if (mPresShell) {
|
|
|
|
mPresShell->EndObservingDocument();
|
2009-05-15 03:08:41 +00:00
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
2019-04-16 07:25:10 +00:00
|
|
|
RefPtr<PresShell> presShell = mPresShell;
|
2013-01-15 20:47:10 +00:00
|
|
|
mPresShell = nullptr;
|
2019-04-16 07:25:10 +00:00
|
|
|
presShell->Destroy();
|
2006-12-10 08:05:05 +00:00
|
|
|
}
|
2012-12-18 04:32:34 +00:00
|
|
|
mPresContext = nullptr;
|
2012-07-30 14:20:58 +00:00
|
|
|
mViewManager = nullptr;
|
2002-08-21 12:01:05 +00:00
|
|
|
}
|