blob: 7ee3464dc7c7697dc77c9532998bfc32bb377977 [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.
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
10class CJS_Object;
11class CJS_Timer;
12class CJS_Context;
13
14class CJS_EmbedObj : public CFX_Object
15{
16public:
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 Xufdc00a72014-10-28 23:03:33 -070029 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 -070030 void Alert(CJS_Context* pContext, FX_LPCWSTR swMsg);
31 FX_BOOL IsSafeMode(IFXJS_Context* cc);
32
33protected:
34
35 CJS_Object* m_pJSObject;
36};
37
38class CJS_Object : public CFX_Object
39{
40public:
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 Xufdc00a72014-10-28 23:03:33 -070059 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 -070060 static void Alert(CJS_Context* pContext, FX_LPCWSTR swMsg);
61
62 v8::Isolate* GetIsolate() {return m_pIsolate;}
63protected:
64 CJS_EmbedObj * m_pEmbedObj;
65 v8::Persistent<v8::Object> m_pObject;
66 v8::Isolate* m_pIsolate;
67};
68
69struct JS_TIMER_MAP
70{
71 FX_UINT nID;
72 CJS_Timer * pTimer;
73};
74
75typedef CFX_ArrayTemplate<JS_TIMER_MAP*> CTimerMapArray;
76
77struct JS_TIMER_MAPARRAY
78{
79public:
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
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800160JS_TIMER_MAPARRAY& GetTimeMap();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161
162class CJS_Runtime;
163
164class CJS_Timer
165{
166public:
Bo Xufdc00a72014-10-28 23:03:33 -0700167 CJS_Timer(CJS_EmbedObj * pObj, CPDFDoc_Environment* pApp):
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168 m_nTimerID(0),
Nico Weberec570e52014-07-30 18:59:57 -0700169 m_pEmbedObj(pObj),
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700170 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
185public:
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);
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800191 GetTimeMap().SetAt(m_nTimerID,this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700192 m_dwElapse = nElapse;
193 return m_nTimerID;
194 };
195
196 void KillJSTimer()
197 {
198 if (m_nTimerID)
199 {
Bo Xufdc00a72014-10-28 23:03:33 -0700200 if (m_pApp == NULL) {
Bruce Dawson3872ad92015-01-05 14:35:07 -0800201 GetTimeMap().RemoveAt(m_nTimerID);
Bo Xufdc00a72014-10-28 23:03:33 -0700202 m_nTimerID = 0;
203 return;
204 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700205 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
206 pHandler->KillTimer(m_nTimerID);
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800207 GetTimeMap().RemoveAt(m_nTimerID);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700208 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 {
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800264 if (CJS_Timer * pTimer = GetTimeMap().GetAt(idEvent))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265 {
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
279private:
280 FX_UINT m_nTimerID;
281 CJS_EmbedObj* m_pEmbedObj;
282 FX_BOOL m_bProcessing;
283
284 //data
Bo Xufdc00a72014-10-28 23:03:33 -0700285 FX_DWORD m_dwStartTime;
286 FX_DWORD m_dwTimeOut;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700287 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_