Merge willEnter/willExitFullScreen into didEnter/didExitFullScreen

The will/did callback pairs came into being in 2011:
https://bugs.webkit.org/show_bug.cgi?id=70477
https://code.google.com/p/chromium/issues/detail?id=100264

The original problem was that the webkitfullscreenchange was
dispatched too early, but this is not affected since the did*
callbacks are the ones left in use.

The risk is that something done in the will* callbacks is too late if
done moved to the did* callbacks. That amounts to:

 - Saving placeholder style and unwrapping/wrapping the renderer in
   Fullscreen::willEnterFullScreenForElement().

 - The overlayFullscreenVideoEnabled() bits in
   HTMLMediaElement::willStopBeingFullscreenElement() and
   FullscreenController::willExitFullScreen().

Since the only difference between will* and did* is that the new size
is known and events aren't fired synchronously, this seems likely to
be safe.

BUG=407002

Review URL: https://codereview.chromium.org/497723002

git-svn-id: svn://svn.chromium.org/blink/trunk@181218 bbb929c8-8fbe-4397-9dbb-9b2b20218538
diff --git a/third_party/WebKit/Source/core/dom/Fullscreen.cpp b/third_party/WebKit/Source/core/dom/Fullscreen.cpp
index 84473a6..14dbbd8 100644
--- a/third_party/WebKit/Source/core/dom/Fullscreen.cpp
+++ b/third_party/WebKit/Source/core/dom/Fullscreen.cpp
@@ -409,7 +409,7 @@
     return fullscreenIsAllowedForAllOwners(document) && fullscreenIsSupported(document);
 }
 
-void Fullscreen::willEnterFullScreenForElement(Element* element)
+void Fullscreen::didEnterFullScreenForElement(Element* element)
 {
     ASSERT(element);
     if (!document()->isActive())
@@ -439,32 +439,12 @@
     // FIXME: This should not call updateStyleIfNeeded.
     document()->setNeedsStyleRecalc(SubtreeStyleChange);
     document()->updateRenderTreeIfNeeded();
-}
-
-void Fullscreen::didEnterFullScreenForElement(Element*)
-{
-    if (!m_fullScreenElement)
-        return;
-
-    if (!document()->isActive())
-        return;
 
     m_fullScreenElement->didBecomeFullscreenElement();
 
     m_eventQueueTimer.startOneShot(0, FROM_HERE);
 }
 
-void Fullscreen::willExitFullScreenForElement(Element*)
-{
-    if (!m_fullScreenElement)
-        return;
-
-    if (!document()->isActive())
-        return;
-
-    m_fullScreenElement->willStopBeingFullscreenElement();
-}
-
 void Fullscreen::didExitFullScreenForElement(Element*)
 {
     if (!m_fullScreenElement)
@@ -473,6 +453,8 @@
     if (!document()->isActive())
         return;
 
+    m_fullScreenElement->willStopBeingFullscreenElement();
+
     m_fullScreenElement->setContainsFullScreenElementOnAncestorsCrossingFrameBoundaries(false);
 
     m_areKeysEnabledInFullScreen = false;
diff --git a/third_party/WebKit/Source/core/dom/Fullscreen.h b/third_party/WebKit/Source/core/dom/Fullscreen.h
index 073e3175..723d8cf6 100644
--- a/third_party/WebKit/Source/core/dom/Fullscreen.h
+++ b/third_party/WebKit/Source/core/dom/Fullscreen.h
@@ -73,9 +73,7 @@
     static bool fullscreenEnabled(Document&);
     Element* fullscreenElement() const { return !m_fullScreenElementStack.isEmpty() ? m_fullScreenElementStack.last().first.get() : 0; }
 
-    void willEnterFullScreenForElement(Element*);
     void didEnterFullScreenForElement(Element*);
-    void willExitFullScreenForElement(Element*);
     void didExitFullScreenForElement(Element*);
 
     void setFullScreenRenderer(RenderFullScreen*);
diff --git a/third_party/WebKit/Source/web/FullscreenController.cpp b/third_party/WebKit/Source/web/FullscreenController.cpp
index f19822ed..acee2487 100644
--- a/third_party/WebKit/Source/web/FullscreenController.cpp
+++ b/third_party/WebKit/Source/web/FullscreenController.cpp
@@ -56,73 +56,40 @@
 {
 }
 
-void FullscreenController::willEnterFullScreen()
+void FullscreenController::didEnterFullScreen()
 {
     if (!m_provisionalFullScreenElement)
         return;
 
-    // Ensure that this element's document is still attached.
-    Document& doc = m_provisionalFullScreenElement->document();
-    if (doc.frame()) {
-        Fullscreen::from(doc).willEnterFullScreenForElement(m_provisionalFullScreenElement.get());
-        m_fullScreenFrame = doc.frame();
-    }
-    m_provisionalFullScreenElement.clear();
-}
+    RefPtrWillBeRawPtr<Element> element = m_provisionalFullScreenElement.release();
+    Document& document = element->document();
+    m_fullScreenFrame = document.frame();
 
-void FullscreenController::didEnterFullScreen()
-{
     if (!m_fullScreenFrame)
         return;
 
-    if (Document* doc = m_fullScreenFrame->document()) {
-        if (Fullscreen::isFullScreen(*doc)) {
-            if (!m_exitFullscreenPageScaleFactor) {
-                m_exitFullscreenPageScaleFactor = m_webViewImpl->pageScaleFactor();
-                m_exitFullscreenScrollOffset = m_webViewImpl->mainFrame()->scrollOffset();
-                m_exitFullscreenPinchViewportOffset = m_webViewImpl->pinchViewportOffset();
-                m_webViewImpl->setPageScaleFactor(1.0f);
-                m_webViewImpl->setMainFrameScrollOffset(IntPoint());
-                m_webViewImpl->setPinchViewportOffset(FloatPoint());
-            }
-
-            Fullscreen::from(*doc).didEnterFullScreenForElement(0);
-            if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()) {
-                Element* element = Fullscreen::currentFullScreenElementFrom(*doc);
-                ASSERT(element);
-                if (isHTMLMediaElement(*element)) {
-                    HTMLMediaElement* mediaElement = toHTMLMediaElement(element);
-                    if (mediaElement->webMediaPlayer() && mediaElement->webMediaPlayer()->canEnterFullscreen()
-                        // FIXME: There is no embedder-side handling in layout test mode.
-                        && !LayoutTestSupport::isRunningLayoutTest()) {
-                        mediaElement->webMediaPlayer()->enterFullscreen();
-                    }
-                    if (m_webViewImpl->layerTreeView())
-                        m_webViewImpl->layerTreeView()->setHasTransparentBackground(true);
-                }
-            }
-        }
+    if (!m_exitFullscreenPageScaleFactor) {
+        m_exitFullscreenPageScaleFactor = m_webViewImpl->pageScaleFactor();
+        m_exitFullscreenScrollOffset = m_webViewImpl->mainFrame()->scrollOffset();
+        m_exitFullscreenPinchViewportOffset = m_webViewImpl->pinchViewportOffset();
+        m_webViewImpl->setPageScaleFactor(1.0f);
+        m_webViewImpl->setMainFrameScrollOffset(IntPoint());
+        m_webViewImpl->setPinchViewportOffset(FloatPoint());
     }
-}
 
-void FullscreenController::willExitFullScreen()
-{
-    if (!m_fullScreenFrame)
-        return;
+    Fullscreen::from(document).didEnterFullScreenForElement(element.get());
+    ASSERT(Fullscreen::currentFullScreenElementFrom(document) == element);
 
-    if (Document* doc = m_fullScreenFrame->document()) {
-        Fullscreen* fullscreen = Fullscreen::fromIfExists(*doc);
-        if (!fullscreen)
-            return;
-        if (fullscreen->isFullScreen(*doc)) {
-            // 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(*doc);
-            m_isCancelingFullScreen = false;
-            fullscreen->willExitFullScreenForElement(0);
-            if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled() && m_webViewImpl->layerTreeView())
-                m_webViewImpl->layerTreeView()->setHasTransparentBackground(m_webViewImpl->isTransparent());
+    if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled()) {
+        if (isHTMLMediaElement(element)) {
+            HTMLMediaElement* mediaElement = toHTMLMediaElement(element);
+            if (mediaElement->webMediaPlayer() && mediaElement->webMediaPlayer()->canEnterFullscreen()
+                // FIXME: There is no embedder-side handling in layout test mode.
+                && !LayoutTestSupport::isRunningLayoutTest()) {
+                mediaElement->webMediaPlayer()->enterFullscreen();
+            }
+            if (m_webViewImpl->layerTreeView())
+                m_webViewImpl->layerTreeView()->setHasTransparentBackground(true);
         }
     }
 }
@@ -132,9 +99,18 @@
     if (!m_fullScreenFrame)
         return;
 
-    if (Document* doc = m_fullScreenFrame->document()) {
-        if (Fullscreen* fullscreen = Fullscreen::fromIfExists(*doc)) {
+    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) {
                     m_webViewImpl->setPageScaleFactor(m_exitFullscreenPageScaleFactor);
                     m_webViewImpl->setMainFrameScrollOffset(IntPoint(m_exitFullscreenScrollOffset));
@@ -162,7 +138,6 @@
     // We are already in fullscreen mode.
     if (m_fullScreenFrame) {
         m_provisionalFullScreenElement = element;
-        willEnterFullScreen();
         didEnterFullScreen();
         return;
     }
diff --git a/third_party/WebKit/Source/web/FullscreenController.h b/third_party/WebKit/Source/web/FullscreenController.h
index 4eb36d1..682d137c 100644
--- a/third_party/WebKit/Source/web/FullscreenController.h
+++ b/third_party/WebKit/Source/web/FullscreenController.h
@@ -47,9 +47,7 @@
 public:
     static PassOwnPtr<FullscreenController> create(WebViewImpl*);
 
-    void willEnterFullScreen();
     void didEnterFullScreen();
-    void willExitFullScreen();
     void didExitFullScreen();
 
     void enterFullScreenForElement(Element*);
diff --git a/third_party/WebKit/Source/web/WebViewImpl.cpp b/third_party/WebKit/Source/web/WebViewImpl.cpp
index 0a3b0633..a22d0eb 100644
--- a/third_party/WebKit/Source/web/WebViewImpl.cpp
+++ b/third_party/WebKit/Source/web/WebViewImpl.cpp
@@ -1719,21 +1719,11 @@
         pluginContainer->willEndLiveResize();
 }
 
-void WebViewImpl::willEnterFullScreen()
-{
-    m_fullscreenController->willEnterFullScreen();
-}
-
 void WebViewImpl::didEnterFullScreen()
 {
     m_fullscreenController->didEnterFullScreen();
 }
 
-void WebViewImpl::willExitFullScreen()
-{
-    m_fullscreenController->willExitFullScreen();
-}
-
 void WebViewImpl::didExitFullScreen()
 {
     m_fullscreenController->didExitFullScreen();
diff --git a/third_party/WebKit/Source/web/WebViewImpl.h b/third_party/WebKit/Source/web/WebViewImpl.h
index 0db68d6..c93df05 100644
--- a/third_party/WebKit/Source/web/WebViewImpl.h
+++ b/third_party/WebKit/Source/web/WebViewImpl.h
@@ -92,9 +92,7 @@
     virtual void resize(const WebSize&) OVERRIDE;
     virtual void resizePinchViewport(const WebSize&) OVERRIDE;
     virtual void willEndLiveResize() OVERRIDE;
-    virtual void willEnterFullScreen() OVERRIDE;
     virtual void didEnterFullScreen() OVERRIDE;
-    virtual void willExitFullScreen() OVERRIDE;
     virtual void didExitFullScreen() OVERRIDE;
 
     virtual void beginFrame(const WebBeginFrameArgs&) OVERRIDE;
diff --git a/third_party/WebKit/Source/web/tests/WebFrameTest.cpp b/third_party/WebKit/Source/web/tests/WebFrameTest.cpp
index d174a6e..629eec70 100644
--- a/third_party/WebKit/Source/web/tests/WebFrameTest.cpp
+++ b/third_party/WebKit/Source/web/tests/WebFrameTest.cpp
@@ -5695,7 +5695,6 @@
     blink::UserGestureIndicator gesture(blink::DefinitelyProcessingUserGesture);
     Element* divFullscreen = document->getElementById("div1");
     Fullscreen::from(*document).requestFullscreen(*divFullscreen, Fullscreen::PrefixedRequest);
-    webViewImpl->willEnterFullScreen();
     webViewImpl->didEnterFullScreen();
     webViewImpl->layout();
 
@@ -5705,7 +5704,6 @@
     ASSERT_FALSE(webScrollLayer->scrollable());
 
     // Verify that the main frame is scrollable upon exiting fullscreen.
-    webViewImpl->willExitFullScreen();
     webViewImpl->didExitFullScreen();
     webViewImpl->layout();
     ASSERT_FALSE(blink::Fullscreen::isFullScreen(*document));
@@ -5727,7 +5725,6 @@
     Document* document = toWebLocalFrameImpl(webViewImpl->mainFrame())->frame()->document();
     blink::UserGestureIndicator gesture(blink::DefinitelyProcessingUserGesture);
     Fullscreen::from(*document).requestFullscreen(*document->documentElement(), Fullscreen::PrefixedRequest);
-    webViewImpl->willEnterFullScreen();
     webViewImpl->didEnterFullScreen();
     webViewImpl->layout();
 
diff --git a/third_party/WebKit/Source/web/tests/WebViewTest.cpp b/third_party/WebKit/Source/web/tests/WebViewTest.cpp
index 3fbf463..c45519c 100644
--- a/third_party/WebKit/Source/web/tests/WebViewTest.cpp
+++ b/third_party/WebKit/Source/web/tests/WebViewTest.cpp
@@ -902,7 +902,6 @@
 
     RefPtrWillBeRawPtr<blink::Element> element = static_cast<PassRefPtrWillBeRawPtr<blink::Element> >(webViewImpl->mainFrame()->document().body());
     webViewImpl->enterFullScreenForElement(element.get());
-    webViewImpl->willEnterFullScreen();
     webViewImpl->didEnterFullScreen();
 
     // Page scale factor must be 1.0 during fullscreen for elements to be sized
@@ -914,7 +913,6 @@
     webViewImpl->enterFullScreenForElement(otherElement.get());
 
     // Confirm that exiting fullscreen restores the parameters.
-    webViewImpl->willExitFullScreen();
     webViewImpl->didExitFullScreen();
     EXPECT_EQ(2.0f, webViewImpl->pageScaleFactor());
     EXPECT_EQ(116, webViewImpl->mainFrame()->scrollOffset().width);