blob: 33d2956ce3d973e465876ae7add68d2276acbff5 [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"
dsinclairb3f24672016-07-12 10:42:14 -070019#include "fxjs/include/fxjs_v8.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070020
21class CJS_Context;
22
tsepeza4941912016-08-15 11:40:12 -070023class CJS_Runtime : public IJS_Runtime, public CFXJS_Engine {
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);
tsepezb4694242016-08-15 16:44:55 -070036 static CJS_Runtime* CurrentRuntimeFromIsolate(v8::Isolate* pIsolate);
Tom Sepez67fd5df2015-10-08 12:24:19 -070037
Tom Sepez7d0fcbf2015-09-15 15:30:34 -070038 explicit CJS_Runtime(CPDFDoc_Environment* pApp);
Lei Zhang2b1a2d52015-08-14 22:16:22 -070039 ~CJS_Runtime() override;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040
Tom Sepezba038bc2015-10-08 12:03:00 -070041 // IJS_Runtime
42 IJS_Context* NewContext() override;
43 void ReleaseContext(IJS_Context* pContext) override;
44 IJS_Context* GetCurrentContext() override;
Lei Zhang2b1a2d52015-08-14 22:16:22 -070045 void SetReaderDocument(CPDFSDK_Document* pReaderDoc) override;
weili625ad662016-06-15 11:21:33 -070046 CPDFSDK_Document* GetReaderDocument() override;
tsepezb4694242016-08-15 16:44:55 -070047 int ExecuteScript(const CFX_WideString& 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 Sepez51da0932015-11-25 16:05:49 -080060#ifdef PDF_ENABLE_XFA
dsinclair12a6b0c2016-05-26 11:14:08 -070061 FX_BOOL GetValueByName(const CFX_ByteStringC& utf8Name,
62 CFXJSE_Value* pValue) override;
63 FX_BOOL SetValueByName(const CFX_ByteStringC& utf8Name,
64 CFXJSE_Value* pValue) override;
Tom Sepez40e9ff32015-11-30 12:39:54 -080065#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -070066
Lei Zhang2d5a0e12015-10-05 17:00:03 -070067 void AddObserver(Observer* observer);
68 void RemoveObserver(Observer* observer);
69
Tom Sepez570875c2015-09-11 08:35:03 -070070 private:
Tom Sepez142165e2015-09-11 13:21:50 -070071 void DefineJSObjects();
Tom Sepez570875c2015-09-11 08:35:03 -070072
tsepez41a53ad2016-03-28 16:59:30 -070073 std::vector<std::unique_ptr<CJS_Context>> m_ContextArray;
74 CPDFDoc_Environment* const m_pApp;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 CPDFSDK_Document* m_pDocument;
tsepeza4941912016-08-15 11:40:12 -070076 bool m_bBlocking;
Jochen Eisinger29007842015-08-05 09:02:13 +020077 bool m_isolateManaged;
tsepeza4941912016-08-15 11:40:12 -070078 std::set<FieldEvent> m_FieldEventSet;
Lei Zhang2d5a0e12015-10-05 17:00:03 -070079 std::set<Observer*> m_observers;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080};
81
dsinclair64376be2016-03-31 20:03:24 -070082#endif // FPDFSDK_JAVASCRIPT_CJS_RUNTIME_H_