Deprecate ActiveDOMObject::canSuspendForDocumentSuspension()
[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 "EventTarget.h"
32 #include "GenericEventQueue.h"
33 #include "IDLTypes.h"
34 #include "SWClientConnection.h"
35 #include "SWServer.h"
36 #include "ServiceWorkerJobClient.h"
37 #include "ServiceWorkerRegistration.h"
38 #include "ServiceWorkerRegistrationOptions.h"
39 #include "SuspendableTaskQueue.h"
40 #include "WorkerType.h"
41 #include <wtf/Threading.h>
42
43 namespace WebCore {
44
45 class DeferredPromise;
46 class NavigatorBase;
47 class ServiceWorker;
48
49 enum class ServiceWorkerUpdateViaCache : uint8_t;
50 enum class WorkerType;
51
52 template<typename IDLType> class DOMPromiseProxy;
53
54 class ServiceWorkerContainer final : public EventTargetWithInlineData, public ActiveDOMObject, public ServiceWorkerJobClient {
55     WTF_MAKE_NONCOPYABLE(ServiceWorkerContainer);
56     WTF_MAKE_ISO_ALLOCATED(ServiceWorkerContainer);
57 public:
58     ServiceWorkerContainer(ScriptExecutionContext*, NavigatorBase&);
59     ~ServiceWorkerContainer();
60
61     ServiceWorker* controller() const;
62
63     using ReadyPromise = DOMPromiseProxy<IDLInterface<ServiceWorkerRegistration>>;
64     ReadyPromise& ready();
65
66     using RegistrationOptions = ServiceWorkerRegistrationOptions;
67     void addRegistration(const String& scriptURL, const RegistrationOptions&, Ref<DeferredPromise>&&);
68     void removeRegistration(const URL& scopeURL, Ref<DeferredPromise>&&);
69     void updateRegistration(const URL& scopeURL, const URL& scriptURL, WorkerType, RefPtr<DeferredPromise>&&);
70
71     void getRegistration(const String& clientURL, Ref<DeferredPromise>&&);
72     void updateRegistrationState(ServiceWorkerRegistrationIdentifier, ServiceWorkerRegistrationState, const Optional<ServiceWorkerData>&);
73     void fireUpdateFoundEvent(ServiceWorkerRegistrationIdentifier);
74     void fireControllerChangeEvent();
75
76     void postMessage(MessageWithMessagePorts&&, ServiceWorkerData&& sourceData, String&& sourceOrigin);
77
78     void getRegistrations(Ref<DeferredPromise>&&);
79
80     ServiceWorkerRegistration* registration(ServiceWorkerRegistrationIdentifier identifier) const { return m_registrations.get(identifier); }
81
82     void addRegistration(ServiceWorkerRegistration&);
83     void removeRegistration(ServiceWorkerRegistration&);
84
85     ServiceWorkerJob* job(ServiceWorkerJobIdentifier);
86
87     void startMessages();
88
89     bool isStopped() const { return m_isStopped; };
90
91     bool isAlwaysOnLoggingAllowed() const;
92
93     NavigatorBase* navigator() { return &m_navigator; }
94
95 private:
96     bool addEventListener(const AtomString& eventType, Ref<EventListener>&&, const AddEventListenerOptions& = { }) final;
97
98     void scheduleJob(std::unique_ptr<ServiceWorkerJob>&&);
99
100     void jobFailedWithException(ServiceWorkerJob&, const Exception&) final;
101     void jobResolvedWithRegistration(ServiceWorkerJob&, ServiceWorkerRegistrationData&&, ShouldNotifyWhenResolved) final;
102     void jobResolvedWithUnregistrationResult(ServiceWorkerJob&, bool unregistrationResult) final;
103     void startScriptFetchForJob(ServiceWorkerJob&, FetchOptions::Cache) final;
104     void jobFinishedLoadingScript(ServiceWorkerJob&, const String& script, const ContentSecurityPolicyResponseHeaders&, const String& referrerPolicy) final;
105     void jobFailedLoadingScript(ServiceWorkerJob&, const ResourceError&, Exception&&) final;
106
107     void notifyFailedFetchingScript(ServiceWorkerJob&, const ResourceError&);
108     void destroyJob(ServiceWorkerJob&);
109
110     DocumentOrWorkerIdentifier contextIdentifier() final;
111
112     SWClientConnection& ensureSWClientConnection();
113
114     // ActiveDOMObject.
115     const char* activeDOMObjectName() const final;
116     
117     ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); }
118     EventTargetInterface eventTargetInterface() const final { return ServiceWorkerContainerEventTargetInterfaceType; }
119     void refEventTarget() final;
120     void derefEventTarget() final;
121     void stop() final;
122
123     void notifyRegistrationIsSettled(const ServiceWorkerRegistrationKey&);
124
125     std::unique_ptr<ReadyPromise> m_readyPromise;
126
127     NavigatorBase& m_navigator;
128
129     RefPtr<SWClientConnection> m_swConnection;
130
131     struct OngoingJob {
132         std::unique_ptr<ServiceWorkerJob> job;
133         RefPtr<PendingActivity<ServiceWorkerContainer>> pendingActivity;
134     };
135     HashMap<ServiceWorkerJobIdentifier, OngoingJob> m_jobMap;
136
137     bool m_isStopped { false };
138     HashMap<ServiceWorkerRegistrationIdentifier, ServiceWorkerRegistration*> m_registrations;
139
140 #ifndef NDEBUG
141     Ref<Thread> m_creationThread { Thread::current() };
142 #endif
143
144     uint64_t m_lastOngoingSettledRegistrationIdentifier { 0 };
145     HashMap<uint64_t, ServiceWorkerRegistrationKey> m_ongoingSettledRegistrations;
146     UniqueRef<GenericEventQueue> m_messageQueue;
147     UniqueRef<SuspendableTaskQueue> m_taskQueue;
148 };
149
150 } // namespace WebCore
151
152 #endif // ENABLE(SERVICE_WORKER)