From a752edfd2ddc4913aeffd31b67f6fdb53e4116ae Mon Sep 17 00:00:00 2001 From: tsepez Date: Fri, 19 Aug 2016 14:57:42 -0700 Subject: Introduce pdfium::FakeUniquePtr for keys to sets of unique ptrs. Review-Url: https://codereview.chromium.org/2262473002 --- fpdfsdk/javascript/app.cpp | 17 ++++++++--------- fpdfsdk/javascript/app.h | 4 ++-- third_party/base/stl_util.h | 9 +++++++++ 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/fpdfsdk/javascript/app.cpp b/fpdfsdk/javascript/app.cpp index 573de4030a..f95ec0b09b 100644 --- a/fpdfsdk/javascript/app.cpp +++ b/fpdfsdk/javascript/app.cpp @@ -19,6 +19,7 @@ #include "fpdfsdk/javascript/cjs_context.h" #include "fpdfsdk/javascript/cjs_runtime.h" #include "fpdfsdk/javascript/resource.h" +#include "third_party/base/stl_util.h" class GlobalTimer : public CJS_Runtime::Observer { public: @@ -510,10 +511,9 @@ FX_BOOL app::setInterval(IJS_Context* cc, uint32_t dwInterval = params.size() > 1 ? params[1].ToInt(pRuntime) : 1000; CPDFDoc_Environment* pApp = pRuntime->GetReaderApp(); - std::unique_ptr timer( - new GlobalTimer(this, pApp, pRuntime, 0, script, dwInterval, 0)); - GlobalTimer* timerRef = timer.get(); - m_Timers[timerRef] = std::move(timer); + GlobalTimer* timerRef = + new GlobalTimer(this, pApp, pRuntime, 0, script, dwInterval, 0); + m_Timers.insert(std::unique_ptr(timerRef)); v8::Local pRetObj = pRuntime->NewFxDynamicObj(CJS_TimerObj::g_nObjDefnID); @@ -547,10 +547,9 @@ FX_BOOL app::setTimeOut(IJS_Context* cc, uint32_t dwTimeOut = params.size() > 1 ? params[1].ToInt(pRuntime) : 1000; CPDFDoc_Environment* pApp = pRuntime->GetReaderApp(); - std::unique_ptr timer( - new GlobalTimer(this, pApp, pRuntime, 1, script, dwTimeOut, dwTimeOut)); - GlobalTimer* timerRef = timer.get(); - m_Timers[timerRef] = std::move(timer); + GlobalTimer* timerRef = + new GlobalTimer(this, pApp, pRuntime, 1, script, dwTimeOut, dwTimeOut); + m_Timers.insert(std::unique_ptr(timerRef)); v8::Local pRetObj = pRuntime->NewFxDynamicObj(CJS_TimerObj::g_nObjDefnID); @@ -626,7 +625,7 @@ void app::TimerProc(GlobalTimer* pTimer) { } void app::CancelProc(GlobalTimer* pTimer) { - m_Timers.erase(pTimer); + m_Timers.erase(pdfium::FakeUniquePtr(pTimer)); } void app::RunJsScript(CJS_Runtime* pRuntime, const CFX_WideString& wsScript) { diff --git a/fpdfsdk/javascript/app.h b/fpdfsdk/javascript/app.h index 911d86586a..1efd34168c 100644 --- a/fpdfsdk/javascript/app.h +++ b/fpdfsdk/javascript/app.h @@ -7,8 +7,8 @@ #ifndef FPDFSDK_JAVASCRIPT_APP_H_ #define FPDFSDK_JAVASCRIPT_APP_H_ -#include #include +#include #include #include "fpdfsdk/javascript/JS_Define.h" @@ -166,7 +166,7 @@ class app : public CJS_EmbedObj { bool m_bCalculate; bool m_bRuntimeHighLight; - std::map> m_Timers; + std::unordered_set> m_Timers; }; class CJS_App : public CJS_Object { diff --git a/third_party/base/stl_util.h b/third_party/base/stl_util.h index 2d1846724c..ccf3c09073 100644 --- a/third_party/base/stl_util.h +++ b/third_party/base/stl_util.h @@ -27,6 +27,15 @@ bool ContainsValue(const Collection& collection, const Value& value) { collection.end(); } +// Means of generating a key for searching STL collections of std::unique_ptr +// that avoids the side effect of deleting the pointer. +template +class FakeUniquePtr : public std::unique_ptr { + public: + using std::unique_ptr::unique_ptr; + ~FakeUniquePtr() { std::unique_ptr::release(); } +}; + // Convenience routine for "int-fected" code, so that the stl collection // size_t size() method return values will be checked. template -- cgit v1.2.3