refactor: use gin::Wrappable for electron::api::PowerMonitor (#49509)

* refactor: use `gin::Wrappable` for `electron::api::PowerMonitor`

* chore: update patches
This commit is contained in:
Charles Kerr
2026-01-25 22:51:31 -06:00
committed by GitHub
parent e8250f9955
commit 30f365d9d8
3 changed files with 39 additions and 29 deletions

View File

@@ -8,20 +8,21 @@ 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 573bcb2e56068a2ade6d8ab28964b077487874fd..41759b87e1aec4ec125560b9a52828fb44bc5999 100644
index 573bcb2e56068a2ade6d8ab28964b077487874fd..acb0c0b44f6530e49b32ea7602c25d498ae4f210 100644
--- a/gin/public/wrappable_pointer_tags.h
+++ b/gin/public/wrappable_pointer_tags.h
@@ -74,7 +74,18 @@ enum WrappablePointerTag : uint16_t {
@@ -74,7 +74,19 @@ enum WrappablePointerTag : uint16_t {
kTextInputControllerBindings, // content::TextInputControllerBindings
kWebAXObjectProxy, // content::WebAXObjectProxy
kWrappedExceptionHandler, // extensions::WrappedExceptionHandler
- kLastPointerTag = kWrappedExceptionHandler,
+ kElectronApp, // electron::api::App
+ kElectronDebugger, // electron::api::Debugger
+ kElectronDataPipeHolder, // electron::api::DataPipeHolder
+ kElectronDebugger, // electron::api::Debugger
+ kElectronEvent, // gin_helper::internal::Event
+ kElectronMenu, // electron::api::Menu
+ kElectronNetLog, // electron::api::NetLog
+ kElectronPowerMonitor, // electron::api::PowerMonitor
+ kElectronPowerSaveBlocker, // electron::api::PowerSaveBlocker
+ kElectronReplyChannel, // gin_helper::internal::ReplyChannel
+ kElectronScreen, // electron::api::Screen

View File

@@ -5,16 +5,15 @@
#include "shell/browser/api/electron_api_power_monitor.h"
#include "base/power_monitor/power_monitor.h"
#include "base/power_monitor/power_monitor_device_source.h"
#include "base/power_monitor/power_observer.h"
#include "gin/data_object_builder.h"
#include "shell/browser/browser.h"
#include "shell/browser/javascript_environment.h"
#include "shell/common/gin_converters/callback_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/node_includes.h"
#include "ui/base/idle/idle.h"
#include "v8/include/cppgc/allocation.h"
#include "v8/include/v8-cppgc.h"
namespace gin {
@@ -60,10 +59,11 @@ struct Converter<base::PowerThermalObserver::DeviceThermalState> {
namespace electron::api {
gin::DeprecatedWrapperInfo PowerMonitor::kWrapperInfo = {
gin::kEmbedderNativeGin};
const gin::WrapperInfo PowerMonitor::kWrapperInfo = {
{gin::kEmbedderNativeGin},
gin::kElectronPowerMonitor};
PowerMonitor::PowerMonitor(v8::Isolate* isolate) {
PowerMonitor::PowerMonitor() {
#if BUILDFLAG(IS_MAC)
Browser::Get()->SetShutdownHandler(base::BindRepeating(
&PowerMonitor::ShouldShutdown, base::Unretained(this)));
@@ -142,11 +142,9 @@ void PowerMonitor::SetListeningForShutdown(bool is_listening) {
#endif
// static
v8::Local<v8::Value> PowerMonitor::Create(v8::Isolate* isolate) {
auto* pm = new PowerMonitor(isolate);
auto handle = gin_helper::CreateHandle(isolate, pm).ToV8();
pm->Pin(isolate);
return handle;
PowerMonitor* PowerMonitor::Create(v8::Isolate* isolate) {
return cppgc::MakeGarbageCollected<PowerMonitor>(
isolate->GetCppHeap()->GetAllocationHandle());
}
gin::ObjectTemplateBuilder PowerMonitor::GetObjectTemplateBuilder(
@@ -161,8 +159,12 @@ gin::ObjectTemplateBuilder PowerMonitor::GetObjectTemplateBuilder(
return builder;
}
const char* PowerMonitor::GetTypeName() {
return "PowerMonitor";
const gin::WrapperInfo* PowerMonitor::wrapper_info() const {
return &kWrapperInfo;
}
const char* PowerMonitor::GetHumanReadableName() const {
return "Electron / PowerMonitor";
}
} // namespace electron::api

View File

@@ -6,40 +6,45 @@
#define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_POWER_MONITOR_H_
#include "base/power_monitor/power_observer.h"
#include "gin/wrappable.h"
#include "shell/browser/event_emitter_mixin.h"
#include "shell/common/gin_helper/pinnable.h"
#include "shell/common/gin_helper/wrappable.h"
#include "ui/base/idle/idle.h"
#include "shell/common/gin_helper/self_keep_alive.h"
#if BUILDFLAG(IS_LINUX)
#include "shell/browser/lib/power_observer_linux.h"
#endif
namespace gin {
class ObjectTemplateBuilder;
} // namespace gin
namespace electron::api {
class PowerMonitor final : public gin_helper::DeprecatedWrappable<PowerMonitor>,
class PowerMonitor final : public gin::Wrappable<PowerMonitor>,
public gin_helper::EventEmitterMixin<PowerMonitor>,
public gin_helper::Pinnable<PowerMonitor>,
private base::PowerStateObserver,
private base::PowerSuspendObserver,
private base::PowerThermalObserver {
public:
static v8::Local<v8::Value> Create(v8::Isolate* isolate);
static PowerMonitor* Create(v8::Isolate* isolate);
// gin_helper::Wrappable
static gin::DeprecatedWrapperInfo kWrapperInfo;
// gin::Wrappable
static const gin::WrapperInfo kWrapperInfo;
const gin::WrapperInfo* wrapper_info() const override;
const char* GetHumanReadableName() const override;
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
const char* GetTypeName() override;
const char* GetClassName() const { return "PowerMonitor"; }
// Make public for cppgc::MakeGarbageCollected.
PowerMonitor();
~PowerMonitor() override;
// disable copy
PowerMonitor(const PowerMonitor&) = delete;
PowerMonitor& operator=(const PowerMonitor&) = delete;
private:
explicit PowerMonitor(v8::Isolate* isolate);
~PowerMonitor() override;
#if BUILDFLAG(IS_LINUX)
void SetListeningForShutdown(bool);
#endif
@@ -88,6 +93,8 @@ class PowerMonitor final : public gin_helper::DeprecatedWrappable<PowerMonitor>,
#if BUILDFLAG(IS_LINUX)
PowerObserverLinux power_observer_linux_{this};
#endif
gin_helper::SelfKeepAlive<PowerMonitor> keep_alive_{this};
};
} // namespace electron::api