• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CFXJSE_Value;
18 class CJS_Runtime;
19 class CPDFSDK_FormFillEnvironment;
20 class IJS_EventContext;
21 
22 // Owns the FJXS objects needed to actually execute JS, if possible. This
23 // virtual interface is backed by either an actual JS runtime, or a stub,
24 // when JS is not present.
25 class IJS_Runtime {
26  public:
27   struct JS_Error {
28     int line;
29     int column;
30     WideString exception;
31 
32     JS_Error(int line, int column, const WideString& exception);
33   };
34 
35   class ScopedEventContext {
36    public:
37     explicit ScopedEventContext(IJS_Runtime* pRuntime);
38     ~ScopedEventContext();
39 
Get()40     IJS_EventContext* Get() const { return m_pContext.Get(); }
41     IJS_EventContext* operator->() const { return m_pContext.Get(); }
42 
43    private:
44     UnownedPtr<IJS_Runtime> const m_pRuntime;
45     UnownedPtr<IJS_EventContext> m_pContext;
46   };
47 
48   static void Initialize(unsigned int slot, void* isolate);
49   static void Destroy();
50   static std::unique_ptr<IJS_Runtime> Create(
51       CPDFSDK_FormFillEnvironment* pFormFillEnv);
52 
53   virtual ~IJS_Runtime();
54 
55   virtual CJS_Runtime* AsCJSRuntime() = 0;
56   virtual IJS_EventContext* NewEventContext() = 0;
57   virtual void ReleaseEventContext(IJS_EventContext* pContext) = 0;
58   virtual CPDFSDK_FormFillEnvironment* GetFormFillEnv() const = 0;
59   virtual Optional<JS_Error> ExecuteScript(const WideString& script) = 0;
60 
61  protected:
62   IJS_Runtime() = default;
63 };
64 
65 #endif  // FXJS_IJS_RUNTIME_H_
66