Implement "UpdateWorkerState" and use it
[WebKit-https.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     typedef WebCore::RegistrationOptions RegistrationOptions;
57
58     ServiceWorker* controller() const;
59
60     using ReadyPromise = DOMPromiseProxy<IDLInterface<ServiceWorkerRegistration>>;
61     ReadyPromise& ready() { return m_readyPromise; }
62
63     void addRegistration(const String& scriptURL, const RegistrationOptions&, Ref<DeferredPromise>&&);
64     void removeRegistration(const URL& scopeURL, Ref<DeferredPromise>&&);
65     void updateRegistration(const URL& scopeURL, const URL& scriptURL, WorkerType, Ref<DeferredPromise>&&);
66
67     void getRegistration(const String& clientURL, Ref<DeferredPromise>&&);
68     void updateRegistrationState(const ServiceWorkerRegistrationKey&, ServiceWorkerRegistrationState, const std::optional<ServiceWorkerIdentifier>&);
69     void fireUpdateFoundEvent(const ServiceWorkerRegistrationKey&);
70     void firePostInstallEvents(const ServiceWorkerRegistrationKey&);
71
72     using RegistrationsPromise = DOMPromiseDeferred<IDLSequence<IDLInterface<ServiceWorkerRegistration>>>;
73     void getRegistrations(RegistrationsPromise&&);
74
75     void addRegistration(ServiceWorkerRegistration& registration) { m_registrations.add(registration.data().key, &registration); }
76     void removeRegistration(ServiceWorkerRegistration& registration)  { m_registrations.remove(registration.data().key); }
77
78     void startMessages();
79
80     void ref() final { refEventTarget(); }
81     void deref() final { derefEventTarget(); }
82
83     bool isStopped() const { return m_isStopped; };
84
85 private:
86     void scheduleJob(Ref<ServiceWorkerJob>&&);
87
88     void jobFailedWithException(ServiceWorkerJob&, const Exception&) final;
89     void jobResolvedWithRegistration(ServiceWorkerJob&, ServiceWorkerRegistrationData&&) final;
90     void jobResolvedWithUnregistrationResult(ServiceWorkerJob&, bool unregistrationResult) final;
91     void startScriptFetchForJob(ServiceWorkerJob&) final;
92     void jobFinishedLoadingScript(ServiceWorkerJob&, const String&) final;
93     void jobFailedLoadingScript(ServiceWorkerJob&, const ResourceError&, std::optional<Exception>&&) final;
94
95     void jobDidFinish(ServiceWorkerJob&);
96
97     uint64_t connectionIdentifier() final;
98
99     const char* activeDOMObjectName() const final;
100     bool canSuspendForDocumentSuspension() const final;
101     ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); }
102     EventTargetInterface eventTargetInterface() const final { return ServiceWorkerContainerEventTargetInterfaceType; }
103     void refEventTarget() final;
104     void derefEventTarget() final;
105     void stop() final { m_isStopped = true; }
106
107     ReadyPromise m_readyPromise;
108
109     NavigatorBase& m_navigator;
110
111     RefPtr<SWClientConnection> m_swConnection;
112     HashMap<uint64_t, Ref<ServiceWorkerJob>> m_jobMap;
113
114     bool m_isStopped { false };
115     HashMap<ServiceWorkerRegistrationKey, ServiceWorkerRegistration*> m_registrations;
116
117 #ifndef NDEBUG
118     ThreadIdentifier m_creationThread { currentThread() };
119 #endif
120 };
121
122 } // namespace WebCore
123
124 #endif // ENABLE(SERVICE_WORKER)