1/*
2 * Copyright (C) 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "config.h"
26
27#if ENABLE(FULLSCREEN_API)
28
29#include "RenderFullScreen.h"
30
31#include "FullscreenManager.h"
32#include "RenderBlockFlow.h"
33#include "RenderLayer.h"
34#include "RenderLayerCompositor.h"
35#include "RenderTreeBuilder.h"
36#include <wtf/IsoMallocInlines.h>
37
38namespace WebCore {
39
40WTF_MAKE_ISO_ALLOCATED_IMPL(RenderFullScreen);
41
42RenderFullScreenPlaceholder::RenderFullScreenPlaceholder(Document& document, RenderStyle&& style)
43 : RenderBlockFlow(document, WTFMove(style))
44{
45}
46
47bool RenderFullScreenPlaceholder::isRenderFullScreenPlaceholder() const
48{
49 return true;
50}
51
52WTF_MAKE_ISO_ALLOCATED_IMPL(RenderFullScreenPlaceholder);
53
54RenderFullScreen::RenderFullScreen(Document& document, RenderStyle&& style)
55 : RenderFlexibleBox(document, WTFMove(style))
56{
57 setReplaced(false);
58}
59
60RenderFullScreen::~RenderFullScreen()
61{
62 ASSERT(!m_placeholder);
63}
64
65static RenderStyle createFullScreenStyle()
66{
67 auto fullscreenStyle = RenderStyle::create();
68
69 // Create a stacking context:
70 fullscreenStyle.setZIndex(INT_MAX);
71
72 fullscreenStyle.setFontDescription({ });
73 fullscreenStyle.fontCascade().update(nullptr);
74
75 fullscreenStyle.setDisplay(DisplayType::Flex);
76 fullscreenStyle.setJustifyContentPosition(ContentPosition::Center);
77 fullscreenStyle.setAlignItemsPosition(ItemPosition::Center);
78 fullscreenStyle.setFlexDirection(FlexDirection::Column);
79
80 fullscreenStyle.setPosition(PositionType::Fixed);
81 fullscreenStyle.setWidth(Length(100.0, Percent));
82 fullscreenStyle.setHeight(Length(100.0, Percent));
83 fullscreenStyle.setLeft(Length(0, WebCore::Fixed));
84 fullscreenStyle.setTop(Length(0, WebCore::Fixed));
85
86 fullscreenStyle.setBackgroundColor(Color::black);
87
88 return fullscreenStyle;
89}
90
91RenderPtr<RenderFullScreen> RenderFullScreen::wrapNewRenderer(RenderTreeBuilder& builder, RenderPtr<RenderElement> renderer, RenderElement& parent, Document& document)
92{
93 auto newFullscreenRenderer = createRenderer<RenderFullScreen>(document, createFullScreenStyle());
94 newFullscreenRenderer->initializeStyle();
95
96 auto& fullscreenRenderer = *newFullscreenRenderer;
97 if (!parent.isChildAllowed(fullscreenRenderer, fullscreenRenderer.style()))
98 return nullptr;
99
100 builder.attach(fullscreenRenderer, WTFMove(renderer));
101 fullscreenRenderer.setNeedsLayoutAndPrefWidthsRecalc();
102
103 document.fullscreenManager().setFullscreenRenderer(builder, fullscreenRenderer);
104
105 return newFullscreenRenderer;
106}
107
108void RenderFullScreen::wrapExistingRenderer(RenderElement& renderer, Document& document)
109{
110 // FIXME: This should be done by RenderTreeUpdater.
111 RenderTreeBuilder builder(*document.renderView());
112
113 auto newFullscreenRenderer = createRenderer<RenderFullScreen>(document, createFullScreenStyle());
114 newFullscreenRenderer->initializeStyle();
115
116 auto& fullscreenRenderer = *newFullscreenRenderer;
117 auto& parent = *renderer.parent();
118 if (!parent.isChildAllowed(fullscreenRenderer, fullscreenRenderer.style()))
119 return;
120
121 RenderBlock* containingBlock = renderer.containingBlock();
122 ASSERT(containingBlock);
123 // Since we are moving the |object| to a new parent |fullscreenRenderer|,
124 // the line box tree underneath our |containingBlock| is not longer valid.
125 containingBlock->deleteLines();
126
127 builder.attach(parent, WTFMove(newFullscreenRenderer), &renderer);
128
129 auto toMove = builder.detach(parent, renderer);
130
131 // Always just do a full layout to ensure that line boxes get deleted properly.
132 // Because objects moved from |parent| to |fullscreenRenderer|, we want to
133 // make new line boxes instead of leaving the old ones around.
134 parent.setNeedsLayoutAndPrefWidthsRecalc();
135 containingBlock->setNeedsLayoutAndPrefWidthsRecalc();
136
137 builder.attach(fullscreenRenderer, WTFMove(toMove));
138 fullscreenRenderer.setNeedsLayoutAndPrefWidthsRecalc();
139
140 document.fullscreenManager().setFullscreenRenderer(builder, fullscreenRenderer);
141}
142
143void RenderFullScreen::unwrapRenderer(bool& requiresRenderTreeRebuild)
144{
145 RenderTreeBuilder builder(view());
146
147 requiresRenderTreeRebuild = false;
148 if (parent()) {
149 auto* child = firstChild();
150 // Things can get very complicated with anonymous block generation.
151 // We can restore correctly without rebuild in simple cases only.
152 // FIXME: We should have a mechanism for removing a block without reconstructing the tree.
153 if (child != lastChild())
154 requiresRenderTreeRebuild = true;
155 else if (child && child->isAnonymousBlock()) {
156 auto& anonymousBlock = downcast<RenderBlock>(*child);
157 if (anonymousBlock.firstChild() != anonymousBlock.lastChild())
158 requiresRenderTreeRebuild = true;
159 }
160
161 while ((child = firstChild())) {
162 if (child->isAnonymousBlock() && !requiresRenderTreeRebuild) {
163 if (auto* nonAnonymousChild = downcast<RenderBlock>(*child).firstChild())
164 child = nonAnonymousChild;
165 else {
166 builder.destroy(*child);
167 continue;
168 }
169 }
170 // We have to clear the override size, because as a flexbox, we
171 // may have set one on the child, and we don't want to leave that
172 // lying around on the child.
173 if (is<RenderBox>(*child))
174 downcast<RenderBox>(*child).clearOverrideContentSize();
175 auto childToMove = builder.detach(*child->parent(), *child);
176 builder.attach(*parent(), WTFMove(childToMove), this);
177 parent()->setNeedsLayoutAndPrefWidthsRecalc();
178 }
179 }
180 if (placeholder())
181 builder.destroy(*placeholder());
182 ASSERT(!placeholder());
183
184 builder.destroy(*this);
185}
186
187}
188
189#endif
190