John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 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. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 7 | #ifndef FXJS_CFX_GLOBALDATA_H_ |
| 8 | #define FXJS_CFX_GLOBALDATA_H_ |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 9 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 10 | #include <memory> |
| 11 | #include <vector> |
| 12 | |
Dan Sinclair | cd07123 | 2017-08-30 16:21:36 -0400 | [diff] [blame] | 13 | #include "core/fxcrt/cfx_binarybuf.h" |
Tom Sepez | eccfe0e | 2018-11-01 16:34:52 +0000 | [diff] [blame] | 14 | #include "core/fxcrt/unowned_ptr.h" |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 15 | #include "fxjs/cfx_keyvalue.h" |
Tom Sepez | eccfe0e | 2018-11-01 16:34:52 +0000 | [diff] [blame] | 16 | #include "third_party/base/optional.h" |
| 17 | #include "third_party/base/span.h" |
Tom Sepez | 9a3f812 | 2015-04-07 15:35:48 -0700 | [diff] [blame] | 18 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 19 | class CPDFSDK_FormFillEnvironment; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 20 | |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 21 | class CFX_GlobalData { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 22 | public: |
Tom Sepez | eccfe0e | 2018-11-01 16:34:52 +0000 | [diff] [blame] | 23 | 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 Sepez | a8a69e7 | 2018-10-17 21:53:43 +0000 | [diff] [blame] | 32 | class Element { |
| 33 | public: |
| 34 | Element(); |
| 35 | ~Element(); |
| 36 | |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 37 | CFX_KeyValue data; |
Tom Sepez | a8a69e7 | 2018-10-17 21:53:43 +0000 | [diff] [blame] | 38 | bool bPersistent; |
| 39 | }; |
| 40 | |
Tom Sepez | eccfe0e | 2018-11-01 16:34:52 +0000 | [diff] [blame] | 41 | static CFX_GlobalData* GetRetainedInstance(Delegate* pDelegate); |
| 42 | bool Release(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 43 | |
Tom Sepez | e005dc3 | 2018-06-19 17:33:32 +0000 | [diff] [blame] | 44 | void SetGlobalVariableNumber(ByteString propname, double dData); |
| 45 | void SetGlobalVariableBoolean(ByteString propname, bool bData); |
| 46 | void SetGlobalVariableString(ByteString propname, const ByteString& sData); |
Tom Sepez | 436c9f1 | 2018-11-05 18:20:23 +0000 | [diff] [blame] | 47 | void SetGlobalVariableObject(ByteString propname, CFX_GlobalArray array); |
Tom Sepez | e005dc3 | 2018-06-19 17:33:32 +0000 | [diff] [blame] | 48 | void SetGlobalVariableNull(ByteString propname); |
| 49 | bool SetGlobalVariablePersistent(ByteString propname, bool bPersistent); |
| 50 | bool DeleteGlobalVariable(ByteString propname); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 51 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 52 | int32_t GetSize() const; |
Tom Sepez | bf2ef78 | 2018-11-07 20:03:30 +0000 | [diff] [blame] | 53 | Element* GetAt(int index); |
| 54 | |
| 55 | // Exposed for testing. |
| 56 | Element* GetGlobalVariable(const ByteString& sPropname); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 57 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 58 | private: |
Tom Sepez | a8a69e7 | 2018-10-17 21:53:43 +0000 | [diff] [blame] | 59 | using iterator = std::vector<std::unique_ptr<Element>>::iterator; |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 60 | |
Tom Sepez | eccfe0e | 2018-11-01 16:34:52 +0000 | [diff] [blame] | 61 | explicit CFX_GlobalData(Delegate* pDelegate); |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 62 | ~CFX_GlobalData(); |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 63 | |
Tom Sepez | 3eaad18 | 2018-11-07 21:52:21 +0000 | [diff] [blame] | 64 | bool LoadGlobalPersistentVariables(); |
| 65 | bool LoadGlobalPersistentVariablesFromBuffer(pdfium::span<uint8_t> buffer); |
| 66 | bool SaveGlobalPersisitentVariables(); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 67 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 68 | iterator FindGlobalVariable(const ByteString& sPropname); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 69 | |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 70 | void LoadFileBuffer(const wchar_t* sFilePath, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 71 | uint8_t*& pBuffer, |
| 72 | int32_t& nLength); |
Dan Sinclair | 812e96c | 2017-03-13 16:43:37 -0400 | [diff] [blame] | 73 | void WriteFileBuffer(const wchar_t* sFilePath, |
| 74 | const char* pBuffer, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 75 | int32_t nLength); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 76 | |
Tom Sepez | a8a69e7 | 2018-10-17 21:53:43 +0000 | [diff] [blame] | 77 | size_t m_RefCount = 0; |
Tom Sepez | eccfe0e | 2018-11-01 16:34:52 +0000 | [diff] [blame] | 78 | UnownedPtr<Delegate> const m_pDelegate; |
Tom Sepez | a8a69e7 | 2018-10-17 21:53:43 +0000 | [diff] [blame] | 79 | std::vector<std::unique_ptr<Element>> m_arrayGlobalData; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 82 | #endif // FXJS_CFX_GLOBALDATA_H_ |