Separate PngModule from ModuleMgr.

PngModule also does not need to implement ProgressiveDecoderIface. Make
its method static and disallow instantiation.

Change-Id: I2e0198a0302472fc943be7aee7ee22ec8fea3920
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/69917
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxcodec/fx_codec.cpp b/core/fxcodec/fx_codec.cpp
index 920cffe..9aba833 100644
--- a/core/fxcodec/fx_codec.cpp
+++ b/core/fxcodec/fx_codec.cpp
@@ -51,10 +51,6 @@
 #ifdef PDF_ENABLE_XFA_GIF
   SetGifModule(pdfium::MakeUnique<GifModule>());
 #endif
-
-#ifdef PDF_ENABLE_XFA_PNG
-  SetPngModule(pdfium::MakeUnique<PngModule>());
-#endif
 }
 
 ModuleMgr::~ModuleMgr() = default;
diff --git a/core/fxcodec/fx_codec.h b/core/fxcodec/fx_codec.h
index 3202031..21a79ed 100644
--- a/core/fxcodec/fx_codec.h
+++ b/core/fxcodec/fx_codec.h
@@ -23,11 +23,7 @@
 #ifdef PDF_ENABLE_XFA_GIF
 #include "core/fxcodec/gif/gifmodule.h"
 #endif  // PDF_ENABLE_XFA_GIF
-
-#ifdef PDF_ENABLE_XFA_PNG
-#include "core/fxcodec/png/pngmodule.h"
-#endif  // PDF_ENABLE_XFA_PNG
-#endif  // PDF_ENABLE_XFA
+#endif
 
 namespace fxcodec {
 
@@ -69,13 +65,6 @@
     m_pGifModule = std::move(module);
   }
 #endif  // PDF_ENABLE_XFA_GIF
-
-#ifdef PDF_ENABLE_XFA_PNG
-  PngModule* GetPngModule() const { return m_pPngModule.get(); }
-  void SetPngModule(std::unique_ptr<PngModule> module) {
-    m_pPngModule = std::move(module);
-  }
-#endif  // PDF_ENABLE_XFA_PNG
 #endif  // PDF_ENABLE_XFA
 
  private:
@@ -90,10 +79,6 @@
 #ifdef PDF_ENABLE_XFA_GIF
   std::unique_ptr<GifModule> m_pGifModule;
 #endif  // PDF_ENABLE_XFA_GIF
-
-#ifdef PDF_ENABLE_XFA_PNG
-  std::unique_ptr<PngModule> m_pPngModule;
-#endif  // PDF_ENABLE_XFA_PNG
 #endif  // PDF_ENABLE_XFA
 };
 
diff --git a/core/fxcodec/png/pngmodule.cpp b/core/fxcodec/png/pngmodule.cpp
index 84e33bf..b254d33 100644
--- a/core/fxcodec/png/pngmodule.cpp
+++ b/core/fxcodec/png/pngmodule.cpp
@@ -184,11 +184,8 @@
 
 namespace fxcodec {
 
-PngModule::PngModule() = default;
-
-PngModule::~PngModule() = default;
-
-std::unique_ptr<ProgressiveDecoderIface::Context> PngModule::Start(
+// static
+std::unique_ptr<ProgressiveDecoderIface::Context> PngModule::StartDecode(
     Delegate* pDelegate) {
   auto p = pdfium::MakeUnique<CPngContext>(pDelegate);
   p->m_pPng =
@@ -210,14 +207,10 @@
   return p;
 }
 
-FX_FILESIZE PngModule::GetAvailInput(Context* pContext) const {
-  NOTREACHED();
-  return 0;
-}
-
-bool PngModule::Input(Context* pContext,
-                      RetainPtr<CFX_CodecMemory> codec_memory,
-                      CFX_DIBAttribute* pAttribute) {
+// static
+bool PngModule::ContinueDecode(ProgressiveDecoderIface::Context* pContext,
+                               RetainPtr<CFX_CodecMemory> codec_memory,
+                               CFX_DIBAttribute* pAttribute) {
   auto* ctx = static_cast<CPngContext*>(pContext);
   if (setjmp(png_jmpbuf(ctx->m_pPng))) {
     if (pAttribute &&
diff --git a/core/fxcodec/png/pngmodule.h b/core/fxcodec/png/pngmodule.h
index 76e10ca..b2c0bfd 100644
--- a/core/fxcodec/png/pngmodule.h
+++ b/core/fxcodec/png/pngmodule.h
@@ -11,9 +11,13 @@
 
 #include "core/fxcodec/progressive_decoder_iface.h"
 
+#ifndef PDF_ENABLE_XFA_PNG
+#error "PNG must be enabled"
+#endif
+
 namespace fxcodec {
 
-class PngModule final : public ProgressiveDecoderIface {
+class PngModule {
  public:
   class Delegate {
    public:
@@ -31,16 +35,16 @@
     virtual void PngFillScanlineBufCompleted(int pass, int line) = 0;
   };
 
-  PngModule();
-  ~PngModule() override;
+  static std::unique_ptr<ProgressiveDecoderIface::Context> StartDecode(
+      Delegate* pDelegate);
 
-  // ProgressiveDecoderIface:
-  FX_FILESIZE GetAvailInput(Context* pContext) const override;
-  bool Input(Context* pContext,
-             RetainPtr<CFX_CodecMemory> codec_memory,
-             CFX_DIBAttribute* pAttribute) override;
+  static bool ContinueDecode(ProgressiveDecoderIface::Context* pContext,
+                             RetainPtr<CFX_CodecMemory> codec_memory,
+                             CFX_DIBAttribute* pAttribute);
 
-  std::unique_ptr<Context> Start(Delegate* pDelegate);
+  PngModule() = delete;
+  PngModule(const PngModule&) = delete;
+  PngModule& operator=(const PngModule&) = delete;
 };
 
 }  // namespace fxcodec
diff --git a/core/fxcodec/progressivedecoder.cpp b/core/fxcodec/progressivedecoder.cpp
index 09c3d39..0438797 100644
--- a/core/fxcodec/progressivedecoder.cpp
+++ b/core/fxcodec/progressivedecoder.cpp
@@ -1190,13 +1190,13 @@
 
 bool ProgressiveDecoder::PngDetectImageTypeInBuffer(
     CFX_DIBAttribute* pAttribute) {
-  PngModule* pPngModule = m_pCodecMgr->GetPngModule();
-  m_pPngContext = pPngModule->Start(this);
+  m_pPngContext = PngModule::StartDecode(this);
   if (!m_pPngContext) {
     m_status = FXCODEC_STATUS_ERR_MEMORY;
     return false;
   }
-  while (pPngModule->Input(m_pPngContext.get(), m_pCodecMemory, pAttribute)) {
+  while (PngModule::ContinueDecode(m_pPngContext.get(), m_pCodecMemory,
+                                   pAttribute)) {
     uint32_t remain_size = static_cast<uint32_t>(m_pFile->GetSize()) - m_offSet;
     uint32_t input_size = std::min<uint32_t>(remain_size, kBlockSize);
     if (input_size == 0) {
@@ -1224,8 +1224,7 @@
 
 FXCODEC_STATUS ProgressiveDecoder::PngStartDecode(
     const RetainPtr<CFX_DIBitmap>& pDIBitmap) {
-  PngModule* pPngModule = m_pCodecMgr->GetPngModule();
-  m_pPngContext = pPngModule->Start(this);
+  m_pPngContext = PngModule::StartDecode(this);
   if (!m_pPngContext) {
     m_pDeviceBitmap = nullptr;
     m_pFile = nullptr;
@@ -1265,7 +1264,6 @@
 }
 
 FXCODEC_STATUS ProgressiveDecoder::PngContinueDecode() {
-  PngModule* pPngModule = m_pCodecMgr->GetPngModule();
   while (true) {
     uint32_t remain_size = (uint32_t)m_pFile->GetSize() - m_offSet;
     uint32_t input_size = std::min<uint32_t>(remain_size, kBlockSize);
@@ -1288,7 +1286,8 @@
       return m_status;
     }
     m_offSet += input_size;
-    bResult = pPngModule->Input(m_pPngContext.get(), m_pCodecMemory, nullptr);
+    bResult =
+        PngModule::ContinueDecode(m_pPngContext.get(), m_pCodecMemory, nullptr);
     if (!bResult) {
       m_pDeviceBitmap = nullptr;
       m_pFile = nullptr;