2017-10-27 20:33:53 +03: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 15:12:37 +04: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 16:01:05 +04:00
|
|
|
|
|
|
|
#include "nsPrintObject.h"
|
2018-11-13 01:20:52 +03:00
|
|
|
|
2006-12-10 11:05:05 +03:00
|
|
|
#include "nsIContentViewer.h"
|
2013-08-14 10:56:21 +04:00
|
|
|
#include "nsContentUtils.h" // for nsAutoScriptBlocker
|
2009-12-11 07:02:13 +03:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2018-11-13 01:20:52 +03:00
|
|
|
#include "nsPresContext.h"
|
2009-12-11 07:02:13 +03:00
|
|
|
#include "nsGkAtoms.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2011-01-14 15:27:31 +03:00
|
|
|
#include "nsIBaseWindow.h"
|
2018-11-05 15:43:10 +03:00
|
|
|
#include "nsDocShell.h"
|
|
|
|
|
2019-04-16 10:25:10 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2018-11-05 15:43:10 +03:00
|
|
|
#include "mozilla/dom/BrowsingContext.h"
|
2019-04-16 10:25:10 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2018-11-13 01:20:52 +03:00
|
|
|
#include "mozilla/dom/Element.h"
|
|
|
|
|
2019-05-01 19:03:35 +03:00
|
|
|
using mozilla::PresShell;
|
2018-11-13 01:20:52 +03:00
|
|
|
using mozilla::dom::BrowsingContext;
|
2019-05-01 19:03:35 +03:00
|
|
|
using mozilla::dom::Document;
|
2018-11-13 01:20:52 +03:00
|
|
|
using mozilla::dom::Element;
|
2013-10-03 00:09:18 +04:00
|
|
|
|
2002-08-21 16:01:05 +04:00
|
|
|
//---------------------------------------------------
|
|
|
|
//-- nsPrintObject Class Impl
|
|
|
|
//---------------------------------------------------
|
2018-04-13 16:01:28 +03:00
|
|
|
nsPrintObject::nsPrintObject()
|
|
|
|
: mContent(nullptr),
|
|
|
|
mFrameType(eFrame),
|
|
|
|
mParent(nullptr),
|
|
|
|
mHasBeenPrinted(false),
|
2018-07-12 09:42:14 +03:00
|
|
|
mInvisible(false),
|
|
|
|
mDidCreateDocShell(false),
|
2018-04-13 16:01:28 +03:00
|
|
|
mShrinkRatio(1.0),
|
|
|
|
mZoomRatio(1.0) {
|
2011-06-16 22:20:13 +04:00
|
|
|
MOZ_COUNT_CTOR(nsPrintObject);
|
2002-08-21 16:01:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPrintObject::~nsPrintObject() {
|
2011-06-16 22:20:13 +04:00
|
|
|
MOZ_COUNT_DTOR(nsPrintObject);
|
2002-08-21 16:01:05 +04:00
|
|
|
|
2006-12-10 11:05:05 +03:00
|
|
|
DestroyPresentation();
|
2011-01-14 15:27:31 +03:00
|
|
|
if (mDidCreateDocShell && mDocShell) {
|
2020-02-06 22:07:56 +03:00
|
|
|
RefPtr<BrowsingContext> bc(mDocShell->GetBrowsingContext());
|
|
|
|
nsDocShell::Cast(mDocShell)->Destroy();
|
|
|
|
bc->Detach();
|
2017-03-06 10:35:39 +03:00
|
|
|
}
|
2020-04-23 19:41:49 +03:00
|
|
|
if (mDocument) {
|
|
|
|
mDocument->RemoveProperty(nsGkAtoms::printselectionranges);
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mDocShell = nullptr;
|
2017-03-06 10:35:39 +03:00
|
|
|
mTreeOwner = nullptr; // mTreeOwner must be released after mDocShell;
|
2002-08-21 16:01:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
2020-04-23 19:41:49 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the node in a static-clone document that corresponds to aOrigNOde,
|
|
|
|
* which is a node in the original document from which aStaticClone was cloned.
|
|
|
|
*/
|
|
|
|
static nsINode* GetCorrespondingNodeInDocument(const nsINode* aOrigNode,
|
|
|
|
Document& aStaticClone) {
|
|
|
|
MOZ_ASSERT(aOrigNode);
|
|
|
|
|
|
|
|
// Selections in anonymous subtrees aren't supported.
|
2020-05-25 14:43:51 +03:00
|
|
|
if (aOrigNode->IsInNativeAnonymousSubtree() || aOrigNode->IsInShadowTree()) {
|
2020-04-23 19:41:49 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<int32_t> indexArray;
|
|
|
|
const nsINode* child = aOrigNode;
|
|
|
|
while (const nsINode* parent = child->GetParentNode()) {
|
|
|
|
int32_t index = parent->ComputeIndexOf(child);
|
|
|
|
MOZ_ASSERT(index >= 0);
|
|
|
|
indexArray.AppendElement(index);
|
|
|
|
child = parent;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(child->IsDocument());
|
|
|
|
|
|
|
|
nsINode* correspondingNode = &aStaticClone;
|
|
|
|
for (int32_t i : Reversed(indexArray)) {
|
|
|
|
correspondingNode = correspondingNode->GetChildAt_Deprecated(i);
|
|
|
|
NS_ENSURE_TRUE(correspondingNode, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return correspondingNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Caches the selection ranges from the source document onto the static clone in
|
|
|
|
* case the "Print Selection Only" functionality is invoked.
|
|
|
|
*
|
|
|
|
* Note that we cannot use the selection obtained from
|
|
|
|
* Document::GetOriginalDocument() since that selection may have mutated after
|
|
|
|
* the print was invoked.
|
|
|
|
*
|
|
|
|
* Note also that because nsRange objects point into a specific document's
|
|
|
|
* nodes, we cannot reuse an array of nsRange objects across multiple static
|
|
|
|
* clone documents. For that reason we cache a new array of ranges on each
|
|
|
|
* static clone that we create.
|
|
|
|
*/
|
|
|
|
static void CachePrintSelectionRanges(const Document& aSourceDoc,
|
|
|
|
Document& aStaticClone) {
|
|
|
|
MOZ_ASSERT(aStaticClone.IsStaticDocument());
|
|
|
|
MOZ_ASSERT(!aStaticClone.GetProperty(nsGkAtoms::printselectionranges));
|
|
|
|
|
|
|
|
const Selection* origSelection = nullptr;
|
|
|
|
const nsTArray<RefPtr<nsRange>>* origRanges = nullptr;
|
|
|
|
bool sourceDocIsStatic = aSourceDoc.IsStaticDocument();
|
|
|
|
|
|
|
|
if (sourceDocIsStatic) {
|
|
|
|
origRanges = static_cast<nsTArray<RefPtr<nsRange>>*>(
|
|
|
|
aSourceDoc.GetProperty(nsGkAtoms::printselectionranges));
|
|
|
|
} else if (PresShell* shell = aSourceDoc.GetPresShell()) {
|
|
|
|
origSelection = shell->GetCurrentSelection(SelectionType::eNormal);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!origSelection && !origRanges) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t rangeCount =
|
|
|
|
sourceDocIsStatic ? origRanges->Length() : origSelection->RangeCount();
|
|
|
|
auto* printRanges = new nsTArray<RefPtr<nsRange>>(rangeCount);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < rangeCount; ++i) {
|
2020-05-03 02:46:01 +03:00
|
|
|
const nsRange* range = sourceDocIsStatic ? origRanges->ElementAt(i).get()
|
|
|
|
: origSelection->GetRangeAt(i);
|
2020-04-23 19:41:49 +03:00
|
|
|
nsINode* startContainer = range->GetStartContainer();
|
|
|
|
nsINode* endContainer = range->GetEndContainer();
|
|
|
|
|
|
|
|
if (!startContainer || !endContainer) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsINode* startNode =
|
|
|
|
GetCorrespondingNodeInDocument(startContainer, aStaticClone);
|
|
|
|
nsINode* endNode =
|
|
|
|
GetCorrespondingNodeInDocument(endContainer, aStaticClone);
|
|
|
|
|
|
|
|
if (!startNode || !endNode) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<nsRange> clonedRange =
|
|
|
|
nsRange::Create(startNode, range->StartOffset(), endNode,
|
|
|
|
range->EndOffset(), IgnoreErrors());
|
|
|
|
if (clonedRange && !clonedRange->Collapsed()) {
|
|
|
|
printRanges->AppendElement(std::move(clonedRange));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aStaticClone.SetProperty(nsGkAtoms::printselectionranges, printRanges,
|
|
|
|
nsINode::DeleteProperty<nsTArray<RefPtr<nsRange>>>);
|
|
|
|
}
|
|
|
|
|
2019-05-28 14:48:21 +03:00
|
|
|
nsresult nsPrintObject::InitAsRootObject(nsIDocShell* aDocShell, Document* aDoc,
|
|
|
|
bool aForPrintPreview) {
|
|
|
|
NS_ENSURE_STATE(aDocShell);
|
2018-05-11 20:46:15 +03:00
|
|
|
NS_ENSURE_STATE(aDoc);
|
|
|
|
|
2019-05-28 14:48:21 +03:00
|
|
|
if (aForPrintPreview) {
|
2019-06-04 18:01:33 +03: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 07:02:13 +03:00
|
|
|
mDocShell = aDocShell;
|
|
|
|
} else {
|
2019-05-28 14:48:21 +03:00
|
|
|
// When doing an actual print, we create a BrowsingContext/nsDocShell that
|
|
|
|
// is detached from any browser window or tab.
|
2018-11-05 15:43:10 +03:00
|
|
|
|
|
|
|
// Create a new BrowsingContext to create our DocShell in.
|
2020-04-24 21:33:01 +03:00
|
|
|
RefPtr<BrowsingContext> bc = BrowsingContext::CreateIndependent(
|
2020-02-14 19:31:01 +03:00
|
|
|
nsDocShell::Cast(aDocShell)->GetBrowsingContext()->GetType());
|
2018-11-05 15:43:10 +03:00
|
|
|
|
2009-12-11 07:02:13 +03:00
|
|
|
// Create a container docshell for printing.
|
2018-11-05 15:43:10 +03:00
|
|
|
mDocShell = nsDocShell::Create(bc);
|
2009-12-11 07:02:13 +03:00
|
|
|
NS_ENSURE_TRUE(mDocShell, NS_ERROR_OUT_OF_MEMORY);
|
2018-11-05 15:43:10 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mDidCreateDocShell = true;
|
2018-11-05 15:43:10 +03:00
|
|
|
MOZ_ASSERT(mDocShell->ItemType() == aDocShell->ItemType());
|
2019-05-28 14:48:21 +03:00
|
|
|
|
|
|
|
mTreeOwner = do_GetInterface(aDocShell);
|
2013-02-13 02:02:51 +04:00
|
|
|
mDocShell->SetTreeOwner(mTreeOwner);
|
2019-05-28 14:48:21 +03:00
|
|
|
|
|
|
|
// Make sure nsDocShell::EnsureContentViewer() is called:
|
|
|
|
mozilla::Unused << nsDocShell::Cast(mDocShell)->GetDocument();
|
2009-12-11 07:02:13 +03:00
|
|
|
}
|
|
|
|
|
2019-05-28 14:48:21 +03:00
|
|
|
mDocument = aDoc->CreateStaticClone(mDocShell);
|
|
|
|
NS_ENSURE_STATE(mDocument);
|
2020-04-23 19:41:49 +03:00
|
|
|
CachePrintSelectionRanges(*aDoc, *mDocument);
|
2016-07-18 19:37:11 +03:00
|
|
|
|
2006-12-10 11:05:05 +03:00
|
|
|
nsCOMPtr<nsIContentViewer> viewer;
|
2009-12-11 07:02:13 +03:00
|
|
|
mDocShell->GetContentViewer(getter_AddRefs(viewer));
|
2019-09-05 17:55:11 +03:00
|
|
|
NS_ENSURE_STATE(viewer);
|
|
|
|
viewer->SetDocument(mDocument);
|
2009-12-11 07:02:13 +03:00
|
|
|
|
2019-05-28 14:48:21 +03: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 16:01:05 +04:00
|
|
|
|
2020-06-03 00:03:20 +03:00
|
|
|
// "frame" elements not in a frameset context should be treated
|
|
|
|
// as iframes
|
|
|
|
if (mContent->IsHTMLElement(nsGkAtoms::frame) &&
|
|
|
|
mParent->mFrameType == eFrameSet) {
|
|
|
|
mFrameType = eFrame;
|
|
|
|
} else {
|
|
|
|
// Assume something iframe-like, i.e. iframe, object, or embed
|
|
|
|
mFrameType = eIFrame;
|
|
|
|
}
|
|
|
|
|
2002-08-21 16:01:05 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Resets PO by destroying the presentation
|
|
|
|
void nsPrintObject::DestroyPresentation() {
|
2006-12-10 11:05:05 +03:00
|
|
|
if (mPresShell) {
|
|
|
|
mPresShell->EndObservingDocument();
|
2009-05-15 07:08:41 +04:00
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
2019-04-16 10:25:10 +03:00
|
|
|
RefPtr<PresShell> presShell = mPresShell;
|
2013-01-16 00:47:10 +04:00
|
|
|
mPresShell = nullptr;
|
2019-04-16 10:25:10 +03:00
|
|
|
presShell->Destroy();
|
2006-12-10 11:05:05 +03:00
|
|
|
}
|
2012-12-18 08:32:34 +04:00
|
|
|
mPresContext = nullptr;
|
2012-07-30 18:20:58 +04:00
|
|
|
mViewManager = nullptr;
|
2002-08-21 16:01:05 +04:00
|
|
|
}
|
2020-05-22 12:56:55 +03:00
|
|
|
|
2020-05-22 12:57:12 +03:00
|
|
|
void nsPrintObject::EnablePrinting(bool aEnable) {
|
2020-05-31 22:00:42 +03:00
|
|
|
mPrintingIsEnabled = aEnable;
|
2020-05-22 12:57:12 +03:00
|
|
|
|
|
|
|
for (const UniquePtr<nsPrintObject>& kid : mKids) {
|
|
|
|
kid->EnablePrinting(aEnable);
|
|
|
|
}
|
|
|
|
}
|