Split off CPDF_ModuleMgr:Create() from CPDF_ModuleMgr::Get()

Similarly, split off CFX_GEModule::Create() from CFX_GEModule::Get().
We know the modules live for the lifetime of the pdfium library.

- Make some more members const.
- Make CPDF_ModuleMgr create the GE module itself.
- Need to initialize in tests.

Change-Id: I32395e16fccf5eac80cbd35edc3c15d7cb2ca0aa
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/54832
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/cpdf_modulemgr.cpp b/core/fpdfapi/cpdf_modulemgr.cpp
index 34fe049..889e557 100644
--- a/core/fpdfapi/cpdf_modulemgr.cpp
+++ b/core/fpdfapi/cpdf_modulemgr.cpp
@@ -37,29 +37,32 @@
 }  // namespace
 
 // static
-CPDF_ModuleMgr* CPDF_ModuleMgr::Get() {
-  if (!g_pDefaultMgr)
-    g_pDefaultMgr = new CPDF_ModuleMgr;
-  return g_pDefaultMgr;
+void CPDF_ModuleMgr::Create() {
+  ASSERT(!g_pDefaultMgr);
+  g_pDefaultMgr = new CPDF_ModuleMgr;
+  g_pDefaultMgr->InitCodecModule();
+  g_pDefaultMgr->InitPageModule();
+  g_pDefaultMgr->LoadEmbeddedMaps();
+  g_pDefaultMgr->LoadCodecModules();
 }
 
 // static
 void CPDF_ModuleMgr::Destroy() {
+  ASSERT(g_pDefaultMgr);
   delete g_pDefaultMgr;
   g_pDefaultMgr = nullptr;
 }
 
-CPDF_ModuleMgr::CPDF_ModuleMgr() {}
-
-CPDF_ModuleMgr::~CPDF_ModuleMgr() {}
-
-void CPDF_ModuleMgr::Init() {
-  InitCodecModule();
-  InitPageModule();
-  LoadEmbeddedMaps();
-  LoadCodecModules();
+// static
+CPDF_ModuleMgr* CPDF_ModuleMgr::Get() {
+  ASSERT(g_pDefaultMgr);
+  return g_pDefaultMgr;
 }
 
+CPDF_ModuleMgr::CPDF_ModuleMgr() = default;
+
+CPDF_ModuleMgr::~CPDF_ModuleMgr() = default;
+
 CCodec_FaxModule* CPDF_ModuleMgr::GetFaxModule() {
   return m_pCodecModule->GetFaxModule();
 }
diff --git a/core/fpdfapi/cpdf_modulemgr.h b/core/fpdfapi/cpdf_modulemgr.h
index 69b4833..e916947 100644
--- a/core/fpdfapi/cpdf_modulemgr.h
+++ b/core/fpdfapi/cpdf_modulemgr.h
@@ -35,12 +35,11 @@
 
 class CPDF_ModuleMgr {
  public:
-  static CPDF_ModuleMgr* Get();
+  static void Create();
   static void Destroy();
+  static CPDF_ModuleMgr* Get();
   static const int kFileBufSize = 512;
 
-  void Init();
-
   void SetUnsupportInfoAdapter(
       std::unique_ptr<fpdfapi::UnsupportedInfoAdapter> pAdapter) {
     m_pUnsupportInfoAdapter = std::move(pAdapter);
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
index 975b3c9..1ce21f4 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator_unittest.cpp
@@ -25,11 +25,8 @@
 
 class CPDF_PageContentGeneratorTest : public testing::Test {
  protected:
-  void SetUp() override { CPDF_ModuleMgr::Get()->Init(); }
-
-  void TearDown() override {
-    CPDF_ModuleMgr::Destroy();
-  }
+  void SetUp() override { CPDF_ModuleMgr::Create(); }
+  void TearDown() override { CPDF_ModuleMgr::Destroy(); }
 
   void TestProcessPath(CPDF_PageContentGenerator* pGen,
                        std::ostringstream* buf,
diff --git a/core/fpdfapi/font/cpdf_cidfont_unittest.cpp b/core/fpdfapi/font/cpdf_cidfont_unittest.cpp
index 4166a93..fe10bc9 100644
--- a/core/fpdfapi/font/cpdf_cidfont_unittest.cpp
+++ b/core/fpdfapi/font/cpdf_cidfont_unittest.cpp
@@ -15,8 +15,7 @@
 
 class CPDF_CIDFontTest : public testing::Test {
  protected:
-  void SetUp() override { CPDF_ModuleMgr::Get()->Init(); }
-
+  void SetUp() override { CPDF_ModuleMgr::Create(); }
   void TearDown() override { CPDF_ModuleMgr::Destroy(); }
 };
 
diff --git a/core/fpdfapi/parser/cpdf_document_unittest.cpp b/core/fpdfapi/parser/cpdf_document_unittest.cpp
index 5078bf4..682ac95 100644
--- a/core/fpdfapi/parser/cpdf_document_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_document_unittest.cpp
@@ -151,7 +151,7 @@
 
 class cpdf_document_test : public testing::Test {
  public:
-  void SetUp() override { CPDF_ModuleMgr::Get()->Init(); }
+  void SetUp() override { CPDF_ModuleMgr::Create(); }
   void TearDown() override { CPDF_ModuleMgr::Destroy(); }
 };
 
diff --git a/core/fpdfapi/parser/cpdf_hint_tables_unittest.cpp b/core/fpdfapi/parser/cpdf_hint_tables_unittest.cpp
index 85db7d3..fda209f 100644
--- a/core/fpdfapi/parser/cpdf_hint_tables_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_hint_tables_unittest.cpp
@@ -63,7 +63,7 @@
  public:
   CPDF_HintTablesTest() {
     // Needs for encoding Hint table stream.
-    CPDF_ModuleMgr::Get()->Init();
+    CPDF_ModuleMgr::Create();
   }
 
   ~CPDF_HintTablesTest() override { CPDF_ModuleMgr::Destroy(); }
diff --git a/core/fxge/cfx_gemodule.cpp b/core/fxge/cfx_gemodule.cpp
index b9b232d..ea6b4ac 100644
--- a/core/fxge/cfx_gemodule.cpp
+++ b/core/fxge/cfx_gemodule.cpp
@@ -17,17 +17,19 @@
 
 }  // namespace
 
-CFX_GEModule::CFX_GEModule()
-    : m_pFontMgr(pdfium::MakeUnique<CFX_FontMgr>()),
-      m_pPlatform(PlatformIface::Create()) {}
+CFX_GEModule::CFX_GEModule(const char** pUserFontPaths)
+    : m_pFontCache(pdfium::MakeUnique<CFX_FontCache>()),
+      m_pFontMgr(pdfium::MakeUnique<CFX_FontMgr>()),
+      m_pPlatform(PlatformIface::Create()),
+      m_pUserFontPaths(pUserFontPaths) {}
 
 CFX_GEModule::~CFX_GEModule() = default;
 
 // static
-CFX_GEModule* CFX_GEModule::Get() {
-  if (!g_pGEModule)
-    g_pGEModule = new CFX_GEModule();
-  return g_pGEModule;
+void CFX_GEModule::Create(const char** pUserFontPaths) {
+  ASSERT(!g_pGEModule);
+  g_pGEModule = new CFX_GEModule(pUserFontPaths);
+  g_pGEModule->m_pPlatform->Init();
 }
 
 // static
@@ -37,14 +39,8 @@
   g_pGEModule = nullptr;
 }
 
-void CFX_GEModule::Init(const char** userFontPaths) {
+// static
+CFX_GEModule* CFX_GEModule::Get() {
   ASSERT(g_pGEModule);
-  m_pUserFontPaths = userFontPaths;
-  m_pPlatform->Init();
-}
-
-CFX_FontCache* CFX_GEModule::GetFontCache() {
-  if (!m_pFontCache)
-    m_pFontCache = pdfium::MakeUnique<CFX_FontCache>();
-  return m_pFontCache.get();
+  return g_pGEModule;
 }
diff --git a/core/fxge/cfx_gemodule.h b/core/fxge/cfx_gemodule.h
index afcbff4..59eddb7 100644
--- a/core/fxge/cfx_gemodule.h
+++ b/core/fxge/cfx_gemodule.h
@@ -22,23 +22,23 @@
     virtual void Init() = 0;
   };
 
-  static CFX_GEModule* Get();
+  static void Create(const char** pUserFontPaths);
   static void Destroy();
+  static CFX_GEModule* Get();
 
-  void Init(const char** pUserFontPaths);
-  CFX_FontCache* GetFontCache();
+  CFX_FontCache* GetFontCache() const { return m_pFontCache.get(); }
   CFX_FontMgr* GetFontMgr() const { return m_pFontMgr.get(); }
   PlatformIface* GetPlatform() const { return m_pPlatform.get(); }
   const char** GetUserFontPaths() const { return m_pUserFontPaths; }
 
  private:
-  CFX_GEModule();
+  explicit CFX_GEModule(const char** pUserFontPaths);
   ~CFX_GEModule();
 
-  std::unique_ptr<CFX_FontCache> m_pFontCache;
-  std::unique_ptr<CFX_FontMgr> m_pFontMgr;
-  std::unique_ptr<PlatformIface> m_pPlatform;
-  const char** m_pUserFontPaths = nullptr;
+  std::unique_ptr<CFX_FontCache> const m_pFontCache;
+  std::unique_ptr<CFX_FontMgr> const m_pFontMgr;
+  std::unique_ptr<PlatformIface> const m_pPlatform;
+  const char** const m_pUserFontPaths;
 };
 
 #endif  // CORE_FXGE_CFX_GEMODULE_H_
diff --git a/fpdfsdk/fpdf_catalog_unittest.cpp b/fpdfsdk/fpdf_catalog_unittest.cpp
index 180fb79..ff3adcf 100644
--- a/fpdfsdk/fpdf_catalog_unittest.cpp
+++ b/fpdfsdk/fpdf_catalog_unittest.cpp
@@ -27,7 +27,7 @@
 class PDFCatalogTest : public testing::Test {
  public:
   void SetUp() override {
-    CPDF_ModuleMgr::Get()->Init();
+    CPDF_ModuleMgr::Create();
     auto pTestDoc = pdfium::MakeUnique<CPDF_TestDocument>();
     m_pDoc.reset(FPDFDocumentFromCPDFDocument(pTestDoc.release()));
     m_pRootObj = pdfium::MakeRetain<CPDF_Dictionary>();
diff --git a/fpdfsdk/fpdf_doc_unittest.cpp b/fpdfsdk/fpdf_doc_unittest.cpp
index b0337e8..08517d7 100644
--- a/fpdfsdk/fpdf_doc_unittest.cpp
+++ b/fpdfsdk/fpdf_doc_unittest.cpp
@@ -40,7 +40,7 @@
   };
 
   void SetUp() override {
-    CPDF_ModuleMgr::Get()->Init();
+    CPDF_ModuleMgr::Create();
     auto pTestDoc = pdfium::MakeUnique<CPDF_TestDocument>();
     m_pIndirectObjs = pTestDoc->GetHolder();
     m_pRootObj.Reset(m_pIndirectObjs->NewIndirect<CPDF_Dictionary>());
diff --git a/fpdfsdk/fpdf_edit_unittest.cpp b/fpdfsdk/fpdf_edit_unittest.cpp
index 499a655..1fcd93d 100644
--- a/fpdfsdk/fpdf_edit_unittest.cpp
+++ b/fpdfsdk/fpdf_edit_unittest.cpp
@@ -8,8 +8,7 @@
 #include "testing/gtest/include/gtest/gtest.h"
 
 class PDFEditTest : public testing::Test {
-  void SetUp() override { CPDF_ModuleMgr::Get()->Init(); }
-
+  void SetUp() override { CPDF_ModuleMgr::Create(); }
   void TearDown() override { CPDF_ModuleMgr::Destroy(); }
 };
 
diff --git a/fpdfsdk/fpdf_editimg_unittest.cpp b/fpdfsdk/fpdf_editimg_unittest.cpp
index fa04758..81a4877 100644
--- a/fpdfsdk/fpdf_editimg_unittest.cpp
+++ b/fpdfsdk/fpdf_editimg_unittest.cpp
@@ -9,8 +9,7 @@
 #include "testing/gtest/include/gtest/gtest.h"
 
 class PDFEditImgTest : public testing::Test {
-  void SetUp() override { CPDF_ModuleMgr::Get()->Init(); }
-
+  void SetUp() override { CPDF_ModuleMgr::Create(); }
   void TearDown() override { CPDF_ModuleMgr::Destroy(); }
 };
 
diff --git a/fpdfsdk/fpdf_view.cpp b/fpdfsdk/fpdf_view.cpp
index f9bc6c0..3712697 100644
--- a/fpdfsdk/fpdf_view.cpp
+++ b/fpdfsdk/fpdf_view.cpp
@@ -158,12 +158,8 @@
     return;
 
   FXMEM_InitializePartitionAlloc();
-
-  CFX_GEModule* pModule = CFX_GEModule::Get();
-  pModule->Init(cfg ? cfg->m_pUserFontPaths : nullptr);
-
-  CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get();
-  pModuleMgr->Init();
+  CFX_GEModule::Create(cfg ? cfg->m_pUserFontPaths : nullptr);
+  CPDF_ModuleMgr::Create();
 
 #ifdef PDF_ENABLE_XFA
   BC_Library_Init();
@@ -184,7 +180,6 @@
 
   CPDF_ModuleMgr::Destroy();
   CFX_GEModule::Destroy();
-
   IJS_Runtime::Destroy();
 
   g_bLibraryInitialized = false;
diff --git a/testing/BUILD.gn b/testing/BUILD.gn
index d2a0411..9eee51c 100644
--- a/testing/BUILD.gn
+++ b/testing/BUILD.gn
@@ -17,6 +17,7 @@
     "string_write_stream.h",
     "test_loader.cpp",
     "test_loader.h",
+    "test_support.cpp",
     "test_support.h",
     "utils/bitmap_saver.cpp",
     "utils/bitmap_saver.h",
@@ -34,6 +35,7 @@
     "../:pdfium_public_headers",
     "../core/fdrm",
     "../core/fxcrt",
+    "../core/fxge",
     "image_diff",
   ]
   configs += [ "../:pdfium_core_config" ]
diff --git a/testing/test_support.cpp b/testing/test_support.cpp
new file mode 100644
index 0000000..94ce528
--- /dev/null
+++ b/testing/test_support.cpp
@@ -0,0 +1,15 @@
+// Copyright 2019 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "testing/test_support.h"
+
+#include "core/fxge/cfx_gemodule.h"
+
+void InitializePDFTestEnvironment() {
+  CFX_GEModule::Create(nullptr);
+}
+
+void DestroyPDFTestEnvironment() {
+  CFX_GEModule::Destroy();
+}
diff --git a/testing/test_support.h b/testing/test_support.h
index f257787..f237ea5 100644
--- a/testing/test_support.h
+++ b/testing/test_support.h
@@ -5,6 +5,8 @@
 #ifndef TESTING_TEST_SUPPORT_H_
 #define TESTING_TEST_SUPPORT_H_
 
+#include <stdint.h>
+
 namespace pdfium {
 
 #define STR_IN_TEST_CASE(input_literal, ...)               \
@@ -44,4 +46,7 @@
 
 }  // namespace pdfium
 
+void InitializePDFTestEnvironment();
+void DestroyPDFTestEnvironment();
+
 #endif  // TESTING_TEST_SUPPORT_H_
diff --git a/testing/unit_test_main.cpp b/testing/unit_test_main.cpp
index 44918fc..35c4939 100644
--- a/testing/unit_test_main.cpp
+++ b/testing/unit_test_main.cpp
@@ -8,6 +8,7 @@
 #include "core/fxcrt/fx_memory.h"
 #include "testing/gmock/include/gmock/gmock.h"
 #include "testing/gtest/include/gtest/gtest.h"
+#include "testing/test_support.h"
 
 #ifdef PDF_ENABLE_V8
 #include "testing/v8_initializer.h"
@@ -36,6 +37,7 @@
 #endif  // V8_USE_EXTERNAL_STARTUP_DATA
 #endif  // PDF_ENABLE_V8
 
+  InitializePDFTestEnvironment();
 #ifdef PDF_ENABLE_XFA
   InitializeXFATestEnvironment();
 #endif  // PDF_ENABLE_XFA
@@ -45,6 +47,9 @@
 
   int ret_val = RUN_ALL_TESTS();
 
+  DestroyPDFTestEnvironment();
+  // NOTE: XFA test environment, if present, destroyed by gtest.
+
 #ifdef PDF_ENABLE_V8
   v8::V8::ShutdownPlatform();
 #endif  // PDF_ENABLE_V8
diff --git a/xfa/fgas/crt/cfgas_stringformatter_unittest.cpp b/xfa/fgas/crt/cfgas_stringformatter_unittest.cpp
index e06e04c..f9bd450 100644
--- a/xfa/fgas/crt/cfgas_stringformatter_unittest.cpp
+++ b/xfa/fgas/crt/cfgas_stringformatter_unittest.cpp
@@ -21,10 +21,10 @@
  public:
   CFGAS_StringFormatterTest() {
     SetTZ("UTC");
-    CPDF_ModuleMgr::Get()->Init();
+    CPDF_ModuleMgr::Create();
   }
 
-  ~CFGAS_StringFormatterTest() override { CPDF_ModuleMgr::Get()->Destroy(); }
+  ~CFGAS_StringFormatterTest() override { CPDF_ModuleMgr::Destroy(); }
 
   void TearDown() override {
     fmt_.reset();