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