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