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