blob: 3ad2e0fed5ce6ed63a3d57967c0f576117dd856e [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
dsinclaira52ab742016-09-29 13:59:29 -070016#include "core/fxcrt/cfx_observable.h"
17#include "core/fxcrt/fx_basic.h"
Dan Sinclairf766ad22016-03-14 13:51:24 -040018#include "fpdfsdk/javascript/JS_EventHandler.h"
dsinclair64376be2016-03-31 20:03:24 -070019#include "fpdfsdk/javascript/ijs_runtime.h"
dsinclair43554682016-09-29 17:29:48 -070020#include "fxjs/fxjs_v8.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070021
22class CJS_Context;
23
tsepez8832fbf2016-09-08 10:25:55 -070024class CJS_Runtime : public IJS_Runtime,
25 public CFXJS_Engine,
26 public CFX_Observable<CJS_Runtime> {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027 public:
Tom Sepez5d0e8432015-09-22 15:50:03 -070028 using FieldEvent = std::pair<CFX_WideString, JS_EVENT_T>;
29
Tom Sepez67fd5df2015-10-08 12:24:19 -070030 static CJS_Runtime* FromContext(const IJS_Context* cc);
tsepezb4694242016-08-15 16:44:55 -070031 static CJS_Runtime* CurrentRuntimeFromIsolate(v8::Isolate* pIsolate);
Tom Sepez67fd5df2015-10-08 12:24:19 -070032
dsinclair79db6092016-09-14 07:27:21 -070033 explicit CJS_Runtime(CPDFSDK_Environment* pApp);
Lei Zhang2b1a2d52015-08-14 22:16:22 -070034 ~CJS_Runtime() override;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070035
Tom Sepezba038bc2015-10-08 12:03:00 -070036 // IJS_Runtime
37 IJS_Context* NewContext() override;
38 void ReleaseContext(IJS_Context* pContext) override;
39 IJS_Context* GetCurrentContext() override;
Lei Zhang2b1a2d52015-08-14 22:16:22 -070040 void SetReaderDocument(CPDFSDK_Document* pReaderDoc) override;
weili625ad662016-06-15 11:21:33 -070041 CPDFSDK_Document* GetReaderDocument() override;
tsepezb4694242016-08-15 16:44:55 -070042 int ExecuteScript(const CFX_WideString& script,
43 CFX_WideString* info) override;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
dsinclair8e0638b2016-09-22 11:06:02 -070045 CPDFSDK_Environment* GetReaderEnv() const { return m_pEnv; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046
Tom Sepez5d0e8432015-09-22 15:50:03 -070047 // Returns true if the event isn't already found in the set.
48 bool AddEventToSet(const FieldEvent& event);
49 void RemoveEventFromSet(const FieldEvent& event);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050
Nico Weber9d8ec5a2015-08-04 13:00:21 -070051 void BeginBlock() { m_bBlocking = TRUE; }
52 void EndBlock() { m_bBlocking = FALSE; }
Tom Sepez808a99e2015-09-10 12:28:37 -070053 FX_BOOL IsBlocking() const { return m_bBlocking; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070054
Tom Sepez51da0932015-11-25 16:05:49 -080055#ifdef PDF_ENABLE_XFA
dsinclair12a6b0c2016-05-26 11:14:08 -070056 FX_BOOL GetValueByName(const CFX_ByteStringC& utf8Name,
57 CFXJSE_Value* pValue) override;
58 FX_BOOL SetValueByName(const CFX_ByteStringC& utf8Name,
59 CFXJSE_Value* pValue) override;
Tom Sepez40e9ff32015-11-30 12:39:54 -080060#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -070061
Tom Sepez570875c2015-09-11 08:35:03 -070062 private:
Tom Sepez142165e2015-09-11 13:21:50 -070063 void DefineJSObjects();
Tom Sepez570875c2015-09-11 08:35:03 -070064
tsepez41a53ad2016-03-28 16:59:30 -070065 std::vector<std::unique_ptr<CJS_Context>> m_ContextArray;
dsinclair8e0638b2016-09-22 11:06:02 -070066 CPDFSDK_Environment* const m_pEnv;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070067 CPDFSDK_Document* m_pDocument;
tsepeza4941912016-08-15 11:40:12 -070068 bool m_bBlocking;
Jochen Eisinger29007842015-08-05 09:02:13 +020069 bool m_isolateManaged;
tsepeza4941912016-08-15 11:40:12 -070070 std::set<FieldEvent> m_FieldEventSet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070071};
72
dsinclair64376be2016-03-31 20:03:24 -070073#endif // FPDFSDK_JAVASCRIPT_CJS_RUNTIME_H_