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