xref: /aosp_15_r20/external/pdfium/fxjs/cfx_globaldata.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1 // Copyright 2014 The PDFium Authors
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_CFX_GLOBALDATA_H_
8 #define FXJS_CFX_GLOBALDATA_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "core/fxcrt/binary_buffer.h"
14 #include "core/fxcrt/unowned_ptr.h"
15 #include "fxjs/cfx_keyvalue.h"
16 #include "third_party/abseil-cpp/absl/types/optional.h"
17 #include "third_party/base/containers/span.h"
18 
19 class CFX_GlobalData {
20  public:
21   class Delegate {
22    public:
23     virtual ~Delegate() = default;
24 
25     virtual bool StoreBuffer(pdfium::span<const uint8_t> pBuffer) = 0;
26     virtual absl::optional<pdfium::span<uint8_t>> LoadBuffer() = 0;
27     virtual void BufferDone() = 0;
28   };
29 
30   class Element {
31    public:
32     Element();
33     ~Element();
34 
35     CFX_KeyValue data;
36     bool bPersistent = false;
37   };
38 
39   static CFX_GlobalData* GetRetainedInstance(Delegate* pDelegate);
40   bool Release();
41 
42   void SetGlobalVariableNumber(ByteString propname, double dData);
43   void SetGlobalVariableBoolean(ByteString propname, bool bData);
44   void SetGlobalVariableString(ByteString propname, const ByteString& sData);
45   void SetGlobalVariableObject(
46       ByteString propname,
47       std::vector<std::unique_ptr<CFX_KeyValue>> array);
48   void SetGlobalVariableNull(ByteString propname);
49   bool SetGlobalVariablePersistent(ByteString propname, bool bPersistent);
50   bool DeleteGlobalVariable(ByteString propname);
51 
52   int32_t GetSize() const;
53   Element* GetAt(int index);
54 
55   // Exposed for testing.
56   Element* GetGlobalVariable(const ByteString& sPropname);
57 
58  private:
59   using iterator = std::vector<std::unique_ptr<Element>>::iterator;
60 
61   explicit CFX_GlobalData(Delegate* pDelegate);
62   ~CFX_GlobalData();
63 
64   bool LoadGlobalPersistentVariables();
65   bool LoadGlobalPersistentVariablesFromBuffer(pdfium::span<uint8_t> buffer);
66   bool SaveGlobalPersisitentVariables();
67   iterator FindGlobalVariable(const ByteString& sPropname);
68 
69   size_t m_RefCount = 0;
70   UnownedPtr<Delegate> const m_pDelegate;
71   std::vector<std::unique_ptr<Element>> m_arrayGlobalData;
72 };
73 
74 #endif  // FXJS_CFX_GLOBALDATA_H_
75