From d7ac26c410073359ef83c84bba21df7956b024fb Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Wed, 25 Oct 2017 20:30:02 -0400 Subject: Move global.{h|cpp} to match class This CL moves global.{h|cpp} to cjs_global.{h|cpp} to match the internal class name. Change-Id: I58a0dfcb36ee35320a0ad0d134a544e615dc93bf Reviewed-on: https://pdfium-review.googlesource.com/16870 Reviewed-by: Tom Sepez Commit-Queue: dsinclair --- BUILD.gn | 4 +- fpdfsdk/javascript/cjs_global.cpp | 614 +++++++++++++++++++++++++++++++++++++ fpdfsdk/javascript/cjs_global.h | 39 +++ fpdfsdk/javascript/cjs_runtime.cpp | 2 +- fpdfsdk/javascript/global.cpp | 614 ------------------------------------- fpdfsdk/javascript/global.h | 39 --- 6 files changed, 656 insertions(+), 656 deletions(-) create mode 100644 fpdfsdk/javascript/cjs_global.cpp create mode 100644 fpdfsdk/javascript/cjs_global.h delete mode 100644 fpdfsdk/javascript/global.cpp delete mode 100644 fpdfsdk/javascript/global.h diff --git a/BUILD.gn b/BUILD.gn index 8d3148dd01..7cdd7e210e 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1219,6 +1219,8 @@ static_library("javascript") { "fpdfsdk/javascript/cjs_event_context.h", "fpdfsdk/javascript/cjs_font.cpp", "fpdfsdk/javascript/cjs_font.h", + "fpdfsdk/javascript/cjs_global.cpp", + "fpdfsdk/javascript/cjs_global.h", "fpdfsdk/javascript/cjs_globalarrays.cpp", "fpdfsdk/javascript/cjs_globalarrays.h", "fpdfsdk/javascript/cjs_globalconsts.cpp", @@ -1243,8 +1245,6 @@ static_library("javascript") { "fpdfsdk/javascript/console.h", "fpdfsdk/javascript/event.cpp", "fpdfsdk/javascript/event.h", - "fpdfsdk/javascript/global.cpp", - "fpdfsdk/javascript/global.h", "fpdfsdk/javascript/report.cpp", "fpdfsdk/javascript/report.h", "fpdfsdk/javascript/resource.cpp", diff --git a/fpdfsdk/javascript/cjs_global.cpp b/fpdfsdk/javascript/cjs_global.cpp new file mode 100644 index 0000000000..f282a2694e --- /dev/null +++ b/fpdfsdk/javascript/cjs_global.cpp @@ -0,0 +1,614 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "fpdfsdk/javascript/cjs_global.h" + +#include +#include +#include +#include + +#include "core/fxcrt/fx_extension.h" +#include "fpdfsdk/javascript/JS_Define.h" +#include "fpdfsdk/javascript/JS_EventHandler.h" +#include "fpdfsdk/javascript/JS_GlobalData.h" +#include "fpdfsdk/javascript/JS_KeyValue.h" +#include "fpdfsdk/javascript/JS_Object.h" +#include "fpdfsdk/javascript/JS_Value.h" +#include "fpdfsdk/javascript/cjs_event_context.h" +#include "fpdfsdk/javascript/resource.h" + +#define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \ + IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ + IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ + IMPLEMENT_JS_CLASS_PART(js_class_name, class_alternate, class_name) \ + void js_class_name::queryprop_static( \ + v8::Local property, \ + const v8::PropertyCallbackInfo& info) { \ + JSSpecialPropQuery(#class_name, property, info); \ + } \ + void js_class_name::getprop_static( \ + v8::Local property, \ + const v8::PropertyCallbackInfo& info) { \ + JSSpecialPropGet(#class_name, property, info); \ + } \ + void js_class_name::putprop_static( \ + v8::Local property, v8::Local value, \ + const v8::PropertyCallbackInfo& info) { \ + JSSpecialPropPut(#class_name, property, value, info); \ + } \ + void js_class_name::delprop_static( \ + v8::Local property, \ + const v8::PropertyCallbackInfo& info) { \ + JSSpecialPropDel(#class_name, property, info); \ + } \ + void js_class_name::DefineAllProperties(CFXJS_Engine* pEngine) { \ + pEngine->DefineObjAllProperties( \ + g_nObjDefnID, js_class_name::queryprop_static, \ + js_class_name::getprop_static, js_class_name::putprop_static, \ + js_class_name::delprop_static); \ + } \ + void js_class_name::DefineJSObjects(CFXJS_Engine* pEngine, \ + FXJSOBJTYPE eObjType) { \ + g_nObjDefnID = pEngine->DefineObj(js_class_name::g_pClassName, eObjType, \ + JSConstructor, JSDestructor); \ + DefineConsts(pEngine); \ + DefineProps(pEngine); \ + DefineMethods(pEngine); \ + DefineAllProperties(pEngine); \ + } + +namespace { + +WideString PropFromV8Prop(v8::Local property) { + v8::String::Utf8Value utf8_value(property); + return WideString::FromUTF8(ByteStringView(*utf8_value, utf8_value.length())); +} + +template +void JSSpecialPropQuery(const char*, + v8::Local property, + const v8::PropertyCallbackInfo& info) { + CJS_Runtime* pRuntime = + CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + if (!pRuntime) + return; + + CJS_Object* pJSObj = + static_cast(pRuntime->GetObjectPrivate(info.Holder())); + if (!pJSObj) + return; + + Alt* pObj = reinterpret_cast(pJSObj->GetEmbedObject()); + CJS_Return result = pObj->QueryProperty(PropFromV8Prop(property).c_str()); + info.GetReturnValue().Set(!result.HasError() ? 4 : 0); +} + +template +void JSSpecialPropGet(const char* class_name, + v8::Local property, + const v8::PropertyCallbackInfo& info) { + CJS_Runtime* pRuntime = + CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + if (!pRuntime) + return; + + CJS_Object* pJSObj = + static_cast(pRuntime->GetObjectPrivate(info.Holder())); + if (!pJSObj) + return; + + Alt* pObj = reinterpret_cast(pJSObj->GetEmbedObject()); + CJS_Return result = + pObj->GetProperty(pRuntime, PropFromV8Prop(property).c_str()); + if (result.HasError()) { + pRuntime->Error( + JSFormatErrorString(class_name, "GetProperty", result.Error())); + return; + } + + if (result.HasReturn()) + info.GetReturnValue().Set(result.Return()); +} + +template +void JSSpecialPropPut(const char* class_name, + v8::Local property, + v8::Local value, + const v8::PropertyCallbackInfo& info) { + CJS_Runtime* pRuntime = + CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + if (!pRuntime) + return; + + CJS_Object* pJSObj = + static_cast(pRuntime->GetObjectPrivate(info.Holder())); + if (!pJSObj) + return; + + Alt* pObj = reinterpret_cast(pJSObj->GetEmbedObject()); + CJS_Return result = + pObj->SetProperty(pRuntime, PropFromV8Prop(property).c_str(), value); + if (result.HasError()) { + pRuntime->Error( + JSFormatErrorString(class_name, "PutProperty", result.Error())); + } +} + +template +void JSSpecialPropDel(const char* class_name, + v8::Local property, + const v8::PropertyCallbackInfo& info) { + CJS_Runtime* pRuntime = + CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); + if (!pRuntime) + return; + + CJS_Object* pJSObj = + static_cast(pRuntime->GetObjectPrivate(info.Holder())); + if (!pJSObj) + return; + + Alt* pObj = reinterpret_cast(pJSObj->GetEmbedObject()); + CJS_Return result = + pObj->DelProperty(pRuntime, PropFromV8Prop(property).c_str()); + if (result.HasError()) { + // TODO(dsinclair): Should this set the pRuntime->Error result? + // ByteString cbName; + // cbName.Format("%s.%s", class_name, "DelProperty"); + } +} + +struct JSGlobalData { + JSGlobalData(); + ~JSGlobalData(); + + JS_GlobalDataType nType; + double dData; + bool bData; + ByteString sData; + v8::Global pData; + bool bPersistent; + bool bDeleted; +}; + +class JSGlobalAlternate : public CJS_EmbedObj { + public: + explicit JSGlobalAlternate(CJS_Object* pJSObject); + ~JSGlobalAlternate() override; + + CJS_Return setPersistent(CJS_Runtime* pRuntime, + const std::vector>& params); + CJS_Return QueryProperty(const wchar_t* propname); + CJS_Return GetProperty(CJS_Runtime* pRuntime, const wchar_t* propname); + CJS_Return SetProperty(CJS_Runtime* pRuntime, + const wchar_t* propname, + v8::Local vp); + CJS_Return DelProperty(CJS_Runtime* pRuntime, const wchar_t* propname); + void Initial(CPDFSDK_FormFillEnvironment* pFormFillEnv); + + private: + void UpdateGlobalPersistentVariables(); + void CommitGlobalPersisitentVariables(CJS_Runtime* pRuntime); + void DestroyGlobalPersisitentVariables(); + CJS_Return SetGlobalVariables(const ByteString& propname, + JS_GlobalDataType nType, + double dData, + bool bData, + const ByteString& sData, + v8::Local pData, + bool bDefaultPersistent); + void ObjectToArray(CJS_Runtime* pRuntime, + v8::Local pObj, + CJS_GlobalVariableArray& array); + void PutObjectProperty(v8::Local obj, CJS_KeyValue* pData); + + std::map> m_MapGlobal; + WideString m_sFilePath; + CJS_GlobalData* m_pGlobalData; + CPDFSDK_FormFillEnvironment::ObservedPtr m_pFormFillEnv; +}; + +} // namespace + +JSConstSpec CJS_Global::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}}; + +JSPropertySpec CJS_Global::PropertySpecs[] = {{0, 0, 0}}; + +JSMethodSpec CJS_Global::MethodSpecs[] = { + {"setPersistent", setPersistent_static}, + {0, 0}}; + +IMPLEMENT_SPECIAL_JS_CLASS(CJS_Global, JSGlobalAlternate, global); + +// static +void CJS_Global::setPersistent_static( + const v8::FunctionCallbackInfo& info) { + JSMethod( + "setPersistent", "global", info); +} + +void CJS_Global::InitInstance(IJS_Runtime* pIRuntime) { + CJS_Runtime* pRuntime = static_cast(pIRuntime); + JSGlobalAlternate* pGlobal = + static_cast(GetEmbedObject()); + pGlobal->Initial(pRuntime->GetFormFillEnv()); +} + +JSGlobalData::JSGlobalData() + : nType(JS_GlobalDataType::NUMBER), + dData(0), + bData(false), + sData(""), + bPersistent(false), + bDeleted(false) {} + +JSGlobalData::~JSGlobalData() { + pData.Reset(); +} + +JSGlobalAlternate::JSGlobalAlternate(CJS_Object* pJSObject) + : CJS_EmbedObj(pJSObject), m_pFormFillEnv(nullptr) {} + +JSGlobalAlternate::~JSGlobalAlternate() { + DestroyGlobalPersisitentVariables(); + m_pGlobalData->Release(); +} + +void JSGlobalAlternate::Initial(CPDFSDK_FormFillEnvironment* pFormFillEnv) { + m_pFormFillEnv.Reset(pFormFillEnv); + m_pGlobalData = CJS_GlobalData::GetRetainedInstance(pFormFillEnv); + UpdateGlobalPersistentVariables(); +} + +CJS_Return JSGlobalAlternate::QueryProperty(const wchar_t* propname) { + return CJS_Return(WideString(propname) != L"setPersistent"); +} + +CJS_Return JSGlobalAlternate::DelProperty(CJS_Runtime* pRuntime, + const wchar_t* propname) { + auto it = m_MapGlobal.find(ByteString::FromUnicode(propname)); + if (it == m_MapGlobal.end()) + return CJS_Return(false); + + it->second->bDeleted = true; + return CJS_Return(true); +} + +CJS_Return JSGlobalAlternate::GetProperty(CJS_Runtime* pRuntime, + const wchar_t* propname) { + auto it = m_MapGlobal.find(ByteString::FromUnicode(propname)); + if (it == m_MapGlobal.end()) + return CJS_Return(true); + + JSGlobalData* pData = it->second.get(); + if (pData->bDeleted) + return CJS_Return(true); + + switch (pData->nType) { + case JS_GlobalDataType::NUMBER: + return CJS_Return(pRuntime->NewNumber(pData->dData)); + case JS_GlobalDataType::BOOLEAN: + return CJS_Return(pRuntime->NewBoolean(pData->bData)); + case JS_GlobalDataType::STRING: + return CJS_Return(pRuntime->NewString( + WideString::FromLocal(pData->sData.c_str()).c_str())); + case JS_GlobalDataType::OBJECT: + return CJS_Return( + v8::Local::New(pRuntime->GetIsolate(), pData->pData)); + case JS_GlobalDataType::NULLOBJ: + return CJS_Return(pRuntime->NewNull()); + default: + break; + } + return CJS_Return(false); +} + +CJS_Return JSGlobalAlternate::SetProperty(CJS_Runtime* pRuntime, + const wchar_t* propname, + v8::Local vp) { + ByteString sPropName = ByteString::FromUnicode(propname); + if (vp->IsNumber()) { + return SetGlobalVariables(sPropName, JS_GlobalDataType::NUMBER, + pRuntime->ToDouble(vp), false, "", + v8::Local(), false); + } + if (vp->IsBoolean()) { + return SetGlobalVariables(sPropName, JS_GlobalDataType::BOOLEAN, 0, + pRuntime->ToBoolean(vp), "", + v8::Local(), false); + } + if (vp->IsString()) { + return SetGlobalVariables( + sPropName, JS_GlobalDataType::STRING, 0, false, + ByteString::FromUnicode(pRuntime->ToWideString(vp)), + v8::Local(), false); + } + if (vp->IsObject()) { + return SetGlobalVariables(sPropName, JS_GlobalDataType::OBJECT, 0, false, + "", pRuntime->ToObject(vp), false); + } + if (vp->IsNull()) { + return SetGlobalVariables(sPropName, JS_GlobalDataType::NULLOBJ, 0, false, + "", v8::Local(), false); + } + if (vp->IsUndefined()) { + DelProperty(pRuntime, propname); + return CJS_Return(true); + } + return CJS_Return(false); +} + +CJS_Return JSGlobalAlternate::setPersistent( + CJS_Runtime* pRuntime, + const std::vector>& params) { + if (params.size() != 2) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); + + auto it = m_MapGlobal.find( + ByteString::FromUnicode(pRuntime->ToWideString(params[0]))); + if (it == m_MapGlobal.end() || it->second->bDeleted) + return CJS_Return(JSGetStringFromID(IDS_STRING_JSNOGLOBAL)); + + it->second->bPersistent = pRuntime->ToBoolean(params[1]); + return CJS_Return(true); +} + +void JSGlobalAlternate::UpdateGlobalPersistentVariables() { + CJS_Runtime* pRuntime = + static_cast(CFXJS_Engine::CurrentEngineFromIsolate( + m_pJSObject->ToV8Object()->GetIsolate())); + + for (int i = 0, sz = m_pGlobalData->GetSize(); i < sz; i++) { + CJS_GlobalData_Element* pData = m_pGlobalData->GetAt(i); + switch (pData->data.nType) { + case JS_GlobalDataType::NUMBER: + SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::NUMBER, + pData->data.dData, false, "", + v8::Local(), pData->bPersistent == 1); + pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(), + pData->data.sKey.UTF8Decode(), + pRuntime->NewNumber(pData->data.dData)); + break; + case JS_GlobalDataType::BOOLEAN: + SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::BOOLEAN, 0, + pData->data.bData == 1, "", v8::Local(), + pData->bPersistent == 1); + pRuntime->PutObjectProperty( + m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(), + pRuntime->NewBoolean(pData->data.bData == 1)); + break; + case JS_GlobalDataType::STRING: + SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::STRING, 0, + false, pData->data.sData, v8::Local(), + pData->bPersistent == 1); + pRuntime->PutObjectProperty( + m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(), + pRuntime->NewString(pData->data.sData.UTF8Decode().AsStringView())); + break; + case JS_GlobalDataType::OBJECT: { + v8::Local pObj = pRuntime->NewFxDynamicObj(-1); + if (!pObj.IsEmpty()) { + PutObjectProperty(pObj, &pData->data); + SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::OBJECT, 0, + false, "", pObj, pData->bPersistent == 1); + pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(), + pData->data.sKey.UTF8Decode(), pObj); + } + } break; + case JS_GlobalDataType::NULLOBJ: + SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::NULLOBJ, 0, + false, "", v8::Local(), + pData->bPersistent == 1); + pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(), + pData->data.sKey.UTF8Decode(), + pRuntime->NewNull()); + break; + } + } +} + +void JSGlobalAlternate::CommitGlobalPersisitentVariables( + CJS_Runtime* pRuntime) { + for (const auto& iter : m_MapGlobal) { + ByteString name = iter.first; + JSGlobalData* pData = iter.second.get(); + if (pData->bDeleted) { + m_pGlobalData->DeleteGlobalVariable(name); + continue; + } + switch (pData->nType) { + case JS_GlobalDataType::NUMBER: + m_pGlobalData->SetGlobalVariableNumber(name, pData->dData); + m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); + break; + case JS_GlobalDataType::BOOLEAN: + m_pGlobalData->SetGlobalVariableBoolean(name, pData->bData); + m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); + break; + case JS_GlobalDataType::STRING: + m_pGlobalData->SetGlobalVariableString(name, pData->sData); + m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); + break; + case JS_GlobalDataType::OBJECT: { + CJS_GlobalVariableArray array; + v8::Local obj = v8::Local::New( + GetJSObject()->GetIsolate(), pData->pData); + ObjectToArray(pRuntime, obj, array); + m_pGlobalData->SetGlobalVariableObject(name, array); + m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); + } break; + case JS_GlobalDataType::NULLOBJ: + m_pGlobalData->SetGlobalVariableNull(name); + m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); + break; + } + } +} + +void JSGlobalAlternate::ObjectToArray(CJS_Runtime* pRuntime, + v8::Local pObj, + CJS_GlobalVariableArray& array) { + std::vector pKeyList = pRuntime->GetObjectPropertyNames(pObj); + for (const auto& ws : pKeyList) { + ByteString sKey = ws.UTF8Encode(); + v8::Local v = pRuntime->GetObjectProperty(pObj, ws); + if (v->IsNumber()) { + CJS_KeyValue* pObjElement = new CJS_KeyValue; + pObjElement->nType = JS_GlobalDataType::NUMBER; + pObjElement->sKey = sKey; + pObjElement->dData = pRuntime->ToDouble(v); + array.Add(pObjElement); + continue; + } + if (v->IsBoolean()) { + CJS_KeyValue* pObjElement = new CJS_KeyValue; + pObjElement->nType = JS_GlobalDataType::BOOLEAN; + pObjElement->sKey = sKey; + pObjElement->dData = pRuntime->ToBoolean(v); + array.Add(pObjElement); + continue; + } + if (v->IsString()) { + ByteString sValue = ByteString::FromUnicode(pRuntime->ToWideString(v)); + CJS_KeyValue* pObjElement = new CJS_KeyValue; + pObjElement->nType = JS_GlobalDataType::STRING; + pObjElement->sKey = sKey; + pObjElement->sData = sValue; + array.Add(pObjElement); + continue; + } + if (v->IsObject()) { + CJS_KeyValue* pObjElement = new CJS_KeyValue; + pObjElement->nType = JS_GlobalDataType::OBJECT; + pObjElement->sKey = sKey; + ObjectToArray(pRuntime, pRuntime->ToObject(v), pObjElement->objData); + array.Add(pObjElement); + continue; + } + if (v->IsNull()) { + CJS_KeyValue* pObjElement = new CJS_KeyValue; + pObjElement->nType = JS_GlobalDataType::NULLOBJ; + pObjElement->sKey = sKey; + array.Add(pObjElement); + } + } +} + +void JSGlobalAlternate::PutObjectProperty(v8::Local pObj, + CJS_KeyValue* pData) { + CJS_Runtime* pRuntime = CJS_Runtime::CurrentRuntimeFromIsolate( + m_pJSObject->ToV8Object()->GetIsolate()); + + for (int i = 0, sz = pData->objData.Count(); i < sz; i++) { + CJS_KeyValue* pObjData = pData->objData.GetAt(i); + switch (pObjData->nType) { + case JS_GlobalDataType::NUMBER: + pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), + pRuntime->NewNumber(pObjData->dData)); + break; + case JS_GlobalDataType::BOOLEAN: + pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), + pRuntime->NewBoolean(pObjData->bData == 1)); + break; + case JS_GlobalDataType::STRING: + pRuntime->PutObjectProperty( + pObj, pObjData->sKey.UTF8Decode(), + pRuntime->NewString(pObjData->sData.UTF8Decode().AsStringView())); + break; + case JS_GlobalDataType::OBJECT: { + v8::Local pNewObj = pRuntime->NewFxDynamicObj(-1); + if (!pNewObj.IsEmpty()) { + PutObjectProperty(pNewObj, pObjData); + pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), + pNewObj); + } + } break; + case JS_GlobalDataType::NULLOBJ: + pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), + pRuntime->NewNull()); + break; + } + } +} + +void JSGlobalAlternate::DestroyGlobalPersisitentVariables() { + m_MapGlobal.clear(); +} + +CJS_Return JSGlobalAlternate::SetGlobalVariables(const ByteString& propname, + JS_GlobalDataType nType, + double dData, + bool bData, + const ByteString& sData, + v8::Local pData, + bool bDefaultPersistent) { + if (propname.IsEmpty()) + return CJS_Return(false); + + auto it = m_MapGlobal.find(propname); + if (it != m_MapGlobal.end()) { + JSGlobalData* pTemp = it->second.get(); + if (pTemp->bDeleted || pTemp->nType != nType) { + pTemp->dData = 0; + pTemp->bData = 0; + pTemp->sData = ""; + pTemp->nType = nType; + } + pTemp->bDeleted = false; + switch (nType) { + case JS_GlobalDataType::NUMBER: + pTemp->dData = dData; + break; + case JS_GlobalDataType::BOOLEAN: + pTemp->bData = bData; + break; + case JS_GlobalDataType::STRING: + pTemp->sData = sData; + break; + case JS_GlobalDataType::OBJECT: + pTemp->pData.Reset(pData->GetIsolate(), pData); + break; + case JS_GlobalDataType::NULLOBJ: + break; + default: + return CJS_Return(false); + } + return CJS_Return(true); + } + + auto pNewData = pdfium::MakeUnique(); + switch (nType) { + case JS_GlobalDataType::NUMBER: + pNewData->nType = JS_GlobalDataType::NUMBER; + pNewData->dData = dData; + pNewData->bPersistent = bDefaultPersistent; + break; + case JS_GlobalDataType::BOOLEAN: + pNewData->nType = JS_GlobalDataType::BOOLEAN; + pNewData->bData = bData; + pNewData->bPersistent = bDefaultPersistent; + break; + case JS_GlobalDataType::STRING: + pNewData->nType = JS_GlobalDataType::STRING; + pNewData->sData = sData; + pNewData->bPersistent = bDefaultPersistent; + break; + case JS_GlobalDataType::OBJECT: + pNewData->nType = JS_GlobalDataType::OBJECT; + pNewData->pData.Reset(pData->GetIsolate(), pData); + pNewData->bPersistent = bDefaultPersistent; + break; + case JS_GlobalDataType::NULLOBJ: + pNewData->nType = JS_GlobalDataType::NULLOBJ; + pNewData->bPersistent = bDefaultPersistent; + break; + default: + return CJS_Return(false); + } + m_MapGlobal[propname] = std::move(pNewData); + return CJS_Return(true); +} diff --git a/fpdfsdk/javascript/cjs_global.h b/fpdfsdk/javascript/cjs_global.h new file mode 100644 index 0000000000..a98bf97f78 --- /dev/null +++ b/fpdfsdk/javascript/cjs_global.h @@ -0,0 +1,39 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef FPDFSDK_JAVASCRIPT_CJS_GLOBAL_H_ +#define FPDFSDK_JAVASCRIPT_CJS_GLOBAL_H_ + +#include "fpdfsdk/javascript/JS_Define.h" + +class CJS_Global : public CJS_Object { + public: + explicit CJS_Global(v8::Local pObject) : CJS_Object(pObject) {} + ~CJS_Global() override {} + + // CJS_Object + void InitInstance(IJS_Runtime* pIRuntime) override; + + DECLARE_JS_CLASS_BASE_PART(); + DECLARE_JS_CLASS_CONST_PART(); + DECLARE_JS_CLASS_PART(); + static void queryprop_static( + v8::Local property, + const v8::PropertyCallbackInfo& info); + static void getprop_static(v8::Local property, + const v8::PropertyCallbackInfo& info); + static void putprop_static(v8::Local property, + v8::Local value, + const v8::PropertyCallbackInfo& info); + static void delprop_static(v8::Local property, + const v8::PropertyCallbackInfo& info); + static void DefineAllProperties(CFXJS_Engine* pEngine); + + static void setPersistent_static( + const v8::FunctionCallbackInfo& info); +}; + +#endif // FPDFSDK_JAVASCRIPT_CJS_GLOBAL_H_ diff --git a/fpdfsdk/javascript/cjs_runtime.cpp b/fpdfsdk/javascript/cjs_runtime.cpp index 05bdb7c235..e48d696866 100644 --- a/fpdfsdk/javascript/cjs_runtime.cpp +++ b/fpdfsdk/javascript/cjs_runtime.cpp @@ -24,6 +24,7 @@ #include "fpdfsdk/javascript/cjs_display.h" #include "fpdfsdk/javascript/cjs_event_context.h" #include "fpdfsdk/javascript/cjs_font.h" +#include "fpdfsdk/javascript/cjs_global.h" #include "fpdfsdk/javascript/cjs_globalarrays.h" #include "fpdfsdk/javascript/cjs_globalconsts.h" #include "fpdfsdk/javascript/cjs_highlight.h" @@ -35,7 +36,6 @@ #include "fpdfsdk/javascript/color.h" #include "fpdfsdk/javascript/console.h" #include "fpdfsdk/javascript/event.h" -#include "fpdfsdk/javascript/global.h" #include "fpdfsdk/javascript/report.h" #include "fpdfsdk/javascript/util.h" #include "public/fpdf_formfill.h" diff --git a/fpdfsdk/javascript/global.cpp b/fpdfsdk/javascript/global.cpp deleted file mode 100644 index df7927b116..0000000000 --- a/fpdfsdk/javascript/global.cpp +++ /dev/null @@ -1,614 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "fpdfsdk/javascript/global.h" - -#include -#include -#include -#include - -#include "core/fxcrt/fx_extension.h" -#include "fpdfsdk/javascript/JS_Define.h" -#include "fpdfsdk/javascript/JS_EventHandler.h" -#include "fpdfsdk/javascript/JS_GlobalData.h" -#include "fpdfsdk/javascript/JS_KeyValue.h" -#include "fpdfsdk/javascript/JS_Object.h" -#include "fpdfsdk/javascript/JS_Value.h" -#include "fpdfsdk/javascript/cjs_event_context.h" -#include "fpdfsdk/javascript/resource.h" - -#define IMPLEMENT_SPECIAL_JS_CLASS(js_class_name, class_alternate, class_name) \ - IMPLEMENT_JS_CLASS_BASE_PART(js_class_name, class_name) \ - IMPLEMENT_JS_CLASS_CONST_PART(js_class_name, class_name) \ - IMPLEMENT_JS_CLASS_PART(js_class_name, class_alternate, class_name) \ - void js_class_name::queryprop_static( \ - v8::Local property, \ - const v8::PropertyCallbackInfo& info) { \ - JSSpecialPropQuery(#class_name, property, info); \ - } \ - void js_class_name::getprop_static( \ - v8::Local property, \ - const v8::PropertyCallbackInfo& info) { \ - JSSpecialPropGet(#class_name, property, info); \ - } \ - void js_class_name::putprop_static( \ - v8::Local property, v8::Local value, \ - const v8::PropertyCallbackInfo& info) { \ - JSSpecialPropPut(#class_name, property, value, info); \ - } \ - void js_class_name::delprop_static( \ - v8::Local property, \ - const v8::PropertyCallbackInfo& info) { \ - JSSpecialPropDel(#class_name, property, info); \ - } \ - void js_class_name::DefineAllProperties(CFXJS_Engine* pEngine) { \ - pEngine->DefineObjAllProperties( \ - g_nObjDefnID, js_class_name::queryprop_static, \ - js_class_name::getprop_static, js_class_name::putprop_static, \ - js_class_name::delprop_static); \ - } \ - void js_class_name::DefineJSObjects(CFXJS_Engine* pEngine, \ - FXJSOBJTYPE eObjType) { \ - g_nObjDefnID = pEngine->DefineObj(js_class_name::g_pClassName, eObjType, \ - JSConstructor, JSDestructor); \ - DefineConsts(pEngine); \ - DefineProps(pEngine); \ - DefineMethods(pEngine); \ - DefineAllProperties(pEngine); \ - } - -namespace { - -WideString PropFromV8Prop(v8::Local property) { - v8::String::Utf8Value utf8_value(property); - return WideString::FromUTF8(ByteStringView(*utf8_value, utf8_value.length())); -} - -template -void JSSpecialPropQuery(const char*, - v8::Local property, - const v8::PropertyCallbackInfo& info) { - CJS_Runtime* pRuntime = - CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); - if (!pRuntime) - return; - - CJS_Object* pJSObj = - static_cast(pRuntime->GetObjectPrivate(info.Holder())); - if (!pJSObj) - return; - - Alt* pObj = reinterpret_cast(pJSObj->GetEmbedObject()); - CJS_Return result = pObj->QueryProperty(PropFromV8Prop(property).c_str()); - info.GetReturnValue().Set(!result.HasError() ? 4 : 0); -} - -template -void JSSpecialPropGet(const char* class_name, - v8::Local property, - const v8::PropertyCallbackInfo& info) { - CJS_Runtime* pRuntime = - CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); - if (!pRuntime) - return; - - CJS_Object* pJSObj = - static_cast(pRuntime->GetObjectPrivate(info.Holder())); - if (!pJSObj) - return; - - Alt* pObj = reinterpret_cast(pJSObj->GetEmbedObject()); - CJS_Return result = - pObj->GetProperty(pRuntime, PropFromV8Prop(property).c_str()); - if (result.HasError()) { - pRuntime->Error( - JSFormatErrorString(class_name, "GetProperty", result.Error())); - return; - } - - if (result.HasReturn()) - info.GetReturnValue().Set(result.Return()); -} - -template -void JSSpecialPropPut(const char* class_name, - v8::Local property, - v8::Local value, - const v8::PropertyCallbackInfo& info) { - CJS_Runtime* pRuntime = - CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); - if (!pRuntime) - return; - - CJS_Object* pJSObj = - static_cast(pRuntime->GetObjectPrivate(info.Holder())); - if (!pJSObj) - return; - - Alt* pObj = reinterpret_cast(pJSObj->GetEmbedObject()); - CJS_Return result = - pObj->SetProperty(pRuntime, PropFromV8Prop(property).c_str(), value); - if (result.HasError()) { - pRuntime->Error( - JSFormatErrorString(class_name, "PutProperty", result.Error())); - } -} - -template -void JSSpecialPropDel(const char* class_name, - v8::Local property, - const v8::PropertyCallbackInfo& info) { - CJS_Runtime* pRuntime = - CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); - if (!pRuntime) - return; - - CJS_Object* pJSObj = - static_cast(pRuntime->GetObjectPrivate(info.Holder())); - if (!pJSObj) - return; - - Alt* pObj = reinterpret_cast(pJSObj->GetEmbedObject()); - CJS_Return result = - pObj->DelProperty(pRuntime, PropFromV8Prop(property).c_str()); - if (result.HasError()) { - // TODO(dsinclair): Should this set the pRuntime->Error result? - // ByteString cbName; - // cbName.Format("%s.%s", class_name, "DelProperty"); - } -} - -struct JSGlobalData { - JSGlobalData(); - ~JSGlobalData(); - - JS_GlobalDataType nType; - double dData; - bool bData; - ByteString sData; - v8::Global pData; - bool bPersistent; - bool bDeleted; -}; - -class JSGlobalAlternate : public CJS_EmbedObj { - public: - explicit JSGlobalAlternate(CJS_Object* pJSObject); - ~JSGlobalAlternate() override; - - CJS_Return setPersistent(CJS_Runtime* pRuntime, - const std::vector>& params); - CJS_Return QueryProperty(const wchar_t* propname); - CJS_Return GetProperty(CJS_Runtime* pRuntime, const wchar_t* propname); - CJS_Return SetProperty(CJS_Runtime* pRuntime, - const wchar_t* propname, - v8::Local vp); - CJS_Return DelProperty(CJS_Runtime* pRuntime, const wchar_t* propname); - void Initial(CPDFSDK_FormFillEnvironment* pFormFillEnv); - - private: - void UpdateGlobalPersistentVariables(); - void CommitGlobalPersisitentVariables(CJS_Runtime* pRuntime); - void DestroyGlobalPersisitentVariables(); - CJS_Return SetGlobalVariables(const ByteString& propname, - JS_GlobalDataType nType, - double dData, - bool bData, - const ByteString& sData, - v8::Local pData, - bool bDefaultPersistent); - void ObjectToArray(CJS_Runtime* pRuntime, - v8::Local pObj, - CJS_GlobalVariableArray& array); - void PutObjectProperty(v8::Local obj, CJS_KeyValue* pData); - - std::map> m_MapGlobal; - WideString m_sFilePath; - CJS_GlobalData* m_pGlobalData; - CPDFSDK_FormFillEnvironment::ObservedPtr m_pFormFillEnv; -}; - -} // namespace - -JSConstSpec CJS_Global::ConstSpecs[] = {{0, JSConstSpec::Number, 0, 0}}; - -JSPropertySpec CJS_Global::PropertySpecs[] = {{0, 0, 0}}; - -JSMethodSpec CJS_Global::MethodSpecs[] = { - {"setPersistent", setPersistent_static}, - {0, 0}}; - -IMPLEMENT_SPECIAL_JS_CLASS(CJS_Global, JSGlobalAlternate, global); - -// static -void CJS_Global::setPersistent_static( - const v8::FunctionCallbackInfo& info) { - JSMethod( - "setPersistent", "global", info); -} - -void CJS_Global::InitInstance(IJS_Runtime* pIRuntime) { - CJS_Runtime* pRuntime = static_cast(pIRuntime); - JSGlobalAlternate* pGlobal = - static_cast(GetEmbedObject()); - pGlobal->Initial(pRuntime->GetFormFillEnv()); -} - -JSGlobalData::JSGlobalData() - : nType(JS_GlobalDataType::NUMBER), - dData(0), - bData(false), - sData(""), - bPersistent(false), - bDeleted(false) {} - -JSGlobalData::~JSGlobalData() { - pData.Reset(); -} - -JSGlobalAlternate::JSGlobalAlternate(CJS_Object* pJSObject) - : CJS_EmbedObj(pJSObject), m_pFormFillEnv(nullptr) {} - -JSGlobalAlternate::~JSGlobalAlternate() { - DestroyGlobalPersisitentVariables(); - m_pGlobalData->Release(); -} - -void JSGlobalAlternate::Initial(CPDFSDK_FormFillEnvironment* pFormFillEnv) { - m_pFormFillEnv.Reset(pFormFillEnv); - m_pGlobalData = CJS_GlobalData::GetRetainedInstance(pFormFillEnv); - UpdateGlobalPersistentVariables(); -} - -CJS_Return JSGlobalAlternate::QueryProperty(const wchar_t* propname) { - return CJS_Return(WideString(propname) != L"setPersistent"); -} - -CJS_Return JSGlobalAlternate::DelProperty(CJS_Runtime* pRuntime, - const wchar_t* propname) { - auto it = m_MapGlobal.find(ByteString::FromUnicode(propname)); - if (it == m_MapGlobal.end()) - return CJS_Return(false); - - it->second->bDeleted = true; - return CJS_Return(true); -} - -CJS_Return JSGlobalAlternate::GetProperty(CJS_Runtime* pRuntime, - const wchar_t* propname) { - auto it = m_MapGlobal.find(ByteString::FromUnicode(propname)); - if (it == m_MapGlobal.end()) - return CJS_Return(true); - - JSGlobalData* pData = it->second.get(); - if (pData->bDeleted) - return CJS_Return(true); - - switch (pData->nType) { - case JS_GlobalDataType::NUMBER: - return CJS_Return(pRuntime->NewNumber(pData->dData)); - case JS_GlobalDataType::BOOLEAN: - return CJS_Return(pRuntime->NewBoolean(pData->bData)); - case JS_GlobalDataType::STRING: - return CJS_Return(pRuntime->NewString( - WideString::FromLocal(pData->sData.c_str()).c_str())); - case JS_GlobalDataType::OBJECT: - return CJS_Return( - v8::Local::New(pRuntime->GetIsolate(), pData->pData)); - case JS_GlobalDataType::NULLOBJ: - return CJS_Return(pRuntime->NewNull()); - default: - break; - } - return CJS_Return(false); -} - -CJS_Return JSGlobalAlternate::SetProperty(CJS_Runtime* pRuntime, - const wchar_t* propname, - v8::Local vp) { - ByteString sPropName = ByteString::FromUnicode(propname); - if (vp->IsNumber()) { - return SetGlobalVariables(sPropName, JS_GlobalDataType::NUMBER, - pRuntime->ToDouble(vp), false, "", - v8::Local(), false); - } - if (vp->IsBoolean()) { - return SetGlobalVariables(sPropName, JS_GlobalDataType::BOOLEAN, 0, - pRuntime->ToBoolean(vp), "", - v8::Local(), false); - } - if (vp->IsString()) { - return SetGlobalVariables( - sPropName, JS_GlobalDataType::STRING, 0, false, - ByteString::FromUnicode(pRuntime->ToWideString(vp)), - v8::Local(), false); - } - if (vp->IsObject()) { - return SetGlobalVariables(sPropName, JS_GlobalDataType::OBJECT, 0, false, - "", pRuntime->ToObject(vp), false); - } - if (vp->IsNull()) { - return SetGlobalVariables(sPropName, JS_GlobalDataType::NULLOBJ, 0, false, - "", v8::Local(), false); - } - if (vp->IsUndefined()) { - DelProperty(pRuntime, propname); - return CJS_Return(true); - } - return CJS_Return(false); -} - -CJS_Return JSGlobalAlternate::setPersistent( - CJS_Runtime* pRuntime, - const std::vector>& params) { - if (params.size() != 2) - return CJS_Return(JSGetStringFromID(IDS_STRING_JSPARAMERROR)); - - auto it = m_MapGlobal.find( - ByteString::FromUnicode(pRuntime->ToWideString(params[0]))); - if (it == m_MapGlobal.end() || it->second->bDeleted) - return CJS_Return(JSGetStringFromID(IDS_STRING_JSNOGLOBAL)); - - it->second->bPersistent = pRuntime->ToBoolean(params[1]); - return CJS_Return(true); -} - -void JSGlobalAlternate::UpdateGlobalPersistentVariables() { - CJS_Runtime* pRuntime = - static_cast(CFXJS_Engine::CurrentEngineFromIsolate( - m_pJSObject->ToV8Object()->GetIsolate())); - - for (int i = 0, sz = m_pGlobalData->GetSize(); i < sz; i++) { - CJS_GlobalData_Element* pData = m_pGlobalData->GetAt(i); - switch (pData->data.nType) { - case JS_GlobalDataType::NUMBER: - SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::NUMBER, - pData->data.dData, false, "", - v8::Local(), pData->bPersistent == 1); - pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(), - pData->data.sKey.UTF8Decode(), - pRuntime->NewNumber(pData->data.dData)); - break; - case JS_GlobalDataType::BOOLEAN: - SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::BOOLEAN, 0, - pData->data.bData == 1, "", v8::Local(), - pData->bPersistent == 1); - pRuntime->PutObjectProperty( - m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(), - pRuntime->NewBoolean(pData->data.bData == 1)); - break; - case JS_GlobalDataType::STRING: - SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::STRING, 0, - false, pData->data.sData, v8::Local(), - pData->bPersistent == 1); - pRuntime->PutObjectProperty( - m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(), - pRuntime->NewString(pData->data.sData.UTF8Decode().AsStringView())); - break; - case JS_GlobalDataType::OBJECT: { - v8::Local pObj = pRuntime->NewFxDynamicObj(-1); - if (!pObj.IsEmpty()) { - PutObjectProperty(pObj, &pData->data); - SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::OBJECT, 0, - false, "", pObj, pData->bPersistent == 1); - pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(), - pData->data.sKey.UTF8Decode(), pObj); - } - } break; - case JS_GlobalDataType::NULLOBJ: - SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::NULLOBJ, 0, - false, "", v8::Local(), - pData->bPersistent == 1); - pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(), - pData->data.sKey.UTF8Decode(), - pRuntime->NewNull()); - break; - } - } -} - -void JSGlobalAlternate::CommitGlobalPersisitentVariables( - CJS_Runtime* pRuntime) { - for (const auto& iter : m_MapGlobal) { - ByteString name = iter.first; - JSGlobalData* pData = iter.second.get(); - if (pData->bDeleted) { - m_pGlobalData->DeleteGlobalVariable(name); - continue; - } - switch (pData->nType) { - case JS_GlobalDataType::NUMBER: - m_pGlobalData->SetGlobalVariableNumber(name, pData->dData); - m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); - break; - case JS_GlobalDataType::BOOLEAN: - m_pGlobalData->SetGlobalVariableBoolean(name, pData->bData); - m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); - break; - case JS_GlobalDataType::STRING: - m_pGlobalData->SetGlobalVariableString(name, pData->sData); - m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); - break; - case JS_GlobalDataType::OBJECT: { - CJS_GlobalVariableArray array; - v8::Local obj = v8::Local::New( - GetJSObject()->GetIsolate(), pData->pData); - ObjectToArray(pRuntime, obj, array); - m_pGlobalData->SetGlobalVariableObject(name, array); - m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); - } break; - case JS_GlobalDataType::NULLOBJ: - m_pGlobalData->SetGlobalVariableNull(name); - m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); - break; - } - } -} - -void JSGlobalAlternate::ObjectToArray(CJS_Runtime* pRuntime, - v8::Local pObj, - CJS_GlobalVariableArray& array) { - std::vector pKeyList = pRuntime->GetObjectPropertyNames(pObj); - for (const auto& ws : pKeyList) { - ByteString sKey = ws.UTF8Encode(); - v8::Local v = pRuntime->GetObjectProperty(pObj, ws); - if (v->IsNumber()) { - CJS_KeyValue* pObjElement = new CJS_KeyValue; - pObjElement->nType = JS_GlobalDataType::NUMBER; - pObjElement->sKey = sKey; - pObjElement->dData = pRuntime->ToDouble(v); - array.Add(pObjElement); - continue; - } - if (v->IsBoolean()) { - CJS_KeyValue* pObjElement = new CJS_KeyValue; - pObjElement->nType = JS_GlobalDataType::BOOLEAN; - pObjElement->sKey = sKey; - pObjElement->dData = pRuntime->ToBoolean(v); - array.Add(pObjElement); - continue; - } - if (v->IsString()) { - ByteString sValue = ByteString::FromUnicode(pRuntime->ToWideString(v)); - CJS_KeyValue* pObjElement = new CJS_KeyValue; - pObjElement->nType = JS_GlobalDataType::STRING; - pObjElement->sKey = sKey; - pObjElement->sData = sValue; - array.Add(pObjElement); - continue; - } - if (v->IsObject()) { - CJS_KeyValue* pObjElement = new CJS_KeyValue; - pObjElement->nType = JS_GlobalDataType::OBJECT; - pObjElement->sKey = sKey; - ObjectToArray(pRuntime, pRuntime->ToObject(v), pObjElement->objData); - array.Add(pObjElement); - continue; - } - if (v->IsNull()) { - CJS_KeyValue* pObjElement = new CJS_KeyValue; - pObjElement->nType = JS_GlobalDataType::NULLOBJ; - pObjElement->sKey = sKey; - array.Add(pObjElement); - } - } -} - -void JSGlobalAlternate::PutObjectProperty(v8::Local pObj, - CJS_KeyValue* pData) { - CJS_Runtime* pRuntime = CJS_Runtime::CurrentRuntimeFromIsolate( - m_pJSObject->ToV8Object()->GetIsolate()); - - for (int i = 0, sz = pData->objData.Count(); i < sz; i++) { - CJS_KeyValue* pObjData = pData->objData.GetAt(i); - switch (pObjData->nType) { - case JS_GlobalDataType::NUMBER: - pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), - pRuntime->NewNumber(pObjData->dData)); - break; - case JS_GlobalDataType::BOOLEAN: - pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), - pRuntime->NewBoolean(pObjData->bData == 1)); - break; - case JS_GlobalDataType::STRING: - pRuntime->PutObjectProperty( - pObj, pObjData->sKey.UTF8Decode(), - pRuntime->NewString(pObjData->sData.UTF8Decode().AsStringView())); - break; - case JS_GlobalDataType::OBJECT: { - v8::Local pNewObj = pRuntime->NewFxDynamicObj(-1); - if (!pNewObj.IsEmpty()) { - PutObjectProperty(pNewObj, pObjData); - pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), - pNewObj); - } - } break; - case JS_GlobalDataType::NULLOBJ: - pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), - pRuntime->NewNull()); - break; - } - } -} - -void JSGlobalAlternate::DestroyGlobalPersisitentVariables() { - m_MapGlobal.clear(); -} - -CJS_Return JSGlobalAlternate::SetGlobalVariables(const ByteString& propname, - JS_GlobalDataType nType, - double dData, - bool bData, - const ByteString& sData, - v8::Local pData, - bool bDefaultPersistent) { - if (propname.IsEmpty()) - return CJS_Return(false); - - auto it = m_MapGlobal.find(propname); - if (it != m_MapGlobal.end()) { - JSGlobalData* pTemp = it->second.get(); - if (pTemp->bDeleted || pTemp->nType != nType) { - pTemp->dData = 0; - pTemp->bData = 0; - pTemp->sData = ""; - pTemp->nType = nType; - } - pTemp->bDeleted = false; - switch (nType) { - case JS_GlobalDataType::NUMBER: - pTemp->dData = dData; - break; - case JS_GlobalDataType::BOOLEAN: - pTemp->bData = bData; - break; - case JS_GlobalDataType::STRING: - pTemp->sData = sData; - break; - case JS_GlobalDataType::OBJECT: - pTemp->pData.Reset(pData->GetIsolate(), pData); - break; - case JS_GlobalDataType::NULLOBJ: - break; - default: - return CJS_Return(false); - } - return CJS_Return(true); - } - - auto pNewData = pdfium::MakeUnique(); - switch (nType) { - case JS_GlobalDataType::NUMBER: - pNewData->nType = JS_GlobalDataType::NUMBER; - pNewData->dData = dData; - pNewData->bPersistent = bDefaultPersistent; - break; - case JS_GlobalDataType::BOOLEAN: - pNewData->nType = JS_GlobalDataType::BOOLEAN; - pNewData->bData = bData; - pNewData->bPersistent = bDefaultPersistent; - break; - case JS_GlobalDataType::STRING: - pNewData->nType = JS_GlobalDataType::STRING; - pNewData->sData = sData; - pNewData->bPersistent = bDefaultPersistent; - break; - case JS_GlobalDataType::OBJECT: - pNewData->nType = JS_GlobalDataType::OBJECT; - pNewData->pData.Reset(pData->GetIsolate(), pData); - pNewData->bPersistent = bDefaultPersistent; - break; - case JS_GlobalDataType::NULLOBJ: - pNewData->nType = JS_GlobalDataType::NULLOBJ; - pNewData->bPersistent = bDefaultPersistent; - break; - default: - return CJS_Return(false); - } - m_MapGlobal[propname] = std::move(pNewData); - return CJS_Return(true); -} diff --git a/fpdfsdk/javascript/global.h b/fpdfsdk/javascript/global.h deleted file mode 100644 index 77794a2e34..0000000000 --- a/fpdfsdk/javascript/global.h +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef FPDFSDK_JAVASCRIPT_GLOBAL_H_ -#define FPDFSDK_JAVASCRIPT_GLOBAL_H_ - -#include "fpdfsdk/javascript/JS_Define.h" - -class CJS_Global : public CJS_Object { - public: - explicit CJS_Global(v8::Local pObject) : CJS_Object(pObject) {} - ~CJS_Global() override {} - - // CJS_Object - void InitInstance(IJS_Runtime* pIRuntime) override; - - DECLARE_JS_CLASS_BASE_PART(); - DECLARE_JS_CLASS_CONST_PART(); - DECLARE_JS_CLASS_PART(); - static void queryprop_static( - v8::Local property, - const v8::PropertyCallbackInfo& info); - static void getprop_static(v8::Local property, - const v8::PropertyCallbackInfo& info); - static void putprop_static(v8::Local property, - v8::Local value, - const v8::PropertyCallbackInfo& info); - static void delprop_static(v8::Local property, - const v8::PropertyCallbackInfo& info); - static void DefineAllProperties(CFXJS_Engine* pEngine); - - static void setPersistent_static( - const v8::FunctionCallbackInfo& info); -}; - -#endif // FPDFSDK_JAVASCRIPT_GLOBAL_H_ -- cgit v1.2.3