xref: /aosp_15_r20/external/pdfium/fxjs/ijs_runtime.h (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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_IJS_RUNTIME_H_
8 #define FXJS_IJS_RUNTIME_H_
9 
10 #include <memory>
11 
12 #include "core/fxcrt/fx_memory.h"
13 #include "core/fxcrt/unowned_ptr.h"
14 #include "core/fxcrt/widestring.h"
15 #include "third_party/abseil-cpp/absl/types/optional.h"
16 
17 class CJS_Runtime;
18 class CPDFSDK_FormFillEnvironment;
19 class IJS_EventContext;
20 
21 // Owns the FJXS objects needed to actually execute JS, if possible. This
22 // virtual interface is backed by either an actual JS runtime, or a stub,
23 // when JS is not present.
24 class IJS_Runtime {
25  public:
26   struct JS_Error {
27     int line;
28     int column;
29     WideString exception;
30 
31     JS_Error(int line, int column, const WideString& exception);
32   };
33 
34   class ScopedEventContext {
35    public:
36     FX_STACK_ALLOCATED();
37 
38     explicit ScopedEventContext(IJS_Runtime* pRuntime);
39     ~ScopedEventContext();
40 
Get()41     IJS_EventContext* Get() const { return m_pContext; }
42     IJS_EventContext* operator->() const { return m_pContext; }
43 
44    private:
45     UnownedPtr<IJS_Runtime> const m_pRuntime;
46     UnownedPtr<IJS_EventContext> m_pContext;
47   };
48 
49   static void Initialize(unsigned int slot, void* isolate, void* platform);
50   static void Destroy();
51   static std::unique_ptr<IJS_Runtime> Create(
52       CPDFSDK_FormFillEnvironment* pFormFillEnv);
53 
54   virtual ~IJS_Runtime();
55 
56   virtual CJS_Runtime* AsCJSRuntime() = 0;
57   virtual IJS_EventContext* NewEventContext() = 0;
58   virtual void ReleaseEventContext(IJS_EventContext* pContext) = 0;
59   virtual CPDFSDK_FormFillEnvironment* GetFormFillEnv() const = 0;
60   virtual absl::optional<JS_Error> ExecuteScript(const WideString& script) = 0;
61 
62  protected:
63   IJS_Runtime() = default;
64 };
65 
66 #endif  // FXJS_IJS_RUNTIME_H_
67