Home | History | Annotate | Download | only in rendering
      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 
     30 using namespace WebCore;
     31 
     32 class RenderFullScreenPlaceholder FINAL : public RenderBlock {
     33 public:
     34     RenderFullScreenPlaceholder(RenderFullScreen* owner)
     35         : RenderBlock(0)
     36         , m_owner(owner)
     37     {
     38         setDocumentForAnonymous(owner->document());
     39     }
     40 private:
     41     virtual bool isRenderFullScreenPlaceholder() const { return true; }
     42     virtual void willBeDestroyed();
     43     RenderFullScreen* m_owner;
     44 };
     45 
     46 void RenderFullScreenPlaceholder::willBeDestroyed()
     47 {
     48     m_owner->setPlaceholder(0);
     49     RenderBlock::willBeDestroyed();
     50 }
     51 
     52 RenderFullScreen::RenderFullScreen()
     53     : RenderFlexibleBox(0)
     54     , m_placeholder(0)
     55 {
     56     setReplaced(false);
     57 }
     58 
     59 RenderFullScreen* RenderFullScreen::createAnonymous(Document* document)
     60 {
     61     RenderFullScreen* renderer = new RenderFullScreen();
     62     renderer->setDocumentForAnonymous(document);
     63     return renderer;
     64 }
     65 
     66 void RenderFullScreen::willBeDestroyed()
     67 {
     68     if (m_placeholder) {
     69         remove();
     70         if (!m_placeholder->beingDestroyed())
     71             m_placeholder->destroy();
     72         ASSERT(!m_placeholder);
     73     }
     74 
     75     // RenderObjects are unretained, so notify the document (which holds a pointer to a RenderFullScreen)
     76     // if it's RenderFullScreen is destroyed.
     77     if (document()) {
     78         FullscreenElementStack* controller = FullscreenElementStack::from(document());
     79         if (controller->fullScreenRenderer() == this)
     80             controller->fullScreenRendererDestroyed();
     81     }
     82 
     83     RenderFlexibleBox::willBeDestroyed();
     84 }
     85 
     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 
    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 
    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 
    168 void RenderFullScreen::setPlaceholder(RenderBlock* placeholder)
    169 {
    170     m_placeholder = placeholder;
    171 }
    172 
    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