Don't enable speculative tiles immediately after main load stops progressing
[WebKit-https.git] / Source / WebCore / page / FrameView.cpp
1 /*
2  * Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
3  *                     1999 Lars Knoll <knoll@kde.org>
4  *                     1999 Antti Koivisto <koivisto@kde.org>
5  *                     2000 Dirk Mueller <mueller@kde.org>
6  * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2013 Apple Inc. All rights reserved.
7  *           (C) 2006 Graham Dennis (graham.dennis@gmail.com)
8  *           (C) 2006 Alexey Proskuryakov (ap@nypop.com)
9  * Copyright (C) 2009 Google Inc. All rights reserved.
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public License
22  * along with this library; see the file COPYING.LIB.  If not, write to
23  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  * Boston, MA 02110-1301, USA.
25  */
26
27 #include "config.h"
28 #include "FrameView.h"
29
30 #include "AXObjectCache.h"
31 #include "AnimationController.h"
32 #include "BackForwardController.h"
33 #include "CachedImage.h"
34 #include "CachedResourceLoader.h"
35 #include "Chrome.h"
36 #include "ChromeClient.h"
37 #include "DOMWindow.h"
38 #include "DocumentMarkerController.h"
39 #include "EventHandler.h"
40 #include "FloatRect.h"
41 #include "FocusController.h"
42 #include "FontCache.h"
43 #include "FontLoader.h"
44 #include "FrameLoader.h"
45 #include "FrameLoaderClient.h"
46 #include "FrameSelection.h"
47 #include "FrameTree.h"
48 #include "GraphicsContext.h"
49 #include "HTMLDocument.h"
50 #include "HTMLFrameElement.h"
51 #include "HTMLFrameSetElement.h"
52 #include "HTMLNames.h"
53 #include "HTMLPlugInImageElement.h"
54 #include "InspectorClient.h"
55 #include "InspectorController.h"
56 #include "InspectorInstrumentation.h"
57 #include "MainFrame.h"
58 #include "OverflowEvent.h"
59 #include "ProgressTracker.h"
60 #include "RenderEmbeddedObject.h"
61 #include "RenderFullScreen.h"
62 #include "RenderIFrame.h"
63 #include "RenderLayer.h"
64 #include "RenderLayerBacking.h"
65 #include "RenderScrollbar.h"
66 #include "RenderScrollbarPart.h"
67 #include "RenderStyle.h"
68 #include "RenderText.h"
69 #include "RenderTheme.h"
70 #include "RenderView.h"
71 #include "RenderWidget.h"
72 #include "ScrollAnimator.h"
73 #include "ScrollingCoordinator.h"
74 #include "Settings.h"
75 #include "StyleResolver.h"
76 #include "TextResourceDecoder.h"
77 #include "TextStream.h"
78
79 #include <wtf/CurrentTime.h>
80 #include <wtf/Ref.h>
81 #include <wtf/TemporaryChange.h>
82
83 #if USE(ACCELERATED_COMPOSITING)
84 #include "RenderLayerCompositor.h"
85 #include "TiledBacking.h"
86 #endif
87
88 #if ENABLE(SVG)
89 #include "RenderSVGRoot.h"
90 #include "SVGDocument.h"
91 #include "SVGSVGElement.h"
92 #endif
93
94 #if USE(TILED_BACKING_STORE)
95 #include "TiledBackingStore.h"
96 #endif
97
98 #if ENABLE(TEXT_AUTOSIZING)
99 #include "TextAutosizer.h"
100 #endif
101
102 #if PLATFORM(IOS)
103 #include "DocumentLoader.h"
104 #include "Logging.h"
105 #include "MemoryCache.h"
106 #include "MemoryPressureHandler.h"
107 #include "SystemMemory.h"
108 #include "TileCache.h"
109 #endif
110
111 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
112 #include "HTMLMediaElement.h"
113 #endif
114
115 namespace WebCore {
116
117 using namespace HTMLNames;
118
119 double FrameView::sCurrentPaintTimeStamp = 0.0;
120
121
122 // REPAINT_THROTTLING now chooses default values for throttling parameters.
123 // Should be removed when applications start using runtime configuration.
124 #if ENABLE(REPAINT_THROTTLING)
125 // Normal delay
126 double FrameView::s_normalDeferredRepaintDelay = 0.016;
127 // Negative value would mean that first few repaints happen without a delay
128 double FrameView::s_initialDeferredRepaintDelayDuringLoading = 0;
129 // The delay grows on each repaint to this maximum value
130 double FrameView::s_maxDeferredRepaintDelayDuringLoading = 2.5;
131 // On each repaint the delay increses by this amount
132 double FrameView::s_deferredRepaintDelayIncrementDuringLoading = 0.5;
133 #else
134 // FIXME: Repaint throttling could be good to have on all platform.
135 // The balance between CPU use and repaint frequency will need some tuning for desktop.
136 // More hooks may be needed to reset the delay on things like GIF and CSS animations.
137 double FrameView::s_normalDeferredRepaintDelay = 0;
138 double FrameView::s_initialDeferredRepaintDelayDuringLoading = 0;
139 double FrameView::s_maxDeferredRepaintDelayDuringLoading = 0;
140 double FrameView::s_deferredRepaintDelayIncrementDuringLoading = 0;
141 #endif
142
143 // The maximum number of updateEmbeddedObjects iterations that should be done before returning.
144 static const unsigned maxUpdateEmbeddedObjectsIterations = 2;
145
146 static RenderLayer::UpdateLayerPositionsFlags updateLayerPositionFlags(RenderLayer* layer, bool isRelayoutingSubtree, bool didFullRepaint)
147 {
148     RenderLayer::UpdateLayerPositionsFlags flags = RenderLayer::defaultFlags;
149     if (didFullRepaint) {
150         flags &= ~RenderLayer::CheckForRepaint;
151         flags |= RenderLayer::NeedsFullRepaintInBacking;
152     }
153     if (isRelayoutingSubtree && layer->isPaginated())
154         flags |= RenderLayer::UpdatePagination;
155     return flags;
156 }
157
158 Pagination::Mode paginationModeForRenderStyle(const RenderStyle& style)
159 {
160     EOverflow overflow = style.overflowY();
161     if (overflow != OPAGEDX && overflow != OPAGEDY)
162         return Pagination::Unpaginated;
163
164     bool isHorizontalWritingMode = style.isHorizontalWritingMode();
165     TextDirection textDirection = style.direction();
166     WritingMode writingMode = style.writingMode();
167
168     // paged-x always corresponds to LeftToRightPaginated or RightToLeftPaginated. If the WritingMode
169     // is horizontal, then we use TextDirection to choose between those options. If the WritingMode
170     // is vertical, then the direction of the verticality dictates the choice.
171     if (overflow == OPAGEDX) {
172         if ((isHorizontalWritingMode && textDirection == LTR) || writingMode == LeftToRightWritingMode)
173             return Pagination::LeftToRightPaginated;
174         return Pagination::RightToLeftPaginated;
175     }
176
177     // paged-y always corresponds to TopToBottomPaginated or BottomToTopPaginated. If the WritingMode
178     // is horizontal, then the direction of the horizontality dictates the choice. If the WritingMode
179     // is vertical, then we use TextDirection to choose between those options. 
180     if (writingMode == TopToBottomWritingMode || (!isHorizontalWritingMode && textDirection == RTL))
181         return Pagination::TopToBottomPaginated;
182     return Pagination::BottomToTopPaginated;
183 }
184
185 FrameView::FrameView(Frame& frame)
186     : m_frame(&frame)
187     , m_canHaveScrollbars(true)
188     , m_layoutTimer(this, &FrameView::layoutTimerFired)
189     , m_layoutRoot(0)
190     , m_layoutPhase(OutsideLayout)
191     , m_inSynchronousPostLayout(false)
192     , m_postLayoutTasksTimer(this, &FrameView::postLayoutTimerFired)
193     , m_isTransparent(false)
194     , m_baseBackgroundColor(Color::white)
195     , m_mediaType("screen")
196     , m_overflowStatusDirty(true)
197     , m_viewportRenderer(0)
198     , m_wasScrolledByUser(false)
199     , m_inProgrammaticScroll(false)
200     , m_safeToPropagateScrollToParent(true)
201     , m_deferredRepaintTimer(this, &FrameView::deferredRepaintTimerFired)
202     , m_isTrackingRepaints(false)
203     , m_shouldUpdateWhileOffscreen(true)
204     , m_exposedRect(FloatRect::infiniteRect())
205     , m_deferSetNeedsLayouts(0)
206     , m_setNeedsLayoutWasDeferred(false)
207     , m_speculativeTilingEnabled(false)
208     , m_speculativeTilingEnableTimer(this, &FrameView::speculativeTilingEnableTimerFired)
209 #if PLATFORM(IOS)
210     , m_useCustomFixedPositionLayoutRect(false)
211 #endif
212     , m_shouldAutoSize(false)
213     , m_inAutoSize(false)
214     , m_didRunAutosize(false)
215     , m_autoSizeFixedMinimumHeight(0)
216     , m_headerHeight(0)
217     , m_footerHeight(0)
218     , m_milestonesPendingPaint(0)
219     , m_visualUpdatesAllowedByClient(true)
220     , m_scrollPinningBehavior(DoNotPin)
221 {
222     init();
223
224     if (frame.isMainFrame()) {
225         ScrollableArea::setVerticalScrollElasticity(ScrollElasticityAllowed);
226         ScrollableArea::setHorizontalScrollElasticity(ScrollElasticityAutomatic);
227     }
228 }
229
230 PassRefPtr<FrameView> FrameView::create(Frame& frame)
231 {
232     RefPtr<FrameView> view = adoptRef(new FrameView(frame));
233     view->show();
234     return view.release();
235 }
236
237 PassRefPtr<FrameView> FrameView::create(Frame& frame, const IntSize& initialSize)
238 {
239     RefPtr<FrameView> view = adoptRef(new FrameView(frame));
240     view->Widget::setFrameRect(IntRect(view->location(), initialSize));
241     view->show();
242     return view.release();
243 }
244
245 FrameView::~FrameView()
246 {
247     if (m_postLayoutTasksTimer.isActive())
248         m_postLayoutTasksTimer.stop();
249     
250     removeFromAXObjectCache();
251     resetScrollbars();
252
253     // Custom scrollbars should already be destroyed at this point
254     ASSERT(!horizontalScrollbar() || !horizontalScrollbar()->isCustomScrollbar());
255     ASSERT(!verticalScrollbar() || !verticalScrollbar()->isCustomScrollbar());
256
257     setHasHorizontalScrollbar(false); // Remove native scrollbars now before we lose the connection to the HostWindow.
258     setHasVerticalScrollbar(false);
259     
260     ASSERT(!m_scrollCorner);
261
262     ASSERT(frame().view() != this || !frame().contentRenderer());
263 }
264
265 void FrameView::reset()
266 {
267     m_cannotBlitToWindow = false;
268     m_isOverlapped = false;
269     m_contentIsOpaque = false;
270     m_borderX = 30;
271     m_borderY = 30;
272     m_layoutTimer.stop();
273     m_layoutRoot = 0;
274     m_delayedLayout = false;
275     m_needsFullRepaint = true;
276     m_layoutSchedulingEnabled = true;
277     m_layoutPhase = OutsideLayout;
278     m_inSynchronousPostLayout = false;
279     m_layoutCount = 0;
280     m_nestedLayoutCount = 0;
281     m_postLayoutTasksTimer.stop();
282     m_firstLayout = true;
283     m_firstLayoutCallbackPending = false;
284     m_wasScrolledByUser = false;
285     m_safeToPropagateScrollToParent = true;
286     m_lastViewportSize = IntSize();
287     m_lastZoomFactor = 1.0f;
288     m_deferringRepaints = 0;
289     m_repaintCount = 0;
290     m_repaintRects.clear();
291     m_deferredRepaintDelay = s_initialDeferredRepaintDelayDuringLoading;
292     m_deferredRepaintTimer.stop();
293     m_isTrackingRepaints = false;
294     m_trackedRepaintRects.clear();
295     m_lastPaintTime = 0;
296     m_paintBehavior = PaintBehaviorNormal;
297     m_isPainting = false;
298     m_visuallyNonEmptyCharacterCount = 0;
299     m_visuallyNonEmptyPixelCount = 0;
300     m_isVisuallyNonEmpty = false;
301     m_firstVisuallyNonEmptyLayoutCallbackPending = true;
302     m_maintainScrollPositionAnchor = 0;
303 }
304
305 void FrameView::removeFromAXObjectCache()
306 {
307     if (AXObjectCache* cache = axObjectCache())
308         cache->remove(this);
309 }
310
311 void FrameView::resetScrollbars()
312 {
313     // Reset the document's scrollbars back to our defaults before we yield the floor.
314     m_firstLayout = true;
315     setScrollbarsSuppressed(true);
316     if (m_canHaveScrollbars)
317         setScrollbarModes(ScrollbarAuto, ScrollbarAuto);
318     else
319         setScrollbarModes(ScrollbarAlwaysOff, ScrollbarAlwaysOff);
320     setScrollbarsSuppressed(false);
321 }
322
323 void FrameView::resetScrollbarsAndClearContentsSize()
324 {
325     resetScrollbars();
326
327     setScrollbarsSuppressed(true);
328     setContentsSize(IntSize());
329     setScrollbarsSuppressed(false);
330 }
331
332 void FrameView::init()
333 {
334     reset();
335
336     m_margins = LayoutSize(-1, -1); // undefined
337     m_size = LayoutSize();
338
339     // Propagate the marginwidth/height and scrolling modes to the view.
340     Element* ownerElement = frame().ownerElement();
341     if (ownerElement && (ownerElement->hasTagName(frameTag) || ownerElement->hasTagName(iframeTag))) {
342         HTMLFrameElementBase* frameElt = toHTMLFrameElementBase(ownerElement);
343         if (frameElt->scrollingMode() == ScrollbarAlwaysOff)
344             setCanHaveScrollbars(false);
345         LayoutUnit marginWidth = frameElt->marginWidth();
346         LayoutUnit marginHeight = frameElt->marginHeight();
347         if (marginWidth != -1)
348             setMarginWidth(marginWidth);
349         if (marginHeight != -1)
350             setMarginHeight(marginHeight);
351     }
352
353     Page* page = frame().page();
354     if (page && page->chrome().client().shouldPaintEntireContents())
355         setPaintsEntireContents(true);
356 }
357     
358 void FrameView::prepareForDetach()
359 {
360     detachCustomScrollbars();
361     // When the view is no longer associated with a frame, it needs to be removed from the ax object cache
362     // right now, otherwise it won't be able to reach the topDocument()'s axObject cache later.
363     removeFromAXObjectCache();
364
365     if (frame().page()) {
366         if (ScrollingCoordinator* scrollingCoordinator = frame().page()->scrollingCoordinator())
367             scrollingCoordinator->willDestroyScrollableArea(this);
368     }
369 }
370
371 void FrameView::detachCustomScrollbars()
372 {
373     Scrollbar* horizontalBar = horizontalScrollbar();
374     if (horizontalBar && horizontalBar->isCustomScrollbar())
375         setHasHorizontalScrollbar(false);
376
377     Scrollbar* verticalBar = verticalScrollbar();
378     if (verticalBar && verticalBar->isCustomScrollbar())
379         setHasVerticalScrollbar(false);
380
381     m_scrollCorner = nullptr;
382 }
383
384 void FrameView::recalculateScrollbarOverlayStyle()
385 {
386     ScrollbarOverlayStyle oldOverlayStyle = scrollbarOverlayStyle();
387     ScrollbarOverlayStyle overlayStyle = ScrollbarOverlayStyleDefault;
388
389     Color backgroundColor = documentBackgroundColor();
390     if (backgroundColor.isValid()) {
391         // Reduce the background color from RGB to a lightness value
392         // and determine which scrollbar style to use based on a lightness
393         // heuristic.
394         double hue, saturation, lightness;
395         backgroundColor.getHSL(hue, saturation, lightness);
396         if (lightness <= .5 && backgroundColor.alpha() > 0)
397             overlayStyle = ScrollbarOverlayStyleLight;
398     }
399
400     if (oldOverlayStyle != overlayStyle)
401         setScrollbarOverlayStyle(overlayStyle);
402 }
403
404 void FrameView::clear()
405 {
406     setCanBlitOnScroll(true);
407     
408     reset();
409
410     setScrollbarsSuppressed(true);
411
412 #if PLATFORM(IOS)
413     // To avoid flashes of white, disable tile updates immediately when view is cleared at the beginning of a page load.
414     // Tiling will be re-enabled from UIKit via [WAKWindow setTilingMode:] when we have content to draw.
415     if (TileCache* tileCache = this->tileCache())
416         tileCache->setTilingMode(TileCache::Disabled);
417 #endif
418 }
419
420 bool FrameView::didFirstLayout() const
421 {
422     return !m_firstLayout;
423 }
424
425 void FrameView::invalidateRect(const IntRect& rect)
426 {
427     if (!parent()) {
428         if (HostWindow* window = hostWindow())
429             window->invalidateContentsAndRootView(rect, false /*immediate*/);
430         return;
431     }
432
433     RenderWidget* renderer = frame().ownerRenderer();
434     if (!renderer)
435         return;
436
437     IntRect repaintRect = rect;
438     repaintRect.move(renderer->borderLeft() + renderer->paddingLeft(),
439                      renderer->borderTop() + renderer->paddingTop());
440     renderer->repaintRectangle(repaintRect);
441 }
442
443 void FrameView::setFrameRect(const IntRect& newRect)
444 {
445     IntRect oldRect = frameRect();
446     if (newRect == oldRect)
447         return;
448
449 #if ENABLE(TEXT_AUTOSIZING)
450     // Autosized font sizes depend on the width of the viewing area.
451     if (newRect.width() != oldRect.width()) {
452         Page* page = frame().page();
453         if (frame().isMainFrame() && page->settings().textAutosizingEnabled()) {
454             for (Frame* frame = &page->mainFrame(); frame; frame = frame->tree().traverseNext())
455                 frame().document()->textAutosizer()->recalculateMultipliers();
456         }
457     }
458 #endif
459
460     ScrollView::setFrameRect(newRect);
461
462     updateScrollableAreaSet();
463
464 #if USE(ACCELERATED_COMPOSITING)
465     if (RenderView* renderView = this->renderView()) {
466         if (renderView->usesCompositing())
467             renderView->compositor().frameViewDidChangeSize();
468     }
469 #endif
470
471     if (!frameFlatteningEnabled())
472         sendResizeEventIfNeeded();
473 }
474
475 #if ENABLE(REQUEST_ANIMATION_FRAME)
476 bool FrameView::scheduleAnimation()
477 {
478     if (HostWindow* window = hostWindow()) {
479         window->scheduleAnimation();
480         return true;
481     }
482     return false;
483 }
484 #endif
485
486 void FrameView::setMarginWidth(LayoutUnit w)
487 {
488     // make it update the rendering area when set
489     m_margins.setWidth(w);
490 }
491
492 void FrameView::setMarginHeight(LayoutUnit h)
493 {
494     // make it update the rendering area when set
495     m_margins.setHeight(h);
496 }
497
498 bool FrameView::frameFlatteningEnabled() const
499 {
500     return frame().settings().frameFlatteningEnabled();
501 }
502
503 bool FrameView::isFrameFlatteningValidForThisFrame() const
504 {
505     if (!frameFlatteningEnabled())
506         return false;
507
508     HTMLFrameOwnerElement* owner = frame().ownerElement();
509     if (!owner)
510         return false;
511
512     // Frame flattening is valid only for <frame> and <iframe>.
513     return owner->hasTagName(frameTag) || owner->hasTagName(iframeTag);
514 }
515
516 bool FrameView::avoidScrollbarCreation() const
517 {
518     // with frame flattening no subframe can have scrollbars
519     // but we also cannot turn scrollbars off as we determine
520     // our flattening policy using that.
521     return isFrameFlatteningValidForThisFrame();
522 }
523
524 void FrameView::setCanHaveScrollbars(bool canHaveScrollbars)
525 {
526     m_canHaveScrollbars = canHaveScrollbars;
527     ScrollView::setCanHaveScrollbars(canHaveScrollbars);
528 }
529
530 void FrameView::updateCanHaveScrollbars()
531 {
532     ScrollbarMode hMode;
533     ScrollbarMode vMode;
534     scrollbarModes(hMode, vMode);
535     if (hMode == ScrollbarAlwaysOff && vMode == ScrollbarAlwaysOff)
536         setCanHaveScrollbars(false);
537     else
538         setCanHaveScrollbars(true);
539 }
540
541 PassRefPtr<Scrollbar> FrameView::createScrollbar(ScrollbarOrientation orientation)
542 {
543     if (!frame().settings().allowCustomScrollbarInMainFrame() && frame().isMainFrame())
544         return ScrollView::createScrollbar(orientation);
545
546     // FIXME: We need to update the scrollbar dynamically as documents change (or as doc elements and bodies get discovered that have custom styles).
547     Document* doc = frame().document();
548
549     // Try the <body> element first as a scrollbar source.
550     Element* body = doc ? doc->body() : 0;
551     if (body && body->renderer() && body->renderer()->style().hasPseudoStyle(SCROLLBAR))
552         return RenderScrollbar::createCustomScrollbar(this, orientation, body);
553     
554     // If the <body> didn't have a custom style, then the root element might.
555     Element* docElement = doc ? doc->documentElement() : 0;
556     if (docElement && docElement->renderer() && docElement->renderer()->style().hasPseudoStyle(SCROLLBAR))
557         return RenderScrollbar::createCustomScrollbar(this, orientation, docElement);
558         
559     // If we have an owning iframe/frame element, then it can set the custom scrollbar also.
560     RenderWidget* frameRenderer = frame().ownerRenderer();
561     if (frameRenderer && frameRenderer->style().hasPseudoStyle(SCROLLBAR))
562         return RenderScrollbar::createCustomScrollbar(this, orientation, 0, &frame());
563     
564     // Nobody set a custom style, so we just use a native scrollbar.
565     return ScrollView::createScrollbar(orientation);
566 }
567
568 void FrameView::setContentsSize(const IntSize& size)
569 {
570     if (size == contentsSize())
571         return;
572
573     m_deferSetNeedsLayouts++;
574
575     ScrollView::setContentsSize(size);
576     ScrollView::contentsResized();
577     
578     Page* page = frame().page();
579     if (!page)
580         return;
581
582     updateScrollableAreaSet();
583
584     page->chrome().contentsSizeChanged(&frame(), size); // Notify only.
585
586     ASSERT(m_deferSetNeedsLayouts);
587     m_deferSetNeedsLayouts--;
588     
589     if (!m_deferSetNeedsLayouts)
590         m_setNeedsLayoutWasDeferred = false; // FIXME: Find a way to make the deferred layout actually happen.
591 }
592
593 void FrameView::adjustViewSize()
594 {
595     RenderView* renderView = this->renderView();
596     if (!renderView)
597         return;
598
599     ASSERT(frame().view() == this);
600
601     const IntRect rect = renderView->documentRect();
602     const IntSize& size = rect.size();
603     ScrollView::setScrollOrigin(IntPoint(-rect.x(), -rect.y()), !frame().document()->printing(), size == contentsSize());
604     
605     setContentsSize(size);
606 }
607
608 void FrameView::applyOverflowToViewport(RenderElement* o, ScrollbarMode& hMode, ScrollbarMode& vMode)
609 {
610     // Handle the overflow:hidden/scroll case for the body/html elements.  WinIE treats
611     // overflow:hidden and overflow:scroll on <body> as applying to the document's
612     // scrollbars.  The CSS2.1 draft states that HTML UAs should use the <html> or <body> element and XML/XHTML UAs should
613     // use the root element.
614
615     // To combat the inability to scroll on a page with overflow:hidden on the root when scaled, disregard hidden when
616     // there is a frameScaleFactor that is greater than one on the main frame. Also disregard hidden if there is a
617     // header or footer.
618
619     bool overrideHidden = frame().isMainFrame() && ((frame().frameScaleFactor() > 1) || headerHeight() || footerHeight());
620
621     EOverflow overflowX = o->style().overflowX();
622     EOverflow overflowY = o->style().overflowY();
623
624 #if ENABLE(SVG)
625     if (o->isSVGRoot()) {
626         // overflow is ignored in stand-alone SVG documents.
627         if (!toRenderSVGRoot(o)->isEmbeddedThroughFrameContainingSVGDocument())
628             return;
629         overflowX = OHIDDEN;
630         overflowY = OHIDDEN;
631     }
632 #endif
633
634     switch (overflowX) {
635         case OHIDDEN:
636             if (overrideHidden)
637                 hMode = ScrollbarAuto;
638             else
639                 hMode = ScrollbarAlwaysOff;
640             break;
641         case OSCROLL:
642             hMode = ScrollbarAlwaysOn;
643             break;
644         case OAUTO:
645             hMode = ScrollbarAuto;
646             break;
647         default:
648             // Don't set it at all.
649             ;
650     }
651     
652      switch (overflowY) {
653         case OHIDDEN:
654             if (overrideHidden)
655                 vMode = ScrollbarAuto;
656             else
657                 vMode = ScrollbarAlwaysOff;
658             break;
659         case OSCROLL:
660             vMode = ScrollbarAlwaysOn;
661             break;
662         case OAUTO:
663             vMode = ScrollbarAuto;
664             break;
665         default:
666             // Don't set it at all. Values of OPAGEDX and OPAGEDY are handled by applyPaginationToViewPort().
667             ;
668     }
669
670     m_viewportRenderer = o;
671 }
672
673 void FrameView::applyPaginationToViewport()
674 {
675     Document* document = frame().document();
676     auto documentElement = document->documentElement();
677     RenderElement* documentRenderer = documentElement ? documentElement->renderer() : nullptr;
678     RenderElement* documentOrBodyRenderer = documentRenderer;
679     auto body = document->body();
680     if (body && body->renderer()) {
681         if (body->hasTagName(bodyTag))
682             documentOrBodyRenderer = documentRenderer->style().overflowX() == OVISIBLE && documentElement->hasTagName(htmlTag) ? body->renderer() : documentRenderer;
683     }
684
685     Pagination pagination;
686
687     if (!documentOrBodyRenderer) {
688         setPagination(pagination);
689         return;
690     }
691
692     EOverflow overflowY = documentOrBodyRenderer->style().overflowY();
693     if (overflowY == OPAGEDX || overflowY == OPAGEDY) {
694         pagination.mode = WebCore::paginationModeForRenderStyle(documentOrBodyRenderer->style());
695         pagination.gap = static_cast<unsigned>(documentOrBodyRenderer->style().columnGap());
696     }
697
698     setPagination(pagination);
699 }
700
701 void FrameView::calculateScrollbarModesForLayout(ScrollbarMode& hMode, ScrollbarMode& vMode, ScrollbarModesCalculationStrategy strategy)
702 {
703     m_viewportRenderer = 0;
704
705     const HTMLFrameOwnerElement* owner = frame().ownerElement();
706     if (owner && (owner->scrollingMode() == ScrollbarAlwaysOff)) {
707         hMode = ScrollbarAlwaysOff;
708         vMode = ScrollbarAlwaysOff;
709         return;
710     }  
711     
712     if (m_canHaveScrollbars || strategy == RulesFromWebContentOnly) {
713         hMode = ScrollbarAuto;
714         // Seamless documents begin with heights of 0; we special case that here
715         // to correctly render documents that don't need scrollbars.
716         IntSize fullVisibleSize = visibleContentRect(IncludeScrollbars).size();
717         bool isSeamlessDocument = frame().document() && frame().document()->shouldDisplaySeamlesslyWithParent();
718         vMode = (isSeamlessDocument && !fullVisibleSize.height()) ? ScrollbarAlwaysOff : ScrollbarAuto;
719     } else {
720         hMode = ScrollbarAlwaysOff;
721         vMode = ScrollbarAlwaysOff;
722     }
723     
724     if (!m_layoutRoot) {
725         Document* document = frame().document();
726         auto documentElement = document->documentElement();
727         RenderElement* rootRenderer = documentElement ? documentElement->renderer() : nullptr;
728         auto body = document->body();
729         if (body && body->renderer()) {
730             if (body->hasTagName(framesetTag) && !frameFlatteningEnabled()) {
731                 vMode = ScrollbarAlwaysOff;
732                 hMode = ScrollbarAlwaysOff;
733             } else if (body->hasTagName(bodyTag)) {
734                 // It's sufficient to just check the X overflow,
735                 // since it's illegal to have visible in only one direction.
736                 RenderElement* o = rootRenderer->style().overflowX() == OVISIBLE && document->documentElement()->hasTagName(htmlTag) ? body->renderer() : rootRenderer;
737                 applyOverflowToViewport(o, hMode, vMode);
738             }
739         } else if (rootRenderer)
740             applyOverflowToViewport(rootRenderer, hMode, vMode);
741     }    
742 }
743
744 #if USE(ACCELERATED_COMPOSITING)
745 void FrameView::updateCompositingLayersAfterStyleChange()
746 {
747     RenderView* renderView = this->renderView();
748     if (!renderView)
749         return;
750
751     // If we expect to update compositing after an incipient layout, don't do so here.
752     if (inPreLayoutStyleUpdate() || layoutPending() || renderView->needsLayout())
753         return;
754
755     RenderLayerCompositor& compositor = renderView->compositor();
756     // This call will make sure the cached hasAcceleratedCompositing is updated from the pref
757     compositor.cacheAcceleratedCompositingFlags();
758     compositor.updateCompositingLayers(CompositingUpdateAfterStyleChange);
759 }
760
761 void FrameView::updateCompositingLayersAfterLayout()
762 {
763     RenderView* renderView = this->renderView();
764     if (!renderView)
765         return;
766
767     // This call will make sure the cached hasAcceleratedCompositing is updated from the pref
768     renderView->compositor().cacheAcceleratedCompositingFlags();
769     renderView->compositor().updateCompositingLayers(CompositingUpdateAfterLayout);
770 }
771
772 void FrameView::clearBackingStores()
773 {
774     RenderView* renderView = this->renderView();
775     if (!renderView)
776         return;
777
778     RenderLayerCompositor& compositor = renderView->compositor();
779     ASSERT(compositor.inCompositingMode());
780     compositor.enableCompositingMode(false);
781     compositor.clearBackingForAllLayers();
782 }
783
784 void FrameView::restoreBackingStores()
785 {
786     RenderView* renderView = this->renderView();
787     if (!renderView)
788         return;
789
790     RenderLayerCompositor& compositor = renderView->compositor();
791     compositor.enableCompositingMode(true);
792     compositor.updateCompositingLayers(CompositingUpdateAfterLayout);
793 }
794
795 bool FrameView::usesCompositedScrolling() const
796 {
797     RenderView* renderView = this->renderView();
798     if (!renderView)
799         return false;
800     if (frame().settings().compositedScrollingForFramesEnabled())
801         return renderView->compositor().inForcedCompositingMode();
802     return false;
803 }
804
805 GraphicsLayer* FrameView::layerForScrolling() const
806 {
807     RenderView* renderView = this->renderView();
808     if (!renderView)
809         return 0;
810     return renderView->compositor().scrollLayer();
811 }
812
813 GraphicsLayer* FrameView::layerForHorizontalScrollbar() const
814 {
815     RenderView* renderView = this->renderView();
816     if (!renderView)
817         return 0;
818     return renderView->compositor().layerForHorizontalScrollbar();
819 }
820
821 GraphicsLayer* FrameView::layerForVerticalScrollbar() const
822 {
823     RenderView* renderView = this->renderView();
824     if (!renderView)
825         return 0;
826     return renderView->compositor().layerForVerticalScrollbar();
827 }
828
829 GraphicsLayer* FrameView::layerForScrollCorner() const
830 {
831     RenderView* renderView = this->renderView();
832     if (!renderView)
833         return 0;
834     return renderView->compositor().layerForScrollCorner();
835 }
836
837 TiledBacking* FrameView::tiledBacking() const
838 {
839     RenderView* renderView = this->renderView();
840     if (!renderView)
841         return 0;
842
843     RenderLayerBacking* backing = renderView->layer()->backing();
844     if (!backing)
845         return 0;
846
847     return backing->graphicsLayer()->tiledBacking();
848 }
849
850 uint64_t FrameView::scrollLayerID() const
851 {
852     RenderView* renderView = this->renderView();
853     if (!renderView)
854         return 0;
855
856     RenderLayerBacking* backing = renderView->layer()->backing();
857     if (!backing)
858         return 0;
859
860     return backing->scrollLayerID();
861 }
862
863 #if ENABLE(RUBBER_BANDING)
864 GraphicsLayer* FrameView::layerForOverhangAreas() const
865 {
866     RenderView* renderView = this->renderView();
867     if (!renderView)
868         return 0;
869     return renderView->compositor().layerForOverhangAreas();
870 }
871
872 GraphicsLayer* FrameView::setWantsLayerForTopOverHangArea(bool wantsLayer) const
873 {
874     RenderView* renderView = this->renderView();
875     if (!renderView)
876         return 0;
877
878     return renderView->compositor().updateLayerForTopOverhangArea(wantsLayer);
879 }
880
881 GraphicsLayer* FrameView::setWantsLayerForBottomOverHangArea(bool wantsLayer) const
882 {
883     RenderView* renderView = this->renderView();
884     if (!renderView)
885         return 0;
886
887     return renderView->compositor().updateLayerForBottomOverhangArea(wantsLayer);
888 }
889
890 #endif // ENABLE(RUBBER_BANDING)
891
892 bool FrameView::flushCompositingStateForThisFrame(Frame* rootFrameForFlush)
893 {
894     RenderView* renderView = this->renderView();
895     if (!renderView)
896         return true; // We don't want to keep trying to update layers if we have no renderer.
897
898     ASSERT(frame().view() == this);
899
900     // If we sync compositing layers when a layout is pending, we may cause painting of compositing
901     // layer content to occur before layout has happened, which will cause paintContents() to bail.
902     if (needsLayout())
903         return false;
904
905     // If we sync compositing layers and allow the repaint to be deferred, there is time for a
906     // visible flash to occur. Instead, stop the deferred repaint timer and repaint immediately.
907     flushDeferredRepaints();
908
909 #if PLATFORM(IOS)
910     if (TileCache* tileCache = this->tileCache())
911         tileCache->doPendingRepaints();
912 #endif
913
914     renderView->compositor().flushPendingLayerChanges(rootFrameForFlush == &frame());
915
916     return true;
917 }
918
919 void FrameView::setNeedsOneShotDrawingSynchronization()
920 {
921     if (Page* page = frame().page())
922         page->chrome().client().setNeedsOneShotDrawingSynchronization();
923 }
924
925 GraphicsLayer* FrameView::graphicsLayerForPlatformWidget(PlatformWidget platformWidget)
926 {
927     // To find the Widget that corresponds with platformWidget we have to do a linear
928     // search of our child widgets.
929     Widget* foundWidget = nullptr;
930     for (auto& widget : children()) {
931         if (widget->platformWidget() != platformWidget)
932             continue;
933         foundWidget = widget.get();
934         break;
935     }
936
937     if (!foundWidget)
938         return nullptr;
939
940     auto* renderWidget = RenderWidget::find(foundWidget);
941     if (!renderWidget)
942         return nullptr;
943
944     RenderLayer* widgetLayer = renderWidget->layer();
945     if (!widgetLayer || !widgetLayer->isComposited())
946         return nullptr;
947
948     return widgetLayer->backing()->parentForSublayers();
949 }
950
951 void FrameView::scheduleLayerFlushAllowingThrottling()
952 {
953     RenderView* view = this->renderView();
954     if (!view)
955         return;
956     view->compositor().scheduleLayerFlush(true /* canThrottle */);
957 }
958 #endif // USE(ACCELERATED_COMPOSITING)
959
960 void FrameView::setHeaderHeight(int headerHeight)
961 {
962     if (frame().page())
963         ASSERT(frame().isMainFrame());
964     m_headerHeight = headerHeight;
965
966     if (RenderView* renderView = this->renderView())
967         renderView->setNeedsLayout();
968 }
969
970 void FrameView::setFooterHeight(int footerHeight)
971 {
972     if (frame().page())
973         ASSERT(frame().isMainFrame());
974     m_footerHeight = footerHeight;
975
976     if (RenderView* renderView = this->renderView())
977         renderView->setNeedsLayout();
978 }
979
980 bool FrameView::hasCompositedContent() const
981 {
982 #if USE(ACCELERATED_COMPOSITING)
983     if (RenderView* renderView = this->renderView())
984         return renderView->compositor().inCompositingMode();
985 #endif
986     return false;
987 }
988
989 bool FrameView::hasCompositedContentIncludingDescendants() const
990 {
991 #if USE(ACCELERATED_COMPOSITING)
992     for (Frame* frame = m_frame.get(); frame; frame = frame->tree().traverseNext(m_frame.get())) {
993         RenderView* renderView = frame->contentRenderer();
994         if (RenderLayerCompositor* compositor = renderView ? &renderView->compositor() : 0) {
995             if (compositor->inCompositingMode())
996                 return true;
997
998             if (!RenderLayerCompositor::allowsIndependentlyCompositedFrames(this))
999                 break;
1000         }
1001     }
1002 #endif
1003     return false;
1004 }
1005
1006 bool FrameView::hasCompositingAncestor() const
1007 {
1008 #if USE(ACCELERATED_COMPOSITING)
1009     for (Frame* frame = this->frame().tree().parent(); frame; frame = frame->tree().parent()) {
1010         if (FrameView* view = frame->view()) {
1011             if (view->hasCompositedContent())
1012                 return true;
1013         }
1014     }
1015 #endif
1016     return false;
1017 }
1018
1019 // Sometimes (for plug-ins) we need to eagerly go into compositing mode.
1020 void FrameView::enterCompositingMode()
1021 {
1022 #if USE(ACCELERATED_COMPOSITING)
1023     if (RenderView* renderView = this->renderView()) {
1024         renderView->compositor().enableCompositingMode();
1025         if (!needsLayout())
1026             renderView->compositor().scheduleCompositingLayerUpdate();
1027     }
1028 #endif
1029 }
1030
1031 bool FrameView::isEnclosedInCompositingLayer() const
1032 {
1033 #if USE(ACCELERATED_COMPOSITING)
1034     auto frameOwnerRenderer = frame().ownerRenderer();
1035     if (frameOwnerRenderer && frameOwnerRenderer->containerForRepaint())
1036         return true;
1037
1038     if (FrameView* parentView = parentFrameView())
1039         return parentView->isEnclosedInCompositingLayer();
1040 #endif
1041     return false;
1042 }
1043
1044 bool FrameView::flushCompositingStateIncludingSubframes()
1045 {
1046 #if USE(ACCELERATED_COMPOSITING)
1047     bool allFramesFlushed = flushCompositingStateForThisFrame(&frame());
1048     
1049     for (Frame* child = frame().tree().firstChild(); child; child = child->tree().traverseNext(&frame())) {
1050         bool flushed = child->view()->flushCompositingStateForThisFrame(&frame());
1051         allFramesFlushed &= flushed;
1052     }
1053     return allFramesFlushed;
1054 #else // USE(ACCELERATED_COMPOSITING)
1055     return true;
1056 #endif
1057 }
1058
1059 bool FrameView::isSoftwareRenderable() const
1060 {
1061 #if USE(ACCELERATED_COMPOSITING)
1062     RenderView* renderView = this->renderView();
1063     return !renderView || !renderView->compositor().has3DContent();
1064 #else
1065     return true;
1066 #endif
1067 }
1068
1069 void FrameView::didMoveOnscreen()
1070 {
1071     contentAreaDidShow();
1072 }
1073
1074 void FrameView::willMoveOffscreen()
1075 {
1076     contentAreaDidHide();
1077 }
1078
1079 void FrameView::setIsInWindow(bool isInWindow)
1080 {
1081     if (RenderView* renderView = this->renderView())
1082         renderView->setIsInWindow(isInWindow);
1083 }
1084
1085 RenderObject* FrameView::layoutRoot(bool onlyDuringLayout) const
1086 {
1087     return onlyDuringLayout && layoutPending() ? 0 : m_layoutRoot;
1088 }
1089
1090 inline void FrameView::forceLayoutParentViewIfNeeded()
1091 {
1092 #if ENABLE(SVG)
1093     RenderWidget* ownerRenderer = frame().ownerRenderer();
1094     if (!ownerRenderer)
1095         return;
1096
1097     RenderBox* contentBox = embeddedContentBox();
1098     if (!contentBox)
1099         return;
1100
1101     RenderSVGRoot* svgRoot = toRenderSVGRoot(contentBox);
1102     if (svgRoot->everHadLayout() && !svgRoot->needsLayout())
1103         return;
1104
1105     // If the embedded SVG document appears the first time, the ownerRenderer has already finished
1106     // layout without knowing about the existence of the embedded SVG document, because RenderReplaced
1107     // embeddedContentBox() returns 0, as long as the embedded document isn't loaded yet. Before
1108     // bothering to lay out the SVG document, mark the ownerRenderer needing layout and ask its
1109     // FrameView for a layout. After that the RenderEmbeddedObject (ownerRenderer) carries the
1110     // correct size, which RenderSVGRoot::computeReplacedLogicalWidth/Height rely on, when laying
1111     // out for the first time, or when the RenderSVGRoot size has changed dynamically (eg. via <script>).
1112     Ref<FrameView> frameView(ownerRenderer->view().frameView());
1113
1114     // Mark the owner renderer as needing layout.
1115     ownerRenderer->setNeedsLayoutAndPrefWidthsRecalc();
1116
1117     // Synchronously enter layout, to layout the view containing the host object/embed/iframe.
1118     frameView->layout();
1119 #endif
1120 }
1121
1122 void FrameView::layout(bool allowSubtree)
1123 {
1124     if (isInLayout())
1125         return;
1126
1127     // Many of the tasks performed during layout can cause this function to be re-entered,
1128     // so save the layout phase now and restore it on exit.
1129     TemporaryChange<LayoutPhase> layoutPhaseRestorer(m_layoutPhase, InPreLayout);
1130
1131     // Protect the view from being deleted during layout (in recalcStyle)
1132     Ref<FrameView> protect(*this);
1133
1134     // Every scroll that happens during layout is programmatic.
1135     TemporaryChange<bool> changeInProgrammaticScroll(m_inProgrammaticScroll, true);
1136
1137     bool inChildFrameLayoutWithFrameFlattening = isInChildFrameWithFrameFlattening();
1138
1139     if (inChildFrameLayoutWithFrameFlattening) {
1140         startLayoutAtMainFrameViewIfNeeded(allowSubtree);
1141         RenderElement* root = m_layoutRoot ? m_layoutRoot : frame().document()->renderView();
1142         if (!root->needsLayout())
1143             return;
1144     }
1145
1146 #if PLATFORM(IOS)
1147     if (updateFixedPositionLayoutRect())
1148         allowSubtree = false;
1149 #endif
1150
1151     m_layoutTimer.stop();
1152     m_delayedLayout = false;
1153     m_setNeedsLayoutWasDeferred = false;
1154     
1155     // we shouldn't enter layout() while painting
1156     ASSERT(!isPainting());
1157     if (isPainting())
1158         return;
1159
1160     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willLayout(&frame());
1161
1162     if (!allowSubtree && m_layoutRoot) {
1163         m_layoutRoot->markContainingBlocksForLayout(false);
1164         m_layoutRoot = 0;
1165     }
1166
1167     ASSERT(frame().view() == this);
1168     ASSERT(frame().document());
1169
1170     Document& document = *frame().document();
1171     ASSERT(!document.inPageCache());
1172
1173     bool subtree;
1174     RenderElement* root;
1175
1176     {
1177         TemporaryChange<bool> changeSchedulingEnabled(m_layoutSchedulingEnabled, false);
1178
1179         if (!m_nestedLayoutCount && !m_inSynchronousPostLayout && m_postLayoutTasksTimer.isActive() && !inChildFrameLayoutWithFrameFlattening) {
1180             // This is a new top-level layout. If there are any remaining tasks from the previous
1181             // layout, finish them now.
1182             TemporaryChange<bool> inSynchronousPostLayoutChange(m_inSynchronousPostLayout, true);
1183             performPostLayoutTasks();
1184         }
1185
1186         m_layoutPhase = InPreLayoutStyleUpdate;
1187
1188         // Viewport-dependent media queries may cause us to need completely different style information.
1189         StyleResolver* styleResolver = document.styleResolverIfExists();
1190         if (!styleResolver || styleResolver->affectedByViewportChange()) {
1191             document.styleResolverChanged(DeferRecalcStyle);
1192             // FIXME: This instrumentation event is not strictly accurate since cached media query results do not persist across StyleResolver rebuilds.
1193             InspectorInstrumentation::mediaQueryResultChanged(&document);
1194         } else
1195             document.evaluateMediaQueryList();
1196
1197         // If there is any pagination to apply, it will affect the RenderView's style, so we should
1198         // take care of that now.
1199         applyPaginationToViewport();
1200
1201         // Always ensure our style info is up-to-date. This can happen in situations where
1202         // the layout beats any sort of style recalc update that needs to occur.
1203         document.updateStyleIfNeeded();
1204         m_layoutPhase = InPreLayout;
1205
1206         subtree = m_layoutRoot;
1207
1208         // If there is only one ref to this view left, then its going to be destroyed as soon as we exit, 
1209         // so there's no point to continuing to layout
1210         if (hasOneRef())
1211             return;
1212
1213         root = subtree ? m_layoutRoot : document.renderView();
1214         if (!root) {
1215             // FIXME: Do we need to set m_size here?
1216             return;
1217         }
1218
1219         // Close block here so we can set up the font cache purge preventer, which we will still
1220         // want in scope even after we want m_layoutSchedulingEnabled to be restored again.
1221         // The next block sets m_layoutSchedulingEnabled back to false once again.
1222     }
1223
1224     FontCachePurgePreventer fontCachePurgePreventer;
1225     RenderLayer* layer;
1226
1227     ++m_nestedLayoutCount;
1228
1229     {
1230         TemporaryChange<bool> changeSchedulingEnabled(m_layoutSchedulingEnabled, false);
1231
1232         if (!m_layoutRoot) {
1233             HTMLElement* body = document.body();
1234             if (body && body->renderer()) {
1235                 if (body->hasTagName(framesetTag) && !frameFlatteningEnabled()) {
1236                     body->renderer()->setChildNeedsLayout();
1237                 } else if (body->hasTagName(bodyTag)) {
1238                     if (!m_firstLayout && m_size.height() != layoutHeight() && body->renderer()->enclosingBox()->stretchesToViewport())
1239                         body->renderer()->setChildNeedsLayout();
1240                 }
1241             }
1242
1243 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1244             if (m_firstLayout && !frame().ownerElement())
1245                 printf("Elapsed time before first layout: %d\n", document.elapsedTime());
1246 #endif        
1247         }
1248
1249         autoSizeIfEnabled();
1250
1251         m_needsFullRepaint = !subtree && (m_firstLayout || toRenderView(*root).printing());
1252
1253         if (!subtree) {
1254             ScrollbarMode hMode;
1255             ScrollbarMode vMode;    
1256             calculateScrollbarModesForLayout(hMode, vMode);
1257
1258             if (m_firstLayout || (hMode != horizontalScrollbarMode() || vMode != verticalScrollbarMode())) {
1259                 if (m_firstLayout) {
1260                     setScrollbarsSuppressed(true);
1261
1262                     m_firstLayout = false;
1263                     m_firstLayoutCallbackPending = true;
1264                     if (useFixedLayout() && !fixedLayoutSize().isEmpty() && delegatesScrolling())
1265                         m_lastViewportSize = fixedLayoutSize();
1266                     else
1267 #if PLATFORM(IOS)
1268                         m_lastViewportSize = actualVisibleContentRect().size();
1269 #else
1270                         m_lastViewportSize = visibleContentRect(IncludeScrollbars).size();
1271 #endif
1272
1273                     m_lastZoomFactor = root->style().zoom();
1274
1275                     // Set the initial vMode to AlwaysOn if we're auto.
1276                     if (vMode == ScrollbarAuto)
1277                         setVerticalScrollbarMode(ScrollbarAlwaysOn); // This causes a vertical scrollbar to appear.
1278                     // Set the initial hMode to AlwaysOff if we're auto.
1279                     if (hMode == ScrollbarAuto)
1280                         setHorizontalScrollbarMode(ScrollbarAlwaysOff); // This causes a horizontal scrollbar to disappear.
1281
1282                     setScrollbarModes(hMode, vMode);
1283                     setScrollbarsSuppressed(false, true);
1284                 } else
1285                     setScrollbarModes(hMode, vMode);
1286             }
1287
1288             LayoutSize oldSize = m_size;
1289
1290             m_size = layoutSize();
1291
1292             if (oldSize != m_size) {
1293                 m_needsFullRepaint = true;
1294                 if (!m_firstLayout) {
1295                     RenderBox* rootRenderer = document.documentElement() ? document.documentElement()->renderBox() : 0;
1296                     RenderBox* bodyRenderer = rootRenderer && document.body() ? document.body()->renderBox() : 0;
1297                     if (bodyRenderer && bodyRenderer->stretchesToViewport())
1298                         bodyRenderer->setChildNeedsLayout();
1299                     else if (rootRenderer && rootRenderer->stretchesToViewport())
1300                         rootRenderer->setChildNeedsLayout();
1301                 }
1302             }
1303
1304             m_layoutPhase = InPreLayout;
1305         }
1306
1307         layer = root->enclosingLayer();
1308
1309         bool disableLayoutState = false;
1310         if (subtree) {
1311             disableLayoutState = root->view().shouldDisableLayoutStateForSubtree(root);
1312             root->view().pushLayoutState(*root);
1313         }
1314         LayoutStateDisabler layoutStateDisabler(disableLayoutState ? &root->view() : 0);
1315
1316         ASSERT(m_layoutPhase == InPreLayout);
1317         m_layoutPhase = InLayout;
1318
1319         beginDeferredRepaints();
1320         forceLayoutParentViewIfNeeded();
1321
1322         ASSERT(m_layoutPhase == InLayout);
1323
1324         root->layout();
1325 #if ENABLE(IOS_TEXT_AUTOSIZING)
1326         float minZoomFontSize = frame().settings().minimumZoomFontSize();
1327         float visWidth = frame().page()->mainFrame().textAutosizingWidth();
1328         if (minZoomFontSize && visWidth && !root->view().printing()) {
1329             root->adjustComputedFontSizesOnBlocks(minZoomFontSize, visWidth);    
1330             bool needsLayout = root->needsLayout();
1331             if (needsLayout)
1332                 root->layout();
1333         }
1334 #endif
1335 #if ENABLE(TEXT_AUTOSIZING)
1336         if (document.textAutosizer()->processSubtree(root) && root->needsLayout())
1337             root->layout();
1338 #endif
1339         endDeferredRepaints();
1340
1341         ASSERT(m_layoutPhase == InLayout);
1342
1343         if (subtree)
1344             root->view().popLayoutState(*root);
1345
1346         m_layoutRoot = 0;
1347
1348         // Close block here to end the scope of changeSchedulingEnabled and layoutStateDisabler.
1349     }
1350
1351     m_layoutPhase = InViewSizeAdjust;
1352
1353     bool neededFullRepaint = m_needsFullRepaint;
1354
1355     if (!subtree && !toRenderView(*root).printing())
1356         adjustViewSize();
1357
1358     m_layoutPhase = InPostLayout;
1359
1360     m_needsFullRepaint = neededFullRepaint;
1361
1362     // Now update the positions of all layers.
1363     beginDeferredRepaints();
1364     if (m_needsFullRepaint)
1365         root->view().repaintRootContents();
1366
1367     layer->updateLayerPositionsAfterLayout(renderView()->layer(), updateLayerPositionFlags(layer, subtree, m_needsFullRepaint));
1368
1369     endDeferredRepaints();
1370
1371 #if USE(ACCELERATED_COMPOSITING)
1372     updateCompositingLayersAfterLayout();
1373 #endif
1374     
1375     m_layoutCount++;
1376
1377 #if PLATFORM(MAC) || PLATFORM(WIN) || PLATFORM(GTK) || PLATFORM(EFL)
1378     if (AXObjectCache* cache = root->document().existingAXObjectCache())
1379         cache->postNotification(root, AXObjectCache::AXLayoutComplete);
1380 #endif
1381
1382 #if ENABLE(DASHBOARD_SUPPORT) || ENABLE(DRAGGABLE_REGION)
1383     updateAnnotatedRegions();
1384 #endif
1385
1386 #if ENABLE(IOS_TOUCH_EVENTS)
1387     document.dirtyTouchEventRects();
1388 #endif
1389
1390     ASSERT(!root->needsLayout());
1391
1392     updateCanBlitOnScrollRecursively();
1393
1394     if (document.hasListenerType(Document::OVERFLOWCHANGED_LISTENER))
1395         updateOverflowStatus(layoutWidth() < contentsWidth(), layoutHeight() < contentsHeight());
1396
1397     if (!m_postLayoutTasksTimer.isActive()) {
1398         if (!m_inSynchronousPostLayout) {
1399             if (inChildFrameLayoutWithFrameFlattening)
1400                 updateWidgetPositions();
1401             else {
1402                 TemporaryChange<bool> inSynchronousPostLayoutChange(m_inSynchronousPostLayout, true);
1403                 performPostLayoutTasks(); // Calls resumeScheduledEvents().
1404             }
1405         }
1406
1407         if (!m_postLayoutTasksTimer.isActive() && (needsLayout() || m_inSynchronousPostLayout || inChildFrameLayoutWithFrameFlattening)) {
1408             // If we need layout or are already in a synchronous call to postLayoutTasks(), 
1409             // defer widget updates and event dispatch until after we return. postLayoutTasks()
1410             // can make us need to update again, and we can get stuck in a nasty cycle unless
1411             // we call it through the timer here.
1412             m_postLayoutTasksTimer.startOneShot(0);
1413             if (needsLayout())
1414                 layout();
1415         }
1416     }
1417
1418     InspectorInstrumentation::didLayout(cookie, root);
1419
1420     --m_nestedLayoutCount;
1421
1422     if (m_nestedLayoutCount)
1423         return;
1424
1425     if (Page* page = frame().page())
1426         page->chrome().client().layoutUpdated(&frame());
1427 }
1428
1429 RenderBox* FrameView::embeddedContentBox() const
1430 {
1431 #if ENABLE(SVG)
1432     RenderView* renderView = this->renderView();
1433     if (!renderView)
1434         return nullptr;
1435
1436     RenderObject* firstChild = renderView->firstChild();
1437     if (!firstChild || !firstChild->isBox())
1438         return nullptr;
1439
1440     // Curently only embedded SVG documents participate in the size-negotiation logic.
1441     if (toRenderBox(firstChild)->isSVGRoot())
1442         return toRenderBox(firstChild);
1443 #endif
1444
1445     return nullptr;
1446 }
1447
1448 void FrameView::addEmbeddedObjectToUpdate(RenderEmbeddedObject& embeddedObject)
1449 {
1450     if (!m_embeddedObjectsToUpdate)
1451         m_embeddedObjectsToUpdate = adoptPtr(new ListHashSet<RenderEmbeddedObject*>);
1452
1453     HTMLFrameOwnerElement& element = embeddedObject.frameOwnerElement();
1454     if (isHTMLObjectElement(element) || isHTMLEmbedElement(element)) {
1455         // Tell the DOM element that it needs a widget update.
1456         HTMLPlugInImageElement& pluginElement = toHTMLPlugInImageElement(element);
1457         if (!pluginElement.needsCheckForSizeChange())
1458             pluginElement.setNeedsWidgetUpdate(true);
1459     }
1460
1461     m_embeddedObjectsToUpdate->add(&embeddedObject);
1462 }
1463
1464 void FrameView::removeEmbeddedObjectToUpdate(RenderEmbeddedObject& embeddedObject)
1465 {
1466     if (!m_embeddedObjectsToUpdate)
1467         return;
1468
1469     m_embeddedObjectsToUpdate->remove(&embeddedObject);
1470 }
1471
1472 void FrameView::setMediaType(const String& mediaType)
1473 {
1474     m_mediaType = mediaType;
1475 }
1476
1477 String FrameView::mediaType() const
1478 {
1479     // See if we have an override type.
1480     String overrideType = frame().loader().client().overrideMediaType();
1481     InspectorInstrumentation::applyEmulatedMedia(&frame(), &overrideType);
1482     if (!overrideType.isNull())
1483         return overrideType;
1484     return m_mediaType;
1485 }
1486
1487 void FrameView::adjustMediaTypeForPrinting(bool printing)
1488 {
1489     if (printing) {
1490         if (m_mediaTypeWhenNotPrinting.isNull())
1491             m_mediaTypeWhenNotPrinting = mediaType();
1492             setMediaType("print");
1493     } else {
1494         if (!m_mediaTypeWhenNotPrinting.isNull())
1495             setMediaType(m_mediaTypeWhenNotPrinting);
1496         m_mediaTypeWhenNotPrinting = String();
1497     }
1498 }
1499
1500 bool FrameView::useSlowRepaints(bool considerOverlap) const
1501 {
1502     bool mustBeSlow = hasSlowRepaintObjects() || (platformWidget() && hasViewportConstrainedObjects());
1503
1504     // FIXME: WidgetMac.mm makes the assumption that useSlowRepaints ==
1505     // m_contentIsOpaque, so don't take the fast path for composited layers
1506     // if they are a platform widget in order to get painting correctness
1507     // for transparent layers. See the comment in WidgetMac::paint.
1508     if (contentsInCompositedLayer() && !platformWidget())
1509         return mustBeSlow;
1510
1511     bool isOverlapped = m_isOverlapped && considerOverlap;
1512
1513     if (mustBeSlow || m_cannotBlitToWindow || isOverlapped || !m_contentIsOpaque)
1514         return true;
1515
1516     if (FrameView* parentView = parentFrameView())
1517         return parentView->useSlowRepaints(considerOverlap);
1518
1519     return false;
1520 }
1521
1522 bool FrameView::useSlowRepaintsIfNotOverlapped() const
1523 {
1524     return useSlowRepaints(false);
1525 }
1526
1527 void FrameView::updateCanBlitOnScrollRecursively()
1528 {
1529     for (Frame* frame = m_frame.get(); frame; frame = frame->tree().traverseNext(m_frame.get())) {
1530         if (FrameView* view = frame->view())
1531             view->setCanBlitOnScroll(!view->useSlowRepaints());
1532     }
1533 }
1534
1535 bool FrameView::contentsInCompositedLayer() const
1536 {
1537 #if USE(ACCELERATED_COMPOSITING)
1538     RenderView* renderView = this->renderView();
1539     if (renderView && renderView->isComposited()) {
1540         GraphicsLayer* layer = renderView->layer()->backing()->graphicsLayer();
1541         if (layer && layer->drawsContent())
1542             return true;
1543     }
1544 #endif
1545     return false;
1546 }
1547
1548 void FrameView::setCannotBlitToWindow()
1549 {
1550     m_cannotBlitToWindow = true;
1551     updateCanBlitOnScrollRecursively();
1552 }
1553
1554 void FrameView::addSlowRepaintObject(RenderElement* o)
1555 {
1556     bool hadSlowRepaintObjects = hasSlowRepaintObjects();
1557
1558     if (!m_slowRepaintObjects)
1559         m_slowRepaintObjects = adoptPtr(new HashSet<RenderElement*>);
1560
1561     m_slowRepaintObjects->add(o);
1562
1563     if (!hadSlowRepaintObjects) {
1564         updateCanBlitOnScrollRecursively();
1565
1566         if (Page* page = frame().page()) {
1567             if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
1568                 scrollingCoordinator->frameViewHasSlowRepaintObjectsDidChange(this);
1569         }
1570     }
1571 }
1572
1573 void FrameView::removeSlowRepaintObject(RenderElement* o)
1574 {
1575     if (!m_slowRepaintObjects)
1576         return;
1577
1578     m_slowRepaintObjects->remove(o);
1579     if (m_slowRepaintObjects->isEmpty()) {
1580         m_slowRepaintObjects = nullptr;
1581         updateCanBlitOnScrollRecursively();
1582
1583         if (Page* page = frame().page()) {
1584             if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
1585                 scrollingCoordinator->frameViewHasSlowRepaintObjectsDidChange(this);
1586         }
1587     }
1588 }
1589
1590 void FrameView::addViewportConstrainedObject(RenderElement* object)
1591 {
1592     if (!m_viewportConstrainedObjects)
1593         m_viewportConstrainedObjects = adoptPtr(new ViewportConstrainedObjectSet);
1594
1595     if (!m_viewportConstrainedObjects->contains(object)) {
1596         m_viewportConstrainedObjects->add(object);
1597         if (platformWidget())
1598             updateCanBlitOnScrollRecursively();
1599
1600         if (Page* page = frame().page()) {
1601             if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
1602                 scrollingCoordinator->frameViewFixedObjectsDidChange(this);
1603         }
1604     }
1605 }
1606
1607 void FrameView::removeViewportConstrainedObject(RenderElement* object)
1608 {
1609     if (m_viewportConstrainedObjects && m_viewportConstrainedObjects->remove(object)) {
1610         if (Page* page = frame().page()) {
1611             if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
1612                 scrollingCoordinator->frameViewFixedObjectsDidChange(this);
1613         }
1614
1615         // FIXME: In addFixedObject() we only call this if there's a platform widget,
1616         // why isn't the same check being made here?
1617         updateCanBlitOnScrollRecursively();
1618     }
1619 }
1620
1621 LayoutRect FrameView::viewportConstrainedVisibleContentRect() const
1622 {
1623 #if PLATFORM(IOS)
1624     if (useCustomFixedPositionLayoutRect())
1625         return customFixedPositionLayoutRect();
1626 #endif
1627     LayoutRect viewportRect = visibleContentRect();
1628     viewportRect.setLocation(toPoint(scrollOffsetForFixedPosition()));
1629     return viewportRect;
1630 }
1631
1632 IntSize FrameView::scrollOffsetForFixedPosition(const IntRect& visibleContentRect, const IntSize& totalContentsSize, const IntPoint& scrollPosition, const IntPoint& scrollOrigin, float frameScaleFactor, bool fixedElementsLayoutRelativeToFrame, ScrollBehaviorForFixedElements behaviorForFixed, int headerHeight, int footerHeight)
1633 {
1634     IntPoint position;
1635     if (behaviorForFixed == StickToDocumentBounds)
1636         position = ScrollableArea::constrainScrollPositionForOverhang(visibleContentRect, totalContentsSize, scrollPosition, scrollOrigin, headerHeight, footerHeight);
1637     else {
1638         position = scrollPosition;
1639         position.setY(position.y() - headerHeight);
1640     }
1641
1642     IntSize maxSize = totalContentsSize - visibleContentRect.size();
1643
1644     float dragFactorX = (fixedElementsLayoutRelativeToFrame || !maxSize.width()) ? 1 : (totalContentsSize.width() - visibleContentRect.width() * frameScaleFactor) / maxSize.width();
1645     float dragFactorY = (fixedElementsLayoutRelativeToFrame || !maxSize.height()) ? 1 : (totalContentsSize.height() - visibleContentRect.height() * frameScaleFactor) / maxSize.height();
1646
1647     return IntSize(position.x() * dragFactorX / frameScaleFactor, position.y() * dragFactorY / frameScaleFactor);
1648 }
1649
1650 IntSize FrameView::scrollOffsetForFixedPosition() const
1651 {
1652     IntRect visibleContentRect = this->visibleContentRect();
1653     IntSize totalContentsSize = this->totalContentsSize();
1654     IntPoint scrollPosition = this->scrollPosition();
1655     IntPoint scrollOrigin = this->scrollOrigin();
1656     float frameScaleFactor = frame().frameScaleFactor();
1657     ScrollBehaviorForFixedElements behaviorForFixed = scrollBehaviorForFixedElements();
1658     return scrollOffsetForFixedPosition(visibleContentRect, totalContentsSize, scrollPosition, scrollOrigin, frameScaleFactor, fixedElementsLayoutRelativeToFrame(), behaviorForFixed, headerHeight(), footerHeight());
1659 }
1660     
1661 IntPoint FrameView::minimumScrollPosition() const
1662 {
1663     IntPoint minimumPosition(ScrollView::minimumScrollPosition());
1664
1665     if (frame().isMainFrame() && m_scrollPinningBehavior == PinToBottom)
1666         minimumPosition.setY(maximumScrollPosition().y());
1667     
1668     return minimumPosition;
1669 }
1670
1671 IntPoint FrameView::maximumScrollPosition() const
1672 {
1673     IntPoint maximumOffset(contentsWidth() - visibleWidth() - scrollOrigin().x(), totalContentsSize().height() - visibleHeight() - scrollOrigin().y());
1674
1675     maximumOffset.clampNegativeToZero();
1676
1677     if (frame().isMainFrame() && m_scrollPinningBehavior == PinToTop)
1678         maximumOffset.setY(minimumScrollPosition().y());
1679     
1680     return maximumOffset;
1681 }
1682
1683 bool FrameView::fixedElementsLayoutRelativeToFrame() const
1684 {
1685     return frame().settings().fixedElementsLayoutRelativeToFrame();
1686 }
1687
1688 IntPoint FrameView::lastKnownMousePosition() const
1689 {
1690     return frame().eventHandler().lastKnownMousePosition();
1691 }
1692
1693 bool FrameView::isHandlingWheelEvent() const
1694 {
1695     return frame().eventHandler().isHandlingWheelEvent();
1696 }
1697
1698 bool FrameView::shouldSetCursor() const
1699 {
1700     Page* page = frame().page();
1701     return page && page->isVisible() && page->focusController().isActive();
1702 }
1703
1704 bool FrameView::scrollContentsFastPath(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect)
1705 {
1706     if (!m_viewportConstrainedObjects || m_viewportConstrainedObjects->isEmpty()) {
1707         hostWindow()->scroll(scrollDelta, rectToScroll, clipRect);
1708         return true;
1709     }
1710
1711     const bool isCompositedContentLayer = contentsInCompositedLayer();
1712
1713     // Get the rects of the fixed objects visible in the rectToScroll
1714     Region regionToUpdate;
1715     for (auto& renderer : *m_viewportConstrainedObjects) {
1716         if (!renderer->style().hasViewportConstrainedPosition())
1717             continue;
1718 #if USE(ACCELERATED_COMPOSITING)
1719         if (renderer->isComposited())
1720             continue;
1721 #endif
1722
1723         // Fixed items should always have layers.
1724         ASSERT(renderer->hasLayer());
1725         RenderLayer* layer = toRenderBoxModelObject(renderer)->layer();
1726
1727 #if USE(ACCELERATED_COMPOSITING)
1728         if (layer->viewportConstrainedNotCompositedReason() == RenderLayer::NotCompositedForBoundsOutOfView
1729             || layer->viewportConstrainedNotCompositedReason() == RenderLayer::NotCompositedForNoVisibleContent) {
1730             // Don't invalidate for invisible fixed layers.
1731             continue;
1732         }
1733 #endif
1734
1735 #if ENABLE(CSS_FILTERS)
1736         if (layer->hasAncestorWithFilterOutsets()) {
1737             // If the fixed layer has a blur/drop-shadow filter applied on at least one of its parents, we cannot 
1738             // scroll using the fast path, otherwise the outsets of the filter will be moved around the page.
1739             return false;
1740         }
1741 #endif
1742         IntRect updateRect = pixelSnappedIntRect(layer->repaintRectIncludingNonCompositingDescendants());
1743         updateRect = contentsToRootView(updateRect);
1744         if (!isCompositedContentLayer && clipsRepaints())
1745             updateRect.intersect(rectToScroll);
1746         if (!updateRect.isEmpty())
1747             regionToUpdate.unite(updateRect);
1748     }
1749
1750     // 1) scroll
1751     hostWindow()->scroll(scrollDelta, rectToScroll, clipRect);
1752
1753     // 2) update the area of fixed objects that has been invalidated
1754     Vector<IntRect> subRectsToUpdate = regionToUpdate.rects();
1755     size_t viewportConstrainedObjectsCount = subRectsToUpdate.size();
1756     for (size_t i = 0; i < viewportConstrainedObjectsCount; ++i) {
1757         IntRect updateRect = subRectsToUpdate[i];
1758         IntRect scrolledRect = updateRect;
1759         scrolledRect.move(scrollDelta);
1760         updateRect.unite(scrolledRect);
1761 #if USE(ACCELERATED_COMPOSITING)
1762         if (isCompositedContentLayer) {
1763             updateRect = rootViewToContents(updateRect);
1764             ASSERT(renderView());
1765             renderView()->layer()->setBackingNeedsRepaintInRect(updateRect);
1766             continue;
1767         }
1768 #endif
1769         if (clipsRepaints())
1770             updateRect.intersect(rectToScroll);
1771         hostWindow()->invalidateContentsAndRootView(updateRect, false);
1772     }
1773
1774     return true;
1775 }
1776
1777 void FrameView::scrollContentsSlowPath(const IntRect& updateRect)
1778 {
1779 #if USE(ACCELERATED_COMPOSITING)
1780     if (contentsInCompositedLayer()) {
1781         IntRect updateRect = visibleContentRect();
1782
1783         // Make sure to "apply" the scale factor here since we're converting from frame view
1784         // coordinates to layer backing coordinates.
1785         updateRect.scale(1 / frame().frameScaleFactor());
1786
1787         ASSERT(renderView());
1788         renderView()->layer()->setBackingNeedsRepaintInRect(updateRect);
1789     }
1790
1791     repaintSlowRepaintObjects();
1792
1793     if (RenderWidget* frameRenderer = frame().ownerRenderer()) {
1794         if (isEnclosedInCompositingLayer()) {
1795             LayoutRect rect(frameRenderer->borderLeft() + frameRenderer->paddingLeft(),
1796                             frameRenderer->borderTop() + frameRenderer->paddingTop(),
1797                             visibleWidth(), visibleHeight());
1798             frameRenderer->repaintRectangle(rect);
1799             return;
1800         }
1801     }
1802 #endif
1803
1804     ScrollView::scrollContentsSlowPath(updateRect);
1805 }
1806
1807 void FrameView::repaintSlowRepaintObjects()
1808 {
1809     if (!m_slowRepaintObjects)
1810         return;
1811
1812     // Renderers with fixed backgrounds may be in compositing layers, so we need to explicitly
1813     // repaint them after scrolling.
1814     for (auto& renderer : *m_slowRepaintObjects)
1815         renderer->repaint();
1816 }
1817
1818 // Note that this gets called at painting time.
1819 void FrameView::setIsOverlapped(bool isOverlapped)
1820 {
1821     if (isOverlapped == m_isOverlapped)
1822         return;
1823
1824     m_isOverlapped = isOverlapped;
1825     updateCanBlitOnScrollRecursively();
1826     
1827 #if USE(ACCELERATED_COMPOSITING)
1828     if (hasCompositedContentIncludingDescendants()) {
1829         // Overlap can affect compositing tests, so if it changes, we need to trigger
1830         // a layer update in the parent document.
1831         if (Frame* parentFrame = frame().tree().parent()) {
1832             if (RenderView* parentView = parentFrame->contentRenderer()) {
1833                 RenderLayerCompositor& compositor = parentView->compositor();
1834                 compositor.setCompositingLayersNeedRebuild();
1835                 compositor.scheduleCompositingLayerUpdate();
1836             }
1837         }
1838
1839         if (RenderLayerCompositor::allowsIndependentlyCompositedFrames(this)) {
1840             // We also need to trigger reevaluation for this and all descendant frames,
1841             // since a frame uses compositing if any ancestor is compositing.
1842             for (Frame* frame = m_frame.get(); frame; frame = frame->tree().traverseNext(m_frame.get())) {
1843                 if (RenderView* view = frame->contentRenderer()) {
1844                     RenderLayerCompositor& compositor = view->compositor();
1845                     compositor.setCompositingLayersNeedRebuild();
1846                     compositor.scheduleCompositingLayerUpdate();
1847                 }
1848             }
1849         }
1850     }
1851 #endif
1852 }
1853
1854 bool FrameView::isOverlappedIncludingAncestors() const
1855 {
1856     if (isOverlapped())
1857         return true;
1858
1859     if (FrameView* parentView = parentFrameView()) {
1860         if (parentView->isOverlapped())
1861             return true;
1862     }
1863
1864     return false;
1865 }
1866
1867 void FrameView::setContentIsOpaque(bool contentIsOpaque)
1868 {
1869     if (contentIsOpaque == m_contentIsOpaque)
1870         return;
1871
1872     m_contentIsOpaque = contentIsOpaque;
1873     updateCanBlitOnScrollRecursively();
1874 }
1875
1876 void FrameView::restoreScrollbar()
1877 {
1878     setScrollbarsSuppressed(false);
1879 }
1880
1881 bool FrameView::scrollToFragment(const URL& url)
1882 {
1883     // If our URL has no ref, then we have no place we need to jump to.
1884     // OTOH If CSS target was set previously, we want to set it to 0, recalc
1885     // and possibly repaint because :target pseudo class may have been
1886     // set (see bug 11321).
1887     if (!url.hasFragmentIdentifier() && !frame().document()->cssTarget())
1888         return false;
1889
1890     String fragmentIdentifier = url.fragmentIdentifier();
1891     if (scrollToAnchor(fragmentIdentifier))
1892         return true;
1893
1894     // Try again after decoding the ref, based on the document's encoding.
1895     if (TextResourceDecoder* decoder = frame().document()->decoder())
1896         return scrollToAnchor(decodeURLEscapeSequences(fragmentIdentifier, decoder->encoding()));
1897
1898     return false;
1899 }
1900
1901 bool FrameView::scrollToAnchor(const String& name)
1902 {
1903     ASSERT(frame().document());
1904
1905     if (!frame().document()->haveStylesheetsLoaded()) {
1906         frame().document()->setGotoAnchorNeededAfterStylesheetsLoad(true);
1907         return false;
1908     }
1909
1910     frame().document()->setGotoAnchorNeededAfterStylesheetsLoad(false);
1911
1912     Element* anchorElement = frame().document()->findAnchor(name);
1913
1914     // Setting to null will clear the current target.
1915     frame().document()->setCSSTarget(anchorElement);
1916
1917 #if ENABLE(SVG)
1918     if (frame().document()->isSVGDocument()) {
1919         if (SVGSVGElement* svg = toSVGDocument(frame().document())->rootElement()) {
1920             svg->setupInitialView(name, anchorElement);
1921             if (!anchorElement)
1922                 return true;
1923         }
1924     }
1925 #endif
1926   
1927     // Implement the rule that "" and "top" both mean top of page as in other browsers.
1928     if (!anchorElement && !(name.isEmpty() || equalIgnoringCase(name, "top")))
1929         return false;
1930
1931     maintainScrollPositionAtAnchor(anchorElement ? static_cast<Node*>(anchorElement) : frame().document());
1932     
1933     // If the anchor accepts keyboard focus, move focus there to aid users relying on keyboard navigation.
1934     if (anchorElement && anchorElement->isFocusable())
1935         frame().document()->setFocusedElement(anchorElement);
1936     
1937     return true;
1938 }
1939
1940 void FrameView::maintainScrollPositionAtAnchor(Node* anchorNode)
1941 {
1942     m_maintainScrollPositionAnchor = anchorNode;
1943     if (!m_maintainScrollPositionAnchor)
1944         return;
1945
1946     // We need to update the layout before scrolling, otherwise we could
1947     // really mess things up if an anchor scroll comes at a bad moment.
1948     frame().document()->updateStyleIfNeeded();
1949     // Only do a layout if changes have occurred that make it necessary.
1950     RenderView* renderView = this->renderView();
1951     if (renderView && renderView->needsLayout())
1952         layout();
1953     else
1954         scrollToAnchor();
1955 }
1956
1957 void FrameView::scrollElementToRect(Element* element, const IntRect& rect)
1958 {
1959     frame().document()->updateLayoutIgnorePendingStylesheets();
1960
1961     LayoutRect bounds = element->boundingBox();
1962     int centeringOffsetX = (rect.width() - bounds.width()) / 2;
1963     int centeringOffsetY = (rect.height() - bounds.height()) / 2;
1964     setScrollPosition(IntPoint(bounds.x() - centeringOffsetX - rect.x(), bounds.y() - centeringOffsetY - rect.y()));
1965 }
1966
1967 void FrameView::setScrollPosition(const IntPoint& scrollPoint)
1968 {
1969     TemporaryChange<bool> changeInProgrammaticScroll(m_inProgrammaticScroll, true);
1970     m_maintainScrollPositionAnchor = 0;
1971     ScrollView::setScrollPosition(scrollPoint);
1972 }
1973
1974 void FrameView::delegatesScrollingDidChange()
1975 {
1976 #if USE(ACCELERATED_COMPOSITING)
1977     // When we switch to delgatesScrolling mode, we should destroy the scrolling/clipping layers in RenderLayerCompositor.
1978     if (hasCompositedContent())
1979         clearBackingStores();
1980 #endif
1981 }
1982
1983 #if USE(TILED_BACKING_STORE)
1984 void FrameView::setFixedVisibleContentRect(const IntRect& visibleContentRect)
1985 {
1986     bool visibleContentSizeDidChange = false;
1987     if (visibleContentRect.size() != this->fixedVisibleContentRect().size()) {
1988         // When the viewport size changes or the content is scaled, we need to
1989         // reposition the fixed and sticky positioned elements.
1990         setViewportConstrainedObjectsNeedLayout();
1991         visibleContentSizeDidChange = true;
1992     }
1993
1994     IntSize offset = scrollOffset();
1995     ScrollView::setFixedVisibleContentRect(visibleContentRect);
1996     if (offset != scrollOffset()) {
1997         updateLayerPositionsAfterScrolling();
1998         if (frame().page()->settings().acceleratedCompositingForFixedPositionEnabled())
1999             updateCompositingLayersAfterScrolling();
2000         scrollAnimator()->setCurrentPosition(scrollPosition());
2001         scrollPositionChanged();
2002     }
2003     if (visibleContentSizeDidChange) {
2004         // Update the scroll-bars to calculate new page-step size.
2005         updateScrollbars(scrollOffset());
2006     }
2007     frame().loader().client().didChangeScrollOffset();
2008 }
2009 #endif
2010
2011 void FrameView::setViewportConstrainedObjectsNeedLayout()
2012 {
2013     if (!hasViewportConstrainedObjects())
2014         return;
2015
2016     for (auto& renderer : *m_viewportConstrainedObjects)
2017         renderer->setNeedsLayout();
2018 }
2019
2020 void FrameView::scrollPositionChangedViaPlatformWidget()
2021 {
2022     updateLayerPositionsAfterScrolling();
2023     updateCompositingLayersAfterScrolling();
2024     repaintSlowRepaintObjects();
2025     scrollPositionChanged();
2026 }
2027
2028 void FrameView::scrollPositionChanged()
2029 {
2030     frame().eventHandler().sendScrollEvent();
2031     frame().eventHandler().dispatchFakeMouseMoveEventSoon();
2032
2033 #if USE(ACCELERATED_COMPOSITING)
2034     if (RenderView* renderView = this->renderView()) {
2035         if (renderView->usesCompositing())
2036             renderView->compositor().frameViewDidScroll();
2037     }
2038 #endif
2039 }
2040
2041 void FrameView::updateLayerPositionsAfterScrolling()
2042 {
2043     // If we're scrolling as a result of updating the view size after layout, we'll update widgets and layer positions soon anyway.
2044     if (m_layoutPhase == InViewSizeAdjust)
2045         return;
2046
2047     if (m_nestedLayoutCount <= 1 && hasViewportConstrainedObjects()) {
2048         if (RenderView* renderView = this->renderView()) {
2049             updateWidgetPositions();
2050             renderView->layer()->updateLayerPositionsAfterDocumentScroll();
2051         }
2052     }
2053 }
2054
2055 bool FrameView::shouldUpdateCompositingLayersAfterScrolling() const
2056 {
2057 #if ENABLE(ASYNC_SCROLLING)
2058     // If the scrolling thread is updating the fixed elements, then the FrameView should not update them as well.
2059
2060     Page* page = frame().page();
2061     if (!page)
2062         return true;
2063
2064     if (&page->mainFrame() != &frame())
2065         return true;
2066
2067     ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator();
2068     if (!scrollingCoordinator)
2069         return true;
2070
2071     if (!scrollingCoordinator->supportsFixedPositionLayers())
2072         return true;
2073
2074     if (scrollingCoordinator->shouldUpdateScrollLayerPositionSynchronously())
2075         return true;
2076
2077     if (inProgrammaticScroll())
2078         return true;
2079
2080     return false;
2081 #endif
2082     return true;
2083 }
2084
2085 void FrameView::updateCompositingLayersAfterScrolling()
2086 {
2087 #if USE(ACCELERATED_COMPOSITING)
2088     if (!shouldUpdateCompositingLayersAfterScrolling())
2089         return;
2090
2091     if (m_nestedLayoutCount <= 1 && hasViewportConstrainedObjects()) {
2092         if (RenderView* renderView = this->renderView())
2093             renderView->compositor().updateCompositingLayers(CompositingUpdateOnScroll);
2094     }
2095 #endif
2096 }
2097
2098 bool FrameView::isRubberBandInProgress() const
2099 {
2100     if (scrollbarsSuppressed())
2101         return false;
2102
2103     // If the scrolling thread updates the scroll position for this FrameView, then we should return
2104     // ScrollingCoordinator::isRubberBandInProgress().
2105     if (Page* page = frame().page()) {
2106         if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator()) {
2107             if (!scrollingCoordinator->shouldUpdateScrollLayerPositionSynchronously())
2108                 return scrollingCoordinator->isRubberBandInProgress();
2109         }
2110     }
2111
2112     // If the main thread updates the scroll position for this FrameView, we should return
2113     // ScrollAnimator::isRubberBandInProgress().
2114     if (ScrollAnimator* scrollAnimator = existingScrollAnimator())
2115         return scrollAnimator->isRubberBandInProgress();
2116
2117     return false;
2118 }
2119
2120 bool FrameView::requestScrollPositionUpdate(const IntPoint& position)
2121 {
2122 #if ENABLE(ASYNC_SCROLLING)
2123     if (TiledBacking* tiledBacking = this->tiledBacking()) {
2124         IntRect visibleRect = visibleContentRect();
2125         visibleRect.setLocation(position);
2126         tiledBacking->prepopulateRect(visibleRect);
2127     }
2128
2129     if (Page* page = frame().page()) {
2130         if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
2131             return scrollingCoordinator->requestScrollPositionUpdate(this, position);
2132     }
2133 #else
2134     UNUSED_PARAM(position);
2135 #endif
2136
2137     return false;
2138 }
2139
2140 HostWindow* FrameView::hostWindow() const
2141 {
2142     if (Page* page = frame().page())
2143         return &page->chrome();
2144     return 0;
2145 }
2146
2147 void FrameView::addTrackedRepaintRect(const IntRect& r)
2148 {
2149     if (!m_isTrackingRepaints || r.isEmpty())
2150         return;
2151
2152     IntRect repaintRect = r;
2153     repaintRect.move(-scrollOffset());
2154     m_trackedRepaintRects.append(repaintRect);
2155 }
2156
2157 const unsigned cRepaintRectUnionThreshold = 25;
2158
2159 void FrameView::repaintContentRectangle(const IntRect& r, bool immediate)
2160 {
2161     ASSERT(!frame().ownerElement());
2162
2163     addTrackedRepaintRect(r);
2164
2165     double delay = m_deferringRepaints ? 0 : adjustedDeferredRepaintDelay();
2166     if ((m_deferringRepaints || m_deferredRepaintTimer.isActive() || delay) && !immediate) {
2167         IntRect paintRect = r;
2168         if (clipsRepaints() && !paintsEntireContents())
2169             paintRect.intersect(visibleContentRect());
2170         if (paintRect.isEmpty())
2171             return;
2172         if (m_repaintCount == cRepaintRectUnionThreshold) {
2173             IntRect unionedRect;
2174             for (unsigned i = 0; i < cRepaintRectUnionThreshold; ++i)
2175                 unionedRect.unite(pixelSnappedIntRect(m_repaintRects[i]));
2176             m_repaintRects.clear();
2177             m_repaintRects.append(unionedRect);
2178         }
2179         if (m_repaintCount < cRepaintRectUnionThreshold)
2180             m_repaintRects.append(paintRect);
2181         else
2182             m_repaintRects[0].unite(paintRect);
2183         m_repaintCount++;
2184
2185         if (!m_deferringRepaints)
2186             startDeferredRepaintTimer(delay);
2187
2188         return;
2189     }
2190     
2191     if (!shouldUpdate(immediate))
2192         return;
2193
2194 #if USE(TILED_BACKING_STORE)
2195     if (frame().tiledBackingStore()) {
2196         frame().tiledBackingStore()->invalidate(r);
2197         return;
2198     }
2199 #endif
2200     ScrollView::repaintContentRectangle(r, immediate);
2201 }
2202
2203 static unsigned countRenderedCharactersInRenderObjectWithThreshold(const RenderObject& renderer, unsigned countSoFar, unsigned threshold)
2204 {
2205     // FIXME: Consider writing this using RenderObject::nextInPreOrder() instead of using recursion.
2206     if (renderer.isText())
2207         countSoFar += toRenderText(renderer).text()->length();
2208
2209     for (RenderObject* child = renderer.firstChildSlow(); child; child = child->nextSibling()) {
2210         if (countSoFar >= threshold)
2211             break;
2212         countSoFar = countRenderedCharactersInRenderObjectWithThreshold(*child, countSoFar, threshold);
2213     }
2214     return countSoFar;
2215 }
2216
2217 bool FrameView::renderedCharactersExceed(unsigned threshold)
2218 {
2219     if (!m_frame->contentRenderer())
2220         return false;
2221     return countRenderedCharactersInRenderObjectWithThreshold(*m_frame->contentRenderer(), 0, threshold) >= threshold;
2222 }
2223
2224 void FrameView::contentsResized()
2225 {
2226     ScrollView::contentsResized();
2227     setNeedsLayout();
2228 }
2229
2230 void FrameView::fixedLayoutSizeChanged()
2231 {
2232     // Can be triggered before the view is set, see comment in FrameView::visibleContentsResized().
2233     // An ASSERT is triggered when a view schedules a layout before being attached to a frame.
2234     if (!frame().view())
2235         return;
2236     ScrollView::fixedLayoutSizeChanged();
2237 }
2238
2239 void FrameView::visibleContentsResized()
2240 {
2241     // We check to make sure the view is attached to a frame() as this method can
2242     // be triggered before the view is attached by Frame::createView(...) setting
2243     // various values such as setScrollBarModes(...) for example.  An ASSERT is
2244     // triggered when a view is layout before being attached to a frame().
2245     if (!frame().view())
2246         return;
2247
2248 #if PLATFORM(IOS)
2249     if (RenderView* root = m_frame->contentRenderer()) {
2250         if (useCustomFixedPositionLayoutRect() && hasViewportConstrainedObjects()) {
2251             setViewportConstrainedObjectsNeedLayout();
2252             // We must eagerly enter compositing mode because fixed position elements
2253             // will not have been made compositing via a preceding style change before
2254             // m_useCustomFixedPositionLayoutRect was true.
2255             root->compositor().enableCompositingMode();
2256         }
2257     }
2258 #endif
2259
2260     if (!useFixedLayout() && needsLayout())
2261         layout();
2262
2263 #if USE(ACCELERATED_COMPOSITING)
2264     if (RenderView* renderView = this->renderView()) {
2265         if (renderView->usesCompositing())
2266             renderView->compositor().frameViewDidChangeSize();
2267     }
2268 #endif
2269 }
2270
2271 void FrameView::addedOrRemovedScrollbar()
2272 {
2273 #if USE(ACCELERATED_COMPOSITING)
2274     if (RenderView* renderView = this->renderView()) {
2275         if (renderView->usesCompositing())
2276             renderView->compositor().frameViewDidAddOrRemoveScrollbars();
2277     }
2278 #endif
2279 }
2280
2281 void FrameView::beginDeferredRepaints()
2282 {
2283     if (!frame().isMainFrame()) {
2284         frame().mainFrame().view()->beginDeferredRepaints();
2285         return;
2286     }
2287
2288     m_deferringRepaints++;
2289 }
2290
2291 void FrameView::endDeferredRepaints()
2292 {
2293     if (!frame().isMainFrame()) {
2294         frame().mainFrame().view()->endDeferredRepaints();
2295         return;
2296     }
2297
2298     ASSERT(m_deferringRepaints > 0);
2299
2300     if (--m_deferringRepaints)
2301         return;
2302
2303     if (m_deferredRepaintTimer.isActive())
2304         return;
2305
2306     if (double delay = adjustedDeferredRepaintDelay()) {
2307         startDeferredRepaintTimer(delay);
2308         return;
2309     }
2310     
2311     doDeferredRepaints();
2312 }
2313
2314 void FrameView::startDeferredRepaintTimer(double delay)
2315 {
2316     if (m_deferredRepaintTimer.isActive())
2317         return;
2318
2319     m_deferredRepaintTimer.startOneShot(delay);
2320 }
2321
2322 void FrameView::handleLoadCompleted()
2323 {
2324     // Once loading has completed, allow autoSize one last opportunity to
2325     // reduce the size of the frame.
2326     autoSizeIfEnabled();
2327     if (shouldUseLoadTimeDeferredRepaintDelay())
2328         return;
2329     m_deferredRepaintDelay = s_normalDeferredRepaintDelay;
2330     flushDeferredRepaints();
2331 }
2332
2333 void FrameView::flushDeferredRepaints()
2334 {
2335     if (!m_deferredRepaintTimer.isActive())
2336         return;
2337     m_deferredRepaintTimer.stop();
2338     doDeferredRepaints();
2339 }
2340
2341 void FrameView::doDeferredRepaints()
2342 {
2343     ASSERT(!m_deferringRepaints);
2344     if (!shouldUpdate()) {
2345         m_repaintRects.clear();
2346         m_repaintCount = 0;
2347         return;
2348     }
2349     unsigned size = m_repaintRects.size();
2350     for (unsigned i = 0; i < size; i++) {
2351 #if USE(TILED_BACKING_STORE)
2352         if (frame().tiledBackingStore()) {
2353             frame().tiledBackingStore()->invalidate(pixelSnappedIntRect(m_repaintRects[i]));
2354             continue;
2355         }
2356 #endif
2357         ScrollView::repaintContentRectangle(pixelSnappedIntRect(m_repaintRects[i]), false);
2358     }
2359     m_repaintRects.clear();
2360     m_repaintCount = 0;
2361     
2362     updateDeferredRepaintDelayAfterRepaint();
2363 }
2364
2365 bool FrameView::shouldUseLoadTimeDeferredRepaintDelay() const
2366 {
2367     // Don't defer after the initial load of the page has been completed.
2368     if (frame().tree().top().loader().isComplete())
2369         return false;
2370     Document* document = frame().document();
2371     if (!document)
2372         return false;
2373     if (document->parsing())
2374         return true;
2375     if (document->cachedResourceLoader()->requestCount())
2376         return true;
2377     return false;
2378 }
2379
2380 void FrameView::updateDeferredRepaintDelayAfterRepaint()
2381 {
2382     if (!shouldUseLoadTimeDeferredRepaintDelay()) {
2383         m_deferredRepaintDelay = s_normalDeferredRepaintDelay;
2384         return;
2385     }
2386     double incrementedRepaintDelay = m_deferredRepaintDelay + s_deferredRepaintDelayIncrementDuringLoading;
2387     m_deferredRepaintDelay = std::min(incrementedRepaintDelay, s_maxDeferredRepaintDelayDuringLoading);
2388 }
2389
2390 void FrameView::resetDeferredRepaintDelay()
2391 {
2392     m_deferredRepaintDelay = 0;
2393     if (m_deferredRepaintTimer.isActive()) {
2394         m_deferredRepaintTimer.stop();
2395         if (!m_deferringRepaints)
2396             doDeferredRepaints();
2397     }
2398 #if USE(ACCELERATED_COMPOSITING)
2399     if (RenderView* view = renderView())
2400         view->compositor().disableLayerFlushThrottlingTemporarilyForInteraction();
2401 #endif
2402 }
2403
2404 double FrameView::adjustedDeferredRepaintDelay() const
2405 {
2406     ASSERT(!m_deferringRepaints);
2407     if (!m_deferredRepaintDelay)
2408         return 0;
2409     double timeSinceLastPaint = monotonicallyIncreasingTime() - m_lastPaintTime;
2410     return std::max<double>(0, m_deferredRepaintDelay - timeSinceLastPaint);
2411 }
2412     
2413 void FrameView::deferredRepaintTimerFired(Timer<FrameView>&)
2414 {
2415     doDeferredRepaints();
2416 }
2417
2418 void FrameView::updateLayerFlushThrottlingInAllFrames()
2419 {
2420 #if USE(ACCELERATED_COMPOSITING)
2421     bool isMainLoadProgressing = frame().page()->progress().isMainLoadProgressing();
2422     for (Frame* frame = m_frame.get(); frame; frame = frame->tree().traverseNext(m_frame.get())) {
2423         if (RenderView* renderView = frame->contentRenderer())
2424             renderView->compositor().setLayerFlushThrottlingEnabled(isMainLoadProgressing);
2425     }
2426 #endif
2427 }
2428
2429 void FrameView::adjustTiledBackingCoverage()
2430 {
2431     if (!m_speculativeTilingEnabled)
2432         enableSpeculativeTilingIfNeeded();
2433
2434 #if USE(ACCELERATED_COMPOSITING)
2435     RenderView* renderView = this->renderView();
2436     if (renderView && renderView->layer()->backing())
2437         renderView->layer()->backing()->adjustTiledBackingCoverage();
2438 #endif
2439 #if PLATFORM(IOS)
2440     if (TileCache* tileCache = this->tileCache())
2441         tileCache->setSpeculativeTileCreationEnabled(m_speculativeTilingEnabled);
2442 #endif
2443 }
2444
2445 static bool shouldEnableSpeculativeTilingDuringLoading(const FrameView& view)
2446 {
2447     return view.isVisuallyNonEmpty() && !view.frame().page()->progress().isMainLoadProgressing();
2448 }
2449
2450 void FrameView::enableSpeculativeTilingIfNeeded()
2451 {
2452     ASSERT(!m_speculativeTilingEnabled);
2453     if (m_wasScrolledByUser) {
2454         m_speculativeTilingEnabled = true;
2455         return;
2456     }
2457     if (!shouldEnableSpeculativeTilingDuringLoading(*this))
2458         return;
2459     if (m_speculativeTilingEnableTimer.isActive())
2460         return;
2461     // Delay enabling a bit as load completion may trigger further loading from scripts.
2462     static const double speculativeTilingEnableDelay = 0.5;
2463     m_speculativeTilingEnableTimer.startOneShot(speculativeTilingEnableDelay);
2464 }
2465
2466 void FrameView::speculativeTilingEnableTimerFired(Timer<FrameView>&)
2467 {
2468     if (m_speculativeTilingEnabled)
2469         return;
2470     m_speculativeTilingEnabled = shouldEnableSpeculativeTilingDuringLoading(*this);
2471     adjustTiledBackingCoverage();
2472 }
2473
2474 void FrameView::layoutTimerFired(Timer<FrameView>&)
2475 {
2476 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
2477     if (!frame().document()->ownerElement())
2478         printf("Layout timer fired at %d\n", frame().document()->elapsedTime());
2479 #endif
2480     layout();
2481 }
2482
2483 void FrameView::scheduleRelayout()
2484 {
2485     // FIXME: We should assert the page is not in the page cache, but that is causing
2486     // too many false assertions.  See <rdar://problem/7218118>.
2487     ASSERT(frame().view() == this);
2488
2489     if (m_layoutRoot) {
2490         m_layoutRoot->markContainingBlocksForLayout(false);
2491         m_layoutRoot = 0;
2492     }
2493     if (!m_layoutSchedulingEnabled)
2494         return;
2495     if (!needsLayout())
2496         return;
2497     if (!frame().document()->shouldScheduleLayout())
2498         return;
2499     InspectorInstrumentation::didInvalidateLayout(&frame());
2500     // When frame flattening is enabled, the contents of the frame could affect the layout of the parent frames.
2501     // Also invalidate parent frame starting from the owner element of this frame.
2502     if (frame().ownerRenderer() && isInChildFrameWithFrameFlattening())
2503         frame().ownerRenderer()->setNeedsLayout(MarkContainingBlockChain);
2504
2505     int delay = frame().document()->minimumLayoutDelay();
2506     if (m_layoutTimer.isActive() && m_delayedLayout && !delay)
2507         unscheduleRelayout();
2508     if (m_layoutTimer.isActive())
2509         return;
2510
2511     m_delayedLayout = delay != 0;
2512
2513 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
2514     if (!frame().document()->ownerElement())
2515         printf("Scheduling layout for %d\n", delay);
2516 #endif
2517
2518     m_layoutTimer.startOneShot(delay * 0.001);
2519 }
2520
2521 static bool isObjectAncestorContainerOf(RenderObject* ancestor, RenderObject* descendant)
2522 {
2523     for (RenderObject* r = descendant; r; r = r->container()) {
2524         if (r == ancestor)
2525             return true;
2526     }
2527     return false;
2528 }
2529
2530 void FrameView::scheduleRelayoutOfSubtree(RenderElement& newRelayoutRoot)
2531 {
2532     ASSERT(renderView());
2533     RenderView& renderView = *this->renderView();
2534
2535     // Try to catch unnecessary work during render tree teardown.
2536     ASSERT(!renderView.documentBeingDestroyed());
2537     ASSERT(frame().view() == this);
2538
2539     if (renderView.needsLayout()) {
2540         newRelayoutRoot.markContainingBlocksForLayout(false);
2541         return;
2542     }
2543
2544     if (!layoutPending() && m_layoutSchedulingEnabled) {
2545         int delay = renderView.document().minimumLayoutDelay();
2546         ASSERT(!newRelayoutRoot.container() || !newRelayoutRoot.container()->needsLayout());
2547         m_layoutRoot = &newRelayoutRoot;
2548         InspectorInstrumentation::didInvalidateLayout(&frame());
2549         m_delayedLayout = delay != 0;
2550         m_layoutTimer.startOneShot(delay * 0.001);
2551         return;
2552     }
2553
2554     if (m_layoutRoot == &newRelayoutRoot)
2555         return;
2556
2557     if (!m_layoutRoot) {
2558         // Just relayout the subtree.
2559         newRelayoutRoot.markContainingBlocksForLayout(false);
2560         InspectorInstrumentation::didInvalidateLayout(&frame());
2561         return;
2562     }
2563
2564     if (isObjectAncestorContainerOf(m_layoutRoot, &newRelayoutRoot)) {
2565         // Keep the current root.
2566         newRelayoutRoot.markContainingBlocksForLayout(false, m_layoutRoot);
2567         ASSERT(!m_layoutRoot->container() || !m_layoutRoot->container()->needsLayout());
2568         return;
2569     }
2570
2571     if (isObjectAncestorContainerOf(&newRelayoutRoot, m_layoutRoot)) {
2572         // Re-root at newRelayoutRoot.
2573         m_layoutRoot->markContainingBlocksForLayout(false, &newRelayoutRoot);
2574         m_layoutRoot = &newRelayoutRoot;
2575         ASSERT(!m_layoutRoot->container() || !m_layoutRoot->container()->needsLayout());
2576         InspectorInstrumentation::didInvalidateLayout(&frame());
2577         return;
2578     }
2579
2580     // Just do a full relayout.
2581     m_layoutRoot->markContainingBlocksForLayout(false);
2582     m_layoutRoot = 0;
2583     newRelayoutRoot.markContainingBlocksForLayout(false);
2584     InspectorInstrumentation::didInvalidateLayout(&frame());
2585 }
2586
2587 bool FrameView::layoutPending() const
2588 {
2589     return m_layoutTimer.isActive();
2590 }
2591
2592 bool FrameView::needsLayout() const
2593 {
2594     // This can return true in cases where the document does not have a body yet.
2595     // Document::shouldScheduleLayout takes care of preventing us from scheduling
2596     // layout in that case.
2597     RenderView* renderView = this->renderView();
2598     return layoutPending()
2599         || (renderView && renderView->needsLayout())
2600         || m_layoutRoot
2601         || (m_deferSetNeedsLayouts && m_setNeedsLayoutWasDeferred);
2602 }
2603
2604 void FrameView::setNeedsLayout()
2605 {
2606     if (m_deferSetNeedsLayouts) {
2607         m_setNeedsLayoutWasDeferred = true;
2608         return;
2609     }
2610
2611     if (RenderView* renderView = this->renderView())
2612         renderView->setNeedsLayout();
2613 }
2614
2615 void FrameView::unscheduleRelayout()
2616 {
2617     if (!m_layoutTimer.isActive())
2618         return;
2619
2620 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
2621     if (!frame().document()->ownerElement())
2622         printf("Layout timer unscheduled at %d\n", frame().document()->elapsedTime());
2623 #endif
2624     
2625     m_layoutTimer.stop();
2626     m_delayedLayout = false;
2627 }
2628
2629 #if ENABLE(REQUEST_ANIMATION_FRAME)
2630 void FrameView::serviceScriptedAnimations(double monotonicAnimationStartTime)
2631 {
2632     for (Frame* frame = m_frame.get(); frame; frame = frame->tree().traverseNext()) {
2633         frame->view()->serviceScrollAnimations();
2634         frame->animation().serviceAnimations();
2635     }
2636
2637     Vector<RefPtr<Document>> documents;
2638     for (Frame* frame = m_frame.get(); frame; frame = frame->tree().traverseNext())
2639         documents.append(frame->document());
2640
2641     for (size_t i = 0; i < documents.size(); ++i)
2642         documents[i]->serviceScriptedAnimations(monotonicAnimationStartTime);
2643 }
2644 #endif
2645
2646 bool FrameView::isTransparent() const
2647 {
2648     return m_isTransparent;
2649 }
2650
2651 void FrameView::setTransparent(bool isTransparent)
2652 {
2653     m_isTransparent = isTransparent;
2654 }
2655
2656 bool FrameView::hasOpaqueBackground() const
2657 {
2658     return !m_isTransparent && !m_baseBackgroundColor.hasAlpha();
2659 }
2660
2661 Color FrameView::baseBackgroundColor() const
2662 {
2663     return m_baseBackgroundColor;
2664 }
2665
2666 void FrameView::setBaseBackgroundColor(const Color& backgroundColor)
2667 {
2668     if (!backgroundColor.isValid())
2669         m_baseBackgroundColor = Color::white;
2670     else
2671         m_baseBackgroundColor = backgroundColor;
2672
2673     recalculateScrollbarOverlayStyle();
2674 }
2675
2676 void FrameView::updateBackgroundRecursively(const Color& backgroundColor, bool transparent)
2677 {
2678     for (Frame* frame = m_frame.get(); frame; frame = frame->tree().traverseNext(m_frame.get())) {
2679         if (FrameView* view = frame->view()) {
2680             view->setTransparent(transparent);
2681             view->setBaseBackgroundColor(backgroundColor);
2682         }
2683     }
2684 }
2685
2686 bool FrameView::hasExtendedBackground() const
2687 {
2688 #if USE(ACCELERATED_COMPOSITING)
2689     if (!frame().settings().backgroundShouldExtendBeyondPage())
2690         return false;
2691
2692     TiledBacking* tiledBacking = this->tiledBacking();
2693     if (!tiledBacking)
2694         return false;
2695
2696     return tiledBacking->hasMargins();
2697 #else
2698     return false;
2699 #endif
2700 }
2701
2702 IntRect FrameView::extendedBackgroundRect() const
2703 {
2704 #if USE(ACCELERATED_COMPOSITING)
2705     TiledBacking* tiledBacking = this->tiledBacking();
2706     if (!tiledBacking)
2707         return IntRect();
2708
2709     return tiledBacking->bounds();
2710 #else
2711     RenderView* renderView = this->renderView();
2712     if (!renderView)
2713         return IntRect();
2714
2715     return renderView->unscaledDocumentRect();
2716 #endif
2717 }
2718
2719 #if USE(ACCELERATED_COMPOSITING)
2720 void FrameView::setBackgroundExtendsBeyondPage(bool extendBackground)
2721 {
2722     RenderView* renderView = this->renderView();
2723     if (!renderView)
2724         return;
2725
2726     RenderLayerBacking* backing = renderView->layer()->backing();
2727     if (!backing)
2728         return;
2729
2730     backing->setTiledBackingHasMargins(extendBackground);
2731 }
2732 #endif
2733
2734 bool FrameView::shouldUpdateWhileOffscreen() const
2735 {
2736     return m_shouldUpdateWhileOffscreen;
2737 }
2738
2739 void FrameView::setShouldUpdateWhileOffscreen(bool shouldUpdateWhileOffscreen)
2740 {
2741     m_shouldUpdateWhileOffscreen = shouldUpdateWhileOffscreen;
2742 }
2743
2744 bool FrameView::shouldUpdate(bool immediateRequested) const
2745 {
2746     if (!immediateRequested && isOffscreen() && !shouldUpdateWhileOffscreen())
2747         return false;
2748     return true;
2749 }
2750
2751 void FrameView::scrollToAnchor()
2752 {
2753     RefPtr<Node> anchorNode = m_maintainScrollPositionAnchor;
2754     if (!anchorNode)
2755         return;
2756
2757     if (!anchorNode->renderer())
2758         return;
2759
2760     LayoutRect rect;
2761     if (anchorNode != frame().document())
2762         rect = anchorNode->boundingBox();
2763
2764     // Scroll nested layers and frames to reveal the anchor.
2765     // Align to the top and to the closest side (this matches other browsers).
2766     anchorNode->renderer()->scrollRectToVisible(rect, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignTopAlways);
2767
2768     if (AXObjectCache* cache = frame().document()->existingAXObjectCache())
2769         cache->handleScrolledToAnchor(anchorNode.get());
2770
2771     // scrollRectToVisible can call into setScrollPosition(), which resets m_maintainScrollPositionAnchor.
2772     m_maintainScrollPositionAnchor = anchorNode;
2773 }
2774
2775 void FrameView::updateEmbeddedObject(RenderEmbeddedObject& embeddedObject)
2776 {
2777     // No need to update if it's already crashed or known to be missing.
2778     if (embeddedObject.isPluginUnavailable())
2779         return;
2780
2781     HTMLFrameOwnerElement& element = embeddedObject.frameOwnerElement();
2782
2783     if (embeddedObject.isSnapshottedPlugIn()) {
2784         if (isHTMLObjectElement(element) || isHTMLEmbedElement(element)) {
2785             HTMLPlugInImageElement& pluginElement = toHTMLPlugInImageElement(element);
2786             pluginElement.checkSnapshotStatus();
2787         }
2788         return;
2789     }
2790
2791     auto weakRenderer = embeddedObject.createWeakPtr();
2792
2793     // FIXME: This could turn into a real virtual dispatch if we defined
2794     // updateWidget(PluginCreationOption) on HTMLElement.
2795     if (isHTMLObjectElement(element) || isHTMLEmbedElement(element) || isHTMLAppletElement(element)) {
2796         HTMLPlugInImageElement& pluginElement = toHTMLPlugInImageElement(element);
2797         if (pluginElement.needsCheckForSizeChange()) {
2798             pluginElement.checkSnapshotStatus();
2799             return;
2800         }
2801         if (pluginElement.needsWidgetUpdate())
2802             pluginElement.updateWidget(CreateAnyWidgetType);
2803     }
2804
2805     // FIXME: It is not clear that Media elements need or want this updateWidget() call.
2806 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
2807     else if (element.isMediaElement())
2808         toHTMLMediaElement(element).updateWidget(CreateAnyWidgetType);
2809 #endif
2810     else
2811         ASSERT_NOT_REACHED();
2812
2813     // It's possible the renderer was destroyed below updateWidget() since loading a plugin may execute arbitrary JavaScript.
2814     if (!weakRenderer)
2815         return;
2816
2817     embeddedObject.updateWidgetPosition();
2818 }
2819
2820 bool FrameView::updateEmbeddedObjects()
2821 {
2822     if (m_nestedLayoutCount > 1 || !m_embeddedObjectsToUpdate || m_embeddedObjectsToUpdate->isEmpty())
2823         return true;
2824
2825     WidgetHierarchyUpdatesSuspensionScope suspendWidgetHierarchyUpdates;
2826
2827     // Insert a marker for where we should stop.
2828     ASSERT(!m_embeddedObjectsToUpdate->contains(nullptr));
2829     m_embeddedObjectsToUpdate->add(nullptr);
2830
2831     while (!m_embeddedObjectsToUpdate->isEmpty()) {
2832         RenderEmbeddedObject* embeddedObject = m_embeddedObjectsToUpdate->takeFirst();
2833         if (!embeddedObject)
2834             break;
2835         updateEmbeddedObject(*embeddedObject);
2836     }
2837
2838     return m_embeddedObjectsToUpdate->isEmpty();
2839 }
2840
2841 void FrameView::flushAnyPendingPostLayoutTasks()
2842 {
2843     if (!m_postLayoutTasksTimer.isActive())
2844         return;
2845
2846     performPostLayoutTasks();
2847 }
2848
2849 void FrameView::performPostLayoutTasks()
2850 {
2851     m_postLayoutTasksTimer.stop();
2852
2853     frame().selection().setCaretRectNeedsUpdate();
2854     frame().selection().updateAppearance();
2855
2856     LayoutMilestones requestedMilestones = 0;
2857     LayoutMilestones milestonesAchieved = 0;
2858     Page* page = frame().page();
2859     if (page)
2860         requestedMilestones = page->requestedLayoutMilestones();
2861
2862     if (m_nestedLayoutCount <= 1 && frame().document()->documentElement()) {
2863         if (m_firstLayoutCallbackPending) {
2864             m_firstLayoutCallbackPending = false;
2865             frame().loader().didFirstLayout();
2866             if (requestedMilestones & DidFirstLayout)
2867                 milestonesAchieved |= DidFirstLayout;
2868             if (frame().isMainFrame())
2869                 page->startCountingRelevantRepaintedObjects();
2870         }
2871         updateIsVisuallyNonEmpty();
2872
2873         // If the layout was done with pending sheets, we are not in fact visually non-empty yet.
2874         if (m_isVisuallyNonEmpty && !frame().document()->didLayoutWithPendingStylesheets() && m_firstVisuallyNonEmptyLayoutCallbackPending) {
2875             m_firstVisuallyNonEmptyLayoutCallbackPending = false;
2876             if (requestedMilestones & DidFirstVisuallyNonEmptyLayout)
2877                 milestonesAchieved |= DidFirstVisuallyNonEmptyLayout;
2878         }
2879     }
2880
2881 #if PLATFORM(IOS)
2882     // Only send layout-related delegate callbacks synchronously for the main frame to
2883     // avoid re-entering layout for the main frame while delivering a layout-related delegate
2884     // callback for a subframe.
2885     if (frame().isMainFrame())
2886         page->chrome().client().didLayout();
2887 #endif
2888
2889     if (milestonesAchieved && frame().isMainFrame())
2890         frame().loader().didLayout(milestonesAchieved);
2891
2892 #if ENABLE(FONT_LOAD_EVENTS)
2893     if (RuntimeEnabledFeatures::sharedFeatures().fontLoadEventsEnabled())
2894         frame().document()->fontloader()->didLayout();
2895 #endif
2896     
2897     // FIXME: We should consider adding DidLayout as a LayoutMilestone. That would let us merge this
2898     // with didLayout(LayoutMilestones).
2899     frame().loader().client().dispatchDidLayout();
2900
2901     updateWidgetPositions();
2902     
2903     // layout() protects FrameView, but it still can get destroyed when updateEmbeddedObjects()
2904     // is called through the post layout timer.
2905     Ref<FrameView> protect(*this);
2906
2907     for (unsigned i = 0; i < maxUpdateEmbeddedObjectsIterations; i++) {
2908         if (updateEmbeddedObjects())
2909             break;
2910     }
2911
2912     if (page) {
2913         if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
2914             scrollingCoordinator->frameViewLayoutUpdated(this);
2915     }
2916
2917 #if USE(ACCELERATED_COMPOSITING)
2918     if (RenderView* renderView = this->renderView()) {
2919         if (renderView->usesCompositing())
2920             renderView->compositor().frameViewDidLayout();
2921     }
2922 #endif
2923
2924     scrollToAnchor();
2925
2926     sendResizeEventIfNeeded();
2927 }
2928
2929 void FrameView::sendResizeEventIfNeeded()
2930 {
2931     RenderView* renderView = this->renderView();
2932     if (!renderView || renderView->printing())
2933         return;
2934     if (frame().page() && frame().page()->chrome().client().isSVGImageChromeClient())
2935         return;
2936
2937     IntSize currentSize;
2938     if (useFixedLayout() && !fixedLayoutSize().isEmpty() && delegatesScrolling())
2939         currentSize = fixedLayoutSize();
2940     else
2941 #if PLATFORM(IOS)
2942         currentSize = actualVisibleContentRect().size();
2943 #else
2944         currentSize = visibleContentRect(IncludeScrollbars).size();
2945 #endif
2946
2947     float currentZoomFactor = renderView->style().zoom();
2948     bool shouldSendResizeEvent = !m_firstLayout && (currentSize != m_lastViewportSize || currentZoomFactor != m_lastZoomFactor);
2949
2950     m_lastViewportSize = currentSize;
2951     m_lastZoomFactor = currentZoomFactor;
2952
2953     if (!shouldSendResizeEvent)
2954         return;
2955
2956 #if PLATFORM(IOS)
2957     // Don't send the resize event if the document is loading. Some pages automatically reload
2958     // when the window is resized; Safari on iOS often resizes the window while setting up its
2959     // viewport. This obviously can cause problems.
2960     if (DocumentLoader* documentLoader = frame().loader().documentLoader()) {
2961         if (documentLoader->isLoadingInAPISense())
2962             return;
2963     }
2964 #endif
2965
2966     bool isMainFrame = frame().isMainFrame();
2967     bool canSendResizeEventSynchronously = isMainFrame && !isInLayout();
2968
2969     // If we resized during layout, queue up a resize event for later, otherwise fire it right away.
2970     RefPtr<Event> resizeEvent = Event::create(eventNames().resizeEvent, false, false);
2971     if (canSendResizeEventSynchronously)
2972         frame().document()->dispatchWindowEvent(resizeEvent.release(), frame().document()->domWindow());
2973     else
2974         frame().document()->enqueueWindowEvent(resizeEvent.release());
2975
2976 #if ENABLE(INSPECTOR)
2977     Page* page = frame().page();
2978     if (InspectorInstrumentation::hasFrontends() && isMainFrame) {
2979         if (InspectorClient* inspectorClient = page ? page->inspectorController().inspectorClient() : nullptr)
2980             inspectorClient->didResizeMainFrame(&frame());
2981     }
2982 #endif
2983 }
2984
2985 void FrameView::willStartLiveResize()
2986 {
2987     ScrollView::willStartLiveResize();
2988     adjustTiledBackingCoverage();
2989 }
2990     
2991 void FrameView::willEndLiveResize()
2992 {
2993     ScrollView::willEndLiveResize();
2994     adjustTiledBackingCoverage();
2995 }
2996
2997 void FrameView::postLayoutTimerFired(Timer<FrameView>&)
2998 {
2999     performPostLayoutTasks();
3000 }
3001
3002 void FrameView::autoSizeIfEnabled()
3003 {
3004     if (!m_shouldAutoSize)
3005         return;
3006
3007     if (m_inAutoSize)
3008         return;
3009
3010     TemporaryChange<bool> changeInAutoSize(m_inAutoSize, true);
3011
3012     Document* document = frame().document();
3013     if (!document)
3014         return;
3015
3016     RenderView* documentView = document->renderView();
3017     Element* documentElement = document->documentElement();
3018     if (!documentView || !documentElement)
3019         return;
3020
3021     // Start from the minimum size and allow it to grow.
3022     resize(m_minAutoSize.width(), m_minAutoSize.height());
3023
3024     IntSize size = frameRect().size();
3025
3026     // Do the resizing twice. The first time is basically a rough calculation using the preferred width
3027     // which may result in a height change during the second iteration.
3028     for (int i = 0; i < 2; i++) {
3029         // Update various sizes including contentsSize, scrollHeight, etc.
3030         document->updateLayoutIgnorePendingStylesheets();
3031         int width = documentView->minPreferredLogicalWidth();
3032         int height = documentView->documentRect().height();
3033         IntSize newSize(width, height);
3034
3035         // Check to see if a scrollbar is needed for a given dimension and
3036         // if so, increase the other dimension to account for the scrollbar.
3037         // Since the dimensions are only for the view rectangle, once a
3038         // dimension exceeds the maximum, there is no need to increase it further.
3039         if (newSize.width() > m_maxAutoSize.width()) {
3040             RefPtr<Scrollbar> localHorizontalScrollbar = horizontalScrollbar();
3041             if (!localHorizontalScrollbar)
3042                 localHorizontalScrollbar = createScrollbar(HorizontalScrollbar);
3043             if (!localHorizontalScrollbar->isOverlayScrollbar())
3044                 newSize.setHeight(newSize.height() + localHorizontalScrollbar->height());
3045
3046             // Don't bother checking for a vertical scrollbar because the width is at
3047             // already greater the maximum.
3048         } else if (newSize.height() > m_maxAutoSize.height()) {
3049             RefPtr<Scrollbar> localVerticalScrollbar = verticalScrollbar();
3050             if (!localVerticalScrollbar)
3051                 localVerticalScrollbar = createScrollbar(VerticalScrollbar);
3052             if (!localVerticalScrollbar->isOverlayScrollbar())
3053                 newSize.setWidth(newSize.width() + localVerticalScrollbar->width());
3054
3055             // Don't bother checking for a horizontal scrollbar because the height is
3056             // already greater the maximum.
3057         }
3058
3059         // Ensure the size is at least the min bounds.
3060         newSize = newSize.expandedTo(m_minAutoSize);
3061
3062         // Bound the dimensions by the max bounds and determine what scrollbars to show.
3063         ScrollbarMode horizonalScrollbarMode = ScrollbarAlwaysOff;
3064         if (newSize.width() > m_maxAutoSize.width()) {
3065             newSize.setWidth(m_maxAutoSize.width());
3066             horizonalScrollbarMode = ScrollbarAlwaysOn;
3067         }
3068         ScrollbarMode verticalScrollbarMode = ScrollbarAlwaysOff;
3069         if (newSize.height() > m_maxAutoSize.height()) {
3070             newSize.setHeight(m_maxAutoSize.height());
3071             verticalScrollbarMode = ScrollbarAlwaysOn;
3072         }
3073
3074         if (newSize == size)
3075             continue;
3076
3077         // While loading only allow the size to increase (to avoid twitching during intermediate smaller states)
3078         // unless autoresize has just been turned on or the maximum size is smaller than the current size.
3079         if (m_didRunAutosize && size.height() <= m_maxAutoSize.height() && size.width() <= m_maxAutoSize.width()
3080             && !frame().loader().isComplete() && (newSize.height() < size.height() || newSize.width() < size.width()))
3081             break;
3082
3083         resize(newSize.width(), newSize.height());
3084         // Force the scrollbar state to avoid the scrollbar code adding them and causing them to be needed. For example,
3085         // a vertical scrollbar may cause text to wrap and thus increase the height (which is the only reason the scollbar is needed).
3086         setVerticalScrollbarLock(false);
3087         setHorizontalScrollbarLock(false);
3088         setScrollbarModes(horizonalScrollbarMode, verticalScrollbarMode, true, true);
3089     }
3090
3091     m_autoSizeContentSize = contentsSize();
3092
3093     if (m_autoSizeFixedMinimumHeight) {
3094         resize(m_autoSizeContentSize.width(), std::max(m_autoSizeFixedMinimumHeight, m_autoSizeContentSize.height()));
3095         document->updateLayoutIgnorePendingStylesheets();
3096     }
3097
3098     m_didRunAutosize = true;
3099 }
3100
3101 void FrameView::setAutoSizeFixedMinimumHeight(int fixedMinimumHeight)
3102 {
3103     if (m_autoSizeFixedMinimumHeight == fixedMinimumHeight)
3104         return;
3105
3106     m_autoSizeFixedMinimumHeight = fixedMinimumHeight;
3107
3108     setNeedsLayout();
3109 }
3110
3111 void FrameView::updateOverflowStatus(bool horizontalOverflow, bool verticalOverflow)
3112 {
3113     if (!m_viewportRenderer)
3114         return;
3115     
3116     if (m_overflowStatusDirty) {
3117         m_horizontalOverflow = horizontalOverflow;
3118         m_verticalOverflow = verticalOverflow;
3119         m_overflowStatusDirty = false;
3120         return;
3121     }
3122     
3123     bool horizontalOverflowChanged = (m_horizontalOverflow != horizontalOverflow);
3124     bool verticalOverflowChanged = (m_verticalOverflow != verticalOverflow);
3125     
3126     if (horizontalOverflowChanged || verticalOverflowChanged) {
3127         m_horizontalOverflow = horizontalOverflow;
3128         m_verticalOverflow = verticalOverflow;
3129
3130         RefPtr<OverflowEvent> overflowEvent = OverflowEvent::create(horizontalOverflowChanged, horizontalOverflow,
3131             verticalOverflowChanged, verticalOverflow);
3132         overflowEvent->setTarget(m_viewportRenderer->element());
3133
3134         frame().document()->enqueueOverflowEvent(overflowEvent.release());
3135     }
3136     
3137 }
3138
3139 const Pagination& FrameView::pagination() const
3140 {
3141     if (m_pagination != Pagination())
3142         return m_pagination;
3143
3144     if (frame().isMainFrame())
3145         return frame().page()->pagination();
3146
3147     return m_pagination;
3148 }
3149
3150 void FrameView::setPagination(const Pagination& pagination)
3151 {
3152     if (m_pagination == pagination)
3153         return;
3154
3155     m_pagination = pagination;
3156
3157     frame().document()->styleResolverChanged(DeferRecalcStyle);
3158 }
3159
3160 IntRect FrameView::windowClipRect(bool clipToContents) const
3161 {
3162     ASSERT(frame().view() == this);
3163
3164     if (paintsEntireContents())
3165         return IntRect(IntPoint(), totalContentsSize());
3166
3167     // Set our clip rect to be our contents.
3168     IntRect clipRect = contentsToWindow(visibleContentRect(clipToContents ? ExcludeScrollbars : IncludeScrollbars));
3169     if (!frame().ownerElement())
3170         return clipRect;
3171
3172     // Take our owner element and get its clip rect.
3173     HTMLFrameOwnerElement* ownerElement = frame().ownerElement();
3174     if (FrameView* parentView = ownerElement->document().view())
3175         clipRect.intersect(parentView->windowClipRectForFrameOwner(ownerElement, true));
3176     return clipRect;
3177 }
3178
3179 IntRect FrameView::windowClipRectForFrameOwner(const HTMLFrameOwnerElement* ownerElement, bool clipToLayerContents) const
3180 {
3181     // The renderer can sometimes be null when style="display:none" interacts
3182     // with external content and plugins.
3183     if (!ownerElement->renderer())
3184         return windowClipRect();
3185
3186     // If we have no layer, just return our window clip rect.
3187     const RenderLayer* enclosingLayer = ownerElement->renderer()->enclosingLayer();
3188     if (!enclosingLayer)
3189         return windowClipRect();
3190
3191     // Apply the clip from the layer.
3192     IntRect clipRect;
3193     if (clipToLayerContents)
3194         clipRect = pixelSnappedIntRect(enclosingLayer->childrenClipRect());
3195     else
3196         clipRect = pixelSnappedIntRect(enclosingLayer->selfClipRect());
3197     clipRect = contentsToWindow(clipRect); 
3198     return intersection(clipRect, windowClipRect());
3199 }
3200
3201 bool FrameView::isActive() const
3202 {
3203     Page* page = frame().page();
3204     return page && page->focusController().isActive();
3205 }
3206
3207 bool FrameView::updatesScrollLayerPositionOnMainThread() const
3208 {
3209     if (Page* page = frame().page()) {
3210         if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
3211             return scrollingCoordinator->shouldUpdateScrollLayerPositionSynchronously();
3212     }
3213
3214     return true;
3215 }
3216
3217 void FrameView::scrollTo(const IntSize& newOffset)
3218 {
3219     LayoutSize offset = scrollOffset();
3220     ScrollView::scrollTo(newOffset);
3221     if (offset != scrollOffset())
3222         scrollPositionChanged();
3223     frame().loader().client().didChangeScrollOffset();
3224 }
3225
3226 void FrameView::invalidateScrollbarRect(Scrollbar* scrollbar, const IntRect& rect)
3227 {
3228     // Add in our offset within the FrameView.
3229     IntRect dirtyRect = rect;
3230     dirtyRect.moveBy(scrollbar->location());
3231     invalidateRect(dirtyRect);
3232 }
3233
3234 IntRect FrameView::windowResizerRect() const
3235 {
3236     if (Page* page = frame().page())
3237         return page->chrome().windowResizerRect();
3238     return IntRect();
3239 }
3240
3241 float FrameView::visibleContentScaleFactor() const
3242 {
3243     if (!frame().isMainFrame() || !frame().settings().delegatesPageScaling())
3244         return 1;
3245
3246     return frame().page()->pageScaleFactor();
3247 }
3248
3249 void FrameView::setVisibleScrollerThumbRect(const IntRect& scrollerThumb)
3250 {
3251     if (!frame().isMainFrame())
3252         return;
3253
3254     frame().page()->chrome().client().notifyScrollerThumbIsVisibleInRect(scrollerThumb);
3255 }
3256
3257 ScrollableArea* FrameView::enclosingScrollableArea() const
3258 {
3259     // FIXME: Walk up the frame tree and look for a scrollable parent frame or RenderLayer.
3260     return 0;
3261 }
3262
3263 IntRect FrameView::scrollableAreaBoundingBox() const
3264 {
3265     RenderWidget* ownerRenderer = frame().ownerRenderer();
3266     if (!ownerRenderer)
3267         return frameRect();
3268
3269     return ownerRenderer->absoluteContentQuad().enclosingBoundingBox();
3270 }
3271
3272 bool FrameView::isScrollable()
3273 {
3274     // Check for:
3275     // 1) If there an actual overflow.
3276     // 2) display:none or visibility:hidden set to self or inherited.
3277     // 3) overflow{-x,-y}: hidden;
3278     // 4) scrolling: no;
3279
3280     // Covers #1
3281     IntSize totalContentsSize = this->totalContentsSize();
3282     IntSize visibleContentSize = visibleContentRect().size();
3283     if ((totalContentsSize.height() <= visibleContentSize.height() && totalContentsSize.width() <= visibleContentSize.width()))
3284         return false;
3285
3286     // Covers #2.
3287     HTMLFrameOwnerElement* owner = frame().ownerElement();
3288     if (owner && (!owner->renderer() || !owner->renderer()->visibleToHitTesting()))
3289         return false;
3290
3291     // Cover #3 and #4.
3292     ScrollbarMode horizontalMode;
3293     ScrollbarMode verticalMode;
3294     calculateScrollbarModesForLayout(horizontalMode, verticalMode, RulesFromWebContentOnly);
3295     if (horizontalMode == ScrollbarAlwaysOff && verticalMode == ScrollbarAlwaysOff)
3296         return false;
3297
3298     return true;
3299 }
3300
3301 void FrameView::updateScrollableAreaSet()
3302 {
3303     // That ensures that only inner frames are cached.
3304     FrameView* parentFrameView = this->parentFrameView();
3305     if (!parentFrameView)
3306         return;
3307
3308     if (!isScrollable()) {
3309         parentFrameView->removeScrollableArea(this);
3310         return;
3311     }
3312
3313     parentFrameView->addScrollableArea(this);
3314 }
3315
3316 bool FrameView::shouldSuspendScrollAnimations() const
3317 {
3318     return frame().loader().state() != FrameStateComplete;
3319 }
3320
3321 void FrameView::scrollbarStyleChanged(int newStyle, bool forceUpdate)
3322 {
3323     if (!frame().isMainFrame())
3324         return;
3325
3326     frame().page()->chrome().client().recommendedScrollbarStyleDidChange(newStyle);
3327
3328     if (forceUpdate)
3329         ScrollView::scrollbarStyleChanged(newStyle, forceUpdate);
3330 }
3331
3332 void FrameView::notifyPageThatContentAreaWillPaint() const
3333 {
3334     Page* page = frame().page();
3335     if (!page)
3336         return;
3337
3338     contentAreaWillPaint();
3339
3340     if (!m_scrollableAreas)
3341         return;
3342
3343     for (auto& scrollableArea : *m_scrollableAreas)
3344         scrollableArea->contentAreaWillPaint();
3345 }
3346
3347 bool FrameView::scrollAnimatorEnabled() const
3348 {
3349 #if ENABLE(SMOOTH_SCROLLING)
3350     if (Page* page = frame().page())
3351         return page->settings().scrollAnimatorEnabled();
3352 #endif
3353
3354     return false;
3355 }
3356
3357 #if ENABLE(DASHBOARD_SUPPORT) || ENABLE(DRAGGABLE_REGION)
3358 void FrameView::updateAnnotatedRegions()
3359 {
3360     Document* document = frame().document();
3361     if (!document->hasAnnotatedRegions())
3362         return;
3363     Vector<AnnotatedRegionValue> newRegions;
3364     document->renderBox()->collectAnnotatedRegions(newRegions);
3365     if (newRegions == document->annotatedRegions())
3366         return;
3367     document->setAnnotatedRegions(newRegions);
3368     Page* page = frame().page();
3369     if (!page)
3370         return;
3371     page->chrome().client().annotatedRegionsChanged();
3372 }
3373 #endif
3374
3375 void FrameView::updateScrollCorner()
3376 {
3377     RenderElement* renderer = 0;
3378     RefPtr<RenderStyle> cornerStyle;
3379     IntRect cornerRect = scrollCornerRect();
3380     
3381     if (!cornerRect.isEmpty()) {
3382         // Try the <body> element first as a scroll corner source.
3383         Document* doc = frame().document();
3384         Element* body = doc ? doc->body() : 0;
3385         if (body && body->renderer()) {
3386             renderer = body->renderer();
3387             cornerStyle = renderer->getUncachedPseudoStyle(PseudoStyleRequest(SCROLLBAR_CORNER), &renderer->style());
3388         }
3389         
3390         if (!cornerStyle) {
3391             // If the <body> didn't have a custom style, then the root element might.
3392             Element* docElement = doc ? doc->documentElement() : 0;
3393             if (docElement && docElement->renderer()) {
3394                 renderer = docElement->renderer();
3395                 cornerStyle = renderer->getUncachedPseudoStyle(PseudoStyleRequest(SCROLLBAR_CORNER), &renderer->style());
3396             }
3397         }
3398         
3399         if (!cornerStyle) {
3400             // If we have an owning iframe/frame element, then it can set the custom scrollbar also.
3401             if (RenderWidget* renderer = frame().ownerRenderer())
3402                 cornerStyle = renderer->getUncachedPseudoStyle(PseudoStyleRequest(SCROLLBAR_CORNER), &renderer->style());
3403         }
3404     }
3405
3406     if (!cornerStyle)
3407         m_scrollCorner = nullptr;
3408     else {
3409         if (!m_scrollCorner) {
3410             m_scrollCorner = createRenderer<RenderScrollbarPart>(renderer->document(), cornerStyle.releaseNonNull());
3411             m_scrollCorner->initializeStyle();
3412         } else
3413             m_scrollCorner->setStyle(cornerStyle.releaseNonNull());
3414         invalidateScrollCorner(cornerRect);
3415     }
3416
3417     ScrollView::updateScrollCorner();
3418 }
3419
3420 void FrameView::paintScrollCorner(GraphicsContext* context, const IntRect& cornerRect)
3421 {
3422     if (context->updatingControlTints()) {
3423         updateScrollCorner();
3424         return;
3425     }
3426
3427     if (m_scrollCorner) {
3428         if (frame().isMainFrame())
3429             context->fillRect(cornerRect, baseBackgroundColor(), ColorSpaceDeviceRGB);
3430         m_scrollCorner->paintIntoRect(context, cornerRect.location(), cornerRect);
3431         return;
3432     }
3433
3434     ScrollView::paintScrollCorner(context, cornerRect);
3435 }
3436
3437 void FrameView::paintScrollbar(GraphicsContext* context, Scrollbar* bar, const IntRect& rect)
3438 {
3439     if (bar->isCustomScrollbar() && frame().isMainFrame()) {
3440         IntRect toFill = bar->frameRect();
3441         toFill.intersect(rect);
3442         context->fillRect(toFill, baseBackgroundColor(), ColorSpaceDeviceRGB);
3443     }
3444
3445     ScrollView::paintScrollbar(context, bar, rect);
3446 }
3447
3448 Color FrameView::documentBackgroundColor() const
3449 {
3450     // <https://bugs.webkit.org/show_bug.cgi?id=59540> We blend the background color of
3451     // the document and the body against the base background color of the frame view.
3452     // Background images are unfortunately impractical to include.
3453
3454     // Return invalid Color objects whenever there is insufficient information.
3455     if (!frame().document())
3456         return Color();
3457
3458     Element* htmlElement = frame().document()->documentElement();
3459     Element* bodyElement = frame().document()->body();
3460
3461     // Start with invalid colors.
3462     Color htmlBackgroundColor;
3463     Color bodyBackgroundColor;
3464     if (htmlElement && htmlElement->renderer())
3465         htmlBackgroundColor = htmlElement->renderer()->style().visitedDependentColor(CSSPropertyBackgroundColor);
3466     if (bodyElement && bodyElement->renderer())
3467         bodyBackgroundColor = bodyElement->renderer()->style().visitedDependentColor(CSSPropertyBackgroundColor);
3468
3469     if (!bodyBackgroundColor.isValid()) {
3470         if (!htmlBackgroundColor.isValid())
3471             return Color();
3472         return baseBackgroundColor().blend(htmlBackgroundColor);
3473     }
3474
3475     if (!htmlBackgroundColor.isValid())
3476         return baseBackgroundColor().blend(bodyBackgroundColor);
3477
3478     // We take the aggregate of the base background color
3479     // the <html> background color, and the <body>
3480     // background color to find the document color. The
3481     // addition of the base background color is not
3482     // technically part of the document background, but it
3483     // otherwise poses problems when the aggregate is not
3484     // fully opaque.
3485     return baseBackgroundColor().blend(htmlBackgroundColor).blend(bodyBackgroundColor);
3486 }
3487
3488 bool FrameView::hasCustomScrollbars() const
3489 {
3490     for (auto& widget : children()) {
3491         if (widget->isFrameView()) {
3492             if (toFrameView(*widget).hasCustomScrollbars())
3493                 return true;
3494         } else if (widget->isScrollbar()) {
3495             if (toScrollbar(*widget).isCustomScrollbar())
3496                 return true;
3497         }
3498     }
3499
3500     return false;
3501 }
3502
3503 FrameView* FrameView::parentFrameView() const
3504 {
3505     if (!parent())
3506         return 0;
3507
3508     if (Frame* parentFrame = frame().tree().parent())
3509         return parentFrame->view();
3510
3511     return 0;
3512 }
3513
3514 bool FrameView::isInChildFrameWithFrameFlattening() const
3515 {
3516     if (!parent() || !frame().ownerElement())
3517         return false;
3518
3519     // Frame flattening applies when the owner element is either in a frameset or
3520     // an iframe with flattening parameters.
3521     if (frame().ownerElement()->hasTagName(iframeTag)) {
3522         RenderIFrame* iframeRenderer = toRenderIFrame(frame().ownerElement()->renderWidget());
3523         if (iframeRenderer->flattenFrame() || iframeRenderer->isSeamless())
3524             return true;
3525     }
3526
3527     if (!frameFlatteningEnabled())
3528         return false;
3529
3530     if (frame().ownerElement()->hasTagName(frameTag))
3531         return true;
3532
3533     return false;
3534 }
3535
3536 void FrameView::startLayoutAtMainFrameViewIfNeeded(bool allowSubtree)
3537 {
3538     // When we start a layout at the child level as opposed to the topmost frame view and this child
3539     // frame requires flattening, we need to re-initiate the layout at the topmost view. Layout
3540     // will hit this view eventually.
3541     FrameView* parentView = parentFrameView();
3542     if (!parentView)
3543         return;
3544
3545     // In the middle of parent layout, no need to restart from topmost.
3546     if (parentView->m_nestedLayoutCount)
3547         return;
3548
3549     // Parent tree is clean. Starting layout from it would have no effect.
3550     if (!parentView->needsLayout())
3551         return;
3552
3553     while (parentView->parentFrameView())
3554         parentView = parentView->parentFrameView();
3555
3556     parentView->layout(allowSubtree);
3557
3558     RenderElement* root = m_layoutRoot ? m_layoutRoot : frame().document()->renderView();
3559     ASSERT_UNUSED(root, !root->needsLayout());
3560 }
3561
3562 void FrameView::updateControlTints()
3563 {
3564     // This is called when control tints are changed from aqua/graphite to clear and vice versa.
3565     // We do a "fake" paint, and when the theme gets a paint call, it can then do an invalidate.
3566     // This is only done if the theme supports control tinting. It's up to the theme and platform
3567     // to define when controls get the tint and to call this function when that changes.
3568     
3569     // Optimize the common case where we bring a window to the front while it's still empty.
3570     if (frame().document()->url().isEmpty())
3571         return;
3572
3573     RenderView* renderView = this->renderView();
3574     if ((renderView && renderView->theme().supportsControlTints()) || hasCustomScrollbars())
3575         paintControlTints();
3576 }
3577
3578 void FrameView::paintControlTints()
3579 {
3580     if (needsLayout())
3581         layout();
3582     PlatformGraphicsContext* const noContext = 0;
3583     GraphicsContext context(noContext);
3584     context.setUpdatingControlTints(true);
3585     if (platformWidget())
3586         paintContents(&context, visibleContentRect());
3587     else
3588         paint(&context, frameRect());
3589 }
3590
3591 bool FrameView::wasScrolledByUser() const
3592 {
3593     return m_wasScrolledByUser;
3594 }
3595
3596 void FrameView::setWasScrolledByUser(bool wasScrolledByUser)
3597 {
3598     if (m_inProgrammaticScroll)
3599         return;
3600     m_maintainScrollPositionAnchor = 0;
3601     if (m_wasScrolledByUser == wasScrolledByUser)
3602         return;
3603     m_wasScrolledByUser = wasScrolledByUser;
3604     adjustTiledBackingCoverage();
3605 }
3606
3607 void FrameView::paintContents(GraphicsContext* p, const IntRect& rect)
3608 {
3609     Document* document = frame().document();
3610
3611 #ifndef NDEBUG
3612     bool fillWithRed;
3613     if (document->printing())
3614         fillWithRed = false; // Printing, don't fill with red (can't remember why).
3615     else if (frame().ownerElement())
3616         fillWithRed = false; // Subframe, don't fill with red.
3617     else if (isTransparent())
3618         fillWithRed = false; // Transparent, don't fill with red.
3619     else if (m_paintBehavior & PaintBehaviorSelectionOnly)
3620         fillWithRed = false; // Selections are transparent, don't fill with red.
3621     else if (m_nodeToDraw)
3622         fillWithRed = false; // Element images are transparent, don't fill with red.
3623     else
3624         fillWithRed = true;
3625     
3626     if (fillWithRed)
3627         p->fillRect(rect, Color(0xFF, 0, 0), ColorSpaceDeviceRGB);
3628 #endif
3629
3630     RenderView* renderView = this->renderView();