blob: 91f87afe8a7f2d42be966b83a3f0fec52eda2e82 [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
Tom Sepez41d04e12018-10-30 22:07:36 +00007#ifndef FXJS_CFX_GLOBALDATA_H_
8#define FXJS_CFX_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 Sinclaircd071232017-08-30 16:21:36 -040013#include "core/fxcrt/cfx_binarybuf.h"
Tom Sepezeccfe0e2018-11-01 16:34:52 +000014#include "core/fxcrt/unowned_ptr.h"
Tom Sepez41d04e12018-10-30 22:07:36 +000015#include "fxjs/cfx_keyvalue.h"
Tom Sepezeccfe0e2018-11-01 16:34:52 +000016#include "third_party/base/optional.h"
17#include "third_party/base/span.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070018
dsinclair735606d2016-10-05 15:47:02 -070019class CPDFSDK_FormFillEnvironment;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020
Tom Sepez41d04e12018-10-30 22:07:36 +000021class CFX_GlobalData {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070022 public:
Tom Sepezeccfe0e2018-11-01 16:34:52 +000023 class Delegate {
24 public:
25 virtual ~Delegate() {}
26
27 virtual bool StoreBuffer(pdfium::span<const uint8_t> pBuffer) = 0;
28 virtual Optional<pdfium::span<uint8_t>> LoadBuffer() = 0;
29 virtual void BufferDone() = 0;
30 };
31
Tom Sepeza8a69e72018-10-17 21:53:43 +000032 class Element {
33 public:
34 Element();
35 ~Element();
36
Tom Sepez41d04e12018-10-30 22:07:36 +000037 CFX_KeyValue data;
Tom Sepeza8a69e72018-10-17 21:53:43 +000038 bool bPersistent;
39 };
40
Tom Sepezeccfe0e2018-11-01 16:34:52 +000041 static CFX_GlobalData* GetRetainedInstance(Delegate* pDelegate);
42 bool Release();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070043
Tom Sepeze005dc32018-06-19 17:33:32 +000044 void SetGlobalVariableNumber(ByteString propname, double dData);
45 void SetGlobalVariableBoolean(ByteString propname, bool bData);
46 void SetGlobalVariableString(ByteString propname, const ByteString& sData);
Tom Sepez436c9f12018-11-05 18:20:23 +000047 void SetGlobalVariableObject(ByteString propname, CFX_GlobalArray array);
Tom Sepeze005dc32018-06-19 17:33:32 +000048 void SetGlobalVariableNull(ByteString propname);
49 bool SetGlobalVariablePersistent(ByteString propname, bool bPersistent);
50 bool DeleteGlobalVariable(ByteString propname);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070051
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 int32_t GetSize() const;
Tom Sepezbf2ef782018-11-07 20:03:30 +000053 Element* GetAt(int index);
54
55 // Exposed for testing.
56 Element* GetGlobalVariable(const ByteString& sPropname);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 private:
Tom Sepeza8a69e72018-10-17 21:53:43 +000059 using iterator = std::vector<std::unique_ptr<Element>>::iterator;
tsepez41a53ad2016-03-28 16:59:30 -070060
Tom Sepezeccfe0e2018-11-01 16:34:52 +000061 explicit CFX_GlobalData(Delegate* pDelegate);
Tom Sepez41d04e12018-10-30 22:07:36 +000062 ~CFX_GlobalData();
Tom Sepezf4583622015-09-14 15:06:53 -070063
Tom Sepez3eaad182018-11-07 21:52:21 +000064 bool LoadGlobalPersistentVariables();
65 bool LoadGlobalPersistentVariablesFromBuffer(pdfium::span<uint8_t> buffer);
66 bool SaveGlobalPersisitentVariables();
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070067
Ryan Harrison275e2602017-09-18 14:23:18 -040068 iterator FindGlobalVariable(const ByteString& sPropname);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070069
Dan Sinclair812e96c2017-03-13 16:43:37 -040070 void LoadFileBuffer(const wchar_t* sFilePath,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 uint8_t*& pBuffer,
72 int32_t& nLength);
Dan Sinclair812e96c2017-03-13 16:43:37 -040073 void WriteFileBuffer(const wchar_t* sFilePath,
74 const char* pBuffer,
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 int32_t nLength);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076
Tom Sepeza8a69e72018-10-17 21:53:43 +000077 size_t m_RefCount = 0;
Tom Sepezeccfe0e2018-11-01 16:34:52 +000078 UnownedPtr<Delegate> const m_pDelegate;
Tom Sepeza8a69e72018-10-17 21:53:43 +000079 std::vector<std::unique_ptr<Element>> m_arrayGlobalData;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080};
81
Tom Sepez41d04e12018-10-30 22:07:36 +000082#endif // FXJS_CFX_GLOBALDATA_H_