ASSERTION FAILED: m_wrapper on imported/w3c/web-platform-tests/html/semantics/embedded-content/media-elements/ready-states/autoplay.html
https://bugs.webkit.org/show_bug.cgi?id=209684
<rdar://problem/60987285>

Reviewed by Darin Adler.

Source/WebCore:

Make sure the JS wrapper does not get collected while the HTMLMediaElement is in a state where
is may still fire events (and there are JS event listeners registered). In particular, it used
to be possible for the wrapper to get collected because media playback had started and we would
crash trying to fire the very early 'canplay' JS event.

No new tests, covered by existing test.

* html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::virtualHasPendingActivity const):

LayoutTests:

Unmark the test as crashing.

* platform/mac/TestExpectations:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@259359 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/LayoutTests/ChangeLog b/LayoutTests/ChangeLog
index 85692f1..db36b87 100644
--- a/LayoutTests/ChangeLog
+++ b/LayoutTests/ChangeLog
@@ -1,3 +1,15 @@
+2020-04-01  Chris Dumez  <cdumez@apple.com>
+
+        ASSERTION FAILED: m_wrapper on imported/w3c/web-platform-tests/html/semantics/embedded-content/media-elements/ready-states/autoplay.html
+        https://bugs.webkit.org/show_bug.cgi?id=209684
+        <rdar://problem/60987285>
+
+        Reviewed by Darin Adler.
+
+        Unmark the test as crashing.
+
+        * platform/mac/TestExpectations:
+
 2020-04-01  Jason Lawrence  <lawrence.j@apple.com>
 
         [ iOS wk2 ] crypto/subtle/rsa-indexeddb-non-exportable-private.html is flaky timing out.
diff --git a/LayoutTests/platform/mac/TestExpectations b/LayoutTests/platform/mac/TestExpectations
index 45f4618..bf13677 100644
--- a/LayoutTests/platform/mac/TestExpectations
+++ b/LayoutTests/platform/mac/TestExpectations
@@ -1652,7 +1652,7 @@
 
 webkit.org/b/189680 platform/mac/media/audio-session-category-video-paused.html [ Pass Timeout ]
 
-webkit.org/b/195466 imported/w3c/web-platform-tests/html/semantics/embedded-content/media-elements/ready-states/autoplay.html [ Pass Failure Crash ]
+webkit.org/b/195466 imported/w3c/web-platform-tests/html/semantics/embedded-content/media-elements/ready-states/autoplay.html [ Pass Failure ]
 webkit.org/b/195466 imported/w3c/web-platform-tests/html/semantics/embedded-content/media-elements/error-codes/error.html [ Pass Failure ]
 
 webkit.org/b/193399 media/media-fullscreen-return-to-inline.html [ Pass Timeout Crash ]
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 05344f5..2f22d49 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,21 @@
+2020-04-01  Chris Dumez  <cdumez@apple.com>
+
+        ASSERTION FAILED: m_wrapper on imported/w3c/web-platform-tests/html/semantics/embedded-content/media-elements/ready-states/autoplay.html
+        https://bugs.webkit.org/show_bug.cgi?id=209684
+        <rdar://problem/60987285>
+
+        Reviewed by Darin Adler.
+
+        Make sure the JS wrapper does not get collected while the HTMLMediaElement is in a state where
+        is may still fire events (and there are JS event listeners registered). In particular, it used
+        to be possible for the wrapper to get collected because media playback had started and we would
+        crash trying to fire the very early 'canplay' JS event.
+
+        No new tests, covered by existing test.
+
+        * html/HTMLMediaElement.cpp:
+        (WebCore::HTMLMediaElement::virtualHasPendingActivity const):
+
 2020-04-01  Don Olmstead  <don.olmstead@sony.com>
 
         [PlayStation] Use OBJECT libraries for WebCore and PAL
diff --git a/Source/WebCore/html/HTMLMediaElement.cpp b/Source/WebCore/html/HTMLMediaElement.cpp
index 71517a7..3e8d038 100644
--- a/Source/WebCore/html/HTMLMediaElement.cpp
+++ b/Source/WebCore/html/HTMLMediaElement.cpp
@@ -5761,7 +5761,10 @@
 
 bool HTMLMediaElement::virtualHasPendingActivity() const
 {
-    return (hasAudio() && isPlaying()) || m_asyncEventQueue->hasPendingActivity() || m_playbackTargetIsWirelessQueue.hasPendingTasks() || m_creatingControls;
+    return m_creatingControls
+        || (m_asyncEventQueue->hasPendingActivity() || m_playbackTargetIsWirelessQueue.hasPendingTasks())
+        || (hasAudio() && isPlaying())
+        || (m_player && (!ended() || seeking() || m_networkState >= NETWORK_IDLE) && hasEventListeners());
 }
 
 void HTMLMediaElement::mediaVolumeDidChange()