• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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 #ifndef FXJS_CJS_V8_H_
8 #define FXJS_CJS_V8_H_
9 
10 #include <v8-util.h>
11 #include <v8.h>
12 
13 #include <map>
14 #include <vector>
15 
16 #include "core/fxcrt/bytestring.h"
17 #include "core/fxcrt/fx_string.h"
18 #include "core/fxcrt/widestring.h"
19 
20 #ifdef PDF_ENABLE_XFA
21 class CXFA_Object;
22 #endif  // PDF_ENABLE_XFA
23 
24 class CJS_V8 {
25  public:
26   explicit CJS_V8(v8::Isolate* pIsolate);
27   virtual ~CJS_V8();
28 
GetIsolate()29   v8::Isolate* GetIsolate() const { return m_isolate; }
30 
31   v8::Local<v8::Context> NewLocalContext();
32   v8::Local<v8::Context> GetPersistentContext();
33 
34   v8::Local<v8::Value> NewNull();
35   v8::Local<v8::Value> NewUndefined();
36   v8::Local<v8::Array> NewArray();
37   v8::Local<v8::Number> NewNumber(int number);
38   v8::Local<v8::Number> NewNumber(double number);
39   v8::Local<v8::Number> NewNumber(float number);
40   v8::Local<v8::Boolean> NewBoolean(bool b);
41   v8::Local<v8::String> NewString(const ByteStringView& str);
42   v8::Local<v8::String> NewString(const WideStringView& str);
43   v8::Local<v8::Date> NewDate(double d);
44 
45   int ToInt32(v8::Local<v8::Value> pValue);
46   bool ToBoolean(v8::Local<v8::Value> pValue);
47   double ToDouble(v8::Local<v8::Value> pValue);
48   WideString ToWideString(v8::Local<v8::Value> pValue);
49   ByteString ToByteString(v8::Local<v8::Value> pValue);
50   v8::Local<v8::Object> ToObject(v8::Local<v8::Value> pValue);
51   v8::Local<v8::Array> ToArray(v8::Local<v8::Value> pValue);
52 
53 #ifdef PDF_ENABLE_XFA
54   CXFA_Object* ToXFAObject(v8::Local<v8::Value> obj);
55   v8::Local<v8::Value> NewXFAObject(CXFA_Object* obj,
56                                     v8::Global<v8::FunctionTemplate>& tmpl);
57 #endif  // PDF_ENABLE_XFA
58 
59   // Arrays.
60   unsigned GetArrayLength(v8::Local<v8::Array> pArray);
61   v8::Local<v8::Value> GetArrayElement(v8::Local<v8::Array> pArray,
62                                        unsigned index);
63   unsigned PutArrayElement(v8::Local<v8::Array> pArray,
64                            unsigned index,
65                            v8::Local<v8::Value> pValue);
66 
67   void SetConstArray(const WideString& name, v8::Local<v8::Array> array);
68   v8::Local<v8::Array> GetConstArray(const WideString& name);
69 
70   // Objects.
71   std::vector<WideString> GetObjectPropertyNames(v8::Local<v8::Object> pObj);
72   v8::Local<v8::Value> GetObjectProperty(v8::Local<v8::Object> pObj,
73                                          const WideString& PropertyName);
74   void PutObjectProperty(v8::Local<v8::Object> pObj,
75                          const WideString& PropertyName,
76                          v8::Local<v8::Value> pValue);
77 
78  protected:
SetIsolate(v8::Isolate * pIsolate)79   void SetIsolate(v8::Isolate* pIsolate) { m_isolate = pIsolate; }
ClearConstArray()80   void ClearConstArray() { m_ConstArrays.clear(); }
81 
ResetPersistentContext(v8::Local<v8::Context> context)82   void ResetPersistentContext(v8::Local<v8::Context> context) {
83     m_V8PersistentContext.Reset(m_isolate, context);
84   }
ReleasePersistentContext()85   void ReleasePersistentContext() { m_V8PersistentContext.Reset(); }
86 
87  private:
88   v8::Isolate* m_isolate;
89   std::map<WideString, v8::Global<v8::Array>> m_ConstArrays;
90   v8::Global<v8::Context> m_V8PersistentContext;
91 };
92 
93 #endif  // FXJS_CJS_V8_H_
94