OLD | NEW |
| (Empty) |
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. | |
4 | |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | |
6 | |
7 // FXJS_V8 is a layer that makes it easier to define native objects in V8, but | |
8 // has no knowledge of PDF-specific native objects. It could in theory be used | |
9 // to implement other sets of native objects. | |
10 | |
11 // PDFium code should include this file rather than including V8 headers | |
12 // directly. | |
13 | |
14 #ifndef FPDFSDK_JSAPI_INCLUDE_FXJS_V8_H_ | |
15 #define FPDFSDK_JSAPI_INCLUDE_FXJS_V8_H_ | |
16 | |
17 #include <v8-util.h> | |
18 #include <v8.h> | |
19 | |
20 #include <vector> | |
21 | |
22 #include "core/fxcrt/include/fx_string.h" | |
23 | |
24 class CFXJS_ObjDefinition; | |
25 | |
26 // FXJS_V8 places no restrictions on these two classes; it merely passes them | |
27 // on to caller-provided methods. | |
28 class IJS_Context; // A description of the event that caused JS execution. | |
29 class IJS_Runtime; // A native runtime, typically owns the v8::Context. | |
30 | |
31 #ifdef PDF_ENABLE_XFA | |
32 // FXJS_V8 places no interpreation on this calass; it merely passes it | |
33 // along to XFA. | |
34 class CFXJSE_RuntimeData; | |
35 #endif // PDF_ENABLE_XFA | |
36 | |
37 enum FXJSOBJTYPE { | |
38 FXJSOBJTYPE_DYNAMIC = 0, // Created by native method and returned to JS. | |
39 FXJSOBJTYPE_STATIC, // Created by init and hung off of global object. | |
40 FXJSOBJTYPE_GLOBAL, // The global object itself (may only appear once). | |
41 }; | |
42 | |
43 struct FXJSErr { | |
44 const wchar_t* message; | |
45 const wchar_t* srcline; | |
46 unsigned linnum; | |
47 }; | |
48 | |
49 // Global weak map to save dynamic objects. | |
50 class V8TemplateMapTraits : public v8::StdMapTraits<void*, v8::Object> { | |
51 public: | |
52 typedef v8::GlobalValueMap<void*, v8::Object, V8TemplateMapTraits> MapType; | |
53 typedef void WeakCallbackDataType; | |
54 | |
55 static WeakCallbackDataType* WeakCallbackParameter( | |
56 MapType* map, | |
57 void* key, | |
58 const v8::Local<v8::Object>& value) { | |
59 return key; | |
60 } | |
61 static MapType* MapFromWeakCallbackInfo( | |
62 const v8::WeakCallbackInfo<WeakCallbackDataType>&); | |
63 | |
64 static void* KeyFromWeakCallbackInfo( | |
65 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) { | |
66 return data.GetParameter(); | |
67 } | |
68 static const v8::PersistentContainerCallbackType kCallbackType = | |
69 v8::kWeakWithInternalFields; | |
70 static void DisposeWeak( | |
71 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {} | |
72 static void OnWeakCallback( | |
73 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {} | |
74 static void Dispose(v8::Isolate* isolate, | |
75 v8::Global<v8::Object> value, | |
76 void* key); | |
77 static void DisposeCallbackData(WeakCallbackDataType* callbackData) {} | |
78 }; | |
79 | |
80 class V8TemplateMap { | |
81 public: | |
82 typedef v8::GlobalValueMap<void*, v8::Object, V8TemplateMapTraits> MapType; | |
83 | |
84 explicit V8TemplateMap(v8::Isolate* isolate); | |
85 ~V8TemplateMap(); | |
86 | |
87 void set(void* key, v8::Local<v8::Object> handle); | |
88 | |
89 friend class V8TemplateMapTraits; | |
90 | |
91 private: | |
92 MapType m_map; | |
93 }; | |
94 | |
95 class FXJS_PerIsolateData { | |
96 public: | |
97 ~FXJS_PerIsolateData(); | |
98 | |
99 static void SetUp(v8::Isolate* pIsolate); | |
100 static FXJS_PerIsolateData* Get(v8::Isolate* pIsolate); | |
101 | |
102 void CreateDynamicObjsMap(v8::Isolate* pIsolate) { | |
103 if (!m_pDynamicObjsMap) | |
104 m_pDynamicObjsMap = new V8TemplateMap(pIsolate); | |
105 } | |
106 void ReleaseDynamicObjsMap() { | |
107 delete m_pDynamicObjsMap; | |
108 m_pDynamicObjsMap = nullptr; | |
109 } | |
110 | |
111 std::vector<CFXJS_ObjDefinition*> m_ObjectDefnArray; | |
112 #ifdef PDF_ENABLE_XFA | |
113 CFXJSE_RuntimeData* m_pFXJSERuntimeData; | |
114 #endif // PDF_ENABLE_XFA | |
115 V8TemplateMap* m_pDynamicObjsMap; | |
116 | |
117 protected: | |
118 FXJS_PerIsolateData(); | |
119 }; | |
120 | |
121 extern const wchar_t kFXJSValueNameString[]; | |
122 extern const wchar_t kFXJSValueNameNumber[]; | |
123 extern const wchar_t kFXJSValueNameBoolean[]; | |
124 extern const wchar_t kFXJSValueNameDate[]; | |
125 extern const wchar_t kFXJSValueNameObject[]; | |
126 extern const wchar_t kFXJSValueNameFxobj[]; | |
127 extern const wchar_t kFXJSValueNameNull[]; | |
128 extern const wchar_t kFXJSValueNameUndefined[]; | |
129 | |
130 class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | |
131 void* Allocate(size_t length) override; | |
132 void* AllocateUninitialized(size_t length) override; | |
133 void Free(void* data, size_t length) override; | |
134 }; | |
135 | |
136 using FXJS_CONSTRUCTOR = void (*)(IJS_Runtime* cc, v8::Local<v8::Object> obj); | |
137 using FXJS_DESTRUCTOR = void (*)(v8::Local<v8::Object> obj); | |
138 | |
139 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate); | |
140 void FXJS_Release(); | |
141 | |
142 // Gets the global isolate set by FXJS_Initialize(), or makes a new one each | |
143 // time if there is no such isolate. Returns true if a new isolate had to be | |
144 // created. | |
145 bool FXJS_GetIsolate(v8::Isolate** pResultIsolate); | |
146 | |
147 // Get the global isolate's ref count. | |
148 size_t FXJS_GlobalIsolateRefCount(); | |
149 | |
150 // Always returns a valid, newly-created objDefnID. | |
151 int FXJS_DefineObj(v8::Isolate* pIsolate, | |
152 const wchar_t* sObjName, | |
153 FXJSOBJTYPE eObjType, | |
154 FXJS_CONSTRUCTOR pConstructor, | |
155 FXJS_DESTRUCTOR pDestructor); | |
156 | |
157 void FXJS_DefineObjMethod(v8::Isolate* pIsolate, | |
158 int nObjDefnID, | |
159 const wchar_t* sMethodName, | |
160 v8::FunctionCallback pMethodCall); | |
161 void FXJS_DefineObjProperty(v8::Isolate* pIsolate, | |
162 int nObjDefnID, | |
163 const wchar_t* sPropName, | |
164 v8::AccessorGetterCallback pPropGet, | |
165 v8::AccessorSetterCallback pPropPut); | |
166 void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate, | |
167 int nObjDefnID, | |
168 v8::NamedPropertyQueryCallback pPropQurey, | |
169 v8::NamedPropertyGetterCallback pPropGet, | |
170 v8::NamedPropertySetterCallback pPropPut, | |
171 v8::NamedPropertyDeleterCallback pPropDel); | |
172 void FXJS_DefineObjConst(v8::Isolate* pIsolate, | |
173 int nObjDefnID, | |
174 const wchar_t* sConstName, | |
175 v8::Local<v8::Value> pDefault); | |
176 void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate, | |
177 const wchar_t* sMethodName, | |
178 v8::FunctionCallback pMethodCall); | |
179 void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, | |
180 const wchar_t* sConstName, | |
181 v8::FunctionCallback pConstGetter); | |
182 | |
183 // Called after FXJS_Define* calls made. | |
184 void FXJS_InitializeRuntime( | |
185 v8::Isolate* pIsolate, | |
186 IJS_Runtime* pIRuntime, | |
187 v8::Global<v8::Context>* pV8PersistentContext, | |
188 std::vector<v8::Global<v8::Object>*>* pStaticObjects); | |
189 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, | |
190 v8::Global<v8::Context>* pV8PersistentContext, | |
191 std::vector<v8::Global<v8::Object>*>* pStaticObjects); | |
192 IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate); | |
193 | |
194 #ifdef PDF_ENABLE_XFA | |
195 // Called as part of FXJS_InitializeRuntime, exposed so PDF can make its | |
196 // own contexts compatible with XFA or vice versa. | |
197 void FXJS_SetRuntimeForV8Context(v8::Local<v8::Context> v8Context, | |
198 IJS_Runtime* pIRuntime); | |
199 #endif // PDF_ENABLE_XFA | |
200 | |
201 // Called after FXJS_InitializeRuntime call made. | |
202 int FXJS_Execute(v8::Isolate* pIsolate, | |
203 const CFX_WideString& script, | |
204 FXJSErr* perror); | |
205 | |
206 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, | |
207 IJS_Runtime* pJSContext, | |
208 int nObjDefnID, | |
209 bool bStatic = false); | |
210 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate); | |
211 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj); | |
212 const wchar_t* FXJS_GetTypeof(v8::Local<v8::Value> pObj); | |
213 | |
214 void FXJS_SetPrivate(v8::Isolate* pIsolate, | |
215 v8::Local<v8::Object> pObj, | |
216 void* p); | |
217 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj); | |
218 void FXJS_FreePrivate(void* p); | |
219 void FXJS_FreePrivate(v8::Local<v8::Object> pObj); | |
220 void FXJS_Error(v8::Isolate* isolate, const CFX_WideString& message); | |
221 | |
222 v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate, | |
223 const CFX_WideString& wsPropertyName); | |
224 v8::Local<v8::Value> FXJS_GetObjectElement(v8::Isolate* pIsolate, | |
225 v8::Local<v8::Object> pObj, | |
226 const CFX_WideString& PropertyName); | |
227 v8::Local<v8::Array> FXJS_GetObjectElementNames(v8::Isolate* pIsolate, | |
228 v8::Local<v8::Object> pObj); | |
229 v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate, | |
230 v8::Local<v8::Array> pArray, | |
231 unsigned index); | |
232 | |
233 unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray); | |
234 void FXJS_PutObjectString(v8::Isolate* pIsolate, | |
235 v8::Local<v8::Object> pObj, | |
236 const CFX_WideString& wsPropertyName, | |
237 const CFX_WideString& wsValue); | |
238 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, | |
239 v8::Local<v8::Object> pObj, | |
240 const CFX_WideString& PropertyName, | |
241 int nValue); | |
242 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, | |
243 v8::Local<v8::Object> pObj, | |
244 const CFX_WideString& PropertyName, | |
245 float fValue); | |
246 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, | |
247 v8::Local<v8::Object> pObj, | |
248 const CFX_WideString& PropertyName, | |
249 double dValue); | |
250 void FXJS_PutObjectBoolean(v8::Isolate* pIsolate, | |
251 v8::Local<v8::Object> pObj, | |
252 const CFX_WideString& PropertyName, | |
253 bool bValue); | |
254 void FXJS_PutObjectObject(v8::Isolate* pIsolate, | |
255 v8::Local<v8::Object> pObj, | |
256 const CFX_WideString& PropertyName, | |
257 v8::Local<v8::Object> pPut); | |
258 void FXJS_PutObjectNull(v8::Isolate* pIsolate, | |
259 v8::Local<v8::Object> pObj, | |
260 const CFX_WideString& PropertyName); | |
261 unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate, | |
262 v8::Local<v8::Array> pArray, | |
263 unsigned index, | |
264 v8::Local<v8::Value> pValue); | |
265 | |
266 v8::Local<v8::Array> FXJS_NewArray(v8::Isolate* pIsolate); | |
267 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, int number); | |
268 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, double number); | |
269 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, float number); | |
270 v8::Local<v8::Value> FXJS_NewBoolean(v8::Isolate* pIsolate, bool b); | |
271 v8::Local<v8::Value> FXJS_NewObject(v8::Isolate* pIsolate, | |
272 v8::Local<v8::Object> pObj); | |
273 v8::Local<v8::Value> FXJS_NewObject2(v8::Isolate* pIsolate, | |
274 v8::Local<v8::Array> pObj); | |
275 v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate, const wchar_t* str); | |
276 v8::Local<v8::Value> FXJS_NewNull(); | |
277 v8::Local<v8::Value> FXJS_NewDate(v8::Isolate* pIsolate, double d); | |
278 | |
279 int FXJS_ToInt32(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue); | |
280 bool FXJS_ToBoolean(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue); | |
281 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue); | |
282 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate, | |
283 v8::Local<v8::Value> pValue); | |
284 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate, | |
285 v8::Local<v8::Value> pValue); | |
286 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate, | |
287 v8::Local<v8::Value> pValue); | |
288 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom); | |
289 | |
290 #endif // FPDFSDK_JSAPI_INCLUDE_FXJS_V8_H_ | |
OLD | NEW |