#include "config.h"
#include "WorkerThread.h"
#include "DedicatedWorkerGlobalScope.h"
#include "InspectorInstrumentation.h"
#include "ScriptSourceCode.h"
#include "SecurityOrigin.h"
#include "ThreadGlobalData.h"
#include "URL.h"
#include <utility>
#include <wtf/NeverDestroyed.h>
#include <wtf/Noncopyable.h>
#include <wtf/text/WTFString.h>
#if ENABLE(SQL_DATABASE)
#include "DatabaseManager.h"
#include "DatabaseTask.h"
#endif
#if PLATFORM(IOS)
#include "FloatingPointEnvironment.h"
#include "WebCoreThread.h"
#endif
namespace WebCore {
static std::mutex& threadSetMutex()
{
static std::once_flag onceFlag;
static LazyNeverDestroyed<std::mutex> mutex;
std::call_once(onceFlag, []{
mutex.construct();
});
return mutex;
}
static HashSet<WorkerThread*>& workerThreads()
{
static NeverDestroyed<HashSet<WorkerThread*>> workerThreads;
return workerThreads;
}
unsigned WorkerThread::workerThreadCount()
{
std::lock_guard<std::mutex> lock(threadSetMutex());
return workerThreads().size();
}
struct WorkerThreadStartupData {
WTF_MAKE_NONCOPYABLE(WorkerThreadStartupData); WTF_MAKE_FAST_ALLOCATED;
public:
WorkerThreadStartupData(const URL& scriptURL, const String& userAgent, const GroupSettings*, const String& sourceCode, WorkerThreadStartMode, const String& contentSecurityPolicy, ContentSecurityPolicy::HeaderType contentSecurityPolicyType, const SecurityOrigin* topOrigin);
URL m_scriptURL;
String m_userAgent;
std::unique_ptr<GroupSettings> m_groupSettings;
String m_sourceCode;
WorkerThreadStartMode m_startMode;
String m_contentSecurityPolicy;
ContentSecurityPolicy::HeaderType m_contentSecurityPolicyType;
RefPtr<SecurityOrigin> m_topOrigin;
};
WorkerThreadStartupData::WorkerThreadStartupData(const URL& scriptURL, const String& userAgent, const GroupSettings* settings, const String& sourceCode, WorkerThreadStartMode startMode, const String& contentSecurityPolicy, ContentSecurityPolicy::HeaderType contentSecurityPolicyType, const SecurityOrigin* topOrigin)
: m_scriptURL(scriptURL.copy())
, m_userAgent(userAgent.isolatedCopy())
, m_sourceCode(sourceCode.isolatedCopy())
, m_startMode(startMode)
, m_contentSecurityPolicy(contentSecurityPolicy.isolatedCopy())
, m_contentSecurityPolicyType(contentSecurityPolicyType)
, m_topOrigin(topOrigin ? topOrigin->isolatedCopy() : 0)
{
if (!settings)
return;
m_groupSettings = std::make_unique<GroupSettings>();
m_groupSettings->setLocalStorageQuotaBytes(settings->localStorageQuotaBytes());
m_groupSettings->setIndexedDBQuotaBytes(settings->indexedDBQuotaBytes());
m_groupSettings->setIndexedDBDatabasePath(settings->indexedDBDatabasePath().isolatedCopy());
}
WorkerThread::WorkerThread(const URL& scriptURL, const String& userAgent, const GroupSettings* settings, const String& sourceCode, WorkerLoaderProxy& workerLoaderProxy, WorkerReportingProxy& workerReportingProxy, WorkerThreadStartMode startMode, const String& contentSecurityPolicy, ContentSecurityPolicy::HeaderType contentSecurityPolicyType, const SecurityOrigin* topOrigin)
: m_threadID(0)
, m_workerLoaderProxy(workerLoaderProxy)
, m_workerReportingProxy(workerReportingProxy)
, m_startupData(std::make_unique<WorkerThreadStartupData>(scriptURL, userAgent, settings, sourceCode, startMode, contentSecurityPolicy, contentSecurityPolicyType, topOrigin))
#if ENABLE(NOTIFICATIONS) || ENABLE(LEGACY_NOTIFICATIONS)
, m_notificationClient(0)
#endif
{
std::lock_guard<std::mutex> lock(threadSetMutex());
workerThreads().add(this);
}
WorkerThread::~WorkerThread()
{
std::lock_guard<std::mutex> lock(threadSetMutex());
ASSERT(workerThreads().contains(this));
workerThreads().remove(this);
}
bool WorkerThread::start()
{
MutexLocker 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::shared().propagateMainThreadEnvironment();
#endif
{
MutexLocker lock(m_threadCreationMutex);
m_workerGlobalScope = createWorkerGlobalScope(m_startupData->m_scriptURL, m_startupData->m_userAgent, WTF::move(m_startupData->m_groupSettings), m_startupData->m_contentSecurityPolicy, m_startupData->m_contentSecurityPolicyType, m_startupData->m_topOrigin.release());
if (m_runLoop.terminated()) {
m_workerGlobalScope->script()->forbidExecution();
}
}
WorkerScriptController* script = m_workerGlobalScope->script();
#if ENABLE(INSPECTOR)
InspectorInstrumentation::willEvaluateWorkerScript(workerGlobalScope(), m_startupData->m_startMode);
#endif
script->evaluate(ScriptSourceCode(m_startupData->m_sourceCode, m_startupData->m_scriptURL));
m_startupData = nullptr;
runEventLoop();
ThreadIdentifier threadID = m_threadID;
ASSERT(m_workerGlobalScope->hasOneRef());
m_workerGlobalScope = 0;
threadGlobalData().destroy();
detachThread(threadID);
}
void WorkerThread::runEventLoop()
{
m_runLoop.run(m_workerGlobalScope.get());
}
void WorkerThread::stop()
{
MutexLocker lock(m_threadCreationMutex);
if (m_workerGlobalScope) {
m_workerGlobalScope->script()->scheduleExecutionTermination();
#if ENABLE(SQL_DATABASE)
DatabaseManager::manager().interruptAllDatabasesForContext(m_workerGlobalScope.get());
#endif
m_runLoop.postTaskAndTerminate({ ScriptExecutionContext::Task::CleanupTask, [] (ScriptExecutionContext& context ) {
WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(&context);
#if ENABLE(SQL_DATABASE)
DatabaseTaskSynchronizer cleanupSync;
DatabaseManager::manager().stopDatabases(workerGlobalScope, &cleanupSync);
#endif
workerGlobalScope->stopActiveDOMObjects();
workerGlobalScope->notifyObserversOfStop();
workerGlobalScope->removeAllEventListeners();
#if ENABLE(SQL_DATABASE)
cleanupSync.waitForTaskCompletion();
#endif
workerGlobalScope->postTask({ ScriptExecutionContext::Task::CleanupTask, [] (ScriptExecutionContext& context) {
WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(&context);
workerGlobalScope->clearScript();
} });
} });
return;
}
m_runLoop.terminate();
}
void WorkerThread::releaseFastMallocFreeMemoryInAllThreads()
{
std::lock_guard<std::mutex> lock(threadSetMutex());
for (auto* workerThread : workerThreads())
workerThread->runLoop().postTask([] (ScriptExecutionContext&) {
WTF::releaseFastMallocFreeMemory();
});
}
}