Home | History | Annotate | Download | only in parser
      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 XFA_FXFA_PARSER_CXFA_SCRIPTCONTEXT_H_
      8 #define XFA_FXFA_PARSER_CXFA_SCRIPTCONTEXT_H_
      9 
     10 #include <map>
     11 #include <memory>
     12 #include <vector>
     13 
     14 #include "fxjs/cfxjse_arguments.h"
     15 #include "xfa/fxfa/cxfa_eventparam.h"
     16 #include "xfa/fxfa/fm2js/xfa_fm2jscontext.h"
     17 #include "xfa/fxfa/parser/cxfa_document.h"
     18 #include "xfa/fxfa/parser/xfa_resolvenode_rs.h"
     19 
     20 #define XFA_RESOLVENODE_TagName 0x0002
     21 
     22 class CXFA_ResolveProcessor;
     23 
     24 class CXFA_ScriptContext {
     25  public:
     26   explicit CXFA_ScriptContext(CXFA_Document* pDocument);
     27   ~CXFA_ScriptContext();
     28 
     29   void Initialize(v8::Isolate* pIsolate);
     30   void SetEventParam(CXFA_EventParam param) { m_eventParam = param; }
     31   CXFA_EventParam* GetEventParam() { return &m_eventParam; }
     32   bool RunScript(XFA_SCRIPTLANGTYPE eScriptType,
     33                  const CFX_WideStringC& wsScript,
     34                  CFXJSE_Value* pRetValue,
     35                  CXFA_Object* pThisObject = nullptr);
     36 
     37   int32_t ResolveObjects(CXFA_Object* refNode,
     38                          const CFX_WideStringC& wsExpression,
     39                          XFA_RESOLVENODE_RS& resolveNodeRS,
     40                          uint32_t dwStyles = XFA_RESOLVENODE_Children,
     41                          CXFA_Node* bindNode = nullptr);
     42   CFXJSE_Value* GetJSValueFromMap(CXFA_Object* pObject);
     43   void AddToCacheList(std::unique_ptr<CXFA_NodeList> pList);
     44   CXFA_Object* GetThisObject() const { return m_pThisObject; }
     45   v8::Isolate* GetRuntime() const { return m_pIsolate; }
     46 
     47   int32_t GetIndexByName(CXFA_Node* refNode);
     48   int32_t GetIndexByClassName(CXFA_Node* refNode);
     49   void GetSomExpression(CXFA_Node* refNode, CFX_WideString& wsExpression);
     50 
     51   void SetNodesOfRunScript(CXFA_NodeArray* pArray);
     52   void AddNodesOfRunScript(const CXFA_NodeArray& nodes);
     53   void AddNodesOfRunScript(CXFA_Node* pNode);
     54   CFXJSE_Class* GetJseNormalClass();
     55 
     56   void SetRunAtType(XFA_ATTRIBUTEENUM eRunAt) { m_eRunAtType = eRunAt; }
     57   bool IsRunAtClient() { return m_eRunAtType != XFA_ATTRIBUTEENUM_Server; }
     58   bool QueryNodeByFlag(CXFA_Node* refNode,
     59                        const CFX_WideStringC& propname,
     60                        CFXJSE_Value* pValue,
     61                        uint32_t dwFlag,
     62                        bool bSetting);
     63   bool QueryVariableValue(CXFA_Node* pScriptNode,
     64                           const CFX_ByteStringC& szPropName,
     65                           CFXJSE_Value* pValue,
     66                           bool bGetter);
     67   bool QueryBuiltinValue(const CFX_ByteStringC& szPropName,
     68                          CFXJSE_Value* pValue);
     69   static void GlobalPropertyGetter(CFXJSE_Value* pObject,
     70                                    const CFX_ByteStringC& szPropName,
     71                                    CFXJSE_Value* pValue);
     72   static void GlobalPropertySetter(CFXJSE_Value* pObject,
     73                                    const CFX_ByteStringC& szPropName,
     74                                    CFXJSE_Value* pValue);
     75   static void NormalPropertyGetter(CFXJSE_Value* pObject,
     76                                    const CFX_ByteStringC& szPropName,
     77                                    CFXJSE_Value* pValue);
     78   static void NormalPropertySetter(CFXJSE_Value* pObject,
     79                                    const CFX_ByteStringC& szPropName,
     80                                    CFXJSE_Value* pValue);
     81   static void NormalMethodCall(CFXJSE_Value* hThis,
     82                                const CFX_ByteStringC& szFuncName,
     83                                CFXJSE_Arguments& args);
     84   static int32_t NormalPropTypeGetter(CFXJSE_Value* pObject,
     85                                       const CFX_ByteStringC& szPropName,
     86                                       bool bQueryIn);
     87   static int32_t GlobalPropTypeGetter(CFXJSE_Value* pObject,
     88                                       const CFX_ByteStringC& szPropName,
     89                                       bool bQueryIn);
     90   bool RunVariablesScript(CXFA_Node* pScriptNode);
     91   CXFA_Object* GetVariablesThis(CXFA_Object* pObject, bool bScriptNode = false);
     92   bool IsStrictScopeInJavaScript();
     93   XFA_SCRIPTLANGTYPE GetType();
     94   CXFA_NodeArray& GetUpObjectArray() { return m_upObjectArray; }
     95   CXFA_Document* GetDocument() const { return m_pDocument; }
     96 
     97   static CXFA_Object* ToObject(CFXJSE_Value* pValue, CFXJSE_Class* pClass);
     98 
     99  private:
    100   void DefineJsContext();
    101   CFXJSE_Context* CreateVariablesContext(CXFA_Node* pScriptNode,
    102                                          CXFA_Node* pSubform);
    103   void DefineJsClass();
    104   void RemoveBuiltInObjs(CFXJSE_Context* pContext) const;
    105 
    106   CXFA_Document* m_pDocument;
    107   std::unique_ptr<CFXJSE_Context> m_JsContext;
    108   v8::Isolate* m_pIsolate;
    109   CFXJSE_Class* m_pJsClass;
    110   XFA_SCRIPTLANGTYPE m_eScriptType;
    111   std::map<CXFA_Object*, std::unique_ptr<CFXJSE_Value>> m_mapObjectToValue;
    112   std::map<CXFA_Object*, CFXJSE_Context*> m_mapVariableToContext;
    113   CXFA_EventParam m_eventParam;
    114   CXFA_NodeArray m_upObjectArray;
    115   // CacheList holds the NodeList items so we can clean them up when we're done.
    116   std::vector<std::unique_ptr<CXFA_NodeList>> m_CacheList;
    117   CXFA_NodeArray* m_pScriptNodeArray;
    118   std::unique_ptr<CXFA_ResolveProcessor> m_ResolveProcessor;
    119   std::unique_ptr<CXFA_FM2JSContext> m_FM2JSContext;
    120   CXFA_Object* m_pThisObject;
    121   uint32_t m_dwBuiltInInFlags;
    122   XFA_ATTRIBUTEENUM m_eRunAtType;
    123 };
    124 
    125 #endif  //  XFA_FXFA_PARSER_CXFA_SCRIPTCONTEXT_H_
    126