Convert more c-style pointers to CFX_UnownedPtr

Change-Id: I551b4210c95db0b916e9fe6cddf11e6c3d015c50
Reviewed-on: https://pdfium-review.googlesource.com/5790
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index e431b9b..8238414 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -49,7 +49,7 @@
 }  // namespace
 
 CPDF_PageContentGenerator::CPDF_PageContentGenerator(CPDF_Page* pPage)
-    : m_pPage(pPage), m_pDocument(m_pPage->m_pDocument) {
+    : m_pPage(pPage), m_pDocument(m_pPage->m_pDocument.Get()) {
   for (const auto& pObj : *pPage->GetPageObjectList()) {
     if (pObj)
       m_pageObjects.push_back(pObj.get());
@@ -68,7 +68,7 @@
     else if (CPDF_TextObject* pTextObj = pPageObj->AsText())
       ProcessText(&buf, pTextObj);
   }
-  CPDF_Dictionary* pPageDict = m_pPage->m_pFormDict;
+  CPDF_Dictionary* pPageDict = m_pPage->m_pFormDict.Get();
   CPDF_Object* pContent =
       pPageDict ? pPageDict->GetDirectObjectFor("Contents") : nullptr;
   if (pContent)
diff --git a/core/fpdfapi/font/cpdf_type3font.cpp b/core/fpdfapi/font/cpdf_type3font.cpp
index 79f6617..1e8c567 100644
--- a/core/fpdfapi/font/cpdf_type3font.cpp
+++ b/core/fpdfapi/font/cpdf_type3font.cpp
@@ -107,7 +107,8 @@
 
   auto pNewChar =
       pdfium::MakeUnique<CPDF_Type3Char>(pdfium::MakeUnique<CPDF_Form>(
-          m_pDocument, m_pFontResources ? m_pFontResources : m_pPageResources,
+          m_pDocument,
+          m_pFontResources ? m_pFontResources.Get() : m_pPageResources.Get(),
           pStream, nullptr));
 
   // This can trigger recursion into this method. The content of |m_CacheMap|
diff --git a/core/fpdfapi/font/cpdf_type3font.h b/core/fpdfapi/font/cpdf_type3font.h
index 0bae192..135a19f 100644
--- a/core/fpdfapi/font/cpdf_type3font.h
+++ b/core/fpdfapi/font/cpdf_type3font.h
@@ -48,9 +48,9 @@
   void LoadGlyphMap() override {}
 
   int m_CharWidthL[256];
-  CPDF_Dictionary* m_pCharProcs;
-  CPDF_Dictionary* m_pPageResources;
-  CPDF_Dictionary* m_pFontResources;
+  CFX_UnownedPtr<CPDF_Dictionary> m_pCharProcs;
+  CFX_UnownedPtr<CPDF_Dictionary> m_pPageResources;
+  CFX_UnownedPtr<CPDF_Dictionary> m_pFontResources;
   std::map<uint32_t, std::unique_ptr<CPDF_Type3Char>> m_CacheMap;
   // The depth char loading is in, to avoid recurive calling LoadChar().
   int m_CharLoadingDepth;
diff --git a/core/fpdfapi/page/cpdf_colorspace.h b/core/fpdfapi/page/cpdf_colorspace.h
index 76072d1..1502902 100644
--- a/core/fpdfapi/page/cpdf_colorspace.h
+++ b/core/fpdfapi/page/cpdf_colorspace.h
@@ -9,6 +9,7 @@
 
 #include <memory>
 
+#include "core/fxcrt/cfx_unowned_ptr.h"
 #include "core/fxcrt/fx_string.h"
 #include "core/fxcrt/fx_system.h"
 
@@ -61,7 +62,7 @@
                                   int image_height,
                                   bool bTransMask) const;
 
-  CPDF_Array*& GetArray() { return m_pArray; }
+  CPDF_Array* GetArray() const { return m_pArray.Get(); }
   virtual CPDF_ColorSpace* GetBaseCS() const;
 
   virtual void EnableStdConversion(bool bEnabled);
@@ -82,7 +83,7 @@
 
   int m_Family;
   uint32_t m_nComponents;
-  CPDF_Array* m_pArray;
+  CFX_UnownedPtr<CPDF_Array> m_pArray;
   uint32_t m_dwStdConversion;
 };
 
diff --git a/core/fpdfapi/page/cpdf_contentparser.cpp b/core/fpdfapi/page/cpdf_contentparser.cpp
index eeac3c1..4f4036f 100644
--- a/core/fpdfapi/page/cpdf_contentparser.cpp
+++ b/core/fpdfapi/page/cpdf_contentparser.cpp
@@ -98,8 +98,9 @@
 
   CPDF_Dictionary* pResources = pForm->m_pFormDict->GetDictFor("Resources");
   m_pParser = pdfium::MakeUnique<CPDF_StreamContentParser>(
-      pForm->m_pDocument, pForm->m_pPageResources, pForm->m_pResources,
-      pParentMatrix, pForm, pResources, &form_bbox, pGraphicStates, level);
+      pForm->m_pDocument.Get(), pForm->m_pPageResources.Get(),
+      pForm->m_pResources.Get(), pParentMatrix, pForm, pResources, &form_bbox,
+      pGraphicStates, level);
   m_pParser->GetCurStates()->m_CTM = form_matrix;
   m_pParser->GetCurStates()->m_ParentMatrix = form_matrix;
   if (ClipPath.HasRef()) {
@@ -114,7 +115,8 @@
     pState->SetSoftMask(nullptr);
   }
   m_nStreams = 0;
-  m_pSingleStream = pdfium::MakeRetain<CPDF_StreamAcc>(pForm->m_pFormStream);
+  m_pSingleStream =
+      pdfium::MakeRetain<CPDF_StreamAcc>(pForm->m_pFormStream.Get());
   m_pSingleStream->LoadAllData(false);
   m_pData = (uint8_t*)m_pSingleStream->GetData();
   m_Size = m_pSingleStream->GetSize();
@@ -167,8 +169,9 @@
     if (m_InternalStage == STAGE_PARSE) {
       if (!m_pParser) {
         m_pParser = pdfium::MakeUnique<CPDF_StreamContentParser>(
-            m_pObjectHolder->m_pDocument, m_pObjectHolder->m_pPageResources,
-            nullptr, nullptr, m_pObjectHolder, m_pObjectHolder->m_pResources,
+            m_pObjectHolder->m_pDocument.Get(),
+            m_pObjectHolder->m_pPageResources.Get(), nullptr, nullptr,
+            m_pObjectHolder.Get(), m_pObjectHolder->m_pResources.Get(),
             &m_pObjectHolder->m_BBox, nullptr, 0);
         m_pParser->GetCurStates()->m_ColorState.SetDefault();
       }
diff --git a/core/fpdfapi/page/cpdf_contentparser.h b/core/fpdfapi/page/cpdf_contentparser.h
index 402fd2f..58a9773 100644
--- a/core/fpdfapi/page/cpdf_contentparser.h
+++ b/core/fpdfapi/page/cpdf_contentparser.h
@@ -13,6 +13,7 @@
 #include "core/fpdfapi/page/cpdf_pageobjectholder.h"
 #include "core/fpdfapi/page/cpdf_streamcontentparser.h"
 #include "core/fpdfapi/parser/cpdf_stream_acc.h"
+#include "core/fxcrt/cfx_unowned_ptr.h"
 
 class CPDF_AllStates;
 class CPDF_Form;
@@ -45,9 +46,9 @@
 
   ParseStatus m_Status;
   InternalStage m_InternalStage;
-  CPDF_PageObjectHolder* m_pObjectHolder;
+  CFX_UnownedPtr<CPDF_PageObjectHolder> m_pObjectHolder;
   bool m_bForm;
-  CPDF_Type3Char* m_pType3Char;
+  CFX_UnownedPtr<CPDF_Type3Char> m_pType3Char;
   uint32_t m_nStreams;
   CFX_RetainPtr<CPDF_StreamAcc> m_pSingleStream;
   std::vector<CFX_RetainPtr<CPDF_StreamAcc>> m_StreamArray;
diff --git a/core/fpdfapi/page/cpdf_generalstate.cpp b/core/fpdfapi/page/cpdf_generalstate.cpp
index 513e5ff..e07a3ca 100644
--- a/core/fpdfapi/page/cpdf_generalstate.cpp
+++ b/core/fpdfapi/page/cpdf_generalstate.cpp
@@ -108,7 +108,7 @@
 
 CPDF_Object* CPDF_GeneralState::GetSoftMask() const {
   const StateData* pData = m_Ref.GetObject();
-  return pData ? pData->m_pSoftMask : nullptr;
+  return pData ? pData->m_pSoftMask.Get() : nullptr;
 }
 
 void CPDF_GeneralState::SetSoftMask(CPDF_Object* pObject) {
@@ -117,7 +117,7 @@
 
 CPDF_Object* CPDF_GeneralState::GetTR() const {
   const StateData* pData = m_Ref.GetObject();
-  return pData ? pData->m_pTR : nullptr;
+  return pData ? pData->m_pTR.Get() : nullptr;
 }
 
 void CPDF_GeneralState::SetTR(CPDF_Object* pObject) {
@@ -271,7 +271,7 @@
     CPDF_DocRenderData* pDocCache =
         that.m_pTransferFunc->m_pPDFDoc->GetRenderData();
     if (pDocCache)
-      m_pTransferFunc = pDocCache->GetTransferFunc(m_pTR);
+      m_pTransferFunc = pDocCache->GetTransferFunc(m_pTR.Get());
   }
 }
 
@@ -280,7 +280,7 @@
     CPDF_DocRenderData* pDocCache = m_pTransferFunc->m_pPDFDoc->GetRenderData();
     if (pDocCache) {
       m_pTransferFunc.Reset();  // Give up our reference first.
-      pDocCache->MaybePurgeTransferFunc(m_pTR);
+      pDocCache->MaybePurgeTransferFunc(m_pTR.Get());
     }
   }
 }
diff --git a/core/fpdfapi/page/cpdf_generalstate.h b/core/fpdfapi/page/cpdf_generalstate.h
index 9305785..4ec08ca 100644
--- a/core/fpdfapi/page/cpdf_generalstate.h
+++ b/core/fpdfapi/page/cpdf_generalstate.h
@@ -8,6 +8,7 @@
 #define CORE_FPDFAPI_PAGE_CPDF_GENERALSTATE_H_
 
 #include "core/fxcrt/cfx_shared_copy_on_write.h"
+#include "core/fxcrt/cfx_unowned_ptr.h"
 #include "core/fxcrt/fx_basic.h"
 #include "core/fxcrt/fx_coordinates.h"
 #include "core/fxge/fx_dib.h"
@@ -83,11 +84,11 @@
 
     CFX_ByteString m_BlendMode;
     int m_BlendType;
-    CPDF_Object* m_pSoftMask;
+    CFX_UnownedPtr<CPDF_Object> m_pSoftMask;
     CFX_Matrix m_SMaskMatrix;
     float m_StrokeAlpha;
     float m_FillAlpha;
-    CPDF_Object* m_pTR;
+    CFX_UnownedPtr<CPDF_Object> m_pTR;
     CFX_RetainPtr<CPDF_TransferFunc> m_pTransferFunc;
     CFX_Matrix m_Matrix;
     int m_RenderIntent;
@@ -97,9 +98,9 @@
     bool m_StrokeOP;
     bool m_FillOP;
     int m_OPMode;
-    CPDF_Object* m_pBG;
-    CPDF_Object* m_pUCR;
-    CPDF_Object* m_pHT;
+    CFX_UnownedPtr<CPDF_Object> m_pBG;
+    CFX_UnownedPtr<CPDF_Object> m_pUCR;
+    CFX_UnownedPtr<CPDF_Object> m_pHT;
     float m_Flatness;
     float m_Smoothness;
   };
diff --git a/core/fpdfapi/page/cpdf_page.cpp b/core/fpdfapi/page/cpdf_page.cpp
index 5ea575e..d528d3d 100644
--- a/core/fpdfapi/page/cpdf_page.cpp
+++ b/core/fpdfapi/page/cpdf_page.cpp
@@ -103,7 +103,7 @@
 }
 
 CPDF_Object* CPDF_Page::GetPageAttr(const CFX_ByteString& name) const {
-  CPDF_Dictionary* pPageDict = m_pFormDict;
+  CPDF_Dictionary* pPageDict = m_pFormDict.Get();
   std::set<CPDF_Dictionary*> visited;
   while (1) {
     visited.insert(pPageDict);
diff --git a/core/fpdfapi/page/cpdf_pageobjectholder.h b/core/fpdfapi/page/cpdf_pageobjectholder.h
index f5b60ff..c95ae50 100644
--- a/core/fpdfapi/page/cpdf_pageobjectholder.h
+++ b/core/fpdfapi/page/cpdf_pageobjectholder.h
@@ -11,6 +11,7 @@
 #include <vector>
 
 #include "core/fpdfapi/page/cpdf_pageobjectlist.h"
+#include "core/fxcrt/cfx_unowned_ptr.h"
 #include "core/fxcrt/fx_coordinates.h"
 #include "core/fxcrt/fx_system.h"
 
@@ -50,11 +51,11 @@
   void Transform(const CFX_Matrix& matrix);
   CFX_FloatRect CalcBoundingBox() const;
 
-  CPDF_Dictionary* m_pFormDict;
-  CPDF_Stream* m_pFormStream;
-  CPDF_Document* m_pDocument;
-  CPDF_Dictionary* m_pPageResources;
-  CPDF_Dictionary* m_pResources;
+  CFX_UnownedPtr<CPDF_Dictionary> m_pFormDict;
+  CFX_UnownedPtr<CPDF_Stream> m_pFormStream;
+  CFX_UnownedPtr<CPDF_Document> m_pDocument;
+  CFX_UnownedPtr<CPDF_Dictionary> m_pPageResources;
+  CFX_UnownedPtr<CPDF_Dictionary> m_pResources;
   CFX_FloatRect m_BBox;
   int m_Transparency;
 
diff --git a/core/fpdfapi/page/cpdf_shadingpattern.cpp b/core/fpdfapi/page/cpdf_shadingpattern.cpp
index dae5ec3..80eea63 100644
--- a/core/fpdfapi/page/cpdf_shadingpattern.cpp
+++ b/core/fpdfapi/page/cpdf_shadingpattern.cpp
@@ -93,7 +93,7 @@
   m_ShadingType = ToShadingType(pShadingDict->GetIntegerFor("ShadingType"));
 
   // We expect to have a stream if our shading type is a mesh.
-  if (IsMeshShading() && !ToStream(m_pShadingObj))
+  if (IsMeshShading() && !ToStream(m_pShadingObj.Get()))
     return false;
 
   return true;
diff --git a/core/fpdfapi/page/cpdf_shadingpattern.h b/core/fpdfapi/page/cpdf_shadingpattern.h
index 136153d..c2d55cc 100644
--- a/core/fpdfapi/page/cpdf_shadingpattern.h
+++ b/core/fpdfapi/page/cpdf_shadingpattern.h
@@ -12,6 +12,7 @@
 
 #include "core/fpdfapi/page/cpdf_countedobject.h"
 #include "core/fpdfapi/page/cpdf_pattern.h"
+#include "core/fxcrt/cfx_unowned_ptr.h"
 #include "core/fxcrt/fx_system.h"
 
 enum ShadingType {
@@ -53,7 +54,7 @@
 
   ShadingType GetShadingType() const { return m_ShadingType; }
   bool IsShadingObject() const { return m_bShadingObj; }
-  CPDF_Object* GetShadingObject() const { return m_pShadingObj; }
+  CPDF_Object* GetShadingObject() const { return m_pShadingObj.Get(); }
   CPDF_ColorSpace* GetCS() const { return m_pCS; }
   const std::vector<std::unique_ptr<CPDF_Function>>& GetFuncs() const {
     return m_pFunctions;
@@ -62,7 +63,7 @@
  private:
   ShadingType m_ShadingType;
   bool m_bShadingObj;
-  CPDF_Object* m_pShadingObj;
+  CFX_UnownedPtr<CPDF_Object> m_pShadingObj;
 
   // Still keep |m_pCS| as some CPDF_ColorSpace (name object) are not managed
   // as counted objects. Refer to CPDF_DocPageData::GetColorSpace.
diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
index 099ddf6..a5af1db 100644
--- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp
+++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp
@@ -633,7 +633,7 @@
     if (!key.IsEmpty()) {
       uint32_t dwObjNum = pObj ? pObj->GetObjNum() : 0;
       if (dwObjNum)
-        pDict->SetNewFor<CPDF_Reference>(key, m_pDocument, dwObjNum);
+        pDict->SetNewFor<CPDF_Reference>(key, m_pDocument.Get(), dwObjNum);
       else
         pDict->SetFor(key, std::move(pObj));
     }
@@ -653,7 +653,7 @@
   }
   pDict->SetNewFor<CPDF_Name>("Subtype", "Image");
   std::unique_ptr<CPDF_Stream> pStream =
-      m_pSyntax->ReadInlineStream(m_pDocument, std::move(pDict), pCSObj);
+      m_pSyntax->ReadInlineStream(m_pDocument.Get(), std::move(pDict), pCSObj);
   while (1) {
     CPDF_StreamParser::SyntaxType type = m_pSyntax->ParseNextElement();
     if (type == CPDF_StreamParser::EndOfData) {
@@ -771,7 +771,7 @@
 void CPDF_StreamContentParser::AddForm(CPDF_Stream* pStream) {
   auto pFormObj = pdfium::MakeUnique<CPDF_FormObject>();
   pFormObj->m_pForm = pdfium::MakeUnique<CPDF_Form>(
-      m_pDocument, m_pPageResources, pStream, m_pResources);
+      m_pDocument.Get(), m_pPageResources.Get(), pStream, m_pResources.Get());
   pFormObj->m_FormMatrix = m_pCurStates->m_CTM;
   pFormObj->m_FormMatrix.Concat(m_mtContentToUser);
   CPDF_AllStates status;
@@ -796,7 +796,7 @@
 
   auto pImageObj = pdfium::MakeUnique<CPDF_ImageObject>();
   pImageObj->SetImage(
-      pdfium::MakeRetain<CPDF_Image>(m_pDocument, std::move(pStream)));
+      pdfium::MakeRetain<CPDF_Image>(m_pDocument.Get(), std::move(pStream)));
   return AddImageObject(std::move(pImageObj));
 }
 
@@ -1176,12 +1176,12 @@
   CPDF_Dictionary* pFontDict = ToDictionary(FindResourceObj("Font", name));
   if (!pFontDict) {
     m_bResourceMissing = true;
-    return CPDF_Font::GetStockFont(m_pDocument, "Helvetica");
+    return CPDF_Font::GetStockFont(m_pDocument.Get(), "Helvetica");
   }
 
   CPDF_Font* pFont = m_pDocument->LoadFont(pFontDict);
   if (pFont && pFont->IsType3Font()) {
-    pFont->AsType3Font()->SetPageResources(m_pResources);
+    pFont->AsType3Font()->SetPageResources(m_pResources.Get());
     pFont->AsType3Font()->CheckType3FontMetrics();
   }
   return pFont;
@@ -1255,7 +1255,7 @@
   {
     auto pText = pdfium::MakeUnique<CPDF_TextObject>();
     m_pLastTextObject = pText.get();
-    SetGraphicStates(m_pLastTextObject, true, true, true);
+    SetGraphicStates(m_pLastTextObject.Get(), true, true, true);
     if (TextRenderingModeIsStrokeMode(text_mode)) {
       float* pCTM = pText->m_TextState.GetMutableCTM();
       pCTM[0] = m_pCurStates->m_CTM.a;
diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.h b/core/fpdfapi/page/cpdf_streamcontentparser.h
index 50eb0eb..5436f8f 100644
--- a/core/fpdfapi/page/cpdf_streamcontentparser.h
+++ b/core/fpdfapi/page/cpdf_streamcontentparser.h
@@ -43,7 +43,9 @@
   ~CPDF_StreamContentParser();
 
   uint32_t Parse(const uint8_t* pData, uint32_t dwSize, uint32_t max_cost);
-  CPDF_PageObjectHolder* GetPageObjectHolder() const { return m_pObjectHolder; }
+  CPDF_PageObjectHolder* GetPageObjectHolder() const {
+    return m_pObjectHolder.Get();
+  }
   CPDF_AllStates* GetCurStates() const { return m_pCurStates.get(); }
   bool IsColored() const { return m_bColored; }
   const float* GetType3Data() const { return m_Type3Data; }
@@ -186,11 +188,11 @@
   void Handle_NextLineShowText_Space();
   void Handle_Invalid();
 
-  CPDF_Document* const m_pDocument;
-  CPDF_Dictionary* m_pPageResources;
-  CPDF_Dictionary* m_pParentResources;
-  CPDF_Dictionary* m_pResources;
-  CPDF_PageObjectHolder* m_pObjectHolder;
+  CFX_UnownedPtr<CPDF_Document> const m_pDocument;
+  CFX_UnownedPtr<CPDF_Dictionary> m_pPageResources;
+  CFX_UnownedPtr<CPDF_Dictionary> m_pParentResources;
+  CFX_UnownedPtr<CPDF_Dictionary> m_pResources;
+  CFX_UnownedPtr<CPDF_PageObjectHolder> m_pObjectHolder;
   int m_Level;
   CFX_Matrix m_mtContentToUser;
   CFX_FloatRect m_BBox;
@@ -201,7 +203,7 @@
   std::unique_ptr<CPDF_AllStates> m_pCurStates;
   CPDF_ContentMark m_CurContentMark;
   std::vector<std::unique_ptr<CPDF_TextObject>> m_ClipTextList;
-  CPDF_TextObject* m_pLastTextObject;
+  CFX_UnownedPtr<CPDF_TextObject> m_pLastTextObject;
   float m_DefFontSize;
   std::vector<FX_PATHPOINT> m_PathPoints;
   float m_PathStartX;
diff --git a/core/fpdfapi/render/cpdf_imagerenderer.cpp b/core/fpdfapi/render/cpdf_imagerenderer.cpp
index 22eb32b..70371d8 100644
--- a/core/fpdfapi/render/cpdf_imagerenderer.cpp
+++ b/core/fpdfapi/render/cpdf_imagerenderer.cpp
@@ -138,11 +138,12 @@
   CPDF_Page* pPage = nullptr;
   if (auto* pPageCache = m_pRenderStatus->m_pContext->GetPageCache()) {
     pPage = pPageCache->GetPage();
-    pDocument = pPage->m_pDocument;
+    pDocument = pPage->m_pDocument.Get();
   } else {
     pDocument = m_pImageObject->GetImage()->GetDocument();
   }
-  CPDF_Dictionary* pPageResources = pPage ? pPage->m_pPageResources : nullptr;
+  CPDF_Dictionary* pPageResources =
+      pPage ? pPage->m_pPageResources.Get() : nullptr;
   CPDF_Object* pCSObj =
       m_pImageObject->GetImage()->GetStream()->GetDict()->GetDirectObjectFor(
           "ColorSpace");
diff --git a/core/fpdfapi/render/cpdf_pagerendercache.cpp b/core/fpdfapi/render/cpdf_pagerendercache.cpp
index fa23355..cc1f267 100644
--- a/core/fpdfapi/render/cpdf_pagerendercache.cpp
+++ b/core/fpdfapi/render/cpdf_pagerendercache.cpp
@@ -88,10 +88,10 @@
     m_pCurImageCacheEntry = it->second;
   } else {
     m_pCurImageCacheEntry =
-        new CPDF_ImageCacheEntry(m_pPage->m_pDocument, pStream);
+        new CPDF_ImageCacheEntry(m_pPage->m_pDocument.Get(), pStream);
   }
   int ret = m_pCurImageCacheEntry->StartGetCachedBitmap(
-      pRenderStatus->m_pFormResource, m_pPage->m_pPageResources, bStdCS,
+      pRenderStatus->m_pFormResource, m_pPage->m_pPageResources.Get(), bStdCS,
       GroupFamily, bLoadMask, pRenderStatus);
   if (ret == 2)
     return true;
@@ -129,7 +129,7 @@
     if (!pBitmap)
       return;
 
-    pEntry = new CPDF_ImageCacheEntry(m_pPage->m_pDocument, pStream);
+    pEntry = new CPDF_ImageCacheEntry(m_pPage->m_pDocument.Get(), pStream);
     m_ImageCache[pStream] = pEntry;
   } else {
     pEntry = it->second;
diff --git a/core/fpdfapi/render/cpdf_rendercontext.cpp b/core/fpdfapi/render/cpdf_rendercontext.cpp
index c8626af..2cd59ca 100644
--- a/core/fpdfapi/render/cpdf_rendercontext.cpp
+++ b/core/fpdfapi/render/cpdf_rendercontext.cpp
@@ -20,8 +20,8 @@
 #include "core/fxge/fx_dib.h"
 
 CPDF_RenderContext::CPDF_RenderContext(CPDF_Page* pPage)
-    : m_pDocument(pPage->m_pDocument),
-      m_pPageResources(pPage->m_pPageResources),
+    : m_pDocument(pPage->m_pDocument.Get()),
+      m_pPageResources(pPage->m_pPageResources.Get()),
       m_pPageCache(pPage->GetRenderCache()) {}
 
 CPDF_RenderContext::CPDF_RenderContext(CPDF_Document* pDoc,
diff --git a/core/fpdfapi/render/cpdf_renderstatus.cpp b/core/fpdfapi/render/cpdf_renderstatus.cpp
index d485d1d..e6b01ef 100644
--- a/core/fpdfapi/render/cpdf_renderstatus.cpp
+++ b/core/fpdfapi/render/cpdf_renderstatus.cpp
@@ -1487,11 +1487,12 @@
     CPDF_Page* pPage = nullptr;
     if (m_pContext->GetPageCache()) {
       pPage = m_pContext->GetPageCache()->GetPage();
-      pDocument = pPage->m_pDocument;
+      pDocument = pPage->m_pDocument.Get();
     } else {
       pDocument = pPageObj->AsImage()->GetImage()->GetDocument();
     }
-    CPDF_Dictionary* pPageResources = pPage ? pPage->m_pPageResources : nullptr;
+    CPDF_Dictionary* pPageResources =
+        pPage ? pPage->m_pPageResources.Get() : nullptr;
     CPDF_Object* pCSObj = pPageObj->AsImage()
                               ->GetImage()
                               ->GetStream()
diff --git a/core/fpdfdoc/cpdf_annot.cpp b/core/fpdfdoc/cpdf_annot.cpp
index a3ee70a..146c863 100644
--- a/core/fpdfdoc/cpdf_annot.cpp
+++ b/core/fpdfdoc/cpdf_annot.cpp
@@ -204,8 +204,8 @@
   if (it != m_APMap.end())
     return it->second.get();
 
-  auto pNewForm =
-      pdfium::MakeUnique<CPDF_Form>(m_pDocument, pPage->m_pResources, pStream);
+  auto pNewForm = pdfium::MakeUnique<CPDF_Form>(
+      m_pDocument, pPage->m_pResources.Get(), pStream);
   pNewForm->ParseContent(nullptr, nullptr, nullptr);
 
   CPDF_Form* pResult = pNewForm.get();
diff --git a/core/fpdfdoc/cpdf_annotlist.cpp b/core/fpdfdoc/cpdf_annotlist.cpp
index ed1b60c..1427c85 100644
--- a/core/fpdfdoc/cpdf_annotlist.cpp
+++ b/core/fpdfdoc/cpdf_annotlist.cpp
@@ -62,7 +62,7 @@
 }  // namespace
 
 CPDF_AnnotList::CPDF_AnnotList(CPDF_Page* pPage)
-    : m_pDocument(pPage->m_pDocument) {
+    : m_pDocument(pPage->m_pDocument.Get()) {
   if (!pPage->m_pFormDict)
     return;
 
diff --git a/core/fpdfdoc/cpdf_interform.cpp b/core/fpdfdoc/cpdf_interform.cpp
index 88787d2..21e2244 100644
--- a/core/fpdfdoc/cpdf_interform.cpp
+++ b/core/fpdfdoc/cpdf_interform.cpp
@@ -1048,7 +1048,7 @@
 }
 
 void CPDF_InterForm::FixPageFields(const CPDF_Page* pPage) {
-  CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
+  CPDF_Dictionary* pPageDict = pPage->m_pFormDict.Get();
   if (!pPageDict)
     return;
 
diff --git a/fpdfsdk/cpdfsdk_pageview.cpp b/fpdfsdk/cpdfsdk_pageview.cpp
index 2dd8e30..982eed1 100644
--- a/fpdfsdk/cpdfsdk_pageview.cpp
+++ b/fpdfsdk/cpdfsdk_pageview.cpp
@@ -206,7 +206,7 @@
 #ifdef PDF_ENABLE_XFA
     return m_page->GetContext()->GetPDFDoc();
 #else   // PDF_ENABLE_XFA
-    return m_page->m_pDocument;
+    return m_page->m_pDocument.Get();
 #endif  // PDF_ENABLE_XFA
   }
   return nullptr;
@@ -484,7 +484,7 @@
 }
 
 int CPDFSDK_PageView::GetPageIndexForStaticPDF() const {
-  CPDF_Dictionary* pDict = GetPDFPage()->m_pFormDict;
+  CPDF_Dictionary* pDict = GetPDFPage()->m_pFormDict.Get();
   CPDF_Document* pDoc = m_pFormFillEnv->GetPDFDocument();
   return (pDoc && pDict) ? pDoc->GetPageIndex(pDict->GetObjNum()) : -1;
 }
diff --git a/fpdfsdk/cpdfsdk_widgethandler.cpp b/fpdfsdk/cpdfsdk_widgethandler.cpp
index cab210d..1e63f5e 100644
--- a/fpdfsdk/cpdfsdk_widgethandler.cpp
+++ b/fpdfsdk/cpdfsdk_widgethandler.cpp
@@ -46,8 +46,7 @@
     return true;
 
   CPDF_Page* pPage = pWidget->GetPDFPage();
-  CPDF_Document* pDocument = pPage->m_pDocument;
-  uint32_t dwPermissions = pDocument->GetUserPermissions();
+  uint32_t dwPermissions = pPage->m_pDocument->GetUserPermissions();
   return (dwPermissions & FPDFPERM_FILL_FORM) ||
          (dwPermissions & FPDFPERM_ANNOT_FORM);
 }
diff --git a/fpdfsdk/formfiller/cba_fontmap.cpp b/fpdfsdk/formfiller/cba_fontmap.cpp
index 30f25d4..2733174 100644
--- a/fpdfsdk/formfiller/cba_fontmap.cpp
+++ b/fpdfsdk/formfiller/cba_fontmap.cpp
@@ -28,7 +28,7 @@
       m_sAPType("N") {
   CPDF_Page* pPage = pAnnot->GetPDFPage();
 
-  m_pDocument = pPage->m_pDocument;
+  m_pDocument = pPage->m_pDocument.Get();
   m_pAnnotDict = pAnnot->GetPDFAnnot()->GetAnnotDict();
   Initialize();
 }
diff --git a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
index bd08c67..5a35852 100644
--- a/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
+++ b/fpdfsdk/formfiller/cffl_interactiveformfiller.cpp
@@ -461,8 +461,7 @@
     return false;
 
   CPDF_Page* pPage = pWidget->GetPDFPage();
-  CPDF_Document* pDocument = pPage->m_pDocument;
-  uint32_t dwPermissions = pDocument->GetUserPermissions();
+  uint32_t dwPermissions = pPage->m_pDocument->GetUserPermissions();
   return (dwPermissions & FPDFPERM_FILL_FORM) ||
          (dwPermissions & FPDFPERM_ANNOT_FORM) ||
          (dwPermissions & FPDFPERM_MODIFY);
diff --git a/fpdfsdk/fpdf_flatten.cpp b/fpdfsdk/fpdf_flatten.cpp
index 31b838c..0477d6f 100644
--- a/fpdfsdk/fpdf_flatten.cpp
+++ b/fpdfsdk/fpdf_flatten.cpp
@@ -246,8 +246,8 @@
   if (!page)
     return FLATTEN_FAIL;
 
-  CPDF_Document* pDocument = pPage->m_pDocument;
-  CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
+  CPDF_Document* pDocument = pPage->m_pDocument.Get();
+  CPDF_Dictionary* pPageDict = pPage->m_pFormDict.Get();
   if (!pDocument || !pPageDict)
     return FLATTEN_FAIL;
 
diff --git a/fpdfsdk/fpdf_structtree.cpp b/fpdfsdk/fpdf_structtree.cpp
index 0c1cc0f..bd6641f 100644
--- a/fpdfsdk/fpdf_structtree.cpp
+++ b/fpdfsdk/fpdf_structtree.cpp
@@ -41,7 +41,8 @@
   CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return nullptr;
-  return CPDF_StructTree::LoadPage(pPage->m_pDocument, pPage->m_pFormDict)
+  return CPDF_StructTree::LoadPage(pPage->m_pDocument.Get(),
+                                   pPage->m_pFormDict.Get())
       .release();
 }
 
diff --git a/fpdfsdk/fpdf_transformpage.cpp b/fpdfsdk/fpdf_transformpage.cpp
index cc5239b..9cf5a0b 100644
--- a/fpdfsdk/fpdf_transformpage.cpp
+++ b/fpdfsdk/fpdf_transformpage.cpp
@@ -119,7 +119,7 @@
                  matrix->d, matrix->e, matrix->f);
   textBuf << bsMatix;
 
-  CPDF_Dictionary* pPageDic = pPage->m_pFormDict;
+  CPDF_Dictionary* pPageDic = pPage->m_pFormDict.Get();
   CPDF_Object* pContentObj =
       pPageDic ? pPageDic->GetObjectFor("Contents") : nullptr;
   if (!pContentObj)
@@ -127,7 +127,7 @@
   if (!pContentObj)
     return false;
 
-  CPDF_Document* pDoc = pPage->m_pDocument;
+  CPDF_Document* pDoc = pPage->m_pDocument.Get();
   if (!pDoc)
     return false;
 
@@ -276,7 +276,7 @@
   if (!pPage)
     return;
 
-  CPDF_Dictionary* pPageDic = pPage->m_pFormDict;
+  CPDF_Dictionary* pPageDic = pPage->m_pFormDict.Get();
   CPDF_Object* pContentObj =
       pPageDic ? pPageDic->GetObjectFor("Contents") : nullptr;
   if (!pContentObj)
@@ -301,7 +301,7 @@
         strClip << "W* n\n";
     }
   }
-  CPDF_Document* pDoc = pPage->m_pDocument;
+  CPDF_Document* pDoc = pPage->m_pDocument.Get();
   if (!pDoc)
     return;
 
diff --git a/fpdfsdk/fpdfdoc.cpp b/fpdfsdk/fpdfdoc.cpp
index 70a27a5..1c445e6 100644
--- a/fpdfsdk/fpdfdoc.cpp
+++ b/fpdfsdk/fpdfdoc.cpp
@@ -53,7 +53,7 @@
   if (!page)
     return nullptr;
 
-  CPDF_Document* pDoc = page->m_pDocument;
+  CPDF_Document* pDoc = page->m_pDocument.Get();
   std::unique_ptr<CPDF_LinkList>* pHolder = pDoc->LinksContext();
   if (!pHolder->get())
     *pHolder = pdfium::MakeUnique<CPDF_LinkList>();
diff --git a/fpdfsdk/fpdfeditpage.cpp b/fpdfsdk/fpdfeditpage.cpp
index 431adca..95ae666 100644
--- a/fpdfsdk/fpdfeditpage.cpp
+++ b/fpdfsdk/fpdfeditpage.cpp
@@ -300,7 +300,6 @@
   if (!IsPageObject(pPage))
     return;
 
-  CPDF_Dictionary* pDict = pPage->m_pFormDict;
   rotate %= 4;
-  pDict->SetNewFor<CPDF_Number>("Rotate", rotate * 90);
+  pPage->m_pFormDict->SetNewFor<CPDF_Number>("Rotate", rotate * 90);
 }
diff --git a/fpdfsdk/fpdfformfill.cpp b/fpdfsdk/fpdfformfill.cpp
index 121e1a9..fb12967 100644
--- a/fpdfsdk/fpdfformfill.cpp
+++ b/fpdfsdk/fpdfformfill.cpp
@@ -141,7 +141,7 @@
       pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options, clip);
 #else   // PDF_ENABLE_XFA
     options.m_pOCContext = pdfium::MakeRetain<CPDF_OCContext>(
-        pPage->m_pDocument, CPDF_OCContext::View);
+        pPage->m_pDocument.Get(), CPDF_OCContext::View);
     if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, pPage))
       pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options);
 #endif  // PDF_ENABLE_XFA
@@ -162,7 +162,7 @@
     return -1;
   CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (pPage) {
-    CPDF_InterForm interform(pPage->m_pDocument);
+    CPDF_InterForm interform(pPage->m_pDocument.Get());
     CPDF_FormControl* pFormCtrl = interform.GetControlAtPoint(
         pPage,
         CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)),
@@ -232,7 +232,7 @@
   CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return -1;
-  CPDF_InterForm interform(pPage->m_pDocument);
+  CPDF_InterForm interform(pPage->m_pDocument.Get());
   int z_order = -1;
   (void)interform.GetControlAtPoint(
       pPage, CFX_PointF(static_cast<float>(page_x), static_cast<float>(page_y)),
@@ -741,7 +741,7 @@
     return;
 
   CPDFSDK_ActionHandler* pActionHandler = pFormFillEnv->GetActionHander();
-  CPDF_Dictionary* pPageDict = pPDFPage->m_pFormDict;
+  CPDF_Dictionary* pPageDict = pPDFPage->m_pFormDict.Get();
   CPDF_AAction aa(pPageDict->GetDictFor("AA"));
   if (FPDFPAGE_AACTION_OPEN == aaType) {
     if (aa.ActionExist(CPDF_AAction::OpenPage)) {
diff --git a/fpdfsdk/fpdftext.cpp b/fpdfsdk/fpdftext.cpp
index 5abd232..916233c 100644
--- a/fpdfsdk/fpdftext.cpp
+++ b/fpdfsdk/fpdftext.cpp
@@ -53,7 +53,7 @@
   CPDFXFA_Context* pContext = pPage->GetContext();
   CPDF_ViewerPreferences viewRef(pContext->GetPDFDoc());
 #else  // PDF_ENABLE_XFA
-  CPDF_ViewerPreferences viewRef(pPDFPage->m_pDocument);
+  CPDF_ViewerPreferences viewRef(pPDFPage->m_pDocument.Get());
 #endif  // PDF_ENABLE_XFA
 
   CPDF_TextPage* textpage = new CPDF_TextPage(
diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp
index 76913ce..f20e8ab 100644
--- a/fpdfsdk/fpdfview.cpp
+++ b/fpdfsdk/fpdfview.cpp
@@ -98,11 +98,10 @@
       (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
   pContext->m_pOptions->m_AddFlags = flags >> 8;
   pContext->m_pOptions->m_pOCContext =
-      pdfium::MakeRetain<CPDF_OCContext>(pPage->m_pDocument, usage);
+      pdfium::MakeRetain<CPDF_OCContext>(pPage->m_pDocument.Get(), usage);
 
   pContext->m_pDevice->SaveState();
   pContext->m_pDevice->SetClip_Rect(clipping_rect);
-
   pContext->m_pContext = pdfium::MakeUnique<CPDF_RenderContext>(pPage);
   pContext->m_pContext->AppendLayer(pPage, &matrix);