Source code

Revision control

Other Tools

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
/* container for a document and its presentation */
#include "gfxContext.h"
#include "mozilla/PresShell.h"
#include "mozilla/RestyleManager.h"
#include "mozilla/ServoStyleSet.h"
#include "mozilla/StaticPrefs_print.h"
#include "mozilla/Telemetry.h"
#include "nsThreadUtils.h"
#include "nscore.h"
#include "nsCOMPtr.h"
#include "nsCRT.h"
#include "nsFrameSelection.h"
#include "nsString.h"
#include "nsReadableUtils.h"
#include "nsIContent.h"
#include "nsIContentViewer.h"
#include "nsIDocumentViewerPrint.h"
#include "nsIScreen.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/BeforeUnloadEvent.h"
#include "mozilla/dom/PopupBlocker.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/DocumentInlines.h"
#include "mozilla/dom/DocGroup.h"
#include "nsPresContext.h"
#include "nsIFrame.h"
#include "nsIWritablePropertyBag2.h"
#include "nsSubDocumentFrame.h"
#include "nsGenericHTMLElement.h"
#include "nsStubMutationObserver.h"
#include "nsISelectionListener.h"
#include "mozilla/dom/Selection.h"
#include "nsContentUtils.h"
#ifdef ACCESSIBILITY
# include "mozilla/a11y/DocAccessible.h"
#endif
#include "mozilla/BasicEvents.h"
#include "mozilla/Encoding.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/Preferences.h"
#include "mozilla/WeakPtr.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/StaticPrefs_javascript.h"
#include "mozilla/StaticPrefs_fission.h"
#include "mozilla/StaticPrefs_print.h"
#include "mozilla/StyleSheet.h"
#include "mozilla/StyleSheetInlines.h"
#include "nsViewManager.h"
#include "nsView.h"
#include "nsPageSequenceFrame.h"
#include "nsNetUtil.h"
#include "nsIContentViewerEdit.h"
#include "mozilla/css/Loader.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsDocShell.h"
#include "nsIBaseWindow.h"
#include "nsILayoutHistoryState.h"
#include "nsIScreen.h"
#include "nsCharsetSource.h"
#include "mozilla/ReflowInput.h"
#include "nsIImageLoadingContent.h"
#include "nsCopySupport.h"
#ifdef MOZ_XUL
# include "nsXULPopupManager.h"
#endif
#include "nsIClipboardHelper.h"
#include "nsPIDOMWindow.h"
#include "nsGlobalWindow.h"
#include "nsDOMNavigationTiming.h"
#include "nsPIWindowRoot.h"
#include "nsJSEnvironment.h"
#include "nsFocusManager.h"
#include "nsIScrollableFrame.h"
#include "nsStyleSheetService.h"
#include "nsILoadContext.h"
#include "mozilla/ThrottledEventQueue.h"
#include "nsIPromptCollection.h"
#include "nsIPromptService.h"
#include "imgIContainer.h" // image animation mode constants
#include "nsSandboxFlags.h"
#include "mozilla/DocLoadingTimelineMarker.h"
//--------------------------
// Printing Include
//---------------------------
#ifdef NS_PRINTING
# include "nsIWebBrowserPrint.h"
# include "nsPrintJob.h"
// Print Options
# include "nsIPrintSettings.h"
# include "nsIPrintSettingsService.h"
# include "nsISimpleEnumerator.h"
# include "nsIPluginDocument.h"
#endif // NS_PRINTING
// focus
#include "nsIDOMEventListener.h"
#include "nsISelectionController.h"
#include "mozilla/EventDispatcher.h"
#include "nsISHEntry.h"
#include "nsISHistory.h"
#include "nsIWebNavigation.h"
#include "mozilla/dom/XMLHttpRequestMainThread.h"
// paint forcing
#include <stdio.h>
#include "mozilla/BasePrincipal.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/Event.h"
#include "mozilla/Telemetry.h"
#include "mozilla/dom/ScriptLoader.h"
#include "mozilla/dom/WindowGlobalChild.h"
namespace mozilla {
namespace dom {
class PrintPreviewResultInfo;
} // namespace dom
} // namespace mozilla
using namespace mozilla;
using namespace mozilla::dom;
using PrintPreviewResolver =
std::function<void(const mozilla::dom::PrintPreviewResultInfo&)>;
//-----------------------------------------------------
// LOGGING
#include "LayoutLogging.h"
#include "mozilla/Logging.h"
extern mozilla::LazyLogModule gPageCacheLog;
#ifdef NS_PRINTING
static mozilla::LazyLogModule gPrintingLog("printing");
# define PR_PL(_p1) MOZ_LOG(gPrintingLog, mozilla::LogLevel::Debug, _p1);
#endif // NS_PRINTING
#define PRT_YESNO(_p) ((_p) ? "YES" : "NO")
//-----------------------------------------------------
class nsDocumentViewer;
// a small delegate class used to avoid circular references
class nsDocViewerSelectionListener final : public nsISelectionListener {
public:
// nsISupports interface...
NS_DECL_ISUPPORTS
// nsISelectionListerner interface
NS_DECL_NSISELECTIONLISTENER
explicit nsDocViewerSelectionListener(nsDocumentViewer* aDocViewer)
: mDocViewer(aDocViewer), mSelectionWasCollapsed(true) {}
void Disconnect() { mDocViewer = nullptr; }
protected:
virtual ~nsDocViewerSelectionListener() = default;
nsDocumentViewer* mDocViewer;
bool mSelectionWasCollapsed;
};
/** editor Implementation of the FocusListener interface */
class nsDocViewerFocusListener final : public nsIDOMEventListener {
public:
explicit nsDocViewerFocusListener(nsDocumentViewer* aDocViewer)
: mDocViewer(aDocViewer) {}
NS_DECL_ISUPPORTS
NS_DECL_NSIDOMEVENTLISTENER
void Disconnect() { mDocViewer = nullptr; }
protected:
virtual ~nsDocViewerFocusListener() = default;
nsDocumentViewer* mDocViewer;
};
namespace viewer_detail {
/**
* Mutation observer for use until we hand ourselves over to our SHEntry.
*/
class BFCachePreventionObserver final : public nsStubMutationObserver {
public:
explicit BFCachePreventionObserver(Document* aDocument)
: mDocument(aDocument) {}
NS_DECL_ISUPPORTS
NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED
NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
NS_DECL_NSIMUTATIONOBSERVER_NODEWILLBEDESTROYED
// Stop observing the document.
void Disconnect();
private:
~BFCachePreventionObserver() = default;
// Helper for the work that needs to happen when mutations happen.
void MutationHappened();
Document* mDocument; // Weak; we get notified if it dies
};
NS_IMPL_ISUPPORTS(BFCachePreventionObserver, nsIMutationObserver)
void BFCachePreventionObserver::CharacterDataChanged(
nsIContent* aContent, const CharacterDataChangeInfo&) {
MutationHappened();
}
void BFCachePreventionObserver::AttributeChanged(Element* aElement,
int32_t aNameSpaceID,
nsAtom* aAttribute,
int32_t aModType,
const nsAttrValue* aOldValue) {
MutationHappened();
}
void BFCachePreventionObserver::ContentAppended(nsIContent* aFirstNewContent) {
MutationHappened();
}
void BFCachePreventionObserver::ContentInserted(nsIContent* aChild) {
MutationHappened();
}
void BFCachePreventionObserver::ContentRemoved(nsIContent* aChild,
nsIContent* aPreviousSibling) {
MutationHappened();
}
void BFCachePreventionObserver::NodeWillBeDestroyed(const nsINode* aNode) {
mDocument = nullptr;
}
void BFCachePreventionObserver::Disconnect() {
if (mDocument) {
mDocument->RemoveMutationObserver(this);
// It will no longer tell us when it goes away, so make sure we're
// not holding a dangling ref.
mDocument = nullptr;
}
}
void BFCachePreventionObserver::MutationHappened() {
MOZ_ASSERT(
mDocument,
"How can we not have a document but be getting notified for mutations?");
mDocument->DisallowBFCaching();
Disconnect();
}
} // namespace viewer_detail
using viewer_detail::BFCachePreventionObserver;
//-------------------------------------------------------------
class nsDocumentViewer final : public nsIContentViewer,
public nsIContentViewerEdit,
public nsIDocumentViewerPrint
#ifdef NS_PRINTING
,
public nsIWebBrowserPrint
#endif
{
friend class nsDocViewerSelectionListener;
friend class nsPagePrintTimer;
friend class nsPrintJob;
public:
nsDocumentViewer();
// nsISupports interface...
NS_DECL_ISUPPORTS
// nsIContentViewer interface...
NS_DECL_NSICONTENTVIEWER
// nsIContentViewerEdit
NS_DECL_NSICONTENTVIEWEREDIT
#ifdef NS_PRINTING
// nsIWebBrowserPrint
NS_DECL_NSIWEBBROWSERPRINT
#endif
using CallChildFunc = FunctionRef<void(nsDocumentViewer*)>;
void CallChildren(CallChildFunc aFunc);
using PresContextFunc = FunctionRef<void(nsPresContext*)>;
/**
* Calls a `CallChildFunc` on all children, a `PresContextFunc`
* on all external documents' pres contexts of our document, and then
* finally on _this_ pres context, in that order.
*
* The children function is expected to call this function reentrantly, and
* thus the `PresContextFunc` won't be called for the children's pres context
* directly here.
*
* FIXME(emilio): Better name for this appreciated.
*/
void PropagateToPresContextsHelper(CallChildFunc, PresContextFunc);
// nsIDocumentViewerPrint Printing Methods
NS_DECL_NSIDOCUMENTVIEWERPRINT
void EmulateMediumInternal(nsAtom*);
using ColorSchemeOverride = Maybe<StylePrefersColorScheme>;
void EmulatePrefersColorSchemeInternal(const ColorSchemeOverride&);
protected:
virtual ~nsDocumentViewer();
private:
/**
* Creates a view manager, root view, and widget for the root view, setting
* mViewManager and mWindow.
* @param aSize the initial size in appunits
* @param aContainerView the container view to hook our root view up
* to as a child, or null if this will be the root view manager
*/
nsresult MakeWindow(const nsSize& aSize, nsView* aContainerView);
/**
* Create our device context
*/
nsresult CreateDeviceContext(nsView* aContainerView);
/**
* If aDoCreation is true, this creates the device context, creates a
* prescontext if necessary, and calls MakeWindow.
*
* If aForceSetNewDocument is false, then SetNewDocument won't be
* called if the window's current document is already mDocument.
*/
nsresult InitInternal(nsIWidget* aParentWidget, nsISupports* aState,
mozilla::dom::WindowGlobalChild* aActor,
const nsIntRect& aBounds, bool aDoCreation,
bool aNeedMakeCX = true,
bool aForceSetNewDocument = true);
/**
* @param aDoInitialReflow set to true if you want to kick off the initial
* reflow
*/
MOZ_CAN_RUN_SCRIPT_BOUNDARY
nsresult InitPresentationStuff(bool aDoInitialReflow);
already_AddRefed<nsINode> GetPopupNode();
already_AddRefed<nsINode> GetPopupLinkNode();
already_AddRefed<nsIImageLoadingContent> GetPopupImageNode();
MOZ_CAN_RUN_SCRIPT_BOUNDARY
nsresult GetContentSizeInternal(int32_t* aWidth, int32_t* aHeight,
nscoord aMaxWidth, nscoord aMaxHeight);
void PrepareToStartLoad(void);
nsresult SyncParentSubDocMap();
void RemoveFocusListener();
void ReinitializeFocusListener();
mozilla::dom::Selection* GetDocumentSelection();
void DestroyPresShell();
void DestroyPresContext();
void InvalidatePotentialSubDocDisplayItem();
// Whether we should attach to the top level widget. This is true if we
// are sharing/recycling a single base widget and not creating multiple
// child widgets.
bool ShouldAttachToTopLevel();
nsresult PrintPreviewScrollToPageForOldUI(int16_t aType, int32_t aPageNum);
std::tuple<const nsIFrame*, int32_t> GetCurrentSheetFrameAndPageNumber()
const;
protected:
// Returns the current viewmanager. Might be null.
nsViewManager* GetViewManager();
void DetachFromTopLevelWidget();
// IMPORTANT: The ownership implicit in the following member
// variables has been explicitly checked and set using nsCOMPtr
// for owning pointers and raw COM interface pointers for weak
// (ie, non owning) references. If you add any members to this
// class, please make the ownership explicit (pinkerton, scc).
WeakPtr<nsDocShell> mContainer; // it owns me!
RefPtr<nsDeviceContext> mDeviceContext; // We create and own this baby
// the following six items are explicitly in this order
// so they will be destroyed in the reverse order (pinkerton, scc)
nsCOMPtr<Document> mDocument;
nsCOMPtr<nsIWidget> mWindow; // may be null
RefPtr<nsViewManager> mViewManager;
RefPtr<nsPresContext> mPresContext;
RefPtr<PresShell> mPresShell;
RefPtr<nsDocViewerSelectionListener> mSelectionListener;
RefPtr<nsDocViewerFocusListener> mFocusListener;
nsCOMPtr<nsIContentViewer> mPreviousViewer;
nsCOMPtr<nsISHEntry> mSHEntry;
// Observer that will prevent bfcaching if it gets notified. This
// is non-null precisely when mSHEntry is non-null.
RefPtr<BFCachePreventionObserver> mBFCachePreventionObserver;
nsIWidget* mParentWidget; // purposely won't be ref counted. May be null
bool mAttachedToParent; // view is attached to the parent widget
nsIntRect mBounds;
float mOverrideDPPX; // DPPX overrided, defaults to 0.0
int16_t mNumURLStarts;
int16_t mDestroyBlockedCount;
unsigned mStopped : 1;
unsigned mLoaded : 1;
unsigned mDeferredWindowClose : 1;
// document management data
// these items are specific to markup documents (html and xml)
// may consider splitting these out into a subclass
unsigned mIsSticky : 1;
unsigned mInPermitUnload : 1;
unsigned mInPermitUnloadPrompt : 1;
#ifdef NS_PRINTING
unsigned mClosingWhilePrinting : 1;
# if NS_PRINT_PREVIEW
RefPtr<nsPrintJob> mPrintJob;
# endif // NS_PRINT_PREVIEW
#endif // NS_PRINTING
/* character set member data */
int32_t mHintCharsetSource;
const Encoding* mHintCharset;
bool mIsPageMode;
bool mInitializedForPrintPreview;
bool mHidden;
};
class nsDocumentShownDispatcher : public Runnable {
public:
explicit nsDocumentShownDispatcher(nsCOMPtr<Document> aDocument)
: Runnable("nsDocumentShownDispatcher"), mDocument(aDocument) {}
NS_IMETHOD Run() override;
private:
nsCOMPtr<Document> mDocument;
};
//------------------------------------------------------------------
// nsDocumentViewer
//------------------------------------------------------------------
//------------------------------------------------------------------
already_AddRefed<nsIContentViewer> NS_NewContentViewer() {
RefPtr<nsDocumentViewer> viewer = new nsDocumentViewer();
return viewer.forget();
}
void nsDocumentViewer::PrepareToStartLoad() {
MOZ_DIAGNOSTIC_ASSERT(!GetIsPrintPreview(),
"Print preview tab should never navigate");
mStopped = false;
mLoaded = false;
mAttachedToParent = false;
mDeferredWindowClose = false;
#ifdef NS_PRINTING
mClosingWhilePrinting = false;
// Make sure we have destroyed it and cleared the data member
if (mPrintJob) {
mPrintJob->Destroy();
mPrintJob = nullptr;
}
#endif // NS_PRINTING
}
nsDocumentViewer::nsDocumentViewer()
: mParentWidget(nullptr),
mAttachedToParent(false),
mOverrideDPPX(0.0),
mNumURLStarts(0),
mDestroyBlockedCount(0),
mStopped(false),
mLoaded(false),
mDeferredWindowClose(false),
mIsSticky(true),
mInPermitUnload(false),
mInPermitUnloadPrompt(false),
#ifdef NS_PRINTING
mClosingWhilePrinting(false),
#endif // NS_PRINTING
mHintCharsetSource(kCharsetUninitialized),
mHintCharset(nullptr),
mIsPageMode(false),
mInitializedForPrintPreview(false),
mHidden(false) {
PrepareToStartLoad();
}
NS_IMPL_ADDREF(nsDocumentViewer)
NS_IMPL_RELEASE(nsDocumentViewer)
NS_INTERFACE_MAP_BEGIN(nsDocumentViewer)
NS_INTERFACE_MAP_ENTRY(nsIContentViewer)
NS_INTERFACE_MAP_ENTRY(nsIContentViewerEdit)
NS_INTERFACE_MAP_ENTRY(nsIDocumentViewerPrint)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIContentViewer)
#ifdef NS_PRINTING
NS_INTERFACE_MAP_ENTRY(nsIWebBrowserPrint)
#endif
NS_INTERFACE_MAP_END
nsDocumentViewer::~nsDocumentViewer() {
if (mDocument) {
Close(nullptr);
mDocument->Destroy();
}
#ifdef NS_PRINTING
if (mPrintJob) {
mPrintJob->Destroy();
mPrintJob = nullptr;
}
#endif
MOZ_RELEASE_ASSERT(mDestroyBlockedCount == 0);
NS_ASSERTION(!mPresShell && !mPresContext,
"User did not call nsIContentViewer::Destroy");
if (mPresShell || mPresContext) {
// Make sure we don't hand out a reference to the content viewer to
// the SHEntry!
mSHEntry = nullptr;
Destroy();
}
if (mSelectionListener) {
mSelectionListener->Disconnect();
}
RemoveFocusListener();
// XXX(?) Revoke pending invalidate events
}
/*
* This method is called by the Document Loader once a document has
* been created for a particular data stream... The content viewer
* must cache this document for later use when Init(...) is called.
*
* This method is also called when an out of band document.write() happens.
* In that case, the document passed in is the same as the previous document.
*/
/* virtual */
void nsDocumentViewer::LoadStart(Document* aDocument) {
MOZ_ASSERT(aDocument);
if (!mDocument) {
mDocument = aDocument;
}
}
void nsDocumentViewer::RemoveFocusListener() {
if (RefPtr<nsDocViewerFocusListener> oldListener =
std::move(mFocusListener)) {
oldListener->Disconnect();
if (mDocument) {
mDocument->RemoveEventListener(u"focus"_ns, oldListener, false);
mDocument->RemoveEventListener(u"blur"_ns, oldListener, false);
}
}
}
void nsDocumentViewer::ReinitializeFocusListener() {
RemoveFocusListener();
mFocusListener = new nsDocViewerFocusListener(this);
if (mDocument) {
mDocument->AddEventListener(u"focus"_ns, mFocusListener, false, false);
mDocument->AddEventListener(u"blur"_ns, mFocusListener, false, false);
}
}
nsresult nsDocumentViewer::SyncParentSubDocMap() {
nsCOMPtr<nsIDocShell> docShell(mContainer);
if (!docShell) {
return NS_OK;
}
nsCOMPtr<nsPIDOMWindowOuter> pwin(docShell->GetWindow());
if (!mDocument || !pwin) {
return NS_OK;
}
nsCOMPtr<Element> element = pwin->GetFrameElementInternal();
if (!element) {
return NS_OK;
}
nsCOMPtr<nsIDocShellTreeItem> parent;
docShell->GetInProcessParent(getter_AddRefs(parent));
nsCOMPtr<nsPIDOMWindowOuter> parent_win =
parent ? parent->GetWindow() : nullptr;
if (!parent_win) {
return NS_OK;
}
nsCOMPtr<Document> parent_doc = parent_win->GetDoc();
if (!parent_doc) {
return NS_OK;
}
if (mDocument && parent_doc->GetSubDocumentFor(element) != mDocument &&
parent_doc->EventHandlingSuppressed()) {
mDocument->SuppressEventHandling(parent_doc->EventHandlingSuppressed());
}
return parent_doc->SetSubDocumentFor(element, mDocument);
}
NS_IMETHODIMP
nsDocumentViewer::SetContainer(nsIDocShell* aContainer) {
mContainer = static_cast<nsDocShell*>(aContainer);
// We're loading a new document into the window where this document
// viewer lives, sync the parent document's frame element -> sub
// document map
return SyncParentSubDocMap();
}
NS_IMETHODIMP
nsDocumentViewer::GetContainer(nsIDocShell** aResult) {
NS_ENSURE_ARG_POINTER(aResult);
nsCOMPtr<nsIDocShell> container(mContainer);
container.swap(*aResult);
return NS_OK;
}
NS_IMETHODIMP
nsDocumentViewer::Init(nsIWidget* aParentWidget, const nsIntRect& aBounds,
WindowGlobalChild* aActor) {
return InitInternal(aParentWidget, nullptr, aActor, aBounds, true);
}
nsresult nsDocumentViewer::InitPresentationStuff(bool aDoInitialReflow) {
// We assert this because initializing the pres shell could otherwise cause
// re-entrancy into nsDocumentViewer methods, which might cause a different
// pres shell to be created. Callers of InitPresentationStuff should ensure
// the call is appropriately bounded by an nsAutoScriptBlocker to decide
// when it is safe for these re-entrant calls to be made.
MOZ_ASSERT(!nsContentUtils::IsSafeToRunScript(),
"InitPresentationStuff must only be called when scripts are "
"blocked");
#ifdef NS_PRINTING
// When getting printed, either for print or print preview, the print job
// takes care of setting up the presentation of the document.
if (mPrintJob) {
return NS_OK;
}
#endif
NS_ASSERTION(!mPresShell, "Someone should have destroyed the presshell!");
// Now make the shell for the document
mPresShell = mDocument->CreatePresShell(mPresContext, mViewManager);
if (!mPresShell) {
return NS_ERROR_FAILURE;
}
if (aDoInitialReflow) {
// Since Initialize() will create frames for *all* items
// that are currently in the document tree, we need to flush
// any pending notifications to prevent the content sink from
// duplicating layout frames for content it has added to the tree
// but hasn't notified the document about. (Bug 154018)
//
// Note that we are flushing before we add mPresShell as an observer
// to avoid bogus notifications.
mDocument->FlushPendingNotifications(FlushType::ContentAndNotify);
}
mPresShell->BeginObservingDocument();
// Initialize our view manager
{
int32_t p2a = mPresContext->AppUnitsPerDevPixel();
MOZ_ASSERT(
p2a ==
mPresContext->DeviceContext()->AppUnitsPerDevPixelAtUnitFullZoom());
nscoord width = p2a * mBounds.width;
nscoord height = p2a * mBounds.height;
mViewManager->SetWindowDimensions(width, height);
mPresContext->SetVisibleArea(nsRect(0, 0, width, height));
// We rely on the default zoom not being initialized until here.
mPresContext->RecomputeBrowsingContextDependentData();
mPresContext->SetOverrideDPPX(mOverrideDPPX);
}
if (mWindow && mDocument->IsTopLevelContentDocument()) {
// Set initial safe area insets
ScreenIntMargin windowSafeAreaInsets;
LayoutDeviceIntRect windowRect = mWindow->GetScreenBounds();
nsCOMPtr<nsIScreen> screen = mWindow->GetWidgetScreen();
if (screen) {
windowSafeAreaInsets = nsContentUtils::GetWindowSafeAreaInsets(
screen, mWindow->GetSafeAreaInsets(), windowRect);
}
mPresContext->SetSafeAreaInsets(windowSafeAreaInsets);
}
if (aDoInitialReflow) {
RefPtr<PresShell> presShell = mPresShell;
// Initial reflow
presShell->Initialize();
}
// now register ourselves as a selection listener, so that we get
// called when the selection changes in the window
if (!mSelectionListener) {
mSelectionListener = new nsDocViewerSelectionListener(this);
}
RefPtr<mozilla::dom::Selection> selection = GetDocumentSelection();
if (!selection) {
return NS_ERROR_FAILURE;
}
selection->AddSelectionListener(mSelectionListener);
ReinitializeFocusListener();
if (aDoInitialReflow && mDocument) {
nsCOMPtr<Document> document = mDocument;
document->ScrollToRef();
}
return NS_OK;
}
static nsPresContext* CreatePresContext(Document* aDocument,
nsPresContext::nsPresContextType aType,
nsView* aContainerView) {
if (aContainerView) return new nsPresContext(aDocument, aType);
return new nsRootPresContext(aDocument, aType);
}
//-----------------------------------------------
// This method can be used to initial the "presentation"
// The aDoCreation indicates whether it should create
// all the new objects or just initialize the existing ones
nsresult nsDocumentViewer::InitInternal(
nsIWidget* aParentWidget, nsISupports* aState, WindowGlobalChild* aActor,
const nsIntRect& aBounds, bool aDoCreation, bool aNeedMakeCX /*= true*/,
bool aForceSetNewDocument /* = true*/) {
// We don't want any scripts to run here. That can cause flushing,
// which can cause reentry into initialization of this document viewer,
// which would be disastrous.
nsAutoScriptBlocker blockScripts;
mParentWidget = aParentWidget; // not ref counted
mBounds = aBounds;
nsresult rv = NS_OK;
NS_ENSURE_TRUE(mDocument, NS_ERROR_NULL_POINTER);
nsView* containerView = FindContainerView();
bool makeCX = false;
if (aDoCreation) {
nsresult rv = CreateDeviceContext(containerView);
NS_ENSURE_SUCCESS(rv, rv);
// XXXbz this is a nasty hack to do with the fact that we create
// presentations both in Init() and in Show()... Ideally we would only do
// it in one place (Show()) and require that callers call init(), open(),
// show() in that order or something.
if (!mPresContext &&
(aParentWidget || containerView || mDocument->IsBeingUsedAsImage() ||
(mDocument->GetDisplayDocument() &&
mDocument->GetDisplayDocument()->GetPresShell()))) {
// Create presentation context
if (mIsPageMode) {
// Presentation context already created in SetPageModeForTesting which
// is calling this method
} else {
mPresContext = CreatePresContext(
mDocument, nsPresContext::eContext_Galley, containerView);
}
NS_ENSURE_TRUE(mPresContext, NS_ERROR_OUT_OF_MEMORY);
nsresult rv = mPresContext->Init(mDeviceContext);
if (NS_FAILED(rv)) {
mPresContext = nullptr;
return rv;
}
#if defined(NS_PRINTING) && defined(NS_PRINT_PREVIEW)
makeCX = !GetIsPrintPreview() &&
aNeedMakeCX; // needs to be true except when we are already in
// PP or we are enabling/disabling paginated mode.
#else
makeCX = true;
#endif
}
if (mPresContext) {
// Create the ViewManager and Root View...
// We must do this before we tell the script global object about
// this new document since doing that will cause us to re-enter
// into nsSubDocumentFrame code through reflows caused by
// FlushPendingNotifications() calls down the road...
rv = MakeWindow(nsSize(mPresContext->DevPixelsToAppUnits(aBounds.width),
mPresContext->DevPixelsToAppUnits(aBounds.height)),
containerView);
NS_ENSURE_SUCCESS(rv, rv);
Hide();
#ifdef NS_PRINT_PREVIEW
if (mIsPageMode) {
// I'm leaving this in a broken state for the moment; we should
// be measuring/scaling with the print device context, not the
// screen device context, but this is good enough to allow
// printing reftests to work.
double pageWidth = 0, pageHeight = 0;
mPresContext->GetPrintSettings()->GetEffectivePageSize(&pageWidth,
&pageHeight);
mPresContext->SetPageSize(
nsSize(mPresContext->CSSTwipsToAppUnits(NSToIntFloor(pageWidth)),
mPresContext->CSSTwipsToAppUnits(NSToIntFloor(pageHeight))));
mPresContext->SetIsRootPaginatedDocument(true);
mPresContext->SetPageScale(1.0f);
}
#endif
} else {
// Avoid leaking the old viewer.
if (mPreviousViewer) {
mPreviousViewer->Destroy();
mPreviousViewer = nullptr;
}
}
}
nsCOMPtr<nsIInterfaceRequestor> requestor(mContainer);
if (requestor) {
// Set script-context-owner in the document
nsCOMPtr<nsPIDOMWindowOuter> window = do_GetInterface(requestor);
if (window) {
nsCOMPtr<Document> curDoc = window->GetExtantDoc();
if (aForceSetNewDocument || curDoc != mDocument) {
rv = window->SetNewDocument(mDocument, aState, false, aActor);
if (NS_FAILED(rv)) {
Destroy();
return rv;
}
}
}
}
if (aDoCreation && mPresContext) {
// The ViewManager and Root View was created above (in
// MakeWindow())...
rv = InitPresentationStuff(!makeCX);
}
return rv;
}
void nsDocumentViewer::SetNavigationTiming(nsDOMNavigationTiming* timing) {
NS_ASSERTION(mDocument, "Must have a document to set navigation timing.");
if (mDocument) {
mDocument->SetNavigationTiming(timing);
}
}
//
// LoadComplete(aStatus)
//
// aStatus - The status returned from loading the document.
//
// This method is called by the container when the document has been
// completely loaded.
//
NS_IMETHODIMP
nsDocumentViewer::LoadComplete(nsresult aStatus) {
/* We need to protect ourself against auto-destruction in case the
window is closed while processing the OnLoad event. See bug
explanation.
*/
RefPtr<nsDocumentViewer> kungFuDeathGrip(this);
// Flush out layout so it's up-to-date by the time onload is called.
// Note that this could destroy the window, so do this before
// checking for our mDocument and its window.
if (mPresShell && !mStopped) {
// Hold strong ref because this could conceivably run script
RefPtr<PresShell> presShell = mPresShell;
presShell->FlushPendingNotifications(FlushType::Layout);
}
nsresult rv = NS_OK;
NS_ENSURE_TRUE(mDocument, NS_ERROR_NOT_AVAILABLE);
// First, get the window from the document...
nsCOMPtr<nsPIDOMWindowOuter> window = mDocument->GetWindow();
mLoaded = true;
// Now, fire either an OnLoad or OnError event to the document...
bool restoring = false;
// XXXbz imagelib kills off the document load for a full-page image with
// NS_ERROR_PARSED_DATA_CACHED if it's in the cache. So we want to treat
// that one as a success code; otherwise whether we fire onload for the image
// will depend on whether it's cached!
if (window &&
(NS_SUCCEEDED(aStatus) || aStatus == NS_ERROR_PARSED_DATA_CACHED)) {
// If this code changes, the code in nsDocLoader::DocLoaderIsEmpty
// that fires load events for document.open() cases might need to
// be updated too.
nsEventStatus status = nsEventStatus_eIgnore;
WidgetEvent event(true, eLoad);
event.mFlags.mBubbles = false;
event.mFlags.mCancelable = false;
// XXX Dispatching to |window|, but using |document| as the target.
event.mTarget = mDocument;
// If the document presentation is being restored, we don't want to fire
// onload to the document content since that would likely confuse scripts
// on the page.
nsIDocShell* docShell = window->GetDocShell();
NS_ENSURE_TRUE(docShell, NS_ERROR_UNEXPECTED);
// Unfortunately, docShell->GetRestoringDocument() might no longer be set
// correctly. In particular, it can be false by now if someone took it upon
// themselves to block onload from inside restoration and unblock it later.
// But we can detect the restoring case very simply: by whether our
// document's readyState is COMPLETE.
restoring =
(mDocument->GetReadyStateEnum() == Document::READYSTATE_COMPLETE);
if (!restoring) {
NS_ASSERTION(
mDocument->GetReadyStateEnum() == Document::READYSTATE_INTERACTIVE ||
// test_stricttransportsecurity.html has old-style
// docshell-generated about:blank docs reach this code!
(mDocument->GetReadyStateEnum() ==
Document::READYSTATE_UNINITIALIZED &&
NS_IsAboutBlank(mDocument->GetDocumentURI())),
"Bad readystate");
#ifdef DEBUG
bool docShellThinksWeAreRestoring;
docShell->GetRestoringDocument(&docShellThinksWeAreRestoring);
MOZ_ASSERT(!docShellThinksWeAreRestoring,
"How can docshell think we are restoring if we don't have a "
"READYSTATE_COMPLETE document?");
#endif // DEBUG
nsCOMPtr<Document> d = mDocument;
mDocument->SetReadyStateInternal(Document::READYSTATE_COMPLETE);
RefPtr<nsDOMNavigationTiming> timing(d->GetNavigationTiming());
if (timing) {
timing->NotifyLoadEventStart();
}
// Dispatch observer notification to notify observers document load is
// complete.
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
if (os) {
nsIPrincipal* principal = d->NodePrincipal();
os->NotifyObservers(ToSupports(d),
principal->IsSystemPrincipal()
? "chrome-document-loaded"
: "content-document-loaded",
nullptr);
}
// Notify any devtools about the load.
RefPtr<TimelineConsumers> timelines = TimelineConsumers::Get();
if (timelines && timelines->HasConsumer(docShell)) {
timelines->AddMarkerForDocShell(
docShell, MakeUnique<DocLoadingTimelineMarker>("document::Load"));
}
nsPIDOMWindowInner* innerWindow = window->GetCurrentInnerWindow();
RefPtr<DocGroup> docGroup = d->GetDocGroup();
// It is possible that the parent document's load event fires earlier than
// childs' load event, and in this case we need to fire some artificial
// load events to make the parent thinks the load events for child has
// been done
if (innerWindow && DocGroup::TryToLoadIframesInBackground()) {
nsTArray<nsCOMPtr<nsIDocShell>> docShells;
nsCOMPtr<nsIDocShell> container(mContainer);
if (container) {
int32_t count;
container->GetInProcessChildCount(&count);
// We first find all background loading iframes that need to
// fire artificial load events, and instead of firing them as
// soon as we find them, we store them in an array, to prevent
// us from skipping some events.
for (int32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIDocShellTreeItem> child;
container->GetInProcessChildAt(i, getter_AddRefs(child));
nsCOMPtr<nsIDocShell> childIDocShell = do_QueryInterface(child);
RefPtr<nsDocShell> docShell = nsDocShell::Cast(childIDocShell);
if (docShell && docShell->TreatAsBackgroundLoad() &&
docShell->GetDocument()->GetReadyStateEnum() <
Document::READYSTATE_COMPLETE) {
docShells.AppendElement(childIDocShell);
}
}
// Re-iterate the stored docShells to fire artificial load events
for (size_t i = 0; i < docShells.Length(); ++i) {
RefPtr<nsDocShell> docShell = nsDocShell::Cast(docShells[i]);
if (docShell && docShell->TreatAsBackgroundLoad() &&
docShell->GetDocument()->GetReadyStateEnum() <
Document::READYSTATE_COMPLETE) {
nsEventStatus status = nsEventStatus_eIgnore;
WidgetEvent event(true, eLoad);
event.mFlags.mBubbles = false;
event.mFlags.mCancelable = false;
nsCOMPtr<nsPIDOMWindowOuter> win = docShell->GetWindow();
nsCOMPtr<Element> element = win->GetFrameElementInternal();
docShell->SetFakeOnLoadDispatched();
EventDispatcher::Dispatch(element, nullptr, &event, nullptr,
&status);
}
}
}
}
d->SetLoadEventFiring(true);
EventDispatcher::Dispatch(window, mPresContext, &event, nullptr, &status);
d->SetLoadEventFiring(false);
RefPtr<nsDocShell> dShell = nsDocShell::Cast(docShell);
if (docGroup && dShell->TreatAsBackgroundLoad()) {
docGroup->TryFlushIframePostMessages(dShell->GetOuterWindowID());
}
if (timing) {
timing->NotifyLoadEventEnd();
}
if (innerWindow) {
innerWindow->QueuePerformanceNavigationTiming();
}
}
} else {
// XXX: Should fire error event to the document...
// If our load was explicitly aborted, then we want to set our
// readyState to COMPLETE, and fire a readystatechange event.
if (aStatus == NS_BINDING_ABORTED && mDocument) {
mDocument->NotifyAbortedLoad();
}
}
// Notify the document that it has been shown (regardless of whether
// it was just loaded). Note: mDocument may be null now if the above
// firing of onload caused the document to unload. Or, mDocument may not be
// the "current active" document, if the above firing of onload caused our
// docshell to navigate away. NOTE: In this latter scenario, it's likely that
// we fired pagehide (when navigating away) without ever having fired
// pageshow, and that's pretty broken... Fortunately, this should be rare.
// (It requires us to spin the event loop in onload handler, e.g. via sync
// XHR, in order for the navigation-away to happen before onload completes.)
// We skip firing pageshow if we're currently handling unload, or if loading
// was explicitly aborted.
if (mDocument && mDocument->IsCurrentActiveDocument() &&
aStatus != NS_BINDING_ABORTED) {
// Re-get window, since it might have changed during above firing of onload
window = mDocument->GetWindow();
if (window) {
nsIDocShell* docShell = window->GetDocShell();
bool isInUnload;
if (docShell && NS_SUCCEEDED(docShell->GetIsInUnload(&isInUnload)) &&
!isInUnload) {
mDocument->OnPageShow(restoring, nullptr);
}
}
}
if (!mStopped) {
if (mDocument) {
nsCOMPtr<Document> document = mDocument;
document->ScrollToRef();
}
// Now that the document has loaded, we can tell the presshell
// to unsuppress painting.
if (mPresShell) {
RefPtr<PresShell> presShell = mPresShell;
presShell->UnsuppressPainting();
// mPresShell could have been removed now, see bug 378682/421432
if (mPresShell) {
mPresShell->LoadComplete();
}
}
}
// Release the JS bytecode cache from its wait on the load event, and
// potentially dispatch the encoding of the bytecode.
if (mDocument && mDocument->ScriptLoader()) {
mDocument->ScriptLoader()->LoadEventFired();
}
// It's probably a good idea to GC soon since we have finished loading.
nsJSContext::PokeGC(
JS::GCReason::LOAD_END,
mDocument ? mDocument->GetWrapperPreserveColor() : nullptr);
#ifdef NS_PRINTING
// Check to see if someone tried to print during the load
if (window) {
auto* outerWin = nsGlobalWindowOuter::Cast(window);
outerWin->StopDelayingPrintingUntilAfterLoad();
if (outerWin->DelayedPrintUntilAfterLoad()) {
// We call into the inner because it ensures there's an active document
// and such.
if (RefPtr<nsPIDOMWindowInner> inner = window->GetCurrentInnerWindow()) {
nsGlobalWindowInner::Cast(inner)->Print(IgnoreErrors());
}
}
}
#endif
return rv;
}
bool nsDocumentViewer::GetLoadCompleted() { return mLoaded; }
bool nsDocumentViewer::GetIsStopped() { return mStopped; }
NS_IMETHODIMP
nsDocumentViewer::PermitUnload(PermitUnloadAction aAction,
bool* aPermitUnload) {
if (StaticPrefs::dom_disable_beforeunload()) {
aAction = eDontPromptAndUnload;
}
*aPermitUnload = true;
RefPtr<BrowsingContext> bc = mContainer->GetBrowsingContext();
if (!bc) {
return NS_OK;
}
// Per spec, we need to increase the ignore-opens-during-unload counter while
// dispatching the "beforeunload" event on both the document we're currently
// dispatching the event to and the document that we explicitly asked to
// unload.
IgnoreOpensDuringUnload ignoreOpens(mDocument);
bool foundBlocker = false;
bool foundOOPListener = false;
bc->PreOrderWalk([&](BrowsingContext* aBC) {
if (aBC->IsInProcess()) {
nsCOMPtr<nsIContentViewer> contentViewer;
aBC->GetDocShell()->GetContentViewer(getter_AddRefs(contentViewer));
if (contentViewer &&
contentViewer->DispatchBeforeUnload() == eRequestBlockNavigation) {
foundBlocker = true;
}
} else {
WindowContext* wc = aBC->GetCurrentWindowContext();
if (wc && wc->HasBeforeUnload()) {
foundOOPListener = true;
}
}
});
if (!foundOOPListener) {
if (!foundBlocker) {
return NS_OK;
}
if (aAction != ePrompt) {
*aPermitUnload = aAction == eDontPromptAndUnload;
return NS_OK;
}
}
// NB: we nullcheck mDocument because it might now be dead as a result of
// the event being dispatched.
RefPtr<WindowGlobalChild> wgc(mDocument ? mDocument->GetWindowGlobalChild()
: nullptr);
if (!wgc) {
return NS_OK;
}
nsAutoSyncOperation sync(mDocument);
AutoSuppressEventHandlingAndSuspend seh(bc->Group());
mInPermitUnloadPrompt = true;
bool done = false;
wgc->SendCheckPermitUnload(
foundBlocker, aAction,
[&](bool aPermit) {
done = true;
*aPermitUnload = aPermit;
},
[&](auto) {
// If the prompt aborted, we tell our consumer that it is not allowed
// to unload the page. One reason that prompts abort is that the user
// performed some action that caused the page to unload while our prompt
// was active. In those cases we don't want our consumer to also unload
// the page.
//
// XXX: Are there other cases where prompts can abort? Is it ok to
// prevent unloading the page in those cases?
done = true;
*aPermitUnload = false;
});
SpinEventLoopUntil([&]() { return done; });
mInPermitUnloadPrompt = false;
return NS_OK;
}
PermitUnloadResult nsDocumentViewer::DispatchBeforeUnload() {
AutoDontWarnAboutSyncXHR disableSyncXHRWarning;
if (!mDocument || mInPermitUnload || mInPermitUnloadPrompt) {
return eAllowNavigation;
}
// First, get the script global object from the document...
auto* window = nsGlobalWindowOuter::Cast(mDocument->GetWindow());
if (!window) {
// This is odd, but not fatal
NS_WARNING("window not set for document!");
return eAllowNavigation;
}
NS_ASSERTION(nsContentUtils::IsSafeToRunScript(), "This is unsafe");
// Create an RAII object on mDocument that will increment the
// should-ignore-opens-during-unload counter on initialization
// and decrement it again when it goes out of score (regardless
// of how we exit this function).
IgnoreOpensDuringUnload ignoreOpens(mDocument);
// Now, fire an BeforeUnload event to the document and see if it's ok
// to unload...
nsPresContext* presContext = mDocument->GetPresContext();
RefPtr<BeforeUnloadEvent> event =
new BeforeUnloadEvent(mDocument, presContext, nullptr);
event->InitEvent(u"beforeunload"_ns, false, true);
// Dispatching to |window|, but using |document| as the target.
event->SetTarget(mDocument);
event->SetTrusted(true);
// In evil cases we might be destroyed while handling the
// onbeforeunload event, don't let that happen. (see also bug#331040)
RefPtr<nsDocumentViewer> kungFuDeathGrip(this);
{
// Never permit popups from the beforeunload handler, no matter
// how we get here.
AutoPopupStatePusher popupStatePusher(PopupBlocker::openAbused, true);
// Never permit dialogs from the beforeunload handler
nsGlobalWindowOuter::TemporarilyDisableDialogs disableDialogs(window);
Document::PageUnloadingEventTimeStamp timestamp(mDocument);
mInPermitUnload = true;
EventDispatcher::DispatchDOMEvent(ToSupports(window), nullptr, event,
mPresContext, nullptr);
mInPermitUnload = false;
}
nsAutoString text;
event->GetReturnValue(text);
// NB: we nullcheck mDocument because it might now be dead as a result of
// the event being dispatched.
if (window->AreDialogsEnabled() && mDocument &&
!(mDocument->GetSandboxFlags() & SANDBOXED_MODALS) &&
(!StaticPrefs::dom_require_user_interaction_for_beforeunload() ||
mDocument->UserHasInteracted()) &&
(event->WidgetEventPtr()->DefaultPrevented() || !text.IsEmpty())) {
return eRequestBlockNavigation;
}
return eAllowNavigation;
}
NS_IMETHODIMP
nsDocumentViewer::GetBeforeUnloadFiring(bool* aInEvent) {
*aInEvent = mInPermitUnload;
return NS_OK;
}
NS_IMETHODIMP
nsDocumentViewer::GetInPermitUnload(bool* aInEvent) {
*aInEvent = mInPermitUnloadPrompt;
return NS_OK;
}
NS_IMETHODIMP
nsDocumentViewer::PageHide(bool aIsUnload) {
AutoDontWarnAboutSyncXHR disableSyncXHRWarning;
mHidden = true;
if (!mDocument) {
return NS_ERROR_NULL_POINTER;
}
if (aIsUnload) {
// Poke the GC. The window might be collectable garbage now.
nsJSContext::PokeGC(JS::GCReason::PAGE_HIDE,
mDocument->GetWrapperPreserveColor(),
StaticPrefs::javascript_options_gc_delay() * 2);
}
mDocument->OnPageHide(!aIsUnload, nullptr);
// inform the window so that the focus state is reset.
NS_ENSURE_STATE(mDocument);
nsPIDOMWindowOuter* window = mDocument->GetWindow();
if (window) window->PageHidden();
if (aIsUnload) {
// if Destroy() was called during OnPageHide(), mDocument is nullptr.
NS_ENSURE_STATE(mDocument);
// First, get the window from the document...
nsPIDOMWindowOuter* window = mDocument->GetWindow();
if (!window) {
// Fail if no window is available...
NS_WARNING("window not set for document!");
return NS_ERROR_NULL_POINTER;
}
// Create an RAII object on mDocument that will increment the
// should-ignore-opens-during-unload counter on initialization
// and decrement it again when it goes out of scope.
IgnoreOpensDuringUnload ignoreOpens(mDocument);
// Now, fire an Unload event to the document...
nsEventStatus status = nsEventStatus_eIgnore;
WidgetEvent event(true, eUnload);
event.mFlags.mBubbles = false;
// XXX Dispatching to |window|, but using |document| as the target.
event.mTarget = mDocument;
// Never permit popups from the unload handler, no matter how we get
// here.
AutoPopupStatePusher popupStatePusher(PopupBlocker::openAbused, true);
Document::PageUnloadingEventTimeStamp timestamp(mDocument);
EventDispatcher::Dispatch(window, mPresContext, &event, nullptr, &status);
}
#ifdef MOZ_XUL
// look for open menupopups and close them after the unload event, in case
// the unload event listeners open any new popups
nsContentUtils::HidePopupsInDocument(mDocument);
#endif
return NS_OK;
}
static void AttachContainerRecurse(nsIDocShell* aShell) {
nsCOMPtr<nsIContentViewer> viewer;
aShell->GetContentViewer(getter_AddRefs(viewer));
if (viewer) {
viewer->SetIsHidden(false);
Document* doc = viewer->GetDocument();
if (doc) {
doc->SetContainer(static_cast<nsDocShell*>(aShell));
}
if (PresShell* presShell = viewer->GetPresShell()) {
presShell->SetForwardingContainer(WeakPtr<nsDocShell>());
}
}
// Now recurse through the children
int32_t childCount;
aShell->GetInProcessChildCount(&childCount);
for (int32_t i = 0; i < childCount; ++i) {
nsCOMPtr<nsIDocShellTreeItem> childItem;
aShell->GetInProcessChildAt(i, getter_AddRefs(childItem));
nsCOMPtr<nsIDocShell> shell = do_QueryInterface(childItem);
AttachContainerRecurse(shell);
}
}
NS_IMETHODIMP
nsDocumentViewer::Open(nsISupports* aState, nsISHEntry* aSHEntry) {
NS_ENSURE_TRUE(mPresShell, NS_ERROR_NOT_INITIALIZED);
if (mDocument) {
mDocument->SetContainer(mContainer);
}
nsresult rv = InitInternal(mParentWidget, aState, nullptr, mBounds, false);
NS_ENSURE_SUCCESS(rv, rv);
mHidden = false;
if (mPresShell) mPresShell->SetForwardingContainer(WeakPtr<nsDocShell>());
// Rehook the child presentations. The child shells are still in
// session history, so get them from there.
if (aSHEntry) {
nsCOMPtr<nsIDocShellTreeItem> item;
int32_t itemIndex = 0;
while (NS_SUCCEEDED(
aSHEntry->ChildShellAt(itemIndex++, getter_AddRefs(item))) &&
item) {
nsCOMPtr<nsIDocShell> shell = do_QueryInterface(item);
AttachContainerRecurse(shell);
}
}
SyncParentSubDocMap();
ReinitializeFocusListener();
// XXX re-enable image animations once that works correctly
PrepareToStartLoad();
// When loading a page from the bfcache with puppet widgets, we do the
// widget attachment here (it is otherwise done in MakeWindow, which is
// called for non-bfcache pages in the history, but not bfcache pages).
// Attachment is necessary, since we get detached when another page
// is browsed to. That is, if we are one page A, then when we go to
// page B, we detach. So page A's view has no widget. If we then go
// back to it, and it is in the bfcache, we will use that view, which
// doesn't have a widget. The attach call here will properly attach us.
if (nsIWidget::UsePuppetWidgets() && mPresContext &&
ShouldAttachToTopLevel()) {
// If the old view is already attached to our parent, detach
DetachFromTopLevelWidget();
nsViewManager* vm = GetViewManager();
MOZ_ASSERT(vm, "no view manager");
nsView* v = vm->GetRootView();
MOZ_ASSERT(v, "no root view");
MOZ_ASSERT(mParentWidget, "no mParentWidget to set");
v->AttachToTopLevelWidget(mParentWidget);
mAttachedToParent = true;
}
return NS_OK;
}
NS_IMETHODIMP
nsDocumentViewer::Close(nsISHEntry* aSHEntry) {
// All callers are supposed to call close to break circular
// references. If we do this stuff in the destructor, the
// destructor might never be called (especially if we're being
// used from JS.
mSHEntry = aSHEntry;
// Close is also needed to disable scripts during paint suppression,
// since we transfer the existing global object to the new document
// that is loaded. In the future, the global object may become a proxy
// for an object that can be switched in and out so that we don't need
// to disable scripts during paint suppression.
if (!mDocument) return NS_OK;
if (mSHEntry) {
if (mBFCachePreventionObserver) {
mBFCachePreventionObserver->Disconnect();
}
mBFCachePreventionObserver = new BFCachePreventionObserver(mDocument);
mDocument->AddMutationObserver(mBFCachePreventionObserver);
}
#ifdef NS_PRINTING
// A Close was called while we were printing
// so don't clear the ScriptGlobalObject
// or clear the mDocument below
if (mPrintJob && !mClosingWhilePrinting) {
mClosingWhilePrinting = true;
} else
#endif
{
// out of band cleanup of docshell
mDocument->SetScriptGlobalObject(nullptr);
if (!mSHEntry && mDocument) mDocument->RemovedFromDocShell();
}
RemoveFocusListener();
return NS_OK;
}
static void DetachContainerRecurse(nsIDocShell* aShell) {
// Unhook this docshell's presentation
aShell->SynchronizeLayoutHistoryState();
nsCOMPtr<nsIContentViewer> viewer;
aShell->GetContentViewer(getter_AddRefs(viewer));
if (viewer) {
if (Document* doc = viewer->GetDocument()) {
doc->SetContainer(nullptr);
}
if (PresShell* presShell = viewer->GetPresShell()) {
auto weakShell = static_cast<nsDocShell*>(aShell);
presShell->SetForwardingContainer(weakShell);
}
}
// Now recurse through the children
int32_t childCount;
aShell->GetInProcessChildCount(&childCount);
for (int32_t i = 0; i < childCount; ++i) {
nsCOMPtr<nsIDocShellTreeItem> childItem;
aShell->GetInProcessChildAt(i, getter_AddRefs(childItem));
nsCOMPtr<nsIDocShell> shell = do_QueryInterface(childItem);
DetachContainerRecurse(shell);
}
}
NS_IMETHODIMP
nsDocumentViewer::Destroy() {
NS_ASSERTION(mDocument, "No document in Destroy()!");
// Don't let the document get unloaded while we are printing.
// this could happen if we hit the back button during printing.
// We also keep the viewer from being cached in session history, since
// we require all documents there to be sanitized.
if (mDestroyBlockedCount != 0) {
return NS_OK;
}
#ifdef NS_PRINTING
// Here is where we check to see if the document was still being prepared
// for printing when it was asked to be destroy from someone externally
// This usually happens if the document is unloaded while the user is in the
// Print Dialog
//
// So we flip the bool to remember that the document is going away
// and we can clean up and abort later after returning from the Print Dialog
if (mPrintJob && mPrintJob->CheckBeforeDestroy()) {
return NS_OK;
}
#endif
// We want to make sure to disconnect mBFCachePreventionObserver before we
// Sanitize() below.
if (mBFCachePreventionObserver) {
mBFCachePreventionObserver->Disconnect();
mBFCachePreventionObserver = nullptr;
}
if (mSHEntry && mDocument && !mDocument->IsBFCachingAllowed()) {
// Just drop the SHEntry now and pretend like we never even tried to bfcache
// this viewer. This should only happen when someone calls
// DisallowBFCaching() after CanSavePresentation() already ran. Ensure that
// the SHEntry has no viewer and its state is synced up. We want to do this
// via a stack reference, in case those calls mess with our members.
MOZ_LOG(gPageCacheLog, LogLevel::Debug,
("BFCache not allowed, dropping SHEntry"));
nsCOMPtr<nsISHEntry> shEntry = std::move(mSHEntry);
shEntry->SetContentViewer(nullptr);
shEntry->SyncPresentationState();
}
// If we were told to put ourselves into session history instead of destroy
// the presentation, do that now.
if (mSHEntry) {
if (mPresShell) mPresShell->Freeze();
// Make sure the presentation isn't torn down by Hide().
mSHEntry->SetSticky(mIsSticky);
mIsSticky = true;
// Remove our root view from the view hierarchy.
if (mPresShell) {
nsViewManager* vm = mPresShell->GetViewManager();
if (vm) {
nsView* rootView = vm->GetRootView();
if (rootView) {
nsView* rootViewParent = rootView->GetParent();
if (rootViewParent) {
nsView* subdocview = rootViewParent->GetParent();
if (subdocview) {
nsIFrame* f = subdocview->GetFrame();
if (f) {
nsSubDocumentFrame* s = do_QueryFrame(f);
if (s) {
s->ClearDisplayItems();
}
}
}
nsViewManager* parentVM = rootViewParent->GetViewManager();
if (parentVM) {
parentVM->RemoveChild(rootView);
}
}
}
}
}
Hide();
// This is after Hide() so that the user doesn't see the inputs clear.
if (mDocument) {
mDocument->Sanitize();
}
// Reverse ownership. Do this *after* calling sanitize so that sanitize
// doesn't cause mutations that make the SHEntry drop the presentation
// Grab a reference to mSHEntry before calling into things like
// SyncPresentationState that might mess with our members.
nsCOMPtr<nsISHEntry> shEntry =
std::move(mSHEntry); // we'll need this below
MOZ_LOG(gPageCacheLog, LogLevel::Debug,
("Storing content viewer into cache entry"));
shEntry->SetContentViewer(this);
// Always sync the presentation state. That way even if someone screws up
// and shEntry has no window state at this point we'll be ok; we just won't
// cache ourselves.
shEntry->SyncPresentationState();
// XXX Synchronize layout history state to parent once bfcache is supported
// in session-history-in-parent.
// Shut down accessibility for the document before we start to tear it down.
#ifdef ACCESSIBILITY
if (mPresShell) {
a11y::DocAccessible* docAcc = mPresShell->GetDocAccessible();
if (docAcc) {
docAcc->Shutdown();
}
}
#endif
// Break the link from the document/presentation to the docshell, so that
// link traversals cannot affect the currently-loaded document.
// When the presentation is restored, Open() and InitInternal() will reset
// these pointers to their original values.
if (mDocument) {
mDocument->SetContainer(nullptr);
}
if (mPresShell) {
mPresShell->SetForwardingContainer(mContainer);
}
// Do the same for our children. Note that we need to get the child
// docshells from the SHEntry now; the docshell will have cleared them.
nsCOMPtr<nsIDocShellTreeItem> item;
int32_t itemIndex = 0;
while (NS_SUCCEEDED(
shEntry->ChildShellAt(itemIndex++, getter_AddRefs(item))) &&
item) {
nsCOMPtr<nsIDocShell> shell = do_QueryInterface(item);
DetachContainerRecurse(shell);
}
return NS_OK;
}
// The document was not put in the bfcache
// Protect against pres shell destruction running scripts and re-entrantly
// creating a new presentation.
nsAutoScriptBlocker scriptBlocker;
if (mPresShell) {
DestroyPresShell();
}
if (mDocument) {
mDocument->Destroy();
mDocument = nullptr;
}
// All callers are supposed to call destroy to break circular
// references. If we do this stuff in the destructor, the
// destructor might never be called (especially if we're being
// used from JS.
#ifdef NS_PRINTING
if (mPrintJob) {
RefPtr<nsPrintJob> printJob = std::move(mPrintJob);
# ifdef NS_PRINT_PREVIEW
if (printJob->CreatedForPrintPreview()) {
printJob->FinishPrintPreview();
}
# endif
printJob->Destroy();
MOZ_ASSERT(!mPrintJob,
"mPrintJob shouldn't be recreated while destroying it");
}
#endif
// Avoid leaking the old viewer.
if (mPreviousViewer) {
mPreviousViewer->Destroy();
mPreviousViewer = nullptr;
}
mDeviceContext = nullptr;
if (mPresContext) {
DestroyPresContext();
}
mWindow = nullptr;
mViewManager = nullptr;
mContainer = WeakPtr<nsDocShell>();
return NS_OK;
}
NS_IMETHODIMP
nsDocumentViewer::Stop(void) {
NS_ASSERTION(mDocument, "Stop called too early or too late");
if (mDocument) {
mDocument->StopDocumentLoad();
}
if (!mHidden && (mLoaded || mStopped) && mPresContext && !mSHEntry)
mPresContext->SetImageAnimationMode(imgIContainer::kDontAnimMode);
mStopped = true;
if (!mLoaded && mPresShell) {
// Well, we might as well paint what we have so far.
RefPtr<PresShell> presShell = mPresShell; // bug 378682
presShell->UnsuppressPainting();
}
return NS_OK;
}
NS_IMETHODIMP
nsDocumentViewer::GetDOMDocument(Document** aResult) {
NS_ENSURE_TRUE(mDocument, NS_ERROR_NOT_AVAILABLE);
nsCOMPtr<Document> document = mDocument;
document.forget(aResult);
return NS_OK;
}
Document* nsDocumentViewer::GetDocument() { return mDocument; }
nsresult nsDocumentViewer::SetDocument(Document* aDocument) {
// Assumptions:
//
// 1) this document viewer has been initialized with a call to Init().
// 2) the stylesheets associated with the document have been added
// to the document.
// XXX Right now, this method assumes that the layout of the current
// document hasn't started yet. More cleanup will probably be
// necessary to make this method work for the case when layout *has*
// occurred for the current document.
// That work can happen when and if it is needed.
if (!aDocument) return NS_ERROR_NULL_POINTER;
return SetDocumentInternal(aDocument, false);
}
NS_IMETHODIMP
nsDocumentViewer::SetDocumentInternal(Document* aDocument,
bool aForceReuseInnerWindow) {
MOZ_ASSERT(aDocument);
// Set new container
aDocument->SetContainer(mContainer);
if (mDocument != aDocument) {
if (aForceReuseInnerWindow) {
// Transfer the navigation timing information to the new document, since
// we're keeping the same inner and hence should really have the same
// timing information.
aDocument->SetNavigationTiming(mDocument->GetNavigationTiming());
}
if (mDocument && mDocument->IsStaticDocument()) {
mDocument->Destroy();
}
// Clear the list of old child docshells. Child docshells for the new
// document will be constructed as frames are created.
if (!aDocument->IsStaticDocument()) {
nsCOMPtr<nsIDocShell> node(mContainer);
if (node) {
int32_t count;
node->GetInProcessChildCount(&count);
for (int32_t i = 0; i < count; ++i) {
nsCOMPtr<nsIDocShellTreeItem> child;
node->GetInProcessChildAt(0, getter_AddRefs(child));
node->RemoveChild(child);
}
}
}
// Replace the old document with the new one. Do this only when
// the new document really is a new document.
mDocument = aDocument;
// Set the script global object on the new document
nsCOMPtr<nsPIDOMWindowOuter> window =
mContainer ? mContainer->GetWindow() : nullptr;
if (window) {
nsresult rv =
window->SetNewDocument(aDocument, nullptr, aForceReuseInnerWindow);
if (NS_FAILED(rv)) {
Destroy();
return rv;
}
}
}
nsresult rv = SyncParentSubDocMap();
NS_ENSURE_SUCCESS(rv, rv);
// Replace the current pres shell with a new shell for the new document
// Protect against pres shell destruction running scripts and re-entrantly
// creating a new presentation.
nsAutoScriptBlocker scriptBlocker;
if (mPresShell) {
DestroyPresShell();
}
if (mPresContext) {
DestroyPresContext();
mWindow = nullptr;
rv = InitInternal(mParentWidget, nullptr, nullptr, mBounds, true, true,
false);
}
return rv;
}
PresShell* nsDocumentViewer::GetPresShell() { return mPresShell; }
nsPresContext* nsDocumentViewer::GetPresContext() { return mPresContext; }
nsViewManager* nsDocumentViewer::GetViewManager() { return mViewManager; }
NS_IMETHODIMP
nsDocumentViewer::GetBounds(nsIntRect& aResult) {
NS_ENSURE_TRUE(mDocument, NS_ERROR_NOT_AVAILABLE);
aResult = mBounds;
return NS_OK;
}
nsIContentViewer* nsDocumentViewer::GetPreviousViewer() {
return mPreviousViewer;
}
void nsDocumentViewer::SetPreviousViewer(nsIContentViewer* aViewer) {
// NOTE: |Show| sets |mPreviousViewer| to null without calling this
// function.
if (aViewer) {
NS_ASSERTION(!mPreviousViewer,
"can't set previous viewer when there already is one");
// In a multiple chaining situation (which occurs when running a thrashing
// test like i-bench or jrgm's tests with no delay), we can build up a
// whole chain of viewers. In order to avoid this, we always set our
// previous viewer to the MOST previous viewer in the chain, and then dump
// the intermediate link from the chain. This ensures that at most only 2
// documents are alive and undestroyed at any given time (the one that is
// showing and the one that is loading with painting suppressed). It's very
// important that if this ever gets changed the code before the
// RestorePresentation call in nsDocShell::InternalLoad be changed
// accordingly.
//
// Make sure we hold a strong ref to prevViewer here, since we'll
// tell aViewer to drop it.
nsCOMPtr<nsIContentViewer> prevViewer = aViewer->GetPreviousViewer();
if (prevViewer) {
aViewer->SetPreviousViewer(nullptr);
aViewer->Destroy();
return SetPreviousViewer(prevViewer);
}
}
mPreviousViewer = aViewer;
}
NS_IMETHODIMP
nsDocumentViewer::SetBoundsWithFlags(const nsIntRect& aBounds,
uint32_t aFlags) {
NS_ENSURE_TRUE(mDocument, NS_ERROR_NOT_AVAILABLE);
bool boundsChanged = !mBounds.IsEqualEdges(aBounds);
mBounds = aBounds;
if (mWindow && !mAttachedToParent) {
// Resize the widget, but don't trigger repaint. Layout will generate
// repaint requests during reflow.
mWindow->Resize(aBounds.x, aBounds.y, aBounds.width, aBounds.height, false);
} else if (mPresContext && mViewManager) {
// Ensure presContext's deviceContext is up to date, as we sometimes get
// here before a resolution-change notification has been fully handled
// during display configuration changes, especially when there are lots
// of windows/widgets competing to handle the notifications.
// (See bug 1154125.)
if (mPresContext->DeviceContext()->CheckDPIChange()) {
mPresContext->UIResolutionChanged();
}
int32_t p2a = mPresContext->AppUnitsPerDevPixel();
nscoord width = NSIntPixelsToAppUnits(mBounds.width, p2a);
nscoord height = NSIntPixelsToAppUnits(mBounds.height, p2a);
nsView* rootView = mViewManager->GetRootView();
if (boundsChanged && rootView) {
nsRect viewDims = rootView->GetDimensions();
// If the view/frame tree and prescontext visible area already has the new
// size but we did not, then it's likely that we got reflowed in response
// to a call to GetContentSize. Thus there is a disconnect between the
// size on the document viewer/docshell/containing widget and view
// tree/frame tree/prescontext visible area). SetWindowDimensions compares
// to the root view dimenstions to determine if it needs to do anything;
// if they are the same as the new size it won't do anything, but we still
// need to invalidate because what we want to draw to the screen has
// changed.
if (viewDims.width == width && viewDims.height == height) {
nsIFrame* f = rootView->GetFrame();
if (f) {
f->InvalidateFrame();
}
}
}
mViewManager->SetWindowDimensions(
width, height, !!(aFlags & nsIContentViewer::eDelayResize));
}
// If there's a previous viewer, it's the one that's actually showing,
// so be sure to resize it as well so it paints over the right area.
// This may slow down the performance of the new page load, but resize
// during load is also probably a relatively unusual condition
// relating to things being hidden while something is loaded. It so
// happens that Firefox does this a good bit with its infobar, and it
// looks ugly if we don't do this.