Changeset 251985 in webkit


Ignore:
Timestamp:
Nov 4, 2019 5:31:58 AM (4 years ago)
Author:
youenn@apple.com
Message:

Order of postMessage and fetch events should be preserved when going from client to service worker
https://bugs.webkit.org/show_bug.cgi?id=203236

Reviewed by Chris Dumez.

Source/WebCore:

Now that post message is going through network process, we can make the order predictable.
Remove unnecessary task posting when firing events in service worker context and when posting message from a client.
Test: http/wpt/service-workers/service-worker/postMessage-fetch-order.https.html

  • workers/service/ServiceWorker.cpp:

(WebCore::ServiceWorker::swConnection):
(WebCore::ServiceWorker::postMessage):

  • workers/service/ServiceWorker.h:
  • workers/service/context/ServiceWorkerThread.cpp:

(WebCore::ServiceWorkerThread::postFetchTask):
(WebCore::ServiceWorkerThread::fireInstallEvent):
(WebCore::ServiceWorkerThread::fireActivateEvent):

LayoutTests:

  • http/wpt/service-workers/service-worker/postMessage-fetch-order-worker.js: Added.
  • http/wpt/service-workers/service-worker/postMessage-fetch-order.https-expected.txt: Added.
  • http/wpt/service-workers/service-worker/postMessage-fetch-order.https.html: Added.
Location:
trunk
Files:
3 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r251984 r251985  
     12019-11-04  youenn fablet  <youenn@apple.com>
     2
     3        Order of postMessage and fetch events should be preserved when going from client to service worker
     4        https://bugs.webkit.org/show_bug.cgi?id=203236
     5
     6        Reviewed by Chris Dumez.
     7
     8        * http/wpt/service-workers/service-worker/postMessage-fetch-order-worker.js: Added.
     9        * http/wpt/service-workers/service-worker/postMessage-fetch-order.https-expected.txt: Added.
     10        * http/wpt/service-workers/service-worker/postMessage-fetch-order.https.html: Added.
     11
    1122019-11-04  Oriol Brufau  <obrufau@igalia.com>
    213
  • trunk/Source/WebCore/ChangeLog

    r251977 r251985  
     12019-11-04  youenn fablet  <youenn@apple.com>
     2
     3        Order of postMessage and fetch events should be preserved when going from client to service worker
     4        https://bugs.webkit.org/show_bug.cgi?id=203236
     5
     6        Reviewed by Chris Dumez.
     7
     8        Now that post message is going through network process, we can make the order predictable.
     9        Remove unnecessary task posting when firing events in service worker context and when posting message from a client.
     10        Test: http/wpt/service-workers/service-worker/postMessage-fetch-order.https.html
     11
     12        * workers/service/ServiceWorker.cpp:
     13        (WebCore::ServiceWorker::swConnection):
     14        (WebCore::ServiceWorker::postMessage):
     15        * workers/service/ServiceWorker.h:
     16        * workers/service/context/ServiceWorkerThread.cpp:
     17        (WebCore::ServiceWorkerThread::postFetchTask):
     18        (WebCore::ServiceWorkerThread::fireInstallEvent):
     19        (WebCore::ServiceWorkerThread::fireActivateEvent):
     20
    1212019-11-03  Zalan Bujtas  <zalan@apple.com>
    222
  • trunk/Source/WebCore/workers/service/ServiceWorker.cpp

    r251244 r251985  
    4141#include "ServiceWorkerProvider.h"
    4242#include "ServiceWorkerThread.h"
     43#include "WorkerSWClientConnection.h"
    4344#include <JavaScriptCore/JSCJSValueInlines.h>
    4445#include <wtf/IsoMallocInlines.h>
     
    9495
    9596    updatePendingActivityForEventDispatch();
     97}
     98
     99SWClientConnection& ServiceWorker::swConnection()
     100{
     101    ASSERT(scriptExecutionContext());
     102    if (is<WorkerGlobalScope>(scriptExecutionContext()))
     103        return downcast<WorkerGlobalScope>(scriptExecutionContext())->swClientConnection();
     104    return ServiceWorkerProvider::singleton().serviceWorkerConnection();
    96105}
    97106
     
    122131    }
    123132
    124     MessageWithMessagePorts message = { messageData.releaseReturnValue(), portsOrException.releaseReturnValue() };
    125     callOnMainThread([destinationIdentifier = identifier(), message = WTFMove(message), sourceIdentifier = WTFMove(sourceIdentifier)]() mutable {
    126         auto& connection = ServiceWorkerProvider::singleton().serviceWorkerConnection();
    127         connection.postMessageToServiceWorker(destinationIdentifier, WTFMove(message), sourceIdentifier);
    128     });
     133    MessageWithMessagePorts message { messageData.releaseReturnValue(), portsOrException.releaseReturnValue() };
     134    swConnection().postMessageToServiceWorker(identifier(), WTFMove(message), sourceIdentifier);
    129135    return { };
    130136}
  • trunk/Source/WebCore/workers/service/ServiceWorker.h

    r251244 r251985  
    4343
    4444class Frame;
     45class SWClientConnection;
    4546
    4647class ServiceWorker final : public RefCounted<ServiceWorker>, public EventTargetWithInlineData, public ActiveDOMObject {
     
    8384    bool isAlwaysOnLoggingAllowed() const;
    8485
     86    SWClientConnection& swConnection();
     87
    8588    ServiceWorkerData m_data;
    8689    bool m_isStopped { false };
  • trunk/Source/WebCore/workers/service/context/ServiceWorkerThread.cpp

    r250104 r251985  
    9999    // FIXME: request and options come straigth from IPC so are already isolated. We should be able to take benefit of that.
    100100    runLoop().postTaskForMode([client = WTFMove(client), clientId, request = request.isolatedCopy(), referrer = referrer.isolatedCopy(), options = options.isolatedCopy()] (ScriptExecutionContext& context) mutable {
    101         context.postTask([client = WTFMove(client), clientId, request = WTFMove(request), referrer = WTFMove(referrer), options = WTFMove(options)] (ScriptExecutionContext& context) mutable {
    102             ServiceWorkerFetch::dispatchFetchEvent(WTFMove(client), downcast<ServiceWorkerGlobalScope>(context), clientId, WTFMove(request), WTFMove(referrer), WTFMove(options));
    103         });
     101        ServiceWorkerFetch::dispatchFetchEvent(WTFMove(client), downcast<ServiceWorkerGlobalScope>(context), clientId, WTFMove(request), WTFMove(referrer), WTFMove(options));
    104102    }, WorkerRunLoop::defaultMode());
    105103}
     
    141139void ServiceWorkerThread::fireInstallEvent()
    142140{
    143     ScriptExecutionContext::Task task([jobDataIdentifier = m_data.jobDataIdentifier, serviceWorkerIdentifier = this->identifier()] (ScriptExecutionContext& context) mutable {
    144         context.postTask([jobDataIdentifier, serviceWorkerIdentifier](ScriptExecutionContext& context) {
    145             auto& serviceWorkerGlobalScope = downcast<ServiceWorkerGlobalScope>(context);
    146             auto installEvent = ExtendableEvent::create(eventNames().installEvent, { }, ExtendableEvent::IsTrusted::Yes);
    147             serviceWorkerGlobalScope.dispatchEvent(installEvent);
    148 
    149             installEvent->whenAllExtendLifetimePromisesAreSettled([jobDataIdentifier, serviceWorkerIdentifier](HashSet<Ref<DOMPromise>>&& extendLifetimePromises) {
    150                 bool hasRejectedAnyPromise = false;
    151                 for (auto& promise : extendLifetimePromises) {
    152                     if (promise->status() == DOMPromise::Status::Rejected) {
    153                         hasRejectedAnyPromise = true;
    154                         break;
    155                     }
     141    runLoop().postTask([jobDataIdentifier = m_data.jobDataIdentifier, serviceWorkerIdentifier = this->identifier()] (ScriptExecutionContext& context) mutable {
     142        auto& serviceWorkerGlobalScope = downcast<ServiceWorkerGlobalScope>(context);
     143        auto installEvent = ExtendableEvent::create(eventNames().installEvent, { }, ExtendableEvent::IsTrusted::Yes);
     144        serviceWorkerGlobalScope.dispatchEvent(installEvent);
     145
     146        installEvent->whenAllExtendLifetimePromisesAreSettled([jobDataIdentifier, serviceWorkerIdentifier](HashSet<Ref<DOMPromise>>&& extendLifetimePromises) {
     147            bool hasRejectedAnyPromise = false;
     148            for (auto& promise : extendLifetimePromises) {
     149                if (promise->status() == DOMPromise::Status::Rejected) {
     150                    hasRejectedAnyPromise = true;
     151                    break;
    156152                }
    157                 callOnMainThread([jobDataIdentifier, serviceWorkerIdentifier, hasRejectedAnyPromise] () mutable {
    158                     if (auto* connection = SWContextManager::singleton().connection())
    159                         connection->didFinishInstall(jobDataIdentifier, serviceWorkerIdentifier, !hasRejectedAnyPromise);
    160                 });
     153            }
     154            callOnMainThread([jobDataIdentifier, serviceWorkerIdentifier, hasRejectedAnyPromise] () mutable {
     155                if (auto* connection = SWContextManager::singleton().connection())
     156                    connection->didFinishInstall(jobDataIdentifier, serviceWorkerIdentifier, !hasRejectedAnyPromise);
    161157            });
    162158        });
    163159    });
    164     runLoop().postTask(WTFMove(task));
    165160}
    166161
    167162void ServiceWorkerThread::fireActivateEvent()
    168163{
    169     ScriptExecutionContext::Task task([serviceWorkerIdentifier = this->identifier()] (ScriptExecutionContext& context) mutable {
    170         context.postTask([serviceWorkerIdentifier](ScriptExecutionContext& context) {
    171             auto& serviceWorkerGlobalScope = downcast<ServiceWorkerGlobalScope>(context);
    172             auto activateEvent = ExtendableEvent::create(eventNames().activateEvent, { }, ExtendableEvent::IsTrusted::Yes);
    173             serviceWorkerGlobalScope.dispatchEvent(activateEvent);
    174 
    175             activateEvent->whenAllExtendLifetimePromisesAreSettled([serviceWorkerIdentifier](HashSet<Ref<DOMPromise>>&&) {
    176                 callOnMainThread([serviceWorkerIdentifier] () mutable {
    177                     if (auto* connection = SWContextManager::singleton().connection())
    178                         connection->didFinishActivation(serviceWorkerIdentifier);
    179                 });
     164    runLoop().postTask([serviceWorkerIdentifier = this->identifier()] (ScriptExecutionContext& context) mutable {
     165        auto& serviceWorkerGlobalScope = downcast<ServiceWorkerGlobalScope>(context);
     166        auto activateEvent = ExtendableEvent::create(eventNames().activateEvent, { }, ExtendableEvent::IsTrusted::Yes);
     167        serviceWorkerGlobalScope.dispatchEvent(activateEvent);
     168
     169        activateEvent->whenAllExtendLifetimePromisesAreSettled([serviceWorkerIdentifier](HashSet<Ref<DOMPromise>>&&) {
     170            callOnMainThread([serviceWorkerIdentifier] () mutable {
     171                if (auto* connection = SWContextManager::singleton().connection())
     172                    connection->didFinishActivation(serviceWorkerIdentifier);
    180173            });
    181174        });
    182175    });
    183     runLoop().postTask(WTFMove(task));
    184176}
    185177
Note: See TracChangeset for help on using the changeset viewer.