From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: deepak1556 <hop2deep@gmail.com>
Date: Tue, 5 Aug 2025 02:26:29 +0900
Subject: chore: restore some deprecated wrapper utility in gin

Restores part of https://chromium-review.googlesource.com/c/chromium/src/+/6799157

Patch can be removed once cppgc migration is complete
https://github.com/electron/electron/issues/47922

diff --git a/gin/function_template.h b/gin/function_template.h
index c80020b2bda2af39b38295dad3c6208cb8294b88..873015289db9709c00c32080e5387d9cdfea1f69 100644
--- a/gin/function_template.h
+++ b/gin/function_template.h
@@ -79,6 +79,7 @@ class GIN_EXPORT CallbackHolderBase {
                     CallbackHolderBase* holder);
     ~DisposeObserver() override;
     void OnBeforeDispose(v8::Isolate* isolate) override;
+    void OnBeforeMicrotasksRunnerDispose(v8::Isolate* isolate) override {}
     void OnDisposed() override;
 
    private:
diff --git a/gin/isolate_holder.cc b/gin/isolate_holder.cc
index 7c640559dc7a09730f0053be8798d10bc8ac1a1c..24127c579e149345cc9b937b17dbfe630b0d3c35 100644
--- a/gin/isolate_holder.cc
+++ b/gin/isolate_holder.cc
@@ -225,6 +225,7 @@ void IsolateHolder::WillCreateMicrotasksRunner() {
 
 void IsolateHolder::WillDestroyMicrotasksRunner() {
   DCHECK(g_initialized_microtasks_runner);
+  isolate_data_->NotifyBeforeMicrotasksRunnerDispose();
   g_destroyed_microtasks_runner = true;
 }
 
diff --git a/gin/per_isolate_data.cc b/gin/per_isolate_data.cc
index 9670f9f904c6c864e82409617ac4c7698c6fc3ef..d1014af4b63da244820ff865a8e824ddf68433a9 100644
--- a/gin/per_isolate_data.cc
+++ b/gin/per_isolate_data.cc
@@ -79,8 +79,13 @@ void PerIsolateData::SetObjectTemplate(
   object_templates_[info] = Eternal<ObjectTemplate>(isolate_, templ);
 }
 
-void PerIsolateData::SetFunctionTemplate(DeprecatedWrapperInfo* info,
-                                         Local<FunctionTemplate> templ) {
+void PerIsolateData::DeprecatedSetFunctionTemplate(
+    DeprecatedWrapperInfo* info, Local<FunctionTemplate> templ) {
+  deprecated_function_templates_[info] = Eternal<FunctionTemplate>(isolate_, templ);
+}
+
+void PerIsolateData::SetFunctionTemplate(
+    const WrapperInfo* info, Local<FunctionTemplate> templ) {
   function_templates_[info] = Eternal<FunctionTemplate>(isolate_, templ);
 }
 
@@ -103,11 +108,22 @@ v8::Local<v8::ObjectTemplate> PerIsolateData::GetObjectTemplate(
   return it->second.Get(isolate_);
 }
 
-v8::Local<v8::FunctionTemplate> PerIsolateData::GetFunctionTemplate(
+v8::Local<v8::FunctionTemplate> PerIsolateData::DeprecatedGetFunctionTemplate(
     DeprecatedWrapperInfo* info) {
+  DeprecatedFunctionTemplateMap::iterator it =
+      deprecated_function_templates_.find(info);
+  if (it == deprecated_function_templates_.end()) {
+    return v8::Local<v8::FunctionTemplate>();
+  }
+  return it->second.Get(isolate_);
+}
+
+v8::Local<v8::FunctionTemplate> PerIsolateData::GetFunctionTemplate(
+    const WrapperInfo* info) {
   FunctionTemplateMap::iterator it = function_templates_.find(info);
-  if (it == function_templates_.end())
+  if (it == function_templates_.end()) {
     return v8::Local<v8::FunctionTemplate>();
+  }
   return it->second.Get(isolate_);
 }
 
@@ -125,6 +141,12 @@ void PerIsolateData::NotifyBeforeDispose() {
   }
 }
 
+void PerIsolateData::NotifyBeforeMicrotasksRunnerDispose() {
+  for (auto& observer : dispose_observers_) {
+    observer.OnBeforeMicrotasksRunnerDispose(isolate_.get());
+  }
+}
+
 void PerIsolateData::NotifyDisposed() {
   for (auto& observer : dispose_observers_) {
     observer.OnDisposed();
diff --git a/gin/per_isolate_data.h b/gin/per_isolate_data.h
index 9146df7ec5e65401771f452ed44c63369be1d31f..5ae71c795cc98a885090553177f57deccb8bc546 100644
--- a/gin/per_isolate_data.h
+++ b/gin/per_isolate_data.h
@@ -34,6 +34,10 @@ class GIN_EXPORT PerIsolateData {
     // be entered before the observer is notified, but there will not be a
     // handle scope by default.
     virtual void OnBeforeDispose(v8::Isolate* isolate) = 0;
+
+    // Called just before the microtasks runner is about to be disposed.
+    virtual void OnBeforeMicrotasksRunnerDispose(v8::Isolate* isolate) = 0;
+
     // Called just after the isolate has been disposed.
     virtual void OnDisposed() = 0;
   };
@@ -61,24 +65,29 @@ class GIN_EXPORT PerIsolateData {
   void SetObjectTemplate(const WrapperInfo* info,
                          v8::Local<v8::ObjectTemplate> object_template);
 
-  void SetFunctionTemplate(DeprecatedWrapperInfo* info,
-                           v8::Local<v8::FunctionTemplate> function_template);
+  void DeprecatedSetFunctionTemplate(
+      DeprecatedWrapperInfo* info,
+      v8::Local<v8::FunctionTemplate> function_template);
+
+  void SetFunctionTemplate(
+      const WrapperInfo* info,
+      v8::Local<v8::FunctionTemplate> function_template);
 
-  // These are low-level functions for retrieving object or function templates
-  // stored in this object. Because these templates are often created lazily,
-  // most clients should call higher-level functions that know how to populate
-  // these templates if they haven't already been created.
   v8::Local<v8::ObjectTemplate> DeprecatedGetObjectTemplate(
       DeprecatedWrapperInfo* info);
 
   v8::Local<v8::ObjectTemplate> GetObjectTemplate(const WrapperInfo* info);
 
-  v8::Local<v8::FunctionTemplate> GetFunctionTemplate(
+  v8::Local<v8::FunctionTemplate> DeprecatedGetFunctionTemplate(
       DeprecatedWrapperInfo* info);
 
+  v8::Local<v8::FunctionTemplate> GetFunctionTemplate(
+      const WrapperInfo* info);
+
   void AddDisposeObserver(DisposeObserver* observer);
   void RemoveDisposeObserver(DisposeObserver* observer);
   void NotifyBeforeDispose();
+  void NotifyBeforeMicrotasksRunnerDispose();
   void NotifyDisposed();
 
   void EnableIdleTasks(std::unique_ptr<V8IdleTaskRunner> idle_task_runner);
@@ -99,6 +108,8 @@ class GIN_EXPORT PerIsolateData {
   typedef std::map<const WrapperInfo*, v8::Eternal<v8::ObjectTemplate>>
       ObjectTemplateMap;
   typedef std::map<DeprecatedWrapperInfo*, v8::Eternal<v8::FunctionTemplate>>
+      DeprecatedFunctionTemplateMap;
+  typedef std::map<const WrapperInfo*, v8::Eternal<v8::FunctionTemplate>>
       FunctionTemplateMap;
 
   // PerIsolateData doesn't actually own |isolate_|. Instead, the isolate is
@@ -107,6 +118,7 @@ class GIN_EXPORT PerIsolateData {
   raw_ptr<v8::ArrayBuffer::Allocator, DanglingUntriaged> allocator_;
   DeprecatedObjectTemplateMap deprecated_object_templates_;
   ObjectTemplateMap object_templates_;
+  DeprecatedFunctionTemplateMap deprecated_function_templates_;
   FunctionTemplateMap function_templates_;
   base::ObserverList<DisposeObserver> dispose_observers_;
   std::shared_ptr<V8ForegroundTaskRunnerBase> task_runner_;
