1 // Copyright 2016 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_IJS_RUNTIME_H_
8 #define FXJS_IJS_RUNTIME_H_
9 
10 #include <memory>
11 
12 #include "core/fxcrt/fx_string.h"
13 #include "core/fxcrt/fx_system.h"
14 #include "core/fxcrt/unowned_ptr.h"
15 #include "third_party/base/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     explicit ScopedEventContext(IJS_Runtime* pRuntime);
37     ~ScopedEventContext();
38 
Get()39     IJS_EventContext* Get() const { return m_pContext.Get(); }
40     IJS_EventContext* operator->() const { return m_pContext.Get(); }
41 
42    private:
43     UnownedPtr<IJS_Runtime> const m_pRuntime;
44     UnownedPtr<IJS_EventContext> m_pContext;
45   };
46 
47   static void Initialize(unsigned int slot, void* isolate);
48   static void Destroy();
49   static std::unique_ptr<IJS_Runtime> Create(
50       CPDFSDK_FormFillEnvironment* pFormFillEnv);
51 
52   virtual ~IJS_Runtime();
53 
54   virtual CJS_Runtime* AsCJSRuntime() = 0;
55   virtual IJS_EventContext* NewEventContext() = 0;
56   virtual void ReleaseEventContext(IJS_EventContext* pContext) = 0;
57   virtual CPDFSDK_FormFillEnvironment* GetFormFillEnv() const = 0;
58   virtual Optional<JS_Error> ExecuteScript(const WideString& script) = 0;
59 
60  protected:
61   IJS_Runtime() = default;
62 };
63 
64 #endif  // FXJS_IJS_RUNTIME_H_
65