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