#include "config.h"
#include "WorkerThread.h"
#include "ContentSecurityPolicyResponseHeaders.h"
#include "DedicatedWorkerGlobalScope.h"
#include "IDBConnectionProxy.h"
#include "ScriptSourceCode.h"
#include "SecurityOrigin.h"
#include "SocketProvider.h"
#include "ThreadGlobalData.h"
#include "URL.h"
#include <utility>
#include <wtf/Lock.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/Noncopyable.h>
#include <wtf/text/WTFString.h>
#if PLATFORM(IOS)
#include "FloatingPointEnvironment.h"
#include "WebCoreThread.h"
#endif
#if PLATFORM(GTK)
#include <wtf/glib/GRefPtr.h>
#endif
namespace WebCore {
static StaticLock threadSetMutex;
static HashSet<WorkerThread*>& workerThreads()
{
static NeverDestroyed<HashSet<WorkerThread*>> workerThreads;
return workerThreads;
}
unsigned WorkerThread::workerThreadCount()
{
std::lock_guard<StaticLock> lock(threadSetMutex);
return workerThreads().size();
}
struct WorkerThreadStartupData {
WTF_MAKE_NONCOPYABLE(WorkerThreadStartupData); WTF_MAKE_FAST_ALLOCATED;
public:
WorkerThreadStartupData(const URL& scriptURL, const String& userAgent, const String& sourceCode, WorkerThreadStartMode, const ContentSecurityPolicyResponseHeaders&, bool shouldBypassMainWorldContentSecurityPolicy, const SecurityOrigin* topOrigin);
URL m_scriptURL;
String m_userAgent;
String m_sourceCode;
WorkerThreadStartMode m_startMode;
ContentSecurityPolicyResponseHeaders m_contentSecurityPolicyResponseHeaders;
bool m_shouldBypassMainWorldContentSecurityPolicy;
RefPtr<SecurityOrigin> m_topOrigin;
};
WorkerThreadStartupData::WorkerThreadStartupData(const URL& scriptURL, const String& userAgent, const String& sourceCode, WorkerThreadStartMode startMode, const ContentSecurityPolicyResponseHeaders& contentSecurityPolicyResponseHeaders, bool shouldBypassMainWorldContentSecurityPolicy, const SecurityOrigin* topOrigin)
: m_scriptURL(scriptURL.isolatedCopy())
, m_userAgent(userAgent.isolatedCopy())
, m_sourceCode(sourceCode.isolatedCopy())
, m_startMode(startMode)
, m_contentSecurityPolicyResponseHeaders(contentSecurityPolicyResponseHeaders.isolatedCopy())
, m_shouldBypassMainWorldContentSecurityPolicy(shouldBypassMainWorldContentSecurityPolicy)
, m_topOrigin(topOrigin ? &topOrigin->isolatedCopy().get() : nullptr)
{
}
WorkerThread::WorkerThread(const URL& scriptURL, const String& userAgent, const String& sourceCode, WorkerLoaderProxy& workerLoaderProxy, WorkerReportingProxy& workerReportingProxy, WorkerThreadStartMode startMode, const ContentSecurityPolicyResponseHeaders& contentSecurityPolicyResponseHeaders, bool shouldBypassMainWorldContentSecurityPolicy, const SecurityOrigin* topOrigin, IDBClient::IDBConnectionProxy* connectionProxy, SocketProvider* socketProvider)
: m_threadID(0)
, m_workerLoaderProxy(workerLoaderProxy)
, m_workerReportingProxy(workerReportingProxy)
, m_startupData(std::make_unique<WorkerThreadStartupData>(scriptURL, userAgent, sourceCode, startMode, contentSecurityPolicyResponseHeaders, shouldBypassMainWorldContentSecurityPolicy, topOrigin))
#if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
, m_notificationClient(0)
#endif
#if ENABLE(INDEXED_DATABASE)
, m_idbConnectionProxy(connectionProxy)
#endif
#if ENABLE(WEB_SOCKETS)
, m_socketProvider(socketProvider)
#endif
{
#if !ENABLE(INDEXED_DATABASE)
UNUSED_PARAM(connectionProxy);
#endif
#if !ENABLE(WEB_SOCKETS)
UNUSED_PARAM(socketProvider);
#endif
std::lock_guard<StaticLock> lock(threadSetMutex);
workerThreads().add(this);
}
WorkerThread::~WorkerThread()
{
std::lock_guard<StaticLock> lock(threadSetMutex);
ASSERT(workerThreads().contains(this));
workerThreads().remove(this);
}
bool WorkerThread::start()
{
LockHolder lock(m_threadCreationMutex);
if (m_threadID)
return true;
m_threadID = createThread(WorkerThread::workerThreadStart, this, "WebCore: Worker");
return m_threadID;
}
void WorkerThread::workerThreadStart(void* thread)
{
static_cast<WorkerThread*>(thread)->workerThread();
}
void WorkerThread::workerThread()
{
#if PLATFORM(IOS)
FloatingPointEnvironment::singleton().propagateMainThreadEnvironment();
#endif
#if PLATFORM(GTK)
GRefPtr<GMainContext> mainContext = adoptGRef(g_main_context_new());
g_main_context_push_thread_default(mainContext.get());
#endif
{
LockHolder lock(m_threadCreationMutex);
m_workerGlobalScope = createWorkerGlobalScope(m_startupData->m_scriptURL, m_startupData->m_userAgent, m_startupData->m_contentSecurityPolicyResponseHeaders, m_startupData->m_shouldBypassMainWorldContentSecurityPolicy, WTFMove(m_startupData->m_topOrigin));
if (m_runLoop.terminated()) {
m_workerGlobalScope->script()->forbidExecution();
}
}
WorkerScriptController* script = m_workerGlobalScope->script();
script->evaluate(ScriptSourceCode(m_startupData->m_sourceCode, m_startupData->m_scriptURL));
m_startupData = nullptr;
runEventLoop();
#if PLATFORM(GTK)
g_main_context_pop_thread_default(mainContext.get());
#endif
ThreadIdentifier threadID = m_threadID;
ASSERT(m_workerGlobalScope->hasOneRef());
m_workerGlobalScope = nullptr;
threadGlobalData().destroy();
detachThread(threadID);
}
void WorkerThread::runEventLoop()
{
m_runLoop.run(m_workerGlobalScope.get());
}
void WorkerThread::stop()
{
LockHolder lock(m_threadCreationMutex);
if (m_workerGlobalScope) {
m_workerGlobalScope->script()->scheduleExecutionTermination();
m_runLoop.postTaskAndTerminate({ ScriptExecutionContext::Task::CleanupTask, [] (ScriptExecutionContext& context ) {
WorkerGlobalScope& workerGlobalScope = downcast<WorkerGlobalScope>(context);
#if ENABLE(INDEXED_DATABASE)
workerGlobalScope.stopIndexedDatabase();
#endif
workerGlobalScope.stopActiveDOMObjects();
workerGlobalScope.notifyObserversOfStop();
workerGlobalScope.removeAllEventListeners();
workerGlobalScope.postTask({ ScriptExecutionContext::Task::CleanupTask, [] (ScriptExecutionContext& context) {
WorkerGlobalScope& workerGlobalScope = downcast<WorkerGlobalScope>(context);
workerGlobalScope.clearScript();
} });
} });
return;
}
m_runLoop.terminate();
}
void WorkerThread::releaseFastMallocFreeMemoryInAllThreads()
{
std::lock_guard<StaticLock> lock(threadSetMutex);
for (auto* workerThread : workerThreads()) {
workerThread->runLoop().postTask([] (ScriptExecutionContext&) {
WTF::releaseFastMallocFreeMemory();
});
}
}
IDBClient::IDBConnectionProxy* WorkerThread::idbConnectionProxy()
{
#if ENABLE(INDEXED_DATABASE)
return m_idbConnectionProxy.get();
#else
return nullptr;
#endif
}
SocketProvider* WorkerThread::socketProvider()
{
#if ENABLE(WEB_SOCKETS)
return m_socketProvider.get();
#else
return nullptr;
#endif
}
}