blob: 457843f387c12c008e95c985d7718e5b3552bce0 [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
dsinclair43554682016-09-29 17:29:48 -070014#ifndef FXJS_FXJS_V8_H_
15#define FXJS_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
dsinclaira52ab742016-09-29 13:59:29 -070024#include "core/fxcrt/fx_string.h"
Chris Palmere4b035b2017-03-26 15:48:34 -070025
weili54be7be2016-09-21 10:19:50 -070026#ifdef PDF_ENABLE_XFA
27// Header for CFXJSE_RuntimeData. FXJS_V8 doesn't interpret this class,
28// it is just passed along to XFA.
29#include "fxjs/cfxjse_runtimedata.h"
30#endif // PDF_ENABLE_XFA
Lei Zhangd88a3642015-11-10 09:38:57 -080031
tsepeza4941912016-08-15 11:40:12 -070032class CFXJS_Engine;
Lei Zhangd88a3642015-11-10 09:38:57 -080033class CFXJS_ObjDefinition;
Tom Sepezed7b2b52015-09-22 08:36:17 -070034
tsepeza4941912016-08-15 11:40:12 -070035// FXJS_V8 places no restrictions on this class; it merely passes it
Tom Sepez33420902015-10-13 15:00:10 -070036// on to caller-provided methods.
Tom Sepezd6ae2af2017-02-16 11:49:55 -080037class IJS_EventContext; // A description of the event that caused JS execution.
Tom Sepezed7b2b52015-09-22 08:36:17 -070038
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039enum FXJSOBJTYPE {
Tom Sepezcd56a7d2015-10-06 11:45:28 -070040 FXJSOBJTYPE_DYNAMIC = 0, // Created by native method and returned to JS.
41 FXJSOBJTYPE_STATIC, // Created by init and hung off of global object.
42 FXJSOBJTYPE_GLOBAL, // The global object itself (may only appear once).
John Abd-El-Malek5110c472014-05-17 22:33:34 -070043};
44
Nico Weber9d8ec5a2015-08-04 13:00:21 -070045struct FXJSErr {
46 const wchar_t* message;
47 const wchar_t* srcline;
48 unsigned linnum;
John Abd-El-Malek5110c472014-05-17 22:33:34 -070049};
50
jinming_wang61dc96f2016-01-28 14:39:56 +080051// Global weak map to save dynamic objects.
52class V8TemplateMapTraits : public v8::StdMapTraits<void*, v8::Object> {
53 public:
54 typedef v8::GlobalValueMap<void*, v8::Object, V8TemplateMapTraits> MapType;
55 typedef void WeakCallbackDataType;
56
57 static WeakCallbackDataType* WeakCallbackParameter(
58 MapType* map,
59 void* key,
60 const v8::Local<v8::Object>& value) {
61 return key;
62 }
63 static MapType* MapFromWeakCallbackInfo(
64 const v8::WeakCallbackInfo<WeakCallbackDataType>&);
65
66 static void* KeyFromWeakCallbackInfo(
67 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
68 return data.GetParameter();
69 }
70 static const v8::PersistentContainerCallbackType kCallbackType =
71 v8::kWeakWithInternalFields;
72 static void DisposeWeak(
73 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {}
74 static void OnWeakCallback(
75 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {}
76 static void Dispose(v8::Isolate* isolate,
77 v8::Global<v8::Object> value,
78 void* key);
79 static void DisposeCallbackData(WeakCallbackDataType* callbackData) {}
80};
81
82class V8TemplateMap {
83 public:
84 typedef v8::GlobalValueMap<void*, v8::Object, V8TemplateMapTraits> MapType;
85
weili625ad662016-06-15 11:21:33 -070086 explicit V8TemplateMap(v8::Isolate* isolate);
87 ~V8TemplateMap();
88
89 void set(void* key, v8::Local<v8::Object> handle);
90
jinming_wang61dc96f2016-01-28 14:39:56 +080091 friend class V8TemplateMapTraits;
92
93 private:
94 MapType m_map;
95};
96
Tom Sepezed7b2b52015-09-22 08:36:17 -070097class FXJS_PerIsolateData {
98 public:
weili625ad662016-06-15 11:21:33 -070099 ~FXJS_PerIsolateData();
100
Tom Sepezed7b2b52015-09-22 08:36:17 -0700101 static void SetUp(v8::Isolate* pIsolate);
102 static FXJS_PerIsolateData* Get(v8::Isolate* pIsolate);
weili625ad662016-06-15 11:21:33 -0700103
weilia4ad5952016-09-22 10:38:53 -0700104 std::vector<std::unique_ptr<CFXJS_ObjDefinition>> m_ObjectDefnArray;
Tom Sepez51da0932015-11-25 16:05:49 -0800105#ifdef PDF_ENABLE_XFA
weili54be7be2016-09-21 10:19:50 -0700106 std::unique_ptr<CFXJSE_RuntimeData> m_pFXJSERuntimeData;
Tom Sepez40e9ff32015-11-30 12:39:54 -0800107#endif // PDF_ENABLE_XFA
weilia4ad5952016-09-22 10:38:53 -0700108 std::unique_ptr<V8TemplateMap> m_pDynamicObjsMap;
Tom Sepezed7b2b52015-09-22 08:36:17 -0700109
110 protected:
weilia4ad5952016-09-22 10:38:53 -0700111 explicit FXJS_PerIsolateData(v8::Isolate* pIsolate);
Tom Sepezed7b2b52015-09-22 08:36:17 -0700112};
113
Tom Sepez39bfe122015-09-17 15:25:23 -0700114class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
Tom Sepez972eb5c2017-03-15 15:24:57 -0700115 static const size_t kMaxAllowedBytes = 0x10000000;
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700116 void* Allocate(size_t length) override;
117 void* AllocateUninitialized(size_t length) override;
118 void Free(void* data, size_t length) override;
119};
120
Tom Sepeza72e8e22015-10-07 10:17:53 -0700121void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate);
Tom Sepez39bfe122015-09-17 15:25:23 -0700122void FXJS_Release();
123
Tom Sepeza72e8e22015-10-07 10:17:53 -0700124// Gets the global isolate set by FXJS_Initialize(), or makes a new one each
125// time if there is no such isolate. Returns true if a new isolate had to be
126// created.
127bool FXJS_GetIsolate(v8::Isolate** pResultIsolate);
128
Lei Zhang3fa115b2015-10-08 12:04:47 -0700129// Get the global isolate's ref count.
130size_t FXJS_GlobalIsolateRefCount();
131
tsepezb4694242016-08-15 16:44:55 -0700132class CFXJS_Engine {
133 public:
weili0b2a9872016-09-21 11:50:43 -0700134 explicit CFXJS_Engine(v8::Isolate* pIsolate);
tsepezb4694242016-08-15 16:44:55 -0700135 ~CFXJS_Engine();
Tom Sepez142165e2015-09-11 13:21:50 -0700136
tsepezb4694242016-08-15 16:44:55 -0700137 using Constructor = void (*)(CFXJS_Engine* pEngine,
138 v8::Local<v8::Object> obj);
139 using Destructor = void (*)(CFXJS_Engine* pEngine, v8::Local<v8::Object> obj);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700140
tsepezb4694242016-08-15 16:44:55 -0700141 static CFXJS_Engine* CurrentEngineFromIsolate(v8::Isolate* pIsolate);
142 static int GetObjDefnID(v8::Local<v8::Object> pObj);
Tom Sepezb8ec0a32015-11-20 14:23:02 -0800143
tsepezb4694242016-08-15 16:44:55 -0700144 v8::Isolate* GetIsolate() const { return m_isolate; }
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700145
tsepezb4694242016-08-15 16:44:55 -0700146 // Always returns a valid, newly-created objDefnID.
Tom Sepez892d7512017-02-21 13:57:13 -0800147 int DefineObj(const char* sObjName,
tsepezb4694242016-08-15 16:44:55 -0700148 FXJSOBJTYPE eObjType,
149 Constructor pConstructor,
150 Destructor pDestructor);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700151
tsepezb4694242016-08-15 16:44:55 -0700152 void DefineObjMethod(int nObjDefnID,
Tom Sepez9b99b632017-02-21 15:05:57 -0800153 const char* sMethodName,
tsepezb4694242016-08-15 16:44:55 -0700154 v8::FunctionCallback pMethodCall);
155 void DefineObjProperty(int nObjDefnID,
Tom Sepez4d5b8c52017-02-21 15:17:07 -0800156 const char* sPropName,
tsepezb4694242016-08-15 16:44:55 -0700157 v8::AccessorGetterCallback pPropGet,
158 v8::AccessorSetterCallback pPropPut);
159 void DefineObjAllProperties(int nObjDefnID,
160 v8::NamedPropertyQueryCallback pPropQurey,
161 v8::NamedPropertyGetterCallback pPropGet,
162 v8::NamedPropertySetterCallback pPropPut,
163 v8::NamedPropertyDeleterCallback pPropDel);
164 void DefineObjConst(int nObjDefnID,
Tom Sepez55db0912017-02-21 15:26:52 -0800165 const char* sConstName,
tsepezb4694242016-08-15 16:44:55 -0700166 v8::Local<v8::Value> pDefault);
Tom Sepez9b99b632017-02-21 15:05:57 -0800167 void DefineGlobalMethod(const char* sMethodName,
tsepezb4694242016-08-15 16:44:55 -0700168 v8::FunctionCallback pMethodCall);
169 void DefineGlobalConst(const wchar_t* sConstName,
170 v8::FunctionCallback pConstGetter);
Tom Sepez39bfe122015-09-17 15:25:23 -0700171
tsepezb4694242016-08-15 16:44:55 -0700172 // Called after FXJS_Define* calls made.
173 void InitializeEngine();
174 void ReleaseEngine();
tsepezd0b6ed12016-08-11 19:50:57 -0700175
tsepezb4694242016-08-15 16:44:55 -0700176 // Called after FXJS_InitializeEngine call made.
Ryan Harrison275e2602017-09-18 14:23:18 -0400177 int Execute(const WideString& script, FXJSErr* perror);
tsepezd0b6ed12016-08-11 19:50:57 -0700178
tsepezb4694242016-08-15 16:44:55 -0700179 v8::Local<v8::Context> NewLocalContext();
180 v8::Local<v8::Context> GetPersistentContext();
tsepeze6cf0132017-01-18 14:38:18 -0800181 v8::Local<v8::Object> GetThisObj();
Tom Sepez39bfe122015-09-17 15:25:23 -0700182
tsepezb4694242016-08-15 16:44:55 -0700183 v8::Local<v8::Value> NewNull();
184 v8::Local<v8::Array> NewArray();
Tom Sepezf6ca07b2017-06-01 09:17:18 -0700185 v8::Local<v8::Number> NewNumber(int number);
186 v8::Local<v8::Number> NewNumber(double number);
187 v8::Local<v8::Number> NewNumber(float number);
188 v8::Local<v8::Boolean> NewBoolean(bool b);
Ryan Harrison275e2602017-09-18 14:23:18 -0400189 v8::Local<v8::String> NewString(const ByteStringView& str);
190 v8::Local<v8::String> NewString(const WideStringView& str);
tsepezb4694242016-08-15 16:44:55 -0700191 v8::Local<v8::Date> NewDate(double d);
192 v8::Local<v8::Object> NewFxDynamicObj(int nObjDefnID, bool bStatic = false);
Tom Sepez39bfe122015-09-17 15:25:23 -0700193
tsepezb4694242016-08-15 16:44:55 -0700194 int ToInt32(v8::Local<v8::Value> pValue);
195 bool ToBoolean(v8::Local<v8::Value> pValue);
tsepeze6cf0132017-01-18 14:38:18 -0800196 double ToDouble(v8::Local<v8::Value> pValue);
Ryan Harrison275e2602017-09-18 14:23:18 -0400197 WideString ToWideString(v8::Local<v8::Value> pValue);
tsepezb4694242016-08-15 16:44:55 -0700198 v8::Local<v8::Object> ToObject(v8::Local<v8::Value> pValue);
199 v8::Local<v8::Array> ToArray(v8::Local<v8::Value> pValue);
Tom Sepez39bfe122015-09-17 15:25:23 -0700200
tsepeze6cf0132017-01-18 14:38:18 -0800201 // Arrays.
tsepezb4694242016-08-15 16:44:55 -0700202 unsigned GetArrayLength(v8::Local<v8::Array> pArray);
203 v8::Local<v8::Value> GetArrayElement(v8::Local<v8::Array> pArray,
204 unsigned index);
205 unsigned PutArrayElement(v8::Local<v8::Array> pArray,
206 unsigned index,
207 v8::Local<v8::Value> pValue);
208
tsepeze6cf0132017-01-18 14:38:18 -0800209 // Objects.
Ryan Harrison275e2602017-09-18 14:23:18 -0400210 std::vector<WideString> GetObjectPropertyNames(v8::Local<v8::Object> pObj);
tsepezb4694242016-08-15 16:44:55 -0700211 v8::Local<v8::Value> GetObjectProperty(v8::Local<v8::Object> pObj,
Ryan Harrison275e2602017-09-18 14:23:18 -0400212 const WideString& PropertyName);
tsepeze6cf0132017-01-18 14:38:18 -0800213 void PutObjectProperty(v8::Local<v8::Object> pObj,
Ryan Harrison275e2602017-09-18 14:23:18 -0400214 const WideString& PropertyName,
tsepeze6cf0132017-01-18 14:38:18 -0800215 v8::Local<v8::Value> pValue);
tsepezb4694242016-08-15 16:44:55 -0700216
217 // Native object binding.
218 void SetObjectPrivate(v8::Local<v8::Object> pObj, void* p);
219 void* GetObjectPrivate(v8::Local<v8::Object> pObj);
220 static void FreeObjectPrivate(void* p);
221 static void FreeObjectPrivate(v8::Local<v8::Object> pObj);
222
Ryan Harrison275e2602017-09-18 14:23:18 -0400223 void SetConstArray(const WideString& name, v8::Local<v8::Array> array);
224 v8::Local<v8::Array> GetConstArray(const WideString& name);
tsepezb4694242016-08-15 16:44:55 -0700225
Ryan Harrison275e2602017-09-18 14:23:18 -0400226 void Error(const WideString& message);
tsepezb4694242016-08-15 16:44:55 -0700227
weili0b2a9872016-09-21 11:50:43 -0700228 protected:
229 CFXJS_Engine();
230
231 void SetIsolate(v8::Isolate* pIsolate) { m_isolate = pIsolate; }
232
tsepezb4694242016-08-15 16:44:55 -0700233 private:
234 v8::Isolate* m_isolate;
235 v8::Global<v8::Context> m_V8PersistentContext;
236 std::vector<v8::Global<v8::Object>*> m_StaticObjects;
Ryan Harrison275e2602017-09-18 14:23:18 -0400237 std::map<WideString, v8::Global<v8::Array>> m_ConstArrays;
tsepezb4694242016-08-15 16:44:55 -0700238};
239
dsinclair43554682016-09-29 17:29:48 -0700240#endif // FXJS_FXJS_V8_H_