OSDN Git Service

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