Provide an array buffer allocator to V8.
[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 #include "../jsapi/fxjs_v8.h"
14
15 class CPDFSDK_PageView;
16 class CJS_Object;
17 class CJS_Timer;
18 class CJS_Context;
19
20 class CJS_EmbedObj 
21 {
22 public:
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);
35         int                                                     MsgBox(CPDFDoc_Environment* pApp, CPDFSDK_PageView* pPageView, FX_LPCWSTR swMsg, FX_LPCWSTR swTitle = NULL, FX_UINT nType = 0, FX_UINT nIcon = 0);
36         void                                            Alert(CJS_Context* pContext, FX_LPCWSTR swMsg);
37
38 protected:
39         CJS_Object*                                     m_pJSObject;
40 };
41
42 class CJS_Object 
43 {
44 public:
45         CJS_Object(JSFXObject pObject);
46         virtual ~CJS_Object(void);
47
48         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);
63         static int                                      MsgBox(CPDFDoc_Environment* pApp, CPDFSDK_PageView* pPageView, FX_LPCWSTR swMsg, FX_LPCWSTR swTitle = NULL, FX_UINT nType = 0,FX_UINT nIcon = 0);
64         static void                                     Alert(CJS_Context* pContext, FX_LPCWSTR swMsg);
65
66         v8::Isolate*                                    GetIsolate() {return m_pIsolate;}
67 protected:
68         CJS_EmbedObj *                          m_pEmbedObj;
69         v8::Persistent<v8::Object>                      m_pObject;
70         v8::Isolate*                                    m_pIsolate;
71 };
72
73 struct JS_TIMER_MAP
74 {
75         FX_UINT nID;
76         CJS_Timer * pTimer;
77 };
78
79 typedef CFX_ArrayTemplate<JS_TIMER_MAP*>        CTimerMapArray;
80
81 struct JS_TIMER_MAPARRAY
82 {
83 public:
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)
148         {
149                 for (int i=0,sz=m_Array.GetSize(); i<sz; i++)
150                 {
151                         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
164 JS_TIMER_MAPARRAY& GetTimeMap();
165
166 class CJS_Runtime;
167
168 class CJS_Timer
169 {
170 public:
171         CJS_Timer(CJS_EmbedObj * pObj,CPDFDoc_Environment* pApp):
172                 m_nTimerID(0),
173                 m_pEmbedObj(pObj),
174                 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         }
183
184         virtual ~CJS_Timer()
185         {
186                 KillJSTimer();
187         }
188
189 public:
190         FX_UINT SetJSTimer(FX_UINT nElapse)
191         {
192                 if (m_nTimerID)KillJSTimer();
193                 IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
194                 m_nTimerID = pHandler->SetTimer(nElapse,TimerProc);
195                 GetTimeMap().SetAt(m_nTimerID,this);
196                 m_dwElapse = nElapse;
197                 return m_nTimerID;
198         };
199
200         void KillJSTimer()
201         {
202                 if (m_nTimerID)
203                 {
204                         IFX_SystemHandler* pHandler = m_pApp->GetSysHandler();
205                         pHandler->KillTimer(m_nTimerID);
206                         GetTimeMap().RemoveAt(m_nTimerID);
207                         m_nTimerID = 0;
208                 }
209         };
210
211         void SetType(int nType)
212         {
213                 m_nType = nType;
214         }
215
216         int GetType() const
217         {
218                 return m_nType;
219         }
220
221         void SetStartTime(FX_DWORD dwStartTime)
222         {
223                 m_dwStartTime = dwStartTime;
224         }
225
226         FX_DWORD GetStartTime() const
227         {
228                 return m_dwStartTime;
229         }
230
231         void SetTimeOut(FX_DWORD dwTimeOut)
232         {
233                 m_dwTimeOut = dwTimeOut;
234         }
235
236         FX_DWORD GetTimeOut() const
237         {
238                 return m_dwTimeOut;
239         }
240
241         void SetRuntime(CJS_Runtime* pRuntime)
242         {
243                 m_pRuntime = pRuntime;
244         }
245
246         CJS_Runtime* GetRuntime() const
247         {
248                 return m_pRuntime;
249         }
250
251         void SetJScript(const CFX_WideString& script)
252         {
253                 m_swJScript = script;
254         }
255
256         CFX_WideString GetJScript() const
257         {
258                 return m_swJScript;
259         }
260
261         static void TimerProc(int idEvent)
262         {
263                 if (CJS_Timer * pTimer = GetTimeMap().GetAt(idEvent))
264                 {
265                         if (!pTimer->m_bProcessing)
266                         {
267                                 pTimer->m_bProcessing = TRUE;
268                                 if (pTimer->m_pEmbedObj) pTimer->m_pEmbedObj->TimerProc(pTimer);
269                                 pTimer->m_bProcessing = FALSE;
270                         }
271                         else
272                         {
273                         //      TRACE(L"BUSY!\n");
274                         }
275                 }
276         };
277
278 private:
279         FX_UINT                                                 m_nTimerID;
280         CJS_EmbedObj*                                   m_pEmbedObj;
281         FX_BOOL                                                 m_bProcessing;
282
283         //data
284         FX_DWORD                                                        m_dwStartTime;
285         FX_DWORD                                                        m_dwTimeOut;
286         FX_DWORD                                                m_dwElapse;
287         CJS_Runtime*                                    m_pRuntime;
288         CFX_WideString                                  m_swJScript;
289         int                                                             m_nType; //0:Interval; 1:TimeOut
290
291         CPDFDoc_Environment*                    m_pApp;
292 };
293 #endif //_JS_OBJECT_H_