Changeset 225403 in webkit


Ignore:
Timestamp:
Dec 1, 2017 11:45:41 AM (6 years ago)
Author:
beidson@apple.com
Message:

Add Internals.terminateServiceWorker, and the ability to restart service workers for postMessage.
https://bugs.webkit.org/show_bug.cgi?id=180170

Reviewed by Chris Dumez.

Source/WebCore:

Test: http/tests/workers/service/postmessage-after-terminate.https.html

  • dom/ActiveDOMObject.cpp:

(WebCore::ActiveDOMObject::~ActiveDOMObject):

  • testing/Internals.cpp:

(WebCore::Internals::terminateServiceWorker):

  • testing/Internals.h:
  • testing/Internals.idl:
  • workers/service/SWClientConnection.h:
  • workers/service/ServiceWorker.idl:
  • workers/service/context/SWContextManager.cpp:

(WebCore::SWContextManager::postMessageToServiceWorkerGlobalScope):
(WebCore::SWContextManager::terminateWorker):

  • workers/service/context/SWContextManager.h:
  • workers/service/context/ServiceWorkerThreadProxy.h:
  • workers/service/server/SWServer.cpp:

(WebCore::SWServer::workerByID const):
(WebCore::SWServer::Connection::syncTerminateWorker):
(WebCore::SWServer::installContextData):
(WebCore::SWServer::invokeRunServiceWorker):
(WebCore::SWServer::terminateWorker):
(WebCore::SWServer::syncTerminateWorker):
(WebCore::SWServer::terminateWorkerInternal):
(WebCore::SWServer::workerContextTerminated):

  • workers/service/server/SWServer.h:

(WebCore::SWServer::workerByID const): Deleted.

  • workers/service/server/SWServerToContextConnection.h:
  • workers/service/server/SWServerWorker.cpp:

(WebCore::SWServerWorker::contextData const):

  • workers/service/server/SWServerWorker.h:

(WebCore::SWServerWorker::isRunning const):
(WebCore::SWServerWorker::isTerminating const):
(WebCore::SWServerWorker::setState):
(WebCore::SWServerWorker::server):

Source/WebKit:

  • StorageProcess/ServiceWorker/WebSWServerConnection.cpp:

(WebKit::WebSWServerConnection::startFetch):
(WebKit::WebSWServerConnection::postMessageToServiceWorkerGlobalScope):

  • StorageProcess/ServiceWorker/WebSWServerConnection.h:
  • StorageProcess/ServiceWorker/WebSWServerConnection.messages.in:
  • StorageProcess/ServiceWorker/WebSWServerToContextConnection.cpp:

(WebKit::WebSWServerToContextConnection::syncTerminateWorker):

  • StorageProcess/ServiceWorker/WebSWServerToContextConnection.h:
  • StorageProcess/StorageToWebProcessConnection.cpp:

(WebKit::StorageToWebProcessConnection::didReceiveSyncMessage):

  • WebProcess/Storage/WebSWClientConnection.cpp:

(WebKit::WebSWClientConnection::syncTerminateWorker):

  • WebProcess/Storage/WebSWClientConnection.h:
  • WebProcess/Storage/WebSWContextManagerConnection.cpp:

(WebKit::WebSWContextManagerConnection::terminateWorker):
(WebKit::WebSWContextManagerConnection::syncTerminateWorker):

  • WebProcess/Storage/WebSWContextManagerConnection.h:
  • WebProcess/Storage/WebSWContextManagerConnection.messages.in:
  • WebProcess/Storage/WebToStorageProcessConnection.cpp:

(WebKit::WebToStorageProcessConnection::didReceiveSyncMessage):

  • WebProcess/Storage/WebToStorageProcessConnection.h:

Source/WTF:

  • wtf/CompletionHandler.h:

(WTF::CompletionHandler<Out):

LayoutTests:

  • http/tests/workers/service/postmessage-after-terminate.https-expected.txt: Added.
  • http/tests/workers/service/postmessage-after-terminate.https.html: Added.
  • http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js:

(then):

  • http/tests/workers/service/resources/postmessage-after-terminate.js: Copied from LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js.

(then):

  • http/tests/workers/service/resources/postmessage-echo-worker.js: Renamed from LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage-worker.js.
Location:
trunk
Files:
2 added
33 edited
1 copied
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r225402 r225403  
     12017-12-01  Brady Eidson  <beidson@apple.com>
     2
     3        Add Internals.terminateServiceWorker, and the ability to restart service workers for postMessage.
     4        https://bugs.webkit.org/show_bug.cgi?id=180170
     5
     6        Reviewed by Chris Dumez.
     7
     8        * http/tests/workers/service/postmessage-after-terminate.https-expected.txt: Added.
     9        * http/tests/workers/service/postmessage-after-terminate.https.html: Added.
     10        * http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js:
     11        (then):
     12        * http/tests/workers/service/resources/postmessage-after-terminate.js: Copied from LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js.
     13        (then):
     14        * http/tests/workers/service/resources/postmessage-echo-worker.js: Renamed from LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage-worker.js.
     15
    1162017-12-01  Zalan Bujtas  <zalan@apple.com>
    217
  • trunk/LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js

    r225308 r225403  
    1010});
    1111
    12 navigator.serviceWorker.register("resources/basic-ServiceWorker-postMessage-worker.js", { }).then(function(registration) {
     12navigator.serviceWorker.register("resources/postmessage-echo-worker.js", { }).then(function(registration) {
    1313    registration.installing.postMessage("Message 1");
    1414});
  • trunk/LayoutTests/http/tests/workers/service/resources/postmessage-after-terminate.js

    r225402 r225403  
    44    log(event.data);
    55    if (messageNumber == 1) {
     6        window.internals.terminateServiceWorker(event.source);
    67        event.source.postMessage("Message 2");
    78        messageNumber++;
     
    1011});
    1112
    12 navigator.serviceWorker.register("resources/basic-ServiceWorker-postMessage-worker.js", { }).then(function(registration) {
     13navigator.serviceWorker.register("resources/postmessage-echo-worker.js", { }).then(function(registration) {
    1314    registration.installing.postMessage("Message 1");
    1415});
  • trunk/Source/WTF/ChangeLog

    r225375 r225403  
     12017-12-01  Brady Eidson  <beidson@apple.com>
     2
     3        Add Internals.terminateServiceWorker, and the ability to restart service workers for postMessage.
     4        https://bugs.webkit.org/show_bug.cgi?id=180170
     5
     6        Reviewed by Chris Dumez.
     7
     8        * wtf/CompletionHandler.h:
     9        (WTF::CompletionHandler<Out):
     10
    1112017-11-30  Yusuke Suzuki  <utatane.tea@gmail.com>
    212
  • trunk/Source/WTF/wtf/CompletionHandler.h

    r223617 r225403  
    5454    explicit operator bool() const { return !!m_function; }
    5555
    56     Out operator()(In... in)
     56    Out operator()(In... in) const
    5757    {
    5858        ASSERT_WITH_MESSAGE(m_function, "Completion handler should not be called more than once");
     
    6262
    6363private:
    64     WTF::Function<Out(In...)> m_function;
     64    mutable WTF::Function<Out(In...)> m_function;
    6565};
    6666
  • trunk/Source/WebCore/ChangeLog

    r225402 r225403  
     12017-12-01  Brady Eidson  <beidson@apple.com>
     2
     3        Add Internals.terminateServiceWorker, and the ability to restart service workers for postMessage.
     4        https://bugs.webkit.org/show_bug.cgi?id=180170
     5
     6        Reviewed by Chris Dumez.
     7
     8        Test: http/tests/workers/service/postmessage-after-terminate.https.html
     9
     10        * dom/ActiveDOMObject.cpp:
     11        (WebCore::ActiveDOMObject::~ActiveDOMObject):
     12
     13        * testing/Internals.cpp:
     14        (WebCore::Internals::terminateServiceWorker):
     15        * testing/Internals.h:
     16        * testing/Internals.idl:
     17
     18        * workers/service/SWClientConnection.h:
     19
     20        * workers/service/ServiceWorker.idl:
     21
     22        * workers/service/context/SWContextManager.cpp:
     23        (WebCore::SWContextManager::postMessageToServiceWorkerGlobalScope):
     24        (WebCore::SWContextManager::terminateWorker):
     25        * workers/service/context/SWContextManager.h:
     26
     27        * workers/service/context/ServiceWorkerThreadProxy.h:
     28
     29        * workers/service/server/SWServer.cpp:
     30        (WebCore::SWServer::workerByID const):
     31        (WebCore::SWServer::Connection::syncTerminateWorker):
     32        (WebCore::SWServer::installContextData):
     33        (WebCore::SWServer::invokeRunServiceWorker):
     34        (WebCore::SWServer::terminateWorker):
     35        (WebCore::SWServer::syncTerminateWorker):
     36        (WebCore::SWServer::terminateWorkerInternal):
     37        (WebCore::SWServer::workerContextTerminated):
     38        * workers/service/server/SWServer.h:
     39        (WebCore::SWServer::workerByID const): Deleted.
     40
     41        * workers/service/server/SWServerToContextConnection.h:
     42
     43        * workers/service/server/SWServerWorker.cpp:
     44        (WebCore::SWServerWorker::contextData const):
     45        * workers/service/server/SWServerWorker.h:
     46        (WebCore::SWServerWorker::isRunning const):
     47        (WebCore::SWServerWorker::isTerminating const):
     48        (WebCore::SWServerWorker::setState):
     49        (WebCore::SWServerWorker::server):
     50
    1512017-12-01  Zalan Bujtas  <zalan@apple.com>
    252
  • trunk/Source/WebCore/dom/ActiveDOMObject.cpp

    r192848 r225403  
    4848ActiveDOMObject::~ActiveDOMObject()
    4949{
    50     if (!m_scriptExecutionContext)
    51         return;
    52 
    53     ASSERT(m_suspendIfNeededWasCalled);
    54 
    5550    // ActiveDOMObject may be inherited by a sub-class whose life-cycle
    5651    // exceeds that of the associated ScriptExecutionContext. In those cases,
     
    5954    // inherit). Hence, we should ensure that this is not 0 before use it
    6055    // here.
    61     if (m_scriptExecutionContext) {
    62         ASSERT(m_scriptExecutionContext->isContextThread());
    63         m_scriptExecutionContext->willDestroyActiveDOMObject(*this);
    64     }
     56    if (!m_scriptExecutionContext)
     57        return;
     58
     59    ASSERT(m_suspendIfNeededWasCalled);
     60    ASSERT(m_scriptExecutionContext->isContextThread());
     61    m_scriptExecutionContext->willDestroyActiveDOMObject(*this);
    6562}
    6663
  • trunk/Source/WebCore/testing/Internals.cpp

    r225395 r225403  
    133133#include "SecurityOrigin.h"
    134134#include "SerializedScriptValue.h"
     135#include "ServiceWorker.h"
    135136#include "ServiceWorkerProvider.h"
    136137#include "ServiceWorkerRegistrationData.h"
     
    42714272    });
    42724273}
     4274
     4275void Internals::terminateServiceWorker(ServiceWorker& worker)
     4276{
     4277    if (!contextDocument())
     4278        return;
     4279
     4280    ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(contextDocument()->sessionID()).syncTerminateWorker(worker.identifier());
     4281}
    42734282#endif
    42744283
  • trunk/Source/WebCore/testing/Internals.h

    r225223 r225403  
    9191class XMLHttpRequest;
    9292
     93#if ENABLE(SERVICE_WORKER)
     94class ServiceWorker;
     95#endif
     96
    9397class Internals final : public RefCounted<Internals>, private ContextDestructionObserver
    9498#if ENABLE(MEDIA_STREAM)
     
    624628    using HasRegistrationPromise = DOMPromiseDeferred<IDLBoolean>;
    625629    void hasServiceWorkerRegistration(const String& clientURL, HasRegistrationPromise&&);
     630    void terminateServiceWorker(ServiceWorker&);
    626631#endif
    627632
  • trunk/Source/WebCore/testing/Internals.idl

    r225223 r225403  
    568568    [Conditional=SERVICE_WORKER, CallWith=ScriptExecutionContext] FetchEvent createBeingDispatchedFetchEvent();
    569569    [Conditional=SERVICE_WORKER] Promise<boolean> hasServiceWorkerRegistration(DOMString scopeURL);
     570    [Conditional=SERVICE_WORKER] void terminateServiceWorker(ServiceWorker worker);
    570571
    571572    [EnabledAtRuntime=WebAnimations] DOMString timelineDescription(AnimationTimeline timeline);
  • trunk/Source/WebCore/workers/service/SWClientConnection.h

    r225399 r225403  
    7474    virtual SWServerConnectionIdentifier serverConnectionIdentifier() const = 0;
    7575    virtual bool mayHaveServiceWorkerRegisteredForOrigin(const SecurityOrigin&) const = 0;
     76    virtual void syncTerminateWorker(ServiceWorkerIdentifier) = 0;
    7677
    7778    virtual void serviceWorkerStartedControllingClient(ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier) = 0;
  • trunk/Source/WebCore/workers/service/ServiceWorker.idl

    r225221 r225403  
    3232    Exposed=(Window,ServiceWorker),
    3333    Conditional=SERVICE_WORKER,
    34     EnabledAtRuntime=ServiceWorker
     34    EnabledAtRuntime=ServiceWorker,
     35    ExportMacro=WEBCORE_EXPORT
    3536] interface ServiceWorker : EventTarget {
    3637    readonly attribute USVString scriptURL;
  • trunk/Source/WebCore/workers/service/context/SWContextManager.cpp

    r225341 r225403  
    7070{
    7171    auto* serviceWorker = m_workerMap.get(destination);
    72     if (!serviceWorker)
    73         return;
     72    ASSERT(serviceWorker);
     73    ASSERT(!serviceWorker->isTerminatingOrTerminated());
    7474
    7575    // FIXME: We should pass valid MessagePortChannels.
     
    9595}
    9696
    97 void SWContextManager::terminateWorker(ServiceWorkerIdentifier identifier)
     97void SWContextManager::terminateWorker(ServiceWorkerIdentifier identifier, Function<void()>&& completionHandler)
    9898{
    9999    auto* serviceWorker = m_workerMap.get(identifier);
     
    101101        return;
    102102
    103     serviceWorker->thread().stop([identifier] {
     103    serviceWorker->setTerminatingOrTerminated(true);
     104
     105    serviceWorker->thread().stop([this, identifier, completionHandler = WTFMove(completionHandler)] {
    104106        if (auto* connection = SWContextManager::singleton().connection())
    105107            connection->workerTerminated(identifier);
     108
     109        if (completionHandler)
     110            completionHandler();
     111       
     112        auto worker = m_workerMap.take(identifier);
     113        ASSERT(worker);
     114       
     115        // Spin the runloop before releasing the worker thread proxy, as there would otherwise be
     116        // a race towards its destruction.
     117        callOnMainThread([worker = WTFMove(worker)] { });
    106118    });
    107119}
  • trunk/Source/WebCore/workers/service/context/SWContextManager.h

    r225341 r225403  
    6262    WEBCORE_EXPORT void fireInstallEvent(ServiceWorkerIdentifier);
    6363    WEBCORE_EXPORT void fireActivateEvent(ServiceWorkerIdentifier);
    64     WEBCORE_EXPORT void terminateWorker(ServiceWorkerIdentifier);
     64    WEBCORE_EXPORT void terminateWorker(ServiceWorkerIdentifier, Function<void()>&&);
    6565
    6666    void forEachServiceWorkerThread(const WTF::Function<void(ServiceWorkerThreadProxy&)>&);
  • trunk/Source/WebCore/workers/service/context/ServiceWorkerThreadProxy.h

    r224801 r225403  
    5757    ServiceWorkerInspectorProxy& inspectorProxy() { return m_inspectorProxy; }
    5858
     59    bool isTerminatingOrTerminated() const { return m_isTerminatingOrTerminated; }
     60    void setTerminatingOrTerminated(bool terminating) { m_isTerminatingOrTerminated = terminating; }
     61
    5962private:
    6063    WEBCORE_EXPORT ServiceWorkerThreadProxy(PageConfiguration&&, const ServiceWorkerContextData&, PAL::SessionID, CacheStorageProvider&);
     
    7578    RefPtr<CacheStorageConnection> m_cacheStorageConnection;
    7679    PAL::SessionID m_sessionID;
     80    bool m_isTerminatingOrTerminated { false };
     81
    7782    ServiceWorkerInspectorProxy m_inspectorProxy;
    7883#if ENABLE(REMOTE_INSPECTOR)
  • trunk/Source/WebCore/workers/service/server/SWServer.cpp

    r225298 r225403  
    8888}
    8989
     90SWServerWorker* SWServer::workerByID(ServiceWorkerIdentifier identifier) const
     91{
     92    auto* worker = SWServerWorker::existingWorkerForIdentifier(identifier);
     93    ASSERT(!worker || &worker->server() == this);
     94    return worker;
     95}
     96
    9097SWServerRegistration* SWServer::getRegistration(const ServiceWorkerRegistrationKey& registrationKey)
    9198{
     
    189196}
    190197
     198void SWServer::Connection::syncTerminateWorker(ServiceWorkerIdentifier identifier)
     199{
     200    if (auto* worker = m_server.workerByID(identifier))
     201        m_server.syncTerminateWorker(*worker);
     202}
     203
    191204SWServer::SWServer(UniqueRef<SWOriginStore>&& originStore)
    192205    : m_originStore(WTFMove(originStore))
     
    298311    if (auto* registration = getRegistration(worker.registrationKey()))
    299312        SWServerJobQueue::didFinishActivation(*registration, worker.identifier());
    300 }
    301 
    302 void SWServer::workerContextTerminated(SWServerWorker& worker)
    303 {
    304     auto result = m_workersByID.remove(worker.identifier());
    305     ASSERT_UNUSED(result, result);
    306313}
    307314
     
    388395    RELEASE_ASSERT(registration);
    389396
    390     auto result = m_workersByID.add(data.serviceWorkerIdentifier, SWServerWorker::create(*this, *registration, connection->identifier(), data.scriptURL, data.script, data.workerType, data.serviceWorkerIdentifier));
    391     ASSERT_UNUSED(result, result.isNewEntry);
     397    auto result = m_runningOrTerminatingWorkers.add(data.serviceWorkerIdentifier, SWServerWorker::create(*this, *registration, connection->identifier(), data.scriptURL, data.script, data.workerType, data.serviceWorkerIdentifier));
     398    ASSERT(result.isNewEntry);
     399
     400    result.iterator->value->setState(SWServerWorker::State::Running);
    392401
    393402    connection->installServiceWorkerContext(data);
    394403}
    395404
     405bool SWServer::invokeRunServiceWorker(ServiceWorkerIdentifier identifier)
     406{
     407    if (auto* worker = m_runningOrTerminatingWorkers.get(identifier)) {
     408        if (worker->isRunning())
     409            return true;
     410    }
     411
     412    // Nobody should have a ServiceWorkerIdentifier for a SWServerWorker that doesn't exist.
     413    auto* worker = workerByID(identifier);
     414    ASSERT(worker);
     415
     416    // If the registration for a working has been removed then the request to run
     417    // the worker is moot.
     418    if (!getRegistration(worker->registrationKey()))
     419        return false;
     420
     421    m_runningOrTerminatingWorkers.add(identifier, *worker);
     422    worker->setState(SWServerWorker::State::Running);
     423
     424    auto* connection = SWServerToContextConnection::globalServerToContextConnection();
     425    ASSERT(connection);
     426    connection->installServiceWorkerContext(worker->contextData());
     427   
     428    return true;
     429}
    396430
    397431void SWServer::terminateWorker(SWServerWorker& worker)
    398432{
     433    terminateWorkerInternal(worker, Asynchronous);
     434}
     435
     436void SWServer::syncTerminateWorker(SWServerWorker& worker)
     437{
     438    terminateWorkerInternal(worker, Synchronous);
     439}
     440
     441void SWServer::terminateWorkerInternal(SWServerWorker& worker, TerminationMode mode)
     442{
    399443    auto* connection = SWServerToContextConnection::connectionForIdentifier(worker.contextConnectionIdentifier());
    400     if (connection)
     444    if (!connection) {
     445        LOG_ERROR("Request to terminate a worker whose context connection does not exist");
     446        return;
     447    }
     448
     449    ASSERT(m_runningOrTerminatingWorkers.get(worker.identifier()) == &worker);
     450    ASSERT(!worker.isTerminating());
     451
     452    worker.setState(SWServerWorker::State::Terminating);
     453
     454    switch (mode) {
     455    case Asynchronous:
    401456        connection->terminateWorker(worker.identifier());
    402     else
    403         LOG_ERROR("Request to terminate a worker whose context connection does not exist");
     457        break;
     458    case Synchronous:
     459        connection->syncTerminateWorker(worker.identifier());
     460        break;
     461    };
     462}
     463
     464void SWServer::workerContextTerminated(SWServerWorker& worker)
     465{
     466    ASSERT(worker.isTerminating());
     467
     468    worker.setState(SWServerWorker::State::NotRunning);
     469
     470    // At this point if no registrations are referencing the worker then it will be destroyed,
     471    // removing itself from the m_workersByID map.
     472    auto result = m_runningOrTerminatingWorkers.take(worker.identifier());
     473    ASSERT_UNUSED(result, result && result->ptr() == &worker);
    404474}
    405475
  • trunk/Source/WebCore/workers/service/server/SWServer.h

    r225344 r225403  
    9191        WEBCORE_EXPORT void serviceWorkerStartedControllingClient(ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier);
    9292        WEBCORE_EXPORT void serviceWorkerStoppedControllingClient(ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier);
     93        WEBCORE_EXPORT void syncTerminateWorker(ServiceWorkerIdentifier);
    9394
    9495    private:
     
    109110    WEBCORE_EXPORT void clear(const SecurityOrigin&);
    110111
    111 
    112112    SWServerRegistration* getRegistration(const ServiceWorkerRegistrationKey&);
    113113    void addRegistration(std::unique_ptr<SWServerRegistration>&&);
     
    126126    void updateWorker(Connection&, const ServiceWorkerJobDataIdentifier&, SWServerRegistration&, const URL&, const String& script, WorkerType);
    127127    void terminateWorker(SWServerWorker&);
     128    void syncTerminateWorker(SWServerWorker&);
    128129    void fireInstallEvent(SWServerWorker&);
    129130    void fireActivateEvent(SWServerWorker&);
    130     SWServerWorker* workerByID(ServiceWorkerIdentifier identifier) const { return m_workersByID.get(identifier); }
     131    WEBCORE_EXPORT SWServerWorker* workerByID(ServiceWorkerIdentifier) const;
    131132   
    132133    Connection* getConnection(SWServerConnectionIdentifier identifier) { return m_connections.get(identifier); }
     
    146147    WEBCORE_EXPORT void unregisterServiceWorkerClient(const ClientOrigin&, ServiceWorkerClientIdentifier);
    147148
     149    WEBCORE_EXPORT bool invokeRunServiceWorker(ServiceWorkerIdentifier);
     150
    148151private:
    149152    void registerConnection(Connection&);
     
    166169    void installContextData(const ServiceWorkerContextData&);
    167170
     171    enum TerminationMode {
     172        Synchronous,
     173        Asynchronous,
     174    };
     175    void terminateWorkerInternal(SWServerWorker&, TerminationMode);
     176
    168177    HashMap<SWServerConnectionIdentifier, Connection*> m_connections;
    169178    HashMap<ServiceWorkerRegistrationKey, std::unique_ptr<SWServerRegistration>> m_registrations;
     
    171180    HashMap<ServiceWorkerRegistrationKey, std::unique_ptr<SWServerJobQueue>> m_jobQueues;
    172181
    173     HashMap<ServiceWorkerIdentifier, Ref<SWServerWorker>> m_workersByID;
     182    HashMap<ServiceWorkerIdentifier, Ref<SWServerWorker>> m_runningOrTerminatingWorkers;
    174183
    175184    struct ClientInformation {
  • trunk/Source/WebCore/workers/service/server/SWServerToContextConnection.h

    r225347 r225403  
    4949    virtual void fireActivateEvent(ServiceWorkerIdentifier) = 0;
    5050    virtual void terminateWorker(ServiceWorkerIdentifier) = 0;
     51    virtual void syncTerminateWorker(ServiceWorkerIdentifier) = 0;
    5152
    5253    // Messages back from the SW host process
  • trunk/Source/WebCore/workers/service/server/SWServerWorker.cpp

    r225298 r225403  
    6161}
    6262
     63ServiceWorkerContextData SWServerWorker::contextData() const
     64{
     65    auto* registration = m_server.getRegistration(m_registrationKey);
     66    ASSERT(registration);
     67
     68    return { std::nullopt, registration->data(), m_data.identifier, m_script, m_data.scriptURL, m_data.type };
     69}
     70
    6371void SWServerWorker::terminate()
    6472{
  • trunk/Source/WebCore/workers/service/server/SWServerWorker.h

    r225347 r225403  
    4040class SWServerRegistration;
    4141enum class WorkerType;
     42struct ServiceWorkerContextData;
    4243struct ServiceWorkerJobDataIdentifier;
    4344
     
    5455    void terminate();
    5556
    56     SWServer& server();
     57    enum class State {
     58        Running,
     59        Terminating,
     60        NotRunning,
     61    };
     62    bool isRunning() const { return m_state == State::Running; }
     63    bool isTerminating() const { return m_state == State::Terminating; }
     64    void setState(State state) { m_state = state; }
     65
     66    SWServer& server() { return m_server; }
    5767    const ServiceWorkerRegistrationKey& registrationKey() const { return m_registrationKey; }
    5868    const URL& scriptURL() const { return m_data.scriptURL; }
     
    7888
    7989    const ServiceWorkerData& data() const { return m_data; }
     90    ServiceWorkerContextData contextData() const;
    8091
    8192private:
     
    8899    String m_script;
    89100    bool m_hasPendingEvents { false };
     101    State m_state { State::NotRunning };
    90102};
    91103
  • trunk/Source/WebKit/ChangeLog

    r225399 r225403  
     12017-12-01  Brady Eidson  <beidson@apple.com>
     2
     3        Add Internals.terminateServiceWorker, and the ability to restart service workers for postMessage.
     4        https://bugs.webkit.org/show_bug.cgi?id=180170
     5
     6        Reviewed by Chris Dumez.
     7
     8        * StorageProcess/ServiceWorker/WebSWServerConnection.cpp:
     9        (WebKit::WebSWServerConnection::startFetch):
     10        (WebKit::WebSWServerConnection::postMessageToServiceWorkerGlobalScope):
     11        * StorageProcess/ServiceWorker/WebSWServerConnection.h:
     12        * StorageProcess/ServiceWorker/WebSWServerConnection.messages.in:
     13
     14        * StorageProcess/ServiceWorker/WebSWServerToContextConnection.cpp:
     15        (WebKit::WebSWServerToContextConnection::syncTerminateWorker):
     16        * StorageProcess/ServiceWorker/WebSWServerToContextConnection.h:
     17
     18        * StorageProcess/StorageToWebProcessConnection.cpp:
     19        (WebKit::StorageToWebProcessConnection::didReceiveSyncMessage):
     20
     21        * WebProcess/Storage/WebSWClientConnection.cpp:
     22        (WebKit::WebSWClientConnection::syncTerminateWorker):
     23        * WebProcess/Storage/WebSWClientConnection.h:
     24
     25        * WebProcess/Storage/WebSWContextManagerConnection.cpp:
     26        (WebKit::WebSWContextManagerConnection::terminateWorker):
     27        (WebKit::WebSWContextManagerConnection::syncTerminateWorker):
     28        * WebProcess/Storage/WebSWContextManagerConnection.h:
     29        * WebProcess/Storage/WebSWContextManagerConnection.messages.in:
     30
     31        * WebProcess/Storage/WebToStorageProcessConnection.cpp:
     32        (WebKit::WebToStorageProcessConnection::didReceiveSyncMessage):
     33        * WebProcess/Storage/WebToStorageProcessConnection.h:
     34
    1352017-12-01  Youenn Fablet  <youenn@apple.com>
    236
  • trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.cpp

    r225251 r225403  
    119119void WebSWServerConnection::startFetch(uint64_t fetchIdentifier, std::optional<ServiceWorkerIdentifier> serviceWorkerIdentifier, const ResourceRequest& request, const FetchOptions& options, const IPC::FormDataReference& formData)
    120120{
     121    // It's possible this specific worker cannot be re-run (e.g. its registration has been removed)
     122    if (serviceWorkerIdentifier && !server().invokeRunServiceWorker(*serviceWorkerIdentifier))
     123        return;
     124
     125    // FIXME: If we don't have a ServiceWorkerIdentifier here then we need to create and run the appropriate Service Worker,
     126    // but it's unclear that we have enough information here to do that.
     127
    121128    sendToContextProcess(Messages::WebSWContextManagerConnection::StartFetch { identifier(), fetchIdentifier, serviceWorkerIdentifier, request, options, formData });
    122129}
     
    124131void WebSWServerConnection::postMessageToServiceWorkerGlobalScope(ServiceWorkerIdentifier destinationServiceWorkerIdentifier, const IPC::DataReference& message, DocumentIdentifier sourceContextIdentifier, ServiceWorkerClientData&& sourceData)
    125132{
     133    // It's possible this specific worker cannot be re-run (e.g. its registration has been removed)
     134    if (!server().invokeRunServiceWorker(destinationServiceWorkerIdentifier))
     135        return;
     136
    126137    ServiceWorkerClientIdentifier sourceIdentifier { identifier(), sourceContextIdentifier };
    127138    sendToContextProcess(Messages::WebSWContextManagerConnection::PostMessageToServiceWorkerGlobalScope { destinationServiceWorkerIdentifier, message, sourceIdentifier, WTFMove(sourceData) });
  • trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.h

    r225251 r225403  
    5555    void disconnectedFromWebProcess();
    5656    void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
     57    void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&);
    5758
    5859    PAL::SessionID sessionID() const { return m_sessionID; }
  • trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.messages.in

    r225251 r225403  
    4141    RegisterServiceWorkerClient(struct WebCore::SecurityOriginData topOrigin, WebCore::DocumentIdentifier identifier, struct WebCore::ServiceWorkerClientData data)
    4242    UnregisterServiceWorkerClient(WebCore::DocumentIdentifier identifier)
     43
     44    SyncTerminateWorker(WebCore::ServiceWorkerIdentifier workerIdentifier) -> ()
    4345}
    4446
  • trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerToContextConnection.cpp

    r224909 r225403  
    7777}
    7878
     79void WebSWServerToContextConnection::syncTerminateWorker(ServiceWorkerIdentifier serviceWorkerIdentifier)
     80{
     81    sendSync(Messages::WebSWContextManagerConnection::SyncTerminateWorker(serviceWorkerIdentifier), Messages::WebSWContextManagerConnection::SyncTerminateWorker::Reply());
     82}
     83
    7984} // namespace WebKit
    8085
  • trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerToContextConnection.h

    r224909 r225403  
    6060    void fireActivateEvent(WebCore::ServiceWorkerIdentifier) final;
    6161    void terminateWorker(WebCore::ServiceWorkerIdentifier) final;
     62    void syncTerminateWorker(WebCore::ServiceWorkerIdentifier) final;
    6263
    6364    Ref<IPC::Connection> m_ipcConnection;
  • trunk/Source/WebKit/StorageProcess/StorageToWebProcessConnection.cpp

    r225289 r225403  
    109109    }
    110110
     111#if ENABLE(SERVICE_WORKER)
     112    if (decoder.messageReceiverName() == Messages::WebSWServerConnection::messageReceiverName()) {
     113        auto iterator = m_swConnections.find(makeObjectIdentifier<SWServerConnectionIdentifierType>(decoder.destinationID()));
     114        if (iterator != m_swConnections.end())
     115            iterator->value->didReceiveSyncMessage(connection, decoder, replyEncoder);
     116        return;
     117    }
     118#endif
     119
    111120    ASSERT_NOT_REACHED();
    112121}
  • trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp

    r225399 r225403  
    191191}
    192192
     193void WebSWClientConnection::syncTerminateWorker(ServiceWorkerIdentifier identifier)
     194{
     195    sendSync(Messages::WebSWServerConnection::SyncTerminateWorker(identifier), Messages::WebSWServerConnection::SyncTerminateWorker::Reply());
     196}
     197
    193198} // namespace WebKit
    194199
  • trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.h

    r225399 r225403  
    6767    void connectionToServerLost();
    6868
     69    void syncTerminateWorker(WebCore::ServiceWorkerIdentifier) final;
     70
    6971private:
    7072    WebSWClientConnection(IPC::Connection&, PAL::SessionID);
  • trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.cpp

    r225298 r225403  
    167167void WebSWContextManagerConnection::terminateWorker(ServiceWorkerIdentifier identifier)
    168168{
    169     SWContextManager::singleton().terminateWorker(identifier);
     169    SWContextManager::singleton().terminateWorker(identifier, nullptr);
     170}
     171
     172void WebSWContextManagerConnection::syncTerminateWorker(ServiceWorkerIdentifier identifier, Ref<Messages::WebSWContextManagerConnection::SyncTerminateWorker::DelayedReply>&& reply)
     173{
     174    SWContextManager::singleton().terminateWorker(identifier, [reply = WTFMove(reply)] {
     175        reply->send();
     176    });
    170177}
    171178
  • trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.h

    r225298 r225403  
    3030#include "Connection.h"
    3131#include "MessageReceiver.h"
     32#include "WebSWContextManagerConnectionMessages.h"
    3233#include <WebCore/SWContextManager.h>
    3334#include <WebCore/ServiceWorkerTypes.h>
     
    5253
    5354    void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
     55    void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) final;
    5456
    5557private:
     
    7173    void fireActivateEvent(WebCore::ServiceWorkerIdentifier);
    7274    void terminateWorker(WebCore::ServiceWorkerIdentifier);
     75    void syncTerminateWorker(WebCore::ServiceWorkerIdentifier, Ref<Messages::WebSWContextManagerConnection::SyncTerminateWorker::DelayedReply>&&);
    7376
    7477    Ref<IPC::Connection> m_connectionToStorageProcess;
  • trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.messages.in

    r225008 r225403  
    3030    FireActivateEvent(WebCore::ServiceWorkerIdentifier identifier)
    3131    TerminateWorker(WebCore::ServiceWorkerIdentifier identifier)
     32    SyncTerminateWorker(WebCore::ServiceWorkerIdentifier identifier) -> () Delayed
    3233}
    3334
  • trunk/Source/WebKit/WebProcess/Storage/WebToStorageProcessConnection.cpp

    r225399 r225403  
    8787}
    8888
     89void WebToStorageProcessConnection::didReceiveSyncMessage(IPC::Connection& connection, IPC::Decoder& decoder, std::unique_ptr<IPC::Encoder>& replyEncoder)
     90{
     91#if ENABLE(SERVICE_WORKER)
     92    if (decoder.messageReceiverName() == Messages::WebSWContextManagerConnection::messageReceiverName()) {
     93        ASSERT(SWContextManager::singleton().connection());
     94        if (auto* contextManagerConnection = SWContextManager::singleton().connection())
     95            static_cast<WebSWContextManagerConnection&>(*contextManagerConnection).didReceiveSyncMessage(connection, decoder, replyEncoder);
     96        return;
     97    }
     98#endif
     99    ASSERT_NOT_REACHED();
     100}
     101
    89102void WebToStorageProcessConnection::didClose(IPC::Connection& connection)
    90103{
  • trunk/Source/WebKit/WebProcess/Storage/WebToStorageProcessConnection.h

    r225399 r225403  
    6363    // IPC::Connection::Client
    6464    void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
     65    void didReceiveSyncMessage(IPC::Connection&, IPC::Decoder&, std::unique_ptr<IPC::Encoder>&) override;
    6566    void didClose(IPC::Connection&) override;
    6667    void didReceiveInvalidMessage(IPC::Connection&, IPC::StringReference messageReceiverName, IPC::StringReference messageName) override;
Note: See TracChangeset for help on using the changeset viewer.