When managing context startups, make ServiceWorkerJobDataIdentifier's optional.
[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 ServiceWorkerContextData;
59 struct ServiceWorkerFetchResult;
60 struct ServiceWorkerRegistrationData;
61
62 class SWServer {
63 public:
64     class Connection {
65     friend class SWServer;
66     public:
67         WEBCORE_EXPORT virtual ~Connection();
68
69         using Identifier = SWServerConnectionIdentifier;
70         Identifier identifier() const { return m_identifier; }
71
72         WEBCORE_EXPORT void didResolveRegistrationPromise(const ServiceWorkerRegistrationKey&);
73         const SWServerRegistration* doRegistrationMatching(const SecurityOriginData& topOrigin, const URL& clientURL) const { return m_server.doRegistrationMatching(topOrigin, clientURL); }
74
75         // Messages to the client WebProcess
76         virtual void updateRegistrationStateInClient(ServiceWorkerRegistrationIdentifier, ServiceWorkerRegistrationState, const std::optional<ServiceWorkerData>&) = 0;
77         virtual void updateWorkerStateInClient(ServiceWorkerIdentifier, ServiceWorkerState) = 0;
78         virtual void fireUpdateFoundEvent(ServiceWorkerRegistrationIdentifier) = 0;
79         virtual void notifyClientsOfControllerChange(const HashSet<DocumentIdentifier>& contextIdentifiers, const ServiceWorkerData& newController) = 0;
80
81     protected:
82         WEBCORE_EXPORT explicit Connection(SWServer&);
83         SWServer& server() { return m_server; }
84
85         WEBCORE_EXPORT void scheduleJobInServer(const ServiceWorkerJobData&);
86         WEBCORE_EXPORT void finishFetchingScriptInServer(const ServiceWorkerFetchResult&);
87         WEBCORE_EXPORT void addServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier);
88         WEBCORE_EXPORT void removeServiceWorkerRegistrationInServer(ServiceWorkerRegistrationIdentifier);
89         WEBCORE_EXPORT void serviceWorkerStartedControllingClient(ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier);
90         WEBCORE_EXPORT void serviceWorkerStoppedControllingClient(ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier);
91
92     private:
93         // Messages to the client WebProcess
94         virtual void rejectJobInClient(const ServiceWorkerJobDataIdentifier&, const ExceptionData&) = 0;
95         virtual void resolveRegistrationJobInClient(const ServiceWorkerJobDataIdentifier&, const ServiceWorkerRegistrationData&, ShouldNotifyWhenResolved) = 0;
96         virtual void resolveUnregistrationJobInClient(const ServiceWorkerJobDataIdentifier&, const ServiceWorkerRegistrationKey&, bool registrationResult) = 0;
97         virtual void startScriptFetchInClient(const ServiceWorkerJobDataIdentifier&) = 0;
98
99         SWServer& m_server;
100         Identifier m_identifier;
101     };
102
103     WEBCORE_EXPORT explicit SWServer(UniqueRef<SWOriginStore>&&);
104     WEBCORE_EXPORT ~SWServer();
105
106     WEBCORE_EXPORT void clearAll();
107     WEBCORE_EXPORT void clear(const SecurityOrigin&);
108
109
110     SWServerRegistration* getRegistration(const ServiceWorkerRegistrationKey&);
111     void addRegistration(std::unique_ptr<SWServerRegistration>&&);
112     void removeRegistration(const ServiceWorkerRegistrationKey&);
113     WEBCORE_EXPORT Vector<ServiceWorkerRegistrationData> getRegistrations(const SecurityOriginData& topOrigin, const URL& clientURL);
114
115     void scheduleJob(const ServiceWorkerJobData&);
116     void rejectJob(const ServiceWorkerJobData&, const ExceptionData&);
117     void resolveRegistrationJob(const ServiceWorkerJobData&, const ServiceWorkerRegistrationData&, ShouldNotifyWhenResolved);
118     void resolveUnregistrationJob(const ServiceWorkerJobData&, const ServiceWorkerRegistrationKey&, bool unregistrationResult);
119     void startScriptFetch(const ServiceWorkerJobData&);
120
121     void postTask(CrossThreadTask&&);
122     void postTaskReply(CrossThreadTask&&);
123
124     void updateWorker(Connection&, const ServiceWorkerJobDataIdentifier&, SWServerRegistration&, const URL&, const String& script, WorkerType);
125     void terminateWorker(SWServerWorker&);
126     void fireInstallEvent(SWServerWorker&);
127     void fireActivateEvent(SWServerWorker&);
128     SWServerWorker* workerByID(ServiceWorkerIdentifier identifier) const { return m_workersByID.get(identifier); }
129     
130     Connection* getConnection(SWServerConnectionIdentifier identifier) { return m_connections.get(identifier); }
131     SWOriginStore& originStore() { return m_originStore; }
132
133     void scriptContextFailedToStart(const std::optional<ServiceWorkerJobDataIdentifier>&, SWServerWorker&, const String& message);
134     void scriptContextStarted(const std::optional<ServiceWorkerJobDataIdentifier>&, SWServerWorker&);
135     void didFinishInstall(const std::optional<ServiceWorkerJobDataIdentifier>&, SWServerWorker&, bool wasSuccessful);
136     void didFinishActivation(SWServerWorker&);
137     void workerContextTerminated(SWServerWorker&);
138
139     WEBCORE_EXPORT void serverToContextConnectionCreated();
140     
141     WEBCORE_EXPORT static HashSet<SWServer*>& allServers();
142
143     WEBCORE_EXPORT void registerServiceWorkerClient(ClientOrigin&&, ServiceWorkerClientIdentifier, ServiceWorkerClientData&&);
144     WEBCORE_EXPORT void unregisterServiceWorkerClient(const ClientOrigin&, ServiceWorkerClientIdentifier);
145
146 private:
147     void registerConnection(Connection&);
148     void unregisterConnection(Connection&);
149
150     void taskThreadEntryPoint();
151     void handleTaskRepliesOnMainThread();
152
153     void scriptFetchFinished(Connection&, const ServiceWorkerFetchResult&);
154
155     void didResolveRegistrationPromise(Connection&, const ServiceWorkerRegistrationKey&);
156
157     void addClientServiceWorkerRegistration(Connection&, ServiceWorkerRegistrationIdentifier);
158     void removeClientServiceWorkerRegistration(Connection&, ServiceWorkerRegistrationIdentifier);
159     void serviceWorkerStartedControllingClient(Connection&, ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier);
160     void serviceWorkerStoppedControllingClient(Connection&, ServiceWorkerIdentifier, ServiceWorkerRegistrationIdentifier, DocumentIdentifier);
161
162     WEBCORE_EXPORT const SWServerRegistration* doRegistrationMatching(const SecurityOriginData& topOrigin, const URL& clientURL) const;
163
164     void installContextData(const ServiceWorkerContextData&);
165
166     HashMap<SWServerConnectionIdentifier, Connection*> m_connections;
167     HashMap<ServiceWorkerRegistrationKey, std::unique_ptr<SWServerRegistration>> m_registrations;
168     HashMap<ServiceWorkerRegistrationIdentifier, SWServerRegistration*> m_registrationsByID;
169     HashMap<ServiceWorkerRegistrationKey, std::unique_ptr<SWServerJobQueue>> m_jobQueues;
170
171     HashMap<ServiceWorkerIdentifier, Ref<SWServerWorker>> m_workersByID;
172
173     struct ClientInformation {
174         ServiceWorkerClientIdentifier identifier;
175         ServiceWorkerClientData data;
176     };
177     HashMap<ClientOrigin, Vector<ClientInformation>> m_clients;
178
179     RefPtr<Thread> m_taskThread;
180     Lock m_taskThreadLock;
181
182     CrossThreadQueue<CrossThreadTask> m_taskQueue;
183     CrossThreadQueue<CrossThreadTask> m_taskReplyQueue;
184
185     Lock m_mainThreadReplyLock;
186     bool m_mainThreadReplyScheduled { false };
187     UniqueRef<SWOriginStore> m_originStore;
188     Deque<ServiceWorkerContextData> m_pendingContextDatas;
189 };
190
191 } // namespace WebCore
192
193 #endif // ENABLE(SERVICE_WORKER)