From 35ee5bb4a2e51fd42f07c23a4008bfb97e390b25 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 26 Jan 2017 09:45:57 -0500 Subject: Cleanup memory in CFDE_CSSStyleSelector This CL fixes up the bare new calls in CFDE_CSSStyleSelector and replaces them with unique_ptrs. Code massaged to work correclty with new types. Change-Id: I90fce1ed7486da97fe7b5e597e9d423748c069c0 Reviewed-on: https://pdfium-review.googlesource.com/2353 Commit-Queue: dsinclair Reviewed-by: Tom Sepez --- xfa/fde/css/cfde_cssstyleselector.cpp | 156 +++++++++++---------------------- xfa/fde/css/cfde_cssstyleselector.h | 34 +++---- xfa/fxfa/app/cxfa_textparsecontext.cpp | 17 +--- xfa/fxfa/app/cxfa_textparsecontext.h | 16 ++-- xfa/fxfa/app/cxfa_textparser.cpp | 20 ++--- 5 files changed, 87 insertions(+), 156 deletions(-) diff --git a/xfa/fde/css/cfde_cssstyleselector.cpp b/xfa/fde/css/cfde_cssstyleselector.cpp index 1b4aa0e13b..e09f79c877 100644 --- a/xfa/fde/css/cfde_cssstyleselector.cpp +++ b/xfa/fde/css/cfde_cssstyleselector.cpp @@ -73,44 +73,26 @@ void CFDE_CSSStyleSelector::UpdateStyleIndex() { m_UARules.AddRulesFrom(m_UAStyles.get(), m_pFontMgr); } -int32_t CFDE_CSSStyleSelector::MatchDeclarations( - CXFA_CSSTagProvider* pTag, - CFX_ArrayTemplate& matchedDecls) { +std::vector +CFDE_CSSStyleSelector::MatchDeclarations(CXFA_CSSTagProvider* pTag) { ASSERT(pTag); CFDE_CSSTagCache* pCache = m_pAccelerator->top(); ASSERT(pCache && pCache->GetTag() == pTag); - matchedDecls.RemoveAt(0, matchedDecls.GetSize()); - - if (m_UARules.CountSelectors() == 0) - return 0; - - // The ::Data is owned by the m_RuleCollection. - std::vector matchedRules; - - if (pCache->HashTag()) { - MatchRules(&matchedRules, pCache, - m_UARules.GetTagRuleData(pCache->HashTag())); - } - - for (const auto& rule : matchedRules) - matchedDecls.Add(rule->pDeclaration); + std::vector matchedDecls; + if (m_UARules.CountSelectors() == 0 || !pCache->HashTag()) + return matchedDecls; - return matchedDecls.GetSize(); -} - -void CFDE_CSSStyleSelector::MatchRules( - std::vector* matchedRules, - CFDE_CSSTagCache* pCache, - const std::vector>* pList) { - if (!pList) - return; + auto rules = m_UARules.GetTagRuleData(pCache->HashTag()); + if (!rules) + return matchedDecls; - for (const auto& d : *pList) { + for (const auto& d : *rules) { if (MatchSelector(pCache, d->pSelector)) - matchedRules->push_back(d.get()); + matchedDecls.push_back(d->pDeclaration); } + return matchedDecls; } bool CFDE_CSSStyleSelector::MatchSelector(CFDE_CSSTagCache* pCache, @@ -143,29 +125,27 @@ bool CFDE_CSSStyleSelector::MatchSelector(CFDE_CSSTagCache* pCache, void CFDE_CSSStyleSelector::ComputeStyle( CXFA_CSSTagProvider* pTag, - const CFDE_CSSDeclaration** ppDeclArray, - int32_t iDeclCount, + const std::vector& declArray, CFDE_CSSComputedStyle* pDestStyle) { - ASSERT(iDeclCount >= 0); ASSERT(pDestStyle); static const uint32_t s_dwStyleHash = FX_HashCode_GetW(L"style", true); static const uint32_t s_dwAlignHash = FX_HashCode_GetW(L"align", true); if (!pTag->empty()) { - CFDE_CSSDeclaration* pDecl = nullptr; + std::unique_ptr pDecl; for (auto it : *pTag) { CFX_WideString wsAttri = it.first; CFX_WideString wsValue = it.second; uint32_t dwAttriHash = FX_HashCode_GetW(wsAttri.AsStringC(), true); if (dwAttriHash == s_dwStyleHash) { if (!pDecl) - pDecl = new CFDE_CSSDeclaration; + pDecl = pdfium::MakeUnique(); - AppendInlineStyle(pDecl, wsValue.c_str(), wsValue.GetLength()); + AppendInlineStyle(pDecl.get(), wsValue.c_str(), wsValue.GetLength()); } else if (dwAttriHash == s_dwAlignHash) { if (!pDecl) - pDecl = new CFDE_CSSDeclaration; + pDecl = pdfium::MakeUnique(); FDE_CSSPropertyArgs args; args.pStringCache = nullptr; @@ -175,89 +155,53 @@ void CFDE_CSSStyleSelector::ComputeStyle( } if (pDecl) { - CFX_ArrayTemplate decls; - decls.SetSize(iDeclCount + 1); - CFDE_CSSDeclaration** ppInline = decls.GetData(); - FXSYS_memcpy(ppInline, ppDeclArray, - iDeclCount * sizeof(CFDE_CSSDeclaration*)); - ppInline[iDeclCount++] = pDecl; - ApplyDeclarations(true, const_cast(ppInline), - iDeclCount, pDestStyle); - ApplyDeclarations(false, - const_cast(ppInline), - iDeclCount, pDestStyle); + ApplyDeclarations(declArray, pDecl.get(), pDestStyle); return; } } - if (iDeclCount > 0) { - ASSERT(ppDeclArray); + if (declArray.empty()) + return; - ApplyDeclarations(true, ppDeclArray, iDeclCount, pDestStyle); - ApplyDeclarations(false, ppDeclArray, iDeclCount, pDestStyle); - } + ApplyDeclarations(declArray, nullptr, pDestStyle); } void CFDE_CSSStyleSelector::ApplyDeclarations( - bool bPriority, - const CFDE_CSSDeclaration** ppDeclArray, - int32_t iDeclCount, + const std::vector& declArray, + const CFDE_CSSDeclaration* extraDecl, CFDE_CSSComputedStyle* pDestStyle) { CFDE_CSSComputedStyle* pComputedStyle = pDestStyle; - int32_t i; - if (bPriority) { - CFDE_CSSValue* pLastest = nullptr; - CFDE_CSSValue* pImportant = nullptr; - for (i = 0; i < iDeclCount; ++i) { - bool bImportant; - CFDE_CSSValue* pVal = - ppDeclArray[i]->GetProperty(FDE_CSSProperty::FontSize, bImportant); - if (!pVal) - continue; - - if (bImportant) - pImportant = pVal; - else - pLastest = pVal; - } - if (pImportant) { - ApplyProperty(FDE_CSSProperty::FontSize, pImportant, pComputedStyle); - } else if (pLastest) { - ApplyProperty(FDE_CSSProperty::FontSize, pLastest, pComputedStyle); - } - } else { - CFX_ArrayTemplate importants; - const CFDE_CSSDeclaration* pDecl = nullptr; - - for (i = 0; i < iDeclCount; ++i) { - pDecl = ppDeclArray[i]; - for (auto it = pDecl->begin(); it != pDecl->end(); it++) { - if ((*it)->eProperty == FDE_CSSProperty::FontSize) - continue; - if (!(*it)->bImportant) { - ApplyProperty((*it)->eProperty, (*it)->pValue.Get(), pComputedStyle); - } else if (importants.GetSize() == 0 || - importants[importants.GetUpperBound()] != pDecl) { - importants.Add(const_cast(pDecl)); - } - } - } - - iDeclCount = importants.GetSize(); - for (i = 0; i < iDeclCount; ++i) { - pDecl = importants[i]; - - for (auto it = pDecl->begin(); it != pDecl->end(); it++) { - if ((*it)->bImportant && (*it)->eProperty != FDE_CSSProperty::FontSize) - ApplyProperty((*it)->eProperty, (*it)->pValue.Get(), pComputedStyle); - } - } + std::vector importants; + std::vector normals; + std::vector customs; + + for (auto& decl : declArray) + ExtractValues(decl, &importants, &normals, &customs); + if (extraDecl) + ExtractValues(extraDecl, &importants, &normals, &customs); + + for (auto& prop : normals) + ApplyProperty(prop->eProperty, prop->pValue.Get(), pComputedStyle); + for (auto& prop : customs) + pComputedStyle->AddCustomStyle(prop->pwsName, prop->pwsValue); + for (auto& prop : importants) + ApplyProperty(prop->eProperty, prop->pValue.Get(), pComputedStyle); +} - for (auto it = pDecl->custom_begin(); it != pDecl->custom_end(); it++) { - pComputedStyle->AddCustomStyle((*it)->pwsName, (*it)->pwsValue); - } +void CFDE_CSSStyleSelector::ExtractValues( + const CFDE_CSSDeclaration* decl, + std::vector* importants, + std::vector* normals, + std::vector* custom) { + for (const auto& holder : *decl) { + if (holder->bImportant) + importants->push_back(holder.get()); + else + normals->push_back(holder.get()); } + for (auto it = decl->custom_begin(); it != decl->custom_end(); it++) + custom->push_back(it->get()); } void CFDE_CSSStyleSelector::AppendInlineStyle(CFDE_CSSDeclaration* pDecl, diff --git a/xfa/fde/css/cfde_cssstyleselector.h b/xfa/fde/css/cfde_cssstyleselector.h index c2262d7aa7..15bce93334 100644 --- a/xfa/fde/css/cfde_cssstyleselector.h +++ b/xfa/fde/css/cfde_cssstyleselector.h @@ -17,7 +17,9 @@ class CFDE_CSSAccelerator; class CFDE_CSSComputedStyle; +class CFDE_CSSCustomProperty; class CFDE_CSSDeclaration; +class CFDE_CSSPropertyHolder; class CFDE_CSSSelector; class CFDE_CSSStyleSheet; class CFDE_CSSTagCache; @@ -31,38 +33,38 @@ class CFDE_CSSStyleSelector { explicit CFDE_CSSStyleSelector(CFGAS_FontMgr* pFontMgr); ~CFDE_CSSStyleSelector(); - void SetDefFontSize(FX_FLOAT fFontSize); + CFDE_CSSAccelerator* InitAccelerator(); + void SetDefFontSize(FX_FLOAT fFontSize); void SetUAStyleSheet(std::unique_ptr pSheet); - void UpdateStyleIndex(); - CFDE_CSSAccelerator* InitAccelerator(); + CFX_RetainPtr CreateComputedStyle( CFDE_CSSComputedStyle* pParentStyle); - int32_t MatchDeclarations( - CXFA_CSSTagProvider* pTag, - CFX_ArrayTemplate& matchedDecls); void ComputeStyle(CXFA_CSSTagProvider* pTag, - const CFDE_CSSDeclaration** ppDeclArray, - int32_t iDeclCount, + const std::vector& declArray, CFDE_CSSComputedStyle* pDestStyle); + std::vector MatchDeclarations( + CXFA_CSSTagProvider* pTag); + private: - void MatchRules( - std::vector* matchedRules, - CFDE_CSSTagCache* pCache, - const std::vector>* pList); bool MatchSelector(CFDE_CSSTagCache* pCache, CFDE_CSSSelector* pSel); + void AppendInlineStyle(CFDE_CSSDeclaration* pDecl, const FX_WCHAR* psz, int32_t iLen); - void ApplyDeclarations(bool bPriority, - const CFDE_CSSDeclaration** ppDeclArray, - int32_t iDeclCount, - CFDE_CSSComputedStyle* pDestStyle); + void ApplyDeclarations( + const std::vector& declArray, + const CFDE_CSSDeclaration* extraDecl, + CFDE_CSSComputedStyle* pDestStyle); void ApplyProperty(FDE_CSSProperty eProperty, CFDE_CSSValue* pValue, CFDE_CSSComputedStyle* pComputedStyle); + void ExtractValues(const CFDE_CSSDeclaration* decl, + std::vector* importants, + std::vector* normals, + std::vector* custom); bool SetLengthWithPercent(FDE_CSSLength& width, FDE_CSSPrimitiveType eType, diff --git a/xfa/fxfa/app/cxfa_textparsecontext.cpp b/xfa/fxfa/app/cxfa_textparsecontext.cpp index e4beb1fcb0..851d84456b 100644 --- a/xfa/fxfa/app/cxfa_textparsecontext.cpp +++ b/xfa/fxfa/app/cxfa_textparsecontext.cpp @@ -12,21 +12,6 @@ CXFA_TextParseContext::CXFA_TextParseContext() : m_pParentStyle(nullptr), - m_ppMatchedDecls(nullptr), - m_dwMatchedDecls(0), m_eDisplay(FDE_CSSDisplay::None) {} -CXFA_TextParseContext::~CXFA_TextParseContext() { - FX_Free(m_ppMatchedDecls); -} - -void CXFA_TextParseContext::SetDecls(const CFDE_CSSDeclaration** ppDeclArray, - int32_t iDeclCount) { - if (iDeclCount <= 0 || !ppDeclArray) - return; - - m_dwMatchedDecls = iDeclCount; - m_ppMatchedDecls = FX_Alloc(CFDE_CSSDeclaration*, iDeclCount); - FXSYS_memcpy(m_ppMatchedDecls, ppDeclArray, - iDeclCount * sizeof(CFDE_CSSDeclaration*)); -} +CXFA_TextParseContext::~CXFA_TextParseContext() {} diff --git a/xfa/fxfa/app/cxfa_textparsecontext.h b/xfa/fxfa/app/cxfa_textparsecontext.h index d13f35b6df..5ea68e9473 100644 --- a/xfa/fxfa/app/cxfa_textparsecontext.h +++ b/xfa/fxfa/app/cxfa_textparsecontext.h @@ -7,9 +7,13 @@ #ifndef XFA_FXFA_APP_CXFA_TEXTPARSECONTEXT_H_ #define XFA_FXFA_APP_CXFA_TEXTPARSECONTEXT_H_ +#include +#include + +#include "third_party/base/stl_util.h" +#include "xfa/fde/css/cfde_cssdeclaration.h" #include "xfa/fde/css/fde_css.h" -class CFDE_CSSDeclaration; class CFDE_CSSComputedStyle; class CXFA_TextParseContext { @@ -20,17 +24,15 @@ class CXFA_TextParseContext { void SetDisplay(FDE_CSSDisplay eDisplay) { m_eDisplay = eDisplay; } FDE_CSSDisplay GetDisplay() const { return m_eDisplay; } - void SetDecls(const CFDE_CSSDeclaration** ppDeclArray, int32_t iDeclCount); - const CFDE_CSSDeclaration** GetDecls() { - return const_cast(m_ppMatchedDecls); + void SetDecls(std::vector&& decl) { + decls_ = std::move(decl); } - uint32_t CountDecls() const { return m_dwMatchedDecls; } + const std::vector& GetDecls() { return decls_; } CFX_RetainPtr m_pParentStyle; protected: - CFDE_CSSDeclaration** m_ppMatchedDecls; - uint32_t m_dwMatchedDecls; + std::vector decls_; FDE_CSSDisplay m_eDisplay; }; diff --git a/xfa/fxfa/app/cxfa_textparser.cpp b/xfa/fxfa/app/cxfa_textparser.cpp index 9bf033f9b0..2a724cc243 100644 --- a/xfa/fxfa/app/cxfa_textparser.cpp +++ b/xfa/fxfa/app/cxfa_textparser.cpp @@ -8,6 +8,7 @@ #include #include +#include #include "third_party/base/ptr_util.h" #include "xfa/fde/css/cfde_cssaccelerator.h" @@ -205,8 +206,8 @@ CFX_RetainPtr CXFA_TextParser::ComputeStyle( auto pStyle = CreateStyle(pParentStyle); CFDE_CSSAccelerator* pCSSAccel = m_pSelector->InitAccelerator(); pCSSAccel->OnEnterTag(&tagProvider); - m_pSelector->ComputeStyle(&tagProvider, pContext->GetDecls(), - pContext->CountDecls(), pStyle.Get()); + + m_pSelector->ComputeStyle(&tagProvider, pContext->GetDecls(), pStyle.Get()); pCSSAccel->OnLeaveTag(&tagProvider); return pStyle; } @@ -241,16 +242,13 @@ void CXFA_TextParser::ParseRichText(CFDE_XMLNode* pXMLNode, pNewStyle = CreateStyle(pParentStyle); CFDE_CSSAccelerator* pCSSAccel = m_pSelector->InitAccelerator(); pCSSAccel->OnEnterTag(&tagProvider); - CFX_ArrayTemplate DeclArray; - int32_t iMatchedDecls = - m_pSelector->MatchDeclarations(&tagProvider, DeclArray); - const CFDE_CSSDeclaration** ppMatchDecls = - const_cast(DeclArray.GetData()); - m_pSelector->ComputeStyle(&tagProvider, ppMatchDecls, iMatchedDecls, - pNewStyle.Get()); + + auto declArray = m_pSelector->MatchDeclarations(&tagProvider); + m_pSelector->ComputeStyle(&tagProvider, declArray, pNewStyle.Get()); + pCSSAccel->OnLeaveTag(&tagProvider); - if (iMatchedDecls > 0) - pTextContext->SetDecls(ppMatchDecls, iMatchedDecls); + if (!declArray.empty()) + pTextContext->SetDecls(std::move(declArray)); eDisplay = pNewStyle->GetDisplay(); } -- cgit v1.2.3