blob: 8b5fc8399aee9e34e126343cc3de465b91c520f5 [file] [log] [blame]
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001// 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.
Tom Sepez2311b782015-02-23 10:22:51 -08004
John Abd-El-Malek5110c472014-05-17 22:33:34 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Tom Sepezba038bc2015-10-08 12:03:00 -07007// 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.
Tom Sepez9a3f8122015-04-07 15:35:48 -070013
dsinclairb3f24672016-07-12 10:42:14 -070014#ifndef FXJS_INCLUDE_FXJS_V8_H_
15#define FXJS_INCLUDE_FXJS_V8_H_
John Abd-El-Malek5110c472014-05-17 22:33:34 -070016
jinming_wang61dc96f2016-01-28 14:39:56 +080017#include <v8-util.h>
Dan Sinclair61046b92016-02-18 14:48:48 -050018#include <v8.h>
Lei Zhanga688a042015-11-09 13:57:49 -080019
tsepeza4941912016-08-15 11:40:12 -070020#include <map>
weili54be7be2016-09-21 10:19:50 -070021#include <memory>
Lei Zhangd88a3642015-11-10 09:38:57 -080022#include <vector>
23
Dan Sinclaira8a28e02016-03-23 15:41:39 -040024#include "core/fxcrt/include/fx_string.h"
weili54be7be2016-09-21 10:19:50 -070025#ifdef PDF_ENABLE_XFA
26// Header for CFXJSE_RuntimeData. FXJS_V8 doesn't interpret this class,
27// it is just passed along to XFA.
28#include "fxjs/cfxjse_runtimedata.h"
29#endif // PDF_ENABLE_XFA
Lei Zhangd88a3642015-11-10 09:38:57 -080030
tsepeza4941912016-08-15 11:40:12 -070031class CFXJS_Engine;
Lei Zhangd88a3642015-11-10 09:38:57 -080032class CFXJS_ObjDefinition;
Tom Sepezed7b2b52015-09-22 08:36:17 -070033
tsepeza4941912016-08-15 11:40:12 -070034// FXJS_V8 places no restrictions on this class; it merely passes it
Tom Sepez33420902015-10-13 15:00:10 -070035// on to caller-provided methods.
36class IJS_Context; // A description of the event that caused JS execution.
Tom Sepezed7b2b52015-09-22 08:36:17 -070037
Nico Weber9d8ec5a2015-08-04 13:00:21 -070038enum FXJSOBJTYPE {
Tom Sepezcd56a7d2015-10-06 11:45:28 -070039 FXJSOBJTYPE_DYNAMIC = 0, // Created by native method and returned to JS.
40 FXJSOBJTYPE_STATIC, // Created by init and hung off of global object.
41 FXJSOBJTYPE_GLOBAL, // The global object itself (may only appear once).
John Abd-El-Malek5110c472014-05-17 22:33:34 -070042};
43
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044struct FXJSErr {
45 const wchar_t* message;
46 const wchar_t* srcline;
47 unsigned linnum;
John Abd-El-Malek5110c472014-05-17 22:33:34 -070048};
49
jinming_wang61dc96f2016-01-28 14:39:56 +080050// Global weak map to save dynamic objects.
51class V8TemplateMapTraits : public v8::StdMapTraits<void*, v8::Object> {
52 public:
53 typedef v8::GlobalValueMap<void*, v8::Object, V8TemplateMapTraits> MapType;
54 typedef void WeakCallbackDataType;
55
56 static WeakCallbackDataType* WeakCallbackParameter(
57 MapType* map,
58 void* key,
59 const v8::Local<v8::Object>& value) {
60 return key;
61 }
62 static MapType* MapFromWeakCallbackInfo(
63 const v8::WeakCallbackInfo<WeakCallbackDataType>&);
64
65 static void* KeyFromWeakCallbackInfo(
66 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
67 return data.GetParameter();
68 }
69 static const v8::PersistentContainerCallbackType kCallbackType =
70 v8::kWeakWithInternalFields;
71 static void DisposeWeak(
72 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {}
73 static void OnWeakCallback(
74 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {}
75 static void Dispose(v8::Isolate* isolate,
76 v8::Global<v8::Object> value,
77 void* key);
78 static void DisposeCallbackData(WeakCallbackDataType* callbackData) {}
79};
80
81class V8TemplateMap {
82 public:
83 typedef v8::GlobalValueMap<void*, v8::Object, V8TemplateMapTraits> MapType;
84
weili625ad662016-06-15 11:21:33 -070085 explicit V8TemplateMap(v8::Isolate* isolate);
86 ~V8TemplateMap();
87
88 void set(void* key, v8::Local<v8::Object> handle);
89
jinming_wang61dc96f2016-01-28 14:39:56 +080090 friend class V8TemplateMapTraits;
91
92 private:
93 MapType m_map;
94};
95
Tom Sepezed7b2b52015-09-22 08:36:17 -070096class FXJS_PerIsolateData {
97 public:
weili625ad662016-06-15 11:21:33 -070098 ~FXJS_PerIsolateData();
99
Tom Sepezed7b2b52015-09-22 08:36:17 -0700100 static void SetUp(v8::Isolate* pIsolate);
101 static FXJS_PerIsolateData* Get(v8::Isolate* pIsolate);
weili625ad662016-06-15 11:21:33 -0700102
jinming_wang61dc96f2016-01-28 14:39:56 +0800103 void CreateDynamicObjsMap(v8::Isolate* pIsolate) {
Tom Sepez9967cc52016-03-24 11:45:37 -0700104 if (!m_pDynamicObjsMap)
105 m_pDynamicObjsMap = new V8TemplateMap(pIsolate);
jinming_wang61dc96f2016-01-28 14:39:56 +0800106 }
107 void ReleaseDynamicObjsMap() {
108 delete m_pDynamicObjsMap;
109 m_pDynamicObjsMap = nullptr;
110 }
Tom Sepezed7b2b52015-09-22 08:36:17 -0700111
Lei Zhangd88a3642015-11-10 09:38:57 -0800112 std::vector<CFXJS_ObjDefinition*> m_ObjectDefnArray;
Tom Sepez51da0932015-11-25 16:05:49 -0800113#ifdef PDF_ENABLE_XFA
weili54be7be2016-09-21 10:19:50 -0700114 std::unique_ptr<CFXJSE_RuntimeData> m_pFXJSERuntimeData;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800115#endif // PDF_ENABLE_XFA
jinming_wang61dc96f2016-01-28 14:39:56 +0800116 V8TemplateMap* m_pDynamicObjsMap;
Tom Sepezed7b2b52015-09-22 08:36:17 -0700117
118 protected:
weili625ad662016-06-15 11:21:33 -0700119 FXJS_PerIsolateData();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700120};
121
Tom Sepez39bfe122015-09-17 15:25:23 -0700122class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700123 void* Allocate(size_t length) override;
124 void* AllocateUninitialized(size_t length) override;
125 void Free(void* data, size_t length) override;
126};
127
Tom Sepeza72e8e22015-10-07 10:17:53 -0700128void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate);
Tom Sepez39bfe122015-09-17 15:25:23 -0700129void FXJS_Release();
130
Tom Sepeza72e8e22015-10-07 10:17:53 -0700131// Gets the global isolate set by FXJS_Initialize(), or makes a new one each
132// time if there is no such isolate. Returns true if a new isolate had to be
133// created.
134bool FXJS_GetIsolate(v8::Isolate** pResultIsolate);
135
Lei Zhang3fa115b2015-10-08 12:04:47 -0700136// Get the global isolate's ref count.
137size_t FXJS_GlobalIsolateRefCount();
138
tsepezb4694242016-08-15 16:44:55 -0700139class CFXJS_Engine {
140 public:
weili0b2a9872016-09-21 11:50:43 -0700141 explicit CFXJS_Engine(v8::Isolate* pIsolate);
tsepezb4694242016-08-15 16:44:55 -0700142 ~CFXJS_Engine();
Tom Sepez142165e2015-09-11 13:21:50 -0700143
tsepezb4694242016-08-15 16:44:55 -0700144 using Constructor = void (*)(CFXJS_Engine* pEngine,
145 v8::Local<v8::Object> obj);
146 using Destructor = void (*)(CFXJS_Engine* pEngine, v8::Local<v8::Object> obj);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700147
tsepezb4694242016-08-15 16:44:55 -0700148 static CFXJS_Engine* CurrentEngineFromIsolate(v8::Isolate* pIsolate);
149 static int GetObjDefnID(v8::Local<v8::Object> pObj);
Tom Sepezb8ec0a32015-11-20 14:23:02 -0800150
Tom Sepez51da0932015-11-25 16:05:49 -0800151#ifdef PDF_ENABLE_XFA
tsepezb4694242016-08-15 16:44:55 -0700152 // Called as part of FXJS_InitializeEngine, exposed so PDF can make its
153 // own contexts compatible with XFA or vice versa.
154 static void SetForV8Context(v8::Local<v8::Context> v8Context,
155 CFXJS_Engine* pEngine);
Tom Sepez40e9ff32015-11-30 12:39:54 -0800156#endif // PDF_ENABLE_XFA
Tom Sepez4f4603c2015-11-10 15:03:12 -0800157
tsepezb4694242016-08-15 16:44:55 -0700158 v8::Isolate* GetIsolate() const { return m_isolate; }
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700159
tsepezb4694242016-08-15 16:44:55 -0700160 // Always returns a valid, newly-created objDefnID.
161 int DefineObj(const wchar_t* sObjName,
162 FXJSOBJTYPE eObjType,
163 Constructor pConstructor,
164 Destructor pDestructor);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700165
tsepezb4694242016-08-15 16:44:55 -0700166 void DefineObjMethod(int nObjDefnID,
167 const wchar_t* sMethodName,
168 v8::FunctionCallback pMethodCall);
169 void DefineObjProperty(int nObjDefnID,
170 const wchar_t* sPropName,
171 v8::AccessorGetterCallback pPropGet,
172 v8::AccessorSetterCallback pPropPut);
173 void DefineObjAllProperties(int nObjDefnID,
174 v8::NamedPropertyQueryCallback pPropQurey,
175 v8::NamedPropertyGetterCallback pPropGet,
176 v8::NamedPropertySetterCallback pPropPut,
177 v8::NamedPropertyDeleterCallback pPropDel);
178 void DefineObjConst(int nObjDefnID,
179 const wchar_t* sConstName,
180 v8::Local<v8::Value> pDefault);
181 void DefineGlobalMethod(const wchar_t* sMethodName,
182 v8::FunctionCallback pMethodCall);
183 void DefineGlobalConst(const wchar_t* sConstName,
184 v8::FunctionCallback pConstGetter);
Tom Sepez39bfe122015-09-17 15:25:23 -0700185
tsepezb4694242016-08-15 16:44:55 -0700186 // Called after FXJS_Define* calls made.
187 void InitializeEngine();
188 void ReleaseEngine();
tsepezd0b6ed12016-08-11 19:50:57 -0700189
tsepezb4694242016-08-15 16:44:55 -0700190 // Called after FXJS_InitializeEngine call made.
191 int Execute(const CFX_WideString& script, FXJSErr* perror);
tsepezd0b6ed12016-08-11 19:50:57 -0700192
tsepezb4694242016-08-15 16:44:55 -0700193 v8::Local<v8::Context> NewLocalContext();
194 v8::Local<v8::Context> GetPersistentContext();
Tom Sepez39bfe122015-09-17 15:25:23 -0700195
tsepezb4694242016-08-15 16:44:55 -0700196 v8::Local<v8::Value> NewNull();
197 v8::Local<v8::Array> NewArray();
198 v8::Local<v8::Value> NewNumber(int number);
199 v8::Local<v8::Value> NewNumber(double number);
200 v8::Local<v8::Value> NewNumber(float number);
201 v8::Local<v8::Value> NewBoolean(bool b);
202 v8::Local<v8::Value> NewString(const wchar_t* str);
203 v8::Local<v8::Date> NewDate(double d);
204 v8::Local<v8::Object> NewFxDynamicObj(int nObjDefnID, bool bStatic = false);
Tom Sepez39bfe122015-09-17 15:25:23 -0700205
tsepezb4694242016-08-15 16:44:55 -0700206 v8::Local<v8::Object> GetThisObj();
207 int ToInt32(v8::Local<v8::Value> pValue);
208 bool ToBoolean(v8::Local<v8::Value> pValue);
209 double ToNumber(v8::Local<v8::Value> pValue);
210 CFX_WideString ToString(v8::Local<v8::Value> pValue);
211 v8::Local<v8::Object> ToObject(v8::Local<v8::Value> pValue);
212 v8::Local<v8::Array> ToArray(v8::Local<v8::Value> pValue);
Tom Sepez39bfe122015-09-17 15:25:23 -0700213
tsepezb4694242016-08-15 16:44:55 -0700214 unsigned GetArrayLength(v8::Local<v8::Array> pArray);
215 v8::Local<v8::Value> GetArrayElement(v8::Local<v8::Array> pArray,
216 unsigned index);
217 unsigned PutArrayElement(v8::Local<v8::Array> pArray,
218 unsigned index,
219 v8::Local<v8::Value> pValue);
220
221 std::vector<CFX_WideString> GetObjectPropertyNames(
222 v8::Local<v8::Object> pObj);
223 v8::Local<v8::Value> GetObjectProperty(v8::Local<v8::Object> pObj,
224 const CFX_WideString& PropertyName);
225
226 void PutObjectString(v8::Local<v8::Object> pObj,
227 const CFX_WideString& wsPropertyName,
228 const CFX_WideString& wsValue);
229 void PutObjectNumber(v8::Local<v8::Object> pObj,
230 const CFX_WideString& PropertyName,
231 int nValue);
232 void PutObjectNumber(v8::Local<v8::Object> pObj,
233 const CFX_WideString& PropertyName,
234 float fValue);
235 void PutObjectNumber(v8::Local<v8::Object> pObj,
236 const CFX_WideString& PropertyName,
237 double dValue);
238 void PutObjectBoolean(v8::Local<v8::Object> pObj,
239 const CFX_WideString& PropertyName,
240 bool bValue);
241 void PutObjectObject(v8::Local<v8::Object> pObj,
242 const CFX_WideString& PropertyName,
243 v8::Local<v8::Object> pPut);
244 void PutObjectNull(v8::Local<v8::Object> pObj,
245 const CFX_WideString& PropertyName);
246
247 // Native object binding.
248 void SetObjectPrivate(v8::Local<v8::Object> pObj, void* p);
249 void* GetObjectPrivate(v8::Local<v8::Object> pObj);
250 static void FreeObjectPrivate(void* p);
251 static void FreeObjectPrivate(v8::Local<v8::Object> pObj);
252
253 void SetConstArray(const CFX_WideString& name, v8::Local<v8::Array> array);
254 v8::Local<v8::Array> GetConstArray(const CFX_WideString& name);
255
256 v8::Local<v8::String> WSToJSString(const CFX_WideString& wsPropertyName);
257 void Error(const CFX_WideString& message);
258
weili0b2a9872016-09-21 11:50:43 -0700259 protected:
260 CFXJS_Engine();
261
262 void SetIsolate(v8::Isolate* pIsolate) { m_isolate = pIsolate; }
263
tsepezb4694242016-08-15 16:44:55 -0700264 private:
265 v8::Isolate* m_isolate;
266 v8::Global<v8::Context> m_V8PersistentContext;
267 std::vector<v8::Global<v8::Object>*> m_StaticObjects;
268 std::map<CFX_WideString, v8::Global<v8::Array>> m_ConstArrays;
269};
270
dsinclairb3f24672016-07-12 10:42:14 -0700271#endif // FXJS_INCLUDE_FXJS_V8_H_