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 FPDFSDK_SRC_JAVASCRIPT_JS_GLOBALDATA_H_ 8 #define FPDFSDK_SRC_JAVASCRIPT_JS_GLOBALDATA_H_ 9 10 #include "core/include/fxcrt/fx_basic.h" 11 12 #define JS_GLOBALDATA_TYPE_NUMBER 0 13 #define JS_GLOBALDATA_TYPE_BOOLEAN 1 14 #define JS_GLOBALDATA_TYPE_STRING 2 15 #define JS_GLOBALDATA_TYPE_OBJECT 3 16 #define JS_GLOBALDATA_TYPE_NULL 4 17 18 class CJS_KeyValue; 19 class CPDFDoc_Environment; 20 21 class CJS_GlobalVariableArray { 22 public: 23 CJS_GlobalVariableArray(); 24 virtual ~CJS_GlobalVariableArray(); 25 26 void Add(CJS_KeyValue* p); 27 int Count() const; 28 CJS_KeyValue* GetAt(int index) const; 29 void Copy(const CJS_GlobalVariableArray& array); 30 31 void Empty(); 32 33 private: 34 CFX_ArrayTemplate<CJS_KeyValue*> array; 35 }; 36 37 class CJS_KeyValue { 38 public: CJS_KeyValue()39 CJS_KeyValue() {} ~CJS_KeyValue()40 virtual ~CJS_KeyValue() {} 41 42 CFX_ByteString sKey; 43 int nType; // 0:int 1:bool 2:string 3:obj 44 double dData; 45 bool bData; 46 CFX_ByteString sData; 47 CJS_GlobalVariableArray objData; 48 }; 49 50 class CJS_GlobalData_Element { 51 public: CJS_GlobalData_Element()52 CJS_GlobalData_Element() {} ~CJS_GlobalData_Element()53 virtual ~CJS_GlobalData_Element() {} 54 55 CJS_KeyValue data; 56 FX_BOOL bPersistent; 57 }; 58 59 class CJS_GlobalData { 60 public: 61 static CJS_GlobalData* GetRetainedInstance(CPDFDoc_Environment* pApp); 62 void Release(); 63 64 void SetGlobalVariableNumber(const FX_CHAR* propname, double dData); 65 void SetGlobalVariableBoolean(const FX_CHAR* propname, bool bData); 66 void SetGlobalVariableString(const FX_CHAR* propname, 67 const CFX_ByteString& sData); 68 void SetGlobalVariableObject(const FX_CHAR* propname, 69 const CJS_GlobalVariableArray& array); 70 void SetGlobalVariableNull(const FX_CHAR* propname); 71 72 FX_BOOL SetGlobalVariablePersistent(const FX_CHAR* propname, 73 FX_BOOL bPersistent); 74 FX_BOOL DeleteGlobalVariable(const FX_CHAR* propname); 75 76 int32_t GetSize() const; 77 CJS_GlobalData_Element* GetAt(int index) const; 78 79 private: 80 static CJS_GlobalData* g_Instance; 81 82 CJS_GlobalData(); 83 ~CJS_GlobalData(); 84 85 void LoadGlobalPersistentVariables(); 86 void SaveGlobalPersisitentVariables(); 87 88 CJS_GlobalData_Element* GetGlobalVariable(const FX_CHAR* propname); 89 int FindGlobalVariable(const FX_CHAR* propname); 90 91 void LoadFileBuffer(const FX_WCHAR* sFilePath, 92 uint8_t*& pBuffer, 93 int32_t& nLength); 94 void WriteFileBuffer(const FX_WCHAR* sFilePath, 95 const FX_CHAR* pBuffer, 96 int32_t nLength); 97 void MakeByteString(const CFX_ByteString& name, 98 CJS_KeyValue* pData, 99 CFX_BinaryBuf& sData); 100 101 size_t m_RefCount; 102 CFX_ArrayTemplate<CJS_GlobalData_Element*> m_arrayGlobalData; 103 CFX_WideString m_sFilePath; 104 }; 105 106 #endif // FPDFSDK_SRC_JAVASCRIPT_JS_GLOBALDATA_H_ 107