2 Copyright (C) 1997 Martin Jones (mjones@kde.org)
3 (C) 1998 Waldo Bastian (bastian@kde.org)
4 (C) 1998, 1999 Torben Weis (weis@kde.org)
5 (C) 1999 Lars Knoll (knoll@kde.org)
6 (C) 1999 Antti Koivisto (koivisto@kde.org)
7 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public License
20 along with this library; see the file COPYING.LIB. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.
28 #include "core/frame/FrameViewAutoSizeInfo.h"
29 #include "core/rendering/PaintPhase.h"
30 #include "platform/RuntimeEnabledFeatures.h"
31 #include "platform/geometry/LayoutRect.h"
32 #include "platform/graphics/Color.h"
33 #include "platform/scroll/ScrollView.h"
34 #include "wtf/Forward.h"
35 #include "wtf/OwnPtr.h"
36 #include "wtf/text/WTFString.h"
41 class DocumentLifecycle;
45 class HTMLFrameOwnerElement;
51 class RenderEmbeddedObject;
53 class RenderScrollbarPart;
56 struct CompositedSelectionBound;
58 typedef unsigned long long DOMTimeStamp;
60 // FIXME: Oilpan: move Widget (and thereby FrameView) to the heap.
61 class FrameView FINAL : public ScrollView {
63 friend class RenderView;
64 friend class Internals;
66 static PassRefPtr<FrameView> create(LocalFrame*);
67 static PassRefPtr<FrameView> create(LocalFrame*, const IntSize& initialSize);
71 virtual HostWindow* hostWindow() const OVERRIDE;
73 virtual void invalidateRect(const IntRect&) OVERRIDE;
74 virtual void setFrameRect(const IntRect&) OVERRIDE;
76 LocalFrame& frame() const
84 RenderView* renderView() const;
86 virtual void setCanHaveScrollbars(bool) OVERRIDE;
88 virtual PassRefPtr<Scrollbar> createScrollbar(ScrollbarOrientation) OVERRIDE;
90 virtual void setContentsSize(const IntSize&) OVERRIDE;
91 IntPoint clampOffsetAtScale(const IntPoint& offset, float scale) const;
93 void layout(bool allowSubtree = true);
94 bool didFirstLayout() const;
95 void scheduleRelayout();
96 void scheduleRelayoutOfSubtree(RenderObject*);
97 bool layoutPending() const;
98 bool isInPerformLayout() const;
100 void setCanInvalidatePaintDuringPerformLayout(bool b) { m_canInvalidatePaintDuringPerformLayout = b; }
101 bool canInvalidatePaintDuringPerformLayout() const { return m_canInvalidatePaintDuringPerformLayout; }
103 RenderObject* layoutRoot(bool onlyDuringLayout = false) const;
104 void clearLayoutSubtreeRoot() { m_layoutSubtreeRoot = 0; }
105 int layoutCount() const { return m_layoutCount; }
107 bool needsLayout() const;
108 void setNeedsLayout();
110 void setNeedsUpdateWidgetPositions() { m_needsUpdateWidgetPositions = true; }
112 // Methods for getting/setting the size Blink should use to layout the contents.
113 IntSize layoutSize(IncludeScrollbarsInRect = ExcludeScrollbars) const;
114 void setLayoutSize(const IntSize&);
116 // If this is set to false, the layout size will need to be explicitly set by the owner.
117 // E.g. WebViewImpl sets its mainFrame's layout size manually
118 void setLayoutSizeFixedToFrameSize(bool isFixed) { m_layoutSizeFixedToFrameSize = isFixed; }
119 bool layoutSizeFixedToFrameSize() { return m_layoutSizeFixedToFrameSize; }
121 bool needsFullPaintInvalidation() const { return m_doFullPaintInvalidation; }
123 void updateAcceleratedCompositingSettings();
125 void recalcOverflowAfterStyleChange();
127 bool isEnclosedInCompositingLayer() const;
129 void resetScrollbars();
130 void prepareForDetach();
131 void detachCustomScrollbars();
132 virtual void recalculateScrollbarOverlayStyle();
136 bool isTransparent() const;
137 void setTransparent(bool isTransparent);
139 // True if the FrameView is not transparent, and the base background color is opaque.
140 bool hasOpaqueBackground() const;
142 Color baseBackgroundColor() const;
143 void setBaseBackgroundColor(const Color&);
144 void updateBackgroundRecursively(const Color&, bool);
146 void adjustViewSize();
148 virtual IntRect windowClipRect(IncludeScrollbarsInRect = ExcludeScrollbars) const OVERRIDE;
149 IntRect windowClipRectForFrameOwner(const HTMLFrameOwnerElement*) const;
151 virtual IntRect windowResizerRect() const OVERRIDE;
153 virtual float visibleContentScaleFactor() const OVERRIDE { return m_visibleContentScaleFactor; }
154 void setVisibleContentScaleFactor(float);
156 virtual float inputEventsScaleFactor() const OVERRIDE;
157 virtual IntSize inputEventsOffsetForEmulation() const OVERRIDE;
158 void setInputEventsTransformForEmulation(const IntSize&, float);
160 virtual void setScrollPosition(const IntPoint&, ScrollBehavior = ScrollBehaviorInstant) OVERRIDE;
161 virtual bool isRubberBandInProgress() const OVERRIDE;
162 void setScrollPositionNonProgrammatically(const IntPoint&);
164 // This is different than visibleContentRect() in that it ignores negative (or overly positive)
165 // offsets from rubber-banding, and it takes zooming into account.
166 LayoutRect viewportConstrainedVisibleContentRect() const;
167 void viewportConstrainedVisibleContentSizeChanged(bool widthChanged, bool heightChanged);
169 AtomicString mediaType() const;
170 void setMediaType(const AtomicString&);
171 void adjustMediaTypeForPrinting(bool printing);
173 void addSlowRepaintObject();
174 void removeSlowRepaintObject();
175 bool hasSlowRepaintObjects() const { return m_slowRepaintObjectCount; }
177 // Fixed-position objects.
178 typedef HashSet<RenderObject*> ViewportConstrainedObjectSet;
179 void addViewportConstrainedObject(RenderObject*);
180 void removeViewportConstrainedObject(RenderObject*);
181 const ViewportConstrainedObjectSet* viewportConstrainedObjects() const { return m_viewportConstrainedObjects.get(); }
182 bool hasViewportConstrainedObjects() const { return m_viewportConstrainedObjects && m_viewportConstrainedObjects->size() > 0; }
184 void handleLoadCompleted();
186 void updateAnnotatedRegions();
188 void restoreScrollbar();
190 void postLayoutTimerFired(Timer<FrameView>*);
192 bool wasScrolledByUser() const;
193 void setWasScrolledByUser(bool);
195 bool safeToPropagateScrollToParent() const { return m_safeToPropagateScrollToParent; }
196 void setSafeToPropagateScrollToParent(bool isSafe) { m_safeToPropagateScrollToParent = isSafe; }
198 void addWidget(RenderWidget*);
199 void removeWidget(RenderWidget*);
200 void updateWidgetPositions();
202 void addWidgetToUpdate(RenderEmbeddedObject&);
204 virtual void paintContents(GraphicsContext*, const IntRect& damageRect) OVERRIDE;
205 void setPaintBehavior(PaintBehavior);
206 PaintBehavior paintBehavior() const;
207 bool isPainting() const;
208 bool hasEverPainted() const { return m_lastPaintTime; }
209 void setNodeToDraw(Node*);
211 virtual void paintOverhangAreas(GraphicsContext*, const IntRect& horizontalOverhangArea, const IntRect& verticalOverhangArea, const IntRect& dirtyRect) OVERRIDE;
212 virtual void paintScrollCorner(GraphicsContext*, const IntRect& cornerRect) OVERRIDE;
213 virtual void paintScrollbar(GraphicsContext*, Scrollbar*, const IntRect&) OVERRIDE;
215 Color documentBackgroundColor() const;
217 static double currentFrameTimeStamp() { return s_currentFrameTimeStamp; }
219 void updateLayoutAndStyleForPainting();
220 void updateLayoutAndStyleIfNeededRecursive();
222 void invalidateTreeIfNeededRecursive();
224 void incrementVisuallyNonEmptyCharacterCount(unsigned);
225 void incrementVisuallyNonEmptyPixelCount(const IntSize&);
226 void setIsVisuallyNonEmpty() { m_isVisuallyNonEmpty = true; }
227 void enableAutoSizeMode(const IntSize& minSize, const IntSize& maxSize);
228 void disableAutoSizeMode() { m_autoSizeInfo.clear(); }
230 void forceLayout(bool allowSubtree = false);
231 void forceLayoutForPagination(const FloatSize& pageSize, const FloatSize& originalPageSize, float maximumShrinkFactor);
233 bool scrollToFragment(const KURL&);
234 bool scrollToAnchor(const String&);
235 void maintainScrollPositionAtAnchor(Node*);
236 void scrollElementToRect(Element*, const IntRect&);
237 void scrollContentsIfNeededRecursive();
239 // Methods to convert points and rects between the coordinate space of the renderer, and this view.
240 IntRect convertFromRenderer(const RenderObject&, const IntRect&) const;
241 IntRect convertToRenderer(const RenderObject&, const IntRect&) const;
242 IntPoint convertFromRenderer(const RenderObject&, const IntPoint&) const;
243 IntPoint convertToRenderer(const RenderObject&, const IntPoint&) const;
245 bool isFrameViewScrollCorner(RenderScrollbarPart* scrollCorner) const { return m_scrollCorner == scrollCorner; }
249 enum ScrollbarModesCalculationStrategy { RulesFromWebContentOnly, AnyRule };
250 void calculateScrollbarModesForLayoutAndSetViewportRenderer(ScrollbarMode& hMode, ScrollbarMode& vMode, ScrollbarModesCalculationStrategy = AnyRule);
252 virtual IntPoint lastKnownMousePosition() const OVERRIDE;
253 bool shouldSetCursor() const;
255 void setCursor(const Cursor&);
257 virtual bool scrollbarsCanBeActive() const OVERRIDE;
259 // FIXME: Remove this method once plugin loading is decoupled from layout.
260 void flushAnyPendingPostLayoutTasks();
262 virtual bool shouldSuspendScrollAnimations() const OVERRIDE;
263 virtual void scrollbarStyleChanged() OVERRIDE;
265 RenderBox* embeddedContentBox() const;
267 void setTracksPaintInvalidations(bool);
268 bool isTrackingPaintInvalidations() const { return m_isTrackingPaintInvalidations; }
269 void resetTrackedPaintInvalidations();
271 String trackedPaintInvalidationRectsAsText() const;
273 typedef HashSet<ScrollableArea*> ScrollableAreaSet;
274 void addScrollableArea(ScrollableArea*);
275 void removeScrollableArea(ScrollableArea*);
276 const ScrollableAreaSet* scrollableAreas() const { return m_scrollableAreas.get(); }
278 // With CSS style "resize:" enabled, a little resizer handle will appear at the bottom
279 // right of the object. We keep track of these resizer areas for checking if touches
280 // (implemented using Scroll gesture) are targeting the resizer.
281 typedef HashSet<RenderBox*> ResizerAreaSet;
282 void addResizerArea(RenderBox&);
283 void removeResizerArea(RenderBox&);
284 const ResizerAreaSet* resizerAreas() const { return m_resizerAreas.get(); }
286 virtual void setParent(Widget*) OVERRIDE;
287 virtual void removeChild(Widget*) OVERRIDE;
289 // This function exists for ports that need to handle wheel events manually.
290 // On Mac WebKit1 the underlying NSScrollView just does the scrolling, but on most other platforms
291 // we need this function in order to do the scroll ourselves.
292 bool wheelEvent(const PlatformWheelEvent&);
294 bool inProgrammaticScroll() const { return m_inProgrammaticScroll; }
295 void setInProgrammaticScroll(bool programmaticScroll) { m_inProgrammaticScroll = programmaticScroll; }
297 virtual bool isActive() const OVERRIDE;
299 // DEPRECATED: Use viewportConstrainedVisibleContentRect() instead.
300 IntSize scrollOffsetForFixedPosition() const;
302 // Override scrollbar notifications to update the AXObject cache.
303 virtual void didAddScrollbar(Scrollbar*, ScrollbarOrientation) OVERRIDE;
304 virtual void willRemoveScrollbar(Scrollbar*, ScrollbarOrientation) OVERRIDE;
306 // FIXME: This should probably be renamed as the 'inSubtreeLayout' parameter
307 // passed around the FrameView layout methods can be true while this returns
309 bool isSubtreeLayout() const { return !!m_layoutSubtreeRoot; }
311 // Sets the tickmarks for the FrameView, overriding the default behavior
312 // which is to display the tickmarks corresponding to find results.
313 // If |m_tickmarks| is empty, the default behavior is restored.
314 void setTickmarks(const Vector<IntRect>& tickmarks) { m_tickmarks = tickmarks; }
316 // Since the compositor can resize the viewport due to top controls and
317 // commit scroll offsets before a WebView::resize occurs, we need to adjust
318 // our scroll extents to prevent clamping the scroll offsets.
319 void setTopControlsViewportAdjustment(float);
321 virtual IntPoint maximumScrollPosition() const OVERRIDE;
323 // ScrollableArea interface
324 virtual void invalidateScrollbarRect(Scrollbar*, const IntRect&) OVERRIDE;
325 virtual void getTickmarks(Vector<IntRect>&) const OVERRIDE;
326 virtual void scrollTo(const IntSize&) OVERRIDE;
327 virtual IntRect scrollableAreaBoundingBox() const OVERRIDE;
328 virtual bool scrollAnimatorEnabled() const OVERRIDE;
329 virtual bool usesCompositedScrolling() const OVERRIDE;
330 virtual GraphicsLayer* layerForScrolling() const OVERRIDE;
331 virtual GraphicsLayer* layerForHorizontalScrollbar() const OVERRIDE;
332 virtual GraphicsLayer* layerForVerticalScrollbar() const OVERRIDE;
333 virtual GraphicsLayer* layerForScrollCorner() const OVERRIDE;
336 virtual bool scrollContentsFastPath(const IntSize& scrollDelta) OVERRIDE;
337 virtual void scrollContentsSlowPath(const IntRect& updateRect) OVERRIDE;
339 virtual bool isVerticalDocument() const OVERRIDE;
340 virtual bool isFlippedDocument() const OVERRIDE;
342 // Prevents creation of scrollbars. Used to prevent drawing two sets of
343 // overlay scrollbars in the case of the pinch viewport.
344 virtual bool scrollbarsDisabled() const OVERRIDE;
347 explicit FrameView(LocalFrame*);
352 virtual void frameRectsChanged() OVERRIDE;
353 virtual bool isFrameView() const OVERRIDE { return true; }
355 friend class RenderWidget;
357 bool contentsInCompositedLayer() const;
359 void applyOverflowToViewportAndSetRenderer(RenderObject*, ScrollbarMode& hMode, ScrollbarMode& vMode);
360 void updateOverflowStatus(bool horizontalOverflow, bool verticalOverflow);
362 void updateCounters();
363 void forceLayoutParentViewIfNeeded();
364 void performPreLayoutTasks();
365 void performLayout(RenderObject* rootForThisLayout, bool inSubtreeLayout);
366 void scheduleOrPerformPostLayoutTasks();
367 void performPostLayoutTasks();
369 void invalidateTreeIfNeeded();
371 void gatherDebugLayoutRects(RenderObject* layoutRoot);
373 DocumentLifecycle& lifecycle() const;
375 virtual void contentRectangleForPaintInvalidation(const IntRect&) OVERRIDE;
376 virtual void contentsResized() OVERRIDE;
377 virtual void scrollbarExistenceDidChange() OVERRIDE;
379 // Override ScrollView methods to do point conversion via renderers, in order to
380 // take transforms into account.
381 virtual IntRect convertToContainingView(const IntRect&) const OVERRIDE;
382 virtual IntRect convertFromContainingView(const IntRect&) const OVERRIDE;
383 virtual IntPoint convertToContainingView(const IntPoint&) const OVERRIDE;
384 virtual IntPoint convertFromContainingView(const IntPoint&) const OVERRIDE;
386 void updateWidgetPositionsIfNeeded();
388 bool wasViewportResized();
389 void sendResizeEventIfNeeded();
391 void updateScrollableAreaSet();
393 virtual void notifyPageThatContentAreaWillPaint() const OVERRIDE;
395 void scheduleUpdateWidgetsIfNecessary();
396 void updateWidgetsTimerFired(Timer<FrameView>*);
397 bool updateWidgets();
399 void scrollToAnchor();
400 void scrollPositionChanged();
401 void didScrollTimerFired(Timer<FrameView>*);
403 void updateLayersAndCompositingAfterScrollIfNeeded();
405 static bool computeCompositedSelectionBounds(LocalFrame&, CompositedSelectionBound& start, CompositedSelectionBound& end);
406 void updateCompositedSelectionBoundsIfNeeded();
408 bool hasCustomScrollbars() const;
409 bool shouldUseCustomScrollbars(Element*& customScrollbarElement, LocalFrame*& customScrollbarFrame);
411 virtual void updateScrollCorner() OVERRIDE;
413 FrameView* parentFrameView() const;
415 AXObjectCache* axObjectCache() const;
416 void removeFromAXObjectCache();
418 void setLayoutSizeInternal(const IntSize&);
420 bool paintInvalidationIsAllowed() const
422 return !isInPerformLayout() || canInvalidatePaintDuringPerformLayout();
425 static double s_currentFrameTimeStamp; // used for detecting decoded resource thrash in the cache
426 static bool s_inPaintContents;
430 typedef WillBeHeapHashSet<RefPtrWillBeMember<RenderEmbeddedObject> > EmbeddedObjectSet;
431 WillBePersistentHeapHashSet<RefPtrWillBeMember<RenderEmbeddedObject> > m_widgetUpdateSet;
433 // FIXME: These are just "children" of the FrameView and should be RefPtr<Widget> instead.
434 WillBePersistentHeapHashSet<RefPtrWillBeMember<RenderWidget> > m_widgets;
436 // Oilpan: the use of a persistent back reference 'emulates' the
437 // RefPtr-cycle that is kept between the two objects non-Oilpan.
439 // That cycle is broken when a LocalFrame is detached by
440 // FrameLoader::detachFromParent(), it then clears its
441 // FrameView's m_frame reference by calling setView(nullptr).
442 RefPtrWillBePersistent<LocalFrame> m_frame;
444 bool m_doFullPaintInvalidation;
446 bool m_canHaveScrollbars;
447 unsigned m_slowRepaintObjectCount;
449 bool m_hasPendingLayout;
450 RenderObject* m_layoutSubtreeRoot;
452 bool m_layoutSchedulingEnabled;
453 bool m_inPerformLayout;
454 bool m_canInvalidatePaintDuringPerformLayout;
455 bool m_inSynchronousPostLayout;
457 unsigned m_nestedLayoutCount;
458 Timer<FrameView> m_postLayoutTasksTimer;
459 Timer<FrameView> m_updateWidgetsTimer;
460 bool m_firstLayoutCallbackPending;
463 bool m_isTransparent;
464 Color m_baseBackgroundColor;
465 IntSize m_lastViewportSize;
466 float m_lastZoomFactor;
468 AtomicString m_mediaType;
469 AtomicString m_mediaTypeWhenNotPrinting;
471 bool m_overflowStatusDirty;
472 bool m_horizontalOverflow;
473 bool m_verticalOverflow;
474 RenderObject* m_viewportRenderer;
476 bool m_wasScrolledByUser;
477 bool m_inProgrammaticScroll;
478 bool m_safeToPropagateScrollToParent;
480 double m_lastPaintTime;
482 bool m_isTrackingPaintInvalidations; // Used for testing.
483 Vector<IntRect> m_trackedPaintInvalidationRects;
485 RefPtrWillBePersistent<Node> m_nodeToDraw;
486 PaintBehavior m_paintBehavior;
489 unsigned m_visuallyNonEmptyCharacterCount;
490 unsigned m_visuallyNonEmptyPixelCount;
491 bool m_isVisuallyNonEmpty;
492 bool m_firstVisuallyNonEmptyLayoutCallbackPending;
494 RefPtrWillBePersistent<Node> m_maintainScrollPositionAnchor;
496 // Renderer to hold our custom scroll corner.
497 RawPtrWillBePersistent<RenderScrollbarPart> m_scrollCorner;
499 OwnPtr<ScrollableAreaSet> m_scrollableAreas;
500 OwnPtr<ResizerAreaSet> m_resizerAreas;
501 OwnPtr<ViewportConstrainedObjectSet> m_viewportConstrainedObjects;
502 OwnPtr<FrameViewAutoSizeInfo> m_autoSizeInfo;
504 float m_visibleContentScaleFactor;
505 IntSize m_inputEventsOffsetForEmulation;
506 float m_inputEventsScaleFactorForEmulation;
508 IntSize m_layoutSize;
509 bool m_layoutSizeFixedToFrameSize;
511 Timer<FrameView> m_didScrollTimer;
513 Vector<IntRect> m_tickmarks;
515 bool m_needsUpdateWidgetPositions;
516 float m_topControlsViewportAdjustment;
519 inline void FrameView::incrementVisuallyNonEmptyCharacterCount(unsigned count)
521 if (m_isVisuallyNonEmpty)
523 m_visuallyNonEmptyCharacterCount += count;
524 // Use a threshold value to prevent very small amounts of visible content from triggering didFirstVisuallyNonEmptyLayout.
525 // The first few hundred characters rarely contain the interesting content of the page.
526 static const unsigned visualCharacterThreshold = 200;
527 if (m_visuallyNonEmptyCharacterCount > visualCharacterThreshold)
528 setIsVisuallyNonEmpty();
531 inline void FrameView::incrementVisuallyNonEmptyPixelCount(const IntSize& size)
533 if (m_isVisuallyNonEmpty)
535 m_visuallyNonEmptyPixelCount += size.width() * size.height();
536 // Use a threshold value to prevent very small amounts of visible content from triggering didFirstVisuallyNonEmptyLayout
537 static const unsigned visualPixelThreshold = 32 * 32;
538 if (m_visuallyNonEmptyPixelCount > visualPixelThreshold)
539 setIsVisuallyNonEmpty();
542 DEFINE_TYPE_CASTS(FrameView, Widget, widget, widget->isFrameView(), widget.isFrameView());
544 class AllowPaintInvalidationScope {
546 explicit AllowPaintInvalidationScope(FrameView* view)
548 , m_originalValue(view ? view->canInvalidatePaintDuringPerformLayout() : false)
553 m_view->setCanInvalidatePaintDuringPerformLayout(true);
556 ~AllowPaintInvalidationScope()
561 m_view->setCanInvalidatePaintDuringPerformLayout(m_originalValue);
565 bool m_originalValue;
570 #endif // FrameView_h