Title: [279487] trunk
Revision
279487
Author
eric.carl...@apple.com
Date
2021-07-01 17:08:31 -0700 (Thu, 01 Jul 2021)

Log Message

WebAudio auto-play policy should come from top document
https://bugs.webkit.org/show_bug.cgi?id=227593
rdar://76920375

Reviewed by Chris Dumez.

Source/WebCore:

Tests: media/auto-play-video-in-about-blank-iframe.html
       media/auto-play-web-audio-in-about-blank-iframe.html

* Modules/webaudio/AudioContext.cpp:
(WebCore::AudioContext::constructCommon): Get auto-play policy from document()->topDocument().

* testing/Internals.cpp:
(WebCore::Internals::setDocumentAutoplayPolicy): New method to test auto-play policy.
* testing/Internals.h:
* testing/Internals.idl:

LayoutTests:

* media/auto-play-video-in-about-blank-iframe-expected.txt: Added.
* media/auto-play-video-in-about-blank-iframe.html: Added.
* media/auto-play-web-audio-in-about-blank-iframe-expected.txt: Added.
* media/auto-play-web-audio-in-about-blank-iframe.html: Added.

* media/video-test.js:
(waitForEventWithTimeout): Return event.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (279486 => 279487)


--- trunk/LayoutTests/ChangeLog	2021-07-01 23:57:13 UTC (rev 279486)
+++ trunk/LayoutTests/ChangeLog	2021-07-02 00:08:31 UTC (rev 279487)
@@ -1,3 +1,19 @@
+2021-07-01  Eric Carlson  <eric.carl...@apple.com>
+
+        WebAudio auto-play policy should come from top document
+        https://bugs.webkit.org/show_bug.cgi?id=227593
+        rdar://76920375
+
+        Reviewed by Chris Dumez.
+
+        * media/auto-play-video-in-about-blank-iframe-expected.txt: Added.
+        * media/auto-play-video-in-about-blank-iframe.html: Added.
+        * media/auto-play-web-audio-in-about-blank-iframe-expected.txt: Added.
+        * media/auto-play-web-audio-in-about-blank-iframe.html: Added.
+
+        * media/video-test.js:
+        (waitForEventWithTimeout): Return event.
+
 2021-07-01  Cameron McCormack  <hey...@apple.com>
 
         Move some Mac MathML test expectation files around

Added: trunk/LayoutTests/media/auto-play-video-in-about-blank-iframe-expected.txt (0 => 279487)


--- trunk/LayoutTests/media/auto-play-video-in-about-blank-iframe-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/media/auto-play-video-in-about-blank-iframe-expected.txt	2021-07-02 00:08:31 UTC (rev 279487)
@@ -0,0 +1,19 @@
+
+
+*** Test with auto-play policy set to 'Allow'
+EVENT(load)
+EVENT(message)
+PASS: Video auto-played
+
+*** Test with auto-play policy set to 'AllowWithoutSound'
+EVENT(load)
+EVENT(message)
+PASS: Video did not auto-play
+
+*** Test with auto-play policy set to 'Deny'
+EVENT(load)
+EVENT(message)
+PASS: Video did not auto-play
+
+END OF TEST
+

Added: trunk/LayoutTests/media/auto-play-video-in-about-blank-iframe.html (0 => 279487)


--- trunk/LayoutTests/media/auto-play-video-in-about-blank-iframe.html	                        (rev 0)
+++ trunk/LayoutTests/media/auto-play-video-in-about-blank-iframe.html	2021-07-02 00:08:31 UTC (rev 279487)
@@ -0,0 +1,80 @@
+<head>
+    <style>
+        iframe {
+            width: 320px;
+            height: 240px;
+        }
+    </style>
+    <script src=""
+    <script src=""
+    <script>
+    if (window.testRunner) {
+        testRunner.waitUntilDone();
+        testRunner.dumpAsText();
+    }
+
+    async function checkAutoPlay(allowed)
+    {
+        let message = allowed ? "Video auto-played" : "Video did not auto-play";
+        let video = iframe.contentDocument.querySelector("video");
+
+        let played;
+        let counter = 0;
+        while (++counter < 20) {
+            played = video.ended || video.currentTime != 0;
+            if (allowed === played)
+                break;
+            await new Promise(resolve => setTimeout(resolve, 50));
+        }
+
+        if (allowed == played)
+            consoleWrite(`PASS: ${allowed ? "Video auto-played" : "Video did not auto-play"}`);
+        else
+            consoleWrite(`FAIL: ${allowed ? "Video did not auto-play" : "Video auto-played"}`);
+    }
+
+    async function loadVideo(policy, allow)
+    {
+        consoleWrite(`<br>*** Test with auto-play policy set to '${policy}'`);
+        
+        if (window.internals)
+            internals.setDocumentAutoplayPolicy(document, policy)
+
+        iframe.src = '';
+        await waitForEventWithTimeout(iframe, 'load', 4000, '"about:blank" iframe failed to load!');
+
+        let script = iframe.contentDocument.head.appendChild(iframe.contentDocument.createElement('script'));
+        let code = `
+            let video=document.createElement('video');
+            document.body.appendChild(video);
+            video.controls = 1;
+            video.autoplay = 1;
+            video.src = '';
+            video._oncanplaythrough_ = () => {
+                window.parent.postMessage('loaded', '*') 
+            }`;
+        script.appendChild(iframe.contentDocument.createTextNode(code));
+
+        await waitForEventWithTimeout(window, 'message', 4000, 'iframe failed to send message!');
+        await new Promise(resolve => setTimeout(resolve, 250));
+        await checkAutoPlay(allow);
+    }
+    
+    window.addEventListener('load', async () => {
+        iframe = document.querySelector('iframe');
+
+        await loadVideo('Allow', true);
+
+        await loadVideo('AllowWithoutSound', false);
+
+        await loadVideo('Deny', false);
+
+        consoleWrite('');
+        endTest();
+    }, false);
+
+    </script>
+</head>
+<body>
+    <iframe ></iframe>
+</body>

Added: trunk/LayoutTests/media/auto-play-web-audio-in-about-blank-iframe-expected.txt (0 => 279487)


--- trunk/LayoutTests/media/auto-play-web-audio-in-about-blank-iframe-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/media/auto-play-web-audio-in-about-blank-iframe-expected.txt	2021-07-02 00:08:31 UTC (rev 279487)
@@ -0,0 +1,19 @@
+
+
+*** Test with auto-play policy set to 'Allow'
+EVENT(load)
+EVENT(message)
+PASS: Video auto-played
+
+*** Test with auto-play policy set to 'AllowWithoutSound'
+EVENT(load)
+EVENT(message)
+PASS: Video did not auto-play
+
+*** Test with auto-play policy set to 'Deny'
+EVENT(load)
+EVENT(message)
+PASS: Video did not auto-play
+
+END OF TEST
+

Added: trunk/LayoutTests/media/auto-play-web-audio-in-about-blank-iframe.html (0 => 279487)


--- trunk/LayoutTests/media/auto-play-web-audio-in-about-blank-iframe.html	                        (rev 0)
+++ trunk/LayoutTests/media/auto-play-web-audio-in-about-blank-iframe.html	2021-07-02 00:08:31 UTC (rev 279487)
@@ -0,0 +1,67 @@
+<head>
+    <style>
+        iframe {
+            width: 320px;
+            height: 240px;
+        }
+    </style>
+    <script src=""
+    <script src=""
+    <script>
+    if (window.testRunner) {
+        testRunner.waitUntilDone();
+        testRunner.dumpAsText();
+    }
+
+    async function checkAutoPlay(state, allowed)
+    {
+        let message = allowed ? "AudioContext auto-played" : "AudioContext did not auto-play";
+        if (allowed === (event.data ="" "running"))
+            consoleWrite(`PASS: ${allowed ? "Video auto-played" : "Video did not auto-play"}`);
+        else
+            consoleWrite(`FAIL: ${allowed ? "Video did not auto-play" : "Video auto-played"}`);
+    }
+
+    async function loadVideo(policy, allow)
+    {
+        consoleWrite(`<br>*** Test with auto-play policy set to '${policy}'`);
+        
+        if (window.internals)
+            internals.setDocumentAutoplayPolicy(document, policy)
+
+        iframe.src = '';
+        await waitForEventWithTimeout(iframe, 'load', 4000, '"about:blank" iframe failed to load!');
+
+        let script = iframe.contentDocument.head.appendChild(iframe.contentDocument.createElement('script'));
+        let code = `
+            let context = new AudioContext();
+            let node = context.createBufferSource();
+            node.connect(context.destination);
+            node.start();
+            setTimeout(() => {
+                window.parent.postMessage(context.state, '*');
+            }, 250)`;
+        script.appendChild(iframe.contentDocument.createTextNode(code));
+
+        let event = await waitForEventWithTimeout(window, 'message', 4000, 'iframe failed to send message!');
+        await checkAutoPlay(event.data, allow);
+    }
+    
+    window.addEventListener('load', async () => {
+        iframe = document.querySelector('iframe');
+
+        await loadVideo('Allow', true);
+
+        await loadVideo('AllowWithoutSound', false);
+
+        await loadVideo('Deny', false);
+
+        consoleWrite('');
+        endTest();
+    }, false);
+
+    </script>
+</head>
+<body>
+    <iframe ></iframe>
+</body>

Modified: trunk/LayoutTests/media/video-test.js (279486 => 279487)


--- trunk/LayoutTests/media/video-test.js	2021-07-01 23:57:13 UTC (rev 279486)
+++ trunk/LayoutTests/media/video-test.js	2021-07-02 00:08:31 UTC (rev 279487)
@@ -213,7 +213,7 @@
         }
         
         consoleWrite(`EVENT(${result.type})`);
-        Promise.resolve(result);
+        return Promise.resolve(result);
     });
 }
 

Modified: trunk/Source/WebCore/ChangeLog (279486 => 279487)


--- trunk/Source/WebCore/ChangeLog	2021-07-01 23:57:13 UTC (rev 279486)
+++ trunk/Source/WebCore/ChangeLog	2021-07-02 00:08:31 UTC (rev 279487)
@@ -1,3 +1,22 @@
+2021-07-01  Eric Carlson  <eric.carl...@apple.com>
+
+        WebAudio auto-play policy should come from top document
+        https://bugs.webkit.org/show_bug.cgi?id=227593
+        rdar://76920375
+
+        Reviewed by Chris Dumez.
+
+        Tests: media/auto-play-video-in-about-blank-iframe.html
+               media/auto-play-web-audio-in-about-blank-iframe.html
+
+        * Modules/webaudio/AudioContext.cpp:
+        (WebCore::AudioContext::constructCommon): Get auto-play policy from document()->topDocument().
+
+        * testing/Internals.cpp:
+        (WebCore::Internals::setDocumentAutoplayPolicy): New method to test auto-play policy.
+        * testing/Internals.h:
+        * testing/Internals.idl:
+
 2021-07-01  Youenn Fablet  <you...@apple.com>
 
         Disable relay for UDP sockets when not needed

Modified: trunk/Source/WebCore/Modules/webaudio/AudioContext.cpp (279486 => 279487)


--- trunk/Source/WebCore/Modules/webaudio/AudioContext.cpp	2021-07-01 23:57:13 UTC (rev 279486)
+++ trunk/Source/WebCore/Modules/webaudio/AudioContext.cpp	2021-07-02 00:08:31 UTC (rev 279487)
@@ -135,7 +135,7 @@
 void AudioContext::constructCommon()
 {
     ASSERT(document());
-    if (document()->audioPlaybackRequiresUserGesture())
+    if (document()->topDocument().audioPlaybackRequiresUserGesture())
         addBehaviorRestriction(RequireUserGestureForAudioStartRestriction);
     else
         m_restrictions = NoRestrictions;

Modified: trunk/Source/WebCore/testing/Internals.cpp (279486 => 279487)


--- trunk/Source/WebCore/testing/Internals.cpp	2021-07-01 23:57:13 UTC (rev 279486)
+++ trunk/Source/WebCore/testing/Internals.cpp	2021-07-02 00:08:31 UTC (rev 279487)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2012 Google Inc. All rights reserved.
- * Copyright (C) 2013-2020 Apple Inc. All rights reserved.
+ * Copyright (C) 2013-2021 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -6418,5 +6418,20 @@
     return result.toString();
 }
 
+ExceptionOr<void> Internals::setDocumentAutoplayPolicy(Document& document, Internals::AutoplayPolicy policy)
+{
+    static_assert(static_cast<uint8_t>(WebCore::AutoplayPolicy::Default) == static_cast<uint8_t>(Internals::AutoplayPolicy::Default), "Internals::Default != WebCore::Default");
+    static_assert(static_cast<uint8_t>(WebCore::AutoplayPolicy::Allow) == static_cast<uint8_t>(Internals::AutoplayPolicy::Allow), "Internals::Allow != WebCore::Allow");
+    static_assert(static_cast<uint8_t>(WebCore::AutoplayPolicy::AllowWithoutSound) == static_cast<uint8_t>(Internals::AutoplayPolicy::AllowWithoutSound), "Internals::AllowWithoutSound != WebCore::AllowWithoutSound");
+    static_assert(static_cast<uint8_t>(WebCore::AutoplayPolicy::Deny) == static_cast<uint8_t>(Internals::AutoplayPolicy::Deny), "Internals::Deny != WebCore::Deny");
 
+    auto* loader = document.loader();
+    if (!loader)
+        return Exception { InvalidStateError };
+
+    loader->setAutoplayPolicy(static_cast<WebCore::AutoplayPolicy>(policy));
+
+    return { };
+}
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/testing/Internals.h (279486 => 279487)


--- trunk/Source/WebCore/testing/Internals.h	2021-07-01 23:57:13 UTC (rev 279486)
+++ trunk/Source/WebCore/testing/Internals.h	2021-07-02 00:08:31 UTC (rev 279487)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2012 Google Inc. All rights reserved.
- * Copyright (C) 2013-2020 Apple Inc. All rights reserved.
+ * Copyright (C) 2013-2021 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -1169,6 +1169,14 @@
 
     String dumpStyleResolvers();
 
+    enum class AutoplayPolicy : uint8_t {
+        Default,
+        Allow,
+        AllowWithoutSound,
+        Deny,
+    };
+    ExceptionOr<void> setDocumentAutoplayPolicy(Document&, AutoplayPolicy);
+
 private:
     explicit Internals(Document&);
     Document* contextDocument() const;

Modified: trunk/Source/WebCore/testing/Internals.idl (279486 => 279487)


--- trunk/Source/WebCore/testing/Internals.idl	2021-07-01 23:57:13 UTC (rev 279486)
+++ trunk/Source/WebCore/testing/Internals.idl	2021-07-02 00:08:31 UTC (rev 279487)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2012 Google Inc. All rights reserved.
- * Copyright (C) 2013-2019 Apple Inc. All rights reserved.
+ * Copyright (C) 2013-2021 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -105,6 +105,13 @@
     "Timeout"
 };
 
+enum AutoplayPolicy {
+    "Default",
+    "Allow",
+    "AllowWithoutSound",
+    "Deny"
+};
+
 [
     ExportMacro=WEBCORE_TESTSUPPORT_EXPORT,
     Conditional=VIDEO,
@@ -1038,4 +1045,6 @@
     undefined systemBeep();
 
     DOMString dumpStyleResolvers();
+
+    undefined setDocumentAutoplayPolicy(Document document, AutoplayPolicy policy);
 };
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to