| 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 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 CJS_Timer* GetAt(FX_UINT nIndex) { | |
| 120 int i = Find(nIndex); | |
| 121 | |
| 122 if (i >= 0) { | |
| 123 if (JS_TIMER_MAP* pMap = m_Array.GetAt(i)) | |
| 124 return pMap->pTimer; | |
| 125 } | |
| 126 return NULL; | |
| 127 } | |
| 128 | |
| 129 void RemoveAt(FX_UINT nIndex) { | |
| 130 int i = Find(nIndex); | |
| 131 | |
| 132 if (i >= 0) { | |
| 133 delete m_Array.GetAt(i); | |
| 134 m_Array.RemoveAt(i); | |
| 135 } | |
| 136 // To prevent potential fake memory leak reported by vc6. | |
| 137 if (m_Array.GetSize() == 0) | |
| 138 m_Array.RemoveAll(); | |
| 139 } | |
| 140 | |
| 141 int Find(FX_UINT nIndex) { | |
| 142 for (int i = 0, sz = m_Array.GetSize(); i < sz; i++) { | |
| 143 if (JS_TIMER_MAP* pMap = m_Array.GetAt(i)) { | |
| 144 if (pMap->nID == nIndex) | |
| 145 return i; | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 return -1; | |
| 150 } | |
| 151 | |
| 152 CTimerMapArray m_Array; | |
| 153 }; | |
| 154 | |
| 155 JS_TIMER_MAPARRAY& GetTimeMap(); | |
| 156 | 89 |
| 157 class CJS_Runtime; | 90 class CJS_Runtime; |
| 158 | 91 |
| 159 class CJS_Timer { | 92 class CJS_Timer { |
| 160 public: | 93 public: |
| 161 CJS_Timer(CJS_EmbedObj* pObj, CPDFDoc_Environment* pApp) | 94 CJS_Timer(CJS_EmbedObj* pObj, CPDFDoc_Environment* pApp) |
| 162 : m_nTimerID(0), | 95 : m_nTimerID(0), |
| 163 m_pEmbedObj(pObj), | 96 m_pEmbedObj(pObj), |
| 164 m_bProcessing(FALSE), | 97 m_bProcessing(FALSE), |
| 165 m_dwStartTime(0), | 98 m_dwStartTime(0), |
| 166 m_dwTimeOut(0), | 99 m_dwTimeOut(0), |
| 167 m_dwElapse(0), | 100 m_dwElapse(0), |
| 168 m_pRuntime(NULL), | 101 m_pRuntime(NULL), |
| 169 m_nType(0), | 102 m_nType(0), |
| 170 m_pApp(pApp) {} | 103 m_pApp(pApp) {} |
| 171 | 104 |
| 172 virtual ~CJS_Timer() { KillJSTimer(); } | 105 virtual ~CJS_Timer() { KillJSTimer(); } |
| 173 | 106 |
| 174 public: | 107 public: |
| 175 FX_UINT SetJSTimer(FX_UINT nElapse) { | 108 FX_UINT SetJSTimer(FX_UINT nElapse) { |
| 176 if (m_nTimerID) | 109 if (m_nTimerID) |
| 177 KillJSTimer(); | 110 KillJSTimer(); |
| 178 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); | 111 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); |
| 179 m_nTimerID = pHandler->SetTimer(nElapse, TimerProc); | 112 m_nTimerID = pHandler->SetTimer(nElapse, TimerProc); |
| 180 GetTimeMap().SetAt(m_nTimerID, this); | 113 (*GetGlobalTimerMap())[m_nTimerID] = this; |
| 181 m_dwElapse = nElapse; | 114 m_dwElapse = nElapse; |
| 182 return m_nTimerID; | 115 return m_nTimerID; |
| 183 }; | 116 }; |
| 184 | 117 |
| 185 void KillJSTimer() { | 118 void KillJSTimer() { |
| 186 if (m_nTimerID) { | 119 if (m_nTimerID) { |
| 187 if (m_pApp == NULL) { | 120 if (m_pApp == NULL) { |
| 188 GetTimeMap().RemoveAt(m_nTimerID); | 121 GetTimeMap().RemoveAt(m_nTimerID); |
| 189 m_nTimerID = 0; | 122 m_nTimerID = 0; |
| 190 return; | 123 return; |
| 191 } | 124 } |
| 192 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); | 125 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); |
| 193 pHandler->KillTimer(m_nTimerID); | 126 pHandler->KillTimer(m_nTimerID); |
| 194 GetTimeMap().RemoveAt(m_nTimerID); | 127 GetGlobalTimerMap()->erase(m_nTimerID); |
| 195 m_nTimerID = 0; | 128 m_nTimerID = 0; |
| 196 } | 129 } |
| 197 }; | 130 }; |
| 198 | 131 |
| 199 void SetType(int nType) { m_nType = nType; } | 132 void SetType(int nType) { m_nType = nType; } |
| 200 | 133 |
| 201 int GetType() const { return m_nType; } | 134 int GetType() const { return m_nType; } |
| 202 | 135 |
| 203 void SetStartTime(FX_DWORD dwStartTime) { m_dwStartTime = dwStartTime; } | 136 void SetStartTime(FX_DWORD dwStartTime) { m_dwStartTime = dwStartTime; } |
| 204 | 137 |
| 205 FX_DWORD GetStartTime() const { return m_dwStartTime; } | 138 FX_DWORD GetStartTime() const { return m_dwStartTime; } |
| 206 | 139 |
| 207 void SetTimeOut(FX_DWORD dwTimeOut) { m_dwTimeOut = dwTimeOut; } | 140 void SetTimeOut(FX_DWORD dwTimeOut) { m_dwTimeOut = dwTimeOut; } |
| 208 | 141 |
| 209 FX_DWORD GetTimeOut() const { return m_dwTimeOut; } | 142 FX_DWORD GetTimeOut() const { return m_dwTimeOut; } |
| 210 | 143 |
| 211 void SetRuntime(CJS_Runtime* pRuntime) { m_pRuntime = pRuntime; } | 144 void SetRuntime(CJS_Runtime* pRuntime) { m_pRuntime = pRuntime; } |
| 212 | 145 |
| 213 CJS_Runtime* GetRuntime() const { return m_pRuntime; } | 146 CJS_Runtime* GetRuntime() const { return m_pRuntime; } |
| 214 | 147 |
| 215 void SetJScript(const CFX_WideString& script) { m_swJScript = script; } | 148 void SetJScript(const CFX_WideString& script) { m_swJScript = script; } |
| 216 | 149 |
| 217 CFX_WideString GetJScript() const { return m_swJScript; } | 150 CFX_WideString GetJScript() const { return m_swJScript; } |
| 218 | 151 |
| 219 static void TimerProc(int idEvent) { | 152 static void TimerProc(int idEvent) { |
| 220 if (CJS_Timer* pTimer = GetTimeMap().GetAt(idEvent)) { | 153 const auto it = GetGlobalTimerMap()->find(idEvent); |
| 154 if (it != GetGlobalTimerMap()->end()) { |
| 155 CJS_Timer* pTimer = it->second; |
| 221 if (!pTimer->m_bProcessing) { | 156 if (!pTimer->m_bProcessing) { |
| 222 pTimer->m_bProcessing = TRUE; | 157 pTimer->m_bProcessing = TRUE; |
| 223 if (pTimer->m_pEmbedObj) | 158 if (pTimer->m_pEmbedObj) |
| 224 pTimer->m_pEmbedObj->TimerProc(pTimer); | 159 pTimer->m_pEmbedObj->TimerProc(pTimer); |
| 225 pTimer->m_bProcessing = FALSE; | 160 pTimer->m_bProcessing = FALSE; |
| 226 } else { | |
| 227 // TRACE(L"BUSY!\n"); | |
| 228 } | 161 } |
| 229 } | 162 } |
| 230 }; | 163 }; |
| 231 | 164 |
| 232 private: | 165 private: |
| 233 FX_UINT m_nTimerID; | 166 FX_UINT m_nTimerID; |
| 234 CJS_EmbedObj* m_pEmbedObj; | 167 CJS_EmbedObj* m_pEmbedObj; |
| 235 FX_BOOL m_bProcessing; | 168 FX_BOOL m_bProcessing; |
| 236 | 169 |
| 237 // data | 170 // data |
| 238 FX_DWORD m_dwStartTime; | 171 FX_DWORD m_dwStartTime; |
| 239 FX_DWORD m_dwTimeOut; | 172 FX_DWORD m_dwTimeOut; |
| 240 FX_DWORD m_dwElapse; | 173 FX_DWORD m_dwElapse; |
| 241 CJS_Runtime* m_pRuntime; | 174 CJS_Runtime* m_pRuntime; |
| 242 CFX_WideString m_swJScript; | 175 CFX_WideString m_swJScript; |
| 243 int m_nType; // 0:Interval; 1:TimeOut | 176 int m_nType; // 0:Interval; 1:TimeOut |
| 244 | 177 |
| 245 CPDFDoc_Environment* m_pApp; | 178 CPDFDoc_Environment* m_pApp; |
| 246 }; | 179 }; |
| 247 | 180 |
| 248 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ | 181 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_ |
| OLD | NEW |