blob: 81148de9a109bf0a6453995f565f252d4b93198c [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"
Tom Sepez41d04e12018-10-30 22:07:36 +000012#include "fxjs/cfx_globaldata.h"
Dan Sinclaire0345a42017-10-30 20:20:42 +000013#include "fxjs/cjs_annot.h"
14#include "fxjs/cjs_app.h"
15#include "fxjs/cjs_border.h"
16#include "fxjs/cjs_color.h"
17#include "fxjs/cjs_console.h"
18#include "fxjs/cjs_display.h"
19#include "fxjs/cjs_document.h"
20#include "fxjs/cjs_event.h"
21#include "fxjs/cjs_event_context.h"
Tom Sepez7dbb85b2019-06-11 19:49:00 +000022#include "fxjs/cjs_eventrecorder.h"
Dan Sinclaire0345a42017-10-30 20:20:42 +000023#include "fxjs/cjs_field.h"
24#include "fxjs/cjs_font.h"
25#include "fxjs/cjs_global.h"
26#include "fxjs/cjs_globalarrays.h"
27#include "fxjs/cjs_globalconsts.h"
28#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"
Dan Sinclaire0345a42017-10-30 20:20:42 +000033#include "fxjs/cjs_scalehow.h"
34#include "fxjs/cjs_scalewhen.h"
35#include "fxjs/cjs_style.h"
36#include "fxjs/cjs_timerobj.h"
37#include "fxjs/cjs_util.h"
38#include "fxjs/cjs_zoomtype.h"
Tom Sepez221f0b32018-06-04 22:11:27 +000039#include "fxjs/js_define.h"
Lei Zhang99f5bbb2018-10-09 21:31:28 +000040#include "third_party/base/ptr_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070041
dsinclair82e17672016-10-11 12:38:01 -070042CJS_Runtime::CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv)
Lei Zhangd5f42792018-08-29 23:18:08 +000043 : m_pFormFillEnv(pFormFillEnv) {
tsepezb4694242016-08-15 16:44:55 -070044 v8::Isolate* pIsolate = nullptr;
dsinclair82e17672016-10-11 12:38:01 -070045 IPDF_JSPLATFORM* pPlatform = m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform;
Tom Sepez51da0932015-11-25 16:05:49 -080046 if (pPlatform->version <= 2) {
47 unsigned int embedderDataSlot = 0;
48 v8::Isolate* pExternalIsolate = nullptr;
49 if (pPlatform->version == 2) {
Tom Sepez9b8b2172018-04-25 22:12:34 +000050 pExternalIsolate = static_cast<v8::Isolate*>(pPlatform->m_isolate);
Tom Sepez51da0932015-11-25 16:05:49 -080051 embedderDataSlot = pPlatform->m_v8EmbedderSlot;
dsinclaird71bae02016-06-09 14:21:20 -070052 }
53 FXJS_Initialize(embedderDataSlot, pExternalIsolate);
54 }
tsepezb4694242016-08-15 16:44:55 -070055 m_isolateManaged = FXJS_GetIsolate(&pIsolate);
56 SetIsolate(pIsolate);
Tom Sepezd2cc1b92015-04-30 15:19:03 -070057
tsepezb4694242016-08-15 16:44:55 -070058 v8::Isolate::Scope isolate_scope(pIsolate);
59 v8::HandleScope handle_scope(pIsolate);
Lei Zhang3fa115b2015-10-08 12:04:47 -070060 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0)
61 DefineJSObjects();
62
Tom Sepezc22d6712018-06-05 22:33:31 +000063 ScopedEventContext pContext(this);
tsepezb4694242016-08-15 16:44:55 -070064 InitializeEngine();
dsinclair82e17672016-10-11 12:38:01 -070065 SetFormFillEnvToDocument();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066}
67
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068CJS_Runtime::~CJS_Runtime() {
Tom Sepezc9aa2f82018-07-02 23:08:53 +000069 NotifyObservers();
tsepezb4694242016-08-15 16:44:55 -070070 ReleaseEngine();
Tom Sepez98b356a2018-07-16 21:35:06 +000071 if (m_isolateManaged)
72 DisposeIsolate();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070073}
74
Tom Sepez142165e2015-09-11 13:21:50 -070075void CJS_Runtime::DefineJSObjects() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 v8::Isolate::Scope isolate_scope(GetIsolate());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 v8::HandleScope handle_scope(GetIsolate());
78 v8::Local<v8::Context> context = v8::Context::New(GetIsolate());
79 v8::Context::Scope context_scope(context);
Tom Sepez570875c2015-09-11 08:35:03 -070080
81 // The call order determines the "ObjDefID" assigned to each class.
82 // ObjDefIDs 0 - 2
Dan Sinclairbef4d3e2017-10-26 16:49:38 -040083 CJS_Border::DefineJSObjects(this);
84 CJS_Display::DefineJSObjects(this);
85 CJS_Font::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086
Tom Sepez570875c2015-09-11 08:35:03 -070087 // ObjDefIDs 3 - 5
Dan Sinclairbef4d3e2017-10-26 16:49:38 -040088 CJS_Highlight::DefineJSObjects(this);
89 CJS_Position::DefineJSObjects(this);
90 CJS_ScaleHow::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070091
Tom Sepez570875c2015-09-11 08:35:03 -070092 // ObjDefIDs 6 - 8
Dan Sinclairbef4d3e2017-10-26 16:49:38 -040093 CJS_ScaleWhen::DefineJSObjects(this);
94 CJS_Style::DefineJSObjects(this);
95 CJS_Zoomtype::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070096
Tom Sepez570875c2015-09-11 08:35:03 -070097 // ObjDefIDs 9 - 11
Dan Sinclairbef4d3e2017-10-26 16:49:38 -040098 CJS_App::DefineJSObjects(this);
99 CJS_Color::DefineJSObjects(this);
100 CJS_Console::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700101
Tom Sepez570875c2015-09-11 08:35:03 -0700102 // ObjDefIDs 12 - 14
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400103 CJS_Document::DefineJSObjects(this);
104 CJS_Event::DefineJSObjects(this);
105 CJS_Field::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106
Tom Sepez570875c2015-09-11 08:35:03 -0700107 // ObjDefIDs 15 - 17
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400108 CJS_Global::DefineJSObjects(this);
109 CJS_Icon::DefineJSObjects(this);
110 CJS_Util::DefineJSObjects(this);
Tom Sepez570875c2015-09-11 08:35:03 -0700111
Tom Sepez142165e2015-09-11 13:21:50 -0700112 // ObjDefIDs 18 - 20 (these can't fail, return void).
tsepezb4694242016-08-15 16:44:55 -0700113 CJS_PublicMethods::DefineJSObjects(this);
Tom Sepez67fd5df2015-10-08 12:24:19 -0700114 CJS_GlobalConsts::DefineJSObjects(this);
115 CJS_GlobalArrays::DefineJSObjects(this);
Tom Sepez570875c2015-09-11 08:35:03 -0700116
Tom Sepez0e5bab12018-10-18 21:39:40 +0000117 // ObjDefIDs 21 - 22.
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400118 CJS_TimerObj::DefineJSObjects(this);
Dan Sinclairbef4d3e2017-10-26 16:49:38 -0400119 CJS_Annot::DefineJSObjects(this);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700120}
121
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800122IJS_EventContext* CJS_Runtime::NewEventContext() {
Dan Sinclair0bb13332017-03-30 16:12:02 -0400123 m_EventContextArray.push_back(pdfium::MakeUnique<CJS_EventContext>(this));
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800124 return m_EventContextArray.back().get();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700125}
126
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800127void CJS_Runtime::ReleaseEventContext(IJS_EventContext* pContext) {
Tom Sepezc22d6712018-06-05 22:33:31 +0000128 ASSERT(pContext == m_EventContextArray.back().get());
129 m_EventContextArray.pop_back();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700130}
131
Tom Sepezb1670b52017-02-16 17:01:00 -0800132CJS_EventContext* CJS_Runtime::GetCurrentEventContext() const {
Tom Sepezd6ae2af2017-02-16 11:49:55 -0800133 return m_EventContextArray.empty() ? nullptr
134 : m_EventContextArray.back().get();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700135}
136
Tom Sepez24374a62019-08-07 21:26:02 +0000137TimerHandlerIface* CJS_Runtime::GetTimerHandler() const {
Tom Sepez7c2d1102019-08-07 22:25:50 +0000138 return m_pFormFillEnv ? m_pFormFillEnv->GetTimerHandler() : nullptr;
Tom Sepez24374a62019-08-07 21:26:02 +0000139}
140
dsinclair82e17672016-10-11 12:38:01 -0700141void CJS_Runtime::SetFormFillEnvToDocument() {
tsepezb4694242016-08-15 16:44:55 -0700142 v8::Isolate::Scope isolate_scope(GetIsolate());
143 v8::HandleScope handle_scope(GetIsolate());
Tom Sepez1258f7f2018-02-02 17:37:37 +0000144 v8::Local<v8::Context> context = GetV8Context();
tsepezb4694242016-08-15 16:44:55 -0700145 v8::Context::Scope context_scope(context);
146
tsepezb4694242016-08-15 16:44:55 -0700147 v8::Local<v8::Object> pThis = GetThisObj();
Lei Zhangb165ffb2018-07-11 17:23:53 +0000148 if (pThis.IsEmpty())
tsepezb4694242016-08-15 16:44:55 -0700149 return;
Lei Zhangb165ffb2018-07-11 17:23:53 +0000150
Tom Sepez82999fa2018-07-16 22:17:46 +0000151 auto pJSDocument = JSGetObject<CJS_Document>(pThis);
tsepezb4694242016-08-15 16:44:55 -0700152 if (!pJSDocument)
153 return;
154
Dan Sinclairf7435522018-02-05 22:27:22 +0000155 pJSDocument->SetFormFillEnv(m_pFormFillEnv.Get());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700156}
157
dsinclair82e17672016-10-11 12:38:01 -0700158CPDFSDK_FormFillEnvironment* CJS_Runtime::GetFormFillEnv() const {
Tom Sepez77f6d0f2017-02-23 12:14:10 -0800159 return m_pFormFillEnv.Get();
weili625ad662016-06-15 11:21:33 -0700160}
161
Dan Sinclairdc5d88b2018-05-17 13:53:52 +0000162Optional<IJS_Runtime::JS_Error> CJS_Runtime::ExecuteScript(
163 const WideString& script) {
164 return Execute(script);
Tom Sepez33420902015-10-13 15:00:10 -0700165}
166
Tom Sepez5d0e8432015-09-22 15:50:03 -0700167bool CJS_Runtime::AddEventToSet(const FieldEvent& event) {
168 return m_FieldEventSet.insert(event).second;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700169}
170
Tom Sepez5d0e8432015-09-22 15:50:03 -0700171void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) {
172 m_FieldEventSet.erase(event);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700173}
174
Tom Sepez2dd06eb2018-10-29 21:16:42 +0000175CJS_Runtime* CJS_Runtime::AsCJSRuntime() {
176 return this;
177}
178
Tom Sepezc839ac72018-12-14 20:34:11 +0000179bool CJS_Runtime::GetValueByNameFromGlobalObject(ByteStringView utf8Name,
Tom Sepezfe8d4e32019-08-15 20:15:08 +0000180 v8::Local<v8::Value>* pValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181 v8::Isolate::Scope isolate_scope(GetIsolate());
Tom Sepez1258f7f2018-02-02 17:37:37 +0000182 v8::Local<v8::Context> context = GetV8Context();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700183 v8::Context::Scope context_scope(context);
Lei Zhangd5f42792018-08-29 23:18:08 +0000184 v8::Local<v8::String> str =
Tom Sepez33b42e42017-07-19 13:19:12 -0700185 v8::String::NewFromUtf8(GetIsolate(), utf8Name.unterminated_c_str(),
Lei Zhangd5f42792018-08-29 23:18:08 +0000186 v8::NewStringType::kNormal, utf8Name.GetLength())
187 .ToLocalChecked();
Lei Zhang56e0c3e2018-10-13 06:38:29 +0000188 v8::MaybeLocal<v8::Value> maybe_propvalue =
189 context->Global()->Get(context, str);
Tom Sepezfe8d4e32019-08-15 20:15:08 +0000190 if (maybe_propvalue.IsEmpty())
Lei Zhang56e0c3e2018-10-13 06:38:29 +0000191 return false;
Tom Sepezfe8d4e32019-08-15 20:15:08 +0000192
193 *pValue = maybe_propvalue.ToLocalChecked();
tsepez4cf55152016-11-02 14:37:54 -0700194 return true;
Bo Xufdc00a72014-10-28 23:03:33 -0700195}
Tom Sepez33b42e42017-07-19 13:19:12 -0700196
Tom Sepezc839ac72018-12-14 20:34:11 +0000197bool CJS_Runtime::SetValueByNameInGlobalObject(ByteStringView utf8Name,
Tom Sepezfe8d4e32019-08-15 20:15:08 +0000198 v8::Local<v8::Value> pValue) {
199 if (utf8Name.IsEmpty() || pValue.IsEmpty())
tsepez4cf55152016-11-02 14:37:54 -0700200 return false;
Tom Sepez33b42e42017-07-19 13:19:12 -0700201
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700202 v8::Isolate* pIsolate = GetIsolate();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700203 v8::Isolate::Scope isolate_scope(pIsolate);
Tom Sepez1258f7f2018-02-02 17:37:37 +0000204 v8::Local<v8::Context> context = GetV8Context();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 v8::Context::Scope context_scope(context);
Lei Zhangd5f42792018-08-29 23:18:08 +0000206 v8::Local<v8::String> str =
Tom Sepez33b42e42017-07-19 13:19:12 -0700207 v8::String::NewFromUtf8(pIsolate, utf8Name.unterminated_c_str(),
Lei Zhangd5f42792018-08-29 23:18:08 +0000208 v8::NewStringType::kNormal, utf8Name.GetLength())
209 .ToLocalChecked();
Tom Sepezfe8d4e32019-08-15 20:15:08 +0000210 v8::Maybe<bool> result = context->Global()->Set(context, str, pValue);
Lei Zhang83458252019-03-14 20:07:20 +0000211 return result.IsJust() && result.FromJust();
Bo Xufdc00a72014-10-28 23:03:33 -0700212}
Dan Sinclaire85107b2017-10-24 15:29:22 -0400213
214v8::Local<v8::Value> CJS_Runtime::MaybeCoerceToNumber(
dan sinclair80435cb2017-10-24 21:40:24 -0400215 v8::Local<v8::Value> value) {
Dan Sinclaire85107b2017-10-24 15:29:22 -0400216 bool bAllowNaN = false;
217 if (value->IsString()) {
Tom Sepez34dab072018-08-08 17:49:02 +0000218 ByteString bstr = ToWideString(value).ToDefANSI();
Dan Sinclaire85107b2017-10-24 15:29:22 -0400219 if (bstr.GetLength() == 0)
220 return value;
221 if (bstr == "NaN")
222 bAllowNaN = true;
223 }
224
225 v8::Isolate* pIsolate = GetIsolate();
226 v8::TryCatch try_catch(pIsolate);
227 v8::MaybeLocal<v8::Number> maybeNum =
228 value->ToNumber(pIsolate->GetCurrentContext());
229 if (maybeNum.IsEmpty())
230 return value;
231
232 v8::Local<v8::Number> num = maybeNum.ToLocalChecked();
233 if (std::isnan(num->Value()) && !bAllowNaN)
234 return value;
235
236 return num;
237}