RefCount CPDF_Type3Cache all the time.

Prefer internal refcounts to external scheme.

Change-Id: Ia99d5fc635a51a38f41b1d64ea44d8202685ddc7
Reviewed-on: https://pdfium-review.googlesource.com/3617
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/render/cpdf_docrenderdata.cpp b/core/fpdfapi/render/cpdf_docrenderdata.cpp
index 39d1fcd..2d0bca4 100644
--- a/core/fpdfapi/render/cpdf_docrenderdata.cpp
+++ b/core/fpdfapi/render/cpdf_docrenderdata.cpp
@@ -32,10 +32,7 @@
 void CPDF_DocRenderData::Clear(bool bRelease) {
   for (auto it = m_Type3FaceMap.begin(); it != m_Type3FaceMap.end();) {
     auto curr_it = it++;
-    CPDF_CountedObject<CPDF_Type3Cache>* cache = curr_it->second;
-    if (bRelease || cache->use_count() < 2) {
-      delete cache->get();
-      delete cache;
+    if (bRelease || curr_it->second->HasOneRef()) {
       m_Type3FaceMap.erase(curr_it);
     }
   }
@@ -47,29 +44,21 @@
   }
 }
 
-CPDF_Type3Cache* CPDF_DocRenderData::GetCachedType3(CPDF_Type3Font* pFont) {
-  CPDF_CountedObject<CPDF_Type3Cache>* pCache;
+CFX_RetainPtr<CPDF_Type3Cache> CPDF_DocRenderData::GetCachedType3(
+    CPDF_Type3Font* pFont) {
   auto it = m_Type3FaceMap.find(pFont);
-  if (it == m_Type3FaceMap.end()) {
-    pCache = new CPDF_CountedObject<CPDF_Type3Cache>(
-        pdfium::MakeUnique<CPDF_Type3Cache>(pFont));
-    m_Type3FaceMap[pFont] = pCache;
-  } else {
-    pCache = it->second;
-  }
-  return pCache->AddRef();
+  if (it != m_Type3FaceMap.end())
+    return it->second;
+
+  auto pCache = pdfium::MakeRetain<CPDF_Type3Cache>(pFont);
+  m_Type3FaceMap[pFont] = pCache;
+  return pCache;
 }
 
-void CPDF_DocRenderData::ReleaseCachedType3(CPDF_Type3Font* pFont) {
+void CPDF_DocRenderData::MaybePurgeCachedType3(CPDF_Type3Font* pFont) {
   auto it = m_Type3FaceMap.find(pFont);
-  if (it != m_Type3FaceMap.end()) {
-    it->second->RemoveRef();
-    if (it->second->use_count() < 2) {
-      delete it->second->get();
-      delete it->second;
-      m_Type3FaceMap.erase(it);
-    }
-  }
+  if (it != m_Type3FaceMap.end() && it->second->HasOneRef())
+    m_Type3FaceMap.erase(it);
 }
 
 CFX_RetainPtr<CPDF_TransferFunc> CPDF_DocRenderData::GetTransferFunc(
diff --git a/core/fpdfapi/render/cpdf_docrenderdata.h b/core/fpdfapi/render/cpdf_docrenderdata.h
index 5daee34..7952f95 100644
--- a/core/fpdfapi/render/cpdf_docrenderdata.h
+++ b/core/fpdfapi/render/cpdf_docrenderdata.h
@@ -23,8 +23,8 @@
   explicit CPDF_DocRenderData(CPDF_Document* pPDFDoc);
   ~CPDF_DocRenderData();
 
-  CPDF_Type3Cache* GetCachedType3(CPDF_Type3Font* pFont);
-  void ReleaseCachedType3(CPDF_Type3Font* pFont);
+  CFX_RetainPtr<CPDF_Type3Cache> GetCachedType3(CPDF_Type3Font* pFont);
+  void MaybePurgeCachedType3(CPDF_Type3Font* pFont);
 
   CFX_RetainPtr<CPDF_TransferFunc> GetTransferFunc(CPDF_Object* pObj);
   void MaybePurgeTransferFunc(CPDF_Object* pOb);
@@ -32,11 +32,8 @@
   void Clear(bool bRelease);
 
  private:
-  using CPDF_Type3CacheMap =
-      std::map<CPDF_Font*, CPDF_CountedObject<CPDF_Type3Cache>*>;
-
   CPDF_Document* m_pPDFDoc;  // Not Owned
-  CPDF_Type3CacheMap m_Type3FaceMap;
+  std::map<CPDF_Font*, CFX_RetainPtr<CPDF_Type3Cache>> m_Type3FaceMap;
   std::map<CPDF_Object*, CFX_RetainPtr<CPDF_TransferFunc>> m_TransferFuncMap;
 };
 
diff --git a/core/fpdfapi/render/cpdf_renderstatus.cpp b/core/fpdfapi/render/cpdf_renderstatus.cpp
index 69293e6..09e5bc1 100644
--- a/core/fpdfapi/render/cpdf_renderstatus.cpp
+++ b/core/fpdfapi/render/cpdf_renderstatus.cpp
@@ -66,11 +66,12 @@
 namespace {
 
 void ReleaseCachedType3(CPDF_Type3Font* pFont) {
-  if (!pFont->m_pDocument)
+  CPDF_Document* pDoc = pFont->m_pDocument;
+  if (!pDoc)
     return;
 
-  pFont->m_pDocument->GetRenderData()->ReleaseCachedType3(pFont);
-  pFont->m_pDocument->GetPageData()->ReleaseFont(pFont->GetFontDict());
+  pDoc->GetRenderData()->MaybePurgeCachedType3(pFont);
+  pDoc->GetPageData()->ReleaseFont(pFont->GetFontDict());
 }
 
 class CPDF_RefType3Cache {
@@ -1795,12 +1796,14 @@
                                            &text_matrix, fill_argb, &m_Options);
 }
 
-CPDF_Type3Cache* CPDF_RenderStatus::GetCachedType3(CPDF_Type3Font* pFont) {
-  if (!pFont->m_pDocument) {
+CFX_RetainPtr<CPDF_Type3Cache> CPDF_RenderStatus::GetCachedType3(
+    CPDF_Type3Font* pFont) {
+  CPDF_Document* pDoc = pFont->m_pDocument;
+  if (!pDoc)
     return nullptr;
-  }
-  pFont->m_pDocument->GetPageData()->GetFont(pFont->GetFontDict());
-  return pFont->m_pDocument->GetRenderData()->GetCachedType3(pFont);
+
+  pDoc->GetPageData()->GetFont(pFont->GetFontDict());
+  return pDoc->GetRenderData()->GetCachedType3(pFont);
 }
 
 // TODO(npm): Font fallback for type 3 fonts? (Completely separate code!!)
@@ -1902,7 +1905,7 @@
       delete pStates;
     } else if (pType3Char->m_pBitmap) {
       if (device_class == FXDC_DISPLAY) {
-        CPDF_Type3Cache* pCache = GetCachedType3(pType3Font);
+        CFX_RetainPtr<CPDF_Type3Cache> pCache = GetCachedType3(pType3Font);
         refTypeCache.m_dwCount++;
         CFX_GlyphBitmap* pBitmap = pCache->LoadGlyph(charcode, &matrix, sa, sd);
         if (!pBitmap)
diff --git a/core/fpdfapi/render/cpdf_renderstatus.h b/core/fpdfapi/render/cpdf_renderstatus.h
index 354c7b1..e9d1f87 100644
--- a/core/fpdfapi/render/cpdf_renderstatus.h
+++ b/core/fpdfapi/render/cpdf_renderstatus.h
@@ -141,7 +141,7 @@
   CFX_RetainPtr<CFX_DIBitmap> LoadSMask(CPDF_Dictionary* pSMaskDict,
                                         FX_RECT* pClipRect,
                                         const CFX_Matrix* pMatrix);
-  static CPDF_Type3Cache* GetCachedType3(CPDF_Type3Font* pFont);
+  static CFX_RetainPtr<CPDF_Type3Cache> GetCachedType3(CPDF_Type3Font* pFont);
   static CPDF_GraphicStates* CloneObjStates(const CPDF_GraphicStates* pPathObj,
                                             bool bStroke);
   CFX_RetainPtr<CPDF_TransferFunc> GetTransferFunc(CPDF_Object* pObject) const;
diff --git a/core/fpdfapi/render/cpdf_type3cache.h b/core/fpdfapi/render/cpdf_type3cache.h
index f035786..0b3cdcb 100644
--- a/core/fpdfapi/render/cpdf_type3cache.h
+++ b/core/fpdfapi/render/cpdf_type3cache.h
@@ -10,16 +10,17 @@
 #include <map>
 
 #include "core/fpdfapi/font/cpdf_type3font.h"
+#include "core/fxcrt/cfx_retain_ptr.h"
 #include "core/fxcrt/fx_coordinates.h"
 #include "core/fxcrt/fx_string.h"
 #include "core/fxcrt/fx_system.h"
 
 class CPDF_Type3Glyphs;
 
-class CPDF_Type3Cache {
+class CPDF_Type3Cache : public CFX_Retainable {
  public:
-  explicit CPDF_Type3Cache(CPDF_Type3Font* pFont);
-  ~CPDF_Type3Cache();
+  template <typename T, typename... Args>
+  friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args);
 
   CFX_GlyphBitmap* LoadGlyph(uint32_t charcode,
                              const CFX_Matrix* pMatrix,
@@ -27,6 +28,9 @@
                              float retinaScaleY);
 
  private:
+  explicit CPDF_Type3Cache(CPDF_Type3Font* pFont);
+  ~CPDF_Type3Cache() override;
+
   CFX_GlyphBitmap* RenderGlyph(CPDF_Type3Glyphs* pSize,
                                uint32_t charcode,
                                const CFX_Matrix* pMatrix,