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
7 #ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_
8 #define FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_
10 #include "../fsdk_define.h" // For FX_UINT
11 #include "../fsdk_mgr.h" // For CPDFDoc_Environment
12 #include "../fx_systemhandler.h" // For IFX_SystemHandler
13 #include "../jsapi/fxjs_v8.h"
15 class CPDFSDK_PageView;
23 CJS_EmbedObj(CJS_Object* pJSObject);
24 virtual ~CJS_EmbedObj();
26 virtual void TimerProc(CJS_Timer* pTimer){};
28 CJS_Timer* BeginTimer(CPDFDoc_Environment * pApp, FX_UINT nElapse);
29 void EndTimer(CJS_Timer* pTimer);
31 CJS_Object* GetJSObject(){return m_pJSObject;};
32 operator CJS_Object* (){return m_pJSObject;};
34 CPDFSDK_PageView * JSGetPageView(IFXJS_Context* cc);
35 int MsgBox(CPDFDoc_Environment* pApp, CPDFSDK_PageView* pPageView, const FX_WCHAR* swMsg, const FX_WCHAR* swTitle = NULL, FX_UINT nType = 0, FX_UINT nIcon = 0);
36 void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg);
39 CJS_Object* m_pJSObject;
45 CJS_Object(JSFXObject pObject);
46 virtual ~CJS_Object(void);
51 virtual FX_BOOL IsType(const FX_CHAR* sClassName){return TRUE;};
52 virtual CFX_ByteString GetClassName(){return "";};
54 virtual FX_BOOL InitInstance(IFXJS_Context* cc){return TRUE;};
55 virtual FX_BOOL ExitInstance(){return TRUE;};
57 operator JSFXObject () {return v8::Local<v8::Object>::New(m_pIsolate, m_pObject);}
58 operator CJS_EmbedObj* (){return m_pEmbedObj;};
60 void SetEmbedObject(CJS_EmbedObj* pObj){m_pEmbedObj = pObj;};
61 CJS_EmbedObj * GetEmbedObject(){return m_pEmbedObj;};
63 static CPDFSDK_PageView * JSGetPageView(IFXJS_Context* cc);
64 static int MsgBox(CPDFDoc_Environment* pApp, CPDFSDK_PageView* pPageView, const FX_WCHAR* swMsg, const FX_WCHAR* swTitle = NULL, FX_UINT nType = 0,FX_UINT nIcon = 0);
65 static void Alert(CJS_Context* pContext, const FX_WCHAR* swMsg);
67 v8::Isolate* GetIsolate() {return m_pIsolate;}
69 CJS_EmbedObj * m_pEmbedObj;
70 v8::Global<v8::Object> m_pObject;
71 v8::Isolate* m_pIsolate;
80 typedef CFX_ArrayTemplate<JS_TIMER_MAP*> CTimerMapArray;
82 struct JS_TIMER_MAPARRAY
96 for (int i=0,sz=m_Array.GetSize(); i<sz; i++)
97 delete m_Array.GetAt(i);
102 void SetAt(FX_UINT nIndex,CJS_Timer * pTimer)
104 int i = Find(nIndex);
108 if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))
109 pMap->pTimer = pTimer;
113 if (JS_TIMER_MAP * pMap = new JS_TIMER_MAP)
116 pMap->pTimer = pTimer;
122 CJS_Timer * GetAt(FX_UINT nIndex)
124 int i = Find(nIndex);
128 if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))
134 void RemoveAt(FX_UINT nIndex)
136 int i = Find(nIndex);
140 delete m_Array.GetAt(i);
143 //To prevent potential fake memory leak reported by vc6.
144 if(m_Array.GetSize() == 0)
148 int Find(FX_UINT nIndex)
150 for (int i=0,sz=m_Array.GetSize(); i<sz; i++)
152 if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))
154 if (pMap->nID == nIndex)
162 CTimerMapArray m_Array;
165 JS_TIMER_MAPARRAY& GetTimeMap();
172 CJS_Timer(CJS_EmbedObj * pObj,CPDFDoc_Environment* pApp):
175 m_bProcessing(FALSE),
191 FX_UINT SetJSTimer(FX_UINT nElapse)
193 if (m_nTimerID)KillJSTimer();
194 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
195 m_nTimerID = pHandler->SetTimer(nElapse,TimerProc);
196 GetTimeMap().SetAt(m_nTimerID,this);
197 m_dwElapse = nElapse;
205 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
206 pHandler->KillTimer(m_nTimerID);
207 GetTimeMap().RemoveAt(m_nTimerID);
212 void SetType(int nType)
222 void SetStartTime(FX_DWORD dwStartTime)
224 m_dwStartTime = dwStartTime;
227 FX_DWORD GetStartTime() const
229 return m_dwStartTime;
232 void SetTimeOut(FX_DWORD dwTimeOut)
234 m_dwTimeOut = dwTimeOut;
237 FX_DWORD GetTimeOut() const
242 void SetRuntime(CJS_Runtime* pRuntime)
244 m_pRuntime = pRuntime;
247 CJS_Runtime* GetRuntime() const
252 void SetJScript(const CFX_WideString& script)
254 m_swJScript = script;
257 CFX_WideString GetJScript() const
262 static void TimerProc(int idEvent)
264 if (CJS_Timer * pTimer = GetTimeMap().GetAt(idEvent))
266 if (!pTimer->m_bProcessing)
268 pTimer->m_bProcessing = TRUE;
269 if (pTimer->m_pEmbedObj) pTimer->m_pEmbedObj->TimerProc(pTimer);
270 pTimer->m_bProcessing = FALSE;
274 // TRACE(L"BUSY!\n");
281 CJS_EmbedObj* m_pEmbedObj;
282 FX_BOOL m_bProcessing;
285 FX_DWORD m_dwStartTime;
286 FX_DWORD m_dwTimeOut;
288 CJS_Runtime* m_pRuntime;
289 CFX_WideString m_swJScript;
290 int m_nType; //0:Interval; 1:TimeOut
292 CPDFDoc_Environment* m_pApp;
295 #endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_