Avoid vending dictionary pointers from CPDF_StructElement.

Introduce operations that operate on behalf of the callers.

Change-Id: I92ce6b0e5a2c744249178bc18df8e35468661f2a
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/98792
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfdoc/cpdf_structelement.cpp b/core/fpdfdoc/cpdf_structelement.cpp
index 211ec1c..34370f3 100644
--- a/core/fpdfdoc/cpdf_structelement.cpp
+++ b/core/fpdfdoc/cpdf_structelement.cpp
@@ -18,21 +18,6 @@
 #include "core/fpdfdoc/cpdf_structtree.h"
 #include "third_party/base/check.h"
 
-namespace {
-
-ByteString GetStructElementType(const CPDF_StructTree* pTree,
-                                const CPDF_Dictionary* pDict) {
-  ByteString type = pDict->GetNameFor("S");
-  if (pTree->GetRoleMap()) {
-    ByteString mapped = pTree->GetRoleMap()->GetNameFor(type);
-    if (!mapped.IsEmpty())
-      type = std::move(mapped);
-  }
-  return type;
-}
-
-}  // namespace
-
 CPDF_StructElement::Kid::Kid() = default;
 
 CPDF_StructElement::Kid::Kid(const Kid& that) = default;
@@ -43,7 +28,7 @@
                                        RetainPtr<const CPDF_Dictionary> pDict)
     : m_pTree(pTree),
       m_pDict(std::move(pDict)),
-      m_Type(GetStructElementType(m_pTree.Get(), m_pDict.Get())) {
+      m_Type(m_pTree->GetRoleMapNameFor(m_pDict->GetNameFor("S"))) {
   LoadKids(m_pDict);
 }
 
@@ -120,7 +105,7 @@
     return;
 
   if (pKidObj->IsNumber()) {
-    if (m_pTree->GetPage()->GetObjNum() != PageObjNum)
+    if (m_pTree->GetPageObjNum() != PageObjNum)
       return;
 
     pKid->m_Type = Kid::kPageContent;
@@ -139,7 +124,7 @@
   }
   ByteString type = pKidDict->GetNameFor("Type");
   if ((type == "MCR" || type == "OBJR") &&
-      m_pTree->GetPage()->GetObjNum() != PageObjNum) {
+      m_pTree->GetPageObjNum() != PageObjNum) {
     return;
   }
 
diff --git a/core/fpdfdoc/cpdf_structtree.cpp b/core/fpdfdoc/cpdf_structtree.cpp
index acfa405..10298f9 100644
--- a/core/fpdfdoc/cpdf_structtree.cpp
+++ b/core/fpdfdoc/cpdf_structtree.cpp
@@ -45,6 +45,15 @@
 
 CPDF_StructTree::~CPDF_StructTree() = default;
 
+ByteString CPDF_StructTree::GetRoleMapNameFor(const ByteString& type) const {
+  if (m_pRoleMap) {
+    ByteString mapped = m_pRoleMap->GetNameFor(type);
+    if (!mapped.IsEmpty())
+      return mapped;
+  }
+  return type;
+}
+
 void CPDF_StructTree::LoadPageTree(const CPDF_Dictionary* pPageDict) {
   m_pPage.Reset(pPageDict);
   if (!m_pTreeRoot)
diff --git a/core/fpdfdoc/cpdf_structtree.h b/core/fpdfdoc/cpdf_structtree.h
index f99205f..aebbc99 100644
--- a/core/fpdfdoc/cpdf_structtree.h
+++ b/core/fpdfdoc/cpdf_structtree.h
@@ -12,9 +12,10 @@
 #include <memory>
 #include <vector>
 
+#include "core/fpdfapi/parser/cpdf_dictionary.h"
+#include "core/fxcrt/bytestring.h"
 #include "core/fxcrt/retain_ptr.h"
 
-class CPDF_Dictionary;
 class CPDF_Document;
 class CPDF_StructElement;
 
@@ -29,9 +30,8 @@
 
   size_t CountTopElements() const { return m_Kids.size(); }
   CPDF_StructElement* GetTopElement(size_t i) const { return m_Kids[i].Get(); }
-  const CPDF_Dictionary* GetRoleMap() const { return m_pRoleMap.Get(); }
-  const CPDF_Dictionary* GetPage() const { return m_pPage.Get(); }
-  const CPDF_Dictionary* GetTreeRoot() const { return m_pTreeRoot.Get(); }
+  uint32_t GetPageObjNum() const { return m_pPage->GetObjNum(); }
+  ByteString GetRoleMapNameFor(const ByteString& type) const;
 
  private:
   using StructElementMap = std::map<RetainPtr<const CPDF_Dictionary>,