Make CXFA_LayoutItem inherit from fxcrt::TreeNode<>.

Long-term, we'd like to manage CXFA_LayoutItem via RetainedTreeNodes,
so this CL converts to a similar API before tackling the issue of
memory management itself.

Change-Id: I89d0ebbf564a236fe7a85dc82f334628d7965745
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/53390
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/xfa/fxfa/layout/cxfa_layoutitem.cpp b/xfa/fxfa/layout/cxfa_layoutitem.cpp
index 1a7798c..a59da61 100644
--- a/xfa/fxfa/layout/cxfa_layoutitem.cpp
+++ b/xfa/fxfa/layout/cxfa_layoutitem.cpp
@@ -78,74 +78,9 @@
 
 CXFA_ViewLayoutItem* CXFA_LayoutItem::GetPage() const {
   for (CXFA_LayoutItem* pCurNode = const_cast<CXFA_LayoutItem*>(this); pCurNode;
-       pCurNode = pCurNode->m_pParent) {
+       pCurNode = pCurNode->GetParent()) {
     if (pCurNode->m_pFormNode->GetElementType() == XFA_Element::PageArea)
       return pCurNode->AsViewLayoutItem();
   }
   return nullptr;
 }
-void CXFA_LayoutItem::AppendLastChild(CXFA_LayoutItem* pChildItem) {
-  if (pChildItem->m_pParent)
-    pChildItem->m_pParent->RemoveChild(pChildItem);
-
-  pChildItem->m_pParent = this;
-  if (!m_pFirstChild) {
-    m_pFirstChild = pChildItem;
-    return;
-  }
-
-  CXFA_LayoutItem* pExistingChildItem = m_pFirstChild;
-  while (pExistingChildItem->m_pNextSibling)
-    pExistingChildItem = pExistingChildItem->m_pNextSibling;
-
-  pExistingChildItem->m_pNextSibling = pChildItem;
-}
-
-void CXFA_LayoutItem::AppendFirstChild(CXFA_LayoutItem* pChildItem) {
-  if (pChildItem->m_pParent)
-    pChildItem->m_pParent->RemoveChild(pChildItem);
-
-  pChildItem->m_pParent = this;
-  if (!m_pFirstChild) {
-    m_pFirstChild = pChildItem;
-    return;
-  }
-
-  CXFA_LayoutItem* pExistingChildItem = m_pFirstChild;
-  m_pFirstChild = pChildItem;
-  m_pFirstChild->m_pNextSibling = pExistingChildItem;
-}
-
-void CXFA_LayoutItem::InsertAfter(CXFA_LayoutItem* pThisChild,
-                                  CXFA_LayoutItem* pThatChild) {
-  if (pThatChild->m_pParent != this)
-    return;
-
-  if (pThisChild->m_pParent)
-    pThisChild->m_pParent = nullptr;
-
-  pThisChild->m_pParent = this;
-
-  CXFA_LayoutItem* pExistingChild = pThatChild->m_pNextSibling;
-  pThatChild->m_pNextSibling = pThisChild;
-  pThisChild->m_pNextSibling = pExistingChild;
-}
-
-void CXFA_LayoutItem::RemoveChild(CXFA_LayoutItem* pChildItem) {
-  if (pChildItem->m_pParent != this)
-    return;
-
-  if (m_pFirstChild == pChildItem) {
-    m_pFirstChild = pChildItem->m_pNextSibling;
-  } else {
-    CXFA_LayoutItem* pExistingChildItem = m_pFirstChild;
-    while (pExistingChildItem &&
-           pExistingChildItem->m_pNextSibling != pChildItem) {
-      pExistingChildItem = pExistingChildItem->m_pNextSibling;
-    }
-    if (pExistingChildItem)
-      pExistingChildItem->m_pNextSibling = pChildItem->m_pNextSibling;
-  }
-  pChildItem->m_pNextSibling = nullptr;
-  pChildItem->m_pParent = nullptr;
-}
diff --git a/xfa/fxfa/layout/cxfa_layoutitem.h b/xfa/fxfa/layout/cxfa_layoutitem.h
index 3319942..cc85ce0 100644
--- a/xfa/fxfa/layout/cxfa_layoutitem.h
+++ b/xfa/fxfa/layout/cxfa_layoutitem.h
@@ -7,6 +7,7 @@
 #ifndef XFA_FXFA_LAYOUT_CXFA_LAYOUTITEM_H_
 #define XFA_FXFA_LAYOUT_CXFA_LAYOUTITEM_H_
 
+#include "core/fxcrt/tree_node.h"
 #include "core/fxcrt/unowned_ptr.h"
 #include "xfa/fxfa/parser/cxfa_document.h"
 
@@ -14,9 +15,9 @@
 class CXFA_LayoutProcessor;
 class CXFA_ViewLayoutItem;
 
-class CXFA_LayoutItem {
+class CXFA_LayoutItem : public TreeNode<CXFA_LayoutItem> {
  public:
-  virtual ~CXFA_LayoutItem();
+  ~CXFA_LayoutItem() override;
 
   bool IsViewLayoutItem() const { return m_ItemType == kViewItem; }
   bool IsContentLayoutItem() const { return m_ItemType == kContentItem; }
@@ -26,28 +27,15 @@
   const CXFA_ContentLayoutItem* AsContentLayoutItem() const;
 
   CXFA_ViewLayoutItem* GetPage() const;
-  CXFA_LayoutItem* GetParent() const { return m_pParent; }
-  CXFA_LayoutItem* GetFirstChild() const { return m_pFirstChild; }
-  CXFA_LayoutItem* GetNextSibling() const { return m_pNextSibling; }
   CXFA_Node* GetFormNode() const { return m_pFormNode.Get(); }
   void SetFormNode(CXFA_Node* pNode) { m_pFormNode = pNode; }
 
-  void AppendLastChild(CXFA_LayoutItem* pChildItem);
-  void AppendFirstChild(CXFA_LayoutItem* pChildItem);
-  void RemoveChild(CXFA_LayoutItem* pChildItem);
-
-  // Insert |pThisChild| after |pThatChild|.
-  void InsertAfter(CXFA_LayoutItem* pThisChild, CXFA_LayoutItem* pThatChild);
-
  protected:
   enum ItemType { kViewItem, kContentItem };
   CXFA_LayoutItem(CXFA_Node* pNode, ItemType type);
 
  private:
   const ItemType m_ItemType;
-  CXFA_LayoutItem* m_pParent = nullptr;       // Raw, intra-tree pointer.
-  CXFA_LayoutItem* m_pFirstChild = nullptr;   // Raw, intra-tree pointer.
-  CXFA_LayoutItem* m_pNextSibling = nullptr;  // Raw, intra-tree pointer.
   UnownedPtr<CXFA_Node> m_pFormNode;
 };