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