Title: [278543] trunk/Source
Revision
278543
Author
[email protected]
Date
2021-06-06 21:22:38 -0700 (Sun, 06 Jun 2021)

Log Message

Stop using legacy EventLoopDeferrableTask
https://bugs.webkit.org/show_bug.cgi?id=226700

Reviewed by Darin Adler.

Source/WebCore:

Stop using legacy EventLoopDeferrableTask and drop the class entirely. Call sites are
now using the HTML event loop directly and using WTF::CancellableTask to wrap the task
they schedule. This achieves the same result as EventLoopDeferrableTask but is more
lightweight and flexible.

* animation/DocumentTimelinesController.cpp:
(WebCore::DocumentTimelinesController::DocumentTimelinesController):
(WebCore::DocumentTimelinesController::detachFromDocument):
(WebCore::DocumentTimelinesController::cacheCurrentTime):
* animation/DocumentTimelinesController.h:
* dom/ActiveDOMObject.h:
* html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::HTMLMediaElement):
(WebCore::HTMLMediaElement::~HTMLMediaElement):
(WebCore::HTMLMediaElement::scheduleCheckPlaybackTargetCompatability):
(WebCore::HTMLMediaElement::seekWithTolerance):
(WebCore::HTMLMediaElement::setVolume):
(WebCore::HTMLMediaElement::scheduleConfigureTextTracks):
(WebCore::HTMLMediaElement::scheduleMediaEngineWasUpdated):
(WebCore::HTMLMediaElement::scheduleUpdatePlayState):
(WebCore::HTMLMediaElement::cancelPendingTasks):
(WebCore::HTMLMediaElement::closeTaskQueues):
(WebCore::HTMLMediaElement::suspend):
(WebCore::HTMLMediaElement::resume):
(WebCore::HTMLMediaElement::markCaptionAndSubtitleTracksAsUnconfigured):
(WebCore::HTMLMediaElement::mediaPlayerBufferedTimeRangesChanged):
(WebCore::HTMLMediaElement::scheduleUpdateMediaState):
(WebCore::HTMLMediaElement::playbackControlsManagerBehaviorRestrictionsTimerFired):
* html/HTMLMediaElement.h:
* platform/DeferrableTask.h:

Source/WTF:

Introduce new WTF::CancellableTask type which is a wrapper around a WTF::Function.
It can create a handle for the task and this handle can be used to check if the
task is still pending and to cancel it. This is useful when scheduling tasks in the
event loop for example as there are cases where we need to know if a task we
scheduled is still pending (to avoid double scheduling) or where we need to cancel
a previously scheduled task.

* WTF.xcodeproj/project.pbxproj:
* wtf/CMakeLists.txt:
* wtf/CancellableTask.h: Added.
(WTF::CancellableTask::CancellableTask):
(WTF::CancellableTask::operator()):
(WTF::CancellableTask::isPending const):
(WTF::CancellableTask::cancel):
(WTF::CancellableTask::Handle::Handle):
(WTF::CancellableTask::Handle::isPending const):
(WTF::CancellableTask::Handle::cancel):
(WTF::CancellableTask::createHandle):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (278542 => 278543)


--- trunk/Source/WTF/ChangeLog	2021-06-07 04:11:09 UTC (rev 278542)
+++ trunk/Source/WTF/ChangeLog	2021-06-07 04:22:38 UTC (rev 278543)
@@ -1,3 +1,29 @@
+2021-06-06  Chris Dumez  <[email protected]>
+
+        Stop using legacy EventLoopDeferrableTask
+        https://bugs.webkit.org/show_bug.cgi?id=226700
+
+        Reviewed by Darin Adler.
+
+        Introduce new WTF::CancellableTask type which is a wrapper around a WTF::Function.
+        It can create a handle for the task and this handle can be used to check if the
+        task is still pending and to cancel it. This is useful when scheduling tasks in the
+        event loop for example as there are cases where we need to know if a task we
+        scheduled is still pending (to avoid double scheduling) or where we need to cancel
+        a previously scheduled task.
+
+        * WTF.xcodeproj/project.pbxproj:
+        * wtf/CMakeLists.txt:
+        * wtf/CancellableTask.h: Added.
+        (WTF::CancellableTask::CancellableTask):
+        (WTF::CancellableTask::operator()):
+        (WTF::CancellableTask::isPending const):
+        (WTF::CancellableTask::cancel):
+        (WTF::CancellableTask::Handle::Handle):
+        (WTF::CancellableTask::Handle::isPending const):
+        (WTF::CancellableTask::Handle::cancel):
+        (WTF::CancellableTask::createHandle):
+
 2021-06-04  Chris Dumez  <[email protected]>
 
         FileSystem::readFromFile() should return data as `void*`

Modified: trunk/Source/WTF/WTF.xcodeproj/project.pbxproj (278542 => 278543)


--- trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2021-06-07 04:11:09 UTC (rev 278542)
+++ trunk/Source/WTF/WTF.xcodeproj/project.pbxproj	2021-06-07 04:22:38 UTC (rev 278543)
@@ -378,6 +378,7 @@
 		430B47871AAAAC1A001223DA /* StringCommon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringCommon.h; sourceTree = "<group>"; };
 		4427C5A921F6D6C300A612A4 /* ASCIICType.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ASCIICType.cpp; sourceTree = "<group>"; };
 		4468567225094FE8008CCA05 /* ThreadSanitizerSupport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ThreadSanitizerSupport.h; sourceTree = "<group>"; };
+		46209A27266D543A007F8F4A /* CancellableTask.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CancellableTask.h; sourceTree = "<group>"; };
 		46BA9EAB1F4CD61E009A2BBC /* CompletionHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CompletionHandler.h; sourceTree = "<group>"; };
 		46BEB6E922FFDDD500269867 /* RefCounted.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = RefCounted.cpp; sourceTree = "<group>"; };
 		50DE35F3215BB01500B979C7 /* ExternalStringImpl.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ExternalStringImpl.cpp; sourceTree = "<group>"; };
@@ -653,8 +654,8 @@
 		A8A47322151A825B004123FF /* CString.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CString.h; sourceTree = "<group>"; };
 		A8A47323151A825B004123FF /* StringBuffer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringBuffer.h; sourceTree = "<group>"; };
 		A8A47324151A825B004123FF /* StringBuilder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StringBuilder.cpp; sourceTree = "<group>"; };
+		A8A47325151A825B004123EE /* StringBuilderInternals.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringBuilderInternals.h; sourceTree = "<group>"; };
 		A8A47325151A825B004123FF /* StringBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringBuilder.h; sourceTree = "<group>"; };
-		A8A47325151A825B004123EE /* StringBuilderInternals.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringBuilderInternals.h; sourceTree = "<group>"; };
 		A8A47326151A825B004123FF /* StringConcatenate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringConcatenate.h; sourceTree = "<group>"; };
 		A8A47327151A825B004123FF /* StringHash.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringHash.h; sourceTree = "<group>"; };
 		A8A47328151A825B004123FF /* StringImpl.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StringImpl.cpp; sourceTree = "<group>"; };
@@ -983,6 +984,7 @@
 				0FEC3C4F1F323C6800F59B6C /* CagedPtr.h */,
 				0F5F3D681F3FEBA600B115A2 /* CagedUniquePtr.h */,
 				413FE8F51F8D2EAB00F6D7D7 /* CallbackAggregator.h */,
+				46209A27266D543A007F8F4A /* CancellableTask.h */,
 				A8A4726A151A825A004123FF /* CheckedArithmetic.h */,
 				A8A4726B151A825A004123FF /* CheckedBoolean.h */,
 				9BB91F512648EA4D00A56217 /* CheckedPtr.h */,

Modified: trunk/Source/WTF/wtf/CMakeLists.txt (278542 => 278543)


--- trunk/Source/WTF/wtf/CMakeLists.txt	2021-06-07 04:11:09 UTC (rev 278542)
+++ trunk/Source/WTF/wtf/CMakeLists.txt	2021-06-07 04:22:38 UTC (rev 278543)
@@ -26,6 +26,7 @@
     CagedPtr.h
     CagedUniquePtr.h
     CallbackAggregator.h
+    CancellableTask.h
     CheckedArithmetic.h
     CheckedBoolean.h
     CheckedPtr.h

Added: trunk/Source/WTF/wtf/CancellableTask.h (0 => 278543)


--- trunk/Source/WTF/wtf/CancellableTask.h	                        (rev 0)
+++ trunk/Source/WTF/wtf/CancellableTask.h	2021-06-07 04:22:38 UTC (rev 278543)
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 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
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <wtf/Function.h>
+#include <wtf/UniqueRef.h>
+#include <wtf/WeakPtr.h>
+
+namespace WTF {
+
+namespace Detail {
+struct CancellableTaskImpl : public CanMakeWeakPtr<CancellableTaskImpl> {
+    WTF_MAKE_STRUCT_FAST_ALLOCATED;
+    explicit CancellableTaskImpl(Function<void()>&& task) : task(WTFMove(task)) { }
+    Function<void()> task;
+};
+}
+
+class CancellableTask {
+public:
+    explicit CancellableTask(Function<void()>&&);
+
+    void operator()();
+    bool isPending() const { return !!m_taskWrapper->task; }
+    void cancel() { m_taskWrapper->task = nullptr; }
+
+    class Handle {
+    public:
+        Handle() = default;
+        bool isPending() const { return m_taskWrapper && m_taskWrapper->task; }
+        void cancel();
+    private:
+        friend class CancellableTask;
+        explicit Handle(Detail::CancellableTaskImpl&);
+        WeakPtr<Detail::CancellableTaskImpl> m_taskWrapper;
+    };
+
+    Handle createHandle() { return Handle { m_taskWrapper.get() }; }
+
+private:
+    UniqueRef<Detail::CancellableTaskImpl> m_taskWrapper;
+};
+
+inline CancellableTask::CancellableTask(Function<void()>&& task)
+    : m_taskWrapper(makeUniqueRef<Detail::CancellableTaskImpl>(WTFMove(task)))
+{ }
+
+inline void CancellableTask::operator()()
+{
+    if (auto task = std::exchange(m_taskWrapper->task, nullptr))
+        task();
+}
+
+inline CancellableTask::Handle::Handle(Detail::CancellableTaskImpl& task)
+    : m_taskWrapper(makeWeakPtr(task))
+{ }
+
+inline void CancellableTask::Handle::cancel()
+{
+    if (m_taskWrapper)
+        m_taskWrapper->task = nullptr;
+}
+
+} // namespace WTF
+
+using WTF::CancellableTask;

Modified: trunk/Source/WebCore/ChangeLog (278542 => 278543)


--- trunk/Source/WebCore/ChangeLog	2021-06-07 04:11:09 UTC (rev 278542)
+++ trunk/Source/WebCore/ChangeLog	2021-06-07 04:22:38 UTC (rev 278543)
@@ -1,3 +1,41 @@
+2021-06-06  Chris Dumez  <[email protected]>
+
+        Stop using legacy EventLoopDeferrableTask
+        https://bugs.webkit.org/show_bug.cgi?id=226700
+
+        Reviewed by Darin Adler.
+
+        Stop using legacy EventLoopDeferrableTask and drop the class entirely. Call sites are
+        now using the HTML event loop directly and using WTF::CancellableTask to wrap the task
+        they schedule. This achieves the same result as EventLoopDeferrableTask but is more
+        lightweight and flexible.
+
+        * animation/DocumentTimelinesController.cpp:
+        (WebCore::DocumentTimelinesController::DocumentTimelinesController):
+        (WebCore::DocumentTimelinesController::detachFromDocument):
+        (WebCore::DocumentTimelinesController::cacheCurrentTime):
+        * animation/DocumentTimelinesController.h:
+        * dom/ActiveDOMObject.h:
+        * html/HTMLMediaElement.cpp:
+        (WebCore::HTMLMediaElement::HTMLMediaElement):
+        (WebCore::HTMLMediaElement::~HTMLMediaElement):
+        (WebCore::HTMLMediaElement::scheduleCheckPlaybackTargetCompatability):
+        (WebCore::HTMLMediaElement::seekWithTolerance):
+        (WebCore::HTMLMediaElement::setVolume):
+        (WebCore::HTMLMediaElement::scheduleConfigureTextTracks):
+        (WebCore::HTMLMediaElement::scheduleMediaEngineWasUpdated):
+        (WebCore::HTMLMediaElement::scheduleUpdatePlayState):
+        (WebCore::HTMLMediaElement::cancelPendingTasks):
+        (WebCore::HTMLMediaElement::closeTaskQueues):
+        (WebCore::HTMLMediaElement::suspend):
+        (WebCore::HTMLMediaElement::resume):
+        (WebCore::HTMLMediaElement::markCaptionAndSubtitleTracksAsUnconfigured):
+        (WebCore::HTMLMediaElement::mediaPlayerBufferedTimeRangesChanged):
+        (WebCore::HTMLMediaElement::scheduleUpdateMediaState):
+        (WebCore::HTMLMediaElement::playbackControlsManagerBehaviorRestrictionsTimerFired):
+        * html/HTMLMediaElement.h:
+        * platform/DeferrableTask.h:
+
 2021-06-06  Wenson Hsieh  <[email protected]>
 
         Turn TextIteratorBehaviorFlag into an enum class and wrap it in OptionSet

Modified: trunk/Source/WebCore/animation/DocumentTimelinesController.cpp (278542 => 278543)


--- trunk/Source/WebCore/animation/DocumentTimelinesController.cpp	2021-06-07 04:11:09 UTC (rev 278542)
+++ trunk/Source/WebCore/animation/DocumentTimelinesController.cpp	2021-06-07 04:22:38 UTC (rev 278543)
@@ -43,8 +43,7 @@
 namespace WebCore {
 
 DocumentTimelinesController::DocumentTimelinesController(Document& document)
-    : m_currentTimeClearingTask(&document)
-    , m_document(document)
+    : m_document(document)
 {
     if (auto* page = document.page()) {
         if (page->settings().hiddenPageCSSAnimationSuspensionEnabled() && !page->isVisible())
@@ -54,6 +53,7 @@
 
 DocumentTimelinesController::~DocumentTimelinesController()
 {
+    m_currentTimeClearingTask.cancel();
 }
 
 void DocumentTimelinesController::addTimeline(DocumentTimeline& timeline)
@@ -73,7 +73,7 @@
 
 void DocumentTimelinesController::detachFromDocument()
 {
-    m_currentTimeClearingTask.close();
+    m_currentTimeClearingTask.cancel();
 
     while (!m_timelines.computesEmpty())
         m_timelines.begin()->detachFromDocument();
@@ -233,8 +233,11 @@
     // animations, so we schedule the invalidation task and register a whenIdle callback on the VM, which will
     // fire syncronously if no JS is running.
     m_waitingOnVMIdle = true;
-    if (!m_currentTimeClearingTask.isPending())
-        m_currentTimeClearingTask.scheduleTask(std::bind(&DocumentTimelinesController::maybeClearCachedCurrentTime, this));
+    if (!m_currentTimeClearingTask.isPending()) {
+        CancellableTask task(std::bind(&DocumentTimelinesController::maybeClearCachedCurrentTime, this));
+        m_currentTimeClearingTask = task.createHandle();
+        m_document.eventLoop().queueTask(TaskSource::InternalAsyncTask, WTFMove(task));
+    }
     // We extent the associated Document's lifecycle until the VM became idle since the DocumentTimelinesController
     // is owned by the Document.
     m_document.vm().whenIdle([this, protectedDocument = makeRefPtr(m_document)]() {

Modified: trunk/Source/WebCore/animation/DocumentTimelinesController.h (278542 => 278543)


--- trunk/Source/WebCore/animation/DocumentTimelinesController.h	2021-06-07 04:11:09 UTC (rev 278542)
+++ trunk/Source/WebCore/animation/DocumentTimelinesController.h	2021-06-07 04:22:38 UTC (rev 278543)
@@ -25,9 +25,9 @@
 
 #pragma once
 
-#include "DeferrableTask.h"
 #include "ReducedResolutionSeconds.h"
 #include "Timer.h"
+#include <wtf/CancellableTask.h>
 #include <wtf/Markable.h>
 #include <wtf/Seconds.h>
 #include <wtf/WeakHashSet.h>
@@ -67,7 +67,7 @@
     void maybeClearCachedCurrentTime();
 
     WeakHashSet<DocumentTimeline> m_timelines;
-    EventLoopDeferrableTask m_currentTimeClearingTask;
+    CancellableTask::Handle m_currentTimeClearingTask;
     Document& m_document;
     Markable<Seconds, Seconds::MarkableTraits> m_cachedCurrentTime;
     bool m_isSuspended { false };

Modified: trunk/Source/WebCore/dom/ActiveDOMObject.h (278542 => 278543)


--- trunk/Source/WebCore/dom/ActiveDOMObject.h	2021-06-07 04:11:09 UTC (rev 278542)
+++ trunk/Source/WebCore/dom/ActiveDOMObject.h	2021-06-07 04:22:38 UTC (rev 278543)
@@ -29,6 +29,7 @@
 #include "ContextDestructionObserver.h"
 #include "TaskSource.h"
 #include <wtf/Assertions.h>
+#include <wtf/CancellableTask.h>
 #include <wtf/Forward.h>
 #include <wtf/Function.h>
 #include <wtf/RefCounted.h>
@@ -110,6 +111,17 @@
         });
     }
 
+    template<typename T>
+    static CancellableTask::Handle queueCancellableTaskKeepingObjectAlive(T& object, TaskSource source, Function<void()>&& task)
+    {
+        CancellableTask cancellableTask(WTFMove(task));
+        auto taskHandle = cancellableTask.createHandle();
+        object.queueTaskInEventLoop(source, [protectedObject = makeRef(object), activity = object.ActiveDOMObject::makePendingActivity(object), cancellableTask = WTFMove(cancellableTask)]() mutable {
+            cancellableTask();
+        });
+        return taskHandle;
+    }
+
     template<typename EventTargetType, typename EventType>
     static void queueTaskToDispatchEvent(EventTargetType& target, TaskSource source, Ref<EventType>&& event)
     {

Modified: trunk/Source/WebCore/html/HTMLMediaElement.cpp (278542 => 278543)


--- trunk/Source/WebCore/html/HTMLMediaElement.cpp	2021-06-07 04:11:09 UTC (rev 278542)
+++ trunk/Source/WebCore/html/HTMLMediaElement.cpp	2021-06-07 04:22:38 UTC (rev 278543)
@@ -394,20 +394,8 @@
     , m_scanTimer(*this, &HTMLMediaElement::scanTimerFired)
     , m_playbackControlsManagerBehaviorRestrictionsTimer(*this, &HTMLMediaElement::playbackControlsManagerBehaviorRestrictionsTimerFired)
     , m_seekToPlaybackPositionEndedTimer(*this, &HTMLMediaElement::seekToPlaybackPositionEndedTimerFired)
-    , m_configureTextTracksTask(&document)
-    , m_checkPlaybackTargetCompatibilityTask(&document)
-    , m_updateMediaStateTask(&document)
-    , m_mediaEngineUpdatedTask(&document)
-    , m_updatePlayStateTask(&document)
-    , m_resumeTask(&document)
-    , m_seekTask(&document)
-    , m_playbackControlsManagerBehaviorRestrictionsTask(&document)
-    , m_bufferedTimeRangesChangedTask(&document)
     , m_resourceSelectionTaskQueue(&document)
     , m_asyncEventQueue(EventLoopEventQueue::create(*this))
-#if PLATFORM(IOS_FAMILY)
-    , m_volumeRevertTask(&document)
-#endif
     , m_lastTimeUpdateEventMovieTime(MediaTime::positiveInfiniteTime())
     , m_firstTimePlaying(true)
     , m_playing(false)
@@ -579,9 +567,9 @@
     if (m_isolatedWorld)
         m_isolatedWorld->clearWrappers();
 
-    m_seekTask.close();
-    m_resumeTask.close();
-    m_playbackControlsManagerBehaviorRestrictionsTask.close();
+    m_seekTask.cancel();
+    m_resumeTask.cancel();
+    m_playbackControlsManagerBehaviorRestrictionsTask.cancel();
     m_resourceSelectionTaskQueue.close();
 
     m_completelyLoaded = true;
@@ -996,7 +984,7 @@
         return;
 
     ALWAYS_LOG(LOGIDENTIFIER);
-    m_checkPlaybackTargetCompatibilityTask.scheduleTask([this] {
+    m_checkPlaybackTargetCompatibilityTask = queueCancellableTaskKeepingObjectAlive(*this, TaskSource::MediaElement, [this] {
         checkPlaybackTargetCompatibility();
     });
 }
@@ -2975,7 +2963,7 @@
     // it is running to complete.
     if (m_seekTask.isPending()) {
         INFO_LOG(LOGIDENTIFIER, "cancelling pending seeks");
-        m_seekTask.cancelTask();
+        m_seekTask.cancel();
         if (m_pendingSeek) {
             now = m_pendingSeek->now;
             m_pendingSeek = nullptr;
@@ -2997,7 +2985,7 @@
     m_pendingSeek = makeUnique<PendingSeek>(now, time, negativeTolerance, positiveTolerance);
     if (fromDOM) {
         INFO_LOG(LOGIDENTIFIER, "enqueuing seek from ", now, " to ", time);
-        m_seekTask.scheduleTask(std::bind(&HTMLMediaElement::seekTask, this));
+        m_seekTask = queueCancellableTaskKeepingObjectAlive(*this, TaskSource::MediaElement, std::bind(&HTMLMediaElement::seekTask, this));
     } else
         seekTask();
 
@@ -3712,7 +3700,7 @@
     if (m_volumeRevertTask.isPending())
         return { };
 
-    m_volumeRevertTask.scheduleTask([this, oldVolume] {
+    m_volumeRevertTask = queueCancellableTaskKeepingObjectAlive(*this, TaskSource::MediaElement, [this, oldVolume] {
         m_volume = oldVolume;
     });
 
@@ -4502,7 +4490,7 @@
 
     auto logSiteIdentifier = LOGIDENTIFIER;
     ALWAYS_LOG(logSiteIdentifier, "task scheduled");
-    m_configureTextTracksTask.scheduleTask([this, logSiteIdentifier] {
+    m_configureTextTracksTask = queueCancellableTaskKeepingObjectAlive(*this, TaskSource::MediaElement, [this, logSiteIdentifier] {
         UNUSED_PARAM(logSiteIdentifier);
         ALWAYS_LOG(logSiteIdentifier, "lambda(), task fired");
         Ref<HTMLMediaElement> protectedThis(*this); // configureTextTracks calls methods that can trigger arbitrary DOM mutations.
@@ -5050,7 +5038,7 @@
 
     auto logSiteIdentifier = LOGIDENTIFIER;
     ALWAYS_LOG(logSiteIdentifier, "task scheduled");
-    m_mediaEngineUpdatedTask.scheduleTask([this, logSiteIdentifier] {
+    m_mediaEngineUpdatedTask = queueCancellableTaskKeepingObjectAlive(*this, TaskSource::MediaElement, [this, logSiteIdentifier] {
         UNUSED_PARAM(logSiteIdentifier);
         ALWAYS_LOG(logSiteIdentifier, "lambda(), task fired");
         Ref<HTMLMediaElement> protectedThis(*this); // mediaEngineWasUpdated calls methods that can trigger arbitrary DOM mutations.
@@ -5343,7 +5331,7 @@
 
     auto logSiteIdentifier = LOGIDENTIFIER;
     ALWAYS_LOG(logSiteIdentifier, "task scheduled");
-    m_updatePlayStateTask.scheduleTask([this, logSiteIdentifier] {
+    m_updatePlayStateTask = queueCancellableTaskKeepingObjectAlive(*this, TaskSource::MediaElement, [this, logSiteIdentifier] {
         UNUSED_PARAM(logSiteIdentifier);
         ALWAYS_LOG(logSiteIdentifier, "lambda(), task fired");
         Ref<HTMLMediaElement> protectedThis(*this); // updatePlayState calls methods that can trigger arbitrary DOM mutations.
@@ -5475,13 +5463,16 @@
 
 void HTMLMediaElement::cancelPendingTasks()
 {
-    m_configureTextTracksTask.cancelTask();
-    m_checkPlaybackTargetCompatibilityTask.cancelTask();
-    m_updateMediaStateTask.cancelTask();
-    m_mediaEngineUpdatedTask.cancelTask();
-    m_updatePlayStateTask.cancelTask();
+    m_configureTextTracksTask.cancel();
+    m_checkPlaybackTargetCompatibilityTask.cancel();
+    m_updateMediaStateTask.cancel();
+    m_mediaEngineUpdatedTask.cancel();
+    m_updatePlayStateTask.cancel();
+    m_resumeTask.cancel();
+    m_seekTask.cancel();
+    m_playbackControlsManagerBehaviorRestrictionsTask.cancel();
 #if PLATFORM(IOS_FAMILY)
-    m_volumeRevertTask.cancelTask();
+    m_volumeRevertTask.cancel();
 #endif
 }
 
@@ -5637,21 +5628,9 @@
 
 void HTMLMediaElement::closeTaskQueues()
 {
-    m_configureTextTracksTask.close();
-    m_checkPlaybackTargetCompatibilityTask.close();
-    m_updateMediaStateTask.close();
-    m_mediaEngineUpdatedTask.close();
-    m_updatePlayStateTask.close();
-    m_resumeTask.close();
-    m_seekTask.close();
-    m_playbackControlsManagerBehaviorRestrictionsTask.close();
-    m_seekTask.close();
-    m_resumeTask.close();
+    cancelPendingTasks();
     m_resourceSelectionTaskQueue.close();
     m_asyncEventQueue->close();
-#if PLATFORM(IOS_FAMILY)
-    m_volumeRevertTask.close();
-#endif
 }
 
 void HTMLMediaElement::contextDestroyed()
@@ -5685,7 +5664,7 @@
     ALWAYS_LOG(LOGIDENTIFIER);
     Ref<HTMLMediaElement> protectedThis(*this);
 
-    m_resumeTask.cancelTask();
+    m_resumeTask.cancel();
 
     switch (reason) {
     case ReasonForSuspension::BackForwardCache:
@@ -5724,7 +5703,7 @@
         //  MEDIA_ERR_ABORTED while the abortEvent is being sent, but cleared immediately afterwards).
         // This behavior is not specified but it seems like a sensible thing to do.
         // As it is not safe to immedately start loading now, let's schedule a load.
-        m_resumeTask.scheduleTask(std::bind(&HTMLMediaElement::prepareForLoad, this));
+        m_resumeTask = queueCancellableTaskKeepingObjectAlive(*this, TaskSource::MediaElement, std::bind(&HTMLMediaElement::prepareForLoad, this));
     }
 
     updateRenderer();
@@ -6636,7 +6615,7 @@
     }
 
     m_processingPreferenceChange = true;
-    m_configureTextTracksTask.cancelTask();
+    m_configureTextTracksTask.cancel();
     if (mode == Immediately) {
         Ref<HTMLMediaElement> protectedThis(*this); // configureTextTracks calls methods that can trigger arbitrary DOM mutations.
         configureTextTracks();
@@ -7157,7 +7136,7 @@
 
     auto logSiteIdentifier = LOGIDENTIFIER;
     ALWAYS_LOG(logSiteIdentifier, "task scheduled");
-    m_bufferedTimeRangesChangedTask.scheduleTask([this, logSiteIdentifier] {
+    m_bufferedTimeRangesChangedTask = queueCancellableTaskKeepingObjectAlive(*this, TaskSource::MediaElement, [this, logSiteIdentifier] {
         UNUSED_PARAM(logSiteIdentifier);
         ALWAYS_LOG(logSiteIdentifier, "lambda(), task fired");
         if (!m_player || !m_textTracks)
@@ -7788,7 +7767,7 @@
 
     auto logSiteIdentifier = LOGIDENTIFIER;
     ALWAYS_LOG(logSiteIdentifier, "task scheduled");
-    m_updateMediaStateTask.scheduleTask([this, logSiteIdentifier] {
+    m_updateMediaStateTask = queueCancellableTaskKeepingObjectAlive(*this, TaskSource::MediaElement, [this, logSiteIdentifier] {
         UNUSED_PARAM(logSiteIdentifier);
         ALWAYS_LOG(logSiteIdentifier, "lambda(), task fired");
         Ref<HTMLMediaElement> protectedThis(*this); // updateMediaState calls methods that can trigger arbitrary DOM mutations.
@@ -8062,14 +8041,13 @@
     if (!mediaSession().hasBehaviorRestriction(MediaElementSession::RequireUserGestureToControlControlsManager))
         return;
 
-    RefPtr<HTMLMediaElement> protectedThis(this);
-    m_playbackControlsManagerBehaviorRestrictionsTask.scheduleTask([protectedThis] () {
-        auto& mediaElementSession = protectedThis->mediaSession();
-        if (protectedThis->isPlaying() || mediaElementSession.state() == PlatformMediaSession::Autoplaying || mediaElementSession.state() == PlatformMediaSession::Playing)
+    m_playbackControlsManagerBehaviorRestrictionsTask = queueCancellableTaskKeepingObjectAlive(*this, TaskSource::MediaElement, [this] () {
+        auto& mediaElementSession = mediaSession();
+        if (isPlaying() || mediaElementSession.state() == PlatformMediaSession::Autoplaying || mediaElementSession.state() == PlatformMediaSession::Playing)
             return;
 
         mediaElementSession.addBehaviorRestriction(MediaElementSession::RequirePlaybackToControlControlsManager);
-        protectedThis->schedulePlaybackControlsManagerUpdate();
+        schedulePlaybackControlsManagerUpdate();
     });
 }
 

Modified: trunk/Source/WebCore/html/HTMLMediaElement.h (278542 => 278543)


--- trunk/Source/WebCore/html/HTMLMediaElement.h	2021-06-07 04:11:09 UTC (rev 278542)
+++ trunk/Source/WebCore/html/HTMLMediaElement.h	2021-06-07 04:22:38 UTC (rev 278543)
@@ -31,7 +31,6 @@
 #include "AudioTrack.h"
 #include "AutoplayEvent.h"
 #include "CaptionUserPreferences.h"
-#include "DeferrableTask.h"
 #include "EventLoopEventQueue.h"
 #include "HTMLElement.h"
 #include "HTMLMediaElementEnums.h"
@@ -941,20 +940,20 @@
     Timer m_scanTimer;
     Timer m_playbackControlsManagerBehaviorRestrictionsTimer;
     Timer m_seekToPlaybackPositionEndedTimer;
-    EventLoopDeferrableTask m_configureTextTracksTask;
-    EventLoopDeferrableTask m_checkPlaybackTargetCompatibilityTask;
-    EventLoopDeferrableTask m_updateMediaStateTask;
-    EventLoopDeferrableTask m_mediaEngineUpdatedTask;
-    EventLoopDeferrableTask m_updatePlayStateTask;
-    EventLoopDeferrableTask m_resumeTask;
-    EventLoopDeferrableTask m_seekTask;
-    EventLoopDeferrableTask m_playbackControlsManagerBehaviorRestrictionsTask;
-    EventLoopDeferrableTask m_bufferedTimeRangesChangedTask;
+    CancellableTask::Handle m_configureTextTracksTask;
+    CancellableTask::Handle m_checkPlaybackTargetCompatibilityTask;
+    CancellableTask::Handle m_updateMediaStateTask;
+    CancellableTask::Handle m_mediaEngineUpdatedTask;
+    CancellableTask::Handle m_updatePlayStateTask;
+    CancellableTask::Handle m_resumeTask;
+    CancellableTask::Handle m_seekTask;
+    CancellableTask::Handle m_playbackControlsManagerBehaviorRestrictionsTask;
+    CancellableTask::Handle m_bufferedTimeRangesChangedTask;
     EventLoopTaskQueue m_resourceSelectionTaskQueue;
     RefPtr<TimeRanges> m_playedTimeRanges;
     UniqueRef<EventLoopEventQueue> m_asyncEventQueue;
 #if PLATFORM(IOS_FAMILY)
-    EventLoopDeferrableTask m_volumeRevertTask;
+    CancellableTask::Handle m_volumeRevertTask;
 #endif
 
     PlayPromiseVector m_pendingPlayPromises;

Modified: trunk/Source/WebCore/platform/DeferrableTask.h (278542 => 278543)


--- trunk/Source/WebCore/platform/DeferrableTask.h	2021-06-07 04:11:09 UTC (rev 278542)
+++ trunk/Source/WebCore/platform/DeferrableTask.h	2021-06-07 04:22:38 UTC (rev 278543)
@@ -25,15 +25,14 @@
 
 #pragma once
 
-#include "ContextDestructionObserver.h"
-#include "EventLoop.h"
 #include "GenericTaskQueue.h"
-#include "ScriptExecutionContext.h"
 
 namespace WebCore {
 
-class DeferrableTaskBase : public CanMakeWeakPtr<DeferrableTaskBase> {
+class MainThreadDeferrableTask : public CanMakeWeakPtr<MainThreadDeferrableTask> {
 public:
+    MainThreadDeferrableTask() = default;
+
     void close()
     {
         cancelTask();
@@ -48,35 +47,19 @@
 
     bool isPending() const { return m_isPending; }
 
-protected:
-    ~DeferrableTaskBase() = default;
-    bool isClosed() const { return m_isClosed; }
-    void setIsPending(bool isPending) { m_isPending = isPending; }
-
-private:
-    bool m_isPending { false };
-    bool m_isClosed { false };
-};
-
-class MainThreadDeferrableTask : public DeferrableTaskBase {
-public:
-    MainThreadDeferrableTask()
-        : m_dispatcher()
-    { }
-
     void scheduleTask(Function<void()>&& task)
     {
-        if (isClosed())
+        if (m_isClosed)
             return;
 
         cancelTask();
 
-        setIsPending(true);
+        m_isPending = true;
         m_dispatcher.postTask([weakThis = makeWeakPtr(*this), task = WTFMove(task)] {
             if (!weakThis)
                 return;
             ASSERT(weakThis->isPending());
-            weakThis->setIsPending(false);
+            weakThis->m_isPending = false;
             task();
         });
     }
@@ -83,32 +66,8 @@
 
 private:
     MainThreadTaskDispatcher m_dispatcher;
+    bool m_isPending { false };
+    bool m_isClosed { false };
 };
 
-// Similar to DeferrableTask but based on the HTML event loop.
-class EventLoopDeferrableTask : public DeferrableTaskBase, private ContextDestructionObserver {
-public:
-    EventLoopDeferrableTask(ScriptExecutionContext* context)
-        : ContextDestructionObserver(context)
-    { }
-
-    // FIXME: Pass TaskSource instead of assuming TaskSource::MediaElement.
-    void scheduleTask(Function<void()>&& task)
-    {
-        if (isClosed() || !scriptExecutionContext())
-            return;
-
-        cancelTask();
-
-        setIsPending(true);
-        scriptExecutionContext()->eventLoop().queueTask(TaskSource::MediaElement, [weakThis = makeWeakPtr(*this), task = WTFMove(task)] {
-            if (!weakThis)
-                return;
-            ASSERT(weakThis->isPending());
-            weakThis->setIsPending(false);
-            task();
-        });
-    }
-};
-
-}
+} // namespace WebCore
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to