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