2d9a9f046abb4acef0361798f63f16188d827d00
[WebKit.git] / Source / WebCore / workers / service / ServiceWorkerContainer.h
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 "ActiveDOMObject.h"
31 #include "DOMPromiseProxy.h"
32 #include "EventTarget.h"
33 #include "SWClientConnection.h"
34 #include "SWServer.h"
35 #include "ServiceWorkerJobClient.h"
36 #include "ServiceWorkerRegistration.h"
37 #include "ServiceWorkerRegistrationOptions.h"
38 #include "WorkerType.h"
39 #include <pal/SessionID.h>
40 #include <wtf/Threading.h>
41
42 namespace WebCore {
43
44 class DeferredPromise;
45 class NavigatorBase;
46 class ServiceWorker;
47
48 enum class ServiceWorkerUpdateViaCache;
49 enum class WorkerType;
50
51 class ServiceWorkerContainer final : public EventTargetWithInlineData, public ActiveDOMObject, public ServiceWorkerJobClient {
52 public:
53     ServiceWorkerContainer(ScriptExecutionContext&, NavigatorBase&);
54     ~ServiceWorkerContainer();
55
56     ServiceWorker* controller() const;
57
58     using ReadyPromise = DOMPromiseProxy<IDLInterface<ServiceWorkerRegistration>>;
59     ReadyPromise& ready() { return m_readyPromise; }
60
61     using RegistrationOptions = ServiceWorkerRegistrationOptions;
62     void addRegistration(const String& scriptURL, const RegistrationOptions&, Ref<DeferredPromise>&&);
63     void removeRegistration(const URL& scopeURL, Ref<DeferredPromise>&&);
64     void updateRegistration(const URL& scopeURL, const URL& scriptURL, WorkerType, Ref<DeferredPromise>&&);
65
66     void getRegistration(const String& clientURL, Ref<DeferredPromise>&&);
67     void scheduleTaskToUpdateRegistrationState(ServiceWorkerRegistrationIdentifier, ServiceWorkerRegistrationState, const std::optional<ServiceWorkerData>&);
68     void scheduleTaskToFireUpdateFoundEvent(ServiceWorkerRegistrationIdentifier);
69     void scheduleTaskToFireControllerChangeEvent();
70
71     using RegistrationsPromise = DOMPromiseDeferred<IDLSequence<IDLInterface<ServiceWorkerRegistration>>>;
72     void getRegistrations(RegistrationsPromise&&);
73
74     ServiceWorkerRegistration* registration(ServiceWorkerRegistrationIdentifier identifier) const { return m_registrations.get(identifier); }
75
76     void addRegistration(ServiceWorkerRegistration&);
77     void removeRegistration(ServiceWorkerRegistration&);
78
79     void startMessages();
80
81     void ref() final { refEventTarget(); }
82     void deref() final { derefEventTarget(); }
83
84     bool isStopped() const { return m_isStopped; };
85
86 private:
87     void scheduleJob(Ref<ServiceWorkerJob>&&);
88
89     void jobFailedWithException(ServiceWorkerJob&, const Exception&) final;
90     void jobResolvedWithRegistration(ServiceWorkerJob&, ServiceWorkerRegistrationData&&, ShouldNotifyWhenResolved) final;
91     void jobResolvedWithUnregistrationResult(ServiceWorkerJob&, bool unregistrationResult) final;
92     void startScriptFetchForJob(ServiceWorkerJob&) final;
93     void jobFinishedLoadingScript(ServiceWorkerJob&, const String&) final;
94     void jobFailedLoadingScript(ServiceWorkerJob&, const ResourceError&, std::optional<Exception>&&) final;
95
96     void jobDidFinish(ServiceWorkerJob&);
97
98     SWServerConnectionIdentifier connectionIdentifier() final;
99     DocumentOrWorkerIdentifier contextIdentifier() final;
100
101     SWClientConnection& ensureSWClientConnection();
102
103     const char* activeDOMObjectName() const final;
104     bool canSuspendForDocumentSuspension() const final;
105     ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); }
106     EventTargetInterface eventTargetInterface() const final { return ServiceWorkerContainerEventTargetInterfaceType; }
107     void refEventTarget() final;
108     void derefEventTarget() final;
109     void stop() final;
110
111     ReadyPromise m_readyPromise;
112
113     NavigatorBase& m_navigator;
114
115     RefPtr<SWClientConnection> m_swConnection;
116     HashMap<ServiceWorkerJobIdentifier, Ref<ServiceWorkerJob>> m_jobMap;
117
118     bool m_isStopped { false };
119     HashMap<ServiceWorkerRegistrationIdentifier, ServiceWorkerRegistration*> m_registrations;
120
121 #ifndef NDEBUG
122     ThreadIdentifier m_creationThread { currentThread() };
123 #endif
124 };
125
126 } // namespace WebCore
127
128 #endif // ENABLE(SERVICE_WORKER)