blob: 02d2f57663b4e380a465cd3e02372a36aee840f6 [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
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();
49
50 virtual FX_BOOL IsType(FX_LPCSTR sClassName){return TRUE;};
51 virtual CFX_ByteString GetClassName(){return "";};
52
53 virtual FX_BOOL InitInstance(IFXJS_Context* cc){return TRUE;};
54 virtual FX_BOOL ExitInstance(){return TRUE;};
55
56 operator JSFXObject () {return v8::Local<v8::Object>::New(m_pIsolate, m_pObject);}
57 operator CJS_EmbedObj* (){return m_pEmbedObj;};
58
59 void SetEmbedObject(CJS_EmbedObj* pObj){m_pEmbedObj = pObj;};
60 CJS_EmbedObj * GetEmbedObject(){return m_pEmbedObj;};
61
62 static CPDFSDK_PageView * JSGetPageView(IFXJS_Context* cc);
Bo Xufdc00a72014-10-28 23:03:33 -070063 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 -070064 static void Alert(CJS_Context* pContext, FX_LPCWSTR swMsg);
65
66 v8::Isolate* GetIsolate() {return m_pIsolate;}
67protected:
68 CJS_EmbedObj * m_pEmbedObj;
69 v8::Persistent<v8::Object> m_pObject;
70 v8::Isolate* m_pIsolate;
71};
72
73struct JS_TIMER_MAP
74{
75 FX_UINT nID;
76 CJS_Timer * pTimer;
77};
78
79typedef CFX_ArrayTemplate<JS_TIMER_MAP*> CTimerMapArray;
80
81struct JS_TIMER_MAPARRAY
82{
83public:
84 JS_TIMER_MAPARRAY()
85 {
86 }
87
88 ~JS_TIMER_MAPARRAY()
89 {
90 Reset();
91 }
92
93 void Reset()
94 {
95 for (int i=0,sz=m_Array.GetSize(); i<sz; i++)
96 delete m_Array.GetAt(i);
97
98 m_Array.RemoveAll();
99 }
100
101 void SetAt(FX_UINT nIndex,CJS_Timer * pTimer)
102 {
103 int i = Find(nIndex);
104
105 if (i>=0)
106 {
107 if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))
108 pMap->pTimer = pTimer;
109 }
110 else
111 {
112 if (JS_TIMER_MAP * pMap = new JS_TIMER_MAP)
113 {
114 pMap->nID = nIndex;
115 pMap->pTimer = pTimer;
116 m_Array.Add(pMap);
117 }
118 }
119 }
120
121 CJS_Timer * GetAt(FX_UINT nIndex)
122 {
123 int i = Find(nIndex);
124
125 if (i>=0)
126 {
127 if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))
128 return pMap->pTimer;
129 }
130 return NULL;
131 }
132
133 void RemoveAt(FX_UINT nIndex)
134 {
135 int i = Find(nIndex);
136
137 if (i>=0)
138 {
139 delete m_Array.GetAt(i);
140 m_Array.RemoveAt(i);
141 }
142 //To prevent potential fake memory leak reported by vc6.
143 if(m_Array.GetSize() == 0)
144 m_Array.RemoveAll();
145 }
146
147 int Find(FX_UINT nIndex)
Tom Sepezc6ab1722015-02-05 15:27:25 -0800148 {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700149 for (int i=0,sz=m_Array.GetSize(); i<sz; i++)
Tom Sepezc6ab1722015-02-05 15:27:25 -0800150 {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700151 if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))
152 {
153 if (pMap->nID == nIndex)
154 return i;
155 }
156 }
157
158 return -1;
159 }
160
161 CTimerMapArray m_Array;
162};
163
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800164JS_TIMER_MAPARRAY& GetTimeMap();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700165
166class CJS_Runtime;
167
168class CJS_Timer
169{
170public:
Bo Xufdc00a72014-10-28 23:03:33 -0700171 CJS_Timer(CJS_EmbedObj * pObj, CPDFDoc_Environment* pApp):
Tom Sepezc6ab1722015-02-05 15:27:25 -0800172 m_nTimerID(0),
173 m_pEmbedObj(pObj),
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174 m_bProcessing(FALSE),
175 m_dwStartTime(0),
176 m_dwTimeOut(0),
177 m_dwElapse(0),
178 m_pRuntime(NULL),
179 m_nType(0),
180 m_pApp(pApp)
181 {
182 }
Tom Sepezc6ab1722015-02-05 15:27:25 -0800183
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700184 virtual ~CJS_Timer()
185 {
186 KillJSTimer();
187 }
188
189public:
190 FX_UINT SetJSTimer(FX_UINT nElapse)
Tom Sepezc6ab1722015-02-05 15:27:25 -0800191 {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700192 if (m_nTimerID)KillJSTimer();
193 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
194 m_nTimerID = pHandler->SetTimer(nElapse,TimerProc);
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800195 GetTimeMap().SetAt(m_nTimerID,this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700196 m_dwElapse = nElapse;
197 return m_nTimerID;
198 };
199
200 void KillJSTimer()
201 {
202 if (m_nTimerID)
203 {
Bo Xufdc00a72014-10-28 23:03:33 -0700204 if (m_pApp == NULL) {
Bruce Dawson3872ad92015-01-05 14:35:07 -0800205 GetTimeMap().RemoveAt(m_nTimerID);
Bo Xufdc00a72014-10-28 23:03:33 -0700206 m_nTimerID = 0;
207 return;
208 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700209 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
210 pHandler->KillTimer(m_nTimerID);
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800211 GetTimeMap().RemoveAt(m_nTimerID);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700212 m_nTimerID = 0;
213 }
214 };
215
216 void SetType(int nType)
217 {
218 m_nType = nType;
219 }
220
221 int GetType() const
222 {
223 return m_nType;
224 }
225
226 void SetStartTime(FX_DWORD dwStartTime)
227 {
228 m_dwStartTime = dwStartTime;
229 }
230
231 FX_DWORD GetStartTime() const
232 {
233 return m_dwStartTime;
234 }
235
236 void SetTimeOut(FX_DWORD dwTimeOut)
237 {
238 m_dwTimeOut = dwTimeOut;
239 }
240
241 FX_DWORD GetTimeOut() const
242 {
243 return m_dwTimeOut;
244 }
245
246 void SetRuntime(CJS_Runtime* pRuntime)
247 {
248 m_pRuntime = pRuntime;
249 }
Tom Sepezc6ab1722015-02-05 15:27:25 -0800250
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700251 CJS_Runtime* GetRuntime() const
252 {
253 return m_pRuntime;
254 }
255
256 void SetJScript(const CFX_WideString& script)
257 {
258 m_swJScript = script;
259 }
260
261 CFX_WideString GetJScript() const
262 {
263 return m_swJScript;
264 }
265
266 static void TimerProc(int idEvent)
267 {
Bruce Dawsonb4649dc2015-01-05 13:26:17 -0800268 if (CJS_Timer * pTimer = GetTimeMap().GetAt(idEvent))
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700269 {
270 if (!pTimer->m_bProcessing)
271 {
272 pTimer->m_bProcessing = TRUE;
273 if (pTimer->m_pEmbedObj) pTimer->m_pEmbedObj->TimerProc(pTimer);
274 pTimer->m_bProcessing = FALSE;
275 }
276 else
277 {
278 // TRACE(L"BUSY!\n");
279 }
280 }
281 };
282
283private:
Tom Sepezc6ab1722015-02-05 15:27:25 -0800284 FX_UINT m_nTimerID;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700285 CJS_EmbedObj* m_pEmbedObj;
286 FX_BOOL m_bProcessing;
287
288 //data
Bo Xufdc00a72014-10-28 23:03:33 -0700289 FX_DWORD m_dwStartTime;
290 FX_DWORD m_dwTimeOut;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700291 FX_DWORD m_dwElapse;
292 CJS_Runtime* m_pRuntime;
293 CFX_WideString m_swJScript;
294 int m_nType; //0:Interval; 1:TimeOut
295
296 CPDFDoc_Environment* m_pApp;
297};
298#endif //_JS_OBJECT_H_