2 * Copyright (C) 2008, 2016 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "WorkerRunLoop.h"
30 #include <runtime/RuntimeFlags.h>
31 #include <wtf/Forward.h>
32 #include <wtf/RefCounted.h>
36 class ContentSecurityPolicyResponseHeaders;
38 class NotificationClient;
41 class WorkerGlobalScope;
42 class WorkerLoaderProxy;
43 class WorkerReportingProxy;
45 enum class WorkerThreadStartMode {
51 class IDBConnectionProxy;
54 struct WorkerThreadStartupData;
56 class WorkerThread : public RefCounted<WorkerThread> {
58 virtual ~WorkerThread();
63 ThreadIdentifier threadID() const { return m_thread ? m_thread->id() : 0; }
64 WorkerRunLoop& runLoop() { return m_runLoop; }
65 WorkerLoaderProxy& workerLoaderProxy() const { return m_workerLoaderProxy; }
66 WorkerReportingProxy& workerReportingProxy() const { return m_workerReportingProxy; }
68 // Number of active worker threads.
69 WEBCORE_EXPORT static unsigned workerThreadCount();
70 static void releaseFastMallocFreeMemoryInAllThreads();
72 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
73 NotificationClient* getNotificationClient() { return m_notificationClient; }
74 void setNotificationClient(NotificationClient* client) { m_notificationClient = client; }
77 void startRunningDebuggerTasks();
78 void stopRunningDebuggerTasks();
80 JSC::RuntimeFlags runtimeFlags() const { return m_runtimeFlags; }
83 WorkerThread(const URL&, const String& identifier, const String& userAgent, const String& sourceCode, WorkerLoaderProxy&, WorkerReportingProxy&, WorkerThreadStartMode, const ContentSecurityPolicyResponseHeaders&, bool shouldBypassMainWorldContentSecurityPolicy, const SecurityOrigin& topOrigin, MonotonicTime timeOrigin, IDBClient::IDBConnectionProxy*, SocketProvider*, JSC::RuntimeFlags);
85 // Factory method for creating a new worker context for the thread.
86 virtual Ref<WorkerGlobalScope> createWorkerGlobalScope(const URL&, const String& identifier, const String& userAgent, const ContentSecurityPolicyResponseHeaders&, bool shouldBypassMainWorldContentSecurityPolicy, Ref<SecurityOrigin>&& topOrigin, MonotonicTime timeOrigin) = 0;
88 // Executes the event loop for the worker thread. Derived classes can override to perform actions before/after entering the event loop.
89 virtual void runEventLoop();
91 WorkerGlobalScope* workerGlobalScope() { return m_workerGlobalScope.get(); }
93 IDBClient::IDBConnectionProxy* idbConnectionProxy();
94 SocketProvider* socketProvider();
97 // Static function executed as the core routine on the new thread. Passed a pointer to a WorkerThread object.
98 static void workerThreadStart(void*);
101 RefPtr<Thread> m_thread;
102 WorkerRunLoop m_runLoop;
103 WorkerLoaderProxy& m_workerLoaderProxy;
104 WorkerReportingProxy& m_workerReportingProxy;
105 JSC::RuntimeFlags m_runtimeFlags;
106 bool m_pausedForDebugger { false };
108 RefPtr<WorkerGlobalScope> m_workerGlobalScope;
109 Lock m_threadCreationMutex;
111 std::unique_ptr<WorkerThreadStartupData> m_startupData;
113 #if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
114 NotificationClient* m_notificationClient { nullptr };
117 #if ENABLE(INDEXED_DATABASE)
118 RefPtr<IDBClient::IDBConnectionProxy> m_idbConnectionProxy;
120 #if ENABLE(WEB_SOCKETS)
121 RefPtr<SocketProvider> m_socketProvider;
125 } // namespace WebCore