blob: 3b97677fa6ea64b5776848ac22531639d9f041dc [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
Tom Sepez19922bb2015-05-28 13:23:12 -07007#ifndef FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_
8#define FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009
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
Tom Sepez9a3f8122015-04-07 15:35:48 -070013#include "../jsapi/fxjs_v8.h"
Tom Sepez3a832662015-03-02 12:59:05 -080014
15class CPDFSDK_PageView;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070016class CJS_Object;
17class CJS_Timer;
18class CJS_Context;
19
Tom Sepez6fc8cbb2015-04-14 13:50:34 -070020class CJS_EmbedObj
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070021{
22public:
23 CJS_EmbedObj(CJS_Object* pJSObject);
24 virtual ~CJS_EmbedObj();
25
26 virtual void TimerProc(CJS_Timer* pTimer){};
27
28 CJS_Timer* BeginTimer(CPDFDoc_Environment * pApp, FX_UINT nElapse);
29 void EndTimer(CJS_Timer* pTimer);
30
31 CJS_Object* GetJSObject(){return m_pJSObject;};
32 operator CJS_Object* (){return m_pJSObject;};
33
34 CPDFSDK_PageView * JSGetPageView(IFXJS_Context* cc);
Bo Xufdc00a72014-10-28 23:03:33 -070035 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 -070036 void Alert(CJS_Context* pContext, FX_LPCWSTR swMsg);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070037
38protected:
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039 CJS_Object* m_pJSObject;
40};
41
Tom Sepez6fc8cbb2015-04-14 13:50:34 -070042class CJS_Object
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043{
44public:
45 CJS_Object(JSFXObject pObject);
46 virtual ~CJS_Object(void);
Tom Sepezc6ab1722015-02-05 15:27:25 -080047
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070048 void MakeWeak();
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020049 void Dispose();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050
51 virtual FX_BOOL IsType(FX_LPCSTR sClassName){return TRUE;};
52 virtual CFX_ByteString GetClassName(){return "";};
53
54 virtual FX_BOOL InitInstance(IFXJS_Context* cc){return TRUE;};
55 virtual FX_BOOL ExitInstance(){return TRUE;};
56
57 operator JSFXObject () {return v8::Local<v8::Object>::New(m_pIsolate, m_pObject);}
58 operator CJS_EmbedObj* (){return m_pEmbedObj;};
59
60 void SetEmbedObject(CJS_EmbedObj* pObj){m_pEmbedObj = pObj;};
61 CJS_EmbedObj * GetEmbedObject(){return m_pEmbedObj;};
62
63 static CPDFSDK_PageView * JSGetPageView(IFXJS_Context* cc);
Bo Xufdc00a72014-10-28 23:03:33 -070064 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 -070065 static void Alert(CJS_Context* pContext, FX_LPCWSTR swMsg);
66
67 v8::Isolate* GetIsolate() {return m_pIsolate;}
68protected:
69 CJS_EmbedObj * m_pEmbedObj;
Jochen Eisingerdfa2c992015-05-19 00:38:00 +020070 v8::Global<v8::Object> m_pObject;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070071 v8::Isolate* m_pIsolate;
72};
73
74struct JS_TIMER_MAP
75{
76 FX_UINT nID;
77 CJS_Timer * pTimer;
78};
79
80typedef CFX_ArrayTemplate<JS_TIMER_MAP*> CTimerMapArray;
81
82struct JS_TIMER_MAPARRAY
83{
84public:
85 JS_TIMER_MAPARRAY()
86 {
87 }
88
89 ~JS_TIMER_MAPARRAY()
90 {
91 Reset();
92 }
93
94 void Reset()
95 {
96 for (int i=0,sz=m_Array.GetSize(); i<sz; i++)
97 delete m_Array.GetAt(i);
98
99 m_Array.RemoveAll();
100 }
101
102 void SetAt(FX_UINT nIndex,CJS_Timer * pTimer)
103 {
104 int i = Find(nIndex);
105
106 if (i>=0)
107 {
108 if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))
109 pMap->pTimer = pTimer;
110 }
111 else
112 {
113 if (JS_TIMER_MAP * pMap = new JS_TIMER_MAP)
114 {
115 pMap->nID = nIndex;
116 pMap->pTimer = pTimer;
117 m_Array.Add(pMap);
118 }
119 }
120 }
121
122 CJS_Timer * GetAt(FX_UINT nIndex)
123 {
124 int i = Find(nIndex);
125
126 if (i>=0)
127 {
128 if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))
129 return pMap->pTimer;
130 }
131 return NULL;
132 }
133
134 void RemoveAt(FX_UINT nIndex)
135 {
136 int i = Find(nIndex);
137
138 if (i>=0)
139 {
140 delete m_Array.GetAt(i);
141 m_Array.RemoveAt(i);
142 }
143 //To prevent potential fake memory leak reported by vc6.
144 if(m_Array.GetSize() == 0)
145 m_Array.RemoveAll();
146 }
147
148 int Find(FX_UINT nIndex)
Tom Sepezc6ab1722015-02-05 15:27:25 -0800149 {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700150 for (int i=0,sz=m_Array.GetSize(); i<sz; i++)
Tom Sepezc6ab1722015-02-05 15:27:25 -0800151 {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152 if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))
153 {
154 if (pMap->nID == nIndex)
155 return i;
156 }
157 }
158
159 return -1;
160 }
161
162 CTimerMapArray m_Array;
163};
164
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800165JS_TIMER_MAPARRAY& GetTimeMap();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700166
167class CJS_Runtime;
168
169class CJS_Timer
170{
171public:
Bo Xufdc00a72014-10-28 23:03:33 -0700172 CJS_Timer(CJS_EmbedObj * pObj, CPDFDoc_Environment* pApp):
Tom Sepezc6ab1722015-02-05 15:27:25 -0800173 m_nTimerID(0),
174 m_pEmbedObj(pObj),
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700175 m_bProcessing(FALSE),
176 m_dwStartTime(0),
177 m_dwTimeOut(0),
178 m_dwElapse(0),
179 m_pRuntime(NULL),
180 m_nType(0),
181 m_pApp(pApp)
182 {
183 }
Tom Sepezc6ab1722015-02-05 15:27:25 -0800184
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700185 virtual ~CJS_Timer()
186 {
187 KillJSTimer();
188 }
189
190public:
191 FX_UINT SetJSTimer(FX_UINT nElapse)
Tom Sepezc6ab1722015-02-05 15:27:25 -0800192 {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700193 if (m_nTimerID)KillJSTimer();
194 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
195 m_nTimerID = pHandler->SetTimer(nElapse,TimerProc);
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800196 GetTimeMap().SetAt(m_nTimerID,this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700197 m_dwElapse = nElapse;
198 return m_nTimerID;
199 };
200
201 void KillJSTimer()
202 {
203 if (m_nTimerID)
204 {
Bo Xufdc00a72014-10-28 23:03:33 -0700205 if (m_pApp == NULL) {
Bruce Dawson3872ad92015-01-05 14:35:07 -0800206 GetTimeMap().RemoveAt(m_nTimerID);
Bo Xufdc00a72014-10-28 23:03:33 -0700207 m_nTimerID = 0;
208 return;
209 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700210 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
211 pHandler->KillTimer(m_nTimerID);
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800212 GetTimeMap().RemoveAt(m_nTimerID);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700213 m_nTimerID = 0;
214 }
215 };
216
217 void SetType(int nType)
218 {
219 m_nType = nType;
220 }
221
222 int GetType() const
223 {
224 return m_nType;
225 }
226
227 void SetStartTime(FX_DWORD dwStartTime)
228 {
229 m_dwStartTime = dwStartTime;
230 }
231
232 FX_DWORD GetStartTime() const
233 {
234 return m_dwStartTime;
235 }
236
237 void SetTimeOut(FX_DWORD dwTimeOut)
238 {
239 m_dwTimeOut = dwTimeOut;
240 }
241
242 FX_DWORD GetTimeOut() const
243 {
244 return m_dwTimeOut;
245 }
246
247 void SetRuntime(CJS_Runtime* pRuntime)
248 {
249 m_pRuntime = pRuntime;
250 }
Tom Sepezc6ab1722015-02-05 15:27:25 -0800251
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252 CJS_Runtime* GetRuntime() const
253 {
254 return m_pRuntime;
255 }
256
257 void SetJScript(const CFX_WideString& script)
258 {
259 m_swJScript = script;
260 }
261
262 CFX_WideString GetJScript() const
263 {
264 return m_swJScript;
265 }
266
267 static void TimerProc(int idEvent)
268 {
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800269 if (CJS_Timer * pTimer = GetTimeMap().GetAt(idEvent))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700270 {
271 if (!pTimer->m_bProcessing)
272 {
273 pTimer->m_bProcessing = TRUE;
274 if (pTimer->m_pEmbedObj) pTimer->m_pEmbedObj->TimerProc(pTimer);
275 pTimer->m_bProcessing = FALSE;
276 }
277 else
278 {
279 // TRACE(L"BUSY!\n");
280 }
281 }
282 };
283
284private:
Tom Sepezc6ab1722015-02-05 15:27:25 -0800285 FX_UINT m_nTimerID;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700286 CJS_EmbedObj* m_pEmbedObj;
287 FX_BOOL m_bProcessing;
288
289 //data
Bo Xufdc00a72014-10-28 23:03:33 -0700290 FX_DWORD m_dwStartTime;
291 FX_DWORD m_dwTimeOut;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700292 FX_DWORD m_dwElapse;
293 CJS_Runtime* m_pRuntime;
294 CFX_WideString m_swJScript;
295 int m_nType; //0:Interval; 1:TimeOut
296
297 CPDFDoc_Environment* m_pApp;
298};
Tom Sepez19922bb2015-05-28 13:23:12 -0700299
300#endif // FPDFSDK_INCLUDE_JAVASCRIPT_JS_OBJECT_H_