Make cppgc initialization the responsibility of the embedder.

*** This is a breaking change for XFA builds. ***

This CL keeps PDFium up-to-date with the new method of initializing the
cppgc ("oilpan") garbage collector used by XFA following the change at
https://chromium.googlesource.com/v8/v8/+/8c99b253af975acf09d8239e1798484d5a8a82fe

It is now cumbersome for PDFium to do this as part of its library
initialization, so much like chromium, the embedder must set up the
cppgc environment itself.

The upshot is that embedders using the XFA feature must search for their
existing calls to v8::InitializePlatform() and follow them up by a call
to cppgc::InitializeProcess().

After landing, chrome requires the CL from
  https://chromium-review.googlesource.com/c/chromium/src/+/2693312

-- Introduce new function in v8_initializer.cpp to keep cleanup in
   the same file as initialization.

Bug: chromium:1176416
Change-Id: Ib3c39cd7933c8f9cf048840bd9206672434c7159
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/77950
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Daniel Hosseinian <dhoss@chromium.org>
diff --git a/fxjs/gc/heap.cpp b/fxjs/gc/heap.cpp
index d550253..a100569 100644
--- a/fxjs/gc/heap.cpp
+++ b/fxjs/gc/heap.cpp
@@ -51,13 +51,11 @@
     DCHECK(!g_platform);
     g_platform = platform;
     g_isolate = isolate;
-    cppgc::InitializeProcess(platform->GetPageAllocator());
   }
 }
 
 void FXGC_Release() {
   if (g_platform && g_platform_ref_count == 0) {
-    cppgc::ShutdownProcess();
     g_platform = nullptr;
     g_isolate = nullptr;
   }
diff --git a/samples/pdfium_test.cc b/samples/pdfium_test.cc
index 93af49f..7967758 100644
--- a/samples/pdfium_test.cc
+++ b/samples/pdfium_test.cc
@@ -1283,7 +1283,7 @@
 #ifdef PDF_ENABLE_V8
   if (!options.disable_javascript) {
     isolate.reset();
-    v8::V8::ShutdownPlatform();
+    ShutdownV8ForPDFium();
 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
     free(const_cast<char*>(snapshot.data));
 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
diff --git a/testing/v8_initializer.cpp b/testing/v8_initializer.cpp
index 29d6e55..e635cde 100644
--- a/testing/v8_initializer.cpp
+++ b/testing/v8_initializer.cpp
@@ -12,6 +12,10 @@
 #include "v8/include/libplatform/libplatform.h"
 #include "v8/include/v8.h"
 
+#ifdef PDF_ENABLE_XFA
+#include "v8/include/cppgc/platform.h"
+#endif
+
 namespace {
 
 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
@@ -60,6 +64,9 @@
 
   std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
   v8::V8::InitializePlatform(platform.get());
+#ifdef PDF_ENABLE_XFA
+  cppgc::InitializeProcess(platform->GetPageAllocator());
+#endif
 
   const char* recommended_v8_flags = FPDF_GetRecommendedV8Flags();
   v8::V8::SetFlagsFromString(recommended_v8_flags);
@@ -100,3 +107,10 @@
   return InitializeV8Common(exe_path, js_flags);
 }
 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
+
+void ShutdownV8ForPDFium() {
+#ifdef PDF_ENABLE_XFA
+  cppgc::ShutdownProcess();
+#endif
+  v8::V8::ShutdownPlatform();
+}
diff --git a/testing/v8_initializer.h b/testing/v8_initializer.h
index 2f4c3c5..977bfe8 100644
--- a/testing/v8_initializer.h
+++ b/testing/v8_initializer.h
@@ -31,5 +31,5 @@
     const std::string& js_flags,
     const std::string& exe_path);
 #endif
-
+void ShutdownV8ForPDFium();
 #endif  // TESTING_V8_INITIALIZER_H_
diff --git a/testing/v8_test_environment.cpp b/testing/v8_test_environment.cpp
index ce8fb2d..9213de0 100644
--- a/testing/v8_test_environment.cpp
+++ b/testing/v8_test_environment.cpp
@@ -71,5 +71,5 @@
 
 void V8TestEnvironment::TearDown() {
   isolate_.reset();
-  v8::V8::ShutdownPlatform();
+  ShutdownV8ForPDFium();
 }