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 FXJS_CJS_GLOBALDATA_H_
8 #define FXJS_CJS_GLOBALDATA_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "core/fxcrt/cfx_binarybuf.h"
14 #include "fxjs/cjs_keyvalue.h"
15 
16 class CPDFSDK_FormFillEnvironment;
17 
18 class CJS_GlobalData_Element {
19  public:
20   CJS_GlobalData_Element() {}
21   ~CJS_GlobalData_Element() {}
22 
23   CJS_KeyValue data;
24   bool bPersistent;
25 };
26 
27 class CJS_GlobalData {
28  public:
29   static CJS_GlobalData* GetRetainedInstance(CPDFSDK_FormFillEnvironment* pApp);
30   void Release();
31 
32   void SetGlobalVariableNumber(const ByteString& propname, double dData);
33   void SetGlobalVariableBoolean(const ByteString& propname, bool bData);
34   void SetGlobalVariableString(const ByteString& propname,
35                                const ByteString& sData);
36   void SetGlobalVariableObject(const ByteString& propname,
37                                const CJS_GlobalVariableArray& array);
38   void SetGlobalVariableNull(const ByteString& propname);
39   bool SetGlobalVariablePersistent(const ByteString& propname,
40                                    bool bPersistent);
41   bool DeleteGlobalVariable(const ByteString& propname);
42 
43   int32_t GetSize() const;
44   CJS_GlobalData_Element* GetAt(int index) const;
45 
46  private:
47   using iterator =
48       std::vector<std::unique_ptr<CJS_GlobalData_Element>>::iterator;
49   using const_iterator =
50       std::vector<std::unique_ptr<CJS_GlobalData_Element>>::const_iterator;
51 
52   CJS_GlobalData();
53   ~CJS_GlobalData();
54 
55   void LoadGlobalPersistentVariables();
56   void SaveGlobalPersisitentVariables();
57 
58   CJS_GlobalData_Element* GetGlobalVariable(const ByteString& sPropname);
59   iterator FindGlobalVariable(const ByteString& sPropname);
60   const_iterator FindGlobalVariable(const ByteString& sPropname) const;
61 
62   void LoadFileBuffer(const wchar_t* sFilePath,
63                       uint8_t*& pBuffer,
64                       int32_t& nLength);
65   void WriteFileBuffer(const wchar_t* sFilePath,
66                        const char* pBuffer,
67                        int32_t nLength);
68   void MakeByteString(const ByteString& name,
69                       CJS_KeyValue* pData,
70                       CFX_BinaryBuf& sData);
71 
72   size_t m_RefCount;
73   std::vector<std::unique_ptr<CJS_GlobalData_Element>> m_arrayGlobalData;
74   WideString m_sFilePath;
75 };
76 
77 #endif  // FXJS_CJS_GLOBALDATA_H_
78