Confirmed users
770
edits
| Line 1: | Line 1: | ||
=Dedicated Worker= | =Dedicated Worker= | ||
<source lang="cpp"> | |||
already_AddRefed<WorkerPrivate> | |||
WorkerPrivate::Constructor(JSContext* aCx, | |||
const nsAString& aScriptURL, | |||
bool aIsChromeWorker, WorkerType aWorkerType, | |||
const nsACString& aSharedWorkerName, | |||
LoadInfo* aLoadInfo, ErrorResult& aRv) | |||
{ | |||
WorkerPrivate* parent = NS_IsMainThread() ? | |||
nullptr : | |||
GetCurrentThreadWorkerPrivate(); | |||
if (parent) { | |||
parent->AssertIsOnWorkerThread(); | |||
} else { | |||
AssertIsOnMainThread(); | |||
} | |||
MOZ_ASSERT_IF(aWorkerType != WorkerTypeDedicated, | |||
!aSharedWorkerName.IsVoid()); | |||
MOZ_ASSERT_IF(aWorkerType == WorkerTypeDedicated, | |||
aSharedWorkerName.IsEmpty()); | |||
Maybe<LoadInfo> stackLoadInfo; | |||
if (!aLoadInfo) { | |||
stackLoadInfo.emplace(); | |||
nsresult rv = GetLoadInfo(aCx, nullptr, parent, aScriptURL, | |||
aIsChromeWorker, stackLoadInfo.ptr()); | |||
if (NS_FAILED(rv)) { | |||
scriptloader::ReportLoadError(aCx, aScriptURL, rv, !parent); | |||
aRv.Throw(rv); | |||
return nullptr; | |||
} | |||
aLoadInfo = stackLoadInfo.ptr(); | |||
} | |||
// NB: This has to be done before creating the WorkerPrivate, because it will | |||
// attempt to use static variables that are initialized in the RuntimeService | |||
// constructor. | |||
RuntimeService* runtimeService; | |||
if (!parent) { | |||
runtimeService = RuntimeService::GetOrCreateService(); | |||
if (!runtimeService) { | |||
JS_ReportError(aCx, "Failed to create runtime service!"); | |||
aRv.Throw(NS_ERROR_FAILURE); | |||
return nullptr; | |||
} | |||
} | |||
else { | |||
runtimeService = RuntimeService::GetService(); | |||
} | |||
MOZ_ASSERT(runtimeService); | |||
nsRefPtr<WorkerPrivate> worker = | |||
new WorkerPrivate(aCx, parent, aScriptURL, aIsChromeWorker, | |||
aWorkerType, aSharedWorkerName, *aLoadInfo); | |||
if (!runtimeService->RegisterWorker(aCx, worker)) { | |||
aRv.Throw(NS_ERROR_UNEXPECTED); | |||
return nullptr; | |||
} | |||
worker->EnableDebugger(); | |||
nsRefPtr<CompileScriptRunnable> compiler = new CompileScriptRunnable(worker); | |||
if (!compiler->Dispatch(aCx)) { | |||
aRv.Throw(NS_ERROR_UNEXPECTED); | |||
return nullptr; | |||
} | |||
worker->mSelfRef = worker; | |||
return worker.forget(); | |||
} | |||
</source> | |||
=Shared Worker= | =Shared Worker= | ||