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