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),
|
|
|
|
mDontPrint(true),
|
|
|
|
mPrintAsIs(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
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------
|
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-02-06 22:08:06 +03:00
|
|
|
RefPtr<BrowsingContext> bc = BrowsingContext::CreateWindowless(
|
2018-11-27 12:59:44 +03:00
|
|
|
/* aParent */ nullptr,
|
|
|
|
/* aOpener */ nullptr, EmptyString(),
|
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);
|
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
|
|
|
|
|
|
|
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
|
|
|
}
|