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 "fpdfsdk/javascript/cjs_runtime.h"
8
9 #include <algorithm>
10
11 #include "fpdfsdk/include/fsdk_mgr.h"
12 #include "fpdfsdk/javascript/Consts.h"
13 #include "fpdfsdk/javascript/Document.h"
14 #include "fpdfsdk/javascript/Field.h"
15 #include "fpdfsdk/javascript/Icon.h"
16 #include "fpdfsdk/javascript/JS_Define.h"
17 #include "fpdfsdk/javascript/JS_EventHandler.h"
18 #include "fpdfsdk/javascript/JS_GlobalData.h"
19 #include "fpdfsdk/javascript/JS_Object.h"
20 #include "fpdfsdk/javascript/JS_Value.h"
21 #include "fpdfsdk/javascript/PublicMethods.h"
22 #include "fpdfsdk/javascript/app.h"
23 #include "fpdfsdk/javascript/cjs_context.h"
24 #include "fpdfsdk/javascript/color.h"
25 #include "fpdfsdk/javascript/console.h"
26 #include "fpdfsdk/javascript/event.h"
27 #include "fpdfsdk/javascript/global.h"
28 #include "fpdfsdk/javascript/report.h"
29 #include "fpdfsdk/javascript/util.h"
30 #include "third_party/base/stl_util.h"
31
32 #ifdef PDF_ENABLE_XFA
33 #include "fpdfsdk/fpdfxfa/include/fpdfxfa_app.h"
34 #include "fxjs/include/cfxjse_value.h"
35 #endif // PDF_ENABLE_XFA
36
37 // static
Initialize(unsigned int slot,void * isolate)38 void IJS_Runtime::Initialize(unsigned int slot, void* isolate) {
39 FXJS_Initialize(slot, reinterpret_cast<v8::Isolate*>(isolate));
40 }
41
42 // static
Destroy()43 void IJS_Runtime::Destroy() {
44 FXJS_Release();
45 }
46
47 // static
Create(CPDFDoc_Environment * pEnv)48 IJS_Runtime* IJS_Runtime::Create(CPDFDoc_Environment* pEnv) {
49 return new CJS_Runtime(pEnv);
50 }
51
52 // static
FromContext(const IJS_Context * cc)53 CJS_Runtime* CJS_Runtime::FromContext(const IJS_Context* cc) {
54 const CJS_Context* pContext = static_cast<const CJS_Context*>(cc);
55 return pContext->GetJSRuntime();
56 }
57
CJS_Runtime(CPDFDoc_Environment * pApp)58 CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp)
59 : m_pApp(pApp),
60 m_pDocument(nullptr),
61 m_bBlocking(FALSE),
62 m_isolate(nullptr),
63 m_isolateManaged(false) {
64 #ifndef PDF_ENABLE_XFA
65 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform;
66 if (pPlatform->version <= 2) {
67 unsigned int embedderDataSlot = 0;
68 v8::Isolate* pExternalIsolate = nullptr;
69 if (pPlatform->version == 2) {
70 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate);
71 embedderDataSlot = pPlatform->m_v8EmbedderSlot;
72 }
73 FXJS_Initialize(embedderDataSlot, pExternalIsolate);
74 }
75 m_isolateManaged = FXJS_GetIsolate(&m_isolate);
76 #else
77 if (CPDFXFA_App::GetInstance()->GetJSERuntime()) {
78 // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate.
79 m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime();
80 } else {
81 IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform;
82 if (pPlatform->version <= 2) {
83 unsigned int embedderDataSlot = 0;
84 v8::Isolate* pExternalIsolate = nullptr;
85 if (pPlatform->version == 2) {
86 pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate);
87 embedderDataSlot = pPlatform->m_v8EmbedderSlot;
88 }
89 FXJS_Initialize(embedderDataSlot, pExternalIsolate);
90 }
91 m_isolateManaged = FXJS_GetIsolate(&m_isolate);
92 }
93
94 v8::Isolate* isolate = m_isolate;
95 v8::Isolate::Scope isolate_scope(isolate);
96 v8::HandleScope handle_scope(isolate);
97 if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) {
98 CJS_Context* pContext = (CJS_Context*)NewContext();
99 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects);
100 ReleaseContext(pContext);
101 return;
102 }
103 #endif
104
105 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0)
106 DefineJSObjects();
107
108 #ifdef PDF_ENABLE_XFA
109 CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE);
110 #endif
111
112 CJS_Context* pContext = (CJS_Context*)NewContext();
113 FXJS_InitializeRuntime(GetIsolate(), this, &m_context, &m_StaticObjects);
114 ReleaseContext(pContext);
115 }
116
~CJS_Runtime()117 CJS_Runtime::~CJS_Runtime() {
118 for (auto* obs : m_observers)
119 obs->OnDestroyed();
120
121 m_ContextArray.clear();
122 m_ConstArrays.clear();
123 FXJS_ReleaseRuntime(GetIsolate(), &m_context, &m_StaticObjects);
124 m_context.Reset();
125 if (m_isolateManaged)
126 m_isolate->Dispose();
127 }
128
DefineJSObjects()129 void CJS_Runtime::DefineJSObjects() {
130 v8::Isolate::Scope isolate_scope(GetIsolate());
131 v8::HandleScope handle_scope(GetIsolate());
132 v8::Local<v8::Context> context = v8::Context::New(GetIsolate());
133 v8::Context::Scope context_scope(context);
134
135 // The call order determines the "ObjDefID" assigned to each class.
136 // ObjDefIDs 0 - 2
137 CJS_Border::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
138 CJS_Display::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
139 CJS_Font::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
140
141 // ObjDefIDs 3 - 5
142 CJS_Highlight::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
143 CJS_Position::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
144 CJS_ScaleHow::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
145
146 // ObjDefIDs 6 - 8
147 CJS_ScaleWhen::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
148 CJS_Style::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
149 CJS_Zoomtype::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
150
151 // ObjDefIDs 9 - 11
152 CJS_App::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
153 CJS_Color::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
154 CJS_Console::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
155
156 // ObjDefIDs 12 - 14
157 CJS_Document::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_GLOBAL);
158 CJS_Event::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
159 CJS_Field::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
160
161 // ObjDefIDs 15 - 17
162 CJS_Global::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
163 CJS_Icon::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
164 CJS_Util::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC);
165
166 // ObjDefIDs 18 - 20 (these can't fail, return void).
167 CJS_PublicMethods::DefineJSObjects(GetIsolate());
168 CJS_GlobalConsts::DefineJSObjects(this);
169 CJS_GlobalArrays::DefineJSObjects(this);
170
171 // ObjDefIDs 21 - 22.
172 CJS_TimerObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
173 CJS_PrintParamsObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC);
174 }
175
NewContext()176 IJS_Context* CJS_Runtime::NewContext() {
177 m_ContextArray.push_back(std::unique_ptr<CJS_Context>(new CJS_Context(this)));
178 return m_ContextArray.back().get();
179 }
180
ReleaseContext(IJS_Context * pContext)181 void CJS_Runtime::ReleaseContext(IJS_Context* pContext) {
182 for (auto it = m_ContextArray.begin(); it != m_ContextArray.end(); ++it) {
183 if (it->get() == static_cast<CJS_Context*>(pContext)) {
184 m_ContextArray.erase(it);
185 return;
186 }
187 }
188 }
189
GetCurrentContext()190 IJS_Context* CJS_Runtime::GetCurrentContext() {
191 return m_ContextArray.empty() ? nullptr : m_ContextArray.back().get();
192 }
193
SetReaderDocument(CPDFSDK_Document * pReaderDoc)194 void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) {
195 if (m_pDocument != pReaderDoc) {
196 v8::Isolate::Scope isolate_scope(m_isolate);
197 v8::HandleScope handle_scope(m_isolate);
198 v8::Local<v8::Context> context =
199 v8::Local<v8::Context>::New(m_isolate, m_context);
200 v8::Context::Scope context_scope(context);
201
202 m_pDocument = pReaderDoc;
203 if (pReaderDoc) {
204 v8::Local<v8::Object> pThis = FXJS_GetThisObj(GetIsolate());
205 if (!pThis.IsEmpty()) {
206 if (FXJS_GetObjDefnID(pThis) == CJS_Document::g_nObjDefnID) {
207 if (CJS_Document* pJSDocument =
208 (CJS_Document*)FXJS_GetPrivate(GetIsolate(), pThis)) {
209 if (Document* pDocument = (Document*)pJSDocument->GetEmbedObject())
210 pDocument->AttachDoc(pReaderDoc);
211 }
212 }
213 }
214 }
215 }
216 }
217
GetReaderDocument()218 CPDFSDK_Document* CJS_Runtime::GetReaderDocument() {
219 return m_pDocument;
220 }
221
Execute(const CFX_WideString & script,CFX_WideString * info)222 int CJS_Runtime::Execute(const CFX_WideString& script, CFX_WideString* info) {
223 FXJSErr error = {};
224 int nRet = FXJS_Execute(m_isolate, script, &error);
225 if (nRet < 0) {
226 info->Format(L"[ Line: %05d { %s } ] : %s", error.linnum - 1, error.srcline,
227 error.message);
228 }
229 return nRet;
230 }
231
AddEventToSet(const FieldEvent & event)232 bool CJS_Runtime::AddEventToSet(const FieldEvent& event) {
233 return m_FieldEventSet.insert(event).second;
234 }
235
RemoveEventFromSet(const FieldEvent & event)236 void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) {
237 m_FieldEventSet.erase(event);
238 }
239
NewJSContext()240 v8::Local<v8::Context> CJS_Runtime::NewJSContext() {
241 return v8::Local<v8::Context>::New(m_isolate, m_context);
242 }
243
SetConstArray(const CFX_WideString & name,v8::Local<v8::Array> array)244 void CJS_Runtime::SetConstArray(const CFX_WideString& name,
245 v8::Local<v8::Array> array) {
246 m_ConstArrays[name] = v8::Global<v8::Array>(m_isolate, array);
247 }
248
GetConstArray(const CFX_WideString & name)249 v8::Local<v8::Array> CJS_Runtime::GetConstArray(const CFX_WideString& name) {
250 return v8::Local<v8::Array>::New(m_isolate, m_ConstArrays[name]);
251 }
252
AddObserver(Observer * observer)253 void CJS_Runtime::AddObserver(Observer* observer) {
254 ASSERT(!pdfium::ContainsKey(m_observers, observer));
255 m_observers.insert(observer);
256 }
257
RemoveObserver(Observer * observer)258 void CJS_Runtime::RemoveObserver(Observer* observer) {
259 ASSERT(pdfium::ContainsKey(m_observers, observer));
260 m_observers.erase(observer);
261 }
262
263 #ifdef PDF_ENABLE_XFA
ChangeObjName(const CFX_WideString & str)264 CFX_WideString ChangeObjName(const CFX_WideString& str) {
265 CFX_WideString sRet = str;
266 sRet.Replace(L"_", L".");
267 return sRet;
268 }
GetValueByName(const CFX_ByteStringC & utf8Name,CFXJSE_Value * pValue)269 FX_BOOL CJS_Runtime::GetValueByName(const CFX_ByteStringC& utf8Name,
270 CFXJSE_Value* pValue) {
271 const FX_CHAR* name = utf8Name.c_str();
272
273 v8::Isolate::Scope isolate_scope(GetIsolate());
274 v8::HandleScope handle_scope(GetIsolate());
275 v8::Local<v8::Context> old_context = GetIsolate()->GetCurrentContext();
276 v8::Local<v8::Context> context =
277 v8::Local<v8::Context>::New(GetIsolate(), m_context);
278 v8::Context::Scope context_scope(context);
279
280 // Caution: We're about to hand to XFA an object that in order to invoke
281 // methods will require that the current v8::Context always has a pointer
282 // to a CJS_Runtime in its embedder data slot. Unfortunately, XFA creates
283 // its own v8::Context which has not initialized the embedder data slot.
284 // Do so now.
285 // TODO(tsepez): redesign PDF-side objects to not rely on v8::Context's
286 // embedder data slots, and/or to always use the right context.
287 FXJS_SetRuntimeForV8Context(old_context, this);
288
289 v8::Local<v8::Value> propvalue =
290 context->Global()->Get(v8::String::NewFromUtf8(
291 GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength()));
292
293 if (propvalue.IsEmpty()) {
294 pValue->SetUndefined();
295 return FALSE;
296 }
297 pValue->ForceSetValue(propvalue);
298 return TRUE;
299 }
SetValueByName(const CFX_ByteStringC & utf8Name,CFXJSE_Value * pValue)300 FX_BOOL CJS_Runtime::SetValueByName(const CFX_ByteStringC& utf8Name,
301 CFXJSE_Value* pValue) {
302 if (utf8Name.IsEmpty() || !pValue)
303 return FALSE;
304 const FX_CHAR* name = utf8Name.c_str();
305 v8::Isolate* pIsolate = GetIsolate();
306 v8::Isolate::Scope isolate_scope(pIsolate);
307 v8::HandleScope handle_scope(pIsolate);
308 v8::Local<v8::Context> context =
309 v8::Local<v8::Context>::New(pIsolate, m_context);
310 v8::Context::Scope context_scope(context);
311
312 // v8::Local<v8::Context> tmpCotext =
313 // v8::Local<v8::Context>::New(GetIsolate(), m_context);
314 v8::Local<v8::Value> propvalue =
315 v8::Local<v8::Value>::New(GetIsolate(), pValue->DirectGetValue());
316 context->Global()->Set(
317 v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString,
318 utf8Name.GetLength()),
319 propvalue);
320 return TRUE;
321 }
322 #endif
323