Make CPDF_Dictionary use unique pointers.

Some changes were required to match underlying ctors
as invoked by the templated methods.

Many release() calls go away, a few WrapUniques() are
introduced to avoid going deeper into other code.

Review-Url: https://codereview.chromium.org/2510223002
diff --git a/fpdfsdk/cpdfsdk_baannot.cpp b/fpdfsdk/cpdfsdk_baannot.cpp
index ee4b55d..9fb1e88 100644
--- a/fpdfsdk/cpdfsdk_baannot.cpp
+++ b/fpdfsdk/cpdfsdk_baannot.cpp
@@ -6,10 +6,15 @@
 
 #include "fpdfsdk/cpdfsdk_baannot.h"
 
+#include <algorithm>
+
 #include "core/fpdfapi/parser/cpdf_array.h"
 #include "core/fpdfapi/parser/cpdf_document.h"
+#include "core/fpdfapi/parser/cpdf_name.h"
 #include "core/fpdfapi/parser/cpdf_number.h"
+#include "core/fpdfapi/parser/cpdf_reference.h"
 #include "core/fpdfapi/parser/cpdf_stream.h"
+#include "core/fpdfapi/parser/cpdf_string.h"
 #include "core/fpdfapi/parser/fpdf_parser_decode.h"
 #include "fpdfsdk/cpdfsdk_datetime.h"
 #include "fpdfsdk/cpdfsdk_pageview.h"
@@ -89,11 +94,12 @@
 }
 
 void CPDFSDK_BAAnnot::SetContents(const CFX_WideString& sContents) {
-  if (sContents.IsEmpty())
+  if (sContents.IsEmpty()) {
     m_pAnnot->GetAnnotDict()->RemoveFor("Contents");
-  else
-    m_pAnnot->GetAnnotDict()->SetStringFor("Contents",
-                                           PDF_EncodeText(sContents));
+  } else {
+    m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_String>(
+        "Contents", PDF_EncodeText(sContents), false);
+  }
 }
 
 CFX_WideString CPDFSDK_BAAnnot::GetContents() const {
@@ -101,10 +107,12 @@
 }
 
 void CPDFSDK_BAAnnot::SetAnnotName(const CFX_WideString& sName) {
-  if (sName.IsEmpty())
+  if (sName.IsEmpty()) {
     m_pAnnot->GetAnnotDict()->RemoveFor("NM");
-  else
-    m_pAnnot->GetAnnotDict()->SetStringFor("NM", PDF_EncodeText(sName));
+  } else {
+    m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_String>(
+        "NM", PDF_EncodeText(sName), false);
+  }
 }
 
 CFX_WideString CPDFSDK_BAAnnot::GetAnnotName() const {
@@ -114,25 +122,23 @@
 void CPDFSDK_BAAnnot::SetModifiedDate(const FX_SYSTEMTIME& st) {
   CPDFSDK_DateTime dt(st);
   CFX_ByteString str = dt.ToPDFDateTimeString();
-
   if (str.IsEmpty())
     m_pAnnot->GetAnnotDict()->RemoveFor("M");
   else
-    m_pAnnot->GetAnnotDict()->SetStringFor("M", str);
+    m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_String>("M", str, false);
 }
 
 FX_SYSTEMTIME CPDFSDK_BAAnnot::GetModifiedDate() const {
   FX_SYSTEMTIME systime;
   CFX_ByteString str = m_pAnnot->GetAnnotDict()->GetStringFor("M");
-
   CPDFSDK_DateTime dt(str);
   dt.ToSystemTime(systime);
-
   return systime;
 }
 
 void CPDFSDK_BAAnnot::SetFlags(uint32_t nFlags) {
-  m_pAnnot->GetAnnotDict()->SetIntegerFor("F", nFlags);
+  m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Number>("F",
+                                                   static_cast<int>(nFlags));
 }
 
 uint32_t CPDFSDK_BAAnnot::GetFlags() const {
@@ -143,7 +149,7 @@
   if (str.IsEmpty())
     m_pAnnot->GetAnnotDict()->RemoveFor("AS");
   else
-    m_pAnnot->GetAnnotDict()->SetStringFor("AS", str);
+    m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_String>("AS", str, false);
 }
 
 CFX_ByteString CPDFSDK_BAAnnot::GetAppState() const {
@@ -151,7 +157,7 @@
 }
 
 void CPDFSDK_BAAnnot::SetStructParent(int key) {
-  m_pAnnot->GetAnnotDict()->SetIntegerFor("StructParent", key);
+  m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Number>("StructParent", key);
 }
 
 int CPDFSDK_BAAnnot::GetStructParent() const {
@@ -165,12 +171,10 @@
     pBorder->SetNewAt<CPDF_Number>(2, nWidth);
   } else {
     CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictFor("BS");
-    if (!pBSDict) {
-      pBSDict =
-          new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool());
-      m_pAnnot->GetAnnotDict()->SetFor("BS", pBSDict);
-    }
-    pBSDict->SetIntegerFor("W", nWidth);
+    if (!pBSDict)
+      pBSDict = m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Dictionary>("BS");
+
+    pBSDict->SetNewFor<CPDF_Number>("W", nWidth);
   }
 }
 
@@ -186,26 +190,24 @@
 
 void CPDFSDK_BAAnnot::SetBorderStyle(BorderStyle nStyle) {
   CPDF_Dictionary* pBSDict = m_pAnnot->GetAnnotDict()->GetDictFor("BS");
-  if (!pBSDict) {
-    pBSDict = new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool());
-    m_pAnnot->GetAnnotDict()->SetFor("BS", pBSDict);
-  }
+  if (!pBSDict)
+    pBSDict = m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Dictionary>("BS");
 
   switch (nStyle) {
     case BorderStyle::SOLID:
-      pBSDict->SetNameFor("S", "S");
+      pBSDict->SetNewFor<CPDF_Name>("S", "S");
       break;
     case BorderStyle::DASH:
-      pBSDict->SetNameFor("S", "D");
+      pBSDict->SetNewFor<CPDF_Name>("S", "D");
       break;
     case BorderStyle::BEVELED:
-      pBSDict->SetNameFor("S", "B");
+      pBSDict->SetNewFor<CPDF_Name>("S", "B");
       break;
     case BorderStyle::INSET:
-      pBSDict->SetNameFor("S", "I");
+      pBSDict->SetNewFor<CPDF_Name>("S", "I");
       break;
     case BorderStyle::UNDERLINE:
-      pBSDict->SetNameFor("S", "U");
+      pBSDict->SetNewFor<CPDF_Name>("S", "U");
       break;
     default:
       break;
@@ -241,14 +243,13 @@
 }
 
 void CPDFSDK_BAAnnot::SetColor(FX_COLORREF color) {
-  CPDF_Array* pArray = new CPDF_Array;
+  CPDF_Array* pArray = m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Array>("C");
   pArray->AddNew<CPDF_Number>(static_cast<FX_FLOAT>(FXSYS_GetRValue(color)) /
                               255.0f);
   pArray->AddNew<CPDF_Number>(static_cast<FX_FLOAT>(FXSYS_GetGValue(color)) /
                               255.0f);
   pArray->AddNew<CPDF_Number>(static_cast<FX_FLOAT>(FXSYS_GetBValue(color)) /
                               255.0f);
-  m_pAnnot->GetAnnotDict()->SetFor("C", pArray);
 }
 
 void CPDFSDK_BAAnnot::RemoveColor() {
@@ -297,10 +298,8 @@
                                       const CFX_ByteString& sContents,
                                       const CFX_ByteString& sAPState) {
   CPDF_Dictionary* pAPDict = m_pAnnot->GetAnnotDict()->GetDictFor("AP");
-  if (!pAPDict) {
-    pAPDict = new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool());
-    m_pAnnot->GetAnnotDict()->SetFor("AP", pAPDict);
-  }
+  if (!pAPDict)
+    pAPDict = m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Dictionary>("AP");
 
   CPDF_Stream* pStream = nullptr;
   CPDF_Dictionary* pParentDict = nullptr;
@@ -309,11 +308,9 @@
     pStream = pAPDict->GetStreamFor(sAPType);
   } else {
     CPDF_Dictionary* pAPTypeDict = pAPDict->GetDictFor(sAPType);
-    if (!pAPTypeDict) {
-      pAPTypeDict =
-          new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool());
-      pAPDict->SetFor(sAPType, pAPTypeDict);
-    }
+    if (!pAPTypeDict)
+      pAPTypeDict = pAPDict->SetNewFor<CPDF_Dictionary>(sAPType);
+
     pParentDict = pAPTypeDict;
     pStream = pAPTypeDict->GetStreamFor(sAPState);
   }
@@ -321,16 +318,16 @@
   if (!pStream) {
     CPDF_Document* pDoc = m_pPageView->GetPDFDocument();
     pStream = pDoc->NewIndirect<CPDF_Stream>();
-    pParentDict->SetReferenceFor(sAPType, pDoc, pStream);
+    pParentDict->SetNewFor<CPDF_Reference>(sAPType, pDoc, pStream->GetObjNum());
   }
 
   CPDF_Dictionary* pStreamDict = pStream->GetDict();
   if (!pStreamDict) {
     pStreamDict =
         new CPDF_Dictionary(m_pAnnot->GetDocument()->GetByteStringPool());
-    pStreamDict->SetNameFor("Type", "XObject");
-    pStreamDict->SetNameFor("Subtype", "Form");
-    pStreamDict->SetIntegerFor("FormType", 1);
+    pStreamDict->SetNewFor<CPDF_Name>("Type", "XObject");
+    pStreamDict->SetNewFor<CPDF_Name>("Subtype", "Form");
+    pStreamDict->SetNewFor<CPDF_Number>("FormType", 1);
     pStream->InitStream(nullptr, 0, pStreamDict);
   }
 
@@ -358,7 +355,8 @@
     CPDF_Document* pDoc = m_pPageView->GetPDFDocument();
     if (pDict->IsInline())
       pDict = pDoc->AddIndirectObject(pDict->Clone())->AsDictionary();
-    m_pAnnot->GetAnnotDict()->SetReferenceFor("A", pDoc, pDict);
+    m_pAnnot->GetAnnotDict()->SetNewFor<CPDF_Reference>("A", pDoc,
+                                                        pDict->GetObjNum());
   }
 }
 
@@ -372,7 +370,7 @@
 
 void CPDFSDK_BAAnnot::SetAAction(const CPDF_AAction& aa) {
   if (aa.GetDict() != m_pAnnot->GetAnnotDict()->GetDictFor("AA"))
-    m_pAnnot->GetAnnotDict()->SetFor("AA", aa.GetDict());
+    m_pAnnot->GetAnnotDict()->SetFor("AA", pdfium::WrapUnique(aa.GetDict()));
 }
 
 void CPDFSDK_BAAnnot::RemoveAAction() {
@@ -381,7 +379,6 @@
 
 CPDF_Action CPDFSDK_BAAnnot::GetAAction(CPDF_AAction::AActionType eAAT) {
   CPDF_AAction AAction = GetAAction();
-
   if (AAction.ActionExist(eAAT))
     return AAction.GetAction(eAAT);