• 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_global.h"
8 
9 #include <memory>
10 #include <utility>
11 #include <vector>
12 
13 #include "core/fxcrt/fx_extension.h"
14 #include "fxjs/cfx_globaldata.h"
15 #include "fxjs/cfx_keyvalue.h"
16 #include "fxjs/cjs_event_context.h"
17 #include "fxjs/cjs_object.h"
18 #include "fxjs/fxv8.h"
19 #include "fxjs/js_define.h"
20 #include "fxjs/js_resources.h"
21 #include "third_party/base/check.h"
22 #include "third_party/base/containers/contains.h"
23 #include "v8/include/v8-isolate.h"
24 
25 namespace {
26 
ByteStringFromV8Name(v8::Isolate * pIsolate,v8::Local<v8::Name> name)27 ByteString ByteStringFromV8Name(v8::Isolate* pIsolate,
28                                 v8::Local<v8::Name> name) {
29   CHECK(name->IsString());
30   return fxv8::ToByteString(pIsolate, name.As<v8::String>());
31 }
32 
33 }  // namespace
34 
35 CJS_Global::JSGlobalData::JSGlobalData() = default;
36 
37 CJS_Global::JSGlobalData::~JSGlobalData() = default;
38 
39 const JSMethodSpec CJS_Global::MethodSpecs[] = {
40     {"setPersistent", setPersistent_static}};
41 
42 uint32_t CJS_Global::ObjDefnID = 0;
43 
44 // static
setPersistent_static(const v8::FunctionCallbackInfo<v8::Value> & info)45 void CJS_Global::setPersistent_static(
46     const v8::FunctionCallbackInfo<v8::Value>& info) {
47   JSMethod<CJS_Global, &CJS_Global::setPersistent>("setPersistent", "global",
48                                                    info);
49 }
50 
51 // static
queryprop_static(v8::Local<v8::Name> property,const v8::PropertyCallbackInfo<v8::Integer> & info)52 void CJS_Global::queryprop_static(
53     v8::Local<v8::Name> property,
54     const v8::PropertyCallbackInfo<v8::Integer>& info) {
55   auto pObj = JSGetObject<CJS_Global>(info.GetIsolate(), info.Holder());
56   if (!pObj)
57     return;
58 
59   ByteString bsProp = ByteStringFromV8Name(info.GetIsolate(), property);
60   if (pObj->HasProperty(bsProp))
61     info.GetReturnValue().Set(static_cast<int>(v8::PropertyAttribute::None));
62 }
63 
64 // static
getprop_static(v8::Local<v8::Name> property,const v8::PropertyCallbackInfo<v8::Value> & info)65 void CJS_Global::getprop_static(
66     v8::Local<v8::Name> property,
67     const v8::PropertyCallbackInfo<v8::Value>& info) {
68   auto pObj = JSGetObject<CJS_Global>(info.GetIsolate(), info.Holder());
69   if (!pObj)
70     return;
71 
72   CJS_Runtime* pRuntime = pObj->GetRuntime();
73   if (!pRuntime)
74     return;
75 
76   ByteString bsProp = ByteStringFromV8Name(info.GetIsolate(), property);
77   CJS_Result result = pObj->GetProperty(pRuntime, bsProp);
78   if (result.HasError()) {
79     pRuntime->Error(
80         JSFormatErrorString("global", "GetProperty", result.Error()));
81     return;
82   }
83   if (result.HasReturn())
84     info.GetReturnValue().Set(result.Return());
85 }
86 
87 // static
putprop_static(v8::Local<v8::Name> property,v8::Local<v8::Value> value,const v8::PropertyCallbackInfo<v8::Value> & info)88 void CJS_Global::putprop_static(
89     v8::Local<v8::Name> property,
90     v8::Local<v8::Value> value,
91     const v8::PropertyCallbackInfo<v8::Value>& info) {
92   auto pObj = JSGetObject<CJS_Global>(info.GetIsolate(), info.Holder());
93   if (!pObj)
94     return;
95 
96   CJS_Runtime* pRuntime = pObj->GetRuntime();
97   if (!pRuntime)
98     return;
99 
100   ByteString bsProp = ByteStringFromV8Name(info.GetIsolate(), property);
101   CJS_Result result = pObj->SetProperty(pRuntime, bsProp, value);
102   if (result.HasError()) {
103     pRuntime->Error(
104         JSFormatErrorString("global", "PutProperty", result.Error()));
105     return;
106   }
107   info.GetReturnValue().Set(value);
108 }
109 
110 // static
delprop_static(v8::Local<v8::Name> property,const v8::PropertyCallbackInfo<v8::Boolean> & info)111 void CJS_Global::delprop_static(
112     v8::Local<v8::Name> property,
113     const v8::PropertyCallbackInfo<v8::Boolean>& info) {
114   auto pObj = JSGetObject<CJS_Global>(info.GetIsolate(), info.Holder());
115   if (!pObj)
116     return;
117 
118   ByteString bsProp = ByteStringFromV8Name(info.GetIsolate(), property);
119   if (pObj->DelProperty(bsProp))
120     info.GetReturnValue().Set(true);
121 }
122 
enumprop_static(const v8::PropertyCallbackInfo<v8::Array> & info)123 void CJS_Global::enumprop_static(
124     const v8::PropertyCallbackInfo<v8::Array>& info) {
125   auto pObj = JSGetObject<CJS_Global>(info.GetIsolate(), info.Holder());
126   if (!pObj)
127     return;
128 
129   CJS_Runtime* pRuntime = pObj->GetRuntime();
130   if (!pRuntime)
131     return;
132 
133   pObj->EnumProperties(pRuntime, info);
134 }
135 
136 // static
DefineAllProperties(CFXJS_Engine * pEngine)137 void CJS_Global::DefineAllProperties(CFXJS_Engine* pEngine) {
138   pEngine->DefineObjAllProperties(
139       ObjDefnID, CJS_Global::queryprop_static, CJS_Global::getprop_static,
140       CJS_Global::putprop_static, CJS_Global::delprop_static,
141       CJS_Global::enumprop_static);
142 }
143 
144 // static
GetObjDefnID()145 uint32_t CJS_Global::GetObjDefnID() {
146   return ObjDefnID;
147 }
148 
149 // static
DefineJSObjects(CFXJS_Engine * pEngine)150 void CJS_Global::DefineJSObjects(CFXJS_Engine* pEngine) {
151   ObjDefnID = pEngine->DefineObj("global", FXJSOBJTYPE_STATIC,
152                                  JSConstructor<CJS_Global>, JSDestructor);
153   DefineMethods(pEngine, ObjDefnID, MethodSpecs);
154   DefineAllProperties(pEngine);
155 }
156 
CJS_Global(v8::Local<v8::Object> pObject,CJS_Runtime * pRuntime)157 CJS_Global::CJS_Global(v8::Local<v8::Object> pObject, CJS_Runtime* pRuntime)
158     : CJS_Object(pObject, pRuntime),
159       m_pGlobalData(CFX_GlobalData::GetRetainedInstance(nullptr)) {
160   UpdateGlobalPersistentVariables();
161 }
162 
~CJS_Global()163 CJS_Global::~CJS_Global() {
164   DestroyGlobalPersisitentVariables();
165   m_pGlobalData.ExtractAsDangling()->Release();
166 }
167 
HasProperty(const ByteString & propname)168 bool CJS_Global::HasProperty(const ByteString& propname) {
169   return pdfium::Contains(m_MapGlobal, propname);
170 }
171 
DelProperty(const ByteString & propname)172 bool CJS_Global::DelProperty(const ByteString& propname) {
173   auto it = m_MapGlobal.find(propname);
174   if (it == m_MapGlobal.end())
175     return false;
176 
177   it->second->bDeleted = true;
178   return true;
179 }
180 
GetProperty(CJS_Runtime * pRuntime,const ByteString & propname)181 CJS_Result CJS_Global::GetProperty(CJS_Runtime* pRuntime,
182                                    const ByteString& propname) {
183   auto it = m_MapGlobal.find(propname);
184   if (it == m_MapGlobal.end())
185     return CJS_Result::Success();
186 
187   JSGlobalData* pData = it->second.get();
188   if (pData->bDeleted)
189     return CJS_Result::Success();
190 
191   switch (pData->nType) {
192     case CFX_Value::DataType::kNumber:
193       return CJS_Result::Success(pRuntime->NewNumber(pData->dData));
194     case CFX_Value::DataType::kBoolean:
195       return CJS_Result::Success(pRuntime->NewBoolean(pData->bData));
196     case CFX_Value::DataType::kString:
197       return CJS_Result::Success(
198           pRuntime->NewString(pData->sData.AsStringView()));
199     case CFX_Value::DataType::kObject:
200       return CJS_Result::Success(
201           v8::Local<v8::Object>::New(pRuntime->GetIsolate(), pData->pData));
202     case CFX_Value::DataType::kNull:
203       return CJS_Result::Success(pRuntime->NewNull());
204     default:
205       break;
206   }
207   return CJS_Result::Failure(JSMessage::kObjectTypeError);
208 }
209 
SetProperty(CJS_Runtime * pRuntime,const ByteString & propname,v8::Local<v8::Value> vp)210 CJS_Result CJS_Global::SetProperty(CJS_Runtime* pRuntime,
211                                    const ByteString& propname,
212                                    v8::Local<v8::Value> vp) {
213   if (vp->IsNumber()) {
214     return SetGlobalVariables(propname, CFX_Value::DataType::kNumber,
215                               pRuntime->ToDouble(vp), false, ByteString(),
216                               v8::Local<v8::Object>(), false);
217   }
218   if (vp->IsBoolean()) {
219     return SetGlobalVariables(propname, CFX_Value::DataType::kBoolean, 0,
220                               pRuntime->ToBoolean(vp), ByteString(),
221                               v8::Local<v8::Object>(), false);
222   }
223   if (vp->IsString()) {
224     return SetGlobalVariables(propname, CFX_Value::DataType::kString, 0, false,
225                               pRuntime->ToByteString(vp),
226                               v8::Local<v8::Object>(), false);
227   }
228   if (vp->IsObject()) {
229     return SetGlobalVariables(propname, CFX_Value::DataType::kObject, 0, false,
230                               ByteString(), pRuntime->ToObject(vp), false);
231   }
232   if (vp->IsNull()) {
233     return SetGlobalVariables(propname, CFX_Value::DataType::kNull, 0, false,
234                               ByteString(), v8::Local<v8::Object>(), false);
235   }
236   if (vp->IsUndefined()) {
237     DelProperty(propname);
238     return CJS_Result::Success();
239   }
240   return CJS_Result::Failure(JSMessage::kObjectTypeError);
241 }
242 
EnumProperties(CJS_Runtime * pRuntime,const v8::PropertyCallbackInfo<v8::Array> & info)243 void CJS_Global::EnumProperties(
244     CJS_Runtime* pRuntime,
245     const v8::PropertyCallbackInfo<v8::Array>& info) {
246   v8::Local<v8::Array> result = pRuntime->NewArray();
247   int idx = 0;
248   for (const auto& it : m_MapGlobal) {
249     if (it.second->bDeleted)
250       continue;
251     v8::Local<v8::Name> name = pRuntime->NewString(it.first.AsStringView());
252     pRuntime->PutArrayElement(result, idx, name);
253     ++idx;
254   }
255   info.GetReturnValue().Set(result);
256 }
257 
setPersistent(CJS_Runtime * pRuntime,const std::vector<v8::Local<v8::Value>> & params)258 CJS_Result CJS_Global::setPersistent(
259     CJS_Runtime* pRuntime,
260     const std::vector<v8::Local<v8::Value>>& params) {
261   if (params.size() != 2)
262     return CJS_Result::Failure(JSMessage::kParamError);
263 
264   auto it = m_MapGlobal.find(pRuntime->ToByteString(params[0]));
265   if (it == m_MapGlobal.end() || it->second->bDeleted)
266     return CJS_Result::Failure(JSMessage::kGlobalNotFoundError);
267 
268   it->second->bPersistent = pRuntime->ToBoolean(params[1]);
269   return CJS_Result::Success();
270 }
271 
UpdateGlobalPersistentVariables()272 void CJS_Global::UpdateGlobalPersistentVariables() {
273   CJS_Runtime* pRuntime = GetRuntime();
274   if (!pRuntime)
275     return;
276 
277   for (int i = 0, sz = m_pGlobalData->GetSize(); i < sz; i++) {
278     CFX_GlobalData::Element* pData = m_pGlobalData->GetAt(i);
279     switch (pData->data.nType) {
280       case CFX_Value::DataType::kNumber:
281         SetGlobalVariables(pData->data.sKey, CFX_Value::DataType::kNumber,
282                            pData->data.dData, false, ByteString(),
283                            v8::Local<v8::Object>(), pData->bPersistent);
284         pRuntime->PutObjectProperty(ToV8Object(),
285                                     pData->data.sKey.AsStringView(),
286                                     pRuntime->NewNumber(pData->data.dData));
287         break;
288       case CFX_Value::DataType::kBoolean:
289         SetGlobalVariables(pData->data.sKey, CFX_Value::DataType::kBoolean, 0,
290                            pData->data.bData == 1, ByteString(),
291                            v8::Local<v8::Object>(), pData->bPersistent);
292         pRuntime->PutObjectProperty(
293             ToV8Object(), pData->data.sKey.AsStringView(),
294             pRuntime->NewBoolean(pData->data.bData == 1));
295         break;
296       case CFX_Value::DataType::kString:
297         SetGlobalVariables(pData->data.sKey, CFX_Value::DataType::kString, 0,
298                            false, pData->data.sData, v8::Local<v8::Object>(),
299                            pData->bPersistent);
300         pRuntime->PutObjectProperty(
301             ToV8Object(), pData->data.sKey.AsStringView(),
302             pRuntime->NewString(pData->data.sData.AsStringView()));
303         break;
304       case CFX_Value::DataType::kObject: {
305         v8::Local<v8::Object> pObj = pRuntime->NewObject();
306         if (!pObj.IsEmpty()) {
307           PutObjectProperty(pObj, &pData->data);
308           SetGlobalVariables(pData->data.sKey, CFX_Value::DataType::kObject, 0,
309                              false, ByteString(), pObj, pData->bPersistent);
310           pRuntime->PutObjectProperty(ToV8Object(),
311                                       pData->data.sKey.AsStringView(), pObj);
312         }
313       } break;
314       case CFX_Value::DataType::kNull:
315         SetGlobalVariables(pData->data.sKey, CFX_Value::DataType::kNull, 0,
316                            false, ByteString(), v8::Local<v8::Object>(),
317                            pData->bPersistent);
318         pRuntime->PutObjectProperty(
319             ToV8Object(), pData->data.sKey.AsStringView(), pRuntime->NewNull());
320         break;
321     }
322   }
323 }
324 
CommitGlobalPersisitentVariables()325 void CJS_Global::CommitGlobalPersisitentVariables() {
326   CJS_Runtime* pRuntime = GetRuntime();
327   if (!pRuntime)
328     return;
329 
330   for (const auto& iter : m_MapGlobal) {
331     ByteString name = iter.first;
332     JSGlobalData* pData = iter.second.get();
333     if (pData->bDeleted) {
334       m_pGlobalData->DeleteGlobalVariable(name);
335       continue;
336     }
337     switch (pData->nType) {
338       case CFX_Value::DataType::kNumber:
339         m_pGlobalData->SetGlobalVariableNumber(name, pData->dData);
340         m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent);
341         break;
342       case CFX_Value::DataType::kBoolean:
343         m_pGlobalData->SetGlobalVariableBoolean(name, pData->bData);
344         m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent);
345         break;
346       case CFX_Value::DataType::kString:
347         m_pGlobalData->SetGlobalVariableString(name, pData->sData);
348         m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent);
349         break;
350       case CFX_Value::DataType::kObject: {
351         v8::Local<v8::Object> obj =
352             v8::Local<v8::Object>::New(pRuntime->GetIsolate(), pData->pData);
353         m_pGlobalData->SetGlobalVariableObject(name,
354                                                ObjectToArray(pRuntime, obj));
355         m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent);
356       } break;
357       case CFX_Value::DataType::kNull:
358         m_pGlobalData->SetGlobalVariableNull(name);
359         m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent);
360         break;
361     }
362   }
363 }
364 
ObjectToArray(CJS_Runtime * pRuntime,v8::Local<v8::Object> pObj)365 std::vector<std::unique_ptr<CFX_KeyValue>> CJS_Global::ObjectToArray(
366     CJS_Runtime* pRuntime,
367     v8::Local<v8::Object> pObj) {
368   std::vector<std::unique_ptr<CFX_KeyValue>> array;
369   std::vector<WideString> pKeyList = pRuntime->GetObjectPropertyNames(pObj);
370   for (const auto& ws : pKeyList) {
371     ByteString sKey = ws.ToUTF8();
372     v8::Local<v8::Value> v =
373         pRuntime->GetObjectProperty(pObj, sKey.AsStringView());
374     if (v->IsNumber()) {
375       auto pObjElement = std::make_unique<CFX_KeyValue>();
376       pObjElement->nType = CFX_Value::DataType::kNumber;
377       pObjElement->sKey = sKey;
378       pObjElement->dData = pRuntime->ToDouble(v);
379       array.push_back(std::move(pObjElement));
380       continue;
381     }
382     if (v->IsBoolean()) {
383       auto pObjElement = std::make_unique<CFX_KeyValue>();
384       pObjElement->nType = CFX_Value::DataType::kBoolean;
385       pObjElement->sKey = sKey;
386       pObjElement->dData = pRuntime->ToBoolean(v);
387       array.push_back(std::move(pObjElement));
388       continue;
389     }
390     if (v->IsString()) {
391       ByteString sValue = pRuntime->ToByteString(v);
392       auto pObjElement = std::make_unique<CFX_KeyValue>();
393       pObjElement->nType = CFX_Value::DataType::kString;
394       pObjElement->sKey = sKey;
395       pObjElement->sData = sValue;
396       array.push_back(std::move(pObjElement));
397       continue;
398     }
399     if (v->IsObject()) {
400       auto pObjElement = std::make_unique<CFX_KeyValue>();
401       pObjElement->nType = CFX_Value::DataType::kObject;
402       pObjElement->sKey = sKey;
403       pObjElement->objData = ObjectToArray(pRuntime, pRuntime->ToObject(v));
404       array.push_back(std::move(pObjElement));
405       continue;
406     }
407     if (v->IsNull()) {
408       auto pObjElement = std::make_unique<CFX_KeyValue>();
409       pObjElement->nType = CFX_Value::DataType::kNull;
410       pObjElement->sKey = sKey;
411       array.push_back(std::move(pObjElement));
412     }
413   }
414   return array;
415 }
416 
PutObjectProperty(v8::Local<v8::Object> pObj,CFX_KeyValue * pData)417 void CJS_Global::PutObjectProperty(v8::Local<v8::Object> pObj,
418                                    CFX_KeyValue* pData) {
419   CJS_Runtime* pRuntime = GetRuntime();
420   if (pRuntime)
421     return;
422 
423   for (size_t i = 0; i < pData->objData.size(); ++i) {
424     CFX_KeyValue* pObjData = pData->objData.at(i).get();
425     switch (pObjData->nType) {
426       case CFX_Value::DataType::kNumber:
427         pRuntime->PutObjectProperty(pObj, pObjData->sKey.AsStringView(),
428                                     pRuntime->NewNumber(pObjData->dData));
429         break;
430       case CFX_Value::DataType::kBoolean:
431         pRuntime->PutObjectProperty(pObj, pObjData->sKey.AsStringView(),
432                                     pRuntime->NewBoolean(pObjData->bData == 1));
433         break;
434       case CFX_Value::DataType::kString:
435         pRuntime->PutObjectProperty(
436             pObj, pObjData->sKey.AsStringView(),
437             pRuntime->NewString(pObjData->sData.AsStringView()));
438         break;
439       case CFX_Value::DataType::kObject: {
440         v8::Local<v8::Object> pNewObj = pRuntime->NewObject();
441         if (!pNewObj.IsEmpty()) {
442           PutObjectProperty(pNewObj, pObjData);
443           pRuntime->PutObjectProperty(pObj, pObjData->sKey.AsStringView(),
444                                       pNewObj);
445         }
446       } break;
447       case CFX_Value::DataType::kNull:
448         pRuntime->PutObjectProperty(pObj, pObjData->sKey.AsStringView(),
449                                     pRuntime->NewNull());
450         break;
451     }
452   }
453 }
454 
DestroyGlobalPersisitentVariables()455 void CJS_Global::DestroyGlobalPersisitentVariables() {
456   m_MapGlobal.clear();
457 }
458 
SetGlobalVariables(const ByteString & propname,CFX_Value::DataType nType,double dData,bool bData,const ByteString & sData,v8::Local<v8::Object> pData,bool bDefaultPersistent)459 CJS_Result CJS_Global::SetGlobalVariables(const ByteString& propname,
460                                           CFX_Value::DataType nType,
461                                           double dData,
462                                           bool bData,
463                                           const ByteString& sData,
464                                           v8::Local<v8::Object> pData,
465                                           bool bDefaultPersistent) {
466   if (propname.IsEmpty())
467     return CJS_Result::Failure(JSMessage::kUnknownProperty);
468 
469   auto it = m_MapGlobal.find(propname);
470   if (it != m_MapGlobal.end()) {
471     JSGlobalData* pTemp = it->second.get();
472     if (pTemp->bDeleted || pTemp->nType != nType) {
473       pTemp->dData = 0;
474       pTemp->bData = false;
475       pTemp->sData.clear();
476       pTemp->nType = nType;
477     }
478     pTemp->bDeleted = false;
479     switch (nType) {
480       case CFX_Value::DataType::kNumber:
481         pTemp->dData = dData;
482         break;
483       case CFX_Value::DataType::kBoolean:
484         pTemp->bData = bData;
485         break;
486       case CFX_Value::DataType::kString:
487         pTemp->sData = sData;
488         break;
489       case CFX_Value::DataType::kObject:
490         pTemp->pData.Reset(pData->GetIsolate(), pData);
491         break;
492       case CFX_Value::DataType::kNull:
493         break;
494       default:
495         return CJS_Result::Failure(JSMessage::kObjectTypeError);
496     }
497     return CJS_Result::Success();
498   }
499 
500   auto pNewData = std::make_unique<JSGlobalData>();
501   switch (nType) {
502     case CFX_Value::DataType::kNumber:
503       pNewData->nType = CFX_Value::DataType::kNumber;
504       pNewData->dData = dData;
505       pNewData->bPersistent = bDefaultPersistent;
506       break;
507     case CFX_Value::DataType::kBoolean:
508       pNewData->nType = CFX_Value::DataType::kBoolean;
509       pNewData->bData = bData;
510       pNewData->bPersistent = bDefaultPersistent;
511       break;
512     case CFX_Value::DataType::kString:
513       pNewData->nType = CFX_Value::DataType::kString;
514       pNewData->sData = sData;
515       pNewData->bPersistent = bDefaultPersistent;
516       break;
517     case CFX_Value::DataType::kObject:
518       pNewData->nType = CFX_Value::DataType::kObject;
519       pNewData->pData.Reset(pData->GetIsolate(), pData);
520       pNewData->bPersistent = bDefaultPersistent;
521       break;
522     case CFX_Value::DataType::kNull:
523       pNewData->nType = CFX_Value::DataType::kNull;
524       pNewData->bPersistent = bDefaultPersistent;
525       break;
526     default:
527       return CJS_Result::Failure(JSMessage::kObjectTypeError);
528   }
529   m_MapGlobal[propname] = std::move(pNewData);
530   return CJS_Result::Success();
531 }
532