Restore the HTMLVideoElement-specific prefixed fullscreen API behind a flag

The removal has caused more trouble than expected on Android, because
usage is higher there and the UseCounter data from Windows was used in
the "Intent to Remove" thread.

Restore the API behind a flag so that it can more easily be turned on
and off. It's off by default, but likely it will have to be enabled on
at least the upcoming M35 branch.

BUG=346236

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

git-svn-id: svn://svn.chromium.org/blink/trunk@170303 bbb929c8-8fbe-4397-9dbb-9b2b20218538
diff --git a/LayoutTests/fullscreen/full-screen-iframe-legacy-expected.txt b/LayoutTests/fullscreen/full-screen-iframe-legacy-expected.txt
index dbc192c..8bb0e10 100644
--- a/LayoutTests/fullscreen/full-screen-iframe-legacy-expected.txt
+++ b/LayoutTests/fullscreen/full-screen-iframe-legacy-expected.txt
@@ -1,7 +1,9 @@
-Test for bug 61461: Handle entering full screen security restrictions. Note that the original pass condition has been reversed.
+CONSOLE WARNING: 'HTMLVideoElement.webkitEnterFullScreen()' is deprecated. Please use 'Element.requestFullscreen()' and 'Element.webkitRequestFullscreen()' instead.
+Test for bug 61461: Handle entering full screen security restrictions
 
-To test manually, click the video "full screen" button - the page should NOT enter full screen mode.
+To test manually, click the video "full screen" button - the page should enter full screen mode.
 
-EVENT(webkitfullscreenerror)
+EVENT(webkitfullscreenchange)
+TEST(document.getElementById('frame').contentDocument.width==document.width) OK
 END OF TEST
 
diff --git a/LayoutTests/fullscreen/full-screen-iframe-legacy.html b/LayoutTests/fullscreen/full-screen-iframe-legacy.html
index 187a339..6a3bb52 100644
--- a/LayoutTests/fullscreen/full-screen-iframe-legacy.html
+++ b/LayoutTests/fullscreen/full-screen-iframe-legacy.html
@@ -1,7 +1,6 @@
 <p>Test for <a href="https://bugs.webkit.org/show_bug.cgi?id=61461">bug 61461</a>: 
-Handle entering full screen security restrictions.
-Note that the original pass condition has been reversed.</p>
-<p>To test manually, click the video "full screen" button - the page should NOT enter full screen mode.</p>
+Handle entering full screen security restrictions</p>
+<p>To test manually, click the video "full screen" button - the page should enter full screen mode.</p>
 <script src="full-screen-test.js"></script>
 <script src="../media/media-file.js"></script>
 <script>
@@ -11,15 +10,16 @@
 
     runWithKeyDown(function() {
         var video = frame.contentDocument.getElementsByTagName('video')[0];
-        video.webkitRequestFullscreen();
+        video.webkitEnterFullScreen();
     });
 }
 
 function runTest() {
     var frame = document.getElementById('frame');
 
-    waitForEvent(frame.contentDocument, 'webkitfullscreenerror', function() {
-        endTest();
+    waitForEvent(frame.contentDocument, 'webkitfullscreenchange', function() {
+            test("document.getElementById('frame').contentDocument.width==document.width")
+            endTest();
     });
 
     var video = frame.contentDocument.getElementsByTagName('video')[0];
diff --git a/LayoutTests/media/video-prefixed-fullscreen-expected.txt b/LayoutTests/media/video-prefixed-fullscreen-expected.txt
new file mode 100644
index 0000000..31040cb
--- /dev/null
+++ b/LayoutTests/media/video-prefixed-fullscreen-expected.txt
@@ -0,0 +1,25 @@
+CONSOLE WARNING: 'HTMLVideoElement.webkitSupportsFullscreen' is deprecated. Its value is true if the video is loaded.
+CONSOLE WARNING: 'HTMLVideoElement.webkitDisplayingFullscreen' is deprecated. Please use the 'fullscreenchange' and 'webkitfullscreenchange' events instead.
+CONSOLE WARNING: 'HTMLVideoElement.webkitEnterFullscreen()' is deprecated. Please use 'Element.requestFullscreen()' and 'Element.webkitRequestFullscreen()' instead.
+CONSOLE WARNING: 'HTMLVideoElement.webkitExitFullscreen()' is deprecated. Please use 'Document.exitFullscreen()' and 'Document.webkitExitFullscreen()' instead.
+Test the prefixed HTMLVideoElement fullscreen API
+
+EXPECTED (video.webkitSupportsFullscreen == 'false') OK
+EXPECTED (video.webkitDisplayingFullscreen == 'false') OK
+EVENT(loadeddata)
+EXPECTED (video.webkitSupportsFullscreen == 'true') OK
+EXPECTED (video.webkitDisplayingFullscreen == 'false') OK
+RUN(video.webkitEnterFullscreen())
+EVENT(webkitfullscreenerror)
+EXPECTED (video.webkitSupportsFullscreen == 'true') OK
+EXPECTED (video.webkitDisplayingFullscreen == 'false') OK
+RUN(video.webkitEnterFullscreen())
+EVENT(webkitfullscreenchange)
+EXPECTED (video.webkitSupportsFullscreen == 'true') OK
+EXPECTED (video.webkitDisplayingFullscreen == 'true') OK
+RUN(video.webkitExitFullscreen())
+EVENT(webkitfullscreenchange)
+EXPECTED (video.webkitSupportsFullscreen == 'true') OK
+EXPECTED (video.webkitDisplayingFullscreen == 'false') OK
+END OF TEST
+
diff --git a/LayoutTests/media/video-prefixed-fullscreen.html b/LayoutTests/media/video-prefixed-fullscreen.html
new file mode 100644
index 0000000..e00062f
--- /dev/null
+++ b/LayoutTests/media/video-prefixed-fullscreen.html
@@ -0,0 +1,58 @@
+<!doctype html>
+<html>
+    <head>
+        <title>Test the prefixed HTMLVideoElement fullscreen API</title>
+        <script src="media-file.js"></script>
+        <script src="video-test.js"></script>
+        <script>
+            function start()
+            {
+                findMediaElement();
+                testExpected("video.webkitSupportsFullscreen", false);
+                testExpected("video.webkitDisplayingFullscreen", false);
+                video.src = findMediaFile("video", "content/test");
+                waitForEvent("loadeddata", loadeddata);
+            }
+
+            function loadeddata()
+            {
+                testExpected("video.webkitSupportsFullscreen", true);
+                testExpected("video.webkitDisplayingFullscreen", false);
+                run("video.webkitEnterFullscreen()");
+                waitForEvent("webkitfullscreenerror", fullscreenerror);
+            }
+
+            function fullscreenerror()
+            {
+                testExpected("video.webkitSupportsFullscreen", true);
+                testExpected("video.webkitDisplayingFullscreen", false);
+                document.onclick = function()
+                {
+                    run("video.webkitEnterFullscreen()");
+                    waitForEventOnce("webkitfullscreenchange", fullscreenenter);
+                };
+                eventSender.mouseDown();
+                eventSender.mouseUp();
+            }
+
+            function fullscreenenter()
+            {
+                testExpected("video.webkitSupportsFullscreen", true);
+                testExpected("video.webkitDisplayingFullscreen", true);
+                run("video.webkitExitFullscreen()");
+                waitForEvent("webkitfullscreenchange", fullscreenexit);
+            }
+
+            function fullscreenexit()
+            {
+                testExpected("video.webkitSupportsFullscreen", true);
+                testExpected("video.webkitDisplayingFullscreen", false);
+                endTest();
+            }
+        </script>
+    </head>
+    <body onload="start()">
+        <p>Test the prefixed HTMLVideoElement fullscreen API</p>
+        <video></video>
+    </body>
+</html>
diff --git a/Source/core/frame/UseCounter.cpp b/Source/core/frame/UseCounter.cpp
index 90747c9..e8daec0 100644
--- a/Source/core/frame/UseCounter.cpp
+++ b/Source/core/frame/UseCounter.cpp
@@ -661,6 +661,24 @@
     case CSSStyleSheetInsertRuleOptionalArg:
         return "Calling CSSStyleSheet.insertRule() with one argument is deprecated. Please pass the index argument as well: insertRule(x, 0).";
 
+    case PrefixedVideoSupportsFullscreen:
+        return "'HTMLVideoElement.webkitSupportsFullscreen' is deprecated. Its value is true if the video is loaded.";
+
+    case PrefixedVideoDisplayingFullscreen:
+        return "'HTMLVideoElement.webkitDisplayingFullscreen' is deprecated. Please use the 'fullscreenchange' and 'webkitfullscreenchange' events instead.";
+
+    case PrefixedVideoEnterFullscreen:
+        return "'HTMLVideoElement.webkitEnterFullscreen()' is deprecated. Please use 'Element.requestFullscreen()' and 'Element.webkitRequestFullscreen()' instead.";
+
+    case PrefixedVideoExitFullscreen:
+        return "'HTMLVideoElement.webkitExitFullscreen()' is deprecated. Please use 'Document.exitFullscreen()' and 'Document.webkitExitFullscreen()' instead.";
+
+    case PrefixedVideoEnterFullScreen:
+        return "'HTMLVideoElement.webkitEnterFullScreen()' is deprecated. Please use 'Element.requestFullscreen()' and 'Element.webkitRequestFullscreen()' instead.";
+
+    case PrefixedVideoExitFullScreen:
+        return "'HTMLVideoElement.webkitExitFullScreen()' is deprecated. Please use 'Document.exitFullscreen()' and 'Document.webkitExitFullscreen()' instead.";
+
     case PrefixedMediaSourceOpen:
         return "'WebKitMediaSource' is deprecated. Please use 'MediaSource' instead.";
 
diff --git a/Source/core/frame/UseCounter.h b/Source/core/frame/UseCounter.h
index 1291114..294edef 100644
--- a/Source/core/frame/UseCounter.h
+++ b/Source/core/frame/UseCounter.h
@@ -194,6 +194,12 @@
         AttributeSpecified = 162, // Removed in DOM4.
         PrefixedAudioDecodedByteCount = 164,
         PrefixedVideoDecodedByteCount = 165,
+        PrefixedVideoSupportsFullscreen = 166,
+        PrefixedVideoDisplayingFullscreen = 167,
+        PrefixedVideoEnterFullscreen = 168,
+        PrefixedVideoExitFullscreen = 169,
+        PrefixedVideoEnterFullScreen = 170,
+        PrefixedVideoExitFullScreen = 171,
         PrefixedVideoDecodedFrameCount = 172,
         PrefixedVideoDroppedFrameCount = 173,
         PrefixedElementRequestFullscreen = 176,
diff --git a/Source/core/html/HTMLMediaElement.cpp b/Source/core/html/HTMLMediaElement.cpp
index f4f6f36..55800ba 100644
--- a/Source/core/html/HTMLMediaElement.cpp
+++ b/Source/core/html/HTMLMediaElement.cpp
@@ -3190,6 +3190,14 @@
         FullscreenElementStack::from(document()).requestFullScreenForElement(this, 0, FullscreenElementStack::ExemptIFrameAllowFullScreenRequirement);
 }
 
+void HTMLMediaElement::exitFullscreen()
+{
+    WTF_LOG(Media, "HTMLMediaElement::exitFullscreen");
+
+    if (document().settings() && document().settings()->fullScreenEnabled() && isFullscreen())
+        FullscreenElementStack::from(document()).webkitCancelFullScreen();
+}
+
 void HTMLMediaElement::didBecomeFullscreenElement()
 {
     if (hasMediaControls())
diff --git a/Source/core/html/HTMLMediaElement.h b/Source/core/html/HTMLMediaElement.h
index 0ce2107..63f3ab8 100644
--- a/Source/core/html/HTMLMediaElement.h
+++ b/Source/core/html/HTMLMediaElement.h
@@ -234,6 +234,7 @@
 
     bool isFullscreen() const;
     void enterFullscreen();
+    void exitFullscreen();
 
     bool hasClosedCaptions() const;
     bool closedCaptionsVisible() const;
diff --git a/Source/core/html/HTMLVideoElement.cpp b/Source/core/html/HTMLVideoElement.cpp
index 3cc8b3d..4399b98 100644
--- a/Source/core/html/HTMLVideoElement.cpp
+++ b/Source/core/html/HTMLVideoElement.cpp
@@ -121,6 +121,17 @@
         HTMLMediaElement::parseAttribute(name, value);
 }
 
+bool HTMLVideoElement::supportsFullscreen() const
+{
+    if (!document().page())
+        return false;
+
+    if (!player())
+        return false;
+
+    return true;
+}
+
 unsigned HTMLVideoElement::videoWidth() const
 {
     if (!player())
@@ -197,6 +208,35 @@
     return player()->hasVideo() && player()->readyState() >= MediaPlayer::HaveCurrentData;
 }
 
+void HTMLVideoElement::webkitEnterFullscreen(ExceptionState& exceptionState)
+{
+    if (isFullscreen())
+        return;
+
+    if (!supportsFullscreen()) {
+        exceptionState.throwDOMException(InvalidStateError, "This element does not support fullscreen mode.");
+        return;
+    }
+
+    enterFullscreen();
+}
+
+void HTMLVideoElement::webkitExitFullscreen()
+{
+    if (isFullscreen())
+        exitFullscreen();
+}
+
+bool HTMLVideoElement::webkitSupportsFullscreen()
+{
+    return supportsFullscreen();
+}
+
+bool HTMLVideoElement::webkitDisplayingFullscreen()
+{
+    return isFullscreen();
+}
+
 void HTMLVideoElement::didMoveToNewDocument(Document& oldDocument)
 {
     if (m_imageLoader)
diff --git a/Source/core/html/HTMLVideoElement.h b/Source/core/html/HTMLVideoElement.h
index b4d92d1..e14b435 100644
--- a/Source/core/html/HTMLVideoElement.h
+++ b/Source/core/html/HTMLVideoElement.h
@@ -45,6 +45,12 @@
     unsigned videoWidth() const;
     unsigned videoHeight() const;
 
+    // Fullscreen
+    void webkitEnterFullscreen(ExceptionState&);
+    void webkitExitFullscreen();
+    bool webkitSupportsFullscreen();
+    bool webkitDisplayingFullscreen();
+
     // Statistics
     unsigned webkitDecodedFrameCount() const;
     unsigned webkitDroppedFrameCount() const;
@@ -79,6 +85,7 @@
     virtual bool isPresentationAttribute(const QualifiedName&) const OVERRIDE;
     virtual void collectStyleForPresentationAttribute(const QualifiedName&, const AtomicString&, MutableStylePropertySet*) OVERRIDE;
     virtual bool hasVideo() const OVERRIDE { return player() && player()->hasVideo(); }
+    bool supportsFullscreen() const;
     virtual bool isURLAttribute(const Attribute&) const OVERRIDE;
     virtual const AtomicString imageSourceURL() const OVERRIDE;
 
diff --git a/Source/core/html/HTMLVideoElement.idl b/Source/core/html/HTMLVideoElement.idl
index 2d196d3..901da95 100644
--- a/Source/core/html/HTMLVideoElement.idl
+++ b/Source/core/html/HTMLVideoElement.idl
@@ -32,6 +32,16 @@
     readonly attribute unsigned long videoHeight;
     [Reflect, URL, PerWorldBindings, ActivityLogging=SetterForIsolatedWorlds] attribute DOMString poster;
 
+    [RuntimeEnabled=PrefixedVideoFullscreen, DeprecateAs=PrefixedVideoSupportsFullscreen] readonly attribute boolean webkitSupportsFullscreen;
+    [RuntimeEnabled=PrefixedVideoFullscreen, DeprecateAs=PrefixedVideoDisplayingFullscreen] readonly attribute boolean webkitDisplayingFullscreen;
+
+    [RuntimeEnabled=PrefixedVideoFullscreen, DeprecateAs=PrefixedVideoEnterFullscreen, RaisesException, PerWorldBindings, ActivityLogging=ForAllWorlds] void webkitEnterFullscreen();
+    [RuntimeEnabled=PrefixedVideoFullscreen, DeprecateAs=PrefixedVideoExitFullscreen] void webkitExitFullscreen();
+
+    // Note the different capitalization of the "S" in FullScreen.
+    [RuntimeEnabled=PrefixedVideoFullscreen, DeprecateAs=PrefixedVideoEnterFullScreen, ImplementedAs=webkitEnterFullscreen, RaisesException, PerWorldBindings, ActivityLogging=ForAllWorlds] void webkitEnterFullScreen();
+    [RuntimeEnabled=PrefixedVideoFullscreen, DeprecateAs=PrefixedVideoExitFullScreen, ImplementedAs=webkitExitFullscreen] void webkitExitFullScreen();
+
     // The number of frames that have been decoded and made available for
     // playback.
     [MeasureAs=PrefixedVideoDecodedFrameCount] readonly attribute unsigned long webkitDecodedFrameCount;
diff --git a/Source/platform/RuntimeEnabledFeatures.in b/Source/platform/RuntimeEnabledFeatures.in
index 093fc0b..c79f82d 100644
--- a/Source/platform/RuntimeEnabledFeatures.in
+++ b/Source/platform/RuntimeEnabledFeatures.in
@@ -18,6 +18,7 @@
 ApplicationCache status=stable
 AuthorShadowDOMForAnyElement
 BatteryStatus status=test
+PrefixedVideoFullscreen status=test
 
 // Enable bleeding-edge code to make Blink draw content faster.
 // The changes enabled behind this flag are very likely to break lots of content.