diff options
author | Lei Zhang <thestig@chromium.org> | 2015-10-05 12:19:17 -0700 |
---|---|---|
committer | Lei Zhang <thestig@chromium.org> | 2015-10-05 12:19:17 -0700 |
commit | 15720b520d75205c3fecb5aa6be2082632bd1ab3 (patch) | |
tree | 002ef675311f2ad16e81b4e781ea1c4293d74bbd /fpdfsdk/src/javascript | |
parent | 231bb969b6950724eb152081477a57f834016927 (diff) | |
download | pdfium-15720b520d75205c3fecb5aa6be2082632bd1ab3.tar.xz |
Merge to M46: CJS_Timer should observe CJS_Runtime destruction.
Also remove dead CJS_EmbedObj::{Begin,End}Timer code.
BUG=539107
R=tsepez@chromium.org
Review URL: https://codereview.chromium.org/1384883002 .
(cherry picked from commit 794c9b67d3d519342aa7e15052766f7d4a99f551)
Review URL: https://codereview.chromium.org/1385833002 .
Diffstat (limited to 'fpdfsdk/src/javascript')
-rw-r--r-- | fpdfsdk/src/javascript/JS_Object.cpp | 58 | ||||
-rw-r--r-- | fpdfsdk/src/javascript/JS_Runtime.cpp | 14 | ||||
-rw-r--r-- | fpdfsdk/src/javascript/app.cpp | 27 |
3 files changed, 57 insertions, 42 deletions
diff --git a/fpdfsdk/src/javascript/JS_Object.cpp b/fpdfsdk/src/javascript/JS_Object.cpp index 02978982d8..b3b7bafb21 100644 --- a/fpdfsdk/src/javascript/JS_Object.cpp +++ b/fpdfsdk/src/javascript/JS_Object.cpp @@ -56,20 +56,6 @@ void CJS_EmbedObj::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) { CJS_Object::Alert(pContext, swMsg); } -CJS_Timer* CJS_EmbedObj::BeginTimer(CPDFDoc_Environment* pApp, - FX_UINT nElapse) { - CJS_Timer* pTimer = new CJS_Timer(this, pApp); - pTimer->SetJSTimer(nElapse); - - return pTimer; -} - -void CJS_EmbedObj::EndTimer(CJS_Timer* pTimer) { - ASSERT(pTimer != NULL); - pTimer->KillJSTimer(); - delete pTimer; -} - void FreeObject(const v8::WeakCallbackInfo<CJS_Object>& data) { CJS_Object* pJSObj = data.GetParameter(); pJSObj->ExitInstance(); @@ -124,20 +110,40 @@ void CJS_Object::Alert(CJS_Context* pContext, const FX_WCHAR* swMsg) { } } -FX_UINT CJS_Timer::SetJSTimer(FX_UINT nElapse) { - if (m_nTimerID) - KillJSTimer(); +CJS_Timer::CJS_Timer(CJS_EmbedObj* pObj, + CPDFDoc_Environment* pApp, + CJS_Runtime* pRuntime, + int nType, + const CFX_WideString& script, + FX_DWORD dwElapse, + FX_DWORD dwTimeOut) + : m_nTimerID(0), + m_pEmbedObj(pObj), + m_bProcessing(false), + m_bValid(true), + m_nType(nType), + m_dwTimeOut(dwTimeOut), + m_pRuntime(pRuntime), + m_pApp(pApp) { IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); - m_nTimerID = pHandler->SetTimer(nElapse, TimerProc); + m_nTimerID = pHandler->SetTimer(dwElapse, TimerProc); (*GetGlobalTimerMap())[m_nTimerID] = this; - m_dwElapse = nElapse; - return m_nTimerID; + m_pRuntime->AddObserver(this); +} + +CJS_Timer::~CJS_Timer() { + CJS_Runtime* pRuntime = GetRuntime(); + if (pRuntime) + pRuntime->RemoveObserver(this); + KillJSTimer(); } void CJS_Timer::KillJSTimer() { if (m_nTimerID) { - IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); - pHandler->KillTimer(m_nTimerID); + if (m_bValid) { + IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); + pHandler->KillTimer(m_nTimerID); + } GetGlobalTimerMap()->erase(m_nTimerID); m_nTimerID = 0; } @@ -149,10 +155,10 @@ void CJS_Timer::TimerProc(int idEvent) { if (it != GetGlobalTimerMap()->end()) { CJS_Timer* pTimer = it->second; if (!pTimer->m_bProcessing) { - pTimer->m_bProcessing = TRUE; + CFX_AutoRestorer<bool> scoped_processing(&pTimer->m_bProcessing); + pTimer->m_bProcessing = true; if (pTimer->m_pEmbedObj) pTimer->m_pEmbedObj->TimerProc(pTimer); - pTimer->m_bProcessing = FALSE; } } } @@ -163,3 +169,7 @@ CJS_Timer::TimerMap* CJS_Timer::GetGlobalTimerMap() { static auto* s_TimerMap = new TimerMap; return s_TimerMap; } + +void CJS_Timer::OnDestroyed() { + m_bValid = false; +} diff --git a/fpdfsdk/src/javascript/JS_Runtime.cpp b/fpdfsdk/src/javascript/JS_Runtime.cpp index 4ef34a8d20..8efb277dd7 100644 --- a/fpdfsdk/src/javascript/JS_Runtime.cpp +++ b/fpdfsdk/src/javascript/JS_Runtime.cpp @@ -12,7 +12,6 @@ #include "../../include/javascript/JS_Define.h" #include "../../include/javascript/JS_Object.h" #include "../../include/javascript/JS_Value.h" -#include "../../include/javascript/Document.h" #include "../../include/javascript/app.h" #include "../../include/javascript/color.h" #include "../../include/javascript/Consts.h" @@ -121,6 +120,9 @@ CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) } CJS_Runtime::~CJS_Runtime() { + for (auto* obs : m_observers) + obs->OnDestroyed(); + for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) delete m_ContextArray.GetAt(i); @@ -329,6 +331,16 @@ v8::Local<v8::Context> CJS_Runtime::NewJSContext() { return v8::Local<v8::Context>::New(m_isolate, m_context); } +void CJS_Runtime::AddObserver(Observer* observer) { + ASSERT(m_observers.find(observer) == m_observers.end()); + m_observers.insert(observer); +} + +void CJS_Runtime::RemoveObserver(Observer* observer) { + ASSERT(m_observers.find(observer) != m_observers.end()); + m_observers.erase(observer); +} + CFX_WideString ChangeObjName(const CFX_WideString& str) { CFX_WideString sRet = str; sRet.Replace(L"_", L"."); diff --git a/fpdfsdk/src/javascript/app.cpp b/fpdfsdk/src/javascript/app.cpp index 8d895ab260..b6da00fd69 100644 --- a/fpdfsdk/src/javascript/app.cpp +++ b/fpdfsdk/src/javascript/app.cpp @@ -414,16 +414,10 @@ FX_BOOL app::setInterval(IFXJS_Context* cc, CPDFDoc_Environment* pApp = pRuntime->GetReaderApp(); ASSERT(pApp); - CJS_Timer* pTimer = new CJS_Timer(this, pApp); + CJS_Timer* pTimer = + new CJS_Timer(this, pApp, pRuntime, 0, script, dwInterval, 0); m_aTimer.Add(pTimer); - pTimer->SetType(0); - pTimer->SetRuntime(pRuntime); - pTimer->SetJScript(script); - pTimer->SetTimeOut(0); - // pTimer->SetStartTime(GetTickCount()); - pTimer->SetJSTimer(dwInterval); - JSFXObject pRetObj = JS_NewFxDynamicObj( *pRuntime, pContext, JS_GetObjDefnID(*pRuntime, L"TimerObj")); @@ -467,15 +461,10 @@ FX_BOOL app::setTimeOut(IFXJS_Context* cc, CPDFDoc_Environment* pApp = pRuntime->GetReaderApp(); ASSERT(pApp); - CJS_Timer* pTimer = new CJS_Timer(this, pApp); + CJS_Timer* pTimer = + new CJS_Timer(this, pApp, pRuntime, 1, script, dwTimeOut, dwTimeOut); m_aTimer.Add(pTimer); - pTimer->SetType(1); - pTimer->SetRuntime(pRuntime); - pTimer->SetJScript(script); - pTimer->SetTimeOut(dwTimeOut); - pTimer->SetJSTimer(dwTimeOut); - JSFXObject pRetObj = JS_NewFxDynamicObj( *pRuntime, pContext, JS_GetObjDefnID(*pRuntime, L"TimerObj")); @@ -587,13 +576,17 @@ FX_BOOL app::execMenuItem(IFXJS_Context* cc, void app::TimerProc(CJS_Timer* pTimer) { ASSERT(pTimer != NULL); + CJS_Runtime* pRuntime = pTimer->GetRuntime(); + switch (pTimer->GetType()) { case 0: // interval - RunJsScript(pTimer->GetRuntime(), pTimer->GetJScript()); + if (pRuntime) + RunJsScript(pRuntime, pTimer->GetJScript()); break; case 1: if (pTimer->GetTimeOut() > 0) { - RunJsScript(pTimer->GetRuntime(), pTimer->GetJScript()); + if (pRuntime) + RunJsScript(pRuntime, pTimer->GetJScript()); pTimer->KillJSTimer(); } break; |