refactor: make api::NetLog inherit from gin::Wrappable (#48308)

* refactor: remove unused v8::Isolate* arg from NetLog ctor

* refactor: allocate api::NetLog on cpp heap

* refactor: make electron::api::Session::net_log_ a cppgc::Member<api::NetLog>

* refactor: remove unnecessary EscapableHandleScope

* chore: code style consistency
This commit is contained in:
Charles Kerr
2025-09-15 16:29:46 -05:00
committed by GitHub
parent bac383c219
commit 15cdae1cf0
5 changed files with 48 additions and 37 deletions

View File

@@ -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 80ec409efe1635390887d1324be661643818abff..2b82f96d12715b6476c456b73dfd1a7342736f8e 100644
index 80ec409efe1635390887d1324be661643818abff..2e20b63d1fca56efb43c18d0fa04b1e2b4cf339a 100644
--- a/gin/public/wrappable_pointer_tags.h
+++ b/gin/public/wrappable_pointer_tags.h
@@ -66,7 +66,11 @@ enum WrappablePointerTag : uint16_t {
@@ -66,7 +66,12 @@ enum WrappablePointerTag : uint16_t {
kTextInputControllerBindings, // content::TextInputControllerBindings
kWebAXObjectProxy, // content::WebAXObjectProxy
kWrappedExceptionHandler, // extensions::WrappedExceptionHandler
@@ -19,6 +19,7 @@ index 80ec409efe1635390887d1324be661643818abff..2b82f96d12715b6476c456b73dfd1a73
+ kElectronApp, // electron::api::App
+ kElectronDebugger, // electron::api::Debugger
+ kElectronEvent, // gin_helper::internal::Event
+ kElectronNetLog, // electron::api::NetLog
+ kElectronSession, // electron::api::Session
+ kLastPointerTag = kElectronEvent,
};

View File

@@ -22,6 +22,8 @@
#include "shell/common/gin_converters/file_path_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/handle.h"
#include "v8/include/cppgc/allocation.h"
#include "v8/include/v8-cppgc.h"
namespace gin {
@@ -79,9 +81,10 @@ void ResolvePromiseWithNetError(gin_helper::Promise<void> promise,
namespace api {
gin::DeprecatedWrapperInfo NetLog::kWrapperInfo = {gin::kEmbedderNativeGin};
gin::WrapperInfo NetLog::kWrapperInfo = {{gin::kEmbedderNativeGin},
gin::kElectronNetLog};
NetLog::NetLog(v8::Isolate* isolate, ElectronBrowserContext* browser_context)
NetLog::NetLog(ElectronBrowserContext* const browser_context)
: browser_context_(browser_context) {
file_task_runner_ = CreateFileTaskRunner();
}
@@ -219,23 +222,25 @@ v8::Local<v8::Promise> NetLog::StopLogging(v8::Isolate* const isolate) {
gin::ObjectTemplateBuilder NetLog::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin_helper::DeprecatedWrappable<NetLog>::GetObjectTemplateBuilder(
isolate)
return gin::Wrappable<NetLog>::GetObjectTemplateBuilder(isolate)
.SetProperty("currentlyLogging", &NetLog::IsCurrentlyLogging)
.SetMethod("startLogging", &NetLog::StartLogging)
.SetMethod("stopLogging", &NetLog::StopLogging);
}
const char* NetLog::GetTypeName() {
return "NetLog";
const gin::WrapperInfo* NetLog::wrapper_info() const {
return &kWrapperInfo;
}
const char* NetLog::GetHumanReadableName() const {
return "Electron / NetLog";
}
// static
gin_helper::Handle<NetLog> NetLog::Create(
v8::Isolate* isolate,
ElectronBrowserContext* browser_context) {
return gin_helper::CreateHandle(isolate,
new NetLog(isolate, browser_context));
NetLog* NetLog::Create(v8::Isolate* isolate,
ElectronBrowserContext* browser_context) {
return cppgc::MakeGarbageCollected<NetLog>(
isolate->GetCppHeap()->GetAllocationHandle(), browser_context);
}
} // namespace api

View File

@@ -10,11 +10,11 @@
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "gin/wrappable.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/log/net_log_capture_mode.h"
#include "services/network/public/mojom/net_log.mojom.h"
#include "shell/common/gin_helper/promise.h"
#include "shell/common/gin_helper/wrappable.h"
namespace base {
class FilePath;
@@ -37,32 +37,32 @@ class ElectronBrowserContext;
namespace api {
// The code is referenced from the net_log::NetExportFileWriter class.
class NetLog final : public gin_helper::DeprecatedWrappable<NetLog> {
class NetLog final : public gin::Wrappable<NetLog> {
public:
static gin_helper::Handle<NetLog> Create(
v8::Isolate* isolate,
ElectronBrowserContext* browser_context);
static NetLog* Create(v8::Isolate* isolate,
ElectronBrowserContext* browser_context);
// Make public for cppgc::MakeGarbageCollected.
explicit NetLog(ElectronBrowserContext* browser_context);
~NetLog() override;
// disable copy
NetLog(const NetLog&) = delete;
NetLog& operator=(const NetLog&) = delete;
// gin_helper::Wrappable
static gin::WrapperInfo kWrapperInfo;
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
const gin::WrapperInfo* wrapper_info() const override;
const char* GetHumanReadableName() const override;
v8::Local<v8::Promise> StartLogging(base::FilePath log_path,
gin::Arguments* args);
v8::Local<v8::Promise> StopLogging(v8::Isolate* isolate);
bool IsCurrentlyLogging() const;
// gin_helper::Wrappable
static gin::DeprecatedWrapperInfo kWrapperInfo;
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
const char* GetTypeName() override;
// disable copy
NetLog(const NetLog&) = delete;
NetLog& operator=(const NetLog&) = delete;
protected:
explicit NetLog(v8::Isolate* isolate,
ElectronBrowserContext* browser_context);
~NetLog() override;
void OnConnectionError();
void StartNetLogAfterCreateFile(net::NetLogCaptureMode capture_mode,

View File

@@ -1361,11 +1361,14 @@ v8::Local<v8::Value> Session::WebRequest(v8::Isolate* isolate) {
}
v8::Local<v8::Value> Session::NetLog(v8::Isolate* isolate) {
if (net_log_.IsEmptyThreadSafe()) {
auto handle = NetLog::Create(isolate, browser_context());
net_log_.Reset(isolate, handle.ToV8());
if (!net_log_) {
net_log_ = NetLog::Create(isolate, browser_context());
}
return net_log_.Get(isolate);
v8::Local<v8::Object> wrapper;
return net_log_->GetWrapper(isolate).ToLocal(&wrapper)
? wrapper.As<v8::Value>()
: v8::Null(isolate);
}
static void StartPreconnectOnUI(ElectronBrowserContext* browser_context,

View File

@@ -60,6 +60,8 @@ struct PreloadScript;
namespace api {
class NetLog;
class Session final : public gin::Wrappable<Session>,
public gin_helper::Constructible<Session>,
public gin_helper::EventEmitterMixin<Session>,
@@ -208,7 +210,7 @@ class Session final : public gin::Wrappable<Session>,
v8::TracedReference<v8::Value> cookies_;
v8::TracedReference<v8::Value> extensions_;
v8::TracedReference<v8::Value> protocol_;
v8::TracedReference<v8::Value> net_log_;
cppgc::Member<api::NetLog> net_log_;
v8::TracedReference<v8::Value> service_worker_context_;
v8::TracedReference<v8::Value> web_request_;