Merge to XFA: Kill off unused IFXJS interface methods.
[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 FX_BOOL CJS_Runtime::GetHValueByName(FX_BSTR utf8Name, FXJSE_HVALUE hValue)
344 {
345         FX_LPCSTR name = utf8Name.GetCStr();
346
347         v8::Locker lock(GetIsolate());
348     v8::Isolate::Scope isolate_scope(GetIsolate());
349     v8::HandleScope handle_scope(GetIsolate());
350     v8::Local<v8::Context> context =
351         v8::Local<v8::Context>::New(GetIsolate(), m_context);
352     v8::Context::Scope context_scope(context);
353
354
355         //v8::Local<v8::Context> tmpCotext = v8::Local<v8::Context>::New(GetIsolate(), m_context);
356         v8::Local<v8::Value> propvalue = context->Global()->Get(v8::String::NewFromUtf8(GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength()));
357          
358         if (propvalue.IsEmpty()) {
359                 FXJSE_Value_SetUndefined(hValue);
360                 return FALSE;
361         }
362         ((CFXJSE_Value*)hValue)->ForceSetValue(propvalue);
363
364         return TRUE;
365 }
366 FX_BOOL CJS_Runtime::SetHValueByName(FX_BSTR utf8Name, FXJSE_HVALUE hValue)
367 {
368         if (utf8Name.IsEmpty() || hValue == NULL)
369                 return FALSE;
370         FX_LPCSTR name = utf8Name.GetCStr();
371         v8::Isolate* pIsolate = GetIsolate();
372         v8::Locker lock(pIsolate);
373     v8::Isolate::Scope isolate_scope(pIsolate);
374     v8::HandleScope handle_scope(pIsolate);
375     v8::Local<v8::Context> context =
376         v8::Local<v8::Context>::New(pIsolate, m_context);
377     v8::Context::Scope context_scope(context);
378
379         //v8::Local<v8::Context> tmpCotext = v8::Local<v8::Context>::New(GetIsolate(), m_context);
380         v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New(GetIsolate(),((CFXJSE_Value*)hValue)->DirectGetValue());
381         context->Global()->Set(v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, utf8Name.GetLength()), propvalue);
382          
383         return TRUE;
384 }