blob: bc5443daf614e609880efcc22ea0dad9b8a1df53 [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 Sepez37458412015-10-06 11:33:46 -070035IFXJS_Runtime* IFXJS_Runtime::Create(CPDFDoc_Environment* pEnv) {
36 return new CJS_Runtime(pEnv);
37}
38
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp)
40 : m_pApp(pApp),
41 m_pDocument(NULL),
42 m_bBlocking(FALSE),
Jochen Eisinger29007842015-08-05 09:02:13 +020043 m_isolate(NULL),
44 m_isolateManaged(false) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 if (CPDFXFA_App::GetInstance()->GetJSERuntime()) {
46 // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate.
47 m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime();
Tom Sepeza72e8e22015-10-07 10:17:53 -070048 } else {
49 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform;
50 if (pPlatform->version <= 2) {
51 unsigned int embedderDataSlot = 0;
52 v8::Isolate* pExternalIsolate = nullptr;
53 if (pPlatform->version == 2) {
54 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate);
55 embedderDataSlot = pPlatform->m_v8EmbedderSlot;
56 }
57 FXJS_Initialize(embedderDataSlot, pExternalIsolate);
58 }
59 m_isolateManaged = FXJS_GetIsolate(&m_isolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 }
Tom Sepezd2cc1b92015-04-30 15:19:03 -070061
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 v8::Isolate* isolate = m_isolate;
63 v8::Isolate::Scope isolate_scope(isolate);
64 v8::Locker locker(isolate);
65 v8::HandleScope handle_scope(isolate);
Tom Sepezed7b2b52015-09-22 08:36:17 -070066 if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 CJS_Context* pContext = (CJS_Context*)NewContext();
Tom Sepez39bfe122015-09-17 15:25:23 -070068 FXJS_InitializeRuntime(GetIsolate(), this, pContext, m_context);
Tom Sepez2f2ffec2015-07-23 14:42:09 -070069 ReleaseContext(pContext);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 return;
71 }
72
Tom Sepez570875c2015-09-11 08:35:03 -070073 DefineJSObjects();
Tom Sepezed7b2b52015-09-22 08:36:17 -070074 CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075
76 CJS_Context* pContext = (CJS_Context*)NewContext();
Tom Sepez39bfe122015-09-17 15:25:23 -070077 FXJS_InitializeRuntime(GetIsolate(), this, pContext, m_context);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 ReleaseContext(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070079}
80
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081CJS_Runtime::~CJS_Runtime() {
Lei Zhang2d5a0e12015-10-05 17:00:03 -070082 for (auto* obs : m_observers)
83 obs->OnDestroyed();
84
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 int size = m_ContextArray.GetSize();
86 for (int i = 0; i < size; i++)
87 delete m_ContextArray.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 m_ContextArray.RemoveAll();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 m_pApp = NULL;
92 m_pDocument = NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 m_context.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070094
Jochen Eisinger29007842015-08-05 09:02:13 +020095 if (m_isolateManaged)
96 m_isolate->Dispose();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070097 m_isolate = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098}
99
Tom Sepez142165e2015-09-11 13:21:50 -0700100void CJS_Runtime::DefineJSObjects() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 v8::Isolate::Scope isolate_scope(GetIsolate());
102 v8::Locker locker(GetIsolate());
103 v8::HandleScope handle_scope(GetIsolate());
104 v8::Local<v8::Context> context = v8::Context::New(GetIsolate());
105 v8::Context::Scope context_scope(context);
Tom Sepez570875c2015-09-11 08:35:03 -0700106
107 // The call order determines the "ObjDefID" assigned to each class.
108 // ObjDefIDs 0 - 2
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700109 CJS_Border::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
110 CJS_Display::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
111 CJS_Font::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700112
Tom Sepez570875c2015-09-11 08:35:03 -0700113 // ObjDefIDs 3 - 5
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700114 CJS_Highlight::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
115 CJS_Position::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
116 CJS_ScaleHow::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700117
Tom Sepez570875c2015-09-11 08:35:03 -0700118 // ObjDefIDs 6 - 8
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700119 CJS_ScaleWhen::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
120 CJS_Style::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
121 CJS_Zoomtype::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700122
Tom Sepez570875c2015-09-11 08:35:03 -0700123 // ObjDefIDs 9 - 11
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700124 CJS_App::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
125 CJS_Color::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
126 CJS_Console::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700127
Tom Sepez570875c2015-09-11 08:35:03 -0700128 // ObjDefIDs 12 - 14
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700129 CJS_Document::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_GLOBAL);
130 CJS_Event::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
131 CJS_Field::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132
Tom Sepez570875c2015-09-11 08:35:03 -0700133 // ObjDefIDs 15 - 17
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700134 CJS_Global::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
135 CJS_Icon::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
136 CJS_Util::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
Tom Sepez570875c2015-09-11 08:35:03 -0700137
Tom Sepez142165e2015-09-11 13:21:50 -0700138 // ObjDefIDs 18 - 20 (these can't fail, return void).
139 CJS_PublicMethods::DefineJSObjects(GetIsolate());
140 CJS_GlobalConsts::DefineJSObjects(GetIsolate());
141 CJS_GlobalArrays::DefineJSObjects(GetIsolate());
Tom Sepez570875c2015-09-11 08:35:03 -0700142
Tom Sepez142165e2015-09-11 13:21:50 -0700143 // ObjDefIDs 21 - 22.
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700144 CJS_TimerObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
145 CJS_PrintParamsObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146}
147
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148IFXJS_Context* CJS_Runtime::NewContext() {
149 CJS_Context* p = new CJS_Context(this);
150 m_ContextArray.Add(p);
151 return p;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152}
153
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154void CJS_Runtime::ReleaseContext(IFXJS_Context* pContext) {
155 CJS_Context* pJSContext = (CJS_Context*)pContext;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700156
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) {
158 if (pJSContext == m_ContextArray.GetAt(i)) {
159 delete pJSContext;
160 m_ContextArray.RemoveAt(i);
161 break;
162 }
163 }
164}
165
166IFXJS_Context* CJS_Runtime::GetCurrentContext() {
167 if (!m_ContextArray.GetSize())
168 return NULL;
169 return m_ContextArray.GetAt(m_ContextArray.GetSize() - 1);
170}
171
172void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) {
173 if (m_pDocument != pReaderDoc) {
174 v8::Isolate::Scope isolate_scope(m_isolate);
175 v8::Locker locker(m_isolate);
176 v8::HandleScope handle_scope(m_isolate);
177 v8::Local<v8::Context> context =
178 v8::Local<v8::Context>::New(m_isolate, m_context);
179 v8::Context::Scope context_scope(context);
180
181 m_pDocument = pReaderDoc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 if (pReaderDoc) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700183 v8::Local<v8::Object> pThis = FXJS_GetThisObj(GetIsolate());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 if (!pThis.IsEmpty()) {
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700185 if (FXJS_GetObjDefnID(pThis) == CJS_Document::g_nObjDefnID) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700186 if (CJS_Document* pJSDocument =
Tom Sepezd5a0e952015-09-17 15:40:06 -0700187 (CJS_Document*)FXJS_GetPrivate(GetIsolate(), pThis)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 if (Document* pDocument = (Document*)pJSDocument->GetEmbedObject())
189 pDocument->AttachDoc(pReaderDoc);
190 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700191 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700192 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700193 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700194 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195}
196
Tom Sepez5d0e8432015-09-22 15:50:03 -0700197bool CJS_Runtime::AddEventToSet(const FieldEvent& event) {
198 return m_FieldEventSet.insert(event).second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199}
200
Tom Sepez5d0e8432015-09-22 15:50:03 -0700201void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) {
202 m_FieldEventSet.erase(event);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203}
204
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205v8::Local<v8::Context> CJS_Runtime::NewJSContext() {
206 return v8::Local<v8::Context>::New(m_isolate, m_context);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700207}
208
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209CFX_WideString ChangeObjName(const CFX_WideString& str) {
210 CFX_WideString sRet = str;
211 sRet.Replace(L"_", L".");
212 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700213}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214FX_BOOL CJS_Runtime::GetHValueByName(const CFX_ByteStringC& utf8Name,
215 FXJSE_HVALUE hValue) {
216 const FX_CHAR* name = utf8Name.GetCStr();
Bo Xufdc00a72014-10-28 23:03:33 -0700217
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 v8::Locker lock(GetIsolate());
219 v8::Isolate::Scope isolate_scope(GetIsolate());
220 v8::HandleScope handle_scope(GetIsolate());
221 v8::Local<v8::Context> context =
222 v8::Local<v8::Context>::New(GetIsolate(), m_context);
223 v8::Context::Scope context_scope(context);
Bo Xufdc00a72014-10-28 23:03:33 -0700224
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 // v8::Local<v8::Context> tmpCotext =
226 // v8::Local<v8::Context>::New(GetIsolate(), m_context);
227 v8::Local<v8::Value> propvalue =
228 context->Global()->Get(v8::String::NewFromUtf8(
229 GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength()));
Bo Xufdc00a72014-10-28 23:03:33 -0700230
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 if (propvalue.IsEmpty()) {
232 FXJSE_Value_SetUndefined(hValue);
233 return FALSE;
234 }
235 ((CFXJSE_Value*)hValue)->ForceSetValue(propvalue);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700236
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700237 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700238}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700239FX_BOOL CJS_Runtime::SetHValueByName(const CFX_ByteStringC& utf8Name,
240 FXJSE_HVALUE hValue) {
241 if (utf8Name.IsEmpty() || hValue == NULL)
242 return FALSE;
243 const FX_CHAR* name = utf8Name.GetCStr();
244 v8::Isolate* pIsolate = GetIsolate();
245 v8::Locker lock(pIsolate);
246 v8::Isolate::Scope isolate_scope(pIsolate);
247 v8::HandleScope handle_scope(pIsolate);
248 v8::Local<v8::Context> context =
249 v8::Local<v8::Context>::New(pIsolate, m_context);
250 v8::Context::Scope context_scope(context);
Bo Xufdc00a72014-10-28 23:03:33 -0700251
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 // v8::Local<v8::Context> tmpCotext =
253 // v8::Local<v8::Context>::New(GetIsolate(), m_context);
254 v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New(
255 GetIsolate(), ((CFXJSE_Value*)hValue)->DirectGetValue());
256 context->Global()->Set(
257 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString,
258 utf8Name.GetLength()),
259 propvalue);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700260
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700262}
Lei Zhang2d5a0e12015-10-05 17:00:03 -0700263
264void CJS_Runtime::AddObserver(Observer* observer) {
265 ASSERT(m_observers.find(observer) == m_observers.end());
266 m_observers.insert(observer);
267}
268
269void CJS_Runtime::RemoveObserver(Observer* observer) {
270 ASSERT(m_observers.find(observer) != m_observers.end());
271 m_observers.erase(observer);
272}