Replace CountedFaceCache with RetainPtr<CFX_FaceCache>

Removes some add-hoc refcounting in favor of smart pointers. The
combination of Retainable and Observable creates a cache entry that
remains valid so long as there is at least one retain ptr outstanding.

Change-Id: I14f3c0b3ec42d9b845b6fb3965b0d7e6f9fc1a28
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/54272
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fxge/cfx_fontcache.cpp b/core/fxge/cfx_fontcache.cpp
index 5ccac8c..b9300bc 100644
--- a/core/fxge/cfx_fontcache.cpp
+++ b/core/fxge/cfx_fontcache.cpp
@@ -10,40 +10,26 @@
 #include <utility>
 
 #include "core/fxge/cfx_facecache.h"
+#include "core/fxge/cfx_font.h"
 #include "core/fxge/fx_font.h"
 #include "core/fxge/fx_freetype.h"
 #include "third_party/base/ptr_util.h"
 
-CFX_FontCache::CountedFaceCache::CountedFaceCache() {}
+CFX_FontCache::CFX_FontCache() = default;
 
-CFX_FontCache::CountedFaceCache::~CountedFaceCache() {}
+CFX_FontCache::~CFX_FontCache() = default;
 
-CFX_FontCache::CFX_FontCache() {}
-
-CFX_FontCache::~CFX_FontCache() {
-  ASSERT(m_ExtFaceMap.empty());
-  ASSERT(m_FTFaceMap.empty());
-}
-
-CFX_FaceCache* CFX_FontCache::GetCachedFace(const CFX_Font* pFont) {
+RetainPtr<CFX_FaceCache> CFX_FontCache::GetCachedFace(const CFX_Font* pFont) {
   FXFT_Face face = pFont->GetFace();
   const bool bExternal = !face;
-  CFX_FTCacheMap& map = bExternal ? m_ExtFaceMap : m_FTFaceMap;
+  auto& map = bExternal ? m_ExtFaceMap : m_FTFaceMap;
   auto it = map.find(face);
-  if (it != map.end()) {
-    CountedFaceCache* counted_face_cache = it->second.get();
-    counted_face_cache->m_nCount++;
-    return counted_face_cache->m_Obj.get();
-  }
+  if (it != map.end() && it->second)
+    return pdfium::WrapRetain(it->second.Get());
 
-  auto counted_face_cache = pdfium::MakeUnique<CountedFaceCache>();
-  counted_face_cache->m_nCount = 2;
-  auto new_cache =
-      pdfium::MakeUnique<CFX_FaceCache>(bExternal ? nullptr : face);
-  CFX_FaceCache* face_cache = new_cache.get();
-  counted_face_cache->m_Obj = std::move(new_cache);
-  map[face] = std::move(counted_face_cache);
-  return face_cache;
+  auto new_cache = pdfium::MakeRetain<CFX_FaceCache>(face);
+  map[face].Reset(new_cache.Get());
+  return new_cache;
 }
 
 #ifdef _SKIA_SUPPORT_
@@ -51,20 +37,3 @@
   return GetCachedFace(pFont)->GetDeviceCache(pFont);
 }
 #endif
-
-void CFX_FontCache::ReleaseCachedFace(const CFX_Font* pFont) {
-  FXFT_Face face = pFont->GetFace();
-  const bool bExternal = !face;
-  CFX_FTCacheMap& map = bExternal ? m_ExtFaceMap : m_FTFaceMap;
-
-  auto it = map.find(face);
-  if (it == map.end())
-    return;
-
-  CountedFaceCache* counted_face_cache = it->second.get();
-  if (counted_face_cache->m_nCount > 2) {
-    counted_face_cache->m_nCount--;
-  } else {
-    map.erase(it);
-  }
-}