refactor: allocate api::App on cpp heap (#48118)

This commit is contained in:
Robo 2025-08-20 20:35:08 +09:00 committed by GitHub
commit dd54e84a58
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 133 additions and 26 deletions

View file

@ -75,7 +75,6 @@
#include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/error_thrower.h"
#include "shell/common/gin_helper/handle.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/language_util.h"
#include "shell/common/node_includes.h"
@ -83,6 +82,8 @@
#include "shell/common/thread_restrictions.h"
#include "shell/common/v8_util.h"
#include "ui/gfx/image/image.h"
#include "v8/include/cppgc/allocation.h"
#include "v8/include/v8-traced-handle.h"
#if BUILDFLAG(IS_WIN)
#include "base/strings/utf_string_conversions.h"
@ -365,7 +366,11 @@ struct Converter<net::SecureDnsMode> {
namespace electron::api {
gin::DeprecatedWrapperInfo App::kWrapperInfo = {gin::kEmbedderNativeGin};
gin::WrapperInfo App::kWrapperInfo = {{gin::kEmbedderNativeGin},
gin::kElectronApp};
// static
cppgc::Persistent<App> App::instance_;
namespace {
@ -561,7 +566,6 @@ App::App() {
App::~App() {
static_cast<ElectronBrowserClient*>(ElectronBrowserClient::Get())
->set_delegate(nullptr);
Browser::Get()->RemoveObserver(this);
content::GpuDataManager::GetInstance()->RemoveObserver(this);
content::BrowserChildProcessObserver::Remove(this);
}
@ -1582,7 +1586,7 @@ void DockSetMenu(electron::api::Menu* menu) {
}
v8::Local<v8::Value> App::GetDockAPI(v8::Isolate* isolate) {
if (dock_.IsEmpty()) {
if (dock_.IsEmptyThreadSafe()) {
// Initialize the Dock API, the methods are bound to "dock" which exists
// for the lifetime of "app"
auto browser = base::Unretained(Browser::Get());
@ -1610,7 +1614,7 @@ v8::Local<v8::Value> App::GetDockAPI(v8::Isolate* isolate) {
dock_.Reset(isolate, dock_obj.GetHandle());
}
return v8::Local<v8::Value>::New(isolate, dock_);
return dock_.Get(isolate);
}
#endif
@ -1697,18 +1701,33 @@ void ConfigureHostResolver(v8::Isolate* isolate,
// static
App* App::Get() {
static base::NoDestructor<App> app;
return app.get();
CHECK_NE(instance_, nullptr);
return instance_.Get();
}
// static
gin_helper::Handle<App> App::Create(v8::Isolate* isolate) {
return gin_helper::CreateHandle(isolate, Get());
App* App::Create(v8::Isolate* isolate) {
if (!instance_) {
instance_ = cppgc::MakeGarbageCollected<App>(
isolate->GetCppHeap()->GetAllocationHandle());
}
return instance_.Get();
}
const gin::WrapperInfo* App::wrapper_info() const {
return &kWrapperInfo;
}
void App::Trace(cppgc::Visitor* visitor) const {
gin::Wrappable<App>::Trace(visitor);
#if BUILDFLAG(IS_MAC)
visitor->Trace(dock_);
#endif
}
gin::ObjectTemplateBuilder App::GetObjectTemplateBuilder(v8::Isolate* isolate) {
auto browser = base::Unretained(Browser::Get());
return gin_helper::EventEmitterMixin<App>::GetObjectTemplateBuilder(isolate)
return gin::Wrappable<App>::GetObjectTemplateBuilder(isolate)
.SetMethod("quit", base::BindRepeating(&Browser::Quit, browser))
.SetMethod("exit", base::BindRepeating(&Browser::Exit, browser))
.SetMethod("focus", base::BindRepeating(&Browser::Focus, browser))
@ -1850,8 +1869,8 @@ gin::ObjectTemplateBuilder App::GetObjectTemplateBuilder(v8::Isolate* isolate) {
.SetMethod("resolveProxy", &App::ResolveProxy);
}
const char* App::GetTypeName() {
return "App";
const char* App::GetHumanReadableName() const {
return "Electron / App";
}
} // namespace electron::api