Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : /*
7 : * rendering object that is the root of the frame tree, which contains
8 : * the document's scrollbars and contains fixed-positioned elements
9 : */
10 :
11 : #include "mozilla/ViewportFrame.h"
12 :
13 : #include "mozilla/ServoRestyleManager.h"
14 : #include "nsGkAtoms.h"
15 : #include "nsIScrollableFrame.h"
16 : #include "nsSubDocumentFrame.h"
17 : #include "nsCanvasFrame.h"
18 : #include "nsAbsoluteContainingBlock.h"
19 : #include "GeckoProfiler.h"
20 : #include "nsIMozBrowserFrame.h"
21 : #include "nsPlaceholderFrame.h"
22 :
23 : using namespace mozilla;
24 : typedef nsAbsoluteContainingBlock::AbsPosReflowFlags AbsPosReflowFlags;
25 :
26 : ViewportFrame*
27 24 : NS_NewViewportFrame(nsIPresShell* aPresShell, nsStyleContext* aContext)
28 : {
29 24 : return new (aPresShell) ViewportFrame(aContext);
30 : }
31 :
32 24 : NS_IMPL_FRAMEARENA_HELPERS(ViewportFrame)
33 186 : NS_QUERYFRAME_HEAD(ViewportFrame)
34 0 : NS_QUERYFRAME_ENTRY(ViewportFrame)
35 186 : NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
36 :
37 : void
38 24 : ViewportFrame::Init(nsIContent* aContent,
39 : nsContainerFrame* aParent,
40 : nsIFrame* aPrevInFlow)
41 : {
42 24 : nsContainerFrame::Init(aContent, aParent, aPrevInFlow);
43 : // No need to call CreateView() here - the frame ctor will call SetView()
44 : // with the ViewManager's root view, so we'll assign it in SetViewInternal().
45 :
46 24 : nsIFrame* parent = nsLayoutUtils::GetCrossDocParentFrame(this);
47 24 : if (parent) {
48 0 : nsFrameState state = parent->GetStateBits();
49 :
50 0 : mState |= state & (NS_FRAME_IN_POPUP);
51 : }
52 24 : }
53 :
54 : void
55 53 : ViewportFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
56 : const nsRect& aDirtyRect,
57 : const nsDisplayListSet& aLists)
58 : {
59 106 : AUTO_PROFILER_LABEL("ViewportFrame::BuildDisplayList", GRAPHICS);
60 :
61 53 : if (nsIFrame* kid = mFrames.FirstChild()) {
62 : // make the kid's BorderBackground our own. This ensures that the canvas
63 : // frame's background becomes our own background and therefore appears
64 : // below negative z-index elements.
65 53 : BuildDisplayListForChild(aBuilder, kid, aDirtyRect, aLists);
66 : }
67 :
68 106 : nsDisplayList topLayerList;
69 53 : BuildDisplayListForTopLayer(aBuilder, &topLayerList);
70 53 : if (!topLayerList.IsEmpty()) {
71 : // Wrap the whole top layer in a single item with maximum z-index,
72 : // and append it at the very end, so that it stays at the topmost.
73 : nsDisplayWrapList* wrapList =
74 0 : new (aBuilder) nsDisplayWrapList(aBuilder, this, &topLayerList);
75 0 : wrapList->SetOverrideZIndex(
76 0 : std::numeric_limits<decltype(wrapList->ZIndex())>::max());
77 0 : aLists.PositionedDescendants()->AppendNewToTop(wrapList);
78 : }
79 53 : }
80 :
81 : #ifdef DEBUG
82 : /**
83 : * Returns whether we are going to put an element in the top layer for
84 : * fullscreen. This function should matches the CSS rule in ua.css.
85 : */
86 : static bool
87 0 : ShouldInTopLayerForFullscreen(Element* aElement)
88 : {
89 0 : if (!aElement->GetParent()) {
90 0 : return false;
91 : }
92 0 : nsCOMPtr<nsIMozBrowserFrame> browserFrame = do_QueryInterface(aElement);
93 0 : if (browserFrame && browserFrame->GetReallyIsBrowser()) {
94 0 : return false;
95 : }
96 0 : return true;
97 : }
98 : #endif // DEBUG
99 :
100 : static void
101 0 : BuildDisplayListForTopLayerFrame(nsDisplayListBuilder* aBuilder,
102 : nsIFrame* aFrame,
103 : nsDisplayList* aList)
104 : {
105 0 : nsRect dirty;
106 0 : DisplayListClipState::AutoClipMultiple clipState(aBuilder);
107 0 : nsDisplayListBuilder::AutoCurrentActiveScrolledRootSetter asrSetter(aBuilder);
108 : nsDisplayListBuilder::OutOfFlowDisplayData*
109 0 : savedOutOfFlowData = nsDisplayListBuilder::GetOutOfFlowData(aFrame);
110 0 : if (savedOutOfFlowData) {
111 0 : dirty = savedOutOfFlowData->mDirtyRect;
112 : // This function is called after we've finished building display items for
113 : // the root scroll frame. That means that the content clip from the root
114 : // scroll frame is no longer on aBuilder. However, we need to make sure
115 : // that the display items we build in this function have finite clipped
116 : // bounds with respect to the root ASR, so we restore the *combined clip*
117 : // that we saved earlier. The combined clip will include the clip from the
118 : // root scroll frame.
119 0 : clipState.SetClipChainForContainingBlockDescendants(
120 0 : savedOutOfFlowData->mCombinedClipChain);
121 : clipState.ClipContainingBlockDescendantsExtra(
122 0 : dirty + aBuilder->ToReferenceFrame(aFrame), nullptr);
123 0 : asrSetter.SetCurrentActiveScrolledRoot(
124 0 : savedOutOfFlowData->mContainingBlockActiveScrolledRoot);
125 : }
126 0 : nsDisplayList list;
127 0 : aFrame->BuildDisplayListForStackingContext(aBuilder, dirty, &list);
128 0 : aList->AppendToTop(&list);
129 0 : }
130 :
131 : void
132 53 : ViewportFrame::BuildDisplayListForTopLayer(nsDisplayListBuilder* aBuilder,
133 : nsDisplayList* aList)
134 : {
135 53 : nsIDocument* doc = PresContext()->Document();
136 106 : nsTArray<Element*> fullscreenStack = doc->GetFullscreenStack();
137 53 : for (Element* elem : fullscreenStack) {
138 0 : if (nsIFrame* frame = elem->GetPrimaryFrame()) {
139 : // There are two cases where an element in fullscreen is not in
140 : // the top layer:
141 : // 1. When building display list for purpose other than painting,
142 : // it is possible that there is inconsistency between the style
143 : // info and the content tree.
144 : // 2. This is an element which we are not going to put in the top
145 : // layer for fullscreen. See ShouldInTopLayerForFullscreen().
146 : // In both cases, we want to skip the frame here and paint it in
147 : // the normal path.
148 0 : if (frame->StyleDisplay()->mTopLayer == NS_STYLE_TOP_LAYER_NONE) {
149 0 : MOZ_ASSERT(!aBuilder->IsForPainting() ||
150 : !ShouldInTopLayerForFullscreen(elem));
151 0 : continue;
152 : }
153 0 : MOZ_ASSERT(ShouldInTopLayerForFullscreen(elem));
154 : // Inner SVG, MathML elements, as well as children of some XUL
155 : // elements are not allowed to be out-of-flow. They should not
156 : // be handled as top layer element here.
157 0 : if (!(frame->GetStateBits() & NS_FRAME_OUT_OF_FLOW)) {
158 0 : MOZ_ASSERT(!elem->GetParent()->IsHTMLElement(), "HTML element "
159 : "should always be out-of-flow if in the top layer");
160 0 : continue;
161 : }
162 0 : if (nsIFrame* backdropPh =
163 0 : frame->GetChildList(kBackdropList).FirstChild()) {
164 0 : MOZ_ASSERT(backdropPh->IsPlaceholderFrame());
165 : nsIFrame* backdropFrame =
166 0 : static_cast<nsPlaceholderFrame*>(backdropPh)->GetOutOfFlowFrame();
167 0 : MOZ_ASSERT(backdropFrame);
168 0 : BuildDisplayListForTopLayerFrame(aBuilder, backdropFrame, aList);
169 : }
170 0 : BuildDisplayListForTopLayerFrame(aBuilder, frame, aList);
171 : }
172 : }
173 :
174 53 : nsIPresShell* shell = PresContext()->PresShell();
175 53 : if (nsCanvasFrame* canvasFrame = shell->GetCanvasFrame()) {
176 20 : if (Element* container = canvasFrame->GetCustomContentContainer()) {
177 20 : if (nsIFrame* frame = container->GetPrimaryFrame()) {
178 0 : BuildDisplayListForTopLayerFrame(aBuilder, frame, aList);
179 : }
180 : }
181 : }
182 53 : }
183 :
184 : #ifdef DEBUG
185 : void
186 0 : ViewportFrame::AppendFrames(ChildListID aListID,
187 : nsFrameList& aFrameList)
188 : {
189 0 : NS_ASSERTION(aListID == kPrincipalList, "unexpected child list");
190 0 : NS_ASSERTION(GetChildList(aListID).IsEmpty(), "Shouldn't have any kids!");
191 0 : nsContainerFrame::AppendFrames(aListID, aFrameList);
192 0 : }
193 :
194 : void
195 0 : ViewportFrame::InsertFrames(ChildListID aListID,
196 : nsIFrame* aPrevFrame,
197 : nsFrameList& aFrameList)
198 : {
199 0 : NS_ASSERTION(aListID == kPrincipalList, "unexpected child list");
200 0 : NS_ASSERTION(GetChildList(aListID).IsEmpty(), "Shouldn't have any kids!");
201 0 : nsContainerFrame::InsertFrames(aListID, aPrevFrame, aFrameList);
202 0 : }
203 :
204 : void
205 0 : ViewportFrame::RemoveFrame(ChildListID aListID,
206 : nsIFrame* aOldFrame)
207 : {
208 0 : NS_ASSERTION(aListID == kPrincipalList, "unexpected child list");
209 0 : nsContainerFrame::RemoveFrame(aListID, aOldFrame);
210 0 : }
211 : #endif
212 :
213 : /* virtual */ nscoord
214 0 : ViewportFrame::GetMinISize(gfxContext *aRenderingContext)
215 : {
216 : nscoord result;
217 0 : DISPLAY_MIN_WIDTH(this, result);
218 0 : if (mFrames.IsEmpty())
219 0 : result = 0;
220 : else
221 0 : result = mFrames.FirstChild()->GetMinISize(aRenderingContext);
222 :
223 0 : return result;
224 : }
225 :
226 : /* virtual */ nscoord
227 0 : ViewportFrame::GetPrefISize(gfxContext *aRenderingContext)
228 : {
229 : nscoord result;
230 0 : DISPLAY_PREF_WIDTH(this, result);
231 0 : if (mFrames.IsEmpty())
232 0 : result = 0;
233 : else
234 0 : result = mFrames.FirstChild()->GetPrefISize(aRenderingContext);
235 :
236 0 : return result;
237 : }
238 :
239 : nsPoint
240 23 : ViewportFrame::AdjustReflowInputForScrollbars(ReflowInput* aReflowInput) const
241 : {
242 : // Get our prinicpal child frame and see if we're scrollable
243 23 : nsIFrame* kidFrame = mFrames.FirstChild();
244 23 : nsIScrollableFrame* scrollingFrame = do_QueryFrame(kidFrame);
245 :
246 23 : if (scrollingFrame) {
247 0 : WritingMode wm = aReflowInput->GetWritingMode();
248 0 : LogicalMargin scrollbars(wm, scrollingFrame->GetActualScrollbarSizes());
249 0 : aReflowInput->SetComputedISize(aReflowInput->ComputedISize() -
250 0 : scrollbars.IStartEnd(wm));
251 0 : aReflowInput->AvailableISize() -= scrollbars.IStartEnd(wm);
252 0 : aReflowInput->SetComputedBSizeWithoutResettingResizeFlags(
253 0 : aReflowInput->ComputedBSize() - scrollbars.BStartEnd(wm));
254 0 : return nsPoint(scrollbars.Left(wm), scrollbars.Top(wm));
255 : }
256 23 : return nsPoint(0, 0);
257 : }
258 :
259 : nsRect
260 23 : ViewportFrame::AdjustReflowInputAsContainingBlock(ReflowInput* aReflowInput) const
261 : {
262 : #ifdef DEBUG
263 : nsPoint offset =
264 : #endif
265 23 : AdjustReflowInputForScrollbars(aReflowInput);
266 :
267 23 : NS_ASSERTION(GetAbsoluteContainingBlock()->GetChildList().IsEmpty() ||
268 : (offset.x == 0 && offset.y == 0),
269 : "We don't handle correct positioning of fixed frames with "
270 : "scrollbars in odd positions");
271 :
272 : // If a scroll position clamping scroll-port size has been set, layout
273 : // fixed position elements to this size instead of the computed size.
274 23 : nsRect rect(0, 0, aReflowInput->ComputedWidth(), aReflowInput->ComputedHeight());
275 23 : nsIPresShell* ps = PresContext()->PresShell();
276 23 : if (ps->IsScrollPositionClampingScrollPortSizeSet()) {
277 0 : rect.SizeTo(ps->GetScrollPositionClampingScrollPortSize());
278 : }
279 :
280 23 : return rect;
281 : }
282 :
283 : void
284 66 : ViewportFrame::Reflow(nsPresContext* aPresContext,
285 : ReflowOutput& aDesiredSize,
286 : const ReflowInput& aReflowInput,
287 : nsReflowStatus& aStatus)
288 : {
289 66 : MarkInReflow();
290 66 : DO_GLOBAL_REFLOW_COUNT("ViewportFrame");
291 132 : DISPLAY_REFLOW(aPresContext, this, aReflowInput, aDesiredSize, aStatus);
292 66 : NS_FRAME_TRACE_REFLOW_IN("ViewportFrame::Reflow");
293 :
294 : // Initialize OUT parameters
295 66 : aStatus.Reset();
296 :
297 : // Because |Reflow| sets ComputedBSize() on the child to our
298 : // ComputedBSize().
299 66 : AddStateBits(NS_FRAME_CONTAINS_RELATIVE_BSIZE);
300 :
301 : // Set our size up front, since some parts of reflow depend on it
302 : // being already set. Note that the computed height may be
303 : // unconstrained; that's ok. Consumers should watch out for that.
304 66 : SetSize(nsSize(aReflowInput.ComputedWidth(), aReflowInput.ComputedHeight()));
305 :
306 : // Reflow the main content first so that the placeholders of the
307 : // fixed-position frames will be in the right places on an initial
308 : // reflow.
309 66 : nscoord kidBSize = 0;
310 66 : WritingMode wm = aReflowInput.GetWritingMode();
311 :
312 66 : if (mFrames.NotEmpty()) {
313 : // Deal with a non-incremental reflow or an incremental reflow
314 : // targeted at our one-and-only principal child frame.
315 153 : if (aReflowInput.ShouldReflowAllKids() ||
316 87 : aReflowInput.IsBResize() ||
317 21 : NS_SUBTREE_DIRTY(mFrames.FirstChild())) {
318 : // Reflow our one-and-only principal child frame
319 65 : nsIFrame* kidFrame = mFrames.FirstChild();
320 130 : ReflowOutput kidDesiredSize(aReflowInput);
321 65 : WritingMode wm = kidFrame->GetWritingMode();
322 65 : LogicalSize availableSpace = aReflowInput.AvailableSize(wm);
323 : ReflowInput kidReflowInput(aPresContext, aReflowInput,
324 65 : kidFrame, availableSpace);
325 :
326 : // Reflow the frame
327 65 : kidReflowInput.SetComputedBSize(aReflowInput.ComputedBSize());
328 65 : ReflowChild(kidFrame, aPresContext, kidDesiredSize, kidReflowInput,
329 65 : 0, 0, 0, aStatus);
330 65 : kidBSize = kidDesiredSize.BSize(wm);
331 :
332 65 : FinishReflowChild(kidFrame, aPresContext, kidDesiredSize, nullptr, 0, 0, 0);
333 : } else {
334 1 : kidBSize = LogicalSize(wm, mFrames.FirstChild()->GetSize()).BSize(wm);
335 : }
336 : }
337 :
338 66 : NS_ASSERTION(aReflowInput.AvailableISize() != NS_UNCONSTRAINEDSIZE,
339 : "shouldn't happen anymore");
340 :
341 : // Return the max size as our desired size
342 : LogicalSize maxSize(wm, aReflowInput.AvailableISize(),
343 : // Being flowed initially at an unconstrained block size
344 : // means we should return our child's intrinsic size.
345 66 : aReflowInput.ComputedBSize() != NS_UNCONSTRAINEDSIZE
346 : ? aReflowInput.ComputedBSize()
347 66 : : kidBSize);
348 66 : aDesiredSize.SetSize(wm, maxSize);
349 66 : aDesiredSize.SetOverflowAreasToDesiredBounds();
350 :
351 66 : if (HasAbsolutelyPositionedChildren()) {
352 : // Make a copy of the reflow state and change the computed width and height
353 : // to reflect the available space for the fixed items
354 23 : ReflowInput reflowInput(aReflowInput);
355 :
356 23 : if (reflowInput.AvailableBSize() == NS_UNCONSTRAINEDSIZE) {
357 : // We have an intrinsic-height document with abs-pos/fixed-pos children.
358 : // Set the available height and mComputedHeight to our chosen height.
359 23 : reflowInput.AvailableBSize() = maxSize.BSize(wm);
360 : // Not having border/padding simplifies things
361 23 : NS_ASSERTION(reflowInput.ComputedPhysicalBorderPadding() == nsMargin(0,0,0,0),
362 : "Viewports can't have border/padding");
363 23 : reflowInput.SetComputedBSize(maxSize.BSize(wm));
364 : }
365 :
366 46 : nsRect rect = AdjustReflowInputAsContainingBlock(&reflowInput);
367 23 : nsOverflowAreas* overflowAreas = &aDesiredSize.mOverflowAreas;
368 : nsIScrollableFrame* rootScrollFrame =
369 23 : aPresContext->PresShell()->GetRootScrollFrameAsScrollable();
370 23 : if (rootScrollFrame && !rootScrollFrame->IsIgnoringViewportClipping()) {
371 0 : overflowAreas = nullptr;
372 : }
373 : AbsPosReflowFlags flags =
374 23 : AbsPosReflowFlags::eCBWidthAndHeightChanged; // XXX could be optimized
375 23 : GetAbsoluteContainingBlock()->Reflow(this, aPresContext, reflowInput, aStatus,
376 23 : rect, flags, overflowAreas);
377 : }
378 :
379 66 : if (mFrames.NotEmpty()) {
380 66 : ConsiderChildOverflow(aDesiredSize.mOverflowAreas, mFrames.FirstChild());
381 : }
382 :
383 : // If we were dirty then do a repaint
384 66 : if (GetStateBits() & NS_FRAME_IS_DIRTY) {
385 25 : InvalidateFrame();
386 : }
387 :
388 : // Clipping is handled by the document container (e.g., nsSubDocumentFrame),
389 : // so we don't need to change our overflow areas.
390 66 : bool overflowChanged = FinishAndStoreOverflow(&aDesiredSize);
391 66 : if (overflowChanged) {
392 : // We may need to alert our container to get it to pick up the
393 : // overflow change.
394 : nsSubDocumentFrame* container = static_cast<nsSubDocumentFrame*>
395 3 : (nsLayoutUtils::GetCrossDocParentFrame(this));
396 3 : if (container && !container->ShouldClipSubdocument()) {
397 0 : container->PresContext()->PresShell()->
398 0 : FrameNeedsReflow(container, nsIPresShell::eResize, NS_FRAME_IS_DIRTY);
399 : }
400 : }
401 :
402 66 : NS_FRAME_TRACE_REFLOW_OUT("ViewportFrame::Reflow", aStatus);
403 66 : NS_FRAME_SET_TRUNCATION(aStatus, aReflowInput, aDesiredSize);
404 66 : }
405 :
406 : bool
407 0 : ViewportFrame::ComputeCustomOverflow(nsOverflowAreas& aOverflowAreas)
408 : {
409 : nsIScrollableFrame* rootScrollFrame =
410 0 : PresContext()->PresShell()->GetRootScrollFrameAsScrollable();
411 0 : if (rootScrollFrame && !rootScrollFrame->IsIgnoringViewportClipping()) {
412 0 : return false;
413 : }
414 :
415 0 : return nsContainerFrame::ComputeCustomOverflow(aOverflowAreas);
416 : }
417 :
418 : void
419 0 : ViewportFrame::UpdateStyle(ServoRestyleState& aRestyleState)
420 : {
421 0 : ServoStyleContext* oldContext = StyleContext()->AsServo();
422 0 : nsIAtom* pseudo = oldContext->GetPseudo();
423 : RefPtr<ServoStyleContext> newContext =
424 0 : aRestyleState.StyleSet().ResolveInheritingAnonymousBoxStyle(pseudo, nullptr);
425 :
426 : // We're special because we have a null GetContent(), so don't call things
427 : // like UpdateStyleOfOwnedChildFrame that try to append changes for the
428 : // content to the change list. Nor do we computed a changehint, since we have
429 : // no way to apply it anyway.
430 0 : newContext->ResolveSameStructsAs(PresContext(), oldContext);
431 :
432 0 : MOZ_ASSERT(!GetNextContinuation(), "Viewport has continuations?");
433 0 : SetStyleContext(newContext);
434 :
435 0 : UpdateStyleOfOwnedAnonBoxes(aRestyleState);
436 0 : }
437 :
438 : void
439 0 : ViewportFrame::AppendDirectlyOwnedAnonBoxes(nsTArray<OwnedAnonBox>& aResult)
440 : {
441 0 : if (mFrames.NotEmpty()) {
442 0 : aResult.AppendElement(mFrames.FirstChild());
443 : }
444 0 : }
445 :
446 : #ifdef DEBUG_FRAME_DUMP
447 : nsresult
448 0 : ViewportFrame::GetFrameName(nsAString& aResult) const
449 : {
450 0 : return MakeFrameName(NS_LITERAL_STRING("Viewport"), aResult);
451 : }
452 : #endif
|