mirror of
https://github.com/electron/electron.git
synced 2026-01-09 15:38:08 -05:00
refactor: allocate api::Session on cpp heap (#48141)
This commit is contained in:
@@ -577,11 +577,11 @@ gin::ObjectTemplateBuilder NativeImage::GetObjectTemplateBuilder(
|
||||
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
|
||||
auto* wrapper_info = &kWrapperInfo;
|
||||
v8::Local<v8::FunctionTemplate> constructor =
|
||||
data->GetFunctionTemplate(wrapper_info);
|
||||
data->DeprecatedGetFunctionTemplate(wrapper_info);
|
||||
if (constructor.IsEmpty()) {
|
||||
constructor = v8::FunctionTemplate::New(isolate);
|
||||
constructor->SetClassName(gin::StringToV8(isolate, GetTypeName()));
|
||||
data->SetFunctionTemplate(wrapper_info, constructor);
|
||||
data->DeprecatedSetFunctionTemplate(wrapper_info, constructor);
|
||||
}
|
||||
return gin::ObjectTemplateBuilder(isolate, GetTypeName(),
|
||||
constructor->InstanceTemplate())
|
||||
|
||||
@@ -698,14 +698,15 @@ gin_helper::Handle<SimpleURLLoaderWrapper> SimpleURLLoaderWrapper::Create(
|
||||
ElectronBrowserContext* browser_context = nullptr;
|
||||
if (electron::IsBrowserProcess()) {
|
||||
std::string partition;
|
||||
gin_helper::Handle<Session> session;
|
||||
Session* session = nullptr;
|
||||
if (!opts.Get("session", &session)) {
|
||||
if (opts.Get("partition", &partition))
|
||||
session = Session::FromPartition(args->isolate(), partition);
|
||||
else // default session
|
||||
session = Session::FromPartition(args->isolate(), "");
|
||||
}
|
||||
browser_context = session->browser_context();
|
||||
if (session)
|
||||
browser_context = session->browser_context();
|
||||
}
|
||||
|
||||
auto ret = gin_helper::CreateHandle(
|
||||
|
||||
@@ -45,7 +45,7 @@ class Constructible {
|
||||
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
|
||||
auto* wrapper_info = &T::kWrapperInfo;
|
||||
v8::Local<v8::FunctionTemplate> constructor =
|
||||
data->GetFunctionTemplate(wrapper_info);
|
||||
data->DeprecatedGetFunctionTemplate(wrapper_info);
|
||||
if (constructor.IsEmpty()) {
|
||||
constructor = gin::CreateConstructorFunctionTemplate(
|
||||
isolate, base::BindRepeating(&T::New));
|
||||
@@ -59,6 +59,30 @@ class Constructible {
|
||||
T::FillObjectTemplate(isolate, constructor->PrototypeTemplate());
|
||||
data->DeprecatedSetObjectTemplate(wrapper_info,
|
||||
constructor->InstanceTemplate());
|
||||
data->DeprecatedSetFunctionTemplate(wrapper_info, constructor);
|
||||
}
|
||||
return constructor->GetFunction(context).ToLocalChecked();
|
||||
}
|
||||
|
||||
static v8::Local<v8::Function> GetConstructor(
|
||||
v8::Isolate* const isolate,
|
||||
v8::Local<v8::Context> context,
|
||||
gin::WrapperInfo* wrapper_info) {
|
||||
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
|
||||
v8::Local<v8::FunctionTemplate> constructor =
|
||||
data->GetFunctionTemplate(wrapper_info);
|
||||
if (constructor.IsEmpty()) {
|
||||
constructor = gin::CreateConstructorFunctionTemplate(
|
||||
isolate, base::BindRepeating(&T::New));
|
||||
if (std::is_base_of<EventEmitterMixin<T>, T>::value) {
|
||||
constructor->Inherit(
|
||||
gin_helper::internal::GetEventEmitterTemplate(isolate));
|
||||
}
|
||||
constructor->InstanceTemplate()->SetInternalFieldCount(
|
||||
gin::kNumberOfInternalFields);
|
||||
constructor->SetClassName(gin::StringToV8(isolate, T::GetClassName()));
|
||||
T::FillObjectTemplate(isolate, constructor->PrototypeTemplate());
|
||||
data->SetObjectTemplate(wrapper_info, constructor->InstanceTemplate());
|
||||
data->SetFunctionTemplate(wrapper_info, constructor);
|
||||
}
|
||||
return constructor->GetFunction(context).ToLocalChecked();
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include "base/containers/span.h"
|
||||
#include "gin/converter.h"
|
||||
#include "gin/wrappable.h"
|
||||
#include "shell/common/gin_converters/std_converter.h" // for ConvertToV8(iso, &&)
|
||||
#include "shell/common/gin_helper/wrappable.h"
|
||||
|
||||
@@ -76,6 +77,28 @@ v8::Local<v8::Value> CallMethod(gin_helper::DeprecatedWrappable<T>* object,
|
||||
return CallMethod(isolate, object, method_name, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
v8::Local<v8::Value> CallMethod(v8::Isolate* isolate,
|
||||
gin::Wrappable<T>* object,
|
||||
const char* method_name,
|
||||
Args&&... args) {
|
||||
v8::EscapableHandleScope scope(isolate);
|
||||
v8::Local<v8::Object> v8_object;
|
||||
if (object->GetWrapper(isolate).ToLocal(&v8_object))
|
||||
return scope.Escape(CustomEmit(isolate, v8_object, method_name,
|
||||
std::forward<Args>(args)...));
|
||||
else
|
||||
return {};
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
v8::Local<v8::Value> CallMethod(gin::Wrappable<T>* object,
|
||||
const char* method_name,
|
||||
Args&&... args) {
|
||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||
return CallMethod(isolate, object, method_name, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
} // namespace gin_helper
|
||||
|
||||
#endif // ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_
|
||||
|
||||
@@ -17,7 +17,7 @@ gin::DeprecatedWrapperInfo kWrapperInfo = {gin::kEmbedderNativeGin};
|
||||
v8::Local<v8::FunctionTemplate> GetEventEmitterTemplate(v8::Isolate* isolate) {
|
||||
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
|
||||
v8::Local<v8::FunctionTemplate> tmpl =
|
||||
data->GetFunctionTemplate(&kWrapperInfo);
|
||||
data->DeprecatedGetFunctionTemplate(&kWrapperInfo);
|
||||
|
||||
if (tmpl.IsEmpty()) {
|
||||
tmpl = v8::FunctionTemplate::New(isolate);
|
||||
@@ -35,7 +35,7 @@ v8::Local<v8::FunctionTemplate> GetEventEmitterTemplate(v8::Isolate* isolate) {
|
||||
->SetPrototypeV2(context, eventemitter_prototype)
|
||||
.ToChecked());
|
||||
|
||||
data->SetFunctionTemplate(&kWrapperInfo, tmpl);
|
||||
data->DeprecatedSetFunctionTemplate(&kWrapperInfo, tmpl);
|
||||
}
|
||||
|
||||
return tmpl;
|
||||
|
||||
@@ -81,6 +81,7 @@ class CallbackHolderBase {
|
||||
|
||||
// gin::PerIsolateData::DisposeObserver
|
||||
void OnBeforeDispose(v8::Isolate* isolate) override;
|
||||
void OnBeforeMicrotasksRunnerDispose(v8::Isolate* isolate) override {}
|
||||
void OnDisposed() override;
|
||||
|
||||
private:
|
||||
|
||||
36
shell/common/gin_helper/self_keep_alive.h
Normal file
36
shell/common/gin_helper/self_keep_alive.h
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright (c) 2025 Microsoft, GmbH.
|
||||
// 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_SELF_KEEP_ALIVE_H_
|
||||
#define ELECTRON_SHELL_COMMON_GIN_HELPER_SELF_KEEP_ALIVE_H_
|
||||
|
||||
#include "gin/weak_cell.h"
|
||||
|
||||
namespace gin_helper {
|
||||
|
||||
// Based on third_party/blink/renderer/platform/heap/self_keep_alive.h
|
||||
template <typename Self>
|
||||
class SelfKeepAlive final {
|
||||
GIN_DISALLOW_NEW();
|
||||
|
||||
public:
|
||||
explicit SelfKeepAlive(Self* self) : keep_alive_(self) {}
|
||||
|
||||
SelfKeepAlive& operator=(Self* self) {
|
||||
DCHECK(!keep_alive_ || keep_alive_.Get() == self);
|
||||
keep_alive_ = self;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Clear() { keep_alive_.Clear(); }
|
||||
|
||||
explicit operator bool() const { return keep_alive_; }
|
||||
|
||||
private:
|
||||
cppgc::Persistent<Self> keep_alive_;
|
||||
};
|
||||
|
||||
} // namespace gin_helper
|
||||
|
||||
#endif // ELECTRON_SHELL_COMMON_GIN_HELPER_SELF_KEEP_ALIVE_H_
|
||||
@@ -37,19 +37,19 @@ class Wrappable : public WrappableBase {
|
||||
isolate, base::BindRepeating(&internal::InvokeNew<Sig>, constructor));
|
||||
templ->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
T::BuildPrototype(isolate, templ);
|
||||
gin::PerIsolateData::From(isolate)->SetFunctionTemplate(&kWrapperInfo,
|
||||
templ);
|
||||
gin::PerIsolateData::From(isolate)->DeprecatedSetFunctionTemplate(
|
||||
&kWrapperInfo, templ);
|
||||
}
|
||||
|
||||
static v8::Local<v8::FunctionTemplate> GetConstructor(v8::Isolate* isolate) {
|
||||
// Fill the object template.
|
||||
auto* data = gin::PerIsolateData::From(isolate);
|
||||
auto templ = data->GetFunctionTemplate(&kWrapperInfo);
|
||||
auto templ = data->DeprecatedGetFunctionTemplate(&kWrapperInfo);
|
||||
if (templ.IsEmpty()) {
|
||||
templ = v8::FunctionTemplate::New(isolate);
|
||||
templ->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
T::BuildPrototype(isolate, templ);
|
||||
data->SetFunctionTemplate(&kWrapperInfo, templ);
|
||||
data->DeprecatedSetFunctionTemplate(&kWrapperInfo, templ);
|
||||
}
|
||||
return templ;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user