Changeset 221461 in webkit


Ignore:
Timestamp:
Aug 31, 2017 5:12:52 PM (7 years ago)
Author:
beidson@apple.com
Message:

Add (entirely incorrect) fetching of ServiceWorker scripts.
https://bugs.webkit.org/show_bug.cgi?id=176179

Reviewed by Andy Estes.

Source/WebCore:

No new tests (Covered by changes to existing tests).

When the Storage process is running the "Update" algorithm and a ServiceWorker script file needs to be fetched, this patch:

  • Messages back to the WebContent process that started the register/update job
  • Executes a FetchLoad in that script context for the script
  • Sends the results back to the Storage process

We don't do anything with the results yet.

Soon.

  • WebCore.xcodeproj/project.pbxproj:
  • workers/service/ServiceWorkerContainer.cpp:

(WebCore::ServiceWorkerContainer::startScriptFetchForJob):
(WebCore::ServiceWorkerContainer::jobFinishedLoadingScript):
(WebCore::ServiceWorkerContainer::jobFailedLoadingScript):

  • workers/service/ServiceWorkerContainer.h:
  • workers/service/ServiceWorkerFetchResult.h: Copied from Source/WebCore/workers/service/server/SWClientConnection.h.

(WebCore::ServiceWorkerFetchResult::encode const):
(WebCore::ServiceWorkerFetchResult::decode):

  • workers/service/ServiceWorkerJob.cpp:

(WebCore::ServiceWorkerJob::startScriptFetch):
(WebCore::ServiceWorkerJob::fetchScriptWithContext):
(WebCore::ServiceWorkerJob::didReceiveResponse):
(WebCore::ServiceWorkerJob::didReceiveData):
(WebCore::ServiceWorkerJob::didSucceed):
(WebCore::ServiceWorkerJob::didFail):

  • workers/service/ServiceWorkerJob.h:
  • workers/service/ServiceWorkerJobClient.h:
  • workers/service/server/SWClientConnection.cpp:

(WebCore::SWClientConnection::finishedFetchingScript):
(WebCore::SWClientConnection::failedFetchingScript):
(WebCore::SWClientConnection::startScriptFetchForServer):

  • workers/service/server/SWClientConnection.h:
  • workers/service/server/SWServer.cpp:

(WebCore::SWServer::Connection::finishFetchingScriptInServer):
(WebCore::SWServer::startScriptFetch):
(WebCore::SWServer::scriptFetchFinished):

  • workers/service/server/SWServer.h:
  • workers/service/server/SWServerRegistration.cpp:

(WebCore::SWServerRegistration::scriptFetchFinished):
(WebCore::SWServerRegistration::runUpdateJob):
(WebCore::SWServerRegistration::startScriptFetchFromMainThread):
(WebCore::SWServerRegistration::startScriptFetchForCurrentJob):

  • workers/service/server/SWServerRegistration.h:

Source/WebKit:

  • StorageProcess/ServiceWorker/WebSWServerConnection.cpp:

(WebKit::WebSWServerConnection::startScriptFetchInClient):

  • StorageProcess/ServiceWorker/WebSWServerConnection.h:
  • StorageProcess/ServiceWorker/WebSWServerConnection.messages.in:
  • WebProcess/Storage/WebSWClientConnection.cpp:

(WebKit::WebSWClientConnection::finishFetchingScriptInServer):

  • WebProcess/Storage/WebSWClientConnection.h:
  • WebProcess/Storage/WebSWClientConnection.messages.in:

LayoutTests:

  • http/tests/workers/service/basic-register-expected.txt:
  • http/tests/workers/service/resources/basic-register.js:
  • http/tests/workers/service/resources/registration-task-queue-scheduling-1.js:
Location:
trunk
Files:
24 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r221456 r221461  
     12017-08-31  Brady Eidson  <beidson@apple.com>
     2
     3        Add (entirely incorrect) fetching of ServiceWorker scripts.
     4        https://bugs.webkit.org/show_bug.cgi?id=176179
     5
     6        Reviewed by Andy Estes.
     7
     8        * http/tests/workers/service/basic-register-expected.txt:
     9        * http/tests/workers/service/resources/basic-register.js:
     10        * http/tests/workers/service/resources/registration-task-queue-scheduling-1.js:
     11
    1122017-08-31  Antti Koivisto  <antti@apple.com>
    213
  • trunk/LayoutTests/http/tests/workers/service/basic-register-expected.txt

    r220539 r221461  
    1 CONSOLE MESSAGE: line 11: Registration failed with error: UnknownError: serviceWorker job scheduling is not yet implemented
     1CONSOLE MESSAGE: line 10: Registration failed with error: UnknownError: Script URL http://127.0.0.1:8000/workers/service/resources/empty-worker.js fetched with 41 bytes, but we're not using the result yet
     2CONSOLE MESSAGE: line 21: Registration failed with error: UnknownError: Script URL http://127.0.0.1:8000/workers/service/resources/empty-worker-doesnt-exist.js fetch resulted in error: ServiceWorker script network fetch failed with HTTP status code 404
    23
  • trunk/LayoutTests/http/tests/workers/service/resources/basic-register.js

    r220924 r221461  
    55
    66navigator.serviceWorker.register("resources/empty-worker.js", { })
     7.then(function(r) {
     8        console.log("Registered!");
     9}, function(e) {
     10        console.log("Registration failed with error: " + e);
     11})
     12.catch(function(e) {
     13        console.log("Exception registering: " + e);
     14});
     15
     16navigator.serviceWorker.register("resources/empty-worker-doesnt-exist.js", { })
    717.then(function(r) {
    818        console.log("Registered!");
  • trunk/LayoutTests/http/tests/workers/service/resources/registration-task-queue-scheduling-1.js

    r221198 r221461  
    4949                done();
    5050        }, function(e) {
    51                 if (e+"" != "UnknownError: serviceWorker job scheduling is not yet implemented") {
    52                         alert("Unexpected error received from server");
     51                if (e+"" != "UnknownError: Script URL http://127.0.0.1:8000/workers/service/resources/empty-worker.js fetched with 41 bytes, but we're not using the result yet") {
     52                        alert("Unexpected error received from server: " + e);
    5353                        finishSWTest();
    5454                }
  • trunk/Source/WebCore/ChangeLog

    r221459 r221461  
     12017-08-31  Brady Eidson  <beidson@apple.com>
     2
     3        Add (entirely incorrect) fetching of ServiceWorker scripts.
     4        https://bugs.webkit.org/show_bug.cgi?id=176179
     5
     6        Reviewed by Andy Estes.
     7
     8        No new tests (Covered by changes to existing tests).
     9
     10        When the Storage process is running the "Update" algorithm and a ServiceWorker script file needs to be fetched, this patch:
     11          - Messages back to the WebContent process that started the register/update job
     12          - Executes a FetchLoad in that script context for the script
     13          - Sends the results back to the Storage process
     14
     15        We don't do anything with the results yet.
     16
     17        Soon.
     18
     19        * WebCore.xcodeproj/project.pbxproj:
     20
     21        * workers/service/ServiceWorkerContainer.cpp:
     22        (WebCore::ServiceWorkerContainer::startScriptFetchForJob):
     23        (WebCore::ServiceWorkerContainer::jobFinishedLoadingScript):
     24        (WebCore::ServiceWorkerContainer::jobFailedLoadingScript):
     25        * workers/service/ServiceWorkerContainer.h:
     26
     27        * workers/service/ServiceWorkerFetchResult.h: Copied from Source/WebCore/workers/service/server/SWClientConnection.h.
     28        (WebCore::ServiceWorkerFetchResult::encode const):
     29        (WebCore::ServiceWorkerFetchResult::decode):
     30
     31        * workers/service/ServiceWorkerJob.cpp:
     32        (WebCore::ServiceWorkerJob::startScriptFetch):
     33        (WebCore::ServiceWorkerJob::fetchScriptWithContext):
     34        (WebCore::ServiceWorkerJob::didReceiveResponse):
     35        (WebCore::ServiceWorkerJob::didReceiveData):
     36        (WebCore::ServiceWorkerJob::didSucceed):
     37        (WebCore::ServiceWorkerJob::didFail):
     38        * workers/service/ServiceWorkerJob.h:
     39
     40        * workers/service/ServiceWorkerJobClient.h:
     41
     42        * workers/service/server/SWClientConnection.cpp:
     43        (WebCore::SWClientConnection::finishedFetchingScript):
     44        (WebCore::SWClientConnection::failedFetchingScript):
     45        (WebCore::SWClientConnection::startScriptFetchForServer):
     46        * workers/service/server/SWClientConnection.h:
     47
     48        * workers/service/server/SWServer.cpp:
     49        (WebCore::SWServer::Connection::finishFetchingScriptInServer):
     50        (WebCore::SWServer::startScriptFetch):
     51        (WebCore::SWServer::scriptFetchFinished):
     52        * workers/service/server/SWServer.h:
     53
     54        * workers/service/server/SWServerRegistration.cpp:
     55        (WebCore::SWServerRegistration::scriptFetchFinished):
     56        (WebCore::SWServerRegistration::runUpdateJob):
     57        (WebCore::SWServerRegistration::startScriptFetchFromMainThread):
     58        (WebCore::SWServerRegistration::startScriptFetchForCurrentJob):
     59        * workers/service/server/SWServerRegistration.h:
     60
    1612017-08-31  Daniel Bates  <dabates@apple.com>
    262
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r221441 r221461  
    23062306                517A534E1F54A8BA00DCDC0A /* ServiceWorkerRegistrationData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 517A534B1F549D4A00DCDC0A /* ServiceWorkerRegistrationData.cpp */; };
    23072307                517A534F1F54A8BA00DCDC0A /* ServiceWorkerRegistrationData.h in Headers */ = {isa = PBXBuildFile; fileRef = 517A534C1F549D4A00DCDC0A /* ServiceWorkerRegistrationData.h */; settings = {ATTRIBUTES = (Private, ); }; };
     2308                517A53581F5889E800DCDC0A /* FetchLoaderClient.h in Headers */ = {isa = PBXBuildFile; fileRef = 4147E2B61C89912600A7E715 /* FetchLoaderClient.h */; settings = {ATTRIBUTES = (Private, ); }; };
     2309                517A535A1F5889EF00DCDC0A /* FetchLoader.h in Headers */ = {isa = PBXBuildFile; fileRef = 4147E2B51C89912600A7E715 /* FetchLoader.h */; settings = {ATTRIBUTES = (Private, ); }; };
     2310                517A535B1F588A4C00DCDC0A /* FetchBodyConsumer.h in Headers */ = {isa = PBXBuildFile; fileRef = 41CF8BE51D46222000707DC9 /* FetchBodyConsumer.h */; settings = {ATTRIBUTES = (Private, ); }; };
     2311                517A535D1F5899FE00DCDC0A /* ServiceWorkerFetchResult.h in Headers */ = {isa = PBXBuildFile; fileRef = 517A535C1F5899F200DCDC0A /* ServiceWorkerFetchResult.h */; settings = {ATTRIBUTES = (Private, ); }; };
    23082312                517A63C31B74318700E7DCDC /* KeyedDecoderCF.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 517A63BF1B74317E00E7DCDC /* KeyedDecoderCF.cpp */; };
    23092313                517A63C41B74318B00E7DCDC /* KeyedEncoderCF.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 517A63C11B74317E00E7DCDC /* KeyedEncoderCF.cpp */; };
     
    1013210136                517A534B1F549D4A00DCDC0A /* ServiceWorkerRegistrationData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ServiceWorkerRegistrationData.cpp; sourceTree = "<group>"; };
    1013310137                517A534C1F549D4A00DCDC0A /* ServiceWorkerRegistrationData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ServiceWorkerRegistrationData.h; sourceTree = "<group>"; };
     10138                517A535C1F5899F200DCDC0A /* ServiceWorkerFetchResult.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ServiceWorkerFetchResult.h; sourceTree = "<group>"; };
    1013410139                517A63BF1B74317E00E7DCDC /* KeyedDecoderCF.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = KeyedDecoderCF.cpp; sourceTree = "<group>"; };
    1013510140                517A63C01B74317E00E7DCDC /* KeyedDecoderCF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KeyedDecoderCF.h; sourceTree = "<group>"; };
     
    1886318868                                51F175571F3EBC0C00C74950 /* ServiceWorkerContainer.h */,
    1886418869                                51F175561F3EBC0C00C74950 /* ServiceWorkerContainer.idl */,
     18870                                517A535C1F5899F200DCDC0A /* ServiceWorkerFetchResult.h */,
    1886518871                                51F175551F3EBC0C00C74950 /* ServiceWorkerGlobalScope.cpp */,
    1886618872                                51F175541F3EBC0C00C74950 /* ServiceWorkerGlobalScope.h */,
     
    2682226828                                7C6522F51E00A51700677F22 /* ApplePayPaymentPass.h in Headers */,
    2682326829                                7C6579E01E0079F900E3A27A /* ApplePayPaymentRequest.h in Headers */,
     26830                                517A535A1F5889EF00DCDC0A /* FetchLoader.h in Headers */,
    2682426831                                1A8A64591D19FCFC00D0E00F /* ApplePaySession.h in Headers */,
    2682526832                                1A58E8651D19D4EB00C0EA73 /* ApplePaySessionPaymentRequest.h in Headers */,
     
    2779027797                                977B3867122883E900B81FF8 /* HTMLDocumentParser.h in Headers */,
    2779127798                                93F198E608245E59001E9ABC /* HTMLElement.h in Headers */,
     27799                                517A53581F5889E800DCDC0A /* FetchLoaderClient.h in Headers */,
    2779227800                                A17C81230F2A5CF7005DAAEB /* HTMLElementFactory.h in Headers */,
    2779327801                                977B37241228721700B81FF8 /* HTMLElementStack.h in Headers */,
     
    2894128949                                37C738EF1EDBD71B003F2B0B /* MathMLUnknownElement.h in Headers */,
    2894228950                                439046EA12DA25E812AF80AC /* MathOperator.h in Headers */,
     28951                                517A535B1F588A4C00DCDC0A /* FetchBodyConsumer.h in Headers */,
    2894328952                                49D5DC2C0F423A73008F20FD /* Matrix3DTransformOperation.h in Headers */,
    2894428953                                49E911C70EF86D47009D0CAF /* MatrixTransformOperation.h in Headers */,
     
    2910329112                                A19D934B1AA11B1E00B46C24 /* NetworkExtensionContentFilter.h in Headers */,
    2910429113                                628D214C12131ED10055DCFC /* NetworkingContext.h in Headers */,
     29114                                517A535D1F5899FE00DCDC0A /* ServiceWorkerFetchResult.h in Headers */,
    2910529115                                8A81BF8511DCFD9000DA2B98 /* NetworkLoadMetrics.h in Headers */,
    2910629116                                59C27F07138D28CF0079B7E2 /* NetworkResourcesData.h in Headers */,
  • trunk/Source/WebCore/workers/service/ServiceWorkerContainer.cpp

    r221392 r221461  
    3333#include "JSDOMPromiseDeferred.h"
    3434#include "JSServiceWorkerRegistration.h"
     35#include "Logging.h"
    3536#include "NavigatorBase.h"
     37#include "ResourceError.h"
    3638#include "ScopeGuard.h"
    3739#include "ScriptExecutionContext.h"
     
    182184}
    183185
     186void ServiceWorkerContainer::startScriptFetchForJob(ServiceWorkerJob& job)
     187{
     188    LOG(ServiceWorker, "SeviceWorkerContainer %p starting script fetch for job %" PRIu64, this, job.data().identifier());
     189
     190    auto* context = scriptExecutionContext();
     191    if (!context) {
     192        LOG_ERROR("ServiceWorkerContainer::jobResolvedWithRegistration called but the container's ScriptExecutionContext is gone");
     193        m_swConnection->failedFetchingScript(job, { errorDomainWebKitInternal, 0, job.data().scriptURL, ASCIILiteral("Attempt to fetch service worker script with no ScriptExecutionContext") });
     194        jobDidFinish(job);
     195        return;
     196    }
     197
     198    job.fetchScriptWithContext(*context);
     199}
     200
     201void ServiceWorkerContainer::jobFinishedLoadingScript(ServiceWorkerJob& job, Ref<SharedBuffer>&& data)
     202{
     203    LOG(ServiceWorker, "SeviceWorkerContainer %p finished fetching script for job %" PRIu64, this, job.data().identifier());
     204
     205    m_swConnection->finishedFetchingScript(job, data.get());
     206}
     207
     208void ServiceWorkerContainer::jobFailedLoadingScript(ServiceWorkerJob& job, const ResourceError& error)
     209{
     210    LOG(ServiceWorker, "SeviceWorkerContainer %p failed fetching script for job %" PRIu64, this, job.data().identifier());
     211
     212    m_swConnection->failedFetchingScript(job, error);
     213}
     214
    184215void ServiceWorkerContainer::jobDidFinish(ServiceWorkerJob& job)
    185216{
  • trunk/Source/WebCore/workers/service/ServiceWorkerContainer.h

    r221392 r221461  
    7474    void jobFailedWithException(ServiceWorkerJob&, const Exception&) final;
    7575    void jobResolvedWithRegistration(ServiceWorkerJob&, const ServiceWorkerRegistrationData&) final;
     76    void startScriptFetchForJob(ServiceWorkerJob&) final;
     77    void jobFinishedLoadingScript(ServiceWorkerJob&, Ref<SharedBuffer>&&) final;
     78    void jobFailedLoadingScript(ServiceWorkerJob&, const ResourceError&) final;
     79
    7680    void jobDidFinish(ServiceWorkerJob&);
    7781
  • trunk/Source/WebCore/workers/service/ServiceWorkerFetchResult.h

    r221460 r221461  
    2828#if ENABLE(SERVICE_WORKER)
    2929
    30 #include "ServiceWorkerJob.h"
    31 #include <wtf/HashMap.h>
    32 #include <wtf/ThreadSafeRefCounted.h>
     30#include "ResourceError.h"
     31#include "ServiceWorkerRegistrationKey.h"
     32#include <wtf/Vector.h>
    3333
    3434namespace WebCore {
    3535
    36 struct ExceptionData;
    37 struct ServiceWorkerRegistrationData;
     36struct ServiceWorkerFetchResult {
     37    uint64_t jobIdentifier;
     38    uint64_t connectionIdentifier;
     39    ServiceWorkerRegistrationKey registrationKey;
     40    Vector<uint8_t> scriptData;
     41    ResourceError scriptError;
    3842
    39 class SWClientConnection : public ThreadSafeRefCounted<SWClientConnection> {
    40 public:
    41     WEBCORE_EXPORT SWClientConnection();
    42     WEBCORE_EXPORT virtual ~SWClientConnection();
     43    template<class Encoder> void encode(Encoder&) const;
     44    template<class Decoder> static bool decode(Decoder&, ServiceWorkerFetchResult&);
     45};
    4346
    44     void scheduleJob(ServiceWorkerJob&);
     47template<class Encoder>
     48void ServiceWorkerFetchResult::encode(Encoder& encoder) const
     49{
     50    encoder << jobIdentifier << connectionIdentifier << registrationKey << scriptData << scriptError;
     51}
    4552
    46     virtual uint64_t identifier() const = 0;
     53template<class Decoder>
     54bool ServiceWorkerFetchResult::decode(Decoder& decoder, ServiceWorkerFetchResult& result)
     55{
     56    if (!decoder.decode(result.jobIdentifier))
     57        return false;
     58    if (!decoder.decode(result.connectionIdentifier))
     59        return false;
     60    if (!decoder.decode(result.registrationKey))
     61        return false;
     62    if (!decoder.decode(result.scriptData))
     63        return false;
     64    if (!decoder.decode(result.scriptError))
     65        return false;
    4766
    48 protected:
    49     WEBCORE_EXPORT void jobRejectedInServer(uint64_t jobIdentifier, const ExceptionData&);
    50     WEBCORE_EXPORT void jobResolvedInServer(uint64_t jobIdentifier, const ServiceWorkerRegistrationData&);
    51 
    52 private:
    53     virtual void scheduleJobInServer(const ServiceWorkerJobData&) = 0;
    54 
    55     HashMap<uint64_t, RefPtr<ServiceWorkerJob>> m_scheduledJobs;
    56 };
     67    return true;
     68}
    5769
    5870} // namespace WebCore
  • trunk/Source/WebCore/workers/service/ServiceWorkerJob.cpp

    r221392 r221461  
    2929#if ENABLE(SERVICE_WORKER)
    3030
     31#include "FetchHeaders.h"
     32#include "FetchLoader.h"
     33#include "FetchRequest.h"
    3134#include "JSDOMPromiseDeferred.h"
     35#include "ResourceError.h"
     36#include "ResourceResponse.h"
    3237#include "ServiceWorkerJobData.h"
    3338#include "ServiceWorkerRegistration.h"
     
    6570}
    6671
     72void ServiceWorkerJob::startScriptFetch()
     73{
     74    ASSERT(currentThread() == m_creationThread);
     75    ASSERT(!m_completed);
     76
     77    m_client->startScriptFetchForJob(*this);
     78}
     79
     80void ServiceWorkerJob::fetchScriptWithContext(ScriptExecutionContext& context)
     81{
     82    ASSERT(currentThread() == m_creationThread);
     83    ASSERT(!m_completed);
     84
     85    m_fetchLoader = std::make_unique<FetchLoader>(*this, nullptr);
     86
     87    // FIXME: This Fetch request is set up incorrectly and without proper care.
     88    // The ServiceWorkers spec specifics many details to apply here.
     89
     90    auto fetchHeaders = FetchHeaders::create();
     91    auto referrer = m_jobData.clientCreationURL.string();
     92    auto fetchRequest = FetchRequest::create(context, std::nullopt, WTFMove(fetchHeaders), { m_jobData.scriptURL }, { }, WTFMove(referrer));
     93
     94    m_fetchLoader->start(context, fetchRequest);
     95}
     96
     97void ServiceWorkerJob::didReceiveResponse(const ResourceResponse& response)
     98{
     99    ASSERT(currentThread() == m_creationThread);
     100    ASSERT(!m_completed);
     101    ASSERT(m_fetchLoader);
     102    ASSERT(!m_scriptData);
     103
     104    m_lastResponse = response;
     105
     106    if (response.httpStatusCode() < 200 || response.httpStatusCode() > 399) {
     107        auto message = makeString(ASCIILiteral("ServiceWorker script network fetch failed with HTTP status code "), String::number(response.httpStatusCode()));
     108        didFail({ errorDomainWebKitInternal, 0, m_lastResponse.url(), message });
     109    }
     110}
     111
     112void ServiceWorkerJob::didReceiveData(const char* data, size_t size)
     113{
     114    ASSERT(currentThread() == m_creationThread);
     115    ASSERT(!m_completed);
     116    ASSERT(m_fetchLoader);
     117
     118    if (!m_scriptData)
     119        m_scriptData = SharedBuffer::create();
     120
     121    m_scriptData->get().append(data, size);
     122}
     123
     124void ServiceWorkerJob::didSucceed()
     125{
     126    ASSERT(currentThread() == m_creationThread);
     127    ASSERT(!m_completed);
     128    ASSERT(m_fetchLoader);
     129
     130    if (!m_scriptData)
     131        m_scriptData = SharedBuffer::create();
     132    m_client->jobFinishedLoadingScript(*this, WTFMove(*m_scriptData));
     133    m_fetchLoader = nullptr;
     134}
     135
     136void ServiceWorkerJob::didFail(const ResourceError& error)
     137{
     138    ASSERT(currentThread() == m_creationThread);
     139    ASSERT(!m_completed);
     140    ASSERT(m_fetchLoader);
     141
     142    m_client->jobFailedLoadingScript(*this, error);
     143    m_fetchLoader = nullptr;
     144}
     145
    67146} // namespace WebCore
    68147
  • trunk/Source/WebCore/workers/service/ServiceWorkerJob.h

    r221392 r221461  
    2828#if ENABLE(SERVICE_WORKER)
    2929
     30#include "FetchLoader.h"
     31#include "FetchLoaderClient.h"
     32#include "ResourceResponse.h"
    3033#include "ServiceWorkerJobClient.h"
    3134#include "ServiceWorkerJobData.h"
     
    3942class DeferredPromise;
    4043class Exception;
     44class ScriptExecutionContext;
    4145enum class ServiceWorkerJobType;
    4246struct ServiceWorkerRegistrationData;
    4347
    44 class ServiceWorkerJob : public ThreadSafeRefCounted<ServiceWorkerJob> {
     48class ServiceWorkerJob : public ThreadSafeRefCounted<ServiceWorkerJob>, public FetchLoaderClient {
    4549public:
    4650    static Ref<ServiceWorkerJob> create(ServiceWorkerJobClient& client, Ref<DeferredPromise>&& promise, ServiceWorkerJobData&& jobData)
     
    5357    void failedWithException(const Exception&);
    5458    void resolvedWithRegistration(const ServiceWorkerRegistrationData&);
     59    void startScriptFetch();
    5560
    5661    ServiceWorkerJobData data() const { return m_jobData; }
    5762    DeferredPromise& promise() { return m_promise.get(); }
    5863
     64    void fetchScriptWithContext(ScriptExecutionContext&);
     65
    5966private:
    6067    ServiceWorkerJob(ServiceWorkerJobClient&, Ref<DeferredPromise>&&, ServiceWorkerJobData&&);
     68
     69    // FetchLoaderClient
     70    void didReceiveResponse(const ResourceResponse&) final;
     71    void didReceiveData(const char*, size_t) final;
     72    void didSucceed() final;
     73    void didFail(const ResourceError&) final;
    6174
    6275    Ref<ServiceWorkerJobClient> m_client;
     
    6780
    6881    Ref<RunLoop> m_runLoop { RunLoop::current() };
     82    std::unique_ptr<FetchLoader> m_fetchLoader;
     83    ResourceResponse m_lastResponse;
     84    std::optional<Ref<SharedBuffer>> m_scriptData;
    6985
    7086#if !ASSERT_DISABLED
  • trunk/Source/WebCore/workers/service/ServiceWorkerJobClient.h

    r221392 r221461  
    3131
    3232class Exception;
     33class ResourceError;
    3334class ServiceWorkerJob;
     35class SharedBuffer;
    3436struct ServiceWorkerRegistrationData;
    3537
     
    4042    virtual void jobFailedWithException(ServiceWorkerJob&, const Exception&) = 0;
    4143    virtual void jobResolvedWithRegistration(ServiceWorkerJob&, const ServiceWorkerRegistrationData&) = 0;
     44    virtual void startScriptFetchForJob(ServiceWorkerJob&) = 0;
     45    virtual void jobFinishedLoadingScript(ServiceWorkerJob&, Ref<SharedBuffer>&&) = 0;
     46    virtual void jobFailedLoadingScript(ServiceWorkerJob&, const ResourceError&) = 0;
    4247
    4348    virtual uint64_t connectionIdentifier() = 0;
  • trunk/Source/WebCore/workers/service/server/SWClientConnection.cpp

    r221392 r221461  
    3030
    3131#include "ExceptionData.h"
     32#include "ServiceWorkerFetchResult.h"
    3233#include "ServiceWorkerJobData.h"
    3334
     
    4849
    4950    scheduleJobInServer(job.data());
     51}
     52
     53void SWClientConnection::finishedFetchingScript(ServiceWorkerJob& job, SharedBuffer& data)
     54{
     55    ASSERT(m_scheduledJobs.get(job.data().identifier()) == &job);
     56
     57    Vector<uint8_t> vector;
     58    vector.append(reinterpret_cast<const uint8_t *>(data.data()), data.size());
     59    finishFetchingScriptInServer({ job.data().identifier(), job.data().connectionIdentifier(), job.data().registrationKey(), vector, { } });
     60}
     61
     62void SWClientConnection::failedFetchingScript(ServiceWorkerJob& job, const ResourceError& error)
     63{
     64    ASSERT(m_scheduledJobs.get(job.data().identifier()) == &job);
     65
     66    finishFetchingScriptInServer({ job.data().identifier(), job.data().connectionIdentifier(), job.data().registrationKey(), { }, error });
    5067}
    5168
     
    7289}
    7390
     91void SWClientConnection::startScriptFetchForServer(uint64_t jobIdentifier)
     92{
     93    auto job = m_scheduledJobs.get(jobIdentifier);
     94    if (!job) {
     95        LOG_ERROR("Job %" PRIu64 " instructed to start fetch from server, but job was not found", jobIdentifier);
     96
     97        // FIXME: Should message back to the server here to signal failure to fetch,
     98        // but we currently need the registration key to do so, and don't have it here.
     99        // In the future we'll refactor to have a global, cross-process job identifier that can be used to overcome this.
     100
     101        return;
     102    }
     103
     104    job->startScriptFetch();
     105}
     106
    74107} // namespace WebCore
    75108
  • trunk/Source/WebCore/workers/service/server/SWClientConnection.h

    r221392 r221461  
    3434namespace WebCore {
    3535
     36class ResourceError;
     37class SharedBuffer;
    3638struct ExceptionData;
     39struct ServiceWorkerFetchResult;
    3740struct ServiceWorkerRegistrationData;
    3841
     
    4346
    4447    void scheduleJob(ServiceWorkerJob&);
     48    void finishedFetchingScript(ServiceWorkerJob&, SharedBuffer&);
     49    void failedFetchingScript(ServiceWorkerJob&, const ResourceError&);
    4550
    4651    virtual uint64_t identifier() const = 0;
     
    4954    WEBCORE_EXPORT void jobRejectedInServer(uint64_t jobIdentifier, const ExceptionData&);
    5055    WEBCORE_EXPORT void jobResolvedInServer(uint64_t jobIdentifier, const ServiceWorkerRegistrationData&);
     56    WEBCORE_EXPORT void startScriptFetchForServer(uint64_t jobIdentifier);
    5157
    5258private:
    5359    virtual void scheduleJobInServer(const ServiceWorkerJobData&) = 0;
     60    virtual void finishFetchingScriptInServer(const ServiceWorkerFetchResult&) = 0;
    5461
    5562    HashMap<uint64_t, RefPtr<ServiceWorkerJob>> m_scheduledJobs;
  • trunk/Source/WebCore/workers/service/server/SWServer.cpp

    r221399 r221461  
    3333#include "Logging.h"
    3434#include "SWServerRegistration.h"
     35#include "ServiceWorkerFetchResult.h"
    3536#include "ServiceWorkerJobData.h"
    3637#include <wtf/text/WTFString.h>
     
    7374}
    7475
     76void SWServer::Connection::finishFetchingScriptInServer(const ServiceWorkerFetchResult& result)
     77{
     78    m_server.scriptFetchFinished(result);
     79}
     80
    7581SWServer::SWServer()
    7682{
     
    111117
    112118    connection->resolveJobInClient(jobData.identifier(), registrationData);
     119}
     120
     121void SWServer::startScriptFetch(const ServiceWorkerJobData& jobData)
     122{
     123    LOG(ServiceWorker, "Server issuing startScriptFetch for current job %" PRIu64 "-%" PRIu64 " in client", jobData.connectionIdentifier(), jobData.identifier());
     124    auto* connection = m_connections.get(jobData.connectionIdentifier());
     125    if (!connection)
     126        return;
     127
     128    connection->startScriptFetchInClient(jobData.identifier());
     129}
     130
     131void SWServer::scriptFetchFinished(const ServiceWorkerFetchResult& result)
     132{
     133    LOG(ServiceWorker, "Server handling scriptFetchFinished for current job %" PRIu64 "-%" PRIu64 " in client", result.connectionIdentifier, result.jobIdentifier);
     134
     135    ASSERT(m_connections.contains(result.connectionIdentifier));
     136
     137    auto registration = m_registrations.get(result.registrationKey);
     138    if (!registration)
     139        return;
     140
     141    registration->scriptFetchFinished(result);
    113142}
    114143
  • trunk/Source/WebCore/workers/service/server/SWServer.h

    r221399 r221461  
    4444class SWServerRegistration;
    4545struct ExceptionData;
     46struct ServiceWorkerFetchResult;
    4647struct ServiceWorkerRegistrationData;
    4748
     
    5859
    5960        WEBCORE_EXPORT void scheduleJobInServer(const ServiceWorkerJobData&);
     61        WEBCORE_EXPORT void finishFetchingScriptInServer(const ServiceWorkerFetchResult&);
    6062
    6163    private:
    6264        virtual void rejectJobInClient(uint64_t jobIdentifier, const ExceptionData&) = 0;
    6365        virtual void resolveJobInClient(uint64_t jobIdentifier, const ServiceWorkerRegistrationData&) = 0;
     66        virtual void startScriptFetchInClient(uint64_t jobIdentifier) = 0;
    6467
    6568        SWServer& m_server;
     
    7275    void rejectJob(const ServiceWorkerJobData&, const ExceptionData&);
    7376    void resolveJob(const ServiceWorkerJobData&, const ServiceWorkerRegistrationData&);
     77    void startScriptFetch(const ServiceWorkerJobData&);
     78
    7479    void postTask(CrossThreadTask&&);
    7580    void postTaskReply(CrossThreadTask&&);
     
    8186    void taskThreadEntryPoint();
    8287    void handleTaskRepliesOnMainThread();
     88
     89    void scriptFetchFinished(const ServiceWorkerFetchResult&);
    8390
    8491    HashMap<uint64_t, Connection*> m_connections;
  • trunk/Source/WebCore/workers/service/server/SWServerRegistration.cpp

    r221399 r221461  
    3333#include "SWServerWorker.h"
    3434#include "SecurityOrigin.h"
     35#include "ServiceWorkerFetchResult.h"
    3536#include "ServiceWorkerRegistrationData.h"
    3637#include "WorkerType.h"
     
    6263    if (!m_jobTimer.isActive())
    6364        m_jobTimer.startOneShot(0_s);
     65}
     66
     67void SWServerRegistration::scriptFetchFinished(const ServiceWorkerFetchResult& result)
     68{
     69    ASSERT(m_currentJob && m_currentJob->identifier() == result.jobIdentifier);
     70
     71    // FIXME: We fetched the script contents but don't do anything with them yet.
     72    // These errors are for testing the current state of the feature.
     73
     74    String message;
     75    if (result.scriptError.isNull())
     76        message = makeString("Script URL ", m_currentJob->scriptURL.string(), " fetched with ", String::number(result.scriptData.size()), " bytes, but we're not using the result yet");
     77    else
     78        message = makeString("Script URL ", m_currentJob->scriptURL.string(), " fetch resulted in error: ", result.scriptError.localizedDescription());
     79
     80    rejectCurrentJob(ExceptionData { UnknownError, message });
    6481}
    6582
     
    150167        return rejectWithExceptionOnMainThread(ExceptionData { TypeError, ASCIILiteral("Cannot update a service worker with a requested script URL whose newest worker has a different script URL") });
    151168
    152     // FIXME: At this point we are ready to actually fetch the script for the worker in the registering context.
    153     // For now we're still hard coding the same rejection we have so far.
    154     rejectWithExceptionOnMainThread(ExceptionData { UnknownError, ASCIILiteral("serviceWorker job scheduling is not yet implemented") });
     169    startScriptFetchFromMainThread();
    155170}
    156171
     
    167182}
    168183
     184void SWServerRegistration::startScriptFetchFromMainThread()
     185{
     186    ASSERT(!isMainThread());
     187    m_server.postTaskReply(createCrossThreadTask(*this, &SWServerRegistration::startScriptFetchForCurrentJob));
     188}
     189
    169190void SWServerRegistration::rejectCurrentJob(const ExceptionData& exceptionData)
    170191{
     
    185206
    186207    finishCurrentJob();
     208}
     209
     210void SWServerRegistration::startScriptFetchForCurrentJob()
     211{
     212    ASSERT(isMainThread());
     213    ASSERT(m_currentJob);
     214
     215    m_server.startScriptFetch(*m_currentJob);
    187216}
    188217
  • trunk/Source/WebCore/workers/service/server/SWServerRegistration.h

    r221399 r221461  
    3939class SWServerWorker;
    4040struct ExceptionData;
     41struct ServiceWorkerFetchResult;
    4142
    4243class SWServerRegistration : public ThreadSafeIdentified<SWServerRegistration> {
     
    4748
    4849    void enqueueJob(const ServiceWorkerJobData&);
     50    void scriptFetchFinished(const ServiceWorkerFetchResult&);
    4951
    5052    ServiceWorkerRegistrationData data() const;
     
    5557    void rejectCurrentJob(const ExceptionData&);
    5658    void resolveCurrentJob(const ServiceWorkerRegistrationData&);
     59    void startScriptFetchForCurrentJob();
    5760    void finishCurrentJob();
    5861
     
    6265    void rejectWithExceptionOnMainThread(const ExceptionData&);
    6366    void resolveWithRegistrationOnMainThread();
     67    void startScriptFetchFromMainThread();
    6468    bool isEmpty();
    6569    SWServerWorker* getNewestWorker();
  • trunk/Source/WebKit/ChangeLog

    r221457 r221461  
     12017-08-31  Brady Eidson  <beidson@apple.com>
     2
     3        Add (entirely incorrect) fetching of ServiceWorker scripts.
     4        https://bugs.webkit.org/show_bug.cgi?id=176179
     5
     6        Reviewed by Andy Estes.
     7
     8        * StorageProcess/ServiceWorker/WebSWServerConnection.cpp:
     9        (WebKit::WebSWServerConnection::startScriptFetchInClient):
     10        * StorageProcess/ServiceWorker/WebSWServerConnection.h:
     11        * StorageProcess/ServiceWorker/WebSWServerConnection.messages.in:
     12
     13        * WebProcess/Storage/WebSWClientConnection.cpp:
     14        (WebKit::WebSWClientConnection::finishFetchingScriptInServer):
     15        * WebProcess/Storage/WebSWClientConnection.h:
     16        * WebProcess/Storage/WebSWClientConnection.messages.in:
     17
    1182017-08-31  Megan Gardner  <megan_gardner@apple.com>
    219
  • trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.cpp

    r221392 r221461  
    2929#if ENABLE(SERVICE_WORKER)
    3030
     31#include "DataReference.h"
    3132#include "Logging.h"
    3233#include "StorageToWebProcessConnectionMessages.h"
     
    7172}
    7273
     74void WebSWServerConnection::startScriptFetchInClient(uint64_t jobIdentifier)
     75{
     76    send(Messages::WebSWClientConnection::StartScriptFetchForServer(jobIdentifier));
     77}
     78
    7379} // namespace WebKit
    7480
  • trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.h

    r221392 r221461  
    5252    void rejectJobInClient(uint64_t jobIdentifier, const WebCore::ExceptionData&) final;
    5353    void resolveJobInClient(uint64_t jobIdentifier, const WebCore::ServiceWorkerRegistrationData&) final;
     54    void startScriptFetchInClient(uint64_t jobIdentifier) final;
    5455
    5556    IPC::Connection* messageSenderConnection() final { return m_connection.ptr(); }
  • trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.messages.in

    r220977 r221461  
    2626    # When possible, these messages can be implemented directly by WebCore::SWClientConnection
    2727    ScheduleJobInServer(struct WebCore::ServiceWorkerJobData jobData)
     28    FinishFetchingScriptInServer(struct WebCore::ServiceWorkerFetchResult result)
    2829}
    2930
  • trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp

    r220977 r221461  
    3131#include "Logging.h"
    3232#include "StorageToWebProcessConnectionMessages.h"
     33#include "WebCoreArgumentCoders.h"
    3334#include "WebSWServerConnectionMessages.h"
     35#include <WebCore/ServiceWorkerFetchResult.h>
    3436#include <WebCore/ServiceWorkerJobData.h>
    3537
     
    5759}
    5860
     61void WebSWClientConnection::finishFetchingScriptInServer(const ServiceWorkerFetchResult& result)
     62{
     63    send(Messages::WebSWServerConnection::FinishFetchingScriptInServer(result));
     64}
     65
    5966} // namespace WebKit
    6067
  • trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.h

    r221198 r221461  
    4949
    5050    void scheduleJobInServer(const WebCore::ServiceWorkerJobData&) final;
     51    void finishFetchingScriptInServer(const WebCore::ServiceWorkerFetchResult&) final;
    5152
    5253    void disconnectedFromWebProcess();
  • trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.messages.in

    r221392 r221461  
    2727    JobRejectedInServer(uint64_t identifier, struct WebCore::ExceptionData exception)
    2828    JobResolvedInServer(uint64_t identifier, struct WebCore::ServiceWorkerRegistrationData registration)
     29    StartScriptFetchForServer(uint64_t jobIdentifier)
    2930}
    3031
Note: See TracChangeset for help on using the changeset viewer.