blob: 3e1f74beed390a09e4a8e54ddd9d5914d01448c9 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Tom Sepez37458412015-10-06 11:33:46 -07007#include "JS_Runtime.h"
8
Bo Xufdc00a72014-10-28 23:03:33 -07009#include "../../../xfa/src/fxjse/src/value.h"
Tom Sepez37458412015-10-06 11:33:46 -070010#include "../../include/fpdfxfa/fpdfxfa_app.h"
11#include "../../include/fsdk_mgr.h" // For CPDFDoc_Environment.
12#include "../../include/javascript/IJavaScript.h"
13#include "Consts.h"
14#include "Document.h"
15#include "Field.h"
16#include "Icon.h"
17#include "JS_Context.h"
18#include "JS_Define.h"
19#include "JS_EventHandler.h"
20#include "JS_GlobalData.h"
21#include "JS_Object.h"
22#include "JS_Value.h"
23#include "PublicMethods.h"
24#include "app.h"
25#include "color.h"
26#include "console.h"
27#include "event.h"
28#include "global.h"
29#include "report.h"
30#include "util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070032/* ------------------------------ CJS_Runtime ------------------------------ */
Tom Sepez808a99e2015-09-10 12:28:37 -070033v8::Global<v8::ObjectTemplate>& _getGlobalObjectTemplate(v8::Isolate* pIsolate);
34
Tom Sepez67fd5df2015-10-08 12:24:19 -070035// static
Tom Sepez452b4f32015-10-13 09:27:27 -070036void IJS_Runtime::Initialize(unsigned int slot, void* isolate) {
37 FXJS_Initialize(slot, reinterpret_cast<v8::Isolate*>(isolate));
38}
39
40// static
Tom Sepezba038bc2015-10-08 12:03:00 -070041IJS_Runtime* IJS_Runtime::Create(CPDFDoc_Environment* pEnv) {
Tom Sepez37458412015-10-06 11:33:46 -070042 return new CJS_Runtime(pEnv);
43}
44
Tom Sepez67fd5df2015-10-08 12:24:19 -070045// static
46CJS_Runtime* CJS_Runtime::FromContext(const IJS_Context* cc) {
47 const CJS_Context* pContext = static_cast<const CJS_Context*>(cc);
48 return pContext->GetJSRuntime();
49}
50
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp)
52 : m_pApp(pApp),
53 m_pDocument(NULL),
54 m_bBlocking(FALSE),
Jochen Eisinger29007842015-08-05 09:02:13 +020055 m_isolate(NULL),
56 m_isolateManaged(false) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070057 if (CPDFXFA_App::GetInstance()->GetJSERuntime()) {
58 // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate.
59 m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime();
Tom Sepeza72e8e22015-10-07 10:17:53 -070060 } else {
61 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform;
62 if (pPlatform->version <= 2) {
63 unsigned int embedderDataSlot = 0;
64 v8::Isolate* pExternalIsolate = nullptr;
65 if (pPlatform->version == 2) {
66 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate);
67 embedderDataSlot = pPlatform->m_v8EmbedderSlot;
68 }
69 FXJS_Initialize(embedderDataSlot, pExternalIsolate);
70 }
71 m_isolateManaged = FXJS_GetIsolate(&m_isolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 }
Tom Sepezd2cc1b92015-04-30 15:19:03 -070073
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 v8::Isolate* isolate = m_isolate;
75 v8::Isolate::Scope isolate_scope(isolate);
76 v8::Locker locker(isolate);
77 v8::HandleScope handle_scope(isolate);
Tom Sepezed7b2b52015-09-22 08:36:17 -070078 if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 CJS_Context* pContext = (CJS_Context*)NewContext();
Tom Sepez4237aed2015-11-10 15:19:17 -080080 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects);
Tom Sepez2f2ffec2015-07-23 14:42:09 -070081 ReleaseContext(pContext);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 return;
83 }
84
Lei Zhang3fa115b2015-10-08 12:04:47 -070085 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0)
86 DefineJSObjects();
87
Tom Sepezed7b2b52015-09-22 08:36:17 -070088 CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089
90 CJS_Context* pContext = (CJS_Context*)NewContext();
Tom Sepez4237aed2015-11-10 15:19:17 -080091 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 ReleaseContext(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093}
94
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095CJS_Runtime::~CJS_Runtime() {
Lei Zhang2d5a0e12015-10-05 17:00:03 -070096 for (auto* obs : m_observers)
97 obs->OnDestroyed();
98
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 int size = m_ContextArray.GetSize();
100 for (int i = 0; i < size; i++)
101 delete m_ContextArray.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 m_ContextArray.RemoveAll();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700104
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 m_pApp = NULL;
106 m_pDocument = NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700107 m_context.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700108
Jochen Eisinger29007842015-08-05 09:02:13 +0200109 if (m_isolateManaged)
110 m_isolate->Dispose();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 m_isolate = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700112}
113
Tom Sepez142165e2015-09-11 13:21:50 -0700114void CJS_Runtime::DefineJSObjects() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700115 v8::Isolate::Scope isolate_scope(GetIsolate());
116 v8::Locker locker(GetIsolate());
117 v8::HandleScope handle_scope(GetIsolate());
118 v8::Local<v8::Context> context = v8::Context::New(GetIsolate());
119 v8::Context::Scope context_scope(context);
Tom Sepez570875c2015-09-11 08:35:03 -0700120
121 // The call order determines the "ObjDefID" assigned to each class.
122 // ObjDefIDs 0 - 2
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700123 CJS_Border::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
124 CJS_Display::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
125 CJS_Font::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700126
Tom Sepez570875c2015-09-11 08:35:03 -0700127 // ObjDefIDs 3 - 5
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700128 CJS_Highlight::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
129 CJS_Position::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
130 CJS_ScaleHow::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700131
Tom Sepez570875c2015-09-11 08:35:03 -0700132 // ObjDefIDs 6 - 8
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700133 CJS_ScaleWhen::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
134 CJS_Style::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
135 CJS_Zoomtype::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700136
Tom Sepez570875c2015-09-11 08:35:03 -0700137 // ObjDefIDs 9 - 11
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700138 CJS_App::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
139 CJS_Color::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
140 CJS_Console::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141
Tom Sepez570875c2015-09-11 08:35:03 -0700142 // ObjDefIDs 12 - 14
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700143 CJS_Document::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_GLOBAL);
144 CJS_Event::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
145 CJS_Field::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146
Tom Sepez570875c2015-09-11 08:35:03 -0700147 // ObjDefIDs 15 - 17
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700148 CJS_Global::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
149 CJS_Icon::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
150 CJS_Util::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
Tom Sepez570875c2015-09-11 08:35:03 -0700151
Tom Sepez142165e2015-09-11 13:21:50 -0700152 // ObjDefIDs 18 - 20 (these can't fail, return void).
153 CJS_PublicMethods::DefineJSObjects(GetIsolate());
Tom Sepez67fd5df2015-10-08 12:24:19 -0700154 CJS_GlobalConsts::DefineJSObjects(this);
155 CJS_GlobalArrays::DefineJSObjects(this);
Tom Sepez570875c2015-09-11 08:35:03 -0700156
Tom Sepez142165e2015-09-11 13:21:50 -0700157 // ObjDefIDs 21 - 22.
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700158 CJS_TimerObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
159 CJS_PrintParamsObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700160}
161
Tom Sepezba038bc2015-10-08 12:03:00 -0700162IJS_Context* CJS_Runtime::NewContext() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 CJS_Context* p = new CJS_Context(this);
164 m_ContextArray.Add(p);
165 return p;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700166}
167
Tom Sepezba038bc2015-10-08 12:03:00 -0700168void CJS_Runtime::ReleaseContext(IJS_Context* pContext) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 CJS_Context* pJSContext = (CJS_Context*)pContext;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700170
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) {
172 if (pJSContext == m_ContextArray.GetAt(i)) {
173 delete pJSContext;
174 m_ContextArray.RemoveAt(i);
175 break;
176 }
177 }
178}
179
Tom Sepezba038bc2015-10-08 12:03:00 -0700180IJS_Context* CJS_Runtime::GetCurrentContext() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181 if (!m_ContextArray.GetSize())
182 return NULL;
183 return m_ContextArray.GetAt(m_ContextArray.GetSize() - 1);
184}
185
186void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) {
187 if (m_pDocument != pReaderDoc) {
188 v8::Isolate::Scope isolate_scope(m_isolate);
189 v8::Locker locker(m_isolate);
190 v8::HandleScope handle_scope(m_isolate);
191 v8::Local<v8::Context> context =
192 v8::Local<v8::Context>::New(m_isolate, m_context);
193 v8::Context::Scope context_scope(context);
194
195 m_pDocument = pReaderDoc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 if (pReaderDoc) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700197 v8::Local<v8::Object> pThis = FXJS_GetThisObj(GetIsolate());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700198 if (!pThis.IsEmpty()) {
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700199 if (FXJS_GetObjDefnID(pThis) == CJS_Document::g_nObjDefnID) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700200 if (CJS_Document* pJSDocument =
Tom Sepezd5a0e952015-09-17 15:40:06 -0700201 (CJS_Document*)FXJS_GetPrivate(GetIsolate(), pThis)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 if (Document* pDocument = (Document*)pJSDocument->GetEmbedObject())
203 pDocument->AttachDoc(pReaderDoc);
204 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700205 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700207 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700209}
210
Tom Sepez33420902015-10-13 15:00:10 -0700211int CJS_Runtime::Execute(IJS_Context* cc,
212 const wchar_t* script,
213 CFX_WideString* info) {
214 FXJSErr error = {};
215 int nRet = FXJS_Execute(m_isolate, cc, script, &error);
216 if (nRet < 0) {
217 info->Format(L"[ Line: %05d { %s } ] : %s", error.linnum - 1, error.srcline,
218 error.message);
219 }
220 return nRet;
221}
222
Tom Sepez5d0e8432015-09-22 15:50:03 -0700223bool CJS_Runtime::AddEventToSet(const FieldEvent& event) {
224 return m_FieldEventSet.insert(event).second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700225}
226
Tom Sepez5d0e8432015-09-22 15:50:03 -0700227void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) {
228 m_FieldEventSet.erase(event);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700229}
230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231v8::Local<v8::Context> CJS_Runtime::NewJSContext() {
232 return v8::Local<v8::Context>::New(m_isolate, m_context);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233}
234
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235CFX_WideString ChangeObjName(const CFX_WideString& str) {
236 CFX_WideString sRet = str;
237 sRet.Replace(L"_", L".");
238 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700239}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240FX_BOOL CJS_Runtime::GetHValueByName(const CFX_ByteStringC& utf8Name,
241 FXJSE_HVALUE hValue) {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700242#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700243 const FX_CHAR* name = utf8Name.GetCStr();
Bo Xufdc00a72014-10-28 23:03:33 -0700244
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700245 v8::Locker lock(GetIsolate());
246 v8::Isolate::Scope isolate_scope(GetIsolate());
247 v8::HandleScope handle_scope(GetIsolate());
Tom Sepez4f4603c2015-11-10 15:03:12 -0800248 v8::Local<v8::Context> old_context = GetIsolate()->GetCurrentContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700249 v8::Local<v8::Context> context =
250 v8::Local<v8::Context>::New(GetIsolate(), m_context);
251 v8::Context::Scope context_scope(context);
Bo Xufdc00a72014-10-28 23:03:33 -0700252
Tom Sepez4f4603c2015-11-10 15:03:12 -0800253 // Caution: We're about to hand to XFA an object that in order to invoke
254 // methods will require that the current v8::Context always has a pointer
255 // to a CJS_Runtime in its embedder data slot. Unfortunately, XFA creates
256 // its own v8::Context which has not initialized the embedder data slot.
257 // Do so now.
258 // TODO(tsepez): redesign PDF-side objects to not rely on v8::Context's
259 // embedder data slots, and/or to always use the right context.
260 FXJS_SetRuntimeForV8Context(old_context, this);
261
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 v8::Local<v8::Value> propvalue =
263 context->Global()->Get(v8::String::NewFromUtf8(
264 GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength()));
Bo Xufdc00a72014-10-28 23:03:33 -0700265
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 if (propvalue.IsEmpty()) {
267 FXJSE_Value_SetUndefined(hValue);
268 return FALSE;
269 }
270 ((CFXJSE_Value*)hValue)->ForceSetValue(propvalue);
Tom Sepeza8a39e22015-10-12 15:47:07 -0700271#endif
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700272
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700274}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700275FX_BOOL CJS_Runtime::SetHValueByName(const CFX_ByteStringC& utf8Name,
276 FXJSE_HVALUE hValue) {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700277#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 if (utf8Name.IsEmpty() || hValue == NULL)
279 return FALSE;
280 const FX_CHAR* name = utf8Name.GetCStr();
281 v8::Isolate* pIsolate = GetIsolate();
282 v8::Locker lock(pIsolate);
283 v8::Isolate::Scope isolate_scope(pIsolate);
284 v8::HandleScope handle_scope(pIsolate);
285 v8::Local<v8::Context> context =
286 v8::Local<v8::Context>::New(pIsolate, m_context);
287 v8::Context::Scope context_scope(context);
Bo Xufdc00a72014-10-28 23:03:33 -0700288
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 // v8::Local<v8::Context> tmpCotext =
290 // v8::Local<v8::Context>::New(GetIsolate(), m_context);
291 v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New(
292 GetIsolate(), ((CFXJSE_Value*)hValue)->DirectGetValue());
293 context->Global()->Set(
294 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString,
295 utf8Name.GetLength()),
296 propvalue);
Tom Sepeza8a39e22015-10-12 15:47:07 -0700297#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700299}
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700300
301void CJS_Runtime::AddObserver(Observer* observer) {
302 ASSERT(m_observers.find(observer) == m_observers.end());
303 m_observers.insert(observer);
304}
305
306void CJS_Runtime::RemoveObserver(Observer* observer) {
307 ASSERT(m_observers.find(observer) != m_observers.end());
308 m_observers.erase(observer);
309}