blob: 1d2fa84323bd200bde57e9c7fa47e406faf720d6 [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 Sepez39bfe122015-09-17 15:25:23 -070080 FXJS_InitializeRuntime(GetIsolate(), this, pContext, m_context);
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 Sepez39bfe122015-09-17 15:25:23 -070091 FXJS_InitializeRuntime(GetIsolate(), this, pContext, m_context);
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 Sepez5d0e8432015-09-22 15:50:03 -0700211bool CJS_Runtime::AddEventToSet(const FieldEvent& event) {
212 return m_FieldEventSet.insert(event).second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700213}
214
Tom Sepez5d0e8432015-09-22 15:50:03 -0700215void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) {
216 m_FieldEventSet.erase(event);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217}
218
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700219v8::Local<v8::Context> CJS_Runtime::NewJSContext() {
220 return v8::Local<v8::Context>::New(m_isolate, m_context);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700221}
222
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700223CFX_WideString ChangeObjName(const CFX_WideString& str) {
224 CFX_WideString sRet = str;
225 sRet.Replace(L"_", L".");
226 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700227}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228FX_BOOL CJS_Runtime::GetHValueByName(const CFX_ByteStringC& utf8Name,
229 FXJSE_HVALUE hValue) {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700230#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 const FX_CHAR* name = utf8Name.GetCStr();
Bo Xufdc00a72014-10-28 23:03:33 -0700232
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 v8::Locker lock(GetIsolate());
234 v8::Isolate::Scope isolate_scope(GetIsolate());
235 v8::HandleScope handle_scope(GetIsolate());
236 v8::Local<v8::Context> context =
237 v8::Local<v8::Context>::New(GetIsolate(), m_context);
238 v8::Context::Scope context_scope(context);
Bo Xufdc00a72014-10-28 23:03:33 -0700239
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 // v8::Local<v8::Context> tmpCotext =
241 // v8::Local<v8::Context>::New(GetIsolate(), m_context);
242 v8::Local<v8::Value> propvalue =
243 context->Global()->Get(v8::String::NewFromUtf8(
244 GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength()));
Bo Xufdc00a72014-10-28 23:03:33 -0700245
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 if (propvalue.IsEmpty()) {
247 FXJSE_Value_SetUndefined(hValue);
248 return FALSE;
249 }
250 ((CFXJSE_Value*)hValue)->ForceSetValue(propvalue);
Tom Sepeza8a39e22015-10-12 15:47:07 -0700251#endif
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700252
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700253 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700254}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700255FX_BOOL CJS_Runtime::SetHValueByName(const CFX_ByteStringC& utf8Name,
256 FXJSE_HVALUE hValue) {
Tom Sepeza8a39e22015-10-12 15:47:07 -0700257#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 if (utf8Name.IsEmpty() || hValue == NULL)
259 return FALSE;
260 const FX_CHAR* name = utf8Name.GetCStr();
261 v8::Isolate* pIsolate = GetIsolate();
262 v8::Locker lock(pIsolate);
263 v8::Isolate::Scope isolate_scope(pIsolate);
264 v8::HandleScope handle_scope(pIsolate);
265 v8::Local<v8::Context> context =
266 v8::Local<v8::Context>::New(pIsolate, m_context);
267 v8::Context::Scope context_scope(context);
Bo Xufdc00a72014-10-28 23:03:33 -0700268
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 // v8::Local<v8::Context> tmpCotext =
270 // v8::Local<v8::Context>::New(GetIsolate(), m_context);
271 v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New(
272 GetIsolate(), ((CFXJSE_Value*)hValue)->DirectGetValue());
273 context->Global()->Set(
274 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString,
275 utf8Name.GetLength()),
276 propvalue);
Tom Sepeza8a39e22015-10-12 15:47:07 -0700277#endif
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700279}
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700280
281void CJS_Runtime::AddObserver(Observer* observer) {
282 ASSERT(m_observers.find(observer) == m_observers.end());
283 m_observers.insert(observer);
284}
285
286void CJS_Runtime::RemoveObserver(Observer* observer) {
287 ASSERT(m_observers.find(observer) != m_observers.end());
288 m_observers.erase(observer);
289}