From 54b31f153c0cf7096725f4673522734e7a497e45 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Mon, 5 Aug 2024 21:46:51 -0500 Subject: [PATCH] fix: gin_helper::MicrotasksScope instantiation (#43209) Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr --- .../browser/api/electron_api_auto_updater.cc | 4 ++-- shell/common/api/electron_bindings.cc | 5 +++-- shell/common/gin_helper/callback.h | 15 ++++++++------ .../common/gin_helper/event_emitter_caller.cc | 5 +++-- shell/common/gin_helper/function_template.h | 10 ++++++---- shell/common/gin_helper/microtasks_scope.h | 9 ++++----- shell/common/gin_helper/promise.cc | 20 +++++++++++-------- shell/common/gin_helper/promise.h | 5 +++-- shell/common/node_bindings.cc | 2 +- shell/common/v8_value_serializer.cc | 6 +++--- .../api/electron_api_spell_check_client.cc | 6 +++--- .../electron_sandboxed_renderer_client.cc | 12 +++++------ 12 files changed, 55 insertions(+), 44 deletions(-) diff --git a/shell/browser/api/electron_api_auto_updater.cc b/shell/browser/api/electron_api_auto_updater.cc index 387fda264de3..18d2b402ff6c 100644 --- a/shell/browser/api/electron_api_auto_updater.cc +++ b/shell/browser/api/electron_api_auto_updater.cc @@ -46,9 +46,9 @@ void AutoUpdater::OnError(const std::string& message) { gin::StringToV8(isolate, message), }; - gin_helper::MicrotasksScope microtasks_scope( + gin_helper::MicrotasksScope microtasks_scope{ isolate, wrapper->GetCreationContextChecked()->GetMicrotaskQueue(), - true); + true, v8::MicrotasksScope::kRunMicrotasks}; node::MakeCallback(isolate, wrapper, "emit", args.size(), args.data(), {0, 0}); diff --git a/shell/common/api/electron_bindings.cc b/shell/common/api/electron_bindings.cc index 549bccca4b17..0ad917d98106 100644 --- a/shell/common/api/electron_bindings.cc +++ b/shell/common/api/electron_bindings.cc @@ -240,8 +240,9 @@ void ElectronBindings::DidReceiveMemoryDump( v8::HandleScope handle_scope(isolate); v8::Local local_context = v8::Local::New(isolate, context); - gin_helper::MicrotasksScope microtasks_scope( - isolate, local_context->GetMicrotaskQueue(), true); + gin_helper::MicrotasksScope microtasks_scope{ + isolate, local_context->GetMicrotaskQueue(), true, + v8::MicrotasksScope::kRunMicrotasks}; v8::Context::Scope context_scope(local_context); if (!success) { diff --git a/shell/common/gin_helper/callback.h b/shell/common/gin_helper/callback.h index d047e7fdbb73..cea4a73a1bcb 100644 --- a/shell/common/gin_helper/callback.h +++ b/shell/common/gin_helper/callback.h @@ -50,8 +50,9 @@ struct V8FunctionInvoker(ArgTypes...)> { return v8::Null(isolate); v8::Local holder = function.NewHandle(isolate); v8::Local context = holder->GetCreationContextChecked(); - gin_helper::MicrotasksScope microtasks_scope( - isolate, context->GetMicrotaskQueue(), true); + gin_helper::MicrotasksScope microtasks_scope{ + isolate, context->GetMicrotaskQueue(), true, + v8::MicrotasksScope::kRunMicrotasks}; v8::Context::Scope context_scope(context); std::vector> args{ gin::ConvertToV8(isolate, std::forward(raw))...}; @@ -75,8 +76,9 @@ struct V8FunctionInvoker { return; v8::Local holder = function.NewHandle(isolate); v8::Local context = holder->GetCreationContextChecked(); - gin_helper::MicrotasksScope microtasks_scope( - isolate, context->GetMicrotaskQueue(), true); + gin_helper::MicrotasksScope microtasks_scope{ + isolate, context->GetMicrotaskQueue(), true, + v8::MicrotasksScope::kRunMicrotasks}; v8::Context::Scope context_scope(context); std::vector> args{ gin::ConvertToV8(isolate, std::forward(raw))...}; @@ -99,8 +101,9 @@ struct V8FunctionInvoker { return ret; v8::Local holder = function.NewHandle(isolate); v8::Local context = holder->GetCreationContextChecked(); - gin_helper::MicrotasksScope microtasks_scope( - isolate, context->GetMicrotaskQueue(), true); + gin_helper::MicrotasksScope microtasks_scope{ + isolate, context->GetMicrotaskQueue(), true, + v8::MicrotasksScope::kRunMicrotasks}; v8::Context::Scope context_scope(context); std::vector> args{ gin::ConvertToV8(isolate, std::forward(raw))...}; diff --git a/shell/common/gin_helper/event_emitter_caller.cc b/shell/common/gin_helper/event_emitter_caller.cc index c18c2a7895a0..124a300cb4e9 100644 --- a/shell/common/gin_helper/event_emitter_caller.cc +++ b/shell/common/gin_helper/event_emitter_caller.cc @@ -24,8 +24,9 @@ v8::Local CallMethodWithArgs(v8::Isolate* isolate, } // Perform microtask checkpoint after running JavaScript. - gin_helper::MicrotasksScope microtasks_scope( - isolate, obj->GetCreationContextChecked()->GetMicrotaskQueue(), true); + gin_helper::MicrotasksScope microtasks_scope{ + isolate, obj->GetCreationContextChecked()->GetMicrotaskQueue(), true, + v8::MicrotasksScope::kRunMicrotasks}; // node::MakeCallback will also run pending tasks in Node.js. v8::MaybeLocal ret = node::MakeCallback( diff --git a/shell/common/gin_helper/function_template.h b/shell/common/gin_helper/function_template.h index f1b276c75da7..8aa4859f5786 100644 --- a/shell/common/gin_helper/function_template.h +++ b/shell/common/gin_helper/function_template.h @@ -262,9 +262,10 @@ class Invoker, ArgTypes...> template void DispatchToCallback( base::RepeatingCallback callback) { - gin_helper::MicrotasksScope microtasks_scope( + gin_helper::MicrotasksScope microtasks_scope{ args_->isolate(), - args_->GetHolderCreationContext()->GetMicrotaskQueue(), true); + args_->GetHolderCreationContext()->GetMicrotaskQueue(), true, + v8::MicrotasksScope::kRunMicrotasks}; args_->Return( callback.Run(std::move(ArgumentHolder::value)...)); } @@ -273,9 +274,10 @@ class Invoker, ArgTypes...> // expression to foo. As a result, we must specialize the case of Callbacks // that have the void return type. void DispatchToCallback(base::RepeatingCallback callback) { - gin_helper::MicrotasksScope microtasks_scope( + gin_helper::MicrotasksScope microtasks_scope{ args_->isolate(), - args_->GetHolderCreationContext()->GetMicrotaskQueue(), true); + args_->GetHolderCreationContext()->GetMicrotaskQueue(), true, + v8::MicrotasksScope::kRunMicrotasks}; callback.Run(std::move(ArgumentHolder::value)...); } diff --git a/shell/common/gin_helper/microtasks_scope.h b/shell/common/gin_helper/microtasks_scope.h index a8d27d3ccbdc..ad0dd7c8cc68 100644 --- a/shell/common/gin_helper/microtasks_scope.h +++ b/shell/common/gin_helper/microtasks_scope.h @@ -15,11 +15,10 @@ namespace gin_helper { // In the render process creates a v8::MicrotasksScope. class MicrotasksScope { public: - explicit MicrotasksScope(v8::Isolate* isolate, - v8::MicrotaskQueue* microtask_queue, - bool ignore_browser_checkpoint = false, - v8::MicrotasksScope::Type scope_type = - v8::MicrotasksScope::kRunMicrotasks); + MicrotasksScope(v8::Isolate* isolate, + v8::MicrotaskQueue* microtask_queue, + bool ignore_browser_checkpoint, + v8::MicrotasksScope::Type scope_type); ~MicrotasksScope(); // disable copy diff --git a/shell/common/gin_helper/promise.cc b/shell/common/gin_helper/promise.cc index bc659c9c2fda..57e67d6d2e76 100644 --- a/shell/common/gin_helper/promise.cc +++ b/shell/common/gin_helper/promise.cc @@ -29,8 +29,9 @@ PromiseBase& PromiseBase::operator=(PromiseBase&&) = default; v8::Maybe PromiseBase::Reject() { v8::HandleScope handle_scope(isolate()); - gin_helper::MicrotasksScope microtasks_scope( - isolate(), GetContext()->GetMicrotaskQueue()); + gin_helper::MicrotasksScope microtasks_scope{ + isolate(), GetContext()->GetMicrotaskQueue(), false, + v8::MicrotasksScope::kRunMicrotasks}; v8::Context::Scope context_scope(GetContext()); return GetInner()->Reject(GetContext(), v8::Undefined(isolate())); @@ -38,8 +39,9 @@ v8::Maybe PromiseBase::Reject() { v8::Maybe PromiseBase::Reject(v8::Local except) { v8::HandleScope handle_scope(isolate()); - gin_helper::MicrotasksScope microtasks_scope( - isolate(), GetContext()->GetMicrotaskQueue()); + gin_helper::MicrotasksScope microtasks_scope{ + isolate(), GetContext()->GetMicrotaskQueue(), false, + v8::MicrotasksScope::kRunMicrotasks}; v8::Context::Scope context_scope(GetContext()); return GetInner()->Reject(GetContext(), except); @@ -48,8 +50,9 @@ v8::Maybe PromiseBase::Reject(v8::Local except) { v8::Maybe PromiseBase::RejectWithErrorMessage( const std::string_view message) { v8::HandleScope handle_scope(isolate()); - gin_helper::MicrotasksScope microtasks_scope( - isolate(), GetContext()->GetMicrotaskQueue()); + gin_helper::MicrotasksScope microtasks_scope{ + isolate(), GetContext()->GetMicrotaskQueue(), false, + v8::MicrotasksScope::kRunMicrotasks}; v8::Context::Scope context_scope(GetContext()); v8::Local error = @@ -91,8 +94,9 @@ v8::Local Promise::ResolvedPromise(v8::Isolate* isolate) { v8::Maybe Promise::Resolve() { v8::HandleScope handle_scope(isolate()); - gin_helper::MicrotasksScope microtasks_scope( - isolate(), GetContext()->GetMicrotaskQueue()); + gin_helper::MicrotasksScope microtasks_scope{ + isolate(), GetContext()->GetMicrotaskQueue(), false, + v8::MicrotasksScope::kRunMicrotasks}; v8::Context::Scope context_scope(GetContext()); return GetInner()->Resolve(GetContext(), v8::Undefined(isolate())); diff --git a/shell/common/gin_helper/promise.h b/shell/common/gin_helper/promise.h index ee1b41b57caf..ce6e44f9ee8a 100644 --- a/shell/common/gin_helper/promise.h +++ b/shell/common/gin_helper/promise.h @@ -122,8 +122,9 @@ class Promise : public PromiseBase { v8::Maybe Resolve(const RT& value) { gin_helper::Locker locker(isolate()); v8::HandleScope handle_scope(isolate()); - gin_helper::MicrotasksScope microtasks_scope( - isolate(), GetContext()->GetMicrotaskQueue()); + gin_helper::MicrotasksScope microtasks_scope{ + isolate(), GetContext()->GetMicrotaskQueue(), false, + v8::MicrotasksScope::kRunMicrotasks}; v8::Context::Scope context_scope(GetContext()); return GetInner()->Resolve(GetContext(), diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc index 84f8f5ea91f4..dc7180da0d64 100644 --- a/shell/common/node_bindings.cc +++ b/shell/common/node_bindings.cc @@ -286,7 +286,7 @@ void ErrorMessageListener(v8::Local message, node::Environment* env = node::Environment::GetCurrent(isolate); if (env) { gin_helper::MicrotasksScope microtasks_scope( - isolate, env->context()->GetMicrotaskQueue(), + isolate, env->context()->GetMicrotaskQueue(), false, v8::MicrotasksScope::kDoNotRunMicrotasks); // Emit the after() hooks now that the exception has been handled. // Analogous to node/lib/internal/process/execution.js#L176-L180 diff --git a/shell/common/v8_value_serializer.cc b/shell/common/v8_value_serializer.cc index 5420adb4dbfd..96251a149ad3 100644 --- a/shell/common/v8_value_serializer.cc +++ b/shell/common/v8_value_serializer.cc @@ -34,9 +34,9 @@ class V8Serializer : public v8::ValueSerializer::Delegate { ~V8Serializer() override = default; bool Serialize(v8::Local value, blink::CloneableMessage* out) { - gin_helper::MicrotasksScope microtasks_scope( - isolate_, isolate_->GetCurrentContext()->GetMicrotaskQueue(), - v8::MicrotasksScope::kDoNotRunMicrotasks); + gin_helper::MicrotasksScope microtasks_scope{ + isolate_, isolate_->GetCurrentContext()->GetMicrotaskQueue(), false, + v8::MicrotasksScope::kDoNotRunMicrotasks}; WriteBlinkEnvelope(19); serializer_.WriteHeader(); diff --git a/shell/renderer/api/electron_api_spell_check_client.cc b/shell/renderer/api/electron_api_spell_check_client.cc index 79bcc8044be7..d99fbe851415 100644 --- a/shell/renderer/api/electron_api_spell_check_client.cc +++ b/shell/renderer/api/electron_api_spell_check_client.cc @@ -220,9 +220,9 @@ void SpellCheckClient::SpellCheckWords(const SpellCheckScope& scope, DCHECK(!scope.spell_check_.IsEmpty()); auto context = isolate_->GetCurrentContext(); - gin_helper::MicrotasksScope microtasks_scope( - isolate_, context->GetMicrotaskQueue(), - v8::MicrotasksScope::kDoNotRunMicrotasks); + gin_helper::MicrotasksScope microtasks_scope{ + isolate_, context->GetMicrotaskQueue(), false, + v8::MicrotasksScope::kDoNotRunMicrotasks}; v8::Local templ = gin_helper::CreateFunctionTemplate( isolate_, diff --git a/shell/renderer/electron_sandboxed_renderer_client.cc b/shell/renderer/electron_sandboxed_renderer_client.cc index 77cb3071288e..109b2729257a 100644 --- a/shell/renderer/electron_sandboxed_renderer_client.cc +++ b/shell/renderer/electron_sandboxed_renderer_client.cc @@ -207,9 +207,9 @@ void ElectronSandboxedRendererClient::WillReleaseScriptContext( return; auto* isolate = context->GetIsolate(); - gin_helper::MicrotasksScope microtasks_scope( - isolate, context->GetMicrotaskQueue(), - v8::MicrotasksScope::kDoNotRunMicrotasks); + gin_helper::MicrotasksScope microtasks_scope{ + isolate, context->GetMicrotaskQueue(), false, + v8::MicrotasksScope::kDoNotRunMicrotasks}; v8::HandleScope handle_scope(isolate); v8::Context::Scope context_scope(context); InvokeEmitProcessEvent(context, "exit"); @@ -226,9 +226,9 @@ void ElectronSandboxedRendererClient::EmitProcessEvent( v8::HandleScope handle_scope{isolate}; v8::Local context = GetContext(frame, isolate); - gin_helper::MicrotasksScope microtasks_scope( - isolate, context->GetMicrotaskQueue(), - v8::MicrotasksScope::kDoNotRunMicrotasks); + gin_helper::MicrotasksScope microtasks_scope{ + isolate, context->GetMicrotaskQueue(), false, + v8::MicrotasksScope::kDoNotRunMicrotasks}; v8::Context::Scope context_scope(context); InvokeEmitProcessEvent(context, event_name);