From 41a53ad771411db3cdd98822f9d240456767fba8 Mon Sep 17 00:00:00 2001 From: tsepez Date: Mon, 28 Mar 2016 16:59:30 -0700 Subject: use std::vector in more places in JavaScript bindings code. use unique_ptrs for app:m_Timers. BUG= Review URL: https://codereview.chromium.org/1834203002 --- fpdfsdk/javascript/app.cpp | 133 +++++++++++++++++++-------------------------- 1 file changed, 55 insertions(+), 78 deletions(-) (limited to 'fpdfsdk/javascript/app.cpp') diff --git a/fpdfsdk/javascript/app.cpp b/fpdfsdk/javascript/app.cpp index 41fa15259a..83836759d7 100644 --- a/fpdfsdk/javascript/app.cpp +++ b/fpdfsdk/javascript/app.cpp @@ -32,7 +32,7 @@ END_JS_STATIC_METHOD() IMPLEMENT_JS_CLASS(CJS_TimerObj, TimerObj) TimerObj::TimerObj(CJS_Object* pJSObject) - : CJS_EmbedObj(pJSObject), m_pTimer(NULL) {} + : CJS_EmbedObj(pJSObject), m_pTimer(nullptr) {} TimerObj::~TimerObj() {} @@ -102,10 +102,6 @@ app::app(CJS_Object* pJSObject) : CJS_EmbedObj(pJSObject), m_bCalculate(true), m_bRuntimeHighLight(false) {} app::~app() { - for (int i = 0, sz = m_aTimer.GetSize(); i < sz; i++) - delete m_aTimer[i]; - - m_aTimer.RemoveAll(); } FX_BOOL app::activeDocs(IJS_Context* cc, @@ -120,7 +116,7 @@ FX_BOOL app::activeDocs(IJS_Context* cc, CPDFSDK_Document* pCurDoc = pContext->GetReaderDocument(); CJS_Array aDocs(pRuntime); if (CPDFSDK_Document* pDoc = pApp->GetSDKDocument()) { - CJS_Document* pJSDocument = NULL; + CJS_Document* pJSDocument = nullptr; if (pDoc == pCurDoc) { v8::Local pObj = FXJS_GetThisObj(pRuntime->GetIsolate()); if (FXJS_GetObjDefnID(pObj) == CJS_Document::g_nObjDefnID) @@ -384,19 +380,16 @@ FX_BOOL app::setInterval(IJS_Context* cc, CJS_Runtime* pRuntime = pContext->GetJSRuntime(); uint32_t dwInterval = params.size() > 1 ? params[1].ToInt() : 1000; - CPDFDoc_Environment* pApp = pRuntime->GetReaderApp(); - ASSERT(pApp); - CJS_Timer* pTimer = - new CJS_Timer(this, pApp, pRuntime, 0, script, dwInterval, 0); - m_aTimer.Add(pTimer); + m_Timers.push_back(std::unique_ptr( + new CJS_Timer(this, pApp, pRuntime, 0, script, dwInterval, 0))); v8::Local pRetObj = FXJS_NewFxDynamicObj( pRuntime->GetIsolate(), pRuntime, CJS_TimerObj::g_nObjDefnID); - CJS_TimerObj* pJS_TimerObj = - (CJS_TimerObj*)FXJS_GetPrivate(pRuntime->GetIsolate(), pRetObj); - TimerObj* pTimerObj = (TimerObj*)pJS_TimerObj->GetEmbedObject(); - pTimerObj->SetTimer(pTimer); + CJS_TimerObj* pJS_TimerObj = static_cast( + FXJS_GetPrivate(pRuntime->GetIsolate(), pRetObj)); + TimerObj* pTimerObj = static_cast(pJS_TimerObj->GetEmbedObject()); + pTimerObj->SetTimer(m_Timers.back().get()); vRet = pRetObj; return TRUE; @@ -406,36 +399,32 @@ FX_BOOL app::setTimeOut(IJS_Context* cc, const std::vector& params, CJS_Value& vRet, CFX_WideString& sError) { + CJS_Context* pContext = static_cast(cc); if (params.size() > 2 || params.size() == 0) { - sError = JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSPARAMERROR); + sError = JSGetStringFromID(pContext, IDS_STRING_JSPARAMERROR); return FALSE; } - CJS_Context* pContext = (CJS_Context*)cc; - CJS_Runtime* pRuntime = pContext->GetJSRuntime(); - - CFX_WideString script = params.size() > 0 ? params[0].ToCFXWideString() : L""; + CFX_WideString script = params[0].ToCFXWideString(); if (script.IsEmpty()) { - sError = - JSGetStringFromID((CJS_Context*)cc, IDS_STRING_JSAFNUMBER_KEYSTROKE); + sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE); return TRUE; } uint32_t dwTimeOut = params.size() > 1 ? params[1].ToInt() : 1000; - + CJS_Runtime* pRuntime = pContext->GetJSRuntime(); CPDFDoc_Environment* pApp = pRuntime->GetReaderApp(); - ASSERT(pApp); - - CJS_Timer* pTimer = - new CJS_Timer(this, pApp, pRuntime, 1, script, dwTimeOut, dwTimeOut); - m_aTimer.Add(pTimer); + m_Timers.push_back(std::unique_ptr( + new CJS_Timer(this, pApp, pRuntime, 1, script, dwTimeOut, dwTimeOut))); v8::Local pRetObj = FXJS_NewFxDynamicObj( pRuntime->GetIsolate(), pRuntime, CJS_TimerObj::g_nObjDefnID); - CJS_TimerObj* pJS_TimerObj = - (CJS_TimerObj*)FXJS_GetPrivate(pRuntime->GetIsolate(), pRetObj); - TimerObj* pTimerObj = (TimerObj*)pJS_TimerObj->GetEmbedObject(); - pTimerObj->SetTimer(pTimer); + + CJS_TimerObj* pJS_TimerObj = static_cast( + FXJS_GetPrivate(pRuntime->GetIsolate(), pRetObj)); + + TimerObj* pTimerObj = static_cast(pJS_TimerObj->GetEmbedObject()); + pTimerObj->SetTimer(m_Timers.back().get()); vRet = pRetObj; return TRUE; @@ -451,29 +440,7 @@ FX_BOOL app::clearTimeOut(IJS_Context* cc, return FALSE; } - if (params[0].GetType() == CJS_Value::VT_fxobject) { - v8::Local pObj = params[0].ToV8Object(); - if (FXJS_GetObjDefnID(pObj) == CJS_TimerObj::g_nObjDefnID) { - if (CJS_Object* pJSObj = params[0].ToCJSObject()) { - if (TimerObj* pTimerObj = (TimerObj*)pJSObj->GetEmbedObject()) { - if (CJS_Timer* pTimer = pTimerObj->GetTimer()) { - pTimer->KillJSTimer(); - - for (int i = 0, sz = m_aTimer.GetSize(); i < sz; i++) { - if (m_aTimer[i] == pTimer) { - m_aTimer.RemoveAt(i); - break; - } - } - - delete pTimer; - pTimerObj->SetTimer(NULL); - } - } - } - } - } - + app::ClearTimerCommon(params[0]); return TRUE; } @@ -487,32 +454,42 @@ FX_BOOL app::clearInterval(IJS_Context* cc, return FALSE; } - if (params[0].GetType() == CJS_Value::VT_fxobject) { - v8::Local pObj = params[0].ToV8Object(); - if (FXJS_GetObjDefnID(pObj) == CJS_TimerObj::g_nObjDefnID) { - if (CJS_Object* pJSObj = params[0].ToCJSObject()) { - if (TimerObj* pTimerObj = (TimerObj*)pJSObj->GetEmbedObject()) { - if (CJS_Timer* pTimer = pTimerObj->GetTimer()) { - pTimer->KillJSTimer(); - - for (int i = 0, sz = m_aTimer.GetSize(); i < sz; i++) { - if (m_aTimer[i] == pTimer) { - m_aTimer.RemoveAt(i); - break; - } - } - - delete pTimer; - pTimerObj->SetTimer(NULL); - } - } - } - } - } - + app::ClearTimerCommon(params[0]); return TRUE; } +void app::ClearTimerCommon(const CJS_Value& param) { + if (param.GetType() != CJS_Value::VT_fxobject) + return; + + v8::Local pObj = param.ToV8Object(); + if (FXJS_GetObjDefnID(pObj) != CJS_TimerObj::g_nObjDefnID) + return; + + CJS_Object* pJSObj = param.ToCJSObject(); + if (!pJSObj) + return; + + TimerObj* pTimerObj = static_cast(pJSObj->GetEmbedObject()); + if (!pTimerObj) + return; + + CJS_Timer* pTimer = pTimerObj->GetTimer(); + if (!pTimer) + return; + + pTimer->KillJSTimer(); + auto iter = std::find_if(m_Timers.begin(), m_Timers.end(), + [pTimer](const std::unique_ptr& that) { + return pTimer == that.get(); + }); + + if (iter != m_Timers.end()) + m_Timers.erase(iter); + + pTimerObj->SetTimer(nullptr); +} + FX_BOOL app::execMenuItem(IJS_Context* cc, const std::vector& params, CJS_Value& vRet, -- cgit v1.2.3