Cherry-pick: Find the current fullscreen video element rather than the one on top of stack

BUG=17007901

--------------------------------------------------------

Find the current fullscreen video element rather than the one on top of stack

When requestFullscren() is called, we don't know when willEnterFullscreen() will get called.

For android webview, requestFullscreen() might even result in no calls
to willenterfullscreen() if webapp doesn't support fullscreen video.
So before willEnterfullscreen is called, we should not use the provisional
video element on top of the stack to modify the layer tree.

Instead, we should wait for the provisional element to become current.
Therefore, we should call currentFullScreenElementFrom() on the fullscreen document.

BUG=389496

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

Change-Id: I1817f26ae4113aeb8015046b321ebec567a98534
git-svn-id: svn://svn.chromium.org/blink/trunk@178714 bbb929c8-8fbe-4397-9dbb-9b2b20218538
diff --git a/LayoutTests/fullscreen/video-fail-to-enter-full-screen-expected.txt b/LayoutTests/fullscreen/video-fail-to-enter-full-screen-expected.txt
new file mode 100644
index 0000000..5497735
--- /dev/null
+++ b/LayoutTests/fullscreen/video-fail-to-enter-full-screen-expected.txt
@@ -0,0 +1,4 @@
+This tests that the video should not be rendered in full screen if requestFullScreen() fails.
+
+END OF TEST
+
diff --git a/LayoutTests/fullscreen/video-fail-to-enter-full-screen.html b/LayoutTests/fullscreen/video-fail-to-enter-full-screen.html
new file mode 100644
index 0000000..aed847b
--- /dev/null
+++ b/LayoutTests/fullscreen/video-fail-to-enter-full-screen.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<p>This tests that the video should not be rendered in full screen if requestFullScreen() fails.</p>
+<video id="video" width="300"></video>
+<script>
+    if (window.internals)
+    runPixelTests = internals.runtimeFlags.overlayFullscreenVideoEnabled;
+</script>
+<script src="full-screen-test.js"></script>
+<script src="../media/media-controls.js"></script>
+<script>
+    var video = document.getElementById('video');
+    video.webkitRequestFullScreen();
+    setTimeout(function() {
+        endTest();
+    }, 300);
+</script>
diff --git a/LayoutTests/platform/linux/virtual/android/fullscreen/video-fail-to-enter-full-screen-expected.png b/LayoutTests/platform/linux/virtual/android/fullscreen/video-fail-to-enter-full-screen-expected.png
new file mode 100644
index 0000000..b2b7e77
--- /dev/null
+++ b/LayoutTests/platform/linux/virtual/android/fullscreen/video-fail-to-enter-full-screen-expected.png
Binary files differ
diff --git a/Source/core/rendering/compositing/RenderLayerCompositor.cpp b/Source/core/rendering/compositing/RenderLayerCompositor.cpp
index 5d35300..c7c136e 100644
--- a/Source/core/rendering/compositing/RenderLayerCompositor.cpp
+++ b/Source/core/rendering/compositing/RenderLayerCompositor.cpp
@@ -162,13 +162,17 @@
 
 static RenderVideo* findFullscreenVideoRenderer(Document& document)
 {
+    // Recursively find the document that is in fullscreen.
     Element* fullscreenElement = FullscreenElementStack::fullscreenElementFrom(document);
+    Document* contentDocument = &document;
     while (fullscreenElement && fullscreenElement->isFrameOwnerElement()) {
-        Document* contentDocument = toHTMLFrameOwnerElement(fullscreenElement)->contentDocument();
+        contentDocument = toHTMLFrameOwnerElement(fullscreenElement)->contentDocument();
         if (!contentDocument)
             return 0;
         fullscreenElement = FullscreenElementStack::fullscreenElementFrom(*contentDocument);
     }
+    // Get the current fullscreen element from the document.
+    fullscreenElement = FullscreenElementStack::currentFullScreenElementFrom(*contentDocument);
     if (!isHTMLVideoElement(fullscreenElement))
         return 0;
     RenderObject* renderer = fullscreenElement->renderer();