• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The PDFium Authors
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_XFA_CFXJSE_CONTEXT_H_
8 #define FXJS_XFA_CFXJSE_CONTEXT_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "core/fxcrt/bytestring.h"
14 #include "core/fxcrt/unowned_ptr.h"
15 #include "v8/include/cppgc/persistent.h"
16 #include "v8/include/v8-forward.h"
17 #include "v8/include/v8-persistent-handle.h"
18 
19 class CFXJSE_Class;
20 class CFXJSE_HostObject;
21 class CFXJSE_Value;
22 class CXFA_ThisProxy;
23 struct FXJSE_CLASS_DESCRIPTOR;
24 
25 class CFXJSE_Context {
26  public:
27   static std::unique_ptr<CFXJSE_Context> Create(
28       v8::Isolate* pIsolate,
29       const FXJSE_CLASS_DESCRIPTOR* pGlobalClass,
30       CFXJSE_HostObject* pGlobalObject,
31       CXFA_ThisProxy* pProxy);
32 
33   ~CFXJSE_Context();
34 
GetIsolate()35   v8::Isolate* GetIsolate() const { return m_pIsolate; }
36   v8::Local<v8::Context> GetContext();
37   v8::Local<v8::Object> GetGlobalObject();
38 
39   void AddClass(std::unique_ptr<CFXJSE_Class> pClass);
40   CFXJSE_Class* GetClassByName(ByteStringView szName) const;
41   void EnableCompatibleMode();
42 
43   // Note: `pNewThisObject` may be empty.
44   bool ExecuteScript(ByteStringView bsScript,
45                      CFXJSE_Value* pRetValue,
46                      v8::Local<v8::Object> pNewThisObject);
47 
48  private:
49   CFXJSE_Context(v8::Isolate* pIsolate, CXFA_ThisProxy* pProxy);
50   CFXJSE_Context(const CFXJSE_Context&) = delete;
51   CFXJSE_Context& operator=(const CFXJSE_Context&) = delete;
52 
53   v8::Global<v8::Context> m_hContext;
54   UnownedPtr<v8::Isolate> m_pIsolate;
55   std::vector<std::unique_ptr<CFXJSE_Class>> m_rgClasses;
56   cppgc::Persistent<CXFA_ThisProxy> m_pProxy;
57 };
58 
59 void FXJSE_UpdateObjectBinding(v8::Local<v8::Object> hObject,
60                                CFXJSE_HostObject* pNewBinding);
61 
62 void FXJSE_ClearObjectBinding(v8::Local<v8::Object> hJSObject);
63 CFXJSE_HostObject* FXJSE_RetrieveObjectBinding(v8::Local<v8::Value> hValue);
64 
65 #endif  // FXJS_XFA_CFXJSE_CONTEXT_H_
66