From 24a48881c5407651a58cfd6547ac9b6a9823a63e Mon Sep 17 00:00:00 2001 From: tsepez Date: Mon, 11 Apr 2016 15:18:40 -0700 Subject: Pass CFX_ByteStrings rather than raw ptrs to JS_GlobalData. Helps to avoid re-allocating strings when the caller already has one allocated. Review URL: https://codereview.chromium.org/1876203002 --- fpdfsdk/javascript/JS_GlobalData.cpp | 57 +++++++++++++++++------------------- fpdfsdk/javascript/JS_GlobalData.h | 21 +++++++------ fpdfsdk/javascript/global.cpp | 2 +- fpdfsdk/javascript/global.h | 2 +- 4 files changed, 39 insertions(+), 43 deletions(-) (limited to 'fpdfsdk/javascript') diff --git a/fpdfsdk/javascript/JS_GlobalData.cpp b/fpdfsdk/javascript/JS_GlobalData.cpp index fe3475c2b1..8f7810f111 100644 --- a/fpdfsdk/javascript/JS_GlobalData.cpp +++ b/fpdfsdk/javascript/JS_GlobalData.cpp @@ -94,9 +94,7 @@ static const uint8_t JS_RC4KEY[] = { 0xf8, 0x77, 0xd5, 0xa3}; // Returns true if non-empty, setting sPropName -static bool TrimPropName(const char* propname, CFX_ByteString* sPropName) { - ASSERT(propname); - *sPropName = propname; +static bool TrimPropName(CFX_ByteString* sPropName) { sPropName->TrimLeft(); sPropName->TrimRight(); return sPropName->GetLength() != 0; @@ -130,7 +128,7 @@ CJS_GlobalData::~CJS_GlobalData() { } CJS_GlobalData::iterator CJS_GlobalData::FindGlobalVariable( - const FX_CHAR* propname) { + const CFX_ByteString& propname) { for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); ++it) { if ((*it)->data.sKey == propname) @@ -140,7 +138,7 @@ CJS_GlobalData::iterator CJS_GlobalData::FindGlobalVariable( } CJS_GlobalData::const_iterator CJS_GlobalData::FindGlobalVariable( - const FX_CHAR* propname) const { + const CFX_ByteString& propname) const { for (auto it = m_arrayGlobalData.begin(); it != m_arrayGlobalData.end(); ++it) { if ((*it)->data.sKey == propname) @@ -150,15 +148,15 @@ CJS_GlobalData::const_iterator CJS_GlobalData::FindGlobalVariable( } CJS_GlobalData_Element* CJS_GlobalData::GetGlobalVariable( - const FX_CHAR* propname) { + const CFX_ByteString& propname) { auto iter = FindGlobalVariable(propname); return iter != m_arrayGlobalData.end() ? iter->get() : nullptr; } -void CJS_GlobalData::SetGlobalVariableNumber(const FX_CHAR* propname, +void CJS_GlobalData::SetGlobalVariableNumber(const CFX_ByteString& propname, double dData) { - CFX_ByteString sPropName; - if (!TrimPropName(propname, &sPropName)) + CFX_ByteString sPropName(propname); + if (!TrimPropName(&sPropName)) return; if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { @@ -173,10 +171,10 @@ void CJS_GlobalData::SetGlobalVariableNumber(const FX_CHAR* propname, m_arrayGlobalData.push_back(std::move(pNewData)); } -void CJS_GlobalData::SetGlobalVariableBoolean(const FX_CHAR* propname, +void CJS_GlobalData::SetGlobalVariableBoolean(const CFX_ByteString& propname, bool bData) { - CFX_ByteString sPropName; - if (!TrimPropName(propname, &sPropName)) + CFX_ByteString sPropName(propname); + if (!TrimPropName(&sPropName)) return; if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { @@ -191,10 +189,10 @@ void CJS_GlobalData::SetGlobalVariableBoolean(const FX_CHAR* propname, m_arrayGlobalData.push_back(std::move(pNewData)); } -void CJS_GlobalData::SetGlobalVariableString(const FX_CHAR* propname, +void CJS_GlobalData::SetGlobalVariableString(const CFX_ByteString& propname, const CFX_ByteString& sData) { - CFX_ByteString sPropName; - if (!TrimPropName(propname, &sPropName)) + CFX_ByteString sPropName(propname); + if (!TrimPropName(&sPropName)) return; if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { @@ -210,10 +208,10 @@ void CJS_GlobalData::SetGlobalVariableString(const FX_CHAR* propname, } void CJS_GlobalData::SetGlobalVariableObject( - const FX_CHAR* propname, + const CFX_ByteString& propname, const CJS_GlobalVariableArray& array) { - CFX_ByteString sPropName; - if (!TrimPropName(propname, &sPropName)) + CFX_ByteString sPropName(propname); + if (!TrimPropName(&sPropName)) return; if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { @@ -228,9 +226,9 @@ void CJS_GlobalData::SetGlobalVariableObject( m_arrayGlobalData.push_back(std::move(pNewData)); } -void CJS_GlobalData::SetGlobalVariableNull(const FX_CHAR* propname) { - CFX_ByteString sPropName; - if (!TrimPropName(propname, &sPropName)) +void CJS_GlobalData::SetGlobalVariableNull(const CFX_ByteString& propname) { + CFX_ByteString sPropName(propname); + if (!TrimPropName(&sPropName)) return; if (CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName)) { @@ -243,10 +241,11 @@ void CJS_GlobalData::SetGlobalVariableNull(const FX_CHAR* propname) { m_arrayGlobalData.push_back(std::move(pNewData)); } -FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent(const FX_CHAR* propname, - FX_BOOL bPersistent) { - CFX_ByteString sPropName; - if (!TrimPropName(propname, &sPropName)) +FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent( + const CFX_ByteString& propname, + FX_BOOL bPersistent) { + CFX_ByteString sPropName(propname); + if (!TrimPropName(&sPropName)) return FALSE; CJS_GlobalData_Element* pData = GetGlobalVariable(sPropName); @@ -257,9 +256,9 @@ FX_BOOL CJS_GlobalData::SetGlobalVariablePersistent(const FX_CHAR* propname, return TRUE; } -FX_BOOL CJS_GlobalData::DeleteGlobalVariable(const FX_CHAR* propname) { - CFX_ByteString sPropName; - if (!TrimPropName(propname, &sPropName)) +FX_BOOL CJS_GlobalData::DeleteGlobalVariable(const CFX_ByteString& propname) { + CFX_ByteString sPropName(propname); + if (!TrimPropName(&sPropName)) return FALSE; auto iter = FindGlobalVariable(sPropName); @@ -292,8 +291,6 @@ void CJS_GlobalData::LoadGlobalPersistentVariables() { uint16_t wType = *((uint16_t*)p); p += sizeof(uint16_t); - // uint16_t wTemp = (uint16_t)(('X' << 8) | 'F'); - if (wType == (uint16_t)(('X' << 8) | 'F')) { uint16_t wVersion = *((uint16_t*)p); p += sizeof(uint16_t); diff --git a/fpdfsdk/javascript/JS_GlobalData.h b/fpdfsdk/javascript/JS_GlobalData.h index 65bb921ad9..056f26193f 100644 --- a/fpdfsdk/javascript/JS_GlobalData.h +++ b/fpdfsdk/javascript/JS_GlobalData.h @@ -64,17 +64,16 @@ class CJS_GlobalData { static CJS_GlobalData* GetRetainedInstance(CPDFDoc_Environment* pApp); void Release(); - void SetGlobalVariableNumber(const FX_CHAR* propname, double dData); - void SetGlobalVariableBoolean(const FX_CHAR* propname, bool bData); - void SetGlobalVariableString(const FX_CHAR* propname, + void SetGlobalVariableNumber(const CFX_ByteString& propname, double dData); + void SetGlobalVariableBoolean(const CFX_ByteString& propname, bool bData); + void SetGlobalVariableString(const CFX_ByteString& propname, const CFX_ByteString& sData); - void SetGlobalVariableObject(const FX_CHAR* propname, + void SetGlobalVariableObject(const CFX_ByteString& propname, const CJS_GlobalVariableArray& array); - void SetGlobalVariableNull(const FX_CHAR* propname); - - FX_BOOL SetGlobalVariablePersistent(const FX_CHAR* propname, + void SetGlobalVariableNull(const CFX_ByteString& propname); + FX_BOOL SetGlobalVariablePersistent(const CFX_ByteString& propname, FX_BOOL bPersistent); - FX_BOOL DeleteGlobalVariable(const FX_CHAR* propname); + FX_BOOL DeleteGlobalVariable(const CFX_ByteString& propname); int32_t GetSize() const; CJS_GlobalData_Element* GetAt(int index) const; @@ -93,9 +92,9 @@ class CJS_GlobalData { void LoadGlobalPersistentVariables(); void SaveGlobalPersisitentVariables(); - CJS_GlobalData_Element* GetGlobalVariable(const FX_CHAR* propname); - iterator FindGlobalVariable(const FX_CHAR* propname); - const_iterator FindGlobalVariable(const FX_CHAR* propname) const; + CJS_GlobalData_Element* GetGlobalVariable(const CFX_ByteString& sPropname); + iterator FindGlobalVariable(const CFX_ByteString& sPropname); + const_iterator FindGlobalVariable(const CFX_ByteString& sPropname) const; void LoadFileBuffer(const FX_WCHAR* sFilePath, uint8_t*& pBuffer, diff --git a/fpdfsdk/javascript/global.cpp b/fpdfsdk/javascript/global.cpp index fe974c7af2..d843b60105 100644 --- a/fpdfsdk/javascript/global.cpp +++ b/fpdfsdk/javascript/global.cpp @@ -407,7 +407,7 @@ void JSGlobalAlternate::DestroyGlobalPersisitentVariables() { m_mapGlobal.clear(); } -FX_BOOL JSGlobalAlternate::SetGlobalVariables(const FX_CHAR* propname, +FX_BOOL JSGlobalAlternate::SetGlobalVariables(const CFX_ByteString& propname, int nType, double dData, bool bData, diff --git a/fpdfsdk/javascript/global.h b/fpdfsdk/javascript/global.h index 335b540271..02c51d98a9 100644 --- a/fpdfsdk/javascript/global.h +++ b/fpdfsdk/javascript/global.h @@ -59,7 +59,7 @@ class JSGlobalAlternate : public CJS_EmbedObj { void UpdateGlobalPersistentVariables(); void CommitGlobalPersisitentVariables(IJS_Context* cc); void DestroyGlobalPersisitentVariables(); - FX_BOOL SetGlobalVariables(const FX_CHAR* propname, + FX_BOOL SetGlobalVariables(const CFX_ByteString& propname, int nType, double dData, bool bData, -- cgit v1.2.3