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