Make Document audio producers use WeakPtr
authoryouenn@apple.com <youenn@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 30 Apr 2019 15:16:11 +0000 (15:16 +0000)
committeryouenn@apple.com <youenn@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 30 Apr 2019 15:16:11 +0000 (15:16 +0000)
https://bugs.webkit.org/show_bug.cgi?id=197382

Reviewed by Eric Carlson.

Source/WebCore:

Move from a hash set of raw pointers to a hash set of weak pointers.
This helps make the code cleaner.
No observable change of behavior.

* Modules/mediastream/MediaStreamTrack.h:
* dom/Document.cpp:
(WebCore::Document::addAudioProducer):
(WebCore::Document::removeAudioProducer):
(WebCore::Document::updateIsPlayingMedia):
(WebCore::Document::pageMutedStateDidChange):
* dom/Document.h:
* html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::updateActiveTextTrackCues):
* html/HTMLMediaElement.h:
* page/MediaProducer.h:

Source/WTF:

* wtf/WeakHashSet.h:
(WTF::WeakHashSet::hasNullReferences const):

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

Source/WTF/ChangeLog
Source/WTF/wtf/WeakHashSet.h
Source/WebCore/ChangeLog
Source/WebCore/Modules/mediastream/MediaStreamTrack.h
Source/WebCore/dom/Document.cpp
Source/WebCore/dom/Document.h
Source/WebCore/html/HTMLMediaElement.cpp
Source/WebCore/html/HTMLMediaElement.h
Source/WebCore/page/MediaProducer.h

index 83599a9..96192bc 100644 (file)
@@ -1,3 +1,13 @@
+2019-04-30  Youenn Fablet  <youenn@apple.com>
+
+        Make Document audio producers use WeakPtr
+        https://bugs.webkit.org/show_bug.cgi?id=197382
+
+        Reviewed by Eric Carlson.
+
+        * wtf/WeakHashSet.h:
+        (WTF::WeakHashSet::hasNullReferences const):
+
 2019-04-29  Alex Christensen  <achristensen@webkit.org>
 
         <rdar://problem/50299396> Fix internal High Sierra build
index 8c8987b..092011c 100644 (file)
@@ -25,6 +25,7 @@
 
 #pragma once
 
+#include <wtf/Algorithms.h>
 #include <wtf/HashSet.h>
 #include <wtf/HashTraits.h>
 #include <wtf/WeakPtr.h>
@@ -123,6 +124,11 @@ public:
         return true;
     }
 
+    bool hasNullReferences() const
+    {
+        return WTF::anyOf(m_set, [] (auto& value) { return !value->get(); });
+    }
+
     unsigned computeSize() const
     {
         const_cast<WeakReferenceSet&>(m_set).removeIf([] (auto& value) { return !value->get(); });
index 7627182..4af44ef 100644 (file)
@@ -1,3 +1,26 @@
+2019-04-30  Youenn Fablet  <youenn@apple.com>
+
+        Make Document audio producers use WeakPtr
+        https://bugs.webkit.org/show_bug.cgi?id=197382
+
+        Reviewed by Eric Carlson.
+
+        Move from a hash set of raw pointers to a hash set of weak pointers.
+        This helps make the code cleaner.
+        No observable change of behavior.
+
+        * Modules/mediastream/MediaStreamTrack.h:
+        * dom/Document.cpp:
+        (WebCore::Document::addAudioProducer):
+        (WebCore::Document::removeAudioProducer):
+        (WebCore::Document::updateIsPlayingMedia):
+        (WebCore::Document::pageMutedStateDidChange):
+        * dom/Document.h:
+        * html/HTMLMediaElement.cpp:
+        (WebCore::HTMLMediaElement::updateActiveTextTrackCues):
+        * html/HTMLMediaElement.h:
+        * page/MediaProducer.h:
+
 2019-04-30  Antti Koivisto  <antti@apple.com>
 
         Tighten type of ScrollingTree:rootNode() to ScrollingTreeFrameScrollingNode
index 7c11f3f..de8b5a6 100644 (file)
@@ -51,8 +51,7 @@ class MediaStreamTrack
     : public RefCounted<MediaStreamTrack>
     , public ActiveDOMObject
     , public EventTargetWithInlineData
-    , public CanMakeWeakPtr<MediaStreamTrack>
-    , private MediaProducer
+    , public MediaProducer
     , private MediaStreamTrackPrivate::Observer
 #if !RELEASE_LOG_DISABLED
     , private LoggerHelper
index 1794bed..224ea10 100644 (file)
@@ -3902,13 +3902,13 @@ void Document::updateViewportUnitsOnResize()
 
 void Document::addAudioProducer(MediaProducer& audioProducer)
 {
-    m_audioProducers.add(&audioProducer);
+    m_audioProducers.add(audioProducer);
     updateIsPlayingMedia();
 }
 
 void Document::removeAudioProducer(MediaProducer& audioProducer)
 {
-    m_audioProducers.remove(&audioProducer);
+    m_audioProducers.remove(audioProducer);
     updateIsPlayingMedia();
 }
 
@@ -3926,9 +3926,11 @@ void Document::noteUserInteractionWithMediaElement()
 
 void Document::updateIsPlayingMedia(uint64_t sourceElementID)
 {
+    ASSERT(!m_audioProducers.hasNullReferences());
+
     MediaProducer::MediaStateFlags state = MediaProducer::IsNotPlaying;
-    for (auto* audioProducer : m_audioProducers)
-        state |= audioProducer->mediaState();
+    for (auto& audioProducer : m_audioProducers)
+        state |= audioProducer.mediaState();
 
 #if ENABLE(MEDIA_SESSION)
     if (HTMLMediaElement* sourceElement = HTMLMediaElement::elementWithID(sourceElementID)) {
@@ -3969,8 +3971,8 @@ void Document::updateIsPlayingMedia(uint64_t sourceElementID)
 
 void Document::pageMutedStateDidChange()
 {
-    for (auto* audioProducer : m_audioProducers)
-        audioProducer->pageMutedStateDidChange();
+    for (auto& audioProducer : m_audioProducers)
+        audioProducer.pageMutedStateDidChange();
 }
 
 static bool isNodeInSubtree(Node& node, Node& container, Document::NodeRemoval nodeRemoval)
index 1417c2e..45f8598 100644 (file)
@@ -66,6 +66,7 @@
 #include <wtf/Logger.h>
 #include <wtf/ObjectIdentifier.h>
 #include <wtf/UniqueRef.h>
+#include <wtf/WeakHashSet.h>
 #include <wtf/WeakPtr.h>
 #include <wtf/text/AtomicStringHash.h>
 
@@ -1891,7 +1892,7 @@ private:
 
     Ref<CSSFontSelector> m_fontSelector;
 
-    HashSet<MediaProducer*> m_audioProducers;
+    WeakHashSet<MediaProducer> m_audioProducers;
 
     HashSet<ShadowRoot*> m_inDocumentShadowRoots;
 
index 6b0f42f..e3093f2 100644 (file)
@@ -1768,9 +1768,9 @@ void HTMLMediaElement::updateActiveTextTrackCues(const MediaTime& movieTime)
             if (!weakThis)
                 return;
 
-            auto currentMediaTime = weakThis->currentMediaTime();
+            auto currentMediaTime = this->currentMediaTime();
             INFO_LOG(LOGIDENTIFIER, " lambda, currentMediaTime: ", currentMediaTime);
-            weakThis->updateActiveTextTrackCues(currentMediaTime);
+            this->updateActiveTextTrackCues(currentMediaTime);
         }, nextInterestingTime);
     }
 
index 576c7dc..2296c66 100644 (file)
@@ -573,6 +573,8 @@ public:
 
     enum class AutoplayEventPlaybackState { None, PreventedAutoplay, StartedWithUserGesture, StartedWithoutUserGesture };
 
+    using HTMLElement::weakPtrFactory;
+
 protected:
     HTMLMediaElement(const QualifiedName&, Document&, bool createdByParser);
     virtual void finishInitialization();
index b01ab94..92c19e8 100644 (file)
 
 #pragma once
 
+#include <wtf/WeakPtr.h>
+
 namespace WebCore {
 
-class MediaProducer {
+class MediaProducer : public CanMakeWeakPtr<MediaProducer> {
 public:
     enum MediaState {
         IsNotPlaying = 0,