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