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