WebCore should build successfully even with -DENABLE_UNIFIED_BUILDS=OFF
[WebKit-https.git] / Source / WebCore / rendering / RenderFullScreen.cpp
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 "RenderBlockFlow.h"
32 #include "RenderLayer.h"
33 #include "RenderLayerCompositor.h"
34 #include "RenderTreeBuilder.h"
35 #include <wtf/IsoMallocInlines.h>
36
37 namespace WebCore {
38
39 WTF_MAKE_ISO_ALLOCATED_IMPL(RenderFullScreen);
40
41 RenderFullScreenPlaceholder::RenderFullScreenPlaceholder(Document& document, RenderStyle&& style)
42     : RenderBlockFlow(document, WTFMove(style))
43 {
44 }
45
46 bool RenderFullScreenPlaceholder::isRenderFullScreenPlaceholder() const
47 {
48     return true;
49 }
50
51 WTF_MAKE_ISO_ALLOCATED_IMPL(RenderFullScreenPlaceholder);
52
53 RenderFullScreen::RenderFullScreen(Document& document, RenderStyle&& style)
54     : RenderFlexibleBox(document, WTFMove(style))
55 {
56     setReplaced(false); 
57 }
58
59 RenderFullScreen::~RenderFullScreen()
60 {
61     ASSERT(!m_placeholder);
62 }
63
64 static RenderStyle createFullScreenStyle()
65 {
66     auto fullscreenStyle = RenderStyle::create();
67
68     // Create a stacking context:
69     fullscreenStyle.setZIndex(INT_MAX);
70
71     fullscreenStyle.setFontDescription({ });
72     fullscreenStyle.fontCascade().update(nullptr);
73
74     fullscreenStyle.setDisplay(DisplayType::Flex);
75     fullscreenStyle.setJustifyContentPosition(ContentPosition::Center);
76     fullscreenStyle.setAlignItemsPosition(ItemPosition::Center);
77     fullscreenStyle.setFlexDirection(FlexDirection::Column);
78     
79     fullscreenStyle.setPosition(PositionType::Fixed);
80     fullscreenStyle.setWidth(Length(100.0, Percent));
81     fullscreenStyle.setHeight(Length(100.0, Percent));
82     fullscreenStyle.setLeft(Length(0, WebCore::Fixed));
83     fullscreenStyle.setTop(Length(0, WebCore::Fixed));
84     
85     fullscreenStyle.setBackgroundColor(Color::black);
86
87     return fullscreenStyle;
88 }
89
90 RenderPtr<RenderFullScreen> RenderFullScreen::wrapNewRenderer(RenderTreeBuilder& builder, RenderPtr<RenderElement> renderer, RenderElement& parent, Document& document)
91 {
92     auto newFullscreenRenderer = createRenderer<RenderFullScreen>(document, createFullScreenStyle());
93     newFullscreenRenderer->initializeStyle();
94
95     auto& fullscreenRenderer = *newFullscreenRenderer;
96     if (!parent.isChildAllowed(fullscreenRenderer, fullscreenRenderer.style()))
97         return nullptr;
98
99     builder.attach(fullscreenRenderer, WTFMove(renderer));
100     fullscreenRenderer.setNeedsLayoutAndPrefWidthsRecalc();
101
102     document.setFullScreenRenderer(builder, fullscreenRenderer);
103
104     return newFullscreenRenderer;
105 }
106
107 void RenderFullScreen::wrapExistingRenderer(RenderElement& renderer, Document& document)
108 {
109     // FIXME: This should be done by RenderTreeUpdater.
110     RenderTreeBuilder builder(*document.renderView());
111
112     auto newFullscreenRenderer = createRenderer<RenderFullScreen>(document, createFullScreenStyle());
113     newFullscreenRenderer->initializeStyle();
114
115     auto& fullscreenRenderer = *newFullscreenRenderer;
116     auto& parent = *renderer.parent();
117     if (!parent.isChildAllowed(fullscreenRenderer, fullscreenRenderer.style()))
118         return;
119
120     RenderBlock* containingBlock = renderer.containingBlock();
121     ASSERT(containingBlock);
122     // Since we are moving the |object| to a new parent |fullscreenRenderer|,
123     // the line box tree underneath our |containingBlock| is not longer valid.
124     containingBlock->deleteLines();
125
126     builder.attach(parent, WTFMove(newFullscreenRenderer), &renderer);
127
128     auto toMove = builder.detach(parent, renderer);
129
130     // Always just do a full layout to ensure that line boxes get deleted properly.
131     // Because objects moved from |parent| to |fullscreenRenderer|, we want to
132     // make new line boxes instead of leaving the old ones around.
133     parent.setNeedsLayoutAndPrefWidthsRecalc();
134     containingBlock->setNeedsLayoutAndPrefWidthsRecalc();
135
136     builder.attach(fullscreenRenderer, WTFMove(toMove));
137     fullscreenRenderer.setNeedsLayoutAndPrefWidthsRecalc();
138
139     document.setFullScreenRenderer(builder, fullscreenRenderer);
140 }
141
142 void RenderFullScreen::unwrapRenderer(bool& requiresRenderTreeRebuild)
143 {
144     RenderTreeBuilder builder(view());
145
146     requiresRenderTreeRebuild = false;
147     if (parent()) {
148         auto* child = firstChild();
149         // Things can get very complicated with anonymous block generation.
150         // We can restore correctly without rebuild in simple cases only.
151         // FIXME: We should have a mechanism for removing a block without reconstructing the tree.
152         if (child != lastChild())
153             requiresRenderTreeRebuild = true;
154         else if (child && child->isAnonymousBlock()) {
155             auto& anonymousBlock = downcast<RenderBlock>(*child);
156             if (anonymousBlock.firstChild() != anonymousBlock.lastChild())
157                 requiresRenderTreeRebuild = true;
158         }
159
160         while ((child = firstChild())) {
161             if (child->isAnonymousBlock() && !requiresRenderTreeRebuild) {
162                 if (auto* nonAnonymousChild = downcast<RenderBlock>(*child).firstChild())
163                     child = nonAnonymousChild;
164                 else {
165                     builder.destroy(*child);
166                     continue;
167                 }
168             }
169             // We have to clear the override size, because as a flexbox, we
170             // may have set one on the child, and we don't want to leave that
171             // lying around on the child.
172             if (is<RenderBox>(*child))
173                 downcast<RenderBox>(*child).clearOverrideContentSize();
174             auto childToMove = builder.detach(*child->parent(), *child);
175             builder.attach(*parent(), WTFMove(childToMove), this);
176             parent()->setNeedsLayoutAndPrefWidthsRecalc();
177         }
178     }
179     if (placeholder())
180         builder.destroy(*placeholder());
181     ASSERT(!placeholder());
182
183     builder.destroy(*this);
184 }
185
186 }
187
188 #endif