2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #include "core/rendering/RenderFullScreen.h"
28 #include "core/dom/FullscreenElementStack.h"
29 #include "core/rendering/RenderBlockFlow.h"
31 using namespace WebCore;
33 class RenderFullScreenPlaceholder FINAL : public RenderBlockFlow {
35 RenderFullScreenPlaceholder(RenderFullScreen* owner)
39 setDocumentForAnonymous(&owner->document());
42 virtual bool isRenderFullScreenPlaceholder() const OVERRIDE { return true; }
43 virtual void willBeDestroyed() OVERRIDE;
44 RenderFullScreen* m_owner;
47 void RenderFullScreenPlaceholder::willBeDestroyed()
49 m_owner->setPlaceholder(0);
50 RenderBlock::willBeDestroyed();
53 RenderFullScreen::RenderFullScreen()
54 : RenderFlexibleBox(0)
60 RenderFullScreen* RenderFullScreen::createAnonymous(Document* document)
62 RenderFullScreen* renderer = new RenderFullScreen();
63 renderer->setDocumentForAnonymous(document);
67 void RenderFullScreen::willBeDestroyed()
71 if (!m_placeholder->beingDestroyed())
72 m_placeholder->destroy();
73 ASSERT(!m_placeholder);
76 // RenderObjects are unretained, so notify the document (which holds a pointer to a RenderFullScreen)
77 // if it's RenderFullScreen is destroyed.
78 FullscreenElementStack& controller = FullscreenElementStack::from(document());
79 if (controller.fullScreenRenderer() == this)
80 controller.fullScreenRendererDestroyed();
82 RenderFlexibleBox::willBeDestroyed();
85 static PassRefPtr<RenderStyle> createFullScreenStyle()
87 RefPtr<RenderStyle> fullscreenStyle = RenderStyle::createDefaultStyle();
89 // Create a stacking context:
90 fullscreenStyle->setZIndex(INT_MAX);
92 fullscreenStyle->setFontDescription(FontDescription());
93 fullscreenStyle->font().update(nullptr);
95 fullscreenStyle->setDisplay(FLEX);
96 fullscreenStyle->setJustifyContent(JustifyCenter);
97 fullscreenStyle->setAlignItems(ItemPositionCenter);
98 fullscreenStyle->setFlexDirection(FlowColumn);
100 fullscreenStyle->setPosition(FixedPosition);
101 fullscreenStyle->setWidth(Length(100.0, Percent));
102 fullscreenStyle->setHeight(Length(100.0, Percent));
103 fullscreenStyle->setLeft(Length(0, WebCore::Fixed));
104 fullscreenStyle->setTop(Length(0, WebCore::Fixed));
106 fullscreenStyle->setBackgroundColor(StyleColor(Color::black));
108 return fullscreenStyle.release();
111 RenderObject* RenderFullScreen::wrapRenderer(RenderObject* object, RenderObject* parent, Document* document)
113 // FIXME: We should not modify the structure of the render tree during
114 // layout. crbug.com/370459
115 DeprecatedDisableModifyRenderTreeStructureAsserts disabler;
117 RenderFullScreen* fullscreenRenderer = RenderFullScreen::createAnonymous(document);
118 fullscreenRenderer->setStyle(createFullScreenStyle());
119 if (parent && !parent->isChildAllowed(fullscreenRenderer, fullscreenRenderer->style())) {
120 fullscreenRenderer->destroy();
124 // |object->parent()| can be null if the object is not yet attached
126 if (RenderObject* parent = object->parent()) {
127 RenderBlock* containingBlock = object->containingBlock();
128 ASSERT(containingBlock);
129 // Since we are moving the |object| to a new parent |fullscreenRenderer|,
130 // the line box tree underneath our |containingBlock| is not longer valid.
131 containingBlock->deleteLineBoxTree();
133 parent->addChild(fullscreenRenderer, object);
136 // Always just do a full layout to ensure that line boxes get deleted properly.
137 // Because objects moved from |parent| to |fullscreenRenderer|, we want to
138 // make new line boxes instead of leaving the old ones around.
139 parent->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
140 containingBlock->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
142 fullscreenRenderer->addChild(object);
143 fullscreenRenderer->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
147 FullscreenElementStack::from(*document).setFullScreenRenderer(fullscreenRenderer);
148 return fullscreenRenderer;
151 void RenderFullScreen::unwrapRenderer()
153 // FIXME: We should not modify the structure of the render tree during
154 // layout. crbug.com/370459
155 DeprecatedDisableModifyRenderTreeStructureAsserts disabler;
159 while ((child = firstChild())) {
160 // We have to clear the override size, because as a flexbox, we
161 // may have set one on the child, and we don't want to leave that
162 // lying around on the child.
164 toRenderBox(child)->clearOverrideSize();
166 parent()->addChild(child, this);
167 parent()->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
171 placeholder()->remove();
173 FullscreenElementStack::from(document()).setFullScreenRenderer(0);
176 void RenderFullScreen::setPlaceholder(RenderBlock* placeholder)
178 m_placeholder = placeholder;
181 void RenderFullScreen::createPlaceholder(PassRefPtr<RenderStyle> style, const LayoutRect& frameRect)
183 if (style->width().isAuto())
184 style->setWidth(Length(frameRect.width(), Fixed));
185 if (style->height().isAuto())
186 style->setHeight(Length(frameRect.height(), Fixed));
188 if (!m_placeholder) {
189 m_placeholder = new RenderFullScreenPlaceholder(this);
190 m_placeholder->setStyle(style);
192 parent()->addChild(m_placeholder, this);
193 parent()->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
196 m_placeholder->setStyle(style);