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 #ifndef FXJS_XFA_CFXJSE_ENGINE_H_ 8 #define FXJS_XFA_CFXJSE_ENGINE_H_ 9 10 #include <map> 11 #include <memory> 12 #include <vector> 13 14 #include "core/fxcrt/unowned_ptr.h" 15 #include "fxjs/cfx_v8.h" 16 #include "v8/include/v8.h" 17 #include "xfa/fxfa/cxfa_eventparam.h" 18 #include "xfa/fxfa/parser/cxfa_document.h" 19 #include "xfa/fxfa/parser/cxfa_script.h" 20 #include "xfa/fxfa/parser/xfa_resolvenode_rs.h" 21 22 class CFXJSE_Class; 23 class CFXJSE_Context; 24 class CFXJSE_FormCalcContext; 25 class CFXJSE_ResolveProcessor; 26 class CJS_Runtime; 27 class CXFA_List; 28 29 // Flags for |dwStyles| argument to CFXJSE_Engine::ResolveObjects(). 30 #define XFA_RESOLVENODE_Children 0x0001 31 #define XFA_RESOLVENODE_TagName 0x0002 32 #define XFA_RESOLVENODE_Attributes 0x0004 33 #define XFA_RESOLVENODE_Properties 0x0008 34 #define XFA_RESOLVENODE_Siblings 0x0020 35 #define XFA_RESOLVENODE_Parent 0x0040 36 #define XFA_RESOLVENODE_AnyChild 0x0080 37 #define XFA_RESOLVENODE_ALL 0x0100 38 #define XFA_RESOLVENODE_CreateNode 0x0400 39 #define XFA_RESOLVENODE_Bind 0x0800 40 #define XFA_RESOLVENODE_BindNew 0x1000 41 42 class CFXJSE_Engine final : public CFX_V8 { 43 public: 44 static CXFA_Object* ToObject(const v8::FunctionCallbackInfo<v8::Value>& info); 45 static CXFA_Object* ToObject(CFXJSE_Value* pValue); 46 static void GlobalPropertyGetter(CFXJSE_Value* pObject, 47 ByteStringView szPropName, 48 CFXJSE_Value* pValue); 49 static void GlobalPropertySetter(CFXJSE_Value* pObject, 50 ByteStringView szPropName, 51 CFXJSE_Value* pValue); 52 static void NormalPropertyGetter(CFXJSE_Value* pObject, 53 ByteStringView szPropName, 54 CFXJSE_Value* pValue); 55 static void NormalPropertySetter(CFXJSE_Value* pObject, 56 ByteStringView szPropName, 57 CFXJSE_Value* pValue); 58 static CJS_Result NormalMethodCall( 59 const v8::FunctionCallbackInfo<v8::Value>& info, 60 const WideString& functionName); 61 static int32_t NormalPropTypeGetter(CFXJSE_Value* pObject, 62 ByteStringView szPropName, 63 bool bQueryIn); 64 static int32_t GlobalPropTypeGetter(CFXJSE_Value* pObject, 65 ByteStringView szPropName, 66 bool bQueryIn); 67 68 CFXJSE_Engine(CXFA_Document* pDocument, CJS_Runtime* fxjs_runtime); 69 ~CFXJSE_Engine() override; 70 SetEventParam(CXFA_EventParam * param)71 void SetEventParam(CXFA_EventParam* param) { m_eventParam = param; } GetEventParam()72 CXFA_EventParam* GetEventParam() const { return m_eventParam.Get(); } 73 bool RunScript(CXFA_Script::Type eScriptType, 74 WideStringView wsScript, 75 CFXJSE_Value* pRetValue, 76 CXFA_Object* pThisObject); 77 78 bool ResolveObjects(CXFA_Object* refObject, 79 WideStringView wsExpression, 80 XFA_RESOLVENODE_RS* resolveNodeRS, 81 uint32_t dwStyles, 82 CXFA_Node* bindNode); 83 84 CFXJSE_Value* GetOrCreateJSBindingFromMap(CXFA_Object* pObject); 85 void RemoveJSBindingFromMap(CXFA_Object* pObject); 86 87 void AddToCacheList(std::unique_ptr<CXFA_List> pList); GetThisObject()88 CXFA_Object* GetThisObject() const { return m_pThisObject.Get(); } 89 90 void SetNodesOfRunScript(std::vector<CXFA_Node*>* pArray); 91 void AddNodesOfRunScript(CXFA_Node* pNode); GetJseNormalClass()92 CFXJSE_Class* GetJseNormalClass() const { return m_pJsClass.Get(); } 93 SetRunAtType(XFA_AttributeValue eRunAt)94 void SetRunAtType(XFA_AttributeValue eRunAt) { m_eRunAtType = eRunAt; } IsRunAtClient()95 bool IsRunAtClient() { return m_eRunAtType != XFA_AttributeValue::Server; } 96 97 CXFA_Script::Type GetType(); GetUpObjectArray()98 std::vector<CXFA_Node*>* GetUpObjectArray() { return &m_upObjectArray; } GetDocument()99 CXFA_Document* GetDocument() const { return m_pDocument.Get(); } 100 101 CXFA_Object* ToXFAObject(v8::Local<v8::Value> obj); 102 v8::Local<v8::Value> NewXFAObject(CXFA_Object* obj, 103 v8::Global<v8::FunctionTemplate>& tmpl); 104 105 private: 106 CFXJSE_Context* CreateVariablesContext(CXFA_Node* pScriptNode, 107 CXFA_Node* pSubform); 108 void RemoveBuiltInObjs(CFXJSE_Context* pContext) const; 109 bool QueryNodeByFlag(CXFA_Node* refNode, 110 WideStringView propname, 111 CFXJSE_Value* pValue, 112 uint32_t dwFlag, 113 bool bSetting); 114 bool IsStrictScopeInJavaScript(); 115 CXFA_Object* GetVariablesThis(CXFA_Object* pObject, bool bScriptNode); 116 bool QueryVariableValue(CXFA_Node* pScriptNode, 117 ByteStringView szPropName, 118 CFXJSE_Value* pValue, 119 bool bGetter); 120 bool RunVariablesScript(CXFA_Node* pScriptNode); 121 122 UnownedPtr<CJS_Runtime> const m_pSubordinateRuntime; 123 UnownedPtr<CXFA_Document> const m_pDocument; 124 std::unique_ptr<CFXJSE_Context> m_JsContext; 125 UnownedPtr<CFXJSE_Class> m_pJsClass; 126 CXFA_Script::Type m_eScriptType = CXFA_Script::Type::Unknown; 127 std::map<CXFA_Object*, std::unique_ptr<CFXJSE_Value>> m_mapObjectToValue; 128 std::map<CXFA_Object*, std::unique_ptr<CFXJSE_Context>> 129 m_mapVariableToContext; 130 UnownedPtr<CXFA_EventParam> m_eventParam; 131 std::vector<CXFA_Node*> m_upObjectArray; 132 // CacheList holds the List items so we can clean them up when we're done. 133 std::vector<std::unique_ptr<CXFA_List>> m_CacheList; 134 UnownedPtr<std::vector<CXFA_Node*>> m_pScriptNodeArray; 135 std::unique_ptr<CFXJSE_ResolveProcessor> const m_ResolveProcessor; 136 std::unique_ptr<CFXJSE_FormCalcContext> m_FM2JSContext; 137 UnownedPtr<CXFA_Object> m_pThisObject; 138 XFA_AttributeValue m_eRunAtType = XFA_AttributeValue::Client; 139 }; 140 141 #endif // FXJS_XFA_CFXJSE_ENGINE_H_ 142