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