John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 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. |
Tom Sepez | 2311b78 | 2015-02-23 10:22:51 -0800 | [diff] [blame] | 4 | |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Tom Sepez | ba038bc | 2015-10-08 12:03:00 -0700 | [diff] [blame] | 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. |
Tom Sepez | 9a3f812 | 2015-04-07 15:35:48 -0700 | [diff] [blame] | 13 | |
dsinclair | 4355468 | 2016-09-29 17:29:48 -0700 | [diff] [blame] | 14 | #ifndef FXJS_FXJS_V8_H_ |
| 15 | #define FXJS_FXJS_V8_H_ |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 16 | |
jinming_wang | 61dc96f | 2016-01-28 14:39:56 +0800 | [diff] [blame] | 17 | #include <v8-util.h> |
Dan Sinclair | 61046b9 | 2016-02-18 14:48:48 -0500 | [diff] [blame] | 18 | #include <v8.h> |
Lei Zhang | a688a04 | 2015-11-09 13:57:49 -0800 | [diff] [blame] | 19 | |
tsepez | a494191 | 2016-08-15 11:40:12 -0700 | [diff] [blame] | 20 | #include <map> |
weili | 54be7be | 2016-09-21 10:19:50 -0700 | [diff] [blame] | 21 | #include <memory> |
Lei Zhang | d88a364 | 2015-11-10 09:38:57 -0800 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
dsinclair | a52ab74 | 2016-09-29 13:59:29 -0700 | [diff] [blame] | 24 | #include "core/fxcrt/fx_string.h" |
Chris Palmer | e4b035b | 2017-03-26 15:48:34 -0700 | [diff] [blame] | 25 | |
weili | 54be7be | 2016-09-21 10:19:50 -0700 | [diff] [blame] | 26 | #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 Zhang | d88a364 | 2015-11-10 09:38:57 -0800 | [diff] [blame] | 31 | |
tsepez | a494191 | 2016-08-15 11:40:12 -0700 | [diff] [blame] | 32 | class CFXJS_Engine; |
Lei Zhang | d88a364 | 2015-11-10 09:38:57 -0800 | [diff] [blame] | 33 | class CFXJS_ObjDefinition; |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 34 | |
tsepez | a494191 | 2016-08-15 11:40:12 -0700 | [diff] [blame] | 35 | // FXJS_V8 places no restrictions on this class; it merely passes it |
Tom Sepez | 3342090 | 2015-10-13 15:00:10 -0700 | [diff] [blame] | 36 | // on to caller-provided methods. |
Tom Sepez | d6ae2af | 2017-02-16 11:49:55 -0800 | [diff] [blame] | 37 | class IJS_EventContext; // A description of the event that caused JS execution. |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 38 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 39 | enum FXJSOBJTYPE { |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 40 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 45 | struct FXJSErr { |
| 46 | const wchar_t* message; |
| 47 | const wchar_t* srcline; |
| 48 | unsigned linnum; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
jinming_wang | 61dc96f | 2016-01-28 14:39:56 +0800 | [diff] [blame] | 51 | // Global weak map to save dynamic objects. |
| 52 | class 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 | |
| 82 | class V8TemplateMap { |
| 83 | public: |
| 84 | typedef v8::GlobalValueMap<void*, v8::Object, V8TemplateMapTraits> MapType; |
| 85 | |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 86 | explicit V8TemplateMap(v8::Isolate* isolate); |
| 87 | ~V8TemplateMap(); |
| 88 | |
| 89 | void set(void* key, v8::Local<v8::Object> handle); |
| 90 | |
jinming_wang | 61dc96f | 2016-01-28 14:39:56 +0800 | [diff] [blame] | 91 | friend class V8TemplateMapTraits; |
| 92 | |
| 93 | private: |
| 94 | MapType m_map; |
| 95 | }; |
| 96 | |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 97 | class FXJS_PerIsolateData { |
| 98 | public: |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 99 | ~FXJS_PerIsolateData(); |
| 100 | |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 101 | static void SetUp(v8::Isolate* pIsolate); |
| 102 | static FXJS_PerIsolateData* Get(v8::Isolate* pIsolate); |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 103 | |
weili | a4ad595 | 2016-09-22 10:38:53 -0700 | [diff] [blame] | 104 | std::vector<std::unique_ptr<CFXJS_ObjDefinition>> m_ObjectDefnArray; |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 105 | #ifdef PDF_ENABLE_XFA |
weili | 54be7be | 2016-09-21 10:19:50 -0700 | [diff] [blame] | 106 | std::unique_ptr<CFXJSE_RuntimeData> m_pFXJSERuntimeData; |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 107 | #endif // PDF_ENABLE_XFA |
weili | a4ad595 | 2016-09-22 10:38:53 -0700 | [diff] [blame] | 108 | std::unique_ptr<V8TemplateMap> m_pDynamicObjsMap; |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 109 | |
| 110 | protected: |
weili | a4ad595 | 2016-09-22 10:38:53 -0700 | [diff] [blame] | 111 | explicit FXJS_PerIsolateData(v8::Isolate* pIsolate); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 114 | class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { |
Tom Sepez | 972eb5c | 2017-03-15 15:24:57 -0700 | [diff] [blame] | 115 | static const size_t kMaxAllowedBytes = 0x10000000; |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 116 | 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 Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 121 | void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 122 | void FXJS_Release(); |
| 123 | |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 124 | // 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. |
| 127 | bool FXJS_GetIsolate(v8::Isolate** pResultIsolate); |
| 128 | |
Lei Zhang | 3fa115b | 2015-10-08 12:04:47 -0700 | [diff] [blame] | 129 | // Get the global isolate's ref count. |
| 130 | size_t FXJS_GlobalIsolateRefCount(); |
| 131 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 132 | class CFXJS_Engine { |
| 133 | public: |
weili | 0b2a987 | 2016-09-21 11:50:43 -0700 | [diff] [blame] | 134 | explicit CFXJS_Engine(v8::Isolate* pIsolate); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 135 | ~CFXJS_Engine(); |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 136 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 137 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 140 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 141 | static CFXJS_Engine* CurrentEngineFromIsolate(v8::Isolate* pIsolate); |
| 142 | static int GetObjDefnID(v8::Local<v8::Object> pObj); |
Tom Sepez | b8ec0a3 | 2015-11-20 14:23:02 -0800 | [diff] [blame] | 143 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 144 | v8::Isolate* GetIsolate() const { return m_isolate; } |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 145 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 146 | // Always returns a valid, newly-created objDefnID. |
Tom Sepez | 892d751 | 2017-02-21 13:57:13 -0800 | [diff] [blame] | 147 | int DefineObj(const char* sObjName, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 148 | FXJSOBJTYPE eObjType, |
| 149 | Constructor pConstructor, |
| 150 | Destructor pDestructor); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 151 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 152 | void DefineObjMethod(int nObjDefnID, |
Tom Sepez | 9b99b63 | 2017-02-21 15:05:57 -0800 | [diff] [blame] | 153 | const char* sMethodName, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 154 | v8::FunctionCallback pMethodCall); |
| 155 | void DefineObjProperty(int nObjDefnID, |
Tom Sepez | 4d5b8c5 | 2017-02-21 15:17:07 -0800 | [diff] [blame] | 156 | const char* sPropName, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 157 | 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 Sepez | 55db091 | 2017-02-21 15:26:52 -0800 | [diff] [blame] | 165 | const char* sConstName, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 166 | v8::Local<v8::Value> pDefault); |
Tom Sepez | 9b99b63 | 2017-02-21 15:05:57 -0800 | [diff] [blame] | 167 | void DefineGlobalMethod(const char* sMethodName, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 168 | v8::FunctionCallback pMethodCall); |
| 169 | void DefineGlobalConst(const wchar_t* sConstName, |
| 170 | v8::FunctionCallback pConstGetter); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 171 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 172 | // Called after FXJS_Define* calls made. |
| 173 | void InitializeEngine(); |
| 174 | void ReleaseEngine(); |
tsepez | d0b6ed1 | 2016-08-11 19:50:57 -0700 | [diff] [blame] | 175 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 176 | // Called after FXJS_InitializeEngine call made. |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame^] | 177 | int Execute(const WideString& script, FXJSErr* perror); |
tsepez | d0b6ed1 | 2016-08-11 19:50:57 -0700 | [diff] [blame] | 178 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 179 | v8::Local<v8::Context> NewLocalContext(); |
| 180 | v8::Local<v8::Context> GetPersistentContext(); |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 181 | v8::Local<v8::Object> GetThisObj(); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 182 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 183 | v8::Local<v8::Value> NewNull(); |
| 184 | v8::Local<v8::Array> NewArray(); |
Tom Sepez | f6ca07b | 2017-06-01 09:17:18 -0700 | [diff] [blame] | 185 | 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 Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame^] | 189 | v8::Local<v8::String> NewString(const ByteStringView& str); |
| 190 | v8::Local<v8::String> NewString(const WideStringView& str); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 191 | v8::Local<v8::Date> NewDate(double d); |
| 192 | v8::Local<v8::Object> NewFxDynamicObj(int nObjDefnID, bool bStatic = false); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 193 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 194 | int ToInt32(v8::Local<v8::Value> pValue); |
| 195 | bool ToBoolean(v8::Local<v8::Value> pValue); |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 196 | double ToDouble(v8::Local<v8::Value> pValue); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame^] | 197 | WideString ToWideString(v8::Local<v8::Value> pValue); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 198 | v8::Local<v8::Object> ToObject(v8::Local<v8::Value> pValue); |
| 199 | v8::Local<v8::Array> ToArray(v8::Local<v8::Value> pValue); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 200 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 201 | // Arrays. |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 202 | 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 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 209 | // Objects. |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame^] | 210 | std::vector<WideString> GetObjectPropertyNames(v8::Local<v8::Object> pObj); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 211 | v8::Local<v8::Value> GetObjectProperty(v8::Local<v8::Object> pObj, |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame^] | 212 | const WideString& PropertyName); |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 213 | void PutObjectProperty(v8::Local<v8::Object> pObj, |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame^] | 214 | const WideString& PropertyName, |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 215 | v8::Local<v8::Value> pValue); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 216 | |
| 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 Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame^] | 223 | void SetConstArray(const WideString& name, v8::Local<v8::Array> array); |
| 224 | v8::Local<v8::Array> GetConstArray(const WideString& name); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 225 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame^] | 226 | void Error(const WideString& message); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 227 | |
weili | 0b2a987 | 2016-09-21 11:50:43 -0700 | [diff] [blame] | 228 | protected: |
| 229 | CFXJS_Engine(); |
| 230 | |
| 231 | void SetIsolate(v8::Isolate* pIsolate) { m_isolate = pIsolate; } |
| 232 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 233 | private: |
| 234 | v8::Isolate* m_isolate; |
| 235 | v8::Global<v8::Context> m_V8PersistentContext; |
| 236 | std::vector<v8::Global<v8::Object>*> m_StaticObjects; |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame^] | 237 | std::map<WideString, v8::Global<v8::Array>> m_ConstArrays; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 238 | }; |
| 239 | |
dsinclair | 4355468 | 2016-09-29 17:29:48 -0700 | [diff] [blame] | 240 | #endif // FXJS_FXJS_V8_H_ |