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 */
/* a presentation of a document, part 2 */
#ifndef mozilla_PresShell_h
#define mozilla_PresShell_h
#include "mozilla/PresShellForwards.h"
#include <stdio.h> // for FILE definition
#include "FrameMetrics.h"
#include "GeckoProfiler.h"
#include "TouchManager.h"
#include "Units.h"
#include "Visibility.h"
#include "mozilla/ArenaObjectID.h"
#include "mozilla/Attributes.h"
#include "mozilla/FlushType.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/ScrollTypes.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/WeakPtr.h"
#include "mozilla/layers/FocusTarget.h"
#include "mozilla/layout/LayoutTelemetryTools.h"
#include "nsColor.h"
#include "nsCOMArray.h"
#include "nsCoord.h"
#include "nsDOMNavigationTiming.h"
#include "nsFrameManager.h"
#include "nsFrameState.h"
#include "nsIContent.h"
#include "nsIObserver.h"
#include "nsISelectionController.h"
#include "nsQueryFrame.h"
#include "nsPresArena.h"
#include "nsPresContext.h"
#include "nsRect.h"
#include "nsRefreshObservers.h"
#include "nsStringFwd.h"
#include "nsStubDocumentObserver.h"
#include "nsTHashtable.h"
#include "nsThreadUtils.h"
#include "nsWeakReference.h"
class AutoPointerEventTargetUpdater;
class AutoWeakFrame;
class gfxContext;
class MobileViewportManager;
class nsAccessibilityService;
class nsAutoCauseReflowNotifier;
class nsCanvasFrame;
class nsCaret;
class nsCSSFrameConstructor;
class nsDisplayList;
class nsDisplayListBuilder;
class nsDocShell;
class nsFrameSelection;
class nsIDocShell;
class nsIFrame;
class nsILayoutHistoryState;
class nsINode;
class nsPageSequenceFrame;
class nsIReflowCallback;
class nsIScrollableFrame;
class nsITimer;
class nsPIDOMWindowOuter;
class nsPresShellEventCB;
class nsRange;
class nsRefreshDriver;
class nsRegion;
class nsView;
class nsViewManager;
class nsWindowSizes;
struct RangePaintInfo;
class ReflowCountMgr;
class WeakFrame;
class ZoomConstraintsClient;
struct nsCallbackEventRequest;
namespace mozilla {
class AccessibleCaretEventHub;
class EventStates;
class GeckoMVMContext;
class OverflowChangedTracker;
class StyleSheet;
namespace a11y {
class DocAccessible;
} // namespace a11y
namespace dom {
class Element;
class Event;
class HTMLSlotElement;
class Selection;
} // namespace dom
namespace gfx {
class SourceSurface;
} // namespace gfx
namespace layers {
class LayerManager;
} // namespace layers
namespace layout {
class ScrollAnchorContainer;
} // namespace layout
// 039d8ffc-fa55-42d7-a53a-388cb129b052
{ \
0x039d8ffc, 0xfa55, 0x42d7, { \
0xa5, 0x3a, 0x38, 0x8c, 0xb1, 0x29, 0xb0, 0x52 \
} \
* Presentation shell. Presentation shells are the controlling point for
* managing the presentation of a document. The presentation shell holds a
* live reference to the document, the presentation context, the style
* manager, the style set and the root frame.
* When this object is Release'd, it will release the document, the
* presentation context, the style manager, the style set and the root frame.
class PresShell final : public nsStubDocumentObserver,
public nsISelectionController,
public nsIObserver,
public nsSupportsWeakReference {
typedef dom::Document Document;
typedef dom::Element Element;
typedef gfx::SourceSurface SourceSurface;
typedef layers::FocusTarget FocusTarget;
typedef layers::FrameMetrics FrameMetrics;
typedef layers::LayerManager LayerManager;
// A set type for tracking visible frames, for use by the visibility code in
// PresShell. The set contains nsIFrame* pointers.
typedef nsTHashtable<nsPtrHashKey<nsIFrame>> VisibleFrames;
explicit PresShell(Document* aDocument);
// nsISupports
static bool AccessibleCaretEnabled(nsIDocShell* aDocShell);
* Return the active content currently capturing the mouse if any.
static nsIContent* GetCapturingContent() {
return sCapturingContentInfo.mContent;
* Allow or disallow mouse capturing.
static void AllowMouseCapture(bool aAllowed) {
sCapturingContentInfo.mAllowed = aAllowed;
* Returns true if there is an active mouse capture that wants to prevent
* drags.
static bool IsMouseCapturePreventingDrag() {
return sCapturingContentInfo.mPreventDrag && sCapturingContentInfo.mContent;
static void ClearMouseCaptureOnView(nsView* aView);
// If a frame in the subtree rooted at aFrame is capturing the mouse then
// clears that capture.
static void ClearMouseCapture(nsIFrame* aFrame);
* Return the document accessible for this PresShell if there is one.
a11y::DocAccessible* GetDocAccessible() const { return mDocAccessible; }
* Set the document accessible for this PresShell.
void SetDocAccessible(a11y::DocAccessible* aDocAccessible) {
mDocAccessible = aDocAccessible;
* Return true if accessibility is active.
static bool IsAccessibilityActive();
* Return accessibility service if accessibility is active.
static nsAccessibilityService* GetAccessibilityService();
#endif // #ifdef ACCESSIBILITY
void Init(nsPresContext*, nsViewManager*);
* All callers are responsible for calling |Destroy| after calling
* |EndObservingDocument|. It needs to be separate only because form
* controls incorrectly store their data in the frames rather than the
* content model and printing calls |EndObservingDocument| multiple
* times to make form controls behave nicely when printed.
void Destroy();
bool IsDestroying() { return mIsDestroying; }
* All frames owned by the shell are allocated from an arena. They
* are also recycled using free lists. Separate free lists are
* maintained for each frame type (aID), which must always correspond
* to the same aSize value. AllocateFrame is infallible and will abort
* on out-of-memory.
void* AllocateFrame(nsQueryFrame::FrameIID aID, size_t aSize) {
#define FRAME_ID(classname, ...) \
static_assert(size_t(nsQueryFrame::FrameIID::classname##_id) == \
size_t(eArenaObjectID_##classname), \
#define ABSTRACT_FRAME_ID(classname) \
static_assert(size_t(nsQueryFrame::FrameIID::classname##_id) == \
size_t(eArenaObjectID_##classname), \
#include "mozilla/FrameIdList.h"
#undef FRAME_ID
return AllocateByObjectID(ArenaObjectID(size_t(aID)), aSize);
void FreeFrame(nsQueryFrame::FrameIID aID, void* aPtr) {
return FreeByObjectID(ArenaObjectID(size_t(aID)), aPtr);
void* AllocateByObjectID(ArenaObjectID aID, size_t aSize) {
void* result = mFrameArena.Allocate(aID, aSize);
return result;
void FreeByObjectID(ArenaObjectID aID, void* aPtr) {
if (!mIsDestroying) {
mFrameArena.Free(aID, aPtr);
Document* GetDocument() const { return mDocument; }
nsPresContext* GetPresContext() const { return mPresContext; }
nsViewManager* GetViewManager() const { return mViewManager; }
nsRefreshDriver* GetRefreshDriver() const;
nsCSSFrameConstructor* FrameConstructor() const {
return mFrameConstructor.get();
* FrameSelection will return the Frame based selection API.
* You cannot go back and forth anymore with QI between nsIDOM sel and
* nsIFrame sel.
already_AddRefed<nsFrameSelection> FrameSelection();
* ConstFrameSelection returns an object which methods are safe to use for
* example in nsIFrame code.
const nsFrameSelection* ConstFrameSelection() const { return mSelection; }
// Start receiving notifications from our document. If called after Destroy,
// this will be ignored.
void BeginObservingDocument();
// Stop receiving notifications from our document. If called after Destroy,
// this will be ignored.
void EndObservingDocument();
bool IsObservingDocument() const { return mIsObservingDocument; }
* Return whether Initialize() was previously called.
bool DidInitialize() const { return mDidInitialize; }
* Perform initialization. Constructs the frame for the root content
* object and then enqueues a reflow of the frame model.
* Callers of this method must hold a reference to this shell that
* is guaranteed to survive through arbitrary script execution.
* Calling Initialize can execute arbitrary script.
MOZ_CAN_RUN_SCRIPT_BOUNDARY nsresult Initialize();
* Reflow the frame model into a new width and height. The
* coordinates for aWidth and aHeight must be in standard nscoord's.
ResizeReflow(nscoord aWidth, nscoord aHeight,
ResizeReflowOptions = ResizeReflowOptions::NoOption);
MOZ_CAN_RUN_SCRIPT nsresult ResizeReflowIgnoreOverride(nscoord aWidth,
nscoord aHeight,
* Add this pres shell to the refresh driver to be observed for resize
* event if applicable.
void AddResizeEventFlushObserverIfNeeded();
* Returns true if the document hosted by this presShell is in a devtools
* Responsive Design Mode browsing context.
bool InRDMPane();
* This is what ResizeReflowIgnoreOverride does when not shrink-wrapping (that
* is, when ResizeReflowOptions::BSizeLimit is not specified).
void SimpleResizeReflow(nscoord aWidth, nscoord aHeight, ResizeReflowOptions);
* Note that the assumptions that determine whether we need a mobile viewport
* manager may have changed.
void MaybeRecreateMobileViewportManager(bool aAfterInitialization);
* Returns true if this document uses mobile viewport sizing (including
* processing of <meta name="viewport"> tags).
* Note that having a MobileViewportManager does not necessarily mean using
* mobile viewport sizing, as with desktop zooming we can have a
* MobileViewportManager on desktop, but we only want to do mobile viewport
* sizing on mobile. (TODO: Rename MobileViewportManager to reflect its more
* general role.)
bool UsesMobileViewportSizing() const;
* Get the MobileViewportManager used to manage the document's mobile
* viewport. Will return null in situations where we don't have a mobile
* viewport, and for documents that are not the root content document.
RefPtr<MobileViewportManager> GetMobileViewportManager() const;
* Return true if the presshell expects layout flush.
bool IsLayoutFlushObserver();
* Called when document load completes.
void LoadComplete();
* This calls through to the frame manager to get the root frame.
nsIFrame* GetRootFrame() const { return mFrameManager->GetRootFrame(); }
* Get root scroll frame from FrameManager()->GetRootFrame().
nsIFrame* GetRootScrollFrame() const;
* The same as GetRootScrollFrame, but returns an nsIScrollableFrame
nsIScrollableFrame* GetRootScrollFrameAsScrollable() const;
* Get the current focused content or DOM selection that should be the
* target for scrolling.
already_AddRefed<nsIContent> GetContentForScrolling() const;
* Get the DOM selection that should be the target for scrolling, if there
* is no focused content.
already_AddRefed<nsIContent> GetSelectedContentForScrolling() const;
* Gets nearest scrollable frame from the specified content node. The frame
* is scrollable with overflow:scroll or overflow:auto in some direction when
* aDirection is eEither. Otherwise, this returns a nearest frame that is
* scrollable in the specified direction.
nsIScrollableFrame* GetScrollableFrameToScrollForContent(
nsIContent* aContent, ScrollableDirection aDirection);
* Gets nearest scrollable frame from current focused content or DOM
* selection if there is no focused content. The frame is scrollable with
* overflow:scroll or overflow:auto in some direction when aDirection is
* eEither. Otherwise, this returns a nearest frame that is scrollable in
* the specified direction.
nsIScrollableFrame* GetScrollableFrameToScroll(
ScrollableDirection aDirection);
* Gets nearest ancestor scrollable frame from aFrame. The frame is
* scrollable with overflow:scroll or overflow:auto in some direction when
* aDirection is eEither. Otherwise, this returns a nearest frame that is
* scrollable in the specified direction.
nsIScrollableFrame* GetNearestScrollableFrame(nsIFrame* aFrame,
ScrollableDirection aDirection);
* Returns the page sequence frame associated with the frame hierarchy.
* Returns nullptr if not a paginated view.
nsPageSequenceFrame* GetPageSequenceFrame() const;
* Returns the canvas frame associated with the frame hierarchy.
* Returns nullptr if is XUL document.
nsCanvasFrame* GetCanvasFrame() const;
void PostPendingScrollAnchorSelection(
layout::ScrollAnchorContainer* aContainer);
void FlushPendingScrollAnchorSelections();
void PostPendingScrollAnchorAdjustment(
layout::ScrollAnchorContainer* aContainer);
void CancelAllPendingReflows();
MOZ_CAN_RUN_SCRIPT_BOUNDARY void NotifyCounterStylesAreDirty();
bool FrameIsAncestorOfDirtyRoot(nsIFrame* aFrame) const;
* Destroy the frames for aElement, and reconstruct them asynchronously if
* needed.
* Note that this may destroy frames for an ancestor instead.
void DestroyFramesForAndRestyle(Element* aElement);
* Handles all the layout stuff needed when the slot assignment for an element
* is about to change.
* Only called when the slot attribute of the element changes, the rest of
* the changes should be handled in ShadowRoot.
void SlotAssignmentWillChange(Element& aElement,
dom::HTMLSlotElement* aOldSlot,
dom::HTMLSlotElement* aNewSlot);
void PostRecreateFramesFor(Element*);
void RestyleForAnimation(Element*, RestyleHint);
* Determine if it is safe to flush all pending notifications.
bool IsSafeToFlush() const;
* Informs the document's FontFaceSet that the refresh driver ticked,
* flushing style and layout.
void NotifyFontFaceSetOnRefresh();
// Removes ourself from the list of layout / style / and resize refresh driver
// observers.
// Right now this is only used for documents in the BFCache, so if you want to
// use this for anything else you need to ensure we don't end up in those
// lists after calling this, but before calling StartObservingRefreshDriver
// again.
// That is handled by the mDocument->GetBFCacheEntry checks in
// DoObserve*Flushes functions, though that could conceivably become a boolean
// member in the shell if needed.
// Callers are responsible of manually calling StartObservingRefreshDriver
// again.
void StopObservingRefreshDriver();
void StartObservingRefreshDriver();
bool ObservingStyleFlushes() const { return mObservingStyleFlushes; }
bool ObservingLayoutFlushes() const { return mObservingLayoutFlushes; }
void ObserveStyleFlushes() {
if (!ObservingStyleFlushes()) {
* Callbacks will be called even if reflow itself fails for
* some reason.
nsresult PostReflowCallback(nsIReflowCallback* aCallback);
void CancelReflowCallback(nsIReflowCallback* aCallback);
void ScheduleBeforeFirstPaint();
void UnsuppressAndInvalidate();
void ClearFrameRefs(nsIFrame* aFrame);
// Clears the selection of the older focused frame selection if any.
void FrameSelectionWillTakeFocus(nsFrameSelection&);
// Clears and repaint mFocusedFrameSelection if it matches the argument.
void FrameSelectionWillLoseFocus(nsFrameSelection&);
* Get a reference rendering context. This is a context that should not
* be rendered to, but is suitable for measuring text and performing
* other non-rendering operations. Guaranteed to return non-null.
already_AddRefed<gfxContext> CreateReferenceRenderingContext();
* Scrolls the view of the document so that the given area of a frame
* is visible, if possible. Layout is not flushed before scrolling.
* @param aRect Relative to aFrame. The rect edges will be respected even if
* the rect is empty.
* @param aVertical see ScrollContentIntoView and ScrollAxis
* @param aHorizontal see ScrollContentIntoView and ScrollAxis
* @param aScrollFlags if SCROLL_FIRST_ANCESTOR_ONLY is set, only the
* nearest scrollable ancestor is scrolled, otherwise all
* scrollable ancestors may be scrolled if necessary
* if SCROLL_OVERFLOW_HIDDEN is set then we may scroll in a direction
* even if overflow:hidden is specified in that direction; otherwise
* we will not scroll in that direction when overflow:hidden is
* set for that direction
* If SCROLL_NO_PARENT_FRAMES is set then we only scroll
* nodes in this document, not in any parent documents which
* contain this document in a iframe or the like.
* If SCROLL_IGNORE_SCROLL_MARGIN_AND_PADDING is set we ignore scroll-margin
* value specified for |aFrame| and scroll-padding value for the scroll
* container. This option is typically used to locate poped-up frames into
* view.
* @return true if any scrolling happened, false if no scrolling happened
bool ScrollFrameRectIntoView(nsIFrame* aFrame, const nsRect& aRect,
ScrollAxis aVertical, ScrollAxis aHorizontal,
ScrollFlags aScrollFlags);
* Suppress notification of the frame manager that frames are
* being destroyed.
void SetIgnoreFrameDestruction(bool aIgnore);
* Get the AccessibleCaretEventHub, if it exists. AddRefs it.
already_AddRefed<AccessibleCaretEventHub> GetAccessibleCaretEventHub() const;
* Get the caret, if it exists. AddRefs it.
already_AddRefed<nsCaret> GetCaret() const;
* Set the current caret to a new caret. To undo this, call RestoreCaret.
void SetCaret(nsCaret* aNewCaret);
* Restore the caret to the original caret that this pres shell was created
* with.
void RestoreCaret();
dom::Selection* GetCurrentSelection(SelectionType aSelectionType);
* Gets the last selection that took focus in this document. This is basically
* the frame selection that's visible to the user.
nsFrameSelection* GetLastFocusedFrameSelection();
* Interface to dispatch events via the presshell
* @note The caller must have a strong reference to the PresShell.
nsresult HandleEventWithTarget(WidgetEvent* aEvent, nsIFrame* aFrame,
nsIContent* aContent,
nsEventStatus* aEventStatus,
bool aIsHandlingNativeEvent = false,
nsIContent** aTargetContent = nullptr,
nsIContent* aOverrideClickTarget = nullptr) {
EventHandler eventHandler(*this);
return eventHandler.HandleEventWithTarget(
aEvent, aFrame, aContent, aEventStatus, aIsHandlingNativeEvent,
aTargetContent, aOverrideClickTarget);
* Dispatch event to content only (NOT full processing)
nsresult HandleDOMEventWithTarget(nsIContent* aTargetContent,
WidgetEvent* aEvent,
nsEventStatus* aStatus);
* Dispatch event to content only (NOT full processing)
nsresult HandleDOMEventWithTarget(nsIContent* aTargetContent,
dom::Event* aEvent, nsEventStatus* aStatus);
* Return whether or not the event is valid to be dispatched
bool CanDispatchEvent(const WidgetGUIEvent* aEvent = nullptr) const;
* Gets the current target event frame from the PresShell
nsIFrame* GetCurrentEventFrame();
* Gets the current target event frame from the PresShell
already_AddRefed<nsIContent> GetEventTargetContent(WidgetEvent* aEvent);
* Get and set the history state for the current document
nsresult CaptureHistoryState(nsILayoutHistoryState** aLayoutHistoryState);
* Determine if reflow is currently locked
* returns true if reflow is locked, false otherwise
bool IsReflowLocked() const { return mIsReflowing; }
* Called to find out if painting is suppressed for this presshell. If it is
* suppressd, we don't allow the painting of any layer but the background, and
* we don't recur into our children.
bool IsPaintingSuppressed() const { return mPaintingSuppressed; }
* Pause painting by freezing the refresh driver of this and all parent
* presentations. This may not have the desired effect if this pres shell
* has its own refresh driver.
void PausePainting();
* Resume painting by thawing the refresh driver of this and all parent
* presentations. This may not have the desired effect if this pres shell
* has its own refresh driver.
void ResumePainting();
* Unsuppress painting.
void UnsuppressPainting();
* Reconstruct frames for all elements in the document
MOZ_CAN_RUN_SCRIPT void ReconstructFrames();
* See if reflow verification is enabled. To enable reflow verification add
* "verifyreflow:1" to your MOZ_LOG environment variable (any non-zero
* debug level will work). Or, call SetVerifyReflowEnable with true.
static bool GetVerifyReflowEnable();
* Set the verify-reflow enable flag.
static void SetVerifyReflowEnable(bool aEnabled);
nsIFrame* GetAbsoluteContainingBlock(nsIFrame* aFrame);
void DumpReflows();
void CountReflows(const char* aName, nsIFrame* aFrame);
void PaintCount(const char* aName, gfxContext* aRenderingContext,
nsPresContext* aPresContext, nsIFrame* aFrame,
const nsPoint& aOffset, uint32_t aColor);
void SetPaintFrameCount(bool aOn);
bool IsPaintingFrameCounts();
#endif // #ifdef MOZ_REFLOW_PERF
// Debugging hooks
#ifdef DEBUG
void ListComputedStyles(FILE* out, int32_t aIndent = 0);
#if defined(DEBUG) || defined(MOZ_LAYOUT_DEBUGGER)
void ListStyleSheets(FILE* out, int32_t aIndent = 0);
* Stop all active elements (plugins and the caret) in this presentation and
* in the presentations of subdocuments. Resets painting to a suppressed
* state.
* XXX this should include image animations
void Freeze();
bool IsFrozen() { return mFrozen; }
* Restarts active elements (plugins) in this presentation and in the
* presentations of subdocuments, then do a full invalidate of the content
* area.
void Thaw();
void FireOrClearDelayedEvents(bool aFireEvents);
* When this shell is disconnected from its containing docshell, we
* lose our container pointer. However, we'd still like to be able to target
* user events at the docshell's parent. This pointer allows us to do that.
* It should not be used for any other purpose.
void SetForwardingContainer(const WeakPtr<nsDocShell>& aContainer);
* Render the document into an arbitrary gfxContext
* Designed for getting a picture of a document or a piece of a document
* Note that callers will generally want to call FlushPendingNotifications
* to get an up-to-date view of the document
* @param aRect is the region to capture into the offscreen buffer, in the
* root frame's coordinate system (if aIgnoreViewportScrolling is false)
* or in the root scrolled frame's coordinate system
* (if aIgnoreViewportScrolling is true). The coordinates are in appunits.
* @param aFlags see below;
* set RenderDocumentFlags::IsUntrusted if the contents may be passed to
* malicious agents. E.g. we might choose not to paint the contents of
* sensitive widgets such as the file name in a file upload widget, and we
* might choose not to paint themes.
* set RenderDocumentFlags::IgnoreViewportScrolling to ignore clipping and
* scrollbar painting due to scrolling in the viewport
* set RenderDocumentFlags::DrawCaret to draw the caret if one would be
* visible (by default the caret is never drawn)
* set RenderDocumentFlags::UseWidgetLayers to force rendering to go
* through the layer manager for the window. This may be unexpectedly slow
* (if the layer manager must read back data from the GPU) or low-quality
* (if the layer manager reads back pixel data and scales it
* instead of rendering using the appropriate scaling). It may also
* slow everything down if the area rendered does not correspond to the
* normal visible area of the window.
* set RenderDocumentFlags::AsyncDecodeImages to avoid having images
* synchronously decoded during rendering.
* (by default images decode synchronously with RenderDocument)
* set RenderDocumentFlags::DocumentRelative to render the document as if
* there has been no scrolling and interpret |aRect| relative to the document
* instead of the CSS viewport. Only considered if
* RenderDocumentFlags::IgnoreViewportScrolling is set or the document is in
* ignore viewport scrolling mode
* (PresShell::SetIgnoreViewportScrolling/IgnoringViewportScrolling).
* set RenderDocumentFlags::UseHighQualityScaling to enable downscale on
* decode for images.
* @param aBackgroundColor a background color to render onto
* @param aRenderedContext the gfxContext to render to. We render so that
* one CSS pixel in the source document is rendered to one unit in the current
* transform.
nsresult RenderDocument(const nsRect& aRect, RenderDocumentFlags aFlags,
nscolor aBackgroundColor,
gfxContext* aRenderedContext);
* Renders a node aNode to a surface and returns it. The aRegion may be used
* to clip the rendering. This region is measured in CSS pixels from the
* edge of the presshell area. The aPoint, aScreenRect and aFlags arguments
* function in a similar manner as RenderSelection.
already_AddRefed<SourceSurface> RenderNode(nsINode* aNode,
const Maybe<CSSIntRegion>& aRegion,
const LayoutDeviceIntPoint aPoint,
LayoutDeviceIntRect* aScreenRect,
RenderImageFlags aFlags);
* Renders a selection to a surface and returns it. This method is primarily
* intended to create the drag feedback when dragging a selection.
* aScreenRect will be filled in with the bounding rectangle of the
* selection area on screen.
* If the area of the selection is large and the RenderImageFlags::AutoScale
* is set, the image will be scaled down. The argument aPoint is used in this
* case as a reference point when determining the new screen rectangle after
* scaling. Typically, this will be the mouse position, so that the screen
* rectangle is positioned such that the mouse is over the same point in the
* scaled image as in the original. When scaling does not occur, the mouse
* point isn't used because the position can be determined from the displayed
* frames.
already_AddRefed<SourceSurface> RenderSelection(
dom::Selection* aSelection, const LayoutDeviceIntPoint aPoint,
LayoutDeviceIntRect* aScreenRect, RenderImageFlags aFlags);
void AddAutoWeakFrame(AutoWeakFrame* aWeakFrame);
void AddWeakFrame(WeakFrame* aWeakFrame);
void RemoveAutoWeakFrame(AutoWeakFrame* aWeakFrame);
void RemoveWeakFrame(WeakFrame* aWeakFrame);
* Stop or restart non synthetic test mouse event handling on *all*
* presShells.
* @param aDisable If true, disable all non synthetic test mouse
* events on all presShells. Otherwise, enable them.
void DisableNonTestMouseEvents(bool aDisable);
* Record the background color of the most recently drawn canvas. This color
* is composited on top of the user's default background color and then used
* to draw the background color of the canvas. See PresShell::Paint,
* PresShell::PaintDefaultBackground, and nsDocShell::SetupNewViewer;
* bug 488242, bug 476557 and other bugs mentioned there.
void SetCanvasBackground(nscolor aColor) { mCanvasBackgroundColor = aColor; }
nscolor GetCanvasBackground() { return mCanvasBackgroundColor; }
* Use the current frame tree (if it exists) to update the background
* color of the most recently drawn canvas.
void UpdateCanvasBackground();
* Add a solid color item to the bottom of aList with frame aFrame and
* bounds aBounds representing the dark grey background behind the page of a
* print preview presentation.
void AddPrintPreviewBackgroundItem(nsDisplayListBuilder* aBuilder,
nsDisplayList* aList, nsIFrame* aFrame,
const nsRect& aBounds);
* Computes the backstop color for the view: transparent if in a transparent
* widget, otherwise the PresContext default background color. This color is
* only visible if the contents of the view as a whole are translucent.
nscolor ComputeBackstopColor(nsView* aDisplayRoot);
void ObserveNativeAnonMutationsForPrint(bool aObserve) {
mObservesMutationsForPrint = aObserve;
bool ObservesNativeAnonMutationsForPrint() {
return mObservesMutationsForPrint;
nsresult SetIsActive(bool aIsActive);
bool IsActive() { return mIsActive; }
* Keep track of how many times this presshell has been rendered to
* a window.
uint64_t GetPaintCount() { return mPaintCount; }
void IncrementPaintCount() { ++mPaintCount; }
* Get the root DOM window of this presShell.
already_AddRefed<nsPIDOMWindowOuter> GetRootWindow();
* This returns the focused DOM window under our top level window.
* I.e., when we are deactive, this returns the *last* focused DOM window.
already_AddRefed<nsPIDOMWindowOuter> GetFocusedDOMWindowInOurWindow();
* Get the focused content under this window.
already_AddRefed<nsIContent> GetFocusedContentInOurWindow() const;
* Get the layer manager for the widget of the root view, if it has
* one.
LayerManager* GetLayerManager();
* Return true iff there is a widget rendering this presShell and that
* widget is APZ-enabled.
bool AsyncPanZoomEnabled();
* Track whether we're ignoring viewport scrolling for the purposes
* of painting. If we are ignoring, then layers aren't clipped to
* the CSS viewport and scrollbars aren't drawn.
bool IgnoringViewportScrolling() const {
return !!(mRenderingStateFlags &
float GetResolution() const { return mResolution.valueOr(1.0); }
float GetCumulativeResolution();
* Accessors for a flag that tracks whether the most recent change to
* the pres shell's resolution was originated by the main thread.
bool IsResolutionUpdated() const { return mResolutionUpdated; }
void SetResolutionUpdated(bool aUpdated) { mResolutionUpdated = aUpdated; }
* Returns true if the resolution has ever been changed by APZ.
bool IsResolutionUpdatedByApz() const { return mResolutionUpdatedByApz; }
* Used by session restore code to restore a resolution before the first
* paint.
void SetRestoreResolution(float aResolution,
LayoutDeviceIntSize aDisplaySize);
* Returns whether we are in a DrawWindow() call that used the
bool InDrawWindowNotFlushing() const {
return !!(mRenderingStateFlags &
* Set the isFirstPaint flag.
void SetIsFirstPaint(bool aIsFirstPaint) { mIsFirstPaint = aIsFirstPaint; }
* Get the isFirstPaint flag.
bool GetIsFirstPaint() const { return mIsFirstPaint; }
uint32_t GetPresShellId() { return mPresShellId; }
* Dispatch a mouse move event based on the most recent mouse position if
* this PresShell is visible. This is used when the contents of the page
* moved (aFromScroll is false) or scrolled (aFromScroll is true).
void SynthesizeMouseMove(bool aFromScroll);
nsresult HandleEvent(nsIFrame* aFrame, WidgetGUIEvent* aEvent,
bool aDontRetargetEvents, nsEventStatus* aEventStatus);
bool ShouldIgnoreInvalidation();
* Notify that we're going to call Paint with PaintFlags::PaintComposite.
* Fires on the presshell for the painted widget.
* This is issued at a time when it's safe to modify widget geometry.
MOZ_CAN_RUN_SCRIPT void WillPaintWindow();
* Notify that we called Paint with PaintFlags::PaintComposite.
* Fires on the presshell for the painted widget.
* This is issued at a time when it's safe to modify widget geometry.
MOZ_CAN_RUN_SCRIPT void DidPaintWindow();
bool IsVisible() const;
bool IsUnderHiddenEmbedderElement() const {
return mUnderHiddenEmbedderElement;
void SetIsUnderHiddenEmbedderElement(bool aUnderHiddenEmbedderElement);
void DispatchSynthMouseMove(WidgetGUIEvent* aEvent);
/* Temporarily ignore the Displayport for better paint performance. We
* trigger a repaint once suppression is disabled. Without that
* the displayport may get left at the suppressed size for an extended
* period of time and result in unnecessary checkerboarding (see bug
* 1255054). */
void SuppressDisplayport(bool aEnabled);
/* Whether or not displayport suppression should be turned on. Note that
* this only affects the return value of |IsDisplayportSuppressed()|, and
* doesn't change the value of the internal counter.
void RespectDisplayportSuppression(bool aEnabled);
/* Whether or not the displayport is currently suppressed. */
bool IsDisplayportSuppressed();
void AddSizeOfIncludingThis(nsWindowSizes& aWindowSizes) const;
* Methods that retrieve the cached font inflation preferences.
uint32_t FontSizeInflationEmPerLine() const {
return mFontSizeInflationEmPerLine;
uint32_t FontSizeInflationMinTwips() const {
return mFontSizeInflationMinTwips;
uint32_t FontSizeInflationLineThreshold() const {
return mFontSizeInflationLineThreshold;
bool FontSizeInflationForceEnabled() const {
return mFontSizeInflationForceEnabled;
bool FontSizeInflationDisabledInMasterProcess() const {
return mFontSizeInflationDisabledInMasterProcess;
bool FontSizeInflationEnabled() const { return mFontSizeInflationEnabled; }
* Recomputes whether font-size inflation is enabled.
void RecomputeFontSizeInflationEnabled();
* Return true if the most recent interruptible reflow was interrupted.
bool IsReflowInterrupted() const { return mWasLastReflowInterrupted; }
* Return true if the the interruptible reflows have to be suppressed.
* This may happen only if if the most recent reflow was interrupted.
bool SuppressInterruptibleReflows() const {
return mWasLastReflowInterrupted;
// Approximate frame visibility tracking public API.
* Schedule an update of the list of approximately visible frames "soon".
* This lets the refresh driver know that we want a visibility update in the
* near future. The refresh driver applies its own heuristics and throttling
* to decide when to actually perform the visibility update.
void ScheduleApproximateFrameVisibilityUpdateSoon();
* Schedule an update of the list of approximately visible frames "now". The
* update runs asynchronously, but it will be posted to the event loop
* immediately. Prefer the "soon" variation of this method when possible, as
* this variation ignores the refresh driver's heuristics.
void ScheduleApproximateFrameVisibilityUpdateNow();
* Clears the current list of approximately visible frames on this pres shell
* and replaces it with frames that are in the display list @aList.
void RebuildApproximateFrameVisibilityDisplayList(const nsDisplayList& aList);
void RebuildApproximateFrameVisibility(nsRect* aRect = nullptr,
bool aRemoveOnly = false);
* Ensures @aFrame is in the list of approximately visible frames.
void EnsureFrameInApproximatelyVisibleList(nsIFrame* aFrame);
/// Removes @aFrame from the list of approximately visible frames if present.
void RemoveFrameFromApproximatelyVisibleList(nsIFrame* aFrame);
/// Whether we should assume all frames are visible.
bool AssumeAllFramesVisible();
* Returns whether the document's style set's rule processor for the
* specified level of the cascade is shared by multiple style sets.
* @param aSheetType One of the nsIStyleSheetService.*_SHEET constants.
nsresult HasRuleProcessorUsedByMultipleStyleSets(uint32_t aSheetType,
bool* aRetVal);
* Returns whether or not the document has ever handled user input
bool HasHandledUserInput() const { return mHasHandledUserInput; }
MOZ_CAN_RUN_SCRIPT void FireResizeEvent();
void NativeAnonymousContentRemoved(nsIContent* aAnonContent);
* See HTMLDocument.setKeyPressEventModel() in HTMLDocument.webidl for the
* detail.
void SetKeyPressEventModel(uint16_t aKeyPressEventModel) {
mForceUseLegacyKeyCodeAndCharCodeValues |=
aKeyPressEventModel ==
bool AddRefreshObserver(nsARefreshObserver* aObserver, FlushType aFlushType);
bool RemoveRefreshObserver(nsARefreshObserver* aObserver,
FlushType aFlushType);
bool AddPostRefreshObserver(nsAPostRefreshObserver* aObserver);
bool RemovePostRefreshObserver(nsAPostRefreshObserver* aObserver);
// Represents an update to the visual scroll offset that will be sent to APZ.
// The update type is used to determine priority compared to other scroll
// updates.
struct VisualScrollUpdate {
nsPoint mVisualScrollOffset;
FrameMetrics::ScrollOffsetUpdateType mUpdateType;
bool mAcknowledged = false;
// Ask APZ in the next transaction to scroll to the given visual viewport
// offset (relative to the document).
// This is intended to be used when desired in cases where the browser
// internally triggers scrolling; scrolling triggered explicitly by web
// content (such as via window.scrollTo() should scroll the layout viewport
// only).
// If scrolling "far away", i.e. not just within the existing layout
// viewport, it's recommended to use both nsIScrollableFrame.ScrollTo*()
// (via window.scrollTo if calling from JS) *and* this function; otherwise,
// temporary checkerboarding may result. If doing this:
// * Be sure to call ScrollTo*() first, as a subsequent layout scroll
// in the same transaction will cancel the pending visual scroll.
// * Keep in mind that ScrollTo*() can tear down the pres shell and
// frame tree. Depending on how the pres shell is obtained for the
// subsequent ScrollToVisual() call, AutoWeakFrame or similar may
// need to be used.
// Please request APZ review if adding a new call site.
void ScrollToVisual(const nsPoint& aVisualViewportOffset,
FrameMetrics::ScrollOffsetUpdateType aUpdateType,
ScrollMode aMode);
void AcknowledgePendingVisualScrollUpdate();
void ClearPendingVisualScrollUpdate();
const Maybe<VisualScrollUpdate>& GetPendingVisualScrollUpdate() const {
return mPendingVisualScrollUpdate;
nsPoint GetLayoutViewportOffset() const;
nsSize GetLayoutViewportSize() const;
* Documents belonging to an invisible DocShell must not be painted ever.
bool IsNeverPainting() { return mIsNeverPainting; }
void SetNeverPainting(bool aNeverPainting) {
mIsNeverPainting = aNeverPainting;
* True if a reflow event has been scheduled, or is going to be scheduled
* to run in the future.
bool HasPendingReflow() const {
return mObservingLayoutFlushes || mReflowContinueTimer;
void SyncWindowProperties(nsView* aView);
Document* GetPrimaryContentDocument();
struct MOZ_RAII AutoAssertNoFlush {
explicit AutoAssertNoFlush(PresShell& aPresShell)
: mPresShell(aPresShell), mOldForbidden(mPresShell.mForbiddenToFlush) {
mPresShell.mForbiddenToFlush = true;
~AutoAssertNoFlush() { mPresShell.mForbiddenToFlush = mOldForbidden; }
PresShell& mPresShell;
const bool mOldForbidden;
NS_IMETHOD GetSelectionFromScript(RawSelectionType aRawSelectionType,
dom::Selection** aSelection) override;
dom::Selection* GetSelection(RawSelectionType aRawSelectionType) override;
NS_IMETHOD SetDisplaySelection(int16_t aToggle) override;
NS_IMETHOD GetDisplaySelection(int16_t* aToggle) override;
NS_IMETHOD ScrollSelectionIntoView(RawSelectionType aRawSelectionType,
SelectionRegion aRegion,
int16_t aFlags) override;
NS_IMETHOD RepaintSelection(RawSelectionType aRawSelectionType) override;
void SelectionWillTakeFocus() override;
void SelectionWillLoseFocus() override;
* Set a "resolution" for the document, which if not 1.0 will
* allocate more or fewer pixels for rescalable content by a factor
* of |resolution| in both dimensions. Return NS_OK iff the
* resolution bounds are sane, and the resolution of this was
* actually updated.
* Also increase the scale of the content by the same amount
* (that's the "AndScaleTo" part).
* The resolution defaults to 1.0.
* |aOrigin| specifies who originated the resolution change. For changes
* sent by APZ, pass ResolutionChangeOrigin::Apz. For changes sent by
* the main thread, use pass ResolutionChangeOrigin::MainThread (similar
* to the |aOrigin| parameter of nsIScrollableFrame::ScrollToCSSPixels()).
nsresult SetResolutionAndScaleTo(float aResolution,
ResolutionChangeOrigin aOrigin);
// Widget notificiations
void WindowSizeMoveDone();
void ThemeChanged() { mPresContext->ThemeChanged(); }
void BackingScaleFactorChanged() { mPresContext->UIResolutionChangedSync(); }
void Paint(nsView* aViewToPaint, const nsRegion& aDirtyRegion,
PaintFlags aFlags);
* Notify that we're going to call Paint with PaintFlags::PaintLayers
* on the pres shell for a widget (which might not be this one, since
* WillPaint is called on all presshells in the same toplevel window as the
* painted widget). This is issued at a time when it's safe to modify
* widget geometry.
MOZ_CAN_RUN_SCRIPT void WillPaint();
* Ensures that the refresh driver is running, and schedules a view
* manager flush on the next tick.
* @param aType PaintType::DelayedCompress : Schedule a paint to be executed
* after a delay, and put FrameLayerBuilder in 'compressed' mode that avoids
* short cut optimizations.
void ScheduleViewManagerFlush(PaintType aType = PaintType::Default);
// caret handling
NS_IMETHOD SetCaretEnabled(bool aInEnable) override;
NS_IMETHOD SetCaretReadOnly(bool aReadOnly) override;
NS_IMETHOD GetCaretEnabled(bool* aOutEnabled) override;
NS_IMETHOD SetCaretVisibilityDuringSelection(bool aVisibility) override;
NS_IMETHOD GetCaretVisible(bool* _retval) override;
* Should the images have borders etc. Actual visual effects are determined
* by the frames. Visual effects may not effect layout, only display.
* Takes effect on next repaint, does not force a repaint itself.
* @param aFlags may be multiple of nsISelectionDisplay::DISPLAY_*.
NS_IMETHOD SetSelectionFlags(int16_t aFlags) override;
NS_IMETHOD GetSelectionFlags(int16_t* aFlags) override;
* Gets the current state of non text selection effects
* @return current state of non text selection,
* as set by SetDisplayNonTextSelection
int16_t GetSelectionFlags() const { return mSelectionFlags; }
// nsISelectionController
NS_IMETHOD PhysicalMove(int16_t aDirection, int16_t aAmount,
bool aExtend) override;
NS_IMETHOD CharacterMove(bool aForward, bool aExtend) override;
NS_IMETHOD WordMove(bool aForward, bool aExtend) override;
NS_IMETHOD LineMove(bool aForward, bool aExtend) override;
NS_IMETHOD IntraLineMove(bool aForward, bool aExtend) override;
NS_IMETHOD PageMove(bool aForward, bool aExtend) override;
NS_IMETHOD ScrollPage(bool aForward) override;
NS_IMETHOD ScrollLine(bool aForward) override;
NS_IMETHOD ScrollCharacter(bool aRight) override;
NS_IMETHOD CompleteScroll(bool aForward) override;
NS_IMETHOD CompleteMove(bool aForward, bool aExtend) override;
NS_IMETHOD SelectAll() override;
NS_IMETHOD CheckVisibility(nsINode* node, int16_t startOffset,
int16_t EndOffset, bool* _retval) override;
nsresult CheckVisibilityContent(nsIContent* aNode, int16_t aStartOffset,
int16_t aEndOffset, bool* aRetval) override;
// Notifies that the state of the document has changed.
void DocumentStatesChanged(EventStates);
// nsIDocumentObserver
// nsIMutationObserver
// Inline methods defined in PresShellInlines.h
inline void EnsureStyleFlush();
inline void SetNeedStyleFlush();
inline void SetNeedLayoutFlush();
inline void SetNeedThrottledAnimationFlush();
inline ServoStyleSet* StyleSet() const;
* Whether we might need a flush for the given flush type. If this
* function returns false, we definitely don't need to flush.
* @param aFlushType The flush type to check. This must be
* >= FlushType::Style. This also returns true if a throttled
* animation flush is required.
bool NeedFlush(FlushType aType) const {
// We check mInFlush to handle re-entrant calls to FlushPendingNotifications
// by reporting that we always need a flush in that case. Otherwise,
// we could end up missing needed flushes, since we clear the mNeedXXXFlush
// flags at the top of FlushPendingNotifications.
MOZ_ASSERT(aType >= FlushType::Style);
return mNeedStyleFlush ||
(mNeedLayoutFlush && aType >= FlushType::InterruptibleLayout) ||
aType >= FlushType::Display || mNeedThrottledAnimationFlush ||
* Returns true if we might need to flush layout, even if we haven't scheduled
* one yet (as opposed to HasPendingReflow, which returns true if a flush is
* scheduled or will soon be scheduled).
bool NeedLayoutFlush() const { return mNeedLayoutFlush; }
bool NeedStyleFlush() const { return mNeedStyleFlush; }
* Flush pending notifications of the type specified. This method
* will not affect the content model; it'll just affect style and
* frames. Callers that actually want up-to-date presentation (other
* than the document itself) should probably be calling
* Document::FlushPendingNotifications.
* This method can execute script, which can destroy this presshell object
* unless someone is holding a reference to it on the stack. The presshell
* itself will ensure it lives up until the method returns, but callers who
* plan to use the presshell after this call should hold a strong ref
* themselves!
* @param aType the type of notifications to flush
void FlushPendingNotifications(FlushType aType) {
if (!NeedFlush(aType)) {
void FlushPendingNotifications(ChangesToFlush aType) {
if (!NeedFlush(aType.mFlushType)) {
* Tell the pres shell that a frame needs to be marked dirty and needs
* Reflow. It's OK if this is an ancestor of the frame needing reflow as
* long as the ancestor chain between them doesn't cross a reflow root.
* or nsFrameState(0); passing 0 means that dirty bits won't be set on the
* frame or its ancestors/descendants, but that intrinsic widths will still
* be marked dirty. Passing aIntrinsicDirty = eResize and aBitToAdd = 0
* would result in no work being done, so don't do that.
void FrameNeedsReflow(
nsIFrame* aFrame, IntrinsicDirty aIntrinsicDirty, nsFrameState aBitToAdd,
ReflowRootHandling aRootHandling = ReflowRootHandling::InferFromBitToAdd);
* Calls FrameNeedsReflow on all fixed position children of the root frame.
void MarkFixedFramesForReflow(IntrinsicDirty aIntrinsicDirty);
void MaybeReflowForInflationScreenSizeChange();
// This function handles all the work after VisualViewportSize is set
// or reset.
void CompleteChangeToVisualViewportSize();
* The return value indicates whether the offset actually changed.
bool SetVisualViewportOffset(const nsPoint& aScrollOffset,
const nsPoint& aPrevLayoutScrollPos);
nsPoint GetVisualViewportOffset() const {
return mVisualViewportOffset.valueOr(nsPoint());
bool IsVisualViewportOffsetSet() const {
return mVisualViewportOffset.isSome();
void SetVisualViewportSize(nscoord aWidth, nscoord aHeight);
void ResetVisualViewportSize();
bool IsVisualViewportSizeSet() { return mVisualViewportSizeSet; }
nsSize GetVisualViewportSize() {
"asking for visual viewport size when its not set?");
return mVisualViewportSize;
nsPoint GetVisualViewportOffsetRelativeToLayoutViewport() const;
// Returns state of the dynamic toolbar.
DynamicToolbarState GetDynamicToolbarState() const {
if (!mPresContext) {
return DynamicToolbarState::None;
return mPresContext->GetDynamicToolbarState();
// Returns the visual viewport size during the dynamic toolbar is being
// shown/hidden.
nsSize GetVisualViewportSizeUpdatedByDynamicToolbar() const;
/* Enable/disable author style level. Disabling author style disables the
* entire author level of the cascade, including the HTML preshint level.
// XXX these could easily be inlined, but there is a circular #include
// problem with nsStyleSet.
void SetAuthorStyleDisabled(bool aDisabled);
bool GetAuthorStyleDisabled() const;
* Update the style set somehow to take into account changed prefs which
* affect document styling.
void UpdatePreferenceStyles();
// aSheetType is one of the nsIStyleSheetService *_SHEET constants.
void NotifyStyleSheetServiceSheetAdded(StyleSheet* aSheet,
uint32_t aSheetType);
void NotifyStyleSheetServiceSheetRemoved(StyleSheet* aSheet,
uint32_t aSheetType);
// DoReflow returns whether the reflow finished without interruption
// If aFrame is not the root frame, the caller must pass a non-null
// aOverflowTracker.
bool DoReflow(nsIFrame* aFrame, bool aInterruptible,
OverflowChangedTracker* aOverflowTracker);
* Add a solid color item to the bottom of aList with frame aFrame and bounds
* aBounds. Checks first if this needs to be done by checking if aFrame is a
* canvas frame (if the AddCanvasBackgroundColorFlags::ForceDraw is passed
* then this check is skipped). aBackstopColor is composed behind the
* background color of the canvas, it is transparent by default.
* We attempt to make the background color part of the scrolled canvas (to
* reduce transparent layers), and if async scrolling is enabled (and the
* background is opaque) then we add a second, unscrolled item to handle the
* checkerboarding case.
* AddCanvasBackgroundColorFlags::AddSubDocument should be specified when
* calling this for a subdocument, and LayoutUseContainersForRootFrame might
* cause the whole list to be scrolled. In that case the second unscrolled
* item will be elided.
* AddCanvasBackgroundColorFlags::AppendUnscrolledOnly only attempts to add
* the unscrolled item, so that we can add it manually after
* LayoutUseContainersForRootFrame has built the scrolling ContainerLayer.
void AddCanvasBackgroundColorItem(
nsDisplayListBuilder* aBuilder, nsDisplayList* aList, nsIFrame* aFrame,
const nsRect& aBounds, nscolor aBackstopColor = NS_RGBA(0, 0, 0, 0),
AddCanvasBackgroundColorFlags aFlags =
size_t SizeOfTextRuns(MallocSizeOf aMallocSizeOf) const;
void SetNextPaintCompressed() { mNextPaintCompressed = true; }
static PresShell* GetShellForEventTarget(nsIFrame* aFrame,
nsIContent* aContent);
static PresShell* GetShellForTouchEvent(WidgetGUIEvent* aEvent);
* Informs the pres shell that the document is now at the anchor with
* the given name. If |aScroll| is true, scrolls the view of the
* document so that the anchor with the specified name is displayed at
* the top of the window. If |aAnchorName| is empty, then this informs
* the pres shell that there is no current target, and |aScroll| must
* be false. If |aAdditionalScrollFlags| is ScrollFlags::ScrollSmoothAuto
* and |aScroll| is true, the scrolling may be performed with an animation.
nsresult GoToAnchor(const nsAString& aAnchorName, bool aScroll,
ScrollFlags aAdditionalScrollFlags = ScrollFlags::None);
* Tells the presshell to scroll again to the last anchor scrolled to by
* GoToAnchor, if any. This scroll only happens if the scroll
* position has not changed since the last GoToAnchor (modulo scroll anchoring
* adjustments). This is called by nsDocumentViewer::LoadComplete. This clears
* the last anchor scrolled to by GoToAnchor (we don't want to keep it alive
* if it's removed from the DOM), so don't call this more than once.
MOZ_CAN_RUN_SCRIPT nsresult ScrollToAnchor();
* When scroll anchoring adjusts positions in the root frame during page load,
* it may move our scroll position in the root frame.
* While that's generally desirable, when scrolling to an anchor via an id-ref
* we have a more direct target. If the id-ref points to something that cannot
* be selected as a scroll anchor container (like an image or an inline), we
* may select a node following it as a scroll anchor, and if then stuff is
* inserted on top, we may end up moving the id-ref element offscreen to the
* top inadvertently.
* On page load, the document viewer will call ScrollToAnchor(), and will only
* scroll to the anchor again if the scroll position is not changed. We don't
* want scroll anchoring adjustments to prevent this, so account for them.
void RootScrollFrameAdjusted(nscoord aYAdjustment) {
if (mLastAnchorScrolledTo) {
mLastAnchorScrollPositionY += aYAdjustment;
* Scrolls the view of the document so that the primary frame of the content
* is displayed in the window. Layout is flushed before scrolling.
* @param aContent The content object of which primary frame should be
* scrolled into view.
* @param aVertical How to align the frame vertically and when to do so.
* This is a ScrollAxis of Where and When.
* @param aHorizontal How to align the frame horizontally and when to do so.
* This is a ScrollAxis of Where and When.
* @param aScrollFlags If ScrollFlags::ScrollFirstAncestorOnly is set,
* only the nearest scrollable ancestor is scrolled,
* otherwise all scrollable ancestors may be scrolled
* if necessary. If ScrollFlags::ScrollOverflowHidden
* is set then we may scroll in a direction even if
* overflow:hidden is specified in that direction;
* otherwise we will not scroll in that direction when
* overflow:hidden is set for that direction. If
* ScrollFlags::ScrollNoParentFrames is set then we
* only scroll nodes in this document, not in any
* parent documents which contain this document in a
* iframe or the like. If ScrollFlags::ScrollSmooth
* is set and CSSOM-VIEW scroll-behavior is enabled,
* we will scroll smoothly using
* nsIScrollableFrame::ScrollMode::SMOOTH_MSD;
* otherwise, nsIScrollableFrame::ScrollMode::INSTANT
* will be used. If ScrollFlags::ScrollSmoothAuto is
* set, the CSSOM-View scroll-behavior attribute is
* set to 'smooth' on the scroll frame, and CSSOM-VIEW
* scroll-behavior is enabled, we will scroll smoothly
* using nsIScrollableFrame::ScrollMode::SMOOTH_MSD;
* otherwise, nsIScrollableFrame::ScrollMode::INSTANT
* will be used.
nsresult ScrollContentIntoView(nsIContent* aContent, ScrollAxis aVertical,
ScrollAxis aHorizontal,
ScrollFlags aScrollFlags);
* When capturing content is set, it traps all mouse events and retargets
* them at this content node. If capturing is not allowed
* (gCaptureInfo.mAllowed is false), then capturing is not set. However, if
* the CaptureFlags::IgnoreAllowedState is set, the allowed state is ignored
* and capturing is set regardless. To disable capture, pass null for the
* value of aContent.
* If CaptureFlags::RetargetedToElement is set, all mouse events are
* targeted at aContent only. Otherwise, mouse events are targeted at
* aContent or its descendants. That is, descendants of aContent receive
* mouse events as they normally would, but mouse events outside of aContent
* are retargeted to aContent.
* If CaptureFlags::PreventDragStart is set then drags are prevented from
* starting while this capture is active.
* If CaptureFlags::PointerLock is set, similar to
* CaptureFlags::RetargetToElement, then events are targeted at aContent,
* but capturing is held more strongly (i.e., calls to SetCapturingContent()
* won't unlock unless CaptureFlags::PointerLock is set again).
static void SetCapturingContent(nsIContent* aContent, CaptureFlags aFlags);
* Alias for SetCapturingContent(nullptr, CaptureFlags::None) for making
* callers what they do clearer.
static void ReleaseCapturingContent() {
PresShell::SetCapturingContent(nullptr, CaptureFlags::None);
// Called at the end of nsLayoutUtils::PaintFrame() if we were painting to
// the widget.
// This is used to clear any pending visual scroll updates that have been
// acknowledged, to make sure they don't stick around for the next paint.
void EndPaint();
* Tell the presshell that the given frame's reflow was interrupted. This
* will mark as having dirty children a path from the given frame (inclusive)
* to the nearest ancestor with a dirty subtree, or to the reflow root
* currently being reflowed if no such ancestor exists (inclusive). This is
* to be done immediately after reflow of the current reflow root completes.
* This method must only be called during reflow, and the frame it's being
* called on must be in the process of being reflowed when it's called. This
* method doesn't mark any intrinsic widths dirty and doesn't add any bits
void FrameNeedsToContinueReflow(nsIFrame* aFrame);
* Notification sent by a frame informing the pres shell that it is about to
* be destroyed.
* This allows any outstanding references to the frame to be cleaned up
void NotifyDestroyingFrame(nsIFrame* aFrame);
#ifdef DEBUG
nsIFrame* GetDrawEventTargetFrame() { return mDrawEventTargetFrame; }
* Refresh observer management.
void DoObserveStyleFlushes();
void DoObserveLayoutFlushes();
* Does the actual work of figuring out the current state of font size
* inflation.
bool DetermineFontSizeInflationState();
void RecordAlloc(void* aPtr) {
void RecordFree(void* aPtr) {
void PushCurrentEventInfo(nsIFrame* aFrame, nsIContent* aContent);
void PopCurrentEventInfo();
nsIContent* GetCurrentEventContent();
friend class ::nsRefreshDriver;
friend class ::nsAutoCauseReflowNotifier;
void WillCauseReflow();
MOZ_CAN_RUN_SCRIPT void DidCauseReflow();
void CancelPostedReflowCallbacks();
void FlushPendingScrollAnchorAdjustments();
void SetPendingVisualScrollUpdate(
const nsPoint& aVisualViewportOffset,
FrameMetrics::ScrollOffsetUpdateType aUpdateType);
UniquePtr<ReflowCountMgr> mReflowCountMgr;
void WillDoReflow();
// This data is stored as a content property (nsGkAtoms::scrolling) on
// mContentToScrollTo when we have a pending ScrollIntoView.
struct ScrollIntoViewData {
ScrollAxis mContentScrollVAxis;
ScrollAxis mContentScrollHAxis;
ScrollFlags mContentToScrollToFlags;
static LazyLogModule gLog;
DOMHighResTimeStamp GetPerformanceNowUnclamped();
// The callback for the mReflowContinueTimer timer.
static void sReflowContinueCallback(nsITimer* aTimer, void* aPresShell);
bool ScheduleReflowOffTimer();
// MaybeScheduleReflow checks if posting a reflow is needed, then checks if
// the last reflow was interrupted. In the interrupted case ScheduleReflow is
// called off a timer, otherwise it is called directly.
void MaybeScheduleReflow();
// Actually schedules a reflow. This should only be called by
// MaybeScheduleReflow and the reflow timer ScheduleReflowOffTimer
// sets up.
void ScheduleReflow();
friend class ::AutoPointerEventTargetUpdater;
// ProcessReflowCommands returns whether we processed all our dirty roots
// without interruptions.
MOZ_CAN_RUN_SCRIPT bool ProcessReflowCommands(bool aInterruptible);
* Callback handler for whether reflow happened.
* @param aInterruptible Whether or not reflow interruption is allowed.
MOZ_CAN_RUN_SCRIPT void DidDoReflow(bool aInterruptible);
MOZ_CAN_RUN_SCRIPT void HandlePostedReflowCallbacks(bool aInterruptible);
* Helper for ScrollContentIntoView()
MOZ_CAN_RUN_SCRIPT void DoScrollContentIntoView();
* Methods to handle changes to user and UA sheet lists that we get
* notified about.
void AddUserSheet(StyleSheet*);
void AddAgentSheet(StyleSheet*);
void AddAuthorSheet(StyleSheet*);
void RemovePreferenceStyles();
* Initialize cached font inflation preference values and do an initial
* computation to determine if font inflation is enabled.
* @see nsLayoutUtils::sFontSizeInflationEmPerLine
* @see nsLayoutUtils::sFontSizeInflationMinTwips
* @see nsLayoutUtils::sFontSizeInflationLineThreshold
void SetupFontInflation();
* Implementation methods for FlushPendingNotifications.
MOZ_CAN_RUN_SCRIPT void DoFlushPendingNotifications(FlushType aType);
MOZ_CAN_RUN_SCRIPT void DoFlushPendingNotifications(ChangesToFlush aType);
struct RenderingState {
explicit RenderingState(PresShell* aPresShell)
: mResolution(aPresShell->mResolution),
mRenderingStateFlags(aPresShell->mRenderingStateFlags) {}
Maybe<float> mResolution;
RenderingStateFlags mRenderingStateFlags;
struct AutoSaveRestoreRenderingState {
explicit AutoSaveRestoreRenderingState(PresShell* aPresShell)
: mPresShell(aPresShell), mOldState(aPresShell) {}
~AutoSaveRestoreRenderingState() {
mPresShell->mRenderingStateFlags = mOldState.mRenderingStateFlags;
mPresShell->mResolution = mOldState.mResolution;
PresShell* mPresShell;
RenderingState mOldState;
void SetRenderingState(const RenderingState& aState);
friend class ::nsPresShellEventCB;
// methods for painting a range to an offscreen buffer
// given a display list, clip the items within the list to
// the range
nsRect ClipListToRange(nsDisplayListBuilder* aBuilder, nsDisplayList* aList,
nsRange* aRange);
// create a RangePaintInfo for the range aRange containing the
// display list needed to paint the range to a surface
UniquePtr<RangePaintInfo> CreateRangePaintInfo(nsRange* aRange,
nsRect& aSurfaceRect,
bool aForPrimarySelection);
* Paint the items to a new surface and return it.
* aSelection - selection being painted, if any
* aRegion - clip region, if any
* aArea - area that the surface occupies, relative to the root frame
* aPoint - reference point, typically the mouse position
* aScreenRect - [out] set to the area of the screen the painted area should
* be displayed at
* aFlags - set RenderImageFlags::AutoScale to scale down large images, but
* it must not be set if a custom image was specified
already_AddRefed<SourceSurface> PaintRangePaintInfo(
const nsTArray<UniquePtr<RangePaintInfo>>& aItems,
dom::Selection* aSelection, const Maybe<CSSIntRegion>& aRegion,
nsRect aArea, const LayoutDeviceIntPoint aPoint,
LayoutDeviceIntRect* aScreenRect, RenderImageFlags aFlags);
// Hide a view if it is a popup
void HideViewIfPopup(nsView* aView);
// Utility method to restore the root scrollframe state
void RestoreRootScrollPosition();
void MaybeReleaseCapturingContent();
class DelayedEvent {
virtual ~DelayedEvent() = default;
virtual void Dispatch() {}
virtual bool IsKeyPressEvent() { return false; }
class DelayedInputEvent : public DelayedEvent {
void Dispatch() override;
~DelayedInputEvent() override;
WidgetInputEvent* mEvent;
class DelayedMouseEvent : public DelayedInputEvent {
explicit DelayedMouseEvent(WidgetMouseEvent* aEvent);
class DelayedKeyEvent : public DelayedInputEvent {
explicit DelayedKeyEvent(WidgetKeyboardEvent* aEvent);
bool IsKeyPressEvent() override;
// Check if aEvent is a mouse event and record the mouse location for later
// synth mouse moves.
void RecordMouseLocation(WidgetGUIEvent* aEvent);
inline bool MouseLocationWasSetBySynthesizedMouseEventForTests() const;
class nsSynthMouseMoveEvent final : public nsARefreshObserver {
nsSynthMouseMoveEvent(PresShell* aPresShell, bool aFromScroll)
: mPresShell(aPresShell), mFromScroll(aFromScroll) {
NS_ASSERTION(mPresShell, "null parameter");
// Private destructor, to discourage deletion outside of Release():
~nsSynthMouseMoveEvent() { Revoke(); }
NS_INLINE_DECL_REFCOUNTING(nsSynthMouseMoveEvent, override)
void Revoke();
void WillRefresh(TimeStamp aTime) override {
if (mPresShell) {
RefPtr<PresShell> shell = mPresShell;
PresShell* mPresShell;
bool mFromScroll;
MOZ_CAN_RUN_SCRIPT void ProcessSynthMouseMoveEvent(bool aFromScroll);
void QueryIsActive();
nsresult UpdateImageLockingState();
already_AddRefed<PresShell> GetParentPresShellForEventHandling();
* EventHandler is implementation of PresShell::HandleEvent().
class MOZ_STACK_CLASS EventHandler final {
EventHandler() = delete;
EventHandler(const EventHandler& aOther) = delete;
explicit EventHandler(PresShell& aPresShell)
: mPresShell(aPresShell), mCurrentEventInfoSetter(nullptr) {}
explicit EventHandler(RefPtr<PresShell>&& aPresShell)
: mPresShell(std::move(aPresShell)), mCurrentEventInfoSetter(nullptr) {}
* HandleEvent() may dispatch aGUIEvent. This may redirect the event to
* another PresShell, or the event may be handled by other classes like
* AccessibleCaretEventHub, or discarded. Otherwise, this sets current
* event info of mPresShell and calls HandleEventWithCurrentEventInfo()
* to dispatch the event into the DOM tree.
* @param aFrameForPresShell The frame for PresShell. If PresShell
* has root frame, it should be set.
* Otherwise, a frame which contains the
* PresShell should be set instead. I.e.,
* in the latter case, the frame is in
* a parent document.
* @param aGUIEvent Event to be handled. Must be a trusted
* event.
* @param aDontRetargetEvents true if this shouldn't redirect the
* event to different PresShell.
* false if this can redirect the event to
* different PresShell.
* @param aEventStatus [in/out] EventStatus of aGUIEvent.
nsresult HandleEvent(nsIFrame* aFrameForPresShell,
WidgetGUIEvent* aGUIEvent, bool aDontRetargetEvents,
nsEventStatus* aEventStatus);
* HandleEventWithTarget() tries to dispatch aEvent on aContent after
* setting current event target content to aNewEventContent and current
* event frame to aNewEventFrame temporarily. Note that this supports
* WidgetEvent, not WidgetGUIEvent. So, you can dispatch a simple event
* with this.
* @param aEvent Event to be dispatched. Must be a
* trusted event.
* @param aNewEventFrame Temporal new event frame.
* @param aNewEventContent Temporal new event content.
* @param aEventStatus [in/out] EventStuatus of aEvent.
* @param aIsHandlingNativeEvent true if aEvent represents a native
* event.
* @param aTargetContent This is used only when aEvent is a
* pointer event. If
* PresShell::mPointerEventTarget is
* changed during dispatching aEvent,
* this is set to the new target.
* @param aOverrideClickTarget Override click event target.
nsresult HandleEventWithTarget(WidgetEvent* aEvent,
nsIFrame* aNewEventFrame,
nsIContent* aNewEventContent,
nsEventStatus* aEventStatus,
bool aIsHandlingNativeEvent,
nsIContent** aTargetContent,
nsIContent* aOverrideClickTarget);
* OnPresShellDestroy() is called when every PresShell instance is being
* destroyed.
static inline void OnPresShellDestroy(Document* aDocument);
static bool InZombieDocument(nsIContent* aContent);
static nsIFrame* GetNearestFrameContainingPresShell(PresShell* aPresShell);
static nsIPrincipal* GetDocumentPrincipalToCompareWithBlacklist(
PresShell& aPresShell);
* HandleEventUsingCoordinates() handles aGUIEvent whose
* IsUsingCoordinates() returns true with the following helper methods.
* @param aFrameForPresShell The frame for PresShell. See
* explanation of HandleEvent() for the
* details.
* @param aGUIEvent The handling event. Make sure that
* its IsUsingCoordinates() returns true.
* @param aEventStatus The status of aGUIEvent.
* @param aDontRetargetEvents true if we've already retarget document.
* Otherwise, false.
nsresult HandleEventUsingCoordinates(nsIFrame* aFrameForPresShell,
WidgetGUIEvent* aGUIEvent,
nsEventStatus* aEventStatus,
bool aDontRetargetEvents);
* EventTargetData struct stores a set of a PresShell (event handler),
* a frame (to handle the event) and a content (event target for the frame).
struct MOZ_STACK_CLASS EventTargetData final {
EventTargetData() = delete;
EventTargetData(const EventTargetData& aOther) = delete;
explicit EventTargetData(nsIFrame* aFrameToHandleEvent) {
void SetFrameAndComputePresShell(nsIFrame* aFrameToHandleEvent);
void SetFrameAndComputePresShellAndContent(nsIFrame* aFrameToHandleEvent,
WidgetGUIEvent* aGUIEvent);
void SetContentForEventFromFrame(WidgetGUIEvent* aGUIEvent);
nsPresContext* GetPresContext() const {
return mPresShell ? mPresShell->GetPresContext() : nullptr;
EventStateManager* GetEventStateManager() const {
nsPresContext* presContext = GetPresContext();
return presContext ? presContext->EventStateManager() : nullptr;
Document* GetDocument() const {
return mPresShell ? mPresShell->GetDocument() : nullptr;
nsIContent* GetFrameContent() const;
* MaybeRetargetToActiveDocument() tries retarget aGUIEvent into
* active document if there is. Note that this does not support to
* retarget mContent. Make sure it is nullptr before calling this.
* @param aGUIEvent The handling event.
* @return true if retargetted.
bool MaybeRetargetToActiveDocument(WidgetGUIEvent* aGUIEvent);
* ComputeElementFromFrame() computes mContent for aGUIEvent. If
* mContent is set by this method, mContent is always nullptr or an
* Element.
* @param aGUIEvent The handling event.
* @return true if caller can keep handling the event.
* Otherwise, false.
* Note that even if this returns true, mContent
* may be nullptr.
bool ComputeElementFromFrame(WidgetGUIEvent* aGUIEvent);
* UpdateTouchEventTarget() updates mFrame, mPresShell and mContent if
* aGUIEvent is a touch event and there is new proper target.
* @param aGUIEvent The handled event. If it's not a touch event,