John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
| 7 | #ifndef _JS_OBJECT_H_ |
| 8 | #define _JS_OBJECT_H_ |
| 9 | |
| 10 | class CJS_Object; |
| 11 | class CJS_Timer; |
| 12 | class CJS_Context; |
| 13 | |
| 14 | class CJS_EmbedObj : public CFX_Object |
| 15 | { |
| 16 | public: |
| 17 | CJS_EmbedObj(CJS_Object* pJSObject); |
| 18 | virtual ~CJS_EmbedObj(); |
| 19 | |
| 20 | virtual void TimerProc(CJS_Timer* pTimer){}; |
| 21 | |
| 22 | CJS_Timer* BeginTimer(CPDFDoc_Environment * pApp, FX_UINT nElapse); |
| 23 | void EndTimer(CJS_Timer* pTimer); |
| 24 | |
| 25 | CJS_Object* GetJSObject(){return m_pJSObject;}; |
| 26 | operator CJS_Object* (){return m_pJSObject;}; |
| 27 | |
| 28 | CPDFSDK_PageView * JSGetPageView(IFXJS_Context* cc); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 29 | int MsgBox(CPDFDoc_Environment * pApp, CPDFSDK_PageView* pPageView, FX_LPCWSTR swMsg, FX_LPCWSTR swTitle = NULL, FX_UINT nType = 0, FX_UINT nIcon = 0); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 30 | void Alert(CJS_Context* pContext, FX_LPCWSTR swMsg); |
| 31 | FX_BOOL IsSafeMode(IFXJS_Context* cc); |
| 32 | |
| 33 | protected: |
| 34 | |
| 35 | CJS_Object* m_pJSObject; |
| 36 | }; |
| 37 | |
| 38 | class CJS_Object : public CFX_Object |
| 39 | { |
| 40 | public: |
| 41 | CJS_Object(JSFXObject pObject); |
| 42 | virtual ~CJS_Object(void); |
| 43 | |
| 44 | void MakeWeak(); |
| 45 | |
| 46 | virtual FX_BOOL IsType(FX_LPCSTR sClassName){return TRUE;}; |
| 47 | virtual CFX_ByteString GetClassName(){return "";}; |
| 48 | |
| 49 | virtual FX_BOOL InitInstance(IFXJS_Context* cc){return TRUE;}; |
| 50 | virtual FX_BOOL ExitInstance(){return TRUE;}; |
| 51 | |
| 52 | operator JSFXObject () {return v8::Local<v8::Object>::New(m_pIsolate, m_pObject);} |
| 53 | operator CJS_EmbedObj* (){return m_pEmbedObj;}; |
| 54 | |
| 55 | void SetEmbedObject(CJS_EmbedObj* pObj){m_pEmbedObj = pObj;}; |
| 56 | CJS_EmbedObj * GetEmbedObject(){return m_pEmbedObj;}; |
| 57 | |
| 58 | static CPDFSDK_PageView * JSGetPageView(IFXJS_Context* cc); |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 59 | static int MsgBox(CPDFDoc_Environment * pApp, CPDFSDK_PageView* pPageView, FX_LPCWSTR swMsg, FX_LPCWSTR swTitle = NULL, FX_UINT nType = 0,FX_UINT nIcon = 0); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 60 | static void Alert(CJS_Context* pContext, FX_LPCWSTR swMsg); |
| 61 | |
| 62 | v8::Isolate* GetIsolate() {return m_pIsolate;} |
| 63 | protected: |
| 64 | CJS_EmbedObj * m_pEmbedObj; |
| 65 | v8::Persistent<v8::Object> m_pObject; |
| 66 | v8::Isolate* m_pIsolate; |
| 67 | }; |
| 68 | |
| 69 | struct JS_TIMER_MAP |
| 70 | { |
| 71 | FX_UINT nID; |
| 72 | CJS_Timer * pTimer; |
| 73 | }; |
| 74 | |
| 75 | typedef CFX_ArrayTemplate<JS_TIMER_MAP*> CTimerMapArray; |
| 76 | |
| 77 | struct JS_TIMER_MAPARRAY |
| 78 | { |
| 79 | public: |
| 80 | JS_TIMER_MAPARRAY() |
| 81 | { |
| 82 | } |
| 83 | |
| 84 | ~JS_TIMER_MAPARRAY() |
| 85 | { |
| 86 | Reset(); |
| 87 | } |
| 88 | |
| 89 | void Reset() |
| 90 | { |
| 91 | for (int i=0,sz=m_Array.GetSize(); i<sz; i++) |
| 92 | delete m_Array.GetAt(i); |
| 93 | |
| 94 | m_Array.RemoveAll(); |
| 95 | } |
| 96 | |
| 97 | void SetAt(FX_UINT nIndex,CJS_Timer * pTimer) |
| 98 | { |
| 99 | int i = Find(nIndex); |
| 100 | |
| 101 | if (i>=0) |
| 102 | { |
| 103 | if (JS_TIMER_MAP * pMap = m_Array.GetAt(i)) |
| 104 | pMap->pTimer = pTimer; |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | if (JS_TIMER_MAP * pMap = new JS_TIMER_MAP) |
| 109 | { |
| 110 | pMap->nID = nIndex; |
| 111 | pMap->pTimer = pTimer; |
| 112 | m_Array.Add(pMap); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | CJS_Timer * GetAt(FX_UINT nIndex) |
| 118 | { |
| 119 | int i = Find(nIndex); |
| 120 | |
| 121 | if (i>=0) |
| 122 | { |
| 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 | { |
| 131 | int i = Find(nIndex); |
| 132 | |
| 133 | if (i>=0) |
| 134 | { |
| 135 | delete m_Array.GetAt(i); |
| 136 | m_Array.RemoveAt(i); |
| 137 | } |
| 138 | //To prevent potential fake memory leak reported by vc6. |
| 139 | if(m_Array.GetSize() == 0) |
| 140 | m_Array.RemoveAll(); |
| 141 | } |
| 142 | |
| 143 | int Find(FX_UINT nIndex) |
| 144 | { |
| 145 | for (int i=0,sz=m_Array.GetSize(); i<sz; i++) |
| 146 | { |
| 147 | if (JS_TIMER_MAP * pMap = m_Array.GetAt(i)) |
| 148 | { |
| 149 | if (pMap->nID == nIndex) |
| 150 | return i; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return -1; |
| 155 | } |
| 156 | |
| 157 | CTimerMapArray m_Array; |
| 158 | }; |
| 159 | |
| 160 | static JS_TIMER_MAPARRAY m_sTimeMap; |
| 161 | |
| 162 | class CJS_Runtime; |
| 163 | |
| 164 | class CJS_Timer |
| 165 | { |
| 166 | public: |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 167 | CJS_Timer(CJS_EmbedObj * pObj, CPDFDoc_Environment* pApp): |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 168 | m_nTimerID(0), |
Nico Weber | ec570e5 | 2014-07-30 18:59:57 -0700 | [diff] [blame] | 169 | m_pEmbedObj(pObj), |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 170 | m_bProcessing(FALSE), |
| 171 | m_dwStartTime(0), |
| 172 | m_dwTimeOut(0), |
| 173 | m_dwElapse(0), |
| 174 | m_pRuntime(NULL), |
| 175 | m_nType(0), |
| 176 | m_pApp(pApp) |
| 177 | { |
| 178 | } |
| 179 | |
| 180 | virtual ~CJS_Timer() |
| 181 | { |
| 182 | KillJSTimer(); |
| 183 | } |
| 184 | |
| 185 | public: |
| 186 | FX_UINT SetJSTimer(FX_UINT nElapse) |
| 187 | { |
| 188 | if (m_nTimerID)KillJSTimer(); |
| 189 | IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); |
| 190 | m_nTimerID = pHandler->SetTimer(nElapse,TimerProc); |
| 191 | m_sTimeMap.SetAt(m_nTimerID,this); |
| 192 | m_dwElapse = nElapse; |
| 193 | return m_nTimerID; |
| 194 | }; |
| 195 | |
| 196 | void KillJSTimer() |
| 197 | { |
| 198 | if (m_nTimerID) |
| 199 | { |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 200 | if (m_pApp == NULL) { |
| 201 | m_sTimeMap.RemoveAt(m_nTimerID); |
| 202 | m_nTimerID = 0; |
| 203 | return; |
| 204 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 205 | IFX_SystemHandler* pHandler = m_pApp->GetSysHandler(); |
| 206 | pHandler->KillTimer(m_nTimerID); |
| 207 | m_sTimeMap.RemoveAt(m_nTimerID); |
| 208 | m_nTimerID = 0; |
| 209 | } |
| 210 | }; |
| 211 | |
| 212 | void SetType(int nType) |
| 213 | { |
| 214 | m_nType = nType; |
| 215 | } |
| 216 | |
| 217 | int GetType() const |
| 218 | { |
| 219 | return m_nType; |
| 220 | } |
| 221 | |
| 222 | void SetStartTime(FX_DWORD dwStartTime) |
| 223 | { |
| 224 | m_dwStartTime = dwStartTime; |
| 225 | } |
| 226 | |
| 227 | FX_DWORD GetStartTime() const |
| 228 | { |
| 229 | return m_dwStartTime; |
| 230 | } |
| 231 | |
| 232 | void SetTimeOut(FX_DWORD dwTimeOut) |
| 233 | { |
| 234 | m_dwTimeOut = dwTimeOut; |
| 235 | } |
| 236 | |
| 237 | FX_DWORD GetTimeOut() const |
| 238 | { |
| 239 | return m_dwTimeOut; |
| 240 | } |
| 241 | |
| 242 | void SetRuntime(CJS_Runtime* pRuntime) |
| 243 | { |
| 244 | m_pRuntime = pRuntime; |
| 245 | } |
| 246 | |
| 247 | CJS_Runtime* GetRuntime() const |
| 248 | { |
| 249 | return m_pRuntime; |
| 250 | } |
| 251 | |
| 252 | void SetJScript(const CFX_WideString& script) |
| 253 | { |
| 254 | m_swJScript = script; |
| 255 | } |
| 256 | |
| 257 | CFX_WideString GetJScript() const |
| 258 | { |
| 259 | return m_swJScript; |
| 260 | } |
| 261 | |
| 262 | static void TimerProc(int idEvent) |
| 263 | { |
| 264 | if (CJS_Timer * pTimer = m_sTimeMap.GetAt(idEvent)) |
| 265 | { |
| 266 | if (!pTimer->m_bProcessing) |
| 267 | { |
| 268 | pTimer->m_bProcessing = TRUE; |
| 269 | if (pTimer->m_pEmbedObj) pTimer->m_pEmbedObj->TimerProc(pTimer); |
| 270 | pTimer->m_bProcessing = FALSE; |
| 271 | } |
| 272 | else |
| 273 | { |
| 274 | // TRACE(L"BUSY!\n"); |
| 275 | } |
| 276 | } |
| 277 | }; |
| 278 | |
| 279 | private: |
| 280 | FX_UINT m_nTimerID; |
| 281 | CJS_EmbedObj* m_pEmbedObj; |
| 282 | FX_BOOL m_bProcessing; |
| 283 | |
| 284 | //data |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 285 | FX_DWORD m_dwStartTime; |
| 286 | FX_DWORD m_dwTimeOut; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 287 | FX_DWORD m_dwElapse; |
| 288 | CJS_Runtime* m_pRuntime; |
| 289 | CFX_WideString m_swJScript; |
| 290 | int m_nType; //0:Interval; 1:TimeOut |
| 291 | |
| 292 | CPDFDoc_Environment* m_pApp; |
| 293 | }; |
| 294 | #endif //_JS_OBJECT_H_ |