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