2 * Copyright (C) 2017 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #if ENABLE(SERVICE_WORKER)
30 #include "ServiceWorkerJob.h"
31 #include "ServiceWorkerRegistrationKey.h"
32 #include <wtf/CrossThreadQueue.h>
33 #include <wtf/CrossThreadTask.h>
34 #include <wtf/HashMap.h>
35 #include <wtf/HashSet.h>
36 #include <wtf/Identified.h>
37 #include <wtf/RunLoop.h>
38 #include <wtf/ThreadSafeRefCounted.h>
39 #include <wtf/Threading.h>
43 class SWServerRegistration;
46 struct ServiceWorkerContextData;
47 struct ServiceWorkerFetchResult;
48 struct ServiceWorkerRegistrationData;
52 class Connection : public Identified<Connection> {
53 friend class SWServer;
55 WEBCORE_EXPORT virtual ~Connection();
57 WEBCORE_EXPORT void scriptContextFailedToStart(const ServiceWorkerRegistrationKey&, const String& workerID, const String& message);
58 WEBCORE_EXPORT void scriptContextStarted(const ServiceWorkerRegistrationKey&, uint64_t identifier, const String& workerID);
61 WEBCORE_EXPORT Connection(SWServer&, uint64_t identifier);
62 SWServer& server() { return m_server; }
64 WEBCORE_EXPORT void scheduleJobInServer(const ServiceWorkerJobData&);
65 WEBCORE_EXPORT void finishFetchingScriptInServer(const ServiceWorkerFetchResult&);
68 // Messages to the client WebProcess
69 virtual void rejectJobInClient(uint64_t jobIdentifier, const ExceptionData&) = 0;
70 virtual void resolveRegistrationJobInClient(uint64_t jobIdentifier, const ServiceWorkerRegistrationData&) = 0;
71 virtual void resolveUnregistrationJobInClient(uint64_t jobIdentifier, const ServiceWorkerRegistrationKey&, bool registrationResult) = 0;
72 virtual void startScriptFetchInClient(uint64_t jobIdentifier) = 0;
74 // Messages to the SW host WebProcess
75 virtual void startServiceWorkerContext(const ServiceWorkerContextData&) = 0;
80 WEBCORE_EXPORT SWServer();
81 WEBCORE_EXPORT ~SWServer();
83 void scheduleJob(const ServiceWorkerJobData&);
84 void rejectJob(const ServiceWorkerJobData&, const ExceptionData&);
85 void resolveRegistrationJob(const ServiceWorkerJobData&, const ServiceWorkerRegistrationData&);
86 void resolveUnregistrationJob(const ServiceWorkerJobData&, const ServiceWorkerRegistrationKey&, bool unregistrationResult);
87 void startScriptFetch(const ServiceWorkerJobData&);
89 void postTask(CrossThreadTask&&);
90 void postTaskReply(CrossThreadTask&&);
92 Ref<SWServerWorker> createWorker(Connection&, const ServiceWorkerRegistrationKey&, const URL&, const String& script, WorkerType);
95 void registerConnection(Connection&);
96 void unregisterConnection(Connection&);
98 void taskThreadEntryPoint();
99 void handleTaskRepliesOnMainThread();
101 void scriptFetchFinished(Connection&, const ServiceWorkerFetchResult&);
102 void scriptContextFailedToStart(Connection&, const ServiceWorkerRegistrationKey&, const String& workerID, const String& message);
103 void scriptContextStarted(Connection&, const ServiceWorkerRegistrationKey&, uint64_t identifier, const String& workerID);
105 HashMap<uint64_t, Connection*> m_connections;
106 HashMap<ServiceWorkerRegistrationKey, std::unique_ptr<SWServerRegistration>> m_registrations;
108 HashMap<String, Ref<SWServerWorker>> m_workersByID;
110 RefPtr<Thread> m_taskThread;
111 Lock m_taskThreadLock;
113 CrossThreadQueue<CrossThreadTask> m_taskQueue;
114 CrossThreadQueue<CrossThreadTask> m_taskReplyQueue;
116 Lock m_mainThreadReplyLock;
117 bool m_mainThreadReplyScheduled { false };
120 } // namespace WebCore
122 #endif // ENABLE(SERVICE_WORKER)