• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
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 
10 class CJS_Object;
11 class CJS_Timer;
12 class CJS_Context;
13 
14 class CJS_EmbedObj : public CFX_Object
15 {
16 public:
17 	CJS_EmbedObj(CJS_Object* pJSObject);
18 	virtual ~CJS_EmbedObj();
19 
TimerProc(CJS_Timer * pTimer)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 
GetJSObject()25 	CJS_Object*					GetJSObject(){return m_pJSObject;};
26 	operator					CJS_Object* (){return m_pJSObject;};
27 
28 	CPDFSDK_PageView *			JSGetPageView(IFXJS_Context* cc);
29 	int							MsgBox(CPDFDoc_Environment* pApp, CPDFSDK_PageView* pPageView, FX_LPCWSTR swMsg, FX_LPCWSTR swTitle = NULL, FX_UINT nType = 0, FX_UINT nIcon = 0);
30 	void						Alert(CJS_Context* pContext, FX_LPCWSTR swMsg);
31 	FX_BOOL						IsSafeMode(IFXJS_Context* cc);
32 
33 protected:
34 
35 	CJS_Object*					m_pJSObject;
36 };
37 
38 class CJS_Object : public CFX_Object
39 {
40 public:
41 	CJS_Object(JSFXObject pObject);
42 	virtual ~CJS_Object(void);
43 
44 	void						MakeWeak();
45 
IsType(FX_LPCSTR sClassName)46 	virtual FX_BOOL				IsType(FX_LPCSTR sClassName){return TRUE;};
GetClassName()47 	virtual CFX_ByteString		GetClassName(){return "";};
48 
InitInstance(IFXJS_Context * cc)49 	virtual FX_BOOL				InitInstance(IFXJS_Context* cc){return TRUE;};
ExitInstance()50 	virtual FX_BOOL				ExitInstance(){return TRUE;};
51 
JSFXObject()52 	operator					JSFXObject () {return v8::Local<v8::Object>::New(m_pIsolate, m_pObject);}
53 	operator					CJS_EmbedObj* (){return m_pEmbedObj;};
54 
SetEmbedObject(CJS_EmbedObj * pObj)55 	void						SetEmbedObject(CJS_EmbedObj* pObj){m_pEmbedObj = pObj;};
GetEmbedObject()56 	CJS_EmbedObj *				GetEmbedObject(){return m_pEmbedObj;};
57 
58 	static CPDFSDK_PageView *	JSGetPageView(IFXJS_Context* cc);
59 	static int					MsgBox(CPDFDoc_Environment* pApp, CPDFSDK_PageView* pPageView, FX_LPCWSTR swMsg, FX_LPCWSTR swTitle = NULL, FX_UINT nType = 0,FX_UINT nIcon = 0);
60 	static void					Alert(CJS_Context* pContext, FX_LPCWSTR swMsg);
61 
GetIsolate()62 	v8::Isolate*					GetIsolate() {return m_pIsolate;}
63 protected:
64 	CJS_EmbedObj *				m_pEmbedObj;
65 	v8::Persistent<v8::Object>			m_pObject;
66 	v8::Isolate*					m_pIsolate;
67 };
68 
69 struct JS_TIMER_MAP
70 {
71 	FX_UINT nID;
72 	CJS_Timer * pTimer;
73 };
74 
75 typedef CFX_ArrayTemplate<JS_TIMER_MAP*>	CTimerMapArray;
76 
77 struct JS_TIMER_MAPARRAY
78 {
79 public:
JS_TIMER_MAPARRAYJS_TIMER_MAPARRAY80 	JS_TIMER_MAPARRAY()
81 	{
82 	}
83 
~JS_TIMER_MAPARRAYJS_TIMER_MAPARRAY84 	~JS_TIMER_MAPARRAY()
85 	{
86 		Reset();
87 	}
88 
ResetJS_TIMER_MAPARRAY89 	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 
SetAtJS_TIMER_MAPARRAY97 	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 
GetAtJS_TIMER_MAPARRAY117 	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 
RemoveAtJS_TIMER_MAPARRAY129 	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 
FindJS_TIMER_MAPARRAY143 	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 
160 static JS_TIMER_MAPARRAY	m_sTimeMap;
161 
162 class CJS_Runtime;
163 
164 class CJS_Timer
165 {
166 public:
CJS_Timer(CJS_EmbedObj * pObj,CPDFDoc_Environment * pApp)167 	CJS_Timer(CJS_EmbedObj * pObj,CPDFDoc_Environment* pApp): m_pEmbedObj(pObj),
168 		m_nTimerID(0),
169 		m_bProcessing(FALSE),
170 		m_dwStartTime(0),
171 		m_dwTimeOut(0),
172 		m_dwElapse(0),
173 		m_pRuntime(NULL),
174 		m_nType(0),
175 		m_pApp(pApp)
176 	{
177 	}
178 
~CJS_Timer()179 	virtual ~CJS_Timer()
180 	{
181 		KillJSTimer();
182 	}
183 
184 public:
SetJSTimer(FX_UINT nElapse)185 	FX_UINT SetJSTimer(FX_UINT nElapse)
186 	{
187 		if (m_nTimerID)KillJSTimer();
188 		IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
189 		m_nTimerID = pHandler->SetTimer(nElapse,TimerProc);
190 		m_sTimeMap.SetAt(m_nTimerID,this);
191 		m_dwElapse = nElapse;
192 		return m_nTimerID;
193 	};
194 
KillJSTimer()195 	void KillJSTimer()
196 	{
197 		if (m_nTimerID)
198 		{
199 			IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
200 			pHandler->KillTimer(m_nTimerID);
201 			m_sTimeMap.RemoveAt(m_nTimerID);
202 			m_nTimerID = 0;
203 		}
204 	};
205 
SetType(int nType)206 	void SetType(int nType)
207 	{
208 		m_nType = nType;
209 	}
210 
GetType()211 	int GetType() const
212 	{
213 		return m_nType;
214 	}
215 
SetStartTime(FX_DWORD dwStartTime)216 	void SetStartTime(FX_DWORD dwStartTime)
217 	{
218 		m_dwStartTime = dwStartTime;
219 	}
220 
GetStartTime()221 	FX_DWORD GetStartTime() const
222 	{
223 		return m_dwStartTime;
224 	}
225 
SetTimeOut(FX_DWORD dwTimeOut)226 	void SetTimeOut(FX_DWORD dwTimeOut)
227 	{
228 		m_dwTimeOut = dwTimeOut;
229 	}
230 
GetTimeOut()231 	FX_DWORD GetTimeOut() const
232 	{
233 		return m_dwTimeOut;
234 	}
235 
SetRuntime(CJS_Runtime * pRuntime)236 	void SetRuntime(CJS_Runtime* pRuntime)
237 	{
238 		m_pRuntime = pRuntime;
239 	}
240 
GetRuntime()241 	CJS_Runtime* GetRuntime() const
242 	{
243 		return m_pRuntime;
244 	}
245 
SetJScript(const CFX_WideString & script)246 	void SetJScript(const CFX_WideString& script)
247 	{
248 		m_swJScript = script;
249 	}
250 
GetJScript()251 	CFX_WideString GetJScript() const
252 	{
253 		return m_swJScript;
254 	}
255 
TimerProc(int idEvent)256 	static void TimerProc(int idEvent)
257 	{
258 		if (CJS_Timer * pTimer = m_sTimeMap.GetAt(idEvent))
259 		{
260 			if (!pTimer->m_bProcessing)
261 			{
262 				pTimer->m_bProcessing = TRUE;
263 				if (pTimer->m_pEmbedObj) pTimer->m_pEmbedObj->TimerProc(pTimer);
264 				pTimer->m_bProcessing = FALSE;
265 			}
266 			else
267 			{
268 			//	TRACE(L"BUSY!\n");
269 			}
270 		}
271 	};
272 
273 private:
274 	FX_UINT							m_nTimerID;
275 	CJS_EmbedObj*					m_pEmbedObj;
276 	FX_BOOL							m_bProcessing;
277 
278 	//data
279 	FX_DWORD							m_dwStartTime;
280 	FX_DWORD							m_dwTimeOut;
281 	FX_DWORD						m_dwElapse;
282 	CJS_Runtime*					m_pRuntime;
283 	CFX_WideString					m_swJScript;
284 	int								m_nType; //0:Interval; 1:TimeOut
285 
286 	CPDFDoc_Environment*			m_pApp;
287 };
288 #endif //_JS_OBJECT_H_
289