blob: 3914b3671585e23f9b164c6bd57f4d0ab7a9547e [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
Dan Sinclaire0345a42017-10-30 20:20:42 +00007#include "fxjs/cjs_runtime.h"
Tom Sepez37458412015-10-06 11:33:46 -07008
tsepez41a53ad2016-03-28 16:59:30 -07009#include <algorithm>
10
dsinclair735606d2016-10-05 15:47:02 -070011#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
Dan Sinclaire0345a42017-10-30 20:20:42 +000012#include "fxjs/cjs_annot.h"
13#include "fxjs/cjs_app.h"
14#include "fxjs/cjs_border.h"
15#include "fxjs/cjs_color.h"
16#include "fxjs/cjs_console.h"
17#include "fxjs/cjs_display.h"
18#include "fxjs/cjs_document.h"
19#include "fxjs/cjs_event.h"
20#include "fxjs/cjs_event_context.h"
21#include "fxjs/cjs_eventhandler.h"
22#include "fxjs/cjs_field.h"
23#include "fxjs/cjs_font.h"
24#include "fxjs/cjs_global.h"
25#include "fxjs/cjs_globalarrays.h"
26#include "fxjs/cjs_globalconsts.h"
Dan Sinclaira3254622017-10-30 20:24:14 +000027#include "fxjs/cjs_globaldata.h"
Dan Sinclaire0345a42017-10-30 20:20:42 +000028#include "fxjs/cjs_highlight.h"
29#include "fxjs/cjs_icon.h"
30#include "fxjs/cjs_object.h"
31#include "fxjs/cjs_position.h"
Dan Sinclaire0345a42017-10-30 20:20:42 +000032#include "fxjs/cjs_publicmethods.h"
33#include "fxjs/cjs_report.h"
34#include "fxjs/cjs_scalehow.h"
35#include "fxjs/cjs_scalewhen.h"
36#include "fxjs/cjs_style.h"
37#include "fxjs/cjs_timerobj.h"
38#include "fxjs/cjs_util.h"
39#include "fxjs/cjs_zoomtype.h"
Tom Sepez221f0b32018-06-04 22:11:27 +000040#include "fxjs/js_define.h"
Lei Zhang99f5bbb2018-10-09 21:31:28 +000041#include "third_party/base/ptr_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042
Tom Sepez40e9ff32015-11-30 12:39:54 -080043#ifdef PDF_ENABLE_XFA
dsinclair43554682016-09-29 17:29:48 -070044#include "fxjs/cfxjse_value.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080045#endif // PDF_ENABLE_XFA
46
dsinclair82e17672016-10-11 12:38:01 -070047CJS_Runtime::CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv)
Lei Zhangd5f42792018-08-29 23:18:08 +000048 : m_pFormFillEnv(pFormFillEnv) {
tsepezb4694242016-08-15 16:44:55 -070049 v8::Isolate* pIsolate = nullptr;
dsinclair8837c912016-11-01 11:22:37 -070050
dsinclair82e17672016-10-11 12:38:01 -070051 IPDF_JSPLATFORM* pPlatform = m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform;
Tom Sepez51da0932015-11-25 16:05:49 -080052 if (pPlatform->version <= 2) {
53 unsigned int embedderDataSlot = 0;
54 v8::Isolate* pExternalIsolate = nullptr;
55 if (pPlatform->version == 2) {
Tom Sepez9b8b2172018-04-25 22:12:34 +000056 pExternalIsolate = static_cast<v8::Isolate*>(pPlatform->m_isolate);
Tom Sepez51da0932015-11-25 16:05:49 -080057 embedderDataSlot = pPlatform->m_v8EmbedderSlot;
dsinclaird71bae02016-06-09 14:21:20 -070058 }
59 FXJS_Initialize(embedderDataSlot, pExternalIsolate);
60 }
tsepezb4694242016-08-15 16:44:55 -070061 m_isolateManaged = FXJS_GetIsolate(&pIsolate);
62 SetIsolate(pIsolate);
Tom Sepezd2cc1b92015-04-30 15:19:03 -070063
dsinclair8837c912016-11-01 11:22:37 -070064#ifdef PDF_ENABLE_XFA
tsepezb4694242016-08-15 16:44:55 -070065 v8::Isolate::Scope isolate_scope(pIsolate);
66 v8::HandleScope handle_scope(pIsolate);
Tom Sepez51da0932015-11-25 16:05:49 -080067#endif
dsinclaird71bae02016-06-09 14:21:20 -070068
Lei Zhang3fa115b2015-10-08 12:04:47 -070069 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0)
70 DefineJSObjects();
71
Tom Sepezc22d6712018-06-05 22:33:31 +000072 ScopedEventContext pContext(this);
tsepezb4694242016-08-15 16:44:55 -070073 InitializeEngine();
dsinclair82e17672016-10-11 12:38:01 -070074 SetFormFillEnvToDocument();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070075}
76
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077CJS_Runtime::~CJS_Runtime() {
Tom Sepezc9aa2f82018-07-02 23:08:53 +000078 NotifyObservers();
tsepezb4694242016-08-15 16:44:55 -070079 ReleaseEngine();
Tom Sepez98b356a2018-07-16 21:35:06 +000080 if (m_isolateManaged)
81 DisposeIsolate();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082}
83
Tom Sepez142165e2015-09-11 13:21:50 -070084void CJS_Runtime::DefineJSObjects() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 v8::Isolate::Scope isolate_scope(GetIsolate());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 v8::HandleScope handle_scope(GetIsolate());
87 v8::Local<v8::Context> context = v8::Context::New(GetIsolate());
88 v8::Context::Scope context_scope(context);
Tom Sepez570875c2015-09-11 08:35:03 -070089
90 // The call order determines the "ObjDefID" assigned to each class.
91 // ObjDefIDs 0 - 2
Dan Sinclairbef4d3e2017-10-26 16:49:38 -040092 CJS_Border::DefineJSObjects(this);
93 CJS_Display::DefineJSObjects(this);
94 CJS_Font::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095
Tom Sepez570875c2015-09-11 08:35:03 -070096 // ObjDefIDs 3 - 5
Dan Sinclairbef4d3e2017-10-26 16:49:38 -040097 CJS_Highlight::DefineJSObjects(this);
98 CJS_Position::DefineJSObjects(this);
99 CJS_ScaleHow::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100
Tom Sepez570875c2015-09-11 08:35:03 -0700101 // ObjDefIDs 6 - 8
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400102 CJS_ScaleWhen::DefineJSObjects(this);
103 CJS_Style::DefineJSObjects(this);
104 CJS_Zoomtype::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700105
Tom Sepez570875c2015-09-11 08:35:03 -0700106 // ObjDefIDs 9 - 11
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400107 CJS_App::DefineJSObjects(this);
108 CJS_Color::DefineJSObjects(this);
109 CJS_Console::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700110
Tom Sepez570875c2015-09-11 08:35:03 -0700111 // ObjDefIDs 12 - 14
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400112 CJS_Document::DefineJSObjects(this);
113 CJS_Event::DefineJSObjects(this);
114 CJS_Field::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700115
Tom Sepez570875c2015-09-11 08:35:03 -0700116 // ObjDefIDs 15 - 17
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400117 CJS_Global::DefineJSObjects(this);
118 CJS_Icon::DefineJSObjects(this);
119 CJS_Util::DefineJSObjects(this);
Tom Sepez570875c2015-09-11 08:35:03 -0700120
Tom Sepez142165e2015-09-11 13:21:50 -0700121 // ObjDefIDs 18 - 20 (these can't fail, return void).
tsepezb4694242016-08-15 16:44:55 -0700122 CJS_PublicMethods::DefineJSObjects(this);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700123 CJS_GlobalConsts::DefineJSObjects(this);
124 CJS_GlobalArrays::DefineJSObjects(this);
Tom Sepez570875c2015-09-11 08:35:03 -0700125
Tom Sepez0e5bab12018-10-18 21:39:40 +0000126 // ObjDefIDs 21 - 22.
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400127 CJS_TimerObj::DefineJSObjects(this);
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400128 CJS_Annot::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129}
130
Tom Sepez0784c732018-04-23 18:02:57 +0000131CJS_Runtime* CJS_Runtime::AsCJSRuntime() {
132 return this;
133}
134
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800135IJS_EventContext* CJS_Runtime::NewEventContext() {
Dan Sinclair0bb13332017-03-30 16:12:02 -0400136 m_EventContextArray.push_back(pdfium::MakeUnique<CJS_EventContext>(this));
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800137 return m_EventContextArray.back().get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700138}
139
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800140void CJS_Runtime::ReleaseEventContext(IJS_EventContext* pContext) {
Tom Sepezc22d6712018-06-05 22:33:31 +0000141 ASSERT(pContext == m_EventContextArray.back().get());
142 m_EventContextArray.pop_back();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143}
144
Tom Sepezb1670b52017-02-16 17:01:00 -0800145CJS_EventContext* CJS_Runtime::GetCurrentEventContext() const {
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800146 return m_EventContextArray.empty() ? nullptr
147 : m_EventContextArray.back().get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148}
149
dsinclair82e17672016-10-11 12:38:01 -0700150void CJS_Runtime::SetFormFillEnvToDocument() {
tsepezb4694242016-08-15 16:44:55 -0700151 v8::Isolate::Scope isolate_scope(GetIsolate());
152 v8::HandleScope handle_scope(GetIsolate());
Tom Sepez1258f7f2018-02-02 17:37:37 +0000153 v8::Local<v8::Context> context = GetV8Context();
tsepezb4694242016-08-15 16:44:55 -0700154 v8::Context::Scope context_scope(context);
155
tsepezb4694242016-08-15 16:44:55 -0700156 v8::Local<v8::Object> pThis = GetThisObj();
Lei Zhangb165ffb2018-07-11 17:23:53 +0000157 if (pThis.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700158 return;
Lei Zhangb165ffb2018-07-11 17:23:53 +0000159
Tom Sepez82999fa2018-07-16 22:17:46 +0000160 auto pJSDocument = JSGetObject<CJS_Document>(pThis);
tsepezb4694242016-08-15 16:44:55 -0700161 if (!pJSDocument)
162 return;
163
Dan Sinclairf7435522018-02-05 22:27:22 +0000164 pJSDocument->SetFormFillEnv(m_pFormFillEnv.Get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700165}
166
dsinclair82e17672016-10-11 12:38:01 -0700167CPDFSDK_FormFillEnvironment* CJS_Runtime::GetFormFillEnv() const {
Tom Sepez77f6d0f2017-02-23 12:14:10 -0800168 return m_pFormFillEnv.Get();
weili625ad662016-06-15 11:21:33 -0700169}
170
Dan Sinclairdc5d88b2018-05-17 13:53:52 +0000171Optional<IJS_Runtime::JS_Error> CJS_Runtime::ExecuteScript(
172 const WideString& script) {
173 return Execute(script);
Tom Sepez33420902015-10-13 15:00:10 -0700174}
175
Tom Sepez5d0e8432015-09-22 15:50:03 -0700176bool CJS_Runtime::AddEventToSet(const FieldEvent& event) {
177 return m_FieldEventSet.insert(event).second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178}
179
Tom Sepez5d0e8432015-09-22 15:50:03 -0700180void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) {
181 m_FieldEventSet.erase(event);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700182}
183
Tom Sepez51da0932015-11-25 16:05:49 -0800184#ifdef PDF_ENABLE_XFA
Ryan Harrison275e2602017-09-18 14:23:18 -0400185WideString ChangeObjName(const WideString& str) {
186 WideString sRet = str;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 sRet.Replace(L"_", L".");
188 return sRet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189}
Tom Sepez33b42e42017-07-19 13:19:12 -0700190
Tom Sepez9cbd2dd2018-02-02 17:24:58 +0000191bool CJS_Runtime::GetValueByNameFromGlobalObject(const ByteStringView& utf8Name,
192 CFXJSE_Value* pValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700193 v8::Isolate::Scope isolate_scope(GetIsolate());
194 v8::HandleScope handle_scope(GetIsolate());
Tom Sepez1258f7f2018-02-02 17:37:37 +0000195 v8::Local<v8::Context> context = GetV8Context();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 v8::Context::Scope context_scope(context);
Lei Zhangd5f42792018-08-29 23:18:08 +0000197 v8::Local<v8::String> str =
Tom Sepez33b42e42017-07-19 13:19:12 -0700198 v8::String::NewFromUtf8(GetIsolate(), utf8Name.unterminated_c_str(),
Lei Zhangd5f42792018-08-29 23:18:08 +0000199 v8::NewStringType::kNormal, utf8Name.GetLength())
200 .ToLocalChecked();
Lei Zhang56e0c3e2018-10-13 06:38:29 +0000201 v8::MaybeLocal<v8::Value> maybe_propvalue =
202 context->Global()->Get(context, str);
203 if (maybe_propvalue.IsEmpty()) {
204 pValue->SetUndefined();
205 return false;
206 }
207 v8::Local<v8::Value> propvalue = maybe_propvalue.ToLocalChecked();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 if (propvalue.IsEmpty()) {
dsinclairf27aeec2016-06-07 19:36:18 -0700209 pValue->SetUndefined();
tsepez4cf55152016-11-02 14:37:54 -0700210 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 }
dsinclair12a6b0c2016-05-26 11:14:08 -0700212 pValue->ForceSetValue(propvalue);
tsepez4cf55152016-11-02 14:37:54 -0700213 return true;
Bo Xufdc00a72014-10-28 23:03:33 -0700214}
Tom Sepez33b42e42017-07-19 13:19:12 -0700215
Tom Sepez9cbd2dd2018-02-02 17:24:58 +0000216bool CJS_Runtime::SetValueByNameInGlobalObject(const ByteStringView& utf8Name,
217 CFXJSE_Value* pValue) {
dsinclair12a6b0c2016-05-26 11:14:08 -0700218 if (utf8Name.IsEmpty() || !pValue)
tsepez4cf55152016-11-02 14:37:54 -0700219 return false;
Tom Sepez33b42e42017-07-19 13:19:12 -0700220
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221 v8::Isolate* pIsolate = GetIsolate();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700222 v8::Isolate::Scope isolate_scope(pIsolate);
223 v8::HandleScope handle_scope(pIsolate);
Tom Sepez1258f7f2018-02-02 17:37:37 +0000224 v8::Local<v8::Context> context = GetV8Context();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700225 v8::Context::Scope context_scope(context);
dsinclair12a6b0c2016-05-26 11:14:08 -0700226 v8::Local<v8::Value> propvalue =
Tom Sepez1258f7f2018-02-02 17:37:37 +0000227 v8::Local<v8::Value>::New(pIsolate, pValue->DirectGetValue());
Lei Zhangd5f42792018-08-29 23:18:08 +0000228 v8::Local<v8::String> str =
Tom Sepez33b42e42017-07-19 13:19:12 -0700229 v8::String::NewFromUtf8(pIsolate, utf8Name.unterminated_c_str(),
Lei Zhangd5f42792018-08-29 23:18:08 +0000230 v8::NewStringType::kNormal, utf8Name.GetLength())
231 .ToLocalChecked();
232 return context->Global()->Set(context, str, propvalue).FromJust();
Bo Xufdc00a72014-10-28 23:03:33 -0700233}
Tom Sepez51da0932015-11-25 16:05:49 -0800234#endif
Dan Sinclaire85107b2017-10-24 15:29:22 -0400235
236v8::Local<v8::Value> CJS_Runtime::MaybeCoerceToNumber(
dan sinclair80435cb2017-10-24 21:40:24 -0400237 v8::Local<v8::Value> value) {
Dan Sinclaire85107b2017-10-24 15:29:22 -0400238 bool bAllowNaN = false;
239 if (value->IsString()) {
Tom Sepez34dab072018-08-08 17:49:02 +0000240 ByteString bstr = ToWideString(value).ToDefANSI();
Dan Sinclaire85107b2017-10-24 15:29:22 -0400241 if (bstr.GetLength() == 0)
242 return value;
243 if (bstr == "NaN")
244 bAllowNaN = true;
245 }
246
247 v8::Isolate* pIsolate = GetIsolate();
248 v8::TryCatch try_catch(pIsolate);
249 v8::MaybeLocal<v8::Number> maybeNum =
250 value->ToNumber(pIsolate->GetCurrentContext());
251 if (maybeNum.IsEmpty())
252 return value;
253
254 v8::Local<v8::Number> num = maybeNum.ToLocalChecked();
255 if (std::isnan(num->Value()) && !bAllowNaN)
256 return value;
257
258 return num;
259}