9ce9810a3ac442a7ef3281001ba76b08988d68be
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 #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:
35     RenderFullScreenPlaceholder(RenderFullScreen* owner)
36         : RenderBlockFlow(0)
37         , m_owner(owner)
38     {
39         setDocumentForAnonymous(&owner->document());
40     }
41 private:
42     virtual bool isRenderFullScreenPlaceholder() const OVERRIDE { return true; }
43     virtual void willBeDestroyed() OVERRIDE;
44     RenderFullScreen* m_owner;
45 };
46
47 void RenderFullScreenPlaceholder::willBeDestroyed()
48 {
49     m_owner->setPlaceholder(0);
50     RenderBlock::willBeDestroyed();
51 }
52
53 RenderFullScreen::RenderFullScreen()
54     : RenderFlexibleBox(0)
55     , m_placeholder(0)
56 {
57     setReplaced(false);
58 }
59
60 RenderFullScreen* RenderFullScreen::createAnonymous(Document* document)
61 {
62     RenderFullScreen* renderer = new RenderFullScreen();
63     renderer->setDocumentForAnonymous(document);
64     return renderer;
65 }
66
67 void RenderFullScreen::willBeDestroyed()
68 {
69     if (m_placeholder) {
70         remove();
71         if (!m_placeholder->beingDestroyed())
72             m_placeholder->destroy();
73         ASSERT(!m_placeholder);
74     }
75
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();
81
82     RenderFlexibleBox::willBeDestroyed();
83 }
84
85 static PassRefPtr<RenderStyle> createFullScreenStyle()
86 {
87     RefPtr<RenderStyle> fullscreenStyle = RenderStyle::createDefaultStyle();
88
89     // Create a stacking context:
90     fullscreenStyle->setZIndex(INT_MAX);
91
92     fullscreenStyle->setFontDescription(FontDescription());
93     fullscreenStyle->font().update(nullptr);
94
95     fullscreenStyle->setDisplay(FLEX);
96     fullscreenStyle->setJustifyContent(JustifyCenter);
97     fullscreenStyle->setAlignItems(ItemPositionCenter);
98     fullscreenStyle->setFlexDirection(FlowColumn);
99
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));
105
106     fullscreenStyle->setBackgroundColor(StyleColor(Color::black));
107
108     return fullscreenStyle.release();
109 }
110
111 RenderObject* RenderFullScreen::wrapRenderer(RenderObject* object, RenderObject* parent, Document* document)
112 {
113     // FIXME: We should not modify the structure of the render tree during
114     // layout. crbug.com/370459
115     DeprecatedDisableModifyRenderTreeStructureAsserts disabler;
116
117     RenderFullScreen* fullscreenRenderer = RenderFullScreen::createAnonymous(document);
118     fullscreenRenderer->setStyle(createFullScreenStyle());
119     if (parent && !parent->isChildAllowed(fullscreenRenderer, fullscreenRenderer->style())) {
120         fullscreenRenderer->destroy();
121         return 0;
122     }
123     if (object) {
124         // |object->parent()| can be null if the object is not yet attached
125         // to |parent|.
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();
132
133             parent->addChild(fullscreenRenderer, object);
134             object->remove();
135
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();
141         }
142         fullscreenRenderer->addChild(object);
143         fullscreenRenderer->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
144     }
145
146     ASSERT(document);
147     FullscreenElementStack::from(*document).setFullScreenRenderer(fullscreenRenderer);
148     return fullscreenRenderer;
149 }
150
151 void RenderFullScreen::unwrapRenderer()
152 {
153     // FIXME: We should not modify the structure of the render tree during
154     // layout. crbug.com/370459
155     DeprecatedDisableModifyRenderTreeStructureAsserts disabler;
156
157     if (parent()) {
158         RenderObject* child;
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.
163             if (child->isBox())
164                 toRenderBox(child)->clearOverrideSize();
165             child->remove();
166             parent()->addChild(child, this);
167             parent()->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
168         }
169     }
170     if (placeholder())
171         placeholder()->remove();
172     remove();
173     FullscreenElementStack::from(document()).setFullScreenRenderer(0);
174 }
175
176 void RenderFullScreen::setPlaceholder(RenderBlock* placeholder)
177 {
178     m_placeholder = placeholder;
179 }
180
181 void RenderFullScreen::createPlaceholder(PassRefPtr<RenderStyle> style, const LayoutRect& frameRect)
182 {
183     if (style->width().isAuto())
184         style->setWidth(Length(frameRect.width(), Fixed));
185     if (style->height().isAuto())
186         style->setHeight(Length(frameRect.height(), Fixed));
187
188     if (!m_placeholder) {
189         m_placeholder = new RenderFullScreenPlaceholder(this);
190         m_placeholder->setStyle(style);
191         if (parent()) {
192             parent()->addChild(m_placeholder, this);
193             parent()->setNeedsLayoutAndPrefWidthsRecalcAndFullPaintInvalidation();
194         }
195     } else
196         m_placeholder->setStyle(style);
197 }