From e1e12318e232a2bfe51d68b51b052673685cb301 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Tue, 29 Jul 2025 10:34:18 -0400 Subject: [PATCH] refactor: avoid deprecated `v8::Context::GetIsolate()` calls (pt 2) (#47896) * refactor: add a v8::Isolate* arg to Constructible::GetConstructor() Co-authored-by: Charles Kerr * refactor: add a v8::Isolate* arg to NodeBindings::Initialize() This is needed for the GetConstructor() call Co-authored-by: Charles Kerr * refactor: avoid v8::Context::GetIsolate() call in GetIpcObject() by taking it as an arg Co-authored-by: Charles Kerr * refactor: avoid v8::Context::GetIsolate() call in ipc_native::EmitIPCEvent() by taking it as an arg Co-authored-by: Charles Kerr --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr --- shell/browser/api/electron_api_menu.cc | 2 +- shell/browser/api/electron_api_notification.cc | 2 +- shell/browser/api/electron_api_protocol.cc | 3 ++- .../api/electron_api_service_worker_main.cc | 3 ++- shell/browser/api/electron_api_session.cc | 2 +- shell/browser/api/electron_api_tray.cc | 2 +- shell/browser/api/electron_api_web_contents.cc | 2 +- .../browser/api/electron_api_web_frame_main.cc | 2 +- shell/browser/electron_browser_main_parts.cc | 11 ++++++----- shell/common/gin_helper/constructible.h | 2 +- shell/common/node_bindings.cc | 5 +++-- shell/common/node_bindings.h | 2 +- shell/renderer/electron_api_service_impl.cc | 4 ++-- shell/renderer/electron_ipc_native.cc | 17 ++++++++--------- shell/renderer/electron_ipc_native.h | 3 ++- shell/renderer/electron_renderer_client.cc | 2 +- shell/renderer/service_worker_data.cc | 3 ++- shell/services/node/node_service.cc | 11 ++++++----- 18 files changed, 42 insertions(+), 36 deletions(-) diff --git a/shell/browser/api/electron_api_menu.cc b/shell/browser/api/electron_api_menu.cc index 2387a7cd9ef1..817fa2ac7f97 100644 --- a/shell/browser/api/electron_api_menu.cc +++ b/shell/browser/api/electron_api_menu.cc @@ -327,7 +327,7 @@ void Initialize(v8::Local exports, void* priv) { v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate(); gin_helper::Dictionary dict{isolate, exports}; - dict.Set("Menu", Menu::GetConstructor(context)); + dict.Set("Menu", Menu::GetConstructor(isolate, context)); #if BUILDFLAG(IS_MAC) dict.SetMethod("setApplicationMenu", &Menu::SetApplicationMenu); dict.SetMethod("sendActionToFirstResponder", diff --git a/shell/browser/api/electron_api_notification.cc b/shell/browser/api/electron_api_notification.cc index af392d4ac9be..6ceae2e950fb 100644 --- a/shell/browser/api/electron_api_notification.cc +++ b/shell/browser/api/electron_api_notification.cc @@ -253,7 +253,7 @@ void Initialize(v8::Local exports, void* priv) { v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate(); gin_helper::Dictionary dict{isolate, exports}; - dict.Set("Notification", Notification::GetConstructor(context)); + dict.Set("Notification", Notification::GetConstructor(isolate, context)); dict.SetMethod("isSupported", &Notification::IsSupported); } diff --git a/shell/browser/api/electron_api_protocol.cc b/shell/browser/api/electron_api_protocol.cc index 65baf074b483..98307d5a9072 100644 --- a/shell/browser/api/electron_api_protocol.cc +++ b/shell/browser/api/electron_api_protocol.cc @@ -362,7 +362,8 @@ void Initialize(v8::Local exports, void* priv) { v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate(); gin_helper::Dictionary dict{isolate, exports}; - dict.Set("Protocol", electron::api::Protocol::GetConstructor(context)); + dict.Set("Protocol", + electron::api::Protocol::GetConstructor(isolate, context)); dict.SetMethod("registerSchemesAsPrivileged", &RegisterSchemesAsPrivileged); dict.SetMethod("getStandardSchemes", &electron::api::GetStandardSchemes); } diff --git a/shell/browser/api/electron_api_service_worker_main.cc b/shell/browser/api/electron_api_service_worker_main.cc index 2a7d9ba53e3a..4cf5791c2773 100644 --- a/shell/browser/api/electron_api_service_worker_main.cc +++ b/shell/browser/api/electron_api_service_worker_main.cc @@ -356,7 +356,8 @@ void Initialize(v8::Local exports, void* priv) { v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate(); gin_helper::Dictionary dict{isolate, exports}; - dict.Set("ServiceWorkerMain", ServiceWorkerMain::GetConstructor(context)); + dict.Set("ServiceWorkerMain", + ServiceWorkerMain::GetConstructor(isolate, context)); } } // namespace diff --git a/shell/browser/api/electron_api_session.cc b/shell/browser/api/electron_api_session.cc index 0bc37db951ea..582515e2673d 100644 --- a/shell/browser/api/electron_api_session.cc +++ b/shell/browser/api/electron_api_session.cc @@ -1869,7 +1869,7 @@ void Initialize(v8::Local exports, void* priv) { v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate(); gin_helper::Dictionary dict(isolate, exports); - dict.Set("Session", Session::GetConstructor(context)); + dict.Set("Session", Session::GetConstructor(isolate, context)); dict.SetMethod("fromPartition", &FromPartition); dict.SetMethod("fromPath", &FromPath); } diff --git a/shell/browser/api/electron_api_tray.cc b/shell/browser/api/electron_api_tray.cc index fdad80db72c9..4c6c30008e74 100644 --- a/shell/browser/api/electron_api_tray.cc +++ b/shell/browser/api/electron_api_tray.cc @@ -447,7 +447,7 @@ void Initialize(v8::Local exports, void* priv) { v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate(); gin::Dictionary dict{isolate, exports}; - dict.Set("Tray", Tray::GetConstructor(context)); + dict.Set("Tray", Tray::GetConstructor(isolate, context)); } } // namespace diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 4adf08df38c6..15f200a7b9dd 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -4662,7 +4662,7 @@ void Initialize(v8::Local exports, void* priv) { v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate(); gin_helper::Dictionary dict{isolate, exports}; - dict.Set("WebContents", WebContents::GetConstructor(context)); + dict.Set("WebContents", WebContents::GetConstructor(isolate, context)); dict.SetMethod("fromId", &WebContentsFromID); dict.SetMethod("fromFrame", &WebContentsFromFrame); dict.SetMethod("fromDevToolsTargetId", &WebContentsFromDevToolsTargetID); diff --git a/shell/browser/api/electron_api_web_frame_main.cc b/shell/browser/api/electron_api_web_frame_main.cc index 0685ae83775e..5422e3204d42 100644 --- a/shell/browser/api/electron_api_web_frame_main.cc +++ b/shell/browser/api/electron_api_web_frame_main.cc @@ -675,7 +675,7 @@ void Initialize(v8::Local exports, void* priv) { v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate(); gin_helper::Dictionary dict{isolate, exports}; - dict.Set("WebFrameMain", WebFrameMain::GetConstructor(context)); + dict.Set("WebFrameMain", WebFrameMain::GetConstructor(isolate, context)); dict.SetMethod("fromId", &FromID); dict.SetMethod("_fromIdIfExists", &FromIdIfExists); dict.SetMethod("_fromFtnIdIfExists", &FromFtnIdIfExists); diff --git a/shell/browser/electron_browser_main_parts.cc b/shell/browser/electron_browser_main_parts.cc index 7d8cbf08a266..aaa45b082659 100644 --- a/shell/browser/electron_browser_main_parts.cc +++ b/shell/browser/electron_browser_main_parts.cc @@ -232,13 +232,14 @@ void ElectronBrowserMainParts::PostEarlyInitialization() { // avoid conflicts we only initialize our V8 environment after that. js_env_ = std::make_unique(node_bindings_->uv_loop()); - v8::HandleScope scope(js_env_->isolate()); + v8::Isolate* const isolate = js_env_->isolate(); + v8::HandleScope scope(isolate); - node_bindings_->Initialize(js_env_->isolate()->GetCurrentContext()); + node_bindings_->Initialize(isolate, isolate->GetCurrentContext()); // Create the global environment. node_env_ = node_bindings_->CreateEnvironment( - js_env_->isolate(), js_env_->isolate()->GetCurrentContext(), - js_env_->platform(), js_env_->max_young_generation_size_in_bytes()); + isolate, isolate->GetCurrentContext(), js_env_->platform(), + js_env_->max_young_generation_size_in_bytes()); node_env_->set_trace_sync_io(node_env_->options()->trace_sync_io); @@ -246,7 +247,7 @@ void ElectronBrowserMainParts::PostEarlyInitialization() { node_env_->options()->unhandled_rejections = "warn-with-error-code"; // Add Electron extended APIs. - electron_bindings_->BindTo(js_env_->isolate(), node_env_->process_object()); + electron_bindings_->BindTo(isolate, node_env_->process_object()); // Create explicit microtasks runner. js_env_->CreateMicrotasksRunner(); diff --git a/shell/common/gin_helper/constructible.h b/shell/common/gin_helper/constructible.h index b1453ba993a3..82117c955e32 100644 --- a/shell/common/gin_helper/constructible.h +++ b/shell/common/gin_helper/constructible.h @@ -40,8 +40,8 @@ template class Constructible { public: static v8::Local GetConstructor( + v8::Isolate* const isolate, v8::Local context) { - v8::Isolate* isolate = context->GetIsolate(); gin::PerIsolateData* data = gin::PerIsolateData::From(isolate); auto* wrapper_info = &T::kWrapperInfo; v8::Local constructor = diff --git a/shell/common/node_bindings.cc b/shell/common/node_bindings.cc index bc6843428656..47b9cf5f1adb 100644 --- a/shell/common/node_bindings.cc +++ b/shell/common/node_bindings.cc @@ -576,7 +576,8 @@ std::vector NodeBindings::ParseNodeCliFlags() { return args; } -void NodeBindings::Initialize(v8::Local context) { +void NodeBindings::Initialize(v8::Isolate* const isolate, + v8::Local context) { TRACE_EVENT0("electron", "NodeBindings::Initialize"); // Open node's error reporting system for browser process. @@ -635,7 +636,7 @@ void NodeBindings::Initialize(v8::Local context) { SetErrorMode(GetErrorMode() & ~SEM_NOGPFAULTERRORBOX); #endif - gin_helper::internal::Event::GetConstructor(context); + gin_helper::internal::Event::GetConstructor(isolate, context); g_is_initialized = true; } diff --git a/shell/common/node_bindings.h b/shell/common/node_bindings.h index 54491be42fd0..b3974ec80f6b 100644 --- a/shell/common/node_bindings.h +++ b/shell/common/node_bindings.h @@ -125,7 +125,7 @@ class NodeBindings { virtual ~NodeBindings(); // Setup V8, libuv. - void Initialize(v8::Local context); + void Initialize(v8::Isolate* isolate, v8::Local context); std::vector ParseNodeCliFlags(); diff --git a/shell/renderer/electron_api_service_impl.cc b/shell/renderer/electron_api_service_impl.cc index 739404631c8e..e527061455ce 100644 --- a/shell/renderer/electron_api_service_impl.cc +++ b/shell/renderer/electron_api_service_impl.cc @@ -98,7 +98,7 @@ void ElectronApiServiceImpl::Message(bool internal, v8::Local args = gin::ConvertToV8(isolate, arguments); - ipc_native::EmitIPCEvent(context, internal, channel, {}, args); + ipc_native::EmitIPCEvent(isolate, context, internal, channel, {}, args); } void ElectronApiServiceImpl::ReceivePostMessage( @@ -125,7 +125,7 @@ void ElectronApiServiceImpl::ReceivePostMessage( std::vector> args = {message_value}; - ipc_native::EmitIPCEvent(context, false, channel, ports, + ipc_native::EmitIPCEvent(isolate, context, false, channel, ports, gin::ConvertToV8(isolate, args)); } diff --git a/shell/renderer/electron_ipc_native.cc b/shell/renderer/electron_ipc_native.cc index 045ffbd457e6..36396ad2ff48 100644 --- a/shell/renderer/electron_ipc_native.cc +++ b/shell/renderer/electron_ipc_native.cc @@ -19,8 +19,8 @@ namespace { constexpr std::string_view kIpcKey = "ipcNative"; // Gets the private object under kIpcKey -v8::Local GetIpcObject(const v8::Local& context) { - auto* isolate = context->GetIsolate(); +v8::Local GetIpcObject(v8::Isolate* const isolate, + const v8::Local& context) { auto binding_key = gin::StringToV8(isolate, kIpcKey); auto private_binding_key = v8::Private::ForApi(isolate, binding_key); auto global_object = context->Global(); @@ -33,13 +33,13 @@ v8::Local GetIpcObject(const v8::Local& context) { return value->ToObject(context).ToLocalChecked(); } -void InvokeIpcCallback(const v8::Local& context, +void InvokeIpcCallback(v8::Isolate* const isolate, + const v8::Local& context, const std::string& callback_name, std::vector> args) { TRACE_EVENT0("devtools.timeline", "FunctionCall"); - auto* isolate = context->GetIsolate(); - auto ipcNative = GetIpcObject(context); + auto ipcNative = GetIpcObject(isolate, context); if (ipcNative.IsEmpty()) return; @@ -62,13 +62,12 @@ void InvokeIpcCallback(const v8::Local& context, } // namespace -void EmitIPCEvent(const v8::Local& context, +void EmitIPCEvent(v8::Isolate* const isolate, + const v8::Local& context, bool internal, const std::string& channel, std::vector> ports, v8::Local args) { - auto* isolate = context->GetIsolate(); - v8::HandleScope handle_scope(isolate); v8::Context::Scope context_scope(context); v8::MicrotasksScope script_scope(isolate, context->GetMicrotaskQueue(), @@ -78,7 +77,7 @@ void EmitIPCEvent(const v8::Local& context, gin::ConvertToV8(isolate, internal), gin::ConvertToV8(isolate, channel), gin::ConvertToV8(isolate, ports), args}; - InvokeIpcCallback(context, "onMessage", argv); + InvokeIpcCallback(isolate, context, "onMessage", argv); } } // namespace electron::ipc_native diff --git a/shell/renderer/electron_ipc_native.h b/shell/renderer/electron_ipc_native.h index 8580735aad91..ae8e2521652d 100644 --- a/shell/renderer/electron_ipc_native.h +++ b/shell/renderer/electron_ipc_native.h @@ -11,7 +11,8 @@ namespace electron::ipc_native { -void EmitIPCEvent(const v8::Local& context, +void EmitIPCEvent(v8::Isolate* isolate, + const v8::Local& context, bool internal, const std::string& channel, std::vector> ports, diff --git a/shell/renderer/electron_renderer_client.cc b/shell/renderer/electron_renderer_client.cc index 61b2df668655..5e8b126662b8 100644 --- a/shell/renderer/electron_renderer_client.cc +++ b/shell/renderer/electron_renderer_client.cc @@ -105,7 +105,7 @@ void ElectronRendererClient::DidCreateScriptContext( if (!node_integration_initialized_) { node_integration_initialized_ = true; - node_bindings_->Initialize(renderer_context); + node_bindings_->Initialize(isolate, renderer_context); node_bindings_->PrepareEmbedThread(); } diff --git a/shell/renderer/service_worker_data.cc b/shell/renderer/service_worker_data.cc index 618ab20492bc..63f6f3364112 100644 --- a/shell/renderer/service_worker_data.cc +++ b/shell/renderer/service_worker_data.cc @@ -56,7 +56,8 @@ void ServiceWorkerData::Message(bool internal, v8::Local args = gin::ConvertToV8(isolate, arguments); - ipc_native::EmitIPCEvent(preload_context, internal, channel, {}, args); + ipc_native::EmitIPCEvent(isolate, preload_context, internal, channel, {}, + args); } void ServiceWorkerData::ReceivePostMessage(const std::string& channel, diff --git a/shell/services/node/node_service.cc b/shell/services/node/node_service.cc index c9207fc36a1c..e983ea293df9 100644 --- a/shell/services/node/node_service.cc +++ b/shell/services/node/node_service.cc @@ -122,9 +122,10 @@ void NodeService::Initialize( js_env_ = std::make_unique(node_bindings_->uv_loop()); - v8::HandleScope scope(js_env_->isolate()); + v8::Isolate* const isolate = js_env_->isolate(); + v8::HandleScope scope{isolate}; - node_bindings_->Initialize(js_env_->isolate()->GetCurrentContext()); + node_bindings_->Initialize(isolate, isolate->GetCurrentContext()); // Append program path for process.argv0 auto program = base::CommandLine::ForCurrentProcess()->GetProgram(); @@ -136,9 +137,9 @@ void NodeService::Initialize( // Create the global environment. node_env_ = node_bindings_->CreateEnvironment( - js_env_->isolate(), js_env_->isolate()->GetCurrentContext(), - js_env_->platform(), js_env_->max_young_generation_size_in_bytes(), - params->args, params->exec_args); + isolate, isolate->GetCurrentContext(), js_env_->platform(), + js_env_->max_young_generation_size_in_bytes(), params->args, + params->exec_args); // Override the default handler set by NodeBindings. node_env_->isolate()->SetFatalErrorHandler(V8FatalErrorCallback);