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_INCLUDE_JSAPI_FXJS_V8_H_ | |
15 #define FPDFSDK_INCLUDE_JSAPI_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 void set(void* key, v8::Local<v8::Object> handle) { | |
85 ASSERT(!m_map.Contains(key)); | |
86 m_map.Set(key, handle); | |
87 } | |
88 explicit V8TemplateMap(v8::Isolate* isolate) : m_map(isolate) {} | |
89 friend class V8TemplateMapTraits; | |
90 | |
91 private: | |
92 MapType m_map; | |
93 }; | |
94 | |
95 class FXJS_PerIsolateData { | |
96 public: | |
97 static void SetUp(v8::Isolate* pIsolate); | |
98 static FXJS_PerIsolateData* Get(v8::Isolate* pIsolate); | |
99 void CreateDynamicObjsMap(v8::Isolate* pIsolate) { | |
100 if (!m_pDynamicObjsMap) | |
101 m_pDynamicObjsMap = new V8TemplateMap(pIsolate); | |
102 } | |
103 void ReleaseDynamicObjsMap() { | |
104 delete m_pDynamicObjsMap; | |
105 m_pDynamicObjsMap = nullptr; | |
106 } | |
107 | |
108 std::vector<CFXJS_ObjDefinition*> m_ObjectDefnArray; | |
109 #ifdef PDF_ENABLE_XFA | |
110 CFXJSE_RuntimeData* m_pFXJSERuntimeData; | |
111 #endif // PDF_ENABLE_XFA | |
112 V8TemplateMap* m_pDynamicObjsMap; | |
113 | |
114 protected: | |
115 #ifndef PDF_ENABLE_XFA | |
116 FXJS_PerIsolateData() : m_pDynamicObjsMap(nullptr) {} | |
117 #else // PDF_ENABLE_XFA | |
118 FXJS_PerIsolateData() | |
119 : m_pFXJSERuntimeData(nullptr), m_pDynamicObjsMap(nullptr) {} | |
120 #endif // PDF_ENABLE_XFA | |
121 }; | |
122 | |
123 extern const wchar_t kFXJSValueNameString[]; | |
124 extern const wchar_t kFXJSValueNameNumber[]; | |
125 extern const wchar_t kFXJSValueNameBoolean[]; | |
126 extern const wchar_t kFXJSValueNameDate[]; | |
127 extern const wchar_t kFXJSValueNameObject[]; | |
128 extern const wchar_t kFXJSValueNameFxobj[]; | |
129 extern const wchar_t kFXJSValueNameNull[]; | |
130 extern const wchar_t kFXJSValueNameUndefined[]; | |
131 | |
132 class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { | |
133 void* Allocate(size_t length) override; | |
134 void* AllocateUninitialized(size_t length) override; | |
135 void Free(void* data, size_t length) override; | |
136 }; | |
137 | |
138 using FXJS_CONSTRUCTOR = void (*)(IJS_Runtime* cc, v8::Local<v8::Object> obj); | |
139 using FXJS_DESTRUCTOR = void (*)(v8::Local<v8::Object> obj); | |
140 | |
141 // Call before making FXJS_PrepareIsolate call. | |
142 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate); | |
143 void FXJS_Release(); | |
144 | |
145 // Gets the global isolate set by FXJS_Initialize(), or makes a new one each | |
146 // time if there is no such isolate. Returns true if a new isolate had to be | |
147 // created. | |
148 bool FXJS_GetIsolate(v8::Isolate** pResultIsolate); | |
149 | |
150 // Get the global isolate's ref count. | |
151 size_t FXJS_GlobalIsolateRefCount(); | |
152 | |
153 // Call before making FXJS_Define* calls. Resources allocated here are cleared | |
154 // as part of FXJS_ReleaseRuntime(). | |
155 void FXJS_PrepareIsolate(v8::Isolate* pIsolate); | |
156 | |
157 // Call before making JS_Define* calls. Resources allocated here are cleared | |
158 // as part of JS_ReleaseRuntime(). | |
159 void JS_PrepareIsolate(v8::Isolate* pIsolate); | |
160 | |
161 // Always returns a valid, newly-created objDefnID. | |
162 int FXJS_DefineObj(v8::Isolate* pIsolate, | |
163 const wchar_t* sObjName, | |
164 FXJSOBJTYPE eObjType, | |
165 FXJS_CONSTRUCTOR pConstructor, | |
166 FXJS_DESTRUCTOR pDestructor); | |
167 | |
168 void FXJS_DefineObjMethod(v8::Isolate* pIsolate, | |
169 int nObjDefnID, | |
170 const wchar_t* sMethodName, | |
171 v8::FunctionCallback pMethodCall); | |
172 void FXJS_DefineObjProperty(v8::Isolate* pIsolate, | |
173 int nObjDefnID, | |
174 const wchar_t* sPropName, | |
175 v8::AccessorGetterCallback pPropGet, | |
176 v8::AccessorSetterCallback pPropPut); | |
177 void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate, | |
178 int nObjDefnID, | |
179 v8::NamedPropertyQueryCallback pPropQurey, | |
180 v8::NamedPropertyGetterCallback pPropGet, | |
181 v8::NamedPropertySetterCallback pPropPut, | |
182 v8::NamedPropertyDeleterCallback pPropDel); | |
183 void FXJS_DefineObjConst(v8::Isolate* pIsolate, | |
184 int nObjDefnID, | |
185 const wchar_t* sConstName, | |
186 v8::Local<v8::Value> pDefault); | |
187 void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate, | |
188 const wchar_t* sMethodName, | |
189 v8::FunctionCallback pMethodCall); | |
190 void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, | |
191 const wchar_t* sConstName, | |
192 v8::FunctionCallback pConstGetter); | |
193 | |
194 // Called after FXJS_Define* calls made. | |
195 void FXJS_InitializeRuntime( | |
196 v8::Isolate* pIsolate, | |
197 IJS_Runtime* pIRuntime, | |
198 v8::Global<v8::Context>* pV8PersistentContext, | |
199 std::vector<v8::Global<v8::Object>*>* pStaticObjects); | |
200 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, | |
201 v8::Global<v8::Context>* pV8PersistentContext, | |
202 std::vector<v8::Global<v8::Object>*>* pStaticObjects); | |
203 IJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate); | |
204 | |
205 #ifdef PDF_ENABLE_XFA | |
206 // Called as part of FXJS_InitializeRuntime, exposed so PDF can make its | |
207 // own contexts compatible with XFA or vice versa. | |
208 void FXJS_SetRuntimeForV8Context(v8::Local<v8::Context> v8Context, | |
209 IJS_Runtime* pIRuntime); | |
210 #endif // PDF_ENABLE_XFA | |
211 | |
212 // Called after FXJS_InitializeRuntime call made. | |
213 int FXJS_Execute(v8::Isolate* pIsolate, | |
214 IJS_Context* pJSContext, | |
215 const wchar_t* script, | |
216 FXJSErr* perror); | |
217 | |
218 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, | |
219 IJS_Runtime* pJSContext, | |
220 int nObjDefnID, | |
221 bool bStatic = false); | |
222 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate); | |
223 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj); | |
224 const wchar_t* FXJS_GetTypeof(v8::Local<v8::Value> pObj); | |
225 | |
226 void FXJS_SetPrivate(v8::Isolate* pIsolate, | |
227 v8::Local<v8::Object> pObj, | |
228 void* p); | |
229 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj); | |
230 void FXJS_FreePrivate(void* p); | |
231 void FXJS_FreePrivate(v8::Local<v8::Object> pObj); | |
232 | |
233 void FXJS_Error(v8::Isolate* isolate, const CFX_WideString& message); | |
234 v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate, | |
235 const wchar_t* PropertyName, | |
236 int Len = -1); | |
237 | |
238 v8::Local<v8::Value> FXJS_GetObjectElement(v8::Isolate* pIsolate, | |
239 v8::Local<v8::Object> pObj, | |
240 const wchar_t* PropertyName); | |
241 v8::Local<v8::Array> FXJS_GetObjectElementNames(v8::Isolate* pIsolate, | |
242 v8::Local<v8::Object> pObj); | |
243 | |
244 v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate, | |
245 v8::Local<v8::Array> pArray, | |
246 unsigned index); | |
247 unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray); | |
248 | |
249 void FXJS_PutObjectString(v8::Isolate* pIsolate, | |
250 v8::Local<v8::Object> pObj, | |
251 const wchar_t* PropertyName, | |
252 const wchar_t* sValue); | |
253 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, | |
254 v8::Local<v8::Object> pObj, | |
255 const wchar_t* PropertyName, | |
256 int nValue); | |
257 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, | |
258 v8::Local<v8::Object> pObj, | |
259 const wchar_t* PropertyName, | |
260 float fValue); | |
261 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, | |
262 v8::Local<v8::Object> pObj, | |
263 const wchar_t* PropertyName, | |
264 double dValue); | |
265 void FXJS_PutObjectBoolean(v8::Isolate* pIsolate, | |
266 v8::Local<v8::Object> pObj, | |
267 const wchar_t* PropertyName, | |
268 bool bValue); | |
269 void FXJS_PutObjectObject(v8::Isolate* pIsolate, | |
270 v8::Local<v8::Object> pObj, | |
271 const wchar_t* PropertyName, | |
272 v8::Local<v8::Object> pPut); | |
273 void FXJS_PutObjectNull(v8::Isolate* pIsolate, | |
274 v8::Local<v8::Object> pObj, | |
275 const wchar_t* PropertyName); | |
276 unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate, | |
277 v8::Local<v8::Array> pArray, | |
278 unsigned index, | |
279 v8::Local<v8::Value> pValue); | |
280 | |
281 v8::Local<v8::Array> FXJS_NewArray(v8::Isolate* pIsolate); | |
282 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, int number); | |
283 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, double number); | |
284 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, float number); | |
285 v8::Local<v8::Value> FXJS_NewBoolean(v8::Isolate* pIsolate, bool b); | |
286 v8::Local<v8::Value> FXJS_NewObject(v8::Isolate* pIsolate, | |
287 v8::Local<v8::Object> pObj); | |
288 v8::Local<v8::Value> FXJS_NewObject2(v8::Isolate* pIsolate, | |
289 v8::Local<v8::Array> pObj); | |
290 v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate, const wchar_t* str); | |
291 v8::Local<v8::Value> FXJS_NewNull(); | |
292 v8::Local<v8::Value> FXJS_NewDate(v8::Isolate* pIsolate, double d); | |
293 | |
294 int FXJS_ToInt32(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue); | |
295 bool FXJS_ToBoolean(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue); | |
296 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue); | |
297 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate, | |
298 v8::Local<v8::Value> pValue); | |
299 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate, | |
300 v8::Local<v8::Value> pValue); | |
301 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate, | |
302 v8::Local<v8::Value> pValue); | |
303 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom); | |
304 | |
305 #endif // FPDFSDK_INCLUDE_JSAPI_FXJS_V8_H_ | |
OLD | NEW |