[Service Workers] Implement container.getRegistrations()
[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/Identified.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 : public Identified<Connection> {
62     friend class SWServer;
63     public:
64         WEBCORE_EXPORT virtual ~Connection();
65
66         WEBCORE_EXPORT void didResolveRegistrationPromise(const ServiceWorkerRegistrationKey&);
67         const SWServerRegistration* doRegistrationMatching(const SecurityOriginData& topOrigin, const URL& clientURL) const { return m_server.doRegistrationMatching(topOrigin, clientURL); }
68
69         // Messages to the client WebProcess
70         virtual void updateRegistrationStateInClient(ServiceWorkerRegistrationIdentifier, ServiceWorkerRegistrationState, const std::optional<ServiceWorkerData>&) = 0;
71         virtual void updateWorkerStateInClient(ServiceWorkerIdentifier, ServiceWorkerState) = 0;
72         virtual void fireUpdateFoundEvent(ServiceWorkerRegistrationIdentifier) = 0;
73
74     protected:
75         WEBCORE_EXPORT Connection(SWServer&, uint64_t identifier);
76         SWServer& server() { return m_server; }
77
78         WEBCORE_EXPORT void scheduleJobInServer(const ServiceWorkerJobData&);
79         WEBCORE_EXPORT void finishFetchingScriptInServer(const ServiceWorkerFetchResult&);
80         WEBCORE_EXPORT void addServiceWorkerRegistrationInServer(const ServiceWorkerRegistrationKey&, ServiceWorkerRegistrationIdentifier);
81         WEBCORE_EXPORT void removeServiceWorkerRegistrationInServer(const ServiceWorkerRegistrationKey&, ServiceWorkerRegistrationIdentifier);
82
83     private:
84         // Messages to the client WebProcess
85         virtual void rejectJobInClient(uint64_t jobIdentifier, const ExceptionData&) = 0;
86         virtual void resolveRegistrationJobInClient(uint64_t jobIdentifier, const ServiceWorkerRegistrationData&, ShouldNotifyWhenResolved) = 0;
87         virtual void resolveUnregistrationJobInClient(uint64_t jobIdentifier, const ServiceWorkerRegistrationKey&, bool registrationResult) = 0;
88         virtual void startScriptFetchInClient(uint64_t jobIdentifier) = 0;
89
90         SWServer& m_server;
91     };
92
93     WEBCORE_EXPORT explicit SWServer(UniqueRef<SWOriginStore>&&);
94     WEBCORE_EXPORT ~SWServer();
95
96     WEBCORE_EXPORT void clearAll();
97     WEBCORE_EXPORT void clear(const SecurityOrigin&);
98
99
100     SWServerRegistration* getRegistration(const ServiceWorkerRegistrationKey&);
101     void addRegistration(std::unique_ptr<SWServerRegistration>&&);
102     void removeRegistration(const ServiceWorkerRegistrationKey&);
103     WEBCORE_EXPORT Vector<ServiceWorkerRegistrationData> getRegistrations(const SecurityOriginData& topOrigin, const URL& clientURL);
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     void updateWorker(Connection&, const ServiceWorkerRegistrationKey&, const URL&, const String& script, WorkerType);
115     void fireInstallEvent(SWServerWorker&);
116     void fireActivateEvent(SWServerWorker&);
117     SWServerWorker* workerByID(ServiceWorkerIdentifier identifier) const { return m_workersByID.get(identifier); }
118     
119     Connection* getConnection(uint64_t identifier) { return m_connections.get(identifier); }
120     SWOriginStore& originStore() { return m_originStore; }
121
122     void scriptContextFailedToStart(SWServerWorker&, const String& message);
123     void scriptContextStarted(SWServerWorker&);
124     void didFinishInstall(SWServerWorker&, bool wasSuccessful);
125     void didFinishActivation(SWServerWorker&);
126
127     WEBCORE_EXPORT void serverToContextConnectionCreated();
128     
129     WEBCORE_EXPORT static HashSet<SWServer*>& allServers();
130
131 private:
132     void registerConnection(Connection&);
133     void unregisterConnection(Connection&);
134
135     void taskThreadEntryPoint();
136     void handleTaskRepliesOnMainThread();
137
138     void scriptFetchFinished(Connection&, const ServiceWorkerFetchResult&);
139
140     void didResolveRegistrationPromise(Connection&, const ServiceWorkerRegistrationKey&);
141
142     void addClientServiceWorkerRegistration(Connection&, const ServiceWorkerRegistrationKey&, ServiceWorkerRegistrationIdentifier);
143     void removeClientServiceWorkerRegistration(Connection&, const ServiceWorkerRegistrationKey&, ServiceWorkerRegistrationIdentifier);
144
145     WEBCORE_EXPORT const SWServerRegistration* doRegistrationMatching(const SecurityOriginData& topOrigin, const URL& clientURL) const;
146
147     void installContextData(const ServiceWorkerContextData&);
148
149     HashMap<uint64_t, Connection*> m_connections;
150     HashMap<ServiceWorkerRegistrationKey, std::unique_ptr<SWServerRegistration>> m_registrations;
151     HashMap<ServiceWorkerRegistrationKey, std::unique_ptr<SWServerJobQueue>> m_jobQueues;
152
153     HashMap<ServiceWorkerIdentifier, Ref<SWServerWorker>> m_workersByID;
154
155     RefPtr<Thread> m_taskThread;
156     Lock m_taskThreadLock;
157
158     CrossThreadQueue<CrossThreadTask> m_taskQueue;
159     CrossThreadQueue<CrossThreadTask> m_taskReplyQueue;
160
161     Lock m_mainThreadReplyLock;
162     bool m_mainThreadReplyScheduled { false };
163     UniqueRef<SWOriginStore> m_originStore;
164     Deque<ServiceWorkerContextData> m_pendingContextDatas;
165 };
166
167 } // namespace WebCore
168
169 #endif // ENABLE(SERVICE_WORKER)