Make v8 embedder data slot configurable
[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
30 CJS_RuntimeFactory::~CJS_RuntimeFactory()
31 {
32 }
33
34 IFXJS_Runtime*                                  CJS_RuntimeFactory::NewJSRuntime(CPDFDoc_Environment* pApp)
35 {
36         if (!m_bInit)
37         {
38                 unsigned int embedderDataSlot = 0;
39                 if (pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) {
40                         embedderDataSlot = pApp->GetFormFillInfo()->m_pJsPlatform->m_v8EmbedderSlot;
41                 }
42                 JS_Initial(embedderDataSlot);
43                 m_bInit = TRUE;
44         }
45         return new CJS_Runtime(pApp);
46 }
47 void                                                    CJS_RuntimeFactory::AddRef()
48 {
49         //to do.Should be implemented as atom manipulation.
50         m_nRef++;
51 }
52 void                                                    CJS_RuntimeFactory::Release()
53 {
54         if(m_bInit)
55         {
56                 //to do.Should be implemented as atom manipulation.
57                 if (--m_nRef == 0)
58                 {
59                         JS_Release();
60                         ReleaseGlobalData();
61                         m_bInit = FALSE;
62                 }
63         }
64 }
65
66 void                                                    CJS_RuntimeFactory::DeleteJSRuntime(IFXJS_Runtime* pRuntime)
67 {
68         if(pRuntime)
69                 delete (CJS_Runtime*)pRuntime;
70 }
71
72 CJS_GlobalData* CJS_RuntimeFactory::NewGlobalData(CPDFDoc_Environment* pApp)
73 {
74         if (m_pGlobalData)
75         {
76                 m_nGlobalDataCount++;
77                 return m_pGlobalData;
78         }
79         else
80         {
81                 m_nGlobalDataCount = 1;
82                 m_pGlobalData = new CJS_GlobalData(pApp);
83                 return m_pGlobalData;
84         }
85 }
86
87 void CJS_RuntimeFactory::ReleaseGlobalData()
88 {
89         m_nGlobalDataCount--;
90
91         if (m_nGlobalDataCount <= 0)
92         {
93                 delete m_pGlobalData;
94                 m_pGlobalData = NULL;
95         }
96 }
97
98 void* CJS_ArrayBufferAllocator::Allocate(size_t length) {
99     return calloc(1, length);
100 }
101
102 void* CJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) {
103     return malloc(length);
104 }
105
106 void CJS_ArrayBufferAllocator::Free(void* data, size_t length) {
107     free(data);
108 }
109
110 /* ------------------------------ CJS_Runtime ------------------------------ */
111
112 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment * pApp) :
113         m_pApp(pApp),
114         m_pDocument(NULL),
115         m_bBlocking(FALSE),
116         m_bRegistered(FALSE),
117         m_pFieldEventPath(NULL),
118         m_isolate(NULL)
119 {
120         if (m_pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) {
121                 m_isolate = reinterpret_cast<v8::Isolate*>(m_pApp->GetFormFillInfo()->m_pJsPlatform->m_isolate);
122         }
123         if (!m_isolate) {
124                 m_pArrayBufferAllocator.reset(new CJS_ArrayBufferAllocator());
125
126                 v8::Isolate::CreateParams params;
127                 params.array_buffer_allocator = m_pArrayBufferAllocator.get();
128                 m_isolate = v8::Isolate::New(params);
129         }
130
131         InitJSObjects();
132
133         CJS_Context * pContext = (CJS_Context*)NewContext();
134         JS_InitialRuntime(*this, this, pContext, m_context);
135         ReleaseContext(pContext);
136 }
137
138 CJS_Runtime::~CJS_Runtime()
139 {
140         for (int i=0, sz=m_ContextArray.GetSize(); i<sz; i++)
141                 delete m_ContextArray.GetAt(i);
142
143         m_ContextArray.RemoveAll();
144
145         JS_ReleaseRuntime(*this, m_context);
146
147         RemoveEventsInLoop(m_pFieldEventPath);
148
149         m_pApp = NULL;
150         m_pDocument = NULL;
151         m_pFieldEventPath = NULL;
152         m_context.Reset();
153
154         //m_isolate->Exit();
155         m_isolate->Dispose();
156 }
157
158 FX_BOOL CJS_Runtime::InitJSObjects()
159 {
160         v8::Isolate::Scope isolate_scope(GetIsolate());
161         v8::HandleScope handle_scope(GetIsolate());
162         v8::Local<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::HandleScope handle_scope(m_isolate);
235                 v8::Local<v8::Context> context =v8::Local<v8::Context>::New(m_isolate, m_context);
236                 v8::Context::Scope context_scope(context);
237
238                 m_pDocument = pReaderDoc;
239
240                 if (pReaderDoc)
241                 {
242                         JSObject pThis = JS_GetThisObj(*this);
243                         if(!pThis.IsEmpty())
244                         {
245                                 if (JS_GetObjDefnID(pThis) == JS_GetObjDefnID(*this, L"Document"))
246                                 {
247                                         if (CJS_Document* pJSDocument = (CJS_Document*)JS_GetPrivate(pThis))
248                                         {
249                                                 if (Document * pDocument = (Document*)pJSDocument->GetEmbedObject())
250                                                         pDocument->AttachDoc(pReaderDoc);
251                                         }
252                                 }
253                         }
254                         JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"Document"));
255                 }
256                 else
257                 {
258                         JS_SetThisObj(*this, JS_GetObjDefnID(*this, L"app"));
259                 }
260         }
261 }
262
263 FX_BOOL CJS_Runtime::AddEventToLoop(const CFX_WideString& sTargetName, JS_EVENT_T eEventType)
264 {
265         if (m_pFieldEventPath == NULL)
266         {
267                 m_pFieldEventPath = new CJS_FieldEvent;
268                 m_pFieldEventPath->sTargetName = sTargetName;
269                 m_pFieldEventPath->eEventType = eEventType;
270                 m_pFieldEventPath->pNext = NULL;
271
272                 return TRUE;
273         }
274
275         //to search
276         CJS_FieldEvent* p = m_pFieldEventPath;
277         CJS_FieldEvent* pLast = m_pFieldEventPath;
278         while (p)
279         {
280                 if (p->eEventType == eEventType && p->sTargetName == sTargetName)
281                         return FALSE;
282
283                 pLast = p;
284                 p = p->pNext;
285         }
286
287         //to add
288         CJS_FieldEvent* pNew = new CJS_FieldEvent;
289         pNew->sTargetName = sTargetName;
290         pNew->eEventType = eEventType;
291         pNew->pNext = NULL;
292
293         pLast->pNext = pNew;
294
295         return TRUE;
296 }
297
298 void CJS_Runtime::RemoveEventInLoop(const CFX_WideString& sTargetName, JS_EVENT_T eEventType)
299 {
300         FX_BOOL bFind = FALSE;
301
302         CJS_FieldEvent* p = m_pFieldEventPath;
303         CJS_FieldEvent* pLast = NULL;
304         while (p)
305         {
306                 if (p->eEventType == eEventType && p->sTargetName == sTargetName)
307                 {
308                         bFind = TRUE;
309                         break;
310                 }
311
312                 pLast = p;
313                 p = p->pNext;
314         }
315
316         if (bFind)
317         {
318                 RemoveEventsInLoop(p);
319
320                 if (p == m_pFieldEventPath)
321                         m_pFieldEventPath = NULL;
322
323                 if (pLast)
324                         pLast->pNext = NULL;
325         }
326 }
327
328 void CJS_Runtime::RemoveEventsInLoop(CJS_FieldEvent* pStart)
329 {
330         CJS_FieldEvent* p = pStart;
331
332         while (p)
333         {
334                 CJS_FieldEvent* pOld = p;
335                 p = pOld->pNext;
336
337                 delete pOld;
338         }
339 }
340
341 v8::Local<v8::Context>  CJS_Runtime::NewJSContext()
342 {
343         return v8::Local<v8::Context>::New(m_isolate, m_context);
344 }
345
346 CFX_WideString ChangeObjName(const CFX_WideString& str)
347 {
348         CFX_WideString sRet = str;
349         sRet.Replace(L"_", L".");
350         return sRet;
351 }