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 #include "core/rendering/RenderFullScreen.h"
27
28 #include "core/dom/FullscreenElementStack.h"
29 #include "core/rendering/RenderBlockFlow.h"
30
31 using namespace WebCore;
32
33 class RenderFullScreenPlaceholder FINAL : public RenderBlockFlow {
34 public:
RenderFullScreenPlaceholder(RenderFullScreen * owner)35 RenderFullScreenPlaceholder(RenderFullScreen* owner)
36 : RenderBlockFlow(0)
37 , m_owner(owner)
38 {
39 setDocumentForAnonymous(&owner->document());
40 }
41 private:
isRenderFullScreenPlaceholder() const42 virtual bool isRenderFullScreenPlaceholder() const { return true; }
supportsPartialLayout() const43 virtual bool supportsPartialLayout() const OVERRIDE { return false; }
44 virtual void willBeDestroyed();
45 RenderFullScreen* m_owner;
46 };
47
willBeDestroyed()48 void RenderFullScreenPlaceholder::willBeDestroyed()
49 {
50 m_owner->setPlaceholder(0);
51 RenderBlock::willBeDestroyed();
52 }
53
RenderFullScreen()54 RenderFullScreen::RenderFullScreen()
55 : RenderFlexibleBox(0)
56 , m_placeholder(0)
57 {
58 setReplaced(false);
59 }
60
createAnonymous(Document * document)61 RenderFullScreen* RenderFullScreen::createAnonymous(Document* document)
62 {
63 RenderFullScreen* renderer = new RenderFullScreen();
64 renderer->setDocumentForAnonymous(document);
65 return renderer;
66 }
67
willBeDestroyed()68 void RenderFullScreen::willBeDestroyed()
69 {
70 if (m_placeholder) {
71 remove();
72 if (!m_placeholder->beingDestroyed())
73 m_placeholder->destroy();
74 ASSERT(!m_placeholder);
75 }
76
77 // RenderObjects are unretained, so notify the document (which holds a pointer to a RenderFullScreen)
78 // if it's RenderFullScreen is destroyed.
79 FullscreenElementStack* controller = FullscreenElementStack::from(&document());
80 if (controller->fullScreenRenderer() == this)
81 controller->fullScreenRendererDestroyed();
82
83 RenderFlexibleBox::willBeDestroyed();
84 }
85
createFullScreenStyle()86 static PassRefPtr<RenderStyle> createFullScreenStyle()
87 {
88 RefPtr<RenderStyle> fullscreenStyle = RenderStyle::createDefaultStyle();
89
90 // Create a stacking context:
91 fullscreenStyle->setZIndex(INT_MAX);
92
93 fullscreenStyle->setFontDescription(FontDescription());
94 fullscreenStyle->font().update(0);
95
96 fullscreenStyle->setDisplay(FLEX);
97 fullscreenStyle->setJustifyContent(JustifyCenter);
98 fullscreenStyle->setAlignItems(AlignCenter);
99 fullscreenStyle->setFlexDirection(FlowColumn);
100
101 fullscreenStyle->setPosition(FixedPosition);
102 fullscreenStyle->setWidth(Length(100.0, Percent));
103 fullscreenStyle->setHeight(Length(100.0, Percent));
104 fullscreenStyle->setLeft(Length(0, WebCore::Fixed));
105 fullscreenStyle->setTop(Length(0, WebCore::Fixed));
106
107 fullscreenStyle->setBackgroundColor(Color::black);
108
109 return fullscreenStyle.release();
110 }
111
wrapRenderer(RenderObject * object,RenderObject * parent,Document * document)112 RenderObject* RenderFullScreen::wrapRenderer(RenderObject* object, RenderObject* parent, Document* document)
113 {
114 RenderFullScreen* fullscreenRenderer = RenderFullScreen::createAnonymous(document);
115 fullscreenRenderer->setStyle(createFullScreenStyle());
116 if (parent && !parent->isChildAllowed(fullscreenRenderer, fullscreenRenderer->style())) {
117 fullscreenRenderer->destroy();
118 return 0;
119 }
120 if (object) {
121 // |object->parent()| can be null if the object is not yet attached
122 // to |parent|.
123 if (RenderObject* parent = object->parent()) {
124 RenderBlock* containingBlock = object->containingBlock();
125 ASSERT(containingBlock);
126 // Since we are moving the |object| to a new parent |fullscreenRenderer|,
127 // the line box tree underneath our |containingBlock| is not longer valid.
128 containingBlock->deleteLineBoxTree();
129
130 parent->addChild(fullscreenRenderer, object);
131 object->remove();
132
133 // Always just do a full layout to ensure that line boxes get deleted properly.
134 // Because objects moved from |parent| to |fullscreenRenderer|, we want to
135 // make new line boxes instead of leaving the old ones around.
136 parent->setNeedsLayoutAndPrefWidthsRecalc();
137 containingBlock->setNeedsLayoutAndPrefWidthsRecalc();
138 }
139 fullscreenRenderer->addChild(object);
140 fullscreenRenderer->setNeedsLayoutAndPrefWidthsRecalc();
141 }
142
143 FullscreenElementStack::from(document)->setFullScreenRenderer(fullscreenRenderer);
144 return fullscreenRenderer;
145 }
146
unwrapRenderer()147 void RenderFullScreen::unwrapRenderer()
148 {
149 if (parent()) {
150 RenderObject* child;
151 while ((child = firstChild())) {
152 // We have to clear the override size, because as a flexbox, we
153 // may have set one on the child, and we don't want to leave that
154 // lying around on the child.
155 if (child->isBox())
156 toRenderBox(child)->clearOverrideSize();
157 child->remove();
158 parent()->addChild(child, this);
159 parent()->setNeedsLayoutAndPrefWidthsRecalc();
160 }
161 }
162 if (placeholder())
163 placeholder()->remove();
164 remove();
165 FullscreenElementStack::from(&document())->setFullScreenRenderer(0);
166 }
167
setPlaceholder(RenderBlock * placeholder)168 void RenderFullScreen::setPlaceholder(RenderBlock* placeholder)
169 {
170 m_placeholder = placeholder;
171 }
172
createPlaceholder(PassRefPtr<RenderStyle> style,const LayoutRect & frameRect)173 void RenderFullScreen::createPlaceholder(PassRefPtr<RenderStyle> style, const LayoutRect& frameRect)
174 {
175 if (style->width().isAuto())
176 style->setWidth(Length(frameRect.width(), Fixed));
177 if (style->height().isAuto())
178 style->setHeight(Length(frameRect.height(), Fixed));
179
180 if (!m_placeholder) {
181 m_placeholder = new RenderFullScreenPlaceholder(this);
182 m_placeholder->setStyle(style);
183 if (parent()) {
184 parent()->addChild(m_placeholder, this);
185 parent()->setNeedsLayoutAndPrefWidthsRecalc();
186 }
187 } else
188 m_placeholder->setStyle(style);
189 }
190