refactor: allocate api::App on cpp heap (#48118)
This commit is contained in:
parent
d4b7d9e9cf
commit
dd54e84a58
7 changed files with 133 additions and 26 deletions
|
@ -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
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include "content/public/browser/scoped_accessibility_mode.h"
|
||||
#include "crypto/crypto_buildflags.h"
|
||||
#include "electron/mas.h"
|
||||
#include "gin/wrappable.h"
|
||||
#include "net/base/completion_once_callback.h"
|
||||
#include "net/base/completion_repeating_callback.h"
|
||||
#include "net/base/features.h"
|
||||
|
@ -27,7 +28,7 @@
|
|||
#include "shell/browser/browser_observer.h"
|
||||
#include "shell/browser/electron_browser_client.h"
|
||||
#include "shell/browser/event_emitter_mixin.h"
|
||||
#include "shell/common/gin_helper/wrappable.h"
|
||||
#include "v8/include/cppgc/persistent.h"
|
||||
|
||||
#if BUILDFLAG(USE_NSS_CERTS)
|
||||
#include "shell/browser/certificate_manager_model.h"
|
||||
|
@ -40,10 +41,13 @@ class FilePath;
|
|||
namespace gin_helper {
|
||||
class Dictionary;
|
||||
class ErrorThrower;
|
||||
template <typename T>
|
||||
class Handle;
|
||||
} // namespace gin_helper
|
||||
|
||||
namespace v8 {
|
||||
template <typename T>
|
||||
class TracedReference;
|
||||
}
|
||||
|
||||
namespace electron {
|
||||
|
||||
struct ProcessMetric;
|
||||
|
@ -54,21 +58,23 @@ enum class JumpListResult : int;
|
|||
|
||||
namespace api {
|
||||
|
||||
class App final : public ElectronBrowserClient::Delegate,
|
||||
public gin_helper::DeprecatedWrappable<App>,
|
||||
class App final : public gin::Wrappable<App>,
|
||||
public ElectronBrowserClient::Delegate,
|
||||
public gin_helper::EventEmitterMixin<App>,
|
||||
private BrowserObserver,
|
||||
private content::GpuDataManagerObserver,
|
||||
private content::BrowserChildProcessObserver {
|
||||
public:
|
||||
static gin_helper::Handle<App> Create(v8::Isolate* isolate);
|
||||
static App* Create(v8::Isolate* isolate);
|
||||
static App* Get();
|
||||
|
||||
// gin_helper::Wrappable
|
||||
static gin::DeprecatedWrapperInfo kWrapperInfo;
|
||||
// gin::Wrappable
|
||||
static gin::WrapperInfo kWrapperInfo;
|
||||
void Trace(cppgc::Visitor*) const override;
|
||||
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
||||
v8::Isolate* isolate) override;
|
||||
const char* GetTypeName() override;
|
||||
const gin::WrapperInfo* wrapper_info() const override;
|
||||
const char* GetHumanReadableName() const override;
|
||||
|
||||
#if BUILDFLAG(USE_NSS_CERTS)
|
||||
void OnCertificateManagerModelCreated(
|
||||
|
@ -84,14 +90,13 @@ class App final : public ElectronBrowserClient::Delegate,
|
|||
static bool IsPackaged();
|
||||
|
||||
App();
|
||||
~App() override;
|
||||
|
||||
// disable copy
|
||||
App(const App&) = delete;
|
||||
App& operator=(const App&) = delete;
|
||||
|
||||
private:
|
||||
~App() override;
|
||||
|
||||
// BrowserObserver:
|
||||
void OnBeforeQuit(bool* prevent_default) override;
|
||||
void OnWillQuit(bool* prevent_default) override;
|
||||
|
@ -236,7 +241,7 @@ class App final : public ElectronBrowserClient::Delegate,
|
|||
bool MoveToApplicationsFolder(gin_helper::ErrorThrower, gin::Arguments* args);
|
||||
bool IsInApplicationsFolder();
|
||||
v8::Local<v8::Value> GetDockAPI(v8::Isolate* isolate);
|
||||
v8::Global<v8::Value> dock_;
|
||||
v8::TracedReference<v8::Value> dock_;
|
||||
#endif
|
||||
|
||||
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
|
||||
|
@ -277,6 +282,8 @@ class App final : public ElectronBrowserClient::Delegate,
|
|||
bool watch_singleton_socket_on_ready_ = false;
|
||||
|
||||
std::unique_ptr<content::ScopedAccessibilityMode> scoped_accessibility_mode_;
|
||||
|
||||
static cppgc::Persistent<App> instance_;
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue