Changeset 224306 in webkit


Ignore:
Timestamp:
Nov 1, 2017 4:24:31 PM (7 years ago)
Author:
Chris Dumez
Message:

Drop running Service Worker Jobs on a background thread
https://bugs.webkit.org/show_bug.cgi?id=179142

Reviewed by Youenn Fablet.

Drop running Service Worker Jobs on a background thread. We don't really need to
and this simplifies the logic a lot.

  • workers/service/server/SWServer.cpp:

(WebCore::SWServer::getRegistration):
(WebCore::SWServer::addRegistration):
(WebCore::SWServer::removeRegistration):

  • workers/service/server/SWServerJobQueue.cpp:

(WebCore::SWServerJobQueue::scriptContextStarted):
(WebCore::SWServerJobQueue::startNextJob):
(WebCore::SWServerJobQueue::runRegisterJob):
(WebCore::SWServerJobQueue::runUnregisterJob):
(WebCore::SWServerJobQueue::runUpdateJob):
(WebCore::SWServerJobQueue::finishCurrentJob):

  • workers/service/server/SWServerJobQueue.h:
Location:
trunk/Source/WebCore
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r224305 r224306  
     12017-11-01  Chris Dumez  <cdumez@apple.com>
     2
     3        Drop running Service Worker Jobs on a background thread
     4        https://bugs.webkit.org/show_bug.cgi?id=179142
     5
     6        Reviewed by Youenn Fablet.
     7
     8        Drop running Service Worker Jobs on a background thread. We don't really need to
     9        and this simplifies the logic a lot.
     10
     11        * workers/service/server/SWServer.cpp:
     12        (WebCore::SWServer::getRegistration):
     13        (WebCore::SWServer::addRegistration):
     14        (WebCore::SWServer::removeRegistration):
     15        * workers/service/server/SWServerJobQueue.cpp:
     16        (WebCore::SWServerJobQueue::scriptContextStarted):
     17        (WebCore::SWServerJobQueue::startNextJob):
     18        (WebCore::SWServerJobQueue::runRegisterJob):
     19        (WebCore::SWServerJobQueue::runUnregisterJob):
     20        (WebCore::SWServerJobQueue::runUpdateJob):
     21        (WebCore::SWServerJobQueue::finishCurrentJob):
     22        * workers/service/server/SWServerJobQueue.h:
     23
    1242017-11-01  Per Arne Vollan  <pvollan@apple.com>
    225
  • trunk/Source/WebCore/workers/service/server/SWServer.cpp

    r224301 r224306  
    7373SWServerRegistration* SWServer::getRegistration(const ServiceWorkerRegistrationKey& registrationKey)
    7474{
    75     ASSERT(!isMainThread());
    7675    return m_registrations.get(registrationKey);
    7776}
     
    7978void SWServer::addRegistration(std::unique_ptr<SWServerRegistration>&& registration)
    8079{
    81     ASSERT(!isMainThread());
    8280    auto key = registration->key();
    8381    m_registrations.add(key, WTFMove(registration));
     
    8684void SWServer::removeRegistration(const ServiceWorkerRegistrationKey& registrationKey)
    8785{
    88     ASSERT(!isMainThread());
    8986    m_registrations.remove(registrationKey);
    9087}
  • trunk/Source/WebCore/workers/service/server/SWServerJobQueue.cpp

    r224301 r224306  
    6868void SWServerJobQueue::scriptFetchFinished(SWServer::Connection& connection, const ServiceWorkerFetchResult& result)
    6969{
    70     ASSERT(isMainThread());
    7170    ASSERT(m_currentJob && m_currentJob->identifier() == result.jobIdentifier);
    7271
     
    8988void SWServerJobQueue::scriptContextFailedToStart(SWServer::Connection&, const String& workerID, const String& message)
    9089{
    91     ASSERT(isMainThread());
    9290    // FIXME: Install has failed. Run the install failed substeps
    9391    // Run the Update Worker State algorithm passing registration’s installing worker and redundant as the arguments.
     
    10199void SWServerJobQueue::scriptContextStarted(SWServer::Connection&, uint64_t serviceWorkerIdentifier, const String& workerID)
    102100{
    103     ASSERT(isMainThread());
    104101    UNUSED_PARAM(workerID);
    105102
    106     m_server.postTask(createCrossThreadTask(*this, &SWServerJobQueue::resolveCurrentRegistrationJobOnMainThead, serviceWorkerIdentifier));
     103    auto* registration = m_server.getRegistration(m_registrationKey);
     104    ASSERT(registration);
     105    registration->setActiveServiceWorkerIdentifier(serviceWorkerIdentifier);
     106    resolveCurrentRegistrationJob(registration->data());
    107107}
    108108
    109109void SWServerJobQueue::startNextJob()
    110110{
    111     ASSERT(isMainThread());
    112111    ASSERT(!m_currentJob);
    113112    ASSERT(!m_jobQueue.isEmpty());
    114113
    115     m_currentJob = std::make_unique<ServiceWorkerJobData>(m_jobQueue.takeFirst().isolatedCopy());
     114    m_currentJob = std::make_unique<ServiceWorkerJobData>(m_jobQueue.takeFirst());
    116115
    117116    switch (m_currentJob->type) {
    118117    case ServiceWorkerJobType::Register:
    119         m_server.postTask(createCrossThreadTask(*this, &SWServerJobQueue::runRegisterJob, *m_currentJob));
     118        runRegisterJob(*m_currentJob);
    120119        return;
    121120    case ServiceWorkerJobType::Unregister:
    122         m_server.postTask(createCrossThreadTask(*this, &SWServerJobQueue::runUnregisterJob, *m_currentJob));
     121        runUnregisterJob(*m_currentJob);
    123122        return;
    124123    }
     
    129128void SWServerJobQueue::runRegisterJob(const ServiceWorkerJobData& job)
    130129{
    131     ASSERT(!isMainThread());
    132130    ASSERT(job.type == ServiceWorkerJobType::Register);
    133131
    134132    if (!shouldTreatAsPotentiallyTrustworthy(job.scriptURL))
    135         return rejectWithExceptionOnMainThread(ExceptionData { SecurityError, ASCIILiteral("Script URL is not potentially trustworthy") });
     133        return rejectCurrentJob(ExceptionData { SecurityError, ASCIILiteral("Script URL is not potentially trustworthy") });
    136134
    137135    // If the origin of job’s script url is not job’s referrer's origin, then:
    138136    if (!protocolHostAndPortAreEqual(job.scriptURL, job.clientCreationURL))
    139         return rejectWithExceptionOnMainThread(ExceptionData { SecurityError, ASCIILiteral("Script origin does not match the registering client's origin") });
     137        return rejectCurrentJob(ExceptionData { SecurityError, ASCIILiteral("Script origin does not match the registering client's origin") });
    140138
    141139    // If the origin of job’s scope url is not job’s referrer's origin, then:
    142140    if (!protocolHostAndPortAreEqual(job.scopeURL, job.clientCreationURL))
    143         return rejectWithExceptionOnMainThread(ExceptionData { SecurityError, ASCIILiteral("Scope origin does not match the registering client's origin") });
     141        return rejectCurrentJob(ExceptionData { SecurityError, ASCIILiteral("Scope origin does not match the registering client's origin") });
    144142
    145143    // If registration is not null (in our parlance "empty"), then:
     
    148146        auto* newestWorker = registration->getNewestWorker();
    149147        if (newestWorker && equalIgnoringFragmentIdentifier(job.scriptURL, newestWorker->scriptURL()) && job.registrationOptions.updateViaCache == registration->updateViaCache()) {
    150             resolveWithRegistrationOnMainThread(*registration);
     148            resolveCurrentRegistrationJob(registration->data());
    151149            return;
    152150        }
     
    161159void SWServerJobQueue::runUnregisterJob(const ServiceWorkerJobData& job)
    162160{
    163     ASSERT(!isMainThread());
    164 
    165161    // If the origin of job’s scope url is not job's client's origin, then:
    166162    if (!protocolHostAndPortAreEqual(job.scopeURL, job.clientCreationURL))
    167         return rejectWithExceptionOnMainThread(ExceptionData { SecurityError, ASCIILiteral("Origin of scope URL does not match the client's origin") });
     163        return rejectCurrentJob(ExceptionData { SecurityError, ASCIILiteral("Origin of scope URL does not match the client's origin") });
    168164
    169165    // Let registration be the result of running "Get Registration" algorithm passing job’s scope url as the argument.
     
    173169    if (!registration || registration->isUninstalling()) {
    174170        // Invoke Resolve Job Promise with job and false.
    175         resolveWithUnregistrationResultOnMainThread(false);
     171        resolveCurrentUnregistrationJob(false);
    176172        return;
    177173    }
     
    181177
    182178    // Invoke Resolve Job Promise with job and true.
    183     resolveWithUnregistrationResultOnMainThread(true);
     179    resolveCurrentUnregistrationJob(true);
    184180
    185181    // FIXME: Invoke Try Clear Registration with registration.
     
    192188    // If registration is null (in our parlance "empty") or registration’s uninstalling flag is set, then:
    193189    if (!registration)
    194         return rejectWithExceptionOnMainThread(ExceptionData { TypeError, ASCIILiteral("Cannot update a null/nonexistent service worker registration") });
     190        return rejectCurrentJob(ExceptionData { TypeError, ASCIILiteral("Cannot update a null/nonexistent service worker registration") });
    195191    if (registration->isUninstalling())
    196         return rejectWithExceptionOnMainThread(ExceptionData { TypeError, ASCIILiteral("Cannot update a service worker registration that is uninstalling") });
    197 
    198     // If job’s job type is update, and newestWorker’s script url does not equal job’s script url with the exclude fragments flag set, then:
     192        return rejectCurrentJob(ExceptionData { TypeError, ASCIILiteral("Cannot update a service worker registration that is uninstalling") });
     193
     194    // If job's type is update, and newestWorker’s script url does not equal job’s script url with the exclude fragments flag set, then:
    199195    auto* newestWorker = registration->getNewestWorker();
    200196    if (newestWorker && !equalIgnoringFragmentIdentifier(job.scriptURL, newestWorker->scriptURL()))
    201         return rejectWithExceptionOnMainThread(ExceptionData { TypeError, ASCIILiteral("Cannot update a service worker with a requested script URL whose newest worker has a different script URL") });
    202 
    203     startScriptFetchFromMainThread();
    204 }
    205 
    206 void SWServerJobQueue::rejectWithExceptionOnMainThread(const ExceptionData& exception)
    207 {
    208     ASSERT(!isMainThread());
    209     m_server.postTaskReply(createCrossThreadTask(*this, &SWServerJobQueue::rejectCurrentJob, exception));
    210 }
    211 
    212 void SWServerJobQueue::resolveWithRegistrationOnMainThread(SWServerRegistration& registration)
    213 {
    214     ASSERT(!isMainThread());
    215     m_server.postTaskReply(createCrossThreadTask(*this, &SWServerJobQueue::resolveCurrentRegistrationJob, registration.data()));
    216 }
    217 
    218 void SWServerJobQueue::resolveCurrentRegistrationJobOnMainThead(uint64_t serviceWorkerIdentifier)
    219 {
    220     ASSERT(!isMainThread());
    221     auto* registration = m_server.getRegistration(m_registrationKey);
    222     ASSERT(registration);
    223     registration->setActiveServiceWorkerIdentifier(serviceWorkerIdentifier);
    224     resolveWithRegistrationOnMainThread(*registration);
    225 }
    226 
    227 void SWServerJobQueue::resolveWithUnregistrationResultOnMainThread(bool unregistrationResult)
    228 {
    229     ASSERT(!isMainThread());
    230     m_server.postTaskReply(createCrossThreadTask(*this, &SWServerJobQueue::resolveCurrentUnregistrationJob, unregistrationResult));
    231 }
    232 
    233 void SWServerJobQueue::startScriptFetchFromMainThread()
    234 {
    235     ASSERT(!isMainThread());
    236     m_server.postTaskReply(createCrossThreadTask(*this, &SWServerJobQueue::startScriptFetchForCurrentJob));
     197        return rejectCurrentJob(ExceptionData { TypeError, ASCIILiteral("Cannot update a service worker with a requested script URL whose newest worker has a different script URL") });
     198
     199    startScriptFetchForCurrentJob();
    237200}
    238201
    239202void SWServerJobQueue::rejectCurrentJob(const ExceptionData& exceptionData)
    240203{
    241     ASSERT(isMainThread());
    242204    ASSERT(m_currentJob);
    243205
     
    249211void SWServerJobQueue::resolveCurrentRegistrationJob(const ServiceWorkerRegistrationData& data)
    250212{
    251     ASSERT(isMainThread());
    252213    ASSERT(m_currentJob);
    253214    ASSERT(m_currentJob->type == ServiceWorkerJobType::Register);
     
    260221void SWServerJobQueue::resolveCurrentUnregistrationJob(bool unregistrationResult)
    261222{
    262     ASSERT(isMainThread());
    263223    ASSERT(m_currentJob);
    264224    ASSERT(m_currentJob->type == ServiceWorkerJobType::Unregister);
     
    271231void SWServerJobQueue::startScriptFetchForCurrentJob()
    272232{
    273     ASSERT(isMainThread());
    274233    ASSERT(m_currentJob);
    275234
     
    279238void SWServerJobQueue::finishCurrentJob()
    280239{
    281     ASSERT(isMainThread());
    282240    ASSERT(m_currentJob);
    283241    ASSERT(!m_jobTimer.isActive());
     
    287245        return;
    288246
    289     startNextJob();
     247    ASSERT(!m_jobTimer.isActive());
     248    m_jobTimer.startOneShot(0_s);
    290249}
    291250
  • trunk/Source/WebCore/workers/service/server/SWServerJobQueue.h

    r224301 r224306  
    5959    void runUpdateJob(const ServiceWorkerJobData&);
    6060
    61     void rejectWithExceptionOnMainThread(const ExceptionData&);
    62     void resolveWithRegistrationOnMainThread(SWServerRegistration&);
    63     void resolveCurrentRegistrationJobOnMainThead(uint64_t serviceWorkerIdentifier);
    64     void resolveWithUnregistrationResultOnMainThread(bool);
    65     void startScriptFetchFromMainThread();
    66     bool isEmpty();
    67 
    6861    Deque<ServiceWorkerJobData> m_jobQueue;
    6962    std::unique_ptr<ServiceWorkerJobData> m_currentJob;
  • trunk/Source/WebCore/workers/service/server/SWServerRegistration.cpp

    r224301 r224306  
    2929#if ENABLE(SERVICE_WORKER)
    3030
    31 #include "ExceptionData.h"
    3231#include "SWServer.h"
    3332#include "SWServerWorker.h"
    34 #include "SecurityOrigin.h"
    35 #include "ServiceWorkerFetchResult.h"
    36 #include "ServiceWorkerRegistrationData.h"
    3733#include "ServiceWorkerUpdateViaCache.h"
    38 #include "WorkerType.h"
    3934
    4035namespace WebCore {
     
    4338    : m_registrationKey(key)
    4439    , m_updateViaCache(updateViaCache)
    45     , m_scopeURL(scopeURL.isolatedCopy())
    46     , m_scriptURL(scriptURL.isolatedCopy())
     40    , m_scopeURL(scopeURL)
     41    , m_scriptURL(scriptURL)
    4742{
    4843    m_scopeURL.removeFragmentIdentifier();
     
    5550SWServerWorker* SWServerRegistration::getNewestWorker()
    5651{
    57     ASSERT(!isMainThread());
    5852    if (m_installingWorker)
    5953        return m_installingWorker.get();
Note: See TracChangeset for help on using the changeset viewer.