• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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 FPDFSDK_JAVASCRIPT_CJS_RUNTIME_H_
8 #define FPDFSDK_JAVASCRIPT_CJS_RUNTIME_H_
9 
10 #include <map>
11 #include <memory>
12 #include <set>
13 #include <utility>
14 #include <vector>
15 
16 #include "core/fxcrt/cfx_observable.h"
17 #include "core/fxcrt/fx_basic.h"
18 #include "fpdfsdk/cpdfsdk_formfillenvironment.h"
19 #include "fpdfsdk/javascript/JS_EventHandler.h"
20 #include "fpdfsdk/javascript/ijs_runtime.h"
21 #include "fxjs/fxjs_v8.h"
22 
23 class CJS_EventContext;
24 
25 class CJS_Runtime : public IJS_Runtime,
26                     public CFXJS_Engine,
27                     public CFX_Observable<CJS_Runtime> {
28  public:
29   using FieldEvent = std::pair<CFX_WideString, JS_EVENT_T>;
30 
31   static CJS_Runtime* CurrentRuntimeFromIsolate(v8::Isolate* pIsolate);
32 
33   explicit CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv);
34   ~CJS_Runtime() override;
35 
36   // IJS_Runtime
37   IJS_EventContext* NewEventContext() override;
38   void ReleaseEventContext(IJS_EventContext* pContext) override;
39   CPDFSDK_FormFillEnvironment* GetFormFillEnv() const override;
40   int ExecuteScript(const CFX_WideString& script,
41                     CFX_WideString* info) override;
42 
43   CJS_EventContext* GetCurrentEventContext() const;
44 
45   // Returns true if the event isn't already found in the set.
46   bool AddEventToSet(const FieldEvent& event);
47   void RemoveEventFromSet(const FieldEvent& event);
48 
BeginBlock()49   void BeginBlock() { m_bBlocking = true; }
EndBlock()50   void EndBlock() { m_bBlocking = false; }
IsBlocking()51   bool IsBlocking() const { return m_bBlocking; }
52 
53 #ifdef PDF_ENABLE_XFA
54   bool GetValueByName(const CFX_ByteStringC& utf8Name,
55                       CFXJSE_Value* pValue) override;
56   bool SetValueByName(const CFX_ByteStringC& utf8Name,
57                       CFXJSE_Value* pValue) override;
58 #endif  // PDF_ENABLE_XFA
59 
60  private:
61   void DefineJSObjects();
62   void SetFormFillEnvToDocument();
63 
64   std::vector<std::unique_ptr<CJS_EventContext>> m_EventContextArray;
65   CPDFSDK_FormFillEnvironment::ObservedPtr m_pFormFillEnv;
66   bool m_bBlocking;
67   bool m_isolateManaged;
68   std::set<FieldEvent> m_FieldEventSet;
69 };
70 
71 #endif  // FPDFSDK_JAVASCRIPT_CJS_RUNTIME_H_
72