Cleanup ownership of parser members

Change m_pXMLDoc and m_pStream in CXFA_SimpleParser to be a unique_ptr. This
allows removing the CloseParser() call from CXFA_DocumentParser as the items
will get cleaned up automatically.

Review-Url: https://codereview.chromium.org/2131653002
diff --git a/xfa/fde/xml/fde_xml_imp.h b/xfa/fde/xml/fde_xml_imp.h
index ba75f6b..2667a10 100644
--- a/xfa/fde/xml/fde_xml_imp.h
+++ b/xfa/fde/xml/fde_xml_imp.h
@@ -193,7 +193,6 @@
   CFDE_XMLDoc();
   ~CFDE_XMLDoc() override;
 
-  void Release() { delete this; }
   FX_BOOL LoadXML(CFDE_XMLParser* pXMLParser);
   int32_t DoLoad(IFX_Pause* pPause = nullptr);
   void CloseXML();
diff --git a/xfa/fxfa/parser/cxfa_document_parser.cpp b/xfa/fxfa/parser/cxfa_document_parser.cpp
index 565916b..ff54fb3 100644
--- a/xfa/fxfa/parser/cxfa_document_parser.cpp
+++ b/xfa/fxfa/parser/cxfa_document_parser.cpp
@@ -13,12 +13,13 @@
     : m_nodeParser(nullptr, TRUE), m_pNotify(pNotify) {}
 
 CXFA_DocumentParser::~CXFA_DocumentParser() {
-  CloseParser();
 }
 
 int32_t CXFA_DocumentParser::StartParse(IFX_FileRead* pStream,
                                         XFA_XDPPACKET ePacketID) {
-  CloseParser();
+  m_pDocument.reset();
+  m_nodeParser.CloseParser();
+
   int32_t nRetStatus = m_nodeParser.StartParse(pStream, ePacketID);
   if (nRetStatus == XFA_PARSESTATUS_Ready) {
     m_pDocument.reset(new CXFA_Document(this));
@@ -47,8 +48,3 @@
 CXFA_Document* CXFA_DocumentParser::GetDocument() const {
   return m_pDocument.get();
 }
-
-void CXFA_DocumentParser::CloseParser() {
-  m_pDocument.reset();
-  m_nodeParser.CloseParser();
-}
diff --git a/xfa/fxfa/parser/cxfa_document_parser.h b/xfa/fxfa/parser/cxfa_document_parser.h
index 42275df..3e72f9b 100644
--- a/xfa/fxfa/parser/cxfa_document_parser.h
+++ b/xfa/fxfa/parser/cxfa_document_parser.h
@@ -29,7 +29,6 @@
   CFDE_XMLDoc* GetXMLDoc() const;
   CXFA_FFNotify* GetNotify() const;
   CXFA_Document* GetDocument() const;
-  void CloseParser();
 
  protected:
   CXFA_SimpleParser m_nodeParser;
diff --git a/xfa/fxfa/parser/cxfa_simple_parser.cpp b/xfa/fxfa/parser/cxfa_simple_parser.cpp
index c9edf2d..0a57b5d 100644
--- a/xfa/fxfa/parser/cxfa_simple_parser.cpp
+++ b/xfa/fxfa/parser/cxfa_simple_parser.cpp
@@ -24,7 +24,6 @@
       m_bDocumentParser(bDocumentParser) {}
 
 CXFA_SimpleParser::~CXFA_SimpleParser() {
-  CloseParser();
 }
 
 void CXFA_SimpleParser::SetFactory(CXFA_Document* pFactory) {
@@ -57,82 +56,76 @@
   }
   return nullptr;
 }
+
 int32_t CXFA_SimpleParser::StartParse(IFX_FileRead* pStream,
                                       XFA_XDPPACKET ePacketID) {
   CloseParser();
   m_pFileRead = pStream;
-  m_pStream = IFX_Stream::CreateStream(
-      pStream, FX_STREAMACCESS_Read | FX_STREAMACCESS_Text);
-  if (!m_pStream) {
+  m_pStream.reset(IFX_Stream::CreateStream(
+      pStream, FX_STREAMACCESS_Read | FX_STREAMACCESS_Text));
+  if (!m_pStream)
     return XFA_PARSESTATUS_StreamErr;
-  }
+
   uint16_t wCodePage = m_pStream->GetCodePage();
   if (wCodePage != FX_CODEPAGE_UTF16LE && wCodePage != FX_CODEPAGE_UTF16BE &&
       wCodePage != FX_CODEPAGE_UTF8) {
     m_pStream->SetCodePage(FX_CODEPAGE_UTF8);
   }
-  m_pXMLDoc = new CFDE_XMLDoc;
-  m_pXMLParser = new CXFA_XMLParser(m_pXMLDoc->GetRoot(), m_pStream);
-  if (!m_pXMLDoc->LoadXML(m_pXMLParser)) {
+  m_pXMLDoc.reset(new CFDE_XMLDoc);
+  m_pXMLParser = new CXFA_XMLParser(m_pXMLDoc->GetRoot(), m_pStream.get());
+  if (!m_pXMLDoc->LoadXML(m_pXMLParser))
     return XFA_PARSESTATUS_StatusErr;
-  }
+
   m_ePacketID = ePacketID;
   return XFA_PARSESTATUS_Ready;
 }
+
 int32_t CXFA_SimpleParser::DoParse(IFX_Pause* pPause) {
-  if (!m_pXMLDoc || m_ePacketID == XFA_XDPPACKET_UNKNOWN) {
+  if (!m_pXMLDoc || m_ePacketID == XFA_XDPPACKET_UNKNOWN)
     return XFA_PARSESTATUS_StatusErr;
-  }
+
   int32_t iRet = m_pXMLDoc->DoLoad(pPause);
-  if (iRet < 0) {
+  if (iRet < 0)
     return XFA_PARSESTATUS_SyntaxErr;
-  }
-  if (iRet < 100) {
+  if (iRet < 100)
     return iRet / 2;
-  }
-  m_pRootNode = ParseAsXDPPacket(XFA_FDEExtension_GetDocumentNode(m_pXMLDoc),
-                                 m_ePacketID);
+
+  m_pRootNode = ParseAsXDPPacket(
+      XFA_FDEExtension_GetDocumentNode(m_pXMLDoc.get()), m_ePacketID);
   m_pXMLDoc->CloseXML();
-  if (m_pStream) {
-    m_pStream->Release();
-    m_pStream = nullptr;
-  }
-  if (!m_pRootNode) {
+  m_pStream.reset();
+
+  if (!m_pRootNode)
     return XFA_PARSESTATUS_StatusErr;
-  }
   return XFA_PARSESTATUS_Done;
 }
+
 int32_t CXFA_SimpleParser::ParseXMLData(const CFX_WideString& wsXML,
                                         CFDE_XMLNode*& pXMLNode,
                                         IFX_Pause* pPause) {
   CloseParser();
   pXMLNode = nullptr;
-  IFX_Stream* pStream = XFA_CreateWideTextRead(wsXML);
-  if (!pStream) {
+
+  std::unique_ptr<IFX_Stream> pStream(XFA_CreateWideTextRead(wsXML));
+  if (!pStream)
     return XFA_PARSESTATUS_StreamErr;
-  }
-  m_pStream = pStream;
-  m_pXMLDoc = new CFDE_XMLDoc;
-  CXFA_XMLParser* pParser = new CXFA_XMLParser(m_pXMLDoc->GetRoot(), m_pStream);
+
+  m_pXMLDoc.reset(new CFDE_XMLDoc);
+  CXFA_XMLParser* pParser =
+      new CXFA_XMLParser(m_pXMLDoc->GetRoot(), pStream.get());
   pParser->m_dwCheckStatus = 0x03;
-  if (!m_pXMLDoc->LoadXML(pParser)) {
+  if (!m_pXMLDoc->LoadXML(pParser))
     return XFA_PARSESTATUS_StatusErr;
-  }
+
   int32_t iRet = m_pXMLDoc->DoLoad(pPause);
-  if (iRet < 0 || iRet >= 100) {
+  if (iRet < 0 || iRet >= 100)
     m_pXMLDoc->CloseXML();
-  }
-  if (iRet < 0) {
+  if (iRet < 0)
     return XFA_PARSESTATUS_SyntaxErr;
-  }
-  if (iRet < 100) {
+  if (iRet < 100)
     return iRet / 2;
-  }
-  if (m_pStream) {
-    m_pStream->Release();
-    m_pStream = nullptr;
-  }
-  pXMLNode = XFA_FDEExtension_GetDocumentNode(m_pXMLDoc);
+
+  pXMLNode = XFA_FDEExtension_GetDocumentNode(m_pXMLDoc.get());
   return XFA_PARSESTATUS_Done;
 }
 
@@ -187,7 +180,7 @@
 }
 
 CFDE_XMLDoc* CXFA_SimpleParser::GetXMLDoc() const {
-  return m_pXMLDoc;
+  return m_pXMLDoc.get();
 }
 
 FX_BOOL XFA_FDEExtension_ResolveNamespaceQualifier(
@@ -219,6 +212,7 @@
   wsNamespaceURI.clear();
   return bRet;
 }
+
 static inline void XFA_FDEExtension_GetElementTagNamespaceURI(
     CFDE_XMLElement* pElement,
     CFX_WideString& wsNamespaceURI) {
@@ -229,6 +223,7 @@
     wsNamespaceURI.clear();
   }
 }
+
 static FX_BOOL XFA_FDEExtension_MatchNodeName(
     CFDE_XMLNode* pNode,
     const CFX_WideStringC& wsLocalTagName,
@@ -253,6 +248,7 @@
   }
   return wsNodeStr == wsNamespaceURIPrefix;
 }
+
 static FX_BOOL XFA_FDEExtension_GetAttributeLocalName(
     const CFX_WideStringC& wsAttributeName,
     CFX_WideString& wsLocalAttrName) {
@@ -266,6 +262,7 @@
     return TRUE;
   }
 }
+
 static FX_BOOL XFA_FDEExtension_ResolveAttribute(
     CFDE_XMLElement* pElement,
     const CFX_WideStringC& wsAttributeName,
@@ -289,6 +286,7 @@
   }
   return TRUE;
 }
+
 static FX_BOOL XFA_FDEExtension_FindAttributeWithNS(
     CFDE_XMLElement* pElement,
     const CFX_WideStringC& wsLocalAttributeName,
@@ -336,6 +334,7 @@
   }
   return FALSE;
 }
+
 CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket(CFDE_XMLNode* pXMLDocumentNode,
                                                XFA_XDPPACKET ePacketID) {
   switch (ePacketID) {
@@ -361,6 +360,7 @@
       return ParseAsXDPPacket_User(pXMLDocumentNode, ePacketID);
   }
 }
+
 CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_XDP(
     CFDE_XMLNode* pXMLDocumentNode,
     XFA_XDPPACKET ePacketID) {
@@ -499,6 +499,7 @@
   pXFARootNode->SetXMLMappingNode(pXMLDocumentNode);
   return pXFARootNode;
 }
+
 CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_Config(
     CFDE_XMLNode* pXMLDocumentNode,
     XFA_XDPPACKET ePacketID) {
@@ -521,6 +522,7 @@
   pNode->SetXMLMappingNode(pXMLDocumentNode);
   return pNode;
 }
+
 CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_TemplateForm(
     CFDE_XMLNode* pXMLDocumentNode,
     XFA_XDPPACKET ePacketID) {
@@ -604,6 +606,7 @@
   }
   return pNode;
 }
+
 static CFDE_XMLNode* XFA_GetDataSetsFromXDP(CFDE_XMLNode* pXMLDocumentNode) {
   if (XFA_FDEExtension_MatchNodeName(
           pXMLDocumentNode, XFA_GetPacketByIndex(XFA_PACKET_Datasets)->pName,
@@ -631,6 +634,7 @@
   }
   return nullptr;
 }
+
 CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_Data(
     CFDE_XMLNode* pXMLDocumentNode,
     XFA_XDPPACKET ePacketID) {
@@ -695,6 +699,7 @@
   }
   return nullptr;
 }
+
 CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_LocaleConnectionSourceSet(
     CFDE_XMLNode* pXMLDocumentNode,
     XFA_XDPPACKET ePacketID) {
@@ -754,6 +759,7 @@
   }
   return pNode;
 }
+
 CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_Xdc(
     CFDE_XMLNode* pXMLDocumentNode,
     XFA_XDPPACKET ePacketID) {
@@ -773,6 +779,7 @@
   }
   return nullptr;
 }
+
 CXFA_Node* CXFA_SimpleParser::ParseAsXDPPacket_User(
     CFDE_XMLNode* pXMLDocumentNode,
     XFA_XDPPACKET ePacketID) {
@@ -790,20 +797,24 @@
   pNode->SetXMLMappingNode(pXMLDocumentNode);
   return pNode;
 }
+
 CXFA_Node* CXFA_SimpleParser::UserPacketLoader(CXFA_Node* pXFANode,
                                                CFDE_XMLNode* pXMLDoc) {
   return pXFANode;
 }
+
 static FX_BOOL XFA_FDEExtension_IsStringAllWhitespace(CFX_WideString wsText) {
   wsText.TrimRight(L"\x20\x9\xD\xA");
   return wsText.IsEmpty();
 }
+
 CXFA_Node* CXFA_SimpleParser::DataLoader(CXFA_Node* pXFANode,
                                          CFDE_XMLNode* pXMLDoc,
                                          FX_BOOL bDoTransform) {
   ParseDataGroup(pXFANode, pXMLDoc, XFA_XDPPACKET_Datasets);
   return pXFANode;
 }
+
 CXFA_Node* CXFA_SimpleParser::NormalLoader(CXFA_Node* pXFANode,
                                            CFDE_XMLNode* pXMLDoc,
                                            XFA_XDPPACKET ePacketID,
@@ -894,6 +905,7 @@
   }
   return pXFANode;
 }
+
 FX_BOOL XFA_RecognizeRichText(CFDE_XMLElement* pRichTextXMLNode) {
   if (pRichTextXMLNode) {
     CFX_WideString wsNamespaceURI;
@@ -905,6 +917,7 @@
   }
   return FALSE;
 }
+
 class RichTextNodeVisitor {
  public:
   static inline CFDE_XMLNode* GetFirstChild(CFDE_XMLNode* pNode) {
@@ -1306,13 +1319,8 @@
     }
   }
 }
+
 void CXFA_SimpleParser::CloseParser() {
-  if (m_pXMLDoc) {
-    m_pXMLDoc->Release();
-    m_pXMLDoc = nullptr;
-  }
-  if (m_pStream) {
-    m_pStream->Release();
-    m_pStream = nullptr;
-  }
+  m_pXMLDoc.reset();
+  m_pStream.reset();
 }
diff --git a/xfa/fxfa/parser/cxfa_simple_parser.h b/xfa/fxfa/parser/cxfa_simple_parser.h
index d49008d..e8b39b0 100644
--- a/xfa/fxfa/parser/cxfa_simple_parser.h
+++ b/xfa/fxfa/parser/cxfa_simple_parser.h
@@ -7,6 +7,8 @@
 #ifndef XFA_FXFA_PARSER_CXFA_SIMPLE_PARSER_H_
 #define XFA_FXFA_PARSER_CXFA_SIMPLE_PARSER_H_
 
+#include <memory>
+
 #include "xfa/fde/xml/fde_xml_imp.h"
 #include "xfa/fxfa/include/fxfa_basic.h"
 
@@ -73,8 +75,8 @@
   void SetFactory(CXFA_Document* pFactory);
 
   CXFA_XMLParser* m_pXMLParser;
-  CFDE_XMLDoc* m_pXMLDoc;
-  IFX_Stream* m_pStream;
+  std::unique_ptr<CFDE_XMLDoc> m_pXMLDoc;
+  std::unique_ptr<IFX_Stream, ReleaseDeleter<IFX_Stream>> m_pStream;
   IFX_FileRead* m_pFileRead;
   CXFA_Document* m_pFactory;
   CXFA_Node* m_pRootNode;