Make FDF document creation return unique_ptrs

Review-Url: https://codereview.chromium.org/2538533003
diff --git a/core/fpdfapi/parser/cfdf_document.cpp b/core/fpdfapi/parser/cfdf_document.cpp
index 546308c..41e2852 100644
--- a/core/fpdfapi/parser/cfdf_document.cpp
+++ b/core/fpdfapi/parser/cfdf_document.cpp
@@ -25,24 +25,26 @@
     m_pFile->Release();
 }
 
-CFDF_Document* CFDF_Document::CreateNewDoc() {
-  CFDF_Document* pDoc = new CFDF_Document;
+std::unique_ptr<CFDF_Document> CFDF_Document::CreateNewDoc() {
+  auto pDoc = pdfium::MakeUnique<CFDF_Document>();
   pDoc->m_pRootDict = pDoc->NewIndirect<CPDF_Dictionary>();
   pDoc->m_pRootDict->SetNewFor<CPDF_Dictionary>("FDF");
   return pDoc;
 }
 
-CFDF_Document* CFDF_Document::ParseFile(IFX_SeekableReadStream* pFile,
-                                        bool bOwnFile) {
+std::unique_ptr<CFDF_Document> CFDF_Document::ParseFile(
+    IFX_SeekableReadStream* pFile,
+    bool bOwnFile) {
   if (!pFile)
     return nullptr;
 
-  std::unique_ptr<CFDF_Document> pDoc(new CFDF_Document);
+  auto pDoc = pdfium::MakeUnique<CFDF_Document>();
   pDoc->ParseStream(pFile, bOwnFile);
-  return pDoc->m_pRootDict ? pDoc.release() : nullptr;
+  return pDoc->m_pRootDict ? std::move(pDoc) : nullptr;
 }
 
-CFDF_Document* CFDF_Document::ParseMemory(const uint8_t* pData, uint32_t size) {
+std::unique_ptr<CFDF_Document> CFDF_Document::ParseMemory(const uint8_t* pData,
+                                                          uint32_t size) {
   return CFDF_Document::ParseFile(FX_CreateMemoryStream((uint8_t*)pData, size),
                                   true);
 }
diff --git a/core/fpdfapi/parser/cfdf_document.h b/core/fpdfapi/parser/cfdf_document.h
index 0751193..1b47368 100644
--- a/core/fpdfapi/parser/cfdf_document.h
+++ b/core/fpdfapi/parser/cfdf_document.h
@@ -7,6 +7,8 @@
 #ifndef CORE_FPDFAPI_PARSER_CFDF_DOCUMENT_H_
 #define CORE_FPDFAPI_PARSER_CFDF_DOCUMENT_H_
 
+#include <memory>
+
 #include "core/fpdfapi/parser/cpdf_indirect_object_holder.h"
 #include "core/fpdfapi/parser/cpdf_object.h"
 #include "core/fxcrt/fx_basic.h"
@@ -15,17 +17,19 @@
 
 class CFDF_Document : public CPDF_IndirectObjectHolder {
  public:
-  static CFDF_Document* CreateNewDoc();
-  static CFDF_Document* ParseFile(IFX_SeekableReadStream* pFile,
-                                  bool bOwnFile = false);
-  static CFDF_Document* ParseMemory(const uint8_t* pData, uint32_t size);
+  static std::unique_ptr<CFDF_Document> CreateNewDoc();
+  static std::unique_ptr<CFDF_Document> ParseFile(IFX_SeekableReadStream* pFile,
+                                                  bool bOwnFile = false);
+  static std::unique_ptr<CFDF_Document> ParseMemory(const uint8_t* pData,
+                                                    uint32_t size);
+
+  CFDF_Document();
   ~CFDF_Document() override;
 
   bool WriteBuf(CFX_ByteTextBuf& buf) const;
   CPDF_Dictionary* GetRoot() const { return m_pRootDict; }
 
  protected:
-  CFDF_Document();
   void ParseStream(IFX_SeekableReadStream* pFile, bool bOwnFile);
 
   CPDF_Dictionary* m_pRootDict;
diff --git a/core/fpdfdoc/cpdf_interform.cpp b/core/fpdfdoc/cpdf_interform.cpp
index 00365c7..c323986 100644
--- a/core/fpdfdoc/cpdf_interform.cpp
+++ b/core/fpdfdoc/cpdf_interform.cpp
@@ -1185,8 +1185,9 @@
   return nullptr;
 }
 
-CFDF_Document* CPDF_InterForm::ExportToFDF(const CFX_WideStringC& pdf_path,
-                                           bool bSimpleFileSpec) const {
+std::unique_ptr<CFDF_Document> CPDF_InterForm::ExportToFDF(
+    const CFX_WideStringC& pdf_path,
+    bool bSimpleFileSpec) const {
   std::vector<CPDF_FormField*> fields;
   size_t nCount = m_pFieldTree->m_Root.CountFields();
   for (size_t i = 0; i < nCount; ++i)
@@ -1194,12 +1195,12 @@
   return ExportToFDF(pdf_path, fields, true, bSimpleFileSpec);
 }
 
-CFDF_Document* CPDF_InterForm::ExportToFDF(
+std::unique_ptr<CFDF_Document> CPDF_InterForm::ExportToFDF(
     const CFX_WideStringC& pdf_path,
     const std::vector<CPDF_FormField*>& fields,
     bool bIncludeOrExclude,
     bool bSimpleFileSpec) const {
-  CFDF_Document* pDoc = CFDF_Document::CreateNewDoc();
+  std::unique_ptr<CFDF_Document> pDoc = CFDF_Document::CreateNewDoc();
   if (!pDoc)
     return nullptr;
 
diff --git a/core/fpdfdoc/cpdf_interform.h b/core/fpdfdoc/cpdf_interform.h
index fbff028..f03ffb1 100644
--- a/core/fpdfdoc/cpdf_interform.h
+++ b/core/fpdfdoc/cpdf_interform.h
@@ -73,12 +73,14 @@
       const std::vector<CPDF_FormField*>* fields,
       bool bIncludeOrExclude) const;
 
-  CFDF_Document* ExportToFDF(const CFX_WideStringC& pdf_path,
-                             bool bSimpleFileSpec) const;
-  CFDF_Document* ExportToFDF(const CFX_WideStringC& pdf_path,
-                             const std::vector<CPDF_FormField*>& fields,
-                             bool bIncludeOrExclude,
-                             bool bSimpleFileSpec) const;
+  std::unique_ptr<CFDF_Document> ExportToFDF(const CFX_WideStringC& pdf_path,
+                                             bool bSimpleFileSpec) const;
+
+  std::unique_ptr<CFDF_Document> ExportToFDF(
+      const CFX_WideStringC& pdf_path,
+      const std::vector<CPDF_FormField*>& fields,
+      bool bIncludeOrExclude,
+      bool bSimpleFileSpec) const;
 
   bool ResetForm(const std::vector<CPDF_FormField*>& fields,
                  bool bIncludeOrExclude,
diff --git a/fpdfsdk/cpdfsdk_interform.cpp b/fpdfsdk/cpdfsdk_interform.cpp
index f8fd3b3..a2f07b4 100644
--- a/fpdfsdk/cpdfsdk_interform.cpp
+++ b/fpdfsdk/cpdfsdk_interform.cpp
@@ -463,7 +463,8 @@
 
 bool CPDFSDK_InterForm::FDFToURLEncodedData(uint8_t*& pBuf,
                                             FX_STRSIZE& nBufSize) {
-  CFDF_Document* pFDF = CFDF_Document::ParseMemory(pBuf, nBufSize);
+  std::unique_ptr<CFDF_Document> pFDF =
+      CFDF_Document::ParseMemory(pBuf, nBufSize);
   if (!pFDF)
     return true;
 
@@ -506,9 +507,9 @@
     const std::vector<CPDF_FormField*>& fields,
     bool bIncludeOrExclude,
     CFX_ByteTextBuf& textBuf) {
-  std::unique_ptr<CFDF_Document> pFDF(
+  std::unique_ptr<CFDF_Document> pFDF =
       m_pInterForm->ExportToFDF(m_pFormFillEnv->JS_docGetFilePath().AsStringC(),
-                                fields, bIncludeOrExclude, false));
+                                fields, bIncludeOrExclude, false);
   return pFDF ? pFDF->WriteBuf(textBuf) : false;
 }
 
@@ -525,26 +526,21 @@
   if (!m_pFormFillEnv || !m_pInterForm)
     return false;
 
-  CFX_WideString wsPDFFilePath = m_pFormFillEnv->JS_docGetFilePath();
-  CFDF_Document* pFDFDoc =
-      m_pInterForm->ExportToFDF(wsPDFFilePath.AsStringC(), false);
+  std::unique_ptr<CFDF_Document> pFDFDoc = m_pInterForm->ExportToFDF(
+      m_pFormFillEnv->JS_docGetFilePath().AsStringC(), false);
   if (!pFDFDoc)
     return false;
 
   CFX_ByteTextBuf FdfBuffer;
-  bool bRet = pFDFDoc->WriteBuf(FdfBuffer);
-  delete pFDFDoc;
-  if (!bRet)
+  if (!pFDFDoc->WriteBuf(FdfBuffer))
     return false;
 
   uint8_t* pBuffer = FdfBuffer.GetBuffer();
   FX_STRSIZE nBufSize = FdfBuffer.GetLength();
-
   if (bUrlEncoded && !FDFToURLEncodedData(pBuffer, nBufSize))
     return false;
 
   m_pFormFillEnv->JS_docSubmitForm(pBuffer, nBufSize, sDestination.c_str());
-
   if (bUrlEncoded)
     FX_Free(pBuffer);
 
@@ -552,15 +548,9 @@
 }
 
 bool CPDFSDK_InterForm::ExportFormToFDFTextBuf(CFX_ByteTextBuf& textBuf) {
-  CFDF_Document* pFDF = m_pInterForm->ExportToFDF(
+  std::unique_ptr<CFDF_Document> pFDF = m_pInterForm->ExportToFDF(
       m_pFormFillEnv->JS_docGetFilePath().AsStringC(), false);
-  if (!pFDF)
-    return false;
-
-  bool bRet = pFDF->WriteBuf(textBuf);
-  delete pFDF;
-
-  return bRet;
+  return pFDF && pFDF->WriteBuf(textBuf);
 }
 
 bool CPDFSDK_InterForm::DoAction_ResetForm(const CPDF_Action& action) {