void FullscreenController::didExitFullScreen()
{
    if (!m_fullScreenFrame)
        return;

    if (Document* document = m_fullScreenFrame->document()) {
        if (Fullscreen* fullscreen = Fullscreen::fromIfExists(*document)) {
            Element* element = fullscreen->webkitCurrentFullScreenElement();
            if (element) {
                // When the client exits from full screen we have to call fullyExitFullscreen to notify
                // the document. While doing that, suppress notifications back to the client.
                m_isCancelingFullScreen = true;
                Fullscreen::fullyExitFullscreen(*document);
                m_isCancelingFullScreen = false;

                // If the video used overlay fullscreen mode, the background was made transparent. Restore the transparency.
                if (isHTMLVideoElement(element) && m_webViewImpl->layerTreeView())
                    m_webViewImpl->layerTreeView()->setHasTransparentBackground(m_webViewImpl->isTransparent());

                if (m_exitFullscreenPageScaleFactor) {
                    updatePageScaleConstraints(true);
                    m_webViewImpl->setPageScaleFactor(m_exitFullscreenPageScaleFactor);
                    m_webViewImpl->mainFrame()->setScrollOffset(WebSize(m_exitFullscreenScrollOffset));
                    m_webViewImpl->setVisualViewportOffset(m_exitFullscreenVisualViewportOffset);
                    m_exitFullscreenPageScaleFactor = 0;
                    m_exitFullscreenScrollOffset = IntSize();
                }

                fullscreen->didExitFullScreenForElement(0);
            }
        }
    }

    m_fullScreenFrame.clear();
}
void FullscreenController::didExitFullScreen()
{
    if (!m_fullScreenFrame)
        return;

    if (Document* document = m_fullScreenFrame->document()) {
        if (Fullscreen* fullscreen = Fullscreen::fromIfExists(*document)) {
            if (fullscreen->webkitCurrentFullScreenElement()) {
                // When the client exits from full screen we have to call fullyExitFullscreen to notify
                // the document. While doing that, suppress notifications back to the client.
                m_isCancelingFullScreen = true;
                Fullscreen::fullyExitFullscreen(*document);
                m_isCancelingFullScreen = false;

                if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled() && m_webViewImpl->layerTreeView())
                    m_webViewImpl->layerTreeView()->setHasTransparentBackground(m_webViewImpl->isTransparent());

                if (m_exitFullscreenPageScaleFactor) {
                    updatePageScaleConstraints(true);
                    m_webViewImpl->setPageScaleFactor(m_exitFullscreenPageScaleFactor);
                    m_webViewImpl->setMainFrameScrollOffset(IntPoint(m_exitFullscreenScrollOffset));
                    m_webViewImpl->setPinchViewportOffset(m_exitFullscreenPinchViewportOffset);
                    m_exitFullscreenPageScaleFactor = 0;
                    m_exitFullscreenScrollOffset = IntSize();
                }

                fullscreen->didExitFullScreenForElement(0);
            }
        }
    }

    m_fullScreenFrame.clear();
}
void FullscreenController::updateSize()
{
    if (!isFullscreen())
        return;

    updatePageScaleConstraints(false);

    LayoutFullScreen* layoutObject = Fullscreen::from(*m_fullScreenFrame->document()).fullScreenLayoutObject();
    if (layoutObject)
        layoutObject->updateStyle();
}
void FullscreenController::didEnterFullScreen()
{
    if (!m_provisionalFullScreenElement)
        return;

    RefPtrWillBeRawPtr<Element> element = m_provisionalFullScreenElement.release();
    Document& document = element->document();
    m_fullScreenFrame = document.frame();

    if (!m_fullScreenFrame)
        return;

    if (!m_exitFullscreenPageScaleFactor) {
        m_exitFullscreenPageScaleFactor = m_webViewImpl->pageScaleFactor();
        m_exitFullscreenScrollOffset = m_webViewImpl->mainFrame()->scrollOffset();
        m_exitFullscreenPinchViewportOffset = m_webViewImpl->pinchViewportOffset();

        updatePageScaleConstraints(false);
        m_webViewImpl->setPageScaleFactor(1.0f);
        m_webViewImpl->setMainFrameScrollOffset(IntPoint());
        m_webViewImpl->setPinchViewportOffset(FloatPoint());
    }

    Fullscreen::from(document).didEnterFullScreenForElement(element.get());
    ASSERT(Fullscreen::currentFullScreenElementFrom(document) == element);

    if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()) {
        if (isHTMLVideoElement(element)) {
            HTMLVideoElement* videoElement = toHTMLVideoElement(element);
            if (HTMLMediaElement::isMediaStreamURL(videoElement->sourceURL().string()))
                return;
            if (videoElement->webMediaPlayer()
                // FIXME: There is no embedder-side handling in layout test mode.
                && !LayoutTestSupport::isRunningLayoutTest()) {
                videoElement->webMediaPlayer()->enterFullscreen();
            }
            if (m_webViewImpl->layerTreeView())
                m_webViewImpl->layerTreeView()->setHasTransparentBackground(true);
        }
    }
}
void FullscreenController::didEnterFullScreen()
{
    if (!m_provisionalFullScreenElement)
        return;

    RefPtrWillBeRawPtr<Element> element = m_provisionalFullScreenElement.release();
    Document& document = element->document();
    m_fullScreenFrame = document.frame();

    if (!m_fullScreenFrame)
        return;

    if (!m_haveEnteredFullscreen) {
        updatePageScaleConstraints(false);
        m_webViewImpl->setPageScaleFactor(1.0f);
        m_webViewImpl->mainFrame()->setScrollOffset(WebSize());
        m_webViewImpl->setVisualViewportOffset(FloatPoint());
        m_haveEnteredFullscreen = true;
    }

    Fullscreen::from(document).didEnterFullScreenForElement(element.get());
    ASSERT(Fullscreen::currentFullScreenElementFrom(document) == element);

    if (isHTMLVideoElement(element)) {
        HTMLVideoElement* videoElement = toHTMLVideoElement(element);
        if (videoElement->usesOverlayFullscreenVideo()) {
            if (videoElement->webMediaPlayer()
                // FIXME: There is no embedder-side handling in layout test mode.
                && !LayoutTestSupport::isRunningLayoutTest()) {
                videoElement->webMediaPlayer()->enterFullscreen();
            }
            if (m_webViewImpl->layerTreeView())
                m_webViewImpl->layerTreeView()->setHasTransparentBackground(true);
        }
    }
}