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.
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.
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.
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.
28 #include "FrameView.h"
30 #include "AXObjectCache.h"
31 #include "CSSStyleSelector.h"
33 #include "ChromeClient.h"
34 #include "DocLoader.h"
35 #include "EventHandler.h"
36 #include "FloatRect.h"
37 #include "FocusController.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"
57 #include "TextResourceDecoder.h"
58 #include <wtf/CurrentTime.h>
60 #ifdef ANDROID_INSTRUMENT
61 #include "FrameTree.h"
62 #include "TimeCounter.h"
65 #if USE(ACCELERATED_COMPOSITING)
66 #include "RenderLayerCompositor.h"
70 #include "SVGDocument.h"
71 #include "SVGLocatable.h"
73 #include "SVGPreserveAspectRatio.h"
74 #include "SVGSVGElement.h"
75 #include "SVGViewElement.h"
76 #include "SVGViewSpec.h"
79 #if ENABLE(TILED_BACKING_STORE)
80 #include "TiledBackingStore.h"
84 #include "WebCoreFrameBridge.h"
89 using namespace HTMLNames;
91 double FrameView::sCurrentPaintTimeStamp = 0.0;
93 #if ENABLE(REPAINT_THROTTLING)
95 static const double deferredRepaintDelay = 0.025;
96 // Negative value would mean that first few repaints happen without a delay
97 static const double initialDeferredRepaintDelayDuringLoading = 0;
98 // The delay grows on each repaint to this maximum value
99 static const double maxDeferredRepaintDelayDuringLoading = 2.5;
100 // On each repaint the delay increses by this amount
101 static const double deferredRepaintDelayIncrementDuringLoading = 0.5;
103 // FIXME: Repaint throttling could be good to have on all platform.
104 // The balance between CPU use and repaint frequency will need some tuning for desktop.
105 // More hooks may be needed to reset the delay on things like GIF and CSS animations.
106 static const double deferredRepaintDelay = 0;
107 static const double initialDeferredRepaintDelayDuringLoading = 0;
108 static const double maxDeferredRepaintDelayDuringLoading = 0;
109 static const double deferredRepaintDelayIncrementDuringLoading = 0;
112 // The maximum number of updateWidgets iterations that should be done before returning.
113 static const unsigned maxUpdateWidgetsIterations = 2;
115 struct ScheduledEvent : Noncopyable {
116 RefPtr<Event> m_event;
117 RefPtr<Node> m_eventTarget;
120 static inline float parentZoomFactor(Frame* frame)
122 Frame* parent = frame->tree()->parent();
125 FrameView* parentView = parent->view();
128 return parentView->zoomFactor();
131 FrameView::FrameView(Frame* frame)
133 , m_canHaveScrollbars(true)
134 , m_slowRepaintObjectCount(0)
135 , m_fixedObjectCount(0)
136 , m_layoutTimer(this, &FrameView::layoutTimerFired)
138 , m_postLayoutTasksTimer(this, &FrameView::postLayoutTimerFired)
139 , m_isTransparent(false)
140 , m_baseBackgroundColor(Color::white)
141 , m_mediaType("screen")
143 , m_overflowStatusDirty(true)
144 , m_viewportRenderer(0)
145 , m_wasScrolledByUser(false)
146 , m_inProgrammaticScroll(false)
147 , m_deferredRepaintTimer(this, &FrameView::deferredRepaintTimerFired)
148 , m_shouldUpdateWhileOffscreen(true)
149 , m_deferSetNeedsLayouts(0)
150 , m_setNeedsLayoutWasDeferred(false)
152 , m_zoomFactor(parentZoomFactor(frame))
157 PassRefPtr<FrameView> FrameView::create(Frame* frame)
159 RefPtr<FrameView> view = adoptRef(new FrameView(frame));
161 return view.release();
164 PassRefPtr<FrameView> FrameView::create(Frame* frame, const IntSize& initialSize)
166 RefPtr<FrameView> view = adoptRef(new FrameView(frame));
167 view->Widget::setFrameRect(IntRect(view->pos(), initialSize));
169 return view.release();
172 FrameView::~FrameView()
174 if (m_postLayoutTasksTimer.isActive()) {
175 m_postLayoutTasksTimer.stop();
176 m_scheduledEvents.clear();
182 // Custom scrollbars should already be destroyed at this point
183 ASSERT(!horizontalScrollbar() || !horizontalScrollbar()->isCustomScrollbar());
184 ASSERT(!verticalScrollbar() || !verticalScrollbar()->isCustomScrollbar());
186 setHasHorizontalScrollbar(false); // Remove native scrollbars now before we lose the connection to the HostWindow.
187 setHasVerticalScrollbar(false);
189 ASSERT(!m_scrollCorner);
190 ASSERT(m_scheduledEvents.isEmpty());
191 ASSERT(!m_enqueueEvents);
194 ASSERT(m_frame->view() != this || !m_frame->contentRenderer());
195 RenderPart* renderer = m_frame->ownerRenderer();
196 if (renderer && renderer->widget() == this)
197 renderer->setWidget(0);
201 void FrameView::reset()
203 m_useSlowRepaints = false;
204 m_isOverlapped = false;
205 m_contentIsOpaque = false;
208 m_layoutTimer.stop();
210 m_delayedLayout = false;
211 m_doFullRepaint = true;
212 m_layoutSchedulingEnabled = true;
215 m_nestedLayoutCount = 0;
216 m_postLayoutTasksTimer.stop();
217 m_firstLayout = true;
218 m_firstLayoutCallbackPending = false;
219 m_wasScrolledByUser = false;
220 m_lastLayoutSize = IntSize();
221 m_lastZoomFactor = 1.0f;
222 m_deferringRepaints = 0;
224 m_repaintRects.clear();
225 m_deferredRepaintDelay = initialDeferredRepaintDelayDuringLoading;
226 m_deferredRepaintTimer.stop();
228 m_paintBehavior = PaintBehaviorNormal;
229 m_isPainting = false;
230 m_isVisuallyNonEmpty = false;
231 m_firstVisuallyNonEmptyLayoutCallbackPending = true;
232 m_maintainScrollPositionAnchor = 0;
235 bool FrameView::isFrameView() const
240 void FrameView::clearFrame()
245 void FrameView::resetScrollbars()
247 // Reset the document's scrollbars back to our defaults before we yield the floor.
248 m_firstLayout = true;
249 setScrollbarsSuppressed(true);
250 if (m_canHaveScrollbars)
251 setScrollbarModes(ScrollbarAuto, ScrollbarAuto);
253 setScrollbarModes(ScrollbarAlwaysOff, ScrollbarAlwaysOff);
254 setScrollbarsSuppressed(false);
257 void FrameView::init()
261 m_margins = IntSize(-1, -1); // undefined
264 // Propagate the marginwidth/height and scrolling modes to the view.
265 Element* ownerElement = m_frame && m_frame->document() ? m_frame->document()->ownerElement() : 0;
266 if (ownerElement && (ownerElement->hasTagName(frameTag) || ownerElement->hasTagName(iframeTag))) {
267 HTMLFrameElement* frameElt = static_cast<HTMLFrameElement*>(ownerElement);
268 if (frameElt->scrollingMode() == ScrollbarAlwaysOff)
269 setCanHaveScrollbars(false);
270 int marginWidth = frameElt->getMarginWidth();
271 int marginHeight = frameElt->getMarginHeight();
272 if (marginWidth != -1)
273 setMarginWidth(marginWidth);
274 if (marginHeight != -1)
275 setMarginHeight(marginHeight);
279 void FrameView::detachCustomScrollbars()
284 Scrollbar* horizontalBar = horizontalScrollbar();
285 if (horizontalBar && horizontalBar->isCustomScrollbar() && !toRenderScrollbar(horizontalBar)->owningRenderer()->isRenderPart())
286 setHasHorizontalScrollbar(false);
288 Scrollbar* verticalBar = verticalScrollbar();
289 if (verticalBar && verticalBar->isCustomScrollbar() && !toRenderScrollbar(verticalBar)->owningRenderer()->isRenderPart())
290 setHasVerticalScrollbar(false);
292 if (m_scrollCorner) {
293 m_scrollCorner->destroy();
298 void FrameView::clear()
300 setCanBlitOnScroll(true);
305 if (RenderPart* renderer = m_frame->ownerRenderer())
306 renderer->viewCleared();
309 setScrollbarsSuppressed(true);
312 bool FrameView::didFirstLayout() const
314 return !m_firstLayout;
317 void FrameView::invalidateRect(const IntRect& rect)
321 hostWindow()->invalidateContentsAndWindow(rect, false /*immediate*/);
328 RenderPart* renderer = m_frame->ownerRenderer();
332 IntRect repaintRect = rect;
333 repaintRect.move(renderer->borderLeft() + renderer->paddingLeft(),
334 renderer->borderTop() + renderer->paddingTop());
335 renderer->repaintRectangle(repaintRect);
338 void FrameView::setMarginWidth(int w)
340 // make it update the rendering area when set
341 m_margins.setWidth(w);
344 void FrameView::setMarginHeight(int h)
346 // make it update the rendering area when set
347 m_margins.setHeight(h);
350 bool FrameView::avoidScrollbarCreation()
354 // with frame flattening no subframe can have scrollbars
355 // but we also cannot turn scrollbars of as we determine
356 // our flattening policy using that.
358 if (!m_frame->ownerElement())
361 if (!m_frame->settings() || m_frame->settings()->frameFlatteningEnabled())
367 void FrameView::setCanHaveScrollbars(bool canHaveScrollbars)
369 m_canHaveScrollbars = canHaveScrollbars;
370 ScrollView::setCanHaveScrollbars(canHaveScrollbars);
373 void FrameView::updateCanHaveScrollbars()
377 scrollbarModes(hMode, vMode);
378 if (hMode == ScrollbarAlwaysOff && vMode == ScrollbarAlwaysOff)
379 m_canHaveScrollbars = false;
381 m_canHaveScrollbars = true;
384 PassRefPtr<Scrollbar> FrameView::createScrollbar(ScrollbarOrientation orientation)
386 // FIXME: We need to update the scrollbar dynamically as documents change (or as doc elements and bodies get discovered that have custom styles).
387 Document* doc = m_frame->document();
389 // Try the <body> element first as a scrollbar source.
390 Element* body = doc ? doc->body() : 0;
391 if (body && body->renderer() && body->renderer()->style()->hasPseudoStyle(SCROLLBAR))
392 return RenderScrollbar::createCustomScrollbar(this, orientation, body->renderer()->enclosingBox());
394 // If the <body> didn't have a custom style, then the root element might.
395 Element* docElement = doc ? doc->documentElement() : 0;
396 if (docElement && docElement->renderer() && docElement->renderer()->style()->hasPseudoStyle(SCROLLBAR))
397 return RenderScrollbar::createCustomScrollbar(this, orientation, docElement->renderBox());
399 // If we have an owning iframe/frame element, then it can set the custom scrollbar also.
400 RenderPart* frameRenderer = m_frame->ownerRenderer();
401 if (frameRenderer && frameRenderer->style()->hasPseudoStyle(SCROLLBAR))
402 return RenderScrollbar::createCustomScrollbar(this, orientation, frameRenderer);
404 // Nobody set a custom style, so we just use a native scrollbar.
405 return ScrollView::createScrollbar(orientation);
408 void FrameView::setContentsSize(const IntSize& size)
410 if (size == contentsSize())
413 m_deferSetNeedsLayouts++;
415 ScrollView::setContentsSize(size);
417 Page* page = frame() ? frame()->page() : 0;
421 page->chrome()->contentsSizeChanged(frame(), size); //notify only
423 m_deferSetNeedsLayouts--;
425 if (!m_deferSetNeedsLayouts)
426 m_setNeedsLayoutWasDeferred = false; // FIXME: Find a way to make the deferred layout actually happen.
429 void FrameView::adjustViewSize()
431 ASSERT(m_frame->view() == this);
432 RenderView* root = m_frame->contentRenderer();
436 setContentsSize(IntSize(root->rightLayoutOverflow(), root->bottomLayoutOverflow()));
439 void FrameView::applyOverflowToViewport(RenderObject* o, ScrollbarMode& hMode, ScrollbarMode& vMode)
441 // Handle the overflow:hidden/scroll case for the body/html elements. WinIE treats
442 // overflow:hidden and overflow:scroll on <body> as applying to the document's
443 // scrollbars. The CSS2.1 draft states that HTML UAs should use the <html> or <body> element and XML/XHTML UAs should
444 // use the root element.
445 switch (o->style()->overflowX()) {
447 hMode = ScrollbarAlwaysOff;
450 hMode = ScrollbarAlwaysOn;
453 hMode = ScrollbarAuto;
456 // Don't set it at all.
460 switch (o->style()->overflowY()) {
462 vMode = ScrollbarAlwaysOff;
465 vMode = ScrollbarAlwaysOn;
468 vMode = ScrollbarAuto;
471 // Don't set it at all.
475 m_viewportRenderer = o;
478 #if USE(ACCELERATED_COMPOSITING)
479 void FrameView::updateCompositingLayers()
481 RenderView* view = m_frame->contentRenderer();
485 // This call will make sure the cached hasAcceleratedCompositing is updated from the pref
486 view->compositor()->cacheAcceleratedCompositingFlags();
488 if (!view->usesCompositing())
491 view->compositor()->updateCompositingLayers(CompositingUpdateAfterLayoutOrStyleChange);
494 void FrameView::setNeedsOneShotDrawingSynchronization()
496 Page* page = frame() ? frame()->page() : 0;
498 page->chrome()->client()->setNeedsOneShotDrawingSynchronization();
501 #endif // USE(ACCELERATED_COMPOSITING)
503 bool FrameView::hasCompositedContent() const
505 #if USE(ACCELERATED_COMPOSITING)
506 if (RenderView* view = m_frame->contentRenderer())
507 return view->compositor()->inCompositingMode();
512 // Sometimes (for plug-ins) we need to eagerly go into compositing mode.
513 void FrameView::enterCompositingMode()
515 #if USE(ACCELERATED_COMPOSITING)
516 if (RenderView* view = m_frame->contentRenderer())
517 return view->compositor()->enableCompositingMode();
521 bool FrameView::isEnclosedInCompositingLayer() const
523 #if USE(ACCELERATED_COMPOSITING)
524 RenderObject* frameOwnerRenderer = m_frame->ownerRenderer();
525 return frameOwnerRenderer && frameOwnerRenderer->containerForRepaint();
531 bool FrameView::syncCompositingStateRecursive()
533 #if USE(ACCELERATED_COMPOSITING)
534 ASSERT(m_frame->view() == this);
535 RenderView* contentRenderer = m_frame->contentRenderer();
536 if (!contentRenderer)
537 return true; // We don't want to keep trying to update layers if we have no renderer.
539 if (m_layoutTimer.isActive()) {
540 // Don't sync layers if there's a layout pending.
544 if (GraphicsLayer* rootLayer = contentRenderer->compositor()->rootPlatformLayer())
545 rootLayer->syncCompositingState();
547 bool allSubframesSynced = true;
548 const HashSet<RefPtr<Widget> >* viewChildren = children();
549 HashSet<RefPtr<Widget> >::const_iterator end = viewChildren->end();
550 for (HashSet<RefPtr<Widget> >::const_iterator current = viewChildren->begin(); current != end; ++current) {
551 Widget* widget = (*current).get();
552 if (widget->isFrameView()) {
553 bool synced = static_cast<FrameView*>(widget)->syncCompositingStateRecursive();
554 allSubframesSynced &= synced;
557 return allSubframesSynced;
558 #else // USE(ACCELERATED_COMPOSITING)
563 bool FrameView::isSoftwareRenderable() const
565 #if USE(ACCELERATED_COMPOSITING)
566 RenderView* view = m_frame->contentRenderer();
570 return !view->compositor()->has3DContent();
576 void FrameView::didMoveOnscreen()
578 RenderView* view = m_frame->contentRenderer();
580 view->didMoveOnscreen();
583 void FrameView::willMoveOffscreen()
585 RenderView* view = m_frame->contentRenderer();
587 view->willMoveOffscreen();
590 RenderObject* FrameView::layoutRoot(bool onlyDuringLayout) const
592 return onlyDuringLayout && layoutPending() ? 0 : m_layoutRoot;
595 void FrameView::layout(bool allowSubtree)
600 m_layoutTimer.stop();
601 m_delayedLayout = false;
602 m_setNeedsLayoutWasDeferred = false;
604 // Protect the view from being deleted during layout (in recalcStyle)
605 RefPtr<FrameView> protector(this);
608 // FIXME: Do we need to set m_size.width here?
609 // FIXME: Should we set m_size.height here too?
610 m_size.setWidth(layoutWidth());
614 // we shouldn't enter layout() while painting
615 ASSERT(!isPainting());
619 #if ENABLE(INSPECTOR)
620 if (InspectorTimelineAgent* timelineAgent = inspectorTimelineAgent())
621 timelineAgent->willLayout();
624 if (!allowSubtree && m_layoutRoot) {
625 m_layoutRoot->markContainingBlocksForLayout(false);
629 ASSERT(m_frame->view() == this);
630 // This early return should be removed when rdar://5598072 is resolved. In the meantime, there is a
631 // gigantic CrashTracer because of this issue, and the early return will hopefully cause graceful
633 if (m_frame->view() != this)
636 Document* document = m_frame->document();
638 m_layoutSchedulingEnabled = false;
640 if (!m_nestedLayoutCount && m_postLayoutTasksTimer.isActive()) {
641 // This is a new top-level layout. If there are any remaining tasks from the previous
642 // layout, finish them now.
643 m_postLayoutTasksTimer.stop();
644 performPostLayoutTasks();
647 // Viewport-dependent media queries may cause us to need completely different style information.
649 if (document->styleSelector()->affectedByViewportChange())
650 document->updateStyleSelector();
652 // Always ensure our style info is up-to-date. This can happen in situations where
653 // the layout beats any sort of style recalc update that needs to occur.
654 if (m_frame->needsReapplyStyles())
655 m_frame->reapplyStyles();
656 else if (document->childNeedsStyleRecalc())
657 document->recalcStyle();
659 bool subtree = m_layoutRoot;
661 // If there is only one ref to this view left, then its going to be destroyed as soon as we exit,
662 // so there's no point to continuing to layout
663 if (protector->hasOneRef())
666 RenderObject* root = subtree ? m_layoutRoot : document->renderer();
668 // FIXME: Do we need to set m_size here?
669 m_layoutSchedulingEnabled = true;
673 #ifdef ANDROID_INSTRUMENT
674 if (!m_frame->tree() || !m_frame->tree()->parent())
675 android::TimeCounter::start(android::TimeCounter::LayoutTimeCounter);
678 m_nestedLayoutCount++;
682 if (m_canHaveScrollbars) {
683 hMode = ScrollbarAuto;
684 vMode = ScrollbarAuto;
686 hMode = ScrollbarAlwaysOff;
687 vMode = ScrollbarAlwaysOff;
691 Node* documentElement = document->documentElement();
692 RenderObject* rootRenderer = documentElement ? documentElement->renderer() : 0;
693 Node* body = document->body();
694 if (body && body->renderer()) {
695 if (body->hasTagName(framesetTag) && !m_frame->settings()->frameFlatteningEnabled()) {
696 body->renderer()->setChildNeedsLayout(true);
697 vMode = ScrollbarAlwaysOff;
698 hMode = ScrollbarAlwaysOff;
699 } else if (body->hasTagName(bodyTag)) {
700 if (!m_firstLayout && m_size.height() != layoutHeight() && body->renderer()->enclosingBox()->stretchesToViewHeight())
701 body->renderer()->setChildNeedsLayout(true);
702 // It's sufficient to just check the X overflow,
703 // since it's illegal to have visible in only one direction.
704 RenderObject* o = rootRenderer->style()->overflowX() == OVISIBLE && document->documentElement()->hasTagName(htmlTag) ? body->renderer() : rootRenderer;
705 applyOverflowToViewport(o, hMode, vMode);
707 } else if (rootRenderer) {
709 if (documentElement->isSVGElement()) {
710 if (!m_firstLayout && (m_size.width() != layoutWidth() || m_size.height() != layoutHeight()))
711 rootRenderer->setChildNeedsLayout(true);
713 applyOverflowToViewport(rootRenderer, hMode, vMode);
715 applyOverflowToViewport(rootRenderer, hMode, vMode);
718 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
719 if (m_firstLayout && !document->ownerElement())
720 printf("Elapsed time before first layout: %d\n", document->elapsedTime());
724 m_doFullRepaint = !subtree && (m_firstLayout || toRenderView(root)->printing());
727 // Now set our scrollbar state for the layout.
728 ScrollbarMode currentHMode = horizontalScrollbarMode();
729 ScrollbarMode currentVMode = verticalScrollbarMode();
731 if (m_firstLayout || (hMode != currentHMode || vMode != currentVMode)) {
733 setScrollbarsSuppressed(true);
735 m_firstLayout = false;
736 m_firstLayoutCallbackPending = true;
737 m_lastLayoutSize = IntSize(width(), height());
738 m_lastZoomFactor = root->style()->zoom();
740 // Set the initial vMode to AlwaysOn if we're auto.
741 if (vMode == ScrollbarAuto)
742 setVerticalScrollbarMode(ScrollbarAlwaysOn); // This causes a vertical scrollbar to appear.
743 // Set the initial hMode to AlwaysOff if we're auto.
744 if (hMode == ScrollbarAuto)
745 setHorizontalScrollbarMode(ScrollbarAlwaysOff); // This causes a horizontal scrollbar to disappear.
747 setScrollbarModes(hMode, vMode);
748 setScrollbarsSuppressed(false, true);
750 setScrollbarModes(hMode, vMode);
753 IntSize oldSize = m_size;
755 m_size = IntSize(layoutWidth(), layoutHeight());
757 if (oldSize != m_size)
758 m_doFullRepaint = true;
761 RenderLayer* layer = root->enclosingLayer();
763 pauseScheduledEvents();
765 bool disableLayoutState = false;
767 RenderView* view = root->view();
768 disableLayoutState = view->shouldDisableLayoutStateForSubtree(root);
769 view->pushLayoutState(root);
770 if (disableLayoutState)
771 view->disableLayoutState();
775 beginDeferredRepaints();
777 endDeferredRepaints();
781 RenderView* view = root->view();
782 view->popLayoutState();
783 if (disableLayoutState)
784 view->enableLayoutState();
788 m_frame->selection()->setNeedsLayout();
789 m_frame->selection()->updateAppearance();
791 m_layoutSchedulingEnabled = true;
793 if (!subtree && !toRenderView(root)->printing())
796 // Now update the positions of all layers.
797 beginDeferredRepaints();
798 IntPoint cachedOffset;
799 layer->updateLayerPositions((m_doFullRepaint ? RenderLayer::DoFullRepaint : 0)
800 | RenderLayer::CheckForRepaint
801 | RenderLayer::IsCompositingUpdateRoot
802 | RenderLayer::UpdateCompositingLayers,
803 subtree ? 0 : &cachedOffset);
804 endDeferredRepaints();
806 #if USE(ACCELERATED_COMPOSITING)
807 updateCompositingLayers();
813 if (AXObjectCache::accessibilityEnabled())
814 root->document()->axObjectCache()->postNotification(root, AXObjectCache::AXLayoutComplete, true);
816 #if ENABLE(DASHBOARD_SUPPORT)
817 updateDashboardRegions();
820 #ifdef ANDROID_INSTRUMENT
821 if (!m_frame->tree()->parent())
822 android::TimeCounter::record(android::TimeCounter::LayoutTimeCounter, __FUNCTION__);
824 ASSERT(!root->needsLayout());
826 setCanBlitOnScroll(!useSlowRepaints());
828 if (document->hasListenerType(Document::OVERFLOWCHANGED_LISTENER))
829 updateOverflowStatus(layoutWidth() < contentsWidth(),
830 layoutHeight() < contentsHeight());
832 if (!m_postLayoutTasksTimer.isActive()) {
833 // Calls resumeScheduledEvents()
834 performPostLayoutTasks();
836 if (!m_postLayoutTasksTimer.isActive() && needsLayout()) {
837 // Post-layout widget updates or an event handler made us need layout again.
838 // Lay out again, but this time defer widget updates and event dispatch until after
840 m_postLayoutTasksTimer.startOneShot(0);
841 pauseScheduledEvents();
845 resumeScheduledEvents();
846 ASSERT(m_enqueueEvents);
849 #if ENABLE(INSPECTOR)
850 if (InspectorTimelineAgent* timelineAgent = inspectorTimelineAgent())
851 timelineAgent->didLayout();
854 m_nestedLayoutCount--;
857 void FrameView::addWidgetToUpdate(RenderEmbeddedObject* object)
859 if (!m_widgetUpdateSet)
860 m_widgetUpdateSet.set(new RenderEmbeddedObjectSet);
862 m_widgetUpdateSet->add(object);
865 void FrameView::removeWidgetToUpdate(RenderEmbeddedObject* object)
867 if (!m_widgetUpdateSet)
870 m_widgetUpdateSet->remove(object);
873 void FrameView::setMediaType(const String& mediaType)
875 m_mediaType = mediaType;
878 String FrameView::mediaType() const
880 // See if we have an override type.
881 String overrideType = m_frame->loader()->client()->overrideMediaType();
882 if (!overrideType.isNull())
887 void FrameView::adjustMediaTypeForPrinting(bool printing)
890 if (m_mediaTypeWhenNotPrinting.isNull())
891 m_mediaTypeWhenNotPrinting = mediaType();
892 setMediaType("print");
894 if (!m_mediaTypeWhenNotPrinting.isNull())
895 setMediaType(m_mediaTypeWhenNotPrinting);
896 m_mediaTypeWhenNotPrinting = String();
900 bool FrameView::useSlowRepaints() const
902 return m_useSlowRepaints || m_slowRepaintObjectCount > 0 || (platformWidget() && m_fixedObjectCount > 0) || m_isOverlapped || !m_contentIsOpaque;
905 bool FrameView::useSlowRepaintsIfNotOverlapped() const
907 return m_useSlowRepaints || m_slowRepaintObjectCount > 0 || (platformWidget() && m_fixedObjectCount > 0) || !m_contentIsOpaque;
910 void FrameView::setUseSlowRepaints()
912 m_useSlowRepaints = true;
913 setCanBlitOnScroll(false);
916 void FrameView::addSlowRepaintObject()
918 if (!m_slowRepaintObjectCount)
919 setCanBlitOnScroll(false);
920 m_slowRepaintObjectCount++;
923 void FrameView::removeSlowRepaintObject()
925 ASSERT(m_slowRepaintObjectCount > 0);
926 m_slowRepaintObjectCount--;
927 if (!m_slowRepaintObjectCount)
928 setCanBlitOnScroll(!useSlowRepaints());
931 void FrameView::addFixedObject()
933 ++m_fixedObjectCount;
936 void FrameView::removeFixedObject()
938 ASSERT(m_fixedObjectCount > 0);
939 --m_fixedObjectCount;
942 void FrameView::setIsOverlapped(bool isOverlapped)
944 if (isOverlapped == m_isOverlapped)
947 m_isOverlapped = isOverlapped;
948 setCanBlitOnScroll(!useSlowRepaints());
950 #if USE(ACCELERATED_COMPOSITING)
951 // Overlap can affect compositing tests, so if it changes, we need to trigger
952 // a recalcStyle in the parent document.
953 if (hasCompositedContent()) {
954 if (Element* ownerElement = m_frame->document()->ownerElement())
955 ownerElement->setNeedsStyleRecalc(SyntheticStyleChange);
960 void FrameView::setContentIsOpaque(bool contentIsOpaque)
962 if (contentIsOpaque == m_contentIsOpaque)
965 m_contentIsOpaque = contentIsOpaque;
966 setCanBlitOnScroll(!useSlowRepaints());
969 void FrameView::restoreScrollbar()
971 setScrollbarsSuppressed(false);
974 bool FrameView::scrollToFragment(const KURL& url)
976 // If our URL has no ref, then we have no place we need to jump to.
977 // OTOH If CSS target was set previously, we want to set it to 0, recalc
978 // and possibly repaint because :target pseudo class may have been
979 // set (see bug 11321).
980 if (!url.hasFragmentIdentifier() && !m_frame->document()->cssTarget())
983 String fragmentIdentifier = url.fragmentIdentifier();
984 if (scrollToAnchor(fragmentIdentifier))
987 // Try again after decoding the ref, based on the document's encoding.
988 if (TextResourceDecoder* decoder = m_frame->document()->decoder())
989 return scrollToAnchor(decodeURLEscapeSequences(fragmentIdentifier, decoder->encoding()));
994 bool FrameView::scrollToAnchor(const String& name)
996 ASSERT(m_frame->document());
998 if (!m_frame->document()->haveStylesheetsLoaded()) {
999 m_frame->document()->setGotoAnchorNeededAfterStylesheetsLoad(true);
1003 m_frame->document()->setGotoAnchorNeededAfterStylesheetsLoad(false);
1005 Element* anchorNode = m_frame->document()->findAnchor(name);
1008 if (m_frame->document()->isSVGDocument()) {
1009 if (name.startsWith("xpointer(")) {
1010 // We need to parse the xpointer reference here
1011 } else if (name.startsWith("svgView(")) {
1012 RefPtr<SVGSVGElement> svg = static_cast<SVGDocument*>(m_frame->document())->rootElement();
1013 if (!svg->currentView()->parseViewSpec(name))
1015 svg->setUseCurrentView(true);
1017 if (anchorNode && anchorNode->hasTagName(SVGNames::viewTag)) {
1018 RefPtr<SVGViewElement> viewElement = anchorNode->hasTagName(SVGNames::viewTag) ? static_cast<SVGViewElement*>(anchorNode) : 0;
1019 if (viewElement.get()) {
1020 RefPtr<SVGSVGElement> svg = static_cast<SVGSVGElement*>(SVGLocatable::nearestViewportElement(viewElement.get()));
1021 svg->inheritViewAttributes(viewElement.get());
1025 // FIXME: need to decide which <svg> to focus on, and zoom to that one
1026 // FIXME: need to actually "highlight" the viewTarget(s)
1030 m_frame->document()->setCSSTarget(anchorNode); // Setting to null will clear the current target.
1032 // Implement the rule that "" and "top" both mean top of page as in other browsers.
1033 if (!anchorNode && !(name.isEmpty() || equalIgnoringCase(name, "top")))
1036 #ifdef ANDROID_SCROLL_ON_GOTO_ANCHOR
1037 // TODO(andreip): check with Grace if this is correct.
1038 android::WebFrame::getWebFrame(m_frame.get())->setUserInitiatedClick(true);
1040 maintainScrollPositionAtAnchor(anchorNode ? static_cast<Node*>(anchorNode) : m_frame->document());
1041 #ifdef ANDROID_SCROLL_ON_GOTO_ANCHOR
1042 android::WebFrame::getWebFrame(m_frame.get())->setUserInitiatedClick(false);
1047 void FrameView::maintainScrollPositionAtAnchor(Node* anchorNode)
1049 m_maintainScrollPositionAnchor = anchorNode;
1050 if (!m_maintainScrollPositionAnchor)
1053 // We need to update the layout before scrolling, otherwise we could
1054 // really mess things up if an anchor scroll comes at a bad moment.
1055 m_frame->document()->updateStyleIfNeeded();
1056 // Only do a layout if changes have occurred that make it necessary.
1057 if (m_frame->contentRenderer() && m_frame->contentRenderer()->needsLayout())
1063 void FrameView::setScrollPosition(const IntPoint& scrollPoint)
1065 bool wasInProgrammaticScroll = m_inProgrammaticScroll;
1066 m_inProgrammaticScroll = true;
1067 m_maintainScrollPositionAnchor = 0;
1068 ScrollView::setScrollPosition(scrollPoint);
1069 m_inProgrammaticScroll = wasInProgrammaticScroll;
1072 void FrameView::scrollPositionChanged()
1074 frame()->eventHandler()->sendScrollEvent();
1075 repaintFixedElementsAfterScrolling();
1078 void FrameView::repaintFixedElementsAfterScrolling()
1080 // For fixed position elements, update widget positions and compositing layers after scrolling,
1081 // but only if we're not inside of layout.
1082 if (!m_nestedLayoutCount && hasFixedObjects()) {
1083 if (RenderView* root = m_frame->contentRenderer()) {
1084 root->updateWidgetPositions();
1085 root->layer()->updateRepaintRectsAfterScroll();
1086 #if USE(ACCELERATED_COMPOSITING)
1087 if (root->usesCompositing())
1088 root->compositor()->updateCompositingLayers(CompositingUpdateOnScroll);
1093 #if USE(ACCELERATED_COMPOSITING)
1094 if (RenderView* root = m_frame->contentRenderer()) {
1095 if (root->usesCompositing())
1096 root->compositor()->updateContentLayerScrollPosition(scrollPosition());
1101 HostWindow* FrameView::hostWindow() const
1103 Page* page = frame() ? frame()->page() : 0;
1106 return page->chrome();
1109 const unsigned cRepaintRectUnionThreshold = 25;
1111 void FrameView::repaintContentRectangle(const IntRect& r, bool immediate)
1113 ASSERT(!m_frame->document()->ownerElement());
1115 double delay = adjustedDeferredRepaintDelay();
1116 if ((m_deferringRepaints || m_deferredRepaintTimer.isActive() || delay) && !immediate) {
1117 IntRect paintRect = r;
1118 if (!paintsEntireContents())
1119 paintRect.intersect(visibleContentRect());
1120 #ifdef ANDROID_CAPTURE_OFFSCREEN_PAINTS
1122 ScrollView::platformOffscreenContentRectangle(visibleContentRect(), r);
1124 if (paintRect.isEmpty())
1126 if (m_repaintCount == cRepaintRectUnionThreshold) {
1127 IntRect unionedRect;
1128 for (unsigned i = 0; i < cRepaintRectUnionThreshold; ++i)
1129 unionedRect.unite(m_repaintRects[i]);
1130 m_repaintRects.clear();
1131 m_repaintRects.append(unionedRect);
1133 if (m_repaintCount < cRepaintRectUnionThreshold)
1134 m_repaintRects.append(paintRect);
1136 m_repaintRects[0].unite(paintRect);
1139 if (!m_deferringRepaints && !m_deferredRepaintTimer.isActive())
1140 m_deferredRepaintTimer.startOneShot(delay);
1144 if (!immediate && isOffscreen() && !shouldUpdateWhileOffscreen())
1147 #if ENABLE(TILED_BACKING_STORE)
1148 if (frame()->tiledBackingStore()) {
1149 frame()->tiledBackingStore()->invalidate(r);
1153 ScrollView::repaintContentRectangle(r, immediate);
1156 void FrameView::visibleContentsResized()
1158 // We check to make sure the view is attached to a frame() as this method can
1159 // be triggered before the view is attached by Frame::createView(...) setting
1160 // various values such as setScrollBarModes(...) for example. An ASSERT is
1161 // triggered when a view is layout before being attached to a frame().
1162 if (!frame()->view())
1169 void FrameView::beginDeferredRepaints()
1171 Page* page = m_frame->page();
1172 if (page->mainFrame() != m_frame)
1173 return page->mainFrame()->view()->beginDeferredRepaints();
1175 m_deferringRepaints++;
1179 void FrameView::endDeferredRepaints()
1181 Page* page = m_frame->page();
1182 if (page->mainFrame() != m_frame)
1183 return page->mainFrame()->view()->endDeferredRepaints();
1185 ASSERT(m_deferringRepaints > 0);
1187 if (--m_deferringRepaints)
1190 if (m_deferredRepaintTimer.isActive())
1193 if (double delay = adjustedDeferredRepaintDelay()) {
1194 m_deferredRepaintTimer.startOneShot(delay);
1198 doDeferredRepaints();
1201 void FrameView::checkStopDelayingDeferredRepaints()
1203 if (!m_deferredRepaintTimer.isActive())
1206 Document* document = m_frame->document();
1207 if (document && (document->parsing() || document->docLoader()->requestCount()))
1210 m_deferredRepaintTimer.stop();
1212 doDeferredRepaints();
1215 void FrameView::doDeferredRepaints()
1217 ASSERT(!m_deferringRepaints);
1218 if (isOffscreen() && !shouldUpdateWhileOffscreen()) {
1219 m_repaintRects.clear();
1223 unsigned size = m_repaintRects.size();
1224 for (unsigned i = 0; i < size; i++) {
1225 #if ENABLE(TILED_BACKING_STORE)
1226 if (frame()->tiledBackingStore()) {
1227 frame()->tiledBackingStore()->invalidate(m_repaintRects[i]);
1231 ScrollView::repaintContentRectangle(m_repaintRects[i], false);
1233 m_repaintRects.clear();
1236 updateDeferredRepaintDelay();
1239 void FrameView::updateDeferredRepaintDelay()
1241 Document* document = m_frame->document();
1242 if (!document || (!document->parsing() && !document->docLoader()->requestCount())) {
1243 m_deferredRepaintDelay = deferredRepaintDelay;
1246 if (m_deferredRepaintDelay < maxDeferredRepaintDelayDuringLoading) {
1247 m_deferredRepaintDelay += deferredRepaintDelayIncrementDuringLoading;
1248 if (m_deferredRepaintDelay > maxDeferredRepaintDelayDuringLoading)
1249 m_deferredRepaintDelay = maxDeferredRepaintDelayDuringLoading;
1253 void FrameView::resetDeferredRepaintDelay()
1255 m_deferredRepaintDelay = 0;
1256 if (m_deferredRepaintTimer.isActive()) {
1257 m_deferredRepaintTimer.stop();
1258 if (!m_deferringRepaints)
1259 doDeferredRepaints();
1263 double FrameView::adjustedDeferredRepaintDelay() const
1265 if (!m_deferredRepaintDelay)
1267 double timeSinceLastPaint = currentTime() - m_lastPaintTime;
1268 return max(0., m_deferredRepaintDelay - timeSinceLastPaint);
1271 void FrameView::deferredRepaintTimerFired(Timer<FrameView>*)
1273 doDeferredRepaints();
1276 void FrameView::layoutTimerFired(Timer<FrameView>*)
1278 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1279 if (!m_frame->document()->ownerElement())
1280 printf("Layout timer fired at %d\n", m_frame->document()->elapsedTime());
1285 void FrameView::scheduleRelayout()
1287 // FIXME: We should assert the page is not in the page cache, but that is causing
1288 // too many false assertions. See <rdar://problem/7218118>.
1289 ASSERT(m_frame->view() == this);
1292 m_layoutRoot->markContainingBlocksForLayout(false);
1295 if (!m_layoutSchedulingEnabled)
1299 if (!m_frame->document()->shouldScheduleLayout())
1302 #if defined(ANDROID_FLATTEN_IFRAME) || defined(ANDROID_FLATTEN_FRAMESET)
1303 // This is the Android frame flattening code. The common code below is not
1304 // used as frameSetFlatteningEnabled() is false on Android.
1305 if (m_frame->ownerRenderer())
1306 m_frame->ownerRenderer()->setNeedsLayoutAndPrefWidthsRecalc();
1309 // When frame flattening is enabled, the contents of the frame affects layout of the parent frames.
1310 // Also invalidate parent frame starting from the owner element of this frame.
1311 if (m_frame->settings()->frameFlatteningEnabled() && m_frame->ownerRenderer()) {
1312 if (m_frame->ownerElement()->hasTagName(iframeTag) || m_frame->ownerElement()->hasTagName(frameTag))
1313 m_frame->ownerRenderer()->setNeedsLayout(true, true);
1316 int delay = m_frame->document()->minimumLayoutDelay();
1317 if (m_layoutTimer.isActive() && m_delayedLayout && !delay)
1318 unscheduleRelayout();
1319 if (m_layoutTimer.isActive())
1322 m_delayedLayout = delay != 0;
1324 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1325 if (!m_frame->document()->ownerElement())
1326 printf("Scheduling layout for %d\n", delay);
1329 m_layoutTimer.startOneShot(delay * 0.001);
1332 static bool isObjectAncestorContainerOf(RenderObject* ancestor, RenderObject* descendant)
1334 for (RenderObject* r = descendant; r; r = r->container()) {
1341 void FrameView::scheduleRelayoutOfSubtree(RenderObject* relayoutRoot)
1343 ASSERT(m_frame->view() == this);
1345 if (m_frame->contentRenderer() && m_frame->contentRenderer()->needsLayout()) {
1347 relayoutRoot->markContainingBlocksForLayout(false);
1351 if (layoutPending() || !m_layoutSchedulingEnabled) {
1352 if (m_layoutRoot != relayoutRoot) {
1353 if (isObjectAncestorContainerOf(m_layoutRoot, relayoutRoot)) {
1354 // Keep the current root
1355 relayoutRoot->markContainingBlocksForLayout(false, m_layoutRoot);
1356 } else if (m_layoutRoot && isObjectAncestorContainerOf(relayoutRoot, m_layoutRoot)) {
1357 // Re-root at relayoutRoot
1358 m_layoutRoot->markContainingBlocksForLayout(false, relayoutRoot);
1359 m_layoutRoot = relayoutRoot;
1361 // Just do a full relayout
1363 m_layoutRoot->markContainingBlocksForLayout(false);
1365 relayoutRoot->markContainingBlocksForLayout(false);
1368 } else if (m_layoutSchedulingEnabled) {
1369 int delay = m_frame->document()->minimumLayoutDelay();
1370 m_layoutRoot = relayoutRoot;
1371 m_delayedLayout = delay != 0;
1372 m_layoutTimer.startOneShot(delay * 0.001);
1376 bool FrameView::layoutPending() const
1378 return m_layoutTimer.isActive();
1381 bool FrameView::needsLayout() const
1383 // This can return true in cases where the document does not have a body yet.
1384 // Document::shouldScheduleLayout takes care of preventing us from scheduling
1385 // layout in that case.
1388 RenderView* root = m_frame->contentRenderer();
1389 Document* document = m_frame->document();
1390 return layoutPending()
1391 || (root && root->needsLayout())
1393 || (document && document->childNeedsStyleRecalc()) // can occur when using WebKit ObjC interface
1394 || m_frame->needsReapplyStyles()
1395 || (m_deferSetNeedsLayouts && m_setNeedsLayoutWasDeferred);
1398 void FrameView::setNeedsLayout()
1400 if (m_deferSetNeedsLayouts) {
1401 m_setNeedsLayoutWasDeferred = true;
1404 RenderView* root = m_frame->contentRenderer();
1406 root->setNeedsLayout(true);
1409 void FrameView::unscheduleRelayout()
1411 if (!m_layoutTimer.isActive())
1414 #ifdef INSTRUMENT_LAYOUT_SCHEDULING
1415 if (!m_frame->document()->ownerElement())
1416 printf("Layout timer unscheduled at %d\n", m_frame->document()->elapsedTime());
1419 m_layoutTimer.stop();
1420 m_delayedLayout = false;
1423 bool FrameView::isTransparent() const
1425 return m_isTransparent;
1428 void FrameView::setTransparent(bool isTransparent)
1430 m_isTransparent = isTransparent;
1433 Color FrameView::baseBackgroundColor() const
1435 return m_baseBackgroundColor;
1438 void FrameView::setBaseBackgroundColor(Color bc)
1442 m_baseBackgroundColor = bc;
1445 void FrameView::updateBackgroundRecursively(const Color& backgroundColor, bool transparent)
1447 for (Frame* frame = m_frame.get(); frame; frame = frame->tree()->traverseNext(m_frame.get())) {
1448 FrameView* view = frame->view();
1452 view->setTransparent(transparent);
1453 view->setBaseBackgroundColor(backgroundColor);
1457 bool FrameView::shouldUpdateWhileOffscreen() const
1459 return m_shouldUpdateWhileOffscreen;
1462 void FrameView::setShouldUpdateWhileOffscreen(bool shouldUpdateWhileOffscreen)
1464 m_shouldUpdateWhileOffscreen = shouldUpdateWhileOffscreen;
1467 void FrameView::scheduleEvent(PassRefPtr<Event> event, PassRefPtr<Node> eventTarget)
1469 if (!m_enqueueEvents) {
1470 ExceptionCode ec = 0;
1471 eventTarget->dispatchEvent(event, ec);
1475 ScheduledEvent* scheduledEvent = new ScheduledEvent;
1476 scheduledEvent->m_event = event;
1477 scheduledEvent->m_eventTarget = eventTarget;
1478 m_scheduledEvents.append(scheduledEvent);
1481 void FrameView::pauseScheduledEvents()
1483 ASSERT(m_scheduledEvents.isEmpty() || m_enqueueEvents);
1487 void FrameView::resumeScheduledEvents()
1490 if (!m_enqueueEvents)
1491 dispatchScheduledEvents();
1492 ASSERT(m_scheduledEvents.isEmpty() || m_enqueueEvents);
1495 void FrameView::scrollToAnchor()
1497 RefPtr<Node> anchorNode = m_maintainScrollPositionAnchor;
1501 if (!anchorNode->renderer())
1505 if (anchorNode != m_frame->document())
1506 rect = anchorNode->getRect();
1508 // Scroll nested layers and frames to reveal the anchor.
1509 // Align to the top and to the closest side (this matches other browsers).
1510 anchorNode->renderer()->enclosingLayer()->scrollRectToVisible(rect, true, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignTopAlways);
1512 if (AXObjectCache::accessibilityEnabled())
1513 m_frame->document()->axObjectCache()->handleScrolledToAnchor(anchorNode.get());
1515 // scrollRectToVisible can call into setScrollPosition(), which resets m_maintainScrollPositionAnchor.
1516 m_maintainScrollPositionAnchor = anchorNode;
1519 bool FrameView::updateWidgets()
1521 if (m_nestedLayoutCount > 1 || !m_widgetUpdateSet || m_widgetUpdateSet->isEmpty())
1524 size_t size = m_widgetUpdateSet->size();
1526 Vector<RenderEmbeddedObject*> objects;
1527 objects.reserveCapacity(size);
1529 RenderEmbeddedObjectSet::const_iterator end = m_widgetUpdateSet->end();
1530 for (RenderEmbeddedObjectSet::const_iterator it = m_widgetUpdateSet->begin(); it != end; ++it) {
1531 objects.uncheckedAppend(*it);
1535 for (size_t i = 0; i < size; ++i) {
1536 RenderEmbeddedObject* object = objects[i];
1538 // The object may have been destroyed, but our manual ref() keeps the object from being deleted.
1539 object->updateWidget(false);
1540 object->updateWidgetPosition();
1542 m_widgetUpdateSet->remove(object);
1545 RenderArena* arena = m_frame->document()->renderArena();
1546 for (size_t i = 0; i < size; ++i)
1547 objects[i]->deref(arena);
1549 return m_widgetUpdateSet->isEmpty();
1552 void FrameView::performPostLayoutTasks()
1554 if (m_firstLayoutCallbackPending) {
1555 m_firstLayoutCallbackPending = false;
1556 m_frame->loader()->didFirstLayout();
1559 if (m_isVisuallyNonEmpty && m_firstVisuallyNonEmptyLayoutCallbackPending) {
1560 m_firstVisuallyNonEmptyLayoutCallbackPending = false;
1561 m_frame->loader()->didFirstVisuallyNonEmptyLayout();
1564 RenderView* root = m_frame->contentRenderer();
1566 root->updateWidgetPositions();
1568 for (unsigned i = 0; i < maxUpdateWidgetsIterations; i++) {
1569 if (updateWidgets())
1575 resumeScheduledEvents();
1577 if (!root->printing()) {
1578 IntSize currentSize = IntSize(width(), height());
1579 float currentZoomFactor = root->style()->zoom();
1580 bool resized = !m_firstLayout && (currentSize != m_lastLayoutSize || currentZoomFactor != m_lastZoomFactor);
1581 m_lastLayoutSize = currentSize;
1582 m_lastZoomFactor = currentZoomFactor;
1584 m_frame->eventHandler()->sendResizeEvent();
1588 void FrameView::postLayoutTimerFired(Timer<FrameView>*)
1590 performPostLayoutTasks();
1593 void FrameView::updateOverflowStatus(bool horizontalOverflow, bool verticalOverflow)
1595 if (!m_viewportRenderer)
1598 if (m_overflowStatusDirty) {
1599 m_horizontalOverflow = horizontalOverflow;
1600 m_verticalOverflow = verticalOverflow;
1601 m_overflowStatusDirty = false;
1605 bool horizontalOverflowChanged = (m_horizontalOverflow != horizontalOverflow);
1606 bool verticalOverflowChanged = (m_verticalOverflow != verticalOverflow);
1608 if (horizontalOverflowChanged || verticalOverflowChanged) {
1609 m_horizontalOverflow = horizontalOverflow;
1610 m_verticalOverflow = verticalOverflow;
1612 scheduleEvent(OverflowEvent::create(horizontalOverflowChanged, horizontalOverflow,
1613 verticalOverflowChanged, verticalOverflow),
1614 m_viewportRenderer->node());
1619 void FrameView::dispatchScheduledEvents()
1621 if (m_scheduledEvents.isEmpty())
1624 Vector<ScheduledEvent*> scheduledEventsCopy = m_scheduledEvents;
1625 m_scheduledEvents.clear();
1627 Vector<ScheduledEvent*>::iterator end = scheduledEventsCopy.end();
1628 for (Vector<ScheduledEvent*>::iterator it = scheduledEventsCopy.begin(); it != end; ++it) {
1629 ScheduledEvent* scheduledEvent = *it;
1631 ExceptionCode ec = 0;
1633 // Only dispatch events to nodes that are in the document
1634 if (scheduledEvent->m_eventTarget->inDocument())
1635 scheduledEvent->m_eventTarget->dispatchEvent(scheduledEvent->m_event, ec);
1637 delete scheduledEvent;
1641 IntRect FrameView::windowClipRect(bool clipToContents) const
1643 ASSERT(m_frame->view() == this);
1645 // Set our clip rect to be our contents.
1646 IntRect clipRect = contentsToWindow(visibleContentRect(!clipToContents));
1647 if (!m_frame || !m_frame->document()->ownerElement())
1650 // Take our owner element and get the clip rect from the enclosing layer.
1651 Element* elt = m_frame->document()->ownerElement();
1652 RenderLayer* layer = elt->renderer()->enclosingLayer();
1653 // FIXME: layer should never be null, but sometimes seems to be anyway.
1656 FrameView* parentView = elt->document()->view();
1657 clipRect.intersect(parentView->windowClipRectForLayer(layer, true));
1661 IntRect FrameView::windowClipRectForLayer(const RenderLayer* layer, bool clipToLayerContents) const
1663 // If we have no layer, just return our window clip rect.
1665 return windowClipRect();
1667 // Apply the clip from the layer.
1669 if (clipToLayerContents)
1670 clipRect = layer->childrenClipRect();
1672 clipRect = layer->selfClipRect();
1673 clipRect = contentsToWindow(clipRect);
1674 return intersection(clipRect, windowClipRect());
1677 bool FrameView::isActive() const
1679 Page* page = frame()->page();
1680 return page && page->focusController()->isActive();
1683 void FrameView::valueChanged(Scrollbar* bar)
1685 // Figure out if we really moved.
1686 IntSize offset = scrollOffset();
1687 ScrollView::valueChanged(bar);
1688 if (offset != scrollOffset())
1689 frame()->eventHandler()->sendScrollEvent();
1690 frame()->loader()->client()->didChangeScrollOffset();
1693 void FrameView::invalidateScrollbarRect(Scrollbar* scrollbar, const IntRect& rect)
1695 // Add in our offset within the FrameView.
1696 IntRect dirtyRect = rect;
1697 dirtyRect.move(scrollbar->x(), scrollbar->y());
1698 invalidateRect(dirtyRect);
1701 void FrameView::getTickmarks(Vector<IntRect>& tickmarks) const
1703 tickmarks = frame()->document()->renderedRectsForMarkers(DocumentMarker::TextMatch);
1706 IntRect FrameView::windowResizerRect() const
1708 Page* page = frame() ? frame()->page() : 0;
1711 return page->chrome()->windowResizerRect();
1714 #if ENABLE(DASHBOARD_SUPPORT)
1715 void FrameView::updateDashboardRegions()
1717 Document* document = m_frame->document();
1718 if (!document->hasDashboardRegions())
1720 Vector<DashboardRegionValue> newRegions;
1721 document->renderBox()->collectDashboardRegions(newRegions);
1722 if (newRegions == document->dashboardRegions())
1724 document->setDashboardRegions(newRegions);
1725 Page* page = m_frame->page();
1728 page->chrome()->client()->dashboardRegionsChanged();
1732 void FrameView::invalidateScrollCorner()
1734 invalidateRect(scrollCornerRect());
1737 void FrameView::updateScrollCorner()
1739 RenderObject* renderer = 0;
1740 RefPtr<RenderStyle> cornerStyle;
1742 if (!scrollCornerRect().isEmpty()) {
1743 // Try the <body> element first as a scroll corner source.
1744 Document* doc = m_frame->document();
1745 Element* body = doc ? doc->body() : 0;
1746 if (body && body->renderer()) {
1747 renderer = body->renderer();
1748 cornerStyle = renderer->getUncachedPseudoStyle(SCROLLBAR_CORNER, renderer->style());
1752 // If the <body> didn't have a custom style, then the root element might.
1753 Element* docElement = doc ? doc->documentElement() : 0;
1754 if (docElement && docElement->renderer()) {
1755 renderer = docElement->renderer();
1756 cornerStyle = renderer->getUncachedPseudoStyle(SCROLLBAR_CORNER, renderer->style());
1761 // If we have an owning iframe/frame element, then it can set the custom scrollbar also.
1762 if (RenderPart* renderer = m_frame->ownerRenderer())
1763 cornerStyle = renderer->getUncachedPseudoStyle(SCROLLBAR_CORNER, renderer->style());
1768 if (!m_scrollCorner)
1769 m_scrollCorner = new (renderer->renderArena()) RenderScrollbarPart(renderer->document());
1770 m_scrollCorner->setStyle(cornerStyle.release());
1771 invalidateRect(scrollCornerRect());
1772 } else if (m_scrollCorner) {
1773 m_scrollCorner->destroy();
1778 void FrameView::paintScrollCorner(GraphicsContext* context, const IntRect& cornerRect)
1780 if (context->updatingControlTints()) {
1781 updateScrollCorner();
1785 if (m_scrollCorner) {
1786 m_scrollCorner->paintIntoRect(context, cornerRect.x(), cornerRect.y(), cornerRect);
1790 ScrollView::paintScrollCorner(context, cornerRect);
1793 bool FrameView::hasCustomScrollbars() const
1795 const HashSet<RefPtr<Widget> >* viewChildren = children();
1796 HashSet<RefPtr<Widget> >::const_iterator end = viewChildren->end();
1797 for (HashSet<RefPtr<Widget> >::const_iterator current = viewChildren->begin(); current != end; ++current) {
1798 Widget* widget = current->get();
1799 if (widget->isFrameView()) {
1800 if (static_cast<FrameView*>(widget)->hasCustomScrollbars())
1802 } else if (widget->isScrollbar()) {
1803 Scrollbar* scrollbar = static_cast<Scrollbar*>(widget);
1804 if (scrollbar->isCustomScrollbar())
1812 void FrameView::updateControlTints()
1814 // This is called when control tints are changed from aqua/graphite to clear and vice versa.
1815 // We do a "fake" paint, and when the theme gets a paint call, it can then do an invalidate.
1816 // This is only done if the theme supports control tinting. It's up to the theme and platform
1817 // to define when controls get the tint and to call this function when that changes.
1819 // Optimize the common case where we bring a window to the front while it's still empty.
1820 if (!m_frame || m_frame->loader()->url().isEmpty())
1823 if ((m_frame->contentRenderer() && m_frame->contentRenderer()->theme()->supportsControlTints()) || hasCustomScrollbars()) {
1826 PlatformGraphicsContext* const noContext = 0;
1827 GraphicsContext context(noContext);
1828 context.setUpdatingControlTints(true);
1829 if (platformWidget())
1830 paintContents(&context, visibleContentRect());
1832 paint(&context, frameRect());
1836 bool FrameView::wasScrolledByUser() const
1838 return m_wasScrolledByUser;
1841 void FrameView::setWasScrolledByUser(bool wasScrolledByUser)
1843 if (m_inProgrammaticScroll)
1845 m_maintainScrollPositionAnchor = 0;
1846 m_wasScrolledByUser = wasScrolledByUser;
1849 void FrameView::paintContents(GraphicsContext* p, const IntRect& rect)
1854 #if ENABLE(INSPECTOR)
1855 if (InspectorTimelineAgent* timelineAgent = inspectorTimelineAgent())
1856 timelineAgent->willPaint(rect);
1859 Document* document = frame()->document();
1863 if (document->printing())
1864 fillWithRed = false; // Printing, don't fill with red (can't remember why).
1865 else if (document->ownerElement())
1866 fillWithRed = false; // Subframe, don't fill with red.
1867 else if (isTransparent())
1868 fillWithRed = false; // Transparent, don't fill with red.
1869 else if (m_paintBehavior & PaintBehaviorSelectionOnly)
1870 fillWithRed = false; // Selections are transparent, don't fill with red.
1871 else if (m_nodeToDraw)
1872 fillWithRed = false; // Element images are transparent, don't fill with red.
1877 p->fillRect(rect, Color(0xFF, 0, 0), DeviceColorSpace);
1880 bool isTopLevelPainter = !sCurrentPaintTimeStamp;
1881 if (isTopLevelPainter)
1882 sCurrentPaintTimeStamp = currentTime();
1884 RenderView* contentRenderer = frame()->contentRenderer();
1885 if (!contentRenderer) {
1886 LOG_ERROR("called Frame::paint with nil renderer");
1890 ASSERT(!needsLayout());
1894 #if USE(ACCELERATED_COMPOSITING)
1895 if (!p->paintingDisabled()) {
1896 if (GraphicsLayer* rootLayer = contentRenderer->compositor()->rootPlatformLayer())
1897 rootLayer->syncCompositingState();
1901 ASSERT(!m_isPainting);
1903 m_isPainting = true;
1905 // m_nodeToDraw is used to draw only one element (and its descendants)
1906 RenderObject* eltRenderer = m_nodeToDraw ? m_nodeToDraw->renderer() : 0;
1908 PaintBehavior oldPaintBehavior = m_paintBehavior;
1909 if (m_paintBehavior == PaintBehaviorNormal)
1910 document->invalidateRenderedRectsForMarkersInRect(rect);
1912 if (document->printing())
1913 m_paintBehavior |= PaintBehaviorFlattenCompositingLayers;
1915 contentRenderer->layer()->paint(p, rect, m_paintBehavior, eltRenderer);
1917 m_paintBehavior = oldPaintBehavior;
1919 m_isPainting = false;
1920 m_lastPaintTime = currentTime();
1922 #if ENABLE(DASHBOARD_SUPPORT)
1923 // Regions may have changed as a result of the visibility/z-index of element changing.
1924 if (document->dashboardRegionsDirty())
1925 updateDashboardRegions();
1928 if (isTopLevelPainter)
1929 sCurrentPaintTimeStamp = 0;
1931 #if ENABLE(INSPECTOR)
1932 if (InspectorTimelineAgent* timelineAgent = inspectorTimelineAgent())
1933 timelineAgent->didPaint();
1937 void FrameView::setPaintBehavior(PaintBehavior behavior)
1939 m_paintBehavior = behavior;
1942 PaintBehavior FrameView::paintBehavior() const
1944 return m_paintBehavior;
1947 bool FrameView::isPainting() const
1949 return m_isPainting;
1952 void FrameView::setNodeToDraw(Node* node)
1954 m_nodeToDraw = node;
1957 void FrameView::layoutIfNeededRecursive()
1959 // We have to crawl our entire tree looking for any FrameViews that need
1960 // layout and make sure they are up to date.
1961 // Mac actually tests for intersection with the dirty region and tries not to
1962 // update layout for frames that are outside the dirty region. Not only does this seem
1963 // pointless (since those frames will have set a zero timer to layout anyway), but
1964 // it is also incorrect, since if two frames overlap, the first could be excluded from the dirty
1965 // region but then become included later by the second frame adding rects to the dirty region
1966 // when it lays out.
1971 const HashSet<RefPtr<Widget> >* viewChildren = children();
1972 HashSet<RefPtr<Widget> >::const_iterator end = viewChildren->end();
1973 for (HashSet<RefPtr<Widget> >::const_iterator current = viewChildren->begin(); current != end; ++current) {
1974 Widget* widget = (*current).get();
1975 if (widget->isFrameView())
1976 static_cast<FrameView*>(widget)->layoutIfNeededRecursive();
1979 // layoutIfNeededRecursive is called when we need to make sure layout is up-to-date before
1980 // painting, so we need to flush out any deferred repaints too.
1981 flushDeferredRepaints();
1984 void FrameView::flushDeferredRepaints()
1986 if (!m_deferredRepaintTimer.isActive())
1988 m_deferredRepaintTimer.stop();
1989 doDeferredRepaints();
1992 void FrameView::forceLayout(bool allowSubtree)
1994 layout(allowSubtree);
1995 // We cannot unschedule a pending relayout, since the force can be called with
1996 // a tiny rectangle from a drawRect update. By unscheduling we in effect
1997 // "validate" and stop the necessary full repaint from occurring. Basically any basic
1998 // append/remove DHTML is broken by this call. For now, I have removed the optimization
1999 // until we have a better invalidation stategy. -dwh
2000 //unscheduleRelayout();
2003 void FrameView::forceLayoutWithPageWidthRange(float minPageWidth, float maxPageWidth, bool _adjustViewSize)
2005 // Dumping externalRepresentation(m_frame->renderer()).ascii() is a good trick to see
2006 // the state of things before and after the layout
2007 RenderView *root = toRenderView(m_frame->document()->renderer());
2009 // This magic is basically copied from khtmlview::print
2010 int pageW = (int)ceilf(minPageWidth);
2011 root->setWidth(pageW);
2012 root->setNeedsLayoutAndPrefWidthsRecalc();
2015 // If we don't fit in the minimum page width, we'll lay out again. If we don't fit in the
2016 // maximum page width, we will lay out to the maximum page width and clip extra content.
2017 // FIXME: We are assuming a shrink-to-fit printing implementation. A cropping
2018 // implementation should not do this!
2019 int rightmostPos = root->rightmostPosition();
2020 if (rightmostPos > minPageWidth) {
2021 pageW = std::min(rightmostPos, (int)ceilf(maxPageWidth));
2022 root->setWidth(pageW);
2023 root->setNeedsLayoutAndPrefWidthsRecalc();
2028 if (_adjustViewSize)
2032 void FrameView::adjustPageHeight(float *newBottom, float oldTop, float oldBottom, float /*bottomLimit*/)
2034 RenderView* root = m_frame->contentRenderer();
2036 // Use a context with painting disabled.
2037 GraphicsContext context((PlatformGraphicsContext*)0);
2038 root->setTruncatedAt((int)floorf(oldBottom));
2039 IntRect dirtyRect(0, (int)floorf(oldTop), root->rightLayoutOverflow(), (int)ceilf(oldBottom - oldTop));
2040 root->layer()->paint(&context, dirtyRect);
2041 *newBottom = root->bestTruncatedAt();
2042 if (*newBottom == 0)
2043 *newBottom = oldBottom;
2045 *newBottom = oldBottom;
2048 IntRect FrameView::convertFromRenderer(const RenderObject* renderer, const IntRect& rendererRect) const
2050 IntRect rect = renderer->localToAbsoluteQuad(FloatRect(rendererRect)).enclosingBoundingBox();
2052 // Convert from page ("absolute") to FrameView coordinates.
2053 rect.move(-scrollX(), -scrollY());
2058 IntRect FrameView::convertToRenderer(const RenderObject* renderer, const IntRect& viewRect) const
2060 IntRect rect = viewRect;
2062 // Convert from FrameView coords into page ("absolute") coordinates.
2063 rect.move(scrollX(), scrollY());
2065 // FIXME: we don't have a way to map an absolute rect down to a local quad, so just
2066 // move the rect for now.
2067 rect.setLocation(roundedIntPoint(renderer->absoluteToLocal(rect.location(), false, true /* use transforms */)));
2071 IntPoint FrameView::convertFromRenderer(const RenderObject* renderer, const IntPoint& rendererPoint) const
2073 IntPoint point = roundedIntPoint(renderer->localToAbsolute(rendererPoint, false, true /* use transforms */));
2075 // Convert from page ("absolute") to FrameView coordinates.
2076 point.move(-scrollX(), -scrollY());
2080 IntPoint FrameView::convertToRenderer(const RenderObject* renderer, const IntPoint& viewPoint) const
2082 IntPoint point = viewPoint;
2084 // Convert from FrameView coords into page ("absolute") coordinates.
2085 point += IntSize(scrollX(), scrollY());
2087 return roundedIntPoint(renderer->absoluteToLocal(point, false, true /* use transforms */));
2090 IntRect FrameView::convertToContainingView(const IntRect& localRect) const
2092 if (const ScrollView* parentScrollView = parent()) {
2093 if (parentScrollView->isFrameView()) {
2094 const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2095 // Get our renderer in the parent view
2096 RenderPart* renderer = m_frame->ownerRenderer();
2100 IntRect rect(localRect);
2101 // Add borders and padding??
2102 rect.move(renderer->borderLeft() + renderer->paddingLeft(),
2103 renderer->borderTop() + renderer->paddingTop());
2104 return parentView->convertFromRenderer(renderer, rect);
2107 return Widget::convertToContainingView(localRect);
2113 IntRect FrameView::convertFromContainingView(const IntRect& parentRect) const
2115 if (const ScrollView* parentScrollView = parent()) {
2116 if (parentScrollView->isFrameView()) {
2117 const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2119 // Get our renderer in the parent view
2120 RenderPart* renderer = m_frame->ownerRenderer();
2124 IntRect rect = parentView->convertToRenderer(renderer, parentRect);
2125 // Subtract borders and padding
2126 rect.move(-renderer->borderLeft() - renderer->paddingLeft(),
2127 -renderer->borderTop() - renderer->paddingTop());
2131 return Widget::convertFromContainingView(parentRect);
2137 IntPoint FrameView::convertToContainingView(const IntPoint& localPoint) const
2139 if (const ScrollView* parentScrollView = parent()) {
2140 if (parentScrollView->isFrameView()) {
2141 const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2143 // Get our renderer in the parent view
2144 RenderPart* renderer = m_frame->ownerRenderer();
2148 IntPoint point(localPoint);
2150 // Add borders and padding
2151 point.move(renderer->borderLeft() + renderer->paddingLeft(),
2152 renderer->borderTop() + renderer->paddingTop());
2153 return parentView->convertFromRenderer(renderer, point);
2156 return Widget::convertToContainingView(localPoint);
2162 IntPoint FrameView::convertFromContainingView(const IntPoint& parentPoint) const
2164 if (const ScrollView* parentScrollView = parent()) {
2165 if (parentScrollView->isFrameView()) {
2166 const FrameView* parentView = static_cast<const FrameView*>(parentScrollView);
2168 // Get our renderer in the parent view
2169 RenderPart* renderer = m_frame->ownerRenderer();
2173 IntPoint point = parentView->convertToRenderer(renderer, parentPoint);
2174 // Subtract borders and padding
2175 point.move(-renderer->borderLeft() - renderer->paddingLeft(),
2176 -renderer->borderTop() - renderer->paddingTop());
2180 return Widget::convertFromContainingView(parentPoint);
2186 bool FrameView::shouldApplyTextZoom() const
2188 if (m_zoomFactor == 1)
2192 Page* page = m_frame->page();
2193 return page && page->settings()->zoomMode() == ZoomTextOnly;
2196 bool FrameView::shouldApplyPageZoom() const
2198 if (m_zoomFactor == 1)
2202 Page* page = m_frame->page();
2203 return page && page->settings()->zoomMode() == ZoomPage;
2206 void FrameView::setZoomFactor(float percent, ZoomMode mode)
2211 Page* page = m_frame->page();
2215 if (m_zoomFactor == percent && page->settings()->zoomMode() == mode)
2218 Document* document = m_frame->document();
2223 // Respect SVGs zoomAndPan="disabled" property in standalone SVG documents.
2224 // FIXME: How to handle compound documents + zoomAndPan="disabled"? Needs SVG WG clarification.
2225 if (document->isSVGDocument()) {
2226 if (!static_cast<SVGDocument*>(document)->zoomAndPanEnabled())
2228 if (document->renderer())
2229 document->renderer()->setNeedsLayout(true);
2233 if (mode == ZoomPage) {
2234 // Update the scroll position when doing a full page zoom, so the content stays in relatively the same position.
2235 IntPoint scrollPosition = this->scrollPosition();
2236 float percentDifference = (percent / m_zoomFactor);
2237 setScrollPosition(IntPoint(scrollPosition.x() * percentDifference, scrollPosition.y() * percentDifference));
2240 m_zoomFactor = percent;
2241 page->settings()->setZoomMode(mode);
2243 document->recalcStyle(Node::Force);
2245 for (Frame* child = m_frame->tree()->firstChild(); child; child = child->tree()->nextSibling()) {
2246 if (FrameView* childView = child->view())
2247 childView->setZoomFactor(m_zoomFactor, mode);
2250 if (document->renderer() && document->renderer()->needsLayout() && didFirstLayout())
2254 } // namespace WebCore