Files
electron/shell/common/gin_helper/handle.h
deepak1556 676605bdf3 chore: address blink gc plugin errors
Key fixes:
- Replace `base::WeakPtrFactory` with `gin::WeakCellFactory` in
  MenuMac, MenuViews, and NetLog, since weak pointers to cppgc-managed
  objects must go through weak cells
- Replace `v8::Global<v8::Value>` with `cppgc::Persistent<Menu>` for
  the menu reference in BaseWindow
- Stop using `gin_helper::Handle<T>` with cppgc types; use raw `T*`
  and add a `static_assert` to prevent future misuse
- Add proper `Trace()` overrides for Menu, MenuMac, MenuViews, and
  NetLog to ensure cppgc members are visited during garbage collection
- Replace `SelfKeepAlive` prevent-GC mechanism in Menu with a
  `cppgc::Persistent` prevent-GC captured in `BindSelfToClosure`
- Introduce `GC_PLUGIN_IGNORE` macro to suppress
  known-safe violations: mojo::Remote fields, ObjC bridging pointers,
  and intentional persistent self-references
- Mark `ArgumentHolder` as `CPPGC_STACK_ALLOCATED()` in both Electron's
  and gin's function_template.h to silence raw-pointer-to-GC-type
  warnings
2026-04-09 12:31:25 +09:00

86 lines
2.7 KiB
C++

// Copyright (c) 2025 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_HANDLE_H_
#define ELECTRON_SHELL_COMMON_GIN_HELPER_HANDLE_H_
#include "base/memory/raw_ptr.h"
#include "gin/converter.h"
#include "v8/include/cppgc/type-traits.h"
namespace gin_helper {
// You can use gin_helper::Handle on the stack to retain a gin_helper::Wrappable
// object. Currently we don't have a mechanism for retaining a
// gin_helper::Wrappable object in the C++ heap because strong references from
// C++ to V8 can cause memory leaks. Copied from
// https://chromium-review.googlesource.com/c/chromium/src/+/6734440 Should be
// removed once https://github.com/electron/electron/issues/47922 is complete.
//
// This class must NOT be used with cppgc-managed types (gin::Wrappable).
// For cppgc types, use T* directly and gin::Converter<T*> for V8 conversion.
template <typename T>
class Handle {
static_assert(!cppgc::IsGarbageCollectedTypeV<T>,
"gin_helper::Handle must not be used with cppgc "
"garbage-collected types. Use T* directly instead.");
public:
Handle() : object_(nullptr) {}
Handle(v8::Local<v8::Value> wrapper, T* object)
: wrapper_(wrapper), object_(object) {}
bool IsEmpty() const { return !object_; }
void Clear() {
wrapper_.Clear();
object_ = NULL;
}
T* operator->() const { return object_; }
v8::Local<v8::Value> ToV8() const { return wrapper_; }
T* get() const { return object_; }
private:
v8::Local<v8::Value> wrapper_;
raw_ptr<T> object_;
};
// This function is a convenient way to create a handle from a raw pointer
// without having to write out the type of the object explicitly.
template <typename T>
gin_helper::Handle<T> CreateHandle(v8::Isolate* isolate, T* object) {
v8::Local<v8::Object> wrapper;
if (!object->GetWrapper(isolate).ToLocal(&wrapper) || wrapper.IsEmpty())
return gin_helper::Handle<T>();
return gin_helper::Handle<T>(wrapper, object);
}
} // namespace gin_helper
namespace gin {
template <typename T>
struct Converter<gin_helper::Handle<T>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const gin_helper::Handle<T>& val) {
return val.ToV8();
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
gin_helper::Handle<T>* out) {
T* object = NULL;
if (!Converter<T*>::FromV8(isolate, val, &object)) {
return false;
}
*out = gin_helper::Handle<T>(val, object);
return true;
}
};
} // namespace gin
#endif // ELECTRON_SHELL_COMMON_GIN_HELPER_HANDLE_H_