2010-12-02 Mike Lawther <mikelawther@chromium.org>
[WebKit-https.git] / 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 "CSSStyleSelector.h"
32 #include "CachedResourceLoader.h"
33 #include "Chrome.h"
34 #include "ChromeClient.h"
35 #include "EventHandler.h"
36 #include "FloatRect.h"
37 #include "FocusController.h"
38 #include "Frame.h"
39 #include "FrameLoader.h"
40 #include "FrameLoaderClient.h"
41 #include "FrameTree.h"
42 #include "GraphicsContext.h"
43 #include "HTMLDocument.h"
44 #include "HTMLFrameElement.h"
45 #include "HTMLFrameSetElement.h"
46 #include "HTMLNames.h"
47 #include "HTMLPlugInImageElement.h"
48 #include "InspectorInstrumentation.h"
49 #include "OverflowEvent.h"
50 #include "RenderEmbeddedObject.h"
51 #include "RenderLayer.h"
52 #include "RenderPart.h"
53 #include "RenderScrollbar.h"
54 #include "RenderScrollbarPart.h"
55 #include "RenderTheme.h"
56 #include "RenderView.h"
57 #include "Settings.h"
58 #include "TextResourceDecoder.h"
59 #include <wtf/CurrentTime.h>
60
61 #if USE(ACCELERATED_COMPOSITING)
62 #include "RenderLayerCompositor.h"
63 #endif
64
65 #if ENABLE(SVG)
66 #include "SVGDocument.h"
67 #include "SVGLocatable.h"
68 #include "SVGNames.h"
69 #include "SVGPreserveAspectRatio.h"
70 #include "SVGSVGElement.h"
71 #include "SVGViewElement.h"
72 #include "SVGViewSpec.h"
73 #endif
74
75 #if ENABLE(TILED_BACKING_STORE)
76 #include "TiledBackingStore.h"
77 #endif
78
79 namespace WebCore {
80
81 using namespace HTMLNames;
82
83 double FrameView::sCurrentPaintTimeStamp = 0.0;
84
85 // REPAINT_THROTTLING now chooses default values for throttling parameters.
86 // Should be removed when applications start using runtime configuration.
87 #if ENABLE(REPAINT_THROTTLING)
88 // Normal delay
89 double FrameView::s_deferredRepaintDelay = 0.025;
90 // Negative value would mean that first few repaints happen without a delay
91 double FrameView::s_initialDeferredRepaintDelayDuringLoading = 0;
92 // The delay grows on each repaint to this maximum value
93 double FrameView::s_maxDeferredRepaintDelayDuringLoading = 2.5;
94 // On each repaint the delay increses by this amount
95 double FrameView::s_deferredRepaintDelayIncrementDuringLoading = 0.5;
96 #else
97 // FIXME: Repaint throttling could be good to have on all platform.
98 // The balance between CPU use and repaint frequency will need some tuning for desktop.
99 // More hooks may be needed to reset the delay on things like GIF and CSS animations.
100 double FrameView::s_deferredRepaintDelay = 0;
101 double FrameView::s_initialDeferredRepaintDelayDuringLoading = 0;
102 double FrameView::s_maxDeferredRepaintDelayDuringLoading = 0;
103 double FrameView::s_deferredRepaintDelayIncrementDuringLoading = 0;
104 #endif
105
106 // The maximum number of updateWidgets iterations that should be done before returning.
107 static const unsigned maxUpdateWidgetsIterations = 2;
108
109 struct ScheduledEvent : Noncopyable {
110     RefPtr<Event> m_event;
111     RefPtr<Node> m_eventTarget;
112 };
113
114 FrameView::FrameView(Frame* frame)
115     : m_frame(frame)
116     , m_canHaveScrollbars(true)
117     , m_slowRepaintObjectCount(0)
118     , m_fixedObjectCount(0)
119     , m_layoutTimer(this, &FrameView::layoutTimerFired)
120     , m_layoutRoot(0)
121     , m_hasPendingPostLayoutTasks(false)
122     , m_inSynchronousPostLayout(false)
123     , m_postLayoutTasksTimer(this, &FrameView::postLayoutTimerFired)
124     , m_isTransparent(false)
125     , m_baseBackgroundColor(Color::white)
126     , m_mediaType("screen")
127     , m_enqueueEvents(0)
128     , m_overflowStatusDirty(true)
129     , m_viewportRenderer(0)
130     , m_wasScrolledByUser(false)
131     , m_inProgrammaticScroll(false)
132     , m_deferredRepaintTimer(this, &FrameView::deferredRepaintTimerFired)
133     , m_shouldUpdateWhileOffscreen(true)
134     , m_deferSetNeedsLayouts(0)
135     , m_setNeedsLayoutWasDeferred(false)
136     , m_scrollCorner(0)
137 {
138     init();
139 }
140
141 PassRefPtr<FrameView> FrameView::create(Frame* frame)
142 {
143     RefPtr<FrameView> view = adoptRef(new FrameView(frame));
144     view->show();
145     return view.release();
146 }
147
148 PassRefPtr<FrameView> FrameView::create(Frame* frame, const IntSize& initialSize)
149 {
150     RefPtr<FrameView> view = adoptRef(new FrameView(frame));
151     view->Widget::setFrameRect(IntRect(view->pos(), initialSize));
152     view->show();
153     return view.release();
154 }
155
156 FrameView::~FrameView()
157 {
158     if (m_hasPendingPostLayoutTasks) {
159         m_postLayoutTasksTimer.stop();
160         m_scheduledEvents.clear();
161         m_enqueueEvents = 0;
162     }
163
164     resetScrollbars();
165
166     // Custom scrollbars should already be destroyed at this point
167     ASSERT(!horizontalScrollbar() || !horizontalScrollbar()->isCustomScrollbar());
168     ASSERT(!verticalScrollbar() || !verticalScrollbar()->isCustomScrollbar());
169
170     setHasHorizontalScrollbar(false); // Remove native scrollbars now before we lose the connection to the HostWindow.
171     setHasVerticalScrollbar(false);
172     
173     ASSERT(!m_scrollCorner);
174     ASSERT(m_scheduledEvents.isEmpty());
175     ASSERT(!m_enqueueEvents);
176
177     if (m_frame) {
178         ASSERT(m_frame->view() != this || !m_frame->contentRenderer());
179         RenderPart* renderer = m_frame->ownerRenderer();
180         if (renderer && renderer->widget() == this)
181             renderer->setWidget(0);
182     }
183 }
184
185 void FrameView::reset()
186 {
187     m_useSlowRepaints = false;
188     m_isOverlapped = false;
189     m_contentIsOpaque = false;
190     m_borderX = 30;
191     m_borderY = 30;
192     m_layoutTimer.stop();
193     m_layoutRoot = 0;
194     m_delayedLayout = false;
195     m_doFullRepaint = true;
196     m_layoutSchedulingEnabled = true;
197     m_inLayout = false;
198     m_inSynchronousPostLayout = false;
199     m_hasPendingPostLayoutTasks = false;
200     m_layoutCount = 0;
201     m_nestedLayoutCount = 0;
202     m_postLayoutTasksTimer.stop();
203     m_firstLayout = true;
204     m_firstLayoutCallbackPending = false;
205     m_wasScrolledByUser = false;
206     m_lastLayoutSize = IntSize();
207     m_lastZoomFactor = 1.0f;
208     m_deferringRepaints = 0;
209     m_repaintCount = 0;
210     m_repaintRects.clear();
211     m_deferredRepaintDelay = s_initialDeferredRepaintDelayDuringLoading;
212     m_deferredRepaintTimer.stop();
213     m_lastPaintTime = 0;
214     m_paintBehavior = PaintBehaviorNormal;
215     m_isPainting = false;
216     m_isVisuallyNonEmpty = false;
217     m_firstVisuallyNonEmptyLayoutCallbackPending = true;
218     m_maintainScrollPositionAnchor = 0;
219 }
220
221 bool FrameView::isFrameView() const 
222
223     return true; 
224 }
225
226 void FrameView::clearFrame()
227 {
228     m_frame = 0;
229 }
230
231 void FrameView::resetScrollbars()
232 {
233     // Reset the document's scrollbars back to our defaults before we yield the floor.
234     m_firstLayout = true;
235     setScrollbarsSuppressed(true);
236     if (m_canHaveScrollbars)
237         setScrollbarModes(ScrollbarAuto, ScrollbarAuto);
238     else
239         setScrollbarModes(ScrollbarAlwaysOff, ScrollbarAlwaysOff);
240     setScrollbarsSuppressed(false);
241 }
242
243 void FrameView::init()
244 {
245     reset();
246
247     m_margins = IntSize(-1, -1); // undefined
248     m_size = IntSize();
249
250     // Propagate the marginwidth/height and scrolling modes to the view.
251     Element* ownerElement = m_frame && m_frame->document() ? m_frame->document()->ownerElement() : 0;
252     if (ownerElement && (ownerElement->hasTagName(frameTag) || ownerElement->hasTagName(iframeTag))) {
253         HTMLFrameElement* frameElt = static_cast<HTMLFrameElement*>(ownerElement);
254         if (frameElt->scrollingMode() == ScrollbarAlwaysOff)
255             setCanHaveScrollbars(false);
256         int marginWidth = frameElt->getMarginWidth();
257         int marginHeight = frameElt->getMarginHeight();
258         if (marginWidth != -1)
259             setMarginWidth(marginWidth);
260         if (marginHeight != -1)
261             setMarginHeight(marginHeight);
262     }
263 }
264
265 void FrameView::detachCustomScrollbars()
266 {
267     if (!m_frame)
268         return;
269
270     Scrollbar* horizontalBar = horizontalScrollbar();
271     if (horizontalBar && horizontalBar->isCustomScrollbar())
272         setHasHorizontalScrollbar(false);
273
274     Scrollbar* verticalBar = verticalScrollbar();
275     if (verticalBar && verticalBar->isCustomScrollbar())
276         setHasVerticalScrollbar(false);
277
278     if (m_scrollCorner) {
279         m_scrollCorner->destroy();
280         m_scrollCorner = 0;
281     }
282 }
283
284 void FrameView::clear()
285 {
286     setCanBlitOnScroll(true);
287     
288     reset();
289
290     if (m_frame) {
291         if (RenderPart* renderer = m_frame->ownerRenderer())
292             renderer->viewCleared();
293     }
294
295     setScrollbarsSuppressed(true);
296 }
297
298 bool FrameView::didFirstLayout() const
299 {
300     return !m_firstLayout;
301 }
302
303 void FrameView::invalidateRect(const IntRect& rect)
304 {
305     if (!parent()) {
306         if (hostWindow() && shouldUpdate())
307             hostWindow()->invalidateContentsAndWindow(rect, false /*immediate*/);
308         return;
309     }
310
311     if (!m_frame)
312         return;
313
314     RenderPart* renderer = m_frame->ownerRenderer();
315     if (!renderer)
316         return;
317
318     IntRect repaintRect = rect;
319     repaintRect.move(renderer->borderLeft() + renderer->paddingLeft(),
320                      renderer->borderTop() + renderer->paddingTop());
321     renderer->repaintRectangle(repaintRect);
322 }
323
324 void FrameView::setFrameRect(const IntRect& newRect)
325 {
326     IntRect oldRect = frameRect();
327     if (newRect == oldRect)
328         return;
329
330     ScrollView::setFrameRect(newRect);
331
332 #if USE(ACCELERATED_COMPOSITING)
333     if (RenderView* root = m_frame->contentRenderer()) {
334         if (root->usesCompositing())
335             root->compositor()->frameViewDidChangeSize();
336     }
337 #endif
338 }
339
340 void FrameView::setMarginWidth(int w)
341 {
342     // make it update the rendering area when set
343     m_margins.setWidth(w);
344 }
345
346 void FrameView::setMarginHeight(int h)
347 {
348     // make it update the rendering area when set
349     m_margins.setHeight(h);
350 }
351
352 bool FrameView::avoidScrollbarCreation() const
353 {
354     ASSERT(m_frame);
355
356     // with frame flattening no subframe can have scrollbars
357     // but we also cannot turn scrollbars of as we determine
358     // our flattening policy using that.
359
360     if (!m_frame->ownerElement())
361         return false;
362
363     if (!m_frame->settings() || m_frame->settings()->frameFlatteningEnabled())
364         return true;
365
366     return false;
367 }
368
369 void FrameView::setCanHaveScrollbars(bool canHaveScrollbars)
370 {
371     m_canHaveScrollbars = canHaveScrollbars;
372     ScrollView::setCanHaveScrollbars(canHaveScrollbars);
373 }
374
375 void FrameView::updateCanHaveScrollbars()
376 {
377     ScrollbarMode hMode;
378     ScrollbarMode vMode;
379     scrollbarModes(hMode, vMode);
380     if (hMode == ScrollbarAlwaysOff && vMode == ScrollbarAlwaysOff)
381         setCanHaveScrollbars(false);
382     else
383         setCanHaveScrollbars(true);
384 }
385
386 PassRefPtr<Scrollbar> FrameView::createScrollbar(ScrollbarOrientation orientation)
387 {
388     // FIXME: We need to update the scrollbar dynamically as documents change (or as doc elements and bodies get discovered that have custom styles).
389     Document* doc = m_frame->document();
390
391     // Try the <body> element first as a scrollbar source.
392     Element* body = doc ? doc->body() : 0;
393     if (body && body->renderer() && body->renderer()->style()->hasPseudoStyle(SCROLLBAR))
394         return RenderScrollbar::createCustomScrollbar(this, orientation, body->renderer()->enclosingBox());
395     
396     // If the <body> didn't have a custom style, then the root element might.
397     Element* docElement = doc ? doc->documentElement() : 0;
398     if (docElement && docElement->renderer() && docElement->renderer()->style()->hasPseudoStyle(SCROLLBAR))
399         return RenderScrollbar::createCustomScrollbar(this, orientation, docElement->renderBox());
400         
401     // If we have an owning iframe/frame element, then it can set the custom scrollbar also.
402     RenderPart* frameRenderer = m_frame->ownerRenderer();
403     if (frameRenderer && frameRenderer->style()->hasPseudoStyle(SCROLLBAR))
404         return RenderScrollbar::createCustomScrollbar(this, orientation, 0, m_frame.get());
405     
406     // Nobody set a custom style, so we just use a native scrollbar.
407     return ScrollView::createScrollbar(orientation);
408 }
409
410 void FrameView::setContentsSize(const IntSize& size)
411 {
412     if (size == contentsSize())
413         return;
414
415     m_deferSetNeedsLayouts++;
416
417     ScrollView::setContentsSize(size);
418
419     Page* page = frame() ? frame()->page() : 0;
420     if (!page)
421         return;
422
423     page->chrome()->contentsSizeChanged(frame(), size); //notify only
424
425     m_deferSetNeedsLayouts--;
426     
427     if (!m_deferSetNeedsLayouts)
428         m_setNeedsLayoutWasDeferred = false; // FIXME: Find a way to make the deferred layout actually happen.
429 }
430
431 void FrameView::adjustViewSize()
432 {
433     ASSERT(m_frame->view() == this);
434     RenderView* root = m_frame->contentRenderer();
435     if (!root)
436         return;
437
438     IntSize size = IntSize(root->rightLayoutOverflow() - root->leftLayoutOverflow(), root->bottomLayoutOverflow() - root->topLayoutOverflow());
439
440     ScrollView::setScrollOrigin(IntPoint(-root->leftLayoutOverflow(), -root->topLayoutOverflow()), size == contentsSize());
441     
442     setContentsSize(size);
443 }
444
445 void FrameView::applyOverflowToViewport(RenderObject* o, ScrollbarMode& hMode, ScrollbarMode& vMode)
446 {
447     // Handle the overflow:hidden/scroll case for the body/html elements.  WinIE treats
448     // overflow:hidden and overflow:scroll on <body> as applying to the document's
449     // scrollbars.  The CSS2.1 draft states that HTML UAs should use the <html> or <body> element and XML/XHTML UAs should
450     // use the root element.
451     switch (o->style()->overflowX()) {
452         case OHIDDEN:
453             hMode = ScrollbarAlwaysOff;
454             break;
455         case OSCROLL:
456             hMode = ScrollbarAlwaysOn;
457             break;
458         case OAUTO:
459             hMode = ScrollbarAuto;
460             break;
461         default:
462             // Don't set it at all.
463             ;
464     }
465     
466      switch (o->style()->overflowY()) {
467         case OHIDDEN:
468             vMode = ScrollbarAlwaysOff;
469             break;
470         case OSCROLL:
471             vMode = ScrollbarAlwaysOn;
472             break;
473         case OAUTO:
474             vMode = ScrollbarAuto;
475             break;
476         default:
477             // Don't set it at all.
478             ;
479     }
480
481     m_viewportRenderer = o;
482 }
483
484 void FrameView::calculateScrollbarModesForLayout(ScrollbarMode& hMode, ScrollbarMode& vMode)
485 {
486     const HTMLFrameOwnerElement* owner = m_frame->ownerElement();
487     if (owner && (owner->scrollingMode() == ScrollbarAlwaysOff)) {
488         hMode = ScrollbarAlwaysOff;
489         vMode = ScrollbarAlwaysOff;
490         return;
491     }  
492     
493     if (m_canHaveScrollbars) {
494         hMode = ScrollbarAuto;
495         vMode = ScrollbarAuto;
496     } else {
497         hMode = ScrollbarAlwaysOff;
498         vMode = ScrollbarAlwaysOff;
499     }
500     
501     if (!m_layoutRoot) {
502         Document* document = m_frame->document();
503         Node* documentElement = document->documentElement();
504         RenderObject* rootRenderer = documentElement ? documentElement->renderer() : 0;
505         Node* body = document->body();
506         if (body && body->renderer()) {
507             if (body->hasTagName(framesetTag) && m_frame->settings() && !m_frame->settings()->frameFlatteningEnabled()) {
508                 vMode = ScrollbarAlwaysOff;
509                 hMode = ScrollbarAlwaysOff;
510             } else if (body->hasTagName(bodyTag)) {
511                 // It's sufficient to just check the X overflow,
512                 // since it's illegal to have visible in only one direction.
513                 RenderObject* o = rootRenderer->style()->overflowX() == OVISIBLE && document->documentElement()->hasTagName(htmlTag) ? body->renderer() : rootRenderer;
514                 applyOverflowToViewport(o, hMode, vMode);
515             }
516         } else if (rootRenderer) {
517 #if ENABLE(SVG)
518             if (!documentElement->isSVGElement()) {
519                 applyOverflowToViewport(rootRenderer, hMode, vMode);
520 #else
521             applyOverflowToViewport(rootRenderer, hMode, vMode);
522 #endif
523         }
524     }    
525 }
526 }
527     
528 #if USE(ACCELERATED_COMPOSITING)
529 void FrameView::updateCompositingLayers()
530 {
531     RenderView* view = m_frame->contentRenderer();
532     if (!view)
533         return;
534
535     // This call will make sure the cached hasAcceleratedCompositing is updated from the pref
536     view->compositor()->cacheAcceleratedCompositingFlags();
537     view->compositor()->updateCompositingLayers(CompositingUpdateAfterLayoutOrStyleChange);
538 }
539
540 void FrameView::setNeedsOneShotDrawingSynchronization()
541 {
542     Page* page = frame() ? frame()->page() : 0;
543     if (page)
544         page->chrome()->client()->setNeedsOneShotDrawingSynchronization();
545 }
546
547 #endif // USE(ACCELERATED_COMPOSITING)
548
549 bool FrameView::hasCompositedContent() const
550 {
551 #if USE(ACCELERATED_COMPOSITING)
552     if (RenderView* view = m_frame->contentRenderer())
553         return view->compositor()->inCompositingMode();
554 #endif
555     return false;
556 }
557
558 bool FrameView::hasCompositedContentIncludingDescendants() const
559 {
560 #if USE(ACCELERATED_COMPOSITING)
561     for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext(m_frame.get())) {
562         RenderView* renderView = frame->contentRenderer();
563         RenderLayerCompositor* compositor = renderView ? renderView->compositor() : 0;
564         if (compositor) {
565             if (compositor->inCompositingMode())
566                 return true;
567
568             if (!RenderLayerCompositor::allowsIndependentlyCompositedIFrames(this))
569                 break;
570         }
571     }
572 #endif
573     return false;
574 }
575
576 bool FrameView::hasCompositingAncestor() const
577 {
578 #if USE(ACCELERATED_COMPOSITING)
579     for (Frame* frame = m_frame->tree()->parent(); frame; frame = frame->tree()->parent()) {
580         if (FrameView* view = frame->view()) {
581             if (view->hasCompositedContent())
582                 return true;
583         }
584     }
585 #endif
586     return false;
587 }
588
589 // Sometimes (for plug-ins) we need to eagerly go into compositing mode.
590 void FrameView::enterCompositingMode()
591 {
592 #if USE(ACCELERATED_COMPOSITING)
593     if (RenderView* view = m_frame->contentRenderer()) {
594         view->compositor()->enableCompositingMode();
595         if (!needsLayout())
596             view->compositor()->scheduleCompositingLayerUpdate();
597     }
598 #endif
599 }
600
601 bool FrameView::isEnclosedInCompositingLayer() const
602 {
603 #if USE(ACCELERATED_COMPOSITING)
604     RenderObject* frameOwnerRenderer = m_frame->ownerRenderer();
605     if (frameOwnerRenderer && frameOwnerRenderer->containerForRepaint())
606         return true;
607
608     if (Frame* parentFrame = m_frame->tree()->parent()) {
609         if (FrameView* parentView = parentFrame->view())
610             return parentView->isEnclosedInCompositingLayer();
611     }
612 #endif
613     return false;
614 }
615
616 bool FrameView::syncCompositingStateRecursive()
617 {
618 #if USE(ACCELERATED_COMPOSITING)
619     ASSERT(m_frame->view() == this);
620     RenderView* contentRenderer = m_frame->contentRenderer();
621     if (!contentRenderer)
622         return true;    // We don't want to keep trying to update layers if we have no renderer.
623
624     // If we sync compositing layers when a layout is pending, we may cause painting of compositing
625     // layer content to occur before layout has happened, which will cause paintContents() to bail.
626     if (needsLayout())
627         return false;
628     
629     if (GraphicsLayer* rootLayer = contentRenderer->compositor()->rootPlatformLayer())
630         rootLayer->syncCompositingState();
631
632     bool allSubframesSynced = true;
633     const HashSet<RefPtr<Widget> >* viewChildren = children();
634     HashSet<RefPtr<Widget> >::const_iterator end = viewChildren->end();
635     for (HashSet<RefPtr<Widget> >::const_iterator current = viewChildren->begin(); current != end; ++current) {
636         Widget* widget = (*current).get();
637         if (widget->isFrameView()) {
638             bool synced = static_cast<FrameView*>(widget)->syncCompositingStateRecursive();
639             allSubframesSynced &= synced;
640         }
641     }
642     return allSubframesSynced;
643 #else // USE(ACCELERATED_COMPOSITING)
644     return true;
645 #endif
646 }
647
648 bool FrameView::isSoftwareRenderable() const
649 {
650 #if USE(ACCELERATED_COMPOSITING)
651     RenderView* view = m_frame->contentRenderer();
652     if (!view)
653         return true;
654
655     return !view->compositor()->has3DContent();
656 #else
657     return true;
658 #endif
659 }
660
661 void FrameView::didMoveOnscreen()
662 {
663     RenderView* view = m_frame->contentRenderer();
664     if (view)
665         view->didMoveOnscreen();
666 }
667
668 void FrameView::willMoveOffscreen()
669 {
670     RenderView* view = m_frame->contentRenderer();
671     if (view)
672         view->willMoveOffscreen();
673 }
674
675 RenderObject* FrameView::layoutRoot(bool onlyDuringLayout) const
676 {
677     return onlyDuringLayout && layoutPending() ? 0 : m_layoutRoot;
678 }
679
680 void FrameView::layout(bool allowSubtree)
681 {
682     if (m_inLayout)
683         return;
684
685     m_layoutTimer.stop();
686     m_delayedLayout = false;
687     m_setNeedsLayoutWasDeferred = false;
688
689     // Protect the view from being deleted during layout (in recalcStyle)
690     RefPtr<FrameView> protector(this);
691
692     if (!m_frame) {
693         // FIXME: Do we need to set m_size.width here?
694         // FIXME: Should we set m_size.height here too?
695         m_size.setWidth(layoutWidth());
696         return;
697     }
698     
699     // we shouldn't enter layout() while painting
700     ASSERT(!isPainting());
701     if (isPainting())
702         return;
703
704     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willLayout(m_frame.get());
705
706     if (!allowSubtree && m_layoutRoot) {
707         m_layoutRoot->markContainingBlocksForLayout(false);
708         m_layoutRoot = 0;
709     }
710
711     ASSERT(m_frame->view() == this);
712
713     Document* document = m_frame->document();
714
715     m_layoutSchedulingEnabled = false;
716
717     if (!m_nestedLayoutCount && !m_inSynchronousPostLayout && m_hasPendingPostLayoutTasks) {
718         // This is a new top-level layout. If there are any remaining tasks from the previous
719         // layout, finish them now.
720         m_inSynchronousPostLayout = true;
721         m_postLayoutTasksTimer.stop();
722         performPostLayoutTasks();
723         m_inSynchronousPostLayout = false;
724     }
725
726     // Viewport-dependent media queries may cause us to need completely different style information.
727     // Check that here.
728     if (document->styleSelector()->affectedByViewportChange())
729         document->styleSelectorChanged(RecalcStyleImmediately);
730
731     // Always ensure our style info is up-to-date.  This can happen in situations where
732     // the layout beats any sort of style recalc update that needs to occur.
733     document->updateStyleIfNeeded();
734     
735     bool subtree = m_layoutRoot;
736
737     // If there is only one ref to this view left, then its going to be destroyed as soon as we exit, 
738     // so there's no point to continuing to layout
739     if (protector->hasOneRef())
740         return;
741
742     RenderObject* root = subtree ? m_layoutRoot : document->renderer();
743     if (!root) {
744         // FIXME: Do we need to set m_size here?
745         m_layoutSchedulingEnabled = true;
746         return;
747     }
748
749     m_nestedLayoutCount++;
750
751     if (!m_layoutRoot) {
752         Document* document = m_frame->document();
753         Node* documentElement = document->documentElement();
754         RenderObject* rootRenderer = documentElement ? documentElement->renderer() : 0;
755         Node* body = document->body();
756         if (body && body->renderer()) {
757             if (body->hasTagName(framesetTag) && m_frame->settings() && !m_frame->settings()->frameFlatteningEnabled()) {
758                 body->renderer()->setChildNeedsLayout(true);
759             } else if (body->hasTagName(bodyTag)) {
760                 if (!m_firstLayout && m_size.height() != layoutHeight() && body->renderer()->enclosingBox()->stretchesToViewport())
761                     body->renderer()->setChildNeedsLayout(true);
762             }
763         } else if (rootRenderer) {
764 #if ENABLE(SVG)
765             if (documentElement->isSVGElement()) {
766                 if (!m_firstLayout && (m_size.width() != layoutWidth() || m_size.height() != layoutHeight()))
767                     rootRenderer->setChildNeedsLayout(true);
768             }
769 #endif
770         }
771         
772 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
773         if (m_firstLayout && !document->ownerElement())
774             printf("Elapsed time before first layout: %d\n", document->elapsedTime());
775 #endif        
776     }
777     
778     ScrollbarMode hMode;
779     ScrollbarMode vMode;    
780     calculateScrollbarModesForLayout(hMode, vMode);
781
782     m_doFullRepaint = !subtree && (m_firstLayout || toRenderView(root)->printing());
783
784     if (!subtree) {
785         // Now set our scrollbar state for the layout.
786         ScrollbarMode currentHMode = horizontalScrollbarMode();
787         ScrollbarMode currentVMode = verticalScrollbarMode();
788
789         if (m_firstLayout || (hMode != currentHMode || vMode != currentVMode)) {
790             if (m_firstLayout) {
791                 setScrollbarsSuppressed(true);
792
793                 m_firstLayout = false;
794                 m_firstLayoutCallbackPending = true;
795                 m_lastLayoutSize = IntSize(width(), height());
796                 m_lastZoomFactor = root->style()->zoom();
797
798                 // Set the initial vMode to AlwaysOn if we're auto.
799                 if (vMode == ScrollbarAuto)
800                     setVerticalScrollbarMode(ScrollbarAlwaysOn); // This causes a vertical scrollbar to appear.
801                 // Set the initial hMode to AlwaysOff if we're auto.
802                 if (hMode == ScrollbarAuto)
803                     setHorizontalScrollbarMode(ScrollbarAlwaysOff); // This causes a horizontal scrollbar to disappear.
804
805                 setScrollbarModes(hMode, vMode);
806                 setScrollbarsSuppressed(false, true);
807             } else
808                 setScrollbarModes(hMode, vMode);
809         }
810
811         IntSize oldSize = m_size;
812
813         m_size = IntSize(layoutWidth(), layoutHeight());
814
815         if (oldSize != m_size) {
816             m_doFullRepaint = true;
817             if (!m_firstLayout) {
818                 RenderBox* rootRenderer = document->documentElement() ? document->documentElement()->renderBox() : 0;
819                 RenderBox* bodyRenderer = rootRenderer && document->body() ? document->body()->renderBox() : 0;
820                 if (bodyRenderer && bodyRenderer->stretchesToViewport())
821                     bodyRenderer->setChildNeedsLayout(true);
822                 else if (rootRenderer && rootRenderer->stretchesToViewport())
823                     rootRenderer->setChildNeedsLayout(true);
824             }
825         }
826     }
827
828     RenderLayer* layer = root->enclosingLayer();
829
830     pauseScheduledEvents();
831
832     bool disableLayoutState = false;
833     if (subtree) {
834         RenderView* view = root->view();
835         disableLayoutState = view->shouldDisableLayoutStateForSubtree(root);
836         view->pushLayoutState(root);
837         if (disableLayoutState)
838             view->disableLayoutState();
839     }
840         
841     m_inLayout = true;
842     beginDeferredRepaints();
843     root->layout();
844     endDeferredRepaints();
845     m_inLayout = false;
846
847     if (subtree) {
848         RenderView* view = root->view();
849         view->popLayoutState(root);
850         if (disableLayoutState)
851             view->enableLayoutState();
852     }
853     m_layoutRoot = 0;
854
855     m_frame->selection()->setCaretRectNeedsUpdate();
856     m_frame->selection()->updateAppearance();
857    
858     m_layoutSchedulingEnabled = true;
859
860     if (!subtree && !toRenderView(root)->printing())
861         adjustViewSize();
862
863     // Now update the positions of all layers.
864     beginDeferredRepaints();
865     IntPoint cachedOffset;
866     layer->updateLayerPositions((m_doFullRepaint ? RenderLayer::DoFullRepaint : 0)
867                                 | RenderLayer::CheckForRepaint
868                                 | RenderLayer::IsCompositingUpdateRoot
869                                 | RenderLayer::UpdateCompositingLayers,
870                                 subtree ? 0 : &cachedOffset);
871     endDeferredRepaints();
872
873 #if USE(ACCELERATED_COMPOSITING)
874     updateCompositingLayers();
875 #endif
876     
877     m_layoutCount++;
878
879 #if PLATFORM(MAC) || PLATFORM(CHROMIUM)
880     if (AXObjectCache::accessibilityEnabled())
881         root->document()->axObjectCache()->postNotification(root, AXObjectCache::AXLayoutComplete, true);
882 #endif
883 #if ENABLE(DASHBOARD_SUPPORT)
884     updateDashboardRegions();
885 #endif
886
887     ASSERT(!root->needsLayout());
888
889     updateCanBlitOnScrollRecursively();
890
891     if (document->hasListenerType(Document::OVERFLOWCHANGED_LISTENER))
892         updateOverflowStatus(layoutWidth() < contentsWidth(),
893                              layoutHeight() < contentsHeight());
894
895     if (!m_hasPendingPostLayoutTasks) {
896         if (!m_inSynchronousPostLayout) {
897             m_inSynchronousPostLayout = true;
898             // Calls resumeScheduledEvents()
899             performPostLayoutTasks();
900             m_inSynchronousPostLayout = false;
901         }
902
903         if (!m_hasPendingPostLayoutTasks && (needsLayout() || m_inSynchronousPostLayout)) {
904             // If we need layout or are already in a synchronous call to postLayoutTasks(), 
905             // defer widget updates and event dispatch until after we return. postLayoutTasks()
906             // can make us need to update again, and we can get stuck in a nasty cycle unless
907             // we call it through the timer here.
908             m_hasPendingPostLayoutTasks = true;
909             m_postLayoutTasksTimer.startOneShot(0);
910             if (needsLayout()) {
911                 pauseScheduledEvents();
912                 layout();
913             }
914         }
915     } else {
916         resumeScheduledEvents();
917         ASSERT(m_enqueueEvents);
918     }
919
920     InspectorInstrumentation::didLayout(cookie);
921
922     m_nestedLayoutCount--;
923 }
924
925 void FrameView::addWidgetToUpdate(RenderEmbeddedObject* object)
926 {
927     if (!m_widgetUpdateSet)
928         m_widgetUpdateSet.set(new RenderEmbeddedObjectSet);
929
930     m_widgetUpdateSet->add(object);
931 }
932
933 void FrameView::removeWidgetToUpdate(RenderEmbeddedObject* object)
934 {
935     if (!m_widgetUpdateSet)
936         return;
937
938     m_widgetUpdateSet->remove(object);
939 }
940
941 void FrameView::setMediaType(const String& mediaType)
942 {
943     m_mediaType = mediaType;
944 }
945
946 String FrameView::mediaType() const
947 {
948     // See if we have an override type.
949     String overrideType = m_frame->loader()->client()->overrideMediaType();
950     if (!overrideType.isNull())
951         return overrideType;
952     return m_mediaType;
953 }
954
955 void FrameView::adjustMediaTypeForPrinting(bool printing)
956 {
957     if (printing) {
958         if (m_mediaTypeWhenNotPrinting.isNull())
959             m_mediaTypeWhenNotPrinting = mediaType();
960             setMediaType("print");
961     } else {
962         if (!m_mediaTypeWhenNotPrinting.isNull())
963             setMediaType(m_mediaTypeWhenNotPrinting);
964         m_mediaTypeWhenNotPrinting = String();
965     }
966 }
967
968 bool FrameView::useSlowRepaints() const
969 {
970     if (m_useSlowRepaints || m_slowRepaintObjectCount > 0 || (platformWidget() && m_fixedObjectCount > 0) || m_isOverlapped || !m_contentIsOpaque)
971         return true;
972
973     if (Frame* parentFrame = m_frame->tree()->parent()) {
974         if (FrameView* parentView = parentFrame->view())
975             return parentView->useSlowRepaints();
976     }
977
978     return false;
979 }
980
981 bool FrameView::useSlowRepaintsIfNotOverlapped() const
982 {
983     if (m_useSlowRepaints || m_slowRepaintObjectCount > 0 || (platformWidget() && m_fixedObjectCount > 0) || !m_contentIsOpaque)
984         return true;
985
986     if (Frame* parentFrame = m_frame->tree()->parent()) {
987         if (FrameView* parentView = parentFrame->view())
988             return parentView->useSlowRepaintsIfNotOverlapped();
989     }
990
991     return false;
992 }
993
994 void FrameView::updateCanBlitOnScrollRecursively()
995 {
996     for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext(m_frame.get())) {
997         if (FrameView* view = frame->view())
998             view->setCanBlitOnScroll(!view->useSlowRepaints());
999     }
1000 }
1001
1002 void FrameView::setUseSlowRepaints()
1003 {
1004     m_useSlowRepaints = true;
1005     updateCanBlitOnScrollRecursively();
1006 }
1007
1008 void FrameView::addSlowRepaintObject()
1009 {
1010     if (!m_slowRepaintObjectCount)
1011         updateCanBlitOnScrollRecursively();
1012     m_slowRepaintObjectCount++;
1013 }
1014
1015 void FrameView::removeSlowRepaintObject()
1016 {
1017     ASSERT(m_slowRepaintObjectCount > 0);
1018     m_slowRepaintObjectCount--;
1019     if (!m_slowRepaintObjectCount)
1020         updateCanBlitOnScrollRecursively();
1021 }
1022
1023 void FrameView::addFixedObject()
1024 {
1025     if (!m_fixedObjectCount && platformWidget())
1026         updateCanBlitOnScrollRecursively();
1027     ++m_fixedObjectCount;
1028 }
1029
1030 void FrameView::removeFixedObject()
1031 {
1032     ASSERT(m_fixedObjectCount > 0);
1033     --m_fixedObjectCount;
1034     if (!m_fixedObjectCount)
1035         updateCanBlitOnScrollRecursively();
1036 }
1037
1038 bool FrameView::scrollContentsFastPath(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect)
1039 {
1040     const size_t fixedObjectThreshold = 5;
1041
1042     RenderBlock::PositionedObjectsListHashSet* positionedObjects = 0;
1043     if (RenderView* root = m_frame->contentRenderer())
1044         positionedObjects = root->positionedObjects();
1045
1046     if (!positionedObjects || positionedObjects->isEmpty()) {
1047         hostWindow()->scroll(scrollDelta, rectToScroll, clipRect);
1048         return true;
1049     }
1050
1051     // Get the rects of the fixed objects visible in the rectToScroll
1052     Vector<IntRect, fixedObjectThreshold> subRectToUpdate;
1053     bool updateInvalidatedSubRect = true;
1054     RenderBlock::PositionedObjectsListHashSet::const_iterator end = positionedObjects->end();
1055     for (RenderBlock::PositionedObjectsListHashSet::const_iterator it = positionedObjects->begin(); it != end; ++it) {
1056         RenderBox* renderBox = *it;
1057         if (renderBox->style()->position() != FixedPosition)
1058             continue;
1059         IntRect updateRect = renderBox->layer()->repaintRectIncludingDescendants();
1060         updateRect = contentsToWindow(updateRect);
1061
1062         updateRect.intersect(rectToScroll);
1063         if (!updateRect.isEmpty()) {
1064             if (subRectToUpdate.size() >= fixedObjectThreshold) {
1065                 updateInvalidatedSubRect = false;
1066                 break;
1067             }
1068             subRectToUpdate.append(updateRect);
1069         }
1070     }
1071
1072     // Scroll the view
1073     if (updateInvalidatedSubRect) {
1074         // 1) scroll
1075         hostWindow()->scroll(scrollDelta, rectToScroll, clipRect);
1076
1077         // 2) update the area of fixed objects that has been invalidated
1078         size_t fixObjectsCount = subRectToUpdate.size();
1079         for (size_t i = 0; i < fixObjectsCount; ++i) {
1080             IntRect updateRect = subRectToUpdate[i];
1081             IntRect scrolledRect = updateRect;
1082             scrolledRect.move(scrollDelta);
1083             updateRect.unite(scrolledRect);
1084             updateRect.intersect(rectToScroll);
1085             hostWindow()->invalidateContentsAndWindow(updateRect, false);
1086         }
1087         return true;
1088     }
1089
1090     // the number of fixed objects exceed the threshold, we cannot use the fast path
1091     return false;
1092 }
1093
1094 void FrameView::scrollContentsSlowPath(const IntRect& updateRect)
1095 {
1096 #if USE(ACCELERATED_COMPOSITING)
1097     if (RenderPart* frameRenderer = m_frame->ownerRenderer()) {
1098         if (frameRenderer->containerForRepaint()) {
1099             IntRect rect(frameRenderer->borderLeft() + frameRenderer->paddingLeft(),
1100                          frameRenderer->borderTop() + frameRenderer->paddingTop(),
1101                          visibleWidth(), visibleHeight());
1102             frameRenderer->repaintRectangle(rect);
1103             return;
1104         }
1105     }
1106 #endif
1107
1108     ScrollView::scrollContentsSlowPath(updateRect);
1109 }
1110
1111 // Note that this gets called at painting time.
1112 void FrameView::setIsOverlapped(bool isOverlapped)
1113 {
1114     if (isOverlapped == m_isOverlapped)
1115         return;
1116
1117     m_isOverlapped = isOverlapped;
1118     updateCanBlitOnScrollRecursively();
1119     
1120 #if USE(ACCELERATED_COMPOSITING)
1121     if (hasCompositedContentIncludingDescendants()) {
1122         // Overlap can affect compositing tests, so if it changes, we need to trigger
1123         // a layer update in the parent document.
1124         if (Frame* parentFrame = m_frame->tree()->parent()) {
1125             if (RenderView* parentView = parentFrame->contentRenderer()) {
1126                 RenderLayerCompositor* compositor = parentView->compositor();
1127                 compositor->setCompositingLayersNeedRebuild();
1128                 compositor->scheduleCompositingLayerUpdate();
1129             }
1130         }
1131
1132         if (RenderLayerCompositor::allowsIndependentlyCompositedIFrames(this)) {
1133             // We also need to trigger reevaluation for this and all descendant frames,
1134             // since a frame uses compositing if any ancestor is compositing.
1135             for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext(m_frame.get())) {
1136                 if (RenderView* view = frame->contentRenderer()) {
1137                     RenderLayerCompositor* compositor = view->compositor();
1138                     compositor->setCompositingLayersNeedRebuild();
1139                     compositor->scheduleCompositingLayerUpdate();
1140                 }
1141             }
1142         }
1143     }
1144 #endif
1145 }
1146
1147 bool FrameView::isOverlappedIncludingAncestors() const
1148 {
1149     if (isOverlapped())
1150         return true;
1151
1152     if (Frame* parentFrame = m_frame->tree()->parent()) {
1153         if (FrameView* parentView = parentFrame->view()) {
1154             if (parentView->isOverlapped())
1155                 return true;
1156         }
1157     }
1158
1159     return false;
1160 }
1161
1162 void FrameView::setContentIsOpaque(bool contentIsOpaque)
1163 {
1164     if (contentIsOpaque == m_contentIsOpaque)
1165         return;
1166
1167     m_contentIsOpaque = contentIsOpaque;
1168     updateCanBlitOnScrollRecursively();
1169 }
1170
1171 void FrameView::restoreScrollbar()
1172 {
1173     setScrollbarsSuppressed(false);
1174 }
1175
1176 bool FrameView::scrollToFragment(const KURL& url)
1177 {
1178     // If our URL has no ref, then we have no place we need to jump to.
1179     // OTOH If CSS target was set previously, we want to set it to 0, recalc
1180     // and possibly repaint because :target pseudo class may have been
1181     // set (see bug 11321).
1182     if (!url.hasFragmentIdentifier() && !m_frame->document()->cssTarget())
1183         return false;
1184
1185     String fragmentIdentifier = url.fragmentIdentifier();
1186     if (scrollToAnchor(fragmentIdentifier))
1187         return true;
1188
1189     // Try again after decoding the ref, based on the document's encoding.
1190     if (TextResourceDecoder* decoder = m_frame->document()->decoder())
1191         return scrollToAnchor(decodeURLEscapeSequences(fragmentIdentifier, decoder->encoding()));
1192
1193     return false;
1194 }
1195
1196 bool FrameView::scrollToAnchor(const String& name)
1197 {
1198     ASSERT(m_frame->document());
1199
1200     if (!m_frame->document()->haveStylesheetsLoaded()) {
1201         m_frame->document()->setGotoAnchorNeededAfterStylesheetsLoad(true);
1202         return false;
1203     }
1204
1205     m_frame->document()->setGotoAnchorNeededAfterStylesheetsLoad(false);
1206
1207     Element* anchorNode = m_frame->document()->findAnchor(name);
1208
1209 #if ENABLE(SVG)
1210     if (m_frame->document()->isSVGDocument()) {
1211         if (name.startsWith("xpointer(")) {
1212             // We need to parse the xpointer reference here
1213         } else if (name.startsWith("svgView(")) {
1214             RefPtr<SVGSVGElement> svg = static_cast<SVGDocument*>(m_frame->document())->rootElement();
1215             if (!svg->currentView()->parseViewSpec(name))
1216                 return false;
1217             svg->setUseCurrentView(true);
1218         } else {
1219             if (anchorNode && anchorNode->hasTagName(SVGNames::viewTag)) {
1220                 RefPtr<SVGViewElement> viewElement = anchorNode->hasTagName(SVGNames::viewTag) ? static_cast<SVGViewElement*>(anchorNode) : 0;
1221                 if (viewElement.get()) {
1222                     RefPtr<SVGSVGElement> svg = static_cast<SVGSVGElement*>(SVGLocatable::nearestViewportElement(viewElement.get()));
1223                     svg->inheritViewAttributes(viewElement.get());
1224                 }
1225             }
1226         }
1227         // FIXME: need to decide which <svg> to focus on, and zoom to that one
1228         // FIXME: need to actually "highlight" the viewTarget(s)
1229     }
1230 #endif
1231
1232     m_frame->document()->setCSSTarget(anchorNode); // Setting to null will clear the current target.
1233   
1234     // Implement the rule that "" and "top" both mean top of page as in other browsers.
1235     if (!anchorNode && !(name.isEmpty() || equalIgnoringCase(name, "top")))
1236         return false;
1237
1238     maintainScrollPositionAtAnchor(anchorNode ? static_cast<Node*>(anchorNode) : m_frame->document());
1239     return true;
1240 }
1241
1242 void FrameView::maintainScrollPositionAtAnchor(Node* anchorNode)
1243 {
1244     m_maintainScrollPositionAnchor = anchorNode;
1245     if (!m_maintainScrollPositionAnchor)
1246         return;
1247
1248     // We need to update the layout before scrolling, otherwise we could
1249     // really mess things up if an anchor scroll comes at a bad moment.
1250     m_frame->document()->updateStyleIfNeeded();
1251     // Only do a layout if changes have occurred that make it necessary.
1252     if (m_frame->contentRenderer() && m_frame->contentRenderer()->needsLayout())
1253         layout();
1254     else
1255         scrollToAnchor();
1256 }
1257
1258 void FrameView::setScrollPosition(const IntPoint& scrollPoint)
1259 {
1260     bool wasInProgrammaticScroll = m_inProgrammaticScroll;
1261     m_inProgrammaticScroll = true;
1262     m_maintainScrollPositionAnchor = 0;
1263     ScrollView::setScrollPosition(scrollPoint);
1264     m_inProgrammaticScroll = wasInProgrammaticScroll;
1265 }
1266
1267 void FrameView::scrollPositionChangedViaPlatformWidget()
1268 {
1269     repaintFixedElementsAfterScrolling();
1270     scrollPositionChanged();
1271 }
1272
1273 void FrameView::scrollPositionChanged()
1274 {
1275     frame()->eventHandler()->sendScrollEvent();
1276
1277 #if USE(ACCELERATED_COMPOSITING)
1278     if (RenderView* root = m_frame->contentRenderer()) {
1279         if (root->usesCompositing())
1280             root->compositor()->frameViewDidScroll(scrollPosition());
1281     }
1282 #endif
1283 }
1284
1285 void FrameView::repaintFixedElementsAfterScrolling()
1286 {
1287     // For fixed position elements, update widget positions and compositing layers after scrolling,
1288     // but only if we're not inside of layout.
1289     if (!m_nestedLayoutCount && hasFixedObjects()) {
1290         if (RenderView* root = m_frame->contentRenderer()) {
1291             root->updateWidgetPositions();
1292             root->layer()->updateRepaintRectsAfterScroll();
1293 #if USE(ACCELERATED_COMPOSITING)
1294             root->compositor()->updateCompositingLayers(CompositingUpdateOnScroll);
1295 #endif
1296         }
1297     }
1298 }
1299
1300 HostWindow* FrameView::hostWindow() const
1301 {
1302     Page* page = frame() ? frame()->page() : 0;
1303     if (!page)
1304         return 0;
1305     return page->chrome();
1306 }
1307
1308 const unsigned cRepaintRectUnionThreshold = 25;
1309
1310 void FrameView::repaintContentRectangle(const IntRect& r, bool immediate)
1311 {
1312     ASSERT(!m_frame->document()->ownerElement());
1313
1314     double delay = adjustedDeferredRepaintDelay();
1315     if ((m_deferringRepaints || m_deferredRepaintTimer.isActive() || delay) && !immediate) {
1316         IntRect paintRect = r;
1317         if (!paintsEntireContents())
1318             paintRect.intersect(visibleContentRect());
1319         if (paintRect.isEmpty())
1320             return;
1321         if (m_repaintCount == cRepaintRectUnionThreshold) {
1322             IntRect unionedRect;
1323             for (unsigned i = 0; i < cRepaintRectUnionThreshold; ++i)
1324                 unionedRect.unite(m_repaintRects[i]);
1325             m_repaintRects.clear();
1326             m_repaintRects.append(unionedRect);
1327         }
1328         if (m_repaintCount < cRepaintRectUnionThreshold)
1329             m_repaintRects.append(paintRect);
1330         else
1331             m_repaintRects[0].unite(paintRect);
1332         m_repaintCount++;
1333     
1334         if (!m_deferringRepaints && !m_deferredRepaintTimer.isActive())
1335              m_deferredRepaintTimer.startOneShot(delay);
1336         return;
1337     }
1338     
1339     if (!shouldUpdate(immediate))
1340         return;
1341
1342 #if ENABLE(TILED_BACKING_STORE)
1343     if (frame()->tiledBackingStore()) {
1344         frame()->tiledBackingStore()->invalidate(r);
1345         return;
1346     }
1347 #endif
1348     ScrollView::repaintContentRectangle(r, immediate);
1349 }
1350
1351 void FrameView::visibleContentsResized()
1352 {
1353     // We check to make sure the view is attached to a frame() as this method can
1354     // be triggered before the view is attached by Frame::createView(...) setting
1355     // various values such as setScrollBarModes(...) for example.  An ASSERT is
1356     // triggered when a view is layout before being attached to a frame().
1357     if (!frame()->view())
1358         return;
1359
1360     if (needsLayout())
1361         layout();
1362 }
1363
1364 void FrameView::beginDeferredRepaints()
1365 {
1366     Page* page = m_frame->page();
1367     if (page->mainFrame() != m_frame)
1368         return page->mainFrame()->view()->beginDeferredRepaints();
1369
1370     m_deferringRepaints++;
1371 }
1372
1373
1374 void FrameView::endDeferredRepaints()
1375 {
1376     Page* page = m_frame->page();
1377     if (page->mainFrame() != m_frame)
1378         return page->mainFrame()->view()->endDeferredRepaints();
1379
1380     ASSERT(m_deferringRepaints > 0);
1381
1382     if (--m_deferringRepaints)
1383         return;
1384     
1385     if (m_deferredRepaintTimer.isActive())
1386         return;
1387
1388     if (double delay = adjustedDeferredRepaintDelay()) {
1389         m_deferredRepaintTimer.startOneShot(delay);
1390         return;
1391     }
1392     
1393     doDeferredRepaints();
1394 }
1395
1396 void FrameView::checkStopDelayingDeferredRepaints()
1397 {
1398     if (!m_deferredRepaintTimer.isActive())
1399         return;
1400
1401     Document* document = m_frame->document();
1402     if (document && (document->parsing() || document->cachedResourceLoader()->requestCount()))
1403         return;
1404     
1405     m_deferredRepaintTimer.stop();
1406
1407     doDeferredRepaints();
1408 }
1409     
1410 void FrameView::doDeferredRepaints()
1411 {
1412     ASSERT(!m_deferringRepaints);
1413     if (!shouldUpdate()) {
1414         m_repaintRects.clear();
1415         m_repaintCount = 0;
1416         return;
1417     }
1418     unsigned size = m_repaintRects.size();
1419     for (unsigned i = 0; i < size; i++) {
1420 #if ENABLE(TILED_BACKING_STORE)
1421         if (frame()->tiledBackingStore()) {
1422             frame()->tiledBackingStore()->invalidate(m_repaintRects[i]);
1423             continue;
1424         }
1425 #endif
1426         ScrollView::repaintContentRectangle(m_repaintRects[i], false);
1427     }
1428     m_repaintRects.clear();
1429     m_repaintCount = 0;
1430     
1431     updateDeferredRepaintDelay();
1432 }
1433
1434 void FrameView::updateDeferredRepaintDelay()
1435 {
1436     Document* document = m_frame->document();
1437     if (!document || (!document->parsing() && !document->cachedResourceLoader()->requestCount())) {
1438         m_deferredRepaintDelay = s_deferredRepaintDelay;
1439         return;
1440     }
1441     if (m_deferredRepaintDelay < s_maxDeferredRepaintDelayDuringLoading) {
1442         m_deferredRepaintDelay += s_deferredRepaintDelayIncrementDuringLoading;
1443         if (m_deferredRepaintDelay > s_maxDeferredRepaintDelayDuringLoading)
1444             m_deferredRepaintDelay = s_maxDeferredRepaintDelayDuringLoading;
1445     }
1446 }
1447
1448 void FrameView::resetDeferredRepaintDelay()
1449 {
1450     m_deferredRepaintDelay = 0;
1451     if (m_deferredRepaintTimer.isActive()) {
1452         m_deferredRepaintTimer.stop();
1453         if (!m_deferringRepaints)
1454             doDeferredRepaints();
1455     }
1456 }
1457
1458 double FrameView::adjustedDeferredRepaintDelay() const
1459 {
1460     if (!m_deferredRepaintDelay)
1461         return 0;
1462     double timeSinceLastPaint = currentTime() - m_lastPaintTime;
1463     return max(0., m_deferredRepaintDelay - timeSinceLastPaint);
1464 }
1465     
1466 void FrameView::deferredRepaintTimerFired(Timer<FrameView>*)
1467 {
1468     doDeferredRepaints();
1469 }    
1470
1471 void FrameView::layoutTimerFired(Timer<FrameView>*)
1472 {
1473 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1474     if (!m_frame->document()->ownerElement())
1475         printf("Layout timer fired at %d\n", m_frame->document()->elapsedTime());
1476 #endif
1477     layout();
1478 }
1479
1480 void FrameView::scheduleRelayout()
1481 {
1482     // FIXME: We should assert the page is not in the page cache, but that is causing
1483     // too many false assertions.  See <rdar://problem/7218118>.
1484     ASSERT(m_frame->view() == this);
1485
1486     if (m_layoutRoot) {
1487         m_layoutRoot->markContainingBlocksForLayout(false);
1488         m_layoutRoot = 0;
1489     }
1490     if (!m_layoutSchedulingEnabled)
1491         return;
1492     if (!needsLayout())
1493         return;
1494     if (!m_frame->document()->shouldScheduleLayout())
1495         return;
1496
1497     // When frame flattening is enabled, the contents of the frame affects layout of the parent frames.
1498     // Also invalidate parent frame starting from the owner element of this frame.
1499     if (m_frame->settings() && m_frame->settings()->frameFlatteningEnabled() && m_frame->ownerRenderer()) {
1500         if (m_frame->ownerElement()->hasTagName(iframeTag) || m_frame->ownerElement()->hasTagName(frameTag))
1501             m_frame->ownerRenderer()->setNeedsLayout(true, true);
1502     }
1503
1504     int delay = m_frame->document()->minimumLayoutDelay();
1505     if (m_layoutTimer.isActive() && m_delayedLayout && !delay)
1506         unscheduleRelayout();
1507     if (m_layoutTimer.isActive())
1508         return;
1509
1510     m_delayedLayout = delay != 0;
1511
1512 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1513     if (!m_frame->document()->ownerElement())
1514         printf("Scheduling layout for %d\n", delay);
1515 #endif
1516
1517     m_layoutTimer.startOneShot(delay * 0.001);
1518 }
1519
1520 static bool isObjectAncestorContainerOf(RenderObject* ancestor, RenderObject* descendant)
1521 {
1522     for (RenderObject* r = descendant; r; r = r->container()) {
1523         if (r == ancestor)
1524             return true;
1525     }
1526     return false;
1527 }
1528
1529 void FrameView::scheduleRelayoutOfSubtree(RenderObject* relayoutRoot)
1530 {
1531     ASSERT(m_frame->view() == this);
1532
1533     if (m_frame->contentRenderer() && m_frame->contentRenderer()->needsLayout()) {
1534         if (relayoutRoot)
1535             relayoutRoot->markContainingBlocksForLayout(false);
1536         return;
1537     }
1538
1539     if (layoutPending() || !m_layoutSchedulingEnabled) {
1540         if (m_layoutRoot != relayoutRoot) {
1541             if (isObjectAncestorContainerOf(m_layoutRoot, relayoutRoot)) {
1542                 // Keep the current root
1543                 relayoutRoot->markContainingBlocksForLayout(false, m_layoutRoot);
1544             } else if (m_layoutRoot && isObjectAncestorContainerOf(relayoutRoot, m_layoutRoot)) {
1545                 // Re-root at relayoutRoot
1546                 m_layoutRoot->markContainingBlocksForLayout(false, relayoutRoot);
1547                 m_layoutRoot = relayoutRoot;
1548             } else {
1549                 // Just do a full relayout
1550                 if (m_layoutRoot)
1551                     m_layoutRoot->markContainingBlocksForLayout(false);
1552                 m_layoutRoot = 0;
1553                 relayoutRoot->markContainingBlocksForLayout(false);
1554             }
1555         }
1556     } else if (m_layoutSchedulingEnabled) {
1557         int delay = m_frame->document()->minimumLayoutDelay();
1558         m_layoutRoot = relayoutRoot;
1559         m_delayedLayout = delay != 0;
1560         m_layoutTimer.startOneShot(delay * 0.001);
1561     }
1562 }
1563
1564 bool FrameView::layoutPending() const
1565 {
1566     return m_layoutTimer.isActive();
1567 }
1568
1569 bool FrameView::needsLayout() const
1570 {
1571     // This can return true in cases where the document does not have a body yet.
1572     // Document::shouldScheduleLayout takes care of preventing us from scheduling
1573     // layout in that case.
1574     if (!m_frame)
1575         return false;
1576     RenderView* root = m_frame->contentRenderer();
1577     return layoutPending()
1578         || (root && root->needsLayout())
1579         || m_layoutRoot
1580         || (m_deferSetNeedsLayouts && m_setNeedsLayoutWasDeferred);
1581 }
1582
1583 void FrameView::setNeedsLayout()
1584 {
1585     if (m_deferSetNeedsLayouts) {
1586         m_setNeedsLayoutWasDeferred = true;
1587         return;
1588     }
1589     RenderView* root = m_frame->contentRenderer();
1590     if (root)
1591         root->setNeedsLayout(true);
1592 }
1593
1594 void FrameView::unscheduleRelayout()
1595 {
1596     m_postLayoutTasksTimer.stop();
1597
1598     if (!m_layoutTimer.isActive())
1599         return;
1600
1601 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1602     if (!m_frame->document()->ownerElement())
1603         printf("Layout timer unscheduled at %d\n", m_frame->document()->elapsedTime());
1604 #endif
1605     
1606     m_layoutTimer.stop();
1607     m_delayedLayout = false;
1608 }
1609
1610 bool FrameView::isTransparent() const
1611 {
1612     return m_isTransparent;
1613 }
1614
1615 void FrameView::setTransparent(bool isTransparent)
1616 {
1617     m_isTransparent = isTransparent;
1618 }
1619
1620 Color FrameView::baseBackgroundColor() const
1621 {
1622     return m_baseBackgroundColor;
1623 }
1624
1625 void FrameView::setBaseBackgroundColor(Color bc)
1626 {
1627     if (!bc.isValid())
1628         bc = Color::white;
1629     m_baseBackgroundColor = bc;
1630 }
1631
1632 void FrameView::updateBackgroundRecursively(const Color& backgroundColor, bool transparent)
1633 {
1634     for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext(m_frame.get())) {
1635         if (FrameView* view = frame->view()) {
1636             view->setTransparent(transparent);
1637             view->setBaseBackgroundColor(backgroundColor);
1638         }
1639     }
1640 }
1641
1642 bool FrameView::shouldUpdateWhileOffscreen() const
1643 {
1644     return m_shouldUpdateWhileOffscreen;
1645 }
1646
1647 void FrameView::setShouldUpdateWhileOffscreen(bool shouldUpdateWhileOffscreen)
1648 {
1649     m_shouldUpdateWhileOffscreen = shouldUpdateWhileOffscreen;
1650 }
1651
1652 bool FrameView::shouldUpdate(bool immediateRequested) const
1653 {
1654     if (!immediateRequested && isOffscreen() && !shouldUpdateWhileOffscreen())
1655         return false;
1656     if (!m_frame || !m_frame->document() || m_frame->document()->mayCauseFlashOfUnstyledContent())
1657         return false;
1658     return true;
1659 }
1660
1661 void FrameView::scheduleEvent(PassRefPtr<Event> event, PassRefPtr<Node> eventTarget)
1662 {
1663     if (!m_enqueueEvents) {
1664         ExceptionCode ec = 0;
1665         eventTarget->dispatchEvent(event, ec);
1666         return;
1667     }
1668
1669     ScheduledEvent* scheduledEvent = new ScheduledEvent;
1670     scheduledEvent->m_event = event;
1671     scheduledEvent->m_eventTarget = eventTarget;
1672     m_scheduledEvents.append(scheduledEvent);
1673 }
1674
1675 void FrameView::pauseScheduledEvents()
1676 {
1677     ASSERT(m_scheduledEvents.isEmpty() || m_enqueueEvents);
1678     m_enqueueEvents++;
1679 }
1680
1681 void FrameView::resumeScheduledEvents()
1682 {
1683     m_enqueueEvents--;
1684     if (!m_enqueueEvents)
1685         dispatchScheduledEvents();
1686     ASSERT(m_scheduledEvents.isEmpty() || m_enqueueEvents);
1687 }
1688
1689 void FrameView::scrollToAnchor()
1690 {
1691     RefPtr<Node> anchorNode = m_maintainScrollPositionAnchor;
1692     if (!anchorNode)
1693         return;
1694
1695     if (!anchorNode->renderer())
1696         return;
1697
1698     IntRect rect;
1699     if (anchorNode != m_frame->document())
1700         rect = anchorNode->getRect();
1701
1702     // Scroll nested layers and frames to reveal the anchor.
1703     // Align to the top and to the closest side (this matches other browsers).
1704     anchorNode->renderer()->enclosingLayer()->scrollRectToVisible(rect, true, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignTopAlways);
1705
1706     if (AXObjectCache::accessibilityEnabled())
1707         m_frame->document()->axObjectCache()->handleScrolledToAnchor(anchorNode.get());
1708
1709     // scrollRectToVisible can call into setScrollPosition(), which resets m_maintainScrollPositionAnchor.
1710     m_maintainScrollPositionAnchor = anchorNode;
1711 }
1712
1713 void FrameView::updateWidget(RenderEmbeddedObject* object)
1714 {
1715     ASSERT(!object->node() || object->node()->isElementNode());
1716     Element* ownerElement = static_cast<Element*>(object->node());
1717     // The object may have already been destroyed (thus node cleared),
1718     // but FrameView holds a manual ref, so it won't have been deleted.
1719     ASSERT(m_widgetUpdateSet->contains(object));
1720     if (!ownerElement)
1721         return;
1722
1723     // No need to update if it's already crashed or known to be missing.
1724     if (object->pluginCrashedOrWasMissing())
1725         return;
1726
1727     // FIXME: This could turn into a real virtual dispatch if we defined
1728     // updateWidget(bool) on HTMLElement.
1729     if (ownerElement->hasTagName(objectTag) || ownerElement->hasTagName(embedTag))
1730         static_cast<HTMLPlugInImageElement*>(ownerElement)->updateWidget(false);
1731     // FIXME: It is not clear that Media elements need or want this updateWidget() call.
1732 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
1733     else if (ownerElement->hasTagName(videoTag) || ownerElement->hasTagName(audioTag))
1734         static_cast<HTMLMediaElement*>(ownerElement)->updateWidget(false);
1735 #endif
1736     else
1737         ASSERT_NOT_REACHED();
1738
1739     // Caution: it's possible the object was destroyed again, since loading a
1740     // plugin may run any arbitrary javascript.
1741     object->updateWidgetPosition();
1742 }
1743
1744 bool FrameView::updateWidgets()
1745 {
1746     if (m_nestedLayoutCount > 1 || !m_widgetUpdateSet || m_widgetUpdateSet->isEmpty())
1747         return true;
1748     
1749     size_t size = m_widgetUpdateSet->size();
1750
1751     Vector<RenderEmbeddedObject*> objects;
1752     objects.reserveCapacity(size);
1753
1754     RenderEmbeddedObjectSet::const_iterator end = m_widgetUpdateSet->end();
1755     for (RenderEmbeddedObjectSet::const_iterator it = m_widgetUpdateSet->begin(); it != end; ++it) {
1756         objects.uncheckedAppend(*it);
1757         (*it)->ref();
1758     }
1759
1760     for (size_t i = 0; i < size; ++i) {
1761         RenderEmbeddedObject* object = objects[i];
1762         updateWidget(object);
1763         m_widgetUpdateSet->remove(object);
1764     }
1765
1766     RenderArena* arena = m_frame->document()->renderArena();
1767     for (size_t i = 0; i < size; ++i)
1768         objects[i]->deref(arena);
1769     
1770     return m_widgetUpdateSet->isEmpty();
1771 }
1772     
1773 void FrameView::performPostLayoutTasks()
1774 {
1775     m_hasPendingPostLayoutTasks = false;
1776
1777     if (m_firstLayoutCallbackPending) {
1778         m_firstLayoutCallbackPending = false;
1779         m_frame->loader()->didFirstLayout();
1780     }
1781
1782     if (m_isVisuallyNonEmpty && m_firstVisuallyNonEmptyLayoutCallbackPending) {
1783         m_firstVisuallyNonEmptyLayoutCallbackPending = false;
1784         m_frame->loader()->didFirstVisuallyNonEmptyLayout();
1785     }
1786
1787     RenderView* root = m_frame->contentRenderer();
1788
1789     root->updateWidgetPositions();
1790     
1791     for (unsigned i = 0; i < maxUpdateWidgetsIterations; i++) {
1792         if (updateWidgets())
1793             break;
1794     }
1795
1796     scrollToAnchor();
1797
1798     resumeScheduledEvents();
1799
1800     if (!root->printing()) {
1801         IntSize currentSize = IntSize(width(), height());
1802         float currentZoomFactor = root->style()->zoom();
1803         bool resized = !m_firstLayout && (currentSize != m_lastLayoutSize || currentZoomFactor != m_lastZoomFactor);
1804         m_lastLayoutSize = currentSize;
1805         m_lastZoomFactor = currentZoomFactor;
1806         if (resized)
1807             m_frame->eventHandler()->sendResizeEvent();
1808     }
1809 }
1810
1811 void FrameView::postLayoutTimerFired(Timer<FrameView>*)
1812 {
1813     performPostLayoutTasks();
1814 }
1815
1816 void FrameView::updateOverflowStatus(bool horizontalOverflow, bool verticalOverflow)
1817 {
1818     if (!m_viewportRenderer)
1819         return;
1820     
1821     if (m_overflowStatusDirty) {
1822         m_horizontalOverflow = horizontalOverflow;
1823         m_verticalOverflow = verticalOverflow;
1824         m_overflowStatusDirty = false;
1825         return;
1826     }
1827     
1828     bool horizontalOverflowChanged = (m_horizontalOverflow != horizontalOverflow);
1829     bool verticalOverflowChanged = (m_verticalOverflow != verticalOverflow);
1830     
1831     if (horizontalOverflowChanged || verticalOverflowChanged) {
1832         m_horizontalOverflow = horizontalOverflow;
1833         m_verticalOverflow = verticalOverflow;
1834         
1835         scheduleEvent(OverflowEvent::create(horizontalOverflowChanged, horizontalOverflow,
1836             verticalOverflowChanged, verticalOverflow),
1837             m_viewportRenderer->node());
1838     }
1839     
1840 }
1841
1842 void FrameView::dispatchScheduledEvents()
1843 {
1844     if (m_scheduledEvents.isEmpty())
1845         return;
1846
1847     Vector<ScheduledEvent*> scheduledEventsCopy = m_scheduledEvents;
1848     m_scheduledEvents.clear();
1849     
1850     Vector<ScheduledEvent*>::iterator end = scheduledEventsCopy.end();
1851     for (Vector<ScheduledEvent*>::iterator it = scheduledEventsCopy.begin(); it != end; ++it) {
1852         ScheduledEvent* scheduledEvent = *it;
1853         
1854         ExceptionCode ec = 0;
1855         
1856         // Only dispatch events to nodes that are in the document
1857         if (scheduledEvent->m_eventTarget->inDocument())
1858             scheduledEvent->m_eventTarget->dispatchEvent(scheduledEvent->m_event, ec);
1859         
1860         delete scheduledEvent;
1861     }
1862 }
1863
1864 IntRect FrameView::windowClipRect(bool clipToContents) const
1865 {
1866     ASSERT(m_frame->view() == this);
1867
1868     if (paintsEntireContents())
1869         return IntRect(IntPoint(0, 0), contentsSize());
1870
1871     // Set our clip rect to be our contents.
1872     IntRect clipRect = contentsToWindow(visibleContentRect(!clipToContents));
1873     if (!m_frame || !m_frame->document() || !m_frame->document()->ownerElement())
1874         return clipRect;
1875
1876     // Take our owner element and get the clip rect from the enclosing layer.
1877     Element* elt = m_frame->document()->ownerElement();
1878     RenderLayer* layer = elt->renderer()->enclosingLayer();
1879     // FIXME: layer should never be null, but sometimes seems to be anyway.
1880     if (!layer)
1881         return clipRect;
1882     FrameView* parentView = elt->document()->view();
1883     clipRect.intersect(parentView->windowClipRectForLayer(layer, true));
1884     return clipRect;
1885 }
1886
1887 IntRect FrameView::windowClipRectForLayer(const RenderLayer* layer, bool clipToLayerContents) const
1888 {
1889     // If we have no layer, just return our window clip rect.
1890     if (!layer)
1891         return windowClipRect();
1892
1893     // Apply the clip from the layer.
1894     IntRect clipRect;
1895     if (clipToLayerContents)
1896         clipRect = layer->childrenClipRect();
1897     else
1898         clipRect = layer->selfClipRect();
1899     clipRect = contentsToWindow(clipRect); 
1900     return intersection(clipRect, windowClipRect());
1901 }
1902
1903 bool FrameView::isActive() const
1904 {
1905     Page* page = frame()->page();
1906     return page && page->focusController()->isActive();
1907 }
1908
1909 void FrameView::valueChanged(Scrollbar* bar)
1910 {
1911     // Figure out if we really moved.
1912     IntSize offset = scrollOffset();
1913     ScrollView::valueChanged(bar);
1914     if (offset != scrollOffset())
1915         scrollPositionChanged();
1916     frame()->loader()->client()->didChangeScrollOffset();
1917 }
1918
1919 void FrameView::valueChanged(const IntSize& scrollDelta)
1920 {
1921     ScrollView::valueChanged(scrollDelta);
1922     frame()->eventHandler()->sendScrollEvent();
1923     frame()->loader()->client()->didChangeScrollOffset();
1924 }
1925
1926 void FrameView::invalidateScrollbarRect(Scrollbar* scrollbar, const IntRect& rect)
1927 {
1928     // Add in our offset within the FrameView.
1929     IntRect dirtyRect = rect;
1930     dirtyRect.move(scrollbar->x(), scrollbar->y());
1931     invalidateRect(dirtyRect);
1932 }
1933
1934 void FrameView::getTickmarks(Vector<IntRect>& tickmarks) const
1935 {
1936     tickmarks = frame()->document()->markers()->renderedRectsForMarkers(DocumentMarker::TextMatch);
1937 }
1938
1939 IntRect FrameView::windowResizerRect() const
1940 {
1941     Page* page = frame() ? frame()->page() : 0;
1942     if (!page)
1943         return IntRect();
1944     return page->chrome()->windowResizerRect();
1945 }
1946
1947 #if ENABLE(DASHBOARD_SUPPORT)
1948 void FrameView::updateDashboardRegions()
1949 {
1950     Document* document = m_frame->document();
1951     if (!document->hasDashboardRegions())
1952         return;
1953     Vector<DashboardRegionValue> newRegions;
1954     document->renderBox()->collectDashboardRegions(newRegions);
1955     if (newRegions == document->dashboardRegions())
1956         return;
1957     document->setDashboardRegions(newRegions);
1958     Page* page = m_frame->page();
1959     if (!page)
1960         return;
1961     page->chrome()->client()->dashboardRegionsChanged();
1962 }
1963 #endif
1964
1965 void FrameView::invalidateScrollCorner()
1966 {
1967     invalidateRect(scrollCornerRect());
1968 }
1969
1970 void FrameView::updateScrollCorner()
1971 {
1972     RenderObject* renderer = 0;
1973     RefPtr<RenderStyle> cornerStyle;
1974     
1975     if (!scrollCornerRect().isEmpty()) {
1976         // Try the <body> element first as a scroll corner source.
1977         Document* doc = m_frame->document();
1978         Element* body = doc ? doc->body() : 0;
1979         if (body && body->renderer()) {
1980             renderer = body->renderer();
1981             cornerStyle = renderer->getUncachedPseudoStyle(SCROLLBAR_CORNER, renderer->style());
1982         }
1983         
1984         if (!cornerStyle) {
1985             // If the <body> didn't have a custom style, then the root element might.
1986             Element* docElement = doc ? doc->documentElement() : 0;
1987             if (docElement && docElement->renderer()) {
1988                 renderer = docElement->renderer();
1989                 cornerStyle = renderer->getUncachedPseudoStyle(SCROLLBAR_CORNER, renderer->style());
1990             }
1991         }
1992         
1993         if (!cornerStyle) {
1994             // If we have an owning iframe/frame element, then it can set the custom scrollbar also.
1995             if (RenderPart* renderer = m_frame->ownerRenderer())
1996                 cornerStyle = renderer->getUncachedPseudoStyle(SCROLLBAR_CORNER, renderer->style());
1997         }
1998     }
1999
2000     if (cornerStyle) {
2001         if (!m_scrollCorner)
2002             m_scrollCorner = new (renderer->renderArena()) RenderScrollbarPart(renderer->document());
2003         m_scrollCorner->setStyle(cornerStyle.release());
2004         invalidateRect(scrollCornerRect());
2005     } else if (m_scrollCorner) {
2006         m_scrollCorner->destroy();
2007         m_scrollCorner = 0;
2008     }
2009 }
2010
2011 void FrameView::paintScrollCorner(GraphicsContext* context, const IntRect& cornerRect)
2012 {
2013     if (context->updatingControlTints()) {
2014         updateScrollCorner();
2015         return;
2016     }
2017
2018     if (m_scrollCorner) {
2019         m_scrollCorner->paintIntoRect(context, cornerRect.x(), cornerRect.y(), cornerRect);
2020         return;
2021     }
2022
2023     ScrollView::paintScrollCorner(context, cornerRect);
2024 }
2025
2026 bool FrameView::hasCustomScrollbars() const
2027 {
2028     const HashSet<RefPtr<Widget> >* viewChildren = children();
2029     HashSet<RefPtr<Widget> >::const_iterator end = viewChildren->end();
2030     for (HashSet<RefPtr<Widget> >::const_iterator current = viewChildren->begin(); current != end; ++current) {
2031         Widget* widget = current->get();
2032         if (widget->isFrameView()) {
2033             if (static_cast<FrameView*>(widget)->hasCustomScrollbars())
2034                 return true;
2035         } else if (widget->isScrollbar()) {
2036             Scrollbar* scrollbar = static_cast<Scrollbar*>(widget);
2037             if (scrollbar->isCustomScrollbar())
2038                 return true;
2039         }
2040     }
2041
2042     return false;
2043 }
2044
2045 void FrameView::updateControlTints()
2046 {
2047     // This is called when control tints are changed from aqua/graphite to clear and vice versa.
2048     // We do a "fake" paint, and when the theme gets a paint call, it can then do an invalidate.
2049     // This is only done if the theme supports control tinting. It's up to the theme and platform
2050     // to define when controls get the tint and to call this function when that changes.
2051     
2052     // Optimize the common case where we bring a window to the front while it's still empty.
2053     if (!m_frame || m_frame->loader()->url().isEmpty())
2054         return;
2055
2056     if ((m_frame->contentRenderer() && m_frame->contentRenderer()->theme()->supportsControlTints()) || hasCustomScrollbars())  {
2057         if (needsLayout())
2058             layout();
2059         PlatformGraphicsContext* const noContext = 0;
2060         GraphicsContext context(noContext);
2061         context.setUpdatingControlTints(true);
2062         if (platformWidget())
2063             paintContents(&context, visibleContentRect());
2064         else
2065             paint(&context, frameRect());
2066     }
2067 }
2068
2069 bool FrameView::wasScrolledByUser() const
2070 {
2071     return m_wasScrolledByUser;
2072 }
2073
2074 void FrameView::setWasScrolledByUser(bool wasScrolledByUser)
2075 {
2076     if (m_inProgrammaticScroll)
2077         return;
2078     m_maintainScrollPositionAnchor = 0;
2079     m_wasScrolledByUser = wasScrolledByUser;
2080 }
2081
2082 void FrameView::paintContents(GraphicsContext* p, const IntRect& rect)
2083 {
2084     if (!frame())
2085         return;
2086
2087     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willPaint(m_frame.get(), rect);
2088
2089     Document* document = frame()->document();
2090
2091 #ifndef NDEBUG
2092     bool fillWithRed;
2093     if (document->printing())
2094         fillWithRed = false; // Printing, don't fill with red (can't remember why).
2095     else if (document->ownerElement())
2096         fillWithRed = false; // Subframe, don't fill with red.
2097     else if (isTransparent())
2098         fillWithRed = false; // Transparent, don't fill with red.
2099     else if (m_paintBehavior & PaintBehaviorSelectionOnly)
2100         fillWithRed = false; // Selections are transparent, don't fill with red.
2101     else if (m_nodeToDraw)
2102         fillWithRed = false; // Element images are transparent, don't fill with red.
2103     else
2104         fillWithRed = true;
2105     
2106     if (fillWithRed)
2107         p->fillRect(rect, Color(0xFF, 0, 0), ColorSpaceDeviceRGB);
2108 #endif
2109
2110     bool isTopLevelPainter = !sCurrentPaintTimeStamp;
2111     if (isTopLevelPainter)
2112         sCurrentPaintTimeStamp = currentTime();
2113     
2114     RenderView* contentRenderer = frame()->contentRenderer();
2115     if (!contentRenderer) {
2116         LOG_ERROR("called FrameView::paint with nil renderer");
2117         return;
2118     }
2119
2120     ASSERT(!needsLayout());
2121     if (needsLayout())
2122         return;
2123
2124 #if USE(ACCELERATED_COMPOSITING)
2125     if (!p->paintingDisabled()) {
2126         if (GraphicsLayer* rootLayer = contentRenderer->compositor()->rootPlatformLayer())
2127             rootLayer->syncCompositingState();
2128     }
2129 #endif
2130
2131     ASSERT(!m_isPainting);
2132         
2133     m_isPainting = true;
2134         
2135     // m_nodeToDraw is used to draw only one element (and its descendants)
2136     RenderObject* eltRenderer = m_nodeToDraw ? m_nodeToDraw->renderer() : 0;
2137
2138     PaintBehavior oldPaintBehavior = m_paintBehavior;
2139     if (m_paintBehavior == PaintBehaviorNormal)
2140         document->markers()->invalidateRenderedRectsForMarkersInRect(rect);
2141
2142     if (document->printing())
2143         m_paintBehavior |= PaintBehaviorFlattenCompositingLayers;
2144
2145     contentRenderer->layer()->paint(p, rect, m_paintBehavior, eltRenderer);
2146     
2147     m_paintBehavior = oldPaintBehavior;
2148     
2149     m_isPainting = false;
2150     m_lastPaintTime = currentTime();
2151
2152 #if ENABLE(DASHBOARD_SUPPORT)
2153     // Regions may have changed as a result of the visibility/z-index of element changing.
2154     if (document->dashboardRegionsDirty())
2155         updateDashboardRegions();
2156 #endif
2157
2158     if (isTopLevelPainter)
2159         sCurrentPaintTimeStamp = 0;
2160
2161     InspectorInstrumentation::didPaint(cookie);
2162 }
2163
2164 void FrameView::setPaintBehavior(PaintBehavior behavior)
2165 {
2166     m_paintBehavior = behavior;
2167 }
2168
2169 PaintBehavior FrameView::paintBehavior() const
2170 {
2171     return m_paintBehavior;
2172 }
2173
2174 bool FrameView::isPainting() const
2175 {
2176     return m_isPainting;
2177 }
2178
2179 void FrameView::setNodeToDraw(Node* node)
2180 {
2181     m_nodeToDraw = node;
2182 }
2183
2184 void FrameView::updateLayoutAndStyleIfNeededRecursive()
2185 {
2186     // We have to crawl our entire tree looking for any FrameViews that need
2187     // layout and make sure they are up to date.
2188     // Mac actually tests for intersection with the dirty region and tries not to
2189     // update layout for frames that are outside the dirty region.  Not only does this seem
2190     // pointless (since those frames will have set a zero timer to layout anyway), but
2191     // it is also incorrect, since if two frames overlap, the first could be excluded from the dirty
2192     // region but then become included later by the second frame adding rects to the dirty region
2193     // when it lays out.
2194
2195     m_frame->document()->updateStyleIfNeeded();
2196
2197     if (needsLayout())
2198         layout();
2199
2200     const HashSet<RefPtr<Widget> >* viewChildren = children();
2201     HashSet<RefPtr<Widget> >::const_iterator end = viewChildren->end();
2202     for (HashSet<RefPtr<Widget> >::const_iterator current = viewChildren->begin(); current != end; ++current) {
2203         Widget* widget = (*current).get();
2204         if (widget->isFrameView())
2205             static_cast<FrameView*>(widget)->updateLayoutAndStyleIfNeededRecursive();
2206     }
2207
2208     // updateLayoutAndStyleIfNeededRecursive is called when we need to make sure style and layout are up-to-date before
2209     // painting, so we need to flush out any deferred repaints too.
2210     flushDeferredRepaints();
2211 }
2212     
2213 void FrameView::flushDeferredRepaints()
2214 {
2215     if (!m_deferredRepaintTimer.isActive())
2216         return;
2217     m_deferredRepaintTimer.stop();
2218     doDeferredRepaints();
2219 }
2220
2221 void FrameView::forceLayout(bool allowSubtree)
2222 {
2223     layout(allowSubtree);
2224 }
2225
2226 void FrameView::forceLayoutForPagination(const FloatSize& pageSize, float maximumShrinkFactor, Frame::AdjustViewSizeOrNot shouldAdjustViewSize)
2227 {
2228     // Dumping externalRepresentation(m_frame->renderer()).ascii() is a good trick to see
2229     // the state of things before and after the layout
2230     RenderView *root = toRenderView(m_frame->document()->renderer());
2231     if (root) {
2232         int pageW = ceilf(pageSize.width());
2233         root->setWidth(pageW);
2234         root->setPageHeight(pageSize.height());
2235         root->setNeedsLayoutAndPrefWidthsRecalc();
2236         forceLayout();
2237
2238         // If we don't fit in the given page width, we'll lay out again. If we don't fit in the
2239         // page width when shrunk, we will lay out at maximum shrink and clip extra content.
2240         // FIXME: We are assuming a shrink-to-fit printing implementation.  A cropping
2241         // implementation should not do this!
2242         int rightmostPos = root->rightmostPosition();
2243         if (rightmostPos > pageSize.width()) {
2244             pageW = std::min<int>(rightmostPos, ceilf(pageSize.width() * maximumShrinkFactor));
2245             if (pageSize.height())
2246                 root->setPageHeight(pageW / pageSize.width() * pageSize.height());
2247             root->setWidth(pageW);
2248             root->setNeedsLayoutAndPrefWidthsRecalc();
2249             forceLayout();
2250             int docHeight = root->bottomLayoutOverflow();
2251             root->clearLayoutOverflow();
2252             root->addLayoutOverflow(IntRect(0, 0, pageW, docHeight)); // This is how we clip in case we overflow again.
2253         }
2254     }
2255
2256     if (shouldAdjustViewSize)
2257         adjustViewSize();
2258 }
2259
2260 void FrameView::adjustPageHeightDeprecated(float *newBottom, float oldTop, float oldBottom, float /*bottomLimit*/)
2261 {
2262     RenderView* root = m_frame->contentRenderer();
2263     if (root) {
2264         // Use a context with painting disabled.
2265         GraphicsContext context((PlatformGraphicsContext*)0);
2266         root->setTruncatedAt((int)floorf(oldBottom));
2267         IntRect dirtyRect(0, (int)floorf(oldTop), root->rightLayoutOverflow(), (int)ceilf(oldBottom - oldTop));
2268         root->setPrintRect(dirtyRect);
2269         root->layer()->paint(&context, dirtyRect);
2270         *newBottom = root->bestTruncatedAt();
2271         if (*newBottom == 0)
2272             *newBottom = oldBottom;
2273         root->setPrintRect(IntRect());
2274     } else
2275         *newBottom = oldBottom;
2276 }
2277
2278 IntRect FrameView::convertFromRenderer(const RenderObject* renderer, const IntRect& rendererRect) const
2279 {
2280     IntRect rect = renderer->localToAbsoluteQuad(FloatRect(rendererRect)).enclosingBoundingBox();
2281
2282     // Convert from page ("absolute") to FrameView coordinates.
2283     rect.move(-scrollX(), -scrollY());
2284
2285     return rect;
2286 }
2287
2288 IntRect FrameView::convertToRenderer(const RenderObject* renderer, const IntRect& viewRect) const
2289 {
2290     IntRect rect = viewRect;
2291     
2292     // Convert from FrameView coords into page ("absolute") coordinates.
2293     rect.move(scrollX(), scrollY());
2294
2295     // FIXME: we don't have a way to map an absolute rect down to a local quad, so just
2296     // move the rect for now.
2297     rect.setLocation(roundedIntPoint(renderer->absoluteToLocal(rect.location(), false, true /* use transforms */)));
2298     return rect;
2299 }
2300
2301 IntPoint FrameView::convertFromRenderer(const RenderObject* renderer, const IntPoint& rendererPoint) const
2302 {
2303     IntPoint point = roundedIntPoint(renderer->localToAbsolute(rendererPoint, false, true /* use transforms */));
2304
2305     // Convert from page ("absolute") to FrameView coordinates.
2306     point.move(-scrollX(), -scrollY());
2307     return point;
2308 }
2309
2310 IntPoint FrameView::convertToRenderer(const RenderObject* renderer, const IntPoint& viewPoint) const
2311 {
2312     IntPoint point = viewPoint;
2313     
2314     // Convert from FrameView coords into page ("absolute") coordinates.
2315     point += IntSize(scrollX(), scrollY());
2316
2317     return roundedIntPoint(renderer->absoluteToLocal(point, false, true /* use transforms */));
2318 }
2319
2320 IntRect FrameView::convertToContainingView(const IntRect& localRect) const
2321 {
2322     if (const ScrollView* parentScrollView = parent()) {
2323         if (parentScrollView->isFrameView()) {
2324             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2325             // Get our renderer in the parent view
2326             RenderPart* renderer = m_frame->ownerRenderer();
2327             if (!renderer)
2328                 return localRect;
2329                 
2330             IntRect rect(localRect);
2331             // Add borders and padding??
2332             rect.move(renderer->borderLeft() + renderer->paddingLeft(),
2333                       renderer->borderTop() + renderer->paddingTop());
2334             return parentView->convertFromRenderer(renderer, rect);
2335         }
2336         
2337         return Widget::convertToContainingView(localRect);
2338     }
2339     
2340     return localRect;
2341 }
2342
2343 IntRect FrameView::convertFromContainingView(const IntRect& parentRect) const
2344 {
2345     if (const ScrollView* parentScrollView = parent()) {
2346         if (parentScrollView->isFrameView()) {
2347             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2348
2349             // Get our renderer in the parent view
2350             RenderPart* renderer = m_frame->ownerRenderer();
2351             if (!renderer)
2352                 return parentRect;
2353
2354             IntRect rect = parentView->convertToRenderer(renderer, parentRect);
2355             // Subtract borders and padding
2356             rect.move(-renderer->borderLeft() - renderer->paddingLeft(),
2357                       -renderer->borderTop() - renderer->paddingTop());
2358             return rect;
2359         }
2360         
2361         return Widget::convertFromContainingView(parentRect);
2362     }
2363     
2364     return parentRect;
2365 }
2366
2367 IntPoint FrameView::convertToContainingView(const IntPoint& localPoint) const
2368 {
2369     if (const ScrollView* parentScrollView = parent()) {
2370         if (parentScrollView->isFrameView()) {
2371             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2372
2373             // Get our renderer in the parent view
2374             RenderPart* renderer = m_frame->ownerRenderer();
2375             if (!renderer)
2376                 return localPoint;
2377                 
2378             IntPoint point(localPoint);
2379
2380             // Add borders and padding
2381             point.move(renderer->borderLeft() + renderer->paddingLeft(),
2382                        renderer->borderTop() + renderer->paddingTop());
2383             return parentView->convertFromRenderer(renderer, point);
2384         }
2385         
2386         return Widget::convertToContainingView(localPoint);
2387     }
2388     
2389     return localPoint;
2390 }
2391
2392 IntPoint FrameView::convertFromContainingView(const IntPoint& parentPoint) const
2393 {
2394     if (const ScrollView* parentScrollView = parent()) {
2395         if (parentScrollView->isFrameView()) {
2396             const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2397
2398             // Get our renderer in the parent view
2399             RenderPart* renderer = m_frame->ownerRenderer();
2400             if (!renderer)
2401                 return parentPoint;
2402
2403             IntPoint point = parentView->convertToRenderer(renderer, parentPoint);
2404             // Subtract borders and padding
2405             point.move(-renderer->borderLeft() - renderer->paddingLeft(),
2406                        -renderer->borderTop() - renderer->paddingTop());
2407             return point;
2408         }
2409         
2410         return Widget::convertFromContainingView(parentPoint);
2411     }
2412     
2413     return parentPoint;
2414 }
2415
2416 // Normal delay
2417 void FrameView::setRepaintThrottlingDeferredRepaintDelay(double p)
2418 {
2419     s_deferredRepaintDelay = p;
2420 }
2421
2422 // Negative value would mean that first few repaints happen without a delay
2423 void FrameView::setRepaintThrottlingnInitialDeferredRepaintDelayDuringLoading(double p)
2424 {
2425     s_initialDeferredRepaintDelayDuringLoading = p;
2426 }
2427
2428 // The delay grows on each repaint to this maximum value
2429 void FrameView::setRepaintThrottlingMaxDeferredRepaintDelayDuringLoading(double p)
2430 {
2431     s_maxDeferredRepaintDelayDuringLoading = p;
2432 }
2433
2434 // On each repaint the delay increases by this amount
2435 void FrameView::setRepaintThrottlingDeferredRepaintDelayIncrementDuringLoading(double p)
2436 {
2437     s_deferredRepaintDelayIncrementDuringLoading = p;
2438 }
2439
2440 } // namespace WebCore