Consider the current document when allowing autoplay quirks.
authormrajca@apple.com <mrajca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 11 Apr 2017 20:39:51 +0000 (20:39 +0000)
committermrajca@apple.com <mrajca@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 11 Apr 2017 20:39:51 +0000 (20:39 +0000)
https://bugs.webkit.org/show_bug.cgi?id=170744

Reviewed by Eric Carlson.

Added API test.

In addition to checking if the top-level document supports autoplay quirks, we should check
if the current document supports quirks. This allows all embedded YouTube videos (which use
iframes) to play correctly if the client allows autoplay quirks on youtube.com.

* html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::dispatchPlayPauseEventsIfNeedsQuirks):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@215249 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/html/HTMLMediaElement.cpp
Tools/TestWebKitAPI/Tests/WebKit2/autoplay-check-frame.html
Tools/TestWebKitAPI/Tests/WebKit2Cocoa/WebsitePolicies.mm

index fa0a2a5..c3f98f6 100644 (file)
@@ -1,3 +1,19 @@
+2017-04-11  Matt Rajca  <mrajca@apple.com>
+
+        Consider the current document when allowing autoplay quirks.
+        https://bugs.webkit.org/show_bug.cgi?id=170744
+
+        Reviewed by Eric Carlson.
+
+        Added API test.
+
+        In addition to checking if the top-level document supports autoplay quirks, we should check
+        if the current document supports quirks. This allows all embedded YouTube videos (which use
+        iframes) to play correctly if the client allows autoplay quirks on youtube.com.
+
+        * html/HTMLMediaElement.cpp:
+        (WebCore::HTMLMediaElement::dispatchPlayPauseEventsIfNeedsQuirks):
+
 2017-04-11  Ryan Haddad  <ryanhaddad@apple.com>
 
         Unreviewed, rolling out r215245.
index 08f1e00..9068ec8 100644 (file)
@@ -2320,7 +2320,8 @@ SuccessOr<MediaPlaybackDenialReason> HTMLMediaElement::canTransitionFromAutoplay
 
 void HTMLMediaElement::dispatchPlayPauseEventsIfNeedsQuirks()
 {
-    if (!needsAutoplayPlayPauseEventsQuirk(document().topDocument()))
+    auto& document = this->document();
+    if (!needsAutoplayPlayPauseEventsQuirk(document) && !needsAutoplayPlayPauseEventsQuirk(document.topDocument()))
         return;
 
     scheduleEvent(eventNames().playingEvent);
index 2c43fda..a0ee021 100644 (file)
@@ -8,9 +8,13 @@
                     window.parent.postMessage('did-not-play', '*');
                 });
             }
+
+            function videoDidPause() {
+                window.parent.postMessage('on-pause', '*');
+            }
         </script>
     </head>
     <body onload="pageLoaded()">
-        <video id="video" playsinline src="test.mp4" />
+        <video id="video" onpause=videoDidPause() playsinline src="test.mp4" />
     </body>
 </html>
index 10480c1..cb7e8c0 100644 (file)
@@ -161,7 +161,7 @@ TEST(WebKit2, WebsitePoliciesContentBlockersEnabled)
 
 @interface AutoplayPoliciesDelegate : NSObject <WKNavigationDelegate, WKUIDelegate>
 @property (nonatomic, copy) _WKWebsiteAutoplayPolicy(^autoplayPolicyForURL)(NSURL *);
-@property (nonatomic) BOOL allowsAutoplayQuirks;
+@property (nonatomic, copy) BOOL(^allowsAutoplayQuirksForURL)(NSURL *);
 @end
 
 @implementation AutoplayPoliciesDelegate
@@ -176,7 +176,8 @@ TEST(WebKit2, WebsitePoliciesContentBlockersEnabled)
 - (void)_webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy, _WKWebsitePolicies *))decisionHandler
 {
     _WKWebsitePolicies *websitePolicies = [[[_WKWebsitePolicies alloc] init] autorelease];
-    websitePolicies.allowsAutoplayQuirks = _allowsAutoplayQuirks;
+    if (_allowsAutoplayQuirksForURL)
+        websitePolicies.allowsAutoplayQuirks = _allowsAutoplayQuirksForURL(navigationAction.request.URL);
     if (_autoplayPolicyForURL)
         websitePolicies.autoplayPolicy = _autoplayPolicyForURL(navigationAction.request.URL);
     decisionHandler(WKNavigationActionPolicyAllow, websitePolicies);
@@ -487,13 +488,30 @@ TEST(WebKit2, WebsitePoliciesAutoplayQuirks)
 
     NSURLRequest *requestWithAudio = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"autoplay-check" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
 
-    [delegate setAllowsAutoplayQuirks:YES];
+    [delegate setAllowsAutoplayQuirksForURL:^(NSURL *url) {
+        return YES;
+    }];
     [delegate setAutoplayPolicyForURL:^(NSURL *) {
         return _WKWebsiteAutoplayPolicyDeny;
     }];
     [webView loadRequest:requestWithAudio];
     [webView waitForMessage:@"did-not-play"];
     [webView waitForMessage:@"on-pause"];
+
+    receivedAutoplayEvent = std::nullopt;
+    [webView loadHTMLString:@"" baseURL:nil];
+
+    NSURLRequest *requestWithAudioInFrame = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"autoplay-check-in-iframe" withExtension:@"html" subdirectory:@"TestWebKitAPI.resources"]];
+
+    [delegate setAllowsAutoplayQuirksForURL:^(NSURL *url) {
+        return [url.lastPathComponent isEqualToString:@"autoplay-check-frame.html"];
+    }];
+    [delegate setAutoplayPolicyForURL:^(NSURL *) {
+        return _WKWebsiteAutoplayPolicyDeny;
+    }];
+    [webView loadRequest:requestWithAudioInFrame];
+    [webView waitForMessage:@"did-not-play"];
+    [webView waitForMessage:@"on-pause"];
 }
 #endif