Call "Terminate Service Worker" on all workers when all their clients are gone
[WebKit-https.git] / Source / WebCore / workers / service / server / SWServer.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 "ClientOrigin.h"
31 #include "DocumentIdentifier.h"
32 #include "SWServerWorker.h"
33 #include "ServiceWorkerClientData.h"
34 #include "ServiceWorkerIdentifier.h"
35 #include "ServiceWorkerJob.h"
36 #include "ServiceWorkerRegistrationData.h"
37 #include "ServiceWorkerRegistrationKey.h"
38 #include "ServiceWorkerTypes.h"
39 #include <wtf/CrossThreadQueue.h>
40 #include <wtf/CrossThreadTask.h>
41 #include <wtf/HashMap.h>
42 #include <wtf/HashSet.h>
43 #include <wtf/ObjectIdentifier.h>
44 #include <wtf/RunLoop.h>
45 #include <wtf/ThreadSafeRefCounted.h>
46 #include <wtf/Threading.h>
47 #include <wtf/UniqueRef.h>
48
49 namespace WebCore {
50
51 class SWOriginStore;
52 class SWServerJobQueue;
53 class SWServerRegistration;
54 class SWServerToContextConnection;
55 enum class ServiceWorkerRegistrationState;
56 enum class ServiceWorkerState;
57 struct ExceptionData;
58 struct ServiceWorkerClientQueryOptions;
59 struct ServiceWorkerContextData;
60 struct ServiceWorkerFetchResult;
61 struct ServiceWorkerRegistrationData;
62 class Timer;
63
64 class SWServer {
65     WTF_MAKE_FAST_ALLOCATED;
66 public:
67     class Connection {
68         WTF_MAKE_FAST_ALLOCATED;
69         friend class SWServer;
70     public:
71         WEBCORE_EXPORT virtual ~Connection();
72
73         using Identifier = SWServerConnectionIdentifier;
74         Identifier identifier() const { return m_identifier; }
75
76         WEBCORE_EXPORT void didResolveRegistrationPromise(const ServiceWorkerRegistrationKey&);
77         const SWServerRegistration* doRegistrationMatching(const SecurityOriginData& topOrigin, const URL& clientURL) const { return m_server.doRegistrationMatching(topOrigin, clientURL); }
78
79         // Messages to the client WebProcess
80         virtual void updateRegistrationStateInClient(ServiceWorkerRegistrationIdentifier, ServiceWorkerRegistrationState, const std::optional<ServiceWorkerData>&) = 0;
81         virtual void updateWorkerStateInClient(ServiceWorkerIdentifier, ServiceWorkerState) = 0;
82         virtual void fireUpdateFoundEvent(ServiceWorkerRegistrationIdentifier) = 0;
83         virtual void notifyClientsOfControllerChange(const HashSet<DocumentIdentifier>& contextIdentifiers, const ServiceWorkerData& newController) = 0;
84
85     protected:
86         WEBCORE_EXPORT explicit Connection(SWServer&);
87         SWServer& server() { return m_server; }
88
89         WEBCORE_EXPORT void scheduleJobInServer(const ServiceWorkerJobData&);
90         WEBCORE_EXPORT void finishFetchingScriptInServer(const ServiceWorkerFetchResult&);
91         WEBCORE_EXPORT void addServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier);
92         WEBCORE_EXPORT void removeServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier);
93         WEBCORE_EXPORT void serviceWorkerStartedControllingClient(ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier);
94         WEBCORE_EXPORT void serviceWorkerStoppedControllingClient(ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier);
95         WEBCORE_EXPORT void syncTerminateWorker(ServiceWorkerIdentifier);
96
97     private:
98         // Messages to the client WebProcess
99         virtual void rejectJobInClient(const ServiceWorkerJobDataIdentifier&, const ExceptionData&) = 0;
100         virtual void resolveRegistrationJobInClient(const ServiceWorkerJobDataIdentifier&, const ServiceWorkerRegistrationData&, ShouldNotifyWhenResolved) = 0;
101         virtual void resolveUnregistrationJobInClient(const ServiceWorkerJobDataIdentifier&, const ServiceWorkerRegistrationKey&, bool registrationResult) = 0;
102         virtual void startScriptFetchInClient(const ServiceWorkerJobDataIdentifier&) = 0;
103
104         SWServer& m_server;
105         Identifier m_identifier;
106     };
107
108     WEBCORE_EXPORT explicit SWServer(UniqueRef<SWOriginStore>&&);
109     WEBCORE_EXPORT ~SWServer();
110
111     WEBCORE_EXPORT void clearAll();
112     WEBCORE_EXPORT void clear(const SecurityOrigin&);
113
114     SWServerRegistration* getRegistration(const ServiceWorkerRegistrationKey&);
115     void addRegistration(std::unique_ptr<SWServerRegistration>&&);
116     void removeRegistration(const ServiceWorkerRegistrationKey&);
117     WEBCORE_EXPORT Vector<ServiceWorkerRegistrationData> getRegistrations(const SecurityOriginData& topOrigin, const URL& clientURL);
118
119     void scheduleJob(const ServiceWorkerJobData&);
120     void rejectJob(const ServiceWorkerJobData&, const ExceptionData&);
121     void resolveRegistrationJob(const ServiceWorkerJobData&, const ServiceWorkerRegistrationData&, ShouldNotifyWhenResolved);
122     void resolveUnregistrationJob(const ServiceWorkerJobData&, const ServiceWorkerRegistrationKey&, bool unregistrationResult);
123     void startScriptFetch(const ServiceWorkerJobData&);
124
125     void postTask(CrossThreadTask&&);
126     void postTaskReply(CrossThreadTask&&);
127
128     void updateWorker(Connection&, const ServiceWorkerJobDataIdentifier&, SWServerRegistration&, const URL&, const String& script, WorkerType);
129     void terminateWorker(SWServerWorker&);
130     void syncTerminateWorker(SWServerWorker&);
131     void fireInstallEvent(SWServerWorker&);
132     void fireActivateEvent(SWServerWorker&);
133     WEBCORE_EXPORT SWServerWorker* workerByID(ServiceWorkerIdentifier) const;
134     
135     Connection* getConnection(SWServerConnectionIdentifier identifier) { return m_connections.get(identifier); }
136     SWOriginStore& originStore() { return m_originStore; }
137
138     void scriptContextFailedToStart(const std::optional<ServiceWorkerJobDataIdentifier>&, SWServerWorker&, const String& message);
139     void scriptContextStarted(const std::optional<ServiceWorkerJobDataIdentifier>&, SWServerWorker&);
140     void didFinishInstall(const std::optional<ServiceWorkerJobDataIdentifier>&, SWServerWorker&, bool wasSuccessful);
141     void didFinishActivation(SWServerWorker&);
142     void workerContextTerminated(SWServerWorker&);
143     std::optional<ServiceWorkerClientData> findClientByIdentifier(const ClientOrigin&, ServiceWorkerClientIdentifier);
144     void matchAll(SWServerWorker&, const ServiceWorkerClientQueryOptions&, ServiceWorkerClientsMatchAllCallback&&);
145
146     WEBCORE_EXPORT void serverToContextConnectionCreated();
147     
148     WEBCORE_EXPORT static HashSet<SWServer*>& allServers();
149
150     WEBCORE_EXPORT void registerServiceWorkerClient(ClientOrigin&&, ServiceWorkerClientIdentifier, ServiceWorkerClientData&&);
151     WEBCORE_EXPORT void unregisterServiceWorkerClient(const ClientOrigin&, ServiceWorkerClientIdentifier);
152
153     WEBCORE_EXPORT bool invokeRunServiceWorker(ServiceWorkerIdentifier);
154
155 private:
156     void registerConnection(Connection&);
157     void unregisterConnection(Connection&);
158
159     void taskThreadEntryPoint();
160     void handleTaskRepliesOnMainThread();
161
162     void scriptFetchFinished(Connection&, const ServiceWorkerFetchResult&);
163
164     void didResolveRegistrationPromise(Connection&, const ServiceWorkerRegistrationKey&);
165
166     void addClientServiceWorkerRegistration(Connection&, ServiceWorkerRegistrationIdentifier);
167     void removeClientServiceWorkerRegistration(Connection&, ServiceWorkerRegistrationIdentifier);
168     void serviceWorkerStartedControllingClient(Connection&, ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier);
169     void serviceWorkerStoppedControllingClient(Connection&, ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier);
170
171     WEBCORE_EXPORT const SWServerRegistration* doRegistrationMatching(const SecurityOriginData& topOrigin, const URL& clientURL) const;
172
173     void installContextData(const ServiceWorkerContextData&);
174
175     enum TerminationMode {
176         Synchronous,
177         Asynchronous,
178     };
179     void terminateWorkerInternal(SWServerWorker&, TerminationMode);
180
181     HashMap<SWServerConnectionIdentifier, Connection*> m_connections;
182     HashMap<ServiceWorkerRegistrationKey, std::unique_ptr<SWServerRegistration>> m_registrations;
183     HashMap<ServiceWorkerRegistrationIdentifier, SWServerRegistration*> m_registrationsByID;
184     HashMap<ServiceWorkerRegistrationKey, std::unique_ptr<SWServerJobQueue>> m_jobQueues;
185
186     HashMap<ServiceWorkerIdentifier, Ref<SWServerWorker>> m_runningOrTerminatingWorkers;
187
188     struct Clients {
189         Vector<ServiceWorkerClientInformation> clients;
190         std::unique_ptr<Timer> terminateServiceWorkersTimer;
191     };
192     HashMap<ClientOrigin, Clients> m_clients;
193     HashMap<ServiceWorkerClientIdentifier, ServiceWorkerIdentifier> m_clientToControllingWorker;
194
195     RefPtr<Thread> m_taskThread;
196     Lock m_taskThreadLock;
197
198     CrossThreadQueue<CrossThreadTask> m_taskQueue;
199     CrossThreadQueue<CrossThreadTask> m_taskReplyQueue;
200
201     Lock m_mainThreadReplyLock;
202     bool m_mainThreadReplyScheduled { false };
203     UniqueRef<SWOriginStore> m_originStore;
204     Deque<ServiceWorkerContextData> m_pendingContextDatas;
205 };
206
207 } // namespace WebCore
208
209 #endif // ENABLE(SERVICE_WORKER)