mirror of
https://github.com/electron/electron.git
synced 2026-04-10 03:01:51 -04:00
refactor: use gin::Wrappable for electron::api::DataPipeHolder (#49495)
* refactor: make `DataPipeHolder` inherit from `gin::Wrappable` * test: add a test to ensure GC clears the data pipe holder * chore: e patches all * chore: e patches all (trivial only) * refactor: make AllDataPipeHolders a base::flat_map of WeakPersistent
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "base/containers/flat_map.h"
|
||||
#include "base/containers/map_util.h"
|
||||
#include "base/memory/weak_ptr.h"
|
||||
#include "base/no_destructor.h"
|
||||
#include "base/strings/string_number_conversions.h"
|
||||
@@ -14,10 +16,10 @@
|
||||
#include "mojo/public/cpp/system/data_pipe.h"
|
||||
#include "mojo/public/cpp/system/simple_watcher.h"
|
||||
#include "net/base/net_errors.h"
|
||||
#include "shell/common/gin_helper/handle.h"
|
||||
#include "shell/common/gin_helper/promise.h"
|
||||
#include "shell/common/key_weak_map.h"
|
||||
#include "shell/common/node_util.h"
|
||||
#include "v8/include/cppgc/allocation.h"
|
||||
#include "v8/include/v8-cppgc.h"
|
||||
|
||||
#include "shell/common/node_includes.h"
|
||||
|
||||
@@ -29,9 +31,11 @@ namespace {
|
||||
int g_next_id = 0;
|
||||
|
||||
// Map that manages all the DataPipeHolder objects.
|
||||
KeyWeakMap<std::string>& AllDataPipeHolders() {
|
||||
static base::NoDestructor<KeyWeakMap<std::string>> weak_map;
|
||||
return *weak_map.get();
|
||||
[[nodiscard]] auto& AllDataPipeHolders() {
|
||||
static base::NoDestructor<
|
||||
base::flat_map<std::string, cppgc::WeakPersistent<DataPipeHolder>>>
|
||||
weak_map;
|
||||
return *weak_map;
|
||||
}
|
||||
|
||||
// Utility class to read from data pipe.
|
||||
@@ -143,8 +147,9 @@ class DataPipeReader {
|
||||
|
||||
} // namespace
|
||||
|
||||
gin::DeprecatedWrapperInfo DataPipeHolder::kWrapperInfo = {
|
||||
gin::kEmbedderNativeGin};
|
||||
const gin::WrapperInfo DataPipeHolder::kWrapperInfo = {
|
||||
{gin::kEmbedderNativeGin},
|
||||
gin::kElectronDataPipeHolder};
|
||||
|
||||
DataPipeHolder::DataPipeHolder(const network::DataElement& element)
|
||||
: id_(base::NumberToString(++g_next_id)) {
|
||||
@@ -166,30 +171,28 @@ v8::Local<v8::Promise> DataPipeHolder::ReadAll(v8::Isolate* isolate) {
|
||||
return handle;
|
||||
}
|
||||
|
||||
const char* DataPipeHolder::GetTypeName() {
|
||||
return "DataPipeHolder";
|
||||
const gin::WrapperInfo* DataPipeHolder::wrapper_info() const {
|
||||
return &kWrapperInfo;
|
||||
}
|
||||
|
||||
const char* DataPipeHolder::GetHumanReadableName() const {
|
||||
return "Electron / DataPipeHolder";
|
||||
}
|
||||
|
||||
// static
|
||||
gin_helper::Handle<DataPipeHolder> DataPipeHolder::Create(
|
||||
v8::Isolate* isolate,
|
||||
const network::DataElement& element) {
|
||||
auto handle = gin_helper::CreateHandle(isolate, new DataPipeHolder(element));
|
||||
AllDataPipeHolders().Set(isolate, handle->id(),
|
||||
handle->GetWrapper(isolate).ToLocalChecked());
|
||||
return handle;
|
||||
DataPipeHolder* DataPipeHolder::Create(v8::Isolate* isolate,
|
||||
const network::DataElement& element) {
|
||||
auto* holder = cppgc::MakeGarbageCollected<DataPipeHolder>(
|
||||
isolate->GetCppHeap()->GetAllocationHandle(), element);
|
||||
AllDataPipeHolders().insert_or_assign(holder->id(), holder);
|
||||
return holder;
|
||||
}
|
||||
|
||||
// static
|
||||
gin_helper::Handle<DataPipeHolder> DataPipeHolder::From(v8::Isolate* isolate,
|
||||
const std::string& id) {
|
||||
v8::MaybeLocal<v8::Object> object = AllDataPipeHolders().Get(isolate, id);
|
||||
if (!object.IsEmpty()) {
|
||||
gin_helper::Handle<DataPipeHolder> handle;
|
||||
if (gin::ConvertFromV8(isolate, object.ToLocalChecked(), &handle))
|
||||
return handle;
|
||||
}
|
||||
return {};
|
||||
DataPipeHolder* DataPipeHolder::From(v8::Isolate* isolate,
|
||||
const std::string_view id) {
|
||||
auto* found = base::FindOrNull(AllDataPipeHolders(), id);
|
||||
return found ? found->Get() : nullptr;
|
||||
}
|
||||
|
||||
} // namespace electron::api
|
||||
|
||||
Reference in New Issue
Block a user