2 * Copyright (C) 2017 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #if ENABLE(SERVICE_WORKER)
30 #include "ActiveDOMObject.h"
31 #include "DOMPromiseProxy.h"
32 #include "EventTarget.h"
33 #include "SWClientConnection.h"
35 #include "ServiceWorkerJobClient.h"
36 #include "ServiceWorkerRegistration.h"
37 #include "ServiceWorkerRegistrationOptions.h"
38 #include <pal/SessionID.h>
39 #include <wtf/Threading.h>
43 class DeferredPromise;
47 enum class ServiceWorkerUpdateViaCache;
48 enum class WorkerType;
50 class ServiceWorkerContainer final : public EventTargetWithInlineData, public ActiveDOMObject, public ServiceWorkerJobClient {
52 ServiceWorkerContainer(ScriptExecutionContext&, NavigatorBase&);
53 ~ServiceWorkerContainer();
55 typedef WebCore::RegistrationOptions RegistrationOptions;
57 ServiceWorker* controller() const;
59 using ReadyPromise = DOMPromiseProxy<IDLInterface<ServiceWorkerRegistration>>;
60 ReadyPromise& ready() { return m_readyPromise; }
62 void addRegistration(const String& scriptURL, const RegistrationOptions&, Ref<DeferredPromise>&&);
63 void removeRegistration(const URL& scopeURL, Ref<DeferredPromise>&&);
64 void getRegistration(const String& url, Ref<DeferredPromise>&&);
65 void getRegistrations(Ref<DeferredPromise>&&);
69 void ref() final { refEventTarget(); }
70 void deref() final { derefEventTarget(); }
73 void scheduleJob(Ref<ServiceWorkerJob>&&);
75 void jobFailedWithException(ServiceWorkerJob&, const Exception&) final;
76 void jobResolvedWithRegistration(ServiceWorkerJob&, ServiceWorkerRegistrationData&&) final;
77 void jobResolvedWithUnregistrationResult(ServiceWorkerJob&, bool unregistrationResult) final;
78 void startScriptFetchForJob(ServiceWorkerJob&) final;
79 void jobFinishedLoadingScript(ServiceWorkerJob&, const String&) final;
80 void jobFailedLoadingScript(ServiceWorkerJob&, const ResourceError&) final;
82 void jobDidFinish(ServiceWorkerJob&);
84 uint64_t connectionIdentifier() final;
86 const char* activeDOMObjectName() const final;
87 bool canSuspendForDocumentSuspension() const final;
88 ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); }
89 EventTargetInterface eventTargetInterface() const final { return ServiceWorkerContainerEventTargetInterfaceType; }
90 void refEventTarget() final;
91 void derefEventTarget() final;
93 ReadyPromise m_readyPromise;
95 NavigatorBase& m_navigator;
97 RefPtr<SWClientConnection> m_swConnection;
98 HashMap<uint64_t, RefPtr<ServiceWorkerJob>> m_jobMap;
99 mutable RefPtr<ServiceWorker> m_controller;
102 ThreadIdentifier m_creationThread { currentThread() };
106 } // namespace WebCore
108 #endif // ENABLE(SERVICE_WORKER)