ServiceWorkerRegistration objects may get recycled for different SWServerRegistration...
[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 <wtf/CrossThreadQueue.h>
36 #include <wtf/CrossThreadTask.h>
37 #include <wtf/HashMap.h>
38 #include <wtf/HashSet.h>
39 #include <wtf/Identified.h>
40 #include <wtf/RunLoop.h>
41 #include <wtf/ThreadSafeRefCounted.h>
42 #include <wtf/Threading.h>
43
44 namespace WebCore {
45
46 class SWServerJobQueue;
47 class SWServerRegistration;
48 enum class ServiceWorkerRegistrationState;
49 enum class ServiceWorkerState;
50 struct ExceptionData;
51 struct ServiceWorkerContextData;
52 struct ServiceWorkerFetchResult;
53 struct ServiceWorkerRegistrationData;
54
55 class SWServer {
56 public:
57     class Connection : public Identified<Connection> {
58     friend class SWServer;
59     public:
60         WEBCORE_EXPORT virtual ~Connection();
61
62         WEBCORE_EXPORT void scriptContextFailedToStart(const ServiceWorkerRegistrationKey&, ServiceWorkerIdentifier, const String& message);
63         WEBCORE_EXPORT void scriptContextStarted(const ServiceWorkerRegistrationKey&, ServiceWorkerIdentifier);
64         WEBCORE_EXPORT void didFinishInstall(const ServiceWorkerRegistrationKey&, ServiceWorkerIdentifier, bool wasSuccessful);
65         WEBCORE_EXPORT void didResolveRegistrationPromise(const ServiceWorkerRegistrationKey&);
66         const SWServerRegistration* doRegistrationMatching(const SecurityOriginData& topOrigin, const URL& clientURL) const { return m_server.doRegistrationMatching(topOrigin, clientURL); }
67
68         // Messages to the client WebProcess
69         virtual void updateRegistrationStateInClient(ServiceWorkerRegistrationIdentifier, ServiceWorkerRegistrationState, std::optional<ServiceWorkerIdentifier>) = 0;
70         virtual void updateWorkerStateInClient(ServiceWorkerIdentifier, ServiceWorkerState) = 0;
71         virtual void fireUpdateFoundEvent(ServiceWorkerRegistrationIdentifier) = 0;
72
73     protected:
74         WEBCORE_EXPORT Connection(SWServer&, uint64_t identifier);
75         SWServer& server() { return m_server; }
76
77         WEBCORE_EXPORT void scheduleJobInServer(const ServiceWorkerJobData&);
78         WEBCORE_EXPORT void finishFetchingScriptInServer(const ServiceWorkerFetchResult&);
79         WEBCORE_EXPORT void addServiceWorkerRegistrationInServer(const ServiceWorkerRegistrationKey&, ServiceWorkerRegistrationIdentifier);
80         WEBCORE_EXPORT void removeServiceWorkerRegistrationInServer(const ServiceWorkerRegistrationKey&, ServiceWorkerRegistrationIdentifier);
81
82     private:
83         // Messages to the client WebProcess
84         virtual void rejectJobInClient(uint64_t jobIdentifier, const ExceptionData&) = 0;
85         virtual void resolveRegistrationJobInClient(uint64_t jobIdentifier, const ServiceWorkerRegistrationData&, ShouldNotifyWhenResolved) = 0;
86         virtual void resolveUnregistrationJobInClient(uint64_t jobIdentifier, const ServiceWorkerRegistrationKey&, bool registrationResult) = 0;
87         virtual void startScriptFetchInClient(uint64_t jobIdentifier) = 0;
88
89         // Messages to the SW host WebProcess
90         virtual void installServiceWorkerContext(const ServiceWorkerContextData&) = 0;
91         virtual void fireInstallEvent(ServiceWorkerIdentifier) = 0;
92
93         SWServer& m_server;
94     };
95
96     WEBCORE_EXPORT SWServer();
97     WEBCORE_EXPORT ~SWServer();
98
99     WEBCORE_EXPORT void clear();
100
101     SWServerRegistration* getRegistration(const ServiceWorkerRegistrationKey&);
102     void addRegistration(std::unique_ptr<SWServerRegistration>&&);
103     void removeRegistration(const ServiceWorkerRegistrationKey&);
104
105     void scheduleJob(const ServiceWorkerJobData&);
106     void rejectJob(const ServiceWorkerJobData&, const ExceptionData&);
107     void resolveRegistrationJob(const ServiceWorkerJobData&, const ServiceWorkerRegistrationData&, ShouldNotifyWhenResolved);
108     void resolveUnregistrationJob(const ServiceWorkerJobData&, const ServiceWorkerRegistrationKey&, bool unregistrationResult);
109     void startScriptFetch(const ServiceWorkerJobData&);
110
111     void postTask(CrossThreadTask&&);
112     void postTaskReply(CrossThreadTask&&);
113
114     Ref<SWServerWorker> updateWorker(Connection&, const ServiceWorkerRegistrationKey&, const URL&, const String& script, WorkerType);
115     void fireInstallEvent(Connection&, ServiceWorkerIdentifier);
116     SWServerWorker* workerByID(ServiceWorkerIdentifier identifier) const { return m_workersByID.get(identifier); }
117     
118     Connection* getConnection(uint64_t identifier) { return m_connections.get(identifier); }
119
120 private:
121     void registerConnection(Connection&);
122     void unregisterConnection(Connection&);
123
124     void taskThreadEntryPoint();
125     void handleTaskRepliesOnMainThread();
126
127     void scriptFetchFinished(Connection&, const ServiceWorkerFetchResult&);
128     void scriptContextFailedToStart(Connection&, const ServiceWorkerRegistrationKey&, ServiceWorkerIdentifier, const String& message);
129     void scriptContextStarted(Connection&, const ServiceWorkerRegistrationKey&, ServiceWorkerIdentifier);
130     void didFinishInstall(Connection&, const ServiceWorkerRegistrationKey&, ServiceWorkerIdentifier, bool wasSuccessful);
131     void didResolveRegistrationPromise(Connection&, const ServiceWorkerRegistrationKey&);
132
133     void addClientServiceWorkerRegistration(Connection&, const ServiceWorkerRegistrationKey&, ServiceWorkerRegistrationIdentifier);
134     void removeClientServiceWorkerRegistration(Connection&, const ServiceWorkerRegistrationKey&, ServiceWorkerRegistrationIdentifier);
135
136     WEBCORE_EXPORT const SWServerRegistration* doRegistrationMatching(const SecurityOriginData& topOrigin, const URL& clientURL) const;
137
138     HashMap<uint64_t, Connection*> m_connections;
139     HashMap<ServiceWorkerRegistrationKey, std::unique_ptr<SWServerRegistration>> m_registrations;
140     HashMap<ServiceWorkerRegistrationKey, std::unique_ptr<SWServerJobQueue>> m_jobQueues;
141
142     HashMap<ServiceWorkerIdentifier, Ref<SWServerWorker>> m_workersByID;
143
144     RefPtr<Thread> m_taskThread;
145     Lock m_taskThreadLock;
146
147     CrossThreadQueue<CrossThreadTask> m_taskQueue;
148     CrossThreadQueue<CrossThreadTask> m_taskReplyQueue;
149
150     Lock m_mainThreadReplyLock;
151     bool m_mainThreadReplyScheduled { false };
152 };
153
154 } // namespace WebCore
155
156 #endif // ENABLE(SERVICE_WORKER)