Remove "using namespace v8" in header. This allows us to turn all warnings into error...
[pdfium.git] / fpdfsdk / src / javascript / JS_Runtime.cpp
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 #include "../../include/javascript/JavaScript.h"\r
8 #include "../../include/javascript/IJavaScript.h"\r
9 #include "../../include/javascript/JS_EventHandler.h"\r
10 #include "../../include/javascript/JS_Runtime.h"\r
11 #include "../../include/javascript/JS_Context.h"\r
12 #include "../../include/javascript/JS_Define.h"\r
13 #include "../../include/javascript/JS_Object.h"\r
14 #include "../../include/javascript/JS_Value.h"\r
15 #include "../../include/javascript/Document.h"\r
16 #include "../../include/javascript/app.h"\r
17 #include "../../include/javascript/color.h"\r
18 #include "../../include/javascript/Consts.h"\r
19 #include "../../include/javascript/Document.h"\r
20 #include "../../include/javascript/event.h"\r
21 #include "../../include/javascript/Field.h"\r
22 #include "../../include/javascript/Icon.h"\r
23 #include "../../include/javascript/PublicMethods.h"\r
24 #include "../../include/javascript/report.h"\r
25 #include "../../include/javascript/util.h"\r
26 #include "../../include/javascript/JS_GlobalData.h"\r
27 #include "../../include/javascript/global.h"\r
28 #include "../../include/javascript/console.h"\r
29 \r
30 CJS_RuntimeFactory::~CJS_RuntimeFactory()\r
31 {\r
32 }\r
33 \r
34 IFXJS_Runtime*                                  CJS_RuntimeFactory::NewJSRuntime(CPDFDoc_Environment* pApp)\r
35 {\r
36         if (!m_bInit)\r
37         {\r
38                 JS_Initial();\r
39                 \r
40                 m_bInit = TRUE;\r
41         }\r
42         return new CJS_Runtime(pApp);\r
43 }\r
44 void                                                    CJS_RuntimeFactory::AddRef()\r
45 {\r
46         //to do.Should be implemented as atom manipulation.\r
47         m_nRef++;\r
48 }\r
49 void                                                    CJS_RuntimeFactory::Release()\r
50 {       \r
51         if(m_bInit)\r
52         {\r
53                 //to do.Should be implemented as atom manipulation.\r
54                 if (--m_nRef == 0)\r
55                 {\r
56                         JS_Release();\r
57                         ReleaseGlobalData();\r
58                         m_bInit = FALSE;\r
59                 }\r
60         }\r
61 }\r
62 \r
63 void                                                    CJS_RuntimeFactory::DeleteJSRuntime(IFXJS_Runtime* pRuntime)\r
64 {\r
65         if(pRuntime)\r
66                 delete (CJS_Runtime*)pRuntime;\r
67 }\r
68 \r
69 CJS_GlobalData* CJS_RuntimeFactory::NewGlobalData(CPDFDoc_Environment* pApp)\r
70 {\r
71         if (m_pGlobalData)\r
72         {\r
73                 m_nGlobalDataCount++;\r
74                 return m_pGlobalData;\r
75         }\r
76         else\r
77         {\r
78                 m_nGlobalDataCount = 1;\r
79                 m_pGlobalData = new CJS_GlobalData(pApp);\r
80                 return m_pGlobalData;\r
81         }\r
82 }\r
83 \r
84 void CJS_RuntimeFactory::ReleaseGlobalData()\r
85 {\r
86         m_nGlobalDataCount--;\r
87         \r
88         if (m_nGlobalDataCount <= 0)\r
89         {\r
90                 delete m_pGlobalData;\r
91                 m_pGlobalData = NULL;\r
92         }\r
93 }\r
94 \r
95 /* ------------------------------ CJS_Runtime ------------------------------ */\r
96 \r
97 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment * pApp) : \r
98         m_pApp(pApp),\r
99         m_pDocument(NULL),\r
100         m_pFieldEventPath(NULL),\r
101         m_bBlocking(FALSE),\r
102         m_bRegistered(FALSE)\r
103 {\r
104         m_isolate = v8::Isolate::New();\r
105         //m_isolate->Enter();\r
106 \r
107         InitJSObjects();\r
108 \r
109         CJS_Context * pContext = (CJS_Context*)NewContext();\r
110         JS_InitialRuntime(*this, this, pContext, m_context);\r
111         ReleaseContext(pContext);\r
112 }\r
113 \r
114 CJS_Runtime::~CJS_Runtime()\r
115 {\r
116         for (int i=0, sz=m_ContextArray.GetSize(); i<sz; i++)\r
117                 delete m_ContextArray.GetAt(i);\r
118 \r
119         m_ContextArray.RemoveAll();\r
120 \r
121         JS_ReleaseRuntime(*this, m_context);\r
122 \r
123         RemoveEventsInLoop(m_pFieldEventPath);\r
124 \r
125         m_pApp = NULL;\r
126         m_pDocument = NULL;\r
127         m_pFieldEventPath = NULL;\r
128         m_context.Reset();\r
129 \r
130         //m_isolate->Exit();\r
131         m_isolate->Dispose();\r
132 }\r
133 \r
134 FX_BOOL CJS_Runtime::InitJSObjects()\r
135 {\r
136         v8::Isolate::Scope isolate_scope(GetIsolate());\r
137         v8::HandleScope handle_scope(GetIsolate());\r
138         v8::Handle<v8::Context> context = v8::Context::New(GetIsolate());\r
139         v8::Context::Scope context_scope(context);\r
140         //0 - 8\r
141         if (CJS_Border::Init(*this, JS_STATIC) < 0) return FALSE;\r
142         if (CJS_Display::Init(*this, JS_STATIC) < 0) return FALSE;\r
143         if (CJS_Font::Init(*this, JS_STATIC) < 0) return FALSE;\r
144         if (CJS_Highlight::Init(*this, JS_STATIC) < 0) return FALSE;\r
145         if (CJS_Position::Init(*this, JS_STATIC) < 0) return FALSE;\r
146         if (CJS_ScaleHow::Init(*this, JS_STATIC) < 0) return FALSE;\r
147         if (CJS_ScaleWhen::Init(*this, JS_STATIC) < 0) return FALSE;\r
148         if (CJS_Style::Init(*this, JS_STATIC) < 0) return FALSE;        \r
149         if (CJS_Zoomtype::Init(*this, JS_STATIC) < 0) return FALSE;     \r
150 \r
151         //9 - 11\r
152         if (CJS_App::Init(*this, JS_STATIC) < 0) return FALSE;\r
153         if (CJS_Color::Init(*this, JS_STATIC) < 0) return FALSE;   \r
154         if (CJS_Console::Init(*this, JS_STATIC) < 0) return FALSE;\r
155 \r
156         //12 - 14\r
157         if (CJS_Document::Init(*this, JS_DYNAMIC) < 0) return FALSE;  \r
158         if (CJS_Event::Init(*this, JS_STATIC) < 0) return FALSE;                \r
159         if (CJS_Field::Init(*this, JS_DYNAMIC) < 0) return FALSE;    \r
160 \r
161         //15 - 17\r
162         if (CJS_Global::Init(*this, JS_STATIC) < 0) return FALSE;               \r
163         if (CJS_Icon::Init(*this, JS_DYNAMIC) < 0) return FALSE;\r
164         if (CJS_Util::Init(*this, JS_STATIC) < 0) return FALSE;\r
165 \r
166         if (CJS_PublicMethods::Init(*this) < 0) return FALSE;\r
167         if (CJS_GlobalConsts::Init(*this) < 0) return FALSE;\r
168         if (CJS_GlobalArrays::Init(*this) < 0) return FALSE;\r
169 \r
170         if (CJS_TimerObj::Init(*this, JS_DYNAMIC) < 0) return FALSE;\r
171         if (CJS_PrintParamsObj::Init(*this, JS_DYNAMIC) <0) return FALSE;\r
172 \r
173         return TRUE;\r
174 }\r
175 \r
176 IFXJS_Context* CJS_Runtime::NewContext()\r
177 {\r
178         CJS_Context * p = new CJS_Context(this);\r
179         m_ContextArray.Add(p);\r
180         return p;\r
181 }\r
182 \r
183 void CJS_Runtime::ReleaseContext(IFXJS_Context * pContext)\r
184 {\r
185         CJS_Context* pJSContext = (CJS_Context*)pContext;\r
186 \r
187         for (int i=0, sz=m_ContextArray.GetSize(); i<sz; i++)\r
188         {\r
189                 if (pJSContext == m_ContextArray.GetAt(i))\r
190                 {\r
191                         delete pJSContext;\r
192                         m_ContextArray.RemoveAt(i);\r
193                         break;\r
194                 }\r
195         }\r
196 }\r
197 \r
198 IFXJS_Context*  CJS_Runtime::GetCurrentContext()\r
199 {\r
200         if(!m_ContextArray.GetSize())\r
201                 return NULL;\r
202         return m_ContextArray.GetAt(m_ContextArray.GetSize()-1);\r
203 }\r
204 \r
205 void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc)\r
206 {\r
207         if (m_pDocument != pReaderDoc)\r
208         {\r
209                 v8::Isolate::Scope isolate_scope(m_isolate);\r
210                 v8::HandleScope handle_scope(m_isolate);\r
211                 v8::Local<v8::Context> context =v8::Local<v8::Context>::New(m_isolate, m_context);\r
212                 v8::Context::Scope context_scope(context);\r
213 \r
214                 m_pDocument = pReaderDoc;\r
215 \r
216                 if (pReaderDoc)\r
217                 {\r
218                         JSObject pThis = JS_GetThisObj(*this);\r
219                         if(!pThis.IsEmpty())\r
220                         {\r
221                                 if (JS_GetObjDefnID(pThis) == JS_GetObjDefnID(*this, L"Document"))\r
222                                 {\r
223                                         if (CJS_Document* pJSDocument = (CJS_Document*)JS_GetPrivate(pThis))\r
224                                         {\r
225                                                 if (Document * pDocument = (Document*)pJSDocument->GetEmbedObject())\r
226                                                         pDocument->AttachDoc(pReaderDoc);\r
227                                         }\r
228                                 }\r
229                         }\r
230                         JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"Document"));\r
231                 }\r
232                 else\r
233                 {\r
234                         JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"app"));\r
235                 }\r
236         }\r
237 }\r
238 \r
239 FX_BOOL CJS_Runtime::AddEventToLoop(const CFX_WideString& sTargetName, JS_EVENT_T eEventType)\r
240 {\r
241         if (m_pFieldEventPath == NULL)\r
242         {\r
243                 m_pFieldEventPath = new CJS_FieldEvent;\r
244                 m_pFieldEventPath->sTargetName = sTargetName;\r
245                 m_pFieldEventPath->eEventType = eEventType;\r
246                 m_pFieldEventPath->pNext = NULL;\r
247 \r
248                 return TRUE;\r
249         }\r
250 \r
251         //to search\r
252         CJS_FieldEvent* p = m_pFieldEventPath;\r
253         CJS_FieldEvent* pLast = m_pFieldEventPath;\r
254         while (p)\r
255         {\r
256                 if (p->eEventType == eEventType && p->sTargetName == sTargetName)\r
257                         return FALSE;\r
258 \r
259                 pLast = p;\r
260                 p = p->pNext;\r
261         }\r
262 \r
263         //to add\r
264         CJS_FieldEvent* pNew = new CJS_FieldEvent;\r
265         pNew->sTargetName = sTargetName;\r
266         pNew->eEventType = eEventType;\r
267         pNew->pNext = NULL;\r
268 \r
269         pLast->pNext = pNew;\r
270 \r
271         return TRUE;\r
272 }\r
273 \r
274 void CJS_Runtime::RemoveEventInLoop(const CFX_WideString& sTargetName, JS_EVENT_T eEventType)\r
275 {\r
276         FX_BOOL bFind = FALSE;\r
277 \r
278         CJS_FieldEvent* p = m_pFieldEventPath;\r
279         CJS_FieldEvent* pLast = NULL;\r
280         while (p)\r
281         {\r
282                 if (p->eEventType == eEventType && p->sTargetName == sTargetName)\r
283                 {\r
284                         bFind = TRUE;\r
285                         break;\r
286                 }\r
287 \r
288                 pLast = p;\r
289                 p = p->pNext;\r
290         }\r
291 \r
292         if (bFind)\r
293         {\r
294                 RemoveEventsInLoop(p);\r
295 \r
296                 if (p == m_pFieldEventPath)\r
297                         m_pFieldEventPath = NULL;\r
298 \r
299                 if (pLast)\r
300                         pLast->pNext = NULL;\r
301         }\r
302 }\r
303 \r
304 void CJS_Runtime::RemoveEventsInLoop(CJS_FieldEvent* pStart)\r
305 {\r
306         CJS_FieldEvent* p = pStart;\r
307 \r
308         while (p)\r
309         {\r
310                 CJS_FieldEvent* pOld = p;\r
311                 p = pOld->pNext;\r
312 \r
313                 delete pOld;\r
314         }\r
315 }\r
316 \r
317 v8::Handle<v8::Context> CJS_Runtime::NewJSContext()\r
318 {\r
319         return v8::Local<v8::Context>::New(m_isolate, m_context);\r
320 }\r
321 \r
322 CFX_WideString ChangeObjName(const CFX_WideString& str)\r
323 {\r
324         CFX_WideString sRet = str;\r
325         sRet.Replace((FX_LPCWSTR)L"_", (FX_LPCWSTR)L".");\r
326         return sRet;\r
327 }\r
328 \r
329 void CJS_Runtime::GetObjectNames(CFX_WideStringArray& array)\r
330 {\r
331         array.RemoveAll();\r
332 \r
333         array.Add(CJS_Border::m_pClassName);\r
334         array.Add(CJS_Display::m_pClassName);\r
335         array.Add(CJS_Font::m_pClassName);\r
336         array.Add(CJS_Highlight::m_pClassName);\r
337         array.Add(CJS_Position::m_pClassName);\r
338         array.Add(CJS_ScaleHow::m_pClassName);\r
339         array.Add(CJS_ScaleWhen::m_pClassName);\r
340         array.Add(CJS_Style::m_pClassName);\r
341         array.Add(CJS_Zoomtype::m_pClassName);\r
342 \r
343         array.Add(CJS_App::m_pClassName);\r
344         array.Add((FX_LPCWSTR)"this"); \r
345         array.Add(CJS_Event::m_pClassName);     \r
346 \r
347         array.Add(CJS_Global::m_pClassName);    \r
348         array.Add(CJS_Util::m_pClassName);\r
349 }\r
350 \r
351 void CJS_Runtime::GetObjectConsts(const CFX_WideString& sObjName, CFX_WideStringArray& array)\r
352 {\r
353         JSConstSpec* pConsts = NULL;\r
354         int nSize = 0;\r
355 \r
356         if (sObjName == CJS_Border::m_pClassName)\r
357                 CJS_Border::GetConsts(pConsts, nSize);\r
358         else if (sObjName == CJS_Display::m_pClassName)\r
359                 CJS_Display::GetConsts(pConsts, nSize);\r
360         else if (sObjName == CJS_Font::m_pClassName)\r
361                 CJS_Font::GetConsts(pConsts, nSize);\r
362         else if (sObjName == CJS_Highlight::m_pClassName)\r
363                 CJS_Highlight::GetConsts(pConsts, nSize);\r
364         else if (sObjName == CJS_Position::m_pClassName)\r
365                 CJS_Position::GetConsts(pConsts, nSize);\r
366         else if (sObjName == CJS_ScaleHow::m_pClassName)\r
367                 CJS_ScaleHow::GetConsts(pConsts, nSize);\r
368         else if (sObjName == CJS_ScaleWhen::m_pClassName)\r
369                 CJS_ScaleWhen::GetConsts(pConsts, nSize);\r
370         else if (sObjName == CJS_Style::m_pClassName)\r
371                 CJS_Style::GetConsts(pConsts, nSize);\r
372         else if (sObjName == CJS_Zoomtype::m_pClassName)\r
373                 CJS_Zoomtype::GetConsts(pConsts, nSize);\r
374 \r
375         else if (sObjName == CJS_App::m_pClassName)\r
376                 CJS_App::GetConsts(pConsts, nSize);\r
377         else if (sObjName == CJS_Color::m_pClassName)\r
378                 CJS_Color::GetConsts(pConsts, nSize);   \r
379 \r
380         else if (sObjName == L"this") \r
381         {\r
382                 if (GetReaderDocument())\r
383                         CJS_Document::GetConsts(pConsts, nSize);\r
384                 else \r
385                         CJS_App::GetConsts(pConsts, nSize);\r
386         }\r
387 \r
388         if (sObjName == CJS_Event::m_pClassName)\r
389                 CJS_Event::GetConsts(pConsts, nSize);   \r
390         else if (sObjName == CJS_Field::m_pClassName)\r
391                 CJS_Field::GetConsts(pConsts, nSize);   \r
392         else if (sObjName == CJS_Global::m_pClassName)\r
393                 CJS_Global::GetConsts(pConsts, nSize);  \r
394         else if (sObjName == CJS_Util::m_pClassName)\r
395                 CJS_Util::GetConsts(pConsts, nSize);\r
396 \r
397         for (int i=0; i<nSize; i++)\r
398                 array.Add(pConsts[i].pName);\r
399 }\r
400 \r
401 void CJS_Runtime::GetObjectProps(const CFX_WideString& sObjName, CFX_WideStringArray& array)\r
402 {\r
403         JSPropertySpec* pProperties = NULL;\r
404         int nSize = 0;\r
405 \r
406         if (sObjName == CJS_App::m_pClassName)\r
407                 CJS_App::GetProperties(pProperties, nSize);\r
408         else if (sObjName == CJS_Color::m_pClassName)\r
409                 CJS_Color::GetProperties(pProperties, nSize); \r
410         else if (sObjName == L"this")\r
411         {\r
412                 if (GetReaderDocument())\r
413                         CJS_Document::GetProperties(pProperties, nSize);\r
414                 else    \r
415                         CJS_App::GetProperties(pProperties, nSize);\r
416         }\r
417         else if (sObjName == CJS_Event::m_pClassName)\r
418                 CJS_Event::GetProperties(pProperties, nSize);   \r
419         else if (sObjName == CJS_Field::m_pClassName)\r
420                 CJS_Field::GetProperties(pProperties, nSize);   \r
421         else if (sObjName == CJS_Global::m_pClassName)\r
422                 CJS_Global::GetProperties(pProperties, nSize);  \r
423         else if (sObjName == CJS_Util::m_pClassName)\r
424                 CJS_Util::GetProperties(pProperties, nSize);\r
425 \r
426         for (int i=0; i<nSize; i++)\r
427                 array.Add(pProperties[i].pName);\r
428 }\r
429 \r
430 void CJS_Runtime::GetObjectMethods(const CFX_WideString& sObjName, CFX_WideStringArray& array)\r
431 {\r
432         JSMethodSpec* pMethods = NULL;\r
433         int nSize = 0;\r
434 \r
435          if (sObjName == CJS_App::m_pClassName)\r
436                 CJS_App::GetMethods(pMethods, nSize);\r
437         else if (sObjName == CJS_Color::m_pClassName)\r
438                 CJS_Color::GetMethods(pMethods, nSize); \r
439         else if (sObjName == L"this") \r
440         {\r
441                 if (GetReaderDocument())\r
442                         CJS_Document::GetMethods(pMethods, nSize);\r
443                 else    \r
444                         CJS_App::GetMethods(pMethods, nSize);\r
445         }\r
446         else if (sObjName == CJS_Event::m_pClassName)\r
447                 CJS_Event::GetMethods(pMethods, nSize); \r
448         else if (sObjName == CJS_Field::m_pClassName)\r
449                 CJS_Field::GetMethods(pMethods, nSize); \r
450         else if (sObjName == CJS_Global::m_pClassName)\r
451                 CJS_Global::GetMethods(pMethods, nSize);        \r
452         else if (sObjName == CJS_Util::m_pClassName)\r
453                 CJS_Util::GetMethods(pMethods, nSize);\r
454 \r
455         for (int i=0; i<nSize; i++)\r
456                 array.Add(pMethods[i].pName);\r
457 }\r
458 \r
459 FX_BOOL  CJS_Runtime::IsEntered()\r
460 {\r
461         return v8::Isolate::GetCurrent() == m_isolate;\r
462 }\r
463 void    CJS_Runtime::Exit()\r
464 {\r
465         if(m_isolate) m_isolate->Exit();\r
466 }\r
467 void    CJS_Runtime::Enter()\r
468 {\r
469         if(m_isolate) m_isolate->Enter();\r
470 }