Skip to content

Commit 15cdae1

Browse files
authored
refactor: make api::NetLog inherit from gin::Wrappable (electron#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
1 parent bac383c commit 15cdae1

File tree

5 files changed

+47
-36
lines changed

5 files changed

+47
-36
lines changed

patches/chromium/chore_add_electron_objects_to_wrappablepointertag.patch

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ electron objects that extend gin::Wrappable and gets
88
allocated on the cpp heap
99

1010
diff --git a/gin/public/wrappable_pointer_tags.h b/gin/public/wrappable_pointer_tags.h
11-
index 80ec409efe1635390887d1324be661643818abff..2b82f96d12715b6476c456b73dfd1a7342736f8e 100644
11+
index 80ec409efe1635390887d1324be661643818abff..2e20b63d1fca56efb43c18d0fa04b1e2b4cf339a 100644
1212
--- a/gin/public/wrappable_pointer_tags.h
1313
+++ b/gin/public/wrappable_pointer_tags.h
14-
@@ -66,7 +66,11 @@ enum WrappablePointerTag : uint16_t {
14+
@@ -66,7 +66,12 @@ enum WrappablePointerTag : uint16_t {
1515
kTextInputControllerBindings, // content::TextInputControllerBindings
1616
kWebAXObjectProxy, // content::WebAXObjectProxy
1717
kWrappedExceptionHandler, // extensions::WrappedExceptionHandler
1818
- kLastPointerTag = kWrappedExceptionHandler,
1919
+ kElectronApp, // electron::api::App
2020
+ kElectronDebugger, // electron::api::Debugger
2121
+ kElectronEvent, // gin_helper::internal::Event
22+
+ kElectronNetLog, // electron::api::NetLog
2223
+ kElectronSession, // electron::api::Session
2324
+ kLastPointerTag = kElectronEvent,
2425
};

shell/browser/api/electron_api_net_log.cc

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "shell/common/gin_converters/file_path_converter.h"
2323
#include "shell/common/gin_helper/dictionary.h"
2424
#include "shell/common/gin_helper/handle.h"
25+
#include "v8/include/cppgc/allocation.h"
26+
#include "v8/include/v8-cppgc.h"
2527

2628
namespace gin {
2729

@@ -79,9 +81,10 @@ void ResolvePromiseWithNetError(gin_helper::Promise<void> promise,
7981

8082
namespace api {
8183

82-
gin::DeprecatedWrapperInfo NetLog::kWrapperInfo = {gin::kEmbedderNativeGin};
84+
gin::WrapperInfo NetLog::kWrapperInfo = {{gin::kEmbedderNativeGin},
85+
gin::kElectronNetLog};
8386

84-
NetLog::NetLog(v8::Isolate* isolate, ElectronBrowserContext* browser_context)
87+
NetLog::NetLog(ElectronBrowserContext* const browser_context)
8588
: browser_context_(browser_context) {
8689
file_task_runner_ = CreateFileTaskRunner();
8790
}
@@ -219,23 +222,25 @@ v8::Local<v8::Promise> NetLog::StopLogging(v8::Isolate* const isolate) {
219222

220223
gin::ObjectTemplateBuilder NetLog::GetObjectTemplateBuilder(
221224
v8::Isolate* isolate) {
222-
return gin_helper::DeprecatedWrappable<NetLog>::GetObjectTemplateBuilder(
223-
isolate)
225+
return gin::Wrappable<NetLog>::GetObjectTemplateBuilder(isolate)
224226
.SetProperty("currentlyLogging", &NetLog::IsCurrentlyLogging)
225227
.SetMethod("startLogging", &NetLog::StartLogging)
226228
.SetMethod("stopLogging", &NetLog::StopLogging);
227229
}
228230

229-
const char* NetLog::GetTypeName() {
230-
return "NetLog";
231+
const gin::WrapperInfo* NetLog::wrapper_info() const {
232+
return &kWrapperInfo;
233+
}
234+
235+
const char* NetLog::GetHumanReadableName() const {
236+
return "Electron / NetLog";
231237
}
232238

233239
// static
234-
gin_helper::Handle<NetLog> NetLog::Create(
235-
v8::Isolate* isolate,
236-
ElectronBrowserContext* browser_context) {
237-
return gin_helper::CreateHandle(isolate,
238-
new NetLog(isolate, browser_context));
240+
NetLog* NetLog::Create(v8::Isolate* isolate,
241+
ElectronBrowserContext* browser_context) {
242+
return cppgc::MakeGarbageCollected<NetLog>(
243+
isolate->GetCppHeap()->GetAllocationHandle(), browser_context);
239244
}
240245

241246
} // namespace api

shell/browser/api/electron_api_net_log.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
#include "base/memory/raw_ptr.h"
1111
#include "base/memory/weak_ptr.h"
1212
#include "base/values.h"
13+
#include "gin/wrappable.h"
1314
#include "mojo/public/cpp/bindings/remote.h"
1415
#include "net/log/net_log_capture_mode.h"
1516
#include "services/network/public/mojom/net_log.mojom.h"
1617
#include "shell/common/gin_helper/promise.h"
17-
#include "shell/common/gin_helper/wrappable.h"
1818

1919
namespace base {
2020
class FilePath;
@@ -37,32 +37,32 @@ class ElectronBrowserContext;
3737
namespace api {
3838

3939
// The code is referenced from the net_log::NetExportFileWriter class.
40-
class NetLog final : public gin_helper::DeprecatedWrappable<NetLog> {
40+
class NetLog final : public gin::Wrappable<NetLog> {
4141
public:
42-
static gin_helper::Handle<NetLog> Create(
43-
v8::Isolate* isolate,
44-
ElectronBrowserContext* browser_context);
42+
static NetLog* Create(v8::Isolate* isolate,
43+
ElectronBrowserContext* browser_context);
4544

46-
v8::Local<v8::Promise> StartLogging(base::FilePath log_path,
47-
gin::Arguments* args);
48-
v8::Local<v8::Promise> StopLogging(v8::Isolate* isolate);
49-
bool IsCurrentlyLogging() const;
45+
// Make public for cppgc::MakeGarbageCollected.
46+
explicit NetLog(ElectronBrowserContext* browser_context);
47+
~NetLog() override;
48+
49+
// disable copy
50+
NetLog(const NetLog&) = delete;
51+
NetLog& operator=(const NetLog&) = delete;
5052

5153
// gin_helper::Wrappable
52-
static gin::DeprecatedWrapperInfo kWrapperInfo;
54+
static gin::WrapperInfo kWrapperInfo;
5355
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
5456
v8::Isolate* isolate) override;
55-
const char* GetTypeName() override;
57+
const gin::WrapperInfo* wrapper_info() const override;
58+
const char* GetHumanReadableName() const override;
5659

57-
// disable copy
58-
NetLog(const NetLog&) = delete;
59-
NetLog& operator=(const NetLog&) = delete;
60+
v8::Local<v8::Promise> StartLogging(base::FilePath log_path,
61+
gin::Arguments* args);
62+
v8::Local<v8::Promise> StopLogging(v8::Isolate* isolate);
63+
bool IsCurrentlyLogging() const;
6064

6165
protected:
62-
explicit NetLog(v8::Isolate* isolate,
63-
ElectronBrowserContext* browser_context);
64-
~NetLog() override;
65-
6666
void OnConnectionError();
6767

6868
void StartNetLogAfterCreateFile(net::NetLogCaptureMode capture_mode,

shell/browser/api/electron_api_session.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,11 +1361,14 @@ v8::Local<v8::Value> Session::WebRequest(v8::Isolate* isolate) {
13611361
}
13621362

13631363
v8::Local<v8::Value> Session::NetLog(v8::Isolate* isolate) {
1364-
if (net_log_.IsEmptyThreadSafe()) {
1365-
auto handle = NetLog::Create(isolate, browser_context());
1366-
net_log_.Reset(isolate, handle.ToV8());
1364+
if (!net_log_) {
1365+
net_log_ = NetLog::Create(isolate, browser_context());
13671366
}
1368-
return net_log_.Get(isolate);
1367+
1368+
v8::Local<v8::Object> wrapper;
1369+
return net_log_->GetWrapper(isolate).ToLocal(&wrapper)
1370+
? wrapper.As<v8::Value>()
1371+
: v8::Null(isolate);
13691372
}
13701373

13711374
static void StartPreconnectOnUI(ElectronBrowserContext* browser_context,

shell/browser/api/electron_api_session.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ struct PreloadScript;
6060

6161
namespace api {
6262

63+
class NetLog;
64+
6365
class Session final : public gin::Wrappable<Session>,
6466
public gin_helper::Constructible<Session>,
6567
public gin_helper::EventEmitterMixin<Session>,
@@ -208,7 +210,7 @@ class Session final : public gin::Wrappable<Session>,
208210
v8::TracedReference<v8::Value> cookies_;
209211
v8::TracedReference<v8::Value> extensions_;
210212
v8::TracedReference<v8::Value> protocol_;
211-
v8::TracedReference<v8::Value> net_log_;
213+
cppgc::Member<api::NetLog> net_log_;
212214
v8::TracedReference<v8::Value> service_worker_context_;
213215
v8::TracedReference<v8::Value> web_request_;
214216

0 commit comments

Comments
 (0)