Initial commit.
[pdfium.git] / fpdfsdk / include / javascript / JS_Object.h
1 // Copyright 2014 PDFium Authors. All rights reserved.\r
2 // Use of this source code is governed by a BSD-style license that can be\r
3 // found in the LICENSE file.\r
4  \r
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
6 \r
7 #ifndef _JS_OBJECT_H_\r
8 #define _JS_OBJECT_H_\r
9 \r
10 class CJS_Object;\r
11 class CJS_Timer;\r
12 class CJS_Context;\r
13 \r
14 class CJS_EmbedObj : public CFX_Object\r
15 {\r
16 public:\r
17         CJS_EmbedObj(CJS_Object* pJSObject);\r
18         virtual ~CJS_EmbedObj();\r
19 \r
20         virtual void                            TimerProc(CJS_Timer* pTimer){};\r
21 \r
22         CJS_Timer*                                      BeginTimer(CPDFDoc_Environment * pApp, FX_UINT nElapse);\r
23         void                                            EndTimer(CJS_Timer* pTimer);\r
24 \r
25         CJS_Object*                                     GetJSObject(){return m_pJSObject;};\r
26         operator                                        CJS_Object* (){return m_pJSObject;};\r
27 \r
28         CPDFSDK_PageView *                      JSGetPageView(IFXJS_Context* cc);\r
29         int                                                     MsgBox(CPDFDoc_Environment* pApp, CPDFSDK_PageView* pPageView, FX_LPCWSTR swMsg, FX_LPCWSTR swTitle = NULL, FX_UINT nType = 0, FX_UINT nIcon = 0);\r
30         void                                            Alert(CJS_Context* pContext, FX_LPCWSTR swMsg);\r
31         FX_BOOL                                         IsSafeMode(IFXJS_Context* cc);\r
32 \r
33 protected:\r
34 \r
35         CJS_Object*                                     m_pJSObject;\r
36 };\r
37 \r
38 class CJS_Object : public CFX_Object\r
39 {\r
40 public:\r
41         CJS_Object(JSFXObject pObject);\r
42         virtual ~CJS_Object(void);\r
43         \r
44         void                                            MakeWeak();\r
45 \r
46         virtual FX_BOOL                         IsType(FX_LPCSTR sClassName){return TRUE;};\r
47         virtual CFX_ByteString          GetClassName(){return "";};\r
48 \r
49         virtual FX_BOOL                         InitInstance(IFXJS_Context* cc){return TRUE;};\r
50         virtual FX_BOOL                         ExitInstance(){return TRUE;};\r
51 \r
52         operator                                        JSFXObject () {return v8::Local<v8::Object>::New(m_pIsolate, m_pObject);}\r
53         operator                                        CJS_EmbedObj* (){return m_pEmbedObj;};\r
54 \r
55         void                                            SetEmbedObject(CJS_EmbedObj* pObj){m_pEmbedObj = pObj;};\r
56         CJS_EmbedObj *                          GetEmbedObject(){return m_pEmbedObj;};\r
57 \r
58         static CPDFSDK_PageView *       JSGetPageView(IFXJS_Context* cc);\r
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);\r
60         static void                                     Alert(CJS_Context* pContext, FX_LPCWSTR swMsg);\r
61 \r
62         v8::Isolate*                                    GetIsolate() {return m_pIsolate;}\r
63 protected:\r
64         CJS_EmbedObj *                          m_pEmbedObj;\r
65         v8::Persistent<v8::Object>                      m_pObject;\r
66         v8::Isolate*                                    m_pIsolate;\r
67 };\r
68 \r
69 struct JS_TIMER_MAP\r
70 {\r
71         FX_UINT nID;\r
72         CJS_Timer * pTimer;\r
73 };\r
74 \r
75 typedef CFX_ArrayTemplate<JS_TIMER_MAP*>        CTimerMapArray;\r
76 \r
77 struct JS_TIMER_MAPARRAY\r
78 {\r
79 public:\r
80         JS_TIMER_MAPARRAY()\r
81         {\r
82         }\r
83 \r
84         ~JS_TIMER_MAPARRAY()\r
85         {\r
86                 Reset();\r
87         }\r
88 \r
89         void Reset()\r
90         {\r
91                 for (int i=0,sz=m_Array.GetSize(); i<sz; i++)\r
92                         delete m_Array.GetAt(i);\r
93 \r
94                 m_Array.RemoveAll();\r
95         }\r
96 \r
97         void SetAt(FX_UINT nIndex,CJS_Timer * pTimer)\r
98         {\r
99                 int i = Find(nIndex);\r
100 \r
101                 if (i>=0)\r
102                 {\r
103                         if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))\r
104                                 pMap->pTimer = pTimer;\r
105                 }\r
106                 else\r
107                 {\r
108                         if (JS_TIMER_MAP * pMap = new JS_TIMER_MAP)\r
109                         {\r
110                                 pMap->nID = nIndex;\r
111                                 pMap->pTimer = pTimer;\r
112                                 m_Array.Add(pMap);\r
113                         }\r
114                 }\r
115         }\r
116 \r
117         CJS_Timer * GetAt(FX_UINT nIndex)\r
118         {\r
119                 int i = Find(nIndex);\r
120 \r
121                 if (i>=0)\r
122                 {\r
123                         if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))\r
124                                 return pMap->pTimer;\r
125                 }\r
126                 return NULL;\r
127         }\r
128 \r
129         void RemoveAt(FX_UINT nIndex)\r
130         {\r
131                 int i = Find(nIndex);\r
132 \r
133                 if (i>=0)\r
134                 {\r
135                         delete m_Array.GetAt(i);\r
136                         m_Array.RemoveAt(i);\r
137                 }\r
138                 //To prevent potential fake memory leak reported by vc6.\r
139                 if(m_Array.GetSize() == 0)\r
140                         m_Array.RemoveAll();\r
141         }\r
142 \r
143         int Find(FX_UINT nIndex)\r
144         {               \r
145                 for (int i=0,sz=m_Array.GetSize(); i<sz; i++)\r
146                 {                       \r
147                         if (JS_TIMER_MAP * pMap = m_Array.GetAt(i))\r
148                         {\r
149                                 if (pMap->nID == nIndex)\r
150                                         return i;\r
151                         }\r
152                 }\r
153 \r
154                 return -1;\r
155         }\r
156 \r
157         CTimerMapArray          m_Array;\r
158 };\r
159 \r
160 static JS_TIMER_MAPARRAY        m_sTimeMap;\r
161 \r
162 class CJS_Runtime;\r
163 \r
164 class CJS_Timer\r
165 {\r
166 public:\r
167         CJS_Timer(CJS_EmbedObj * pObj,CPDFDoc_Environment* pApp): m_pEmbedObj(pObj), \r
168                 m_nTimerID(0), \r
169                 m_bProcessing(FALSE),\r
170                 m_dwStartTime(0),\r
171                 m_dwTimeOut(0),\r
172                 m_dwElapse(0),\r
173                 m_pRuntime(NULL),\r
174                 m_nType(0),\r
175                 m_pApp(pApp)\r
176         {\r
177         }\r
178         \r
179         virtual ~CJS_Timer()\r
180         {\r
181                 KillJSTimer();\r
182         }\r
183 \r
184 public:\r
185         FX_UINT SetJSTimer(FX_UINT nElapse)\r
186         {       \r
187                 if (m_nTimerID)KillJSTimer();\r
188                 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();\r
189                 m_nTimerID = pHandler->SetTimer(nElapse,TimerProc);\r
190                 m_sTimeMap.SetAt(m_nTimerID,this);\r
191                 m_dwElapse = nElapse;\r
192                 return m_nTimerID;\r
193         };\r
194 \r
195         void KillJSTimer()\r
196         {\r
197                 if (m_nTimerID)\r
198                 {\r
199                         IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();\r
200                         pHandler->KillTimer(m_nTimerID);\r
201                         m_sTimeMap.RemoveAt(m_nTimerID);\r
202                         m_nTimerID = 0;\r
203                 }\r
204         };\r
205 \r
206         void SetType(int nType)\r
207         {\r
208                 m_nType = nType;\r
209         }\r
210 \r
211         int GetType() const\r
212         {\r
213                 return m_nType;\r
214         }\r
215 \r
216         void SetStartTime(FX_DWORD dwStartTime)\r
217         {\r
218                 m_dwStartTime = dwStartTime;\r
219         }\r
220 \r
221         FX_DWORD GetStartTime() const\r
222         {\r
223                 return m_dwStartTime;\r
224         }\r
225 \r
226         void SetTimeOut(FX_DWORD dwTimeOut)\r
227         {\r
228                 m_dwTimeOut = dwTimeOut;\r
229         }\r
230 \r
231         FX_DWORD GetTimeOut() const\r
232         {\r
233                 return m_dwTimeOut;\r
234         }\r
235 \r
236         void SetRuntime(CJS_Runtime* pRuntime)\r
237         {\r
238                 m_pRuntime = pRuntime;\r
239         }\r
240         \r
241         CJS_Runtime* GetRuntime() const\r
242         {\r
243                 return m_pRuntime;\r
244         }\r
245 \r
246         void SetJScript(const CFX_WideString& script)\r
247         {\r
248                 m_swJScript = script;\r
249         }\r
250 \r
251         CFX_WideString GetJScript() const\r
252         {\r
253                 return m_swJScript;\r
254         }\r
255 \r
256         static void TimerProc(int idEvent)\r
257         {\r
258                 if (CJS_Timer * pTimer = m_sTimeMap.GetAt(idEvent))\r
259                 {\r
260                         if (!pTimer->m_bProcessing)\r
261                         {\r
262                                 pTimer->m_bProcessing = TRUE;\r
263                                 if (pTimer->m_pEmbedObj) pTimer->m_pEmbedObj->TimerProc(pTimer);\r
264                                 pTimer->m_bProcessing = FALSE;\r
265                         }\r
266                         else\r
267                         {\r
268                         //      TRACE(L"BUSY!\n");\r
269                         }\r
270                 }\r
271         };\r
272 \r
273 private:\r
274         FX_UINT                                                 m_nTimerID;     \r
275         CJS_EmbedObj*                                   m_pEmbedObj;\r
276         FX_BOOL                                                 m_bProcessing;\r
277 \r
278         //data\r
279         FX_DWORD                                                        m_dwStartTime;\r
280         FX_DWORD                                                        m_dwTimeOut;\r
281         FX_DWORD                                                m_dwElapse;\r
282         CJS_Runtime*                                    m_pRuntime;\r
283         CFX_WideString                                  m_swJScript;\r
284         int                                                             m_nType; //0:Interval; 1:TimeOut\r
285 \r
286         CPDFDoc_Environment*                    m_pApp;\r
287 };\r
288 #endif //_JS_OBJECT_H_\r