| 1 | /* |
| 2 | * Copyright (C) 2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #if ENABLE(SERVICE_WORKER) |
| 29 | |
| 30 | #include "JSDOMPromiseDeferred.h" |
| 31 | #include "ResourceResponse.h" |
| 32 | #include "ServiceWorkerJobClient.h" |
| 33 | #include "ServiceWorkerJobData.h" |
| 34 | #include "ServiceWorkerTypes.h" |
| 35 | #include "WorkerScriptLoader.h" |
| 36 | #include "WorkerScriptLoaderClient.h" |
| 37 | #include <wtf/RefPtr.h> |
| 38 | #include <wtf/RunLoop.h> |
| 39 | #include <wtf/ThreadSafeRefCounted.h> |
| 40 | #include <wtf/Threading.h> |
| 41 | |
| 42 | namespace WebCore { |
| 43 | |
| 44 | class DeferredPromise; |
| 45 | class Exception; |
| 46 | class ScriptExecutionContext; |
| 47 | enum class ServiceWorkerJobType; |
| 48 | struct ServiceWorkerRegistrationData; |
| 49 | |
| 50 | class ServiceWorkerJob : public WorkerScriptLoaderClient { |
| 51 | WTF_MAKE_FAST_ALLOCATED; |
| 52 | public: |
| 53 | ServiceWorkerJob(ServiceWorkerJobClient&, RefPtr<DeferredPromise>&&, ServiceWorkerJobData&&); |
| 54 | WEBCORE_EXPORT ~ServiceWorkerJob(); |
| 55 | |
| 56 | void failedWithException(const Exception&); |
| 57 | void resolvedWithRegistration(ServiceWorkerRegistrationData&&, ShouldNotifyWhenResolved); |
| 58 | void resolvedWithUnregistrationResult(bool); |
| 59 | void startScriptFetch(FetchOptions::Cache); |
| 60 | |
| 61 | using Identifier = ServiceWorkerJobIdentifier; |
| 62 | Identifier identifier() const { return m_jobData.identifier().jobIdentifier; } |
| 63 | |
| 64 | const ServiceWorkerJobData& data() const { return m_jobData; } |
| 65 | bool hasPromise() const { return !!m_promise; } |
| 66 | RefPtr<DeferredPromise> takePromise() { return WTFMove(m_promise); } |
| 67 | |
| 68 | void fetchScriptWithContext(ScriptExecutionContext&, FetchOptions::Cache); |
| 69 | |
| 70 | const DocumentOrWorkerIdentifier& contextIdentifier() { return m_contextIdentifier; } |
| 71 | |
| 72 | bool cancelPendingLoad(); |
| 73 | |
| 74 | private: |
| 75 | // WorkerScriptLoaderClient |
| 76 | void didReceiveResponse(unsigned long identifier, const ResourceResponse&) final; |
| 77 | void notifyFinished() final; |
| 78 | |
| 79 | ServiceWorkerJobClient& m_client; |
| 80 | ServiceWorkerJobData m_jobData; |
| 81 | RefPtr<DeferredPromise> m_promise; |
| 82 | |
| 83 | bool m_completed { false }; |
| 84 | |
| 85 | DocumentOrWorkerIdentifier m_contextIdentifier; |
| 86 | RefPtr<WorkerScriptLoader> m_scriptLoader; |
| 87 | |
| 88 | #if !ASSERT_DISABLED |
| 89 | Ref<Thread> m_creationThread { Thread::current() }; |
| 90 | #endif |
| 91 | }; |
| 92 | |
| 93 | } // namespace WebCore |
| 94 | |
| 95 | #endif // ENABLE(SERVICE_WORKER) |
| 96 | |
| 97 | |