• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FXJS_FXJS_V8_H_
15 #define FXJS_FXJS_V8_H_
16 
17 #include <v8-util.h>
18 #include <v8.h>
19 
20 #include <map>
21 #include <memory>
22 #include <vector>
23 
24 #include "core/fxcrt/fx_string.h"
25 #include "fxjs/cjs_v8.h"
26 
27 #ifdef PDF_ENABLE_XFA
28 // Header for CFXJSE_RuntimeData. FXJS_V8 doesn't interpret this class,
29 // it is just passed along to XFA.
30 #include "fxjs/cfxjse_runtimedata.h"
31 #endif  // PDF_ENABLE_XFA
32 
33 class CFXJS_Engine;
34 class CFXJS_ObjDefinition;
35 
36 // FXJS_V8 places no restrictions on this class; it merely passes it
37 // on to caller-provided methods.
38 class IJS_EventContext;  // A description of the event that caused JS execution.
39 
40 enum FXJSOBJTYPE {
41   FXJSOBJTYPE_DYNAMIC = 0,  // Created by native method and returned to JS.
42   FXJSOBJTYPE_STATIC,       // Created by init and hung off of global object.
43   FXJSOBJTYPE_GLOBAL,       // The global object itself (may only appear once).
44 };
45 
46 struct FXJSErr {
47   const wchar_t* message;
48   const wchar_t* srcline;
49   unsigned linnum;
50 };
51 
52 // Global weak map to save dynamic objects.
53 class V8TemplateMapTraits : public v8::StdMapTraits<void*, v8::Object> {
54  public:
55   typedef v8::GlobalValueMap<void*, v8::Object, V8TemplateMapTraits> MapType;
56   typedef void WeakCallbackDataType;
57 
58   static WeakCallbackDataType*
WeakCallbackParameter(MapType * map,void * key,v8::Local<v8::Object> value)59   WeakCallbackParameter(MapType* map, void* key, v8::Local<v8::Object> value) {
60     return key;
61   }
62   static MapType* MapFromWeakCallbackInfo(
63       const v8::WeakCallbackInfo<WeakCallbackDataType>&);
64 
KeyFromWeakCallbackInfo(const v8::WeakCallbackInfo<WeakCallbackDataType> & data)65   static void* KeyFromWeakCallbackInfo(
66       const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
67     return data.GetParameter();
68   }
69   static const v8::PersistentContainerCallbackType kCallbackType =
70       v8::kWeakWithInternalFields;
DisposeWeak(const v8::WeakCallbackInfo<WeakCallbackDataType> & data)71   static void DisposeWeak(
72       const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {}
OnWeakCallback(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);
DisposeCallbackData(WeakCallbackDataType * callbackData)78   static void DisposeCallbackData(WeakCallbackDataType* callbackData) {}
79 };
80 
81 class V8TemplateMap {
82  public:
83   typedef v8::GlobalValueMap<void*, v8::Object, V8TemplateMapTraits> MapType;
84 
85   explicit V8TemplateMap(v8::Isolate* isolate);
86   ~V8TemplateMap();
87 
88   void set(void* key, v8::Local<v8::Object> handle);
89 
90   friend class V8TemplateMapTraits;
91 
92  private:
93   MapType m_map;
94 };
95 
96 class FXJS_PerIsolateData {
97  public:
98   ~FXJS_PerIsolateData();
99 
100   static void SetUp(v8::Isolate* pIsolate);
101   static FXJS_PerIsolateData* Get(v8::Isolate* pIsolate);
102 
103   std::vector<std::unique_ptr<CFXJS_ObjDefinition>> m_ObjectDefnArray;
104 #ifdef PDF_ENABLE_XFA
105   std::unique_ptr<CFXJSE_RuntimeData> m_pFXJSERuntimeData;
106 #endif  // PDF_ENABLE_XFA
107   std::unique_ptr<V8TemplateMap> m_pDynamicObjsMap;
108 
109  protected:
110   explicit FXJS_PerIsolateData(v8::Isolate* pIsolate);
111 };
112 
113 class FXJS_ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
114   static const size_t kMaxAllowedBytes = 0x10000000;
115   void* Allocate(size_t length) override;
116   void* AllocateUninitialized(size_t length) override;
117   void Free(void* data, size_t length) override;
118 };
119 
120 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate);
121 void FXJS_Release();
122 
123 // Gets the global isolate set by FXJS_Initialize(), or makes a new one each
124 // time if there is no such isolate. Returns true if a new isolate had to be
125 // created.
126 bool FXJS_GetIsolate(v8::Isolate** pResultIsolate);
127 
128 // Get the global isolate's ref count.
129 size_t FXJS_GlobalIsolateRefCount();
130 
131 class CFXJS_Engine : public CJS_V8 {
132  public:
133   explicit CFXJS_Engine(v8::Isolate* pIsolate);
134   ~CFXJS_Engine() override;
135 
136   using Constructor = void (*)(CFXJS_Engine* pEngine,
137                                v8::Local<v8::Object> obj);
138   using Destructor = void (*)(CFXJS_Engine* pEngine, v8::Local<v8::Object> obj);
139 
140   static CFXJS_Engine* CurrentEngineFromIsolate(v8::Isolate* pIsolate);
141   static int GetObjDefnID(v8::Local<v8::Object> pObj);
142 
143   // Always returns a valid, newly-created objDefnID.
144   int DefineObj(const char* sObjName,
145                 FXJSOBJTYPE eObjType,
146                 Constructor pConstructor,
147                 Destructor pDestructor);
148 
149   void DefineObjMethod(int nObjDefnID,
150                        const char* sMethodName,
151                        v8::FunctionCallback pMethodCall);
152   void DefineObjProperty(int nObjDefnID,
153                          const char* sPropName,
154                          v8::AccessorGetterCallback pPropGet,
155                          v8::AccessorSetterCallback pPropPut);
156   void DefineObjAllProperties(int nObjDefnID,
157                               v8::NamedPropertyQueryCallback pPropQurey,
158                               v8::NamedPropertyGetterCallback pPropGet,
159                               v8::NamedPropertySetterCallback pPropPut,
160                               v8::NamedPropertyDeleterCallback pPropDel);
161   void DefineObjConst(int nObjDefnID,
162                       const char* sConstName,
163                       v8::Local<v8::Value> pDefault);
164   void DefineGlobalMethod(const char* sMethodName,
165                           v8::FunctionCallback pMethodCall);
166   void DefineGlobalConst(const wchar_t* sConstName,
167                          v8::FunctionCallback pConstGetter);
168 
169   // Called after FXJS_Define* calls made.
170   void InitializeEngine();
171   void ReleaseEngine();
172 
173   // Called after FXJS_InitializeEngine call made.
174   int Execute(const WideString& script, FXJSErr* perror);
175 
176   v8::Local<v8::Object> GetThisObj();
177 
178   v8::Local<v8::Object> NewFxDynamicObj(int nObjDefnID, bool bStatic = false);
179 
180   // Native object binding.
181   void SetObjectPrivate(v8::Local<v8::Object> pObj, void* p);
182   void* GetObjectPrivate(v8::Local<v8::Object> pObj);
183   static void FreeObjectPrivate(void* p);
184   static void FreeObjectPrivate(v8::Local<v8::Object> pObj);
185 
186   void Error(const WideString& message);
187 
188  protected:
189   CFXJS_Engine();
190 
191  private:
192   std::vector<v8::Global<v8::Object>*> m_StaticObjects;
193 };
194 
195 #endif  // FXJS_FXJS_V8_H_
196