blob: f30bc17ce60c32faebf1e4ea9e679c70d34ebcf9 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Tom Sepezc6ab1722015-02-05 15:27:25 -08004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#ifndef _JS_OBJECT_H_
8#define _JS_OBJECT_H_
9
Tom Sepez3a832662015-03-02 12:59:05 -080010#include "../fsdk_define.h" // For FX_UINT
11#include "../fsdk_mgr.h" // For CPDFDoc_Environment
12#include "../fx_systemhandler.h" // For IFX_SystemHandler
13
14class CPDFSDK_PageView;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070015class CJS_Object;
16class CJS_Timer;
17class CJS_Context;
18
19class CJS_EmbedObj : public CFX_Object
20{
21public:
22 CJS_EmbedObj(CJS_Object* pJSObject);
23 virtual ~CJS_EmbedObj();
24
25 virtual void TimerProc(CJS_Timer* pTimer){};
26
27 CJS_Timer* BeginTimer(CPDFDoc_Environment * pApp, FX_UINT nElapse);
28 void EndTimer(CJS_Timer* pTimer);
29
30 CJS_Object* GetJSObject(){return m_pJSObject;};
31 operator CJS_Object* (){return m_pJSObject;};
32
33 CPDFSDK_PageView * JSGetPageView(IFXJS_Context* cc);
Bo Xufdc00a72014-10-28 23:03:33 -070034 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-Malek3f3b45c2014-05-23 17:28:10 -070035 void Alert(CJS_Context* pContext, FX_LPCWSTR swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070036
37protected:
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038 CJS_Object* m_pJSObject;
39};
40
41class CJS_Object : public CFX_Object
42{
43public:
44 CJS_Object(JSFXObject pObject);
45 virtual ~CJS_Object(void);
Tom Sepezc6ab1722015-02-05 15:27:25 -080046
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047 void MakeWeak();
48
49 virtual FX_BOOL IsType(FX_LPCSTR sClassName){return TRUE;};
50 virtual CFX_ByteString GetClassName(){return "";};
51
52 virtual FX_BOOL InitInstance(IFXJS_Context* cc){return TRUE;};
53 virtual FX_BOOL ExitInstance(){return TRUE;};
54
55 operator JSFXObject () {return v8::Local<v8::Object>::New(m_pIsolate, m_pObject);}
56 operator CJS_EmbedObj* (){return m_pEmbedObj;};
57
58 void SetEmbedObject(CJS_EmbedObj* pObj){m_pEmbedObj = pObj;};
59 CJS_EmbedObj * GetEmbedObject(){return m_pEmbedObj;};
60
61 static CPDFSDK_PageView * JSGetPageView(IFXJS_Context* cc);
Bo Xufdc00a72014-10-28 23:03:33 -070062 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-Malek3f3b45c2014-05-23 17:28:10 -070063 static void Alert(CJS_Context* pContext, FX_LPCWSTR swMsg);
64
65 v8::Isolate* GetIsolate() {return m_pIsolate;}
66protected:
67 CJS_EmbedObj * m_pEmbedObj;
68 v8::Persistent<v8::Object> m_pObject;
69 v8::Isolate* m_pIsolate;
70};
71
72struct JS_TIMER_MAP
73{
74 FX_UINT nID;
75 CJS_Timer * pTimer;
76};
77
78typedef CFX_ArrayTemplate<JS_TIMER_MAP*> CTimerMapArray;
79
80struct JS_TIMER_MAPARRAY
81{
82public:
83 JS_TIMER_MAPARRAY()
84 {
85 }
86
87 ~JS_TIMER_MAPARRAY()
88 {
89 Reset();
90 }
91
92 void Reset()
93 {
94 for (int i=0,sz=m_Array.GetSize(); i<sz; i++)
95 delete m_Array.GetAt(i);
96
97 m_Array.RemoveAll();
98 }
99
100 void SetAt(FX_UINT nIndex,CJS_Timer * pTimer)
101 {
102 int i = Find(nIndex);
103
104 if (i>=0)
105 {
106 if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))
107 pMap->pTimer = pTimer;
108 }
109 else
110 {
111 if (JS_TIMER_MAP * pMap = new JS_TIMER_MAP)
112 {
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 {
122 int i = Find(nIndex);
123
124 if (i>=0)
125 {
126 if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))
127 return pMap->pTimer;
128 }
129 return NULL;
130 }
131
132 void RemoveAt(FX_UINT nIndex)
133 {
134 int i = Find(nIndex);
135
136 if (i>=0)
137 {
138 delete m_Array.GetAt(i);
139 m_Array.RemoveAt(i);
140 }
141 //To prevent potential fake memory leak reported by vc6.
142 if(m_Array.GetSize() == 0)
143 m_Array.RemoveAll();
144 }
145
146 int Find(FX_UINT nIndex)
Tom Sepezc6ab1722015-02-05 15:27:25 -0800147 {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700148 for (int i=0,sz=m_Array.GetSize(); i<sz; i++)
Tom Sepezc6ab1722015-02-05 15:27:25 -0800149 {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700150 if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))
151 {
152 if (pMap->nID == nIndex)
153 return i;
154 }
155 }
156
157 return -1;
158 }
159
160 CTimerMapArray m_Array;
161};
162
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800163JS_TIMER_MAPARRAY& GetTimeMap();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700164
165class CJS_Runtime;
166
167class CJS_Timer
168{
169public:
Bo Xufdc00a72014-10-28 23:03:33 -0700170 CJS_Timer(CJS_EmbedObj * pObj, CPDFDoc_Environment* pApp):
Tom Sepezc6ab1722015-02-05 15:27:25 -0800171 m_nTimerID(0),
172 m_pEmbedObj(pObj),
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700173 m_bProcessing(FALSE),
174 m_dwStartTime(0),
175 m_dwTimeOut(0),
176 m_dwElapse(0),
177 m_pRuntime(NULL),
178 m_nType(0),
179 m_pApp(pApp)
180 {
181 }
Tom Sepezc6ab1722015-02-05 15:27:25 -0800182
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700183 virtual ~CJS_Timer()
184 {
185 KillJSTimer();
186 }
187
188public:
189 FX_UINT SetJSTimer(FX_UINT nElapse)
Tom Sepezc6ab1722015-02-05 15:27:25 -0800190 {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700191 if (m_nTimerID)KillJSTimer();
192 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
193 m_nTimerID = pHandler->SetTimer(nElapse,TimerProc);
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800194 GetTimeMap().SetAt(m_nTimerID,this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195 m_dwElapse = nElapse;
196 return m_nTimerID;
197 };
198
199 void KillJSTimer()
200 {
201 if (m_nTimerID)
202 {
Bo Xufdc00a72014-10-28 23:03:33 -0700203 if (m_pApp == NULL) {
Bruce Dawson3872ad92015-01-05 14:35:07 -0800204 GetTimeMap().RemoveAt(m_nTimerID);
Bo Xufdc00a72014-10-28 23:03:33 -0700205 m_nTimerID = 0;
206 return;
207 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700208 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
209 pHandler->KillTimer(m_nTimerID);
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800210 GetTimeMap().RemoveAt(m_nTimerID);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700211 m_nTimerID = 0;
212 }
213 };
214
215 void SetType(int nType)
216 {
217 m_nType = nType;
218 }
219
220 int GetType() const
221 {
222 return m_nType;
223 }
224
225 void SetStartTime(FX_DWORD dwStartTime)
226 {
227 m_dwStartTime = dwStartTime;
228 }
229
230 FX_DWORD GetStartTime() const
231 {
232 return m_dwStartTime;
233 }
234
235 void SetTimeOut(FX_DWORD dwTimeOut)
236 {
237 m_dwTimeOut = dwTimeOut;
238 }
239
240 FX_DWORD GetTimeOut() const
241 {
242 return m_dwTimeOut;
243 }
244
245 void SetRuntime(CJS_Runtime* pRuntime)
246 {
247 m_pRuntime = pRuntime;
248 }
Tom Sepezc6ab1722015-02-05 15:27:25 -0800249
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700250 CJS_Runtime* GetRuntime() const
251 {
252 return m_pRuntime;
253 }
254
255 void SetJScript(const CFX_WideString& script)
256 {
257 m_swJScript = script;
258 }
259
260 CFX_WideString GetJScript() const
261 {
262 return m_swJScript;
263 }
264
265 static void TimerProc(int idEvent)
266 {
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800267 if (CJS_Timer * pTimer = GetTimeMap().GetAt(idEvent))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700268 {
269 if (!pTimer->m_bProcessing)
270 {
271 pTimer->m_bProcessing = TRUE;
272 if (pTimer->m_pEmbedObj) pTimer->m_pEmbedObj->TimerProc(pTimer);
273 pTimer->m_bProcessing = FALSE;
274 }
275 else
276 {
277 // TRACE(L"BUSY!\n");
278 }
279 }
280 };
281
282private:
Tom Sepezc6ab1722015-02-05 15:27:25 -0800283 FX_UINT m_nTimerID;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700284 CJS_EmbedObj* m_pEmbedObj;
285 FX_BOOL m_bProcessing;
286
287 //data
Bo Xufdc00a72014-10-28 23:03:33 -0700288 FX_DWORD m_dwStartTime;
289 FX_DWORD m_dwTimeOut;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700290 FX_DWORD m_dwElapse;
291 CJS_Runtime* m_pRuntime;
292 CFX_WideString m_swJScript;
293 int m_nType; //0:Interval; 1:TimeOut
294
295 CPDFDoc_Environment* m_pApp;
296};
297#endif //_JS_OBJECT_H_