John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 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. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 7 | #ifndef FPDFSDK_JAVASCRIPT_JS_GLOBALDATA_H_ |
| 8 | #define FPDFSDK_JAVASCRIPT_JS_GLOBALDATA_H_ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 9 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 10 | #include <memory> |
| 11 | #include <vector> |
| 12 | |
Dan Sinclair | a8a28e0 | 2016-03-23 15:41:39 -0400 | [diff] [blame] | 13 | #include "core/fxcrt/include/fx_basic.h" |
Tom Sepez | 9a3f812 | 2015-04-07 15:35:48 -0700 | [diff] [blame] | 14 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 15 | #define JS_GLOBALDATA_TYPE_NUMBER 0 |
| 16 | #define JS_GLOBALDATA_TYPE_BOOLEAN 1 |
| 17 | #define JS_GLOBALDATA_TYPE_STRING 2 |
| 18 | #define JS_GLOBALDATA_TYPE_OBJECT 3 |
| 19 | #define JS_GLOBALDATA_TYPE_NULL 4 |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 20 | |
| 21 | class CJS_KeyValue; |
Tom Sepez | 9a3f812 | 2015-04-07 15:35:48 -0700 | [diff] [blame] | 22 | class CPDFDoc_Environment; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 23 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 24 | class CJS_GlobalVariableArray { |
| 25 | public: |
| 26 | CJS_GlobalVariableArray(); |
| 27 | virtual ~CJS_GlobalVariableArray(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 28 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 29 | void Add(CJS_KeyValue* p); |
| 30 | int Count() const; |
| 31 | CJS_KeyValue* GetAt(int index) const; |
| 32 | void Copy(const CJS_GlobalVariableArray& array); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 33 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 34 | void Empty(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 35 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 36 | private: |
| 37 | CFX_ArrayTemplate<CJS_KeyValue*> array; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 40 | class CJS_KeyValue { |
| 41 | public: |
| 42 | CJS_KeyValue() {} |
| 43 | virtual ~CJS_KeyValue() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 44 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 45 | CFX_ByteString sKey; |
| 46 | int nType; // 0:int 1:bool 2:string 3:obj |
| 47 | double dData; |
| 48 | bool bData; |
| 49 | CFX_ByteString sData; |
| 50 | CJS_GlobalVariableArray objData; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 53 | class CJS_GlobalData_Element { |
| 54 | public: |
| 55 | CJS_GlobalData_Element() {} |
| 56 | virtual ~CJS_GlobalData_Element() {} |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 57 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 58 | CJS_KeyValue data; |
| 59 | FX_BOOL bPersistent; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 62 | class CJS_GlobalData { |
| 63 | public: |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 64 | static CJS_GlobalData* GetRetainedInstance(CPDFDoc_Environment* pApp); |
| 65 | void Release(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 66 | |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 67 | void SetGlobalVariableNumber(const CFX_ByteString& propname, double dData); |
| 68 | void SetGlobalVariableBoolean(const CFX_ByteString& propname, bool bData); |
| 69 | void SetGlobalVariableString(const CFX_ByteString& propname, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 70 | const CFX_ByteString& sData); |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 71 | void SetGlobalVariableObject(const CFX_ByteString& propname, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 72 | const CJS_GlobalVariableArray& array); |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 73 | void SetGlobalVariableNull(const CFX_ByteString& propname); |
| 74 | FX_BOOL SetGlobalVariablePersistent(const CFX_ByteString& propname, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 75 | FX_BOOL bPersistent); |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 76 | FX_BOOL DeleteGlobalVariable(const CFX_ByteString& propname); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 77 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 78 | int32_t GetSize() const; |
| 79 | CJS_GlobalData_Element* GetAt(int index) const; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 80 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 81 | private: |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 82 | using iterator = |
| 83 | std::vector<std::unique_ptr<CJS_GlobalData_Element>>::iterator; |
| 84 | using const_iterator = |
| 85 | std::vector<std::unique_ptr<CJS_GlobalData_Element>>::const_iterator; |
| 86 | |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 87 | static CJS_GlobalData* g_Instance; |
| 88 | |
Tom Sepez | dfbf8e7 | 2015-10-14 14:17:26 -0700 | [diff] [blame] | 89 | CJS_GlobalData(); |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 90 | ~CJS_GlobalData(); |
| 91 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 92 | void LoadGlobalPersistentVariables(); |
| 93 | void SaveGlobalPersisitentVariables(); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 94 | |
tsepez | 24a4888 | 2016-04-11 15:18:40 -0700 | [diff] [blame] | 95 | CJS_GlobalData_Element* GetGlobalVariable(const CFX_ByteString& sPropname); |
| 96 | iterator FindGlobalVariable(const CFX_ByteString& sPropname); |
| 97 | const_iterator FindGlobalVariable(const CFX_ByteString& sPropname) const; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 98 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 99 | void LoadFileBuffer(const FX_WCHAR* sFilePath, |
| 100 | uint8_t*& pBuffer, |
| 101 | int32_t& nLength); |
| 102 | void WriteFileBuffer(const FX_WCHAR* sFilePath, |
| 103 | const FX_CHAR* pBuffer, |
| 104 | int32_t nLength); |
| 105 | void MakeByteString(const CFX_ByteString& name, |
| 106 | CJS_KeyValue* pData, |
| 107 | CFX_BinaryBuf& sData); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 108 | |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 109 | size_t m_RefCount; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 110 | std::vector<std::unique_ptr<CJS_GlobalData_Element>> m_arrayGlobalData; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 111 | CFX_WideString m_sFilePath; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 114 | #endif // FPDFSDK_JAVASCRIPT_JS_GLOBALDATA_H_ |