blob: 0d6951f8c098ce9af8bf05182d64eb2692874103 [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 Sinclair65ecca42017-09-21 15:25:32 -040016#include "core/fxcrt/observable.h"
Tom Sepez77f6d0f2017-02-23 12:14:10 -080017#include "fpdfsdk/cpdfsdk_formfillenvironment.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
Tom Sepezd6ae2af2017-02-16 11:49:55 -080022class CJS_EventContext;
Tom Sepez9a3f8122015-04-07 15:35:48 -070023
tsepez8832fbf2016-09-08 10:25:55 -070024class CJS_Runtime : public IJS_Runtime,
25 public CFXJS_Engine,
Dan Sinclair65ecca42017-09-21 15:25:32 -040026 public Observable<CJS_Runtime> {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027 public:
Ryan Harrison275e2602017-09-18 14:23:18 -040028 using FieldEvent = std::pair<WideString, JS_EVENT_T>;
Tom Sepez5d0e8432015-09-22 15:50:03 -070029
tsepezb4694242016-08-15 16:44:55 -070030 static CJS_Runtime* CurrentRuntimeFromIsolate(v8::Isolate* pIsolate);
Tom Sepez67fd5df2015-10-08 12:24:19 -070031
dsinclair82e17672016-10-11 12:38:01 -070032 explicit CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv);
Lei Zhang2b1a2d52015-08-14 22:16:22 -070033 ~CJS_Runtime() override;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070034
Tom Sepezba038bc2015-10-08 12:03:00 -070035 // IJS_Runtime
Tom Sepezd6ae2af2017-02-16 11:49:55 -080036 IJS_EventContext* NewEventContext() override;
37 void ReleaseEventContext(IJS_EventContext* pContext) override;
dsinclair82e17672016-10-11 12:38:01 -070038 CPDFSDK_FormFillEnvironment* GetFormFillEnv() const override;
Ryan Harrison275e2602017-09-18 14:23:18 -040039 int ExecuteScript(const WideString& script, WideString* info) override;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040
Tom Sepezb1670b52017-02-16 17:01:00 -080041 CJS_EventContext* GetCurrentEventContext() const;
42
Tom Sepez5d0e8432015-09-22 15:50:03 -070043 // Returns true if the event isn't already found in the set.
44 bool AddEventToSet(const FieldEvent& event);
45 void RemoveEventFromSet(const FieldEvent& event);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070046
tsepez4cf55152016-11-02 14:37:54 -070047 void BeginBlock() { m_bBlocking = true; }
48 void EndBlock() { m_bBlocking = false; }
49 bool IsBlocking() const { return m_bBlocking; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050
Tom Sepez51da0932015-11-25 16:05:49 -080051#ifdef PDF_ENABLE_XFA
Ryan Harrison275e2602017-09-18 14:23:18 -040052 bool GetValueByName(const ByteStringView& utf8Name,
tsepez4cf55152016-11-02 14:37:54 -070053 CFXJSE_Value* pValue) override;
Ryan Harrison275e2602017-09-18 14:23:18 -040054 bool SetValueByName(const ByteStringView& utf8Name,
tsepez4cf55152016-11-02 14:37:54 -070055 CFXJSE_Value* pValue) override;
Tom Sepez40e9ff32015-11-30 12:39:54 -080056#endif // PDF_ENABLE_XFA
Bo Xufdc00a72014-10-28 23:03:33 -070057
Tom Sepez570875c2015-09-11 08:35:03 -070058 private:
Tom Sepez142165e2015-09-11 13:21:50 -070059 void DefineJSObjects();
dsinclair82e17672016-10-11 12:38:01 -070060 void SetFormFillEnvToDocument();
Tom Sepez570875c2015-09-11 08:35:03 -070061
Tom Sepezd6ae2af2017-02-16 11:49:55 -080062 std::vector<std::unique_ptr<CJS_EventContext>> m_EventContextArray;
Tom Sepez77f6d0f2017-02-23 12:14:10 -080063 CPDFSDK_FormFillEnvironment::ObservedPtr m_pFormFillEnv;
tsepeza4941912016-08-15 11:40:12 -070064 bool m_bBlocking;
Jochen Eisinger29007842015-08-05 09:02:13 +020065 bool m_isolateManaged;
tsepeza4941912016-08-15 11:40:12 -070066 std::set<FieldEvent> m_FieldEventSet;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067};
68
dsinclair64376be2016-03-31 20:03:24 -070069#endif // FPDFSDK_JAVASCRIPT_CJS_RUNTIME_H_