Changeset 251975 in webkit


Ignore:
Timestamp:
Nov 3, 2019 12:01:55 PM (4 years ago)
Author:
rniwa@webkit.org
Message:

Add a helper function to schedule a task to dispatch an event to ActiveDOMObject
https://bugs.webkit.org/show_bug.cgi?id=203714

Reviewed by Chris Dumez.

Added two helper static member functions queueTaskKeepingObjectAlive and queueTaskToDispatchEvent
to ActiveDOMObject and deployed them in ServiceWorkerContainer.

No new tests since there should be no behavioral changes.

  • Headers.cmake:
  • WebCore.xcodeproj/project.pbxproj:
  • dom/AbstractEventLoop.h:
  • dom/ActiveDOMObject.cpp:

(WebCore::ActiveDOMObject::queueTaskInEventLoop): Added.

  • dom/ActiveDOMObject.h:

(WebCore::ActiveDOMObject::queueTaskKeepingObjectAlive): Added.
(WebCore::ActiveDOMObject::queueTaskToDispatchEvent): Added.

  • dom/TaskSource.h: Added.
  • workers/service/ServiceWorkerContainer.cpp:

(WebCore::ServiceWorkerContainer::ready):
(WebCore::ServiceWorkerContainer::getRegistration):
(WebCore::ServiceWorkerContainer::getRegistrations):
(WebCore::ServiceWorkerContainer::jobFailedWithException):
(WebCore::ServiceWorkerContainer::jobResolvedWithRegistration):
(WebCore::ServiceWorkerContainer::jobResolvedWithUnregistrationResult):
(WebCore::ServiceWorkerContainer::jobFailedLoadingScript):
(WebCore::ServiceWorkerContainer::fireControllerChangeEvent):
(WebCore::ServiceWorkerContainer::enqueueTask): Deleted.

  • workers/service/ServiceWorkerContainer.h:
Location:
trunk/Source/WebCore
Files:
8 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r251974 r251975  
     12019-11-03  Ryosuke Niwa  <rniwa@webkit.org>
     2
     3        Add a helper function to schedule a task to dispatch an event to ActiveDOMObject
     4        https://bugs.webkit.org/show_bug.cgi?id=203714
     5
     6        Reviewed by Chris Dumez.
     7
     8        Added two helper static member functions queueTaskKeepingObjectAlive and queueTaskToDispatchEvent
     9        to ActiveDOMObject and deployed them in ServiceWorkerContainer.
     10
     11        No new tests since there should be no behavioral changes.
     12
     13        * Headers.cmake:
     14        * WebCore.xcodeproj/project.pbxproj:
     15        * dom/AbstractEventLoop.h:
     16        * dom/ActiveDOMObject.cpp:
     17        (WebCore::ActiveDOMObject::queueTaskInEventLoop): Added.
     18        * dom/ActiveDOMObject.h:
     19        (WebCore::ActiveDOMObject::queueTaskKeepingObjectAlive): Added.
     20        (WebCore::ActiveDOMObject::queueTaskToDispatchEvent): Added.
     21        * dom/TaskSource.h: Added.
     22        * workers/service/ServiceWorkerContainer.cpp:
     23        (WebCore::ServiceWorkerContainer::ready):
     24        (WebCore::ServiceWorkerContainer::getRegistration):
     25        (WebCore::ServiceWorkerContainer::getRegistrations):
     26        (WebCore::ServiceWorkerContainer::jobFailedWithException):
     27        (WebCore::ServiceWorkerContainer::jobResolvedWithRegistration):
     28        (WebCore::ServiceWorkerContainer::jobResolvedWithUnregistrationResult):
     29        (WebCore::ServiceWorkerContainer::jobFailedLoadingScript):
     30        (WebCore::ServiceWorkerContainer::fireControllerChangeEvent):
     31        (WebCore::ServiceWorkerContainer::enqueueTask): Deleted.
     32        * workers/service/ServiceWorkerContainer.h:
     33
    1342019-11-03  Andres Gonzalez  <andresg_22@apple.com>
    235
  • trunk/Source/WebCore/Headers.cmake

    r251720 r251975  
    452452    dom/StyledElement.h
    453453    dom/SuccessOr.h
     454    dom/TaskSource.h
    454455    dom/Text.h
    455456    dom/TextEvent.h
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r251959 r251975  
    28562856                9AB1F38018E2489A00534743 /* CSSToLengthConversionData.h in Headers */ = {isa = PBXBuildFile; fileRef = 9AB1F37E18E2489A00534743 /* CSSToLengthConversionData.h */; settings = {ATTRIBUTES = (Private, ); }; };
    28572857                9B02E0C8235EAD2A004044B2 /* TextManipulationController.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B02E0C3235E76AA004044B2 /* TextManipulationController.h */; settings = {ATTRIBUTES = (Private, ); }; };
     2858                9B0ABCAE236BB43100B45085 /* TaskSource.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B0ABCAC236BB40A00B45085 /* TaskSource.h */; settings = {ATTRIBUTES = (Private, ); }; };
    28582859                9B24DE8E15194B9500C59C27 /* HTMLBDIElement.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B24DE8C15194B9500C59C27 /* HTMLBDIElement.h */; };
    28592860                9B27FC60234D9ADB00394A46 /* WindowEventLoop.h in Headers */ = {isa = PBXBuildFile; fileRef = 9B27FC5E234D9ADA00394A46 /* WindowEventLoop.h */; };
     
    1111211113                9B0ABCA323679ACF00B45085 /* WorkerEventLoop.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WorkerEventLoop.cpp; sourceTree = "<group>"; };
    1111311114                9B0ABCA423679ACF00B45085 /* WorkerEventLoop.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WorkerEventLoop.h; sourceTree = "<group>"; };
     11115                9B0ABCAC236BB40A00B45085 /* TaskSource.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TaskSource.h; sourceTree = "<group>"; };
    1111411116                9B0FE8731D9E02DF004A8ACB /* DocumentOrShadowRoot.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = DocumentOrShadowRoot.idl; sourceTree = "<group>"; };
    1111511117                9B13257B1F3D2ABA00DAAB69 /* DataTransferItemList.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DataTransferItemList.cpp; sourceTree = "<group>"; };
     
    2817828180                                463EB6201B8789CB0096ED51 /* TagCollection.cpp */,
    2817928181                                463EB6211B8789CB0096ED51 /* TagCollection.h */,
     28182                                9B0ABCAC236BB40A00B45085 /* TaskSource.h */,
    2818028183                                0F26A7A72054C2270090A141 /* TemplateContentDocumentFragment.cpp */,
    2818128184                                C65046A8167BFB5500CC2A4D /* TemplateContentDocumentFragment.h */,
     
    3241932422                                3BB6B81122A7D313003A2A69 /* TabSize.h in Headers */,
    3242032423                                463EB6231B8789E00096ED51 /* TagCollection.h in Headers */,
     32424                                9B0ABCAE236BB43100B45085 /* TaskSource.h in Headers */,
    3242132425                                F55B3DD61251F12D003EF269 /* TelephoneInputType.h in Headers */,
    3242232426                                7CC564B818BABEA6001B9652 /* TelephoneNumberDetector.h in Headers */,
  • trunk/Source/WebCore/dom/AbstractEventLoop.h

    r251953 r251975  
    2626#pragma once
    2727
     28#include "TaskSource.h"
    2829#include <wtf/Function.h>
    2930#include <wtf/RefCounted.h>
     
    3233
    3334class ScriptExecutionContext;
    34 
    35 enum class TaskSource : uint8_t {
    36     DOMManipulation,
    37     FileReading,
    38     IdleTask,
    39     Networking,
    40     UserInteraction
    41 };
    4235
    4336// https://html.spec.whatwg.org/multipage/webappapis.html#event-loop
  • trunk/Source/WebCore/dom/ActiveDOMObject.cpp

    r251327 r251975  
    2828#include "ActiveDOMObject.h"
    2929
     30#include "AbstractEventLoop.h"
    3031#include "Document.h"
    3132#include "ScriptExecutionContext.h"
     
    131132}
    132133
     134void ActiveDOMObject::queueTaskInEventLoop(TaskSource source, Function<void ()>&& task)
     135{
     136    auto* context = scriptExecutionContext();
     137    if (!context)
     138        return;
     139    context->eventLoop().queueTask(source, *context, WTFMove(task));
     140}
     141
    133142} // namespace WebCore
  • trunk/Source/WebCore/dom/ActiveDOMObject.h

    r251327 r251975  
    2828
    2929#include "ContextDestructionObserver.h"
     30#include "TaskSource.h"
    3031#include <wtf/Assertions.h>
    3132#include <wtf/Forward.h>
     33#include <wtf/Function.h>
    3234#include <wtf/RefCounted.h>
    3335#include <wtf/Threading.h>
     
    118120    virtual ~ActiveDOMObject();
    119121
     122    template<typename T>
     123    static void queueTaskKeepingObjectAlive(T& object, TaskSource source, Function<void ()>&& task)
     124    {
     125        object.queueTaskInEventLoop(source, [protectedObject = makeRef(object), activity = object.makePendingActivity(object), task = WTFMove(task)] () {
     126            task();
     127        });
     128    }
     129
     130    template<typename EventTargetType, typename EventType>
     131    static void queueTaskToDispatchEvent(EventTargetType& target, TaskSource source, Ref<EventType>&& event)
     132    {
     133        ASSERT(!event->target() || &target == event->target());
     134        queueTaskKeepingObjectAlive(target, source, [&target, event = WTFMove(event)] () mutable {
     135            target.dispatchEvent(event.get());
     136        });
     137    }
     138
    120139private:
    121140    enum CheckedScriptExecutionContextType { CheckedScriptExecutionContext };
    122141    ActiveDOMObject(ScriptExecutionContext*, CheckedScriptExecutionContextType);
     142
     143    void queueTaskInEventLoop(TaskSource, Function<void ()>&&);
    123144
    124145    unsigned m_pendingActivityCount { 0 };
  • trunk/Source/WebCore/dom/TaskSource.h

    r251974 r251975  
    2626#pragma once
    2727
    28 #include <wtf/Function.h>
    29 #include <wtf/RefCounted.h>
    30 
    3128namespace WebCore {
    32 
    33 class ScriptExecutionContext;
    3429
    3530enum class TaskSource : uint8_t {
     
    4136};
    4237
    43 // https://html.spec.whatwg.org/multipage/webappapis.html#event-loop
    44 class AbstractEventLoop : public RefCounted<AbstractEventLoop> {
    45 public:
    46     virtual ~AbstractEventLoop() = default;
    47 
    48     typedef WTF::Function<void ()> TaskFunction;
    49     virtual void queueTask(TaskSource, ScriptExecutionContext&, TaskFunction&&) = 0;
    50 
    51 protected:
    52     AbstractEventLoop() = default;
    53 };
    54 
    5538} // namespace WebCore
  • trunk/Source/WebCore/workers/service/ServiceWorkerContainer.cpp

    r251934 r251975  
    110110        auto& context = *scriptExecutionContext();
    111111        ensureSWClientConnection().whenRegistrationReady(context.topOrigin().data(), context.url(), [this, protectedThis = makeRef(*this)](auto&& registrationData) mutable {
    112             enqueueTask([this, registrationData = WTFMove(registrationData)]() mutable {
     112            queueTaskKeepingObjectAlive(*this, TaskSource::DOMManipulation, [this, registrationData = WTFMove(registrationData)]() mutable {
    113113                auto registration = ServiceWorkerRegistration::getOrCreate(*scriptExecutionContext(), *this, WTFMove(registrationData));
    114114                m_readyPromise->resolve(WTFMove(registration));
     
    272272
    273273    ensureSWClientConnection().matchRegistration(SecurityOriginData { context.topOrigin().data() }, parsedURL, [this, protectedThis = makeRef(*this), promise = WTFMove(promise)](auto&& result) mutable {
    274         enqueueTask([this, promise = WTFMove(promise), result = WTFMove(result)]() mutable {
     274        queueTaskKeepingObjectAlive(*this, TaskSource::DOMManipulation, [this, promise = WTFMove(promise), result = WTFMove(result)]() mutable {
    275275            if (!result) {
    276276                promise->resolve();
     
    304304    auto& context = *scriptExecutionContext();
    305305    ensureSWClientConnection().getRegistrations(SecurityOriginData { context.topOrigin().data() }, context.url(), [this, protectedThis = makeRef(*this), promise = WTFMove(promise)] (auto&& registrationDatas) mutable {
    306         enqueueTask([this, promise = WTFMove(promise), registrationDatas = WTFMove(registrationDatas)]() mutable {
     306        queueTaskKeepingObjectAlive(*this, TaskSource::DOMManipulation, [this, promise = WTFMove(promise), registrationDatas = WTFMove(registrationDatas)]() mutable {
    307307            auto registrations = WTF::map(WTFMove(registrationDatas), [&](auto&& registrationData) {
    308308                return ServiceWorkerRegistration::getOrCreate(*scriptExecutionContext(), *this, WTFMove(registrationData));
     
    336336        return;
    337337
    338     enqueueTask([promise = WTFMove(promise), exception]() mutable {
     338    queueTaskKeepingObjectAlive(*this, TaskSource::DOMManipulation, [promise = WTFMove(promise), exception]() mutable {
    339339        promise->reject(exception);
    340340    });
     
    381381        return;
    382382
    383     enqueueTask([this, promise = WTFMove(promise), jobIdentifier = job.identifier(), data = WTFMove(data), shouldNotifyWhenResolved, notifyIfExitEarly = WTFMove(notifyIfExitEarly)]() mutable {
     383    queueTaskKeepingObjectAlive(*this, TaskSource::DOMManipulation, [this, promise = WTFMove(promise), jobIdentifier = job.identifier(), data = WTFMove(data), shouldNotifyWhenResolved, notifyIfExitEarly = WTFMove(notifyIfExitEarly)]() mutable {
    384384        notifyIfExitEarly.release();
    385385
     
    436436    }
    437437
    438     enqueueTask([promise = job.takePromise(), unregistrationResult]() mutable {
     438    queueTaskKeepingObjectAlive(*this, TaskSource::DOMManipulation, [promise = job.takePromise(), unregistrationResult]() mutable {
    439439        promise->resolve<IDLBoolean>(unregistrationResult);
    440440    });
     
    483483
    484484    if (auto promise = job.takePromise()) {
    485         enqueueTask([promise = WTFMove(promise), exception = WTFMove(exception)]() mutable {
     485        queueTaskKeepingObjectAlive(*this, TaskSource::DOMManipulation, [promise = WTFMove(promise), exception = WTFMove(exception)]() mutable {
    486486            promise->reject(WTFMove(exception));
    487487        });
     
    553553#endif
    554554
    555     enqueueTask([this] {
    556         dispatchEvent(Event::create(eventNames().controllerchangeEvent, Event::CanBubble::No, Event::IsCancelable::No));
    557     });
     555    queueTaskToDispatchEvent(*this, TaskSource::DOMManipulation, Event::create(eventNames().controllerchangeEvent, Event::CanBubble::No, Event::IsCancelable::No));
    558556}
    559557
     
    619617}
    620618
    621 void ServiceWorkerContainer::enqueueTask(Function<void()>&& task)
    622 {
    623     auto* context = scriptExecutionContext();
    624     if (!context)
    625         return;
    626     context->eventLoop().queueTask(TaskSource::DOMManipulation, *context, [protectedThis = makeRef(*this), pendingActivity = makePendingActivity(*this), task = WTFMove(task)] {
    627         task();
    628     });
    629 }
    630 
    631619} // namespace WebCore
    632620
  • trunk/Source/WebCore/workers/service/ServiceWorkerContainer.h

    r251934 r251975  
    9595private:
    9696    bool addEventListener(const AtomString& eventType, Ref<EventListener>&&, const AddEventListenerOptions& = { }) final;
    97     void enqueueTask(Function<void()>&&);
    9897
    9998    void scheduleJob(std::unique_ptr<ServiceWorkerJob>&&);
Note: See TracChangeset for help on using the changeset viewer.