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 "fxjs/cjs_runtime.h"
8
9 #include <algorithm>
10
11 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
12 #include "fxjs/cfx_globaldata.h"
13 #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"
22 #include "fxjs/cjs_eventrecorder.h"
23 #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"
32 #include "fxjs/cjs_publicmethods.h"
33 #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"
39 #include "fxjs/fxv8.h"
40 #include "fxjs/js_define.h"
41 #include "third_party/base/ptr_util.h"
42
CJS_Runtime(CPDFSDK_FormFillEnvironment * pFormFillEnv)43 CJS_Runtime::CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv)
44 : m_pFormFillEnv(pFormFillEnv) {
45 v8::Isolate* pIsolate = nullptr;
46 IPDF_JSPLATFORM* pPlatform = m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform;
47 if (pPlatform->version <= 2) {
48 unsigned int embedderDataSlot = 0;
49 v8::Isolate* pExternalIsolate = nullptr;
50 if (pPlatform->version == 2) {
51 pExternalIsolate = static_cast<v8::Isolate*>(pPlatform->m_isolate);
52 embedderDataSlot = pPlatform->m_v8EmbedderSlot;
53 }
54 FXJS_Initialize(embedderDataSlot, pExternalIsolate);
55 }
56 m_isolateManaged = FXJS_GetIsolate(&pIsolate);
57 SetIsolate(pIsolate);
58
59 v8::Isolate::Scope isolate_scope(pIsolate);
60 v8::HandleScope handle_scope(pIsolate);
61 if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0)
62 DefineJSObjects();
63
64 ScopedEventContext pContext(this);
65 InitializeEngine();
66 SetFormFillEnvToDocument();
67 }
68
~CJS_Runtime()69 CJS_Runtime::~CJS_Runtime() {
70 NotifyObservers();
71 ReleaseEngine();
72 if (m_isolateManaged)
73 DisposeIsolate();
74 }
75
DefineJSObjects()76 void CJS_Runtime::DefineJSObjects() {
77 v8::Isolate::Scope isolate_scope(GetIsolate());
78 v8::HandleScope handle_scope(GetIsolate());
79 v8::Local<v8::Context> context = v8::Context::New(GetIsolate());
80 v8::Context::Scope context_scope(context);
81
82 // The call order determines the "ObjDefID" assigned to each class.
83 // ObjDefIDs 0 - 2
84 CJS_Border::DefineJSObjects(this);
85 CJS_Display::DefineJSObjects(this);
86 CJS_Font::DefineJSObjects(this);
87
88 // ObjDefIDs 3 - 5
89 CJS_Highlight::DefineJSObjects(this);
90 CJS_Position::DefineJSObjects(this);
91 CJS_ScaleHow::DefineJSObjects(this);
92
93 // ObjDefIDs 6 - 8
94 CJS_ScaleWhen::DefineJSObjects(this);
95 CJS_Style::DefineJSObjects(this);
96 CJS_Zoomtype::DefineJSObjects(this);
97
98 // ObjDefIDs 9 - 11
99 CJS_App::DefineJSObjects(this);
100 CJS_Color::DefineJSObjects(this);
101 CJS_Console::DefineJSObjects(this);
102
103 // ObjDefIDs 12 - 14
104 CJS_Document::DefineJSObjects(this);
105 CJS_Event::DefineJSObjects(this);
106 CJS_Field::DefineJSObjects(this);
107
108 // ObjDefIDs 15 - 17
109 CJS_Global::DefineJSObjects(this);
110 CJS_Icon::DefineJSObjects(this);
111 CJS_Util::DefineJSObjects(this);
112
113 // ObjDefIDs 18 - 20 (these can't fail, return void).
114 CJS_PublicMethods::DefineJSObjects(this);
115 CJS_GlobalConsts::DefineJSObjects(this);
116 CJS_GlobalArrays::DefineJSObjects(this);
117
118 // ObjDefIDs 21 - 22.
119 CJS_TimerObj::DefineJSObjects(this);
120 CJS_Annot::DefineJSObjects(this);
121 }
122
NewEventContext()123 IJS_EventContext* CJS_Runtime::NewEventContext() {
124 m_EventContextArray.push_back(pdfium::MakeUnique<CJS_EventContext>(this));
125 return m_EventContextArray.back().get();
126 }
127
ReleaseEventContext(IJS_EventContext * pContext)128 void CJS_Runtime::ReleaseEventContext(IJS_EventContext* pContext) {
129 ASSERT(pContext == m_EventContextArray.back().get());
130 m_EventContextArray.pop_back();
131 }
132
GetCurrentEventContext() const133 CJS_EventContext* CJS_Runtime::GetCurrentEventContext() const {
134 return m_EventContextArray.empty() ? nullptr
135 : m_EventContextArray.back().get();
136 }
137
GetTimerHandler() const138 TimerHandlerIface* CJS_Runtime::GetTimerHandler() const {
139 return m_pFormFillEnv ? m_pFormFillEnv->GetTimerHandler() : nullptr;
140 }
141
SetFormFillEnvToDocument()142 void CJS_Runtime::SetFormFillEnvToDocument() {
143 v8::Isolate::Scope isolate_scope(GetIsolate());
144 v8::HandleScope handle_scope(GetIsolate());
145 v8::Local<v8::Context> context = GetV8Context();
146 v8::Context::Scope context_scope(context);
147
148 v8::Local<v8::Object> pThis = GetThisObj();
149 if (pThis.IsEmpty())
150 return;
151
152 auto pJSDocument = JSGetObject<CJS_Document>(pThis);
153 if (!pJSDocument)
154 return;
155
156 pJSDocument->SetFormFillEnv(m_pFormFillEnv.Get());
157 }
158
GetFormFillEnv() const159 CPDFSDK_FormFillEnvironment* CJS_Runtime::GetFormFillEnv() const {
160 return m_pFormFillEnv.Get();
161 }
162
ExecuteScript(const WideString & script)163 Optional<IJS_Runtime::JS_Error> CJS_Runtime::ExecuteScript(
164 const WideString& script) {
165 return Execute(script);
166 }
167
AddEventToSet(const FieldEvent & event)168 bool CJS_Runtime::AddEventToSet(const FieldEvent& event) {
169 return m_FieldEventSet.insert(event).second;
170 }
171
RemoveEventFromSet(const FieldEvent & event)172 void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) {
173 m_FieldEventSet.erase(event);
174 }
175
AsCJSRuntime()176 CJS_Runtime* CJS_Runtime::AsCJSRuntime() {
177 return this;
178 }
179
GetValueByNameFromGlobalObject(ByteStringView utf8Name,v8::Local<v8::Value> * pValue)180 bool CJS_Runtime::GetValueByNameFromGlobalObject(ByteStringView utf8Name,
181 v8::Local<v8::Value>* pValue) {
182 v8::Isolate::Scope isolate_scope(GetIsolate());
183 v8::Local<v8::Context> context = GetV8Context();
184 v8::Context::Scope context_scope(context);
185 v8::Local<v8::String> str = fxv8::NewStringHelper(GetIsolate(), utf8Name);
186 v8::MaybeLocal<v8::Value> maybe_propvalue =
187 context->Global()->Get(context, str);
188 if (maybe_propvalue.IsEmpty())
189 return false;
190
191 *pValue = maybe_propvalue.ToLocalChecked();
192 return true;
193 }
194
SetValueByNameInGlobalObject(ByteStringView utf8Name,v8::Local<v8::Value> pValue)195 bool CJS_Runtime::SetValueByNameInGlobalObject(ByteStringView utf8Name,
196 v8::Local<v8::Value> pValue) {
197 if (utf8Name.IsEmpty() || pValue.IsEmpty())
198 return false;
199
200 v8::Isolate* pIsolate = GetIsolate();
201 v8::Isolate::Scope isolate_scope(pIsolate);
202 v8::Local<v8::Context> context = GetV8Context();
203 v8::Context::Scope context_scope(context);
204 v8::Local<v8::String> str = fxv8::NewStringHelper(pIsolate, utf8Name);
205 v8::Maybe<bool> result = context->Global()->Set(context, str, pValue);
206 return result.IsJust() && result.FromJust();
207 }
208
MaybeCoerceToNumber(v8::Local<v8::Value> value)209 v8::Local<v8::Value> CJS_Runtime::MaybeCoerceToNumber(
210 v8::Local<v8::Value> value) {
211 bool bAllowNaN = false;
212 if (value->IsString()) {
213 ByteString bstr = ToWideString(value).ToDefANSI();
214 if (bstr.IsEmpty())
215 return value;
216 if (bstr == "NaN")
217 bAllowNaN = true;
218 }
219
220 v8::Isolate* pIsolate = GetIsolate();
221 v8::TryCatch try_catch(pIsolate);
222 v8::MaybeLocal<v8::Number> maybeNum =
223 value->ToNumber(pIsolate->GetCurrentContext());
224 if (maybeNum.IsEmpty())
225 return value;
226
227 v8::Local<v8::Number> num = maybeNum.ToLocalChecked();
228 if (std::isnan(num->Value()) && !bAllowNaN)
229 return value;
230
231 return num;
232 }
233