c23677e1d59f7461e8d809f927d40fe0d992e08c
[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         pluginElement->setNeedsWidgetUpdate(true);
1436     }
1437
1438     m_widgetUpdateSet->add(object);
1439 }
1440
1441 void FrameView::removeWidgetToUpdate(RenderObject* object)
1442 {
1443     if (!m_widgetUpdateSet)
1444         return;
1445
1446     m_widgetUpdateSet->remove(object);
1447 }
1448
1449 void FrameView::setMediaType(const String& mediaType)
1450 {
1451     m_mediaType = mediaType;
1452 }
1453
1454 String FrameView::mediaType() const
1455 {
1456     // See if we have an override type.
1457     String overrideType = m_frame->loader()->client()->overrideMediaType();
1458     InspectorInstrumentation::applyEmulatedMedia(m_frame.get(), &overrideType);
1459     if (!overrideType.isNull())
1460         return overrideType;
1461     return m_mediaType;
1462 }
1463
1464 void FrameView::adjustMediaTypeForPrinting(bool printing)
1465 {
1466     if (printing) {
1467         if (m_mediaTypeWhenNotPrinting.isNull())
1468             m_mediaTypeWhenNotPrinting = mediaType();
1469             setMediaType("print");
1470     } else {
1471         if (!m_mediaTypeWhenNotPrinting.isNull())
1472             setMediaType(m_mediaTypeWhenNotPrinting);
1473         m_mediaTypeWhenNotPrinting = String();
1474     }
1475 }
1476
1477 bool FrameView::useSlowRepaints(bool considerOverlap) const
1478 {
1479     bool mustBeSlow = m_slowRepaintObjectCount > 0 || (platformWidget() && hasViewportConstrainedObjects());
1480
1481     // FIXME: WidgetMac.mm makes the assumption that useSlowRepaints ==
1482     // m_contentIsOpaque, so don't take the fast path for composited layers
1483     // if they are a platform widget in order to get painting correctness
1484     // for transparent layers. See the comment in WidgetMac::paint.
1485     if (contentsInCompositedLayer() && !platformWidget())
1486         return mustBeSlow;
1487
1488     bool isOverlapped = m_isOverlapped && considerOverlap;
1489
1490     if (mustBeSlow || m_cannotBlitToWindow || isOverlapped || !m_contentIsOpaque)
1491         return true;
1492
1493     if (FrameView* parentView = parentFrameView())
1494         return parentView->useSlowRepaints(considerOverlap);
1495
1496     return false;
1497 }
1498
1499 bool FrameView::useSlowRepaintsIfNotOverlapped() const
1500 {
1501     return useSlowRepaints(false);
1502 }
1503
1504 void FrameView::updateCanBlitOnScrollRecursively()
1505 {
1506     for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext(m_frame.get())) {
1507         if (FrameView* view = frame->view())
1508             view->setCanBlitOnScroll(!view->useSlowRepaints());
1509     }
1510 }
1511
1512 bool FrameView::contentsInCompositedLayer() const
1513 {
1514 #if USE(ACCELERATED_COMPOSITING)
1515     RenderView* renderView = this->renderView();
1516     if (renderView && renderView->isComposited()) {
1517         GraphicsLayer* layer = renderView->layer()->backing()->graphicsLayer();
1518         if (layer && layer->drawsContent())
1519             return true;
1520     }
1521 #endif
1522     return false;
1523 }
1524
1525 void FrameView::setCannotBlitToWindow()
1526 {
1527     m_cannotBlitToWindow = true;
1528     updateCanBlitOnScrollRecursively();
1529 }
1530
1531 void FrameView::addSlowRepaintObject()
1532 {
1533     if (!m_slowRepaintObjectCount++) {
1534         updateCanBlitOnScrollRecursively();
1535
1536         if (Page* page = m_frame->page()) {
1537             if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
1538                 scrollingCoordinator->frameViewHasSlowRepaintObjectsDidChange(this);
1539         }
1540     }
1541 }
1542
1543 void FrameView::removeSlowRepaintObject()
1544 {
1545     ASSERT(m_slowRepaintObjectCount > 0);
1546     m_slowRepaintObjectCount--;
1547     if (!m_slowRepaintObjectCount) {
1548         updateCanBlitOnScrollRecursively();
1549
1550         if (Page* page = m_frame->page()) {
1551             if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
1552                 scrollingCoordinator->frameViewHasSlowRepaintObjectsDidChange(this);
1553         }
1554     }
1555 }
1556
1557 void FrameView::addViewportConstrainedObject(RenderObject* object)
1558 {
1559     if (!m_viewportConstrainedObjects)
1560         m_viewportConstrainedObjects = adoptPtr(new ViewportConstrainedObjectSet);
1561
1562     if (!m_viewportConstrainedObjects->contains(object)) {
1563         m_viewportConstrainedObjects->add(object);
1564         if (platformWidget())
1565             updateCanBlitOnScrollRecursively();
1566
1567         if (Page* page = m_frame->page()) {
1568             if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
1569                 scrollingCoordinator->frameViewFixedObjectsDidChange(this);
1570         }
1571     }
1572 }
1573
1574 void FrameView::removeViewportConstrainedObject(RenderObject* object)
1575 {
1576     if (m_viewportConstrainedObjects && m_viewportConstrainedObjects->contains(object)) {
1577         m_viewportConstrainedObjects->remove(object);
1578         if (Page* page = m_frame->page()) {
1579             if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
1580                 scrollingCoordinator->frameViewFixedObjectsDidChange(this);
1581         }
1582
1583         // FIXME: In addFixedObject() we only call this if there's a platform widget,
1584         // why isn't the same check being made here?
1585         updateCanBlitOnScrollRecursively();
1586     }
1587 }
1588
1589 LayoutRect FrameView::viewportConstrainedVisibleContentRect() const
1590 {
1591     LayoutRect viewportRect = visibleContentRect();
1592     viewportRect.setLocation(toPoint(scrollOffsetForFixedPosition()));
1593     return viewportRect;
1594 }
1595
1596 IntSize FrameView::scrollOffsetForFixedPosition(const IntRect& visibleContentRect, const IntSize& totalContentsSize, const IntPoint& scrollPosition, const IntPoint& scrollOrigin, float frameScaleFactor, bool fixedElementsLayoutRelativeToFrame, int headerHeight, int footerHeight)
1597 {
1598     IntPoint constrainedPosition = ScrollableArea::constrainScrollPositionForOverhang(visibleContentRect, totalContentsSize, scrollPosition, scrollOrigin, headerHeight, footerHeight);
1599
1600     IntSize maxSize = totalContentsSize - visibleContentRect.size();
1601
1602     float dragFactorX = (fixedElementsLayoutRelativeToFrame || !maxSize.width()) ? 1 : (totalContentsSize.width() - visibleContentRect.width() * frameScaleFactor) / maxSize.width();
1603     float dragFactorY = (fixedElementsLayoutRelativeToFrame || !maxSize.height()) ? 1 : (totalContentsSize.height() - visibleContentRect.height() * frameScaleFactor) / maxSize.height();
1604
1605     return IntSize(constrainedPosition.x() * dragFactorX / frameScaleFactor, constrainedPosition.y() * dragFactorY / frameScaleFactor);
1606 }
1607
1608 IntSize FrameView::scrollOffsetForFixedPosition() const
1609 {
1610     IntRect visibleContentRect = this->visibleContentRect();
1611     IntSize totalContentsSize = this->totalContentsSize();
1612     IntPoint scrollPosition = this->scrollPosition();
1613     IntPoint scrollOrigin = this->scrollOrigin();
1614     float frameScaleFactor = m_frame ? m_frame->frameScaleFactor() : 1;
1615     return scrollOffsetForFixedPosition(visibleContentRect, totalContentsSize, scrollPosition, scrollOrigin, frameScaleFactor, fixedElementsLayoutRelativeToFrame(), headerHeight(), footerHeight());
1616 }
1617
1618 bool FrameView::fixedElementsLayoutRelativeToFrame() const
1619 {
1620     ASSERT(m_frame);
1621     if (!m_frame->settings())
1622         return false;
1623
1624     return m_frame->settings()->fixedElementsLayoutRelativeToFrame();
1625 }
1626
1627 IntPoint FrameView::lastKnownMousePosition() const
1628 {
1629     return m_frame ? m_frame->eventHandler()->lastKnownMousePosition() : IntPoint();
1630 }
1631
1632 bool FrameView::shouldSetCursor() const
1633 {
1634     Page* page = frame()->page();
1635     return page && page->isOnscreen() && page->focusController()->isActive();
1636 }
1637
1638 bool FrameView::scrollContentsFastPath(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect)
1639 {
1640     if (!m_viewportConstrainedObjects || m_viewportConstrainedObjects->isEmpty()) {
1641         hostWindow()->scroll(scrollDelta, rectToScroll, clipRect);
1642         return true;
1643     }
1644
1645     const bool isCompositedContentLayer = contentsInCompositedLayer();
1646
1647     // Get the rects of the fixed objects visible in the rectToScroll
1648     Region regionToUpdate;
1649     ViewportConstrainedObjectSet::const_iterator end = m_viewportConstrainedObjects->end();
1650     for (ViewportConstrainedObjectSet::const_iterator it = m_viewportConstrainedObjects->begin(); it != end; ++it) {
1651         RenderObject* renderer = *it;
1652         if (!renderer->style()->hasViewportConstrainedPosition())
1653             continue;
1654 #if USE(ACCELERATED_COMPOSITING)
1655         if (renderer->isComposited())
1656             continue;
1657 #endif
1658     
1659         // Fixed items should always have layers.
1660         ASSERT(renderer->hasLayer());
1661         RenderLayer* layer = toRenderBoxModelObject(renderer)->layer();
1662
1663 #if USE(ACCELERATED_COMPOSITING)
1664         if (layer->viewportConstrainedNotCompositedReason() == RenderLayer::NotCompositedForBoundsOutOfView
1665             || layer->viewportConstrainedNotCompositedReason() == RenderLayer::NotCompositedForNoVisibleContent) {
1666             // Don't invalidate for invisible fixed layers.
1667             continue;
1668         }
1669 #endif
1670
1671 #if ENABLE(CSS_FILTERS)
1672         if (layer->hasAncestorWithFilterOutsets()) {
1673             // If the fixed layer has a blur/drop-shadow filter applied on at least one of its parents, we cannot 
1674             // scroll using the fast path, otherwise the outsets of the filter will be moved around the page.
1675             return false;
1676         }
1677 #endif
1678         IntRect updateRect = pixelSnappedIntRect(layer->repaintRectIncludingNonCompositingDescendants());
1679         updateRect = contentsToRootView(updateRect);
1680         if (!isCompositedContentLayer && clipsRepaints())
1681             updateRect.intersect(rectToScroll);
1682         if (!updateRect.isEmpty())
1683             regionToUpdate.unite(updateRect);
1684     }
1685
1686     // 1) scroll
1687     hostWindow()->scroll(scrollDelta, rectToScroll, clipRect);
1688
1689     // 2) update the area of fixed objects that has been invalidated
1690     Vector<IntRect> subRectsToUpdate = regionToUpdate.rects();
1691     size_t viewportConstrainedObjectsCount = subRectsToUpdate.size();
1692     for (size_t i = 0; i < viewportConstrainedObjectsCount; ++i) {
1693         IntRect updateRect = subRectsToUpdate[i];
1694         IntRect scrolledRect = updateRect;
1695         scrolledRect.move(scrollDelta);
1696         updateRect.unite(scrolledRect);
1697 #if USE(ACCELERATED_COMPOSITING)
1698         if (isCompositedContentLayer) {
1699             updateRect = rootViewToContents(updateRect);
1700             ASSERT(renderView());
1701             renderView()->layer()->setBackingNeedsRepaintInRect(updateRect);
1702             continue;
1703         }
1704 #endif
1705         if (clipsRepaints())
1706             updateRect.intersect(rectToScroll);
1707         hostWindow()->invalidateContentsAndRootView(updateRect, false);
1708     }
1709
1710     return true;
1711 }
1712
1713 void FrameView::scrollContentsSlowPath(const IntRect& updateRect)
1714 {
1715 #if USE(ACCELERATED_COMPOSITING)
1716     if (contentsInCompositedLayer()) {
1717         IntRect updateRect = visibleContentRect();
1718
1719         // Make sure to "apply" the scale factor here since we're converting from frame view
1720         // coordinates to layer backing coordinates.
1721         updateRect.scale(1 / m_frame->frameScaleFactor());
1722
1723         ASSERT(renderView());
1724         renderView()->layer()->setBackingNeedsRepaintInRect(updateRect);
1725     }
1726     if (RenderPart* frameRenderer = m_frame->ownerRenderer()) {
1727         if (isEnclosedInCompositingLayer()) {
1728             LayoutRect rect(frameRenderer->borderLeft() + frameRenderer->paddingLeft(),
1729                             frameRenderer->borderTop() + frameRenderer->paddingTop(),
1730                             visibleWidth(), visibleHeight());
1731             frameRenderer->repaintRectangle(rect);
1732             return;
1733         }
1734     }
1735 #endif
1736
1737     ScrollView::scrollContentsSlowPath(updateRect);
1738 }
1739
1740 // Note that this gets called at painting time.
1741 void FrameView::setIsOverlapped(bool isOverlapped)
1742 {
1743     if (isOverlapped == m_isOverlapped)
1744         return;
1745
1746     m_isOverlapped = isOverlapped;
1747     updateCanBlitOnScrollRecursively();
1748     
1749 #if USE(ACCELERATED_COMPOSITING)
1750     if (hasCompositedContentIncludingDescendants()) {
1751         // Overlap can affect compositing tests, so if it changes, we need to trigger
1752         // a layer update in the parent document.
1753         if (Frame* parentFrame = m_frame->tree()->parent()) {
1754             if (RenderView* parentView = parentFrame->contentRenderer()) {
1755                 RenderLayerCompositor* compositor = parentView->compositor();
1756                 compositor->setCompositingLayersNeedRebuild();
1757                 compositor->scheduleCompositingLayerUpdate();
1758             }
1759         }
1760
1761         if (RenderLayerCompositor::allowsIndependentlyCompositedFrames(this)) {
1762             // We also need to trigger reevaluation for this and all descendant frames,
1763             // since a frame uses compositing if any ancestor is compositing.
1764             for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext(m_frame.get())) {
1765                 if (RenderView* view = frame->contentRenderer()) {
1766                     RenderLayerCompositor* compositor = view->compositor();
1767                     compositor->setCompositingLayersNeedRebuild();
1768                     compositor->scheduleCompositingLayerUpdate();
1769                 }
1770             }
1771         }
1772     }
1773 #endif
1774 }
1775
1776 bool FrameView::isOverlappedIncludingAncestors() const
1777 {
1778     if (isOverlapped())
1779         return true;
1780
1781     if (FrameView* parentView = parentFrameView()) {
1782         if (parentView->isOverlapped())
1783             return true;
1784     }
1785
1786     return false;
1787 }
1788
1789 void FrameView::setContentIsOpaque(bool contentIsOpaque)
1790 {
1791     if (contentIsOpaque == m_contentIsOpaque)
1792         return;
1793
1794     m_contentIsOpaque = contentIsOpaque;
1795     updateCanBlitOnScrollRecursively();
1796 }
1797
1798 void FrameView::restoreScrollbar()
1799 {
1800     setScrollbarsSuppressed(false);
1801 }
1802
1803 bool FrameView::scrollToFragment(const KURL& url)
1804 {
1805     // If our URL has no ref, then we have no place we need to jump to.
1806     // OTOH If CSS target was set previously, we want to set it to 0, recalc
1807     // and possibly repaint because :target pseudo class may have been
1808     // set (see bug 11321).
1809     if (!url.hasFragmentIdentifier() && !m_frame->document()->cssTarget())
1810         return false;
1811
1812     String fragmentIdentifier = url.fragmentIdentifier();
1813     if (scrollToAnchor(fragmentIdentifier))
1814         return true;
1815
1816     // Try again after decoding the ref, based on the document's encoding.
1817     if (TextResourceDecoder* decoder = m_frame->document()->decoder())
1818         return scrollToAnchor(decodeURLEscapeSequences(fragmentIdentifier, decoder->encoding()));
1819
1820     return false;
1821 }
1822
1823 bool FrameView::scrollToAnchor(const String& name)
1824 {
1825     ASSERT(m_frame->document());
1826
1827     if (!m_frame->document()->haveStylesheetsLoaded()) {
1828         m_frame->document()->setGotoAnchorNeededAfterStylesheetsLoad(true);
1829         return false;
1830     }
1831
1832     m_frame->document()->setGotoAnchorNeededAfterStylesheetsLoad(false);
1833
1834     Element* anchorNode = m_frame->document()->findAnchor(name);
1835
1836     // Setting to null will clear the current target.
1837     m_frame->document()->setCSSTarget(anchorNode);
1838
1839 #if ENABLE(SVG)
1840     if (m_frame->document()->isSVGDocument()) {
1841         if (SVGSVGElement* svg = toSVGDocument(m_frame->document())->rootElement()) {
1842             svg->setupInitialView(name, anchorNode);
1843             if (!anchorNode)
1844                 return true;
1845         }
1846     }
1847 #endif
1848   
1849     // Implement the rule that "" and "top" both mean top of page as in other browsers.
1850     if (!anchorNode && !(name.isEmpty() || equalIgnoringCase(name, "top")))
1851         return false;
1852
1853     maintainScrollPositionAtAnchor(anchorNode ? static_cast<Node*>(anchorNode) : m_frame->document());
1854     
1855     // If the anchor accepts keyboard focus, move focus there to aid users relying on keyboard navigation.
1856     if (anchorNode && anchorNode->isFocusable())
1857         m_frame->document()->setFocusedNode(anchorNode);
1858     
1859     return true;
1860 }
1861
1862 void FrameView::maintainScrollPositionAtAnchor(Node* anchorNode)
1863 {
1864     m_maintainScrollPositionAnchor = anchorNode;
1865     if (!m_maintainScrollPositionAnchor)
1866         return;
1867
1868     // We need to update the layout before scrolling, otherwise we could
1869     // really mess things up if an anchor scroll comes at a bad moment.
1870     m_frame->document()->updateStyleIfNeeded();
1871     // Only do a layout if changes have occurred that make it necessary.
1872     RenderView* renderView = this->renderView();
1873     if (renderView && renderView->needsLayout())
1874         layout();
1875     else
1876         scrollToAnchor();
1877 }
1878
1879 void FrameView::scrollElementToRect(Element* element, const IntRect& rect)
1880 {
1881     m_frame->document()->updateLayoutIgnorePendingStylesheets();
1882
1883     LayoutRect bounds = element->boundingBox();
1884     int centeringOffsetX = (rect.width() - bounds.width()) / 2;
1885     int centeringOffsetY = (rect.height() - bounds.height()) / 2;
1886     setScrollPosition(IntPoint(bounds.x() - centeringOffsetX - rect.x(), bounds.y() - centeringOffsetY - rect.y()));
1887 }
1888
1889 void FrameView::setScrollPosition(const IntPoint& scrollPoint)
1890 {
1891     TemporaryChange<bool> changeInProgrammaticScroll(m_inProgrammaticScroll, true);
1892     m_maintainScrollPositionAnchor = 0;
1893
1894     IntPoint newScrollPosition = adjustScrollPositionWithinRange(scrollPoint);
1895
1896     if (newScrollPosition == scrollPosition())
1897         return;
1898
1899     if (requestScrollPositionUpdate(newScrollPosition))
1900         return;
1901
1902     ScrollView::setScrollPosition(newScrollPosition);
1903 }
1904
1905 void FrameView::delegatesScrollingDidChange()
1906 {
1907 #if USE(ACCELERATED_COMPOSITING)
1908     // When we switch to delgatesScrolling mode, we should destroy the scrolling/clipping layers in RenderLayerCompositor.
1909     if (hasCompositedContent())
1910         clearBackingStores();
1911 #endif
1912 }
1913
1914 void FrameView::setFixedVisibleContentRect(const IntRect& visibleContentRect)
1915 {
1916     bool visibleContentSizeDidChange = false;
1917     if (visibleContentRect.size() != this->fixedVisibleContentRect().size()) {
1918         // When the viewport size changes or the content is scaled, we need to
1919         // reposition the fixed and sticky positioned elements.
1920         setViewportConstrainedObjectsNeedLayout();
1921         visibleContentSizeDidChange = true;
1922     }
1923
1924     IntSize offset = scrollOffset();
1925     ScrollView::setFixedVisibleContentRect(visibleContentRect);
1926     if (offset != scrollOffset()) {
1927         repaintFixedElementsAfterScrolling();
1928         if (m_frame->page()->settings()->acceleratedCompositingForFixedPositionEnabled())
1929             updateFixedElementsAfterScrolling();
1930         scrollAnimator()->setCurrentPosition(scrollPosition());
1931         scrollPositionChanged();
1932     }
1933     if (visibleContentSizeDidChange) {
1934         // Update the scroll-bars to calculate new page-step size.
1935         updateScrollbars(scrollOffset());
1936     }
1937     frame()->loader()->client()->didChangeScrollOffset();
1938 }
1939
1940 void FrameView::setViewportConstrainedObjectsNeedLayout()
1941 {
1942     if (!hasViewportConstrainedObjects())
1943         return;
1944
1945     ViewportConstrainedObjectSet::const_iterator end = m_viewportConstrainedObjects->end();
1946     for (ViewportConstrainedObjectSet::const_iterator it = m_viewportConstrainedObjects->begin(); it != end; ++it) {
1947         RenderObject* renderer = *it;
1948         renderer->setNeedsLayout(true);
1949     }
1950 }
1951
1952
1953 void FrameView::scrollPositionChangedViaPlatformWidget()
1954 {
1955     repaintFixedElementsAfterScrolling();
1956     updateFixedElementsAfterScrolling();
1957     scrollPositionChanged();
1958 }
1959
1960 void FrameView::scrollPositionChanged()
1961 {
1962     frame()->eventHandler()->sendScrollEvent();
1963     frame()->eventHandler()->dispatchFakeMouseMoveEventSoon();
1964
1965 #if USE(ACCELERATED_COMPOSITING)
1966     if (RenderView* renderView = this->renderView()) {
1967         if (renderView->usesCompositing())
1968             renderView->compositor()->frameViewDidScroll();
1969     }
1970 #endif
1971 }
1972
1973 void FrameView::repaintFixedElementsAfterScrolling()
1974 {
1975     // For fixed position elements, update widget positions and compositing layers after scrolling,
1976     // but only if we're not inside of layout.
1977     if (!m_nestedLayoutCount && hasViewportConstrainedObjects()) {
1978         if (RenderView* renderView = this->renderView()) {
1979             renderView->updateWidgetPositions();
1980             renderView->layer()->updateLayerPositionsAfterDocumentScroll();
1981         }
1982     }
1983 }
1984
1985 bool FrameView::shouldUpdateFixedElementsAfterScrolling()
1986 {
1987 #if ENABLE(THREADED_SCROLLING)
1988     Page* page = m_frame->page();
1989     if (!page)
1990         return true;
1991
1992     // If the scrolling thread is updating the fixed elements, then the FrameView should not update them as well.
1993     if (page->mainFrame() != m_frame)
1994         return true;
1995
1996     ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator();
1997     if (!scrollingCoordinator)
1998         return true;
1999
2000     if (!scrollingCoordinator->supportsFixedPositionLayers())
2001         return true;
2002
2003     if (scrollingCoordinator->shouldUpdateScrollLayerPositionOnMainThread())
2004         return true;
2005
2006     if (inProgrammaticScroll())
2007         return true;
2008
2009     return false;
2010 #endif
2011     return true;
2012 }
2013
2014 void FrameView::updateFixedElementsAfterScrolling()
2015 {
2016 #if USE(ACCELERATED_COMPOSITING)
2017     if (!shouldUpdateFixedElementsAfterScrolling())
2018         return;
2019
2020     if (m_nestedLayoutCount <= 1 && hasViewportConstrainedObjects()) {
2021         if (RenderView* renderView = this->renderView())
2022             renderView->compositor()->updateCompositingLayers(CompositingUpdateOnScroll);
2023     }
2024 #endif
2025 }
2026
2027 bool FrameView::shouldRubberBandInDirection(ScrollDirection direction) const
2028 {
2029     Page* page = frame() ? frame()->page() : 0;
2030     if (!page)
2031         return ScrollView::shouldRubberBandInDirection(direction);
2032     return page->chrome()->client()->shouldRubberBandInDirection(direction);
2033 }
2034
2035 bool FrameView::isRubberBandInProgress() const
2036 {
2037     if (scrollbarsSuppressed())
2038         return false;
2039
2040     // If the scrolling thread updates the scroll position for this FrameView, then we should return
2041     // ScrollingCoordinator::isRubberBandInProgress().
2042     if (Page* page = m_frame->page()) {
2043         if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator()) {
2044             if (!scrollingCoordinator->shouldUpdateScrollLayerPositionOnMainThread())
2045                 return scrollingCoordinator->isRubberBandInProgress();
2046         }
2047     }
2048
2049     // If the main thread updates the scroll position for this FrameView, we should return
2050     // ScrollAnimator::isRubberBandInProgress().
2051     if (ScrollAnimator* scrollAnimator = existingScrollAnimator())
2052         return scrollAnimator->isRubberBandInProgress();
2053
2054     return false;
2055 }
2056
2057 bool FrameView::requestScrollPositionUpdate(const IntPoint& position)
2058 {
2059 #if ENABLE(THREADED_SCROLLING)
2060     if (TiledBacking* tiledBacking = this->tiledBacking()) {
2061         IntRect visibleRect = visibleContentRect();
2062         visibleRect.setLocation(position);
2063         tiledBacking->prepopulateRect(visibleRect);
2064     }
2065
2066     if (Page* page = m_frame->page()) {
2067         if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
2068             return scrollingCoordinator->requestScrollPositionUpdate(this, position);
2069     }
2070 #else
2071     UNUSED_PARAM(position);
2072 #endif
2073
2074     return false;
2075 }
2076
2077 HostWindow* FrameView::hostWindow() const
2078 {
2079     Page* page = frame() ? frame()->page() : 0;
2080     if (!page)
2081         return 0;
2082     return page->chrome();
2083 }
2084
2085 const unsigned cRepaintRectUnionThreshold = 25;
2086
2087 void FrameView::repaintContentRectangle(const IntRect& r, bool immediate)
2088 {
2089     ASSERT(!m_frame->ownerElement());
2090
2091     if (m_isTrackingRepaints) {
2092         IntRect repaintRect = r;
2093         repaintRect.move(-scrollOffset());
2094         m_trackedRepaintRects.append(repaintRect);
2095     }
2096
2097     double delay = m_deferringRepaints ? 0 : adjustedDeferredRepaintDelay();
2098     if ((m_deferringRepaints || m_deferredRepaintTimer.isActive() || delay) && !immediate) {
2099         IntRect paintRect = r;
2100         if (clipsRepaints() && !paintsEntireContents())
2101             paintRect.intersect(visibleContentRect());
2102         if (paintRect.isEmpty())
2103             return;
2104         if (m_repaintCount == cRepaintRectUnionThreshold) {
2105             IntRect unionedRect;
2106             for (unsigned i = 0; i < cRepaintRectUnionThreshold; ++i)
2107                 unionedRect.unite(pixelSnappedIntRect(m_repaintRects[i]));
2108             m_repaintRects.clear();
2109             m_repaintRects.append(unionedRect);
2110         }
2111         if (m_repaintCount < cRepaintRectUnionThreshold)
2112             m_repaintRects.append(paintRect);
2113         else
2114             m_repaintRects[0].unite(paintRect);
2115         m_repaintCount++;
2116
2117         if (!m_deferringRepaints)
2118             startDeferredRepaintTimer(delay);
2119
2120         return;
2121     }
2122     
2123     if (!shouldUpdate(immediate))
2124         return;
2125
2126 #if USE(TILED_BACKING_STORE)
2127     if (frame()->tiledBackingStore()) {
2128         frame()->tiledBackingStore()->invalidate(r);
2129         return;
2130     }
2131 #endif
2132     ScrollView::repaintContentRectangle(r, immediate);
2133 }
2134
2135 void FrameView::contentsResized()
2136 {
2137     ScrollView::contentsResized();
2138     setNeedsLayout();
2139 }
2140
2141 void FrameView::visibleContentsResized()
2142 {
2143     // We check to make sure the view is attached to a frame() as this method can
2144     // be triggered before the view is attached by Frame::createView(...) setting
2145     // various values such as setScrollBarModes(...) for example.  An ASSERT is
2146     // triggered when a view is layout before being attached to a frame().
2147     if (!frame()->view())
2148         return;
2149
2150     if (!useFixedLayout() && needsLayout())
2151         layout();
2152
2153 #if USE(ACCELERATED_COMPOSITING)
2154     if (RenderView* renderView = this->renderView()) {
2155         if (renderView->usesCompositing())
2156             renderView->compositor()->frameViewDidChangeSize();
2157     }
2158 #endif
2159 }
2160
2161 void FrameView::beginDeferredRepaints()
2162 {
2163     Page* page = m_frame->page();
2164     if (page->mainFrame() != m_frame) {
2165         page->mainFrame()->view()->beginDeferredRepaints();
2166         return;
2167     }
2168
2169     m_deferringRepaints++;
2170 }
2171
2172 void FrameView::endDeferredRepaints()
2173 {
2174     Page* page = m_frame->page();
2175     if (page->mainFrame() != m_frame) {
2176         page->mainFrame()->view()->endDeferredRepaints();
2177         return;
2178     }
2179
2180     ASSERT(m_deferringRepaints > 0);
2181
2182     if (--m_deferringRepaints)
2183         return;
2184
2185     if (m_deferredRepaintTimer.isActive())
2186         return;
2187
2188     if (double delay = adjustedDeferredRepaintDelay()) {
2189         startDeferredRepaintTimer(delay);
2190         return;
2191     }
2192     
2193     doDeferredRepaints();
2194 }
2195
2196 void FrameView::startDeferredRepaintTimer(double delay)
2197 {
2198     if (m_deferredRepaintTimer.isActive())
2199         return;
2200
2201     if (m_disableRepaints)
2202         return;
2203
2204     m_deferredRepaintTimer.startOneShot(delay);
2205 }
2206
2207 void FrameView::handleLoadCompleted()
2208 {
2209     // Once loading has completed, allow autoSize one last opportunity to
2210     // reduce the size of the frame.
2211     autoSizeIfEnabled();
2212     if (shouldUseLoadTimeDeferredRepaintDelay())
2213         return;
2214     m_deferredRepaintDelay = s_normalDeferredRepaintDelay;
2215     flushDeferredRepaints();
2216 }
2217
2218 void FrameView::flushDeferredRepaints()
2219 {
2220     if (!m_deferredRepaintTimer.isActive())
2221         return;
2222     m_deferredRepaintTimer.stop();
2223     doDeferredRepaints();
2224 }
2225
2226 void FrameView::doDeferredRepaints()
2227 {
2228     if (m_disableRepaints)
2229         return;
2230
2231     ASSERT(!m_deferringRepaints);
2232     if (!shouldUpdate()) {
2233         m_repaintRects.clear();
2234         m_repaintCount = 0;
2235         return;
2236     }
2237     unsigned size = m_repaintRects.size();
2238     for (unsigned i = 0; i < size; i++) {
2239 #if USE(TILED_BACKING_STORE)
2240         if (frame()->tiledBackingStore()) {
2241             frame()->tiledBackingStore()->invalidate(pixelSnappedIntRect(m_repaintRects[i]));
2242             continue;
2243         }
2244 #endif
2245         ScrollView::repaintContentRectangle(pixelSnappedIntRect(m_repaintRects[i]), false);
2246     }
2247     m_repaintRects.clear();
2248     m_repaintCount = 0;
2249     
2250     updateDeferredRepaintDelayAfterRepaint();
2251 }
2252
2253 bool FrameView::shouldUseLoadTimeDeferredRepaintDelay() const
2254 {
2255     // Don't defer after the initial load of the page has been completed.
2256     if (m_frame->tree()->top()->loader()->isComplete())
2257         return false;
2258     Document* document = m_frame->document();
2259     if (!document)
2260         return false;
2261     if (document->parsing())
2262         return true;
2263     if (document->cachedResourceLoader()->requestCount())
2264         return true;
2265     return false;
2266 }
2267
2268 void FrameView::updateDeferredRepaintDelayAfterRepaint()
2269 {
2270     if (!shouldUseLoadTimeDeferredRepaintDelay()) {
2271         m_deferredRepaintDelay = s_normalDeferredRepaintDelay;
2272         return;
2273     }
2274     double incrementedRepaintDelay = m_deferredRepaintDelay + s_deferredRepaintDelayIncrementDuringLoading;
2275     m_deferredRepaintDelay = std::min(incrementedRepaintDelay, s_maxDeferredRepaintDelayDuringLoading);
2276 }
2277
2278 void FrameView::resetDeferredRepaintDelay()
2279 {
2280     m_deferredRepaintDelay = 0;
2281     if (m_deferredRepaintTimer.isActive()) {
2282         m_deferredRepaintTimer.stop();
2283         if (!m_deferringRepaints)
2284             doDeferredRepaints();
2285     }
2286 #if USE(ACCELERATED_COMPOSITING)
2287     if (RenderView* view = renderView())
2288         view->compositor()->disableLayerFlushThrottlingTemporarilyForInteraction();
2289 #endif
2290 }
2291
2292 double FrameView::adjustedDeferredRepaintDelay() const
2293 {
2294     ASSERT(!m_deferringRepaints);
2295     if (!m_deferredRepaintDelay)
2296         return 0;
2297     double timeSinceLastPaint = currentTime() - m_lastPaintTime;
2298     return max(0., m_deferredRepaintDelay - timeSinceLastPaint);
2299 }
2300     
2301 void FrameView::deferredRepaintTimerFired(Timer<FrameView>*)
2302 {
2303     doDeferredRepaints();
2304 }
2305
2306 void FrameView::beginDisableRepaints()
2307 {
2308     m_disableRepaints++;
2309 }
2310
2311 void FrameView::endDisableRepaints()
2312 {
2313     ASSERT(m_disableRepaints > 0);
2314     m_disableRepaints--;
2315 }
2316
2317 void FrameView::updateLayerFlushThrottlingInAllFrames(bool isLoadProgressing)
2318 {
2319 #if USE(ACCELERATED_COMPOSITING)
2320     for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext(m_frame.get())) {
2321         if (RenderView* renderView = frame->contentRenderer())
2322             renderView->compositor()->setLayerFlushThrottlingEnabled(isLoadProgressing);
2323     }
2324 #else
2325     UNUSED_PARAM(isLoadProgressing);
2326 #endif
2327 }
2328
2329 void FrameView::adjustTiledBackingCoverage()
2330 {
2331 #if USE(ACCELERATED_COMPOSITING)
2332     RenderView* renderView = this->renderView();
2333     if (renderView && renderView->layer()->backing())
2334         renderView->layer()->backing()->adjustTiledBackingCoverage();
2335 #endif
2336 }
2337
2338 void FrameView::layoutTimerFired(Timer<FrameView>*)
2339 {
2340 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
2341     if (!m_frame->document()->ownerElement())
2342         printf("Layout timer fired at %d\n", m_frame->document()->elapsedTime());
2343 #endif
2344     layout();
2345 }
2346
2347 void FrameView::scheduleRelayout()
2348 {
2349     // FIXME: We should assert the page is not in the page cache, but that is causing
2350     // too many false assertions.  See <rdar://problem/7218118>.
2351     ASSERT(m_frame->view() == this);
2352
2353     if (m_layoutRoot) {
2354         m_layoutRoot->markContainingBlocksForLayout(false);
2355         m_layoutRoot = 0;
2356     }
2357     if (!m_layoutSchedulingEnabled)
2358         return;
2359     if (!needsLayout())
2360         return;
2361     if (!m_frame->document()->shouldScheduleLayout())
2362         return;
2363     InspectorInstrumentation::didInvalidateLayout(m_frame.get());
2364     // When frame flattening is enabled, the contents of the frame could affect the layout of the parent frames.
2365     // Also invalidate parent frame starting from the owner element of this frame.
2366     if (m_frame->ownerRenderer() && isInChildFrameWithFrameFlattening())
2367         m_frame->ownerRenderer()->setNeedsLayout(true, MarkContainingBlockChain);
2368
2369     int delay = m_frame->document()->minimumLayoutDelay();
2370     if (m_layoutTimer.isActive() && m_delayedLayout && !delay)
2371         unscheduleRelayout();
2372     if (m_layoutTimer.isActive())
2373         return;
2374
2375     m_delayedLayout = delay != 0;
2376
2377 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
2378     if (!m_frame->document()->ownerElement())
2379         printf("Scheduling layout for %d\n", delay);
2380 #endif
2381
2382     m_layoutTimer.startOneShot(delay * 0.001);
2383 }
2384
2385 static bool isObjectAncestorContainerOf(RenderObject* ancestor, RenderObject* descendant)
2386 {
2387     for (RenderObject* r = descendant; r; r = r->container()) {
2388         if (r == ancestor)
2389             return true;
2390     }
2391     return false;
2392 }
2393
2394 void FrameView::scheduleRelayoutOfSubtree(RenderObject* relayoutRoot)
2395 {
2396     ASSERT(m_frame->view() == this);
2397
2398     RenderView* renderView = this->renderView();
2399     if (renderView && renderView->needsLayout()) {
2400         if (relayoutRoot)
2401             relayoutRoot->markContainingBlocksForLayout(false);
2402         return;
2403     }
2404
2405     if (layoutPending() || !m_layoutSchedulingEnabled) {
2406         if (m_layoutRoot != relayoutRoot) {
2407             if (isObjectAncestorContainerOf(m_layoutRoot, relayoutRoot)) {
2408                 // Keep the current root
2409                 relayoutRoot->markContainingBlocksForLayout(false, m_layoutRoot);
2410                 ASSERT(!m_layoutRoot->container() || !m_layoutRoot->container()->needsLayout());
2411             } else if (m_layoutRoot && isObjectAncestorContainerOf(relayoutRoot, m_layoutRoot)) {
2412                 // Re-root at relayoutRoot
2413                 m_layoutRoot->markContainingBlocksForLayout(false, relayoutRoot);
2414                 m_layoutRoot = relayoutRoot;
2415                 ASSERT(!m_layoutRoot->container() || !m_layoutRoot->container()->needsLayout());
2416                 InspectorInstrumentation::didInvalidateLayout(m_frame.get());
2417             } else {
2418                 // Just do a full relayout
2419                 if (m_layoutRoot)
2420                     m_layoutRoot->markContainingBlocksForLayout(false);
2421                 m_layoutRoot = 0;
2422                 relayoutRoot->markContainingBlocksForLayout(false);
2423                 InspectorInstrumentation::didInvalidateLayout(m_frame.get());
2424             }
2425         }
2426     } else if (m_layoutSchedulingEnabled) {
2427         int delay = m_frame->document()->minimumLayoutDelay();
2428         m_layoutRoot = relayoutRoot;
2429         ASSERT(!m_layoutRoot->container() || !m_layoutRoot->container()->needsLayout());
2430         InspectorInstrumentation::didInvalidateLayout(m_frame.get());
2431         m_delayedLayout = delay != 0;
2432         m_layoutTimer.startOneShot(delay * 0.001);
2433     }
2434 }
2435
2436 bool FrameView::layoutPending() const
2437 {
2438     return m_layoutTimer.isActive();
2439 }
2440
2441 bool FrameView::needsLayout() const
2442 {
2443     // This can return true in cases where the document does not have a body yet.
2444     // Document::shouldScheduleLayout takes care of preventing us from scheduling
2445     // layout in that case.
2446     if (!m_frame)
2447         return false;
2448
2449     RenderView* renderView = this->renderView();
2450     return layoutPending()
2451         || (renderView && renderView->needsLayout())
2452         || m_layoutRoot
2453         || (m_deferSetNeedsLayouts && m_setNeedsLayoutWasDeferred);
2454 }
2455
2456 void FrameView::setNeedsLayout()
2457 {
2458     if (m_deferSetNeedsLayouts) {
2459         m_setNeedsLayoutWasDeferred = true;
2460         return;
2461     }
2462
2463     if (RenderView* renderView = this->renderView())
2464         renderView->setNeedsLayout(true);
2465 }
2466
2467 void FrameView::unscheduleRelayout()
2468 {
2469     if (!m_layoutTimer.isActive())
2470         return;
2471
2472 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
2473     if (!m_frame->document()->ownerElement())
2474         printf("Layout timer unscheduled at %d\n", m_frame->document()->elapsedTime());
2475 #endif
2476     
2477     m_layoutTimer.stop();
2478     m_delayedLayout = false;
2479 }
2480
2481 #if ENABLE(REQUEST_ANIMATION_FRAME)
2482 void FrameView::serviceScriptedAnimations(double monotonicAnimationStartTime)
2483 {
2484     for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext()) {
2485         frame->view()->serviceScrollAnimations();
2486         frame->animation()->serviceAnimations();
2487     }
2488
2489     Vector<RefPtr<Document> > documents;
2490     for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext())
2491         documents.append(frame->document());
2492
2493     for (size_t i = 0; i < documents.size(); ++i)
2494         documents[i]->serviceScriptedAnimations(monotonicAnimationStartTime);
2495 }
2496 #endif
2497
2498 bool FrameView::isTransparent() const
2499 {
2500     return m_isTransparent;
2501 }
2502
2503 void FrameView::setTransparent(bool isTransparent)
2504 {
2505     m_isTransparent = isTransparent;
2506 }
2507
2508 bool FrameView::hasOpaqueBackground() const
2509 {
2510     return !m_isTransparent && !m_baseBackgroundColor.hasAlpha();
2511 }
2512
2513 Color FrameView::baseBackgroundColor() const
2514 {
2515     return m_baseBackgroundColor;
2516 }
2517
2518 void FrameView::setBaseBackgroundColor(const Color& backgroundColor)
2519 {
2520     if (!backgroundColor.isValid())
2521         m_baseBackgroundColor = Color::white;
2522     else
2523         m_baseBackgroundColor = backgroundColor;
2524
2525     recalculateScrollbarOverlayStyle();
2526 }
2527
2528 void FrameView::updateBackgroundRecursively(const Color& backgroundColor, bool transparent)
2529 {
2530     for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext(m_frame.get())) {
2531         if (FrameView* view = frame->view()) {
2532             view->setTransparent(transparent);
2533             view->setBaseBackgroundColor(backgroundColor);
2534         }
2535     }
2536 }
2537
2538 bool FrameView::shouldUpdateWhileOffscreen() const
2539 {
2540     return m_shouldUpdateWhileOffscreen;
2541 }
2542
2543 void FrameView::setShouldUpdateWhileOffscreen(bool shouldUpdateWhileOffscreen)
2544 {
2545     m_shouldUpdateWhileOffscreen = shouldUpdateWhileOffscreen;
2546 }
2547
2548 bool FrameView::shouldUpdate(bool immediateRequested) const
2549 {
2550     if (!immediateRequested && isOffscreen() && !shouldUpdateWhileOffscreen())
2551         return false;
2552     return true;
2553 }
2554
2555 void FrameView::scheduleEvent(PassRefPtr<Event> event, PassRefPtr<Node> eventTarget)
2556 {
2557     m_actionScheduler->scheduleEvent(event, eventTarget);
2558 }
2559
2560 void FrameView::pauseScheduledEvents()
2561 {
2562     m_actionScheduler->pause();
2563 }
2564
2565 void FrameView::resumeScheduledEvents()
2566 {
2567     m_actionScheduler->resume();
2568 }
2569
2570 void FrameView::scrollToAnchor()
2571 {
2572     RefPtr<Node> anchorNode = m_maintainScrollPositionAnchor;
2573     if (!anchorNode)
2574         return;
2575
2576     if (!anchorNode->renderer())
2577         return;
2578
2579     LayoutRect rect;
2580     if (anchorNode != m_frame->document())
2581         rect = anchorNode->boundingBox();
2582
2583     // Scroll nested layers and frames to reveal the anchor.
2584     // Align to the top and to the closest side (this matches other browsers).
2585     anchorNode->renderer()->scrollRectToVisible(rect, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignTopAlways);
2586
2587     if (AXObjectCache* cache = m_frame->document()->existingAXObjectCache())
2588         cache->handleScrolledToAnchor(anchorNode.get());
2589
2590     // scrollRectToVisible can call into setScrollPosition(), which resets m_maintainScrollPositionAnchor.
2591     m_maintainScrollPositionAnchor = anchorNode;
2592 }
2593
2594 void FrameView::updateWidget(RenderObject* object)
2595 {
2596     ASSERT(!object->node() || object->node()->isElementNode());
2597     Element* ownerElement = toElement(object->node());
2598     // The object may have already been destroyed (thus node cleared),
2599     // but FrameView holds a manual ref, so it won't have been deleted.
2600     ASSERT(m_widgetUpdateSet->contains(object));
2601     if (!ownerElement)
2602         return;
2603
2604     if (object->isEmbeddedObject()) {
2605         RenderEmbeddedObject* embeddedObject = static_cast<RenderEmbeddedObject*>(object);
2606         // No need to update if it's already crashed or known to be missing.
2607         if (embeddedObject->showsUnavailablePluginIndicator())
2608             return;
2609
2610         if (object->isSnapshottedPlugIn()) {
2611             if (ownerElement->hasTagName(objectTag) || ownerElement->hasTagName(embedTag)) {
2612                 HTMLPlugInImageElement* pluginElement = toHTMLPlugInImageElement(ownerElement);
2613                 pluginElement->updateSnapshotInfo();
2614             }
2615             return;
2616         }
2617
2618         // FIXME: This could turn into a real virtual dispatch if we defined
2619         // updateWidget(PluginCreationOption) on HTMLElement.
2620         if (ownerElement->hasTagName(objectTag) || ownerElement->hasTagName(embedTag) || ownerElement->hasTagName(appletTag)) {
2621             HTMLPlugInImageElement* pluginElement = toHTMLPlugInImageElement(ownerElement);
2622             if (pluginElement->needsWidgetUpdate())
2623                 pluginElement->updateWidget(CreateAnyWidgetType);
2624         }
2625         // FIXME: It is not clear that Media elements need or want this updateWidget() call.
2626 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
2627         else if (ownerElement->isMediaElement())
2628             static_cast<HTMLMediaElement*>(ownerElement)->updateWidget(CreateAnyWidgetType);
2629 #endif
2630         else
2631             ASSERT_NOT_REACHED();
2632
2633         // Caution: it's possible the object was destroyed again, since loading a
2634         // plugin may run any arbitrary JavaScript.
2635         embeddedObject->updateWidgetPosition();
2636     }
2637 }
2638
2639 bool FrameView::updateWidgets()
2640 {
2641     if (m_nestedLayoutCount > 1 || !m_widgetUpdateSet || m_widgetUpdateSet->isEmpty())
2642         return true;
2643     
2644     size_t size = m_widgetUpdateSet->size();
2645
2646     Vector<RenderObject*> objects;
2647     objects.reserveInitialCapacity(size);
2648
2649     RenderObjectSet::const_iterator end = m_widgetUpdateSet->end();
2650     for (RenderObjectSet::const_iterator it = m_widgetUpdateSet->begin(); it != end; ++it) {
2651         RenderObject* object = *it;
2652         objects.uncheckedAppend(object);
2653         if (object->isEmbeddedObject()) {
2654             RenderEmbeddedObject* embeddedObject = static_cast<RenderEmbeddedObject*>(object);
2655             embeddedObject->ref();
2656         }
2657     }
2658
2659     for (size_t i = 0; i < size; ++i) {
2660         RenderObject* object = objects[i];
2661         updateWidget(object);
2662         m_widgetUpdateSet->remove(object);
2663     }
2664
2665     RenderArena* arena = m_frame->document()->renderArena();
2666     for (size_t i = 0; i < size; ++i) {
2667         RenderObject* object = objects[i];
2668         if (object->isEmbeddedObject()) {
2669             RenderEmbeddedObject* embeddedObject = static_cast<RenderEmbeddedObject*>(object);
2670             embeddedObject->deref(arena);
2671         }
2672     }
2673     
2674     return m_widgetUpdateSet->isEmpty();
2675 }
2676
2677 void FrameView::flushAnyPendingPostLayoutTasks()
2678 {
2679     if (!m_postLayoutTasksTimer.isActive())
2680         return;
2681
2682     performPostLayoutTasks();
2683 }
2684
2685 void FrameView::performPostLayoutTasks()
2686 {
2687     m_postLayoutTasksTimer.stop();
2688
2689     m_frame->selection()->setCaretRectNeedsUpdate();
2690     m_frame->selection()->updateAppearance();
2691
2692     LayoutMilestones milestonesOfInterest = 0;
2693     LayoutMilestones milestonesAchieved = 0;
2694     Page* page = m_frame->page();
2695     if (page)
2696         milestonesOfInterest = page->layoutMilestones();
2697
2698     if (m_nestedLayoutCount <= 1) {
2699         if (m_firstLayoutCallbackPending) {
2700             m_firstLayoutCallbackPending = false;
2701             m_frame->loader()->didFirstLayout();
2702             if (milestonesOfInterest & DidFirstLayout)
2703                 milestonesAchieved |= DidFirstLayout;
2704             if (page) {
2705                 if (page->mainFrame() == m_frame)
2706                     page->startCountingRelevantRepaintedObjects();
2707             }
2708         }
2709
2710         // Ensure that we always send this eventually.
2711         if (!m_frame->document()->parsing() && m_frame->loader()->stateMachine()->committedFirstRealDocumentLoad())
2712             m_isVisuallyNonEmpty = true;
2713
2714         // If the layout was done with pending sheets, we are not in fact visually non-empty yet.
2715         if (m_isVisuallyNonEmpty && !m_frame->document()->didLayoutWithPendingStylesheets() && m_firstVisuallyNonEmptyLayoutCallbackPending) {
2716             m_firstVisuallyNonEmptyLayoutCallbackPending = false;
2717             if (milestonesOfInterest & DidFirstVisuallyNonEmptyLayout)
2718                 milestonesAchieved |= DidFirstVisuallyNonEmptyLayout;
2719         }
2720     }
2721
2722     m_frame->loader()->didLayout(milestonesAchieved);
2723 #if ENABLE(FONT_LOAD_EVENTS)
2724     if (RuntimeEnabledFeatures::fontLoadEventsEnabled())
2725         m_frame->document()->fontloader()->didLayout();
2726 #endif
2727     
2728     // FIXME: We should consider adding DidLayout as a LayoutMilestone. That would let us merge this
2729     // with didLayout(LayoutMilestones).
2730     m_frame->loader()->client()->dispatchDidLayout();
2731
2732     RenderView* renderView = this->renderView();
2733     if (renderView)
2734         renderView->updateWidgetPositions();
2735     
2736     for (unsigned i = 0; i < maxUpdateWidgetsIterations; i++) {
2737         if (updateWidgets())
2738             break;
2739     }
2740
2741     if (page) {
2742         if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
2743             scrollingCoordinator->frameViewLayoutUpdated(this);
2744     }
2745
2746 #if USE(ACCELERATED_COMPOSITING)
2747     if (renderView && renderView->usesCompositing())
2748         renderView->compositor()->frameViewDidLayout();
2749 #endif
2750
2751     scrollToAnchor();
2752
2753     m_actionScheduler->resume();
2754
2755     if (renderView && !renderView->printing()) {
2756         IntSize currentSize;
2757         if (useFixedLayout() && !fixedLayoutSize().isEmpty() && delegatesScrolling())
2758             currentSize = fixedLayoutSize();
2759         else
2760             currentSize = visibleContentRect(IncludeScrollbars).size();
2761         float currentZoomFactor = renderView->style()->zoom();
2762         bool resized = !m_firstLayout && (currentSize != m_lastViewportSize || currentZoomFactor != m_lastZoomFactor);
2763         m_lastViewportSize = currentSize;
2764         m_lastZoomFactor = currentZoomFactor;
2765         if (resized) {
2766             if (inLiveResize())
2767                 scheduleResizeEvent();
2768             else
2769                 sendResizeEvent();
2770         }
2771     }
2772 }
2773
2774 void FrameView::sendResizeEvent()
2775 {
2776     if (!m_frame)
2777         return;
2778
2779     m_frame->eventHandler()->sendResizeEvent();
2780
2781 #if ENABLE(INSPECTOR)
2782     if (InspectorInstrumentation::hasFrontends()) {
2783         if (Page* page = m_frame->page()) {
2784             if (page->mainFrame() == m_frame) {
2785                 if (InspectorClient* inspectorClient = page->inspectorController()->inspectorClient())
2786                     inspectorClient->didResizeMainFrame(m_frame.get());
2787             }
2788         }
2789     }
2790 #endif
2791 }
2792
2793 void FrameView::delayedResizeEventTimerFired(Timer<FrameView>*)
2794 {
2795     sendResizeEvent();
2796 }
2797
2798 void FrameView::willStartLiveResize()
2799 {
2800     ScrollView::willStartLiveResize();
2801     adjustTiledBackingCoverage();
2802 }
2803     
2804 void FrameView::willEndLiveResize()
2805 {
2806     ScrollableArea::willEndLiveResize();
2807     if (m_delayedResizeEventTimer.isActive()) {
2808         m_delayedResizeEventTimer.stop();
2809         sendResizeEvent();
2810     }
2811     adjustTiledBackingCoverage();
2812 }
2813
2814 void FrameView::scheduleResizeEvent()
2815 {
2816     if (!m_delayedResizeEventTimer.isActive())
2817         m_delayedResizeEventTimer.startOneShot(minimumIntervalBetweenResizeEventsDuringLiveResizeInSeconds);
2818 }
2819
2820 void FrameView::postLayoutTimerFired(Timer<FrameView>*)
2821 {
2822     performPostLayoutTasks();
2823 }
2824
2825 void FrameView::autoSizeIfEnabled()
2826 {
2827     if (!m_shouldAutoSize)
2828         return;
2829
2830     if (m_inAutoSize)
2831         return;
2832
2833     TemporaryChange<bool> changeInAutoSize(m_inAutoSize, true);
2834
2835     Document* document = frame()->document();
2836     if (!document)
2837         return;
2838
2839     RenderView* documentView = document->renderView();
2840     Element* documentElement = document->documentElement();
2841     if (!documentView || !documentElement)
2842         return;
2843
2844     // Start from the minimum size and allow it to grow.
2845     resize(m_minAutoSize.width(), m_minAutoSize.height());
2846
2847     IntSize size = frameRect().size();
2848
2849     // Do the resizing twice. The first time is basically a rough calculation using the preferred width
2850     // which may result in a height change during the second iteration.
2851     for (int i = 0; i < 2; i++) {
2852         // Update various sizes including contentsSize, scrollHeight, etc.
2853         document->updateLayoutIgnorePendingStylesheets();
2854         int width = documentView->minPreferredLogicalWidth();
2855         int height = documentView->documentRect().height();
2856         IntSize newSize(width, height);
2857
2858         // Check to see if a scrollbar is needed for a given dimension and
2859         // if so, increase the other dimension to account for the scrollbar.
2860         // Since the dimensions are only for the view rectangle, once a
2861         // dimension exceeds the maximum, there is no need to increase it further.
2862         if (newSize.width() > m_maxAutoSize.width()) {
2863             RefPtr<Scrollbar> localHorizontalScrollbar = horizontalScrollbar();
2864             if (!localHorizontalScrollbar)
2865                 localHorizontalScrollbar = createScrollbar(HorizontalScrollbar);
2866             if (!localHorizontalScrollbar->isOverlayScrollbar())
2867                 newSize.setHeight(newSize.height() + localHorizontalScrollbar->height());
2868
2869             // Don't bother checking for a vertical scrollbar because the width is at
2870             // already greater the maximum.
2871         } else if (newSize.height() > m_maxAutoSize.height()) {
2872             RefPtr<Scrollbar> localVerticalScrollbar = verticalScrollbar();
2873             if (!localVerticalScrollbar)
2874                 localVerticalScrollbar = createScrollbar(VerticalScrollbar);
2875             if (!localVerticalScrollbar->isOverlayScrollbar())
2876                 newSize.setWidth(newSize.width() + localVerticalScrollbar->width());
2877
2878             // Don't bother checking for a horizontal scrollbar because the height is
2879             // already greater the maximum.
2880         }
2881
2882         // Ensure the size is at least the min bounds.
2883         newSize = newSize.expandedTo(m_minAutoSize);
2884
2885         // Bound the dimensions by the max bounds and determine what scrollbars to show.
2886         ScrollbarMode horizonalScrollbarMode = ScrollbarAlwaysOff;
2887         if (newSize.width() > m_maxAutoSize.width()) {
2888             newSize.setWidth(m_maxAutoSize.width());
2889             horizonalScrollbarMode = ScrollbarAlwaysOn;
2890         }
2891         ScrollbarMode verticalScrollbarMode = ScrollbarAlwaysOff;
2892         if (newSize.height() > m_maxAutoSize.height()) {
2893             newSize.setHeight(m_maxAutoSize.height());
2894             verticalScrollbarMode = ScrollbarAlwaysOn;
2895         }
2896
2897         if (newSize == size)
2898             continue;
2899
2900         // While loading only allow the size to increase (to avoid twitching during intermediate smaller states)
2901         // unless autoresize has just been turned on or the maximum size is smaller than the current size.
2902         if (m_didRunAutosize && size.height() <= m_maxAutoSize.height() && size.width() <= m_maxAutoSize.width()
2903             && !frame()->loader()->isComplete() && (newSize.height() < size.height() || newSize.width() < size.width()))
2904             break;
2905
2906         resize(newSize.width(), newSize.height());
2907         // Force the scrollbar state to avoid the scrollbar code adding them and causing them to be needed. For example,
2908         // a vertical scrollbar may cause text to wrap and thus increase the height (which is the only reason the scollbar is needed).
2909         setVerticalScrollbarLock(false);
2910         setHorizontalScrollbarLock(false);
2911         setScrollbarModes(horizonalScrollbarMode, verticalScrollbarMode, true, true);
2912     }
2913     m_didRunAutosize = true;
2914 }
2915
2916 void FrameView::updateOverflowStatus(bool horizontalOverflow, bool verticalOverflow)
2917 {
2918     if (!m_viewportRenderer)
2919         return;
2920     
2921     if (m_overflowStatusDirty) {
2922         m_horizontalOverflow = horizontalOverflow;
2923         m_verticalOverflow = verticalOverflow;
2924         m_overflowStatusDirty = false;
2925         return;
2926     }
2927     
2928     bool horizontalOverflowChanged = (m_horizontalOverflow != horizontalOverflow);
2929     bool verticalOverflowChanged = (m_verticalOverflow != verticalOverflow);
2930     
2931     if (horizontalOverflowChanged || verticalOverflowChanged) {
2932         m_horizontalOverflow = horizontalOverflow;
2933         m_verticalOverflow = verticalOverflow;
2934         
2935         m_actionScheduler->scheduleEvent(OverflowEvent::create(horizontalOverflowChanged, horizontalOverflow,
2936             verticalOverflowChanged, verticalOverflow),
2937             m_viewportRenderer->node());
2938     }
2939     
2940 }
2941
2942 const Pagination& FrameView::pagination() const
2943 {
2944     if (m_pagination != Pagination())
2945         return m_pagination;
2946
2947     if (Page* page = m_frame->page()) {
2948         if (page->mainFrame() == m_frame)
2949             return page->pagination();
2950     }
2951
2952     return m_pagination;
2953 }
2954
2955 void FrameView::setPagination(const Pagination& pagination)
2956 {
2957     if (m_pagination == pagination)
2958         return;
2959
2960     m_pagination = pagination;
2961
2962     if (m_frame)
2963         m_frame->document()->styleResolverChanged(DeferRecalcStyle);
2964 }
2965
2966 IntRect FrameView::windowClipRect(bool clipToContents) const
2967 {
2968     ASSERT(m_frame->view() == this);
2969
2970     if (paintsEntireContents())
2971         return IntRect(IntPoint(), totalContentsSize());
2972
2973     // Set our clip rect to be our contents.
2974     IntRect clipRect = contentsToWindow(visibleContentRect(clipToContents ? ExcludeScrollbars : IncludeScrollbars));
2975     if (!m_frame || !m_frame->ownerElement())
2976         return clipRect;
2977
2978     // Take our owner element and get its clip rect.
2979     HTMLFrameOwnerElement* ownerElement = m_frame->ownerElement();
2980     FrameView* parentView = ownerElement->document()->view();
2981     if (parentView)
2982         clipRect.intersect(parentView->windowClipRectForFrameOwner(ownerElement, true));
2983     return clipRect;
2984 }
2985
2986 IntRect FrameView::windowClipRectForFrameOwner(const HTMLFrameOwnerElement* ownerElement, bool clipToLayerContents) const
2987 {
2988     // The renderer can sometimes be null when style="display:none" interacts
2989     // with external content and plugins.
2990     if (!ownerElement->renderer())
2991         return windowClipRect();
2992
2993     // If we have no layer, just return our window clip rect.
2994     const RenderLayer* enclosingLayer = ownerElement->renderer()->enclosingLayer();
2995     if (!enclosingLayer)
2996         return windowClipRect();
2997
2998     // Apply the clip from the layer.
2999     IntRect clipRect;
3000     if (clipToLayerContents)
3001         clipRect = pixelSnappedIntRect(enclosingLayer->childrenClipRect());
3002     else
3003         clipRect = pixelSnappedIntRect(enclosingLayer->selfClipRect());
3004     clipRect = contentsToWindow(clipRect); 
3005     return intersection(clipRect, windowClipRect());
3006 }
3007
3008 bool FrameView::isActive() const
3009 {
3010     Page* page = frame()->page();
3011     return page && page->focusController()->isActive();
3012 }
3013
3014 void FrameView::scrollTo(const IntSize& newOffset)
3015 {
3016     LayoutSize offset = scrollOffset();
3017     ScrollView::scrollTo(newOffset);
3018     if (offset != scrollOffset())
3019         scrollPositionChanged();
3020     frame()->loader()->client()->didChangeScrollOffset();
3021 }
3022
3023 void FrameView::invalidateScrollbarRect(Scrollbar* scrollbar, const IntRect& rect)
3024 {
3025     // Add in our offset within the FrameView.
3026     IntRect dirtyRect = rect;
3027     dirtyRect.moveBy(scrollbar->location());
3028     invalidateRect(dirtyRect);
3029 }
3030
3031 void FrameView::getTickmarks(Vector<IntRect>& tickmarks) const
3032 {
3033     tickmarks = frame()->document()->markers()->renderedRectsForMarkers(DocumentMarker::TextMatch);
3034 }
3035
3036 IntRect FrameView::windowResizerRect() const
3037 {
3038     Page* page = frame() ? frame()->page() : 0;
3039     if (!page)
3040         return IntRect();
3041     return page->chrome()->windowResizerRect();
3042 }
3043
3044 float FrameView::visibleContentScaleFactor() const
3045 {
3046     if (!m_frame || !m_frame->page())
3047         return 1;
3048
3049     if (!m_frame->settings()->applyPageScaleFactorInCompositor() || m_frame != m_frame->page()->mainFrame())
3050         return 1;
3051
3052     return m_frame->page()->pageScaleFactor();
3053 }
3054
3055 void FrameView::setVisibleScrollerThumbRect(const IntRect& scrollerThumb)
3056 {
3057     Page* page = m_frame->page();
3058     if (!page)
3059         return;
3060     if (page->mainFrame() != m_frame)
3061         return;
3062     page->chrome()->client()->notifyScrollerThumbIsVisibleInRect(scrollerThumb);
3063 }
3064
3065 bool FrameView::scrollbarsCanBeActive() const
3066 {
3067     if (!m_frame)
3068         return false;
3069
3070     if (m_frame->view() != this)
3071         return false;
3072
3073     if (Page* page = m_frame->page()) {
3074         if (page->shouldSuppressScrollbarAnimations())
3075             return false;
3076     }
3077
3078     if (Document* document = m_frame->document())
3079         return !document->inPageCache();
3080
3081     return false;
3082 }
3083
3084 ScrollableArea* FrameView::enclosingScrollableArea() const
3085 {
3086     // FIXME: Walk up the frame tree and look for a scrollable parent frame or RenderLayer.
3087     return 0;
3088 }
3089
3090 IntRect FrameView::scrollableAreaBoundingBox() const
3091 {
3092     RenderPart* ownerRenderer = frame()->ownerRenderer();
3093     if (!ownerRenderer)
3094         return frameRect();
3095
3096     return ownerRenderer->absoluteContentQuad().enclosingBoundingBox();
3097 }
3098
3099 bool FrameView::isScrollable()
3100 {
3101     // Check for:
3102     // 1) If there an actual overflow.
3103     // 2) display:none or visibility:hidden set to self or inherited.
3104     // 3) overflow{-x,-y}: hidden;
3105     // 4) scrolling: no;
3106
3107     // Covers #1
3108     IntSize totalContentsSize = this->totalContentsSize();
3109     IntSize visibleContentSize = visibleContentRect().size();
3110     if ((totalContentsSize.height() <= visibleContentSize.height() && totalContentsSize.width() <= visibleContentSize.width()))
3111         return false;
3112
3113     // Covers #2.
3114     HTMLFrameOwnerElement* owner = m_frame->ownerElement();
3115     if (owner && (!owner->renderer() || !owner->renderer()->visibleToHitTesting()))
3116         return false;
3117
3118     // Cover #3 and #4.
3119     ScrollbarMode horizontalMode;
3120     ScrollbarMode verticalMode;
3121     calculateScrollbarModesForLayout(horizontalMode, verticalMode, RulesFromWebContentOnly);
3122     if (horizontalMode == ScrollbarAlwaysOff && verticalMode == ScrollbarAlwaysOff)
3123         return false;
3124
3125     return true;
3126 }
3127
3128 void FrameView::updateScrollableAreaSet()
3129 {
3130     // That ensures that only inner frames are cached.
3131     FrameView* parentFrameView = this->parentFrameView();
3132     if (!parentFrameView)
3133         return;
3134
3135     if (!isScrollable()) {
3136         parentFrameView->removeScrollableArea(this);
3137         return;
3138     }
3139
3140     parentFrameView->addScrollableArea(this);
3141 }
3142
3143 bool FrameView::shouldSuspendScrollAnimations() const
3144 {
3145     return m_frame->loader()->state() != FrameStateComplete;
3146 }
3147
3148 void FrameView::scrollbarStyleChanged(int newStyle, bool forceUpdate)
3149 {
3150     Page* page = m_frame->page();
3151     if (!page)
3152         return;
3153     if (page->mainFrame() != m_frame)
3154         return;
3155     page->chrome()->client()->recommendedScrollbarStyleDidChange(newStyle);
3156
3157     if (forceUpdate)
3158         ScrollView::scrollbarStyleChanged(newStyle, forceUpdate);
3159 }
3160
3161 void FrameView::setAnimatorsAreActive()
3162 {
3163     Page* page = m_frame->page();
3164     if (!page)
3165         return;
3166
3167     if (ScrollAnimator* scrollAnimator = existingScrollAnimator())
3168         scrollAnimator->setIsActive();
3169
3170     if (!m_scrollableAreas)
3171         return;
3172
3173     for (HashSet<ScrollableArea*>::const_iterator it = m_scrollableAreas->begin(), end = m_scrollableAreas->end(); it != end; ++it) {
3174         ScrollableArea* scrollableArea = *it;
3175
3176         ASSERT(scrollableArea->scrollbarsCanBeActive());
3177         scrollableArea->scrollAnimator()->setIsActive();
3178     }
3179 }
3180
3181 void FrameView::notifyPageThatContentAreaWillPaint() const
3182 {
3183     Page* page = m_frame->page();
3184     if (!page)
3185         return;
3186
3187     contentAreaWillPaint();
3188
3189     if (!m_scrollableAreas)
3190         return;
3191
3192     for (HashSet<ScrollableArea*>::const_iterator it = m_scrollableAreas->begin(), end = m_scrollableAreas->end(); it != end; ++it) {
3193         ScrollableArea* scrollableArea = *it;
3194
3195         if (!scrollableArea->scrollbarsCanBeActive())
3196             continue;
3197
3198         scrollableArea->contentAreaWillPaint();
3199     }
3200 }
3201
3202 bool FrameView::scrollAnimatorEnabled() const
3203 {
3204 #if ENABLE(SMOOTH_SCROLLING)
3205     if (Page* page = m_frame->page())
3206         return page->settings()->scrollAnimatorEnabled();
3207 #endif
3208
3209     return false;
3210 }
3211
3212 #if ENABLE(DASHBOARD_SUPPORT) || ENABLE(DRAGGABLE_REGION)
3213 void FrameView::updateAnnotatedRegions()
3214 {
3215     Document* document = m_frame->document();
3216     if (!document->hasAnnotatedRegions())
3217         return;
3218     Vector<AnnotatedRegionValue> newRegions;
3219     document->renderBox()->collectAnnotatedRegions(newRegions);
3220     if (newRegions == document->annotatedRegions())
3221         return;
3222     document->setAnnotatedRegions(newRegions);
3223     Page* page = m_frame->page();
3224     if (!page)
3225         return;
3226     page->chrome()->client()->annotatedRegionsChanged();
3227 }
3228 #endif
3229
3230 void FrameView::updateScrollCorner()
3231 {
3232     RenderObject* renderer = 0;
3233     RefPtr<RenderStyle> cornerStyle;
3234     IntRect cornerRect = scrollCornerRect();
3235     
3236     if (!cornerRect.isEmpty()) {
3237         // Try the <body> element first as a scroll corner source.
3238         Document* doc = m_frame->document();
3239         Element* body = doc ? doc->body() : 0;
3240         if (body && body->renderer()) {
3241             renderer = body->renderer();
3242             cornerStyle = renderer->getUncachedPseudoStyle(PseudoStyleRequest(SCROLLBAR_CORNER), renderer->style());
3243         }
3244         
3245         if (!cornerStyle) {
3246             // If the <body> didn't have a custom style, then the root element might.
3247             Element* docElement = doc ? doc->documentElement() : 0;
3248             if (docElement && docElement->renderer()) {
3249                 renderer = docElement->renderer();
3250                 cornerStyle = renderer->getUncachedPseudoStyle(PseudoStyleRequest(SCROLLBAR_CORNER), renderer->style());
3251             }
3252         }
3253         
3254         if (!cornerStyle) {
3255             // If we have an owning iframe/frame element, then it can set the custom scrollbar also.
3256             if (RenderPart* renderer = m_frame->ownerRenderer())
3257                 cornerStyle = renderer->getUncachedPseudoStyle(PseudoStyleRequest(SCROLLBAR_CORNER), renderer->style());
3258         }
3259     }
3260
3261     if (cornerStyle) {
3262         if (!m_scrollCorner)
3263             m_scrollCorner = RenderScrollbarPart::createAnonymous(renderer->document());
3264         m_scrollCorner->setStyle(cornerStyle.release());
3265         invalidateScrollCorner(cornerRect);
3266     } else if (m_scrollCorner) {
3267         m_scrollCorner->destroy();
3268         m_scrollCorner = 0;
3269     }
3270
3271     ScrollView::updateScrollCorner();
3272 }
3273
3274 void FrameView::paintScrollCorner(GraphicsContext* context, const IntRect& cornerRect)
3275 {
3276     if (context->updatingControlTints()) {
3277         updateScrollCorner();
3278         return;
3279     }
3280
3281     if (m_scrollCorner) {
3282         bool needsBackgorund = m_frame->page() && m_frame->page()->mainFrame() == m_frame;
3283         if (needsBackgorund)
3284             context->fillRect(cornerRect, baseBackgroundColor(), ColorSpaceDeviceRGB);
3285         m_scrollCorner->paintIntoRect(context, cornerRect.location(), cornerRect);
3286         return;
3287     }
3288
3289     ScrollView::paintScrollCorner(context, cornerRect);
3290 }
3291
3292 void FrameView::paintScrollbar(GraphicsContext* context, Scrollbar* bar, const IntRect& rect)
3293 {
3294     bool needsBackgorund = bar->isCustomScrollbar() && (m_frame->page() && m_frame->page()->mainFrame() == m_frame);
3295     if (needsBackgorund) {
3296         IntRect toFill = bar->frameRect();
3297         toFill.intersect(rect);
3298         context->fillRect(toFill, baseBackgroundColor(), ColorSpaceDeviceRGB);
3299     }
3300
3301     ScrollView::paintScrollbar(context, bar, rect);
3302 }
3303
3304 Color FrameView::documentBackgroundColor() const
3305 {
3306     // <https://bugs.webkit.org/show_bug.cgi?id=59540> We blend the background color of
3307     // the document and the body against the base background color of the frame view.
3308     // Background images are unfortunately impractical to include.
3309
3310     // Return invalid Color objects whenever there is insufficient information.
3311     if (!frame()->document())
3312         return Color();
3313
3314     Element* htmlElement = frame()->document()->documentElement();
3315     Element* bodyElement = frame()->document()->body();
3316
3317     // Start with invalid colors.
3318     Color htmlBackgroundColor;
3319     Color bodyBackgroundColor;
3320     if (htmlElement && htmlElement->renderer())
3321         htmlBackgroundColor = htmlElement->renderer()->style()->visitedDependentColor(CSSPropertyBackgroundColor);
3322     if (bodyElement && bodyElement->renderer())
3323         bodyBackgroundColor = bodyElement->renderer()->style()->visitedDependentColor(CSSPropertyBackgroundColor);
3324
3325     if (!bodyBackgroundColor.isValid()) {
3326         if (!htmlBackgroundColor.isValid())
3327             return Color();
3328         return baseBackgroundColor().blend(htmlBackgroundColor);
3329     }
3330
3331     if (!htmlBackgroundColor.isValid())
3332         return baseBackgroundColor().blend(bodyBackgroundColor);
3333
3334     // We take the aggregate of the base background color
3335     // the <html> background color, and the <body>
3336     // background color to find the document color. The
3337     // addition of the base background color is not
3338     // technically part of the document background, but it
3339     // otherwise poses problems when the aggregate is not
3340     // fully opaque.
3341     return baseBackgroundColor().blend(htmlBackgroundColor).blend(bodyBackgroundColor);
3342 }
3343
3344 bool FrameView::hasCustomScrollbars() const
3345 {
3346     const HashSet<RefPtr<Widget> >* viewChildren = children();
3347     HashSet<RefPtr<Widget> >::const_iterator end = viewChildren->end();
3348     for (HashSet<RefPtr<Widget> >::const_iterator current = viewChildren->begin(); current != end; ++current) {
3349         Widget* widget = current->get();
3350         if (widget->isFrameView()) {
3351             if (toFrameView(widget)->hasCustomScrollbars())
3352                 return true;
3353         } else if (widget->isScrollbar()) {
3354             Scrollbar* scrollbar = static_cast<Scrollbar*>(widget);
3355             if (scrollbar->isCustomScrollbar())
3356                 return true;
3357         }
3358     }
3359
3360     return false;
3361 }
3362
3363 FrameView* FrameView::parentFrameView() const
3364 {
3365     if (!parent())
3366         return 0;
3367
3368     if (Frame* parentFrame = m_frame->tree()->parent())
3369         return parentFrame->view();
3370
3371     return 0;
3372 }
3373
3374 bool FrameView::isInChildFrameWithFrameFlattening() const
3375 {
3376     if (!parent() || !m_frame->ownerElement())
3377         return false;
3378
3379     // Frame flattening applies when the owner element is either in a frameset or
3380     // an iframe with flattening parameters.
3381     if (m_frame->ownerElement()->hasTagName(iframeTag)) {
3382         RenderIFrame* iframeRenderer = toRenderIFrame(m_frame->ownerElement()->renderPart());
3383         if (iframeRenderer->flattenFrame() || iframeRenderer->isSeamless())
3384             return true;
3385     }
3386
3387     if (!m_frame->settings() || !m_frame->settings()->frameFlatteningEnabled())
3388         return false;
3389
3390     if (m_frame->ownerElement()->hasTagName(frameTag))
3391         return true;
3392
3393     return false;
3394 }
3395
3396 bool FrameView::doLayoutWithFrameFlattening(bool allowSubtree)
3397 {
3398     // Try initiating layout from the topmost parent.
3399     FrameView* parentView = parentFrameView();
3400
3401     if (!parentView)
3402         return false;
3403
3404     // In the middle of parent layout, no need to restart from topmost.
3405     if (parentView->m_nestedLayoutCount)
3406         return false;
3407
3408     // Parent tree is clean. Starting layout from it would have no effect.
3409     if (!parentView->needsLayout())
3410         return false;
3411
3412     while (parentView->parentFrameView())
3413         parentView = parentView->parentFrameView();
3414
3415     parentView->layout(allowSubtree);
3416
3417     RenderObject* root = m_layoutRoot ? m_layoutRoot : m_frame->document()->renderer();
3418     ASSERT_UNUSED(root, !root->needsLayout());
3419
3420     return true;
3421 }
3422
3423 void FrameView::updateControlTints()
3424 {
3425     // This is called when control tints are changed from aqua/graphite to clear and vice versa.
3426     // We do a "fake" paint, and when the theme gets a paint call, it can then do an invalidate.
3427     // This is only done if the theme supports control tinting. It's up to the theme and platform
3428     // to define when controls get the tint and to call this function when that changes.
3429     
3430     // Optimize the common case where we bring a window to the front while it's still empty.
3431     if (!m_frame || m_frame->document()->url().isEmpty())
3432         return;
3433
3434     RenderView* renderView = this->renderView();
3435     if ((renderView && renderView->theme()->supportsControlTints()) || hasCustomScrollbars())
3436         paintControlTints();
3437 }
3438
3439 void FrameView::paintControlTints()
3440 {
3441     if (needsLayout())
3442         layout();
3443     PlatformGraphicsContext* const noContext = 0;
3444     GraphicsContext context(noContext);
3445     context.setUpdatingControlTints(true);
3446     if (platformWidget())
3447         paintContents(&context, visibleContentRect());
3448     else
3449         paint(&context, frameRect());
3450 }
3451
3452 bool FrameView::wasScrolledByUser() const
3453 {
3454     return m_wasScrolledByUser;
3455 }
3456
3457 void FrameView::setWasScrolledByUser(bool wasScrolledByUser)
3458 {
3459     if (m_inProgrammaticScroll)
3460         return;
3461     m_maintainScrollPositionAnchor = 0;
3462     if (m_wasScrolledByUser == wasScrolledByUser)
3463         return;
3464     m_wasScrolledByUser = wasScrolledByUser;
3465     adjustTiledBackingCoverage();
3466 }
3467
3468 void FrameView::paintContents(GraphicsContext* p, const IntRect& rect)
3469 {
3470     if (!frame())
3471         return;
3472
3473     Document* document = m_frame->document();
3474
3475 #ifndef NDEBUG
3476     bool fillWithRed;
3477     if (document->printing())
3478         fillWithRed = false; // Printing, don't fill with red (can't remember why).
3479     else if (m_frame->ownerElement())
3480         fillWithRed = false; // Subframe, don't fill with red.
3481     else if (isTransparent())
3482         fillWithRed = false; // Transparent, don't fill with red.
3483     else if (m_paintBehavior & PaintBehaviorSelectionOnly)
3484         fillWithRed = false; // Selections are transparent, don't fill with red.
3485     else if (m_nodeToDraw)
3486         fillWithRed = false; // Element images are transparent, don't fill with red.
3487     else
3488         fillWithRed = true;
3489     
3490     if (fillWithRed)
3491         p->fillRect(rect, Color(0xFF, 0, 0), ColorSpaceDeviceRGB);
3492 #endif
3493
3494     RenderView* renderView = this->renderView();
3495     if (!renderView) {
3496         LOG_ERROR("called FrameView::paint with nil renderer");
3497         return;
3498     }
3499
3500     ASSERT(!needsLayout());
3501     if (needsLayout())
3502         return;
3503
3504     InspectorInstrumentation::willPaint(renderView);
3505
3506     bool isTopLevelPainter = !sCurrentPaintTimeStamp;
3507     if (isTopLevelPainter)
3508         sCurrentPaintTimeStamp = currentTime();
3509
3510     FontCachePurgePreventer fontCachePurgePreventer;
3511
3512 #if USE(ACCELERATED_COMPOSITING)
3513     if (!p->paintingDisabled() && !document->printing())
3514         flushCompositingStateForThisFrame(m_frame.get());
3515 #endif
3516
3517     PaintBehavior oldPaintBehavior = m_paintBehavior;
3518     
3519     if (FrameView* parentView = parentFrameView()) {
3520         if (parentView->paintBehavior() & PaintBehaviorFlattenCompositingLayers)
3521             m_paintBehavior |= PaintBehaviorFlattenCompositingLayers;
3522     }
3523     
3524     if (m_paintBehavior == PaintBehaviorNormal)
3525         document->markers()->invalidateRenderedRectsForMarkersInRect(rect);
3526
3527     if (document->printing())
3528         m_paintBehavior |= PaintBehaviorFlattenCompositingLayers;
3529
3530     bool flatteningPaint = m_paintBehavior & PaintBehaviorFlattenCompositingLayers;
3531     bool isRootFrame = !m_frame->ownerElement();
3532     if (flatteningPaint && isRootFrame)
3533         notifyWidgetsInAllFrames(WillPaintFlattened);
3534
3535     ASSERT(!m_isPainting);
3536     m_isPainting = true;
3537
3538     // m_nodeToDraw is used to draw only one element (and its descendants)
3539     RenderObject* eltRenderer = m_nodeToDraw ? m_nodeToDraw->renderer() : 0;
3540     RenderLayer* rootLayer = renderView->layer();
3541
3542 #ifndef NDEBUG
3543     RenderObject::SetLayoutNeededForbiddenScope forbidSetNeedsLayout(rootLayer->renderer());
3544 #endif
3545
3546     rootLayer->paint(p, rect, m_paintBehavior, eltRenderer);
3547
3548     if (rootLayer->containsDirtyOverlayScrollbars())
3549         rootLayer->paintOverlayScrollbars(p, rect, m_paintBehavior, eltRenderer);
3550
3551     m_isPainting = false;
3552
3553     if (flatteningPaint && isRootFrame)
3554         notifyWidgetsInAllFrames(DidPaintFlattened);
3555
3556     m_paintBehavior = oldPaintBehavior;
3557     m_lastPaintTime = currentTime();
3558
3559     // Regions may have changed as a result of the visibility/z-index of element changing.
3560 #if ENABLE(DASHBOARD_SUPPORT) || ENABLE(DRAGGABLE_REGION)
3561     if (document->annotatedRegionsDirty())
3562         updateAnnotatedRegions();
3563 #endif
3564
3565     if (isTopLevelPainter)
3566         sCurrentPaintTimeStamp = 0;
3567
3568     InspectorInstrumentation::didPaint(renderView, p, rect);
3569 }
3570
3571 void FrameView::setPaintBehavior(PaintBehavior behavior)
3572 {
3573     m_paintBehavior = behavior;
3574 }
3575
3576 PaintBehavior FrameView::paintBehavior() const
3577 {
3578     return m_paintBehavior;
3579 }
3580
3581 bool FrameView::isPainting() const
3582 {
3583     return m_isPainting;
3584 }
3585
3586 void FrameView::setNodeToDraw(Node* node)
3587 {
3588     m_nodeToDraw = node;
3589 }
3590
3591 void FrameView::paintContentsForSnapshot(GraphicsContext* context, const IntRect& imageRect, SelectionInSnaphot shouldPaintSelection, CoordinateSpaceForSnapshot coordinateSpace)
3592 {
3593     updateLayoutAndStyleIfNeededRecursive();
3594
3595     // Cache paint behavior and set a new behavior appropriate for snapshots.
3596     PaintBehavior oldBehavior = paintBehavior();
3597     setPaintBehavior(oldBehavior | PaintBehaviorFlattenCompositingLayers);
3598
3599     // If the snapshot should exclude selection, then we'll clear the current selection
3600     // in the render tree only. This will allow us to restore the selection from the DOM
3601     // after we paint the snapshot.
3602     if (shouldPaintSelection == ExcludeSelection) {
3603         for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext(m_frame.get())) {
3604             if (RenderView* root = frame->contentRenderer())
3605                 root->clearSelection();
3606         }
3607     }
3608
3609     if (coordinateSpace == DocumentCoordinates)
3610         paintContents(context, imageRect);
3611     else {
3612         // A snapshot in ViewCoordinates will include a scrollbar, and the snapshot will contain
3613         // whatever content the document is currently scrolled to.
3614         paint(context, imageRect);
3615     }
3616
3617     // Restore selection.
3618     if (shouldPaintSelection == ExcludeSelection) {
3619         for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext(m_frame.get()))
3620             frame->selection()->updateAppearance();
3621     }
3622
3623     // Restore cached paint behavior.
3624     setPaintBehavior(oldBehavior);
3625 }
3626
3627 void FrameView::paintOverhangAreas(GraphicsContext* context, const IntRect& horizontalOverhangArea, const IntRect& verticalOverhangArea, const IntRect& dirtyRect)
3628 {
3629     if (context->paintingDisabled())
3630         return;
3631
3632     if (m_frame->document()->printing())
3633         return;
3634
3635     Page* page = m_frame->page();
3636     if (page->mainFrame() == m_frame) {
3637         if (page->chrome()->client()->paintCustomOverhangArea(context, horizontalOverhangArea, verticalOverhangArea, dirtyRect))
3638             return;
3639     }
3640
3641     ScrollView::paintOverhangAreas(context, horizontalOverhangArea, verticalOverhangArea, dirtyRect);
3642 }
3643
3644 void FrameView::updateLayoutAndStyleIfNeededRecursive()
3645 {
3646     // We have to crawl our entire tree looking for any FrameViews that need
3647     // layout and make sure they are up to date.
3648     // Mac actually tests for intersection with the dirty region and tries not to
3649     // update layout for frames that are outside the dirty region.  Not only does this seem
3650     // pointless (since those frames will have set a zero timer to layout anyway), but
3651     // it is also incorrect, since if two frames overlap, the first could be excluded from the dirty
3652     // region but then become included later by the second frame adding rects to the dirty region
3653     // when it lays out.
3654
3655     m_frame->document()->updateStyleIfNeeded();
3656
3657     if (needsLayout())
3658         layout();
3659
3660     // Grab a copy of the children() set, as it may be mutated by the following updateLayoutAndStyleIfNeededRecursive
3661     // calls, as they can potentially re-enter a layout of the parent frame view, which may add/remove scrollbars
3662     // and thus mutates the children() set.
3663     Vector<RefPtr<FrameView> > frameViews;
3664     collectFrameViewChildren(this, frameViews);
3665
3666     const Vector<RefPtr<FrameView> >::iterator end = frameViews.end();
3667     for (Vector<RefPtr<FrameView> >::iterator it = frameViews.begin(); it != end; ++it)
3668         (*it)->updateLayoutAndStyleIfNeededRecursive();
3669
3670     // updateLayoutAndStyleIfNeededRecursive is called when we need to make sure style and layout are up-to-date before
3671     // painting, so we need to flush out any deferred repaints too.
3672     flushDeferredRepaints();
3673
3674     // When frame flattening is on, child frame can mark parent frame dirty. In such case, child frame
3675     // needs to call layout on parent frame recursively.
3676     // This assert ensures that parent frames are clean, when child frames finished updating layout and style.
3677     ASSERT(!needsLayout());
3678 }
3679
3680 void FrameView::setIsVisuallyNonEmpty()
3681 {
3682     m_isVisuallyNonEmpty = true;
3683     adjustTiledBackingCoverage();
3684 }
3685
3686 void FrameView::enableAutoSizeMode(bool enable, const IntSize& minSize, const IntSize& maxSize)
3687 {
3688     ASSERT(!enable || !minSize.isEmpty());
3689     ASSERT(minSize.width() <= maxSize.width());
3690     ASSERT(minSize.height() <= maxSize.height());
3691
3692     if (m_shouldAutoSize == enable && m_minAutoSize == minSize && m_maxAutoSize == maxSize)
3693         return;
3694
3695     m_shouldAutoSize = enable;
3696     m_minAutoSize = minSize;
3697     m_maxAutoSize = maxSize;
3698     m_didRunAutosize = false;
3699
3700     setNeedsLayout();
3701     scheduleRelayout();
3702     if (m_shouldAutoSize)
3703         return;
3704
3705     // Since autosize mode forces the scrollbar mode, change them to being auto.
3706     setVerticalScrollbarLock(false);
3707     setHorizontalScrollbarLock(false);
3708     setScrollbarModes(ScrollbarAuto, ScrollbarAuto);
3709 }
3710
3711 void FrameView::forceLayout(bool allowSubtree)
3712 {
3713     layout(allowSubtree);
3714 }
3715
3716 void FrameView::forceLayoutForPagination(const FloatSize& pageSize, const FloatSize& originalPageSize, float maximumShrinkFactor, AdjustViewSizeOrNot shouldAdjustViewSize)
3717 {
3718     // Dumping externalRepresentation(m_frame->renderer()).ascii() is a good trick to see
3719     // the state of things before and after the layout
3720     if (RenderView* renderView = this->renderView()) {
3721         float pageLogicalWidth = renderView->style()->isHorizontalWritingMode() ? pageSize.width() : pageSize.height();
3722         float pageLogicalHeight = renderView->style()->isHorizontalWritingMode() ? pageSize.height() : pageSize.width();
3723
3724         LayoutUnit flooredPageLogicalWidth = static_cast<LayoutUnit>(pageLogicalWidth);
3725         LayoutUnit flooredPageLogicalHeight = static_cast<LayoutUnit>(pageLogicalHeight);
3726         renderView->setLogicalWidth(flooredPageLogicalWidth);
3727         renderView->setPageLogicalHeight(flooredPageLogicalHeight);
3728         renderView->setNeedsLayoutAndPrefWidthsRecalc();
3729         forceLayout();
3730
3731         // If we don't fit in the given page width, we'll lay out again. If we don't fit in the
3732         // page width when shrunk, we will lay out at maximum shrink and clip extra content.
3733         // FIXME: We are assuming a shrink-to-fit printing implementation.  A cropping
3734         // implementation should not do this!
3735         bool horizontalWritingMode = renderView->style()->isHorizontalWritingMode();
3736         const LayoutRect& documentRect = renderView->documentRect();
3737         LayoutUnit docLogicalWidth = horizontalWritingMode ? documentRect.width() : documentRect.height();
3738         if (docLogicalWidth > pageLogicalWidth) {
3739             int expectedPageWidth = std::min<float>(documentRect.width(), pageSize.width() * maximumShrinkFactor);
3740             int expectedPageHeight = std::min<float>(documentRect.height(), pageSize.height() * maximumShrinkFactor);
3741             FloatSize maxPageSize = m_frame->resizePageRectsKeepingRatio(FloatSize(originalPageSize.width(), originalPageSize.height()), FloatSize(expectedPageWidth, expectedPageHeight));
3742             pageLogicalWidth = horizontalWritingMode ? maxPageSize.width() : maxPageSize.height();
3743             pageLogicalHeight = horizontalWritingMode ? maxPageSize.height() : maxPageSize.width();
3744
3745             flooredPageLogicalWidth = static_cast<LayoutUnit>(pageLogicalWidth);
3746             flooredPageLogicalHeight = static_cast<LayoutUnit>(pageLogicalHeight);
3747             renderView->setLogicalWidth(flooredPageLogicalWidth);
3748             renderView->setPageLogicalHeight(flooredPageLogicalHeight);
3749             renderView->setNeedsLayoutAndPrefWidthsRecalc();
3750             forceLayout();
3751
3752             const LayoutRect& updatedDocumentRect = renderView->documentRect();
3753             LayoutUnit docLogicalHeight = horizontalWritingMode ? updatedDocumentRect.height() : updatedDocumentRect.width();
3754             LayoutUnit docLogicalTop = horizontalWritingMode ? updatedDocumentRect.y() : updatedDocumentRect.x();
3755             LayoutUnit docLogicalRight = horizontalWritingMode ? updatedDocumentRect.maxX() : updatedDocumentRect.maxY();
3756             LayoutUnit clippedLogicalLeft = 0;
3757             if (!renderView->style()->isLeftToRightDirection())
3758                 clippedLogicalLeft = docLogicalRight - pageLogicalWidth;
3759             LayoutRect overflow(clippedLogicalLeft, docLogicalTop, pageLogicalWidth, docLogicalHeight);
3760
3761             if (!horizontalWritingMode)
3762                 overflow = overflow.transposedRect();
3763             renderView->clearLayoutOverflow();
3764             renderView->addLayoutOverflow(overflow); // This is how we clip in case we overflow again.
3765         }
3766     }
3767
3768     if (shouldAdjustViewSize)
3769         adjustViewSize();
3770 }
3771
3772 void FrameView::adjustPageHeightDeprecated(float *newBottom, float oldTop, float oldBottom, float /*bottomLimit*/)
3773 {
3774     RenderView* renderView = this->renderView();
3775     if (!renderView) {
3776         *newBottom = oldBottom;
3777         return;
3778
3779     }
3780     // Use a context with painting disabled.
3781     GraphicsContext context((PlatformGraphicsContext*)0);
3782     renderView->setTruncatedAt(static_cast<int>(floorf(oldBottom)));
3783     IntRect dirtyRect(0, static_cast<int>(floorf(oldTop)), renderView->layoutOverflowRect().maxX(), static_cast<int>(ceilf(oldBottom - oldTop)));
3784     renderView->setPrintRect(dirtyRect);
3785     renderView->layer()->paint(&context, dirtyRect);
3786     *newBottom = renderView->bestTruncatedAt();
3787     if (!*newBottom)
3788         *newBottom = oldBottom;
3789     renderView->setPrintRect(IntRect());
3790 }
3791
3792 IntRect FrameView::convertFromRenderer(const RenderObject* renderer, const IntRect& rendererRect) const
3793 {
3794     IntRect rect = pixelSnappedIntRect(enclosingLayoutRect(renderer->localToAbsoluteQuad(FloatRect(rendererRect)).boundingBox()));
3795
3796     // Convert from page ("absolute") to FrameView coordinates.
3797     if (!delegatesScrolling())
3798         rect.moveBy(-scrollPosition());
3799
3800     return rect;
3801 }
3802
3803 IntRect FrameView::convertToRenderer(const RenderObject* renderer, const IntRect& viewRect) const
3804 {
3805     IntRect rect = viewRect;
3806     
3807     // Convert from FrameView coords into page ("absolute") coordinates.
3808     if (!delegatesScrolling())
3809         rect.moveBy(scrollPosition());
3810
3811     // FIXME: we don't have a way to map an absolute rect down to a local quad, so just
3812     // move the rect for now.
3813     rect.setLocation(roundedIntPoint(renderer->absoluteToLocal(rect.location(), UseTransforms)));
3814     return rect;
3815 }
3816
3817 IntPoint FrameView::convertFromRenderer(const RenderObject* renderer, const IntPoint& rendererPoint) const
3818 {
3819     IntPoint point = roundedIntPoint(renderer->localToAbsolute(rendererPoint, UseTransforms));
3820
3821     // Convert from page ("absolute") to FrameView coordinates.
3822     if (!delegatesScrolling())
3823         point.moveBy(-scrollPosition());
3824     return point;
3825 }
3826
3827 IntPoint FrameView::convertToRenderer(const RenderObject* renderer, const IntPoint& viewPoint) const
3828 {
3829     IntPoint point = viewPoint;
3830
3831     // Convert from FrameView coords into page ("absolute") coordinates.
3832     if (!delegatesScrolling())
3833         point += IntSize(scrollX(), scrollY());
3834
3835     return roundedIntPoint(renderer->absoluteToLocal(point, UseTransforms));
3836 }
3837
3838 IntRect FrameView::convertToContainingView(const IntRect& localRect) const
3839 {
3840     if (const ScrollView* parentScrollView = parent()) {
3841         if (parentScrollView->isFrameView()) {
3842             const FrameView* parentView = toFrameView(parentScrollView);
3843             // Get our renderer in the parent view
3844             RenderPart* renderer = m_frame->ownerRenderer();
3845             if (!renderer)
3846                 return localRect;
3847                 
3848             IntRect rect(localRect);
3849             // Add borders and padding??
3850             rect.move(renderer->borderLeft() + renderer->paddingLeft(),
3851                       renderer->borderTop() + renderer->paddingTop());
3852             return parentView->convertFromRenderer(renderer, rect);
3853         }
3854         
3855         return Widget::convertToContainingView(localRect);
3856     }
3857     
3858     return localRect;
3859 }
3860
3861 IntRect FrameView::convertFromContainingView(const IntRect& parentRect) const
3862 {
3863     if (const ScrollView* parentScrollView = parent()) {
3864         if (parentScrollView->isFrameView()) {
3865             const FrameView* parentView = toFrameView(parentScrollView);
3866
3867             // Get our renderer in the parent view
3868             RenderPart* renderer = m_frame->ownerRenderer();
3869             if (!renderer)
3870                 return parentRect;
3871
3872             IntRect rect = parentView->convertToRenderer(renderer, parentRect);
3873             // Subtract borders and padding
3874             rect.move(-renderer->borderLeft() - renderer->paddingLeft(),
3875                       -renderer->borderTop() - renderer->paddingTop());
3876             return rect;
3877         }
3878         
3879         return Widget::convertFromContainingView(parentRect);
3880     }
3881     
3882     return parentRect;
3883 }
3884
3885 IntPoint FrameView::convertToContainingView(const IntPoint& localPoint) const
3886 {
3887     if (const ScrollView* parentScrollView = parent()) {
3888         if (parentScrollView->isFrameView()) {
3889             const FrameView* parentView = toFrameView(parentScrollView);
3890
3891             // Get our renderer in the parent view
3892             RenderPart* renderer = m_frame->ownerRenderer();
3893             if (!renderer)
3894                 return localPoint;
3895                 
3896             IntPoint point(localPoint);
3897
3898             // Add borders and padding
3899             point.move(renderer->borderLeft() + renderer->paddingLeft(),
3900                        renderer->borderTop() + renderer->paddingTop());
3901             return parentView->convertFromRenderer(renderer, point);
3902         }
3903         
3904         return Widget::convertToContainingView(localPoint);
3905     }
3906     
3907     return localPoint;
3908 }
3909
3910 IntPoint FrameView::convertFromContainingView(const IntPoint& parentPoint) const
3911 {
3912     if (const ScrollView* parentScrollView = parent()) {
3913         if (parentScrollView->isFrameView()) {
3914             const FrameView* parentView = toFrameView(parentScrollView);
3915
3916             // Get our renderer in the parent view
3917             RenderPart* renderer = m_frame->ownerRenderer();
3918             if (!renderer)
3919                 return parentPoint;
3920
3921             IntPoint point = parentView->convertToRenderer(renderer, parentPoint);
3922             // Subtract borders and padding
3923             point.move(-renderer->borderLeft() - renderer->paddingLeft(),
3924                        -renderer->borderTop() - renderer->paddingTop());
3925             return point;
3926         }
3927         
3928         return Widget::convertFromContainingView(parentPoint);
3929     }
3930     
3931     return parentPoint;
3932 }
3933
3934 // Normal delay
3935 void FrameView::setRepaintThrottlingDeferredRepaintDelay(double p)
3936 {
3937     s_normalDeferredRepaintDelay = p;
3938 }
3939
3940 // Negative value would mean that first few repaints happen without a delay
3941 void FrameView::setRepaintThrottlingnInitialDeferredRepaintDelayDuringLoading(double p)
3942 {
3943     s_initialDeferredRepaintDelayDuringLoading = p;
3944 }
3945
3946 // The delay grows on each repaint to this maximum value
3947 void FrameView::setRepaintThrottlingMaxDeferredRepaintDelayDuringLoading(double p)
3948 {
3949     s_maxDeferredRepaintDelayDuringLoading = p;
3950 }
3951
3952 // On each repaint the delay increases by this amount
3953 void FrameView::setRepaintThrottlingDeferredRepaintDelayIncrementDuringLoading(double p)
3954 {
3955     s_deferredRepaintDelayIncrementDuringLoading = p;
3956 }
3957
3958 void FrameView::setTracksRepaints(bool trackRepaints)
3959 {
3960     if (trackRepaints == m_isTrackingRepaints)
3961         return;
3962
3963     // Force layout to flush out any pending repaints.
3964     if (trackRepaints) {
3965         if (frame() && frame()->document())
3966             frame()->document()->updateLayout();
3967     }
3968
3969 #if USE(ACCELERATED_COMPOSITING)
3970     for (Frame* frame = m_frame->tree()->top(); frame; frame = frame->tree()->traverseNext()) {
3971         if (RenderView* renderView = frame->contentRenderer())
3972             renderView->compositor()->setTracksRepaints(trackRepaints);
3973     }
3974 #endif
3975
3976     resetTrackedRepaints();
3977     m_isTrackingRepaints = trackRepaints;
3978 }
3979
3980 void FrameView::resetTrackedRepaints()
3981 {
3982     m_trackedRepaintRects.clear();
3983 #if USE(ACCELERATED_COMPOSITING)
3984     if (RenderView* renderView = this->renderView())
3985         renderView->compositor()->resetTrackedRepaintRects();
3986 #endif
3987 }
3988
3989 String FrameView::trackedRepaintRectsAsText() const
3990 {
3991     if (frame() && frame()->document())
3992         frame()->document()->updateLayout();
3993
3994     TextStream ts;
3995     if (!m_trackedRepaintRects.isEmpty()) {
3996         ts << "(repaint rects\n";
3997         for (size_t i = 0; i < m_trackedRepaintRects.size(); ++i)
3998             ts << "  (rect " << m_trackedRepaintRects[i].x() << " " << m_trackedRepaintRects[i].y() << " " << m_trackedRepaintRects[i].width() << " " << m_trackedRepaintRects[i].height() << ")\n";
3999         ts << ")\n";
4000     }
4001     return ts.release();
4002 }
4003
4004 bool FrameView::addScrollableArea(ScrollableArea* scrollableArea)
4005 {
4006     if (!m_scrollableAreas)
4007         m_scrollableAreas = adoptPtr(new ScrollableAreaSet);
4008     return m_scrollableAreas->add(scrollableArea).isNewEntry;
4009 }
4010
4011 bool FrameView::removeScrollableArea(ScrollableArea* scrollableArea)
4012 {
4013     if (!m_scrollableAreas)
4014         return false;
4015
4016     ScrollableAreaSet::iterator it = m_scrollableAreas->find(scrollableArea);
4017     if (it == m_scrollableAreas->end())
4018         return false;
4019
4020     m_scrollableAreas->remove(it);
4021     return true;
4022 }
4023
4024 bool FrameView::containsScrollableArea(ScrollableArea* scrollableArea) const
4025 {
4026     if (!m_scrollableAreas)
4027         return false;
4028     return m_scrollableAreas->contains(scrollableArea);
4029 }
4030
4031 void FrameView::removeChild(Widget* widget)
4032 {
4033     if (widget->isFrameView())
4034         removeScrollableArea(toFrameView(widget));
4035
4036     ScrollView::removeChild(widget);
4037 }
4038
4039 bool FrameView::wheelEvent(const PlatformWheelEvent& wheelEvent)
4040 {
4041     // Note that to allow for rubber-band over-scroll behavior, even non-scrollable views
4042     // should handle wheel events.
4043 #if !ENABLE(RUBBER_BANDING)
4044     if (!isScrollable())
4045         return false;
4046 #endif
4047
4048     if (delegatesScrolling()) {
4049         IntSize offset = scrollOffset();
4050         IntSize newOffset = IntSize(offset.width() - wheelEvent.deltaX(), offset.height() - wheelEvent.deltaY());
4051         if (offset != newOffset) {
4052             ScrollView::scrollTo(newOffset);
4053             scrollPositionChanged();
4054             frame()->loader()->client()->didChangeScrollOffset();
4055         }
4056         return true;
4057     }
4058
4059     // We don't allow mouse wheeling to happen in a ScrollView that has had its scrollbars explicitly disabled.
4060     if (!canHaveScrollbars())
4061         return false;
4062
4063 #if !PLATFORM(WX)
4064     if (platformWidget())
4065         return false;
4066 #endif
4067
4068 #if ENABLE(THREADED_SCROLLING)
4069     if (Page* page = m_frame->page()) {
4070         if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator()) {
4071             if (scrollingCoordinator->coordinatesScrollingForFrameView(this))
4072                 return scrollingCoordinator->handleWheelEvent(this, wheelEvent);
4073         }
4074     }
4075 #endif
4076
4077     return ScrollableArea::handleWheelEvent(wheelEvent);
4078 }
4079
4080
4081 bool FrameView::isVerticalDocument() const
4082 {
4083     RenderView* renderView = this->renderView();
4084     if (!renderView)
4085         return true;
4086
4087     return renderView->style()->isHorizontalWritingMode();
4088 }
4089
4090 bool FrameView::isFlippedDocument() const
4091 {
4092     RenderView* renderView = this->renderView();
4093     if (!renderView)
4094         return false;
4095
4096     return renderView->style()->isFlippedBlocksWritingMode();
4097 }
4098
4099 void FrameView::notifyWidgetsInAllFrames(WidgetNotification notification)
4100 {
4101     for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext(m_frame.get())) {
4102         if (RenderView* root = frame->contentRenderer())
4103             root->notifyWidgets(notification);
4104     }
4105 }
4106     
4107 AXObjectCache* FrameView::axObjectCache() const
4108 {
4109     if (frame() && frame()->document())
4110         return frame()->document()->existingAXObjectCache();
4111     return 0;
4112 }
4113     
4114 void FrameView::setScrollingPerformanceLoggingEnabled(bool flag)
4115 {
4116 #if USE(ACCELERATED_COMPOSITING)
4117     if (TiledBacking* tiledBacking = this->tiledBacking())
4118         tiledBacking->setScrollingPerformanceLoggingEnabled(flag);
4119 #else
4120     UNUSED_PARAM(flag);
4121 #endif
4122 }
4123
4124 } // namespace WebCore