Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ | 7 #ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ |
| 8 #define FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ | 8 #define FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ |
| 9 | 9 |
| 10 #include <map> | |
| 11 | |
| 10 #include "../fsdk_define.h" // For FX_UINT | 12 #include "../fsdk_define.h" // For FX_UINT |
| 11 #include "../fsdk_mgr.h" // For CPDFDoc_Environment | 13 #include "../fsdk_mgr.h" // For CPDFDoc_Environment |
| 12 #include "../fx_systemhandler.h" // For IFX_SystemHandler | 14 #include "../fx_systemhandler.h" // For IFX_SystemHandler |
| 13 #include "../jsapi/fxjs_v8.h" | 15 #include "../jsapi/fxjs_v8.h" |
| 14 | 16 |
| 15 class CPDFSDK_PageView; | 17 class CPDFSDK_PageView; |
| 16 class CJS_Object; | 18 class CJS_Object; |
| 17 class CJS_Timer; | 19 class CJS_Timer; |
| 18 class CJS_Context; | 20 class CJS_Context; |
| 19 | 21 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 static void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg); | 77 static void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg); |
| 76 | 78 |
| 77 v8::Isolate* GetIsolate() { return m_pIsolate; } | 79 v8::Isolate* GetIsolate() { return m_pIsolate; } |
| 78 | 80 |
| 79 protected: | 81 protected: |
| 80 CJS_EmbedObj* m_pEmbedObj; | 82 CJS_EmbedObj* m_pEmbedObj; |
| 81 v8::Global<v8::Object> m_pObject; | 83 v8::Global<v8::Object> m_pObject; |
| 82 v8::Isolate* m_pIsolate; | 84 v8::Isolate* m_pIsolate; |
| 83 }; | 85 }; |
| 84 | 86 |
| 85 struct JS_TIMER_MAP { | 87 using JSTimerMap = std::map<FX_UINT, CJS_Timer*>; |
| 86 FX_UINT nID; | 88 JSTimerMap* GetGlobalTimerMap(); |
| 87 CJS_Timer* pTimer; | |
| 88 }; | |
| 89 | |
| 90 typedef CFX_ArrayTemplate<JS_TIMER_MAP*> CTimerMapArray; | |
| 91 | |
| 92 struct JS_TIMER_MAPARRAY { | |
| 93 public: | |
| 94 JS_TIMER_MAPARRAY() {} | |
| 95 | |
| 96 ~JS_TIMER_MAPARRAY() { Reset(); } | |
| 97 | |
| 98 void Reset() { | |
| 99 for (int i = 0, sz = m_Array.GetSize(); i < sz; i++) | |
| 100 delete m_Array.GetAt(i); | |
| 101 | |
| 102 m_Array.RemoveAll(); | |
| 103 } | |
| 104 | |
| 105 void SetAt(FX_UINT nIndex, CJS_Timer* pTimer) { | |
| 106 int i = Find(nIndex); | |
| 107 | |
| 108 if (i >= 0) { | |
| 109 if (JS_TIMER_MAP* pMap = m_Array.GetAt(i)) | |
| 110 pMap->pTimer = pTimer; | |
| 111 } else { | |
| 112 if (JS_TIMER_MAP* pMap = new JS_TIMER_MAP) { | |
| 113 pMap->nID = nIndex; | |
| 114 pMap->pTimer = pTimer; | |
| 115 m_Array.Add(pMap); | |
| 116 } | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 CJS_Timer* GetAt(FX_UINT nIndex) { | |
| 121 int i = Find(nIndex); | |
| 122 | |
| 123 if (i >= 0) { | |
| 124 if (JS_TIMER_MAP* pMap = m_Array.GetAt(i)) | |
| 125 return pMap->pTimer; | |
| 126 } | |
| 127 return NULL; | |
| 128 } | |
| 129 | |
| 130 void RemoveAt(FX_UINT nIndex) { | |
| 131 int i = Find(nIndex); | |
| 132 | |
| 133 if (i >= 0) { | |
| 134 delete m_Array.GetAt(i); | |
| 135 m_Array.RemoveAt(i); | |
| 136 } | |
| 137 // To prevent potential fake memory leak reported by vc6. | |
| 138 if (m_Array.GetSize() == 0) | |
| 139 m_Array.RemoveAll(); | |
| 140 } | |
| 141 | |
| 142 int Find(FX_UINT nIndex) { | |
| 143 for (int i = 0, sz = m_Array.GetSize(); i < sz; i++) { | |
| 144 if (JS_TIMER_MAP* pMap = m_Array.GetAt(i)) { | |
| 145 if (pMap->nID == nIndex) | |
| 146 return i; | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 return -1; | |
| 151 } | |
| 152 | |
| 153 CTimerMapArray m_Array; | |
| 154 }; | |
| 155 | |
| 156 JS_TIMER_MAPARRAY& GetTimeMap(); | |
| 157 | 89 |
| 158 class CJS_Runtime; | 90 class CJS_Runtime; |
| 159 | 91 |
| 160 class CJS_Timer { | 92 class CJS_Timer { |
| 161 public: | 93 public: |
| 162 CJS_Timer(CJS_EmbedObj* pObj, CPDFDoc_Environment* pApp) | 94 CJS_Timer(CJS_EmbedObj* pObj, CPDFDoc_Environment* pApp) |
| 163 : m_nTimerID(0), | 95 : m_nTimerID(0), |
| 164 m_pEmbedObj(pObj), | 96 m_pEmbedObj(pObj), |
| 165 m_bProcessing(FALSE), | 97 m_bProcessing(FALSE), |
| 166 m_dwStartTime(0), | 98 m_dwStartTime(0), |
| 167 m_dwTimeOut(0), | 99 m_dwTimeOut(0), |
| 168 m_dwElapse(0), | 100 m_dwElapse(0), |
| 169 m_pRuntime(NULL), | 101 m_pRuntime(NULL), |
| 170 m_nType(0), | 102 m_nType(0), |
| 171 m_pApp(pApp) {} | 103 m_pApp(pApp) {} |
| 172 | 104 |
| 173 virtual ~CJS_Timer() { KillJSTimer(); } | 105 virtual ~CJS_Timer() { KillJSTimer(); } |
| 174 | 106 |
| 175 public: | 107 public: |
| 176 FX_UINT SetJSTimer(FX_UINT nElapse) { | 108 FX_UINT SetJSTimer(FX_UINT nElapse) { |
| 177 if (m_nTimerID) | 109 if (m_nTimerID) |
| 178 KillJSTimer(); | 110 KillJSTimer(); |
| 179 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); | 111 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); |
| 180 m_nTimerID = pHandler->SetTimer(nElapse, TimerProc); | 112 m_nTimerID = pHandler->SetTimer(nElapse, TimerProc); |
| 181 GetTimeMap().SetAt(m_nTimerID, this); | 113 (*GetGlobalTimerMap())[m_nTimerID] = this; |
| 182 m_dwElapse = nElapse; | 114 m_dwElapse = nElapse; |
| 183 return m_nTimerID; | 115 return m_nTimerID; |
| 184 }; | 116 }; |
| 185 | 117 |
| 186 void KillJSTimer() { | 118 void KillJSTimer() { |
| 187 if (m_nTimerID) { | 119 if (m_nTimerID) { |
| 188 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); | 120 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); |
| 189 pHandler->KillTimer(m_nTimerID); | 121 pHandler->KillTimer(m_nTimerID); |
| 190 GetTimeMap().RemoveAt(m_nTimerID); | 122 GetGlobalTimerMap()->erase(m_nTimerID); |
|
Lei Zhang
2015/08/13 21:48:24
Aren't you leaking memory here? JS_TIMER_MAPARRAY
Tom Sepez
2015/08/13 21:54:11
I don't think so. JS_TIMER_MAPARRAY was an array
| |
| 191 m_nTimerID = 0; | 123 m_nTimerID = 0; |
| 192 } | 124 } |
| 193 }; | 125 }; |
| 194 | 126 |
| 195 void SetType(int nType) { m_nType = nType; } | 127 void SetType(int nType) { m_nType = nType; } |
| 196 | 128 |
| 197 int GetType() const { return m_nType; } | 129 int GetType() const { return m_nType; } |
| 198 | 130 |
| 199 void SetStartTime(FX_DWORD dwStartTime) { m_dwStartTime = dwStartTime; } | 131 void SetStartTime(FX_DWORD dwStartTime) { m_dwStartTime = dwStartTime; } |
| 200 | 132 |
| 201 FX_DWORD GetStartTime() const { return m_dwStartTime; } | 133 FX_DWORD GetStartTime() const { return m_dwStartTime; } |
| 202 | 134 |
| 203 void SetTimeOut(FX_DWORD dwTimeOut) { m_dwTimeOut = dwTimeOut; } | 135 void SetTimeOut(FX_DWORD dwTimeOut) { m_dwTimeOut = dwTimeOut; } |
| 204 | 136 |
| 205 FX_DWORD GetTimeOut() const { return m_dwTimeOut; } | 137 FX_DWORD GetTimeOut() const { return m_dwTimeOut; } |
| 206 | 138 |
| 207 void SetRuntime(CJS_Runtime* pRuntime) { m_pRuntime = pRuntime; } | 139 void SetRuntime(CJS_Runtime* pRuntime) { m_pRuntime = pRuntime; } |
| 208 | 140 |
| 209 CJS_Runtime* GetRuntime() const { return m_pRuntime; } | 141 CJS_Runtime* GetRuntime() const { return m_pRuntime; } |
| 210 | 142 |
| 211 void SetJScript(const CFX_WideString& script) { m_swJScript = script; } | 143 void SetJScript(const CFX_WideString& script) { m_swJScript = script; } |
| 212 | 144 |
| 213 CFX_WideString GetJScript() const { return m_swJScript; } | 145 CFX_WideString GetJScript() const { return m_swJScript; } |
| 214 | 146 |
| 215 static void TimerProc(int idEvent) { | 147 static void TimerProc(int idEvent) { |
| 216 if (CJS_Timer* pTimer = GetTimeMap().GetAt(idEvent)) { | 148 const auto it = GetGlobalTimerMap()->find(idEvent); |
| 149 if (it != GetGlobalTimerMap()->end()) { | |
| 150 CJS_Timer* pTimer = it->second; | |
| 217 if (!pTimer->m_bProcessing) { | 151 if (!pTimer->m_bProcessing) { |
| 218 pTimer->m_bProcessing = TRUE; | 152 pTimer->m_bProcessing = TRUE; |
| 219 if (pTimer->m_pEmbedObj) | 153 if (pTimer->m_pEmbedObj) |
| 220 pTimer->m_pEmbedObj->TimerProc(pTimer); | 154 pTimer->m_pEmbedObj->TimerProc(pTimer); |
| 221 pTimer->m_bProcessing = FALSE; | 155 pTimer->m_bProcessing = FALSE; |
| 222 } else { | |
| 223 // TRACE(L"BUSY!\n"); | |
| 224 } | 156 } |
| 225 } | 157 } |
| 226 }; | 158 }; |
| 227 | 159 |
| 228 private: | 160 private: |
| 229 FX_UINT m_nTimerID; | 161 FX_UINT m_nTimerID; |
| 230 CJS_EmbedObj* m_pEmbedObj; | 162 CJS_EmbedObj* m_pEmbedObj; |
| 231 FX_BOOL m_bProcessing; | 163 FX_BOOL m_bProcessing; |
| 232 | 164 |
| 233 // data | 165 // data |
| 234 FX_DWORD m_dwStartTime; | 166 FX_DWORD m_dwStartTime; |
| 235 FX_DWORD m_dwTimeOut; | 167 FX_DWORD m_dwTimeOut; |
| 236 FX_DWORD m_dwElapse; | 168 FX_DWORD m_dwElapse; |
| 237 CJS_Runtime* m_pRuntime; | 169 CJS_Runtime* m_pRuntime; |
| 238 CFX_WideString m_swJScript; | 170 CFX_WideString m_swJScript; |
| 239 int m_nType; // 0:Interval; 1:TimeOut | 171 int m_nType; // 0:Interval; 1:TimeOut |
| 240 | 172 |
| 241 CPDFDoc_Environment* m_pApp; | 173 CPDFDoc_Environment* m_pApp; |
| 242 }; | 174 }; |
| 243 | 175 |
| 244 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ | 176 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ |
| OLD | NEW |