From 0e606b5ecd6e45f74391f110cc1fe0cce0e80c64 Mon Sep 17 00:00:00 2001 From: tsepez Date: Fri, 18 Nov 2016 16:22:41 -0800 Subject: 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 --- fpdfsdk/cpdfsdk_baannot.cpp | 91 ++++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 47 deletions(-) (limited to 'fpdfsdk/cpdfsdk_baannot.cpp') diff --git a/fpdfsdk/cpdfsdk_baannot.cpp b/fpdfsdk/cpdfsdk_baannot.cpp index ee4b55d290..9fb1e88a84 100644 --- a/fpdfsdk/cpdfsdk_baannot.cpp +++ b/fpdfsdk/cpdfsdk_baannot.cpp @@ -6,10 +6,15 @@ #include "fpdfsdk/cpdfsdk_baannot.h" +#include + #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::ClearCachedAP() { } 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( + "Contents", PDF_EncodeText(sContents), false); + } } CFX_WideString CPDFSDK_BAAnnot::GetContents() const { @@ -101,10 +107,12 @@ CFX_WideString CPDFSDK_BAAnnot::GetContents() const { } 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( + "NM", PDF_EncodeText(sName), false); + } } CFX_WideString CPDFSDK_BAAnnot::GetAnnotName() const { @@ -114,25 +122,23 @@ CFX_WideString CPDFSDK_BAAnnot::GetAnnotName() const { 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("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("F", + static_cast(nFlags)); } uint32_t CPDFSDK_BAAnnot::GetFlags() const { @@ -143,7 +149,7 @@ void CPDFSDK_BAAnnot::SetAppState(const CFX_ByteString& str) { if (str.IsEmpty()) m_pAnnot->GetAnnotDict()->RemoveFor("AS"); else - m_pAnnot->GetAnnotDict()->SetStringFor("AS", str); + m_pAnnot->GetAnnotDict()->SetNewFor("AS", str, false); } CFX_ByteString CPDFSDK_BAAnnot::GetAppState() const { @@ -151,7 +157,7 @@ CFX_ByteString CPDFSDK_BAAnnot::GetAppState() const { } void CPDFSDK_BAAnnot::SetStructParent(int key) { - m_pAnnot->GetAnnotDict()->SetIntegerFor("StructParent", key); + m_pAnnot->GetAnnotDict()->SetNewFor("StructParent", key); } int CPDFSDK_BAAnnot::GetStructParent() const { @@ -165,12 +171,10 @@ void CPDFSDK_BAAnnot::SetBorderWidth(int nWidth) { pBorder->SetNewAt(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("BS"); + + pBSDict->SetNewFor("W", nWidth); } } @@ -186,26 +190,24 @@ int CPDFSDK_BAAnnot::GetBorderWidth() const { 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("BS"); switch (nStyle) { case BorderStyle::SOLID: - pBSDict->SetNameFor("S", "S"); + pBSDict->SetNewFor("S", "S"); break; case BorderStyle::DASH: - pBSDict->SetNameFor("S", "D"); + pBSDict->SetNewFor("S", "D"); break; case BorderStyle::BEVELED: - pBSDict->SetNameFor("S", "B"); + pBSDict->SetNewFor("S", "B"); break; case BorderStyle::INSET: - pBSDict->SetNameFor("S", "I"); + pBSDict->SetNewFor("S", "I"); break; case BorderStyle::UNDERLINE: - pBSDict->SetNameFor("S", "U"); + pBSDict->SetNewFor("S", "U"); break; default: break; @@ -241,14 +243,13 @@ BorderStyle CPDFSDK_BAAnnot::GetBorderStyle() const { } void CPDFSDK_BAAnnot::SetColor(FX_COLORREF color) { - CPDF_Array* pArray = new CPDF_Array; + CPDF_Array* pArray = m_pAnnot->GetAnnotDict()->SetNewFor("C"); pArray->AddNew(static_cast(FXSYS_GetRValue(color)) / 255.0f); pArray->AddNew(static_cast(FXSYS_GetGValue(color)) / 255.0f); pArray->AddNew(static_cast(FXSYS_GetBValue(color)) / 255.0f); - m_pAnnot->GetAnnotDict()->SetFor("C", pArray); } void CPDFSDK_BAAnnot::RemoveColor() { @@ -297,10 +298,8 @@ void CPDFSDK_BAAnnot::WriteAppearance(const CFX_ByteString& sAPType, 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("AP"); CPDF_Stream* pStream = nullptr; CPDF_Dictionary* pParentDict = nullptr; @@ -309,11 +308,9 @@ void CPDFSDK_BAAnnot::WriteAppearance(const CFX_ByteString& sAPType, 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(sAPType); + pParentDict = pAPTypeDict; pStream = pAPTypeDict->GetStreamFor(sAPState); } @@ -321,16 +318,16 @@ void CPDFSDK_BAAnnot::WriteAppearance(const CFX_ByteString& sAPType, if (!pStream) { CPDF_Document* pDoc = m_pPageView->GetPDFDocument(); pStream = pDoc->NewIndirect(); - pParentDict->SetReferenceFor(sAPType, pDoc, pStream); + pParentDict->SetNewFor(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("Type", "XObject"); + pStreamDict->SetNewFor("Subtype", "Form"); + pStreamDict->SetNewFor("FormType", 1); pStream->InitStream(nullptr, 0, pStreamDict); } @@ -358,7 +355,8 @@ void CPDFSDK_BAAnnot::SetAction(const CPDF_Action& action) { 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("A", pDoc, + pDict->GetObjNum()); } } @@ -372,7 +370,7 @@ CPDF_AAction CPDFSDK_BAAnnot::GetAAction() const { 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 @@ void CPDFSDK_BAAnnot::RemoveAAction() { CPDF_Action CPDFSDK_BAAnnot::GetAAction(CPDF_AAction::AActionType eAAT) { CPDF_AAction AAction = GetAAction(); - if (AAction.ActionExist(eAAT)) return AAction.GetAction(eAAT); -- cgit v1.2.3