#include "config.h"
#include "FrameView.h"
#include "AXObjectCache.h"
#include "AnimationController.h"
#include "BackForwardController.h"
#include "CachedImage.h"
#include "CachedResourceLoader.h"
#include "Chrome.h"
#include "ChromeClient.h"
#include "DOMWindow.h"
#include "DebugPageOverlays.h"
#include "DocumentMarkerController.h"
#include "EventHandler.h"
#include "EventNames.h"
#include "FloatRect.h"
#include "FocusController.h"
#include "FrameLoader.h"
#include "FrameLoaderClient.h"
#include "FrameSelection.h"
#include "FrameTree.h"
#include "GraphicsContext.h"
#include "HTMLBodyElement.h"
#include "HTMLDocument.h"
#include "HTMLEmbedElement.h"
#include "HTMLFrameElement.h"
#include "HTMLFrameSetElement.h"
#include "HTMLHtmlElement.h"
#include "HTMLIFrameElement.h"
#include "HTMLNames.h"
#include "HTMLObjectElement.h"
#include "HTMLPlugInImageElement.h"
#include "ImageDocument.h"
#include "InspectorClient.h"
#include "InspectorController.h"
#include "InspectorInstrumentation.h"
#include "Logging.h"
#include "MainFrame.h"
#include "MemoryCache.h"
#include "MemoryPressureHandler.h"
#include "OverflowEvent.h"
#include "Page.h"
#include "PageCache.h"
#include "PageOverlayController.h"
#include "ProgressTracker.h"
#include "RenderEmbeddedObject.h"
#include "RenderFullScreen.h"
#include "RenderIFrame.h"
#include "RenderInline.h"
#include "RenderLayer.h"
#include "RenderLayerBacking.h"
#include "RenderLayerCompositor.h"
#include "RenderSVGRoot.h"
#include "RenderScrollbar.h"
#include "RenderScrollbarPart.h"
#include "RenderStyle.h"
#include "RenderText.h"
#include "RenderTheme.h"
#include "RenderView.h"
#include "RenderWidget.h"
#include "SVGDocument.h"
#include "SVGSVGElement.h"
#include "ScriptedAnimationController.h"
#include "ScrollAnimator.h"
#include "ScrollingCoordinator.h"
#include "Settings.h"
#include "StyleResolver.h"
#include "TextResourceDecoder.h"
#include "TextStream.h"
#include "TiledBacking.h"
#include "WheelEventTestTrigger.h"
#include <wtf/CurrentTime.h>
#include <wtf/Ref.h>
#include <wtf/SystemTracing.h>
#include <wtf/TemporaryChange.h>
#if USE(COORDINATED_GRAPHICS)
#include "TiledBackingStore.h"
#endif
#if ENABLE(TEXT_AUTOSIZING)
#include "TextAutosizer.h"
#endif
#if ENABLE(CSS_SCROLL_SNAP)
#include "AxisScrollSnapOffsets.h"
#endif
#if PLATFORM(IOS)
#include "DocumentLoader.h"
#include "LegacyTileCache.h"
#endif
namespace WebCore {
using namespace HTMLNames;
double FrameView::sCurrentPaintTimeStamp = 0.0;
static const unsigned maxUpdateEmbeddedObjectsIterations = 2;
static RenderLayer::UpdateLayerPositionsFlags updateLayerPositionFlags(RenderLayer* layer, bool isRelayoutingSubtree, bool didFullRepaint)
{
RenderLayer::UpdateLayerPositionsFlags flags = RenderLayer::defaultFlags;
if (didFullRepaint) {
flags &= ~RenderLayer::CheckForRepaint;
flags |= RenderLayer::NeedsFullRepaintInBacking;
}
if (isRelayoutingSubtree && layer->enclosingPaginationLayer(RenderLayer::IncludeCompositedPaginatedLayers))
flags |= RenderLayer::UpdatePagination;
return flags;
}
Pagination::Mode paginationModeForRenderStyle(const RenderStyle& style)
{
EOverflow overflow = style.overflowY();
if (overflow != OPAGEDX && overflow != OPAGEDY)
return Pagination::Unpaginated;
bool isHorizontalWritingMode = style.isHorizontalWritingMode();
TextDirection textDirection = style.direction();
WritingMode writingMode = style.writingMode();
if (overflow == OPAGEDX) {
if ((isHorizontalWritingMode && textDirection == LTR) || writingMode == LeftToRightWritingMode)
return Pagination::LeftToRightPaginated;
return Pagination::RightToLeftPaginated;
}
if (writingMode == TopToBottomWritingMode || (!isHorizontalWritingMode && textDirection == RTL))
return Pagination::TopToBottomPaginated;
return Pagination::BottomToTopPaginated;
}
class SubtreeLayoutStateMaintainer {
public:
SubtreeLayoutStateMaintainer(RenderElement* subtreeLayoutRoot)
: m_layoutRoot(subtreeLayoutRoot)
{
if (m_layoutRoot) {
RenderView& view = m_layoutRoot->view();
view.pushLayoutState(*m_layoutRoot);
m_disableLayoutState = view.shouldDisableLayoutStateForSubtree(m_layoutRoot);
if (m_disableLayoutState)
view.disableLayoutState();
}
}
~SubtreeLayoutStateMaintainer()
{
if (m_layoutRoot) {
RenderView& view = m_layoutRoot->view();
view.popLayoutState(*m_layoutRoot);
if (m_disableLayoutState)
view.enableLayoutState();
}
}
private:
RenderElement* m_layoutRoot { nullptr };
bool m_disableLayoutState { false };
};
FrameView::FrameView(Frame& frame)
: m_frame(frame)
, m_canHaveScrollbars(true)
, m_layoutTimer(*this, &FrameView::layoutTimerFired)
, m_layoutPhase(OutsideLayout)
, m_inSynchronousPostLayout(false)
, m_postLayoutTasksTimer(*this, &FrameView::performPostLayoutTasks)
, m_updateEmbeddedObjectsTimer(*this, &FrameView::updateEmbeddedObjectsTimerFired)
, m_isTransparent(false)
, m_baseBackgroundColor(Color::white)
, m_mediaType("screen")
, m_overflowStatusDirty(true)
, m_wasScrolledByUser(false)
, m_inProgrammaticScroll(false)
, m_safeToPropagateScrollToParent(true)
, m_delayedScrollEventTimer(*this, &FrameView::sendScrollEvent)
, m_isTrackingRepaints(false)
, m_shouldUpdateWhileOffscreen(true)
, m_deferSetNeedsLayoutCount(0)
, m_setNeedsLayoutWasDeferred(false)
, m_speculativeTilingEnabled(false)
, m_speculativeTilingEnableTimer(*this, &FrameView::speculativeTilingEnableTimerFired)
#if PLATFORM(IOS)
, m_useCustomFixedPositionLayoutRect(false)
, m_useCustomSizeForResizeEvent(false)
#endif
, m_hasOverrideViewportSize(false)
, m_shouldAutoSize(false)
, m_inAutoSize(false)
, m_didRunAutosize(false)
, m_autoSizeFixedMinimumHeight(0)
, m_headerHeight(0)
, m_footerHeight(0)
, m_milestonesPendingPaint(0)
, m_visualUpdatesAllowedByClient(true)
, m_hasFlippedBlockRenderers(false)
, m_scrollPinningBehavior(DoNotPin)
{
init();
#if ENABLE(RUBBER_BANDING)
ScrollElasticity verticalElasticity = ScrollElasticityNone;
ScrollElasticity horizontalElasticity = ScrollElasticityNone;
if (m_frame->isMainFrame()) {
verticalElasticity = m_frame->page() ? m_frame->page()->verticalScrollElasticity() : ScrollElasticityAllowed;
horizontalElasticity = m_frame->page() ? m_frame->page()->horizontalScrollElasticity() : ScrollElasticityAllowed;
} else if (m_frame->settings().rubberBandingForSubScrollableRegionsEnabled()) {
verticalElasticity = ScrollElasticityAutomatic;
horizontalElasticity = ScrollElasticityAutomatic;
}
ScrollableArea::setVerticalScrollElasticity(verticalElasticity);
ScrollableArea::setHorizontalScrollElasticity(horizontalElasticity);
#endif
}
Ref<FrameView> FrameView::create(Frame& frame)
{
Ref<FrameView> view = adoptRef(*new FrameView(frame));
view->show();
return view;
}
Ref<FrameView> FrameView::create(Frame& frame, const IntSize& initialSize)
{
Ref<FrameView> view = adoptRef(*new FrameView(frame));
view->Widget::setFrameRect(IntRect(view->location(), initialSize));
view->show();
return view;
}
FrameView::~FrameView()
{
if (m_postLayoutTasksTimer.isActive())
m_postLayoutTasksTimer.stop();
removeFromAXObjectCache();
resetScrollbars();
ASSERT(!horizontalScrollbar() || !horizontalScrollbar()->isCustomScrollbar());
ASSERT(!verticalScrollbar() || !verticalScrollbar()->isCustomScrollbar());
setHasHorizontalScrollbar(false); setHasVerticalScrollbar(false);
ASSERT(!m_scrollCorner);
ASSERT(frame().view() != this || !frame().contentRenderer());
}
void FrameView::reset()
{
m_cannotBlitToWindow = false;
m_isOverlapped = false;
m_contentIsOpaque = false;
m_layoutTimer.stop();
m_layoutRoot = nullptr;
m_delayedLayout = false;
m_needsFullRepaint = true;
m_layoutSchedulingEnabled = true;
m_layoutPhase = OutsideLayout;
m_inSynchronousPostLayout = false;
m_layoutCount = 0;
m_nestedLayoutCount = 0;
m_postLayoutTasksTimer.stop();
m_updateEmbeddedObjectsTimer.stop();
m_firstLayout = true;
m_firstLayoutCallbackPending = false;
m_wasScrolledByUser = false;
m_safeToPropagateScrollToParent = true;
m_delayedScrollEventTimer.stop();
m_lastViewportSize = IntSize();
m_lastZoomFactor = 1.0f;
m_isTrackingRepaints = false;
m_trackedRepaintRects.clear();
m_lastPaintTime = 0;
m_paintBehavior = PaintBehaviorNormal;
m_isPainting = false;
m_visuallyNonEmptyCharacterCount = 0;
m_visuallyNonEmptyPixelCount = 0;
m_isVisuallyNonEmpty = false;
m_firstVisuallyNonEmptyLayoutCallbackPending = true;
m_viewportIsStable = true;
m_needsDeferredScrollbarsUpdate = false;
m_maintainScrollPositionAnchor = nullptr;
}
void FrameView::removeFromAXObjectCache()
{
if (AXObjectCache* cache = axObjectCache()) {
if (HTMLFrameOwnerElement* owner = frame().ownerElement())
cache->childrenChanged(owner->renderer());
cache->remove(this);
}
}
void FrameView::resetScrollbars()
{
m_firstLayout = true;
setScrollbarsSuppressed(true);
if (m_canHaveScrollbars)
setScrollbarModes(ScrollbarAuto, ScrollbarAuto);
else
setScrollbarModes(ScrollbarAlwaysOff, ScrollbarAlwaysOff);
setScrollbarsSuppressed(false);
}
void FrameView::resetScrollbarsAndClearContentsSize()
{
resetScrollbars();
setScrollbarsSuppressed(true);
setContentsSize(IntSize());
setScrollbarsSuppressed(false);
}
void FrameView::init()
{
reset();
m_margins = LayoutSize(-1, -1); m_size = LayoutSize();
Element* ownerElement = frame().ownerElement();
if (is<HTMLFrameElementBase>(ownerElement)) {
HTMLFrameElementBase& frameElement = downcast<HTMLFrameElementBase>(*ownerElement);
if (frameElement.scrollingMode() == ScrollbarAlwaysOff)
setCanHaveScrollbars(false);
LayoutUnit marginWidth = frameElement.marginWidth();
LayoutUnit marginHeight = frameElement.marginHeight();
if (marginWidth != -1)
setMarginWidth(marginWidth);
if (marginHeight != -1)
setMarginHeight(marginHeight);
}
Page* page = frame().page();
if (page && page->chrome().client().shouldPaintEntireContents())
setPaintsEntireContents(true);
}
void FrameView::prepareForDetach()
{
detachCustomScrollbars();
removeFromAXObjectCache();
if (frame().page()) {
if (ScrollingCoordinator* scrollingCoordinator = frame().page()->scrollingCoordinator())
scrollingCoordinator->willDestroyScrollableArea(*this);
}
}
void FrameView::detachCustomScrollbars()
{
Scrollbar* horizontalBar = horizontalScrollbar();
if (horizontalBar && horizontalBar->isCustomScrollbar())
setHasHorizontalScrollbar(false);
Scrollbar* verticalBar = verticalScrollbar();
if (verticalBar && verticalBar->isCustomScrollbar())
setHasVerticalScrollbar(false);
m_scrollCorner = nullptr;
}
void FrameView::recalculateScrollbarOverlayStyle()
{
ScrollbarOverlayStyle oldOverlayStyle = scrollbarOverlayStyle();
WTF::Optional<ScrollbarOverlayStyle> clientOverlayStyle = frame().page() ? frame().page()->chrome().client().preferredScrollbarOverlayStyle() : ScrollbarOverlayStyleDefault;
if (clientOverlayStyle) {
if (clientOverlayStyle.value() != oldOverlayStyle)
setScrollbarOverlayStyle(clientOverlayStyle.value());
return;
}
ScrollbarOverlayStyle computedOverlayStyle = ScrollbarOverlayStyleDefault;
Color backgroundColor = documentBackgroundColor();
if (backgroundColor.isValid()) {
double hue, saturation, lightness;
backgroundColor.getHSL(hue, saturation, lightness);
if (lightness <= .5 && backgroundColor.alpha() > 0)
computedOverlayStyle = ScrollbarOverlayStyleLight;
}
if (oldOverlayStyle != computedOverlayStyle)
setScrollbarOverlayStyle(computedOverlayStyle);
}
void FrameView::clear()
{
setCanBlitOnScroll(true);
reset();
setScrollbarsSuppressed(true);
#if PLATFORM(IOS)
if (LegacyTileCache* tileCache = legacyTileCache())
tileCache->setTilingMode(LegacyTileCache::Disabled);
#endif
}
#if PLATFORM(IOS)
void FrameView::didReplaceMultipartContent()
{
if (LegacyTileCache* tileCache = legacyTileCache())
tileCache->setTilingMode(LegacyTileCache::Normal);
}
#endif
bool FrameView::didFirstLayout() const
{
return !m_firstLayout;
}
void FrameView::invalidateRect(const IntRect& rect)
{
if (!parent()) {
if (HostWindow* window = hostWindow())
window->invalidateContentsAndRootView(rect);
return;
}
RenderWidget* renderer = frame().ownerRenderer();
if (!renderer)
return;
IntRect repaintRect = rect;
repaintRect.move(renderer->borderLeft() + renderer->paddingLeft(),
renderer->borderTop() + renderer->paddingTop());
renderer->repaintRectangle(repaintRect);
}
void FrameView::setFrameRect(const IntRect& newRect)
{
Ref<FrameView> protectedThis(*this);
IntRect oldRect = frameRect();
if (newRect == oldRect)
return;
#if ENABLE(TEXT_AUTOSIZING)
if (newRect.width() != oldRect.width()) {
if (frame().isMainFrame() && page->settings().textAutosizingEnabled()) {
for (Frame* frame = &page->mainFrame(); frame; frame = frame->tree().traverseNext())
frame().document()->textAutosizer()->recalculateMultipliers();
}
}
#endif
ScrollView::setFrameRect(newRect);
updateScrollableAreaSet();
if (RenderView* renderView = this->renderView()) {
if (renderView->usesCompositing())
renderView->compositor().frameViewDidChangeSize();
}
if (frame().isMainFrame())
frame().mainFrame().pageOverlayController().didChangeViewSize();
viewportContentsChanged();
}
#if ENABLE(REQUEST_ANIMATION_FRAME)
bool FrameView::scheduleAnimation()
{
if (HostWindow* window = hostWindow()) {
window->scheduleAnimation();
return true;
}
return false;
}
#endif
void FrameView::setMarginWidth(LayoutUnit w)
{
m_margins.setWidth(w);
}
void FrameView::setMarginHeight(LayoutUnit h)
{
m_margins.setHeight(h);
}
bool FrameView::frameFlatteningEnabled() const
{
return frame().settings().frameFlatteningEnabled();
}
bool FrameView::isFrameFlatteningValidForThisFrame() const
{
if (!frameFlatteningEnabled())
return false;
HTMLFrameOwnerElement* owner = frame().ownerElement();
if (!owner)
return false;
return owner->hasTagName(frameTag) || owner->hasTagName(iframeTag);
}
bool FrameView::avoidScrollbarCreation() const
{
return isFrameFlatteningValidForThisFrame();
}
void FrameView::setCanHaveScrollbars(bool canHaveScrollbars)
{
m_canHaveScrollbars = canHaveScrollbars;
ScrollView::setCanHaveScrollbars(canHaveScrollbars);
}
void FrameView::updateCanHaveScrollbars()
{
ScrollbarMode hMode;
ScrollbarMode vMode;
scrollbarModes(hMode, vMode);
if (hMode == ScrollbarAlwaysOff && vMode == ScrollbarAlwaysOff)
setCanHaveScrollbars(false);
else
setCanHaveScrollbars(true);
}
PassRefPtr<Scrollbar> FrameView::createScrollbar(ScrollbarOrientation orientation)
{
if (!frame().settings().allowCustomScrollbarInMainFrame() && frame().isMainFrame())
return ScrollView::createScrollbar(orientation);
Document* doc = frame().document();
HTMLElement* body = doc ? doc->bodyOrFrameset() : nullptr;
if (body && body->renderer() && body->renderer()->style().hasPseudoStyle(SCROLLBAR))
return RenderScrollbar::createCustomScrollbar(*this, orientation, body);
Element* docElement = doc ? doc->documentElement() : nullptr;
if (docElement && docElement->renderer() && docElement->renderer()->style().hasPseudoStyle(SCROLLBAR))
return RenderScrollbar::createCustomScrollbar(*this, orientation, docElement);
RenderWidget* frameRenderer = frame().ownerRenderer();
if (frameRenderer && frameRenderer->style().hasPseudoStyle(SCROLLBAR))
return RenderScrollbar::createCustomScrollbar(*this, orientation, nullptr, &frame());
return ScrollView::createScrollbar(orientation);
}
void FrameView::setContentsSize(const IntSize& size)
{
if (size == contentsSize())
return;
m_deferSetNeedsLayoutCount++;
ScrollView::setContentsSize(size);
contentsResized();
Page* page = frame().page();
if (!page)
return;
updateScrollableAreaSet();
page->chrome().contentsSizeChanged(&frame(), size);
if (frame().isMainFrame()) {
frame().mainFrame().pageOverlayController().didChangeDocumentSize();
PageCache::singleton().markPagesForContentsSizeChanged(*page);
}
ASSERT(m_deferSetNeedsLayoutCount);
m_deferSetNeedsLayoutCount--;
if (!m_deferSetNeedsLayoutCount)
m_setNeedsLayoutWasDeferred = false; }
void FrameView::adjustViewSize()
{
RenderView* renderView = this->renderView();
if (!renderView)
return;
ASSERT(frame().view() == this);
const IntRect rect = renderView->documentRect();
const IntSize& size = rect.size();
ScrollView::setScrollOrigin(IntPoint(-rect.x(), -rect.y()), !frame().document()->printing(), size == contentsSize());
LOG_WITH_STREAM(Layout, stream << "FrameView " << this << " adjustViewSize: unscaled document rect changed to " << renderView->unscaledDocumentRect() << " (scaled to " << size << ")");
setContentsSize(size);
}
void FrameView::applyOverflowToViewport(const RenderElement& renderer, ScrollbarMode& hMode, ScrollbarMode& vMode)
{
bool overrideHidden = frame().isMainFrame() && ((frame().frameScaleFactor() > 1) || headerHeight() || footerHeight());
EOverflow overflowX = renderer.style().overflowX();
EOverflow overflowY = renderer.style().overflowY();
if (is<RenderSVGRoot>(renderer)) {
if (downcast<RenderSVGRoot>(renderer).isEmbeddedThroughFrameContainingSVGDocument()) {
overflowX = OHIDDEN;
overflowY = OHIDDEN;
}
}
switch (overflowX) {
case OHIDDEN:
if (overrideHidden)
hMode = ScrollbarAuto;
else
hMode = ScrollbarAlwaysOff;
break;
case OSCROLL:
hMode = ScrollbarAlwaysOn;
break;
case OAUTO:
hMode = ScrollbarAuto;
break;
default:
;
}
switch (overflowY) {
case OHIDDEN:
if (overrideHidden)
vMode = ScrollbarAuto;
else
vMode = ScrollbarAlwaysOff;
break;
case OSCROLL:
vMode = ScrollbarAlwaysOn;
break;
case OAUTO:
vMode = ScrollbarAuto;
break;
default:
;
}
}
void FrameView::applyPaginationToViewport()
{
Document* document = frame().document();
auto* documentElement = document->documentElement();
RenderElement* documentRenderer = documentElement ? documentElement->renderer() : nullptr;
RenderElement* documentOrBodyRenderer = documentRenderer;
auto* body = document->body();
if (body && body->renderer())
documentOrBodyRenderer = documentRenderer->style().overflowX() == OVISIBLE && is<HTMLHtmlElement>(*documentElement) ? body->renderer() : documentRenderer;
Pagination pagination;
if (!documentOrBodyRenderer) {
setPagination(pagination);
return;
}
EOverflow overflowY = documentOrBodyRenderer->style().overflowY();
if (overflowY == OPAGEDX || overflowY == OPAGEDY) {
pagination.mode = WebCore::paginationModeForRenderStyle(documentOrBodyRenderer->style());
pagination.gap = static_cast<unsigned>(documentOrBodyRenderer->style().columnGap());
}
setPagination(pagination);
}
void FrameView::calculateScrollbarModesForLayout(ScrollbarMode& hMode, ScrollbarMode& vMode, ScrollbarModesCalculationStrategy strategy)
{
m_viewportRendererType = ViewportRendererType::None;
const HTMLFrameOwnerElement* owner = frame().ownerElement();
if (owner && (owner->scrollingMode() == ScrollbarAlwaysOff)) {
hMode = ScrollbarAlwaysOff;
vMode = ScrollbarAlwaysOff;
return;
}
if (m_canHaveScrollbars || strategy == RulesFromWebContentOnly) {
hMode = ScrollbarAuto;
vMode = ScrollbarAuto;
} else {
hMode = ScrollbarAlwaysOff;
vMode = ScrollbarAlwaysOff;
}
if (m_layoutRoot)
return;
auto* document = frame().document();
if (!document)
return;
auto* documentElement = document->documentElement();
if (!documentElement)
return;
auto* bodyOrFrameset = document->bodyOrFrameset();
auto* rootRenderer = documentElement->renderer();
if (!bodyOrFrameset || !bodyOrFrameset->renderer()) {
if (rootRenderer) {
applyOverflowToViewport(*rootRenderer, hMode, vMode);
m_viewportRendererType = ViewportRendererType::Document;
}
return;
}
if (is<HTMLFrameSetElement>(*bodyOrFrameset) && !frameFlatteningEnabled()) {
vMode = ScrollbarAlwaysOff;
hMode = ScrollbarAlwaysOff;
return;
}
if (is<HTMLBodyElement>(*bodyOrFrameset) && rootRenderer) {
if (rootRenderer->style().overflowX() == OVISIBLE && is<HTMLHtmlElement>(documentElement)) {
auto* bodyRenderer = bodyOrFrameset->renderer();
if (bodyRenderer) {
applyOverflowToViewport(*bodyRenderer, hMode, vMode);
m_viewportRendererType = ViewportRendererType::Body;
}
} else {
applyOverflowToViewport(*rootRenderer, hMode, vMode);
m_viewportRendererType = ViewportRendererType::Document;
}
}
}
void FrameView::willRecalcStyle()
{
RenderView* renderView = this->renderView();
if (!renderView)
return;
renderView->compositor().willRecalcStyle();
}
bool FrameView::updateCompositingLayersAfterStyleChange()
{
RenderView* renderView = this->renderView();
if (!renderView)
return false;
if (inPreLayoutStyleUpdate() || layoutPending() || renderView->needsLayout())
return false;
return renderView->compositor().didRecalcStyleWithNoPendingLayout();
}
void FrameView::updateCompositingLayersAfterLayout()
{
RenderView* renderView = this->renderView();
if (!renderView)
return;
renderView->compositor().cacheAcceleratedCompositingFlags();
renderView->compositor().updateCompositingLayers(CompositingUpdateAfterLayout);
}
void FrameView::clearBackingStores()
{
RenderView* renderView = this->renderView();
if (!renderView)
return;
RenderLayerCompositor& compositor = renderView->compositor();
ASSERT(compositor.inCompositingMode());
compositor.enableCompositingMode(false);
compositor.clearBackingForAllLayers();
}
void FrameView::restoreBackingStores()
{
RenderView* renderView = this->renderView();
if (!renderView)
return;
RenderLayerCompositor& compositor = renderView->compositor();
compositor.enableCompositingMode(true);
compositor.updateCompositingLayers(CompositingUpdateAfterLayout);
}
GraphicsLayer* FrameView::layerForScrolling() const
{
RenderView* renderView = this->renderView();
if (!renderView)
return nullptr;
return renderView->compositor().scrollLayer();
}
GraphicsLayer* FrameView::layerForHorizontalScrollbar() const
{
RenderView* renderView = this->renderView();
if (!renderView)
return nullptr;
return renderView->compositor().layerForHorizontalScrollbar();
}
GraphicsLayer* FrameView::layerForVerticalScrollbar() const
{
RenderView* renderView = this->renderView();
if (!renderView)
return nullptr;
return renderView->compositor().layerForVerticalScrollbar();
}
GraphicsLayer* FrameView::layerForScrollCorner() const
{
RenderView* renderView = this->renderView();
if (!renderView)
return nullptr;
return renderView->compositor().layerForScrollCorner();
}
TiledBacking* FrameView::tiledBacking() const
{
RenderView* renderView = this->renderView();
if (!renderView)
return nullptr;
RenderLayerBacking* backing = renderView->layer()->backing();
if (!backing)
return nullptr;
return backing->graphicsLayer()->tiledBacking();
}
uint64_t FrameView::scrollLayerID() const
{
RenderView* renderView = this->renderView();
if (!renderView)
return 0;
RenderLayerBacking* backing = renderView->layer()->backing();
if (!backing)
return 0;
return backing->scrollingNodeIDForRole(Scrolling);
}
ScrollableArea* FrameView::scrollableAreaForScrollLayerID(uint64_t nodeID) const
{
RenderView* renderView = this->renderView();
if (!renderView)
return nullptr;
return renderView->compositor().scrollableAreaForScrollLayerID(nodeID);
}
#if ENABLE(RUBBER_BANDING)
GraphicsLayer* FrameView::layerForOverhangAreas() const
{
RenderView* renderView = this->renderView();
if (!renderView)
return nullptr;
return renderView->compositor().layerForOverhangAreas();
}
GraphicsLayer* FrameView::setWantsLayerForTopOverHangArea(bool wantsLayer) const
{
RenderView* renderView = this->renderView();
if (!renderView)
return nullptr;
return renderView->compositor().updateLayerForTopOverhangArea(wantsLayer);
}
GraphicsLayer* FrameView::setWantsLayerForBottomOverHangArea(bool wantsLayer) const
{
RenderView* renderView = this->renderView();
if (!renderView)
return nullptr;
return renderView->compositor().updateLayerForBottomOverhangArea(wantsLayer);
}
#endif // ENABLE(RUBBER_BANDING)
#if ENABLE(CSS_SCROLL_SNAP)
void FrameView::updateSnapOffsets()
{
if (!frame().document())
return;
HTMLElement* body = frame().document()->bodyOrFrameset();
if (!renderView() || !body || !body->renderer())
return;
updateSnapOffsetsForScrollableArea(*this, *body, *renderView(), body->renderer()->style());
}
bool FrameView::isScrollSnapInProgress() const
{
if (scrollbarsSuppressed())
return false;
if (Page* page = frame().page()) {
if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator()) {
if (!scrollingCoordinator->shouldUpdateScrollLayerPositionSynchronously(*this))
return scrollingCoordinator->isScrollSnapInProgress();
}
}
if (ScrollAnimator* scrollAnimator = existingScrollAnimator())
return scrollAnimator->isScrollSnapInProgress();
return false;
}
void FrameView::updateScrollingCoordinatorScrollSnapProperties() const
{
renderView()->compositor().updateScrollSnapPropertiesWithFrameView(*this);
}
#endif
bool FrameView::flushCompositingStateForThisFrame(const Frame& rootFrameForFlush)
{
RenderView* renderView = this->renderView();
if (!renderView)
return true;
ASSERT(frame().view() == this);
if (needsLayout())
return false;
#if PLATFORM(IOS)
if (LegacyTileCache* tileCache = legacyTileCache())
tileCache->doPendingRepaints();
#endif
renderView->compositor().flushPendingLayerChanges(&rootFrameForFlush == m_frame.ptr());
return true;
}
void FrameView::setNeedsOneShotDrawingSynchronization()
{
if (Page* page = frame().page())
page->chrome().client().setNeedsOneShotDrawingSynchronization();
}
GraphicsLayer* FrameView::graphicsLayerForPlatformWidget(PlatformWidget platformWidget)
{
Widget* foundWidget = nullptr;
for (auto& widget : children()) {
if (widget->platformWidget() != platformWidget)
continue;
foundWidget = widget.get();
break;
}
if (!foundWidget)
return nullptr;
auto* renderWidget = RenderWidget::find(foundWidget);
if (!renderWidget)
return nullptr;
RenderLayer* widgetLayer = renderWidget->layer();
if (!widgetLayer || !widgetLayer->isComposited())
return nullptr;
return widgetLayer->backing()->parentForSublayers();
}
void FrameView::scheduleLayerFlushAllowingThrottling()
{
RenderView* view = this->renderView();
if (!view)
return;
view->compositor().scheduleLayerFlush(true );
}
LayoutRect FrameView::fixedScrollableAreaBoundsInflatedForScrolling(const LayoutRect& uninflatedBounds) const
{
LayoutPoint scrollPosition = scrollPositionRespectingCustomFixedPosition();
LayoutSize topLeftExpansion = scrollPosition - minimumScrollPosition();
LayoutSize bottomRightExpansion = maximumScrollPosition() - scrollPosition;
return LayoutRect(uninflatedBounds.location() - topLeftExpansion, uninflatedBounds.size() + topLeftExpansion + bottomRightExpansion);
}
LayoutPoint FrameView::scrollPositionRespectingCustomFixedPosition() const
{
#if PLATFORM(IOS)
return useCustomFixedPositionLayoutRect() ? customFixedPositionLayoutRect().location() : scrollPosition();
#else
return scrollPositionForFixedPosition();
#endif
}
void FrameView::setHeaderHeight(int headerHeight)
{
if (frame().page())
ASSERT(frame().isMainFrame());
m_headerHeight = headerHeight;
if (RenderView* renderView = this->renderView())
renderView->setNeedsLayout();
}
void FrameView::setFooterHeight(int footerHeight)
{
if (frame().page())
ASSERT(frame().isMainFrame());
m_footerHeight = footerHeight;
if (RenderView* renderView = this->renderView())
renderView->setNeedsLayout();
}
float FrameView::topContentInset(TopContentInsetType contentInsetTypeToReturn) const
{
if (platformWidget() && contentInsetTypeToReturn == TopContentInsetType::WebCoreOrPlatformContentInset)
return platformTopContentInset();
if (!frame().isMainFrame())
return 0;
Page* page = frame().page();
return page ? page->topContentInset() : 0;
}
void FrameView::topContentInsetDidChange(float newTopContentInset)
{
RenderView* renderView = this->renderView();
if (!renderView)
return;
if (platformWidget())
platformSetTopContentInset(newTopContentInset);
layout();
updateScrollbars(scrollPosition());
if (renderView->usesCompositing())
renderView->compositor().frameViewDidChangeSize();
if (TiledBacking* tiledBacking = this->tiledBacking())
tiledBacking->setTopContentInset(newTopContentInset);
}
void FrameView::topContentDirectionDidChange()
{
m_needsDeferredScrollbarsUpdate = true;
}
void FrameView::handleDeferredScrollbarsUpdateAfterDirectionChange()
{
if (!m_needsDeferredScrollbarsUpdate)
return;
m_needsDeferredScrollbarsUpdate = false;
ASSERT(m_layoutPhase == InPostLayerPositionsUpdatedAfterLayout);
updateScrollbars(scrollPosition());
positionScrollbarLayers();
}
bool FrameView::hasCompositedContent() const
{
if (RenderView* renderView = this->renderView())
return renderView->compositor().inCompositingMode();
return false;
}
void FrameView::enterCompositingMode()
{
if (RenderView* renderView = this->renderView()) {
renderView->compositor().enableCompositingMode();
if (!needsLayout())
renderView->compositor().scheduleCompositingLayerUpdate();
}
}
bool FrameView::isEnclosedInCompositingLayer() const
{
auto frameOwnerRenderer = frame().ownerRenderer();
if (frameOwnerRenderer && frameOwnerRenderer->containerForRepaint())
return true;
if (FrameView* parentView = parentFrameView())
return parentView->isEnclosedInCompositingLayer();
return false;
}
bool FrameView::flushCompositingStateIncludingSubframes()
{
InspectorInstrumentation::willComposite(frame());
bool allFramesFlushed = flushCompositingStateForThisFrame(frame());
for (Frame* child = frame().tree().firstRenderedChild(); child; child = child->tree().traverseNextRendered(m_frame.ptr())) {
if (!child->view())
continue;
bool flushed = child->view()->flushCompositingStateForThisFrame(frame());
allFramesFlushed &= flushed;
}
return allFramesFlushed;
}
bool FrameView::isSoftwareRenderable() const
{
RenderView* renderView = this->renderView();
return !renderView || !renderView->compositor().has3DContent();
}
void FrameView::setIsInWindow(bool isInWindow)
{
if (RenderView* renderView = this->renderView())
renderView->setIsInWindow(isInWindow);
}
inline void FrameView::forceLayoutParentViewIfNeeded()
{
RenderWidget* ownerRenderer = frame().ownerRenderer();
if (!ownerRenderer)
return;
RenderBox* contentBox = embeddedContentBox();
if (!contentBox)
return;
auto& svgRoot = downcast<RenderSVGRoot>(*contentBox);
if (svgRoot.everHadLayout() && !svgRoot.needsLayout())
return;
LOG(Layout, "FrameView %p forceLayoutParentViewIfNeeded scheduling layout on parent FrameView %p", this, &ownerRenderer->view().frameView());
ownerRenderer->setNeedsLayoutAndPrefWidthsRecalc();
ownerRenderer->view().frameView().scheduleRelayout();
}
void FrameView::layout(bool allowSubtree)
{
LOG(Layout, "FrameView %p (%dx%d) layout, main frameview %d, allowSubtree=%d", this, size().width(), size().height(), frame().isMainFrame(), allowSubtree);
if (isInRenderTreeLayout()) {
LOG(Layout, " in layout, bailing");
return;
}
if (layoutDisallowed()) {
LOG(Layout, " layout is disallowed, bailing");
return;
}
Ref<FrameView> protectedThis(*this);
TemporaryChange<LayoutPhase> layoutPhaseRestorer(m_layoutPhase, InPreLayout);
TemporaryChange<bool> changeInProgrammaticScroll(m_inProgrammaticScroll, true);
bool inChildFrameLayoutWithFrameFlattening = isInChildFrameWithFrameFlattening();
if (inChildFrameLayoutWithFrameFlattening) {
startLayoutAtMainFrameViewIfNeeded(allowSubtree);
RenderElement* root = m_layoutRoot ? m_layoutRoot : frame().document()->renderView();
if (!root || !root->needsLayout())
return;
}
TraceScope tracingScope(LayoutStart, LayoutEnd);
#if PLATFORM(IOS)
if (updateFixedPositionLayoutRect())
allowSubtree = false;
#endif
m_layoutTimer.stop();
m_delayedLayout = false;
m_setNeedsLayoutWasDeferred = false;
ASSERT(!isPainting());
if (isPainting())
return;
InspectorInstrumentationCookie cookie = InspectorInstrumentation::willLayout(frame());
AnimationUpdateBlock animationUpdateBlock(&frame().animation());
if (!allowSubtree && m_layoutRoot)
convertSubtreeLayoutToFullLayout();
ASSERT(frame().view() == this);
ASSERT(frame().document());
Document& document = *frame().document();
ASSERT(!document.inPageCache());
{
TemporaryChange<bool> changeSchedulingEnabled(m_layoutSchedulingEnabled, false);
if (!m_nestedLayoutCount && !m_inSynchronousPostLayout && m_postLayoutTasksTimer.isActive() && !inChildFrameLayoutWithFrameFlattening) {
TemporaryChange<bool> inSynchronousPostLayoutChange(m_inSynchronousPostLayout, true);
performPostLayoutTasks();
}
m_layoutPhase = InPreLayoutStyleUpdate;
StyleResolver* styleResolver = document.styleResolverIfExists();
if (!styleResolver || styleResolver->hasMediaQueriesAffectedByViewportChange()) {
LOG(Layout, " hasMediaQueriesAffectedByViewportChange, enqueueing style recalc");
document.styleResolverChanged(DeferRecalcStyle);
InspectorInstrumentation::mediaQueryResultChanged(document);
} else
document.evaluateMediaQueryList();
applyPaginationToViewport();
document.updateStyleIfNeeded();
if (hasOneRef())
return;
}
m_layoutPhase = InPreLayout;
RenderLayer* layer = nullptr;
bool subtree = false;
RenderElement* root = nullptr;
++m_nestedLayoutCount;
{
TemporaryChange<bool> changeSchedulingEnabled(m_layoutSchedulingEnabled, false);
autoSizeIfEnabled();
root = m_layoutRoot ? m_layoutRoot : document.renderView();
if (!root)
return;
subtree = m_layoutRoot;
if (!m_layoutRoot) {
auto* body = document.bodyOrFrameset();
if (body && body->renderer()) {
if (is<HTMLFrameSetElement>(*body) && !frameFlatteningEnabled()) {
body->renderer()->setChildNeedsLayout();
} else if (is<HTMLBodyElement>(*body)) {
if (!m_firstLayout && m_size.height() != layoutHeight() && body->renderer()->enclosingBox().stretchesToViewport())
body->renderer()->setChildNeedsLayout();
}
}
#ifdef INSTRUMENT_LAYOUT_SCHEDULING
if (m_firstLayout && !frame().ownerElement())
printf("Elapsed time before first layout: %lld\n", document.elapsedTime().count());
#endif
}
m_needsFullRepaint = !subtree && (m_firstLayout || downcast<RenderView>(*root).printing());
if (!subtree) {
ScrollbarMode hMode;
ScrollbarMode vMode;
calculateScrollbarModesForLayout(hMode, vMode);
if (m_firstLayout || (hMode != horizontalScrollbarMode() || vMode != verticalScrollbarMode())) {
if (m_firstLayout) {
setScrollbarsSuppressed(true);
m_firstLayout = false;
m_firstLayoutCallbackPending = true;
m_lastViewportSize = sizeForResizeEvent();
m_lastZoomFactor = root->style().zoom();
if (vMode == ScrollbarAuto)
setVerticalScrollbarMode(ScrollbarAlwaysOn); if (hMode == ScrollbarAuto)
setHorizontalScrollbarMode(ScrollbarAlwaysOff); Page* page = frame().page();
if (page && page->expectsWheelEventTriggers())
scrollAnimator().setWheelEventTestTrigger(page->testTrigger());
setScrollbarModes(hMode, vMode);
setScrollbarsSuppressed(false, true);
} else
setScrollbarModes(hMode, vMode);
}
LayoutSize oldSize = m_size;
m_size = layoutSize();
if (oldSize != m_size) {
LOG(Layout, " layout size changed from %.3fx%.3f to %.3fx%.3f", oldSize.width().toFloat(), oldSize.height().toFloat(), m_size.width().toFloat(), m_size.height().toFloat());
m_needsFullRepaint = true;
if (!m_firstLayout) {
RenderBox* rootRenderer = document.documentElement() ? document.documentElement()->renderBox() : nullptr;
auto* body = document.bodyOrFrameset();
RenderBox* bodyRenderer = rootRenderer && body ? body->renderBox() : nullptr;
if (bodyRenderer && bodyRenderer->stretchesToViewport())
bodyRenderer->setChildNeedsLayout();
else if (rootRenderer && rootRenderer->stretchesToViewport())
rootRenderer->setChildNeedsLayout();
}
}
m_layoutPhase = InPreLayout;
}
layer = root->enclosingLayer();
SubtreeLayoutStateMaintainer subtreeLayoutStateMaintainer(m_layoutRoot);
RenderView::RepaintRegionAccumulator repaintRegionAccumulator(&root->view());
ASSERT(m_layoutPhase == InPreLayout);
m_layoutPhase = InRenderTreeLayout;
forceLayoutParentViewIfNeeded();
ASSERT(m_layoutPhase == InRenderTreeLayout);
root->layout();
#if ENABLE(IOS_TEXT_AUTOSIZING)
if (frame().settings().textAutosizingEnabled() && !root->view().printing()) {
float minimumZoomFontSize = frame().settings().minimumZoomFontSize();
float textAutosizingWidth = frame().page() ? frame().page()->textAutosizingWidth() : 0;
if (int overrideWidth = frame().settings().textAutosizingWindowSizeOverride().width())
textAutosizingWidth = overrideWidth;
LOG(TextAutosizing, "Text Autosizing: minimumZoomFontSize=%.2f textAutosizingWidth=%.2f", minimumZoomFontSize, textAutosizingWidth);
if (minimumZoomFontSize && textAutosizingWidth) {
root->adjustComputedFontSizesOnBlocks(minimumZoomFontSize, textAutosizingWidth);
if (root->needsLayout())
root->layout();
}
}
#endif
#if ENABLE(TEXT_AUTOSIZING)
if (document.textAutosizer()->processSubtree(root) && root->needsLayout())
root->layout();
#endif
ASSERT(m_layoutPhase == InRenderTreeLayout);
m_layoutRoot = nullptr;
}
m_layoutPhase = InViewSizeAdjust;
bool neededFullRepaint = m_needsFullRepaint;
if (!subtree && !downcast<RenderView>(*root).printing())
adjustViewSize();
m_layoutPhase = InPostLayout;
m_needsFullRepaint = neededFullRepaint;
if (m_needsFullRepaint)
root->view().repaintRootContents();
root->view().releaseProtectedRenderWidgets();
ASSERT(!root->needsLayout());
layer->updateLayerPositionsAfterLayout(renderView()->layer(), updateLayerPositionFlags(layer, subtree, m_needsFullRepaint));
updateCompositingLayersAfterLayout();
m_layoutPhase = InPostLayerPositionsUpdatedAfterLayout;
m_layoutCount++;
#if PLATFORM(COCOA) || PLATFORM(WIN) || PLATFORM(GTK) || PLATFORM(EFL)
if (AXObjectCache* cache = root->document().existingAXObjectCache())
cache->postNotification(root, AXObjectCache::AXLayoutComplete);
#endif
#if ENABLE(DASHBOARD_SUPPORT)
updateAnnotatedRegions();
#endif
#if ENABLE(IOS_TOUCH_EVENTS)
document.dirtyTouchEventRects();
#endif
updateCanBlitOnScrollRecursively();
handleDeferredScrollUpdateAfterContentSizeChange();
handleDeferredScrollbarsUpdateAfterDirectionChange();
if (document.hasListenerType(Document::OVERFLOWCHANGED_LISTENER))
updateOverflowStatus(layoutWidth() < contentsWidth(), layoutHeight() < contentsHeight());
frame().document()->markers().invalidateRectsForAllMarkers();
if (!m_postLayoutTasksTimer.isActive()) {
if (!m_inSynchronousPostLayout) {
if (inChildFrameLayoutWithFrameFlattening)
updateWidgetPositions();
else {
TemporaryChange<bool> inSynchronousPostLayoutChange(m_inSynchronousPostLayout, true);
performPostLayoutTasks(); }
}
if (!m_postLayoutTasksTimer.isActive() && (needsLayout() || m_inSynchronousPostLayout || inChildFrameLayoutWithFrameFlattening)) {
m_postLayoutTasksTimer.startOneShot(0);
if (needsLayout())
layout();
}
}
InspectorInstrumentation::didLayout(cookie, root);
DebugPageOverlays::didLayout(frame());
--m_nestedLayoutCount;
}
bool FrameView::shouldDeferScrollUpdateAfterContentSizeChange()
{
return (m_layoutPhase < InPostLayout) && (m_layoutPhase != OutsideLayout);
}
RenderBox* FrameView::embeddedContentBox() const
{
RenderView* renderView = this->renderView();
if (!renderView)
return nullptr;
RenderObject* firstChild = renderView->firstChild();
if (is<RenderSVGRoot>(firstChild))
return downcast<RenderSVGRoot>(firstChild);
return nullptr;
}
void FrameView::addEmbeddedObjectToUpdate(RenderEmbeddedObject& embeddedObject)
{
if (!m_embeddedObjectsToUpdate)
m_embeddedObjectsToUpdate = std::make_unique<ListHashSet<RenderEmbeddedObject*>>();
HTMLFrameOwnerElement& element = embeddedObject.frameOwnerElement();
if (is<HTMLObjectElement>(element) || is<HTMLEmbedElement>(element)) {
HTMLPlugInImageElement& pluginElement = downcast<HTMLPlugInImageElement>(element);
if (!pluginElement.needsCheckForSizeChange())
pluginElement.setNeedsWidgetUpdate(true);
}
m_embeddedObjectsToUpdate->add(&embeddedObject);
}
void FrameView::removeEmbeddedObjectToUpdate(RenderEmbeddedObject& embeddedObject)
{
if (!m_embeddedObjectsToUpdate)
return;
m_embeddedObjectsToUpdate->remove(&embeddedObject);
}
void FrameView::setMediaType(const String& mediaType)
{
m_mediaType = mediaType;
}
String FrameView::mediaType() const
{
String overrideType = frame().loader().client().overrideMediaType();
InspectorInstrumentation::applyEmulatedMedia(frame(), overrideType);
if (!overrideType.isNull())
return overrideType;
return m_mediaType;
}
void FrameView::adjustMediaTypeForPrinting(bool printing)
{
if (printing) {
if (m_mediaTypeWhenNotPrinting.isNull())
m_mediaTypeWhenNotPrinting = mediaType();
setMediaType("print");
} else {
if (!m_mediaTypeWhenNotPrinting.isNull())
setMediaType(m_mediaTypeWhenNotPrinting);
m_mediaTypeWhenNotPrinting = String();
}
}
bool FrameView::useSlowRepaints(bool considerOverlap) const
{
bool mustBeSlow = hasSlowRepaintObjects() || (platformWidget() && hasViewportConstrainedObjects());
if (usesCompositedScrolling() && !platformWidget())
return mustBeSlow;
bool isOverlapped = m_isOverlapped && considerOverlap;
if (mustBeSlow || m_cannotBlitToWindow || isOverlapped || !m_contentIsOpaque)
return true;
if (FrameView* parentView = parentFrameView())
return parentView->useSlowRepaints(considerOverlap);
return false;
}
bool FrameView::useSlowRepaintsIfNotOverlapped() const
{
return useSlowRepaints(false);
}
void FrameView::updateCanBlitOnScrollRecursively()
{
for (auto* frame = m_frame.ptr(); frame; frame = frame->tree().traverseNext(m_frame.ptr())) {
if (FrameView* view = frame->view())
view->setCanBlitOnScroll(!view->useSlowRepaints());
}
}
bool FrameView::usesCompositedScrolling() const
{
RenderView* renderView = this->renderView();
if (renderView && renderView->isComposited()) {
GraphicsLayer* layer = renderView->layer()->backing()->graphicsLayer();
if (layer && layer->drawsContent())
return true;
}
return false;
}
bool FrameView::usesAsyncScrolling() const
{
#if ENABLE(ASYNC_SCROLLING)
if (Page* page = frame().page()) {
if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
return scrollingCoordinator->coordinatesScrollingForFrameView(*this);
}
#endif
return false;
}
bool FrameView::usesMockScrollAnimator() const
{
return Settings::usesMockScrollAnimator();
}
void FrameView::logMockScrollAnimatorMessage(const String& message) const
{
Document* document = frame().document();
if (!document)
return;
StringBuilder builder;
if (frame().isMainFrame())
builder.appendLiteral("Main");
builder.appendLiteral("FrameView: ");
builder.append(message);
document->addConsoleMessage(MessageSource::Other, MessageLevel::Debug, builder.toString());
}
void FrameView::setCannotBlitToWindow()
{
m_cannotBlitToWindow = true;
updateCanBlitOnScrollRecursively();
}
void FrameView::addSlowRepaintObject(RenderElement* o)
{
bool hadSlowRepaintObjects = hasSlowRepaintObjects();
if (!m_slowRepaintObjects)
m_slowRepaintObjects = std::make_unique<HashSet<const RenderElement*>>();
m_slowRepaintObjects->add(o);
if (!hadSlowRepaintObjects) {
updateCanBlitOnScrollRecursively();
if (Page* page = frame().page()) {
if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
scrollingCoordinator->frameViewHasSlowRepaintObjectsDidChange(*this);
}
}
}
void FrameView::removeSlowRepaintObject(RenderElement* o)
{
if (!m_slowRepaintObjects)
return;
m_slowRepaintObjects->remove(o);
if (m_slowRepaintObjects->isEmpty()) {
m_slowRepaintObjects = nullptr;
updateCanBlitOnScrollRecursively();
if (Page* page = frame().page()) {
if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
scrollingCoordinator->frameViewHasSlowRepaintObjectsDidChange(*this);
}
}
}
void FrameView::addViewportConstrainedObject(RenderElement* object)
{
if (!m_viewportConstrainedObjects)
m_viewportConstrainedObjects = std::make_unique<ViewportConstrainedObjectSet>();
if (!m_viewportConstrainedObjects->contains(object)) {
m_viewportConstrainedObjects->add(object);
if (platformWidget())
updateCanBlitOnScrollRecursively();
if (Page* page = frame().page()) {
if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
scrollingCoordinator->frameViewFixedObjectsDidChange(*this);
}
}
}
void FrameView::removeViewportConstrainedObject(RenderElement* object)
{
if (m_viewportConstrainedObjects && m_viewportConstrainedObjects->remove(object)) {
if (Page* page = frame().page()) {
if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
scrollingCoordinator->frameViewFixedObjectsDidChange(*this);
}
updateCanBlitOnScrollRecursively();
}
}
LayoutRect FrameView::viewportConstrainedVisibleContentRect() const
{
#if PLATFORM(IOS)
if (useCustomFixedPositionLayoutRect())
return customFixedPositionLayoutRect();
#endif
LayoutRect viewportRect = visibleContentRect();
viewportRect.setLocation(scrollPositionForFixedPosition());
return viewportRect;
}
float FrameView::frameScaleFactor() const
{
return frame().frameScaleFactor();
}
LayoutPoint FrameView::scrollPositionForFixedPosition(const LayoutRect& visibleContentRect, const LayoutSize& totalContentsSize, const LayoutPoint& scrollPosition, const LayoutPoint& scrollOrigin, float frameScaleFactor, bool fixedElementsLayoutRelativeToFrame, ScrollBehaviorForFixedElements behaviorForFixed, int headerHeight, int footerHeight)
{
LayoutPoint position;
if (behaviorForFixed == StickToDocumentBounds)
position = ScrollableArea::constrainScrollPositionForOverhang(visibleContentRect, totalContentsSize, scrollPosition, scrollOrigin, headerHeight, footerHeight);
else {
position = scrollPosition;
position.setY(position.y() - headerHeight);
}
LayoutSize maxSize = totalContentsSize - visibleContentRect.size();
float dragFactorX = (fixedElementsLayoutRelativeToFrame || !maxSize.width()) ? 1 : (totalContentsSize.width() - visibleContentRect.width() * frameScaleFactor) / maxSize.width();
float dragFactorY = (fixedElementsLayoutRelativeToFrame || !maxSize.height()) ? 1 : (totalContentsSize.height() - visibleContentRect.height() * frameScaleFactor) / maxSize.height();
return LayoutPoint(position.x() * dragFactorX / frameScaleFactor, position.y() * dragFactorY / frameScaleFactor);
}
float FrameView::yPositionForInsetClipLayer(const FloatPoint& scrollPosition, float topContentInset)
{
if (!topContentInset)
return 0;
float scrollY = std::max<float>(0, scrollPosition.y());
if (scrollY >= topContentInset)
return 0;
return topContentInset - scrollY;
}
float FrameView::yPositionForHeaderLayer(const FloatPoint& scrollPosition, float topContentInset)
{
if (!topContentInset)
return 0;
float scrollY = std::max<float>(0, scrollPosition.y());
if (scrollY >= topContentInset)
return topContentInset;
return scrollY;
}
float FrameView::yPositionForFooterLayer(const FloatPoint& scrollPosition, float topContentInset, float totalContentsHeight, float footerHeight)
{
return yPositionForHeaderLayer(scrollPosition, topContentInset) + totalContentsHeight - footerHeight;
}
FloatPoint FrameView::positionForRootContentLayer(const FloatPoint& scrollPosition, const FloatPoint& scrollOrigin, float topContentInset, float headerHeight)
{
return FloatPoint(0, yPositionForHeaderLayer(scrollPosition, topContentInset) + headerHeight) - toFloatSize(scrollOrigin);
}
FloatPoint FrameView::positionForRootContentLayer() const
{
return positionForRootContentLayer(scrollPosition(), scrollOrigin(), topContentInset(), headerHeight());
}
#if PLATFORM(IOS)
LayoutRect FrameView::rectForViewportConstrainedObjects(const LayoutRect& visibleContentRect, const LayoutSize& totalContentsSize, float frameScaleFactor, bool fixedElementsLayoutRelativeToFrame, ScrollBehaviorForFixedElements scrollBehavior)
{
if (fixedElementsLayoutRelativeToFrame)
return visibleContentRect;
if (totalContentsSize.isEmpty())
return visibleContentRect;
const LayoutUnit maxContentWidthForZoomThreshold = LayoutUnit::fromPixel(1024);
float zoomedOutScale = frameScaleFactor * visibleContentRect.width() / std::min(maxContentWidthForZoomThreshold, totalContentsSize.width());
float constraintThresholdScale = 1.5 * zoomedOutScale;
float maxPostionedObjectsRectScale = std::min(frameScaleFactor, constraintThresholdScale);
LayoutRect viewportConstrainedObjectsRect = visibleContentRect;
if (frameScaleFactor > constraintThresholdScale) {
FloatRect contentRect(FloatPoint(), totalContentsSize);
FloatRect viewportRect = visibleContentRect;
FloatSize sizeDelta = contentRect.size() - viewportRect.size();
FloatPoint scaleOrigin;
scaleOrigin.setX(contentRect.x() + sizeDelta.width() > 0 ? contentRect.width() * (viewportRect.x() - contentRect.x()) / sizeDelta.width() : 0);
scaleOrigin.setY(contentRect.y() + sizeDelta.height() > 0 ? contentRect.height() * (viewportRect.y() - contentRect.y()) / sizeDelta.height() : 0);
AffineTransform rescaleTransform = AffineTransform::translation(scaleOrigin.x(), scaleOrigin.y());
rescaleTransform.scale(frameScaleFactor / maxPostionedObjectsRectScale, frameScaleFactor / maxPostionedObjectsRectScale);
rescaleTransform = CGAffineTransformTranslate(rescaleTransform, -scaleOrigin.x(), -scaleOrigin.y());
viewportConstrainedObjectsRect = enclosingLayoutRect(rescaleTransform.mapRect(visibleContentRect));
}
if (scrollBehavior == StickToDocumentBounds) {
LayoutRect documentBounds(LayoutPoint(), totalContentsSize);
viewportConstrainedObjectsRect.intersect(documentBounds);
}
return viewportConstrainedObjectsRect;
}
LayoutRect FrameView::viewportConstrainedObjectsRect() const
{
return rectForViewportConstrainedObjects(visibleContentRect(), totalContentsSize(), frame().frameScaleFactor(), fixedElementsLayoutRelativeToFrame(), scrollBehaviorForFixedElements());
}
#endif
ScrollPosition FrameView::minimumScrollPosition() const
{
ScrollPosition minimumPosition = ScrollView::minimumScrollPosition();
if (frame().isMainFrame() && m_scrollPinningBehavior == PinToBottom)
minimumPosition.setY(maximumScrollPosition().y());
return minimumPosition;
}
ScrollPosition FrameView::maximumScrollPosition() const
{
ScrollPosition maximumPosition = ScrollView::maximumScrollPosition();
if (frame().isMainFrame() && m_scrollPinningBehavior == PinToTop)
maximumPosition.setY(minimumScrollPosition().y());
return maximumPosition;
}
void FrameView::viewportContentsChanged()
{
if (!frame().view()) {
return;
}
applyRecursivelyWithVisibleRect([] (FrameView& frameView, const IntRect& visibleRect) {
frameView.resumeVisibleImageAnimations(visibleRect);
frameView.updateScriptedAnimationsAndTimersThrottlingState(visibleRect);
if (auto* renderView = frameView.frame().contentRenderer())
renderView->updateVisibleViewportRect(visibleRect);
});
}
bool FrameView::fixedElementsLayoutRelativeToFrame() const
{
return frame().settings().fixedElementsLayoutRelativeToFrame();
}
IntPoint FrameView::lastKnownMousePosition() const
{
return frame().eventHandler().lastKnownMousePosition();
}
bool FrameView::isHandlingWheelEvent() const
{
return frame().eventHandler().isHandlingWheelEvent();
}
bool FrameView::shouldSetCursor() const
{
Page* page = frame().page();
return page && page->isVisible() && page->focusController().isActive();
}
bool FrameView::scrollContentsFastPath(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect)
{
if (!m_viewportConstrainedObjects || m_viewportConstrainedObjects->isEmpty()) {
hostWindow()->scroll(scrollDelta, rectToScroll, clipRect);
return true;
}
const bool isCompositedContentLayer = usesCompositedScrolling();
Region regionToUpdate;
for (auto& renderer : *m_viewportConstrainedObjects) {
if (!renderer->style().hasViewportConstrainedPosition())
continue;
if (renderer->isComposited())
continue;
ASSERT(renderer->hasLayer());
RenderLayer* layer = downcast<RenderBoxModelObject>(*renderer).layer();
if (layer->viewportConstrainedNotCompositedReason() == RenderLayer::NotCompositedForBoundsOutOfView
|| layer->viewportConstrainedNotCompositedReason() == RenderLayer::NotCompositedForNoVisibleContent) {
continue;
}
if (layer->hasAncestorWithFilterOutsets()) {
return false;
}
IntRect updateRect = enclosingIntRect(layer->repaintRectIncludingNonCompositingDescendants());
updateRect = contentsToRootView(updateRect);
if (!isCompositedContentLayer && clipsRepaints())
updateRect.intersect(rectToScroll);
if (!updateRect.isEmpty())
regionToUpdate.unite(updateRect);
}
hostWindow()->scroll(scrollDelta, rectToScroll, clipRect);
for (auto& updateRect : regionToUpdate.rects()) {
IntRect scrolledRect = updateRect;
scrolledRect.move(scrollDelta);
updateRect.unite(scrolledRect);
if (isCompositedContentLayer) {
updateRect = rootViewToContents(updateRect);
ASSERT(renderView());
renderView()->layer()->setBackingNeedsRepaintInRect(updateRect);
continue;
}
if (clipsRepaints())
updateRect.intersect(rectToScroll);
hostWindow()->invalidateContentsAndRootView(updateRect);
}
return true;
}
void FrameView::scrollContentsSlowPath(const IntRect& updateRect)
{
repaintSlowRepaintObjects();
if (!usesCompositedScrolling() && isEnclosedInCompositingLayer()) {
if (RenderWidget* frameRenderer = frame().ownerRenderer()) {
LayoutRect rect(frameRenderer->borderLeft() + frameRenderer->paddingLeft(), frameRenderer->borderTop() + frameRenderer->paddingTop(),
visibleWidth(), visibleHeight());
frameRenderer->repaintRectangle(rect);
return;
}
}
ScrollView::scrollContentsSlowPath(updateRect);
}
void FrameView::repaintSlowRepaintObjects()
{
if (!m_slowRepaintObjects)
return;
for (auto& renderer : *m_slowRepaintObjects)
renderer->repaintSlowRepaintObject();
}
void FrameView::setIsOverlapped(bool isOverlapped)
{
if (isOverlapped == m_isOverlapped)
return;
m_isOverlapped = isOverlapped;
updateCanBlitOnScrollRecursively();
}
bool FrameView::isOverlappedIncludingAncestors() const
{
if (isOverlapped())
return true;
if (FrameView* parentView = parentFrameView()) {
if (parentView->isOverlapped())
return true;
}
return false;
}
void FrameView::setContentIsOpaque(bool contentIsOpaque)
{
if (contentIsOpaque == m_contentIsOpaque)
return;
m_contentIsOpaque = contentIsOpaque;
updateCanBlitOnScrollRecursively();
}
void FrameView::restoreScrollbar()
{
setScrollbarsSuppressed(false);
}
bool FrameView::scrollToFragment(const URL& url)
{
if (!url.hasFragmentIdentifier() && !frame().document()->cssTarget())
return false;
String fragmentIdentifier = url.fragmentIdentifier();
if (scrollToAnchor(fragmentIdentifier))
return true;
if (TextResourceDecoder* decoder = frame().document()->decoder())
return scrollToAnchor(decodeURLEscapeSequences(fragmentIdentifier, decoder->encoding()));
return false;
}
bool FrameView::scrollToAnchor(const String& name)
{
ASSERT(frame().document());
auto& document = *frame().document();
if (!document.haveStylesheetsLoaded()) {
document.setGotoAnchorNeededAfterStylesheetsLoad(true);
return false;
}
document.setGotoAnchorNeededAfterStylesheetsLoad(false);
Element* anchorElement = document.findAnchor(name);
document.setCSSTarget(anchorElement);
if (is<SVGDocument>(document)) {
if (auto* rootElement = downcast<SVGDocument>(document).rootElement()) {
rootElement->scrollToAnchor(name, anchorElement);
if (!anchorElement)
return true;
}
}
if (!anchorElement && !(name.isEmpty() || equalLettersIgnoringASCIICase(name, "top")))
return false;
ContainerNode* scrollPositionAnchor = anchorElement;
if (!scrollPositionAnchor)
scrollPositionAnchor = frame().document();
maintainScrollPositionAtAnchor(scrollPositionAnchor);
if (anchorElement) {
if (anchorElement->isFocusable())
document.setFocusedElement(anchorElement);
else {
document.setFocusedElement(nullptr);
document.setFocusNavigationStartingNode(anchorElement);
}
}
return true;
}
void FrameView::maintainScrollPositionAtAnchor(ContainerNode* anchorNode)
{
m_maintainScrollPositionAnchor = anchorNode;
if (!m_maintainScrollPositionAnchor)
return;
frame().document()->updateStyleIfNeeded();
RenderView* renderView = this->renderView();
if (renderView && renderView->needsLayout())
layout();
else
scrollToAnchor();
}
void FrameView::scrollElementToRect(const Element& element, const IntRect& rect)
{
frame().document()->updateLayoutIgnorePendingStylesheets();
LayoutRect bounds;
if (RenderElement* renderer = element.renderer())
bounds = renderer->anchorRect();
int centeringOffsetX = (rect.width() - bounds.width()) / 2;
int centeringOffsetY = (rect.height() - bounds.height()) / 2;
setScrollPosition(IntPoint(bounds.x() - centeringOffsetX - rect.x(), bounds.y() - centeringOffsetY - rect.y()));
}
void FrameView::setScrollPosition(const ScrollPosition& scrollPosition)
{
TemporaryChange<bool> changeInProgrammaticScroll(m_inProgrammaticScroll, true);
m_maintainScrollPositionAnchor = nullptr;
Page* page = frame().page();
if (page && page->expectsWheelEventTriggers())
scrollAnimator().setWheelEventTestTrigger(page->testTrigger());
ScrollView::setScrollPosition(scrollPosition);
}
void FrameView::contentsResized()
{
if (delegatesScrolling())
adjustTiledBackingScrollability();
}
void FrameView::delegatesScrollingDidChange()
{
if (hasCompositedContent())
clearBackingStores();
}
#if USE(COORDINATED_GRAPHICS)
void FrameView::setFixedVisibleContentRect(const IntRect& visibleContentRect)
{
bool visibleContentSizeDidChange = false;
if (visibleContentRect.size() != this->fixedVisibleContentRect().size()) {
setViewportConstrainedObjectsNeedLayout();
visibleContentSizeDidChange = true;
}
IntPoint oldPosition = scrollPosition();
ScrollView::setFixedVisibleContentRect(visibleContentRect);
IntPoint newPosition = scrollPosition();
if (oldPosition != newPosition) {
updateLayerPositionsAfterScrolling();
if (frame().settings().acceleratedCompositingForFixedPositionEnabled())
updateCompositingLayersAfterScrolling();
scrollAnimator().setCurrentPosition(newPosition);
scrollPositionChanged(oldPosition, newPosition);
}
if (visibleContentSizeDidChange) {
updateScrollbars(scrollPosition());
}
didChangeScrollOffset();
}
#endif
void FrameView::setViewportConstrainedObjectsNeedLayout()
{
if (!hasViewportConstrainedObjects())
return;
for (auto& renderer : *m_viewportConstrainedObjects)
renderer->setNeedsLayout();
}
void FrameView::didChangeScrollOffset()
{
frame().mainFrame().pageOverlayController().didScrollFrame(frame());
frame().loader().client().didChangeScrollOffset();
}
void FrameView::scrollOffsetChangedViaPlatformWidgetImpl(const ScrollOffset& oldOffset, const ScrollOffset& newOffset)
{
updateLayerPositionsAfterScrolling();
updateCompositingLayersAfterScrolling();
repaintSlowRepaintObjects();
scrollPositionChanged(scrollPositionFromOffset(oldOffset), scrollPositionFromOffset(newOffset));
}
void FrameView::scrollPositionChanged(const ScrollPosition& oldPosition, const ScrollPosition& newPosition)
{
Page* page = frame().page();
auto throttlingDelay = page ? page->chrome().client().eventThrottlingDelay() : 0ms;
if (throttlingDelay == 0ms) {
m_delayedScrollEventTimer.stop();
sendScrollEvent();
} else if (!m_delayedScrollEventTimer.isActive())
m_delayedScrollEventTimer.startOneShot(throttlingDelay);
if (Document* document = frame().document())
document->sendWillRevealEdgeEventsIfNeeded(oldPosition, newPosition, visibleContentRect(), contentsSize());
if (RenderView* renderView = this->renderView()) {
if (renderView->usesCompositing())
renderView->compositor().frameViewDidScroll();
}
viewportContentsChanged();
}
void FrameView::applyRecursivelyWithVisibleRect(const std::function<void (FrameView& frameView, const IntRect& visibleRect)>& apply)
{
IntRect windowClipRect = this->windowClipRect();
auto visibleRect = windowToContents(windowClipRect);
apply(*this, visibleRect);
TemporaryChange<IntRect*> windowClipRectCache(m_cachedWindowClipRect, &windowClipRect);
for (Frame* childFrame = frame().tree().firstChild(); childFrame; childFrame = childFrame->tree().nextSibling()) {
if (auto* childView = childFrame->view())
childView->applyRecursivelyWithVisibleRect(apply);
}
}
void FrameView::resumeVisibleImageAnimations(const IntRect& visibleRect)
{
if (visibleRect.isEmpty())
return;
if (auto* renderView = frame().contentRenderer())
renderView->resumePausedImageAnimationsIfNeeded(visibleRect);
}
void FrameView::updateScriptedAnimationsAndTimersThrottlingState(const IntRect& visibleRect)
{
if (frame().isMainFrame())
return;
auto* document = frame().document();
if (!document)
return;
bool shouldThrottle = visibleRect.isEmpty() && !m_size.isEmpty() && frame().ownerRenderer();
#if ENABLE(REQUEST_ANIMATION_FRAME)
if (auto* scriptedAnimationController = document->scriptedAnimationController())
scriptedAnimationController->setThrottled(shouldThrottle);
#endif
document->setTimerThrottlingEnabled(shouldThrottle);
}
void FrameView::resumeVisibleImageAnimationsIncludingSubframes()
{
applyRecursivelyWithVisibleRect([] (FrameView& frameView, const IntRect& visibleRect) {
frameView.resumeVisibleImageAnimations(visibleRect);
});
}
void FrameView::updateLayerPositionsAfterScrolling()
{
if (m_layoutPhase == InViewSizeAdjust)
return;
if (m_nestedLayoutCount <= 1 && hasViewportConstrainedObjects()) {
if (RenderView* renderView = this->renderView()) {
updateWidgetPositions();
renderView->layer()->updateLayerPositionsAfterDocumentScroll();
}
}
}
bool FrameView::shouldUpdateCompositingLayersAfterScrolling() const
{
#if ENABLE(ASYNC_SCROLLING)
Page* page = frame().page();
if (!page)
return true;
if (&page->mainFrame() != m_frame.ptr())
return true;
ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator();
if (!scrollingCoordinator)
return true;
if (!scrollingCoordinator->supportsFixedPositionLayers())
return true;
if (scrollingCoordinator->shouldUpdateScrollLayerPositionSynchronously(*this))
return true;
if (inProgrammaticScroll())
return true;
return false;
#endif
return true;
}
void FrameView::updateCompositingLayersAfterScrolling()
{
ASSERT(m_layoutPhase >= InPostLayout || m_layoutPhase == OutsideLayout);
if (!shouldUpdateCompositingLayersAfterScrolling())
return;
if (m_nestedLayoutCount <= 1 && hasViewportConstrainedObjects()) {
if (RenderView* renderView = this->renderView())
renderView->compositor().updateCompositingLayers(CompositingUpdateOnScroll);
}
}
bool FrameView::isRubberBandInProgress() const
{
if (scrollbarsSuppressed())
return false;
if (Page* page = frame().page()) {
if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator()) {
if (!scrollingCoordinator->shouldUpdateScrollLayerPositionSynchronously(*this))
return scrollingCoordinator->isRubberBandInProgress();
}
}
if (ScrollAnimator* scrollAnimator = existingScrollAnimator())
return scrollAnimator->isRubberBandInProgress();
return false;
}
bool FrameView::requestScrollPositionUpdate(const ScrollPosition& position)
{
LOG_WITH_STREAM(Scrolling, stream << "FrameView::requestScrollPositionUpdate " << position);
#if ENABLE(ASYNC_SCROLLING)
if (TiledBacking* tiledBacking = this->tiledBacking())
tiledBacking->prepopulateRect(FloatRect(position, visibleContentRect().size()));
#endif
#if ENABLE(ASYNC_SCROLLING) || USE(COORDINATED_GRAPHICS)
if (Page* page = frame().page()) {
if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
return scrollingCoordinator->requestScrollPositionUpdate(*this, position);
}
#else
UNUSED_PARAM(position);
#endif
return false;
}
HostWindow* FrameView::hostWindow() const
{
if (Page* page = frame().page())
return &page->chrome();
return nullptr;
}
void FrameView::addTrackedRepaintRect(const FloatRect& r)
{
if (!m_isTrackingRepaints || r.isEmpty())
return;
FloatRect repaintRect = r;
repaintRect.moveBy(-scrollPosition());
m_trackedRepaintRects.append(repaintRect);
}
void FrameView::repaintContentRectangle(const IntRect& r)
{
ASSERT(!frame().ownerElement());
if (!shouldUpdate())
return;
ScrollView::repaintContentRectangle(r);
}
static unsigned countRenderedCharactersInRenderObjectWithThreshold(const RenderElement& renderer, unsigned threshold)
{
unsigned count = 0;
for (const RenderObject* descendant = &renderer; descendant; descendant = descendant->nextInPreOrder()) {
if (is<RenderText>(*descendant)) {
count += downcast<RenderText>(*descendant).text()->length();
if (count >= threshold)
break;
}
}
return count;
}
bool FrameView::renderedCharactersExceed(unsigned threshold)
{
if (!frame().contentRenderer())
return false;
return countRenderedCharactersInRenderObjectWithThreshold(*frame().contentRenderer(), threshold) >= threshold;
}
void FrameView::availableContentSizeChanged(AvailableSizeChangeReason reason)
{
if (Document* document = frame().document())
document->updateViewportUnitsOnResize();
setNeedsLayout();
ScrollView::availableContentSizeChanged(reason);
}
bool FrameView::shouldLayoutAfterContentsResized() const
{
return !useFixedLayout() || useCustomFixedPositionLayoutRect();
}
void FrameView::updateContentsSize()
{
if (!frame().view())
return;
#if PLATFORM(IOS)
if (RenderView* root = m_frame->contentRenderer()) {
if (useCustomFixedPositionLayoutRect() && hasViewportConstrainedObjects()) {
setViewportConstrainedObjectsNeedLayout();
root->compositor().enableCompositingMode();
}
}
#endif
if (shouldLayoutAfterContentsResized() && needsLayout())
layout();
if (RenderView* renderView = this->renderView()) {
if (renderView->usesCompositing())
renderView->compositor().frameViewDidChangeSize();
}
}
void FrameView::addedOrRemovedScrollbar()
{
if (RenderView* renderView = this->renderView()) {
if (renderView->usesCompositing())
renderView->compositor().frameViewDidAddOrRemoveScrollbars();
}
adjustTiledBackingScrollability();
}
void FrameView::adjustTiledBackingScrollability()
{
auto* tiledBacking = this->tiledBacking();
if (!tiledBacking)
return;
bool horizontallyScrollable;
bool verticallyScrollable;
bool clippedByAncestorView = static_cast<bool>(m_viewExposedRect);
#if PLATFORM(IOS)
if (Page* page = frame().page())
clippedByAncestorView |= page->enclosedInScrollableAncestorView();
#endif
if (delegatesScrolling()) {
IntSize documentSize = contentsSize();
IntSize visibleSize = this->visibleSize();
horizontallyScrollable = clippedByAncestorView || documentSize.width() > visibleSize.width();
verticallyScrollable = clippedByAncestorView || documentSize.height() > visibleSize.height();
} else {
horizontallyScrollable = clippedByAncestorView || horizontalScrollbar();
verticallyScrollable = clippedByAncestorView || verticalScrollbar();
}
TiledBacking::Scrollability scrollability = TiledBacking::NotScrollable;
if (horizontallyScrollable)
scrollability = TiledBacking::HorizontallyScrollable;
if (verticallyScrollable)
scrollability |= TiledBacking::VerticallyScrollable;
tiledBacking->setScrollability(scrollability);
}
#if PLATFORM(IOS)
void FrameView::unobscuredContentSizeChanged()
{
adjustTiledBackingScrollability();
}
#endif
static LayerFlushThrottleState::Flags determineLayerFlushThrottleState(Page& page)
{
if (!page.progress().isMainLoadProgressing())
return 0;
if (page.mainFrame().view()->wasScrolledByUser())
return 0;
auto* document = page.mainFrame().document();
if (!document || is<ImageDocument>(*document))
return 0;
return LayerFlushThrottleState::Enabled;
}
void FrameView::disableLayerFlushThrottlingTemporarilyForInteraction()
{
if (!frame().page())
return;
auto& page = *frame().page();
LayerFlushThrottleState::Flags flags = LayerFlushThrottleState::UserIsInteracting | determineLayerFlushThrottleState(page);
if (page.chrome().client().adjustLayerFlushThrottling(flags))
return;
if (RenderView* view = renderView())
view->compositor().disableLayerFlushThrottlingTemporarilyForInteraction();
}
void FrameView::loadProgressingStatusChanged()
{
updateLayerFlushThrottling();
adjustTiledBackingCoverage();
}
void FrameView::updateLayerFlushThrottling()
{
Page* page = frame().page();
if (!page)
return;
ASSERT(frame().isMainFrame());
LayerFlushThrottleState::Flags flags = determineLayerFlushThrottleState(*page);
if (page->chrome().client().adjustLayerFlushThrottling(flags))
return;
for (auto* frame = m_frame.ptr(); frame; frame = frame->tree().traverseNext(m_frame.ptr())) {
if (RenderView* renderView = frame->contentRenderer())
renderView->compositor().setLayerFlushThrottlingEnabled(flags & LayerFlushThrottleState::Enabled);
}
}
void FrameView::adjustTiledBackingCoverage()
{
if (!m_speculativeTilingEnabled)
enableSpeculativeTilingIfNeeded();
RenderView* renderView = this->renderView();
if (renderView && renderView->layer()->backing())
renderView->layer()->backing()->adjustTiledBackingCoverage();
#if PLATFORM(IOS)
if (LegacyTileCache* tileCache = legacyTileCache())
tileCache->setSpeculativeTileCreationEnabled(m_speculativeTilingEnabled);
#endif
}
static bool shouldEnableSpeculativeTilingDuringLoading(const FrameView& view)
{
Page* page = view.frame().page();
return page && view.isVisuallyNonEmpty() && !page->progress().isMainLoadProgressing();
}
void FrameView::enableSpeculativeTilingIfNeeded()
{
ASSERT(!m_speculativeTilingEnabled);
if (m_wasScrolledByUser) {
m_speculativeTilingEnabled = true;
return;
}
if (!shouldEnableSpeculativeTilingDuringLoading(*this))
return;
if (m_speculativeTilingEnableTimer.isActive())
return;
static const double speculativeTilingEnableDelay = 0.5;
m_speculativeTilingEnableTimer.startOneShot(speculativeTilingEnableDelay);
}
void FrameView::speculativeTilingEnableTimerFired()
{
if (m_speculativeTilingEnabled)
return;
m_speculativeTilingEnabled = shouldEnableSpeculativeTilingDuringLoading(*this);
adjustTiledBackingCoverage();
}
void FrameView::show()
{
ScrollView::show();
if (frame().isMainFrame()) {
m_speculativeTilingEnabled = false;
m_wasScrolledByUser = false;
adjustTiledBackingCoverage();
}
}
void FrameView::convertSubtreeLayoutToFullLayout()
{
ASSERT(m_layoutRoot);
m_layoutRoot->markContainingBlocksForLayout(ScheduleRelayout::No);
m_layoutRoot = nullptr;
}
void FrameView::layoutTimerFired()
{
#ifdef INSTRUMENT_LAYOUT_SCHEDULING
if (!frame().document()->ownerElement())
printf("Layout timer fired at %lld\n", frame().document()->elapsedTime().count());
#endif
layout();
}
void FrameView::scheduleRelayout()
{
ASSERT(frame().view() == this);
if (m_layoutRoot)
convertSubtreeLayoutToFullLayout();
if (!m_layoutSchedulingEnabled)
return;
if (!needsLayout())
return;
if (!frame().document()->shouldScheduleLayout())
return;
InspectorInstrumentation::didInvalidateLayout(frame());
if (frame().ownerRenderer() && isInChildFrameWithFrameFlattening())
frame().ownerRenderer()->setNeedsLayout(MarkContainingBlockChain);
std::chrono::milliseconds delay = frame().document()->minimumLayoutDelay();
if (m_layoutTimer.isActive() && m_delayedLayout && !delay.count())
unscheduleRelayout();
if (m_layoutTimer.isActive())
return;
m_delayedLayout = delay.count();
#ifdef INSTRUMENT_LAYOUT_SCHEDULING
if (!frame().document()->ownerElement())
printf("Scheduling layout for %d\n", delay);
#endif
m_layoutTimer.startOneShot(delay);
}
static bool isObjectAncestorContainerOf(RenderObject* ancestor, RenderObject* descendant)
{
for (RenderObject* r = descendant; r; r = r->container()) {
if (r == ancestor)
return true;
}
return false;
}
void FrameView::scheduleRelayoutOfSubtree(RenderElement& newRelayoutRoot)
{
ASSERT(renderView());
const RenderView& renderView = *this->renderView();
ASSERT(!renderView.documentBeingDestroyed());
ASSERT(frame().view() == this);
if (renderView.needsLayout() && !m_layoutRoot) {
m_layoutRoot = &newRelayoutRoot;
convertSubtreeLayoutToFullLayout();
return;
}
if (!layoutPending() && m_layoutSchedulingEnabled) {
std::chrono::milliseconds delay = renderView.document().minimumLayoutDelay();
ASSERT(!newRelayoutRoot.container() || is<RenderView>(newRelayoutRoot.container()) || !newRelayoutRoot.container()->needsLayout());
m_layoutRoot = &newRelayoutRoot;
InspectorInstrumentation::didInvalidateLayout(frame());
m_delayedLayout = delay.count();
m_layoutTimer.startOneShot(delay);
return;
}
if (m_layoutRoot == &newRelayoutRoot)
return;
if (!m_layoutRoot) {
newRelayoutRoot.markContainingBlocksForLayout(ScheduleRelayout::No);
InspectorInstrumentation::didInvalidateLayout(frame());
return;
}
if (isObjectAncestorContainerOf(m_layoutRoot, &newRelayoutRoot)) {
newRelayoutRoot.markContainingBlocksForLayout(ScheduleRelayout::No, m_layoutRoot);
ASSERT(!m_layoutRoot->container() || is<RenderView>(m_layoutRoot->container()) || !m_layoutRoot->container()->needsLayout());
return;
}
if (isObjectAncestorContainerOf(&newRelayoutRoot, m_layoutRoot)) {
m_layoutRoot->markContainingBlocksForLayout(ScheduleRelayout::No, &newRelayoutRoot);
m_layoutRoot = &newRelayoutRoot;
ASSERT(!m_layoutRoot->container() || is<RenderView>(m_layoutRoot->container()) || !m_layoutRoot->container()->needsLayout());
InspectorInstrumentation::didInvalidateLayout(frame());
return;
}
convertSubtreeLayoutToFullLayout();
newRelayoutRoot.markContainingBlocksForLayout(ScheduleRelayout::No);
InspectorInstrumentation::didInvalidateLayout(frame());
}
bool FrameView::layoutPending() const
{
return m_layoutTimer.isActive();
}
bool FrameView::needsStyleRecalcOrLayout(bool includeSubframes) const
{
if (frame().document() && frame().document()->childNeedsStyleRecalc())
return true;
if (needsLayout())
return true;
if (!includeSubframes)
return false;
for (auto& frameView : renderedChildFrameViews()) {
if (frameView->needsStyleRecalcOrLayout())
return true;
}
return false;
}
bool FrameView::needsLayout() const
{
RenderView* renderView = this->renderView();
return layoutPending()
|| (renderView && renderView->needsLayout())
|| m_layoutRoot
|| (m_deferSetNeedsLayoutCount && m_setNeedsLayoutWasDeferred);
}
void FrameView::setNeedsLayout()
{
if (m_deferSetNeedsLayoutCount) {
m_setNeedsLayoutWasDeferred = true;
return;
}
if (auto* renderView = this->renderView()) {
ASSERT(!renderView->inHitTesting());
renderView->setNeedsLayout();
}
}
void FrameView::unscheduleRelayout()
{
if (m_postLayoutTasksTimer.isActive())
m_postLayoutTasksTimer.stop();
if (!m_layoutTimer.isActive())
return;
#ifdef INSTRUMENT_LAYOUT_SCHEDULING
if (!frame().document()->ownerElement())
printf("Layout timer unscheduled at %d\n", frame().document()->elapsedTime());
#endif
m_layoutTimer.stop();
m_delayedLayout = false;
}
#if ENABLE(REQUEST_ANIMATION_FRAME)
void FrameView::serviceScriptedAnimations()
{
for (auto* frame = m_frame.ptr(); frame; frame = frame->tree().traverseNext()) {
frame->view()->serviceScrollAnimations();
frame->animation().serviceAnimations();
}
if (!frame().document() || !frame().document()->domWindow())
return;
Vector<RefPtr<Document>> documents;
for (auto* frame = m_frame.ptr(); frame; frame = frame->tree().traverseNext())
documents.append(frame->document());
double timestamp = frame().document()->domWindow()->nowTimestamp();
for (auto& document : documents)
document->serviceScriptedAnimations(timestamp);
}
#endif
bool FrameView::isTransparent() const
{
return m_isTransparent;
}
void FrameView::setTransparent(bool isTransparent)
{
if (m_isTransparent == isTransparent)
return;
m_isTransparent = isTransparent;
if (!isViewForDocumentInFrame())
return;
renderView()->compositor().rootBackgroundTransparencyChanged();
}
bool FrameView::hasOpaqueBackground() const
{
return !m_isTransparent && !m_baseBackgroundColor.hasAlpha();
}
Color FrameView::baseBackgroundColor() const
{
return m_baseBackgroundColor;
}
void FrameView::setBaseBackgroundColor(const Color& backgroundColor)
{
bool hadAlpha = m_baseBackgroundColor.hasAlpha();
if (!backgroundColor.isValid())
m_baseBackgroundColor = Color::white;
else
m_baseBackgroundColor = backgroundColor;
if (!isViewForDocumentInFrame())
return;
recalculateScrollbarOverlayStyle();
if (m_baseBackgroundColor.hasAlpha() != hadAlpha)
renderView()->compositor().rootBackgroundTransparencyChanged();
}
void FrameView::updateBackgroundRecursively(const Color& backgroundColor, bool transparent)
{
for (auto* frame = m_frame.ptr(); frame; frame = frame->tree().traverseNext(m_frame.ptr())) {
if (FrameView* view = frame->view()) {
view->setTransparent(transparent);
view->setBaseBackgroundColor(backgroundColor);
}
}
}
bool FrameView::hasExtendedBackgroundRectForPainting() const
{
if (!frame().settings().backgroundShouldExtendBeyondPage())
return false;
TiledBacking* tiledBacking = this->tiledBacking();
if (!tiledBacking)
return false;
return tiledBacking->hasMargins();
}
void FrameView::updateExtendBackgroundIfNecessary()
{
ExtendedBackgroundMode mode = calculateExtendedBackgroundMode();
if (mode == ExtendedBackgroundModeNone)
return;
updateTilesForExtendedBackgroundMode(mode);
}
FrameView::ExtendedBackgroundMode FrameView::calculateExtendedBackgroundMode() const
{
#if PLATFORM(IOS)
return ExtendedBackgroundModeNone;
#else
if (!frame().settings().backgroundShouldExtendBeyondPage())
return ExtendedBackgroundModeNone;
if (!frame().isMainFrame())
return ExtendedBackgroundModeNone;
Document* document = frame().document();
if (!document)
return ExtendedBackgroundModeNone;
auto* documentElement = document->documentElement();
auto* documentElementRenderer = documentElement ? documentElement->renderer() : nullptr;
if (!documentElementRenderer)
return ExtendedBackgroundModeNone;
auto& renderer = documentElementRenderer->rendererForRootBackground();
if (!renderer.style().hasBackgroundImage())
return ExtendedBackgroundModeNone;
ExtendedBackgroundMode mode = ExtendedBackgroundModeNone;
if (renderer.style().backgroundRepeatX() == RepeatFill)
mode |= ExtendedBackgroundModeHorizontal;
if (renderer.style().backgroundRepeatY() == RepeatFill)
mode |= ExtendedBackgroundModeVertical;
return mode;
#endif
}
void FrameView::updateTilesForExtendedBackgroundMode(ExtendedBackgroundMode mode)
{
if (!frame().settings().backgroundShouldExtendBeyondPage())
return;
RenderView* renderView = this->renderView();
if (!renderView)
return;
RenderLayerBacking* backing = renderView->layer()->backing();
if (!backing)
return;
TiledBacking* tiledBacking = backing->graphicsLayer()->tiledBacking();
if (!tiledBacking)
return;
ExtendedBackgroundMode existingMode = ExtendedBackgroundModeNone;
if (tiledBacking->hasVerticalMargins())
existingMode |= ExtendedBackgroundModeVertical;
if (tiledBacking->hasHorizontalMargins())
existingMode |= ExtendedBackgroundModeHorizontal;
if (existingMode == mode)
return;
renderView->compositor().setRootExtendedBackgroundColor(mode == ExtendedBackgroundModeAll ? Color() : documentBackgroundColor());
backing->setTiledBackingHasMargins(mode & ExtendedBackgroundModeHorizontal, mode & ExtendedBackgroundModeVertical);
}
IntRect FrameView::extendedBackgroundRectForPainting() const
{
TiledBacking* tiledBacking = this->tiledBacking();
if (!tiledBacking)
return IntRect();
RenderView* renderView = this->renderView();
if (!renderView)
return IntRect();
LayoutRect extendedRect = renderView->unextendedBackgroundRect();
if (!tiledBacking->hasMargins())
return snappedIntRect(extendedRect);
extendedRect.moveBy(LayoutPoint(-tiledBacking->leftMarginWidth(), -tiledBacking->topMarginHeight()));
extendedRect.expand(LayoutSize(tiledBacking->leftMarginWidth() + tiledBacking->rightMarginWidth(), tiledBacking->topMarginHeight() + tiledBacking->bottomMarginHeight()));
return snappedIntRect(extendedRect);
}
bool FrameView::shouldUpdateWhileOffscreen() const
{
return m_shouldUpdateWhileOffscreen;
}
void FrameView::setShouldUpdateWhileOffscreen(bool shouldUpdateWhileOffscreen)
{
m_shouldUpdateWhileOffscreen = shouldUpdateWhileOffscreen;
}
bool FrameView::shouldUpdate() const
{
if (isOffscreen() && !shouldUpdateWhileOffscreen())
return false;
return true;
}
void FrameView::scrollToAnchor()
{
RefPtr<ContainerNode> anchorNode = m_maintainScrollPositionAnchor;
if (!anchorNode)
return;
if (!anchorNode->renderer())
return;
LayoutRect rect;
if (anchorNode != frame().document() && anchorNode->renderer())
rect = anchorNode->renderer()->anchorRect();
if (anchorNode->renderer()->style().isHorizontalWritingMode())
anchorNode->renderer()->scrollRectToVisible(SelectionRevealMode::Reveal, rect, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignTopAlways);
else if (anchorNode->renderer()->style().isFlippedBlocksWritingMode())
anchorNode->renderer()->scrollRectToVisible(SelectionRevealMode::Reveal, rect, ScrollAlignment::alignRightAlways, ScrollAlignment::alignToEdgeIfNeeded);
else
anchorNode->renderer()->scrollRectToVisible(SelectionRevealMode::Reveal, rect, ScrollAlignment::alignLeftAlways, ScrollAlignment::alignToEdgeIfNeeded);
if (AXObjectCache* cache = frame().document()->existingAXObjectCache())
cache->handleScrolledToAnchor(anchorNode.get());
m_maintainScrollPositionAnchor = anchorNode;
}
void FrameView::updateEmbeddedObject(RenderEmbeddedObject& embeddedObject)
{
if (embeddedObject.isPluginUnavailable())
return;
HTMLFrameOwnerElement& element = embeddedObject.frameOwnerElement();
if (embeddedObject.isSnapshottedPlugIn()) {
if (is<HTMLObjectElement>(element) || is<HTMLEmbedElement>(element)) {
HTMLPlugInImageElement& pluginElement = downcast<HTMLPlugInImageElement>(element);
pluginElement.checkSnapshotStatus();
}
return;
}
auto weakRenderer = embeddedObject.createWeakPtr();
if (is<HTMLPlugInImageElement>(element)) {
HTMLPlugInImageElement& pluginElement = downcast<HTMLPlugInImageElement>(element);
if (pluginElement.needsCheckForSizeChange()) {
pluginElement.checkSnapshotStatus();
return;
}
if (pluginElement.needsWidgetUpdate())
pluginElement.updateWidget(CreateAnyWidgetType);
} else
ASSERT_NOT_REACHED();
if (!weakRenderer)
return;
auto ignoreWidgetState = embeddedObject.updateWidgetPosition();
UNUSED_PARAM(ignoreWidgetState);
}
bool FrameView::updateEmbeddedObjects()
{
if (m_nestedLayoutCount > 1 || !m_embeddedObjectsToUpdate || m_embeddedObjectsToUpdate->isEmpty())
return true;
WidgetHierarchyUpdatesSuspensionScope suspendWidgetHierarchyUpdates;
ASSERT(!m_embeddedObjectsToUpdate->contains(nullptr));
m_embeddedObjectsToUpdate->add(nullptr);
while (!m_embeddedObjectsToUpdate->isEmpty()) {
RenderEmbeddedObject* embeddedObject = m_embeddedObjectsToUpdate->takeFirst();
if (!embeddedObject)
break;
updateEmbeddedObject(*embeddedObject);
}
return m_embeddedObjectsToUpdate->isEmpty();
}
void FrameView::updateEmbeddedObjectsTimerFired()
{
RefPtr<FrameView> protectedThis(this);
m_updateEmbeddedObjectsTimer.stop();
for (unsigned i = 0; i < maxUpdateEmbeddedObjectsIterations; i++) {
if (updateEmbeddedObjects())
break;
}
}
void FrameView::flushAnyPendingPostLayoutTasks()
{
if (m_postLayoutTasksTimer.isActive())
performPostLayoutTasks();
if (m_updateEmbeddedObjectsTimer.isActive())
updateEmbeddedObjectsTimerFired();
}
void FrameView::queuePostLayoutCallback(Function<void()>&& callback)
{
m_postLayoutCallbackQueue.append(WTFMove(callback));
}
void FrameView::flushPostLayoutTasksQueue()
{
if (m_nestedLayoutCount > 1)
return;
if (!m_postLayoutCallbackQueue.size())
return;
Vector<Function<void()>> queue = WTFMove(m_postLayoutCallbackQueue);
for (auto& task : queue)
task();
}
void FrameView::performPostLayoutTasks()
{
LOG(Layout, "FrameView %p performPostLayoutTasks", this);
m_postLayoutTasksTimer.stop();
frame().selection().updateAppearanceAfterLayout();
flushPostLayoutTasksQueue();
if (m_nestedLayoutCount <= 1 && frame().document()->documentElement())
fireLayoutRelatedMilestonesIfNeeded();
#if PLATFORM(IOS)
if (frame().isMainFrame()) {
if (Page* page = frame().page())
page->chrome().client().didLayout();
}
#endif
#if ENABLE(FONT_LOAD_EVENTS)
if (RuntimeEnabledFeatures::sharedFeatures().fontLoadEventsEnabled())
frame().document()->fonts()->didLayout();
#endif
frame().loader().client().dispatchDidLayout();
updateWidgetPositions();
#if ENABLE(CSS_SCROLL_SNAP)
updateSnapOffsets();
#endif
Ref<FrameView> protectedThis(*this);
m_updateEmbeddedObjectsTimer.startOneShot(0);
if (auto* page = frame().page()) {
if (auto* scrollingCoordinator = page->scrollingCoordinator())
scrollingCoordinator->frameViewLayoutUpdated(*this);
}
if (RenderView* renderView = this->renderView()) {
if (renderView->usesCompositing())
renderView->compositor().frameViewDidLayout();
}
scrollToAnchor();
sendResizeEventIfNeeded();
viewportContentsChanged();
updateScrollSnapState();
if (AXObjectCache* cache = frame().document()->existingAXObjectCache())
cache->performDeferredIsIgnoredChange();
}
IntSize FrameView::sizeForResizeEvent() const
{
#if PLATFORM(IOS)
if (m_useCustomSizeForResizeEvent)
return m_customSizeForResizeEvent;
#endif
if (useFixedLayout() && !fixedLayoutSize().isEmpty() && delegatesScrolling())
return fixedLayoutSize();
return visibleContentRectIncludingScrollbars().size();
}
void FrameView::sendResizeEventIfNeeded()
{
if (isInRenderTreeLayout() || needsLayout())
return;
RenderView* renderView = this->renderView();
if (!renderView || renderView->printing())
return;
if (frame().page() && frame().page()->chrome().client().isSVGImageChromeClient())
return;
IntSize currentSize = sizeForResizeEvent();
float currentZoomFactor = renderView->style().zoom();
if (currentSize == m_lastViewportSize && currentZoomFactor == m_lastZoomFactor)
return;
m_lastViewportSize = currentSize;
m_lastZoomFactor = currentZoomFactor;
if (m_firstLayout)
return;
#if PLATFORM(IOS)
if (DocumentLoader* documentLoader = frame().loader().documentLoader()) {
if (documentLoader->isLoadingInAPISense())
return;
}
#endif
bool isMainFrame = frame().isMainFrame();
bool canSendResizeEventSynchronously = isMainFrame && !m_shouldAutoSize;
Ref<Event> resizeEvent = Event::create(eventNames().resizeEvent, false, false);
if (canSendResizeEventSynchronously)
frame().document()->dispatchWindowEvent(resizeEvent);
else {
frame().document()->enqueueWindowEvent(WTFMove(resizeEvent));
}
if (InspectorInstrumentation::hasFrontends() && isMainFrame) {
if (Page* page = frame().page()) {
if (InspectorClient* inspectorClient = page->inspectorController().inspectorClient())
inspectorClient->didResizeMainFrame(&frame());
}
}
}
void FrameView::willStartLiveResize()
{
ScrollView::willStartLiveResize();
adjustTiledBackingCoverage();
}
void FrameView::willEndLiveResize()
{
ScrollView::willEndLiveResize();
adjustTiledBackingCoverage();
}
void FrameView::autoSizeIfEnabled()
{
if (!m_shouldAutoSize)
return;
if (m_inAutoSize)
return;
LOG(Layout, "FrameView %p autoSizeIfEnabled", this);
TemporaryChange<bool> changeInAutoSize(m_inAutoSize, true);
Document* document = frame().document();
if (!document)
return;
RenderView* documentView = document->renderView();
Element* documentElement = document->documentElement();
if (!documentView || !documentElement)
return;
if (m_layoutRoot)
convertSubtreeLayoutToFullLayout();
resize(m_minAutoSize.width(), m_minAutoSize.height());
IntSize size = frameRect().size();
for (int i = 0; i < 2; i++) {
document->updateLayoutIgnorePendingStylesheets();
int width = documentView->minPreferredLogicalWidth();
int height = documentView->documentRect().height();
IntSize newSize(width, height);
if (newSize.width() > m_maxAutoSize.width()) {
RefPtr<Scrollbar> localHorizontalScrollbar = horizontalScrollbar();
if (!localHorizontalScrollbar)
localHorizontalScrollbar = createScrollbar(HorizontalScrollbar);
newSize.expand(0, localHorizontalScrollbar->occupiedHeight());
} else if (newSize.height() > m_maxAutoSize.height()) {
RefPtr<Scrollbar> localVerticalScrollbar = verticalScrollbar();
if (!localVerticalScrollbar)
localVerticalScrollbar = createScrollbar(VerticalScrollbar);
newSize.expand(localVerticalScrollbar->occupiedWidth(), 0);
}
newSize = newSize.expandedTo(m_minAutoSize);
ScrollbarMode horizonalScrollbarMode = ScrollbarAlwaysOff;
if (newSize.width() > m_maxAutoSize.width()) {
newSize.setWidth(m_maxAutoSize.width());
horizonalScrollbarMode = ScrollbarAlwaysOn;
}
ScrollbarMode verticalScrollbarMode = ScrollbarAlwaysOff;
if (newSize.height() > m_maxAutoSize.height()) {
newSize.setHeight(m_maxAutoSize.height());
verticalScrollbarMode = ScrollbarAlwaysOn;
}
if (newSize == size)
continue;
if (m_didRunAutosize && size.height() <= m_maxAutoSize.height() && size.width() <= m_maxAutoSize.width()
&& !frame().loader().isComplete() && (newSize.height() < size.height() || newSize.width() < size.width()))
break;
resize(newSize.width(), i ? newSize.height() : m_minAutoSize.height());
setVerticalScrollbarLock(false);
setHorizontalScrollbarLock(false);
setScrollbarModes(horizonalScrollbarMode, verticalScrollbarMode, true, true);
}
m_autoSizeContentSize = contentsSize();
if (m_autoSizeFixedMinimumHeight) {
resize(m_autoSizeContentSize.width(), std::max(m_autoSizeFixedMinimumHeight, m_autoSizeContentSize.height()));
document->updateLayoutIgnorePendingStylesheets();
}
m_didRunAutosize = true;
}
void FrameView::setAutoSizeFixedMinimumHeight(int fixedMinimumHeight)
{
if (m_autoSizeFixedMinimumHeight == fixedMinimumHeight)
return;
m_autoSizeFixedMinimumHeight = fixedMinimumHeight;
setNeedsLayout();
}
RenderElement* FrameView::viewportRenderer() const
{
if (m_viewportRendererType == ViewportRendererType::None)
return nullptr;
auto* document = frame().document();
if (!document)
return nullptr;
if (m_viewportRendererType == ViewportRendererType::Document) {
auto* documentElement = document->documentElement();
if (!documentElement)
return nullptr;
return documentElement->renderer();
}
if (m_viewportRendererType == ViewportRendererType::Body) {
auto* body = document->body();
if (!body)
return nullptr;
return body->renderer();
}
ASSERT_NOT_REACHED();
return nullptr;
}
void FrameView::updateOverflowStatus(bool horizontalOverflow, bool verticalOverflow)
{
auto* viewportRenderer = this->viewportRenderer();
if (!viewportRenderer)
return;
if (m_overflowStatusDirty) {
m_horizontalOverflow = horizontalOverflow;
m_verticalOverflow = verticalOverflow;
m_overflowStatusDirty = false;
return;
}
bool horizontalOverflowChanged = (m_horizontalOverflow != horizontalOverflow);
bool verticalOverflowChanged = (m_verticalOverflow != verticalOverflow);
if (horizontalOverflowChanged || verticalOverflowChanged) {
m_horizontalOverflow = horizontalOverflow;
m_verticalOverflow = verticalOverflow;
Ref<OverflowEvent> overflowEvent = OverflowEvent::create(horizontalOverflowChanged, horizontalOverflow,
verticalOverflowChanged, verticalOverflow);
overflowEvent->setTarget(viewportRenderer->element());
frame().document()->enqueueOverflowEvent(WTFMove(overflowEvent));
}
}
const Pagination& FrameView::pagination() const
{
if (m_pagination != Pagination())
return m_pagination;
if (frame().isMainFrame()) {
if (Page* page = frame().page())
return page->pagination();
}
return m_pagination;
}
void FrameView::setPagination(const Pagination& pagination)
{
if (m_pagination == pagination)
return;
m_pagination = pagination;
frame().document()->styleResolverChanged(DeferRecalcStyle);
}
IntRect FrameView::windowClipRect() const
{
ASSERT(frame().view() == this);
if (m_cachedWindowClipRect)
return *m_cachedWindowClipRect;
if (paintsEntireContents())
return contentsToWindow(IntRect(IntPoint(), totalContentsSize()));
IntRect clipRect = contentsToWindow(visibleContentRect(LegacyIOSDocumentVisibleRect));
if (!frame().ownerElement())
return clipRect;
HTMLFrameOwnerElement* ownerElement = frame().ownerElement();
if (FrameView* parentView = ownerElement->document().view())
clipRect.intersect(parentView->windowClipRectForFrameOwner(ownerElement, true));
return clipRect;
}
IntRect FrameView::windowClipRectForFrameOwner(const HTMLFrameOwnerElement* ownerElement, bool clipToLayerContents) const
{
if (!ownerElement->renderer())
return windowClipRect();
const RenderLayer* enclosingLayer = ownerElement->renderer()->enclosingLayer();
if (!enclosingLayer)
return windowClipRect();
IntRect clipRect;
if (clipToLayerContents)
clipRect = snappedIntRect(enclosingLayer->childrenClipRect());
else
clipRect = snappedIntRect(enclosingLayer->selfClipRect());
clipRect = contentsToWindow(clipRect);
return intersection(clipRect, windowClipRect());
}
bool FrameView::isActive() const
{
Page* page = frame().page();
return page && page->focusController().isActive();
}
bool FrameView::forceUpdateScrollbarsOnMainThreadForPerformanceTesting() const
{
Page* page = frame().page();
return page && page->settings().forceUpdateScrollbarsOnMainThreadForPerformanceTesting();
}
void FrameView::scrollTo(const ScrollPosition& newPosition)
{
IntPoint oldPosition = scrollPosition();
ScrollView::scrollTo(newPosition);
if (oldPosition != scrollPosition())
scrollPositionChanged(oldPosition, scrollPosition());
didChangeScrollOffset();
}
float FrameView::adjustScrollStepForFixedContent(float step, ScrollbarOrientation orientation, ScrollGranularity granularity)
{
if (granularity != ScrollByPage || orientation == HorizontalScrollbar)
return step;
TrackedRendererListHashSet* positionedObjects = nullptr;
if (RenderView* root = frame().contentRenderer()) {
if (!root->hasPositionedObjects())
return step;
positionedObjects = root->positionedObjects();
}
FloatRect unobscuredContentRect = this->unobscuredContentRect();
float topObscuredArea = 0;
float bottomObscuredArea = 0;
for (const auto& positionedObject : *positionedObjects) {
const RenderStyle& style = positionedObject->style();
if (style.position() != FixedPosition || style.visibility() == HIDDEN || !style.opacity())
continue;
FloatQuad contentQuad = positionedObject->absoluteContentQuad();
if (!contentQuad.isRectilinear())
continue;
FloatRect contentBoundingBox = contentQuad.boundingBox();
FloatRect fixedRectInView = intersection(unobscuredContentRect, contentBoundingBox);
if (fixedRectInView.width() < unobscuredContentRect.width())
continue;
if (fixedRectInView.y() == unobscuredContentRect.y())
topObscuredArea = std::max(topObscuredArea, fixedRectInView.height());
else if (fixedRectInView.maxY() == unobscuredContentRect.maxY())
bottomObscuredArea = std::max(bottomObscuredArea, fixedRectInView.height());
}
return Scrollbar::pageStep(unobscuredContentRect.height(), unobscuredContentRect.height() - topObscuredArea - bottomObscuredArea);
}
void FrameView::invalidateScrollbarRect(Scrollbar& scrollbar, const IntRect& rect)
{
IntRect dirtyRect = rect;
dirtyRect.moveBy(scrollbar.location());
invalidateRect(dirtyRect);
}
float FrameView::visibleContentScaleFactor() const
{
if (!frame().isMainFrame() || !frame().settings().delegatesPageScaling())
return 1;
Page* page = frame().page();
if (!page)
return 1;
return page->pageScaleFactor();
}
void FrameView::setVisibleScrollerThumbRect(const IntRect& scrollerThumb)
{
if (!frame().isMainFrame())
return;
if (Page* page = frame().page())
page->chrome().client().notifyScrollerThumbIsVisibleInRect(scrollerThumb);
}
ScrollableArea* FrameView::enclosingScrollableArea() const
{
return nullptr;
}
IntRect FrameView::scrollableAreaBoundingBox(bool*) const
{
RenderWidget* ownerRenderer = frame().ownerRenderer();
if (!ownerRenderer)
return frameRect();
return ownerRenderer->absoluteContentQuad().enclosingBoundingBox();
}
bool FrameView::isScrollable(Scrollability definitionOfScrollable)
{
bool requiresActualOverflowToBeConsideredScrollable = !frame().isMainFrame() || definitionOfScrollable != Scrollability::ScrollableOrRubberbandable;
#if !ENABLE(RUBBER_BANDING)
requiresActualOverflowToBeConsideredScrollable = true;
#endif
if (requiresActualOverflowToBeConsideredScrollable) {
IntSize totalContentsSize = this->totalContentsSize();
IntSize visibleContentSize = visibleContentRect(LegacyIOSDocumentVisibleRect).size();
if (totalContentsSize.height() <= visibleContentSize.height() && totalContentsSize.width() <= visibleContentSize.width())
return false;
}
HTMLFrameOwnerElement* owner = frame().ownerElement();
if (owner && (!owner->renderer() || !owner->renderer()->visibleToHitTesting()))
return false;
ScrollbarMode horizontalMode;
ScrollbarMode verticalMode;
calculateScrollbarModesForLayout(horizontalMode, verticalMode, RulesFromWebContentOnly);
if (horizontalMode == ScrollbarAlwaysOff && verticalMode == ScrollbarAlwaysOff)
return false;
return true;
}
bool FrameView::isScrollableOrRubberbandable()
{
return isScrollable(Scrollability::ScrollableOrRubberbandable);
}
bool FrameView::hasScrollableOrRubberbandableAncestor()
{
if (frame().isMainFrame())
return isScrollableOrRubberbandable();
for (FrameView* parent = this->parentFrameView(); parent; parent = parent->parentFrameView()) {
Scrollability frameScrollability = parent->frame().isMainFrame() ? Scrollability::ScrollableOrRubberbandable : Scrollability::Scrollable;
if (parent->isScrollable(frameScrollability))
return true;
}
return false;
}
void FrameView::updateScrollableAreaSet()
{
FrameView* parentFrameView = this->parentFrameView();
if (!parentFrameView)
return;
if (!isScrollable()) {
parentFrameView->removeScrollableArea(this);
return;
}
parentFrameView->addScrollableArea(this);
}
bool FrameView::shouldSuspendScrollAnimations() const
{
return frame().loader().state() != FrameStateComplete;
}
void FrameView::scrollbarStyleChanged(ScrollbarStyle newStyle, bool forceUpdate)
{
if (!frame().isMainFrame())
return;
if (Page* page = frame().page())
page->chrome().client().recommendedScrollbarStyleDidChange(newStyle);
ScrollView::scrollbarStyleChanged(newStyle, forceUpdate);
}
void FrameView::notifyPageThatContentAreaWillPaint() const
{
Page* page = frame().page();
if (!page)
return;
contentAreaWillPaint();
if (!m_scrollableAreas)
return;
for (auto& scrollableArea : *m_scrollableAreas)
scrollableArea->contentAreaWillPaint();
}
bool FrameView::scrollAnimatorEnabled() const
{
#if ENABLE(SMOOTH_SCROLLING)
if (Page* page = frame().page())
return page->settings().scrollAnimatorEnabled();
#endif
return false;
}
#if ENABLE(DASHBOARD_SUPPORT)
void FrameView::updateAnnotatedRegions()
{
Document* document = frame().document();
if (!document->hasAnnotatedRegions())
return;
Vector<AnnotatedRegionValue> newRegions;
document->renderBox()->collectAnnotatedRegions(newRegions);
if (newRegions == document->annotatedRegions())
return;
document->setAnnotatedRegions(newRegions);
Page* page = frame().page();
if (!page)
return;
page->chrome().client().annotatedRegionsChanged();
}
#endif
void FrameView::updateScrollCorner()
{
RenderElement* renderer = nullptr;
std::unique_ptr<RenderStyle> cornerStyle;
IntRect cornerRect = scrollCornerRect();
if (!cornerRect.isEmpty()) {
Document* doc = frame().document();
Element* body = doc ? doc->bodyOrFrameset() : nullptr;
if (body && body->renderer()) {
renderer = body->renderer();
cornerStyle = renderer->getUncachedPseudoStyle(PseudoStyleRequest(SCROLLBAR_CORNER), &renderer->style());
}
if (!cornerStyle) {
Element* docElement = doc ? doc->documentElement() : nullptr;
if (docElement && docElement->renderer()) {
renderer = docElement->renderer();
cornerStyle = renderer->getUncachedPseudoStyle(PseudoStyleRequest(SCROLLBAR_CORNER), &renderer->style());
}
}
if (!cornerStyle) {
if (RenderWidget* renderer = frame().ownerRenderer())
cornerStyle = renderer->getUncachedPseudoStyle(PseudoStyleRequest(SCROLLBAR_CORNER), &renderer->style());
}
}
if (!cornerStyle)
m_scrollCorner = nullptr;
else {
if (!m_scrollCorner) {
m_scrollCorner = createRenderer<RenderScrollbarPart>(renderer->document(), WTFMove(*cornerStyle));
m_scrollCorner->initializeStyle();
} else
m_scrollCorner->setStyle(WTFMove(*cornerStyle));
invalidateScrollCorner(cornerRect);
}
ScrollView::updateScrollCorner();
}
void FrameView::paintScrollCorner(GraphicsContext& context, const IntRect& cornerRect)
{
if (context.updatingControlTints()) {
updateScrollCorner();
return;
}
if (m_scrollCorner) {
if (frame().isMainFrame())
context.fillRect(cornerRect, baseBackgroundColor());
m_scrollCorner->paintIntoRect(context, cornerRect.location(), cornerRect);
return;
}
ScrollView::paintScrollCorner(context, cornerRect);
}
void FrameView::paintScrollbar(GraphicsContext& context, Scrollbar& bar, const IntRect& rect)
{
if (bar.isCustomScrollbar() && frame().isMainFrame()) {
IntRect toFill = bar.frameRect();
toFill.intersect(rect);
context.fillRect(toFill, baseBackgroundColor());
}
ScrollView::paintScrollbar(context, bar, rect);
}
Color FrameView::documentBackgroundColor() const
{
if (!frame().document())
return Color();
auto* htmlElement = frame().document()->documentElement();
auto* bodyElement = frame().document()->bodyOrFrameset();
Color htmlBackgroundColor;
Color bodyBackgroundColor;
if (htmlElement && htmlElement->renderer())
htmlBackgroundColor = htmlElement->renderer()->style().visitedDependentColor(CSSPropertyBackgroundColor);
if (bodyElement && bodyElement->renderer())
bodyBackgroundColor = bodyElement->renderer()->style().visitedDependentColor(CSSPropertyBackgroundColor);
if (!bodyBackgroundColor.isValid()) {
if (!htmlBackgroundColor.isValid())
return Color();
return baseBackgroundColor().blend(htmlBackgroundColor);
}
if (!htmlBackgroundColor.isValid())
return baseBackgroundColor().blend(bodyBackgroundColor);
return baseBackgroundColor().blend(htmlBackgroundColor).blend(bodyBackgroundColor);
}
bool FrameView::hasCustomScrollbars() const
{
for (auto& widget : children()) {
if (is<FrameView>(*widget)) {
if (downcast<FrameView>(*widget).hasCustomScrollbars())
return true;
} else if (is<Scrollbar>(*widget)) {
if (downcast<Scrollbar>(*widget).isCustomScrollbar())
return true;
}
}
return false;
}
FrameView* FrameView::parentFrameView() const
{
if (!parent())
return nullptr;
if (Frame* parentFrame = frame().tree().parent())
return parentFrame->view();
return nullptr;
}
bool FrameView::isInChildFrameWithFrameFlattening() const
{
if (!frameFlatteningEnabled())
return false;
if (!parent())
return false;
HTMLFrameOwnerElement* ownerElement = frame().ownerElement();
if (!ownerElement)
return false;
if (!ownerElement->renderWidget())
return false;
if (is<HTMLIFrameElement>(*ownerElement))
return downcast<RenderIFrame>(*ownerElement->renderWidget()).flattenFrame();
if (is<HTMLFrameElement>(*ownerElement))
return true;
return false;
}
void FrameView::startLayoutAtMainFrameViewIfNeeded(bool allowSubtree)
{
FrameView* parentView = parentFrameView();
if (!parentView)
return;
if (parentView->m_nestedLayoutCount)
return;
if (!parentView->needsLayout())
return;
while (parentView->parentFrameView())
parentView = parentView->parentFrameView();
LOG(Layout, " frame flattening, starting from root");
parentView->layout(allowSubtree);
}
void FrameView::updateControlTints()
{
if (frame().document()->url().isEmpty())
return;
Page* page = frame().page();
bool isCurrentlyCountingRelevantRepaintedObject = false;
if (page) {
isCurrentlyCountingRelevantRepaintedObject = page->isCountingRelevantRepaintedObjects();
page->setIsCountingRelevantRepaintedObjects(false);
}
RenderView* renderView = this->renderView();
if ((renderView && renderView->theme().supportsControlTints()) || hasCustomScrollbars())
paintControlTints();
if (page)
page->setIsCountingRelevantRepaintedObjects(isCurrentlyCountingRelevantRepaintedObject);
}
void FrameView::paintControlTints()
{
if (needsLayout())
layout();
GraphicsContext context(GraphicsContext::NonPaintingReasons::UpdatingControlTints);
if (platformWidget()) {
paintContents(context, visibleContentRect(LegacyIOSDocumentVisibleRect));
} else
paint(context, frameRect());
}
bool FrameView::wasScrolledByUser() const
{
return m_wasScrolledByUser;
}
void FrameView::setWasScrolledByUser(bool wasScrolledByUser)
{
if (m_inProgrammaticScroll)
return;
m_maintainScrollPositionAnchor = nullptr;
if (m_wasScrolledByUser == wasScrolledByUser)
return;
m_wasScrolledByUser = wasScrolledByUser;
if (frame().isMainFrame())
updateLayerFlushThrottling();
adjustTiledBackingCoverage();
}
void FrameView::willPaintContents(GraphicsContext& context, const IntRect&, PaintingState& paintingState)
{
Document* document = frame().document();
if (!context.paintingDisabled())
InspectorInstrumentation::willPaint(renderView());
paintingState.isTopLevelPainter = !sCurrentPaintTimeStamp;
if (paintingState.isTopLevelPainter && MemoryPressureHandler::singleton().isUnderMemoryPressure()) {
LOG(MemoryPressure, "Under memory pressure: %s", WTF_PRETTY_FUNCTION);
MemoryCache::singleton().prune();
}
if (paintingState.isTopLevelPainter)
sCurrentPaintTimeStamp = monotonicallyIncreasingTime();
paintingState.paintBehavior = m_paintBehavior;
if (FrameView* parentView = parentFrameView()) {
if (parentView->paintBehavior() & PaintBehaviorFlattenCompositingLayers)
m_paintBehavior |= PaintBehaviorFlattenCompositingLayers;
}
if (document->printing())
m_paintBehavior |= PaintBehaviorFlattenCompositingLayers;
paintingState.isFlatteningPaintOfRootFrame = (m_paintBehavior & PaintBehaviorFlattenCompositingLayers) && !frame().ownerElement();
if (paintingState.isFlatteningPaintOfRootFrame)
notifyWidgetsInAllFrames(WillPaintFlattened);
ASSERT(!m_isPainting);
m_isPainting = true;
}
void FrameView::didPaintContents(GraphicsContext& context, const IntRect& dirtyRect, PaintingState& paintingState)
{
m_isPainting = false;
if (paintingState.isFlatteningPaintOfRootFrame)
notifyWidgetsInAllFrames(DidPaintFlattened);
m_paintBehavior = paintingState.paintBehavior;
m_lastPaintTime = monotonicallyIncreasingTime();
if (paintingState.isTopLevelPainter && MemoryPressureHandler::singleton().isUnderMemoryPressure())
MemoryCache::singleton().pruneLiveResources(true);
#if ENABLE(DASHBOARD_SUPPORT)
if (frame().document()->annotatedRegionsDirty())
updateAnnotatedRegions();
#endif
if (paintingState.isTopLevelPainter)
sCurrentPaintTimeStamp = 0;
if (!context.paintingDisabled()) {
InspectorInstrumentation::didPaint(renderView(), dirtyRect);
firePaintRelatedMilestonesIfNeeded();
}
}
void FrameView::paintContents(GraphicsContext& context, const IntRect& dirtyRect)
{
#ifndef NDEBUG
bool fillWithRed;
if (frame().document()->printing())
fillWithRed = false; else if (frame().ownerElement())
fillWithRed = false; else if (isTransparent())
fillWithRed = false; else if (m_paintBehavior & PaintBehaviorSelectionOnly)
fillWithRed = false; else if (m_nodeToDraw)
fillWithRed = false; else
fillWithRed = true;
if (fillWithRed)
context.fillRect(dirtyRect, Color(0xFF, 0, 0));
#endif
if (m_layoutPhase == InViewSizeAdjust)
return;
TraceScope tracingScope(PaintViewStart, PaintViewEnd);
ASSERT(m_layoutPhase == InPostLayerPositionsUpdatedAfterLayout || m_layoutPhase == OutsideLayout);
RenderView* renderView = this->renderView();
if (!renderView) {
LOG_ERROR("called FrameView::paint with nil renderer");
return;
}
ASSERT(!needsLayout());
if (needsLayout())
return;
PaintingState paintingState;
willPaintContents(context, dirtyRect, paintingState);
RenderObject* renderer = m_nodeToDraw ? m_nodeToDraw->renderer() : nullptr;
RenderLayer* rootLayer = renderView->layer();
#ifndef NDEBUG
RenderElement::SetLayoutNeededForbiddenScope forbidSetNeedsLayout(&rootLayer->renderer());
#endif
while (is<RenderInline>(renderer) && !downcast<RenderInline>(*renderer).firstLineBox())
renderer = renderer->parent();
rootLayer->paint(context, dirtyRect, LayoutSize(), m_paintBehavior, renderer);
if (rootLayer->containsDirtyOverlayScrollbars())
rootLayer->paintOverlayScrollbars(context, dirtyRect, m_paintBehavior, renderer);
didPaintContents(context, dirtyRect, paintingState);
}
void FrameView::setPaintBehavior(PaintBehavior behavior)
{
m_paintBehavior = behavior;
}
PaintBehavior FrameView::paintBehavior() const
{
return m_paintBehavior;
}
bool FrameView::isPainting() const
{
return m_isPainting;
}
void FrameView::setNodeToDraw(Node* node)
{
m_nodeToDraw = node;
}
void FrameView::paintContentsForSnapshot(GraphicsContext& context, const IntRect& imageRect, SelectionInSnapshot shouldPaintSelection, CoordinateSpaceForSnapshot coordinateSpace)
{
updateLayoutAndStyleIfNeededRecursive();
PaintBehavior oldBehavior = paintBehavior();
setPaintBehavior(oldBehavior | PaintBehaviorFlattenCompositingLayers);
if (shouldPaintSelection == ExcludeSelection) {
for (auto* frame = m_frame.ptr(); frame; frame = frame->tree().traverseNext(m_frame.ptr())) {
if (RenderView* root = frame->contentRenderer())
root->clearSelection();
}
}
if (coordinateSpace == DocumentCoordinates)
paintContents(context, imageRect);
else {
paint(context, imageRect);
}
if (shouldPaintSelection == ExcludeSelection) {
for (auto* frame = m_frame.ptr(); frame; frame = frame->tree().traverseNext(m_frame.ptr()))
frame->selection().updateAppearance();
}
setPaintBehavior(oldBehavior);
}
void FrameView::paintOverhangAreas(GraphicsContext& context, const IntRect& horizontalOverhangArea, const IntRect& verticalOverhangArea, const IntRect& dirtyRect)
{
if (context.paintingDisabled())
return;
if (frame().document()->printing())
return;
ScrollView::paintOverhangAreas(context, horizontalOverhangArea, verticalOverhangArea, dirtyRect);
}
FrameView::FrameViewList FrameView::renderedChildFrameViews() const
{
FrameViewList childViews;
for (Frame* frame = m_frame->tree().firstRenderedChild(); frame; frame = frame->tree().nextRenderedSibling()) {
if (frame->view())
childViews.append(*frame->view());
}
return childViews;
}
void FrameView::updateLayoutAndStyleIfNeededRecursive()
{
AnimationUpdateBlock animationUpdateBlock(&frame().animation());
frame().document()->updateStyleIfNeeded();
if (needsLayout())
layout();
for (auto& frameView : renderedChildFrameViews())
frameView->updateLayoutAndStyleIfNeededRecursive();
frame().document()->updateStyleIfNeeded();
if (needsLayout())
layout();
ASSERT(!frame().isMainFrame() || !needsStyleRecalcOrLayout());
}
bool FrameView::qualifiesAsVisuallyNonEmpty() const
{
Element* documentElement = frame().document()->documentElement();
if (!documentElement || !documentElement->renderer())
return false;
if (!frame().document()->parsing() && frame().loader().stateMachine().committedFirstRealDocumentLoad())
return true;
static const int documentHeightThreshold = 48;
LayoutRect overflowRect = documentElement->renderBox()->layoutOverflowRect();
if (snappedIntRect(overflowRect).height() < documentHeightThreshold)
return false;
if (m_visuallyNonEmptyCharacterCount > visualCharacterThreshold)
return true;
if (m_visuallyNonEmptyPixelCount > visualPixelThreshold)
return true;
return false;
}
void FrameView::updateIsVisuallyNonEmpty()
{
if (m_isVisuallyNonEmpty)
return;
if (!qualifiesAsVisuallyNonEmpty())
return;
m_isVisuallyNonEmpty = true;
adjustTiledBackingCoverage();
}
bool FrameView::isViewForDocumentInFrame() const
{
RenderView* renderView = this->renderView();
if (!renderView)
return false;
return &renderView->frameView() == this;
}
void FrameView::enableAutoSizeMode(bool enable, const IntSize& minSize, const IntSize& maxSize)
{
ASSERT(!enable || !minSize.isEmpty());
ASSERT(minSize.width() <= maxSize.width());
ASSERT(minSize.height() <= maxSize.height());
if (m_shouldAutoSize == enable && m_minAutoSize == minSize && m_maxAutoSize == maxSize)
return;
m_shouldAutoSize = enable;
m_minAutoSize = minSize;
m_maxAutoSize = maxSize;
m_didRunAutosize = false;
setNeedsLayout();
scheduleRelayout();
if (m_shouldAutoSize)
return;
setVerticalScrollbarLock(false);
setHorizontalScrollbarLock(false);
setScrollbarModes(ScrollbarAuto, ScrollbarAuto);
}
void FrameView::forceLayout(bool allowSubtree)
{
layout(allowSubtree);
}
void FrameView::forceLayoutForPagination(const FloatSize& pageSize, const FloatSize& originalPageSize, float maximumShrinkFactor, AdjustViewSizeOrNot shouldAdjustViewSize)
{
if (RenderView* renderView = this->renderView()) {
float pageLogicalWidth = renderView->style().isHorizontalWritingMode() ? pageSize.width() : pageSize.height();
float pageLogicalHeight = renderView->style().isHorizontalWritingMode() ? pageSize.height() : pageSize.width();
renderView->setLogicalWidth(floor(pageLogicalWidth));
renderView->setPageLogicalHeight(floor(pageLogicalHeight));
renderView->setNeedsLayoutAndPrefWidthsRecalc();
forceLayout();
bool horizontalWritingMode = renderView->style().isHorizontalWritingMode();
const LayoutRect& documentRect = renderView->documentRect();
LayoutUnit docLogicalWidth = horizontalWritingMode ? documentRect.width() : documentRect.height();
if (docLogicalWidth > pageLogicalWidth) {
int expectedPageWidth = std::min<float>(documentRect.width(), pageSize.width() * maximumShrinkFactor);
int expectedPageHeight = std::min<float>(documentRect.height(), pageSize.height() * maximumShrinkFactor);
FloatSize maxPageSize = frame().resizePageRectsKeepingRatio(FloatSize(originalPageSize.width(), originalPageSize.height()), FloatSize(expectedPageWidth, expectedPageHeight));
pageLogicalWidth = horizontalWritingMode ? maxPageSize.width() : maxPageSize.height();
pageLogicalHeight = horizontalWritingMode ? maxPageSize.height() : maxPageSize.width();
renderView->setLogicalWidth(floor(pageLogicalWidth));
renderView->setPageLogicalHeight(floor(pageLogicalHeight));
renderView->setNeedsLayoutAndPrefWidthsRecalc();
forceLayout();
const LayoutRect& updatedDocumentRect = renderView->documentRect();
LayoutUnit docLogicalHeight = horizontalWritingMode ? updatedDocumentRect.height() : updatedDocumentRect.width();
LayoutUnit docLogicalTop = horizontalWritingMode ? updatedDocumentRect.y() : updatedDocumentRect.x();
LayoutUnit docLogicalRight = horizontalWritingMode ? updatedDocumentRect.maxX() : updatedDocumentRect.maxY();
LayoutUnit clippedLogicalLeft = 0;
if (!renderView->style().isLeftToRightDirection())
clippedLogicalLeft = docLogicalRight - pageLogicalWidth;
LayoutRect overflow(clippedLogicalLeft, docLogicalTop, pageLogicalWidth, docLogicalHeight);
if (!horizontalWritingMode)
overflow = overflow.transposedRect();
renderView->clearLayoutOverflow();
renderView->addLayoutOverflow(overflow); }
}
if (shouldAdjustViewSize)
adjustViewSize();
}
void FrameView::adjustPageHeightDeprecated(float *newBottom, float oldTop, float oldBottom, float )
{
RenderView* renderView = this->renderView();
if (!renderView) {
*newBottom = oldBottom;
return;
}
GraphicsContext context((PlatformGraphicsContext*)nullptr);
renderView->setTruncatedAt(static_cast<int>(floorf(oldBottom)));
IntRect dirtyRect(0, static_cast<int>(floorf(oldTop)), renderView->layoutOverflowRect().maxX(), static_cast<int>(ceilf(oldBottom - oldTop)));
renderView->setPrintRect(dirtyRect);
renderView->layer()->paint(context, dirtyRect);
*newBottom = renderView->bestTruncatedAt();
if (!*newBottom)
*newBottom = oldBottom;
renderView->setPrintRect(IntRect());
}
IntRect FrameView::convertFromRendererToContainingView(const RenderElement* renderer, const IntRect& rendererRect) const
{
IntRect rect = snappedIntRect(enclosingLayoutRect(renderer->localToAbsoluteQuad(FloatRect(rendererRect)).boundingBox()));
if (!delegatesScrolling())
rect = contentsToView(rect);
return rect;
}
IntRect FrameView::convertFromContainingViewToRenderer(const RenderElement* renderer, const IntRect& viewRect) const
{
IntRect rect = viewRect;
if (!delegatesScrolling())
rect = viewToContents(rect);
rect.setLocation(roundedIntPoint(renderer->absoluteToLocal(rect.location(), UseTransforms)));
return rect;
}
IntPoint FrameView::convertFromRendererToContainingView(const RenderElement* renderer, const IntPoint& rendererPoint) const
{
IntPoint point = roundedIntPoint(renderer->localToAbsolute(rendererPoint, UseTransforms));
if (!delegatesScrolling())
point = contentsToView(point);
return point;
}
IntPoint FrameView::convertFromContainingViewToRenderer(const RenderElement* renderer, const IntPoint& viewPoint) const
{
IntPoint point = viewPoint;
if (!delegatesScrolling())
point = viewToContents(point);
return roundedIntPoint(renderer->absoluteToLocal(point, UseTransforms));
}
IntRect FrameView::convertToContainingView(const IntRect& localRect) const
{
if (const ScrollView* parentScrollView = parent()) {
if (is<FrameView>(*parentScrollView)) {
const FrameView& parentView = downcast<FrameView>(*parentScrollView);
RenderWidget* renderer = frame().ownerRenderer();
if (!renderer)
return localRect;
IntRect rect(localRect);
rect.move(renderer->borderLeft() + renderer->paddingLeft(),
renderer->borderTop() + renderer->paddingTop());
return parentView.convertFromRendererToContainingView(renderer, rect);
}
return Widget::convertToContainingView(localRect);
}
return localRect;
}
IntRect FrameView::convertFromContainingView(const IntRect& parentRect) const
{
if (const ScrollView* parentScrollView = parent()) {
if (is<FrameView>(*parentScrollView)) {
const FrameView& parentView = downcast<FrameView>(*parentScrollView);
RenderWidget* renderer = frame().ownerRenderer();
if (!renderer)
return parentRect;
IntRect rect = parentView.convertFromContainingViewToRenderer(renderer, parentRect);
rect.move(-renderer->borderLeft() - renderer->paddingLeft(),
-renderer->borderTop() - renderer->paddingTop());
return rect;
}
return Widget::convertFromContainingView(parentRect);
}
return parentRect;
}
IntPoint FrameView::convertToContainingView(const IntPoint& localPoint) const
{
if (const ScrollView* parentScrollView = parent()) {
if (is<FrameView>(*parentScrollView)) {
const FrameView& parentView = downcast<FrameView>(*parentScrollView);
RenderWidget* renderer = frame().ownerRenderer();
if (!renderer)
return localPoint;
IntPoint point(localPoint);
point.move(renderer->borderLeft() + renderer->paddingLeft(),
renderer->borderTop() + renderer->paddingTop());
return parentView.convertFromRendererToContainingView(renderer, point);
}
return Widget::convertToContainingView(localPoint);
}
return localPoint;
}
IntPoint FrameView::convertFromContainingView(const IntPoint& parentPoint) const
{
if (const ScrollView* parentScrollView = parent()) {
if (is<FrameView>(*parentScrollView)) {
const FrameView& parentView = downcast<FrameView>(*parentScrollView);
RenderWidget* renderer = frame().ownerRenderer();
if (!renderer)
return parentPoint;
IntPoint point = parentView.convertFromContainingViewToRenderer(renderer, parentPoint);
point.move(-renderer->borderLeft() - renderer->paddingLeft(),
-renderer->borderTop() - renderer->paddingTop());
return point;
}
return Widget::convertFromContainingView(parentPoint);
}
return parentPoint;
}
void FrameView::setTracksRepaints(bool trackRepaints)
{
if (trackRepaints == m_isTrackingRepaints)
return;
if (trackRepaints) {
if (frame().document())
frame().document()->updateLayout();
}
for (Frame* frame = &m_frame->tree().top(); frame; frame = frame->tree().traverseNext()) {
if (RenderView* renderView = frame->contentRenderer())
renderView->compositor().setTracksRepaints(trackRepaints);
}
resetTrackedRepaints();
m_isTrackingRepaints = trackRepaints;
}
void FrameView::resetTrackedRepaints()
{
m_trackedRepaintRects.clear();
if (RenderView* renderView = this->renderView())
renderView->compositor().resetTrackedRepaintRects();
}
String FrameView::trackedRepaintRectsAsText() const
{
if (frame().document())
frame().document()->updateLayout();
TextStream ts;
if (!m_trackedRepaintRects.isEmpty()) {
ts << "(repaint rects\n";
for (auto& rect : m_trackedRepaintRects)
ts << " (rect " << LayoutUnit(rect.x()) << " " << LayoutUnit(rect.y()) << " " << LayoutUnit(rect.width()) << " " << LayoutUnit(rect.height()) << ")\n";
ts << ")\n";
}
return ts.release();
}
bool FrameView::addScrollableArea(ScrollableArea* scrollableArea)
{
if (!m_scrollableAreas)
m_scrollableAreas = std::make_unique<ScrollableAreaSet>();
if (m_scrollableAreas->add(scrollableArea).isNewEntry) {
scrollableAreaSetChanged();
return true;
}
return false;
}
bool FrameView::removeScrollableArea(ScrollableArea* scrollableArea)
{
if (m_scrollableAreas && m_scrollableAreas->remove(scrollableArea)) {
scrollableAreaSetChanged();
return true;
}
return false;
}
bool FrameView::containsScrollableArea(ScrollableArea* scrollableArea) const
{
return m_scrollableAreas && m_scrollableAreas->contains(scrollableArea);
}
void FrameView::scrollableAreaSetChanged()
{
if (auto* page = frame().page()) {
if (auto* scrollingCoordinator = page->scrollingCoordinator())
scrollingCoordinator->frameViewEventTrackingRegionsChanged(*this);
}
}
void FrameView::sendScrollEvent()
{
frame().eventHandler().sendScrollEvent();
frame().eventHandler().dispatchFakeMouseMoveEventSoon();
#if ENABLE(CSS_ANIMATIONS_LEVEL_2)
frame().animation().scrollWasUpdated();
#endif
}
void FrameView::removeChild(Widget& widget)
{
if (is<FrameView>(widget))
removeScrollableArea(&downcast<FrameView>(widget));
ScrollView::removeChild(widget);
}
bool FrameView::wheelEvent(const PlatformWheelEvent& wheelEvent)
{
#if !ENABLE(RUBBER_BANDING)
if (!isScrollable())
return false;
#endif
if (delegatesScrolling()) {
ScrollPosition oldPosition = scrollPosition();
ScrollPosition newPosition = oldPosition - IntSize(wheelEvent.deltaX(), wheelEvent.deltaY());
if (oldPosition != newPosition) {
ScrollView::scrollTo(newPosition);
scrollPositionChanged(oldPosition, scrollPosition());
didChangeScrollOffset();
}
return true;
}
if (!canHaveScrollbars())
return false;
if (platformWidget())
return false;
#if ENABLE(ASYNC_SCROLLING)
if (Page* page = frame().page()) {
if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator()) {
if (scrollingCoordinator->coordinatesScrollingForFrameView(*this))
return scrollingCoordinator->handleWheelEvent(*this, wheelEvent);
}
}
#endif
return ScrollableArea::handleWheelEvent(wheelEvent);
}
bool FrameView::isVerticalDocument() const
{
RenderView* renderView = this->renderView();
if (!renderView)
return true;
return renderView->style().isHorizontalWritingMode();
}
bool FrameView::isFlippedDocument() const
{
RenderView* renderView = this->renderView();
if (!renderView)
return false;
return renderView->style().isFlippedBlocksWritingMode();
}
void FrameView::notifyWidgetsInAllFrames(WidgetNotification notification)
{
for (auto* frame = m_frame.ptr(); frame; frame = frame->tree().traverseNext(m_frame.ptr())) {
if (FrameView* view = frame->view())
view->notifyWidgets(notification);
}
}
AXObjectCache* FrameView::axObjectCache() const
{
if (frame().document())
return frame().document()->existingAXObjectCache();
return nullptr;
}
#if PLATFORM(IOS)
void FrameView::setCustomFixedPositionLayoutRect(const IntRect& rect)
{
if (m_useCustomFixedPositionLayoutRect && m_customFixedPositionLayoutRect == rect)
return;
m_useCustomFixedPositionLayoutRect = true;
m_customFixedPositionLayoutRect = rect;
updateContentsSize();
}
bool FrameView::updateFixedPositionLayoutRect()
{
if (!m_useCustomFixedPositionLayoutRect)
return false;
IntRect newRect;
Page* page = frame().page();
if (!page || !page->chrome().client().fetchCustomFixedPositionLayoutRect(newRect))
return false;
if (newRect != m_customFixedPositionLayoutRect) {
m_customFixedPositionLayoutRect = newRect;
setViewportConstrainedObjectsNeedLayout();
return true;
}
return false;
}
void FrameView::setCustomSizeForResizeEvent(IntSize customSize)
{
m_useCustomSizeForResizeEvent = true;
m_customSizeForResizeEvent = customSize;
sendResizeEventIfNeeded();
}
void FrameView::setScrollVelocity(double horizontalVelocity, double verticalVelocity, double scaleChangeRate, double timestamp)
{
if (TiledBacking* tiledBacking = this->tiledBacking())
tiledBacking->setVelocity(VelocityData(horizontalVelocity, verticalVelocity, scaleChangeRate, timestamp));
}
#endif // PLATFORM(IOS)
void FrameView::setScrollingPerformanceLoggingEnabled(bool flag)
{
if (TiledBacking* tiledBacking = this->tiledBacking())
tiledBacking->setScrollingPerformanceLoggingEnabled(flag);
}
void FrameView::didAddScrollbar(Scrollbar* scrollbar, ScrollbarOrientation orientation)
{
ScrollableArea::didAddScrollbar(scrollbar, orientation);
Page* page = frame().page();
if (page && page->expectsWheelEventTriggers())
scrollAnimator().setWheelEventTestTrigger(page->testTrigger());
if (AXObjectCache* cache = axObjectCache())
cache->handleScrollbarUpdate(this);
}
void FrameView::willRemoveScrollbar(Scrollbar* scrollbar, ScrollbarOrientation orientation)
{
ScrollableArea::willRemoveScrollbar(scrollbar, orientation);
if (AXObjectCache* cache = axObjectCache()) {
cache->remove(scrollbar);
cache->handleScrollbarUpdate(this);
}
}
void FrameView::addPaintPendingMilestones(LayoutMilestones milestones)
{
m_milestonesPendingPaint |= milestones;
}
void FrameView::fireLayoutRelatedMilestonesIfNeeded()
{
LayoutMilestones requestedMilestones = 0;
LayoutMilestones milestonesAchieved = 0;
Page* page = frame().page();
if (page)
requestedMilestones = page->requestedLayoutMilestones();
if (m_firstLayoutCallbackPending) {
m_firstLayoutCallbackPending = false;
frame().loader().didFirstLayout();
if (requestedMilestones & DidFirstLayout)
milestonesAchieved |= DidFirstLayout;
if (frame().isMainFrame())
page->startCountingRelevantRepaintedObjects();
}
updateIsVisuallyNonEmpty();
if (m_isVisuallyNonEmpty && !frame().document()->didLayoutWithPendingStylesheets() && m_firstVisuallyNonEmptyLayoutCallbackPending) {
m_firstVisuallyNonEmptyLayoutCallbackPending = false;
if (requestedMilestones & DidFirstVisuallyNonEmptyLayout)
milestonesAchieved |= DidFirstVisuallyNonEmptyLayout;
}
if (milestonesAchieved && frame().isMainFrame())
frame().loader().didReachLayoutMilestone(milestonesAchieved);
}
void FrameView::firePaintRelatedMilestonesIfNeeded()
{
Page* page = frame().page();
if (!page)
return;
LayoutMilestones milestonesAchieved = 0;
if (m_milestonesPendingPaint & DidFirstFlushForHeaderLayer) {
if (page->requestedLayoutMilestones() & DidFirstFlushForHeaderLayer)
milestonesAchieved |= DidFirstFlushForHeaderLayer;
}
if (m_milestonesPendingPaint & DidFirstPaintAfterSuppressedIncrementalRendering) {
if (page->requestedLayoutMilestones() & DidFirstPaintAfterSuppressedIncrementalRendering)
milestonesAchieved |= DidFirstPaintAfterSuppressedIncrementalRendering;
}
m_milestonesPendingPaint = 0;
if (milestonesAchieved)
page->mainFrame().loader().didReachLayoutMilestone(milestonesAchieved);
}
void FrameView::setVisualUpdatesAllowedByClient(bool visualUpdatesAllowed)
{
if (m_visualUpdatesAllowedByClient == visualUpdatesAllowed)
return;
m_visualUpdatesAllowedByClient = visualUpdatesAllowed;
frame().document()->setVisualUpdatesAllowedByClient(visualUpdatesAllowed);
}
void FrameView::setScrollPinningBehavior(ScrollPinningBehavior pinning)
{
m_scrollPinningBehavior = pinning;
if (Page* page = frame().page()) {
if (auto* scrollingCoordinator = page->scrollingCoordinator())
scrollingCoordinator->setScrollPinningBehavior(pinning);
}
updateScrollbars(scrollPosition());
}
ScrollBehaviorForFixedElements FrameView::scrollBehaviorForFixedElements() const
{
return frame().settings().backgroundShouldExtendBeyondPage() ? StickToViewportBounds : StickToDocumentBounds;
}
RenderView* FrameView::renderView() const
{
return frame().contentRenderer();
}
int FrameView::mapFromLayoutToCSSUnits(LayoutUnit value) const
{
return value / (frame().pageZoomFactor() * frame().frameScaleFactor());
}
LayoutUnit FrameView::mapFromCSSToLayoutUnits(int value) const
{
return value * frame().pageZoomFactor() * frame().frameScaleFactor();
}
void FrameView::didAddWidgetToRenderTree(Widget& widget)
{
ASSERT(!m_widgetsInRenderTree.contains(&widget));
m_widgetsInRenderTree.add(&widget);
}
void FrameView::willRemoveWidgetFromRenderTree(Widget& widget)
{
ASSERT(m_widgetsInRenderTree.contains(&widget));
m_widgetsInRenderTree.remove(&widget);
}
static Vector<RefPtr<Widget>> collectAndProtectWidgets(const HashSet<Widget*>& set)
{
Vector<RefPtr<Widget>> widgets;
copyToVector(set, widgets);
return widgets;
}
void FrameView::updateWidgetPositions()
{
for (auto& widget : collectAndProtectWidgets(m_widgetsInRenderTree)) {
if (RenderWidget* renderWidget = RenderWidget::find(widget.get())) {
auto ignoreWidgetState = renderWidget->updateWidgetPosition();
UNUSED_PARAM(ignoreWidgetState);
}
}
}
void FrameView::notifyWidgets(WidgetNotification notification)
{
for (auto& widget : collectAndProtectWidgets(m_widgetsInRenderTree))
widget->notifyWidget(notification);
}
void FrameView::setViewExposedRect(Optional<FloatRect> viewExposedRect)
{
if (m_viewExposedRect == viewExposedRect)
return;
LOG_WITH_STREAM(Scrolling, stream << "FrameView " << this << " setViewExposedRect " << (viewExposedRect ? viewExposedRect.value() : FloatRect()));
bool hasRectChanged = !m_viewExposedRect == !viewExposedRect;
m_viewExposedRect = viewExposedRect;
if (!frame().isMainFrame())
return;
if (TiledBacking* tiledBacking = this->tiledBacking()) {
if (hasRectChanged)
adjustTiledBackingScrollability();
adjustTiledBackingCoverage();
tiledBacking->setTiledScrollingIndicatorPosition(m_viewExposedRect ? m_viewExposedRect.value().location() : FloatPoint());
}
if (auto* view = renderView())
view->compositor().scheduleLayerFlush(false );
frame().mainFrame().pageOverlayController().didChangeViewExposedRect();
}
void FrameView::setViewportSizeForCSSViewportUnits(IntSize size)
{
if (m_hasOverrideViewportSize && m_overrideViewportSize == size)
return;
m_overrideViewportSize = size;
m_hasOverrideViewportSize = true;
if (Document* document = frame().document()) {
document->styleResolverChanged(DeferRecalcStyle);
}
}
IntSize FrameView::viewportSizeForCSSViewportUnits() const
{
if (m_hasOverrideViewportSize)
return m_overrideViewportSize;
if (useFixedLayout())
return fixedLayoutSize();
return visibleContentRectIncludingScrollbars().size();
}
bool FrameView::shouldPlaceBlockDirectionScrollbarOnLeft() const
{
return renderView() && renderView()->shouldPlaceBlockDirectionScrollbarOnLeft();
}
}