chore: enable microtask queue per window agent (#36870)

* chore: enable microtask queue per window agent

* chore: switch policies on context microtask queue

* fix: ensure node::Environment is valid
This commit is contained in:
Robo 2023-01-12 01:59:32 +09:00 committed by GitHub
parent 2a7d0a84c0
commit fefb22a83d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 115 additions and 54 deletions

View file

@ -217,14 +217,14 @@ void SpellCheckClient::SpellCheckWords(const SpellCheckScope& scope,
const std::set<std::u16string>& words) {
DCHECK(!scope.spell_check_.IsEmpty());
auto context = isolate_->GetCurrentContext();
gin_helper::MicrotasksScope microtasks_scope(
isolate_, v8::MicrotasksScope::kDoNotRunMicrotasks);
isolate_, context->GetMicrotaskQueue(),
v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::Local<v8::FunctionTemplate> templ = gin_helper::CreateFunctionTemplate(
isolate_,
base::BindRepeating(&SpellCheckClient::OnSpellCheckDone, AsWeakPtr()));
auto context = isolate_->GetCurrentContext();
v8::Local<v8::Value> args[] = {gin::ConvertToV8(isolate_, words),
templ->GetFunction(context).ToLocalChecked()};
// Call javascript with the words and the callback function

View file

@ -87,7 +87,7 @@ void EmitIPCEvent(v8::Local<v8::Context> context,
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope script_scope(isolate,
v8::MicrotasksScope script_scope(isolate, context->GetMicrotaskQueue(),
v8::MicrotasksScope::kRunMicrotasks);
std::vector<v8::Local<v8::Value>> argv = {

View file

@ -67,9 +67,10 @@ void ElectronRenderFrameObserver::DidClearWindowObject() {
!web_frame->IsOnInitialEmptyDocument()) {
v8::Isolate* isolate = blink::MainThreadIsolate();
v8::HandleScope handle_scope(isolate);
v8::MicrotasksScope microtasks_scope(
isolate, v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::Handle<v8::Context> context = web_frame->MainWorldScriptContext();
v8::MicrotasksScope microtasks_scope(
isolate, context->GetMicrotaskQueue(),
v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::Context::Scope context_scope(context);
// DidClearWindowObject only emits for the main world.
DidInstallConditionalFeatures(context, MAIN_WORLD_ID);
@ -112,7 +113,8 @@ void ElectronRenderFrameObserver::DidInstallConditionalFeatures(
auto* isolate = context->GetIsolate();
v8::MicrotasksScope microtasks_scope(
isolate, v8::MicrotasksScope::kDoNotRunMicrotasks);
isolate, context->GetMicrotaskQueue(),
v8::MicrotasksScope::kDoNotRunMicrotasks);
if (ShouldNotifyClient(world_id))
renderer_client_->DidCreateScriptContext(context, render_frame_);

View file

@ -138,10 +138,10 @@ void ElectronRendererClient::WillReleaseScriptContext(
// checkpoints after every call into JavaScript. Since we use a different
// policy in the renderer - switch to `kExplicit` and then drop back to the
// previous policy value.
v8::Isolate* isolate = context->GetIsolate();
auto old_policy = isolate->GetMicrotasksPolicy();
DCHECK_EQ(v8::MicrotasksScope::GetCurrentDepth(isolate), 0);
isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
v8::MicrotaskQueue* microtask_queue = context->GetMicrotaskQueue();
auto old_policy = microtask_queue->microtasks_policy();
DCHECK_EQ(microtask_queue->GetMicrotasksScopeDepth(), 0);
microtask_queue->set_microtasks_policy(v8::MicrotasksPolicy::kExplicit);
node::FreeEnvironment(env);
if (node_bindings_->uv_env() == nullptr) {
@ -149,7 +149,7 @@ void ElectronRendererClient::WillReleaseScriptContext(
node_bindings_->set_isolate_data(nullptr);
}
isolate->SetMicrotasksPolicy(old_policy);
microtask_queue->set_microtasks_policy(old_policy);
// ElectronBindings is tracking node environments.
electron_bindings_->EnvironmentDestroyed(env);

View file

@ -162,12 +162,12 @@ void ElectronSandboxedRendererClient::RunScriptsAtDocumentStart(
return;
auto* isolate = blink::MainThreadIsolate();
gin_helper::MicrotasksScope microtasks_scope(
isolate, v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context =
GetContext(render_frame->GetWebFrame(), isolate);
gin_helper::MicrotasksScope microtasks_scope(
isolate, context->GetMicrotaskQueue(),
v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::Context::Scope context_scope(context);
InvokeHiddenCallback(context, kLifecycleKey, "onDocumentStart");
@ -180,12 +180,12 @@ void ElectronSandboxedRendererClient::RunScriptsAtDocumentEnd(
return;
auto* isolate = blink::MainThreadIsolate();
gin_helper::MicrotasksScope microtasks_scope(
isolate, v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> context =
GetContext(render_frame->GetWebFrame(), isolate);
gin_helper::MicrotasksScope microtasks_scope(
isolate, context->GetMicrotaskQueue(),
v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::Context::Scope context_scope(context);
InvokeHiddenCallback(context, kLifecycleKey, "onDocumentEnd");
@ -230,7 +230,8 @@ void ElectronSandboxedRendererClient::WillReleaseScriptContext(
auto* isolate = context->GetIsolate();
gin_helper::MicrotasksScope microtasks_scope(
isolate, v8::MicrotasksScope::kDoNotRunMicrotasks);
isolate, context->GetMicrotaskQueue(),
v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(context);
InvokeHiddenCallback(context, kLifecycleKey, "onExit");

View file

@ -41,11 +41,14 @@ WebWorkerObserver::~WebWorkerObserver() {
// Node.js expects `kExplicit` microtasks policy and will run microtasks
// checkpoints after every call into JavaScript. Since we use a different
// policy in the renderer - switch to `kExplicit`
v8::Isolate* isolate = node_bindings_->uv_env()->isolate();
DCHECK_EQ(v8::MicrotasksScope::GetCurrentDepth(isolate), 0);
isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
v8::MicrotaskQueue* microtask_queue =
node_bindings_->uv_env()->context()->GetMicrotaskQueue();
auto old_policy = microtask_queue->microtasks_policy();
DCHECK_EQ(microtask_queue->GetMicrotasksScopeDepth(), 0);
microtask_queue->set_microtasks_policy(v8::MicrotasksPolicy::kExplicit);
node::FreeEnvironment(node_bindings_->uv_env());
node::FreeIsolateData(node_bindings_->isolate_data());
microtask_queue->set_microtasks_policy(old_policy);
}
void WebWorkerObserver::WorkerScriptReadyForEvaluation(
@ -53,7 +56,8 @@ void WebWorkerObserver::WorkerScriptReadyForEvaluation(
v8::Context::Scope context_scope(worker_context);
auto* isolate = worker_context->GetIsolate();
v8::MicrotasksScope microtasks_scope(
isolate, v8::MicrotasksScope::kDoNotRunMicrotasks);
isolate, worker_context->GetMicrotaskQueue(),
v8::MicrotasksScope::kDoNotRunMicrotasks);
// Start the embed thread.
node_bindings_->PrepareEmbedThread();