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 resolveJobInClient(uint64_t jobIdentifier, const ServiceWorkerRegistrationData&) = 0;
71 virtual void startScriptFetchInClient(uint64_t jobIdentifier) = 0;
73 // Messages to the SW host WebProcess
74 virtual void startServiceWorkerContext(const ServiceWorkerContextData&) = 0;
79 WEBCORE_EXPORT SWServer();
80 WEBCORE_EXPORT ~SWServer();
82 void scheduleJob(const ServiceWorkerJobData&);
83 void rejectJob(const ServiceWorkerJobData&, const ExceptionData&);
84 void resolveJob(const ServiceWorkerJobData&, const ServiceWorkerRegistrationData&);
85 void startScriptFetch(const ServiceWorkerJobData&);
87 void postTask(CrossThreadTask&&);
88 void postTaskReply(CrossThreadTask&&);
90 Ref<SWServerWorker> createWorker(Connection&, const ServiceWorkerRegistrationKey&, const URL&, const String& script, WorkerType);
93 void registerConnection(Connection&);
94 void unregisterConnection(Connection&);
96 void taskThreadEntryPoint();
97 void handleTaskRepliesOnMainThread();
99 void scriptFetchFinished(Connection&, const ServiceWorkerFetchResult&);
100 void scriptContextFailedToStart(Connection&, const ServiceWorkerRegistrationKey&, const String& workerID, const String& message);
101 void scriptContextStarted(Connection&, const ServiceWorkerRegistrationKey&, uint64_t identifier, const String& workerID);
103 HashMap<uint64_t, Connection*> m_connections;
104 HashMap<ServiceWorkerRegistrationKey, std::unique_ptr<SWServerRegistration>> m_registrations;
106 HashMap<String, Ref<SWServerWorker>> m_workersByID;
108 RefPtr<Thread> m_taskThread;
109 Lock m_taskThreadLock;
111 CrossThreadQueue<CrossThreadTask> m_taskQueue;
112 CrossThreadQueue<CrossThreadTask> m_taskReplyQueue;
114 Lock m_mainThreadReplyLock;
115 bool m_mainThreadReplyScheduled { false };
118 } // namespace WebCore
120 #endif // ENABLE(SERVICE_WORKER)