refactor: reduce & remove no-op MicrotasksScope calls (#46681)

* fix: do not run microtasks in V8Serializer in browser process

* Remove no-op MicrotasksScope in `shell/browser/api/electron_api_auto_updater.cc`

This call was added in https://github.com/electron/electron/pull/40576 as an expansion of `gin_helper::EmitEvent`.

Since this only runs in the browser process and `bool ignore_browser_checkpoint = true` this code is a no-op.

Node should perform a microtask checkpoint if necessary in `node::MakeCallback`.

* Remove no-op MicrotasksScope in `shell/common/api/electron_bindings.cc`

This method is only called by the browser process. The containing function, `ElectronBindings::DidReceiveMemoryDump`, is only used in two places:

* `ElectronBindings::GetProcessMemoryInfo` in the same file, which has a `CHECK` that it's running in the browser process at the top.
* From `shell/browser/api/electron_api_web_contents.cc`, which is only run in the browser process.

Added a DCHECK for clarity and validation.

* Replace `gin_helper::MicrotasksScope` with `v8::MicrotasksScope` in `shell/renderer/`

The browser check is unnecessary in the renderer. Since `gin_helper::MicrotasksScope` will always act exactly like `v8::MicrotasksScope`, it's clear to just use the v8 object directly. This also brings them in line with the many other uses of `v8::MicrotasksScope` in `shell/renderer/`.
This commit is contained in:
Calvin 2025-04-19 11:18:03 -06:00 committed by GitHub
parent 297c4297b1
commit f15fa56e38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 7 additions and 12 deletions

View file

@ -46,10 +46,6 @@ void AutoUpdater::OnError(const std::string& message) {
gin::StringToV8(isolate, message), gin::StringToV8(isolate, message),
}; };
gin_helper::MicrotasksScope microtasks_scope{
wrapper->GetCreationContextChecked(), true,
v8::MicrotasksScope::kRunMicrotasks};
node::MakeCallback(isolate, wrapper, "emit", args.size(), args.data(), node::MakeCallback(isolate, wrapper, "emit", args.size(), args.data(),
{0, 0}); {0, 0});
} }

View file

@ -235,12 +235,11 @@ void ElectronBindings::DidReceiveMemoryDump(
base::ProcessId target_pid, base::ProcessId target_pid,
bool success, bool success,
std::unique_ptr<memory_instrumentation::GlobalMemoryDump> global_dump) { std::unique_ptr<memory_instrumentation::GlobalMemoryDump> global_dump) {
DCHECK(electron::IsBrowserProcess());
v8::Isolate* isolate = promise.isolate(); v8::Isolate* isolate = promise.isolate();
v8::HandleScope handle_scope(isolate); v8::HandleScope handle_scope(isolate);
v8::Local<v8::Context> local_context = v8::Local<v8::Context> local_context =
v8::Local<v8::Context>::New(isolate, context); v8::Local<v8::Context>::New(isolate, context);
gin_helper::MicrotasksScope microtasks_scope{
local_context, true, v8::MicrotasksScope::kRunMicrotasks};
v8::Context::Scope context_scope(local_context); v8::Context::Scope context_scope(local_context);
if (!success) { if (!success) {

View file

@ -215,8 +215,8 @@ void SpellCheckClient::SpellCheckWords(const SpellCheckScope& scope,
DCHECK(!scope.spell_check_.IsEmpty()); DCHECK(!scope.spell_check_.IsEmpty());
auto context = isolate_->GetCurrentContext(); auto context = isolate_->GetCurrentContext();
gin_helper::MicrotasksScope microtasks_scope{ v8::MicrotasksScope microtasks_scope(
context, false, v8::MicrotasksScope::kDoNotRunMicrotasks}; context, v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::Local<v8::FunctionTemplate> templ = gin_helper::CreateFunctionTemplate( v8::Local<v8::FunctionTemplate> templ = gin_helper::CreateFunctionTemplate(
isolate_, base::BindRepeating(&SpellCheckClient::OnSpellCheckDone, isolate_, base::BindRepeating(&SpellCheckClient::OnSpellCheckDone,

View file

@ -146,8 +146,8 @@ void ElectronSandboxedRendererClient::WillReleaseScriptContext(
return; return;
auto* isolate = context->GetIsolate(); auto* isolate = context->GetIsolate();
gin_helper::MicrotasksScope microtasks_scope{ v8::MicrotasksScope microtasks_scope(
context, false, v8::MicrotasksScope::kDoNotRunMicrotasks}; context, v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::HandleScope handle_scope(isolate); v8::HandleScope handle_scope(isolate);
v8::Context::Scope context_scope(context); v8::Context::Scope context_scope(context);
InvokeEmitProcessEvent(context, "exit"); InvokeEmitProcessEvent(context, "exit");
@ -164,8 +164,8 @@ void ElectronSandboxedRendererClient::EmitProcessEvent(
v8::HandleScope handle_scope{isolate}; v8::HandleScope handle_scope{isolate};
v8::Local<v8::Context> context = GetContext(frame, isolate); v8::Local<v8::Context> context = GetContext(frame, isolate);
gin_helper::MicrotasksScope microtasks_scope{ v8::MicrotasksScope microtasks_scope(
context, false, v8::MicrotasksScope::kDoNotRunMicrotasks}; context, v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::Context::Scope context_scope(context); v8::Context::Scope context_scope(context);
InvokeEmitProcessEvent(context, event_name); InvokeEmitProcessEvent(context, event_name);