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