Use unique_ptr in CFX_FontMgr::m_FaceMap.

Change-Id: Ie34942e6e577dfa270417b17c59a51813f310d27
Reviewed-on: https://pdfium-review.googlesource.com/4436
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxge/cfx_fontmgr.h b/core/fxge/cfx_fontmgr.h
index b7f2e1a..7ccad80 100644
--- a/core/fxge/cfx_fontmgr.h
+++ b/core/fxge/cfx_fontmgr.h
@@ -61,7 +61,7 @@
 
  private:
   std::unique_ptr<CFX_FontMapper> m_pBuiltinMapper;
-  std::map<CFX_ByteString, CTTFontDesc*> m_FaceMap;
+  std::map<CFX_ByteString, std::unique_ptr<CTTFontDesc>> m_FaceMap;
   FXFT_Library m_FTLibrary;
   bool m_FTLibrarySupportsHinting;
 };
diff --git a/core/fxge/ge/cfx_fontmgr.cpp b/core/fxge/ge/cfx_fontmgr.cpp
index 97d1438..ec184ae 100644
--- a/core/fxge/ge/cfx_fontmgr.cpp
+++ b/core/fxge/ge/cfx_fontmgr.cpp
@@ -89,11 +89,9 @@
 }
 
 CFX_FontMgr::~CFX_FontMgr() {
-  for (const auto& pair : m_FaceMap)
-    delete pair.second;
-
-  // |m_pBuiltinMapper| references |m_FTLibrary|, so it has to be destroyed
-  // first.
+  // |m_FaceMap| and |m_pBuiltinMapper| reference |m_FTLibrary|, so they must
+  // be destroyed first.
+  m_FaceMap.clear();
   m_pBuiltinMapper.reset();
   FXFT_Done_FreeType(m_FTLibrary);
 }
@@ -132,7 +130,7 @@
   if (it == m_FaceMap.end())
     return nullptr;
 
-  CTTFontDesc* pFontDesc = it->second;
+  CTTFontDesc* pFontDesc = it->second.get();
   pFontData = pFontDesc->m_pFontData;
   pFontDesc->m_RefCount++;
   return pFontDesc->m_SingleFace;
@@ -144,7 +142,7 @@
                                      uint8_t* pData,
                                      uint32_t size,
                                      int face_index) {
-  CTTFontDesc* pFontDesc = new CTTFontDesc;
+  auto pFontDesc = pdfium::MakeUnique<CTTFontDesc>();
   pFontDesc->m_Type = 1;
   pFontDesc->m_SingleFace = nullptr;
   pFontDesc->m_pFontData = pData;
@@ -154,17 +152,16 @@
   FXFT_Library library = m_FTLibrary;
   int ret = FXFT_New_Memory_Face(library, pData, size, face_index,
                                  &pFontDesc->m_SingleFace);
-  if (ret) {
-    delete pFontDesc;
+  if (ret)
     return nullptr;
-  }
+
   ret = FXFT_Set_Pixel_Sizes(pFontDesc->m_SingleFace, 64, 64);
-  if (ret) {
-    delete pFontDesc;
+  if (ret)
     return nullptr;
-  }
-  m_FaceMap[KeyNameFromFace(face_name, weight, bItalic)] = pFontDesc;
-  return pFontDesc->m_SingleFace;
+
+  CTTFontDesc* pResult = pFontDesc.get();
+  m_FaceMap[KeyNameFromFace(face_name, weight, bItalic)] = std::move(pFontDesc);
+  return pResult->m_SingleFace;
 }
 
 FXFT_Face CFX_FontMgr::GetCachedTTCFace(int ttc_size,
@@ -175,7 +172,7 @@
   if (it == m_FaceMap.end())
     return nullptr;
 
-  CTTFontDesc* pFontDesc = it->second;
+  CTTFontDesc* pFontDesc = it->second.get();
   pFontData = pFontDesc->m_pFontData;
   pFontDesc->m_RefCount++;
   int face_index = GetTTCIndex(pFontDesc->m_pFontData, ttc_size, font_offset);
@@ -191,17 +188,18 @@
                                         uint8_t* pData,
                                         uint32_t size,
                                         int font_offset) {
-  CTTFontDesc* pFontDesc = new CTTFontDesc;
+  auto pFontDesc = pdfium::MakeUnique<CTTFontDesc>();
   pFontDesc->m_Type = 2;
   pFontDesc->m_pFontData = pData;
   for (int i = 0; i < 16; i++)
     pFontDesc->m_TTCFaces[i] = nullptr;
   pFontDesc->m_RefCount++;
-  m_FaceMap[KeyNameFromSize(ttc_size, checksum)] = pFontDesc;
-  int face_index = GetTTCIndex(pFontDesc->m_pFontData, ttc_size, font_offset);
-  pFontDesc->m_TTCFaces[face_index] =
-      GetFixedFace(pFontDesc->m_pFontData, ttc_size, face_index);
-  return pFontDesc->m_TTCFaces[face_index];
+  CTTFontDesc* pResult = pFontDesc.get();
+  m_FaceMap[KeyNameFromSize(ttc_size, checksum)] = std::move(pFontDesc);
+  int face_index = GetTTCIndex(pResult->m_pFontData, ttc_size, font_offset);
+  pResult->m_TTCFaces[face_index] =
+      GetFixedFace(pResult->m_pFontData, ttc_size, face_index);
+  return pResult->m_TTCFaces[face_index];
 }
 
 FXFT_Face CFX_FontMgr::GetFixedFace(const uint8_t* pData,
diff --git a/core/fxge/ge/cttfontdesc.cpp b/core/fxge/ge/cttfontdesc.cpp
index 269abfe..f750395 100644
--- a/core/fxge/ge/cttfontdesc.cpp
+++ b/core/fxge/ge/cttfontdesc.cpp
@@ -34,9 +34,5 @@
     if (i == 16)
       return -1;
   }
-  m_RefCount--;
-  if (m_RefCount)
-    return m_RefCount;
-  delete this;
-  return 0;
+  return --m_RefCount;
 }