Bug 1539039 keep WorkletGlobalScope as long as its Worklet is usable r=baku

Depends on D25353

Differential Revision: https://phabricator.services.mozilla.com/D25354

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Karl Tomlinson 2019-04-01 16:54:58 +00:00
Родитель d0133206f2
Коммит 943896b43c
3 изменённых файлов: 34 добавлений и 17 удалений

Просмотреть файл

@ -326,7 +326,8 @@ NS_IMETHODIMP
ExecutionRunnable::Run() {
// WorkletThread::IsOnWorkletThread() cannot be used here because it depends
// on a WorkletJSContext having been created for this thread. That does not
// happen until the first time RunOnWorkletThread() is called.
// happen until the global scope is created the first time
// RunOnWorkletThread() is called.
if (!NS_IsMainThread()) {
RunOnWorkletThread();
return NS_DispatchToMainThread(this);
@ -339,11 +340,7 @@ ExecutionRunnable::Run() {
void ExecutionRunnable::RunOnWorkletThread() {
WorkletThread::EnsureCycleCollectedJSContext(mParentRuntime);
AutoJSAPI jsapi;
jsapi.Init();
RefPtr<WorkletGlobalScope> globalScope =
mWorkletImpl->CreateGlobalScope(jsapi.cx());
WorkletGlobalScope* globalScope = mWorkletImpl->GetGlobalScope();
MOZ_ASSERT(globalScope);
AutoEntryScript aes(globalScope, "Worklet");

Просмотреть файл

@ -11,6 +11,7 @@
#include "mozilla/BasePrincipal.h"
#include "mozilla/dom/RegisterWorkletBindings.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/dom/WorkletBinding.h"
namespace mozilla {
@ -42,7 +43,7 @@ WorkletLoadInfo::~WorkletLoadInfo() {
WorkletImpl::WorkletImpl(nsPIDOMWindowInner* aWindow, nsIPrincipal* aPrincipal)
: mWorkletLoadInfo(aWindow, aPrincipal), mTerminated(false) {}
WorkletImpl::~WorkletImpl() = default;
WorkletImpl::~WorkletImpl() { MOZ_ASSERT(!mGlobalScope); }
JSObject* WorkletImpl::WrapWorklet(JSContext* aCx, dom::Worklet* aWorklet,
JS::Handle<JSObject*> aGivenProto) {
@ -50,30 +51,46 @@ JSObject* WorkletImpl::WrapWorklet(JSContext* aCx, dom::Worklet* aWorklet,
return dom::Worklet_Binding::Wrap(aCx, aWorklet, aGivenProto);
}
already_AddRefed<dom::WorkletGlobalScope> WorkletImpl::CreateGlobalScope(
JSContext* aCx) {
dom::WorkletGlobalScope* WorkletImpl::GetGlobalScope() {
dom::WorkletThread::AssertIsOnWorkletThread();
RefPtr<dom::WorkletGlobalScope> scope = ConstructGlobalScope();
if (mGlobalScope) {
return mGlobalScope;
}
JS::Rooted<JSObject*> global(aCx);
NS_ENSURE_TRUE(scope->WrapGlobalObject(aCx, &global), nullptr);
dom::AutoJSAPI jsapi;
jsapi.Init();
JSContext* cx = jsapi.cx();
JSAutoRealm ar(aCx, global);
mGlobalScope = ConstructGlobalScope();
JS::Rooted<JSObject*> global(cx);
NS_ENSURE_TRUE(mGlobalScope->WrapGlobalObject(cx, &global), nullptr);
JSAutoRealm ar(cx, global);
// Init Web IDL bindings
if (!dom::RegisterWorkletBindings(aCx, global)) {
if (!dom::RegisterWorkletBindings(cx, global)) {
return nullptr;
}
JS_FireOnNewGlobalObject(aCx, global);
JS_FireOnNewGlobalObject(cx, global);
return scope.forget();
return mGlobalScope;
}
void WorkletImpl::NotifyWorkletFinished() {
MOZ_ASSERT(NS_IsMainThread());
if (mTerminated) {
return;
}
// Release global scope on its thread.
SendControlMessage(NS_NewRunnableFunction(
"WorkletImpl::NotifyWorkletFinished",
[self = RefPtr<WorkletImpl>(this)]() { self->mGlobalScope = nullptr; }));
mTerminated = true;
if (mWorkletThread) {
mWorkletThread->Terminate();

Просмотреть файл

@ -74,7 +74,7 @@ class WorkletImpl {
void NotifyWorkletFinished();
// Execution thread only.
already_AddRefed<dom::WorkletGlobalScope> CreateGlobalScope(JSContext* aCx);
dom::WorkletGlobalScope* GetGlobalScope();
// Any thread.
@ -93,6 +93,9 @@ class WorkletImpl {
// Parent thread only.
RefPtr<dom::WorkletThread> mWorkletThread;
bool mTerminated;
// Execution thread only.
RefPtr<dom::WorkletGlobalScope> mGlobalScope;
};
} // namespace mozilla