Remove bare |delete| from CXFA_FWLAdapterTimerMgr.
The s_TimerArray owns the memory anyways, so use unique_ptr<>.
- Remove an out parameter in favor of return value.
- Used anonymous namespace instead of static class members.
Change-Id: I7fda2df3f9eea10706271e3443d336429024b2e2
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/54610
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.cpp b/fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.cpp
index 8901394..c62f300 100644
--- a/fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.cpp
+++ b/fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.cpp
@@ -6,6 +6,7 @@
#include "fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h"
+#include <memory>
#include <utility>
#include <vector>
@@ -14,6 +15,8 @@
namespace {
+std::vector<std::unique_ptr<CFWL_TimerInfo>>* g_TimerArray = nullptr;
+
class CFWL_FWLAdapterTimerInfo final : public CFWL_TimerInfo {
public:
CFWL_FWLAdapterTimerInfo(IFWL_AdapterTimerMgr* mgr,
@@ -25,29 +28,41 @@
CFWL_Timer* pTimer;
};
+void TimerProc(int32_t idEvent) {
+ if (!g_TimerArray)
+ return;
+
+ for (const auto& info : *g_TimerArray) {
+ auto* pInfo = static_cast<CFWL_FWLAdapterTimerInfo*>(info.get());
+ if (pInfo->idEvent == idEvent) {
+ pInfo->pTimer->Run(pInfo);
+ break;
+ }
+ }
+}
+
} // namespace
-std::vector<CFWL_TimerInfo*>* CXFA_FWLAdapterTimerMgr::s_TimerArray = nullptr;
CXFA_FWLAdapterTimerMgr::CXFA_FWLAdapterTimerMgr(
CPDFSDK_FormFillEnvironment* pFormFillEnv)
: m_pFormFillEnv(pFormFillEnv) {}
-CXFA_FWLAdapterTimerMgr::~CXFA_FWLAdapterTimerMgr() {}
+CXFA_FWLAdapterTimerMgr::~CXFA_FWLAdapterTimerMgr() = default;
-void CXFA_FWLAdapterTimerMgr::Start(CFWL_Timer* pTimer,
- uint32_t dwElapse,
- bool bImmediately,
- CFWL_TimerInfo** pTimerInfo) {
+CFWL_TimerInfo* CXFA_FWLAdapterTimerMgr::Start(CFWL_Timer* pTimer,
+ uint32_t dwElapse,
+ bool bImmediately) {
+ if (!g_TimerArray)
+ g_TimerArray = new std::vector<std::unique_ptr<CFWL_TimerInfo>>;
+
if (!m_pFormFillEnv)
- return;
+ return nullptr;
int32_t id_event = m_pFormFillEnv->SetTimer(dwElapse, TimerProc);
- if (!s_TimerArray)
- s_TimerArray = new std::vector<CFWL_TimerInfo*>;
-
- *pTimerInfo = new CFWL_FWLAdapterTimerInfo(this, id_event, pTimer);
- s_TimerArray->push_back(*pTimerInfo);
+ g_TimerArray->push_back(
+ pdfium::MakeUnique<CFWL_FWLAdapterTimerInfo>(this, id_event, pTimer));
+ return g_TimerArray->back().get();
}
void CXFA_FWLAdapterTimerMgr::Stop(CFWL_TimerInfo* pTimerInfo) {
@@ -57,27 +72,11 @@
CFWL_FWLAdapterTimerInfo* pInfo =
static_cast<CFWL_FWLAdapterTimerInfo*>(pTimerInfo);
m_pFormFillEnv->KillTimer(pInfo->idEvent);
- if (!s_TimerArray)
+ if (!g_TimerArray)
return;
- auto it = std::find(s_TimerArray->begin(), s_TimerArray->end(), pInfo);
- if (it != s_TimerArray->end()) {
- s_TimerArray->erase(it);
- delete pInfo;
- }
-}
-
-// static
-void CXFA_FWLAdapterTimerMgr::TimerProc(int32_t idEvent) {
- if (!s_TimerArray)
- return;
-
- for (auto* info : *s_TimerArray) {
- CFWL_FWLAdapterTimerInfo* pInfo =
- static_cast<CFWL_FWLAdapterTimerInfo*>(info);
- if (pInfo->idEvent == idEvent) {
- pInfo->pTimer->Run(pInfo);
- break;
- }
- }
+ pdfium::FakeUniquePtr<CFWL_TimerInfo> fake(pInfo);
+ auto it = std::find(g_TimerArray->begin(), g_TimerArray->end(), fake);
+ if (it != g_TimerArray->end())
+ g_TimerArray->erase(it);
}
diff --git a/fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h b/fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h
index d4db13f..bb5afb8 100644
--- a/fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h
+++ b/fpdfsdk/fpdfxfa/cxfa_fwladaptertimermgr.h
@@ -19,16 +19,12 @@
explicit CXFA_FWLAdapterTimerMgr(CPDFSDK_FormFillEnvironment* pFormFillEnv);
~CXFA_FWLAdapterTimerMgr() override;
- void Start(CFWL_Timer* pTimer,
- uint32_t dwElapse,
- bool bImmediately,
- CFWL_TimerInfo** pTimerInfo) override;
+ CFWL_TimerInfo* Start(CFWL_Timer* pTimer,
+ uint32_t dwElapse,
+ bool bImmediately) override;
void Stop(CFWL_TimerInfo* pTimerInfo) override;
private:
- static void TimerProc(int32_t idEvent);
-
- static std::vector<CFWL_TimerInfo*>* s_TimerArray;
UnownedPtr<CPDFSDK_FormFillEnvironment> const m_pFormFillEnv;
};
diff --git a/xfa/fwl/cfwl_timer.cpp b/xfa/fwl/cfwl_timer.cpp
index ae9bcf2..c332555 100644
--- a/xfa/fwl/cfwl_timer.cpp
+++ b/xfa/fwl/cfwl_timer.cpp
@@ -27,7 +27,5 @@
if (!m_pAdapterTimerMgr)
return nullptr;
- CFWL_TimerInfo* pTimerInfo = nullptr;
- m_pAdapterTimerMgr->Start(this, dwElapse, bImmediately, &pTimerInfo);
- return pTimerInfo;
+ return m_pAdapterTimerMgr->Start(this, dwElapse, bImmediately);
}
diff --git a/xfa/fwl/ifwl_adaptertimermgr.h b/xfa/fwl/ifwl_adaptertimermgr.h
index 9f9e5ca..afa87c4 100644
--- a/xfa/fwl/ifwl_adaptertimermgr.h
+++ b/xfa/fwl/ifwl_adaptertimermgr.h
@@ -12,10 +12,10 @@
class IFWL_AdapterTimerMgr {
public:
virtual ~IFWL_AdapterTimerMgr() = default;
- virtual void Start(CFWL_Timer* pTimer,
- uint32_t dwElapse,
- bool bImmediately,
- CFWL_TimerInfo** pTimerInfo) = 0;
+
+ virtual CFWL_TimerInfo* Start(CFWL_Timer* pTimer,
+ uint32_t dwElapse,
+ bool bImmediately) = 0;
virtual void Stop(CFWL_TimerInfo* pTimerInfo) = 0;
};