blob: 056f26193ff17fecc6d074455cc7036ec07c9bd5 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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 Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Dan Sinclairf766ad22016-03-14 13:51:24 -04007#ifndef FPDFSDK_JAVASCRIPT_JS_GLOBALDATA_H_
8#define FPDFSDK_JAVASCRIPT_JS_GLOBALDATA_H_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009
tsepez41a53ad2016-03-28 16:59:30 -070010#include <memory>
11#include <vector>
12
Dan Sinclaira8a28e02016-03-23 15:41:39 -040013#include "core/fxcrt/include/fx_basic.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070014
Nico Weber9d8ec5a2015-08-04 13:00:21 -070015#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-Malek3f3b45c2014-05-23 17:28:10 -070020
21class CJS_KeyValue;
Tom Sepez9a3f8122015-04-07 15:35:48 -070022class CPDFDoc_Environment;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070023
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024class CJS_GlobalVariableArray {
25 public:
26 CJS_GlobalVariableArray();
27 virtual ~CJS_GlobalVariableArray();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070028
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029 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-Malek3f3b45c2014-05-23 17:28:10 -070033
Nico Weber9d8ec5a2015-08-04 13:00:21 -070034 void Empty();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035
Nico Weber9d8ec5a2015-08-04 13:00:21 -070036 private:
37 CFX_ArrayTemplate<CJS_KeyValue*> array;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070038};
39
Nico Weber9d8ec5a2015-08-04 13:00:21 -070040class CJS_KeyValue {
41 public:
42 CJS_KeyValue() {}
43 virtual ~CJS_KeyValue() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045 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-Malek3f3b45c2014-05-23 17:28:10 -070051};
52
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053class CJS_GlobalData_Element {
54 public:
55 CJS_GlobalData_Element() {}
56 virtual ~CJS_GlobalData_Element() {}
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 CJS_KeyValue data;
59 FX_BOOL bPersistent;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070060};
61
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062class CJS_GlobalData {
63 public:
Tom Sepezf4583622015-09-14 15:06:53 -070064 static CJS_GlobalData* GetRetainedInstance(CPDFDoc_Environment* pApp);
65 void Release();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066
tsepez24a48882016-04-11 15:18:40 -070067 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 Weber9d8ec5a2015-08-04 13:00:21 -070070 const CFX_ByteString& sData);
tsepez24a48882016-04-11 15:18:40 -070071 void SetGlobalVariableObject(const CFX_ByteString& propname,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 const CJS_GlobalVariableArray& array);
tsepez24a48882016-04-11 15:18:40 -070073 void SetGlobalVariableNull(const CFX_ByteString& propname);
74 FX_BOOL SetGlobalVariablePersistent(const CFX_ByteString& propname,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 FX_BOOL bPersistent);
tsepez24a48882016-04-11 15:18:40 -070076 FX_BOOL DeleteGlobalVariable(const CFX_ByteString& propname);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070077
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 int32_t GetSize() const;
79 CJS_GlobalData_Element* GetAt(int index) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 private:
tsepez41a53ad2016-03-28 16:59:30 -070082 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 Sepezf4583622015-09-14 15:06:53 -070087 static CJS_GlobalData* g_Instance;
88
Tom Sepezdfbf8e72015-10-14 14:17:26 -070089 CJS_GlobalData();
Tom Sepezf4583622015-09-14 15:06:53 -070090 ~CJS_GlobalData();
91
Nico Weber9d8ec5a2015-08-04 13:00:21 -070092 void LoadGlobalPersistentVariables();
93 void SaveGlobalPersisitentVariables();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070094
tsepez24a48882016-04-11 15:18:40 -070095 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-Malek3f3b45c2014-05-23 17:28:10 -070098
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099 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-Malek3f3b45c2014-05-23 17:28:10 -0700108
Tom Sepezf4583622015-09-14 15:06:53 -0700109 size_t m_RefCount;
tsepez41a53ad2016-03-28 16:59:30 -0700110 std::vector<std::unique_ptr<CJS_GlobalData_Element>> m_arrayGlobalData;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 CFX_WideString m_sFilePath;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700112};
113
Dan Sinclairf766ad22016-03-14 13:51:24 -0400114#endif // FPDFSDK_JAVASCRIPT_JS_GLOBALDATA_H_