blob: 998f45ff10b93a331ae954bd7a93e12dfc230691 [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
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07007#include "../../include/javascript/IJavaScript.h"
8#include "../../include/javascript/JS_EventHandler.h"
9#include "../../include/javascript/JS_Runtime.h"
10#include "../../include/javascript/JS_Context.h"
11#include "../../include/javascript/JS_Define.h"
12#include "../../include/javascript/JS_Object.h"
13#include "../../include/javascript/JS_Value.h"
14#include "../../include/javascript/Document.h"
15#include "../../include/javascript/app.h"
16#include "../../include/javascript/color.h"
17#include "../../include/javascript/Consts.h"
18#include "../../include/javascript/Document.h"
19#include "../../include/javascript/event.h"
20#include "../../include/javascript/Field.h"
21#include "../../include/javascript/Icon.h"
22#include "../../include/javascript/PublicMethods.h"
23#include "../../include/javascript/report.h"
24#include "../../include/javascript/util.h"
25#include "../../include/javascript/JS_GlobalData.h"
26#include "../../include/javascript/global.h"
27#include "../../include/javascript/console.h"
Bo Xufdc00a72014-10-28 23:03:33 -070028#include "../../include/fpdfxfa/fpdfxfa_app.h"
Bo Xufdc00a72014-10-28 23:03:33 -070029#include "../../../xfa/src/fxjse/src/value.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070030
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031/* ------------------------------ CJS_Runtime ------------------------------ */
Tom Sepez808a99e2015-09-10 12:28:37 -070032v8::Global<v8::ObjectTemplate>& _getGlobalObjectTemplate(v8::Isolate* pIsolate);
33
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp)
35 : m_pApp(pApp),
36 m_pDocument(NULL),
37 m_bBlocking(FALSE),
Jochen Eisinger29007842015-08-05 09:02:13 +020038 m_isolate(NULL),
39 m_isolateManaged(false) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040 if (CPDFXFA_App::GetInstance()->GetJSERuntime()) {
41 // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate.
42 m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime();
43 } else if (m_pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2) {
44 m_isolate = reinterpret_cast<v8::Isolate*>(
45 m_pApp->GetFormFillInfo()->m_pJsPlatform->m_isolate);
46 }
47 if (!m_isolate) {
Tom Sepez39bfe122015-09-17 15:25:23 -070048 m_pArrayBufferAllocator.reset(new FXJS_ArrayBufferAllocator());
Jochen Eisinger06b60022015-07-30 17:44:35 +020049
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 v8::Isolate::CreateParams params;
51 params.array_buffer_allocator = m_pArrayBufferAllocator.get();
52 m_isolate = v8::Isolate::New(params);
Jochen Eisinger29007842015-08-05 09:02:13 +020053 m_isolateManaged = true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 }
Tom Sepezd2cc1b92015-04-30 15:19:03 -070055
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 v8::Isolate* isolate = m_isolate;
57 v8::Isolate::Scope isolate_scope(isolate);
58 v8::Locker locker(isolate);
59 v8::HandleScope handle_scope(isolate);
Tom Sepezed7b2b52015-09-22 08:36:17 -070060 if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 CJS_Context* pContext = (CJS_Context*)NewContext();
Tom Sepez39bfe122015-09-17 15:25:23 -070062 FXJS_InitializeRuntime(GetIsolate(), this, pContext, m_context);
Tom Sepez2f2ffec2015-07-23 14:42:09 -070063 ReleaseContext(pContext);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064 return;
65 }
66
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070067 unsigned int embedderDataSlot = 0;
68 if (m_pApp->GetFormFillInfo()->m_pJsPlatform->version >= 2)
69 embedderDataSlot = pApp->GetFormFillInfo()->m_pJsPlatform->m_v8EmbedderSlot;
Tom Sepez39bfe122015-09-17 15:25:23 -070070 FXJS_Initialize(embedderDataSlot);
Tom Sepez570875c2015-09-11 08:35:03 -070071 DefineJSObjects();
Tom Sepezed7b2b52015-09-22 08:36:17 -070072 CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073
74 CJS_Context* pContext = (CJS_Context*)NewContext();
Tom Sepez39bfe122015-09-17 15:25:23 -070075 FXJS_InitializeRuntime(GetIsolate(), this, pContext, m_context);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 ReleaseContext(pContext);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077}
78
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079CJS_Runtime::~CJS_Runtime() {
80 int size = m_ContextArray.GetSize();
81 for (int i = 0; i < size; i++)
82 delete m_ContextArray.GetAt(i);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070083
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 m_ContextArray.RemoveAll();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070085
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 m_pApp = NULL;
87 m_pDocument = NULL;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070088 m_context.Reset();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089
Jochen Eisinger29007842015-08-05 09:02:13 +020090 if (m_isolateManaged)
91 m_isolate->Dispose();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 m_isolate = NULL;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093}
94
Tom Sepez142165e2015-09-11 13:21:50 -070095void CJS_Runtime::DefineJSObjects() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 v8::Isolate::Scope isolate_scope(GetIsolate());
97 v8::Locker locker(GetIsolate());
98 v8::HandleScope handle_scope(GetIsolate());
99 v8::Local<v8::Context> context = v8::Context::New(GetIsolate());
100 v8::Context::Scope context_scope(context);
Tom Sepez570875c2015-09-11 08:35:03 -0700101
102 // The call order determines the "ObjDefID" assigned to each class.
103 // ObjDefIDs 0 - 2
Tom Sepez39bfe122015-09-17 15:25:23 -0700104 CJS_Border::DefineJSObjects(GetIsolate(), FXJS_STATIC);
105 CJS_Display::DefineJSObjects(GetIsolate(), FXJS_STATIC);
106 CJS_Font::DefineJSObjects(GetIsolate(), FXJS_STATIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700107
Tom Sepez570875c2015-09-11 08:35:03 -0700108 // ObjDefIDs 3 - 5
Tom Sepez39bfe122015-09-17 15:25:23 -0700109 CJS_Highlight::DefineJSObjects(GetIsolate(), FXJS_STATIC);
110 CJS_Position::DefineJSObjects(GetIsolate(), FXJS_STATIC);
111 CJS_ScaleHow::DefineJSObjects(GetIsolate(), FXJS_STATIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700112
Tom Sepez570875c2015-09-11 08:35:03 -0700113 // ObjDefIDs 6 - 8
Tom Sepez39bfe122015-09-17 15:25:23 -0700114 CJS_ScaleWhen::DefineJSObjects(GetIsolate(), FXJS_STATIC);
115 CJS_Style::DefineJSObjects(GetIsolate(), FXJS_STATIC);
116 CJS_Zoomtype::DefineJSObjects(GetIsolate(), FXJS_STATIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700117
Tom Sepez570875c2015-09-11 08:35:03 -0700118 // ObjDefIDs 9 - 11
Tom Sepez39bfe122015-09-17 15:25:23 -0700119 CJS_App::DefineJSObjects(GetIsolate(), FXJS_STATIC);
120 CJS_Color::DefineJSObjects(GetIsolate(), FXJS_STATIC);
121 CJS_Console::DefineJSObjects(GetIsolate(), FXJS_STATIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700122
Tom Sepez570875c2015-09-11 08:35:03 -0700123 // ObjDefIDs 12 - 14
Tom Sepez39bfe122015-09-17 15:25:23 -0700124 CJS_Document::DefineJSObjects(GetIsolate(), FXJS_DYNAMIC);
125 CJS_Event::DefineJSObjects(GetIsolate(), FXJS_STATIC);
126 CJS_Field::DefineJSObjects(GetIsolate(), FXJS_DYNAMIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700127
Tom Sepez570875c2015-09-11 08:35:03 -0700128 // ObjDefIDs 15 - 17
Tom Sepez39bfe122015-09-17 15:25:23 -0700129 CJS_Global::DefineJSObjects(GetIsolate(), FXJS_STATIC);
130 CJS_Icon::DefineJSObjects(GetIsolate(), FXJS_DYNAMIC);
131 CJS_Util::DefineJSObjects(GetIsolate(), FXJS_STATIC);
Tom Sepez570875c2015-09-11 08:35:03 -0700132
Tom Sepez142165e2015-09-11 13:21:50 -0700133 // ObjDefIDs 18 - 20 (these can't fail, return void).
134 CJS_PublicMethods::DefineJSObjects(GetIsolate());
135 CJS_GlobalConsts::DefineJSObjects(GetIsolate());
136 CJS_GlobalArrays::DefineJSObjects(GetIsolate());
Tom Sepez570875c2015-09-11 08:35:03 -0700137
Tom Sepez142165e2015-09-11 13:21:50 -0700138 // ObjDefIDs 21 - 22.
Tom Sepez39bfe122015-09-17 15:25:23 -0700139 CJS_TimerObj::DefineJSObjects(GetIsolate(), FXJS_DYNAMIC);
140 CJS_PrintParamsObj::DefineJSObjects(GetIsolate(), FXJS_DYNAMIC);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141}
142
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143IFXJS_Context* CJS_Runtime::NewContext() {
144 CJS_Context* p = new CJS_Context(this);
145 m_ContextArray.Add(p);
146 return p;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700147}
148
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149void CJS_Runtime::ReleaseContext(IFXJS_Context* pContext) {
150 CJS_Context* pJSContext = (CJS_Context*)pContext;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700151
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700152 for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) {
153 if (pJSContext == m_ContextArray.GetAt(i)) {
154 delete pJSContext;
155 m_ContextArray.RemoveAt(i);
156 break;
157 }
158 }
159}
160
161IFXJS_Context* CJS_Runtime::GetCurrentContext() {
162 if (!m_ContextArray.GetSize())
163 return NULL;
164 return m_ContextArray.GetAt(m_ContextArray.GetSize() - 1);
165}
166
167void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) {
168 if (m_pDocument != pReaderDoc) {
169 v8::Isolate::Scope isolate_scope(m_isolate);
170 v8::Locker locker(m_isolate);
171 v8::HandleScope handle_scope(m_isolate);
172 v8::Local<v8::Context> context =
173 v8::Local<v8::Context>::New(m_isolate, m_context);
174 v8::Context::Scope context_scope(context);
175
176 m_pDocument = pReaderDoc;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 if (pReaderDoc) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700178 v8::Local<v8::Object> pThis = FXJS_GetThisObj(GetIsolate());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179 if (!pThis.IsEmpty()) {
Tom Sepez39bfe122015-09-17 15:25:23 -0700180 if (FXJS_GetObjDefnID(pThis) ==
181 FXJS_GetObjDefnID(GetIsolate(), L"Document")) {
182 if (CJS_Document* pJSDocument =
Tom Sepezd5a0e952015-09-17 15:40:06 -0700183 (CJS_Document*)FXJS_GetPrivate(GetIsolate(), pThis)) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700184 if (Document* pDocument = (Document*)pJSDocument->GetEmbedObject())
185 pDocument->AttachDoc(pReaderDoc);
186 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700187 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700188 }
Tom Sepez2f2ffec2015-07-23 14:42:09 -0700189 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700191}
192
Tom Sepez5d0e8432015-09-22 15:50:03 -0700193bool CJS_Runtime::AddEventToSet(const FieldEvent& event) {
194 return m_FieldEventSet.insert(event).second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195}
196
Tom Sepez5d0e8432015-09-22 15:50:03 -0700197void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) {
198 m_FieldEventSet.erase(event);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700199}
200
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201v8::Local<v8::Context> CJS_Runtime::NewJSContext() {
202 return v8::Local<v8::Context>::New(m_isolate, m_context);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700203}
204
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205CFX_WideString ChangeObjName(const CFX_WideString& str) {
206 CFX_WideString sRet = str;
207 sRet.Replace(L"_", L".");
208 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700209}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210FX_BOOL CJS_Runtime::GetHValueByName(const CFX_ByteStringC& utf8Name,
211 FXJSE_HVALUE hValue) {
212 const FX_CHAR* name = utf8Name.GetCStr();
Bo Xufdc00a72014-10-28 23:03:33 -0700213
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700214 v8::Locker lock(GetIsolate());
215 v8::Isolate::Scope isolate_scope(GetIsolate());
216 v8::HandleScope handle_scope(GetIsolate());
217 v8::Local<v8::Context> context =
218 v8::Local<v8::Context>::New(GetIsolate(), m_context);
219 v8::Context::Scope context_scope(context);
Bo Xufdc00a72014-10-28 23:03:33 -0700220
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 // v8::Local<v8::Context> tmpCotext =
222 // v8::Local<v8::Context>::New(GetIsolate(), m_context);
223 v8::Local<v8::Value> propvalue =
224 context->Global()->Get(v8::String::NewFromUtf8(
225 GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength()));
Bo Xufdc00a72014-10-28 23:03:33 -0700226
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 if (propvalue.IsEmpty()) {
228 FXJSE_Value_SetUndefined(hValue);
229 return FALSE;
230 }
231 ((CFXJSE_Value*)hValue)->ForceSetValue(propvalue);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700232
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700234}
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235FX_BOOL CJS_Runtime::SetHValueByName(const CFX_ByteStringC& utf8Name,
236 FXJSE_HVALUE hValue) {
237 if (utf8Name.IsEmpty() || hValue == NULL)
238 return FALSE;
239 const FX_CHAR* name = utf8Name.GetCStr();
240 v8::Isolate* pIsolate = GetIsolate();
241 v8::Locker lock(pIsolate);
242 v8::Isolate::Scope isolate_scope(pIsolate);
243 v8::HandleScope handle_scope(pIsolate);
244 v8::Local<v8::Context> context =
245 v8::Local<v8::Context>::New(pIsolate, m_context);
246 v8::Context::Scope context_scope(context);
Bo Xufdc00a72014-10-28 23:03:33 -0700247
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 // v8::Local<v8::Context> tmpCotext =
249 // v8::Local<v8::Context>::New(GetIsolate(), m_context);
250 v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New(
251 GetIsolate(), ((CFXJSE_Value*)hValue)->DirectGetValue());
252 context->Global()->Set(
253 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString,
254 utf8Name.GetLength()),
255 propvalue);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700256
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 return TRUE;
Bo Xufdc00a72014-10-28 23:03:33 -0700258}