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