blob: 65bb921ad91c713ed527de1a75c55e3c82ec6074 [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
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 void SetGlobalVariableNumber(const FX_CHAR* propname, double dData);
68 void SetGlobalVariableBoolean(const FX_CHAR* propname, bool bData);
69 void SetGlobalVariableString(const FX_CHAR* propname,
70 const CFX_ByteString& sData);
71 void SetGlobalVariableObject(const FX_CHAR* propname,
72 const CJS_GlobalVariableArray& array);
73 void SetGlobalVariableNull(const FX_CHAR* propname);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 FX_BOOL SetGlobalVariablePersistent(const FX_CHAR* propname,
76 FX_BOOL bPersistent);
77 FX_BOOL DeleteGlobalVariable(const FX_CHAR* propname);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070078
Nico Weber9d8ec5a2015-08-04 13:00:21 -070079 int32_t GetSize() const;
80 CJS_GlobalData_Element* GetAt(int index) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070081
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 private:
tsepez41a53ad2016-03-28 16:59:30 -070083 using iterator =
84 std::vector<std::unique_ptr<CJS_GlobalData_Element>>::iterator;
85 using const_iterator =
86 std::vector<std::unique_ptr<CJS_GlobalData_Element>>::const_iterator;
87
Tom Sepezf4583622015-09-14 15:06:53 -070088 static CJS_GlobalData* g_Instance;
89
Tom Sepezdfbf8e72015-10-14 14:17:26 -070090 CJS_GlobalData();
Tom Sepezf4583622015-09-14 15:06:53 -070091 ~CJS_GlobalData();
92
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 void LoadGlobalPersistentVariables();
94 void SaveGlobalPersisitentVariables();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070095
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 CJS_GlobalData_Element* GetGlobalVariable(const FX_CHAR* propname);
tsepez41a53ad2016-03-28 16:59:30 -070097 iterator FindGlobalVariable(const FX_CHAR* propname);
98 const_iterator FindGlobalVariable(const FX_CHAR* propname) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 void LoadFileBuffer(const FX_WCHAR* sFilePath,
101 uint8_t*& pBuffer,
102 int32_t& nLength);
103 void WriteFileBuffer(const FX_WCHAR* sFilePath,
104 const FX_CHAR* pBuffer,
105 int32_t nLength);
106 void MakeByteString(const CFX_ByteString& name,
107 CJS_KeyValue* pData,
108 CFX_BinaryBuf& sData);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700109
Tom Sepezf4583622015-09-14 15:06:53 -0700110 size_t m_RefCount;
tsepez41a53ad2016-03-28 16:59:30 -0700111 std::vector<std::unique_ptr<CJS_GlobalData_Element>> m_arrayGlobalData;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 CFX_WideString m_sFilePath;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700113};
114
Dan Sinclairf766ad22016-03-14 13:51:24 -0400115#endif // FPDFSDK_JAVASCRIPT_JS_GLOBALDATA_H_