From 64554bcc13b91b80facf12dc8eabd4d59bfb76f2 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Fri, 10 Apr 2026 16:55:59 +0200 Subject: [PATCH] refactor: migrate `electron::api::Protocol` to cppgc (#50870) refactor: migrate api::Protocol to cppgc Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr --- ...ctron_objects_to_wrappablepointertag.patch | 5 +-- shell/browser/api/electron_api_protocol.cc | 34 ++++++++++-------- shell/browser/api/electron_api_protocol.h | 35 ++++++++----------- shell/browser/api/electron_api_session.cc | 10 +++--- shell/browser/api/electron_api_session.h | 7 ++-- shell/renderer/renderer_client_base.cc | 1 + 6 files changed, 46 insertions(+), 46 deletions(-) diff --git a/patches/chromium/chore_add_electron_objects_to_wrappablepointertag.patch b/patches/chromium/chore_add_electron_objects_to_wrappablepointertag.patch index 520e6e382c..f13a5de45b 100644 --- a/patches/chromium/chore_add_electron_objects_to_wrappablepointertag.patch +++ b/patches/chromium/chore_add_electron_objects_to_wrappablepointertag.patch @@ -8,10 +8,10 @@ electron objects that extend gin::Wrappable and gets allocated on the cpp heap diff --git a/gin/public/wrappable_pointer_tags.h b/gin/public/wrappable_pointer_tags.h -index fee622ebde42211de6f702b754cfa38595df5a1c..6b524632ebb405e473cf4fe8e253bd13bf7b67e5 100644 +index fee622ebde42211de6f702b754cfa38595df5a1c..9f7e1b1b8d871721891255c1f21de825d0df1e30 100644 --- a/gin/public/wrappable_pointer_tags.h +++ b/gin/public/wrappable_pointer_tags.h -@@ -77,7 +77,20 @@ enum WrappablePointerTag : uint16_t { +@@ -77,7 +77,21 @@ enum WrappablePointerTag : uint16_t { kWebAXObjectProxy, // content::WebAXObjectProxy kWrappedExceptionHandler, // extensions::WrappedExceptionHandler kIndigoContext, // indigo::IndigoContext @@ -24,6 +24,7 @@ index fee622ebde42211de6f702b754cfa38595df5a1c..6b524632ebb405e473cf4fe8e253bd13 + kElectronNetLog, // electron::api::NetLog + kElectronPowerMonitor, // electron::api::PowerMonitor + kElectronPowerSaveBlocker, // electron::api::PowerSaveBlocker ++ kElectronProtocol, // electron::api::Protocol + kElectronReplyChannel, // gin_helper::internal::ReplyChannel + kElectronScreen, // electron::api::Screen + kElectronSession, // electron::api::Session diff --git a/shell/browser/api/electron_api_protocol.cc b/shell/browser/api/electron_api_protocol.cc index 5c60d22c3c..d55f82d8b1 100644 --- a/shell/browser/api/electron_api_protocol.cc +++ b/shell/browser/api/electron_api_protocol.cc @@ -12,6 +12,7 @@ #include "base/no_destructor.h" #include "content/common/url_schemes.h" #include "content/public/browser/child_process_security_policy.h" +#include "gin/converter.h" #include "gin/object_template_builder.h" #include "shell/browser/browser.h" #include "shell/browser/javascript_environment.h" @@ -19,13 +20,13 @@ #include "shell/common/gin_converters/callback_converter.h" #include "shell/common/gin_converters/net_converter.h" #include "shell/common/gin_helper/dictionary.h" -#include "shell/common/gin_helper/handle.h" #include "shell/common/gin_helper/object_template_builder.h" #include "shell/common/gin_helper/promise.h" #include "shell/common/node_includes.h" #include "shell/common/node_util.h" #include "shell/common/options_switches.h" #include "url/url_util.h" +#include "v8/include/cppgc/allocation.h" namespace { @@ -81,7 +82,8 @@ struct Converter { namespace electron::api { -gin::DeprecatedWrapperInfo Protocol::kWrapperInfo = {gin::kEmbedderNativeGin}; +const gin::WrapperInfo Protocol::kWrapperInfo = {{gin::kEmbedderNativeGin}, + gin::kElectronProtocol}; std::vector& GetStandardSchemes() { static base::NoDestructor> g_standard_schemes; @@ -296,23 +298,22 @@ void Protocol::HandleOptionalCallback(gin::Arguments* args, Error error) { } // static -gin_helper::Handle Protocol::Create( - v8::Isolate* isolate, - ProtocolRegistry* protocol_registry) { - return gin_helper::CreateHandle(isolate, new Protocol{protocol_registry}); +Protocol* Protocol::Create(v8::Isolate* isolate, + ProtocolRegistry* protocol_registry) { + return cppgc::MakeGarbageCollected( + isolate->GetCppHeap()->GetAllocationHandle(), protocol_registry); } // static -gin_helper::Handle Protocol::New(gin_helper::ErrorThrower thrower) { +Protocol* Protocol::New(gin_helper::ErrorThrower thrower) { thrower.ThrowError("Protocol cannot be created from JS"); return {}; } // static -v8::Local Protocol::FillObjectTemplate( - v8::Isolate* isolate, - v8::Local tmpl) { - return gin::ObjectTemplateBuilder(isolate, GetClassName(), tmpl) +void Protocol::FillObjectTemplate(v8::Isolate* isolate, + v8::Local tmpl) { + gin::ObjectTemplateBuilder(isolate, GetClassName(), tmpl) .SetMethod("registerStringProtocol", &Protocol::RegisterProtocolFor) .SetMethod("registerBufferProtocol", @@ -345,8 +346,12 @@ v8::Local Protocol::FillObjectTemplate( .Build(); } -const char* Protocol::GetTypeName() { - return GetClassName(); +const gin::WrapperInfo* Protocol::wrapper_info() const { + return &kWrapperInfo; +} + +const char* Protocol::GetHumanReadableName() const { + return "Electron / Protocol"; } } // namespace electron::api @@ -372,7 +377,8 @@ void Initialize(v8::Local exports, v8::Isolate* const isolate = electron::JavascriptEnvironment::GetIsolate(); gin_helper::Dictionary dict{isolate, exports}; dict.Set("Protocol", - electron::api::Protocol::GetConstructor(isolate, context)); + electron::api::Protocol::GetConstructor( + isolate, context, &electron::api::Protocol::kWrapperInfo)); dict.SetMethod("registerSchemesAsPrivileged", &RegisterSchemesAsPrivileged); dict.SetMethod("getStandardSchemes", &electron::api::GetStandardSchemes); } diff --git a/shell/browser/api/electron_api_protocol.h b/shell/browser/api/electron_api_protocol.h index 79013260eb..c5b6dc5f23 100644 --- a/shell/browser/api/electron_api_protocol.h +++ b/shell/browser/api/electron_api_protocol.h @@ -9,20 +9,14 @@ #include #include "base/memory/raw_ptr.h" -#include "content/public/browser/content_browser_client.h" +#include "gin/wrappable.h" #include "shell/browser/net/electron_url_loader_factory.h" #include "shell/common/gin_helper/constructible.h" -#include "shell/common/gin_helper/wrappable.h" namespace gin { class Arguments; } // namespace gin -namespace gin_helper { -template -class Handle; -} // namespace gin_helper - namespace electron { class ProtocolRegistry; @@ -38,23 +32,25 @@ void RegisterSchemesAsPrivileged(gin_helper::ErrorThrower thrower, v8::Local val); // Protocol implementation based on network services. -class Protocol final : public gin_helper::DeprecatedWrappable, +class Protocol final : public gin::Wrappable, public gin_helper::Constructible { public: - static gin_helper::Handle Create( - v8::Isolate* isolate, - ProtocolRegistry* protocol_registry); + static Protocol* Create(v8::Isolate* isolate, + ProtocolRegistry* protocol_registry); // gin_helper::Constructible - static gin_helper::Handle New(gin_helper::ErrorThrower thrower); - static v8::Local FillObjectTemplate( - v8::Isolate* isolate, - v8::Local tmpl); + static Protocol* New(gin_helper::ErrorThrower thrower); + static void FillObjectTemplate(v8::Isolate* isolate, + v8::Local tmpl); static const char* GetClassName() { return "Protocol"; } - // gin_helper::Wrappable - static gin::DeprecatedWrapperInfo kWrapperInfo; - const char* GetTypeName() override; + // gin::Wrappable + static const gin::WrapperInfo kWrapperInfo; + const gin::WrapperInfo* wrapper_info() const override; + const char* GetHumanReadableName() const override; + + explicit Protocol(ProtocolRegistry* protocol_registry); + ~Protocol() override; private: // Possible errors. @@ -70,9 +66,6 @@ class Protocol final : public gin_helper::DeprecatedWrappable, using CompletionCallback = base::RepeatingCallback)>; - explicit Protocol(ProtocolRegistry* protocol_registry); - ~Protocol() override; - [[nodiscard]] static std::string_view ErrorCodeToString(Error error); // JS APIs. diff --git a/shell/browser/api/electron_api_session.cc b/shell/browser/api/electron_api_session.cc index 79306dd35b..0626bced7b 100644 --- a/shell/browser/api/electron_api_session.cc +++ b/shell/browser/api/electron_api_session.cc @@ -17,6 +17,7 @@ #include "base/files/file_enumerator.h" #include "base/files/file_path.h" #include "base/files/file_util.h" +#include "base/memory/weak_ptr.h" #include "base/scoped_observation.h" #include "base/strings/string_util.h" #include "base/types/pass_key.h" @@ -76,6 +77,7 @@ #include "shell/browser/media/media_device_id_salt.h" #include "shell/browser/net/cert_verifier_client.h" #include "shell/browser/net/resolve_host_function.h" +#include "shell/browser/net/resolve_proxy_helper.h" #include "shell/browser/session_preferences.h" #include "shell/common/gin_converters/callback_converter.h" #include "shell/common/gin_converters/content_converter.h" @@ -558,9 +560,7 @@ Session::Session(v8::Isolate* isolate, ElectronBrowserContext* browser_context) SessionPreferences::CreateForBrowserContext(browser_context); - protocol_.Reset( - isolate, - Protocol::Create(isolate, browser_context->protocol_registry()).ToV8()); + protocol_ = Protocol::Create(isolate, browser_context->protocol_registry()); browser_context->SetUserData( kElectronApiSessionKey, @@ -1354,8 +1354,8 @@ v8::Local Session::Extensions(v8::Isolate* isolate) { return extensions_.Get(isolate); } -v8::Local Session::Protocol(v8::Isolate* isolate) { - return protocol_.Get(isolate); +api::Protocol* Session::Protocol() { + return protocol_.Get(); } v8::Local Session::ServiceWorkerContext(v8::Isolate* isolate) { diff --git a/shell/browser/api/electron_api_session.h b/shell/browser/api/electron_api_session.h index 48c209d26c..f5e0a29edc 100644 --- a/shell/browser/api/electron_api_session.h +++ b/shell/browser/api/electron_api_session.h @@ -10,7 +10,6 @@ #include #include "base/memory/raw_ptr.h" -#include "base/memory/weak_ptr.h" #include "base/values.h" #include "content/public/browser/download_manager.h" #include "electron/buildflags/buildflags.h" @@ -20,7 +19,6 @@ #include "services/network/public/mojom/ssl_config.mojom-forward.h" #include "shell/browser/api/ipc_dispatcher.h" #include "shell/browser/event_emitter_mixin.h" -#include "shell/browser/net/resolve_proxy_helper.h" #include "shell/common/gin_helper/constructible.h" #include "shell/common/gin_helper/self_keep_alive.h" @@ -60,6 +58,7 @@ struct PreloadScript; namespace api { class NetLog; +class Protocol; class WebRequest; class Session final : public gin::Wrappable, @@ -167,7 +166,7 @@ class Session final : public gin::Wrappable, const gin_helper::Dictionary& options); v8::Local Cookies(v8::Isolate* isolate); v8::Local Extensions(v8::Isolate* isolate); - v8::Local Protocol(v8::Isolate* isolate); + api::Protocol* Protocol(); v8::Local ServiceWorkerContext(v8::Isolate* isolate); WebRequest* WebRequest(v8::Isolate* isolate); api::NetLog* NetLog(v8::Isolate* isolate); @@ -214,7 +213,7 @@ class Session final : public gin::Wrappable, // Cached gin_helper::Wrappable objects. v8::TracedReference cookies_; v8::TracedReference extensions_; - v8::TracedReference protocol_; + cppgc::Member protocol_; cppgc::Member net_log_; v8::TracedReference service_worker_context_; cppgc::Member web_request_; diff --git a/shell/renderer/renderer_client_base.cc b/shell/renderer/renderer_client_base.cc index 5fdcddea74..5f4dad2cb1 100644 --- a/shell/renderer/renderer_client_base.cc +++ b/shell/renderer/renderer_client_base.cc @@ -10,6 +10,7 @@ #include #include "base/command_line.h" +#include "base/functional/callback_helpers.h" #include "base/strings/string_split.h" #include "components/network_hints/renderer/web_prescient_networking_impl.h" #include "content/common/buildflags.h"