Cleanup CPDF_StructElement.

Pre-cursor to properly ref-counting it.

Rename to match the CPDF_/IPDF_ conventions.
Move CPDF_ structure out of IPDF-defining headers.
Break friendships.

Review-Url: https://codereview.chromium.org/2640923006
diff --git a/core/fpdfdoc/doc_tagged.cpp b/core/fpdfdoc/doc_tagged.cpp
index 39feaee..5feb205 100644
--- a/core/fpdfdoc/doc_tagged.cpp
+++ b/core/fpdfdoc/doc_tagged.cpp
@@ -39,27 +39,27 @@
   if (!IsTagged(pDoc))
     return nullptr;
 
-  auto pTree = pdfium::MakeUnique<CPDF_StructTreeImpl>(pDoc);
+  auto pTree = pdfium::MakeUnique<CPDF_StructTree>(pDoc);
   pTree->LoadPageTree(pPageDict);
   return std::move(pTree);
 }
 
-CPDF_StructTreeImpl::CPDF_StructTreeImpl(const CPDF_Document* pDoc)
+CPDF_StructTree::CPDF_StructTree(const CPDF_Document* pDoc)
     : m_pTreeRoot(pDoc->GetRoot()->GetDictFor("StructTreeRoot")),
       m_pRoleMap(m_pTreeRoot ? m_pTreeRoot->GetDictFor("RoleMap") : nullptr),
       m_pPage(nullptr) {}
 
-CPDF_StructTreeImpl::~CPDF_StructTreeImpl() {}
+CPDF_StructTree::~CPDF_StructTree() {}
 
-int CPDF_StructTreeImpl::CountTopElements() const {
+int CPDF_StructTree::CountTopElements() const {
   return pdfium::CollectionSize<int>(m_Kids);
 }
 
-IPDF_StructElement* CPDF_StructTreeImpl::GetTopElement(int i) const {
+IPDF_StructElement* CPDF_StructTree::GetTopElement(int i) const {
   return m_Kids[i].Get();
 }
 
-void CPDF_StructTreeImpl::LoadPageTree(const CPDF_Dictionary* pPageDict) {
+void CPDF_StructTree::LoadPageTree(const CPDF_Dictionary* pPageDict) {
   m_pPage = pPageDict;
   if (!m_pTreeRoot)
     return;
@@ -91,16 +91,16 @@
   if (!pParentArray)
     return;
 
-  std::map<CPDF_Dictionary*, CPDF_StructElementImpl*> element_map;
+  std::map<CPDF_Dictionary*, CPDF_StructElement*> element_map;
   for (size_t i = 0; i < pParentArray->GetCount(); i++) {
     if (CPDF_Dictionary* pParent = pParentArray->GetDictAt(i))
       AddPageNode(pParent, element_map);
   }
 }
 
-CPDF_StructElementImpl* CPDF_StructTreeImpl::AddPageNode(
+CPDF_StructElement* CPDF_StructTree::AddPageNode(
     CPDF_Dictionary* pDict,
-    std::map<CPDF_Dictionary*, CPDF_StructElementImpl*>& map,
+    std::map<CPDF_Dictionary*, CPDF_StructElement*>& map,
     int nLevel) {
   if (nLevel > nMaxRecursion)
     return nullptr;
@@ -109,8 +109,7 @@
   if (it != map.end())
     return it->second;
 
-  CPDF_StructElementImpl* pElement =
-      new CPDF_StructElementImpl(this, nullptr, pDict);
+  CPDF_StructElement* pElement = new CPDF_StructElement(this, nullptr, pDict);
   map[pDict] = pElement;
   CPDF_Dictionary* pParent = pDict->GetDictFor("P");
   if (!pParent || pParent->GetStringFor("Type") == "StructTreeRoot") {
@@ -119,10 +118,9 @@
       map.erase(pDict);
     }
   } else {
-    CPDF_StructElementImpl* pParentElement =
-        AddPageNode(pParent, map, nLevel + 1);
+    CPDF_StructElement* pParentElement = AddPageNode(pParent, map, nLevel + 1);
     bool bSave = false;
-    for (CPDF_StructKid& kid : pParentElement->m_Kids) {
+    for (CPDF_StructKid& kid : *pParentElement->GetKids()) {
       if (kid.m_Type != CPDF_StructKid::Element)
         continue;
       if (kid.m_Element.m_pDict != pDict)
@@ -137,8 +135,8 @@
   }
   return pElement;
 }
-bool CPDF_StructTreeImpl::AddTopLevelNode(CPDF_Dictionary* pDict,
-                                          CPDF_StructElementImpl* pElement) {
+bool CPDF_StructTree::AddTopLevelNode(CPDF_Dictionary* pDict,
+                                      CPDF_StructElement* pElement) {
   CPDF_Object* pObj = m_pTreeRoot->GetDirectObjectFor("K");
   if (!pObj)
     return false;
@@ -163,9 +161,9 @@
   return true;
 }
 
-CPDF_StructElementImpl::CPDF_StructElementImpl(CPDF_StructTreeImpl* pTree,
-                                               CPDF_StructElementImpl* pParent,
-                                               CPDF_Dictionary* pDict)
+CPDF_StructElement::CPDF_StructElement(CPDF_StructTree* pTree,
+                                       CPDF_StructElement* pParent,
+                                       CPDF_Dictionary* pDict)
     : m_RefCount(0),
       m_pTree(pTree),
       m_pParent(pParent),
@@ -179,47 +177,49 @@
   LoadKids(pDict);
 }
 
-IPDF_StructTree* CPDF_StructElementImpl::GetTree() const {
+IPDF_StructTree* CPDF_StructElement::GetTree() const {
   return m_pTree;
 }
 
-const CFX_ByteString& CPDF_StructElementImpl::GetType() const {
+const CFX_ByteString& CPDF_StructElement::GetType() const {
   return m_Type;
 }
 
-IPDF_StructElement* CPDF_StructElementImpl::GetParent() const {
+IPDF_StructElement* CPDF_StructElement::GetParent() const {
   return m_pParent;
 }
 
-CPDF_Dictionary* CPDF_StructElementImpl::GetDict() const {
+CPDF_Dictionary* CPDF_StructElement::GetDict() const {
   return m_pDict;
 }
 
-int CPDF_StructElementImpl::CountKids() const {
+int CPDF_StructElement::CountKids() const {
   return pdfium::CollectionSize<int>(m_Kids);
 }
 
-const CPDF_StructKid& CPDF_StructElementImpl::GetKid(int index) const {
-  return m_Kids[index];
+IPDF_StructElement* CPDF_StructElement::GetKidIfElement(int index) const {
+  return m_Kids[index].m_Type == CPDF_StructKid::Element
+             ? m_Kids[index].m_Element.m_pElement
+             : nullptr;
 }
 
-CPDF_StructElementImpl::~CPDF_StructElementImpl() {
+CPDF_StructElement::~CPDF_StructElement() {
   for (CPDF_StructKid& kid : m_Kids) {
     if (kid.m_Type == CPDF_StructKid::Element && kid.m_Element.m_pElement)
-      static_cast<CPDF_StructElementImpl*>(kid.m_Element.m_pElement)->Release();
+      static_cast<CPDF_StructElement*>(kid.m_Element.m_pElement)->Release();
   }
 }
 
-CPDF_StructElementImpl* CPDF_StructElementImpl::Retain() {
+CPDF_StructElement* CPDF_StructElement::Retain() {
   m_RefCount++;
   return this;
 }
-void CPDF_StructElementImpl::Release() {
+void CPDF_StructElement::Release() {
   if (--m_RefCount < 1) {
     delete this;
   }
 }
-void CPDF_StructElementImpl::LoadKids(CPDF_Dictionary* pDict) {
+void CPDF_StructElement::LoadKids(CPDF_Dictionary* pDict) {
   CPDF_Object* pObj = pDict->GetObjectFor("Pg");
   uint32_t PageObjNum = 0;
   if (CPDF_Reference* pRef = ToReference(pObj))
@@ -241,9 +241,9 @@
     LoadKid(PageObjNum, pKids, &m_Kids[0]);
   }
 }
-void CPDF_StructElementImpl::LoadKid(uint32_t PageObjNum,
-                                     CPDF_Object* pKidObj,
-                                     CPDF_StructKid* pKid) {
+void CPDF_StructElement::LoadKid(uint32_t PageObjNum,
+                                 CPDF_Object* pKidObj,
+                                 CPDF_StructKid* pKid) {
   pKid->m_Type = CPDF_StructKid::Invalid;
   if (!pKidObj)
     return;
@@ -294,7 +294,7 @@
     pKid->m_Element.m_pDict = pKidDict;
     if (!m_pTree->m_pPage) {
       pKid->m_Element.m_pElement =
-          new CPDF_StructElementImpl(m_pTree, this, pKidDict);
+          new CPDF_StructElement(m_pTree, this, pKidDict);
     } else {
       pKid->m_Element.m_pElement = nullptr;
     }
@@ -325,10 +325,10 @@
     return pDict;
   return nullptr;
 }
-CPDF_Object* CPDF_StructElementImpl::GetAttr(const CFX_ByteStringC& owner,
-                                             const CFX_ByteStringC& name,
-                                             bool bInheritable,
-                                             FX_FLOAT fLevel) {
+CPDF_Object* CPDF_StructElement::GetAttr(const CFX_ByteStringC& owner,
+                                         const CFX_ByteStringC& name,
+                                         bool bInheritable,
+                                         FX_FLOAT fLevel) {
   if (fLevel > nMaxRecursion) {
     return nullptr;
   }
@@ -375,10 +375,10 @@
     return pClassDict->GetDirectObjectFor(CFX_ByteString(name));
   return nullptr;
 }
-CPDF_Object* CPDF_StructElementImpl::GetAttr(const CFX_ByteStringC& owner,
-                                             const CFX_ByteStringC& name,
-                                             bool bInheritable,
-                                             int subindex) {
+CPDF_Object* CPDF_StructElement::GetAttr(const CFX_ByteStringC& owner,
+                                         const CFX_ByteStringC& name,
+                                         bool bInheritable,
+                                         int subindex) {
   CPDF_Object* pAttr = GetAttr(owner, name, bInheritable);
   CPDF_Array* pArray = ToArray(pAttr);
   if (!pArray || subindex == -1)
@@ -388,23 +388,22 @@
     return pAttr;
   return pArray->GetDirectObjectAt(subindex);
 }
-CFX_ByteString CPDF_StructElementImpl::GetName(
-    const CFX_ByteStringC& owner,
-    const CFX_ByteStringC& name,
-    const CFX_ByteStringC& default_value,
-    bool bInheritable,
-    int subindex) {
+CFX_ByteString CPDF_StructElement::GetName(const CFX_ByteStringC& owner,
+                                           const CFX_ByteStringC& name,
+                                           const CFX_ByteStringC& default_value,
+                                           bool bInheritable,
+                                           int subindex) {
   CPDF_Object* pAttr = GetAttr(owner, name, bInheritable, subindex);
   if (ToName(pAttr))
     return pAttr->GetString();
   return CFX_ByteString(default_value);
 }
 
-FX_ARGB CPDF_StructElementImpl::GetColor(const CFX_ByteStringC& owner,
-                                         const CFX_ByteStringC& name,
-                                         FX_ARGB default_value,
-                                         bool bInheritable,
-                                         int subindex) {
+FX_ARGB CPDF_StructElement::GetColor(const CFX_ByteStringC& owner,
+                                     const CFX_ByteStringC& name,
+                                     FX_ARGB default_value,
+                                     bool bInheritable,
+                                     int subindex) {
   CPDF_Array* pArray = ToArray(GetAttr(owner, name, bInheritable, subindex));
   if (!pArray)
     return default_value;
@@ -412,19 +411,19 @@
          ((int)(pArray->GetNumberAt(1) * 255) << 8) |
          (int)(pArray->GetNumberAt(2) * 255);
 }
-FX_FLOAT CPDF_StructElementImpl::GetNumber(const CFX_ByteStringC& owner,
-                                           const CFX_ByteStringC& name,
-                                           FX_FLOAT default_value,
-                                           bool bInheritable,
-                                           int subindex) {
+FX_FLOAT CPDF_StructElement::GetNumber(const CFX_ByteStringC& owner,
+                                       const CFX_ByteStringC& name,
+                                       FX_FLOAT default_value,
+                                       bool bInheritable,
+                                       int subindex) {
   CPDF_Object* pAttr = GetAttr(owner, name, bInheritable, subindex);
   return ToNumber(pAttr) ? pAttr->GetNumber() : default_value;
 }
-int CPDF_StructElementImpl::GetInteger(const CFX_ByteStringC& owner,
-                                       const CFX_ByteStringC& name,
-                                       int default_value,
-                                       bool bInheritable,
-                                       int subindex) {
+int CPDF_StructElement::GetInteger(const CFX_ByteStringC& owner,
+                                   const CFX_ByteStringC& name,
+                                   int default_value,
+                                   bool bInheritable,
+                                   int subindex) {
   CPDF_Object* pAttr = GetAttr(owner, name, bInheritable, subindex);
   return ToNumber(pAttr) ? pAttr->GetInteger() : default_value;
 }
diff --git a/core/fpdfdoc/fpdf_tagged.h b/core/fpdfdoc/fpdf_tagged.h
index aa697ca..3a1e223 100644
--- a/core/fpdfdoc/fpdf_tagged.h
+++ b/core/fpdfdoc/fpdf_tagged.h
@@ -27,30 +27,6 @@
   virtual IPDF_StructElement* GetTopElement(int i) const = 0;
 };
 
-struct CPDF_StructKid {
-  enum { Invalid, Element, PageContent, StreamContent, Object } m_Type;
-
-  union {
-    struct {
-      IPDF_StructElement* m_pElement;
-      CPDF_Dictionary* m_pDict;
-    } m_Element;
-    struct {
-      uint32_t m_PageObjNum;
-      uint32_t m_ContentId;
-    } m_PageContent;
-    struct {
-      uint32_t m_PageObjNum;
-      uint32_t m_ContentId;
-      uint32_t m_RefObjNum;
-    } m_StreamContent;
-    struct {
-      uint32_t m_PageObjNum;
-      uint32_t m_RefObjNum;
-    } m_Object;
-  };
-};
-
 class IPDF_StructElement {
  public:
   virtual ~IPDF_StructElement() {}
@@ -60,7 +36,7 @@
   virtual IPDF_StructElement* GetParent() const = 0;
   virtual CPDF_Dictionary* GetDict() const = 0;
   virtual int CountKids() const = 0;
-  virtual const CPDF_StructKid& GetKid(int index) const = 0;
+  virtual IPDF_StructElement* GetKidIfElement(int index) const = 0;
 
   virtual CPDF_Object* GetAttr(const CFX_ByteStringC& owner,
                                const CFX_ByteStringC& name,
diff --git a/core/fpdfdoc/tagged_int.h b/core/fpdfdoc/tagged_int.h
index 03f1c40..6a24700 100644
--- a/core/fpdfdoc/tagged_int.h
+++ b/core/fpdfdoc/tagged_int.h
@@ -15,39 +15,62 @@
 #include "core/fxcrt/cfx_retain_ptr.h"
 #include "third_party/base/stl_util.h"
 
-class CPDF_StructElementImpl;
+class CPDF_StructElement;
 
-class CPDF_StructTreeImpl final : public IPDF_StructTree {
+struct CPDF_StructKid {
+  enum { Invalid, Element, PageContent, StreamContent, Object } m_Type;
+
+  union {
+    struct {
+      CPDF_StructElement* m_pElement;
+      CPDF_Dictionary* m_pDict;
+    } m_Element;
+    struct {
+      uint32_t m_PageObjNum;
+      uint32_t m_ContentId;
+    } m_PageContent;
+    struct {
+      uint32_t m_PageObjNum;
+      uint32_t m_ContentId;
+      uint32_t m_RefObjNum;
+    } m_StreamContent;
+    struct {
+      uint32_t m_PageObjNum;
+      uint32_t m_RefObjNum;
+    } m_Object;
+  };
+};
+
+class CPDF_StructTree final : public IPDF_StructTree {
  public:
-  explicit CPDF_StructTreeImpl(const CPDF_Document* pDoc);
-  ~CPDF_StructTreeImpl() override;
+  explicit CPDF_StructTree(const CPDF_Document* pDoc);
+  ~CPDF_StructTree() override;
 
   // IPDF_StructTree:
   int CountTopElements() const override;
   IPDF_StructElement* GetTopElement(int i) const override;
 
   void LoadPageTree(const CPDF_Dictionary* pPageDict);
-  CPDF_StructElementImpl* AddPageNode(
+  CPDF_StructElement* AddPageNode(
       CPDF_Dictionary* pElement,
-      std::map<CPDF_Dictionary*, CPDF_StructElementImpl*>& map,
+      std::map<CPDF_Dictionary*, CPDF_StructElement*>& map,
       int nLevel = 0);
-  bool AddTopLevelNode(CPDF_Dictionary* pDict,
-                       CPDF_StructElementImpl* pElement);
+  bool AddTopLevelNode(CPDF_Dictionary* pDict, CPDF_StructElement* pElement);
 
  protected:
   const CPDF_Dictionary* const m_pTreeRoot;
   const CPDF_Dictionary* const m_pRoleMap;
   const CPDF_Dictionary* m_pPage;
-  std::vector<CFX_RetainPtr<CPDF_StructElementImpl>> m_Kids;
+  std::vector<CFX_RetainPtr<CPDF_StructElement>> m_Kids;
 
-  friend class CPDF_StructElementImpl;
+  friend class CPDF_StructElement;
 };
 
-class CPDF_StructElementImpl final : public IPDF_StructElement {
+class CPDF_StructElement final : public IPDF_StructElement {
  public:
-  CPDF_StructElementImpl(CPDF_StructTreeImpl* pTree,
-                         CPDF_StructElementImpl* pParent,
-                         CPDF_Dictionary* pDict);
+  CPDF_StructElement(CPDF_StructTree* pTree,
+                     CPDF_StructElement* pParent,
+                     CPDF_Dictionary* pDict);
 
   // IPDF_StructElement
   IPDF_StructTree* GetTree() const override;
@@ -55,7 +78,7 @@
   IPDF_StructElement* GetParent() const override;
   CPDF_Dictionary* GetDict() const override;
   int CountKids() const override;
-  const CPDF_StructKid& GetKid(int index) const override;
+  IPDF_StructElement* GetKidIfElement(int index) const override;
   CPDF_Object* GetAttr(const CFX_ByteStringC& owner,
                        const CFX_ByteStringC& name,
                        bool bInheritable = false,
@@ -81,26 +104,26 @@
                  bool bInheritable = false,
                  int subindex = -1) override;
 
+  std::vector<CPDF_StructKid>* GetKids() { return &m_Kids; }
   void LoadKids(CPDF_Dictionary* pDict);
   void LoadKid(uint32_t PageObjNum, CPDF_Object* pObj, CPDF_StructKid* pKid);
   CPDF_Object* GetAttr(const CFX_ByteStringC& owner,
                        const CFX_ByteStringC& name,
                        bool bInheritable,
                        int subindex);
-  CPDF_StructElementImpl* Retain();
+
+  CPDF_StructElement* Retain();
   void Release();
 
  protected:
-  ~CPDF_StructElementImpl() override;
+  ~CPDF_StructElement() override;
 
   int m_RefCount;
-  CPDF_StructTreeImpl* const m_pTree;
-  CPDF_StructElementImpl* const m_pParent;
+  CPDF_StructTree* const m_pTree;
+  CPDF_StructElement* const m_pParent;
   CPDF_Dictionary* const m_pDict;
   CFX_ByteString m_Type;
   std::vector<CPDF_StructKid> m_Kids;
-
-  friend class CPDF_StructTreeImpl;
 };
 
 #endif  // CORE_FPDFDOC_TAGGED_INT_H_
diff --git a/fpdfsdk/fpdf_structtree.cpp b/fpdfsdk/fpdf_structtree.cpp
index 9bc1df6..26ded3d 100644
--- a/fpdfsdk/fpdf_structtree.cpp
+++ b/fpdfsdk/fpdf_structtree.cpp
@@ -83,7 +83,5 @@
   if (!elem || index < 0 || index >= elem->CountKids())
     return nullptr;
 
-  CPDF_StructKid kid = elem->GetKid(index);
-  return kid.m_Type == CPDF_StructKid::Element ? kid.m_Element.m_pElement
-                                               : nullptr;
+  return elem->GetKidIfElement(index);
 }