• 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 // CFXJS_ENGINE is a layer that makes it easier to define native objects in V8,
8 // but has no knowledge of PDF-specific native objects. It could in theory be
9 // used 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_CFXJS_ENGINE_H_
15 #define FXJS_CFXJS_ENGINE_H_
16 
17 #include <functional>
18 #include <map>
19 #include <memory>
20 #include <vector>
21 
22 #include "core/fxcrt/fx_string.h"
23 #include "fxjs/cfx_v8.h"
24 #include "fxjs/ijs_runtime.h"
25 #include "v8/include/v8.h"
26 
27 class CFXJS_ObjDefinition;
28 class CJS_Object;
29 class V8TemplateMap;
30 
31 // CFXJS_ENGINE places no restrictions on this class; it merely passes it
32 // on to caller-provided methods.
33 class IJS_EventContext;  // A description of the event that caused JS execution.
34 
35 enum FXJSOBJTYPE {
36   FXJSOBJTYPE_DYNAMIC = 0,  // Created by native method and returned to JS.
37   FXJSOBJTYPE_STATIC,       // Created by init and hung off of global object.
38   FXJSOBJTYPE_GLOBAL,       // The global object itself (may only appear once).
39 };
40 
41 class FXJS_PerIsolateData {
42  public:
43   // Hook for XFA's data, when present.
44   class ExtensionIface {
45    public:
46     virtual ~ExtensionIface() = default;
47   };
48 
49   ~FXJS_PerIsolateData();
50 
51   static void SetUp(v8::Isolate* pIsolate);
52   static FXJS_PerIsolateData* Get(v8::Isolate* pIsolate);
53 
54   int MaxObjDefinitionID() const;
55   CFXJS_ObjDefinition* ObjDefinitionForID(int id) const;
56   int AssignIDForObjDefinition(std::unique_ptr<CFXJS_ObjDefinition> pDefn);
57 
58   std::vector<std::unique_ptr<CFXJS_ObjDefinition>> m_ObjectDefnArray;
59   std::unique_ptr<V8TemplateMap> m_pDynamicObjsMap;
60   std::unique_ptr<ExtensionIface> m_pFXJSERuntimeData;
61 
62  protected:
63   explicit FXJS_PerIsolateData(v8::Isolate* pIsolate);
64 };
65 
66 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate);
67 void FXJS_Release();
68 
69 // Gets the global isolate set by FXJS_Initialize(), or makes a new one each
70 // time if there is no such isolate. Returns true if a new isolate had to be
71 // created.
72 bool FXJS_GetIsolate(v8::Isolate** pResultIsolate);
73 
74 // Get the global isolate's ref count.
75 size_t FXJS_GlobalIsolateRefCount();
76 
77 class CFXJS_Engine : public CFX_V8 {
78  public:
79   explicit CFXJS_Engine(v8::Isolate* pIsolate);
80   ~CFXJS_Engine() override;
81 
82   using Constructor =
83       std::function<void(CFXJS_Engine* pEngine, v8::Local<v8::Object> obj)>;
84   using Destructor = std::function<void(v8::Local<v8::Object> obj)>;
85 
86   static int GetObjDefnID(v8::Local<v8::Object> pObj);
87   static CJS_Object* GetObjectPrivate(v8::Local<v8::Object> pObj);
88   static void SetObjectPrivate(v8::Local<v8::Object> pObj,
89                                std::unique_ptr<CJS_Object> p);
90   static void FreeObjectPrivate(v8::Local<v8::Object> pObj);
91 
92   // Always returns a valid, newly-created objDefnID.
93   int DefineObj(const char* sObjName,
94                 FXJSOBJTYPE eObjType,
95                 Constructor pConstructor,
96                 Destructor pDestructor);
97 
98   void DefineObjMethod(int nObjDefnID,
99                        const char* sMethodName,
100                        v8::FunctionCallback pMethodCall);
101   void DefineObjProperty(int nObjDefnID,
102                          const char* sPropName,
103                          v8::AccessorGetterCallback pPropGet,
104                          v8::AccessorSetterCallback pPropPut);
105   void DefineObjAllProperties(int nObjDefnID,
106                               v8::GenericNamedPropertyQueryCallback pPropQurey,
107                               v8::GenericNamedPropertyGetterCallback pPropGet,
108                               v8::GenericNamedPropertySetterCallback pPropPut,
109                               v8::GenericNamedPropertyDeleterCallback pPropDel);
110   void DefineObjConst(int nObjDefnID,
111                       const char* sConstName,
112                       v8::Local<v8::Value> pDefault);
113   void DefineGlobalMethod(const char* sMethodName,
114                           v8::FunctionCallback pMethodCall);
115   void DefineGlobalConst(const wchar_t* sConstName,
116                          v8::FunctionCallback pConstGetter);
117 
118   // Called after FXJS_Define* calls made.
119   void InitializeEngine();
120   void ReleaseEngine();
121 
122   // Called after FXJS_InitializeEngine call made.
123   Optional<IJS_Runtime::JS_Error> Execute(const WideString& script);
124 
125   v8::Local<v8::Object> GetThisObj();
126   v8::Local<v8::Object> NewFXJSBoundObject(int nObjDefnID, FXJSOBJTYPE type);
127   void Error(const WideString& message);
128 
129   v8::Local<v8::Context> GetV8Context();
130 
131   v8::Local<v8::Array> GetConstArray(const WideString& name);
132   void SetConstArray(const WideString& name, v8::Local<v8::Array> array);
133 
134  protected:
135   CFXJS_Engine();
136 
137  private:
138   v8::Global<v8::Context> m_V8Context;
139   std::vector<v8::Global<v8::Object>> m_StaticObjects;
140   std::map<WideString, v8::Global<v8::Array>> m_ConstArrays;
141 };
142 
143 #endif  // FXJS_CFXJS_ENGINE_H_
144