blob: cc0f6545ac4c282309f97b9302b8cab9e2093349 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
dsinclair64376be2016-03-31 20:03:24 -07007#ifndef FPDFSDK_JAVASCRIPT_CJS_RUNTIME_H_
8#define FPDFSDK_JAVASCRIPT_CJS_RUNTIME_H_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009
Dan Sinclair3ebd1212016-03-09 09:59:23 -050010#include <map>
tsepez41a53ad2016-03-28 16:59:30 -070011#include <memory>
Dan Sinclairf766ad22016-03-14 13:51:24 -040012#include <set>
Tom Sepez5d0e8432015-09-22 15:50:03 -070013#include <utility>
Tom Sepez4237aed2015-11-10 15:19:17 -080014#include <vector>
Tom Sepez5d0e8432015-09-22 15:50:03 -070015
Dan Sinclaira8a28e02016-03-23 15:41:39 -040016#include "core/fxcrt/include/fx_basic.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040017#include "fpdfsdk/javascript/JS_EventHandler.h"
dsinclair64376be2016-03-31 20:03:24 -070018#include "fpdfsdk/javascript/ijs_runtime.h"
dsinclair89bdd082016-04-06 10:47:54 -070019#include "fpdfsdk/jsapi/include/fxjs_v8.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070020
21class CJS_Context;
22
Tom Sepezba038bc2015-10-08 12:03:00 -070023class CJS_Runtime : public IJS_Runtime {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070024 public:
Lei Zhang2d5a0e12015-10-05 17:00:03 -070025 class Observer {
26 public:
27 virtual void OnDestroyed() = 0;
28
29 protected:
30 virtual ~Observer() {}
31 };
32
Tom Sepez5d0e8432015-09-22 15:50:03 -070033 using FieldEvent = std::pair<CFX_WideString, JS_EVENT_T>;
34
Tom Sepez67fd5df2015-10-08 12:24:19 -070035 static CJS_Runtime* FromContext(const IJS_Context* cc);
36
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070037 explicit CJS_Runtime(CPDFDoc_Environment* pApp);
Lei Zhang2b1a2d52015-08-14 22:16:22 -070038 ~CJS_Runtime() override;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070039
Tom Sepezba038bc2015-10-08 12:03:00 -070040 // IJS_Runtime
41 IJS_Context* NewContext() override;
42 void ReleaseContext(IJS_Context* pContext) override;
43 IJS_Context* GetCurrentContext() override;
Lei Zhang2b1a2d52015-08-14 22:16:22 -070044 void SetReaderDocument(CPDFSDK_Document* pReaderDoc) override;
45 CPDFSDK_Document* GetReaderDocument() override { return m_pDocument; }
Tom Sepez33420902015-10-13 15:00:10 -070046 int Execute(IJS_Context* cc,
47 const wchar_t* script,
48 CFX_WideString* info) override;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070049
Tom Sepez808a99e2015-09-10 12:28:37 -070050 CPDFDoc_Environment* GetReaderApp() const { return m_pApp; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070051
Tom Sepez5d0e8432015-09-22 15:50:03 -070052 // Returns true if the event isn't already found in the set.
53 bool AddEventToSet(const FieldEvent& event);
54 void RemoveEventFromSet(const FieldEvent& event);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 void BeginBlock() { m_bBlocking = TRUE; }
57 void EndBlock() { m_bBlocking = FALSE; }
Tom Sepez808a99e2015-09-10 12:28:37 -070058 FX_BOOL IsBlocking() const { return m_bBlocking; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070059
Tom Sepez808a99e2015-09-10 12:28:37 -070060 v8::Isolate* GetIsolate() const { return m_isolate; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 v8::Local<v8::Context> NewJSContext();
Bo Xufdc00a72014-10-28 23:03:33 -070062
Tom Sepez297b5152016-03-04 13:43:46 -080063 void SetConstArray(const CFX_WideString& name, v8::Local<v8::Array> array);
64 v8::Local<v8::Array> GetConstArray(const CFX_WideString& name);
65
Tom Sepez51da0932015-11-25 16:05:49 -080066#ifdef PDF_ENABLE_XFA
dsinclair12a6b0c2016-05-26 11:14:08 -070067 FX_BOOL GetValueByName(const CFX_ByteStringC& utf8Name,
68 CFXJSE_Value* pValue) override;
69 FX_BOOL SetValueByName(const CFX_ByteStringC& utf8Name,
70 CFXJSE_Value* pValue) override;
Tom Sepez40e9ff32015-11-30 12:39:54 -080071#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -070072
Lei Zhang2d5a0e12015-10-05 17:00:03 -070073 void AddObserver(Observer* observer);
74 void RemoveObserver(Observer* observer);
75
Tom Sepez570875c2015-09-11 08:35:03 -070076 private:
Tom Sepez142165e2015-09-11 13:21:50 -070077 void DefineJSObjects();
Tom Sepez570875c2015-09-11 08:35:03 -070078
tsepez41a53ad2016-03-28 16:59:30 -070079 std::vector<std::unique_ptr<CJS_Context>> m_ContextArray;
80 CPDFDoc_Environment* const m_pApp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070081 CPDFSDK_Document* m_pDocument;
82 FX_BOOL m_bBlocking;
Tom Sepez5d0e8432015-09-22 15:50:03 -070083 std::set<FieldEvent> m_FieldEventSet;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 v8::Isolate* m_isolate;
Jochen Eisinger29007842015-08-05 09:02:13 +020085 bool m_isolateManaged;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 v8::Global<v8::Context> m_context;
Tom Sepez4237aed2015-11-10 15:19:17 -080087 std::vector<v8::Global<v8::Object>*> m_StaticObjects;
Tom Sepez297b5152016-03-04 13:43:46 -080088 std::map<CFX_WideString, v8::Global<v8::Array>> m_ConstArrays;
Lei Zhang2d5a0e12015-10-05 17:00:03 -070089 std::set<Observer*> m_observers;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090};
91
dsinclair64376be2016-03-31 20:03:24 -070092#endif // FPDFSDK_JAVASCRIPT_CJS_RUNTIME_H_