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_CJS_GLOBAL_H_ 8 #define FXJS_CJS_GLOBAL_H_ 9 10 #include <map> 11 #include <memory> 12 #include <vector> 13 14 #include "core/fxcrt/span.h" 15 #include "core/fxcrt/unowned_ptr.h" 16 #include "fxjs/cfx_keyvalue.h" 17 #include "fxjs/cjs_object.h" 18 #include "fxjs/cjs_result.h" 19 20 class CFX_GlobalData; 21 22 // The CJS_Global object is not the V8 global object (i.e. it is not |this| 23 // in JavaScript outside of a bound function call). It is a facility for 24 // sharing data amongst documents and persisting data within a document 25 // between sessions. It is only partially implemented due to security and 26 // privacy concerns. It provides access via properties in the usual manner, 27 // execpt that these are stored on the C++ side rather than in V8 itself. 28 // It is a static object that is available as "global" property of the V8 29 // global object and can be manipulated from JavaScript as |global['foo']| 30 // for example. 31 32 class CJS_Global final : public CJS_Object { 33 public: 34 static uint32_t GetObjDefnID(); 35 static void DefineJSObjects(CFXJS_Engine* pEngine); 36 static void DefineAllProperties(CFXJS_Engine* pEngine); 37 38 static v8::Intercepted queryprop_static( 39 v8::Local<v8::Name> property, 40 const v8::PropertyCallbackInfo<v8::Integer>& info); 41 static v8::Intercepted getprop_static( 42 v8::Local<v8::Name> property, 43 const v8::PropertyCallbackInfo<v8::Value>& info); 44 static v8::Intercepted putprop_static( 45 v8::Local<v8::Name> property, 46 v8::Local<v8::Value> value, 47 const v8::PropertyCallbackInfo<void>& info); 48 static v8::Intercepted delprop_static( 49 v8::Local<v8::Name> property, 50 const v8::PropertyCallbackInfo<v8::Boolean>& info); 51 static void enumprop_static(const v8::PropertyCallbackInfo<v8::Array>& info); 52 53 static void setPersistent_static( 54 const v8::FunctionCallbackInfo<v8::Value>& info); 55 56 CJS_Global(v8::Local<v8::Object> pObject, CJS_Runtime* pRuntime); 57 ~CJS_Global() override; 58 59 private: 60 struct JSGlobalData : public CFX_Value { 61 public: 62 JSGlobalData(); 63 ~JSGlobalData(); 64 65 v8::Global<v8::Object> pData; 66 bool bPersistent = false; 67 bool bDeleted = false; 68 }; 69 70 static uint32_t ObjDefnID; 71 static const JSMethodSpec MethodSpecs[]; 72 73 void UpdateGlobalPersistentVariables(); 74 // TODO(crbug.com/pdfium/926): This method is never called. 75 void CommitGlobalPersisitentVariables(); 76 void DestroyGlobalPersisitentVariables(); 77 CJS_Result SetGlobalVariables(const ByteString& propname, 78 CFX_Value::DataType nType, 79 double dData, 80 bool bData, 81 const ByteString& sData, 82 v8::Local<v8::Object> pData, 83 bool bDefaultPersistent); 84 std::vector<std::unique_ptr<CFX_KeyValue>> ObjectToArray( 85 CJS_Runtime* pRuntime, 86 v8::Local<v8::Object> pObj); 87 void PutObjectProperty(v8::Local<v8::Object> obj, CFX_KeyValue* pData); 88 CJS_Result setPersistent(CJS_Runtime* pRuntime, 89 pdfium::span<v8::Local<v8::Value>> params); 90 bool HasProperty(const ByteString& propname); 91 bool DelProperty(const ByteString& propname); 92 CJS_Result GetProperty(CJS_Runtime* pRuntime, const ByteString& propname); 93 CJS_Result SetProperty(CJS_Runtime* pRuntime, 94 const ByteString& propname, 95 v8::Local<v8::Value> vp); 96 void EnumProperties(CJS_Runtime* pRuntime, 97 const v8::PropertyCallbackInfo<v8::Array>& info); 98 99 std::map<ByteString, std::unique_ptr<JSGlobalData>> m_MapGlobal; 100 UnownedPtr<CFX_GlobalData> m_pGlobalData; 101 }; 102 103 #endif // FXJS_CJS_GLOBAL_H_ 104