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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
10 #include "../fsdk_define.h" // For FX_UINT
11 #include "../fsdk_mgr.h" // For CPDFDoc_Environment
12 #include "../fx_systemhandler.h" // For IFX_SystemHandler
14 class CPDFSDK_PageView;
19 class CJS_EmbedObj : public CFX_Object
22 CJS_EmbedObj(CJS_Object* pJSObject);
23 virtual ~CJS_EmbedObj();
25 virtual void TimerProc(CJS_Timer* pTimer){};
27 CJS_Timer* BeginTimer(CPDFDoc_Environment * pApp, FX_UINT nElapse);
28 void EndTimer(CJS_Timer* pTimer);
30 CJS_Object* GetJSObject(){return m_pJSObject;};
31 operator CJS_Object* (){return m_pJSObject;};
33 CPDFSDK_PageView * JSGetPageView(IFXJS_Context* cc);
34 int MsgBox(CPDFDoc_Environment * pApp, CPDFSDK_PageView* pPageView, FX_LPCWSTR swMsg, FX_LPCWSTR swTitle = NULL, FX_UINT nType = 0, FX_UINT nIcon = 0);
35 void Alert(CJS_Context* pContext, FX_LPCWSTR swMsg);
38 CJS_Object* m_pJSObject;
41 class CJS_Object : public CFX_Object
44 CJS_Object(JSFXObject pObject);
45 virtual ~CJS_Object(void);
49 virtual FX_BOOL IsType(FX_LPCSTR sClassName){return TRUE;};
50 virtual CFX_ByteString GetClassName(){return "";};
52 virtual FX_BOOL InitInstance(IFXJS_Context* cc){return TRUE;};
53 virtual FX_BOOL ExitInstance(){return TRUE;};
55 operator JSFXObject () {return v8::Local<v8::Object>::New(m_pIsolate, m_pObject);}
56 operator CJS_EmbedObj* (){return m_pEmbedObj;};
58 void SetEmbedObject(CJS_EmbedObj* pObj){m_pEmbedObj = pObj;};
59 CJS_EmbedObj * GetEmbedObject(){return m_pEmbedObj;};
61 static CPDFSDK_PageView * JSGetPageView(IFXJS_Context* cc);
62 static int MsgBox(CPDFDoc_Environment * pApp, CPDFSDK_PageView* pPageView, FX_LPCWSTR swMsg, FX_LPCWSTR swTitle = NULL, FX_UINT nType = 0,FX_UINT nIcon = 0);
63 static void Alert(CJS_Context* pContext, FX_LPCWSTR swMsg);
65 v8::Isolate* GetIsolate() {return m_pIsolate;}
67 CJS_EmbedObj * m_pEmbedObj;
68 v8::Persistent<v8::Object> m_pObject;
69 v8::Isolate* m_pIsolate;
78 typedef CFX_ArrayTemplate<JS_TIMER_MAP*> CTimerMapArray;
80 struct JS_TIMER_MAPARRAY
94 for (int i=0,sz=m_Array.GetSize(); i<sz; i++)
95 delete m_Array.GetAt(i);
100 void SetAt(FX_UINT nIndex,CJS_Timer * pTimer)
102 int i = Find(nIndex);
106 if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))
107 pMap->pTimer = pTimer;
111 if (JS_TIMER_MAP * pMap = new JS_TIMER_MAP)
114 pMap->pTimer = pTimer;
120 CJS_Timer * GetAt(FX_UINT nIndex)
122 int i = Find(nIndex);
126 if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))
132 void RemoveAt(FX_UINT nIndex)
134 int i = Find(nIndex);
138 delete m_Array.GetAt(i);
141 //To prevent potential fake memory leak reported by vc6.
142 if(m_Array.GetSize() == 0)
146 int Find(FX_UINT nIndex)
148 for (int i=0,sz=m_Array.GetSize(); i<sz; i++)
150 if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))
152 if (pMap->nID == nIndex)
160 CTimerMapArray m_Array;
163 JS_TIMER_MAPARRAY& GetTimeMap();
170 CJS_Timer(CJS_EmbedObj * pObj, CPDFDoc_Environment* pApp):
173 m_bProcessing(FALSE),
189 FX_UINT SetJSTimer(FX_UINT nElapse)
191 if (m_nTimerID)KillJSTimer();
192 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
193 m_nTimerID = pHandler->SetTimer(nElapse,TimerProc);
194 GetTimeMap().SetAt(m_nTimerID,this);
195 m_dwElapse = nElapse;
203 if (m_pApp == NULL) {
204 GetTimeMap().RemoveAt(m_nTimerID);
208 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
209 pHandler->KillTimer(m_nTimerID);
210 GetTimeMap().RemoveAt(m_nTimerID);
215 void SetType(int nType)
225 void SetStartTime(FX_DWORD dwStartTime)
227 m_dwStartTime = dwStartTime;
230 FX_DWORD GetStartTime() const
232 return m_dwStartTime;
235 void SetTimeOut(FX_DWORD dwTimeOut)
237 m_dwTimeOut = dwTimeOut;
240 FX_DWORD GetTimeOut() const
245 void SetRuntime(CJS_Runtime* pRuntime)
247 m_pRuntime = pRuntime;
250 CJS_Runtime* GetRuntime() const
255 void SetJScript(const CFX_WideString& script)
257 m_swJScript = script;
260 CFX_WideString GetJScript() const
265 static void TimerProc(int idEvent)
267 if (CJS_Timer * pTimer = GetTimeMap().GetAt(idEvent))
269 if (!pTimer->m_bProcessing)
271 pTimer->m_bProcessing = TRUE;
272 if (pTimer->m_pEmbedObj) pTimer->m_pEmbedObj->TimerProc(pTimer);
273 pTimer->m_bProcessing = FALSE;
277 // TRACE(L"BUSY!\n");
284 CJS_EmbedObj* m_pEmbedObj;
285 FX_BOOL m_bProcessing;
288 FX_DWORD m_dwStartTime;
289 FX_DWORD m_dwTimeOut;
291 CJS_Runtime* m_pRuntime;
292 CFX_WideString m_swJScript;
293 int m_nType; //0:Interval; 1:TimeOut
295 CPDFDoc_Environment* m_pApp;
297 #endif //_JS_OBJECT_H_