Mass conversion of all const-lifetime class members

Sed + minimal conversions to compile, including moving some
constructors into the .cpp file. Any that caused ASAN issues
during the tests were omitted rather than trying to resolve
the underlying issue.

Change-Id: I00a421f33b253eb4071ffd9af3f2922c7443b335
Reviewed-on: https://pdfium-review.googlesource.com/5891
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fpdfapi/font/font_int.h b/core/fpdfapi/font/font_int.h
index f434f2b..ebd08e2 100644
--- a/core/fpdfapi/font/font_int.h
+++ b/core/fpdfapi/font/font_int.h
@@ -150,7 +150,7 @@
                                 const CFX_ByteStringC& first,
                                 const CFX_ByteStringC& second);
 
-  CPDF_CMap* const m_pCMap;
+  CFX_UnownedPtr<CPDF_CMap> const m_pCMap;
   int m_Status;
   int m_CodeSeq;
   uint32_t m_CodePoints[4];
diff --git a/core/fpdfapi/page/cpdf_iccprofile.h b/core/fpdfapi/page/cpdf_iccprofile.h
index 05ac9bf..a0cf744 100644
--- a/core/fpdfapi/page/cpdf_iccprofile.h
+++ b/core/fpdfapi/page/cpdf_iccprofile.h
@@ -8,6 +8,7 @@
 #define CORE_FPDFAPI_PAGE_CPDF_ICCPROFILE_H_
 
 #include "core/fxcrt/cfx_retain_ptr.h"
+#include "core/fxcrt/cfx_unowned_ptr.h"
 
 class CPDF_Stream;
 
@@ -16,7 +17,7 @@
   template <typename T, typename... Args>
   friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args);
 
-  CPDF_Stream* GetStream() const { return m_pStream; }
+  CPDF_Stream* GetStream() const { return m_pStream.Get(); }
   bool IsValid() const { return IsSRGB() || IsSupported(); }
   bool IsSRGB() const { return m_bsRGB; }
   bool IsSupported() const { return !!m_pTransform; }
@@ -28,7 +29,7 @@
   ~CPDF_IccProfile() override;
 
   const bool m_bsRGB;
-  CPDF_Stream* const m_pStream;
+  CFX_UnownedPtr<CPDF_Stream> const m_pStream;
   void* m_pTransform = nullptr;
   uint32_t m_nSrcComponents = 0;
 };
diff --git a/core/fpdfapi/page/cpdf_meshstream.h b/core/fpdfapi/page/cpdf_meshstream.h
index e02c3bb..af636a9 100644
--- a/core/fpdfapi/page/cpdf_meshstream.h
+++ b/core/fpdfapi/page/cpdf_meshstream.h
@@ -67,8 +67,8 @@
 
   const ShadingType m_type;
   const std::vector<std::unique_ptr<CPDF_Function>>& m_funcs;
-  CPDF_Stream* const m_pShadingStream;
-  CPDF_ColorSpace* const m_pCS;
+  CFX_UnownedPtr<CPDF_Stream> const m_pShadingStream;
+  CFX_UnownedPtr<CPDF_ColorSpace> const m_pCS;
   uint32_t m_nCoordBits;
   uint32_t m_nComponentBits;
   uint32_t m_nFlagBits;
diff --git a/core/fpdfapi/parser/cpdf_stream_acc.h b/core/fpdfapi/parser/cpdf_stream_acc.h
index 5bfdd18..bc03cb0 100644
--- a/core/fpdfapi/parser/cpdf_stream_acc.h
+++ b/core/fpdfapi/parser/cpdf_stream_acc.h
@@ -27,7 +27,7 @@
                    uint32_t estimated_size = 0,
                    bool bImageAcc = false);
 
-  const CPDF_Stream* GetStream() const { return m_pStream; }
+  const CPDF_Stream* GetStream() const { return m_pStream.Get(); }
   CPDF_Dictionary* GetDict() const {
     return m_pStream ? m_pStream->GetDict() : nullptr;
   }
@@ -47,7 +47,7 @@
   bool m_bNewBuf;
   CFX_ByteString m_ImageDecoder;
   CPDF_Dictionary* m_pImageParam;
-  const CPDF_Stream* const m_pStream;
+  CFX_UnownedPtr<const CPDF_Stream> const m_pStream;
   uint8_t* m_pSrcData;
 };
 
diff --git a/core/fpdfapi/render/cpdf_pagerendercache.h b/core/fpdfapi/render/cpdf_pagerendercache.h
index 626e24b..a874212 100644
--- a/core/fpdfapi/render/cpdf_pagerendercache.h
+++ b/core/fpdfapi/render/cpdf_pagerendercache.h
@@ -10,6 +10,7 @@
 #include <map>
 
 #include "core/fxcrt/cfx_retain_ptr.h"
+#include "core/fxcrt/cfx_unowned_ptr.h"
 #include "core/fxcrt/fx_system.h"
 
 class CFX_DIBitmap;
@@ -29,7 +30,7 @@
   uint32_t GetTimeCount() const { return m_nTimeCount; }
   void ResetBitmap(const CFX_RetainPtr<CPDF_Image>& pImage,
                    const CFX_RetainPtr<CFX_DIBitmap>& pBitmap);
-  CPDF_Page* GetPage() const { return m_pPage; }
+  CPDF_Page* GetPage() const { return m_pPage.Get(); }
   CPDF_ImageCacheEntry* GetCurImageCacheEntry() const {
     return m_pCurImageCacheEntry;
   }
@@ -45,7 +46,7 @@
  private:
   void ClearImageCacheEntry(CPDF_Stream* pStream);
 
-  CPDF_Page* const m_pPage;
+  CFX_UnownedPtr<CPDF_Page> const m_pPage;
   CPDF_ImageCacheEntry* m_pCurImageCacheEntry;
   std::map<CPDF_Stream*, CPDF_ImageCacheEntry*> m_ImageCache;
   uint32_t m_nTimeCount;
diff --git a/core/fpdfapi/render/cpdf_progressiverenderer.cpp b/core/fpdfapi/render/cpdf_progressiverenderer.cpp
index c3cef1f..4505db6 100644
--- a/core/fpdfapi/render/cpdf_progressiverenderer.cpp
+++ b/core/fpdfapi/render/cpdf_progressiverenderer.cpp
@@ -53,8 +53,9 @@
           m_pCurrentLayer->m_pObjectHolder->GetPageObjectList()->end();
       m_pRenderStatus = pdfium::MakeUnique<CPDF_RenderStatus>();
       m_pRenderStatus->Initialize(
-          m_pContext, m_pDevice, nullptr, nullptr, nullptr, nullptr, m_pOptions,
-          m_pCurrentLayer->m_pObjectHolder->m_Transparency, false, nullptr);
+          m_pContext.Get(), m_pDevice.Get(), nullptr, nullptr, nullptr, nullptr,
+          m_pOptions, m_pCurrentLayer->m_pObjectHolder->m_Transparency, false,
+          nullptr);
       m_pDevice->SaveState();
       m_ClipRect = CFX_FloatRect(m_pDevice->GetClipBox());
       CFX_Matrix device2object;
diff --git a/core/fpdfapi/render/cpdf_progressiverenderer.h b/core/fpdfapi/render/cpdf_progressiverenderer.h
index f0b1579..cadbd2d 100644
--- a/core/fpdfapi/render/cpdf_progressiverenderer.h
+++ b/core/fpdfapi/render/cpdf_progressiverenderer.h
@@ -47,8 +47,8 @@
   static const int kStepLimit = 100;
 
   Status m_Status;
-  CPDF_RenderContext* const m_pContext;
-  CFX_RenderDevice* const m_pDevice;
+  CFX_UnownedPtr<CPDF_RenderContext> const m_pContext;
+  CFX_UnownedPtr<CFX_RenderDevice> const m_pDevice;
   const CPDF_RenderOptions* const m_pOptions;
   std::unique_ptr<CPDF_RenderStatus> m_pRenderStatus;
   CFX_FloatRect m_ClipRect;
diff --git a/core/fpdfapi/render/cpdf_renderstatus.cpp b/core/fpdfapi/render/cpdf_renderstatus.cpp
index 1d2218c..7c17ff5 100644
--- a/core/fpdfapi/render/cpdf_renderstatus.cpp
+++ b/core/fpdfapi/render/cpdf_renderstatus.cpp
@@ -82,11 +82,11 @@
 
   ~CPDF_RefType3Cache() {
     while (m_dwCount--)
-      ReleaseCachedType3(m_pType3Font);
+      ReleaseCachedType3(m_pType3Font.Get());
   }
 
   uint32_t m_dwCount;
-  CPDF_Type3Font* const m_pType3Font;
+  CFX_UnownedPtr<CPDF_Type3Font> const m_pType3Font;
 };
 
 uint32_t CountOutputs(
diff --git a/core/fpdfapi/render/cpdf_type3cache.h b/core/fpdfapi/render/cpdf_type3cache.h
index 3c7d95b..36a1d6f 100644
--- a/core/fpdfapi/render/cpdf_type3cache.h
+++ b/core/fpdfapi/render/cpdf_type3cache.h
@@ -38,7 +38,7 @@
                                                float retinaScaleX,
                                                float retinaScaleY);
 
-  CPDF_Type3Font* const m_pFont;
+  CFX_UnownedPtr<CPDF_Type3Font> const m_pFont;
   std::map<CFX_ByteString, std::unique_ptr<CPDF_Type3Glyphs>> m_SizeMap;
 };
 
diff --git a/core/fpdfdoc/cpdf_aaction.cpp b/core/fpdfdoc/cpdf_aaction.cpp
index 7dc4282..033bf03 100644
--- a/core/fpdfdoc/cpdf_aaction.cpp
+++ b/core/fpdfdoc/cpdf_aaction.cpp
@@ -14,6 +14,14 @@
 
 }  // namespace
 
+CPDF_AAction::CPDF_AAction() {}
+
+CPDF_AAction::CPDF_AAction(CPDF_Dictionary* pDict) : m_pDict(pDict) {}
+
+CPDF_AAction::CPDF_AAction(const CPDF_AAction& that) = default;
+
+CPDF_AAction::~CPDF_AAction() {}
+
 bool CPDF_AAction::ActionExist(AActionType eType) const {
   return m_pDict && m_pDict->KeyExist(g_sAATypes[eType]);
 }
diff --git a/core/fpdfdoc/cpdf_aaction.h b/core/fpdfdoc/cpdf_aaction.h
index d615915..b2b691f 100644
--- a/core/fpdfdoc/cpdf_aaction.h
+++ b/core/fpdfdoc/cpdf_aaction.h
@@ -37,15 +37,17 @@
     DocumentPrinted
   };
 
-  CPDF_AAction() : m_pDict(nullptr) {}
-  explicit CPDF_AAction(CPDF_Dictionary* pDict) : m_pDict(pDict) {}
+  CPDF_AAction();
+  explicit CPDF_AAction(CPDF_Dictionary* pDict);
+  CPDF_AAction(const CPDF_AAction& that);
+  ~CPDF_AAction();
 
   bool ActionExist(AActionType eType) const;
   CPDF_Action GetAction(AActionType eType) const;
-  CPDF_Dictionary* GetDict() const { return m_pDict; }
+  CPDF_Dictionary* GetDict() const { return m_pDict.Get(); }
 
  private:
-  CPDF_Dictionary* const m_pDict;
+  CFX_UnownedPtr<CPDF_Dictionary> const m_pDict;
 };
 
 #endif  // CORE_FPDFDOC_CPDF_AACTION_H_
diff --git a/core/fpdfdoc/cpdf_action.cpp b/core/fpdfdoc/cpdf_action.cpp
index f523f86..b40fdcd 100644
--- a/core/fpdfdoc/cpdf_action.cpp
+++ b/core/fpdfdoc/cpdf_action.cpp
@@ -21,6 +21,14 @@
 
 }  // namespace
 
+CPDF_Action::CPDF_Action() {}
+
+CPDF_Action::CPDF_Action(CPDF_Dictionary* pDict) : m_pDict(pDict) {}
+
+CPDF_Action::CPDF_Action(const CPDF_Action& that) = default;
+
+CPDF_Action::~CPDF_Action() {}
+
 CPDF_Dest CPDF_Action::GetDest(CPDF_Document* pDoc) const {
   if (!m_pDict)
     return CPDF_Dest();
diff --git a/core/fpdfdoc/cpdf_action.h b/core/fpdfdoc/cpdf_action.h
index 426edb1..bd1ce58 100644
--- a/core/fpdfdoc/cpdf_action.h
+++ b/core/fpdfdoc/cpdf_action.h
@@ -37,10 +37,12 @@
     GoTo3DView
   };
 
-  CPDF_Action() : m_pDict(nullptr) {}
-  explicit CPDF_Action(CPDF_Dictionary* pDict) : m_pDict(pDict) {}
+  CPDF_Action();
+  explicit CPDF_Action(CPDF_Dictionary* pDict);
+  CPDF_Action(const CPDF_Action& that);
+  ~CPDF_Action();
 
-  CPDF_Dictionary* GetDict() const { return m_pDict; }
+  CPDF_Dictionary* GetDict() const { return m_pDict.Get(); }
   ActionType GetType() const;
   CPDF_Dest GetDest(CPDF_Document* pDoc) const;
   CFX_WideString GetFilePath() const;
@@ -53,7 +55,7 @@
   CPDF_Action GetSubAction(size_t iIndex) const;
 
  private:
-  CPDF_Dictionary* const m_pDict;
+  CFX_UnownedPtr<CPDF_Dictionary> const m_pDict;
 };
 
 #endif  // CORE_FPDFDOC_CPDF_ACTION_H_
diff --git a/core/fpdfdoc/cpdf_actionfields.cpp b/core/fpdfdoc/cpdf_actionfields.cpp
index 35ec92c..f3c3f5e 100644
--- a/core/fpdfdoc/cpdf_actionfields.cpp
+++ b/core/fpdfdoc/cpdf_actionfields.cpp
@@ -10,6 +10,11 @@
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fpdfdoc/cpdf_action.h"
 
+CPDF_ActionFields::CPDF_ActionFields(const CPDF_Action* pAction)
+    : m_pAction(pAction) {}
+
+CPDF_ActionFields::~CPDF_ActionFields() {}
+
 size_t CPDF_ActionFields::GetFieldsCount() const {
   if (!m_pAction)
     return 0;
diff --git a/core/fpdfdoc/cpdf_actionfields.h b/core/fpdfdoc/cpdf_actionfields.h
index 9e0664c..71b4247 100644
--- a/core/fpdfdoc/cpdf_actionfields.h
+++ b/core/fpdfdoc/cpdf_actionfields.h
@@ -11,19 +11,22 @@
 
 #include <vector>
 
+#include "core/fxcrt/cfx_unowned_ptr.h"
+
 class CPDF_Action;
 class CPDF_Object;
 
 class CPDF_ActionFields {
  public:
-  explicit CPDF_ActionFields(const CPDF_Action* pAction) : m_pAction(pAction) {}
+  explicit CPDF_ActionFields(const CPDF_Action* pAction);
+  ~CPDF_ActionFields();
 
   size_t GetFieldsCount() const;
   std::vector<CPDF_Object*> GetAllFields() const;
   CPDF_Object* GetField(size_t iIndex) const;
 
  private:
-  const CPDF_Action* const m_pAction;
+  CFX_UnownedPtr<const CPDF_Action> const m_pAction;
 };
 
 #endif  // CORE_FPDFDOC_CPDF_ACTIONFIELDS_H_
diff --git a/core/fpdfdoc/cpdf_annot.cpp b/core/fpdfdoc/cpdf_annot.cpp
index 146c863..eab6cd0 100644
--- a/core/fpdfdoc/cpdf_annot.cpp
+++ b/core/fpdfdoc/cpdf_annot.cpp
@@ -90,23 +90,23 @@
   CPDF_Dictionary* pDict = m_pAnnotDict.Get();
   bool result = false;
   if (m_nSubtype == CPDF_Annot::Subtype::CIRCLE)
-    result = CPVT_GenerateAP::GenerateCircleAP(m_pDocument, pDict);
+    result = CPVT_GenerateAP::GenerateCircleAP(m_pDocument.Get(), pDict);
   else if (m_nSubtype == CPDF_Annot::Subtype::HIGHLIGHT)
-    result = CPVT_GenerateAP::GenerateHighlightAP(m_pDocument, pDict);
+    result = CPVT_GenerateAP::GenerateHighlightAP(m_pDocument.Get(), pDict);
   else if (m_nSubtype == CPDF_Annot::Subtype::INK)
-    result = CPVT_GenerateAP::GenerateInkAP(m_pDocument, pDict);
+    result = CPVT_GenerateAP::GenerateInkAP(m_pDocument.Get(), pDict);
   else if (m_nSubtype == CPDF_Annot::Subtype::POPUP)
-    result = CPVT_GenerateAP::GeneratePopupAP(m_pDocument, pDict);
+    result = CPVT_GenerateAP::GeneratePopupAP(m_pDocument.Get(), pDict);
   else if (m_nSubtype == CPDF_Annot::Subtype::SQUARE)
-    result = CPVT_GenerateAP::GenerateSquareAP(m_pDocument, pDict);
+    result = CPVT_GenerateAP::GenerateSquareAP(m_pDocument.Get(), pDict);
   else if (m_nSubtype == CPDF_Annot::Subtype::SQUIGGLY)
-    result = CPVT_GenerateAP::GenerateSquigglyAP(m_pDocument, pDict);
+    result = CPVT_GenerateAP::GenerateSquigglyAP(m_pDocument.Get(), pDict);
   else if (m_nSubtype == CPDF_Annot::Subtype::STRIKEOUT)
-    result = CPVT_GenerateAP::GenerateStrikeOutAP(m_pDocument, pDict);
+    result = CPVT_GenerateAP::GenerateStrikeOutAP(m_pDocument.Get(), pDict);
   else if (m_nSubtype == CPDF_Annot::Subtype::TEXT)
-    result = CPVT_GenerateAP::GenerateTextAP(m_pDocument, pDict);
+    result = CPVT_GenerateAP::GenerateTextAP(m_pDocument.Get(), pDict);
   else if (m_nSubtype == CPDF_Annot::Subtype::UNDERLINE)
-    result = CPVT_GenerateAP::GenerateUnderlineAP(m_pDocument, pDict);
+    result = CPVT_GenerateAP::GenerateUnderlineAP(m_pDocument.Get(), pDict);
 
   if (result) {
     m_pAnnotDict->SetNewFor<CPDF_Boolean>(kPDFiumKey_HasGeneratedAP, result);
@@ -205,7 +205,7 @@
     return it->second.get();
 
   auto pNewForm = pdfium::MakeUnique<CPDF_Form>(
-      m_pDocument, pPage->m_pResources.Get(), pStream);
+      m_pDocument.Get(), pPage->m_pResources.Get(), pStream);
   pNewForm->ParseContent(nullptr, nullptr, nullptr);
 
   CPDF_Form* pResult = pNewForm.get();
diff --git a/core/fpdfdoc/cpdf_annot.h b/core/fpdfdoc/cpdf_annot.h
index 188106a..85a2053 100644
--- a/core/fpdfdoc/cpdf_annot.h
+++ b/core/fpdfdoc/cpdf_annot.h
@@ -82,7 +82,7 @@
   CPDF_Annot::Subtype GetSubtype() const;
   uint32_t GetFlags() const;
   CFX_FloatRect GetRect() const;
-  CPDF_Document* GetDocument() const { return m_pDocument; }
+  CPDF_Document* GetDocument() const { return m_pDocument.Get(); }
   CPDF_Dictionary* GetAnnotDict() const { return m_pAnnotDict.Get(); }
 
   bool DrawAppearance(CPDF_Page* pPage,
@@ -112,7 +112,7 @@
   CFX_FloatRect RectForDrawing() const;
 
   CFX_MaybeOwned<CPDF_Dictionary> m_pAnnotDict;
-  CPDF_Document* const m_pDocument;
+  CFX_UnownedPtr<CPDF_Document> const m_pDocument;
   CPDF_Annot::Subtype m_nSubtype;
   std::map<CPDF_Stream*, std::unique_ptr<CPDF_Form>> m_APMap;
   // |m_bOpenState| is only set for popup annotations.
diff --git a/core/fpdfdoc/cpdf_apsettings.cpp b/core/fpdfdoc/cpdf_apsettings.cpp
index 4c44f5a..ee205e0 100644
--- a/core/fpdfdoc/cpdf_apsettings.cpp
+++ b/core/fpdfdoc/cpdf_apsettings.cpp
@@ -14,6 +14,10 @@
 
 CPDF_ApSettings::CPDF_ApSettings(CPDF_Dictionary* pDict) : m_pDict(pDict) {}
 
+CPDF_ApSettings::CPDF_ApSettings(const CPDF_ApSettings& that) = default;
+
+CPDF_ApSettings::~CPDF_ApSettings() {}
+
 bool CPDF_ApSettings::HasMKEntry(const CFX_ByteString& csEntry) const {
   return m_pDict && m_pDict->KeyExist(csEntry);
 }
diff --git a/core/fpdfdoc/cpdf_apsettings.h b/core/fpdfdoc/cpdf_apsettings.h
index ba0b05b..2f8f9e4 100644
--- a/core/fpdfdoc/cpdf_apsettings.h
+++ b/core/fpdfdoc/cpdf_apsettings.h
@@ -8,6 +8,7 @@
 #define CORE_FPDFDOC_CPDF_APSETTINGS_H_
 
 #include "core/fpdfdoc/cpdf_iconfit.h"
+#include "core/fxcrt/cfx_unowned_ptr.h"
 #include "core/fxcrt/fx_string.h"
 #include "core/fxcrt/fx_system.h"
 #include "core/fxge/fx_dib.h"
@@ -19,6 +20,8 @@
 class CPDF_ApSettings {
  public:
   explicit CPDF_ApSettings(CPDF_Dictionary* pDict);
+  CPDF_ApSettings(const CPDF_ApSettings& that);
+  ~CPDF_ApSettings();
 
   bool HasMKEntry(const CFX_ByteString& csEntry) const;
   int GetRotation() const;
@@ -68,7 +71,7 @@
   CFX_WideString GetCaption(const CFX_ByteString& csEntry) const;
   CPDF_Stream* GetIcon(const CFX_ByteString& csEntry) const;
 
-  CPDF_Dictionary* const m_pDict;
+  CFX_UnownedPtr<CPDF_Dictionary> const m_pDict;
 };
 
 #endif  // CORE_FPDFDOC_CPDF_APSETTINGS_H_
diff --git a/core/fpdfdoc/cpdf_formcontrol.cpp b/core/fpdfdoc/cpdf_formcontrol.cpp
index d9740f7..54524a6 100644
--- a/core/fpdfdoc/cpdf_formcontrol.cpp
+++ b/core/fpdfdoc/cpdf_formcontrol.cpp
@@ -32,6 +32,8 @@
       m_pWidgetDict(pWidgetDict),
       m_pForm(m_pField->GetForm()) {}
 
+CPDF_FormControl::~CPDF_FormControl() {}
+
 CFX_ByteString CPDF_FormControl::GetOnStateName() const {
   ASSERT(GetType() == CPDF_FormField::CheckBox ||
          GetType() == CPDF_FormField::RadioButton);
@@ -167,7 +169,7 @@
   if (m_pWidgetDict->GetIntegerFor("F") & ANNOTFLAG_HIDDEN)
     return;
 
-  CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(m_pWidgetDict, mode);
+  CPDF_Stream* pStream = FPDFDOC_GetAnnotAP(m_pWidgetDict.Get(), mode);
   if (!pStream)
     return;
 
@@ -178,7 +180,7 @@
   CFX_Matrix matrix;
   matrix.MatchRect(arect, form_bbox);
   matrix.Concat(*pMatrix);
-  CPDF_Form form(m_pField->GetForm()->m_pDocument,
+  CPDF_Form form(m_pField->GetForm()->m_pDocument.Get(),
                  m_pField->GetForm()->m_pFormDict->GetDictFor("DR"), pStream);
   form.ParseContent(nullptr, nullptr, nullptr);
   CPDF_RenderContext context(pPage);
@@ -286,7 +288,7 @@
   if (csFontNameTag.IsEmpty())
     return nullptr;
 
-  CPDF_Object* pObj = FPDF_GetFieldAttr(m_pWidgetDict, "DR");
+  CPDF_Object* pObj = FPDF_GetFieldAttr(m_pWidgetDict.Get(), "DR");
   if (CPDF_Dictionary* pDict = ToDictionary(pObj)) {
     CPDF_Dictionary* pFonts = pDict->GetDictFor("Font");
     if (pFonts) {
diff --git a/core/fpdfdoc/cpdf_formcontrol.h b/core/fpdfdoc/cpdf_formcontrol.h
index 154d592..eb63b5b 100644
--- a/core/fpdfdoc/cpdf_formcontrol.h
+++ b/core/fpdfdoc/cpdf_formcontrol.h
@@ -47,11 +47,12 @@
   enum HighlightingMode { None = 0, Invert, Outline, Push, Toggle };
 
   CPDF_FormControl(CPDF_FormField* pField, CPDF_Dictionary* pWidgetDict);
+  ~CPDF_FormControl();
 
   CPDF_FormField::Type GetType() const { return m_pField->GetType(); }
-  const CPDF_InterForm* GetInterForm() const { return m_pForm; }
+  const CPDF_InterForm* GetInterForm() const { return m_pForm.Get(); }
   CPDF_FormField* GetField() const { return m_pField; }
-  CPDF_Dictionary* GetWidget() const { return m_pWidgetDict; }
+  CPDF_Dictionary* GetWidget() const { return m_pWidgetDict.Get(); }
   CFX_FloatRect GetRect() const { return m_pWidgetDict->GetRectFor("Rect"); }
 
   void DrawControl(CFX_RenderDevice* pDevice,
@@ -127,8 +128,8 @@
   CPDF_ApSettings GetMK() const;
 
   CPDF_FormField* const m_pField;
-  CPDF_Dictionary* const m_pWidgetDict;
-  const CPDF_InterForm* const m_pForm;
+  CFX_UnownedPtr<CPDF_Dictionary> const m_pWidgetDict;
+  CFX_UnownedPtr<const CPDF_InterForm> const m_pForm;
 };
 
 #endif  // CORE_FPDFDOC_CPDF_FORMCONTROL_H_
diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp
index 7a48826..97ea052 100644
--- a/core/fpdfdoc/cpdf_formfield.cpp
+++ b/core/fpdfdoc/cpdf_formfield.cpp
@@ -408,7 +408,7 @@
   for (auto& pControl : m_ControlList) {
     if (!pControl)
       continue;
-    CPDF_Dictionary* pWidgetDict = pControl->m_pWidgetDict;
+    CPDF_Dictionary* pWidgetDict = pControl->m_pWidgetDict.Get();
     if (pWidgetDict->KeyExist("MaxLen"))
       return pWidgetDict->GetIntegerFor("MaxLen");
   }
diff --git a/core/fpdfdoc/cpdf_iconfit.cpp b/core/fpdfdoc/cpdf_iconfit.cpp
index 0f05d7d..a28f3bc 100644
--- a/core/fpdfdoc/cpdf_iconfit.cpp
+++ b/core/fpdfdoc/cpdf_iconfit.cpp
@@ -10,6 +10,12 @@
 #include "core/fpdfapi/parser/cpdf_dictionary.h"
 #include "core/fxcrt/fx_string.h"
 
+CPDF_IconFit::CPDF_IconFit(const CPDF_Dictionary* pDict) : m_pDict(pDict) {}
+
+CPDF_IconFit::CPDF_IconFit(const CPDF_IconFit& that) = default;
+
+CPDF_IconFit::~CPDF_IconFit() {}
+
 CPDF_IconFit::ScaleMethod CPDF_IconFit::GetScaleMethod() {
   if (!m_pDict)
     return Always;
diff --git a/core/fpdfdoc/cpdf_iconfit.h b/core/fpdfdoc/cpdf_iconfit.h
index 6e3b8cf..0fa8d37 100644
--- a/core/fpdfdoc/cpdf_iconfit.h
+++ b/core/fpdfdoc/cpdf_iconfit.h
@@ -7,6 +7,7 @@
 #ifndef CORE_FPDFDOC_CPDF_ICONFIT_H_
 #define CORE_FPDFDOC_CPDF_ICONFIT_H_
 
+#include "core/fxcrt/cfx_unowned_ptr.h"
 #include "core/fxcrt/fx_system.h"
 
 class CPDF_Dictionary;
@@ -15,16 +16,18 @@
  public:
   enum ScaleMethod { Always = 0, Bigger, Smaller, Never };
 
-  explicit CPDF_IconFit(const CPDF_Dictionary* pDict) : m_pDict(pDict) {}
+  explicit CPDF_IconFit(const CPDF_Dictionary* pDict);
+  CPDF_IconFit(const CPDF_IconFit& that);
+  ~CPDF_IconFit();
 
   ScaleMethod GetScaleMethod();
   bool IsProportionalScale();
   void GetIconPosition(float& fLeft, float& fBottom);
   bool GetFittingBounds();
-  const CPDF_Dictionary* GetDict() const { return m_pDict; }
+  const CPDF_Dictionary* GetDict() const { return m_pDict.Get(); }
 
  private:
-  const CPDF_Dictionary* const m_pDict;
+  CFX_UnownedPtr<const CPDF_Dictionary> const m_pDict;
 };
 
 #endif  // CORE_FPDFDOC_CPDF_ICONFIT_H_
diff --git a/core/fpdfdoc/cpdf_interform.cpp b/core/fpdfdoc/cpdf_interform.cpp
index 455158b..de99a5d 100644
--- a/core/fpdfdoc/cpdf_interform.cpp
+++ b/core/fpdfdoc/cpdf_interform.cpp
@@ -961,7 +961,7 @@
 }
 
 CPDF_Font* CPDF_InterForm::GetFormFont(CFX_ByteString csNameTag) const {
-  return GetFont(m_pFormDict, m_pDocument, csNameTag);
+  return GetFont(m_pFormDict, m_pDocument.Get(), csNameTag);
 }
 
 CPDF_DefaultAppearance CPDF_InterForm::GetDefaultAppearance() const {
diff --git a/core/fpdfdoc/cpdf_interform.h b/core/fpdfdoc/cpdf_interform.h
index 23530e5..0a9b3e0 100644
--- a/core/fpdfdoc/cpdf_interform.h
+++ b/core/fpdfdoc/cpdf_interform.h
@@ -108,7 +108,7 @@
 
   static bool s_bUpdateAP;
 
-  CPDF_Document* const m_pDocument;
+  CFX_UnownedPtr<CPDF_Document> const m_pDocument;
   CPDF_Dictionary* m_pFormDict;
   std::map<const CPDF_Dictionary*, std::unique_ptr<CPDF_FormControl>>
       m_ControlMap;
diff --git a/core/fpdfdoc/cpdf_numbertree.cpp b/core/fpdfdoc/cpdf_numbertree.cpp
index 5f2bc06..952fb4e 100644
--- a/core/fpdfdoc/cpdf_numbertree.cpp
+++ b/core/fpdfdoc/cpdf_numbertree.cpp
@@ -47,6 +47,10 @@
 
 }  // namespace
 
+CPDF_NumberTree::CPDF_NumberTree(CPDF_Dictionary* pRoot) : m_pRoot(pRoot) {}
+
+CPDF_NumberTree::~CPDF_NumberTree() {}
+
 CPDF_Object* CPDF_NumberTree::LookupValue(int num) const {
-  return SearchNumberNode(m_pRoot, num);
+  return SearchNumberNode(m_pRoot.Get(), num);
 }
diff --git a/core/fpdfdoc/cpdf_numbertree.h b/core/fpdfdoc/cpdf_numbertree.h
index bfe44fd..843e186 100644
--- a/core/fpdfdoc/cpdf_numbertree.h
+++ b/core/fpdfdoc/cpdf_numbertree.h
@@ -7,17 +7,20 @@
 #ifndef CORE_FPDFDOC_CPDF_NUMBERTREE_H_
 #define CORE_FPDFDOC_CPDF_NUMBERTREE_H_
 
+#include "core/fxcrt/cfx_unowned_ptr.h"
+
 class CPDF_Dictionary;
 class CPDF_Object;
 
 class CPDF_NumberTree {
  public:
-  explicit CPDF_NumberTree(CPDF_Dictionary* pRoot) : m_pRoot(pRoot) {}
+  explicit CPDF_NumberTree(CPDF_Dictionary* pRoot);
+  ~CPDF_NumberTree();
 
   CPDF_Object* LookupValue(int num) const;
 
  protected:
-  CPDF_Dictionary* const m_pRoot;
+  CFX_UnownedPtr<CPDF_Dictionary> const m_pRoot;
 };
 
 #endif  // CORE_FPDFDOC_CPDF_NUMBERTREE_H_
diff --git a/core/fpdfdoc/cpdf_occontext.cpp b/core/fpdfdoc/cpdf_occontext.cpp
index c4272f4..66950c9 100644
--- a/core/fpdfdoc/cpdf_occontext.cpp
+++ b/core/fpdfdoc/cpdf_occontext.cpp
@@ -101,7 +101,7 @@
 bool CPDF_OCContext::LoadOCGStateFromConfig(
     const CFX_ByteString& csConfig,
     const CPDF_Dictionary* pOCGDict) const {
-  CPDF_Dictionary* pConfig = GetConfig(m_pDocument, pOCGDict);
+  CPDF_Dictionary* pConfig = GetConfig(m_pDocument.Get(), pOCGDict);
   if (!pConfig)
     return true;
 
diff --git a/core/fpdfdoc/cpdf_occontext.h b/core/fpdfdoc/cpdf_occontext.h
index 753aa5c..ea8eea2 100644
--- a/core/fpdfdoc/cpdf_occontext.h
+++ b/core/fpdfdoc/cpdf_occontext.h
@@ -38,7 +38,7 @@
   bool GetOCGVE(CPDF_Array* pExpression, int nLevel);
   bool LoadOCMDState(const CPDF_Dictionary* pOCMDDict);
 
-  CPDF_Document* const m_pDocument;
+  CFX_UnownedPtr<CPDF_Document> const m_pDocument;
   const UsageType m_eUsageType;
   std::map<const CPDF_Dictionary*, bool> m_OCGStates;
 };
diff --git a/core/fpdfdoc/cpdf_pagelabel.cpp b/core/fpdfdoc/cpdf_pagelabel.cpp
index 4200a28..3892aba 100644
--- a/core/fpdfdoc/cpdf_pagelabel.cpp
+++ b/core/fpdfdoc/cpdf_pagelabel.cpp
@@ -75,6 +75,8 @@
 CPDF_PageLabel::CPDF_PageLabel(CPDF_Document* pDocument)
     : m_pDocument(pDocument) {}
 
+CPDF_PageLabel::~CPDF_PageLabel() {}
+
 bool CPDF_PageLabel::GetLabel(int nPage, CFX_WideString* wsLabel) const {
   if (!m_pDocument)
     return false;
diff --git a/core/fpdfdoc/cpdf_pagelabel.h b/core/fpdfdoc/cpdf_pagelabel.h
index 0f91f61..66324f8 100644
--- a/core/fpdfdoc/cpdf_pagelabel.h
+++ b/core/fpdfdoc/cpdf_pagelabel.h
@@ -14,13 +14,14 @@
 class CPDF_PageLabel {
  public:
   explicit CPDF_PageLabel(CPDF_Document* pDocument);
+  ~CPDF_PageLabel();
 
   bool GetLabel(int nPage, CFX_WideString* wsLabel) const;
   int32_t GetPageByLabel(const CFX_ByteStringC& bsLabel) const;
   int32_t GetPageByLabel(const CFX_WideStringC& wsLabel) const;
 
  private:
-  CPDF_Document* const m_pDocument;
+  CFX_UnownedPtr<CPDF_Document> const m_pDocument;
 };
 
 #endif  // CORE_FPDFDOC_CPDF_PAGELABEL_H_
diff --git a/core/fpdfdoc/cpdf_structelement.cpp b/core/fpdfdoc/cpdf_structelement.cpp
index c85ae0d..c5f2b6b 100644
--- a/core/fpdfdoc/cpdf_structelement.cpp
+++ b/core/fpdfdoc/cpdf_structelement.cpp
@@ -133,5 +133,5 @@
   }
 
   pKid->m_pElement =
-      pdfium::MakeRetain<CPDF_StructElement>(m_pTree, this, pKidDict);
+      pdfium::MakeRetain<CPDF_StructElement>(m_pTree.Get(), this, pKidDict);
 }
diff --git a/core/fpdfdoc/cpdf_structelement.h b/core/fpdfdoc/cpdf_structelement.h
index d8820a1..8fe73e5 100644
--- a/core/fpdfdoc/cpdf_structelement.h
+++ b/core/fpdfdoc/cpdf_structelement.h
@@ -41,7 +41,7 @@
 
   const CFX_ByteString& GetType() const { return m_Type; }
   const CFX_ByteString& GetTitle() const { return m_Title; }
-  CPDF_Dictionary* GetDict() const { return m_pDict; }
+  CPDF_Dictionary* GetDict() const { return m_pDict.Get(); }
 
   int CountKids() const;
   CPDF_StructElement* GetKidIfElement(int index) const;
@@ -56,9 +56,9 @@
   void LoadKids(CPDF_Dictionary* pDict);
   void LoadKid(uint32_t PageObjNum, CPDF_Object* pObj, CPDF_StructKid* pKid);
 
-  CPDF_StructTree* const m_pTree;
-  CPDF_StructElement* const m_pParent;
-  CPDF_Dictionary* const m_pDict;
+  CFX_UnownedPtr<CPDF_StructTree> const m_pTree;
+  CFX_UnownedPtr<CPDF_StructElement> const m_pParent;
+  CFX_UnownedPtr<CPDF_Dictionary> const m_pDict;
   CFX_ByteString m_Type;
   CFX_ByteString m_Title;
   std::vector<CPDF_StructKid> m_Kids;
diff --git a/core/fpdfdoc/cpdf_variabletext.h b/core/fpdfdoc/cpdf_variabletext.h
index 2e6caf6..a226350 100644
--- a/core/fpdfdoc/cpdf_variabletext.h
+++ b/core/fpdfdoc/cpdf_variabletext.h
@@ -58,7 +58,7 @@
 
    private:
     CPVT_WordPlace m_CurPos;
-    CPDF_VariableText* const m_pVT;
+    CFX_UnownedPtr<CPDF_VariableText> const m_pVT;
   };
 
   class Provider {
diff --git a/core/fpdfdoc/csection.h b/core/fpdfdoc/csection.h
index b465d1d..aefdcaf 100644
--- a/core/fpdfdoc/csection.h
+++ b/core/fpdfdoc/csection.h
@@ -57,7 +57,7 @@
   void ClearRightWords(int32_t nWordIndex);
   void ClearMidWords(int32_t nBeginIndex, int32_t nEndIndex);
 
-  CPDF_VariableText* const m_pVT;
+  CFX_UnownedPtr<CPDF_VariableText> const m_pVT;
 };
 
 #endif  // CORE_FPDFDOC_CSECTION_H_
diff --git a/core/fpdfdoc/ctypeset.h b/core/fpdfdoc/ctypeset.h
index f769fe1..1b016ea 100644
--- a/core/fpdfdoc/ctypeset.h
+++ b/core/fpdfdoc/ctypeset.h
@@ -27,7 +27,7 @@
   void OutputLines();
 
   CPVT_FloatRect m_rcRet;
-  CPDF_VariableText* const m_pVT;
+  CFX_UnownedPtr<CPDF_VariableText> const m_pVT;
   CSection* const m_pSection;
 };
 
diff --git a/core/fpdftext/cpdf_linkextract.h b/core/fpdftext/cpdf_linkextract.h
index 43306cf..564d552 100644
--- a/core/fpdftext/cpdf_linkextract.h
+++ b/core/fpdftext/cpdf_linkextract.h
@@ -38,7 +38,7 @@
     CFX_WideString m_strUrl;
   };
 
-  const CPDF_TextPage* const m_pTextPage;
+  CFX_UnownedPtr<const CPDF_TextPage> const m_pTextPage;
   CFX_WideString m_strPageText;
   std::vector<Link> m_LinkArray;
 };
diff --git a/core/fxcodec/codec/cjpx_decoder.h b/core/fxcodec/codec/cjpx_decoder.h
index 2e63caa..a62b41a 100644
--- a/core/fxcodec/codec/cjpx_decoder.h
+++ b/core/fxcodec/codec/cjpx_decoder.h
@@ -9,6 +9,7 @@
 
 #include <vector>
 
+#include "core/fxcrt/cfx_unowned_ptr.h"
 #include "third_party/libopenjpeg20/openjpeg.h"
 
 class CPDF_ColorSpace;
@@ -30,7 +31,7 @@
   opj_image_t* image;
   opj_codec_t* l_codec;
   opj_stream_t* l_stream;
-  const CPDF_ColorSpace* const m_ColorSpace;
+  CFX_UnownedPtr<const CPDF_ColorSpace> const m_ColorSpace;
 };
 
 #endif  // CORE_FXCODEC_CODEC_CJPX_DECODER_H_
diff --git a/core/fxcodec/jbig2/JBig2_ArithDecoder.h b/core/fxcodec/jbig2/JBig2_ArithDecoder.h
index 24fb80c..2ec64fe 100644
--- a/core/fxcodec/jbig2/JBig2_ArithDecoder.h
+++ b/core/fxcodec/jbig2/JBig2_ArithDecoder.h
@@ -9,6 +9,8 @@
 
 #include <stdint.h>
 
+#include "core/fxcrt/cfx_unowned_ptr.h"
+
 class CJBig2_BitStream;
 
 struct JBig2ArithCtx {
@@ -37,7 +39,7 @@
   unsigned int m_C;
   unsigned int m_A;
   unsigned int m_CT;
-  CJBig2_BitStream* const m_pStream;
+  CFX_UnownedPtr<CJBig2_BitStream> const m_pStream;
 };
 
 #endif  // CORE_FXCODEC_JBIG2_JBIG2_ARITHDECODER_H_
diff --git a/core/fxcodec/jbig2/JBig2_Context.h b/core/fxcodec/jbig2/JBig2_Context.h
index 8568aab..4151fa3 100644
--- a/core/fxcodec/jbig2/JBig2_Context.h
+++ b/core/fxcodec/jbig2/JBig2_Context.h
@@ -95,7 +95,7 @@
   bool m_bInPage;
   bool m_bBufSpecified;
   int32_t m_PauseStep;
-  IFX_Pause* const m_pPause;
+  CFX_UnownedPtr<IFX_Pause> const m_pPause;
   FXCODEC_STATUS m_ProcessingStatus;
   std::vector<JBig2ArithCtx> m_gbContext;
   std::unique_ptr<CJBig2_ArithDecoder> m_pArithDecoder;
diff --git a/core/fxcodec/jbig2/JBig2_HuffmanDecoder.h b/core/fxcodec/jbig2/JBig2_HuffmanDecoder.h
index c72346a..c7192b7 100644
--- a/core/fxcodec/jbig2/JBig2_HuffmanDecoder.h
+++ b/core/fxcodec/jbig2/JBig2_HuffmanDecoder.h
@@ -18,7 +18,7 @@
   int decodeAValue(CJBig2_HuffmanTable* pTable, int* nResult);
 
  private:
-  CJBig2_BitStream* const m_pStream;
+  CFX_UnownedPtr<CJBig2_BitStream> const m_pStream;
 };
 
 #endif  // CORE_FXCODEC_JBIG2_JBIG2_HUFFMANDECODER_H_
diff --git a/core/fxcrt/xml/cxml_element.h b/core/fxcrt/xml/cxml_element.h
index 2eb6caf..d708032 100644
--- a/core/fxcrt/xml/cxml_element.h
+++ b/core/fxcrt/xml/cxml_element.h
@@ -30,7 +30,7 @@
   CFX_ByteString GetTagName(bool bQualified = false) const;
   CFX_ByteString GetNamespace(bool bQualified = false) const;
   CFX_ByteString GetNamespaceURI(const CFX_ByteString& qName) const;
-  const CXML_Element* GetParent() const { return m_pParent; }
+  const CXML_Element* GetParent() const { return m_pParent.Get(); }
   uint32_t CountAttrs() const { return m_AttrMap.GetSize(); }
   void GetAttrByIndex(int index,
                       CFX_ByteString* space,
@@ -104,7 +104,7 @@
   friend class CXML_Parser;
   friend class CXML_Composer;
 
-  const CXML_Element* const m_pParent;
+  CFX_UnownedPtr<const CXML_Element> const m_pParent;
   CFX_ByteString m_QSpaceName;
   CFX_ByteString m_TagName;
   CXML_AttrMap m_AttrMap;
diff --git a/core/fxge/cfx_fontmapper.h b/core/fxge/cfx_fontmapper.h
index 96630a7..1b262bf 100644
--- a/core/fxge/cfx_fontmapper.h
+++ b/core/fxge/cfx_fontmapper.h
@@ -80,7 +80,7 @@
   std::vector<FaceData> m_FaceArray;
   std::unique_ptr<IFX_SystemFontInfo> m_pFontInfo;
   FXFT_Face m_FoxitFaces[FOXIT_FACE_COUNT];
-  CFX_FontMgr* const m_pFontMgr;
+  CFX_UnownedPtr<CFX_FontMgr> const m_pFontMgr;
 };
 
 #endif  // CORE_FXGE_CFX_FONTMAPPER_H_
diff --git a/core/fxge/dib/cfx_imagestretcher.cpp b/core/fxge/dib/cfx_imagestretcher.cpp
index 809b72d..3c0a065 100644
--- a/core/fxge/dib/cfx_imagestretcher.cpp
+++ b/core/fxge/dib/cfx_imagestretcher.cpp
@@ -133,8 +133,8 @@
 
 bool CFX_ImageStretcher::StartStretch() {
   m_pStretchEngine = pdfium::MakeUnique<CStretchEngine>(
-      m_pDest, m_DestFormat, m_DestWidth, m_DestHeight, m_ClipRect, m_pSource,
-      m_Flags);
+      m_pDest.Get(), m_DestFormat, m_DestWidth, m_DestHeight, m_ClipRect,
+      m_pSource, m_Flags);
   m_pStretchEngine->StartStretchHorz();
   if (SourceSizeWithinLimit(m_pSource->GetWidth(), m_pSource->GetHeight())) {
     m_pStretchEngine->Continue(nullptr);
diff --git a/core/fxge/dib/cfx_imagestretcher.h b/core/fxge/dib/cfx_imagestretcher.h
index 63863cd..45928ee 100644
--- a/core/fxge/dib/cfx_imagestretcher.h
+++ b/core/fxge/dib/cfx_imagestretcher.h
@@ -37,7 +37,7 @@
   bool ContinueQuickStretch(IFX_Pause* pPause);
   bool ContinueStretch(IFX_Pause* pPause);
 
-  IFX_ScanlineComposer* const m_pDest;
+  CFX_UnownedPtr<IFX_ScanlineComposer> const m_pDest;
   CFX_RetainPtr<CFX_DIBSource> m_pSource;
   std::unique_ptr<CStretchEngine> m_pStretchEngine;
   std::unique_ptr<uint8_t, FxFreeDeleter> m_pScanline;
diff --git a/core/fxge/dib/cfx_imagetransformer.h b/core/fxge/dib/cfx_imagetransformer.h
index 52aca97..90f605d 100644
--- a/core/fxge/dib/cfx_imagetransformer.h
+++ b/core/fxge/dib/cfx_imagetransformer.h
@@ -33,7 +33,7 @@
 
  private:
   const CFX_RetainPtr<CFX_DIBSource> m_pSrc;
-  const CFX_Matrix* const m_pMatrix;
+  CFX_UnownedPtr<const CFX_Matrix> const m_pMatrix;
   const FX_RECT* const m_pClip;
   FX_RECT m_StretchClip;
   FX_RECT m_result;