Remove more |new|s, part 2

Change-Id: I13b43ceafc6a35bcc1e366546a4a408ea01fe4ab
Reviewed-on: https://pdfium-review.googlesource.com/4534
Reviewed-by: dsinclair <dsinclair@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/parser/cpdf_parser_unittest.cpp b/core/fpdfapi/parser/cpdf_parser_unittest.cpp
index bbf707a..f988f82 100644
--- a/core/fpdfapi/parser/cpdf_parser_unittest.cpp
+++ b/core/fpdfapi/parser/cpdf_parser_unittest.cpp
@@ -16,12 +16,8 @@
 // Provide a way to read test data from a buffer instead of a file.
 class CFX_TestBufferRead : public IFX_SeekableReadStream {
  public:
-  static CFX_RetainPtr<CFX_TestBufferRead> Create(
-      const unsigned char* buffer_in,
-      size_t buf_size) {
-    return CFX_RetainPtr<CFX_TestBufferRead>(
-        new CFX_TestBufferRead(buffer_in, buf_size));
-  }
+  template <typename T, typename... Args>
+  friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args);
 
   // IFX_SeekableReadStream:
   bool ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) override {
@@ -62,11 +58,9 @@
 
   // Setup reading from a buffer and initial states.
   bool InitTestFromBuffer(const unsigned char* buffer, size_t len) {
-    CFX_RetainPtr<CFX_TestBufferRead> buffer_reader =
-        CFX_TestBufferRead::Create(buffer, len);
-
     // For the test file, the header is set at the beginning.
-    m_pSyntax->InitParser(buffer_reader, 0);
+    m_pSyntax->InitParser(pdfium::MakeRetain<CFX_TestBufferRead>(buffer, len),
+                          0);
     return true;
   }
 
diff --git a/core/fpdfdoc/cpdf_bookmark.cpp b/core/fpdfdoc/cpdf_bookmark.cpp
index ba1e18c..8ca5d12 100644
--- a/core/fpdfdoc/cpdf_bookmark.cpp
+++ b/core/fpdfdoc/cpdf_bookmark.cpp
@@ -7,6 +7,7 @@
 #include "core/fpdfdoc/cpdf_bookmark.h"
 
 #include <memory>
+#include <vector>
 
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_string.h"
@@ -44,12 +45,12 @@
   if (!len)
     return CFX_WideString();
 
-  std::unique_ptr<wchar_t[]> buf(new wchar_t[len]);
+  std::vector<wchar_t> buf(len);
   for (int i = 0; i < len; i++) {
     wchar_t w = title[i];
     buf[i] = w > 0x20 ? w : 0x20;
   }
-  return CFX_WideString(buf.get(), len);
+  return CFX_WideString(buf.data(), len);
 }
 
 CPDF_Dest CPDF_Bookmark::GetDest(CPDF_Document* pDocument) const {
diff --git a/core/fpdfdoc/cpdf_filespec.cpp b/core/fpdfdoc/cpdf_filespec.cpp
index 086515b..7fe8ec8 100644
--- a/core/fpdfdoc/cpdf_filespec.cpp
+++ b/core/fpdfdoc/cpdf_filespec.cpp
@@ -112,10 +112,9 @@
   return true;
 }
 
-CPDF_FileSpec::CPDF_FileSpec(const CFX_WeakPtr<CFX_ByteStringPool>& pPool) {
-  m_pObj = new CPDF_Dictionary(pPool);
-  m_pObj->AsDictionary()->SetNewFor<CPDF_Name>("Type", "Filespec");
-}
+CPDF_FileSpec::CPDF_FileSpec(CPDF_Object* pObj) : m_pObj(pObj) {}
+
+CPDF_FileSpec::~CPDF_FileSpec() {}
 
 CFX_WideString CPDF_FileSpec::EncodeFileName(const CFX_WideStringC& filepath) {
   if (filepath.GetLength() <= 1)
diff --git a/core/fpdfdoc/cpdf_filespec.h b/core/fpdfdoc/cpdf_filespec.h
index 6266c37..ea002f0 100644
--- a/core/fpdfdoc/cpdf_filespec.h
+++ b/core/fpdfdoc/cpdf_filespec.h
@@ -15,8 +15,8 @@
 
 class CPDF_FileSpec {
  public:
-  explicit CPDF_FileSpec(const CFX_WeakPtr<CFX_ByteStringPool>& pPool);
-  explicit CPDF_FileSpec(CPDF_Object* pObj) : m_pObj(pObj) {}
+  explicit CPDF_FileSpec(CPDF_Object* pObj);
+  ~CPDF_FileSpec();
 
   // Convert a platform dependent file name into pdf format.
   static CFX_WideString EncodeFileName(const CFX_WideStringC& filepath);
@@ -31,7 +31,7 @@
   void SetFileName(const CFX_WideStringC& wsFileName);
 
  private:
-  CPDF_Object* m_pObj;
+  CPDF_Object* const m_pObj;  // not owned.
 };
 
 #endif  // CORE_FPDFDOC_CPDF_FILESPEC_H_
diff --git a/core/fpdfdoc/cpdf_interform.cpp b/core/fpdfdoc/cpdf_interform.cpp
index dad25aa..f498617 100644
--- a/core/fpdfdoc/cpdf_interform.cpp
+++ b/core/fpdfdoc/cpdf_interform.cpp
@@ -23,6 +23,7 @@
 #include "core/fxcrt/fx_codepage.h"
 #include "core/fxge/cfx_substfont.h"
 #include "core/fxge/fx_font.h"
+#include "third_party/base/ptr_util.h"
 #include "third_party/base/stl_util.h"
 
 namespace {
@@ -633,7 +634,7 @@
 CPDF_InterForm::CPDF_InterForm(CPDF_Document* pDocument)
     : m_pDocument(pDocument),
       m_pFormDict(nullptr),
-      m_pFieldTree(new CFieldTree),
+      m_pFieldTree(pdfium::MakeUnique<CFieldTree>()),
       m_pFormNotify(nullptr) {
   CPDF_Dictionary* pRoot = m_pDocument->GetRoot();
   if (!pRoot)
@@ -1198,9 +1199,12 @@
       pMainDict->SetNewFor<CPDF_String>("UF", PDF_EncodeText(wsFilePath),
                                         false);
     } else {
-      CPDF_FileSpec filespec(pDoc->GetByteStringPool());
+      auto pNewDict =
+          pdfium::MakeUnique<CPDF_Dictionary>(pDoc->GetByteStringPool());
+      pNewDict->SetNewFor<CPDF_Name>("Type", "Filespec");
+      CPDF_FileSpec filespec(pNewDict.get());
       filespec.SetFileName(pdf_path);
-      pMainDict->SetFor("F", pdfium::WrapUnique(filespec.GetObj()));
+      pMainDict->SetFor("F", std::move(pNewDict));
     }
   }
 
diff --git a/core/fxcodec/codec/fx_codec.cpp b/core/fxcodec/codec/fx_codec.cpp
index c47231c..38f5a76 100644
--- a/core/fxcodec/codec/fx_codec.cpp
+++ b/core/fxcodec/codec/fx_codec.cpp
@@ -18,14 +18,13 @@
 #include "third_party/base/ptr_util.h"
 
 CCodec_ModuleMgr::CCodec_ModuleMgr()
-    : m_pBasicModule(new CCodec_BasicModule),
-      m_pFaxModule(new CCodec_FaxModule),
-      m_pJpegModule(new CCodec_JpegModule),
-      m_pJpxModule(new CCodec_JpxModule),
-      m_pJbig2Module(new CCodec_Jbig2Module),
-      m_pIccModule(new CCodec_IccModule),
-      m_pFlateModule(new CCodec_FlateModule) {
-}
+    : m_pBasicModule(pdfium::MakeUnique<CCodec_BasicModule>()),
+      m_pFaxModule(pdfium::MakeUnique<CCodec_FaxModule>()),
+      m_pJpegModule(pdfium::MakeUnique<CCodec_JpegModule>()),
+      m_pJpxModule(pdfium::MakeUnique<CCodec_JpxModule>()),
+      m_pJbig2Module(pdfium::MakeUnique<CCodec_Jbig2Module>()),
+      m_pIccModule(pdfium::MakeUnique<CCodec_IccModule>()),
+      m_pFlateModule(pdfium::MakeUnique<CCodec_FlateModule>()) {}
 
 CCodec_ModuleMgr::~CCodec_ModuleMgr() {}
 
diff --git a/core/fxcrt/fx_bidi.cpp b/core/fxcrt/fx_bidi.cpp
index 49333f6..2554454 100644
--- a/core/fxcrt/fx_bidi.cpp
+++ b/core/fxcrt/fx_bidi.cpp
@@ -5,10 +5,12 @@
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
 #include "core/fxcrt/fx_bidi.h"
-#include "core/fxcrt/fx_ucd.h"
 
 #include <algorithm>
 
+#include "core/fxcrt/fx_ucd.h"
+#include "third_party/base/ptr_util.h"
+
 CFX_BidiChar::CFX_BidiChar()
     : m_CurrentSegment({0, 0, NEUTRAL}), m_LastSegment({0, 0, NEUTRAL}) {}
 
@@ -50,7 +52,7 @@
 
 CFX_BidiString::CFX_BidiString(const CFX_WideString& str)
     : m_Str(str),
-      m_pBidiChar(new CFX_BidiChar),
+      m_pBidiChar(pdfium::MakeUnique<CFX_BidiChar>()),
       m_eOverallDirection(CFX_BidiChar::LEFT) {
   for (const auto& c : m_Str) {
     if (m_pBidiChar->AppendChar(c))
diff --git a/core/fxcrt/fx_stream.cpp b/core/fxcrt/fx_stream.cpp
index 2c451c6..32b6ca3 100644
--- a/core/fxcrt/fx_stream.cpp
+++ b/core/fxcrt/fx_stream.cpp
@@ -52,7 +52,7 @@
 CFX_RetainPtr<IFX_SeekableStream> IFX_SeekableStream::CreateFromFilename(
     const char* filename,
     uint32_t dwModes) {
-  std::unique_ptr<IFXCRT_FileAccess> pFA(IFXCRT_FileAccess::Create());
+  std::unique_ptr<IFXCRT_FileAccess> pFA = IFXCRT_FileAccess::Create();
   if (!pFA->Open(filename, dwModes))
     return nullptr;
   return pdfium::MakeRetain<CFX_CRTFileStream>(std::move(pFA));
@@ -62,7 +62,7 @@
 CFX_RetainPtr<IFX_SeekableStream> IFX_SeekableStream::CreateFromFilename(
     const wchar_t* filename,
     uint32_t dwModes) {
-  std::unique_ptr<IFXCRT_FileAccess> pFA(IFXCRT_FileAccess::Create());
+  std::unique_ptr<IFXCRT_FileAccess> pFA = IFXCRT_FileAccess::Create();
   if (!pFA->Open(filename, dwModes))
     return nullptr;
   return pdfium::MakeRetain<CFX_CRTFileStream>(std::move(pFA));
diff --git a/core/fxcrt/fxcrt_posix.cpp b/core/fxcrt/fxcrt_posix.cpp
index 562c70c..3afe2c3 100644
--- a/core/fxcrt/fxcrt_posix.cpp
+++ b/core/fxcrt/fxcrt_posix.cpp
@@ -6,15 +6,18 @@
 
 #include "core/fxcrt/fxcrt_posix.h"
 
+#include <memory>
+
 #include "core/fxcrt/fx_basic.h"
+#include "third_party/base/ptr_util.h"
 
 #if _FXM_PLATFORM_ == _FXM_PLATFORM_LINUX_ || \
     _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ || \
     _FXM_PLATFORM_ == _FXM_PLATFORM_ANDROID_
 
 // static
-IFXCRT_FileAccess* IFXCRT_FileAccess::Create() {
-  return new CFXCRT_FileAccess_Posix;
+std::unique_ptr<IFXCRT_FileAccess> IFXCRT_FileAccess::Create() {
+  return pdfium::MakeUnique<CFXCRT_FileAccess_Posix>();
 }
 
 void FXCRT_Posix_GetFileMode(uint32_t dwModes,
diff --git a/core/fxcrt/fxcrt_windows.cpp b/core/fxcrt/fxcrt_windows.cpp
index 6383387..6230c74 100644
--- a/core/fxcrt/fxcrt_windows.cpp
+++ b/core/fxcrt/fxcrt_windows.cpp
@@ -6,13 +6,16 @@
 
 #include "core/fxcrt/fxcrt_windows.h"
 
+#include <memory>
+
 #include "core/fxcrt/fx_string.h"
+#include "third_party/base/ptr_util.h"
 
 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
 
 // static
-IFXCRT_FileAccess* IFXCRT_FileAccess::Create() {
-  return new CFXCRT_FileAccess_Win64;
+std::unique_ptr<IFXCRT_FileAccess> IFXCRT_FileAccess::Create() {
+  return pdfium::MakeUnique<CFXCRT_FileAccess_Win64>();
 }
 
 void FXCRT_Windows_GetFileMode(uint32_t dwMode,
diff --git a/core/fxcrt/ifxcrt_fileaccess.h b/core/fxcrt/ifxcrt_fileaccess.h
index 9528b3b..340c2d1 100644
--- a/core/fxcrt/ifxcrt_fileaccess.h
+++ b/core/fxcrt/ifxcrt_fileaccess.h
@@ -15,7 +15,7 @@
 
 class IFXCRT_FileAccess {
  public:
-  static IFXCRT_FileAccess* Create();
+  static std::unique_ptr<IFXCRT_FileAccess> Create();
   virtual ~IFXCRT_FileAccess() {}
 
   virtual bool Open(const CFX_ByteStringC& fileName, uint32_t dwMode) = 0;